Skip to content

Support BTF-powered BPF tracing programs for kernel modules

Andrii Nakryiko bpf netdev 6 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v6 Latest14 patches 2 repliesIn review
  2. v514 patches 5 repliesSuperseded
  3. v414 patches 4 repliesSuperseded
Earlier versions · 3
VersionPostedPatchesRepliesStatus
v37 patches3 repliesSuperseded
v27 patches0 repliesSuperseded
v16 patches14 repliesSuperseded

Changes in v6 from the cover letter

- move btf_put() back to syscall.c (kernel test robot);
- added close(fd) in patch #5 (John);

Patches in v6 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/14bpf: fix bpf_put_raw_tracepoint()'s use of __module_address()kernel/trace/bpf_trace.c+5 -30 replies
2/14bpf: keep module's btf_data_size intact after loadkernel/module.c+0 -10 replies
3/14libbpf: add internal helper to load BTF data by FDtools/lib/bpf/btf.c tools/lib/bpf/libbpf_internal.h+36 -260 replies
4/14libbpf: refactor CO-RE relocs to not assume a single BTF objecttools/lib/bpf/libbpf.c+111 -760 replies
5/14libbpf: add kernel module BTF support for CO-RE relocationstools/lib/bpf/libbpf.c+169 -100 replies
6/14selftests/bpf: add bpf_testmod kernel module for testingtools/testing/selftests/bpf/.gitignore tools/testing/selftests/bpf/Makefile tools/testing/selftests/bpf/bpf_testmod/.gitignore +6 more+198 -30 replies
7/14selftests/bpf: add support for marking sub-tests as skippedtools/testing/selftests/bpf/test_progs.c+3 -30 replies
8/14selftests/bpf: add CO-RE relocs selftest relying on kernel module BTFtools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/progs/core_reloc_types.h tools/testing/selftests/bpf/progs/test_core_reloc_module.c+151 -110 replies
9/14bpf: remove hard-coded btf_vmlinux assumption from BPF verifierinclude/linux/bpf.h include/linux/bpf_verifier.h include/linux/btf.h +4 more+148 -670 replies
10/14bpf: allow to specify kernel module BTFs when attaching BPF programsinclude/linux/btf.h include/uapi/linux/bpf.h kernel/bpf/btf.c +2 more+69 -332 replies
11/14libbpf: factor out low-level BPF program loading helpertools/lib/bpf/bpf.c tools/lib/bpf/libbpf.c tools/lib/bpf/libbpf_internal.h+113 -500 replies
12/14libbpf: support attachment of BPF tracing programs to kernel modulestools/lib/bpf/bpf.c tools/lib/bpf/libbpf.c tools/lib/bpf/libbpf_internal.h+112 -320 replies
13/14selftests/bpf: add tp_btf CO-RE reloc test for modulestools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/progs/test_core_reloc_module.c+33 -20 replies
14/14selftests/bpf: add fentry/fexit/fmod_ret selftest for kernel moduletools/testing/selftests/bpf/prog_tests/module_attach.c tools/testing/selftests/bpf/progs/test_module_attach.c+119 -00 replies

Reviews trailers per patch

3 of 14 patches have a Reviewed-by or Acked-by.

PatchMartin KaFai Lau
1/14A-b
2/14
3/14
4/14A-b
5/14
6/14A-b
7/14
8/14
9/14
10/14
11/14
12/14
13/14
14/14

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v6 thread

Cover letter [PATCH v6 bpf-next 00/14] Support BTF-powered BPF tracing programs for kernel modules

This patch sets extends kernel and libbpf with support for attaching BTF-powered raw tracepoint (tp_btf) and tracing (fentry/fexit/fmod_ret/lsm) BPF programs to BPF hooks defined in kernel modules. As part of that, libbpf now supports performing CO-RE relocations against types in kernel module BTFs, in addition to existing vmlinux BTF support.

Kernel UAPI for BPF_PROG_LOAD now allows to specify kernel module (or vmlinux) BTF object FD in attach_btf_obj_fd field, aliased to attach_prog_fd. This is used to identify which BTF object needs to be used for finding BTF type by provided attach_btf_id.

This patch set also sets up a convenient and fully-controlled custom kernel module (called "bpf_testmod"), that is a predictable playground for all the BPF selftests, that rely on module BTFs. Currently pahole doesn't generate BTF_KIND_FUNC info for ftrace-able static functions in kernel modules, so expose traced function in bpf_sidecar.ko. Once pahole is enhanced, we can go back to static function.

From end user perspective there are no extra actions that need to happen. Libbpf will continue searching across all kernel module BTFs, if desired attach BTF type is not found in vmlinux. That way it doesn't matter if BPF hook that user is trying to attach to is built into vmlinux image or is loaded in kernel module.

v5->v6:
- move btf_put() back to syscall.c (kernel test robot);
- added close(fd) in patch #5 (John);
v4->v5:
- use FD to specify BTF object (Alexei);
- move prog->aux->attach_btf putting into bpf_prog_free() for consistency

    with putting prog->aux->dst_prog;

- fix BTF FD leak(s) in libbpf;
v3->v4:
- merge together patch sets [0] and [1];
- avoid increasing bpf_reg_state by reordering fields (Alexei);
- preserve btf_data_size in struct module;
v2->v3:
- fix subtle uninitialized variable use in BTF ID iteration code;
v1->v2:
- module_put() inside preempt_disable() region (Alexei);
- bpf_sidecar -> bpf_testmod rename (Alexei);
- test_progs more relaxed handling of bpf_testmod;
- test_progs marks skipped sub-tests properly as SKIP now.

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=393677&state=*
[1] https://patchwork.kernel.org/project/netdevbpf/list/?series=393679&state=*

Andrii Nakryiko (14):
bpf: fix bpf_put_raw_tracepoint()'s use of __module_address()
bpf: keep module's btf_data_size intact after load
libbpf: add internal helper to load BTF data by FD
libbpf: refactor CO-RE relocs to not assume a single BTF object
libbpf: add kernel module BTF support for CO-RE relocations selftests/bpf: add bpf_testmod kernel module for testing selftests/bpf: add support for marking sub-tests as skipped selftests/bpf: add CO-RE relocs selftest relying on kernel module BTF
bpf: remove hard-coded btf_vmlinux assumption from BPF verifier
bpf: allow to specify kernel module BTFs when attaching BPF programs
libbpf: factor out low-level BPF program loading helper
libbpf: support attachment of BPF tracing programs to kernel modules selftests/bpf: add tp_btf CO-RE reloc test for modules selftests/bpf: add fentry/fexit/fmod_ret selftest for kernel module

 include/linux/bpf.h                           |  13 +-
 include/linux/bpf_verifier.h                  |  28 +-
 include/linux/btf.h                           |   6 +-
 include/uapi/linux/bpf.h                      |   7 +-
 kernel/bpf/btf.c                              |  70 ++-
 kernel/bpf/syscall.c                          |  78 ++-
 kernel/bpf/verifier.c                         |  77 +--
 kernel/module.c                               |   1 -
 kernel/trace/bpf_trace.c                      |   8 +-
 net/ipv4/bpf_tcp_ca.c                         |   3 +-
 tools/include/uapi/linux/bpf.h                |   7 +-
 tools/lib/bpf/bpf.c                           | 101 ++--
 tools/lib/bpf/btf.c                           |  61 ++-
 tools/lib/bpf/libbpf.c                        | 500 ++++++++++++++----
 tools/lib/bpf/libbpf_internal.h               |  31 ++
 tools/testing/selftests/bpf/.gitignore        |   1 +
 tools/testing/selftests/bpf/Makefile          |  12 +-
 .../selftests/bpf/bpf_testmod/.gitignore      |   6 +
 .../selftests/bpf/bpf_testmod/Makefile        |  20 +
 .../bpf/bpf_testmod/bpf_testmod-events.h      |  36 ++
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  52 ++
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |  14 +
 .../selftests/bpf/prog_tests/core_reloc.c     |  80 ++-
 .../selftests/bpf/prog_tests/module_attach.c  |  53 ++
 .../selftests/bpf/progs/core_reloc_types.h    |  17 +
 .../bpf/progs/test_core_reloc_module.c        |  96 ++++
 .../selftests/bpf/progs/test_module_attach.c  |  66 +++
 tools/testing/selftests/bpf/test_progs.c      |  65 ++-
 tools/testing/selftests/bpf/test_progs.h      |   1 +
 29 files changed, 1230 insertions(+), 280 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/bpf_testmod/.gitignore
 create mode 100644 tools/testing/selftests/bpf/bpf_testmod/Makefile
 create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h
 create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
 create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/module_attach.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_module.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_module_attach.c
Signature · 1 line
2.24.1