Skip to content

bpf: implicit bpf_prog_aux argument for kfuncs

Ihor Solodrai bpf dwarves 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest6 patches 16 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6bpf: implement KF_IMPLICIT_PROG_AUX_ARG flaginclude/linux/btf.h kernel/bpf/verifier.c+39 -79 replies
2/6bpf,docs: Add documentation for KF_IMPLICIT_PROG_AUX_ARGDocumentation/bpf/kfuncs.rst+38 -10 replies
3/6selftests/bpf: update bpf_wq_set_callback macrotools/testing/selftests/bpf/bpf_experimental.h+6 -13 replies
4/6bpf: implement bpf_wq_set_callback kfunc with implicit prog_auxkernel/bpf/helpers.c kernel/bpf/verifier.c tools/testing/selftests/bpf/progs/wq_failures.c+24 -130 replies
5/6bpf: mark bpf_stream_vprink kfunc with KF_IMPLICIT_PROG_AUX_ARGkernel/bpf/helpers.c kernel/bpf/stream.c tools/lib/bpf/bpf_helpers.h +1 more+7 -83 replies
6/6bpf: mark bpf_task_work_* kfuncs with KF_IMPLICIT_PROG_AUX_ARGkernel/bpf/helpers.c tools/testing/selftests/bpf/progs/task_work.c tools/testing/selftests/bpf/progs/task_work_fail.c +1 more+16 -161 reply

Reviews trailers per patch

1 of 6 patches has a Reviewed-by or Acked-by.

PatchMykyta Yatsenko
1/6
2/6
3/6
4/6
5/6
6/6A-b

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

Read the v1 thread

Cover letter [PATCH bpf-next v1 0/6] bpf: implicit bpf_prog_aux argument for kfuncs

This series implements KF_IMPLICIT_PROG_AUX_ARG kfunc flag. The flag indicates that the last argument of a BPF kfunc is a pointer to struct bpf_prog_aux implicitly set by the verifier.

There is already a mechanism for passing bpf_prog_aux to kfuncs:
__prog kfunc parameter annotation [1]. While it works technically, it requires explicit declaration of the argument in kfunc prototype in BPF (for example, in vmlinux.h) and a dummy actual parameter on call sites. Which then prompts adding macros like the following:

#define bpf_wq_set_callback(timer, cb, flags) \
bpf_wq_set_callback_impl(timer, cb, flags, NULL)

This is cumbersome in light of potentially many more kfuncs requiring access to bpf_prog_aux data [2].

An alternative approach is implemented in this series in combination with a change in pahole's BTF encoding [3].

When a kfunc needs access to bpf_prog_aux, it's declaration on the kernel side must have struct bpf_prog_aux pointer as it's *last* argument, and the kfunc must have KF_IMPLICIT_PROG_AUX_ARG flag set in its BTF_ID_FLAGS entry.

When generating BTF pahole recognizes KF_IMPLICIT_PROG_AUX_ARG flag and does not emit the last argument of the kfunc to BTF.

During BPF patching in the verifier KF_IMPLICIT_PROG_AUX_ARG is checked for to set the actual pointer, exactly as it happens for __prog annotation currently.

For backwards compatibility __prog annotation handling is preserved in the verifier. Existing kfuncs that use __prog annotation are updated to KF_IMPLICIT_PROG_AUX_ARG in this series.

Successful BPF CI run with modified pahole:
https://github.com/kernel-patches/bpf/actions/runs/17987713438

[1] https://docs.kernel.org/bpf/kfuncs.html#prog-annotation
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://github.com/theihor/dwarves/tree/prog-aux.v1

Ihor Solodrai (6):
bpf: implement KF_IMPLICIT_PROG_AUX_ARG flag bpf,docs: Add documentation for KF_IMPLICIT_PROG_AUX_ARG selftests/bpf: update bpf_wq_set_callback macro
bpf: implement bpf_wq_set_callback kfunc with implicit prog_aux
bpf: mark bpf_stream_vprink kfunc with KF_IMPLICIT_PROG_AUX_ARG
bpf: mark bpf_task_work_* kfuncs with KF_IMPLICIT_PROG_AUX_ARG

 Documentation/bpf/kfuncs.rst                  | 39 ++++++++++++-
 include/linux/btf.h                           |  3 +
 kernel/bpf/helpers.c                          | 36 +++++++-----
 kernel/bpf/stream.c                           |  3 +-
 kernel/bpf/verifier.c                         | 58 ++++++++++++++-----
 tools/lib/bpf/bpf_helpers.h                   |  4 +-
 .../testing/selftests/bpf/bpf_experimental.h  |  7 ++-
 .../testing/selftests/bpf/progs/stream_fail.c |  6 +-
 tools/testing/selftests/bpf/progs/task_work.c |  6 +-
 .../selftests/bpf/progs/task_work_fail.c      |  8 +--
 .../selftests/bpf/progs/task_work_stress.c    |  2 +-
 .../testing/selftests/bpf/progs/wq_failures.c |  4 +-
 12 files changed, 130 insertions(+), 46 deletions(-)
Signature · 1 line
2.51.0