Now that ThisModule is trait we can use it for initializing. This way we
can also use it in const context. This does matter in later on.
---
drivers/android/binder/rust_binder_main.rs | 2 +-
drivers/block/rnull/configfs.rs | 2 +-
drivers/block/rnull/rnull.rs | 2 +-
rust/kernel/auxiliary.rs | 9 ++-------
rust/kernel/configfs.rs | 19 +++++++++----------
rust/kernel/driver.rs | 17 +++++------------
rust/kernel/drm/gem/mod.rs | 4 ++--
rust/kernel/i2c.rs | 8 ++------
rust/kernel/lib.rs | 8 ++++----
rust/kernel/net/phy.rs | 16 ++++++++--------
rust/kernel/pci.rs | 9 ++-------
rust/kernel/platform.rs | 9 ++-------
rust/kernel/usb.rs | 9 ++-------
samples/rust/rust_configfs.rs | 2 +-
samples/rust/rust_driver_auxiliary.rs | 6 +++---
samples/rust/rust_driver_faux.rs | 2 +-
samples/rust/rust_minimal.rs | 2 +-
samples/rust/rust_misc_device.rs | 2 +-
samples/rust/rust_print_main.rs | 2 +-
19 files changed, 49 insertions(+), 81 deletions(-)
diff --git a/drivers/android/binder/rust_binder_main.rs
b/drivers/android/binder/rust_binder_main.rs
index c79a9e742240..169fe552e32a 100644
--- a/drivers/android/binder/rust_binder_main.rs
+++ b/drivers/android/binder/rust_binder_main.rs
@@ -282,7 +282,7 @@ fn ptr_align(value: usize) -> Option<usize> {
struct BinderModule {}
impl kernel::Module for BinderModule {
- fn init(_module: &'static kernel::ThisModule) -> Result<Self> {
+ fn init<M: ThisModule>() -> Result<Self> {
// SAFETY: The module initializer never runs twice, so we only call
this once.
unsafe { crate::context::CONTEXTS.init() };
diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs.rs
index 6713a6d92391..a581c97219c7 100644
--- a/drivers/block/rnull/configfs.rs
+++ b/drivers/block/rnull/configfs.rs
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
-use super::{NullBlkDevice, THIS_MODULE};
+use super::NullBlkDevice;
use kernel::{
block::mq::gen_disk::{GenDisk, GenDiskBuilder},
c_str,
diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index a9d5e575a2c4..c9dff74489c1 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -36,7 +36,7 @@ struct NullBlkModule {
}
impl kernel::InPlaceModule for NullBlkModule {
- fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
+ fn init<M: ThisModule>() -> impl PinInit<Self, Error> {
pr_info!("Rust null_blk loaded\n");
try_pin_init!(Self {
diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
index 56f3c180e8f6..323074322505 100644
--- a/rust/kernel/auxiliary.rs
+++ b/rust/kernel/auxiliary.rs
@@ -12,7 +12,6 @@
error::{from_result, to_result, Result},
prelude::*,
types::Opaque,
- ThisModule,
};
use core::{
marker::PhantomData,
@@ -28,11 +27,7 @@
unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
type RegType = bindings::auxiliary_driver;
- unsafe fn register(
- adrv: &Opaque<Self::RegType>,
- name: &'static CStr,
- module: &'static ThisModule,
- ) -> Result {
+ unsafe fn register<M: ThisModule>(adrv: &Opaque<Self::RegType>, name:
&'static CStr) -> Result {
// SAFETY: It's safe to set the fields of `struct auxiliary_driver` on
initialization.
unsafe {
(*adrv.get()).name = name.as_char_ptr();
@@ -43,7 +38,7 @@ unsafe fn register(
// SAFETY: `adrv` is guaranteed to be a valid `RegType`.
to_result(unsafe {
- bindings::__auxiliary_driver_register(adrv.get(), module.0,
name.as_char_ptr())
+ bindings::__auxiliary_driver_register(adrv.get(),
M::OWNER.as_ptr(), name.as_char_ptr())
})
}
diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs
index 466fb7f40762..f6f0a32e06d1 100644
--- a/rust/kernel/configfs.rs
+++ b/rust/kernel/configfs.rs
@@ -744,17 +744,17 @@ macro_rules! impl_item_type {
($tpe:ty) => {
impl<Data> ItemType<$tpe, Data> {
#[doc(hidden)]
- pub const fn new_with_child_ctor<const N: usize, Child>(
- owner: &'static ThisModule,
+ pub const fn new_with_child_ctor<const N: usize, Child, M>(
attributes: &'static AttributeList<N, Data>,
) -> Self
where
Data: GroupOperations<Child = Child>,
Child: 'static,
+ M: ThisModule,
{
Self {
item_type: Opaque::new(bindings::config_item_type {
- ct_owner: owner.as_ptr(),
+ ct_owner: M::OWNER.as_ptr(),
ct_group_ops: GroupOperationsVTable::<Data,
Child>::vtable_ptr().cast_mut(),
ct_item_ops: ItemOperationsVTable::<$tpe,
Data>::vtable_ptr().cast_mut(),
ct_attrs:
core::ptr::from_ref(attributes).cast_mut().cast(),
@@ -765,13 +765,12 @@ pub const fn new_with_child_ctor<const N: usize, Child>(
}
#[doc(hidden)]
- pub const fn new<const N: usize>(
- owner: &'static ThisModule,
+ pub const fn new<const N: usize, M: ThisModule>(
attributes: &'static AttributeList<N, Data>,
) -> Self {
Self {
item_type: Opaque::new(bindings::config_item_type {
- ct_owner: owner.as_ptr(),
+ ct_owner: M::OWNER.as_ptr(),
ct_group_ops: core::ptr::null_mut(),
ct_item_ops: ItemOperationsVTable::<$tpe,
Data>::vtable_ptr().cast_mut(),
ct_attrs:
core::ptr::from_ref(attributes).cast_mut().cast(),
@@ -1019,8 +1018,8 @@ macro_rules! configfs_attrs {
const [<$no_child:upper>]: bool = true;
static [< $data:upper _TPE >] :
$crate::configfs::ItemType<$container, $data> =
- $crate::configfs::ItemType::<$container,
$data>::new::<N>(
- &THIS_MODULE, &[<$ data:upper _ATTRS >]
+ $crate::configfs::ItemType::<$container,
$data>::new::<N, crate::THIS_MODULE>(
+ &[<$ data:upper _ATTRS >]
);
)?
@@ -1028,8 +1027,8 @@ macro_rules! configfs_attrs {
static [< $data:upper _TPE >]:
$crate::configfs::ItemType<$container, $data> =
$crate::configfs::ItemType::<$container, $data>::
- new_with_child_ctor::<N, $child>(
- &THIS_MODULE, &[<$ data:upper _ATTRS >]
+ new_with_child_ctor::<N, $child,
crate::THIS_MODULE>(
+ &[<$ data:upper _ATTRS >]
);
)?
diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs
index 9beae2e3d57e..7c4ad24bb48a 100644
--- a/rust/kernel/driver.rs
+++ b/rust/kernel/driver.rs
@@ -118,11 +118,7 @@ pub unsafe trait RegistrationOps {
///
/// On success, `reg` must remain pinned and valid until the matching call
to
/// [`RegistrationOps::unregister`].
- unsafe fn register(
- reg: &Opaque<Self::RegType>,
- name: &'static CStr,
- module: &'static ThisModule,
- ) -> Result;
+ unsafe fn register<M: ThisModule>(reg: &Opaque<Self::RegType>, name:
&'static CStr) -> Result;
/// Unregisters a driver previously registered with
[`RegistrationOps::register`].
///
@@ -155,7 +151,7 @@ unsafe impl<T: RegistrationOps> Send for Registration<T> {}
impl<T: RegistrationOps> Registration<T> {
/// Creates a new instance of the registration object.
- pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl
PinInit<Self, Error> {
+ pub fn new<M: ThisModule>(name: &'static CStr) -> impl PinInit<Self,
Error> {
try_pin_init!(Self {
reg <- Opaque::try_ffi_init(|ptr: *mut T::RegType| {
// SAFETY: `try_ffi_init` guarantees that `ptr` is valid for
write.
@@ -166,7 +162,7 @@ pub fn new(name: &'static CStr, module: &'static
ThisModule) -> impl PinInit<Sel
let drv = unsafe { &*(ptr as *const Opaque<T::RegType>) };
// SAFETY: `drv` is guaranteed to be pinned until
`T::unregister`.
- unsafe { T::register(drv, name, module) }
+ unsafe { T::register::<M>(drv, name) }
}),
})
}
@@ -197,13 +193,10 @@ struct DriverModule {
}
impl $crate::InPlaceModule for DriverModule {
- fn init(
- module: &'static $crate::ThisModule
- ) -> impl ::pin_init::PinInit<Self, $crate::error::Error> {
+ fn init<M: ::kernel::ThisModule>() -> impl
::pin_init::PinInit<Self, $crate::error::Error> {
$crate::try_pin_init!(Self {
- _driver <- $crate::driver::Registration::new(
+ _driver <- $crate::driver::Registration::new::<M>(
<Self as $crate::ModuleMetadata>::NAME,
- module,
),
})
}
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index bdaac839dacc..1d9f78752946 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -292,10 +292,10 @@ impl<T: DriverObject> AllocImpl for Object<T> {
};
}
-pub(super) const fn create_fops() -> bindings::file_operations {
+pub(super) const fn create_fops<M: ThisModule>() -> bindings::file_operations {
let mut fops: bindings::file_operations = pin_init::zeroed();
- fops.owner = core::ptr::null_mut();
+ fops.owner = M::OWNER.as_ptr();
fops.open = Some(bindings::drm_open);
fops.release = Some(bindings::drm_release);
fops.unlocked_ioctl = Some(bindings::drm_ioctl);
diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index 491e6cc25cf4..bc154506b16f 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -97,11 +97,7 @@ macro_rules! i2c_device_table {
unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
type RegType = bindings::i2c_driver;
- unsafe fn register(
- idrv: &Opaque<Self::RegType>,
- name: &'static CStr,
- module: &'static ThisModule,
- ) -> Result {
+ unsafe fn register<M: ThisModule>(idrv: &Opaque<Self::RegType>, name:
&'static CStr) -> Result {
build_assert!(
T::ACPI_ID_TABLE.is_some() || T::OF_ID_TABLE.is_some() ||
T::I2C_ID_TABLE.is_some(),
"At least one of ACPI/OF/Legacy tables must be present when
registering an i2c driver"
@@ -134,7 +130,7 @@ unsafe fn register(
}
// SAFETY: `idrv` is guaranteed to be a valid `RegType`.
- to_result(unsafe { bindings::i2c_register_driver(module.0, idrv.get())
})
+ to_result(unsafe { bindings::i2c_register_driver(M::OWNER.as_ptr(),
idrv.get()) })
}
unsafe fn unregister(idrv: &Opaque<Self::RegType>) {
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 224410745734..6d4563662a02 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -173,7 +173,7 @@ pub trait Module: Sized + Sync + Send {
/// should do.
///
/// Equivalent to the `module_init` macro in the C API.
- fn init(module: &'static ThisModule) -> error::Result<Self>;
+ fn init<M: ThisModule>() -> error::Result<Self>;
}
/// A module that is pinned and initialised in-place.
@@ -181,13 +181,13 @@ pub trait InPlaceModule: Sync + Send {
/// Creates an initialiser for the module.
///
/// It is called when the module is loaded.
- fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self,
error::Error>;
+ fn init<M: ThisModule>() -> impl pin_init::PinInit<Self, error::Error>;
}
impl<T: Module> InPlaceModule for T {
- fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self,
error::Error> {
+ fn init<M: ThisModule>() -> impl pin_init::PinInit<Self, error::Error> {
let initer = move |slot: *mut Self| {
- let m = <Self as Module>::init(module)?;
+ let m = <Self as Module>::init::<M>()?;
// SAFETY: `slot` is valid for write per the contract with
`pin_init_from_closure`.
unsafe { slot.write(m) };
diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index bf6272d87a7b..5fc90d949d60 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -648,10 +648,7 @@ unsafe impl Send for Registration {}
impl Registration {
/// Registers a PHY driver.
- pub fn register(
- module: &'static crate::ThisModule,
- drivers: Pin<&'static mut [DriverVTable]>,
- ) -> Result<Self> {
+ pub fn register<M: ThisModule>(drivers: Pin<&'static mut [DriverVTable]>)
-> Result<Self> {
if drivers.is_empty() {
return Err(code::EINVAL);
}
@@ -659,7 +656,11 @@ pub fn register(
// the `drivers` slice are initialized properly. `drivers` will not be
moved.
// So it's just an FFI call.
to_result(unsafe {
- bindings::phy_drivers_register(drivers[0].0.get(),
drivers.len().try_into()?, module.0)
+ bindings::phy_drivers_register(
+ drivers[0].0.get(),
+ drivers.len().try_into()?,
+ M::OWNER.as_ptr(),
+ )
})?;
// INVARIANT: The `drivers` slice is successfully registered to the
kernel via `phy_drivers_register`.
Ok(Registration { drivers })
@@ -891,12 +892,11 @@ struct Module {
[$($crate::net::phy::create_phy_driver::<$driver>()),+];
impl $crate::Module for Module {
- fn init(module: &'static $crate::ThisModule) -> Result<Self> {
+ fn init<M: $crate::ThisModule>() -> Result<Self> {
// SAFETY: The anonymous constant guarantees that nobody
else can access
// the `DRIVERS` static. The array is used only in the C
side.
let drivers = unsafe { &mut DRIVERS };
- let mut reg = $crate::net::phy::Registration::register(
- module,
+ let mut reg =
$crate::net::phy::Registration::register::<M>(
::core::pin::Pin::static_mut(drivers),
)?;
Ok(Module { _reg: reg })
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 82e128431f08..792560ca8020 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -20,7 +20,6 @@
prelude::*,
str::CStr,
types::Opaque,
- ThisModule, //
};
use core::{
marker::PhantomData,
@@ -55,11 +54,7 @@
unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
type RegType = bindings::pci_driver;
- unsafe fn register(
- pdrv: &Opaque<Self::RegType>,
- name: &'static CStr,
- module: &'static ThisModule,
- ) -> Result {
+ unsafe fn register<M: ThisModule>(pdrv: &Opaque<Self::RegType>, name:
&'static CStr) -> Result {
// SAFETY: It's safe to set the fields of `struct pci_driver` on
initialization.
unsafe {
(*pdrv.get()).name = name.as_char_ptr();
@@ -70,7 +65,7 @@ unsafe fn register(
// SAFETY: `pdrv` is guaranteed to be a valid `RegType`.
to_result(unsafe {
- bindings::__pci_register_driver(pdrv.get(), module.0,
name.as_char_ptr())
+ bindings::__pci_register_driver(pdrv.get(), M::OWNER.as_ptr(),
name.as_char_ptr())
})
}
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index ed889f079cab..67d46231600e 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -14,7 +14,6 @@
of,
prelude::*,
types::Opaque,
- ThisModule,
};
use core::{
@@ -31,11 +30,7 @@
unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
type RegType = bindings::platform_driver;
- unsafe fn register(
- pdrv: &Opaque<Self::RegType>,
- name: &'static CStr,
- module: &'static ThisModule,
- ) -> Result {
+ unsafe fn register<M: ThisModule>(pdrv: &Opaque<Self::RegType>, name:
&'static CStr) -> Result {
let of_table = match T::OF_ID_TABLE {
Some(table) => table.as_ptr(),
None => core::ptr::null(),
@@ -56,7 +51,7 @@ unsafe fn register(
}
// SAFETY: `pdrv` is guaranteed to be a valid `RegType`.
- to_result(unsafe { bindings::__platform_driver_register(pdrv.get(),
module.0) })
+ to_result(unsafe { bindings::__platform_driver_register(pdrv.get(),
M::OWNER.as_ptr()) })
}
unsafe fn unregister(pdrv: &Opaque<Self::RegType>) {
diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
index d10b65e9fb6a..c6ee98d12875 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -13,7 +13,6 @@
prelude::*,
str::CStr,
types::{AlwaysRefCounted, Opaque},
- ThisModule,
};
use core::{
marker::PhantomData,
@@ -32,11 +31,7 @@
unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
type RegType = bindings::usb_driver;
- unsafe fn register(
- udrv: &Opaque<Self::RegType>,
- name: &'static CStr,
- module: &'static ThisModule,
- ) -> Result {
+ unsafe fn register<M: ThisModule>(udrv: &Opaque<Self::RegType>, name:
&'static CStr) -> Result {
// SAFETY: It's safe to set the fields of `struct usb_driver` on
initialization.
unsafe {
(*udrv.get()).name = name.as_char_ptr();
@@ -47,7 +42,7 @@ unsafe fn register(
// SAFETY: `udrv` is guaranteed to be a valid `RegType`.
to_result(unsafe {
- bindings::usb_register_driver(udrv.get(), module.0,
name.as_char_ptr())
+ bindings::usb_register_driver(udrv.get(), M::OWNER.as_ptr(),
name.as_char_ptr())
})
}
diff --git a/samples/rust/rust_configfs.rs b/samples/rust/rust_configfs.rs
index 0ccc7553ef39..858b2a45238d 100644
--- a/samples/rust/rust_configfs.rs
+++ b/samples/rust/rust_configfs.rs
@@ -42,7 +42,7 @@ fn new() -> impl PinInit<Self, Error> {
}
impl kernel::InPlaceModule for RustConfigfs {
- fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
+ fn init<M: ThisModule>() -> impl PinInit<Self, Error> {
pr_info!("Rust configfs sample (init)\n");
// Define a subsystem with the data type `Configuration`, two
diff --git a/samples/rust/rust_driver_auxiliary.rs
b/samples/rust/rust_driver_auxiliary.rs
index 5761ea314f44..e996dca19454 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -111,10 +111,10 @@ struct SampleModule {
}
impl InPlaceModule for SampleModule {
- fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, Error> {
+ fn init<M: ThisModule>() -> impl PinInit<Self, Error> {
try_pin_init!(Self {
- _pci_driver <- driver::Registration::new(MODULE_NAME, module),
- _aux_driver <- driver::Registration::new(MODULE_NAME, module),
+ _pci_driver <- driver::Registration::new::<M>(MODULE_NAME),
+ _aux_driver <- driver::Registration::new::<M>(MODULE_NAME),
})
}
}
diff --git a/samples/rust/rust_driver_faux.rs b/samples/rust/rust_driver_faux.rs
index ecc9fd378cbd..f66452c0390c 100644
--- a/samples/rust/rust_driver_faux.rs
+++ b/samples/rust/rust_driver_faux.rs
@@ -17,7 +17,7 @@ struct SampleModule {
}
impl Module for SampleModule {
- fn init(_module: &'static ThisModule) -> Result<Self> {
+ fn init<M: kernel::ThisModule>() -> Result<Self> {
pr_info!("Initialising Rust Faux Device Sample\n");
let reg = faux::Registration::new(c_str!("rust-faux-sample-device"),
None)?;
diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs
index 8eb9583571d7..c3696e69d67b 100644
--- a/samples/rust/rust_minimal.rs
+++ b/samples/rust/rust_minimal.rs
@@ -23,7 +23,7 @@ struct RustMinimal {
}
impl kernel::Module for RustMinimal {
- fn init(_module: &'static ThisModule) -> Result<Self> {
+ fn init<M: kernel::ThisModule>() -> Result<Self> {
pr_info!("Rust minimal sample (init)\n");
pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
pr_info!(
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index d69bc33dbd99..3f1acb6818a5 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -129,7 +129,7 @@ struct RustMiscDeviceModule {
}
impl kernel::InPlaceModule for RustMiscDeviceModule {
- fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
+ fn init<M: ThisModule>() -> impl PinInit<Self, Error> {
pr_info!("Initialising Rust Misc Device Sample\n");
let options = MiscDeviceOptions {
diff --git a/samples/rust/rust_print_main.rs b/samples/rust/rust_print_main.rs
index 4095c72afeab..b60ff0683a0e 100644
--- a/samples/rust/rust_print_main.rs
+++ b/samples/rust/rust_print_main.rs
@@ -59,7 +59,7 @@ fn arc_dyn_print(arc: &Arc<dyn Display>) {
}
impl kernel::Module for RustPrint {
- fn init(_module: &'static ThisModule) -> Result<Self> {
+ fn init<M: kernel::ThisModule>() -> Result<Self> {
pr_info!("Rust printing macros sample (init)\n");
pr_emerg!("Emergency message (level 0) without args\n");
--
2.43.0