Skip to content

Commit acde67c

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 db53e97 commit acde67c

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

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

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

96102
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
97103
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
@@ -328,7 +328,8 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
328328
///
329329
/// This function must only be called once.
330330
unsafe fn __init() -> core::ffi::c_int {{
331-
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
331+
let initer = <{type_} as kernel::InPlaceModule>::init(kernel::c_str!(\"{name}\"),
332+
&super::super::THIS_MODULE);
332333
// SAFETY: No data race, since `__MOD` can only be accessed by this module
333334
// and there only `__init` and `__exit` access it. These functions are only
334335
// 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)