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

Ihor Solodrai
2025-09-24T21:17:37Z

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/20250920005931.2753828-42-tj@kernel.org/
[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(-)

--
2.51.0