Skip to content

Commit ac2e740

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
x86/fgraph,bpf: Fix stack ORC unwind from kprobe_multi return probe
Currently we don't get stack trace via ORC unwinder on top of fgraph exit handler. We can see that when generating stacktrace from kretprobe_multi bpf program which is based on fprobe/fgraph. The reason is that the ORC unwind code won't get pass the return_to_handler callback installed by fgraph return probe machinery. Solving this by creating stack frame in return_to_handler expected by ftrace_graph_ret_addr function to recover original return address and continue with the unwind. Also updating the pt_regs data with cs/flags/rsp which are needed for successful stack retrieval from ebpf bpf_get_stackid helper. - in get_perf_callchain we check user_mode(regs) so CS has to be set - in perf_callchain_kernel we call perf_hw_regs(regs), so EFLAGS/FIXED has to be unset Note I feel like it'd be better to set those directly in return_to_handler, but Steven suggested setting them later in kprobe_multi code path. Signed-off-by: Jiri Olsa <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 6b9ecb1 commit ac2e740

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

arch/x86/include/asm/ftrace.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ arch_ftrace_get_regs(struct ftrace_regs *fregs)
5656
return &arch_ftrace_regs(fregs)->regs;
5757
}
5858

59+
#define arch_ftrace_partial_regs(regs) do { \
60+
regs->flags &= ~X86_EFLAGS_FIXED; \
61+
regs->cs = __KERNEL_CS; \
62+
} while (0)
63+
5964
#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \
6065
(_regs)->ip = arch_ftrace_regs(fregs)->regs.ip; \
6166
(_regs)->sp = arch_ftrace_regs(fregs)->regs.sp; \

arch/x86/kernel/ftrace_64.S

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,17 @@ SYM_CODE_START(return_to_handler)
354354
UNWIND_HINT_UNDEFINED
355355
ANNOTATE_NOENDBR
356356

357+
/* Restore return_to_handler value that got eaten by previous ret instruction. */
358+
subq $8, %rsp
359+
UNWIND_HINT_FUNC
360+
357361
/* Save ftrace_regs for function exit context */
358362
subq $(FRAME_SIZE), %rsp
359363

360364
movq %rax, RAX(%rsp)
361365
movq %rdx, RDX(%rsp)
362366
movq %rbp, RBP(%rsp)
367+
movq %rsp, RSP(%rsp)
363368
movq %rsp, %rdi
364369

365370
call ftrace_return_to_handler
@@ -368,7 +373,8 @@ SYM_CODE_START(return_to_handler)
368373
movq RDX(%rsp), %rdx
369374
movq RAX(%rsp), %rax
370375

371-
addq $(FRAME_SIZE), %rsp
376+
addq $(FRAME_SIZE) + 8, %rsp
377+
372378
/*
373379
* Jump back to the old return address. This cannot be JMP_NOSPEC rdi
374380
* since IBT would demand that contain ENDBR, which simply isn't so for

include/linux/ftrace.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs
193193
#if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) || \
194194
defined(CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS)
195195

196+
#ifndef arch_ftrace_partial_regs
197+
#define arch_ftrace_partial_regs(regs) do {} while (0)
198+
#endif
199+
196200
static __always_inline struct pt_regs *
197201
ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs)
198202
{
@@ -202,7 +206,11 @@ ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs)
202206
* Since arch_ftrace_get_regs() will check some members and may return
203207
* NULL, we can not use it.
204208
*/
205-
return &arch_ftrace_regs(fregs)->regs;
209+
regs = &arch_ftrace_regs(fregs)->regs;
210+
211+
/* Allow arch specific updates to regs. */
212+
arch_ftrace_partial_regs(regs);
213+
return regs;
206214
}
207215

208216
#endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS */

0 commit comments

Comments
 (0)