History

kprobes: Enable stacktrace from pt_regs in kretprobe handler

Since the ORC unwinder from pt_regs requires setting up regs->ip correctly, set the correct return address to the regs->ip before calling user kretprobe handler. This allows the kretrprobe handler to trace stack from the kretprobe's pt_regs by stack_trace_save_regs() (eBPF will do this), instead of stack tracing from the handler context by stack_trace_save() (ftrace will do this). Link: https://lkml.kernel.org/r/163163053237.489837.4272653874525136832.stgit@devnote2

Masami Hiramatsu committed 5 years ago

Commit df91c5b1 parent 7391dd11 file · +7 −0Browse files

Unified · Split

kernel/kprobes.c+7−0
@@ -1937,6 +1937,13 @@ unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, · 1936 unmodified lines
19371937 BUG_ON(1);
19381938 }
19391939
1940+ /*
1941+ * Set the return address as the instruction pointer, because if the
1942+ * user handler calls stack_trace_save_regs() with this 'regs',
1943+ * the stack trace will start from the instruction pointer.
1944+ */
1945+ instruction_pointer_set(regs, (unsigned long)correct_ret_addr);
1946+
19401947 /* Run the user handler of the nodes. */
19411948 first = current->kretprobe_instances.first;
19421949 while (first) {