From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.248.203 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795466399; b=rSy7/Bd2VxpHl2HTFWH4VQKyP5ikhVyMScJAGURqNZ0i8ALjICMHunb2Jeb4Kv9m5aWJ iP8vrMs40Mx7pIQNf4XJx1G4C1Mkid8DwttEoa1ce/LiRhRSpHn9UglFgDkS8+KOTVGXR jHU5/vTkTxfXNqh//OJJzCc94RfnGZnxRioUgP5kdSQxc5bXLaUfd+6/04cEMN+/W1PWn kYLouml9MoAiqxibwVYK8vmfdSocA3HWdb93B9Lik0Itp5xvVUq2eSz8dtTH2fbnpQsVn 3IE+ekctV0toR0fyeWSeyvPkZ4TrBp+s0ZI0aa93PoqaEt8wSDM6xinc1MjxG2PtJDQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795466399; h=DMARC-Filter:From:DKIM-Signature:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=6cQCl+niXlEMYTUYZXjMbn9i9gRKODH83pxxH7Ja3k4=; b=nj2rRH06nwjOb8Shs+VVP2kC5cMqwCWJDkj4j6KHLvT5+kca/eGt6a6Ow+Fk39SMC8AE ezdMuN4ro5f2NhTlkhaPnfMMZAlkKne8lZkgs929MG3TX5k8XGMEpF+ehtm2vpwVOhiZF 8oxtXQTo7a634qgEnF2w+n3cuKIL4375fm4uI3No9K76dG7tuKL12Ep23aRx2ExdydYln s8GpS5egSOzzpT0AwgZEhymgXOBp0JyIqiELncGaas7R8VCfvk8iNgyvBe0Xh8lyMrj05 ZrdvzBjYVQpvR4oRtt7qFxpZ1X5C4X8U1E/A2IXrtgeSCMwX0sC8C2D4KKM0c4tzmhA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=matt3o12.de; spf=pass smtp.mailfrom=matt3o12.de; dkim=pass header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=MQ7zupQl; arc=none smtp.remote-ip=91.216.248.203 Received: from mx1.mail-out.lima-city.de (mx1.mail-out.lima-city.de [91.216.248.203]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB754606A32E; Mon, 23 Nov 2026 20:39:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BB754606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=MQ7zupQl From: Matteo Kloiber DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=matt3o12.de; s=securedbylima-20230709; t=1795466390; bh=XALVTHWMsgu1a0FxagPF6ryKmWly2pkY8mk75dxeV4I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MQ7zupQleLZOjBgocgFfXMWGtGg9t/Q0IYuYF4fU4bBMU4QixbgpQmT0gLWAzfJE8 7Z5KDnFZ/6RrjZ7AKIY53W393WoFqDZOCbOtLlVxdhVH3A4gWDMQntHZofFJm33ey0 phRPbppo8PplLK4gLTQ8O37fsrzPm4dl9FkPR+SDvYWowC7MYwknGb0pvX1hJvJInV DUZVJJReacAQvt86SCbulwtykCQMm5Yhu5GokSORQpnE5TzxVYosJkQv4bV02cjQKn NdqorE9EDdzKH8ipR96f5vZNi2hdQgjG3h31nZX8pBh/RqTF9amkqU/O8YERZ0tlNy ehBul6c5iBNag== To: Danilo Krummrich , Alexandre Courbot , Alice Ryhl , Abdiel Janulgue , Robin Murphy , Andreas Hindborg , Gary Guo Cc: Daniel Almeida , Miguel Ojeda , David Airlie , Simona Vetter , Bjorn Helgaas , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Greg Kroah-Hartman , 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 Subject: [RFC PATCH 1/3] rust: pci, platform: pass InitTokens to probe() Date: Mon, 23 Nov 2026 21:39:24 +0100 Message-ID: <20261123203926.1787169-2-kernel@matt3o12.de> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20261123203926.1787169-1-kernel@matt3o12.de> References: <20261123203926.1787169-1-kernel@matt3o12.de> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Link: https://lore.kernel.org/DLR1JCUWS824.35QJU45D2W9ZE@nvidia.com Signed-off-by: Matteo Kloiber --- 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>, + _tokens: platform::InitTokens<'bound>, _id_info: Option<&'bound Self::IdInfo>, ) -> impl PinInit + 'bound { cpufreq::Registration::::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>, + _tokens: platform::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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>, + _tokens: pci::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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>, + _tokens: platform::InitTokens<'bound>, _id_info: Option<&'bound Self::IdInfo>, ) -> impl PinInit + '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>, +/// _tokens: platform::InitTokens<'bound>, /// _id_info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit + 'bound { /// cpufreq::Registration::::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, resource: &'a Resource) -> S /// /// fn probe<'bound>( /// pdev: &'bound platform::Device>, + /// _tokens: platform::InitTokens<'bound>, /// info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit + 'bound { /// let offset = 0; // Some offset. @@ -130,6 +131,7 @@ pub fn iomap_exclusive_sized(self) -> Result( /// pdev: &'bound platform::Device>, + /// _tokens: platform::InitTokens<'bound>, /// info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit + '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::() }; 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>, +/// _tokens: pci::InitTokens<'bound>, /// _id_info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit, Error> + 'bound { /// Err(ENODEV) @@ -317,6 +324,7 @@ pub trait Driver { /// attempt to initialize the device here. fn probe<'bound>( dev: &'bound Device>, + tokens: InitTokens<'bound>, id_info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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 { ::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>, +/// _tokens: platform::InitTokens<'bound>, /// _id_info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit, 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>, + tokens: InitTokens<'bound>, id_info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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>, + _tokens: platform::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit + '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>, + _tokens: pci::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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>, + _tokens: pci::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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>, + _tokens: pci::InitTokens<'bound>, info: Option<&'bound Self::IdInfo>, ) -> impl PinInit, 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>, + _tokens: platform::InitTokens<'bound>, info: Option<&'bound Self::IdInfo>, ) -> impl PinInit + '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>, + _tokens: platform::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit + '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>, + _tokens: platform::InitTokens<'bound>, _info: Option<&'bound Self::IdInfo>, ) -> impl PinInit + 'bound { dev_dbg!(pdev, "Probe Rust SoC driver sample.\n"); -- 2.51.2