[PATCH v2] rust: enable fentry support

Paul Murphy posted 1 patch 1 month, 2 weeks ago
Makefile                        | 20 +++++++++++++++++++-
scripts/generate_rust_target.rs |  2 ++
2 files changed, 21 insertions(+), 1 deletion(-)
[PATCH v2] rust: enable fentry support
Posted by Paul Murphy 1 month, 2 weeks ago
Turn it on if CONFIG_HAVE_FENTRY=y and rustc supports a compatible
fentry option. Nightly rustc supports fentry, fentry-record, and
fentry-nop-record. These are equivalent gcc's -mfentry, -mfentry
-mrecord-mcount, and -mfentry -mnop-mcount -mrecord-mcount.

Rust 1.98 adds support for x86 and s390x targets for
-Zinstrument-mcount=fentry.

Rust nightly supports all three options on s390. x86 supports only
fentry (this is a limitation of the x86 llvm backend). The expanded
options should arrive with Rust 1.99.

Signed-off-by: Paul Murphy <murp@redhat.com>
Link: https://github.com/rust-lang/rust/pull/152122
Link: https://github.com/rust-lang/rust/pull/158036
Link: https://github.com/rust-lang/rust/pull/160184

---

Changes in v2:
- Refactor for consistency
- Export RUSTC_FLAGS_FTRACE
- When FTRACE_MCOUNT_USE_CC is set, use rust's extended fentry support
  if supported or not at all.
- Link to v1: https://lore.kernel.org/rust-for-linux/20260724170132.3783816-1-murp@redhat.com/
---
 Makefile                        | 20 +++++++++++++++++++-
 scripts/generate_rust_target.rs |  2 ++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index bfb47ad8cb9a..7ece5a327c53 100644
--- a/Makefile
+++ b/Makefile
@@ -832,9 +832,11 @@ endif # KBUILD_EXTMOD
 # Defaults to vmlinux, but the arch makefile usually adds further targets
 all: vmlinux
 
-# The arch Makefiles can override CC_FLAGS_FTRACE. We may also append it later.
+# The arch Makefiles can override CC_FLAGS_FTRACE or RUSTC_FLAGS_FTRACE.
+# We may also append them later.
 ifdef CONFIG_FUNCTION_TRACER
   CC_FLAGS_FTRACE := -pg
+  RUSTC_FLAGS_FTRACE :=
 endif
 
 ifdef CONFIG_TRACEPOINTS
@@ -1053,10 +1055,26 @@ ifdef CONFIG_HAVE_FENTRY
     CC_FLAGS_FTRACE	+= -mfentry
     CC_FLAGS_USING	+= -DCC_USING_FENTRY
   endif
+  ifdef CONFIG_FTRACE_MCOUNT_USE_CC
+    # Support for mcount record/nop is not gated by rustc version for all architectures.
+    ifdef CONFIG_HAVE_NOP_MCOUNT
+      ifeq ($(call rustc-option-yn, -Zinstrument-mcount=fentry-nop-record),y)
+        RUSTC_FLAGS_FTRACE += -Zinstrument-mcount=fentry-nop-record
+      endif
+    else
+      ifeq ($(call rustc-option-yn, -Zinstrument-mcount=fentry-record),y)
+        RUSTC_FLAGS_FTRACE += -Zinstrument-mcount=fentry-record
+      endif
+    endif
+  else
+      RUSTC_FLAGS_FTRACE += $(if $(call rustc-min-version,109800),-Zinstrument-mcount=fentry,)
+  endif
 endif
 export CC_FLAGS_FTRACE
+export RUSTC_FLAGS_FTRACE
 KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
 KBUILD_AFLAGS	+= $(CC_FLAGS_USING)
+KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_FTRACE)
 endif
 
 # We trigger additional mismatches with less inlining
diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs
index 3bf296581a88..eb80a06d50d3 100644
--- a/scripts/generate_rust_target.rs
+++ b/scripts/generate_rust_target.rs
@@ -198,6 +198,7 @@ fn main() {
         ts.push("arch", "x86_64");
         if cfg.rustc_version_atleast(1, 98, 0) {
             ts.push("rustc-abi", "softfloat");
+            ts.push("supports-fentry", true);
         } else if cfg.rustc_version_atleast(1, 86, 0) {
             ts.push("rustc-abi", "x86-softfloat");
         }
@@ -240,6 +241,7 @@ fn main() {
         ts.push("arch", "x86");
         if cfg.rustc_version_atleast(1, 98, 0) {
             ts.push("rustc-abi", "softfloat");
+            ts.push("supports-fentry", true);
         } else if cfg.rustc_version_atleast(1, 86, 0) {
             ts.push("rustc-abi", "x86-softfloat");
         }

base-commit: 7059bdf4f04a3e14f4fafb3ac35fdca913e3e21a
-- 
2.55.0