Previously, `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. Signed-off-by: Gary Guo --- rust/kernel/device_id.rs | 76 +++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/rust/kernel/device_id.rs b/rust/kernel/device_id.rs index 59453588df0e..26618bcda276 100644 --- a/rust/kernel/device_id.rs +++ b/rust/kernel/device_id.rs @@ -86,28 +86,23 @@ unsafe fn info_unchecked_opt(&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 { +pub struct IdArray { // This is `MaybeUninit` 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; N], sentinel: MaybeUninit, + phantom: PhantomData<&'static U>, } -impl RawIdArray { - #[doc(hidden)] - pub const fn size(&self) -> usize { - core::mem::size_of::() - } -} +// SAFETY: device ID is plain data plus a `&'static U` and can thus be sent between threads safely +// if `&U` can. +unsafe impl Send for IdArray {} -/// A zero-terminated device id array, followed by context data. -#[repr(C)] -pub struct IdArray { - raw_ids: RawIdArray, - phantom: PhantomData<&'static U>, -} +// SAFETY: device ID is plain data plus a `&'static U` and can thus be shared between threads safely +// if `&U` can. +unsafe impl Sync for IdArray {} impl IdArray { /// Creates a new instance of the array. @@ -137,22 +132,13 @@ impl IdArray IdArray { - /// Reference to the contained [`RawIdArray`]. - pub const fn raw_ids(&self) -> &RawIdArray { - &self.raw_ids - } -} - impl IdArray { /// Creates a new instance of the array without writing index values. /// @@ -164,10 +150,8 @@ impl IdArray { 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__", 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__", 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 TABLE: [::core::mem::MaybeUninit; $table_name.raw_ids().size()] = - unsafe { ::core::mem::transmute_copy($table_name.raw_ids()) }; - }; }; } -- 2.54.0