rust: driver: use pointers instead of indices for ID info
Subsystems (23)
Gary Guo
reviewerv2 cover rust: driver: use pointers instead of indices for ID info
Most C drivers use a pointer (and cast to kernel_ulong_t) for driver_data fields in device_id. Rust code currently does not do this, but rather use indices. These indices then needs to be translated to &IdInfo separately and this is by a side table.
This leads to open-coded ACPI/OF handling in driver.rs, which is not desirable. Convert the code to use pointers (or rather, static references) instead.
---
Changes in v2:
- Change USB to take Option<&IdInfo> in addition to PCI due to ability to dynamically add IDs.
- Mention dyn IDs and driver_override in safety comments and justify why they're correct.
- Link to v1: https://patch.msgid.link/[email protected]
---
Gary Guo (11):
rust: driver: remove IdTable::id
rust: driver: simplify IdArray::new_without_index
rust: pci: use Option<&IdInfo> for device ID info
rust: usb: use Option<&IdInfo> for device ID info
rust: net/phy: remove expansion from doc
rust: driver: centralize device ID handling
rust: driver: remove $module_table_name from module_device_table
rust: driver: store pointers in DeviceId
rust: driver: remove open-coded matching logic
rust: driver: remove duplicate ID table
RFC: rust: driver: support map-like syntax for ID table
MAINTAINERS | 1 - drivers/acpi/bus.c | 6 +- drivers/cpufreq/rcpufreq_dt.rs | 1 - drivers/gpu/drm/nova/driver.rs | 1 - drivers/gpu/drm/tyr/driver.rs | 1 - drivers/gpu/nova-core/driver.rs | 3 +- drivers/pwm/pwm_th1520.rs | 1 - include/acpi/acpi_bus.h | 11 -- rust/helpers/acpi.c | 16 --- rust/helpers/helpers.c | 1 - rust/kernel/acpi.rs | 14 +-- rust/kernel/auxiliary.rs | 18 +-- rust/kernel/device_id.rs | 207 +++++++++++++++++++--------------- rust/kernel/driver.rs | 137 ++-------------------- rust/kernel/i2c.rs | 26 ++--- rust/kernel/net/phy.rs | 66 +---------- rust/kernel/of.rs | 14 +-- rust/kernel/pci.rs | 25 ++-- rust/kernel/platform.rs | 5 +- rust/kernel/usb.rs | 24 ++-- samples/rust/rust_debugfs.rs | 1 - samples/rust/rust_dma.rs | 3 +- samples/rust/rust_driver_auxiliary.rs | 4 +- samples/rust/rust_driver_i2c.rs | 3 - samples/rust/rust_driver_pci.rs | 11 +- samples/rust/rust_driver_platform.rs | 2 - samples/rust/rust_driver_usb.rs | 3 +- samples/rust/rust_i2c_client.rs | 2 - samples/rust/rust_soc.rs | 2 - 29 files changed, 178 insertions(+), 431 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260612-id_info-23eca472ccd8
Best regards,
Signature · 1 line
Gary Guo <[email protected]>
Gary Guo
reviewerPATCH v2 1/11 rust: driver: remove `IdTable::id`
This is unused.
--- rust/kernel/device_id.rs | 7 ------- 1 file changed, 7 deletions(-)
rust/kernel/device_id.rs +0 -7
@@ -166,9 +166,6 @@ pub trait IdTable<T: RawDeviceId, U> {/// Obtain the pointer to the ID table. fn as_ptr (&self) -> *const T ::RawType ; -/// Obtain the pointer to the bus specific device ID from an index. -fn id (&self, index:usize ) -> &T ::RawType ; -/// Obtain the pointer to the driver-specific information from an index. fn info (&self, index:usize ) -> &U ; }
@@ -180,10 +177,6 @@ fn as_ptr(&self) -> *const T::RawType { core::ptr::from_ref (self).cast () } -fn id (&self, index:usize ) -> &T ::RawType { - &self.raw_ids.ids[index] - } -fn info (&self, index:usize ) -> &U { &self.id_infos[index] }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 01/11] rust: driver: remove `IdTable::id`
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 3 lines
This is unused.
Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Gary Guo
reviewerPATCH v2 2/11 rust: driver: simplify `IdArray::new_without_index`
In reply to Gary GuoThis method can very easily construct the IdArray on its own without delegating to Self::build. Doing so also simplifies the phy device table macro because it does not need to construct tuples anymore.
This also allows simplification of new and build which removes the unsafe.
--- rust/kernel/device_id.rs | 64 ++++++++++++++++++++---------------------------- rust/kernel/net/phy.rs | 2 +- 2 files changed, 28 insertions(+), 38 deletions(-)
rust/kernel/device_id.rs +27 -37
@@ -73,19 +73,11 @@ pub struct IdArray<T: RawDeviceId, U, const N: usize> { id_infos: [U; N], } -impl <T :RawDeviceId ,U ,const N :usize >IdArray <T ,U ,N > { +impl <T :RawDeviceId +RawDeviceIdIndex ,U ,const N :usize >IdArray <T ,U ,N > {/// Creates a new instance of the array. /// /// The contents are derived from the given identifiers and context information. -/// -/// # Safety -/// -/// `data_offset` as `None` is always safe. -/// If `data_offset` is `Some(data_offset)`, then: -/// - `data_offset` must be the correct offset (in bytes) to the context/data field -/// (e.g., the `driver_data` field) within the raw device ID structure. -/// - The field at `data_offset` must be correctly sized to hold a `usize`. -const unsafe fn build (ids: [(T ,U );N ], data_offset:Option <usize >) ->Self { +pub const fn new (ids: [(T ,U );N ]) ->Self {let mut raw_ids = [const {MaybeUninit ::<T ::RawType >::uninit () };N ];let mut infos = [const {MaybeUninit ::uninit () };N ];
@@ -94,16 +86,14 @@ impl<T: RawDeviceId, U, const N: usize> IdArray<T, U, N> {// SAFETY: by the safety requirement of `RawDeviceId`, we're guaranteed that `T` is // layout-wise compatible with `RawType`. raw_ids[i] =unsafe { core::mem::transmute_copy (&ids[i].0 ) }; -if let Some (data_offset) = data_offset { -// SAFETY: by the safety requirement of this function, this would be effectively -// `raw_ids[i].driver_data = i;`. -unsafe { - raw_ids[i] - .as_mut_ptr () - .byte_add (data_offset) - .cast ::<usize >() - .write (i); - } +// SAFETY: by the safety requirement of `RawDeviceIdIndex`, this would be effectively +// `raw_ids[i].driver_data = i;`. +unsafe { + raw_ids[i] + .as_mut_ptr () + .byte_add (T ::DRIVER_DATA_OFFSET ) + .cast ::<usize >() + .write (i); }// SAFETY: this is effectively a move: `infos[i] = ids[i].1`. We make a copy here but
@@ -127,32 +117,32 @@ impl<T: RawDeviceId, U, const N: usize> IdArray<T, U, N> { id_infos: unsafe { core::mem::transmute_copy(&infos) }, } } +} -/// Creates a new instance of the array without writing index values. -/// -/// The contents are derived from the given identifiers and context information. -/// If the device implements [`RawDeviceIdIndex`], consider using [`IdArray::new`] instead. -pub const fn new_without_index (ids: [(T ,U );N ]) ->Self { -// SAFETY: Calling `Self::build` with `offset = None` is always safe, -// because no raw memory writes are performed in this case. -unsafe {Self ::build (ids,None ) } - } - +impl <T :RawDeviceId ,U ,const N :usize >IdArray <T ,U ,N > {/// Reference to the contained [`RawIdArray`]. pub const fn raw_ids (&self) -> &RawIdArray <T ,N > { &self.raw_ids } } -impl <T :RawDeviceId +RawDeviceIdIndex ,U ,const N :usize >IdArray <T ,U ,N > { -/// Creates a new instance of the array. +impl <T :RawDeviceId ,const N :usize >IdArray <T , (),N > { +/// Creates a new instance of the array without writing index values. /// /// The contents are derived from the given identifiers and context information. -pub const fn new (ids: [(T ,U );N ]) ->Self { -// SAFETY: by the safety requirement of `RawDeviceIdIndex`, -// `T::DRIVER_DATA_OFFSET` is guaranteed to be the correct offset (in bytes) to -// a field within `T::RawType`. -unsafe {Self ::build (ids,Some (T ::DRIVER_DATA_OFFSET )) } +/// If the device implements [`RawDeviceIdIndex`], consider using [`IdArray::new`] instead. +pub const fn new_without_index (ids: [T ;N ]) ->Self { +// SAFETY: `T` is layout-wise compatible with `T::RawType`, so is the array of them. +let raw_ids: [T ::RawType ;N ] =unsafe { core::mem::transmute_copy (&ids) }; + core::mem::forget (ids); + +Self { + raw_ids:RawIdArray { + ids: raw_ids, + sentinel:MaybeUninit ::zeroed (), + }, + id_infos: [();N ], + } } }
rust/kernel/net/phy.rs +1 -1
@@ -868,7 +868,7 @@ macro_rules! module_phy_driver {const N :usize = $crate::module_phy_driver!(@count_devices $($dev),+);const TABLE : $crate::device_id::IdArray <$crate::net::phy::DeviceId , (),N > = - $crate::device_id::IdArray::new_without_index([ $(($dev,())),+, ]); + $crate::device_id::IdArray::new_without_index([ $($dev),+, ]); $crate::module_device_table!("mdio" , phydev,TABLE ); };
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 02/11] rust: driver: simplify `IdArray::new_without_index`
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
This method can very easily construct the
IdArrayon its own without delegating toSelf::build. Doing so also simplifies the phy device table macro because it does not need to construct tuples anymore.This also allows simplification of
newandbuildwhich removes theunsafe.Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Gary Guo
reviewerPATCH v2 3/11 rust: pci: use `Option<&IdInfo>` for device ID info
In reply to Gary GuoIt is possible that pci_device_id_any will be passed to the driver, e.g. driver_override is used on the device. Therefore, the driver must be able to handle the case where driver_data is 0. Thus, update the probe functions to get Option.
The current code cannot tell if the info does not exist or is the first entry; however this will be achievable once the code is updated to use a &'static IdInfo pointer instead of indices.
--- drivers/gpu/nova-core/driver.rs | 2 +- rust/kernel/pci.rs | 6 +++--- samples/rust/rust_dma.rs | 2 +- samples/rust/rust_driver_auxiliary.rs | 2 +- samples/rust/rust_driver_pci.rs | 3 ++- 5 files changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/nova-core/driver.rs +1 -1
@@ -70,7 +70,7 @@ impl pci::Driver for NovaCoreDriver {fn probe <'bound>( pdev: &'bound pci::Device <Core <'_>>, - _info: &'boundSelf ::IdInfo , + _info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ::Data <'bound>,Error > + 'bound { pin_init::pin_init_scope (move || {dev_dbg! (pdev,"Probe Nova Core GPU driver.\n" );
rust/kernel/pci.rs +3 -3
@@ -113,7 +113,7 @@ extern "C" fn probe_callback(let info =T ::ID_TABLE .info (id.index ());from_result (|| { -let data =T ::probe (pdev, info); +let data =T ::probe (pdev,Some (info)); pdev.as_ref ().set_drvdata (data)?;Ok (0 )
@@ -284,7 +284,7 @@ macro_rules! pci_device_table {/// /// fn probe<'bound>( /// _pdev: &'bound pci::Device<Core<'_>>, -/// _id_info: &'bound Self::IdInfo, +/// _id_info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound { /// Err(ENODEV) /// }
@@ -313,7 +313,7 @@ pub trait Driver {/// attempt to initialize the device here. fn probe <'bound>( dev: &'boundDevice <device::Core <'_>>, - id_info: &'boundSelf ::IdInfo , + id_info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ::Data <'bound>,Error > + 'bound;/// PCI driver unbind.
samples/rust/rust_dma.rs +1 -1
@@ -63,7 +63,7 @@ impl pci::Driver for DmaSampleDriver {fn probe <'bound>( pdev: &'bound pci::Device <Core <'_>>, - _info: &'boundSelf ::IdInfo , + _info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ,Error > + 'bound { pin_init::pin_init_scope (move || {dev_info! (pdev,"Probe DMA test driver.\n" );
samples/rust/rust_driver_auxiliary.rs +1 -1
@@ -79,7 +79,7 @@ impl pci::Driver for ParentDriver {fn probe <'bound>( pdev: &'bound pci::Device <Core <'_>>, - _info: &'boundSelf ::IdInfo , + _info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ::Data <'bound>,Error > + 'bound {Ok (ParentData {// SAFETY: `ParentData` is the driver's private data, which is dropped when the
samples/rust/rust_driver_pci.rs +2 -1
@@ -144,7 +144,7 @@ impl pci::Driver for SampleDriver {fn probe <'bound>( pdev: &'bound pci::Device <Core <'_>>, - info: &'boundSelf ::IdInfo , + info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ::Data <'bound>,Error > + 'bound {let vendor = pdev.vendor_id ();dev_dbg! (
@@ -153,6 +153,7 @@ fn probe<'bound>( vendor, pdev.device_id () ); +let info = info.ok_or (ENODEV )?; pdev.enable_device_mem ()?; pdev.set_master ();
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 03/11] rust: pci: use `Option<&IdInfo>` for device ID info
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
It is possible that
pci_device_id_anywill be passed to the driver, e.g.driver_overrideis used on the device. Therefore, the driver must be able to handle the case wheredriver_datais 0. Thus, update theprobefunctions to getOption.The current code cannot tell if the info does not exist or is the first entry; however this will be achievable once the code is updated to use a
&'static IdInfopointer instead of indices.Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Gary Guo
reviewerPATCH v2 4/11 rust: usb: use `Option<&IdInfo>` for device ID info
In reply to Gary GuoIt is possible that ID without driver_data will be passed to the driver, e.g. new_id is used to dynamically create a new ID without data. Therefore, the driver must be able to handle the case where driver_data is 0. Thus, update the probe functions to get Option.
The current code cannot tell if the info does not exist or is the first entry; however this will be achievable once the code is updated to use a &'static IdInfo pointer instead of indices.
--- rust/kernel/usb.rs | 6 +++--- samples/rust/rust_driver_usb.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
rust/kernel/usb.rs +3 -3
@@ -90,7 +90,7 @@ extern "C" fn probe_callback(let id =unsafe { &*id.cast ::<DeviceId >() };let info =T ::ID_TABLE .info (id.index ()); -let data =T ::probe (intf, id, info); +let data =T ::probe (intf, id,Some (info));let dev: &device::Device <device::CoreInternal <'_>> = intf.as_ref (); dev.set_drvdata (data)?;
@@ -293,7 +293,7 @@ macro_rules! usb_device_table {/// fn probe<'bound>( /// _interface: &'bound usb::Interface<Core<'_>>, /// _id: &usb::DeviceId, -/// _info: &'bound Self::IdInfo, +/// _info: Option<&'bound Self::IdInfo>, /// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound { /// Err(ENODEV) /// }
@@ -322,7 +322,7 @@ pub trait Driver {fn probe <'bound>( interface: &'boundInterface <device::Core <'_>>, id: &DeviceId , - id_info: &'boundSelf ::IdInfo , + id_info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ::Data <'bound>,Error > + 'bound;/// USB driver disconnect.
samples/rust/rust_driver_usb.rs +1 -1
@@ -32,7 +32,7 @@ impl usb::Driver for SampleDriver {fn probe <'bound>( intf: &'bound usb::Interface <Core <'_>>, _id: &usb::DeviceId , - _info: &'boundSelf ::IdInfo , + _info:Option <&'boundSelf ::IdInfo >, ) ->impl PinInit <Self ,Error > + 'bound {let dev: &device::Device <Core <'_>> = intf.as_ref ();dev_info! (dev,"Rust USB driver sample probed\n" );
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 04/11] rust: usb: use `Option<&IdInfo>` for device ID info
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
It is possible that ID without
driver_datawill be passed to the driver, e.g.new_idis used to dynamically create a new ID without data. Therefore, the driver must be able to handle the case wheredriver_datais 0. Thus, update theprobefunctions to getOption.The current code cannot tell if the info does not exist or is the first entry; however this will be achievable once the code is updated to use a
&'static IdInfopointer instead of indices.Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Gary Guo
reviewerPATCH v2 5/11 rust: net/phy: remove expansion from doc
In reply to Gary GuoThe expansion serves little purpose and it can easily diverge.
--- rust/kernel/net/phy.rs | 56 -------------------------------------------------- 1 file changed, 56 deletions(-)
rust/kernel/net/phy.rs +0 -56
@@ -800,62 +800,6 @@ const fn as_int(&self) -> u32 {/// } /// # } /// ``` -/// -/// This expands to the following code: -/// -/// ```ignore -/// use kernel::net::phy::{self, DeviceId}; -/// use kernel::prelude::*; -/// -/// struct Module { -/// _reg: ::kernel::net::phy::Registration, -/// } -/// -/// module! { -/// type: Module, -/// name: "rust_sample_phy", -/// authors: ["Rust for Linux Contributors"], -/// description: "Rust sample PHYs driver", -/// license: "GPL", -/// } -/// -/// struct PhySample; -/// -/// #[vtable] -/// impl phy::Driver for PhySample { -/// const NAME: &'static CStr = c"PhySample"; -/// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001); -/// } -/// -/// const _: () = { -/// static mut DRIVERS: [::kernel::net::phy::DriverVTable; 1] = -/// [::kernel::net::phy::create_phy_driver::<PhySample>()]; -/// -/// impl ::kernel::Module for Module { -/// fn init(module: &'static ::kernel::ThisModule) -> Result<Self> { -/// let drivers = unsafe { &mut DRIVERS }; -/// let mut reg = ::kernel::net::phy::Registration::register( -/// module, -/// ::core::pin::Pin::static_mut(drivers), -/// )?; -/// Ok(Module { _reg: reg }) -/// } -/// } -/// }; -/// -/// const N: usize = 1; -/// -/// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId, (), N> = -/// ::kernel::device_id::IdArray::new_without_index([ -/// ::kernel::net::phy::DeviceId( -/// ::kernel::bindings::mdio_device_id { -/// phy_id: 0x00000001, -/// phy_id_mask: 0xffffffff, -/// }), -/// ]); -/// -/// ::kernel::module_device_table!("mdio", phydev, TABLE); -/// ``` #[macro_export] macro_rules! module_phy_driver { (@replace_expr $_t:tt $sub:expr) => {$sub};
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 05/11] rust: net/phy: remove expansion from doc
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 3 lines
The expansion serves little purpose and it can easily diverge.
Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Danilo Krummrich
maintainerRe: [PATCH v2 05/11] rust: net/phy: remove expansion from doc
In reply to Gary GuoQuoted 3 lines · Gary Guo
The expansion serves little purpose and it can easily diverge.
Signed-off-by Gary Guo <[email protected]>
I did not pick this one up, as it is not necessarily needed by this series.
However, I agree with the intend and I think it did diverge already before this series.
FUJITA, please consider to pick this one up, or let me know if you want me to take it.
Thanks,
Danilo
Re: [PATCH v2 05/11] rust: net/phy: remove expansion from doc
Quoted 5 lines · On Thu, 30 Jul 2026 23:01:18 +0200 "Danilo Krummrich" <[email protected]> wrote:
Quoted 3 lines · Gary Guo
The expansion serves little purpose and it can easily diverge.
Signed-off-by Gary Guo <[email protected]>
Quoted 7 lines
I did not pick this one up, as it is not necessarily needed by this series.
However, I agree with the intend and I think it did diverge already before this series.
FUJITA, please consider to pick this one up, or let me know if you want me to take it.
net/phy changes normally go through netdev, but this one just removes documentation that is already obsolete, so I think taking it via driver-core as part of this series is the least work for everyone.
So please take it with the series, unless the netdev folks object.
Thanks,
Re: [PATCH v2 05/11] rust: net/phy: remove expansion from doc
In reply to Gary GuoQuoted 6 lines · On Mon, 29 Jun 2026 13:39:39 +0100 Gary Guo <[email protected]> wrote:
The expansion serves little purpose and it can easily diverge.
Signed-off-by Gary Guo <[email protected]>--- rust/kernel/net/phy.rs | 56 -------------------------------------------------- 1 file changed, 56 deletions(-)
Agreed, it has already diverged, so let's drop it.
Gary Guo
reviewerPATCH v2 6/11 rust: driver: centralize device ID handling
In reply to Gary GuoMove the IdArray creation from individual buses to be handled by shared code in device_id.rs.
--- rust/kernel/acpi.rs | 10 ++-------- rust/kernel/auxiliary.rs | 10 ++-------- rust/kernel/device_id.rs | 31 ++++++++++++++++++++++++++++++- rust/kernel/i2c.rs | 10 ++-------- rust/kernel/net/phy.rs | 10 ++++------ rust/kernel/of.rs | 10 ++-------- rust/kernel/pci.rs | 10 ++-------- rust/kernel/usb.rs | 10 ++-------- 8 files changed, 46 insertions(+), 55 deletions(-)
rust/kernel/acpi.rs +2 -8
@@ -53,13 +53,7 @@ pub const fn new(id: &'static CStr) -> Self {/// Create an ACPI `IdTable` with an "alias" for modpost. #[macro_export] macro_rules! acpi_device_table { - ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => { -const $table_name: $crate::device_id::IdArray < - $crate::acpi::DeviceId , - $id_info_type, - { $table_data.len () }, - > = $crate::device_id::IdArray ::new ($table_data); - - $crate::module_device_table!("acpi" , $module_table_name, $table_name); + ($($tt:tt)*) => { + $crate::module_device_table!("acpi" , $crate ::acpi::DeviceId , $($tt)*); }; }
rust/kernel/auxiliary.rs +2 -8
@@ -181,14 +181,8 @@ fn index(&self) -> usize {/// Create a auxiliary `IdTable` with its alias for modpost. #[macro_export] macro_rules! auxiliary_device_table { - ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => { -const $table_name: $crate::device_id::IdArray < - $crate::auxiliary::DeviceId , - $id_info_type, - { $table_data.len () }, - > = $crate::device_id::IdArray ::new ($table_data); - - $crate::module_device_table!("auxiliary" , $module_table_name, $table_name); + ($($tt:tt)*) => { + $crate::module_device_table!("auxiliary" , $crate ::auxiliary::DeviceId , $($tt)*); }; }
rust/kernel/device_id.rs +30 -1
@@ -175,7 +175,36 @@ fn info(&self, index: usize) -> &U {/// Create device table alias for modpost. #[macro_export] macro_rules! module_device_table { - ($table_type: literal, $module_table_name:ident, $table_name:ident) => { + ( + $table_type: literal, $device_id_ty: ty, + $table_name: ident, $module_table_name: ident, $id_info_type: ty, + [$(($id: expr, $info:expr $(,)?)),* $(,)?] + ) => { +const $table_name: $crate::device_id::IdArray < + $device_id_ty, + $id_info_type, + { <[$device_id_ty]>::len (&[$($id,)*]) }, + > = $crate::device_id::IdArray ::new ([$(($id, $info),)*]); + + $crate::module_device_table!($table_type, $module_table_name, $table_name); + }; + +// Case for no ID info. + ( + $table_type: literal, $device_id_ty: ty, + $table_name: ident, $module_table_name: ident, @none, + [$($id: expr),* $(,)?] + ) => { +const $table_name: $crate::device_id::IdArray < + $device_id_ty, + (), + { <[$device_id_ty]>::len (&[$($id,)*]) }, + > = $crate::device_id::IdArray ::new_without_index ([$($id),*]); + + $crate::module_device_table!($table_type, $module_table_name, $table_name); + }; + + ($table_type: literal, $module_table_name: ident, $table_name:ident) => {#[rustfmt::skip] #[export_name = concat!("__mod_device_table__", line!(),
rust/kernel/i2c.rs +2 -8
@@ -77,14 +77,8 @@ fn index(&self) -> usize {/// Create a I2C `IdTable` with its alias for modpost. #[macro_export] macro_rules! i2c_device_table { - ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => { -const $table_name: $crate::device_id::IdArray < - $crate::i2c::DeviceId , - $id_info_type, - { $table_data.len () }, - > = $crate::device_id::IdArray ::new ($table_data); - - $crate::module_device_table!("i2c" , $module_table_name, $table_name); + ($($tt:tt)*) => { + $crate::module_device_table!("i2c" , $crate ::i2c::DeviceId , $($tt)*); }; }
rust/kernel/net/phy.rs +4 -6
@@ -809,12 +809,10 @@ macro_rules! module_phy_driver { }; (@device_table [$($dev:expr),+]) => { -const N :usize = $crate::module_phy_driver!(@count_devices $($dev),+); - -const TABLE : $crate::device_id::IdArray <$crate::net::phy::DeviceId , (),N > = - $crate::device_id::IdArray::new_without_index([ $($dev),+, ]); - - $crate::module_device_table!("mdio" , phydev,TABLE ); + $crate::module_device_table!( +"mdio" , $crate ::net::phy::DeviceId , + phydev,TABLE , @none, [$($dev),+] + ); }; (drivers: [$($driver:ident),+ $(,)?], device_table: [$($dev:expr),+ $(,)?], $($f:tt)*) => {
rust/kernel/of.rs +2 -8
@@ -53,13 +53,7 @@ pub const fn new(compatible: &'static CStr) -> Self {/// Create an OF `IdTable` with an "alias" for modpost. #[macro_export] macro_rules! of_device_table { - ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => { -const $table_name: $crate::device_id::IdArray < - $crate::of::DeviceId , - $id_info_type, - { $table_data.len () }, - > = $crate::device_id::IdArray ::new ($table_data); - - $crate::module_device_table!("of" , $module_table_name, $table_name); + ($($tt:tt)*) => { + $crate::module_device_table!("of" , $crate ::of::DeviceId , $($tt)*); }; }
rust/kernel/pci.rs +2 -8
@@ -245,14 +245,8 @@ fn index(&self) -> usize {/// Create a PCI `IdTable` with its alias for modpost. #[macro_export] macro_rules! pci_device_table { - ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => { -const $table_name: $crate::device_id::IdArray < - $crate::pci::DeviceId , - $id_info_type, - { $table_data.len () }, - > = $crate::device_id::IdArray ::new ($table_data); - - $crate::module_device_table!("pci" , $module_table_name, $table_name); + ($($tt:tt)*) => { + $crate::module_device_table!("pci" , $crate ::pci::DeviceId , $($tt)*); }; }
rust/kernel/usb.rs +2 -8
@@ -254,14 +254,8 @@ fn index(&self) -> usize {/// Create a USB `IdTable` with its alias for modpost. #[macro_export] macro_rules! usb_device_table { - ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => { -const $table_name: $crate::device_id::IdArray < - $crate::usb::DeviceId , - $id_info_type, - { $table_data.len () }, - > = $crate::device_id::IdArray ::new ($table_data); - - $crate::module_device_table!("usb" , $module_table_name, $table_name); + ($($tt:tt)*) => { + $crate::module_device_table!("usb" , $crate ::usb::DeviceId , $($tt)*); }; }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 06/11] rust: driver: centralize device ID handling
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
Move the
IdArraycreation from individual buses to be handled by shared code indevice_id.rs.Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Gary Guo
reviewerPATCH v2 7/11 rust: driver: remove `$module_table_name` from `module_device_table`
In reply to Gary GuoWrap the generated code in a const _: () block to avoid symbol conflict. This removes the need of creating a new identifier.
--- drivers/cpufreq/rcpufreq_dt.rs | 1 - drivers/gpu/drm/nova/driver.rs | 1 - drivers/gpu/drm/tyr/driver.rs | 1 - drivers/gpu/nova-core/driver.rs | 1 - drivers/pwm/pwm_th1520.rs | 1 - rust/kernel/device_id.rs | 30 ++++++++++++++++-------------- rust/kernel/i2c.rs | 3 --- rust/kernel/net/phy.rs | 2 +- rust/kernel/pci.rs | 1 - rust/kernel/platform.rs | 2 -- rust/kernel/usb.rs | 1 - samples/rust/rust_debugfs.rs | 1 - samples/rust/rust_dma.rs | 1 - samples/rust/rust_driver_auxiliary.rs | 2 -- samples/rust/rust_driver_i2c.rs | 3 --- samples/rust/rust_driver_pci.rs | 1 - samples/rust/rust_driver_platform.rs | 2 -- samples/rust/rust_driver_usb.rs | 1 - samples/rust/rust_i2c_client.rs | 2 -- samples/rust/rust_soc.rs | 2 -- 20 files changed, 17 insertions(+), 42 deletions(-)
drivers/cpufreq/rcpufreq_dt.rs +0 -1
@@ -194,7 +194,6 @@ fn register_em(policy: &mut cpufreq::Policy) { kernel::of_device_table!(OF_TABLE , -MODULE_OF_TABLE , <CPUFreqDTDriver as platform::Driver >::IdInfo , [(of::DeviceId ::new (c"operating-points-v2" ), ())] );
drivers/gpu/drm/nova/driver.rs +0 -1
@@ -43,7 +43,6 @@ pub(crate) struct NovaData { kernel::auxiliary_device_table!( AUX_TABLE, - MODULE_AUX_TABLE, <NovaDriver as auxiliary::Driver>::IdInfo, [( auxiliary::DeviceId::new(NOVA_CORE_MODULE_NAME, AUXILIARY_NAME),
drivers/gpu/drm/tyr/driver.rs +0 -1
@@ -87,7 +87,6 @@ fn issue_soft_reset(dev: &Device, iomem: &IoMem<'_>) -> Result { kernel::of_device_table!( OF_TABLE, - MODULE_OF_TABLE, <TyrPlatformDriver as platform::Driver>::IdInfo, [ (of::DeviceId::new(c"rockchip,rk3588-mali"), ()),
drivers/gpu/nova-core/driver.rs +0 -1
@@ -40,7 +40,6 @@ pub(crate) struct NovaCore<'bound> { kernel::pci_device_table!( PCI_TABLE, - MODULE_PCI_TABLE, <NovaCoreDriver as pci::Driver>::IdInfo, [ // Modern NVIDIA GPUs will show up as either VGA or 3D controllers.
drivers/pwm/pwm_th1520.rs +0 -1
@@ -303,7 +303,6 @@ fn drop(self: Pin<&mut Self>) { kernel::of_device_table!(OF_TABLE , -MODULE_OF_TABLE , <Th1520PwmPlatformDriver as platform::Driver >::IdInfo , [(of::DeviceId ::new (c"thead,th1520-pwm" ), ())] );
rust/kernel/device_id.rs +16 -14
@@ -177,7 +177,7 @@ fn info(&self, index: usize) -> &U {macro_rules! module_device_table { ( $table_type: literal, $device_id_ty: ty, - $table_name: ident, $module_table_name: ident, $id_info_type: ty, + $table_name: ident, $id_info_type: ty, [$(($id: expr, $info:expr $(,)?)),* $(,)?] ) => {const $table_name: $crate::device_id::IdArray <
@@ -186,13 +186,13 @@ macro_rules! module_device_table { { <[$device_id_ty]>::len(&[$($id,)*]) }, > = $crate::device_id::IdArray::new([$(($id, $info),)*]); - $crate::module_device_table!($table_type, $module_table_name, $table_name); + $crate::module_device_table!($table_type, $table_name); };// Case for no ID info. ( $table_type: literal, $device_id_ty: ty, - $table_name: ident, $module_table_name: ident, @none, + $table_name: ident, @none, [$($id: expr),* $(,)?] ) => { const $table_name: $crate::device_id::IdArray<
@@ -201,18 +201,20 @@ macro_rules! module_device_table { { <[$device_id_ty]>::len(&[$($id,)*]) }, > = $crate::device_id::IdArray::new_without_index([$($id),*]); - $crate::module_device_table!($table_type, $module_table_name, $table_name); + $crate::module_device_table!($table_type, $table_name); }; - ($table_type: literal, $module_table_name: ident, $table_name:ident) => { -#[rustfmt::skip] -#[export_name = -concat! ( "__mod_device_table__" , line! (), -"__kmod_" , module_path! (), -"__" , $table_type, -"__" , stringify! ($table_name)) -] - static $module_table_name: [::core::mem::MaybeUninit<u8>; $table_name.raw_ids().size()] = -unsafe { ::core::mem::transmute_copy ($table_name.raw_ids ()) }; + ($table_type: literal, $table_name:ident) => { +const _: () = { +#[rustfmt::skip] +#[export_name = +concat! ( "__mod_device_table__" , line! (), +"__kmod_" , module_path! (), +"__" , $table_type, +"__" , stringify! ($table_name)) +] +static MOD_DEVICE_TABLE : [::core::mem::MaybeUninit <u8 >; $table_name.raw_ids ().size ()] = +unsafe { ::core::mem::transmute_copy ($table_name.raw_ids ()) }; + }; }; }
rust/kernel/i2c.rs +0 -3
@@ -261,7 +261,6 @@ macro_rules! module_i2c_driver {/// /// kernel::acpi_device_table!( /// ACPI_TABLE, -/// MODULE_ACPI_TABLE, /// <MyDriver as i2c::Driver>::IdInfo, /// [ /// (acpi::DeviceId::new(c"LNUXBEEF"), ())
@@ -270,7 +269,6 @@ macro_rules! module_i2c_driver {/// /// kernel::i2c_device_table!( /// I2C_TABLE, -/// MODULE_I2C_TABLE, /// <MyDriver as i2c::Driver>::IdInfo, /// [ /// (i2c::DeviceId::new(c"rust_driver_i2c"), ())
@@ -279,7 +277,6 @@ macro_rules! module_i2c_driver {/// /// kernel::of_device_table!( /// OF_TABLE, -/// MODULE_OF_TABLE, /// <MyDriver as i2c::Driver>::IdInfo, /// [ /// (of::DeviceId::new(c"test,device"), ())
rust/kernel/net/phy.rs +1 -1
@@ -811,7 +811,7 @@ macro_rules! module_phy_driver { (@device_table [$($dev:expr),+]) => { $crate::module_device_table!("mdio" , $crate ::net::phy::DeviceId , - phydev,TABLE , @none, [$($dev),+] +TABLE , @none, [$($dev),+] ); };
rust/kernel/pci.rs +0 -1
@@ -261,7 +261,6 @@ macro_rules! pci_device_table {/// /// kernel::pci_device_table!( /// PCI_TABLE, -/// MODULE_PCI_TABLE, /// <MyDriver as pci::Driver>::IdInfo, /// [ /// (
rust/kernel/platform.rs +0 -2
@@ -176,7 +176,6 @@ macro_rules! module_platform_driver {/// /// kernel::of_device_table!( /// OF_TABLE, -/// MODULE_OF_TABLE, /// <MyDriver as platform::Driver>::IdInfo, /// [ /// (of::DeviceId::new(c"test,device"), ())
@@ -185,7 +184,6 @@ macro_rules! module_platform_driver {/// /// kernel::acpi_device_table!( /// ACPI_TABLE, -/// MODULE_ACPI_TABLE, /// <MyDriver as platform::Driver>::IdInfo, /// [ /// (acpi::DeviceId::new(c"LNUXBEEF"), ())
rust/kernel/usb.rs +0 -1
@@ -271,7 +271,6 @@ macro_rules! usb_device_table {/// /// kernel::usb_device_table!( /// USB_TABLE, -/// MODULE_USB_TABLE, /// <MyDriver as usb::Driver>::IdInfo, /// [ /// (usb::DeviceId::from_id(0x1234, 0x5678), ()),
samples/rust/rust_debugfs.rs +0 -1
@@ -110,7 +110,6 @@ fn from_str(s: &str) -> Result<Self> { kernel::acpi_device_table!(ACPI_TABLE , -MODULE_ACPI_TABLE , <RustDebugFs as platform::Driver >::IdInfo , [(acpi::DeviceId ::new (c"LNUXBEEF" ), ())] );
samples/rust/rust_dma.rs +0 -1
@@ -51,7 +51,6 @@ unsafe impl kernel::transmute::FromBytes for MyStruct {} kernel::pci_device_table!(PCI_TABLE , -MODULE_PCI_TABLE , <DmaSampleDriver as pci::Driver >::IdInfo , [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] );
samples/rust/rust_driver_auxiliary.rs +0 -2
@@ -24,7 +24,6 @@ kernel::auxiliary_device_table!(AUX_TABLE , -MODULE_AUX_TABLE , <AuxiliaryDriver as auxiliary::Driver >::IdInfo , [(auxiliary::DeviceId ::new (MODULE_NAME ,AUXILIARY_NAME ), ())] );
@@ -66,7 +65,6 @@ struct ParentData<'bound> { kernel::pci_device_table!(PCI_TABLE , -MODULE_PCI_TABLE , <ParentDriver as pci::Driver >::IdInfo , [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] );
samples/rust/rust_driver_i2c.rs +0 -3
@@ -14,21 +14,18 @@ kernel::acpi_device_table! {ACPI_TABLE , -MODULE_ACPI_TABLE , <SampleDriver as i2c::Driver >::IdInfo , [(acpi::DeviceId ::new (c"LNUXBEEF" ),0 )] } kernel::i2c_device_table! {I2C_TABLE , -MODULE_I2C_TABLE , <SampleDriver as i2c::Driver >::IdInfo , [(i2c::DeviceId ::new (c"rust_driver_i2c" ),0 )] } kernel::of_device_table! {OF_TABLE , -MODULE_OF_TABLE , <SampleDriver as i2c::Driver >::IdInfo , [(of::DeviceId ::new (c"test,rust_driver_i2c" ),0 )] }
samples/rust/rust_driver_pci.rs +0 -1
@@ -74,7 +74,6 @@ struct SampleDriverData<'bound> { kernel::pci_device_table!( PCI_TABLE, - MODULE_PCI_TABLE, <SampleDriver as pci::Driver>::IdInfo, [( pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5),
samples/rust/rust_driver_platform.rs +0 -2
@@ -87,14 +87,12 @@ struct SampleDriver { kernel::of_device_table!(OF_TABLE , -MODULE_OF_TABLE , <SampleDriver as platform::Driver >::IdInfo , [(of::DeviceId ::new (c"test,rust-device" ),Info (42 ))] ); kernel::acpi_device_table!(ACPI_TABLE , -MODULE_ACPI_TABLE , <SampleDriver as platform::Driver >::IdInfo , [(acpi::DeviceId ::new (c"LNUXBEEF" ),Info (0 ))] );
samples/rust/rust_driver_usb.rs +0 -1
@@ -19,7 +19,6 @@ struct SampleDriver { kernel::usb_device_table!(USB_TABLE , -MODULE_USB_TABLE , <SampleDriver as usb::Driver >::IdInfo , [(usb::DeviceId ::from_id (0x1234 ,0x5678 ), ()),] );
samples/rust/rust_i2c_client.rs +0 -2
@@ -87,14 +87,12 @@ struct SampleDriver { kernel::of_device_table!(OF_TABLE , -MODULE_OF_TABLE , <SampleDriver as platform::Driver >::IdInfo , [(of::DeviceId ::new (c"test,rust-device" ), ())] ); kernel::acpi_device_table!(ACPI_TABLE , -MODULE_ACPI_TABLE , <SampleDriver as platform::Driver >::IdInfo , [(acpi::DeviceId ::new (c"LNUXBEEF" ), ())] );
samples/rust/rust_soc.rs +0 -2
@@ -23,14 +23,12 @@ struct SampleSocDriver { kernel::of_device_table!(OF_TABLE , -MODULE_OF_TABLE , <SampleSocDriver as platform::Driver >::IdInfo , [(of::DeviceId ::new (c"test,rust-device" ), ())] ); kernel::acpi_device_table!(ACPI_TABLE , -MODULE_ACPI_TABLE , <SampleSocDriver as platform::Driver >::IdInfo , [(acpi::DeviceId ::new (c"LNUXBEEF" ), ())] );
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 07/11] rust: driver: remove `$module_table_name` from `module_device_table`
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
Wrap the generated code in a
const _: ()block to avoid symbol conflict. This removes the need of creating a new identifier.Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Gary Guo
reviewerPATCH v2 8/11 rust: driver: store pointers in `DeviceId`
In reply to Gary GuoThe common practice in C drivers is to store pointers into driver_data field of device IDs. The Rust code is however currently storing indices into the fields and then carry a side table that maps the index to pointers.
It is much simpler to just have DeviceId carry the pointer like C code does. However, just doing so naively would cause a "pointers cannot be cast to integers during const eval" error, as kernel_ulong_t does not have provenance while pointers do, and Rust forbids expose_provenance during consteval.
Work around this limitation by wrapping raw IDs in MaybeUninit. MaybeUninit is allowed to host arbitrary bytes with or without provenance, so we can just then use unsafe to store a pointer with provenance there. This has the same effect as changing the C-side definition to use void* instead of kernel_ulong_t, but without actually changing the C side.
--- rust/kernel/acpi.rs | 4 --- rust/kernel/auxiliary.rs | 8 ++--- rust/kernel/device_id.rs | 88 +++++++++++++++++++++++++++++------------------- rust/kernel/driver.rs | 14 ++++---- rust/kernel/i2c.rs | 7 ++-- rust/kernel/of.rs | 4 --- rust/kernel/pci.rs | 12 +++---- rust/kernel/usb.rs | 11 +++--- 8 files changed, 77 insertions(+), 71 deletions(-)
rust/kernel/acpi.rs +0 -4
@@ -25,10 +25,6 @@ unsafe impl RawDeviceId for DeviceId {// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. unsafe impl RawDeviceIdIndex for DeviceId {const DRIVER_DATA_OFFSET :usize = core::mem::offset_of!(bindings::acpi_device_id, driver_data); - -fn index (&self) ->usize { - self.0 .driver_data - } }impl DeviceId {
rust/kernel/auxiliary.rs +3 -5
@@ -93,7 +93,9 @@ extern "C" fn probe_callback(// SAFETY: `DeviceId` is a `#[repr(transparent)`] wrapper of `struct auxiliary_device_id` // and does not add additional invariants, so it's safe to transmute. let id =unsafe { &*id.cast ::<DeviceId >() }; -let info =T ::ID_TABLE .info (id.index ()); + +// SAFETY: `id` comes from `T::ID_TABLE` which is of type `IdArray<_, T::IdInfo>`. +let info =unsafe { id.info_unchecked ::<T ::IdInfo >() };from_result (|| {let data =T ::probe (adev, info);
@@ -169,10 +171,6 @@ unsafe impl RawDeviceId for DeviceId {unsafe impl RawDeviceIdIndex for DeviceId {const DRIVER_DATA_OFFSET :usize = core::mem::offset_of!(bindings::auxiliary_device_id, driver_data); - -fn index (&self) ->usize { - self.0 .driver_data - } }/// IdTable type for auxiliary drivers.
rust/kernel/device_id.rs +54 -34
172 lines not on this page · raw message
rust/kernel/driver.rs +7 -7
@@ -107,6 +107,7 @@use crate ::{ acpi, device, + device_id::RawDeviceIdIndex , of, prelude::*, types::Opaque ,
@@ -350,7 +351,8 @@ fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {// and does not add additional invariants, so it's safe to transmute. let id =unsafe { &*raw_id.cast ::<acpi::DeviceId >() }; -Some (table.info (<acpi::DeviceId as crate ::device_id::RawDeviceIdIndex >::index (id))) +// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`. +Some (unsafe { id.info_unchecked ::<Self ::IdInfo >() }) } } }
@@ -381,9 +383,8 @@ fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {// and does not add additional invariants, so it's safe to transmute. let id =unsafe { &*raw_id.cast ::<of::DeviceId >() }; -return Some (table.info ( - <of::DeviceId as crate ::device_id::RawDeviceIdIndex >::index (id), - )); +// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`. +return Some (unsafe { id.info_unchecked ::<Self ::IdInfo >() }); } }
@@ -412,9 +413,8 @@ fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {// and does not add additional invariants, so it's safe to transmute. let id =unsafe { &*raw_id.cast ::<of::DeviceId >() }; -return Some (table.info ( - <of::DeviceId as crate ::device_id::RawDeviceIdIndex >::index (id), - )); +// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`. +return Some (unsafe { id.info_unchecked ::<Self ::IdInfo >() }); } }
rust/kernel/i2c.rs +2 -5
@@ -65,10 +65,6 @@ unsafe impl RawDeviceId for DeviceId {// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. unsafe impl RawDeviceIdIndex for DeviceId {const DRIVER_DATA_OFFSET :usize = core::mem::offset_of!(bindings::i2c_device_id, driver_data); - -fn index (&self) ->usize { - self.0 .driver_data - } }/// IdTable type for I2C
@@ -212,7 +208,8 @@ fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter>::Id// does not add additional invariants, so it's safe to transmute. let id =unsafe { &*raw_id.cast ::<DeviceId >() }; -Some (table.info (<DeviceId as RawDeviceIdIndex >::index (id))) +// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`. +Some (unsafe { id.info_unchecked ::<T ::IdInfo >() }) } }
rust/kernel/of.rs +0 -4
@@ -25,10 +25,6 @@ unsafe impl RawDeviceId for DeviceId {// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `data` field. unsafe impl RawDeviceIdIndex for DeviceId {const DRIVER_DATA_OFFSET :usize = core::mem::offset_of!(bindings::of_device_id, data); - -fn index (&self) ->usize { - self.0 .dataas usize - } }impl DeviceId {
rust/kernel/pci.rs +6 -6
@@ -110,10 +110,14 @@ extern "C" fn probe_callback(// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct pci_device_id` and // does not add additional invariants, so it's safe to transmute. let id =unsafe { &*id.cast ::<DeviceId >() }; -let info =T ::ID_TABLE .info (id.index ()); + +// SAFETY: `id` comes from `T::ID_TABLE` which is of type `IdArray<_, T::IdInfo>` or +// `pci_device_id_any` which has 0 as driver_data. It can also come from dynamic IDs, which +// will ensure that `driver_data` exists in `T::ID_TABLE`. +let info =unsafe { id.info_unchecked_opt ::<T ::IdInfo >() };from_result (|| { -let data =T ::probe (pdev,Some (info)); +let data =T ::probe (pdev, info); pdev.as_ref ().set_drvdata (data)?;Ok (0 )
@@ -233,10 +237,6 @@ unsafe impl RawDeviceId for DeviceId {// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. unsafe impl RawDeviceIdIndex for DeviceId {const DRIVER_DATA_OFFSET :usize = core::mem::offset_of!(bindings::pci_device_id, driver_data); - -fn index (&self) ->usize { - self.0 .driver_data - } }/// `IdTable` type for PCI.
rust/kernel/usb.rs +5 -6
@@ -89,8 +89,11 @@ extern "C" fn probe_callback(// does not add additional invariants, so it's safe to transmute. let id =unsafe { &*id.cast ::<DeviceId >() }; -let info =T ::ID_TABLE .info (id.index ()); -let data =T ::probe (intf, id,Some (info)); +// SAFETY: `id` comes from `T::ID_TABLE` which is of type `IdArray<_, T::IdInfo>`. It +// can also come from dynamic IDs, which will ensure that `driver_data` exists in +// `T::ID_TABLE` or is 0. +let info =unsafe { id.info_unchecked_opt ::<T ::IdInfo >() }; +let data =T ::probe (intf, id, info);let dev: &device::Device <device::CoreInternal <'_>> = intf.as_ref (); dev.set_drvdata (data)?;
@@ -242,10 +245,6 @@ unsafe impl RawDeviceId for DeviceId {// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_info` field. unsafe impl RawDeviceIdIndex for DeviceId {const DRIVER_DATA_OFFSET :usize = core::mem::offset_of!(bindings::usb_device_id, driver_info); - -fn index (&self) ->usize { - self.0 .driver_info - } }/// [`IdTable`](kernel::device_id::IdTable) type for USB.
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 08/11] rust: driver: store pointers in `DeviceId`
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Undefined Behavior (null reference creation) due to uninitialized driver_data in IdArray::new_without_index.
Pre-existing issues:
- [High] Use-After-Free in PCI and USB subsystem cores due to TOCTOU race condition when matching dynamic IDs. --
commit 6b9468ff67342f4c1fd6f7bf72f1fef7fa35aa2e
Author: Gary Guo <[email protected]>
rust: driver: store pointers in DeviceId
The common practice in C drivers is to store pointers into driver_data field of device IDs. The Rust code is however currently storing indices into the fields and then carry a side table that maps the index to pointers.
[ ... ]
Quoted 4 lines
rust/kernel/device_id.rs +0
-0
[ ... ]
rust/kernel/device_id.rs impl<T: RawDeviceId, const N: usize> IdArray<T, (), N> { · line 163
Quoted 6 lines
rust/kernel/device_id.rs +1
-1@@ -133,7 +160,7 @@ impl<T: RawDeviceId, const N: usize> IdArray<T, (), N> {/// If the device implements [`RawDeviceIdIndex`], consider using [`IdArray::new`] instead. pub const fn new_without_index (ids: [T ;N ]) ->Self {// SAFETY: `T` is layout-wise compatible with `T::RawType`, so is the array of them. -let raw_ids: [T ::RawType ;N ] =unsafe { core::mem::transmute_copy (&ids) }; +let raw_ids: [MaybeUninit <T ::RawType >;N ] =unsafe { core::mem::transmute_copy (&ids) };
[Severity: High]
If a driver uses the @none branch in macros like i2c_device_table! or auxiliary_device_table!, it expands to IdArray::new_without_index(). Does this leave the driver_data field uninitialized (0)?
When the device probes, the framework calls info_unchecked::<()>() on the matching ID, which unconditionally reads the 0 from driver_data and casts it to a &() reference:
device_id.rs:info_unchecked() {
unsafe {
core::ptr::from_ref(self)
.byte_add(Self::DRIVER_DATA_OFFSET)
.cast::<&U>()
.read()
}
}Will this cause Undefined Behavior by creating a null reference, even for zero-sized types like ()?
[ ... ]
Quoted 5 lines
rust/kernel/pci.rs +0
-0
rust/kernel/pci.rs extern "C" fn probe_callback( · line 117
Quoted 9 lines
rust/kernel/pci.rs +5
-1@@ -110,10 +110,14 @@ extern "C" fn probe_callback(// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct pci_device_id` and // does not add additional invariants, so it's safe to transmute. let id =unsafe { &*id.cast ::<DeviceId >() }; -let info =T ::ID_TABLE .info (id.index ()); + +// SAFETY: `id` comes from `T::ID_TABLE` which is of type `IdArray<_, T::IdInfo>` or +// `pci_device_id_any` which has 0 as driver_data. It can also come from dynamic IDs, which +// will ensure that `driver_data` exists in `T::ID_TABLE`. +let info =unsafe { id.info_unchecked_opt ::<T ::IdInfo >() };
[Severity: High]
This is a pre-existing issue, but does this dereference a potentially freed id pointer if there is a concurrent sysfs write to remove_id?
In the C PCI core, pci_match_device() searches the dynamic ID list under dynids.lock. When a match is found, it drops the lock and returns a raw pointer to the pci_device_id embedded within the dynid structure:
drivers/pci/pci-driver.c:pci_match_device() {
spin_lock(&drv->dynids.lock);
list_for_each_entry(dynid, &drv->dynids.list, node) {
if (pci_match_one_device(&dynid->id, dev)) {
found_id = &dynid->id;
break;
}
}
spin_unlock(&drv->dynids.lock);
if (found_id)
return found_id;
}If a concurrent sysfs write to remove_id occurs, it unlinks and frees the dynid:
drivers/pci/pci-driver.c:remove_id_store() {
list_del(&dynid->node);
kfree(dynid);
}Since the lock is released before pci_match_device() returns, could this leave the found_id pointer unprotected and lead to a Use-After-Free when the probe callback dereferences it here?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Gary Guo
reviewerPATCH v2 9/11 rust: driver: remove open-coded matching logic
In reply to Gary GuoWith device ID info now including pointers instead of indices, the open-coded ACPI/OF matching is no longer needed and can be replaced with device_get_match_data.
With the removal of open-coded matching, the exposed functions and helpers are also removed; this effectively reverts most of commit 2690d071584e ("rust: ACPI: fix missing match data for PRP0001").
--- MAINTAINERS | 1 - drivers/acpi/bus.c | 6 +-- include/acpi/acpi_bus.h | 11 ---- rust/helpers/acpi.c | 16 ------ rust/helpers/helpers.c | 1 - rust/kernel/driver.rs | 137 ++++-------------------------------------------- rust/kernel/i2c.rs | 6 ++- rust/kernel/platform.rs | 3 +- 8 files changed, 20 insertions(+), 161 deletions(-)
MAINTAINERS +0 -1
@@ -297,7 +297,6 @@ F: include/linux/acpi.h F: include/linux/fwnode.h F: include/linux/fw_table.h F: lib/fw_table.c -F: rust/helpers/acpi.c F: rust/kernel/acpi.rs F: tools/power/acpi/
drivers/acpi/bus.c +3 -3
@@ -898,9 +898,9 @@ const struct acpi_device *acpi_companion_match(const struct device *dev)* identifiers and a _DSD object with the "compatible" property, use that * property to match against the given list of identifiers. */ -bool acpi_of_match_device (const struct acpi_device *adev, -const struct of_device_id *of_match_table, -const struct of_device_id **of_id) +static bool acpi_of_match_device (const struct acpi_device *adev, +const struct of_device_id *of_match_table, +const struct of_device_id **of_id) {const union acpi_object *of_compatible, *obj;int i, nval;
include/acpi/acpi_bus.h +0 -11
@@ -189,10 +189,6 @@ struct acpi_driver {* ----------- */ -bool acpi_of_match_device (const struct acpi_device *adev, -const struct of_device_id *of_match_table, -const struct of_device_id **of_id); -/* Status (_STA) */ struct acpi_device_status {
@@ -1000,13 +996,6 @@ int acpi_scan_add_dep(acpi_handle handle, struct acpi_handle_list *dep_devices);u32 arch_acpi_add_auto_dep (acpi_handle handle);#else /* CONFIG_ACPI */ -static inline bool acpi_of_match_device (const struct acpi_device *adev, -const struct of_device_id *of_match_table, -const struct of_device_id **of_id) -{ -return false; -} -static inline int register_acpi_bus_type (void *bus) {return 0 ; }static inline int unregister_acpi_bus_type (void *bus) {return 0 ; }
rust/helpers/acpi.c deleted +0 -16
@@ -1,16 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -#include <linux/acpi.h> -#include <acpi/acpi_bus.h> - -__rust_helper bool rust_helper_acpi_of_match_device(const struct acpi_device *adev, -const struct of_device_id *of_match_table, -const struct of_device_id **of_id) -{ -return acpi_of_match_device (adev, of_match_table, of_id); -} - -__rust_helper struct acpi_device *rust_helper_to_acpi_device_node(struct fwnode_handle *fwnode) -{ -return to_acpi_device_node (fwnode); -}
rust/helpers/helpers.c +0 -1
@@ -38,7 +38,6 @@#define __rust_helper __always_inline#endif -#include "acpi.c" #include "atomic.c" #include "atomic_ext.c" #include "auxiliary.c"
rust/kernel/driver.rs +11 -126
161 lines not on this page · raw message
rust/kernel/i2c.rs +4 -2
@@ -149,8 +149,10 @@ extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_// INVARIANT: `idev` is valid for the duration of `probe_callback()`. let idev =unsafe { &*idev.cast ::<I2cClient <device::CoreInternal <'_>>>() }; -let info = -Self ::i2c_id_info (idev).or_else (|| <Self as driver::Adapter >::id_info (idev.as_ref ())); +let info =Self ::i2c_id_info (idev).or_else (|| { +// SAFETY: `idev` matched data is of type `Self::IdInfo`. +unsafe { <Self as driver::Adapter >::id_info (idev.as_ref ()) } + });from_result (|| {let data =T ::probe (idev, info);
rust/kernel/platform.rs +2 -1
@@ -100,7 +100,8 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff// // INVARIANT: `pdev` is valid for the duration of `probe_callback()`. let pdev =unsafe { &*pdev.cast ::<Device <device::CoreInternal <'_>>>() }; -let info = <Self as driver::Adapter >::id_info (pdev.as_ref ()); +// SAFETY: `pdev` matched data is of type `Self::IdInfo`. +let info =unsafe { <Self as driver::Adapter >::id_info (pdev.as_ref ()) };from_result (|| {let data =T ::probe (pdev, info);
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 09/11] rust: driver: remove open-coded matching logic
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Data race and NULL pointer dereference due to lockless access of `dev->driver` in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Data race and NULL pointer dereference due to lockless access of dev->driver in Adapter::id_info. --
commit 228e0cfe9ec1514c36d9085e1b3070934920f9ab
Author: Gary Guo <[email protected]>
rust: driver: remove open-coded matching logic
This commit replaces the open-coded ACPI and OF matching logic in the Rust driver abstractions with a single call to device_get_match_data(). It removes the acpi_of_match_device() helper and updates the Adapter::id_info() function to use the C API directly.
Quoted 4 lines
rust/kernel/driver.rs +0
-0
[ ... ]
Quoted 1 line
rust/kernel/driver.rs +0
-0@@ -325,117 +304,23 @@ pub trait Adapter {
[ ... ]
Quoted 1 line
- fn
id_info(dev: &device::Device)-> Option<&'staticSelf::IdInfo> {
[ ... ]
rust/kernel/driver.rs pub trait Adapter { · line 321
Quoted 12 lines
rust/kernel/driver.rs +12
-0+/// Returns the driver's private data from the matching entry of any of the ID tables, if any. +/// +/// If this returns `None`, it means that there is no match in any of the ID tables directly +/// associated with a [`device::Device`]. +/// +/// # Safety +/// +/// The caller must ensure that the `dev` matched data is of type `Self::IdInfo`. +#[inline] +unsafe fn id_info (dev: &device::Device ) ->Option <&'staticSelf ::IdInfo > { +// SAFETY: `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`. +let data =unsafe { bindings::device_get_match_data (dev.as_raw ()) };
[Severity: High]
Does this introduce a potential NULL pointer dereference if dev->driver is not set?
Previously, id_info() performed a table lookup that did not depend on the device being bound to a driver. By switching to device_get_match_data(), the underlying C implementations (such as of_device_get_match_data() or acpi_device_get_match_data()) will unconditionally dereference dev->driver.
If a Rust driver calls Adapter::id_info() on a device::Device that is not currently bound (for example, from a concurrent sysfs attribute or a workqueue), could this lead to a panic?
Should this lifecycle precondition (that the device must be bound to a driver) be explicitly documented in the # Safety contract to prevent unsoundness, or should we ensure dev->driver is valid before calling this function?
Quoted 5 lines
rust/kernel/driver.rs +3
-0+ +// SAFETY: Per safety requirement, `data` is of type `Self::IdInfo`. +unsafe { data.cast ::<Self ::IdInfo >().as_ref () } } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Gary Guo
reviewerPATCH v2 10/11 rust: driver: remove duplicate ID table
In reply to Gary GuoPreviously, IdArray contains both device ID table and info table so we keep a separate copy for MODULE_DEVICE_TABLE for hotplug (which needs to be just the device ID table). With the info being changed to be carried via pointers, IdArray is now layout compatible with raw ID table and hence there is no longer a need to keep the distinction.
Deduplicate the code, and remove the redundant copy for hotplug purpose by just giving the IdArray instance a proper symbol name.
While at it, also update the macro to use ::core::line!() instead of just line!().
--- rust/kernel/device_id.rs | 76 +++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 49 deletions(-)
rust/kernel/device_id.rs +27 -49
@@ -86,28 +86,23 @@ unsafe fn info_unchecked_opt<U>(&self) -> Option<&'static U> { } } -/// A zero-terminated device id array. +/// A zero-terminated device id array, followed by context data. #[repr( C )] -pub struct RawIdArray <T :RawDeviceId ,const N :usize > { +pub struct IdArray <T :RawDeviceId ,U : 'static,const N :usize > {// This is `MaybeUninit<T::RawType>` so any bytes inside it can carry provenance in CTFE. // If this were `T::RawType`, integer fields would not be able to contain pointers. ids: [MaybeUninit <T ::RawType >;N ], sentinel:MaybeUninit <T ::RawType >, + phantom:PhantomData <&'staticU >, } -impl <T :RawDeviceId ,const N :usize >RawIdArray <T ,N > { -#[doc(hidden)] -pub const fn size (&self) ->usize { - core::mem::size_of ::<Self >() - } -} +// SAFETY: device ID is plain data plus a `&'static U` and can thus be sent between threads safely +// if `&U` can. +unsafe impl <T :RawDeviceId ,U :Sync + 'static,const N :usize >Send for IdArray <T ,U ,N > {} -/// A zero-terminated device id array, followed by context data. -#[repr( C )] -pub struct IdArray <T :RawDeviceId ,U : 'static,const N :usize > { - raw_ids:RawIdArray <T ,N >, - phantom:PhantomData <&'staticU >, -} +// SAFETY: device ID is plain data plus a `&'static U` and can thus be shared between threads safely +// if `&U` can. +unsafe impl <T :RawDeviceId ,U :Sync + 'static,const N :usize >Sync for IdArray <T ,U ,N > {}impl <T :RawDeviceId +RawDeviceIdIndex ,U : 'static,const N :usize >IdArray <T ,U ,N > {/// Creates a new instance of the array.
@@ -137,22 +132,13 @@ impl<T: RawDeviceId + RawDeviceIdIndex, U: 'static, const N: usize> IdArray<T, U core::mem::forget (ids);Self { - raw_ids:RawIdArray { - ids: raw_ids, - sentinel:MaybeUninit ::zeroed (), - }, + ids: raw_ids, + sentinel:MaybeUninit ::zeroed (), phantom:PhantomData , } } } -impl <T :RawDeviceId ,U : 'static,const N :usize >IdArray <T ,U ,N > { -/// Reference to the contained [`RawIdArray`]. -pub const fn raw_ids (&self) -> &RawIdArray <T ,N > { - &self.raw_ids - } -} -impl <T :RawDeviceId ,const N :usize >IdArray <T , (),N > {/// Creates a new instance of the array without writing index values. ///
@@ -164,10 +150,8 @@ impl<T: RawDeviceId, const N: usize> IdArray<T, (), N> { core::mem::forget (ids);Self { - raw_ids:RawIdArray { - ids: raw_ids, - sentinel:MaybeUninit ::zeroed (), - }, + ids: raw_ids, + sentinel:MaybeUninit ::zeroed (), phantom:PhantomData , } }
@@ -200,13 +184,17 @@ macro_rules! module_device_table { $table_name: ident, $id_info_type: ty, [$(($id: expr, $info:expr $(,)?)),* $(,)?] ) => { - const $table_name: $crate::device_id::IdArray< +#[export_name = +concat! ( "__mod_device_table__" , ::core::line!(), +"__kmod_" , module_path! (), +"__" , $table_type, +"__" , stringify! ($table_name)) +] + static $table_name: $crate::device_id::IdArray< $device_id_ty, $id_info_type, { <[$device_id_ty]>::len(&[$($id,)*]) }, > = $crate::device_id::IdArray::new([$(($id, &$info),)*]); - - $crate::module_device_table!($table_type, $table_name); };// Case for no ID info.
@@ -215,26 +203,16 @@ macro_rules! module_device_table { $table_name: ident, @none, [$($id: expr),* $(,)?] ) => { - const $table_name: $crate::device_id::IdArray< +#[export_name = +concat! ( "__mod_device_table__" , ::core::line!(), +"__kmod_" , module_path! (), +"__" , $table_type, +"__" , stringify! ($table_name)) +] + static $table_name: $crate::device_id::IdArray< $device_id_ty, (), { <[$device_id_ty]>::len(&[$($id,)*]) }, > = $crate::device_id::IdArray::new_without_index([$($id),*]); - - $crate::module_device_table!($table_type, $table_name); - }; - - ($table_type: literal, $table_name:ident) => { -const _: () = { -#[rustfmt::skip] -#[export_name = -concat! ( "__mod_device_table__" , line! (), -"__kmod_" , module_path! (), -"__" , $table_type, -"__" , stringify! ($table_name)) -] -static MOD_DEVICE_TABLE : [::core::mem::MaybeUninit <u8 >; $table_name.raw_ids ().size ()] = -unsafe { ::core::mem::transmute_copy ($table_name.raw_ids ()) }; - }; }; }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 10/11] rust: driver: remove duplicate ID table
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Previously,
IdArraycontains both device ID table and info table so we keep a separate copy forMODULE_DEVICE_TABLEfor hotplug (which needs to be just the device ID table). With the info being changed to be carried via pointers,IdArrayis now layout compatible with raw ID table and hence there is no longer a need to keep the distinction.
Quoted 7 lines
Deduplicate the code, and remove the redundant copy for hotplug purpose by just giving the
IdArrayinstance a proper symbol name.While at it, also update the macro to use
::core::line!()instead of justline!().Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Gary Guo
reviewerPATCH v2 11/11 RFC: rust: driver: support map-like syntax for ID table
In reply to Gary GuoThe device ID table and its associated info is really just a map. Add a syntax to module_device_table macro that reflects that.
--- rust/kernel/device_id.rs | 11 +++++++++++ samples/rust/rust_driver_pci.rs | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-)
rust/kernel/device_id.rs +11 -0
@@ -183,6 +183,17 @@ macro_rules! module_device_table { $table_type: literal, $device_id_ty: ty, $table_name: ident, $id_info_type: ty, [$(($id: expr, $info:expr $(,)?)),* $(,)?] + ) => { + $crate::module_device_table!( + $table_type, $device_id_ty, $table_name, $id_info_type, + {$($id=>$info,)*} + ); + }; + + ( + $table_type: literal, $device_id_ty: ty, + $table_name: ident, $id_info_type: ty, + {$($id: expr => $info:expr),* $(,)?} ) => { #[export_name = concat!("__mod_device_table__", ::core::line!(),
samples/rust/rust_driver_pci.rs +3 -4
@@ -75,10 +75,9 @@ struct SampleDriverData<'bound> { kernel::pci_device_table!(PCI_TABLE , <SampleDriver as pci::Driver >::IdInfo , - [( - pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), -TestIndex ::NO_EVENTFD - )] + { + pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ) =>TestIndex ::NO_EVENTFD , + } );impl SampleDriverData <'_> {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 11/11] RFC: rust: driver: support map-like syntax for ID table
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
The device ID table and its associated info is really just a map. Add a syntax to
module_device_tablemacro that reflects that.Signed-off-by Gary Guo <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Danilo Krummrich
maintainerRe: [PATCH v2 11/11] RFC: rust: driver: support map-like syntax for ID table
In reply to Gary GuoQuoted 28 lines · Gary Guo
rust/kernel/device_id.rs +11
-0@@ -183,6 +183,17 @@ macro_rules! module_device_table { $table_type: literal, $device_id_ty: ty, $table_name: ident, $id_info_type: ty, [$(($id: expr, $info:expr $(,)?)),* $(,)?] + ) => { + $crate::module_device_table!( + $table_type, $device_id_ty, $table_name, $id_info_type, + {$($id=>$info,)*} + ); + }; + + ( + $table_type: literal, $device_id_ty: ty, + $table_name: ident, $id_info_type: ty, + {$($id: expr => $info:expr),* $(,)?} ) => { #[export_name = concat!("__mod_device_table__", ::core::line!(),samples/rust/rust_driver_pci.rs +0
-0@@ -75,10 +75,9 @@ struct SampleDriverData<'bound> { kernel::pci_device_table!(
samples/rust/rust_driver_pci.rs struct SampleDriverData<'bound> { · line 81
Quoted 10 lines
samples/rust/rust_driver_pci.rs +3
-4PCI_TABLE, <SampleDriver as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), -TestIndex ::NO_EVENTFD - )] + { + pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5) => TestIndex::NO_EVENTFD, + } );
This LGTM, do you want to send a follow-up series?
Thanks,
Danilo
Danilo Krummrich
maintainerQuoted 22 lines · Gary Guo
MAINTAINERS | 1 - drivers/acpi/bus.c | 6 +- drivers/cpufreq/rcpufreq_dt.rs | 1 - drivers/gpu/drm/nova/driver.rs | 1 - drivers/gpu/drm/tyr/driver.rs | 1 - drivers/gpu/nova-core/driver.rs | 3 +- drivers/pwm/pwm_th1520.rs | 1 - include/acpi/acpi_bus.h | 11 -- rust/helpers/acpi.c | 16 --- rust/helpers/helpers.c | 1 - rust/kernel/acpi.rs | 14 +-- rust/kernel/auxiliary.rs | 18 +-- rust/kernel/device_id.rs | 207 +++++++++++++++++++--------------- rust/kernel/driver.rs | 137 ++-------------------- rust/kernel/i2c.rs | 26 ++--- rust/kernel/net/phy.rs | 66 +---------- rust/kernel/of.rs | 14 +-- rust/kernel/pci.rs | 25 ++-- rust/kernel/platform.rs | 5 +- rust/kernel/usb.rs | 24 ++-- samples/rust/rust_debugfs.rs | 1 - samples/rust/rust_dma.rs | 3 +-
Quoted 8 lines
samples/rust/rust_driver_auxiliary.rs | 4 +- samples/rust/rust_driver_i2c.rs | 3 - samples/rust/rust_driver_pci.rs | 11 +- samples/rust/rust_driver_platform.rs | 2 - samples/rust/rust_driver_usb.rs | 3 +- samples/rust/rust_i2c_client.rs | 2 - samples/rust/rust_soc.rs | 2 - 29 files changed, 178 insertions(+), 431 deletions(-)
I plan to pick this up soon. Please let me know in case there are any concerns from the acpi, i2c, of, net or usb side of things.
Thanks,
Danilo
Quoted 32 lines · Danilo Krummrich
Quoted 22 lines · Gary Guo
MAINTAINERS | 1 - drivers/acpi/bus.c | 6 +- drivers/cpufreq/rcpufreq_dt.rs | 1 - drivers/gpu/drm/nova/driver.rs | 1 - drivers/gpu/drm/tyr/driver.rs | 1 - drivers/gpu/nova-core/driver.rs | 3 +- drivers/pwm/pwm_th1520.rs | 1 - include/acpi/acpi_bus.h | 11 -- rust/helpers/acpi.c | 16 --- rust/helpers/helpers.c | 1 - rust/kernel/acpi.rs | 14 +-- rust/kernel/auxiliary.rs | 18 +-- rust/kernel/device_id.rs | 207 +++++++++++++++++++--------------- rust/kernel/driver.rs | 137 ++-------------------- rust/kernel/i2c.rs | 26 ++--- rust/kernel/net/phy.rs | 66 +---------- rust/kernel/of.rs | 14 +-- rust/kernel/pci.rs | 25 ++-- rust/kernel/platform.rs | 5 +- rust/kernel/usb.rs | 24 ++-- samples/rust/rust_debugfs.rs | 1 - samples/rust/rust_dma.rs | 3 +-Quoted 8 lines
samples/rust/rust_driver_auxiliary.rs | 4 +- samples/rust/rust_driver_i2c.rs | 3 - samples/rust/rust_driver_pci.rs | 11 +- samples/rust/rust_driver_platform.rs | 2 - samples/rust/rust_driver_usb.rs | 3 +- samples/rust/rust_i2c_client.rs | 2 - samples/rust/rust_soc.rs | 2 - 29 files changed, 178 insertions(+), 431 deletions(-)
Quoted 2 lines
I plan to pick this up soon. Please let me know in case there are any concerns from the acpi, i2c, of, net or usb side of things.
No objection from me fo rthe USB stuff:
Re: (subset) [PATCH v2 00/11] rust: driver: use pointers instead of indices for ID info
In reply to Gary GuoQuoted 1 line · Gary Guo
[PATCH v2 00/11] rust: driver: use pointers instead of indices for ID info
Applied, thanks!
Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
[1/11] rust: driver: remove IdTable::id
commit: 3474bbba4ddd
[2/11] rust: driver: simplify `IdArray::new_without_index`
commit: e49f626ef541
[3/11] rust: pci: use `Option<&IdInfo>` for device ID info
commit: 4fcbf7f1e4a4
[4/11] rust: usb: use `Option<&IdInfo>` for device ID info
commit: c7b6e07f83b4
[6/11] rust: driver: centralize device ID handling
commit: 1d92f2e8b881[7/11] rust: driver: remove $module_table_name from module_device_table
commit: 0deeb4222dd1
[ Consider the serdev code merged in the meantime. - Danilo ]
[8/11] rust: driver: store pointers in DeviceId
commit: 0b76335e8f32
[9/11] rust: driver: remove open-coded matching logic
commit: 2c5d17cdc4e9
[ Consider the serdev code merged in the meantime. - Danilo ]
[10/11] rust: driver: remove duplicate ID table
commit: 17fcdecb14b4
The patches will appear in the next linux-next integration (typically within 24 hours on weekdays).
The patches are in the driver-core-testing branch and will be promoted to driver-core-next after validation.
Igor Korotin
maintainerRe: (subset) [PATCH v2 00/11] rust: driver: use pointers instead of indices for ID info
Hi Danilo,
Sorry for the silence -- I was offline for personal reasons for about two weeks from the 16th, and missed this before it got applied.
Checked 6-9/11 now: the i2c bits are correct. Unlike PCI/USB, i2c has no dynamic-ID (new_id) mechanism, so i2c_match_id() can only ever return a pointer that IdArray::new() actually populated -- the plain info_unchecked() (not _opt) in i2c_id_info() is the right call, not an oversight.
Cheers
Igor
Re: (subset) [PATCH v2 00/11] rust: driver: use pointers instead of indices for ID info
In reply to Gary GuoQuoted 1 line · Gary Guo
[PATCH v2 00/11] rust: driver: use pointers instead of indices for ID info
Applied, thanks!
Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
[5/11] rust: net/phy: remove expansion from doc
commit: 928369abdd9b
The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).
The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.