With the verifier rules in place, make bpf_task_park() callable from BPF_PROG_TYPE_SYSCALL, as a sleepable kfunc. Syscall programs are the only type for now because their runner, bpf_prog_test_run_syscall(), is the one that has been audited against the sleep. The syscall holds a reference on the program, and through it on its maps, for the whole run, and the runner copies the context into kernel memory first, so neither depends on the Tasks Trace RCU read section the park drops. The run path keeps no per-CPU state across the program: it has no recursion counter, and __bpf_prog_run() samples its stats only after the program returns, with migration disabled again. Helpers confine their per-CPU scratch to the call that uses it, and the verifier keeps the park out of every region that pins the CPU and out of every callback. What remains is that the program may resume on another CPU, which the comment on bpf_prog_run_pin_on_cpu() now says, since the pin it promises is lifted for the sleep. Other program types stay off the list. A trampoline-attached sleepable program is freed after a Tasks Trace grace period the sleeper no longer delays and keeps a per-CPU recursion count that a migrating task would leave behind. Each new type needs its runner audited the same way first. Deferred: - A program type that runs with migration enabled and without Tasks Trace RCU, so that a BPF thread is a plain kernel-mode function of a user thread and the park needs no bracketing at all. It needs its own runner, since __bpf_prog_run() asserts cant_migrate(); an audit of every kfunc that relies on migration being disabled, starting with bpf_mem_alloc() and its users, per-CPU map lookups and bpf_this_cpu_ptr(); and a verifier rule that confines map helpers and map value pointers to bpf_rcu_read_lock() sections, since the helpers assert bpf_rcu_lock_held() and nothing else would protect the values. - Registering the park for further program types. Each needs a runner that holds the program across the sleep and no per-CPU recursion count. - Allowing the park in further callbacks whose kernel caller has been audited, beyond the body of a BPF kthread, which a later patch admits, and retaining dynptr and iterator stack slots beyond bpf_iter_num whose state does not depend on the dropped protection. - The stack in arena, the timed may_goto and tail calls in a program that parks. Each relies on a run staying on its CPU or under its caller's protection and is refused for now. - An opt-in flag to park with migration disabled, for per-CPU worker designs that accept the CPU offline cost. - A per-task marker so that bpf_task_wake() can only wake a task inside bpf_task_park(). Today a wake for a task that is not parked may end an unrelated interruptible sleep of that task, which the kernel contract permits. - Interoperation with user threads blocked in futex() on a word the program can also reach, such as an arena word. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/filter.h | 6 ++++++ kernel/bpf/task_park.c | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/linux/filter.h b/include/linux/filter.h index 61448be53f8d..c30d05a5c26e 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -883,6 +883,12 @@ static __always_inline u32 bpf_prog_run(const struct bpf_prog *prog, const void * This uses migrate_disable/enable() explicitly to document that the * invocation of a BPF program does not require reentrancy protection * against a BPF program which is invoked from a preempting task. + * + * A syscall program may call bpf_task_park(), which lifts the pin for the + * duration of its sleep and restores it before returning to the program, + * so such a program may resume on another CPU. Nothing on this path keeps + * per-CPU state across the program: __bpf_prog_run() samples its stats + * only after the program returns. */ static inline u32 bpf_prog_run_pin_on_cpu(const struct bpf_prog *prog, const void *ctx) diff --git a/kernel/bpf/task_park.c b/kernel/bpf/task_park.c index 5043ccdfc0b8..0e3d7f3ec597 100644 --- a/kernel/bpf/task_park.c +++ b/kernel/bpf/task_park.c @@ -276,3 +276,18 @@ __bpf_kfunc int bpf_task_wake(struct task_struct *task, u64 flags) } __bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(bpf_task_park_kfunc_ids) +BTF_ID_FLAGS(func, bpf_task_park, KF_SLEEPABLE) +BTF_KFUNCS_END(bpf_task_park_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_task_park_kfunc_set = { + .owner = THIS_MODULE, + .set = &bpf_task_park_kfunc_ids, +}; + +static int __init bpf_task_park_kfunc_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_task_park_kfunc_set); +} +late_initcall(bpf_task_park_kfunc_init); -- 2.53.0