lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <cdc52b14-bf2f-4189-8594-25c25d9f78c9@gmail.com>
Date: Sat, 24 Jan 2026 07:31:00 +0100
From: Dirk Behme <dirk.behme@...il.com>
To: Gary Guo <gary@...yguo.net>,
 Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
 "Rafael J. Wysocki" <rafael@...nel.org>, Danilo Krummrich <dakr@...nel.org>,
 Miguel Ojeda <ojeda@...nel.org>, Boqun Feng <boqun.feng@...il.com>,
 Björn Roy Baron <bjorn3_gh@...tonmail.com>,
 Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
 Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 1/8] rust: device: support `dev_printk` on all devices

On 23.01.26 18:58, Gary Guo wrote:
> From: Gary Guo <gary@...yguo.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@...yguo.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@...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


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ