Skip to content

Commit 30946f0

Browse files
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 e74d5d3 commit 30946f0

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
@@ -71,21 +71,27 @@ pub trait Module: Sized + Sync + Send {
7171
/// should do.
7272
///
7373
/// Equivalent to the `module_init` macro in the C API.
74-
fn init(module: &'static ThisModule) -> error::Result<Self>;
74+
fn init(name: &'static str::CStr, module: &'static ThisModule) -> error::Result<Self>;
7575
}
7676

7777
/// A module that is pinned and initialised in-place.
7878
pub trait InPlaceModule: Sync + Send {
7979
/// Creates an initialiser for the module.
8080
///
8181
/// It is called when the module is loaded.
82-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
82+
fn init(
83+
name: &'static str::CStr,
84+
module: &'static ThisModule,
85+
) -> impl init::PinInit<Self, error::Error>;
8386
}
8487

8588
impl<T: Module> InPlaceModule for T {
86-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
89+
fn init(
90+
name: &'static str::CStr,
91+
module: &'static ThisModule,
92+
) -> impl init::PinInit<Self, error::Error> {
8793
let initer = move |slot: *mut Self| {
88-
let m = <Self as Module>::init(module)?;
94+
let m = <Self as Module>::init(name, module)?;
8995

9096
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
9197
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
@@ -302,7 +302,8 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
302302
///
303303
/// This function must only be called once.
304304
unsafe fn __init() -> core::ffi::c_int {{
305-
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
305+
let initer = <{type_} as kernel::InPlaceModule>::init(kernel::c_str!(\"{name}\"),
306+
&super::super::THIS_MODULE);
306307
// SAFETY: No data race, since `__MOD` can only be accessed by this module
307308
// and there only `__init` and `__exit` access it. These functions are only
308309
// 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)