Skip to content

Commit 1b6170f

Browse files
bertschingertojeda
authored andcommitted
rust: module: place generated init_module() function in .init.text
Currently Rust kernel modules have their init code placed in the `.text` section of the .ko file. I don't think this causes any real problems for Rust modules as long as all code called during initialization lives in `.text`. However, if a Rust `init_module()` function (that lives in `.text`) calls a function marked with `__init` (in C) or `#[link_section = ".init.text"]` (in Rust), then a warning is generated by modpost because that function lives in `.init.text`. For example: WARNING: modpost: fs/bcachefs/bcachefs: section mismatch in reference: init_module+0x6 (section: .text) -> _RNvXCsj7d3tFpT5JS_15bcachefs_moduleNtB2_8BcachefsNtCsjDtqRIL3JAG_6kernel6Module4init (section: .init.text) I ran into this while experimenting with converting the bcachefs kernel module from C to Rust. The module's `init()`, written in Rust, calls C functions like `bch2_vfs_init()` which are placed in `.init.text`. This patch places the macro-generated `init_module()` Rust function in the `.init.text` section. It also marks `init_module()` as unsafe--now it may not be called after module initialization completes because it may be freed already. Note that this is not enough on its own to actually get all the module initialization code in that section. The module author must still add the `#[link_section = ".init.text"]` attribute to the Rust `init()` in the `impl kernel::Module` block in order to then call `__init` functions. However, this patch enables module authors do so, when previously it would not be possible (without warnings). Signed-off-by: Thomas Bertschinger <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Reworded title to add prefix. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 5bc8184 commit 1b6170f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

rust/macros/module.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,15 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
222222
}};
223223
224224
// Loadable modules need to export the `{{init,cleanup}}_module` identifiers.
225+
/// # Safety
226+
///
227+
/// This function must not be called after module initialization, because it may be
228+
/// freed after that completes.
225229
#[cfg(MODULE)]
226230
#[doc(hidden)]
227231
#[no_mangle]
228-
pub extern \"C\" fn init_module() -> core::ffi::c_int {{
232+
#[link_section = \".init.text\"]
233+
pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {{
229234
__init()
230235
}}
231236

0 commit comments

Comments
 (0)