Skip to content

Commit db53e97

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]>
1 parent dfa8e7c commit db53e97

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
@@ -80,6 +80,29 @@ pub trait Module: Sized + Sync + Send {
8080
fn init(module: &'static ThisModule) -> error::Result<Self>;
8181
}
8282

83+
/// A module that is pinned and initialised in-place.
84+
pub trait InPlaceModule: Sync + Send {
85+
/// Creates an initialiser for the module.
86+
///
87+
/// It is called when the module is loaded.
88+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
89+
}
90+
91+
impl<T: Module> InPlaceModule for T {
92+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
93+
let initer = move |slot: *mut Self| {
94+
let m = <Self as Module>::init(module)?;
95+
96+
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
97+
unsafe { slot.write(m) };
98+
Ok(())
99+
};
100+
101+
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
102+
unsafe { init::pin_init_from_closure(initer) }
103+
}
104+
}
105+
83106
/// Equivalent to `THIS_MODULE` in the C API.
84107
///
85108
/// 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
@@ -228,6 +228,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
228228
mod __module_init {{
229229
mod __module_init {{
230230
use super::super::{type_};
231+
use kernel::init::PinInit;
231232
232233
/// The \"Rust loadable module\" mark.
233234
//
@@ -238,7 +239,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
238239
#[used]
239240
static __IS_RUST_MODULE: () = ();
240241
241-
static mut __MOD: Option<{type_}> = None;
242+
static mut __MOD: core::mem::MaybeUninit<{type_}> = core::mem::MaybeUninit::uninit();
242243
243244
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
244245
/// # Safety
@@ -327,20 +328,13 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
327328
///
328329
/// This function must only be called once.
329330
unsafe fn __init() -> core::ffi::c_int {{
330-
match <{type_} as kernel::Module>::init(&super::super::THIS_MODULE) {{
331-
Ok(m) => {{
332-
// SAFETY: No data race, since `__MOD` can only be accessed by this
333-
// module and there only `__init` and `__exit` access it. These
334-
// functions are only called once and `__exit` cannot be called
335-
// before or during `__init`.
336-
unsafe {{
337-
__MOD = Some(m);
338-
}}
339-
return 0;
340-
}}
341-
Err(e) => {{
342-
return e.to_errno();
343-
}}
331+
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
332+
// SAFETY: No data race, since `__MOD` can only be accessed by this module
333+
// and there only `__init` and `__exit` access it. These functions are only
334+
// called once and `__exit` cannot be called before or during `__init`.
335+
match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
336+
Ok(m) => 0,
337+
Err(e) => e.to_errno(),
344338
}}
345339
}}
346340
@@ -355,7 +349,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
355349
// called once and `__init` was already called.
356350
unsafe {{
357351
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
358-
__MOD = None;
352+
__MOD.assume_init_drop();
359353
}}
360354
}}
361355

0 commit comments

Comments
 (0)