From: Gary Guo <gary@kernel.org>
To: "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>,
"Gary Guo" <gary@garyguo.net>,
"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>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-pci@vger.kernel.org
Subject: [PATCH 1/4] rust: device: support `dev_printk` on all devices
Date: Tue, 20 Jan 2026 18:11:06 +0000 [thread overview]
Message-ID: <20260120181152.3640314-1-gary@kernel.org> (raw)
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>
---
rust/kernel/device.rs | 9 ++++++++-
rust/kernel/pci.rs | 2 +-
rust/kernel/pci/id.rs | 2 +-
3 files changed, 10 insertions(+), 3 deletions(-)
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)*))
}
}
}
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index bea76ca9c3da..3946ca919332 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -351,7 +351,7 @@ impl Device {
/// // Get an instance of `Vendor`.
/// let vendor = pdev.vendor_id();
/// dev_info!(
- /// pdev.as_ref(),
+ /// pdev,
/// "Device: Vendor={}, Device=0x{:x}\n",
/// vendor,
/// pdev.device_id()
diff --git a/rust/kernel/pci/id.rs b/rust/kernel/pci/id.rs
index c09125946d9e..e2d9e8804347 100644
--- a/rust/kernel/pci/id.rs
+++ b/rust/kernel/pci/id.rs
@@ -22,7 +22,7 @@
/// fn probe_device(pdev: &pci::Device<Core>) -> Result {
/// let pci_class = pdev.pci_class();
/// dev_info!(
-/// pdev.as_ref(),
+/// pdev,
/// "Detected PCI class: {}\n",
/// pci_class
/// );
base-commit: 053966c344dbd346e71305f530e91ea77916189f
--
2.51.2
next reply other threads:[~2026-01-20 18:12 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-20 18:11 Gary Guo [this message]
2026-01-20 18:11 ` [PATCH 2/4] rust: samples: remove redundant `.as_ref()` for `dev_*` print Gary Guo
2026-01-21 6:52 ` Dirk Behme
2026-01-23 17:44 ` Gary Guo
2026-01-27 18:51 ` Igor Korotin
2026-01-20 18:11 ` [PATCH 3/4] gpu: nova-core: " Gary Guo
2026-01-20 18:11 ` Gary Guo
2026-01-20 18:11 ` Gary Guo
2026-01-20 18:11 ` [PATCH 4/4] gpu: tyr: " Gary Guo
2026-01-21 12:25 ` Daniel Almeida
2026-01-26 9:17 ` Alice Ryhl
2026-01-26 13:43 ` Danilo Krummrich
2026-01-26 15:06 ` Alice Ryhl
2026-01-26 17:02 ` Danilo Krummrich
2026-01-20 18:56 ` [PATCH 1/4] rust: device: support `dev_printk` on all devices Danilo Krummrich
2026-01-21 13:46 ` Gary Guo
2026-01-21 14:25 ` Danilo Krummrich
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=20260120181152.3640314-1-gary@kernel.org \
--to=gary@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bhelgaas@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=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.