Skip to contentSkip to conversation

x86/fgraph: Fix the unwind hint at the end of return_to_handler()

PATCH In review Yifan Feng 3 participants 3 messages Nov 18 – Nov 19

Subsystems (3)
lore
Share & tools
lore

Yifan Feng

author
#0 lore raw

PATCH 1/1 x86/fgraph: Fix the unwind hint at the end of return_to_handler()

return_to_handler() reserves 8 bytes of stack for the return address and marks that with UNWIND_HINT_FUNC. The hint stays in effect after "addq $(FRAME_SIZE) + 8, %rsp" has dropped the slot again, so the ORC entry of the following call has CFA = sp+0:

return_to_handler+0x41: sp+0 call .Ldo_rop
return_to_handler+0x46: (und) int3
return_to_handler+0x47: sp+8 mov %rdi, (%rsp)

A stack trace taken from an interrupt which hits the call or the mov ends up at that entry, the stack pointer does not advance and the unwinder prints:

WARNING: stack going in the wrong direction? at return_to_handler+0x46/0x50

The return address is only in %rdi at that point. Mark the code as UNWIND_HINT_UNDEFINED again, as it was before the hint was added, so that the unwinder stops there without the warning.

Fixes 20a0bc10272f ("x86/fgraph,bpf: Fix stack ORC unwind from kprobe_multi return probe")

---
QEMU, function_graph + 20 kHz perf callchains: warning in 2s before, none in 10 min after. Found and written with the help of an AI coding assistant.

 arch/x86/kernel/ftrace_64.S | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/ftrace_64.S +2 -0
index 4b20673a3395..8b488f99de02 100644
@@ -387,6 +387,8 @@ SYM_CODE_START(return_to_handler)
 	movq RAX(%rsp), %rax
 
 	addq $(FRAME_SIZE) + 8, %rsp
+	/* The return address is only in %rdi now, the unwinder cannot follow. */
+	UNWIND_HINT_UNDEFINED
 
 	/*
 	 * Jump back to the old return address. This cannot be JMP_NOSPEC rdi

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca

Signature · 1 line
2.39.3 (Apple Git-146)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Yifan Feng

return_to_handler() reserves 8 bytes of stack for the return address and marks that with UNWIND_HINT_FUNC. The hint stays in effect after "addq $(FRAME_SIZE) + 8, %rsp" has dropped the slot again, so the ORC entry of the following call has CFA = sp+0:

return_to_handler+0x41: sp+0 call .Ldo_rop
return_to_handler+0x46: (und) int3
return_to_handler+0x47: sp+8 mov %rdi, (%rsp)

A stack trace taken from an interrupt which hits the call or the mov ends up at that entry, the stack pointer does not advance and the unwinder prints:

Quoted 8 lines

WARNING: stack going in the wrong direction? at return_to_handler+0x46/0x50

The return address is only in %rdi at that point. Mark the code as UNWIND_HINT_UNDEFINED again, as it was before the hint was added, so that the unwinder stops there without the warning.

Fixes 20a0bc10272f ("x86/fgraph,bpf: Fix stack ORC unwind from kprobe_multi return probe")

thanks,
jirka

Quoted 25 lines

---
QEMU, function_graph + 20 kHz perf callchains: warning in 2s before, none in 10 min after. Found and written with the help of an AI coding assistant.

 arch/x86/kernel/ftrace_64.S | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/ftrace_64.S +2 -0
index 4b20673a3395..8b488f99de02 100644
@@ -387,6 +387,8 @@ SYM_CODE_START(return_to_handler)
 	movq RAX(%rsp), %rax
 
 	addq $(FRAME_SIZE) + 8, %rsp
+	/* The return address is only in %rdi now, the unwinder cannot follow. */
+	UNWIND_HINT_UNDEFINED
 
 	/*
 	 * Jump back to the old return address. This cannot be JMP_NOSPEC rdi

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca

Signature · 1 line
2.39.3 (Apple Git-146)
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Yifan Feng
Quoted 13 lines · On Wed, 18 Nov 2026 15:50:23 -0800 Yifan Feng <[email protected]> wrote:

return_to_handler() reserves 8 bytes of stack for the return address and marks that with UNWIND_HINT_FUNC. The hint stays in effect after "addq $(FRAME_SIZE) + 8, %rsp" has dropped the slot again, so the ORC entry of the following call has CFA = sp+0:

return_to_handler+0x41: sp+0 call .Ldo_rop
return_to_handler+0x46: (und) int3
return_to_handler+0x47: sp+8 mov %rdi, (%rsp)

A stack trace taken from an interrupt which hits the call or the mov ends up at that entry, the stack pointer does not advance and the unwinder prints:

Quoted 5 lines

WARNING: stack going in the wrong direction? at return_to_handler+0x46/0x50

The return address is only in %rdi at that point. Mark the code as UNWIND_HINT_UNDEFINED again, as it was before the hint was added, so that the unwinder stops there without the warning.

Makes sense.

Peter, can you take this?

-- Steve