Skip to content

RFC bpf: Attach kprobe multi link to inlined functions

Jiri Olsa bpf linux-trace-kernel lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest31 patches 4 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/31x86/ptrace: Add regs_get_dwarf_register functionarch/x86/include/asm/ptrace.h arch/x86/kernel/ptrace.c include/linux/ptrace.h+60 -00 replies
2/31kprobes: Make the kprobe hash table size configurablearch/Kconfig kernel/kprobes.c+18 -10 replies
3/31kprobes: Add enable_kprobes to enable multiple kprobes at oncearch/x86/kernel/kprobes/core.c include/linux/kprobes.h kernel/kprobes.c+99 -110 replies
4/31module: Add module_sect_addr functioninclude/linux/module.h kernel/module/sysfs.c+29 -00 replies
5/31bpf: Add bpf_get_btf_vmlinux_inline functioninclude/linux/bpf.h include/linux/btf.h kernel/bpf/btf.c +1 more+99 -31 reply
6/31bpf: Use goto labels for error path in bpf_kprobe_multi_link_attachkernel/trace/bpf_trace.c+10 -90 replies
7/31bpf: Add support to attach inlined functions with kprobe multi linkarch/Kconfig arch/x86/Kconfig include/uapi/linux/bpf.h +2 more+384 -181 reply
8/31bpf: Add cookie support for kprobe multi inlined functionskernel/trace/bpf_trace.c+16 -40 replies
9/31bpf: Add link info support for kprobe multi inlined functionsinclude/uapi/linux/bpf.h kernel/trace/bpf_trace.c tools/include/uapi/linux/bpf.h+76 -40 replies
10/31bpf: Add fdinfo support for kprobe multi inlined functionskernel/trace/bpf_trace.c+5 -10 replies
11/31bpf: Add btf_for_each_module_inline functioninclude/linux/btf.h kernel/bpf/btf.c+75 -00 replies
12/31bpf: Change get_modules_for_addrs to use struct modules_arraykernel/trace/bpf_trace.c+18 -150 replies
13/31tbpf: Add support to attach inlined functions in modules with kprobe multi linkkernel/trace/bpf_trace.c+65 -50 replies
14/31bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for kprobe multi linkinclude/uapi/linux/bpf.h kernel/trace/bpf_trace.c tools/include/uapi/linux/bpf.h+19 -40 replies
15/31bpf: Add get_func_arg[_cnt] support for kprobe multi functionsinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/verifier.c +2 more+192 -111 reply
16/31bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functionsinclude/uapi/linux/bpf.h kernel/trace/bpf_trace.c tools/include/uapi/linux/bpf.h+137 -210 replies
17/31bpf: Add log messages for kprobe multi inline attachinclude/linux/trace_events.h kernel/bpf/syscall.c kernel/trace/bpf_trace.c+68 -190 replies
18/31bpf: Add inlined functions for kprobe multi inline testsnet/bpf/test_run.c+82 -10 replies
19/31libbpf: Add inline options to bpf_kprobe_multi_optstools/lib/bpf/libbpf.c tools/lib/bpf/libbpf.h+22 -20 replies
20/31libbpf: Print kernel log for failed kprobe multi attachtools/lib/bpf/libbpf.c+11 -00 replies
21/31bpftool: Display inlined functions for kprobe multi linktools/bpf/bpftool/link.c+303 -450 replies
22/31selftests/bpf: Add inlined functions to bpf_testmodtools/testing/selftests/bpf/test_kmods/bpf_testmod.c+28 -00 replies
23/31selftests/bpf: Add kprobe multi inline attach testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c tools/testing/selftests/bpf/progs/kprobe_multi_inline.c+254 -00 replies
24/31selftests/bpf: Add kprobe multi inline mixed testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c+50 -00 replies
25/31selftests/bpf: Add kprobe multi inline skip testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c+102 -00 replies
26/31selftests/bpf: Add kprobe multi inline fails testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c tools/testing/selftests/bpf/progs/kprobe_multi_inline.c+174 -00 replies
27/31selftests/bpf: Add kprobe multi inline module testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c+114 -10 replies
28/31selftests/bpf: Add kprobe multi inline strict testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c+82 -00 replies
29/31selftests/bpf: Add kprobe multi inline args testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c tools/testing/selftests/bpf/progs/kprobe_multi_inline.c+217 -00 replies
30/31selftests/bpf: Add kprobe multi inline bench attach testtools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c+50 -00 replies
31/31selftests/bpf: Add kprobe multi inline link info testtools/testing/selftests/bpf/prog_tests/fill_link_info.c+251 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH RFC bpf-next 00/31] bpf: Attach kprobe multi link to inlined functions

hi,
the kprobe multi link attaches only the out-of-line copy of the function, so inlined instances are not traced and a function inlined in all its callers can't be attached at all.

This patchset adds BPF_F_KPROBE_MULTI_INLINE flag that attaches also the inlined instances of the functions in the syms array. The instances are resolved from inline BTF (CONFIG_DEBUG_INFO_BTF_INLINE) of vmlinux and modules and each gets its own kprobe.

Only instances inlined in traceable functions (with ftrace location) are attached, instances in notrace functions are likely not safe to probe. Those and the instances that kprobes refuse are skipped, or fail the attach with BPF_F_KPROBE_MULTI_INLINE_STRICT flag.

The program attached to inlined instance can use cookies, bpf_get_func_ip and bpf_get_func_arg[_cnt] helpers. The instances are visible in link info, fdinfo and bpftool.

The BPF_F_KPROBE_MULTI_INLINE flag can't be used with return probe, session program, addrs array or bpf_override_return. The support is x86_64 only for now.

- patches 1-5: x86 ptrace, kprobes, module and BTF preparation
- patches 6-16: kprobe multi inline attach
- patches 17-19: libbpf and bpftool support
- patches 20-30: selftests

The kprobe_multi_bench_attach test in KVM guest, 67295 vmlinux functions:

                       instances   attach    detach
  without inline               -   0.324s    0.154s
  with inline              19947   6.443s    0.361s

The speed of the inline attach depends on several kprobe changes. Two of them are part of this series:

kprobes: Make the kprobe hash table size configurable
kprobes: Add enable_kprobes to enable multiple kprobes at once

and two were posted separately:

[1] jump_label/x86: Avoid decoding instructions for jump entry size
[2] x86/kprobes: Use ftrace_location_range in __recover_probed_insn

I think there's still space for kprobe changes that would make the attach faster, especially the follow up fixes for [3]. I'm sending this as RFC to get feedback on the interface, while I work on the kprobe attach speedup.

thanks,
jirka

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/bpf/[email protected]/

Assisted-by LLM

---
Jiri Olsa (31):
x86/ptrace: Add regs_get_dwarf_register function
kprobes: Make the kprobe hash table size configurable
kprobes: Add enable_kprobes to enable multiple kprobes at once
module: Add module_sect_addr function
bpf: Add bpf_get_btf_vmlinux_inline function
bpf: Use goto labels for error path in bpf_kprobe_multi_link_attach
bpf: Add support to attach inlined functions with kprobe multi link
bpf: Add cookie support for kprobe multi inlined functions
bpf: Add link info support for kprobe multi inlined functions
bpf: Add fdinfo support for kprobe multi inlined functions
bpf: Add btf_for_each_module_inline function
bpf: Change get_modules_for_addrs to use struct modules_array
tbpf: Add support to attach inlined functions in modules with kprobe multi link
bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for kprobe multi link
bpf: Add get_func_arg[_cnt] support for kprobe multi functions
bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions
bpf: Add log messages for kprobe multi inline attach
bpf: Add inlined functions for kprobe multi inline tests
libbpf: Add inline options to bpf_kprobe_multi_opts
libbpf: Print kernel log for failed kprobe multi attach
bpftool: Display inlined functions for kprobe multi link selftests/bpf: Add inlined functions to bpf_testmod selftests/bpf: Add kprobe multi inline attach test
selftests/bpf: Add kprobe multi inline mixed test
selftests/bpf: Add kprobe multi inline skip test
selftests/bpf: Add kprobe multi inline fails test
selftests/bpf: Add kprobe multi inline module test
selftests/bpf: Add kprobe multi inline strict test
selftests/bpf: Add kprobe multi inline args test
selftests/bpf: Add kprobe multi inline bench attach test selftests/bpf: Add kprobe multi inline link info test

 arch/Kconfig                                               |  23 ++
 arch/x86/Kconfig                                           |   1 +
 arch/x86/include/asm/ptrace.h                              |   8 +
 arch/x86/kernel/kprobes/core.c                             |  13 +
 arch/x86/kernel/ptrace.c                                   |  42 +++
 include/linux/bpf.h                                        |   2 +
 include/linux/btf.h                                        |  17 +
 include/linux/kprobes.h                                    |   7 +
 include/linux/module.h                                     |   9 +
 include/linux/ptrace.h                                     |  10 +
 include/linux/trace_events.h                               |   7 +-
 include/uapi/linux/bpf.h                                   |  29 +-
 kernel/bpf/btf.c                                           | 157 ++++++++-
 kernel/bpf/syscall.c                                       |   2 +-
 kernel/bpf/sysfs_btf.c                                     |   2 +-
 kernel/bpf/verifier.c                                      |   3 +
 kernel/kprobes.c                                           |  92 ++++-
 kernel/module/sysfs.c                                      |  20 ++
 kernel/trace/bpf_trace.c                                   | 921 ++++++++++++++++++++++++++++++++++++++++++++++---
 net/bpf/test_run.c                                         |  83 ++++-
 tools/bpf/bpftool/link.c                                   | 348 ++++++++++++++++---
 tools/include/uapi/linux/bpf.h                             |  29 +-
 tools/lib/bpf/libbpf.c                                     |  29 +-
 tools/lib/bpf/libbpf.h                                     |   6 +-
 tools/testing/selftests/bpf/prog_tests/fill_link_info.c    | 251 ++++++++++++++
 tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 963 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/kprobe_multi_inline.c    |  79 +++++
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c       |  28 ++
 28 files changed, 3058 insertions(+), 123 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_inline.c