mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@gmail.com>
To: "Gary Guo" <gary@garyguo.net>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/8] rust: device: support `dev_printk` on all devices
Date: Sat, 24 Jan 2026 07:31:00 +0100	[thread overview]
Message-ID: <cdc52b14-bf2f-4189-8594-25c25d9f78c9@gmail.com> (raw)
In-Reply-To: <20260123175854.176735-1-gary@kernel.org>

On 23.01.26 18:58, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
> 
> Currently, `dev_*` only works on the core `Device`, but not on any other
> bus or class device objects. This causes a pattern of
> `dev_info!(pdev.as_ref())` which is not ideal.
> 
> This adds support of using these devices directly with `dev_*` macros, by
> adding `AsRef` call inside the macro. To make sure we can still use just
> `kernel::device::Device`, as `AsRef` implementation is added for it; this
> is typical for types that is designed to use with `AsRef` anyway, for
> example, `str` implements `AsRef<str>` and `Path` implements `AsRef<Path>`.
> 
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> 
> Notes:
>     v1 -> v2:
>     - split change to samples/rust to subsystems
>     - converted more cases where `as_ref()` is only for dev_printk


Many thanks for adding the additional cases!

With the discussion in [1], I think the rule of thumb is now:

* If `&Device` is only needed specifically for printing, then no
`.as_ref()` and no helper variable are needed any more (as done in
this patch series).
* If you already need to obtain `&Device` not only for printing, but
also for other use cases, then a helper variable like `let dev =
pdev.as_ref();` is fine. An example is `probe()` in
samples/rust/rust_driver_platform.rs [2].

I think we have a trivial warning from the kernel test robot on the
USB one, now. But with that fixed (if its not too late already):

Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>

Thanks,

Dirk

[1]
https://lore.kernel.org/rust-for-linux/DFW5LUIQVZK0.2W01YO54KTKR7@garyguo.net/

[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/rust/rust_driver_platform.rs#n103


>     - Link to v1: https://lore.kernel.org/rust-for-linux/20260120181152.3640314-1-gary@kernel.org/
> 
>  rust/kernel/device.rs | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index c7b5db9dcca1..94e0548e7687 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -601,6 +601,13 @@ impl DeviceContext for Core {}
>  impl DeviceContext for CoreInternal {}
>  impl DeviceContext for Normal {}
>  
> +impl<Ctx: DeviceContext> AsRef<Device<Ctx>> for Device<Ctx> {
> +    #[inline]
> +    fn as_ref(&self) -> &Device<Ctx> {
> +        self
> +    }
> +}
> +
>  /// Convert device references to bus device references.
>  ///
>  /// Bus devices can implement this trait to allow abstractions to provide the bus device in
> @@ -720,7 +727,7 @@ macro_rules! impl_device_context_into_aref {
>  macro_rules! dev_printk {
>      ($method:ident, $dev:expr, $($f:tt)*) => {
>          {
> -            ($dev).$method($crate::prelude::fmt!($($f)*));
> +            $crate::device::Device::$method($dev.as_ref(), $crate::prelude::fmt!($($f)*))
>          }
>      }
>  }
> 
> base-commit: a0c666c25aeefd16f4b088c6549a6fb6b65a8a1d


      parent reply	other threads:[~2026-01-24  6:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-23 17:58 Gary Guo
2026-01-23 17:58 ` [PATCH v2 2/8] rust: pci: remove redundant `.as_ref()` for `dev_*` print Gary Guo
2026-01-24  0:38   ` Danilo Krummrich
2026-01-23 17:58 ` [PATCH v2 3/8] rust: samples: driver-core: " Gary Guo
2026-01-24  0:38   ` Danilo Krummrich
2026-01-23 17:58 ` [PATCH v2 4/8] rust: samples: dma: " Gary Guo
2026-01-24  0:39   ` Danilo Krummrich
2026-01-23 17:58 ` [PATCH v2 5/8] rust: samples: i2c: " Gary Guo
2026-01-27 18:52   ` Igor Korotin
2026-01-23 17:58 ` [PATCH v2 6/8] rust: samples: usb: " Gary Guo
2026-01-24  5:36   ` kernel test robot
2026-01-23 17:58 ` [PATCH v2 7/8] gpu: nova-core: " Gary Guo
2026-01-24 13:07   ` Alexandre Courbot
2026-01-24 13:29     ` Danilo Krummrich
2026-02-13  0:11       ` Alexandre Courbot
2026-02-24 14:17   ` Danilo Krummrich
2026-02-27 19:30   ` Aditya Rajan
2026-01-23 17:58 ` [PATCH v2 8/8] gpu: tyr: " Gary Guo
2026-01-24  0:37 ` [PATCH v2 1/8] rust: device: support `dev_printk` on all devices Danilo Krummrich
2026-01-24  6:31 ` Dirk Behme [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cdc52b14-bf2f-4189-8594-25c25d9f78c9@gmail.com \
    --to=dirk.behme@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®