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 <[email protected]>
Link: https://lore.kernel.org/[email protected]
Signed-off-by: Matteo Kloiber <[email protected]>
---
 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_driver_sriov/pf.rs  |  1 +
 samples/rust/rust_driver_sriov/vf.rs  |  1 +
 samples/rust/rust_i2c_client.rs       |  1 +
 samples/rust/rust_soc.rs              |  1 +
 17 files changed, 34 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 bea1b1b0844c..365e77ff0cc4 100644
--- a/drivers/gpu/nova-core/driver.rs
+++ b/drivers/gpu/nova-core/driver.rs
@@ -101,6 +101,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 04a6d240afb2..1d61fcb64b03 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -147,7 +147,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)
@@ -451,6 +451,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
@@ -479,6 +485,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)
@@ -513,6 +520,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 804d55a63005..b6a36d10bf74 100644
--- a/samples/rust/rust_dma.rs
+++ b/samples/rust/rust_dma.rs
@@ -77,6 +77,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 6cff9bf8c80c..96954f001b13 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -99,6 +99,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 50c8709739c6..33b864d2cbea 100644
--- a/samples/rust/rust_driver_pci.rs
+++ b/samples/rust/rust_driver_pci.rs
@@ -148,6 +148,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_driver_sriov/pf.rs 
b/samples/rust/rust_driver_sriov/pf.rs
index 8f9958683528..3df204c9951c 100644
--- a/samples/rust/rust_driver_sriov/pf.rs
+++ b/samples/rust/rust_driver_sriov/pf.rs
@@ -69,6 +69,7 @@ impl pci::Driver for SamplePfDriver {
 
     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!(PfDriverData {
diff --git a/samples/rust/rust_driver_sriov/vf.rs 
b/samples/rust/rust_driver_sriov/vf.rs
index 256b1b3dfab7..c4bfca1a1c31 100644
--- a/samples/rust/rust_driver_sriov/vf.rs
+++ b/samples/rust/rust_driver_sriov/vf.rs
@@ -31,6 +31,7 @@ impl pci::Driver for SampleVfDriver {
 
     fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
+        _tokens: pci::InitTokens<'bound>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self, Error> + 'bound {
         dev_info!(
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 via email to