Skip to contentSkip to conversation

rust: place generated init_module() function in .init.text

PATCH Superseded Thomas Bertschinger 3 participants 6 messages Feb 3, 2024 – Feb 5, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
index d62d8710d77a..d09be1bee397 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Thomas Bertschinger

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).

Quoted 3 lines

---
[...]

In resumi, to call init funcs in a module you must do it in .init.text. Amirite?

#2 lore raw
Quoted 2 lines · Martin Rodriguez Reboredo

In resumi, to call init funcs 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

reviewer
#3 lore raw
In reply to Thomas Bertschinger

rust/macros/module.rs pub(crate) fn module(ts: TokenStream) -> TokenStream { · line 231

Quoted 13 lines · Thomas Bertschinger
rust/macros/module.rs +1 -0
index d62d8710d77a..d09be1bee397 100644
@@ -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

#4 lore raw
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
index d62d8710d77a..d09be1bee397 100644
@@ -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

reviewer Reviewed-by
#5 lore raw
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
index d62d8710d77a..d09be1bee397 100644
@@ -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