libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest7 patches 1 replyIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/7 | libbpf: Support CO-RE relocation of tracing program argumentstools/ tools/ tools/ +1 more | +789 | 0 replies | |
| 2/7 | bpf: Support CO-RE relocation of tracing program argumentsinclude/ kernel/ kernel/ | +41 | 0 replies | |
| 3/7 | selftests/bpf: Test CO-RE relocation of tracing program argumentstools/ tools/ tools/ | +359 | 0 replies | |
| 4/7 | selftests/bpf: Test CO-RE relocation of BPF LSM program argumentstools/ tools/ tools/ +1 more | +382 | 0 replies | |
| 5/7 | libbpf: Report function argument relocation support via type existencetools/ tools/ | +28 | 0 replies | |
| 6/7 | selftests/bpf: Test detection of function argument relocation supporttools/ tools/ tools/ +1 more | +28 | 0 replies | |
| 7/7 | Documentation/bpf: Describe CO-RE relocation of function argumentsDocumentation/ Documentation/ | +285 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
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