Skip to content

Commit 806bb12

Browse files
author
Danilo Krummrich
committed
rust: pass module name to Module::init
In a subsequent patch we introduce the `Registration` abstraction used to register driver structures. Some subsystems require the module name on driver registration (e.g. PCI in __pci_register_driver()), hence pass the module name to `Module::init`. Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 40384c8 commit 806bb12

File tree

8 files changed

+27
-14
lines changed

8 files changed

+27
-14
lines changed

drivers/block/rnull.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct NullBlkModule {
3636
}
3737

3838
impl kernel::Module for NullBlkModule {
39-
fn init(_module: &'static ThisModule) -> Result<Self> {
39+
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
4040
pr_info!("Rust null_blk loaded\n");
4141
let tagset = Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?;
4242

rust/kernel/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,27 @@ pub trait Module: Sized + Sync + Send {
9090
/// should do.
9191
///
9292
/// Equivalent to the `module_init` macro in the C API.
93-
fn init(module: &'static ThisModule) -> error::Result<Self>;
93+
fn init(name: &'static str::CStr, module: &'static ThisModule) -> error::Result<Self>;
9494
}
9595

9696
/// A module that is pinned and initialised in-place.
9797
pub trait InPlaceModule: Sync + Send {
9898
/// Creates an initialiser for the module.
9999
///
100100
/// It is called when the module is loaded.
101-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
101+
fn init(
102+
name: &'static str::CStr,
103+
module: &'static ThisModule,
104+
) -> impl init::PinInit<Self, error::Error>;
102105
}
103106

104107
impl<T: Module> InPlaceModule for T {
105-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
108+
fn init(
109+
name: &'static str::CStr,
110+
module: &'static ThisModule,
111+
) -> impl init::PinInit<Self, error::Error> {
106112
let initer = move |slot: *mut Self| {
107-
let m = <Self as Module>::init(module)?;
113+
let m = <Self as Module>::init(name, module)?;
108114

109115
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
110116
unsafe { slot.write(m) };

rust/kernel/net/phy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ macro_rules! module_phy_driver {
903903
[$($crate::net::phy::create_phy_driver::<$driver>()),+];
904904

905905
impl $crate::Module for Module {
906-
fn init(module: &'static ThisModule) -> Result<Self> {
906+
fn init(_name: &'static CStr, module: &'static ThisModule) -> Result<Self> {
907907
// SAFETY: The anonymous constant guarantees that nobody else can access
908908
// the `DRIVERS` static. The array is used only in the C side.
909909
let drivers = unsafe { &mut DRIVERS };

rust/kernel/sync/lock/global.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<T: ?Sized, B: GlobalLockBackend> GlobalLockedBy<T, B> {
187187
/// ```
188188
/// # mod ex {
189189
/// # use kernel::prelude::*;
190+
/// # use kernel::str;
190191
/// kernel::sync::global_lock! {
191192
/// // SAFETY: Initialized in module initializer before first use.
192193
/// unsafe(uninit) static MY_COUNTER: Mutex<u32> = 0;
@@ -199,7 +200,7 @@ impl<T: ?Sized, B: GlobalLockBackend> GlobalLockedBy<T, B> {
199200
/// }
200201
///
201202
/// impl kernel::Module for MyModule {
202-
/// fn init(_module: &'static ThisModule) -> Result<Self> {
203+
/// fn init(_name: &'static str::CStr, _module: &'static ThisModule) -> Result<Self> {
203204
/// // SAFETY: Called exactly once.
204205
/// unsafe { MY_COUNTER.init() };
205206
///
@@ -216,6 +217,7 @@ impl<T: ?Sized, B: GlobalLockBackend> GlobalLockedBy<T, B> {
216217
/// # mod ex {
217218
/// # use kernel::prelude::*;
218219
/// use kernel::sync::{GlobalGuard, GlobalLockedBy};
220+
/// # use kernel::str;
219221
///
220222
/// kernel::sync::global_lock! {
221223
/// // SAFETY: Initialized in module initializer before first use.
@@ -239,7 +241,7 @@ impl<T: ?Sized, B: GlobalLockBackend> GlobalLockedBy<T, B> {
239241
/// }
240242
///
241243
/// impl kernel::Module for MyModule {
242-
/// fn init(_module: &'static ThisModule) -> Result<Self> {
244+
/// fn init(_name: &'static str::CStr, _module: &'static ThisModule) -> Result<Self> {
243245
/// // SAFETY: Called exactly once.
244246
/// unsafe { MY_MUTEX.init() };
245247
///

rust/macros/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use proc_macro::TokenStream;
3232
///
3333
/// ```
3434
/// use kernel::prelude::*;
35+
/// use kernel::str;
3536
///
3637
/// module!{
3738
/// type: MyModule,
@@ -45,7 +46,7 @@ use proc_macro::TokenStream;
4546
/// struct MyModule(i32);
4647
///
4748
/// impl kernel::Module for MyModule {
48-
/// fn init(_module: &'static ThisModule) -> Result<Self> {
49+
/// fn init(_name: &'static str::CStr, _module: &'static ThisModule) -> Result<Self> {
4950
/// let foo: i32 = 42;
5051
/// pr_info!("I contain: {}\n", foo);
5152
/// Ok(Self(foo))
@@ -65,6 +66,7 @@ use proc_macro::TokenStream;
6566
///
6667
/// ```
6768
/// use kernel::prelude::*;
69+
/// use kernel::str;
6870
///
6971
/// module!{
7072
/// type: MyDeviceDriverModule,
@@ -78,7 +80,7 @@ use proc_macro::TokenStream;
7880
/// struct MyDeviceDriverModule;
7981
///
8082
/// impl kernel::Module for MyDeviceDriverModule {
81-
/// fn init(_module: &'static ThisModule) -> Result<Self> {
83+
/// fn init(_name: &'static str::CStr, _module: &'static ThisModule) -> Result<Self> {
8284
/// Ok(Self)
8385
/// }
8486
/// }

rust/macros/module.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,11 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
333333
///
334334
/// This function must only be called once.
335335
unsafe fn __init() -> kernel::ffi::c_int {{
336-
let initer =
337-
<{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
336+
let initer = <{type_} as kernel::InPlaceModule>::init(
337+
kernel::c_str!(\"{name}\"),
338+
&super::super::THIS_MODULE
339+
);
340+
338341
// SAFETY: No data race, since `__MOD` can only be accessed by this module
339342
// and there only `__init` and `__exit` access it. These functions are only
340343
// called once and `__exit` cannot be called before or during `__init`.

samples/rust/rust_minimal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct RustMinimal {
1717
}
1818

1919
impl kernel::Module for RustMinimal {
20-
fn init(_module: &'static ThisModule) -> Result<Self> {
20+
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
2121
pr_info!("Rust minimal sample (init)\n");
2222
pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
2323

samples/rust/rust_print_main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn arc_print() -> Result {
4141
}
4242

4343
impl kernel::Module for RustPrint {
44-
fn init(_module: &'static ThisModule) -> Result<Self> {
44+
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
4545
pr_info!("Rust printing macros sample (init)\n");
4646

4747
pr_emerg!("Emergency message (level 0) without args\n");

0 commit comments

Comments
 (0)