Skip to contentSkip to conversation

rust: arch/um: Fix loading Rust modules

PATCH In review FUJITA Tomonori 3 participants 8 messages Oct 4 – Nov 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: arch/um: Fix loading Rust modules

From: FUJITA Tomonori <[email protected]>

Rust modules fail to load on x86_64 UML:

  # insmod rnull_mod.ko
  subarch: rnull_mod: Unknown rela relocation: 9
  insmod: can't insert 'rnull_mod.ko': invalid module format

arch/um/Makefile adds -Crelocation-model=pie to KBUILD_RUSTFLAGS. This comes after -Crelocation-model=static in the top-level Makefile, so all Rust code on UML, both built-in code and modules, is built as PIE. In PIE mode, rustc accesses statics in other crates through the GOT and emits R_X86_64_GOTPCREL (9) relocations. UML uses the x86 module loader, and it does not handle them.

Remove the flag. Rust code, both built-in code and modules, then uses the static relocation model, the same as C code.

Modules still fail to load with a larger mem=, such as mem=512M, because Rust code uses the small code model while C code uses -mcmodel=large. This is a separate problem.

Tested with clang 20 and rustc 1.90: vmlinux builds and boots, and rnull_mod.ko loads with mem=32M.

---
 arch/um/Makefile | 2 --
 1 file changed, 2 deletions(-)
arch/um/Makefile +0 -2
index 937639edc295..c4a99073f014 100644
@@ -62,8 +62,6 @@ KBUILD_CFLAGS += $(CFLAGS) $(CFLAGS-y) -D__arch_um__ \
 	-Din6addr_any=kernel_in6addr_any -Dstrrchr=kernel_strrchr \
 	-D__close_range=kernel__close_range
 
-KBUILD_RUSTFLAGS += -Crelocation-model=pie
-
 KBUILD_AFLAGS += $(ARCH_INCLUDE)
 
 USER_CFLAGS = $(patsubst $(KERNEL_DEFINES),,$(patsubst -I%,,$(KBUILD_CFLAGS))) \

base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264

Signature · 1 line
2.43.0
#1 lore raw
Quoted 2 lines · FUJITA Tomonori

arch/um/Makefile adds -Crelocation-model=pie to KBUILD_RUSTFLAGS. This

Hmm... That was intentionally added by David back in commit 905a77077573 ("rust: arch/um: Use 'pie' relocation mode under UML"):

UML expects a position independent executable for some reason, so tell rustc to generate pie objects. Otherwise we get a bunch of relocations we can't deal with in libcore.

So it seems David faced other relocation errors (?).

Cheers,
Miguel

#2 lore raw
Quoted 4 lines · On Mon, 12 Oct 2026 16:40:50 +0200 Miguel Ojeda…
Quoted 2 lines · FUJITA Tomonori

arch/um/Makefile adds -Crelocation-model=pie to KBUILD_RUSTFLAGS. This

Quoted 8 lines

Hmm... That was intentionally added by David back in commit 905a77077573 ("rust: arch/um: Use 'pie' relocation mode under UML"):

UML expects a position independent executable for some reason, so tell rustc to generate pie objects. Otherwise we get a bunch of relocations we can't deal with in libcore.

So it seems David faced other relocation errors (?).

I think that at that time, vmlinux was linked as PIE with clang, because -no-pie was not passed to clang. A static libcore cannot be linked into a PIE, so the errors probably came from that.

Since commit 846cfbeed09b ("um: Fix adding '-no-pie' for clang"), vmlinux is linked with -no-pie with clang too. So pie is no longer needed for Rust code, and it breaks modules.

#3 lore raw
In reply to FUJITA Tomonori

Hi, UML maintainers,

Quoted 47 lines · On Sun, 4 Oct 2026 21:12:30 +0900 FUJITA Tomonori <[email protected]> wrote:

From: FUJITA Tomonori <[email protected]>

Rust modules fail to load on x86_64 UML:

  # insmod rnull_mod.ko
  subarch: rnull_mod: Unknown rela relocation: 9
  insmod: can't insert 'rnull_mod.ko': invalid module format

arch/um/Makefile adds -Crelocation-model=pie to KBUILD_RUSTFLAGS. This comes after -Crelocation-model=static in the top-level Makefile, so all Rust code on UML, both built-in code and modules, is built as PIE. In PIE mode, rustc accesses statics in other crates through the GOT and emits R_X86_64_GOTPCREL (9) relocations. UML uses the x86 module loader, and it does not handle them.

Remove the flag. Rust code, both built-in code and modules, then uses the static relocation model, the same as C code.

Modules still fail to load with a larger mem=, such as mem=512M, because Rust code uses the small code model while C code uses -mcmodel=large. This is a separate problem.

Tested with clang 20 and rustc 1.90: vmlinux builds and boots, and rnull_mod.ko loads with mem=32M.

---
 arch/um/Makefile | 2 --
 1 file changed, 2 deletions(-)
arch/um/Makefile +0 -2
index 937639edc295..c4a99073f014 100644
@@ -62,8 +62,6 @@ KBUILD_CFLAGS += $(CFLAGS) $(CFLAGS-y) -D__arch_um__ \
 	-Din6addr_any=kernel_in6addr_any -Dstrrchr=kernel_strrchr \
 	-D__close_range=kernel__close_range
 
-KBUILD_RUSTFLAGS += -Crelocation-model=pie
-
 KBUILD_AFLAGS += $(ARCH_INCLUDE)
 
 USER_CFLAGS = $(patsubst $(KERNEL_DEFINES),,$(patsubst -I%,,$(KBUILD_CFLAGS))) \
Quoted 1 line

base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264

Gentle ping. The patch still applies cleanly on top of v7.4-rc1. If it looks fine, could you take it through the UML tree?

maintainer
#4 lore raw
Quoted 15 lines · FUJITA Tomonori
Quoted 15 lines
arch/um/Makefile +0 -0
index 937639edc295..c4a99073f014 100644
@@ -62,8 +62,6 @@ KBUILD_CFLAGS += $(CFLAGS) $(CFLAGS-y) -D__arch_um__ \
   	-Din6addr_any=kernel_in6addr_any -Dstrrchr=kernel_strrchr \

   -D__close_range=kernel__close_range

arch/um/Makefile +0 -2
-KBUILD_RUSTFLAGS += -Crelocation-model=pie
-

  KBUILD_AFLAGS += $(ARCH_INCLUDE)

  USER_CFLAGS = $(patsubst $(KERNEL_DEFINES),,$(patsubst -I%,,$(KBUILD_CFLAGS))) \

Quoted 4 lines
Quoted 1 line

base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264

Gentle ping. The patch still applies cleanly on top of v7.4-rc1. If it looks fine, could you take it through the UML tree?

I guess I figured with the "rust:" prefix it'd go through their tree, but either way during the merge window and all I wasn't really paying attention to UML.

I'll pick it up now.

johannes

#5 lore raw
Quoted 6 lines · Johannes Berg

I guess I figured with the "rust:" prefix it'd go through their tree, but either way during the merge window and all I wasn't really paying attention to UML.

I'll pick it up now.

Thanks!

(Yeah, I think the title should have been um: or um: rust: or similar. Personally, I check the list of files and the kind of change more than anything else for deciding trees, rather than the titles; maybe I should check those more).

Cheers,
Miguel

#6 lore raw
In reply to Johannes Berg
Quoted 1 line · On Tue, 03 Nov 2026 10:39:11 +0100 Johannes Berg <[email protected]>…

On Tue, 2026-11-03 at 18:02 +0900, FUJITA Tomonori wrote:

[...]

Quoted 8 lines
Quoted 2 lines

Gentle ping. The patch still applies cleanly on top of v7.4-rc1. If it looks fine, could you take it through the UML tree?

I guess I figured with the "rust:" prefix it'd go through their tree, but either way during the merge window and all I wasn't really paying attention to UML.

I'll pick it up now.

Thanks!

As the commit message says, Rust modules still fail to load with a larger mem=, because Rust code uses the small code model. Zigit Zo posted a fix for this in 2024 [1]. It is the same change as the one I would send:

	KBUILD_RUSTFLAGS += -Ccode-model=large

It was not merged. Was it because the patch has no Signed-off-by?

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

maintainer
#7 lore raw
Quoted 9 lines · FUJITA Tomonori

As the commit message says, Rust modules still fail to load with a larger mem=, because Rust code uses the small code model. Zigit Zo posted a fix for this in 2024 [1]. It is the same change as the one I would send:

	KBUILD_RUSTFLAGS += -Ccode-model=large

It was not merged. Was it because the patch has no Signed-off-by?

I have no idea, but I process enough patches (mostly in wireless though) that I almost certainly wouldn't remember one that just gets dropped on the floor because it doesn't have a Signed-off-by (or similar mechanical issues).

johannes