mbox series

[RFC,bpf-next,0/5] bpf: add get_reg_val helper

Message ID 20220512074321.2090073-1-davemarchevsky@fb.com (mailing list archive)
Headers show
Series bpf: add get_reg_val helper | expand

Message

Dave Marchevsky May 12, 2022, 7:43 a.m. UTC
Currently, BPF programs can access register values from struct pt_regs.
Fetching other registers is not supported. For some usecases this limits
the usefulness of BPF programs. This series adds a helper meant to
fetch any register value for the architecture the program is running on.

Concrete motivating usecase: Tracing programs often attach to User
Statically-Defined Tracing (USDT) probes, which can pass arguments using
registers. The registers used to pass arguments for a specific probe are
determined at compile-time. 

Although general-purpose registers which can be accessed via pt_regs are 
usually chosen, register pressure can cause others to be used. Recently
we saw this happening in a Fedora libpthread library [0], where a xmm
register was used. Similarly, floating-point arguments in USDTs will
result in use of xmm register [1]. Since there is no way to access the
registers used to pass these arguments, BPF programs can't use them.

Another usecase: rdtsc access.

Initially the helper was meant to narrowly address the USDT xmm usecase
but conversation with Andrii highlighted the usefulness of a more
general helper. Although only x86 SSE reg fetching is added in this
patchset, the path forward for adding other register sets and
architectures should be clear.

Feedback from someone familiar with s390 or other arch regarding whether
the helper would be usable for other archs in current form would be
appreciated.


Summary of patches:

Patch 1 moves a header so fpregs_state_valid helper can be used.

Patches 2 and 3 contain the meat of the kernel- and libbpf-side
changes, respectively. Libbpf-side changes add use of the helper to usdt
lib in order to address USDT xmm issue that originally prompted this
work.

Patches 4 and 5 add tests.

Submitted as RFC for early feedback while failing usdt12 prog
verification is addressed (see patch 3).

  [0] - https://github.com/iovisor/bcc/pull/3880
	[1] - https://github.com/iovisor/bcc/issues/3875

Dave Marchevsky (5):
  x86/fpu: Move context.h to include/asm
  bpf: add get_reg_val helper
  libbpf: usdt lib wiring of xmm reads
  selftests/bpf: Add test for USDT parse of xmm reg
  selftests/bpf: get_reg_val test exercising fxsave fetch

 .../x86/{kernel => include/asm}/fpu/context.h |   2 +
 arch/x86/kernel/fpu/core.c                    |   2 +-
 arch/x86/kernel/fpu/regset.c                  |   2 +-
 arch/x86/kernel/fpu/signal.c                  |   2 +-
 arch/x86/kernel/fpu/xstate.c                  |   2 +-
 include/linux/bpf.h                           |   1 +
 include/uapi/linux/bpf.h                      |  40 +++++
 kernel/trace/bpf_trace.c                      | 148 ++++++++++++++++++
 kernel/trace/bpf_trace.h                      |   1 +
 net/bpf/bpf_dummy_struct_ops.c                |  13 ++
 tools/include/uapi/linux/bpf.h                |  40 +++++
 tools/lib/bpf/usdt.bpf.h                      |  36 +++--
 tools/lib/bpf/usdt.c                          |  51 +++++-
 tools/testing/selftests/bpf/Makefile          |   8 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  13 ++
 tools/testing/selftests/bpf/prog_tests/usdt.c |  49 ++++++
 .../selftests/bpf/progs/test_urandom_usdt.c   |  37 +++++
 tools/testing/selftests/bpf/test_progs.c      |   7 +
 tools/testing/selftests/bpf/urandom_read.c    |   3 +
 .../selftests/bpf/urandom_read_lib_xmm.c      |  62 ++++++++
 20 files changed, 499 insertions(+), 20 deletions(-)
 rename arch/x86/{kernel => include/asm}/fpu/context.h (96%)
 create mode 100644 tools/testing/selftests/bpf/urandom_read_lib_xmm.c