FROMGIT: kbuild: rust: disable function merging unconditionally
Function merging causes various different issues:
* For CONFIG_CFI there's an LLVM bug causing functions with different
CFI hashes to get merged. This can lead to crashes at runtime.
* For gendwarfksyms it causes functions with different CRCs to get
merged. This leads to broken MODVERSIONS information if the function
changes in the future so that it can no longer be merged.
* Wrong backtraces when a crash occurs in a merged function.
* Possibly also issues with pahole/BTF as the merged function's debug
info is lost.
These issues are new to Rust because in clang, function merging is off
by default, so these things have never come up before. Because of this,
turn this option off for all configurations.
[ The LLVM bug is tracked at [1]. Nick asked what function merging
refers to here, and Alice writes [2]:
No, this isn't linker ICF. rustc doesn't pass --icf, and in any case
kbuild invokes rustc with --emit=obj and does the final link itself, so
rustc's linker arguments never come into play.
What I'm referring to is LLVM's MergeFunctions IR pass
(llvm/lib/Transforms/IPO/MergeFunctions.cpp). It runs as part of the
optimization pipeline, within a single codegen unit, before any object
file is emitted. Clang only enables it with the cc1-only flag
-fmerge-functions, which the kernel never passes, so C code in the
kernel never sees it.
rustc, however, enables it by default whenever optimizing for speed or
size (opt-level 2, 3, s or z), which covers both kernel configurations.
It uses the "aliases" mode: when two functions have identical bodies,
one is deleted and its symbol becomes a GlobalAlias to the other. You
can turn it off with -Zmerge-functions=disabled.
- Miguel ]
Link: https://lore.kernel.org/all/avy_9SPRaPSKD8b4@google.com/
Fixes: ac61506bf2d1 ("rust: Use gendwarfksyms + extended modversions for CONFIG_MODVERSIONS")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Cc: stable@vger.kernel.org
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Sami Tolvanen <samitolvanen@google.com>
Link: https://github.com/llvm/llvm-project/issues/217629 [1]
Link: https://lore.kernel.org/rust-for-linux/CAH5fLgidhnPbP=xXQ3162w6q0U6rWHPZ9Mu6-E5va4_CXyanOg@mail.gmail.com/ [2]
Link: https://patch.msgid.link/20261118-rust-merge-functions-v1-1-fe86126522b9@google.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Bug: 584195065
(cherry picked from commit 73bdf8773de4af2c2b7f49370191ae6d5aeddea5
https://github.com/Rust-for-Linux/linux.git rust-fixes)
Change-Id: I1f162e1b07446d3f09ee0a25c1a4879d89905c2d
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
diff --git a/Makefile b/Makefile
index 04a4993..7d7c850a 100644
--- a/Makefile
+++ b/Makefile
@@ -629,6 +629,7 @@
-Csymbol-mangling-version=v0 \
-Crelocation-model=static \
-Zfunction-sections=n \
+ -Zmerge-functions=disabled \
-Wclippy::float_arithmetic
KBUILD_RUSTFLAGS_OPTION_CHKS :=
@@ -1149,8 +1150,7 @@
ifdef CONFIG_RUST
# Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects
# CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
- # Disable function merging as LLVM incorrectly merges functions with different KCFI types.
- RUSTC_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers -Zmerge-functions=disabled
+ RUSTC_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_CFI)
export RUSTC_FLAGS_CFI
endif