mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matteo Kloiber <kernel@matt3o12.de>
To: Danilo Krummrich <dakr@kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>,
	Alice Ryhl <aliceryhl@google.com>,
	Abdiel Janulgue <abdiel.janulgue@gmail.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Gary Guo <gary@garyguo.net>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
	dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	"Matteo Kloiber" <kernel@matt3o12.de>
Subject: [RFC PATCH 1/3] rust: pci, platform: pass InitTokens to probe()
Date: Mon, 23 Nov 2026 21:39:24 +0100	[thread overview]
Message-ID: <20261123203926.1787169-2-kernel@matt3o12.de> (raw)
In-Reply-To: <20261123203926.1787169-1-kernel@matt3o12.de>

Some capabilities should only be available to a driver while it binds
to a device, for example configuring the device for DMA before any
memory is allocated or mapped. A probe-time token lets the type system
enforce such ordering, but adding a new argument to probe() for every
token would touch every driver each time.

Add an InitTokens struct to the PCI and platform buses and pass it to
Driver::probe(). Buses add new tokens as fields of this struct. It is
marked #[non_exhaustive], so drivers destructure it with `..` and
adding tokens later does not break them.

The struct carries no tokens yet. Update all drivers, samples and
doctests to accept and ignore it.

Suggested-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/DLR1JCUWS824.35QJU45D2W9ZE@nvidia.com
Signed-off-by: Matteo Kloiber <kernel@matt3o12.de>
---
 drivers/cpufreq/rcpufreq_dt.rs        |  1 +
 drivers/gpu/drm/tyr/driver.rs         |  1 +
 drivers/gpu/nova-core/driver.rs       |  1 +
 drivers/pwm/pwm_th1520.rs             |  1 +
 rust/kernel/cpufreq.rs                |  1 +
 rust/kernel/io/mem.rs                 |  2 ++
 rust/kernel/pci.rs                    | 10 +++++++++-
 rust/kernel/platform.rs               | 10 +++++++++-
 samples/rust/rust_debugfs.rs          |  1 +
 samples/rust/rust_dma.rs              |  1 +
 samples/rust/rust_driver_auxiliary.rs |  1 +
 samples/rust/rust_driver_pci.rs       |  1 +
 samples/rust/rust_driver_platform.rs  |  1 +
 samples/rust/rust_i2c_client.rs       |  1 +
 samples/rust/rust_soc.rs              |  1 +
 15 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/rcpufreq_dt.rs b/drivers/cpufreq/rcpufreq_dt.rs
index 2b1f18b7a1e7..a6e314f4d809 100644
--- a/drivers/cpufreq/rcpufreq_dt.rs
+++ b/drivers/cpufreq/rcpufreq_dt.rs
@@ -212,6 +212,7 @@ impl platform::Driver for CPUFreqDTDriver {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         _id_info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         cpufreq::Registration::<CPUFreqDTDriver>::new_foreign_owned(pdev.as_ref())?;
diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs
index 557f2ec0a767..7650ade4ad72 100644
--- a/drivers/gpu/drm/tyr/driver.rs
+++ b/drivers/gpu/drm/tyr/driver.rs
@@ -119,6 +119,7 @@ impl platform::Driver for TyrPlatformDriver {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
         pin_init::pin_init_scope(move || {
diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs
index d6bc972d47c7..e42987e35846 100644
--- a/drivers/gpu/nova-core/driver.rs
+++ b/drivers/gpu/nova-core/driver.rs
@@ -100,6 +100,7 @@ impl pci::Driver for NovaCoreDriver {
 
     fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
+        _tokens: pci::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
         dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
diff --git a/drivers/pwm/pwm_th1520.rs b/drivers/pwm/pwm_th1520.rs
index 728993998446..2486333c4a91 100644
--- a/drivers/pwm/pwm_th1520.rs
+++ b/drivers/pwm/pwm_th1520.rs
@@ -316,6 +316,7 @@ impl platform::Driver for Th1520PwmPlatformDriver {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         _id_info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         let dev = pdev.as_ref();
diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
index b75d692846a2..0032264fe72c 100644
--- a/rust/kernel/cpufreq.rs
+++ b/rust/kernel/cpufreq.rs
@@ -916,6 +916,7 @@ fn register_em(_policy: &mut Policy) {
 ///
 ///     fn probe<'bound>(
 ///         pdev: &'bound platform::Device<Core<'_>>,
+///         _tokens: platform::InitTokens<'bound>,
 ///         _id_info: Option<&'bound Self::IdInfo>,
 ///     ) -> impl PinInit<Self, Error> + 'bound {
 ///         cpufreq::Registration::<SampleDriver>::new_foreign_owned(pdev.as_ref())?;
diff --git a/rust/kernel/io/mem.rs b/rust/kernel/io/mem.rs
index 32a919099dcd..50dedd32d1a7 100644
--- a/rust/kernel/io/mem.rs
+++ b/rust/kernel/io/mem.rs
@@ -70,6 +70,7 @@ pub(crate) unsafe fn new(device: &'a Device<Bound>, resource: &'a Resource) -> S
     ///
     ///    fn probe<'bound>(
     ///       pdev: &'bound platform::Device<Core<'_>>,
+    ///       _tokens: platform::InitTokens<'bound>,
     ///       info: Option<&'bound Self::IdInfo>,
     ///    ) -> impl PinInit<Self, Error> + 'bound {
     ///       let offset = 0; // Some offset.
@@ -130,6 +131,7 @@ pub fn iomap_exclusive_sized<const SIZE: usize>(self) -> Result<ExclusiveIoMem<'
     ///
     ///    fn probe<'bound>(
     ///       pdev: &'bound platform::Device<Core<'_>>,
+    ///       _tokens: platform::InitTokens<'bound>,
     ///       info: Option<&'bound Self::IdInfo>,
     ///    ) -> impl PinInit<Self, Error> + 'bound {
     ///       let offset = 0; // Some offset.
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 769d01d23429..fdb2934eda0d 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -128,7 +128,7 @@ extern "C" fn probe_callback(
         let info = unsafe { id.info_unchecked_opt::<T::IdInfo>() };
 
         from_result(|| {
-            let data = T::probe(pdev, info);
+            let data = T::probe(pdev, InitTokens { _p: PhantomData }, info);
 
             pdev.as_ref().set_drvdata(data)?;
             Ok(0)
@@ -261,6 +261,12 @@ macro_rules! pci_device_table {
     };
 }
 
+/// Initialization tokens passed to a PCI driver's [`Driver::probe`] callback.
+#[non_exhaustive]
+pub struct InitTokens<'bound> {
+    _p: PhantomData<&'bound ()>,
+}
+
 /// The PCI driver trait.
 ///
 /// # Examples
@@ -288,6 +294,7 @@ macro_rules! pci_device_table {
 ///
 ///     fn probe<'bound>(
 ///         _pdev: &'bound pci::Device<Core<'_>>,
+///         _tokens: pci::InitTokens<'bound>,
 ///         _id_info: Option<&'bound Self::IdInfo>,
 ///     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
 ///         Err(ENODEV)
@@ -317,6 +324,7 @@ pub trait Driver {
     /// attempt to initialize the device here.
     fn probe<'bound>(
         dev: &'bound Device<device::Core<'_>>,
+        tokens: InitTokens<'bound>,
         id_info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
 
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index ac0a012ae1bb..a7b33e229a30 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -104,7 +104,7 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff
         let info = unsafe { <Self as driver::Adapter>::id_info(pdev.as_ref()) };
 
         from_result(|| {
-            let data = T::probe(pdev, info);
+            let data = T::probe(pdev, InitTokens { _p: PhantomData }, info);
 
             pdev.as_ref().set_drvdata(data)?;
             Ok(0)
@@ -159,6 +159,12 @@ macro_rules! module_platform_driver {
     };
 }
 
+/// Initialization tokens passed to a platform driver's [`Driver::probe`] callback.
+#[non_exhaustive]
+pub struct InitTokens<'bound> {
+    _p: PhantomData<&'bound ()>,
+}
+
 /// The platform driver trait.
 ///
 /// Drivers must implement this trait in order to get a platform driver registered.
@@ -199,6 +205,7 @@ macro_rules! module_platform_driver {
 ///
 ///     fn probe<'bound>(
 ///         _pdev: &'bound platform::Device<Core<'_>>,
+///         _tokens: platform::InitTokens<'bound>,
 ///         _id_info: Option<&'bound Self::IdInfo>,
 ///     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
 ///         Err(ENODEV)
@@ -229,6 +236,7 @@ pub trait Driver {
     /// Implementers should attempt to initialize the device here.
     fn probe<'bound>(
         dev: &'bound Device<device::Core<'_>>,
+        tokens: InitTokens<'bound>,
         id_info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
 
diff --git a/samples/rust/rust_debugfs.rs b/samples/rust/rust_debugfs.rs
index 77c19a437695..d0cc2b624835 100644
--- a/samples/rust/rust_debugfs.rs
+++ b/samples/rust/rust_debugfs.rs
@@ -122,6 +122,7 @@ impl platform::Driver for RustDebugFs {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         RustDebugFs::new(pdev).pin_chain(|this| {
diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
index ffb693544673..25ce01f2d224 100644
--- a/samples/rust/rust_dma.rs
+++ b/samples/rust/rust_dma.rs
@@ -76,6 +76,7 @@ impl pci::Driver for DmaSampleDriver {
 
     fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
+        _tokens: pci::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
         pin_init::pin_init_scope(move || {
diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driver_auxiliary.rs
index 0bee16faecc6..20d2b6d0fb77 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -98,6 +98,7 @@ impl pci::Driver for ParentDriver {
 
     fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
+        _tokens: pci::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
         try_pin_init!(ParentData {
diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
index 13b035a95756..f658efcd11bf 100644
--- a/samples/rust/rust_driver_pci.rs
+++ b/samples/rust/rust_driver_pci.rs
@@ -147,6 +147,7 @@ impl pci::Driver for SampleDriver {
 
     fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
+        _tokens: pci::InitTokens<'bound>,
         info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
         let vendor = pdev.vendor_id();
diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
index 710145b3605a..80b08d32435f 100644
--- a/samples/rust/rust_driver_platform.rs
+++ b/samples/rust/rust_driver_platform.rs
@@ -105,6 +105,7 @@ impl platform::Driver for SampleDriver {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         let dev = pdev.as_ref();
diff --git a/samples/rust/rust_i2c_client.rs b/samples/rust/rust_i2c_client.rs
index c8a23875ef5b..93b207699860 100644
--- a/samples/rust/rust_i2c_client.rs
+++ b/samples/rust/rust_i2c_client.rs
@@ -110,6 +110,7 @@ impl platform::Driver for SampleDriver {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<device::Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         dev_info!(
diff --git a/samples/rust/rust_soc.rs b/samples/rust/rust_soc.rs
index f5e5f2f9adf7..f54c173fed94 100644
--- a/samples/rust/rust_soc.rs
+++ b/samples/rust/rust_soc.rs
@@ -41,6 +41,7 @@ impl platform::Driver for SampleSocDriver {
 
     fn probe<'bound>(
         pdev: &'bound platform::Device<Core<'_>>,
+        _tokens: platform::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         dev_dbg!(pdev, "Probe Rust SoC driver sample.\n");
-- 
2.51.2


  reply	other threads:[~2026-11-23 20:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 20:39 [RFC PATCH 0/3] rust: dma: require device configuration before DMA Matteo Kloiber
2026-11-23 20:39 ` Matteo Kloiber [this message]
2026-11-23 20:39 ` [RFC PATCH 2/3] gpu: nova-core: build Gpu inside pin_init_scope Matteo Kloiber
2026-11-23 20:39 ` [RFC PATCH 3/3] rust: dma: require device configuration before DMA Matteo Kloiber

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=20261123203926.1787169-2-kernel@matt3o12.de \
    --to=kernel@matt3o12.de \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    /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®