rust: place generated init_module() function in .init.text
Subsystems (2)
PATCH 1/1 rust: 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 but it does present a challenge with implementing Rust functionality for existing C modules.
If a Rust init_module() function (that lives in .text) calls a C function annotated with __init, then a warning is generated because the C function lives in .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 the .init.text section.
This patch places the macro-generated init_module() Rust function in the .init.text section.
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 C __init functions. However, this patch enables module authors do so, when previously it would not be possible (without warnings).
--- rust/macros/module.rs | 1 + 1 file changed, 1 insertion(+)
rust/macros/module.rs +1 -0
@@ -225,6 +225,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {#[cfg( MODULE )] #[doc(hidden)] #[no_mangle] + #[link_section = \".init.text\"] pub extern \"C\" fn init_module() -> core::ffi::c_int {{ __init() }}
Signature · 1 line
2.43.0
Quoted 24 lines · Thomas Bertschinger
Currently Rust kernel modules have their init code placed in the
.textsection of the .ko file. I don't think this causes any real problems for Rust modules but it does present a challenge with implementing Rust functionality for existing C modules.If a Rust
init_module()function (that lives in.text) calls a C function annotated with__init, then a warning is generated because the C function lives in.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 likebch2_vfs_init()which are placed in the.init.textsection.This patch places the macro-generated
init_module()Rust function in the.init.textsection.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 Rustinit()in theimpl kernel::Moduleblock in order to then call C__initfunctions. However, this patch enables module authors do so, when previously it would not be possible (without warnings).
Quoted 3 lines
Signed-off-by Thomas Bertschinger <[email protected]>---
[...]
In resumi, to call init funcs in a module you must do it in .init.text. Amirite?
Quoted 2 lines · Martin Rodriguez Reboredo
In resumi, to call
initfuncs in a module you must do it in.init.text. Amirite?
Yeah, well, I'd say that's needed to do it without warnings from modpost at least.
In theory it "shouldn't" cause any problems to call __init functions from the Rust init() even if it's in .text, since that init() should only ever run once. But then if anything does run Rust's init() after module initialization, that will definitely cause problems. I don't think that should ever happen though...
- Thomas Bertschinger
Alice Ryhl
reviewerrust/macros/module.rs pub(crate) fn module(ts: TokenStream) -> TokenStream { · line 231
Quoted 13 lines · Thomas Bertschinger
rust/macros/module.rs +1
-0@@ -225,6 +225,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {#[cfg( MODULE )] #[doc(hidden)] #[no_mangle] + #[link_section = \".init.text\"] pub extern \"C\" fn init_module() -> core::ffi::c_int {{ __init() }}
Perhaps this function should be made unsafe? I don't think there's currently anything preventing safe non-init Rust code from calling it directly as-is.
Alice
Quoted 20 lines · Alice Ryhl
rust/macros/module.rs pub(crate) fn module(ts: TokenStream) -> TokenStream { · line 231
Quoted 13 lines · Thomas Bertschinger
rust/macros/module.rs +1
-0@@ -225,6 +225,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {#[cfg( MODULE )] #[doc(hidden)] #[no_mangle] + #[link_section = \".init.text\"] pub extern \"C\" fn init_module() -> core::ffi::c_int {{ __init() }}Perhaps this function should be made unsafe? I don't think there's currently anything preventing safe non-init Rust code from calling it directly as-is.
Quoted 1 line
Alice
That's a good point. That makes sense to me; I can work on a v2 that does this.
- Thomas Bertschinger
Quoted 22 lines · Thomas Bertschinger
Quoted 20 lines · Alice Ryhl
rust/macros/module.rs pub(crate) fn module(ts: TokenStream) -> TokenStream { · line 231
Quoted 13 lines · Thomas Bertschinger
rust/macros/module.rs +1
-0@@ -225,6 +225,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {#[cfg( MODULE )] #[doc(hidden)] #[no_mangle] + #[link_section = \".init.text\"] pub extern \"C\" fn init_module() -> core::ffi::c_int {{ __init() }}Perhaps this function should be made unsafe? I don't think there's currently anything preventing safe non-init Rust code from calling it directly as-is.
Quoted 4 lines
Quoted 1 line
Alice
That's a good point. That makes sense to me; I can work on a v2 that does this.
With that fixed, you may add