Skip to content

Commit 9130dcb

Browse files
dakrDanilo Krummrich
authored andcommitted
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 df05774 commit 9130dcb

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

rust/kernel/lib.rs

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

8181
/// A module that is pinned and initialised in-place.
8282
pub trait InPlaceModule: Sync + Send {
8383
/// Creates an initialiser for the module.
8484
///
8585
/// It is called when the module is loaded.
86-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
86+
fn init(
87+
name: &'static str::CStr,
88+
module: &'static ThisModule,
89+
) -> impl init::PinInit<Self, error::Error>;
8790
}
8891

8992
impl<T: Module> InPlaceModule for T {
90-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
93+
fn init(
94+
name: &'static str::CStr,
95+
module: &'static ThisModule,
96+
) -> impl init::PinInit<Self, error::Error> {
9197
let initer = move |slot: *mut Self| {
92-
let m = <Self as Module>::init(module)?;
98+
let m = <Self as Module>::init(name, module)?;
9399

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

rust/kernel/net/phy.rs

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

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

rust/macros/module.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
316316
///
317317
/// This function must only be called once.
318318
unsafe fn __init() -> core::ffi::c_int {{
319-
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
319+
let initer = <{type_} as kernel::InPlaceModule>::init(kernel::c_str!(\"{name}\"),
320+
&super::super::THIS_MODULE);
320321
// SAFETY: No data race, since `__MOD` can only be accessed by this module
321322
// and there only `__init` and `__exit` access it. These functions are only
322323
// 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
@@ -40,7 +40,7 @@ fn arc_print() -> Result {
4040
}
4141

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

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

0 commit comments

Comments
 (0)