Skip to contentSkip to conversation

x86: Better backtraces for leaf functions

PATCH Applied Mathias Krause 2 participants 3 messages May 27 – Sep 25 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86: Better backtraces for leaf functions

Leaf functions are problematic for backtraces as they lack the frame pointer setup epilogue. If such a function causes a fault, the original caller won't be part of the backtrace. That's problematic if, for example, memcpy() is failing because it got passed a bad pointer. The generated backtrace will look like this, providing no clue what the issue may be:

	STACK: @401b31 4001ad
  0x0000000000401b31: memcpy at lib/string.c:136 (discriminator 3)
        	for (i = 0; i < n; ++i)
      > 		a[i] = b[i];

0x00000000004001ac: gdt32_end at x86/cstart64.S:127
lea __environ(%rip), %rdx
> call main
mov %eax, %edi

By abusing profiling, we can force the compiler to emit a frame pointer setup epilogue even for leaf functions, making the above backtrace change like this:

	STACK: @401c21 400512 4001ad
  0x0000000000401c21: memcpy at lib/string.c:136 (discriminator 3)
        	for (i = 0; i < n; ++i)
      > 		a[i] = b[i];

0x0000000000400511: main at x86/hypercall.c:91 (discriminator 24)

      > 	memcpy((void *)~0xbadc0de, (void *)0xdeadbeef, 42);

0x00000000004001ac: gdt32_end at x86/cstart64.S:127
lea __environ(%rip), %rdx
> call main
mov %eax, %edi

Above backtrace includes the failing memcpy() call, making it much easier to spot the bug.

Enable "fake profiling" if supported by the compiler to get better backtraces. The runtime overhead should be negligible for the gained debugability as the profiling call is actually a NOP.

Unfortunately, compiler bugs and nitpicks require additional quirks. Namely, -mnop-mcount doesn't play well with 16-bit code generation for C code. gcc happily emits a 5-byte NOP that transmutes to a 4-byte NOP followed by a zero byte when executed in real mode, wrecking all code that follows.

Fix that by selectively disabling '-mnop-mcount' for realmode.c, making it call mcount / __fentry__, which are provided as stub functions.

Another quirk is the requirement of -mfentry which recent gcc-16 started complaining about if it's not used together with -pg.

Thanks a lot Sean and Paolo for helping finding and fixing these issues!

Note, a fix[1] for the bad gcc behavior has been shipped with gcc-16, i.e. this workaround can be dropped when gcc-16 is the minimal supported version for KUT (so in about 30 years).

---

Sean, I dropped your SoB because it felt inappropriate to keep it after mangling the patch so much.

v4:
- squashed the realmode fix into this change
- integrated -mfentry, reported by Paolo
v3: https://lore.kernel.org/kvm/[email protected]/

 x86/Makefile.common | 22 ++++++++++++++++++++++
 x86/realmode.c      |  4 ++++
 2 files changed, 26 insertions(+)
x86/Makefile.common +22 -0
index a251dd71e3b6..88a671187fe9 100644
@@ -43,6 +43,27 @@ COMMON_CFLAGS += -O1
 # stack.o relies on frame pointers.
 KEEP_FRAME_POINTER := y
 
+ifneq ($(KEEP_FRAME_POINTER),)
+# Fake profiling to force the compiler to emit a frame pointer setup also in
+# leaf function (-mno-omit-leaf-frame-pointer doesn't work, unfortunately).
+#
+# Note:
+# We need to defer the cc-option test until -fno-pic or -no-pie have been
+# added to CFLAGS as -mnop-mcount needs it. The lazy evaluation of CFLAGS
+# during compilation makes this do "The Right Thing."
+#
+# -mfentry is only needed because recent versions of gcc otherwise warn about
+# the lack of it, that it may cause placing the profiling call not directly
+# after the prologue but somewhere later -- not that we care but be nice and
+# avoid that warning.
+#
+# NO_NOP_MCOUNT is needed for the 16-bit realmode.o which causes gcc before
+# gcc-16 to generate broken code (5-byte NOP instead of 3-byte one).
+NOP_PGFLAGS := -pg -mfentry -mnop-mcount
+LATE_CFLAGS += $(call cc-option, $(NOP_PGFLAGS), "")
+NO_NOP_MCOUNT = $(if $(filter $(NOP_PGFLAGS),$(LATE_CFLAGS)),-mno-nop-mcount)
+endif
+
 FLATLIBS = lib/libcflat.a
 
 ifeq ($(CONFIG_EFI),y)
@@ -113,6 +134,7 @@ $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o $(SRCDIR)/$(TEST_DIR)/realmode.
 
 $(TEST_DIR)/realmode.o: bits = $(realmode_bits)
 $(TEST_DIR)/realmode.o: CFLAGS += -Os -Wno-array-bounds
+$(TEST_DIR)/realmode.o: CFLAGS += $(NO_NOP_MCOUNT)
 
 $(TEST_DIR)/access_test.$(bin): $(TEST_DIR)/access.o
 
x86/realmode.c +4 -0
index 7a4423ec6098..38bd6cdbabc1 100644
@@ -23,6 +23,10 @@ void test_function(void);
 asm(
 	"test_function: \n\t"
 	"mov $0x1234, %eax \n\t"
+	"ret\n\t"
+	/* profiling stubs */
+	"__fentry__:\n\t"
+	"mcount:\n\t"
 	"ret"
    );
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 123 lines · Mathias Krause

Leaf functions are problematic for backtraces as they lack the frame pointer setup epilogue. If such a function causes a fault, the original caller won't be part of the backtrace. That's problematic if, for example, memcpy() is failing because it got passed a bad pointer. The generated backtrace will look like this, providing no clue what the issue may be:

	STACK: @401b31 4001ad
  0x0000000000401b31: memcpy at lib/string.c:136 (discriminator 3)
        	for (i = 0; i < n; ++i)
      > 		a[i] = b[i];

0x00000000004001ac: gdt32_end at x86/cstart64.S:127
lea __environ(%rip), %rdx
> call main
mov %eax, %edi

By abusing profiling, we can force the compiler to emit a frame pointer setup epilogue even for leaf functions, making the above backtrace change like this:

	STACK: @401c21 400512 4001ad
  0x0000000000401c21: memcpy at lib/string.c:136 (discriminator 3)
        	for (i = 0; i < n; ++i)
      > 		a[i] = b[i];

0x0000000000400511: main at x86/hypercall.c:91 (discriminator 24)

      > 	memcpy((void *)~0xbadc0de, (void *)0xdeadbeef, 42);

0x00000000004001ac: gdt32_end at x86/cstart64.S:127
lea __environ(%rip), %rdx
> call main
mov %eax, %edi

Above backtrace includes the failing memcpy() call, making it much easier to spot the bug.

Enable "fake profiling" if supported by the compiler to get better backtraces. The runtime overhead should be negligible for the gained debugability as the profiling call is actually a NOP.

Unfortunately, compiler bugs and nitpicks require additional quirks. Namely, -mnop-mcount doesn't play well with 16-bit code generation for C code. gcc happily emits a 5-byte NOP that transmutes to a 4-byte NOP followed by a zero byte when executed in real mode, wrecking all code that follows.

Fix that by selectively disabling '-mnop-mcount' for realmode.c, making it call mcount / __fentry__, which are provided as stub functions.

Another quirk is the requirement of -mfentry which recent gcc-16 started complaining about if it's not used together with -pg.

Thanks a lot Sean and Paolo for helping finding and fixing these issues!

Note, a fix[1] for the bad gcc behavior has been shipped with gcc-16, i.e. this workaround can be dropped when gcc-16 is the minimal supported version for KUT (so in about 30 years).

---

Sean, I dropped your SoB because it felt inappropriate to keep it after mangling the patch so much.

v4:
- squashed the realmode fix into this change
- integrated -mfentry, reported by Paolo
v3: https://lore.kernel.org/kvm/[email protected]/

 x86/Makefile.common | 22 ++++++++++++++++++++++
 x86/realmode.c      |  4 ++++
 2 files changed, 26 insertions(+)
x86/Makefile.common +22 -0
index a251dd71e3b6..88a671187fe9 100644
@@ -43,6 +43,27 @@ COMMON_CFLAGS += -O1
 # stack.o relies on frame pointers.
 KEEP_FRAME_POINTER := y
 
+ifneq ($(KEEP_FRAME_POINTER),)
+# Fake profiling to force the compiler to emit a frame pointer setup also in
+# leaf function (-mno-omit-leaf-frame-pointer doesn't work, unfortunately).
+#
+# Note:
+# We need to defer the cc-option test until -fno-pic or -no-pie have been
+# added to CFLAGS as -mnop-mcount needs it. The lazy evaluation of CFLAGS
+# during compilation makes this do "The Right Thing."
+#
+# -mfentry is only needed because recent versions of gcc otherwise warn about
+# the lack of it, that it may cause placing the profiling call not directly
+# after the prologue but somewhere later -- not that we care but be nice and
+# avoid that warning.
+#
+# NO_NOP_MCOUNT is needed for the 16-bit realmode.o which causes gcc before
+# gcc-16 to generate broken code (5-byte NOP instead of 3-byte one).
+NOP_PGFLAGS := -pg -mfentry -mnop-mcount
+LATE_CFLAGS += $(call cc-option, $(NOP_PGFLAGS), "")
+NO_NOP_MCOUNT = $(if $(filter $(NOP_PGFLAGS),$(LATE_CFLAGS)),-mno-nop-mcount)
+endif
+
 FLATLIBS = lib/libcflat.a
 
 ifeq ($(CONFIG_EFI),y)
@@ -113,6 +134,7 @@ $(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o $(SRCDIR)/$(TEST_DIR)/realmode.
 
 $(TEST_DIR)/realmode.o: bits = $(realmode_bits)
 $(TEST_DIR)/realmode.o: CFLAGS += -Os -Wno-array-bounds
+$(TEST_DIR)/realmode.o: CFLAGS += $(NO_NOP_MCOUNT)
 
 $(TEST_DIR)/access_test.$(bin): $(TEST_DIR)/access.o
 
x86/realmode.c +0 -0
index 7a4423ec6098..38bd6cdbabc1 100644

x86/realmode.c void test_function(void); · line 32

Quoted 10 lines
x86/realmode.c +4 -0
@@ -23,6 +23,10 @@ void test_function(void);
 asm(
 	"test_function: \n\t"
 	"mov $0x1234, %eax \n\t"
+	"ret\n\t"
+	/* profiling stubs */
+	"__fentry__:\n\t"
+	"mcount:\n\t"
 	"ret"
    );
 

Ping! Paolo, can you pick this one up, please?

Thanks,
Mathias

reviewer Applied
#2 lore raw
In reply to Mathias Krause
Quoted 8 lines · Mathias Krause

Leaf functions are problematic for backtraces as they lack the frame pointer setup epilogue. If such a function causes a fault, the original caller won't be part of the backtrace. That's problematic if, for example, memcpy() is failing because it got passed a bad pointer. The generated backtrace will look like this, providing no clue what the issue may be:

[...]

Applied to kvm-x86 next, thanks!

[1/1] x86: Better backtraces for leaf functions

      https://github.com/kvm-x86/kvm-unit-tests/commit/98edcf823feb

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next