Skip to content

Commit 19f84d2

Browse files
wedsonafDanilo Krummrich
authored andcommitted
rust: introduce InPlaceModule
This allows modules to be initialised in-place in pinned memory, which enables the usage of pinned types (e.g., mutexes, spinlocks, driver registrations, etc.) in modules without any extra allocations. Drivers that don't need this may continue to implement `Module` without any changes. Signed-off-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent da4a526 commit 19f84d2

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

rust/kernel/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ pub trait Module: Sized + Sync + Send {
8383
fn init(module: &'static ThisModule) -> error::Result<Self>;
8484
}
8585

86+
/// A module that is pinned and initialised in-place.
87+
pub trait InPlaceModule: Sync + Send {
88+
/// Creates an initialiser for the module.
89+
///
90+
/// It is called when the module is loaded.
91+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
92+
}
93+
94+
impl<T: Module> InPlaceModule for T {
95+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
96+
let initer = move |slot: *mut Self| {
97+
let m = <Self as Module>::init(module)?;
98+
99+
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
100+
unsafe { slot.write(m) };
101+
Ok(())
102+
};
103+
104+
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
105+
unsafe { init::pin_init_from_closure(initer) }
106+
}
107+
}
108+
86109
/// Equivalent to `THIS_MODULE` in the C API.
87110
///
88111
/// C header: [`include/linux/export.h`](srctree/include/linux/export.h)

rust/macros/module.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
232232
mod __module_init {{
233233
mod __module_init {{
234234
use super::super::{type_};
235+
use kernel::init::PinInit;
235236
236237
/// The \"Rust loadable module\" mark.
237238
//
@@ -242,7 +243,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
242243
#[used]
243244
static __IS_RUST_MODULE: () = ();
244245
245-
static mut __MOD: Option<{type_}> = None;
246+
static mut __MOD: core::mem::MaybeUninit<{type_}> = core::mem::MaybeUninit::uninit();
246247
247248
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
248249
/// # Safety
@@ -331,20 +332,13 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
331332
///
332333
/// This function must only be called once.
333334
unsafe fn __init() -> core::ffi::c_int {{
334-
match <{type_} as kernel::Module>::init(&super::super::THIS_MODULE) {{
335-
Ok(m) => {{
336-
// SAFETY: No data race, since `__MOD` can only be accessed by this
337-
// module and there only `__init` and `__exit` access it. These
338-
// functions are only called once and `__exit` cannot be called
339-
// before or during `__init`.
340-
unsafe {{
341-
__MOD = Some(m);
342-
}}
343-
return 0;
344-
}}
345-
Err(e) => {{
346-
return e.to_errno();
347-
}}
335+
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
336+
// SAFETY: No data race, since `__MOD` can only be accessed by this module
337+
// and there only `__init` and `__exit` access it. These functions are only
338+
// called once and `__exit` cannot be called before or during `__init`.
339+
match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
340+
Ok(m) => 0,
341+
Err(e) => e.to_errno(),
348342
}}
349343
}}
350344
@@ -359,7 +353,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
359353
// called once and `__init` was already called.
360354
unsafe {{
361355
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
362-
__MOD = None;
356+
__MOD.assume_init_drop();
363357
}}
364358
}}
365359

0 commit comments

Comments
 (0)