The body of a BPF kthread is an asynchronous callback, and the verifier refuses bpf_task_park() in every callback frame, so a BPF kthread cannot sleep. It can only poll in a bounded loop with migration disabled, which holds off CPU offline and any change of its affinity for as long as it runs, and stalls every Tasks Trace RCU grace period meanwhile. A thread that serves a queue or ticks a controller spends most of its life waiting, and the one primitive the program has for waiting is closed to it. The refusal exists because a callback runs from kernel code that keeps state of its own around the call, an iteration position, a timer or work item that owns the program only through Tasks Trace RCU, an exiting task, and none of that code has been audited against a sleep that drops the program's migration and RCU protection. bpf_kthread_fn() has been: it is the whole of the thread, brackets the callback the way the runner of a syscall program brackets the program, keeps nothing across it, and the program with its maps and code is held until the thread has exited, since the release of the program's last reference stops its threads and waits for them before its code and maps are freed. The callback's one argument is a scalar, so the park has no context pointer to keep alive either. Mark the frame the kthread callback runs in and let the walk that refuses the park skip it. Callbacks nested under the body, a bpf_loop() body or a timer callback the thread arms, stay refused as anywhere else, and a global function that may park is admitted from the thread body by the same walk. The rules on what survives the park are unchanged: the stack, array map values, arena memory and verifier-tracked references stay, and everything else is looked up again after the sleep. At runtime the park nests inside the bracketing of bpf_kthread_fn(): it enables migration and leaves the Tasks Trace RCU read section for the sleep and restores both before returning to the callback. A stop request ends the sleep, since kthread_stop() wakes the thread, and the park reports it as -ESHUTDOWN, as does every park after it, so a body that checks bpf_kthread_should_stop() around its park, or the park's return value, returns as before. The frame flag also takes part in state comparison. A static subprog may be passed both to bpf_kthread_create() and to bpf_wq_set_callback(), and both roles are verified as frame 0 of a sleepable asynchronous state over the same instructions, so a state explored in the kthread role, where the park is admitted, could stand for a state of the workqueue role, where it must be refused, or the reverse. bpf_state_subsumes() and join_func() therefore treat frames that differ in the flag as different: neither state is taken for the other, and the two are not joined. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 6 ++++++ kernel/bpf/join.c | 1 + kernel/bpf/kthread.c | 17 ++++++++++++----- kernel/bpf/states.c | 7 +++++++ kernel/bpf/task_park.c | 16 ++++++++-------- kernel/bpf/verifier.c | 31 +++++++++++++++++++++---------- 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 39ff1359adb9..7097bb7033e8 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -469,6 +469,12 @@ struct bpf_func_state { bool in_callback_fn; bool in_async_callback_fn; bool in_exception_callback_fn; + /* + * The body of a BPF kthread: an async callback whose kernel caller, + * bpf_kthread_fn(), keeps nothing across it that the sleep in + * bpf_task_park() could invalidate, so the park is allowed in it. + */ + bool in_kthread_fn; bool no_stack_arg_load; /* * The helper invokes this callback under rcu_read_lock(), so the diff --git a/kernel/bpf/join.c b/kernel/bpf/join.c index 8bd92af087c0..b63f99c66308 100644 --- a/kernel/bpf/join.c +++ b/kernel/bpf/join.c @@ -452,6 +452,7 @@ static bool join_func(struct bpf_join_state *js, struct bpf_func_state *fd, return false; if (fa->in_callback_fn != fb->in_callback_fn || fa->in_async_callback_fn != fb->in_async_callback_fn || + fa->in_kthread_fn != fb->in_kthread_fn || fa->in_exception_callback_fn != fb->in_exception_callback_fn || fa->in_rcu_callback_fn != fb->in_rcu_callback_fn || fa->async_entry_cnt != fb->async_entry_cnt || diff --git a/kernel/bpf/kthread.c b/kernel/bpf/kthread.c index 3055e427fe76..d8f63461d756 100644 --- a/kernel/bpf/kthread.c +++ b/kernel/bpf/kthread.c @@ -13,8 +13,8 @@ /* * A BPF kthread is a kernel thread whose thread function is one callback of * the program that created it, run once. The callback is the body of the - * thread: it loops, sleeps and returns on its own, and its return ends the - * thread. The kernel adds nothing in between. + * thread: it loops, sleeps in bpf_task_park() and returns on its own, and its + * return ends the thread. The kernel adds nothing in between. * * The thread is known to the program by its task_struct. bpf_kthread_create() * returns it as a referenced pointer, and the control kfuncs take it back, so @@ -99,9 +99,16 @@ static int bpf_kthread_fn(void *data) /* * The callback runs as the body of a sleepable program does, with * migration disabled under a Tasks Trace RCU read section, except - * while it waits in one of the kfuncs the comment at the top names, - * which drop migration disable alone. The verifier checks it as a - * sleepable callback. + * while it sleeps in bpf_task_park(), which drops both for the sleep + * and takes them again before it returns, and while it waits in one + * of the kfuncs the comment at the top names, which drop migration + * disable alone. That is + * safe here for the reason it is safe in a syscall program: this + * function keeps nothing across the callback, and the program, with + * its maps and code, is held until the thread has exited, since the + * release of its last reference stops the thread first. The verifier + * checks the callback as a sleepable callback in which the park is + * allowed. */ /* * A task work queued for this thread with TWA_SIGNAL sets diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c index f75161423377..d0db8074928a 100644 --- a/kernel/bpf/states.c +++ b/kernel/bpf/states.c @@ -759,6 +759,13 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state insn_idx = bpf_frame_insn_idx(old, i); if (old->frame[i]->callsite != cur->frame[i]->callsite) return false; + /* + * The body of a BPF kthread may park where another async + * callback of the same subprog may not, so a state verified + * in one role must not prune the other. + */ + if (old->frame[i]->in_kthread_fn != cur->frame[i]->in_kthread_fn) + return false; if (!func_states_equal(env, old->frame[i], cur->frame[i], insn_idx)) return false; } diff --git a/kernel/bpf/task_park.c b/kernel/bpf/task_park.c index 9de1b496ead1..b6980c1cc1bf 100644 --- a/kernel/bpf/task_park.c +++ b/kernel/bpf/task_park.c @@ -198,14 +198,14 @@ __bpf_kfunc int bpf_task_park(u64 timeout_ns, u64 flags) * the time slice of may_goto, is refused the call, and one that calls * it is kept out of prog arrays, where the caller of a tail call holds * it through this very read section. The verifier confines the call to - * such contexts: the main body of a syscall program and the functions it - * calls directly, never a callback, and never a region that pins the - * CPU, a lock, a saved IRQ state or an RCU read section. It also - * invalidates every pointer the program holds across the call except its - * stack, its context, maps it references directly and the values of its - * arrays, arena memory and verifier-tracked references. What remains is - * that the program may resume on another CPU, which is part of the - * contract. + * such contexts: the main body of a syscall program or of a BPF kthread + * and the functions either calls directly, never any other callback, + * and never a region that pins the CPU, a lock, a saved IRQ state or an + * RCU read section. It also invalidates every pointer the program holds + * across the call except its stack, its context, maps it references + * directly and the values of its arrays, arena memory and + * verifier-tracked references. What remains is that the program may + * resume on another CPU, which is part of the contract. */ migrate_enable(); rcu_read_unlock_trace(); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 0cf0d3a85b35..ee409ac5be77 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11226,14 +11226,22 @@ static void invalidate_non_owning_refs(struct bpf_verifier_env *env) } /* - * bpf_task_park() is confined to the main body of the program and the - * functions it calls directly. A callback runs from kernel code that holds - * state of its own around the call, an iteration position, a work item or - * timer that owns the program only through Tasks Trace RCU, an exiting task, - * none of it audited against a sleep that drops the program's protection. - * Refuse the park in any callback frame, synchronous, asynchronous or - * exception. Asynchronous callbacks are verified as a state of their own - * with the callback as frame 0, so the walk over the frames sees them too. + * bpf_task_park() is confined to the main body of the program, the body of a + * BPF kthread, and the functions either calls directly. Any other callback + * runs from kernel code that holds state of its own around the call, an + * iteration position, a work item or timer that owns the program only through + * Tasks Trace RCU, an exiting task, none of it audited against a sleep that + * drops the program's protection. Refuse the park in any such callback frame, + * synchronous, asynchronous or exception. Asynchronous callbacks are verified + * as a state of their own with the callback as frame 0, so the walk over the + * frames sees them too. + * + * The body of a BPF kthread is the exception because its caller, + * bpf_kthread_fn(), is the whole of the thread: it brackets the callback the + * way the runner of a syscall program brackets the program, keeps no state + * across it, and the program's release stops the thread and waits for it + * before anything the callback uses is freed. Callbacks nested under the + * body are refused as anywhere else. */ static bool in_callback_frames(struct bpf_verifier_env *env) { @@ -11243,6 +11251,8 @@ static bool in_callback_frames(struct bpf_verifier_env *env) for (i = 0; i <= vstate->curframe; i++) { struct bpf_func_state *frame = vstate->frame[i]; + if (frame->in_kthread_fn) + continue; if (frame->in_callback_fn || frame->in_async_callback_fn || frame->in_exception_callback_fn) return true; @@ -12159,7 +12169,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, operation = bpf_diag_fmt(env, "global function %s() that may call bpf_task_park()", sub_name); bpf_diag_policy(env, *insn_idx, operation, "the call is in a callback", - "Call the function from the main program or from a function it calls directly, not from a callback."); + "Call the function from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback."); return -EINVAL; } err = check_park_stack_objects(env, *insn_idx); @@ -12661,6 +12671,7 @@ static int set_kthread_callback_state(struct bpf_verifier_env *env, bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]); bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]); callee->in_async_callback_fn = true; + callee->in_kthread_fn = true; /* * The callback returns an int, which comes back in the low 32 bits of * R0: compare those, as for a global function. @@ -16511,7 +16522,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, if (in_callback_frames(env)) { verbose(env, "bpf_task_park() is not allowed in a callback\n"); bpf_diag_policy(env, insn_idx, "kfunc bpf_task_park", "the call is in a callback", - "Call bpf_task_park() from the main program or from a function it calls directly, not from a callback."); + "Call bpf_task_park() from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback."); return -EINVAL; } err = check_park_stack_objects(env, insn_idx); -- 2.53.0