From: FUJITA Tomonori fujita.tomonori@gmail.com
[ Upstream commit 249c3a0e53acefc2b06d3b3e1fc28fb2081f878d ]
Place cleanup_module() in .exit.text section. Currently, cleanup_module() is likely placed in the .text section. It's inconsistent with the layout of C modules, where cleanup_module() is placed in .exit.text.
[ Boqun asked for an example of how the section changed to be put in the log. Tomonori provided the following examples:
C module:
$ objdump -t ~/build/x86/drivers/block/loop.o|grep clean 0000000000000000 l O .exit.data 0000000000000008 __UNIQUE_ID___addressable_cleanup_module412 0000000000000000 g F .exit.text 000000000000009c cleanup_module
Rust module without this patch:
$ objdump -t ~/build/x86/samples/rust/rust_minimal.o|grep clean 00000000000002b0 g F .text 00000000000000c6 cleanup_module 0000000000000000 g O .exit.data 0000000000000008 _R...___UNIQUE_ID___addressable_cleanup_module
Rust module with this patch:
$ objdump -t ~/build/x86/samples/rust/rust_minimal.o|grep clean 0000000000000000 g F .exit.text 00000000000000c6 cleanup_module 0000000000000000 g O .exit.data 0000000000000008 _R...___UNIQUE_ID___addressable_cleanup_module
- Miguel ]
Signed-off-by: FUJITA Tomonori fujita.tomonori@gmail.com Acked-by: Jarkko Sakkinen jarkko@kernel.org Link: https://lore.kernel.org/r/20250308044506.14458-1-fujita.tomonori@gmail.com Signed-off-by: Miguel Ojeda ojeda@kernel.org Signed-off-by: Sasha Levin sashal@kernel.org ---
**YES**
This commit should be backported to stable kernel trees.
**Analysis:**
This commit adds a single line `#[link_section = ".exit.text"]` to the `cleanup_module()` function in `rust/macros/module.rs`, making Rust modules consistent with C modules by placing cleanup code in the `.exit.text` section instead of the default `.text` section.
**Key factors supporting backporting:**
1. **Consistency fix**: The commit aligns Rust module behavior with established C module conventions. From examining `/home/sasha/linux/include/linux/init.h:56`, C modules use `#define __exit __section(".exit.text")` to place cleanup functions in `.exit.text`.
2. **Minimal and contained**: This is an extremely small change - adding just one line to specify the link section. The risk of regression is essentially zero.
3. **Follows established pattern**: This commit mirrors Similar Commit #1 which was marked "YES" for backporting. That commit placed `init_module()` in `.init.text` for consistency with C modules, and this commit does the same for `cleanup_module()` with `.exit.text`.
4. **Correctness improvement**: The current code places `cleanup_module()` in `.text` while the corresponding C code uses `.exit.text`. This inconsistency could affect tools that rely on standard kernel module section layouts.
5. **Low risk, clear benefit**: The change has no functional impact on module operation but improves kernel consistency and correctness. The commit message includes clear examples showing the section placement before and after the fix.
The commit follows the stable tree criteria of being an important correctness fix with minimal risk, similar to the approved Similar Commit #1 that addressed the same inconsistency for `init_module()`.
rust/macros/module.rs | 1 + 1 file changed, 1 insertion(+)
diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 3f462e71ff0ef..59739f625ce69 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -266,6 +266,7 @@ mod __module_init {{ #[cfg(MODULE)] #[doc(hidden)] #[no_mangle] + #[link_section = ".exit.text"] pub extern "C" fn cleanup_module() {{ // SAFETY: // - This function is inaccessible to the outside due to the double