Skip to content

Commit df05774

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 6e14443 commit df05774

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

81+
/// A module that is pinned and initialised in-place.
82+
pub trait InPlaceModule: Sync + Send {
83+
/// Creates an initialiser for the module.
84+
///
85+
/// It is called when the module is loaded.
86+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
87+
}
88+
89+
impl<T: Module> InPlaceModule for T {
90+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
91+
let initer = move |slot: *mut Self| {
92+
let m = <Self as Module>::init(module)?;
93+
94+
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
95+
unsafe { slot.write(m) };
96+
Ok(())
97+
};
98+
99+
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
100+
unsafe { init::pin_init_from_closure(initer) }
101+
}
102+
}
103+
81104
/// Equivalent to `THIS_MODULE` in the C API.
82105
///
83106
/// 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
@@ -315,20 +316,13 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
315316
///
316317
/// This function must only be called once.
317318
unsafe fn __init() -> core::ffi::c_int {{
318-
match <{type_} as kernel::Module>::init(&super::super::THIS_MODULE) {{
319-
Ok(m) => {{
320-
// SAFETY: No data race, since `__MOD` can only be accessed by this
321-
// module and there only `__init` and `__exit` access it. These
322-
// functions are only called once and `__exit` cannot be called
323-
// before or during `__init`.
324-
unsafe {{
325-
__MOD = Some(m);
326-
}}
327-
return 0;
328-
}}
329-
Err(e) => {{
330-
return e.to_errno();
331-
}}
319+
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
320+
// SAFETY: No data race, since `__MOD` can only be accessed by this module
321+
// and there only `__init` and `__exit` access it. These functions are only
322+
// called once and `__exit` cannot be called before or during `__init`.
323+
match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
324+
Ok(m) => 0,
325+
Err(e) => e.to_errno(),
332326
}}
333327
}}
334328
@@ -343,7 +337,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
343337
// called once and `__init` was already called.
344338
unsafe {{
345339
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
346-
__MOD = None;
340+
__MOD.assume_init_drop();
347341
}}
348342
}}
349343

0 commit comments

Comments
 (0)