Skip to content

bpf: Kernel functions with KF_IMPLICIT_ARGS

Ihor Solodrai bpf linux-input lkml sched-ext 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest13 patches 4 repliesIn review 12 of 13 applied
  2. v213 patches 20 repliesSuperseded
  3. v110 patches 32 repliesSuperseded

Changes in v3 from the cover letter

- resolve_btfids: Use dynamic reallocation for btf2btf_context arrays (Andrii)
- resolve_btfids: Add missing free() for btf2btf_context arrays (AI)
- Other nits in resolve_btfids (Andrii, Eduard)

Patches in v3 12 of 13 applied

#Subject and filesSizeRepliesApplied
1/13bpf: Refactor btf_kfunc_id_set_containsinclude/linux/btf.h kernel/bpf/btf.c kernel/bpf/verifier.c+58 -220 repliesApplied
2/13bpf: Introduce struct bpf_kfunc_metakernel/bpf/verifier.c+91 -650 repliesApplied
3/13bpf: Verifier support for KF_IMPLICIT_ARGSinclude/linux/btf.h kernel/bpf/verifier.c+55 -42 repliesApplied
4/13resolve_btfids: Introduce finalize_btf() steptools/bpf/resolve_btfids/main.c+48 -210 repliesApplied
5/13resolve_btfids: Support for KF_IMPLICIT_ARGStools/bpf/resolve_btfids/main.c+382 -00 repliesApplied
6/13selftests/bpf: Add tests for KF_IMPLICIT_ARGStools/testing/selftests/bpf/prog_tests/kfunc_implicit_args.c tools/testing/selftests/bpf/progs/kfunc_implicit_args.c tools/testing/selftests/bpf/test_kmods/bpf_testmod.c+77 -00 repliesApplied
7/13bpf: Migrate bpf_wq_set_callback_impl() to KF_IMPLICIT_ARGSkernel/bpf/helpers.c kernel/bpf/verifier.c tools/testing/selftests/bpf/bpf_experimental.h +2 more+17 -231 replyApplied
8/13HID: Use bpf_wq_set_callback kernel functiondrivers/hid/bpf/progs/hid_bpf_helpers.h tools/testing/selftests/hid/progs/hid_bpf_helpers.h+6 -100 repliesApplied
9/13bpf: Migrate bpf_task_work_schedule_* kfuncs to KF_IMPLICIT_ARGSkernel/bpf/helpers.c kernel/bpf/verifier.c tools/testing/selftests/bpf/progs/file_reader.c +4 more+50 -341 reply
10/13bpf: Migrate bpf_stream_vprintk() to KF_IMPLICIT_ARGSkernel/bpf/helpers.c kernel/bpf/stream.c tools/lib/bpf/bpf_helpers.h +1 more+9 -100 repliesApplied
11/13selftests/bpf: Migrate struct_ops_assoc test to KF_IMPLICIT_ARGStools/testing/selftests/bpf/progs/struct_ops_assoc.c tools/testing/selftests/bpf/progs/struct_ops_assoc_in_timer.c tools/testing/selftests/bpf/progs/struct_ops_assoc_reuse.c +2 more+17 -160 repliesApplied
12/13bpf: Remove __prog kfunc arg annotationkernel/bpf/verifier.c+2 -90 repliesApplied
13/13bpf,docs: Document KF_IMPLICIT_ARGS flagDocumentation/bpf/kfuncs.rst+32 -170 repliesApplied

Reviews trailers per patch

11 of 13 patches have a Reviewed-by or Acked-by.

PatchEduard ZingermanBenjamin Tissoires
1/13R-b
2/13A-b
3/13A-b
4/13A-b
5/13A-b
6/13A-b
7/13R-b
8/13A-b
9/13R-b
10/13R-b
11/13
12/13A-b
13/13

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

Read the v3 thread

Cover letter [PATCH bpf-next v3 00/13] bpf: Kernel functions with KF_IMPLICIT_ARGS

This series implements a generic "implicit arguments" feature for BPF kernel functions. For context see prior work [1][2].

A mechanism is created for kfuncs to have arguments that are not visible to the BPF programs, and are provided to the kernel function implementation by the verifier.

This mechanism is then used in the kfuncs that have a parameter with __prog annotation [3], which is the current way of passing struct bpf_prog_aux pointer to kfuncs.

The function with implicit arguments is defined by KF_IMPLICIT_ARGS flag in BTF_IDS_FLAGS set. In this series, only a pointer to struct bpf_prog_aux can be implicit, although it is simple to extend this to more types.

The verifier handles a kfunc with KF_IMPLICIT_ARGS by resolving it to a different (actual) BTF prototype early in verification (patch #3).

A <kfunc>_impl function generated in BTF for a kfunc with implicit args does not have a "bpf_kfunc" decl tag, and a kernel address. The verifier will reject a program trying to call such an _impl kfunc.

The usage of <kfunc>_impl functions in BPF is only allowed for kfuncs with an explicit kernel (or kmodule) declaration, that is in "legacy" cases. As of this series, there are no legacy kernel functions, as all __prog users are migrated to KF_IMPLICIT_ARGS. However the implementation allows for legacy cases support in principle.

The series removes the following BPF kernel functions:
- bpf_stream_vprintk_impl
- bpf_task_work_schedule_resume_impl
- bpf_task_work_schedule_signal_impl
- bpf_wq_set_callback_impl

This will break existing BPF programs calling these functions (the verifier will not load them) on new kernels.

To mitigate, BPF users are advised to use the following pattern [4]:

    if (xxx_impl)
        xxx_impl(..., NULL);
    else
        xxx(...);

Which can be wrapped in a macro.

The series consists of the following patches:
- patches #1 and #2 are non-functional refactoring in kernel/bpf
- patch #3 defines KF_IMPLICIT_ARGS flag and teaches the verifier about it
- patches #4-#5 implement btf2btf transformation in resolve_btfids
- patch #6 adds selftests specific to KF_IMPLICIT_ARGS feature
- patches #7-#11 migrate the current users of __prog argument to KF_IMPLICIT_ARGS
- patch #12 removes __prog arg suffix support from the kernel
- patch #13 updates the docs

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://docs.kernel.org/bpf/kfuncs.html#prog-annotation
[4] https://lore.kernel.org/bpf/CAEf4BzbgPfRm9BX=TsZm-TsHFAHcwhPY4vTt=9OT-uhWqf8tqw@mail.gmail.com/

---

v2->v3:
- resolve_btfids: Use dynamic reallocation for btf2btf_context arrays (Andrii)
- resolve_btfids: Add missing free() for btf2btf_context arrays (AI)
- Other nits in resolve_btfids (Andrii, Eduard)

v2: https://lore.kernel.org/bpf/[email protected]/

v1->v2:
- Replace the following kernel functions with KF_IMPLICIT_ARGS version:
- bpf_stream_vprintk_impl -> bpf_stream_vprintk
- bpf_task_work_schedule_resume_impl -> bpf_task_work_schedule_resume
- bpf_task_work_schedule_signal_impl -> bpf_task_work_schedule_signal
- bpf_wq_set_callback_impl -> bpf_wq_set_callback_impl
- Remove __prog arg suffix support from the verifier
- Rework btf2btf implementation in resolve_btfids
- Do distill base and sort before BTF_ids patching
- Collect kfuncs based on BTF decl tags, before BTF_ids are patched
- resolve_btfids: use dynamic memory for intermediate data (Andrii)
- verifier: reset .subreg_def for caller saved registers on kfunc call (Eduard)
- selftests/hid: remove Makefile changes (Benjamin)
- selftests/bpf: Add a patch (#11) migrating struct_ops_assoc test to KF_IMPLICIT_ARGS
- Various nits across the series (Alexei, Andrii, Eduard)

v1: https://lore.kernel.org/bpf/[email protected]/

---

Ihor Solodrai (13):
bpf: Refactor btf_kfunc_id_set_contains
bpf: Introduce struct bpf_kfunc_meta
bpf: Verifier support for KF_IMPLICIT_ARGS
resolve_btfids: Introduce finalize_btf() step
resolve_btfids: Support for KF_IMPLICIT_ARGS selftests/bpf: Add tests for KF_IMPLICIT_ARGS
bpf: Migrate bpf_wq_set_callback_impl() to KF_IMPLICIT_ARGS
HID: Use bpf_wq_set_callback kernel function
bpf: Migrate bpf_task_work_schedule_* kfuncs to KF_IMPLICIT_ARGS
bpf: Migrate bpf_stream_vprintk() to KF_IMPLICIT_ARGS selftests/bpf: Migrate struct_ops_assoc test to KF_IMPLICIT_ARGS
bpf: Remove __prog kfunc arg annotation
bpf,docs: Document KF_IMPLICIT_ARGS flag

 Documentation/bpf/kfuncs.rst                  |  49 +-
 drivers/hid/bpf/progs/hid_bpf_helpers.h       |   8 +-
 include/linux/btf.h                           |   5 +-
 kernel/bpf/btf.c                              |  70 ++-
 kernel/bpf/helpers.c                          |  43 +-
 kernel/bpf/stream.c                           |   5 +-
 kernel/bpf/verifier.c                         | 245 ++++++----
 tools/bpf/resolve_btfids/main.c               | 451 +++++++++++++++++-
 tools/lib/bpf/bpf_helpers.h                   |   6 +-
 .../testing/selftests/bpf/bpf_experimental.h  |   5 -
 .../bpf/prog_tests/kfunc_implicit_args.c      |  10 +
 .../testing/selftests/bpf/progs/file_reader.c |   4 +-
 .../selftests/bpf/progs/kfunc_implicit_args.c |  41 ++
 .../testing/selftests/bpf/progs/stream_fail.c |   6 +-
 .../selftests/bpf/progs/struct_ops_assoc.c    |   8 +-
 .../bpf/progs/struct_ops_assoc_in_timer.c     |   4 +-
 .../bpf/progs/struct_ops_assoc_reuse.c        |   6 +-
 tools/testing/selftests/bpf/progs/task_work.c |  11 +-
 .../selftests/bpf/progs/task_work_fail.c      |  16 +-
 .../selftests/bpf/progs/task_work_stress.c    |   5 +-
 .../bpf/progs/verifier_async_cb_context.c     |  10 +-
 .../testing/selftests/bpf/progs/wq_failures.c |   4 +-
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  35 +-
 .../bpf/test_kmods/bpf_testmod_kfunc.h        |   6 +-
 .../selftests/hid/progs/hid_bpf_helpers.h     |   8 +-
 25 files changed, 837 insertions(+), 224 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_implicit_args.c
 create mode 100644 tools/testing/selftests/bpf/progs/kfunc_implicit_args.c
Signature · 1 line
2.52.0