Skip to content

Commit 484ec70

Browse files
committed
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 a3fe8d8 commit 484ec70

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

rust/kernel/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(dispatch_from_dyn)]
1818
#![feature(new_uninit)]
1919
#![feature(receiver_trait)]
20+
#![feature(return_position_impl_trait_in_trait)]
2021
#![feature(unsize)]
2122

2223
// Ensure conditional compilation based on the kernel configuration works;
@@ -60,7 +61,7 @@ const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
6061
/// The top level entrypoint to implementing a kernel module.
6162
///
6263
/// For any teardown or cleanup operations, your type may implement [`Drop`].
63-
pub trait Module: Sized + Sync {
64+
pub trait Module: Sized + Sync + Send {
6465
/// Called at module initialization time.
6566
///
6667
/// Use this method to perform whatever setup or registration your module
@@ -70,6 +71,29 @@ pub trait Module: Sized + Sync {
7071
fn init(module: &'static ThisModule) -> error::Result<Self>;
7172
}
7273

74+
/// A module that is pinned and initialised in-place.
75+
pub trait InPlaceModule: Sync + Send {
76+
/// Creates an initialiser for the module.
77+
///
78+
/// It is called when the module is loaded.
79+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
80+
}
81+
82+
impl<T: Module> InPlaceModule for T {
83+
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
84+
let initer = move |slot: *mut Self| {
85+
let m = <Self as Module>::init(module)?;
86+
87+
// SAFETY: `slot` is valid for write per the contract with `pin_init_from_closure`.
88+
unsafe { slot.write(m) };
89+
Ok(())
90+
};
91+
92+
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
93+
unsafe { init::pin_init_from_closure(initer) }
94+
}
95+
}
96+
7397
/// Equivalent to `THIS_MODULE` in the C API.
7498
///
7599
/// C header: `include/linux/export.h`

rust/macros/module.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
208208
#[used]
209209
static __IS_RUST_MODULE: () = ();
210210
211-
static mut __MOD: Option<{type_}> = None;
211+
static mut __MOD: core::mem::MaybeUninit<{type_}> = core::mem::MaybeUninit::uninit();
212212
213213
// SAFETY: `__this_module` is constructed by the kernel at load time and will not be
214214
// freed until the module is unloaded.
@@ -270,23 +270,17 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
270270
}}
271271
272272
fn __init() -> core::ffi::c_int {{
273-
match <{type_} as kernel::Module>::init(&THIS_MODULE) {{
274-
Ok(m) => {{
275-
unsafe {{
276-
__MOD = Some(m);
277-
}}
278-
return 0;
279-
}}
280-
Err(e) => {{
281-
return e.to_errno();
282-
}}
273+
let initer = <{type_} as kernel::InPlaceModule>::init(&THIS_MODULE);
274+
match unsafe {{ initer.__pinned_init(__MOD.as_mut_ptr()) }} {{
275+
Ok(m) => 0,
276+
Err(e) => e.to_errno(),
283277
}}
284278
}}
285279
286280
fn __exit() {{
287281
unsafe {{
288282
// Invokes `drop()` on `__MOD`, which should be used for cleanup.
289-
__MOD = None;
283+
__MOD.assume_init_drop();
290284
}}
291285
}}
292286

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ $(obj)/%.lst: $(src)/%.c FORCE
262262
# Compile Rust sources (.rs)
263263
# ---------------------------------------------------------------------------
264264

265-
rust_allowed_features := new_uninit
265+
rust_allowed_features := new_uninit,impl_trait_in_assoc_type
266266

267267
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
268268
# current working directory, which may be not accessible in the out-of-tree

0 commit comments

Comments
 (0)