BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] Add bpf_task_pt_regs() helper
@ 2021-08-18 23:41 Daniel Xu
  2021-08-18 23:41 ` [PATCH bpf-next 1/2] bpf: " Daniel Xu
  2021-08-18 23:41 ` [PATCH bpf-next 2/2] bpf: selftests: Add bpf_task_pt_regs() selftest Daniel Xu
  0 siblings, 2 replies; 6+ messages in thread
From: Daniel Xu @ 2021-08-18 23:41 UTC (permalink / raw)
  To: bpf, yhs, andriin; +Cc: Daniel Xu, kernel-team, linux-kernel

The motivation behind this helper is to access userspace pt_regs in a
kprobe handler.

uprobe's ctx is the userspace pt_regs. kprobe's ctx is the kernelspace
pt_regs. bpf_task_pt_regs() allows accessing userspace pt_regs in a
kprobe handler. The final case (kernelspace pt_regs in uprobe) is
pretty rare (usermode helper) so I think that can be solved later if
necessary.

More concretely, this helper is useful in doing BPF-based DWARF stack
unwinding. Currently the kernel can only do framepointer based stack
unwinds for userspace code. This is because the DWARF state machines are
too fragile to be computed in kernelspace [0]. The idea behind
DWARF-based stack unwinds w/ BPF is to copy a chunk of the userspace
stack (while in prog context) and send it up to userspace for unwinding
(probably with libunwind) [1]. This would effectively enable profiling
applications with -fomit-frame-pointer using kprobes and uprobes.

[0]: https://lkml.org/lkml/2012/2/10/356
[1]: https://github.com/danobi/bpf-dwarf-walk

Daniel Xu (2):
  bpf: Add bpf_task_pt_regs() helper
  bpf: selftests: Add bpf_task_pt_regs() selftest

 include/uapi/linux/bpf.h                      |  7 +++
 kernel/trace/bpf_trace.c                      | 20 ++++++++
 tools/include/uapi/linux/bpf.h                |  7 +++
 .../selftests/bpf/prog_tests/task_pt_regs.c   | 50 +++++++++++++++++++
 .../selftests/bpf/progs/test_task_pt_regs.c   | 29 +++++++++++
 5 files changed, 113 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/task_pt_regs.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_task_pt_regs.c

-- 
2.32.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-24  1:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-18 23:41 [PATCH bpf-next 0/2] Add bpf_task_pt_regs() helper Daniel Xu
2021-08-18 23:41 ` [PATCH bpf-next 1/2] bpf: " Daniel Xu
2021-08-19 20:27   ` Andrii Nakryiko
2021-08-24  1:38     ` Daniel Xu
2021-08-18 23:41 ` [PATCH bpf-next 2/2] bpf: selftests: Add bpf_task_pt_regs() selftest Daniel Xu
2021-08-19 20:28   ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox