Skip to content

libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering

Alan Maguire bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest7 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7libbpf: Support CO-RE relocation of tracing program argumentstools/lib/bpf/bpf_tracing.h tools/lib/bpf/libbpf.c tools/lib/bpf/relo_core.c +1 more+789 -90 replies
2/7bpf: Support CO-RE relocation of tracing program argumentsinclude/linux/bpf.h kernel/bpf/btf.c kernel/bpf/check_btf.c+41 -00 replies
3/7selftests/bpf: Test CO-RE relocation of tracing program argumentstools/testing/selftests/bpf/Makefile.skel tools/testing/selftests/bpf/prog_tests/func_args_core.c tools/testing/selftests/bpf/progs/func_args_core.c+359 -10 replies
4/7selftests/bpf: Test CO-RE relocation of BPF LSM program argumentstools/testing/selftests/bpf/Makefile.skel tools/testing/selftests/bpf/prog_tests/func_args_core.c tools/testing/selftests/bpf/progs/func_args_core_lsm.c +1 more+382 -10 replies
5/7libbpf: Report function argument relocation support via type existencetools/lib/bpf/bpf_tracing.h tools/lib/bpf/relo_core.c+28 -20 replies
6/7selftests/bpf: Test detection of function argument relocation supporttools/testing/selftests/bpf/prog_tests/func_args_core.c tools/testing/selftests/bpf/progs/func_args_core.c tools/testing/selftests/bpf/progs/func_args_core_lsm.c +1 more+28 -40 replies
7/7Documentation/bpf: Describe CO-RE relocation of function argumentsDocumentation/bpf/libbpf/libbpf_overview.rst Documentation/bpf/llvm_reloc.rst+285 -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 bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering

Currently the BPF_PROG() family of macros greatly facilitates BPF tracing/LSM program writing by allowing specification of function, tracepoint or LSM hook arguments directly by name and type. These are then mapped to a context struct consisting of a u64 array, with one value for each argument, with by-value structs > 8 bytes taking two slots.

By instead creating a structure with named members, we can bring a Compile Once, Run Everywhere experience to parameter specifications via a new BPF_PROG_CORE() macro. This will better handle cases like the following:

Problem: A tracing program only needs to use one parameter from a multi-parameter kernel function signature, but verification can fail if the function signature changes due to added or removed parameters which are irrelevant to the case in hand.

Solution: Only specify the needed parameter in BPF_PROG_CORE()

Example:

	/*
	 * Prototype for filename_unlinkat is:
	 * int filename_unlinkat(int dfd, struct filename * name);
	 */
	SEC("fentry/filename_unlinkat")
	int BPF_PROG_CORE(handle, struct filename *, name)
	{
		if (bpf_func_arg_exists(name))
			...

Problem: Ordering of parameters changes for a BPF LSM program

Solution: Again use BPF_PROG_CORE() which is resilient to ordering changes as these become CO-RE relocations in the context structure. Return values should be tagged with __bpf_func_ret .

Problem: A parameter type changes

Solution: Use ___<new> named variants with old and new types:

	int BPF_PROG_CORE(handle, struct vm_area_struct *vma,
			  struct mm_area *vma___new)
	{
		if (bpf_func_arg_exists(vma___new))
			...
		else if (bpf_func_arg_exists(vma)

Because the solution is purely in libbpf, the support needs no kernel changes, aside from the light skeleton case, where CO-RE relocation is done in-kernel (support for the latter is added in patch 2). Patch 5 provides a test that programs can use to see if support is present.

Support covers fentry, fexit, fmod_ret, fsession, tp_btf, lsm_cgroup and BPF LSM programs.

In short, we are making use of BPF CO-RE to extend a CO-RE experience to program parameter ordering, presence and handling of parameter type changes.

Limitations: tp_btf programs require >= 5.10 kernel since it is the first kernel that makes tp_btf parameter names available, while on big-endian (s390x) platforms, signed values smaller than 8 bytes cannot be read.

This support requires BTF declaration tag support in clang/gcc to work; because gcc does not support declaration tags on types we add a marker within the struct itself.

Patch 1 adds libbpf support.
Patch 2 adds kernel-based lskel support. Patch 3 tests tracing program support.
Patch 4 tests BPF LSM support.
Patch 5 provides a test BPF program writers can use - bpf_func_args_supported() - to probe for support. Patch 6 tests that probe.
Patch 7 documents the support.

Alan Maguire (7):
libbpf: Support CO-RE relocation of tracing program arguments
bpf: Support CO-RE relocation of tracing program arguments selftests/bpf: Test CO-RE relocation of tracing program arguments selftests/bpf: Test CO-RE relocation of BPF LSM program arguments
libbpf: Report function argument relocation support via type existence selftests/bpf: Test detection of function argument relocation support Documentation/bpf: Describe CO-RE relocation of function arguments

 Documentation/bpf/libbpf/libbpf_overview.rst  | 190 ++++++++
 Documentation/bpf/llvm_reloc.rst              |  95 ++++
 include/linux/bpf.h                           |   2 +
 kernel/bpf/btf.c                              |  38 ++
 kernel/bpf/check_btf.c                        |   1 +
 tools/lib/bpf/bpf_tracing.h                   | 195 ++++++++
 tools/lib/bpf/libbpf.c                        | 142 +++++-
 tools/lib/bpf/relo_core.c                     | 458 +++++++++++++++++-
 tools/lib/bpf/relo_core.h                     |  29 ++
 tools/testing/selftests/bpf/Makefile.skel     |   3 +-
 .../selftests/bpf/prog_tests/func_args_core.c | 325 +++++++++++++
 .../selftests/bpf/progs/func_args_core.c      | 241 +++++++++
 .../selftests/bpf/progs/func_args_core_lsm.c  | 156 ++++++
 .../bpf/progs/func_args_core_lsm_cgroup.c     |  40 ++
 14 files changed, 1905 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/func_args_core.c
 create mode 100644 tools/testing/selftests/bpf/progs/func_args_core.c
 create mode 100644 tools/testing/selftests/bpf/progs/func_args_core_lsm.c
 create mode 100644 tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c
Signature · 1 line
2.56.0