Skip to content

Commit d88dd90

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 19f84d2 commit d88dd90

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
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
@@ -80,21 +80,27 @@ pub trait Module: Sized + Sync + Send {
8080
/// should do.
8181
///
8282
/// Equivalent to the `module_init` macro in the C API.
83-
fn init(module: &'static ThisModule) -> error::Result<Self>;
83+
fn init(name: &'static str::CStr, module: &'static ThisModule) -> error::Result<Self>;
8484
}
8585

8686
/// A module that is pinned and initialised in-place.
8787
pub trait InPlaceModule: Sync + Send {
8888
/// Creates an initialiser for the module.
8989
///
9090
/// It is called when the module is loaded.
91-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
91+
fn init(
92+
name: &'static str::CStr,
93+
module: &'static ThisModule,
94+
) -> impl init::PinInit<Self, error::Error>;
9295
}
9396

9497
impl<T: Module> InPlaceModule for T {
95-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
98+
fn init(
99+
name: &'static str::CStr,
100+
module: &'static ThisModule,
101+
) -> impl init::PinInit<Self, error::Error> {
96102
let initer = move |slot: *mut Self| {
97-
let m = <Self as Module>::init(module)?;
103+
let m = <Self as Module>::init(name, module)?;
98104

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

rust/kernel/net/phy.rs

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

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

rust/macros/module.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
332332
///
333333
/// This function must only be called once.
334334
unsafe fn __init() -> core::ffi::c_int {{
335-
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
335+
let initer = <{type_} as kernel::InPlaceModule>::init(kernel::c_str!(\"{name}\"),
336+
&super::super::THIS_MODULE);
336337
// SAFETY: No data race, since `__MOD` can only be accessed by this module
337338
// and there only `__init` and `__exit` access it. These functions are only
338339
// 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.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)