This series introduces four new BPF-native inline helpers -- bpf_in_nmi(), bpf_in_hardirq(), bpf_in_serving_softirq(), and bpf_in_task() -- to allow BPF programs to query the current execution context.
Following the feedback on v1, these are implemented in bpf_experimental.h as inline helpers wrapping get_preempt_count(). This approach allows the logic to be JIT-inlined for better performance compared to a kfunc call, while providing the granular context detection (e.g., hardirq vs. softirq) required by subsystems like sched_ext. The series includes a new selftest suite, exe_ctx, which uses bpf_testmod to verify context detection across Task, HardIRQ, and SoftIRQ boundaries via irq_work and tasklets. NMI context testing is omitted as NMIs cannot be triggered deterministically within software-only BPF CI environments. ChangeLog v1 -> v2: - Dropped the core kernel kfunc implementations, and implemented context detection as inline BPF helpers in bpf_experimental.h. - Renamed the selftest suite from ctx_kfunc to exe_ctx to reflect the change from kfuncs to helpers. - Updated BPF programs to use the new inline helpers. - Swapped clean-up order between tasklet and irqwork in bpf_testmod to avoid re-scheduling the already-killed tasklet (reported by bot+bpf-ci). Changwoo Min (2): selftests/bpf: Introduce execution context detection helpers selftests/bpf: Add tests for execution context helpers .../testing/selftests/bpf/bpf_experimental.h | 38 ++++++++++++ .../selftests/bpf/prog_tests/exe_ctx.c | 59 +++++++++++++++++++ tools/testing/selftests/bpf/progs/test_ctx.c | 48 +++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 32 ++++++++++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 4 ++ 5 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/exe_ctx.c create mode 100644 tools/testing/selftests/bpf/progs/test_ctx.c -- 2.52.0

