Skip to contentSkip to conversation

rust: put C helper exports in a dedicated namespace

PATCH Superseded Peder Bergan 3 participants 5 messages Nov 20 – Nov 22

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: put C helper exports in a dedicated namespace

The rust_helper_ prefix marks these C wrappers as Rust implementation details. Their exports use the default namespace, so GPL-compatible modules can link against them without an explicit import.

Export the wrappers in RUST_HELPERS so modpost can detect accidental dependencies at build time by flagging a missing namespace import. Have module! supply the import when the helpers are exported, since inlining and generic instantiation can introduce calls through ordinary Rust abstractions.

External modules that call these helpers without module! need to declare the import explicitly.

---
The two Rust sample modules loaded and unloaded under x86-64 QEMU with namespace enforcement enabled. Removing the import made modpost reject the sample. The exports and imports were also checked with helper inlining enabled and disabled.

OpenAI Codex assisted with coding and testing.

---
 rust/Makefile         | 4 +++-
 rust/exports.c        | 2 ++
 rust/macros/module.rs | 5 +++++
 3 files changed, 10 insertions(+), 1 deletion(-)
rust/Makefile +3 -1
index 0165626decbe7..17c559052e936 100644
@@ -569,9 +569,10 @@ $(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpe
 
 rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
 
+rust_export_macro = EXPORT_SYMBOL_RUST_GPL
 quiet_cmd_exports = EXPORTS $@
       cmd_exports = \
-	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
+	$(call rust_exports,$<,"$(rust_export_macro)(%s);\n") > $@
 
 $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
 	$(call if_changed,exports)
@@ -585,6 +586,7 @@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
 # function either. Therefore, we export all symbols from helpers and bindings.
 # In the future, this may be revisited to reduce the number of exports after
 # the compiler is informed about the places codegen is required.
+$(obj)/exports_helpers_generated.h: private rust_export_macro = EXPORT_SYMBOL_RUST_HELPER_GPL
 $(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
 	$(call if_changed,exports)
 
rust/exports.c +2 -0
index 1b52460b0f4ee..fde2a52d2a1ae 100644
@@ -14,6 +14,8 @@
 #include <linux/export.h>
 
 #define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)
+#define EXPORT_SYMBOL_RUST_HELPER_GPL(sym) \
+	extern int sym; EXPORT_SYMBOL_NS_GPL(sym, "RUST_HELPERS")
 
 #include "exports_core_generated.h"
 #include "exports_bindings_generated.h"
rust/macros/module.rs +5 -0
index 0a7b9e24c9600..ac3376fa8fb85 100644
@@ -467,6 +467,11 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {
         }
     }
 
+    // Inlined kernel code can introduce calls to C helpers in any Rust module.
+    if !cfg!(CONFIG_RUST_INLINE_HELPERS) {
+        modinfo.emit_only_loadable("import_ns", "RUST_HELPERS", false);
+    }
+
     // Built-in modules also export the `file` modinfo string.
     let file =
         std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");

---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261120-rust-helper-namespace-57e8c37ee7df

Best regards,

Signature · 1 line
Peder Bergan <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] rust: missing RUST_HELPERS namespace import in kasan_test module --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] rust: missing RUST_HELPERS namespace import in kasan_test module
--

commit 42a51ff367154ba548e8d27e3154503fd972371b
Author: Peder Bergan <[email protected]>

rust: put C helper exports in a dedicated namespace

This commit moves exported Rust C helpers into the RUST_HELPERS symbol namespace and makes the Rust module! macro import that namespace when helper inlining is disabled.

Quoted 6 lines
rust/macros/module.rs +0 -0
index 0a7b9e24c9600..ac3376fa8fb85 100644
@@ -467,6 +467,11 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {
         }

rust/macros/module.rs pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> { · line 477

Quoted 10 lines
rust/macros/module.rs +5 -0
     }
 
+    // Inlined kernel code can introduce calls to C helpers in any Rust module.
+    if !cfg!(CONFIG_RUST_INLINE_HELPERS) {
+        modinfo.emit_only_loadable("import_ns", "RUST_HELPERS", false);
+    }
+
     // Built-in modules also export the `file` modinfo string.
     let file =
         std::env::var("RUST_MODFILE").expect("Unable to fetch RUST_MODFILE environmental variable");

[Severity: Medium]
Does this break the build of kasan_test.ko when CONFIG_KASAN_KUNIT_TEST=m, CONFIG_RUST=y, and CONFIG_RUST_INLINE_HELPERS=n?

kasan_test is a composite C and Rust module (kasan_test_c.o and kasan_test_rust.o in mm/kasan/Makefile) that defines its module metadata in mm/kasan/kasan_test_c.c rather than with module!. kasan_test_rust_uaf() in mm/kasan/kasan_test_rust.rs uses KVec::push() and drops the vector, which inline into calls to rust_helper_krealloc_node_align. Because neither mm/kasan/kasan_test_c.c nor mm/kasan/kasan_test_rust.rs imports RUST_HELPERS, modpost rejects kasan_test.ko for using a symbol from RUST_HELPERS without importing the namespace.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Peder Bergan
Quoted 3 lines · On Fri, Nov 20, 2026 at 5:32 PM Peder Bergan via B4 Relay…

Nice, thanks for going through these old issues! :)

Quoted 1 line

+rust_export_macro = EXPORT_SYMBOL_RUST_GPL

Do we need a recursively expanded variable?

Quoted 1 line

+++ b/rust/exports.c

Conflicts ahead; this file happens to be going away with:

https://lore.kernel.org/rust-for-linux/[email protected]/

Cc'ing Giuliano.

Also, what Sashiko says seems reasonable: if we do it this way, then we may need manual MODULE_IMPORT_NS()s in some places, which may become a bit annoying, even if there aren't that many mixed language modules so far.

Actually, perhaps we could just import it in modpost if a Rust module has any Rust translation unit in it...

Cheers,
Miguel

#3 lore raw

Hi Miguel,

Thanks for the review. The export formats are literals, so I'll use := for both. V2 already added the namespace import to KASAN's C metadata.
https://patch.msgid.link/[email protected]

For v3, I'll build on Giuliano's assembly conversion and generate EXPORT_SYMBOL_NS_GPL() directly for the helper list.

1. Would you prefer I wait for Giuliano's series to land or send v3 with it listed as a prerequisite?
2. Should I keep the KASAN import for v3 or move automatic imports into modpost in this series?
3. If you prefer modpost, should it add the import only when a module contains a Rust object and references a RUST_HELPERS export?

Cheers,
Peder

Quoted 30 lines · Miguel Ojeda
Quoted 3 lines · On Fri, Nov 20, 2026 at 5:32 PM Peder Bergan via B4 Relay…

Nice, thanks for going through these old issues! :)

Quoted 1 line

+rust_export_macro = EXPORT_SYMBOL_RUST_GPL

Do we need a recursively expanded variable?

Quoted 1 line

+++ b/rust/exports.c

Conflicts ahead; this file happens to be going away with:

https://lore.kernel.org/rust-for-linux/[email protected]/

Cc'ing Giuliano.

Also, what Sashiko says seems reasonable: if we do it this way, then we may need manual MODULE_IMPORT_NS()s in some places, which may become a bit annoying, even if there aren't that many mixed language modules so far.

Actually, perhaps we could just import it in modpost if a Rust module has any Rust translation unit in it...

Cheers,
Miguel

#4 lore raw
Quoted 3 lines · On Sat, Nov 21, 2026 at 6:50 PM <[email protected]> wrote:

1. Would you prefer I wait for Giuliano's series to land or send v3 with it listed as a prerequisite?

Either way is fine, i.e. in general you can send patches that are conditional to other patches (within reason, of course).

Quoted 4 lines

2. Should I keep the KASAN import for v3 or move automatic imports into modpost in this series?
3. If you prefer modpost, should it add the import only when a module contains a Rust object and references a RUST_HELPERS export?

If the modpost approach (or similar) works, then I was thinking it may not need the manual imports, i.e. the idea is to avoid having to write those manual annotations if the automatic way can figure it out when to add it. But I didn't actually try it -- maybe I am missing something.

Thanks!

Cheers,
Miguel