Skip to content

Commit e74d5d3

Browse files
wedsonafdakr
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 9c49161 commit e74d5d3

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

77+
/// A module that is pinned and initialised in-place.
78+
pub trait InPlaceModule: Sync + Send {
79+
/// Creates an initialiser for the module.
80+
///
81+
/// It is called when the module is loaded.
82+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
83+
}
84+
85+
impl<T: Module> InPlaceModule for T {
86+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
87+
let initer = move |slot: *mut Self| {
88+
let m = <Self as Module>::init(module)?;
89+
90+
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
91+
unsafe { slot.write(m) };
92+
Ok(())
93+
};
94+
95+
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
96+
unsafe { init::pin_init_from_closure(initer) }
97+
}
98+
}
99+
77100
/// Equivalent to `THIS_MODULE` in the C API.
78101
///
79102
/// 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
@@ -214,6 +214,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
214214
mod __module_init {{
215215
mod __module_init {{
216216
use super::super::{type_};
217+
use kernel::init::PinInit;
217218
218219
/// The \"Rust loadable module\" mark.
219220
//
@@ -224,7 +225,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
224225
#[used]
225226
static __IS_RUST_MODULE: () = ();
226227
227-
static mut __MOD: Option<{type_}> = None;
228+
static mut __MOD: core::mem::MaybeUninit<{type_}> = core::mem::MaybeUninit::uninit();
228229
229230
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
230231
/// # Safety
@@ -301,20 +302,13 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
301302
///
302303
/// This function must only be called once.
303304
unsafe fn __init() -> core::ffi::c_int {{
304-
match <{type_} as kernel::Module>::init(&super::super::THIS_MODULE) {{
305-
Ok(m) => {{
306-
// SAFETY: No data race, since `__MOD` can only be accessed by this
307-
// module and there only `__init` and `__exit` access it. These
308-
// functions are only called once and `__exit` cannot be called
309-
// before or during `__init`.
310-
unsafe {{
311-
__MOD = Some(m);
312-
}}
313-
return 0;
314-
}}
315-
Err(e) => {{
316-
return e.to_errno();
317-
}}
305+
let initer = <{type_} as kernel::InPlaceModule>::init(&super::super::THIS_MODULE);
306+
// SAFETY: No data race, since `__MOD` can only be accessed by this module
307+
// and there only `__init` and `__exit` access it. These functions are only
308+
// called once and `__exit` cannot be called before or during `__init`.
309+
match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
310+
Ok(m) => 0,
311+
Err(e) => e.to_errno(),
318312
}}
319313
}}
320314
@@ -329,7 +323,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
329323
// called once and `__init` was already called.
330324
unsafe {{
331325
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
332-
__MOD = None;
326+
__MOD.assume_init_drop();
333327
}}
334328
}}
335329

0 commit comments

Comments
 (0)