bpf: Introduce deferred task context execution
From: | Mykyta Yatsenko <mykyta.yatsenko5-AT-gmail.com> | |
To: | bpf-AT-vger.kernel.org, ast-AT-kernel.org, andrii-AT-kernel.org, daniel-AT-iogearbox.net, kafai-AT-meta.com, kernel-team-AT-meta.com, eddyz87-AT-gmail.com | |
Subject: | [PATCH bpf-next 0/4] bpf: Introduce deferred task context execution | |
Date: | Wed, 06 Aug 2025 15:45:20 +0100 | |
Message-ID: | <20250806144554.576706-1-mykyta.yatsenko5@gmail.com> | |
Cc: | Mykyta Yatsenko <yatsenko-AT-meta.com> | |
Archive-link: | Article |
From: Mykyta Yatsenko <yatsenko@meta.com> This patch introduces a new mechanism for BPF programs to schedule deferred execution in the context of a specific task using the kernel’s task_work infrastructure. The new bpf_task_work interface enables BPF use cases that require sleepable subprogram execution within task context, for example, scheduling sleepable function from the context that does not allow sleepable, such as NMI. Introduced kfuncs bpf_task_work_schedule_signal() and bpf_task_work_schedule_resume() for scheduling BPF callbacks correspond to different modes used by task_work (TWA_SIGNAL or TWA_RESUME/TWA_NMI_CURRENT). The implementation leverages BPF maps for storing callback metadata. Indirectly call task_work_add() via irq_work to avoid locking in potentially NMI context. State transitions are managed via an atomic state machine (bpf_task_work_state) to ensure correctness under concurrent usage and deletion. Mykyta Yatsenko (4): bpf: bpf task work plumbing bpf: extract map key pointer calculation bpf: task work scheduling kfuncs selftests/bpf: BPF task work scheduling tests include/linux/bpf.h | 11 + include/uapi/linux/bpf.h | 4 + kernel/bpf/arraymap.c | 8 +- kernel/bpf/btf.c | 15 + kernel/bpf/hashtab.c | 22 +- kernel/bpf/helpers.c | 260 ++++++++++++++++-- kernel/bpf/syscall.c | 23 +- kernel/bpf/verifier.c | 131 ++++++++- tools/include/uapi/linux/bpf.h | 4 + .../selftests/bpf/prog_tests/test_task_work.c | 149 ++++++++++ tools/testing/selftests/bpf/progs/task_work.c | 108 ++++++++ .../selftests/bpf/progs/task_work_fail.c | 98 +++++++ 12 files changed, 800 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_task_work.c create mode 100644 tools/testing/selftests/bpf/progs/task_work.c create mode 100644 tools/testing/selftests/bpf/progs/task_work_fail.c -- 2.50.1