LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Daniel Xu <dxu@dxuuu.xyz>
To: bpf@vger.kernel.org, yhs@fb.com, andriin@fb.com
Cc: Daniel Xu <dxu@dxuuu.xyz>,
	kernel-team@fb.com, linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next v2 0/5] bpf: Add bpf_task_pt_regs() helper
Date: Mon, 23 Aug 2021 19:43:45 -0700	[thread overview]
Message-ID: <cover.1629772842.git.dxu@dxuuu.xyz> (raw)

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

Changes from v1:
- Conwolidate BTF_ID decls for task_struct
- Enable bpf_get_current_task_btf() for all prog types
- Enable bpf_task_pt_regs() for all prog types
- Use ASSERT_* macros instead of CHECK

Daniel Xu (5):
  bpf: Add BTF_ID_LIST_GLOBAL_SINGLE macro
  bpf: Consolidate task_struct BTF_ID declarations
  bpf: Extend bpf_base_func_proto helpers with
    bpf_get_current_task_btf()
  bpf: Add bpf_task_pt_regs() helper
  bpf: selftests: Add bpf_task_pt_regs() selftest

 include/linux/btf_ids.h                       |  5 ++
 include/uapi/linux/bpf.h                      |  7 +++
 kernel/bpf/bpf_task_storage.c                 |  6 +--
 kernel/bpf/helpers.c                          |  6 +++
 kernel/bpf/stackmap.c                         |  4 +-
 kernel/bpf/task_iter.c                        | 11 ++---
 kernel/trace/bpf_trace.c                      | 25 ++++++++--
 tools/include/uapi/linux/bpf.h                |  7 +++
 .../selftests/bpf/prog_tests/task_pt_regs.c   | 47 +++++++++++++++++++
 .../selftests/bpf/progs/test_task_pt_regs.c   | 29 ++++++++++++
 10 files changed, 131 insertions(+), 16 deletions(-)
 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.33.0


             reply	other threads:[~2021-08-24  2:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-24  2:43 Daniel Xu [this message]
2021-08-24  2:43 ` [PATCH bpf-next v2 1/5] bpf: Add BTF_ID_LIST_GLOBAL_SINGLE macro Daniel Xu
2021-08-24  2:43 ` [PATCH bpf-next v2 2/5] bpf: Consolidate task_struct BTF_ID declarations Daniel Xu
2021-08-24  2:43 ` [PATCH bpf-next v2 3/5] bpf: Extend bpf_base_func_proto helpers with bpf_get_current_task_btf() Daniel Xu
2021-08-24  2:43 ` [PATCH bpf-next v2 4/5] bpf: Add bpf_task_pt_regs() helper Daniel Xu
2021-08-24  2:43 ` [PATCH bpf-next v2 5/5] bpf: selftests: Add bpf_task_pt_regs() selftest Daniel Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cover.1629772842.git.dxu@dxuuu.xyz \
    --to=dxu@dxuuu.xyz \
    --cc=andriin@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=yhs@fb.com \
    --subject='Re: [PATCH bpf-next v2 0/5] bpf: Add bpf_task_pt_regs() helper' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).