Currently, there is no BPF primitive for a program to sleep for arbitrary lengths of time while waiting for a wake up controlled by another program to happen. Several BPF programs run in contexts where such unbounded sleeping can be harmful. Moreover, unbounded sleeps can be problematic, since a sleepable BPF program runs with migration disabled under a Tasks Trace RCU read section, and neither may be held across a sleep of unbounded length: the read section would stall every Tasks Trace grace period in the system, and a migration-disabled sleeper keeps rq->nr_pinned raised, which blocks CPU offline in balance_hotplug_wait() and pins its wakeup to the CPU it slept on. Introduce low-level primitives that let BPF programs construct such patterns. bpf_task_park() puts the current task to sleep and bpf_task_wake() makes a task runnable. The program keeps its own queue of waiters and its own condition for when the event has arrived, in arena memory, in graph nodes holding a task kptr, or in a map value, and checks the condition in a loop around the park: waiter waker while (!cond) cond = true; bpf_task_park(...); bpf_task_wake(task, 0); The kernel owns nothing per queue: there is no object to allocate, initialize, tear down, or synchronize against the map value going away. It keeps one word per task, bpf_wake_pending, which has to make that loop work. No wake is lost between the check and the sleep. The check is made in the program, so something has to carry a wake across the gap between it and the sleep, and the word does, the way the stop bit does for kthread_should_stop(). bpf_task_wake() stores the word and calls wake_up_state(); bpf_task_park() sets the task state, reads the word again, and sleeps only if it is clear. The barrier in set_current_state() pairs with the one in try_to_wake_up() that follows the waker's store, so either the waiter sees the pending wake or the waker sees it sleeping. A wake that finds its task running is kept until that task's next park, which then returns at once; a stale one costs a spurious turn of the loop. The waiter sees the condition once the park has returned. The pairing above covers the sleep, but it does not order the word against the program's own condition, and the loop depends on that order: the waker stores the condition and then wakes, the waiter returns from the park and then reads the condition, and the read must see the store. Two things provide it. bpf_task_wake() stores the word with xchg(), which is fully ordered, so the condition stores before the call are visible to whoever observes the word. bpf_task_park() consumes the word with xchg() on every return, so the condition loads after the return cannot be satisfied before the word was observed. On a weakly ordered architecture a plain store and a plain load would let the waiter observe the wake, read a stale condition, park again with the word already cleared, and sleep with no one left to wake it. A release is not enough for the store, because wakers race with each other. Of two wakers' stores one overwrites the other, and the waiter observes only the later one. A release orders a waker's condition before its own store of the word, for whoever reads that store; nobody reads the store that was overwritten, so nothing orders that waker's condition before the waiter's read of it. The waiter then consumes both wakes, misses one condition, and parks again while the waker saw it running. The kernel memory model allows that outcome with a release and forbids it with xchg(). The xchg() also makes the consume atomic. A wake that lands after the park observed the word but before it cleared it is not lost: the exchange returns the value at the instant of the reset, so the wake is either consumed by this park or still pending for the next one. bpf_task_park() returns: - 0 when it consumed a wake, or when the sleep ended for no recorded reason. The program tells a wake from such a spurious return by its own condition. - -ESHUTDOWN when a stop or park request for a kthread is pending. Nothing clears the request while the thread's function runs, so every later park that finds no wake pending fails the same way and the caller has to return, which is why it is not reported as a signal is. - -EINTR when a signal is pending. The signal and the request are both checked for every caller, since a kthread that called allow_signal() has signals too, and both before the sleep and after it. - -ETIMEDOUT when the timeout passed. A timeout of 0 polls and U64_MAX sleeps without a timer. When more than one of these applies, a wake wins over a stop request, a stop request over a signal, and either over the timeout. The wake wins because the waker may have dequeued or accounted for the sleeper on the strength of its wake having been delivered, and the sleeper's return value should agree, the way a futex that was woken returns 0 even if its timer also fired. The consuming xchg() is what makes it win: a wake that arrives while the timeout expires or a signal is noticed is reported rather than discarded with the word. A signal or a stop request wins over a timer that fired at the same time because the request stays pending and the timeout does not. schedule_hrtimeout() tells an expiry from an early return but not a wake from a signal, so the flags are read again after it and decide first. bpf_task_wake() returns 1 when the task was asleep and 0 when it was not. The sleep is interruptible for every caller. A kthread normally has no signals, so TASK_INTERRUPTIBLE is harmless there, avoids the load average and the hung task detector, and lets bpf_task_wake() use a TASK_INTERRUPTIBLE mask, which can never disturb an uninterruptible sleeper elsewhere in the kernel. The word is an int rather than a byte so that the exchange is a plain 4-byte xchg() on every architecture. dup_task_struct() clears it: a child inherits nothing from a wake addressed to its parent. The park drops migration and the Tasks Trace RCU section around the sleep. That is sound only where the invocation's runner holds the program, its maps and its context for the whole invocation, so that the program's own memory needs no grace period, and where nothing on the run path keeps per-CPU state across the program. The kfunc comment states that contract, and the verifier patches that follow confine the call to such contexts and invalidate what the sleep may free. Both kfuncs take a flags argument that must be 0. Only the two functions and the word they share are added here: later patches register the kfuncs, and until then nothing can call them. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/sched.h | 2 + kernel/bpf/Makefile | 2 +- kernel/bpf/task_park.c | 192 +++++++++++++++++++++++++++++++++++++++++ kernel/fork.c | 1 + 4 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/task_park.c diff --git a/include/linux/sched.h b/include/linux/sched.h index 1c93da75226e..43222c34fab8 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1630,6 +1630,8 @@ struct task_struct { struct bpf_local_storage __rcu *bpf_storage; /* Used for BPF run context */ struct bpf_run_ctx *bpf_ctx; + /* Set by bpf_task_wake(), consumed by bpf_task_park() */ + int bpf_wake_pending; #endif /* Used by BPF for per-TASK xdp storage */ struct bpf_net_context *bpf_net_context; diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index d26c62326e58..34357665c34c 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -15,7 +15,7 @@ obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o check_btf.o flow.o join.o obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o obj-$(CONFIG_BPF_JIT) += trampoline.o -obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o +obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o task_park.o ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy) obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o endif diff --git a/kernel/bpf/task_park.c b/kernel/bpf/task_park.c new file mode 100644 index 000000000000..39c9f085f7a9 --- /dev/null +++ b/kernel/bpf/task_park.c @@ -0,0 +1,192 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * A BPF program that blocks for an event keeps its own queue of waiters and + * its own notion of when the event has arrived. The kernel contributes two + * operations over the task: bpf_task_park() puts the current task to sleep + * and bpf_task_wake() makes a task runnable. The only shared state is one + * word in the task, bpf_wake_pending, which lets the program check its + * condition outside the kernel without losing a wake that lands between + * the check and the sleep: the waker sets the word before it wakes the + * task, and a park that finds it set returns instead of sleeping. + * + * The word also carries the ordering the program relies on. A waker + * publishes its condition and then calls bpf_task_wake(); a waiter reads + * the condition, finds it unmet, calls bpf_task_park(), and reads the + * condition again after the return. The second read must see the waker's + * condition whenever the park returned because of that wake, so the store + * of the word and the load that consumes it are both an xchg(): the store + * is fully ordered after the waker's condition stores, the load before the + * waiter's later condition loads. A release would not do for the store. Of + * two wakers' stores one overwrites the other, the waiter observes only the + * later one, and nothing would order the earlier waker's condition before + * the waiter's read of it. The xchg() also makes the consume atomic: a wake + * that lands between the waiter's observation of the word and its reset is + * not erased but stays pending for the next park. + */ + +/* + * What ends a park besides a wake and the timeout: a stop or park request for + * a kthread, or a signal. Both are checked for every caller, since a kthread + * that allowed signals has them too, and the kthread checks are false for a + * user task. The request is reported apart from a signal because nothing + * clears it while the thread's function runs: every later park that finds no + * wake pending fails the same way, so the caller has to return. + */ +static int bpf_task_park_interrupted(void) +{ + if (kthread_should_stop_or_park()) + return -ESHUTDOWN; + if (signal_pending(current)) + return -EINTR; + return 0; +} + +__bpf_kfunc_start_defs(); + +/** + * bpf_task_park - Sleep until bpf_task_wake() is called for the current task + * @timeout_ns: relative timeout, or U64_MAX for none + * @flags: must be 0 + * + * Returns at once if a wake is pending from a bpf_task_wake() that found the + * task running. Otherwise sleeps interruptibly until a wake, the timeout, a + * signal, or a stop or park request for a kthread. Every return past the + * argument check consumes the pending wake, and a wake takes precedence over + * a timeout, a signal or a stop request noticed at the same time, since the + * waker may already have accounted for it. Any return hands control back to + * the program's own check of its condition, and spurious returns are + * possible. A @timeout_ns of 0 polls: it reports a pending wake, + * then a stop or park request as -ESHUTDOWN, a pending signal as -EINTR, and + * otherwise -ETIMEDOUT, without sleeping. + * + * Return: 0 when a wake was consumed or the sleep ended for no recorded + * reason, -ETIMEDOUT when the timeout passed, -ESHUTDOWN when a stop or park + * request for a kthread is pending and -EINTR when a signal is, whether it + * was found before the sleep or ended it, even if the timeout passed + * meanwhile, -EINVAL when @flags is not 0. Nothing clears a stop or park + * request while the thread's function runs, so once a call has returned + * -ESHUTDOWN, every later one that finds no wake pending does too. + */ +__bpf_kfunc int bpf_task_park(u64 timeout_ns, u64 flags) +{ + ktime_t expires = min_t(u64, timeout_ns, KTIME_MAX); + bool expired; + int ret = 0; + + if (flags) + return -EINVAL; + if (READ_ONCE(current->bpf_wake_pending)) + goto out; + + /* + * set_current_state() is a full barrier that pairs with the one in + * try_to_wake_up() after the waker's store of bpf_wake_pending: + * either this task observes the pending wake below or the waker + * observes it sleeping and wakes it. + */ + set_current_state(TASK_INTERRUPTIBLE); + if (READ_ONCE(current->bpf_wake_pending)) { + __set_current_state(TASK_RUNNING); + goto out; + } + /* + * kthread_stop(), kthread_park() and signal delivery set their flag + * and wake the task in the same way, so the same ordering covers + * them. + */ + ret = bpf_task_park_interrupted(); + if (ret) { + __set_current_state(TASK_RUNNING); + goto out; + } + + /* + * Sleepable programs run with migration disabled under a Tasks Trace + * RCU read section. Neither may be held across a sleep of unbounded + * length: the read section would stall every Tasks Trace grace period + * in the system, and a migration-disabled sleeper keeps rq->nr_pinned + * raised, which blocks CPU offline in balance_hotplug_wait() and pins + * the wakeup to the CPU it slept on. + * + * Dropping both is correct only where the caller's runner holds the + * program, and with it its maps and context, for the whole + * invocation, so that the program's own memory needs no grace period, + * and where nothing on the run path keeps per-CPU state across the + * program. 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. + */ + migrate_enable(); + rcu_read_unlock_trace(); + /* + * A NULL expiry sleeps without a timer; a zero one returns at once. + * schedule_hrtimeout() tells an expiry from an early return, but not + * a wake from a signal or a stop or park request, so read the flags + * again, as before the sleep. They win over an expiry that fired at + * the same time, since the request stays pending and the timeout + * does not; a wake, consumed below, wins over both. + */ + expired = !schedule_hrtimeout(timeout_ns == U64_MAX ? NULL : &expires, HRTIMER_MODE_REL); + ret = bpf_task_park_interrupted(); + if (!ret && expired) + ret = -ETIMEDOUT; + rcu_read_lock_trace(); + migrate_disable(); +out: + /* + * Consume the wake. xchg() is fully ordered, so the condition loads + * the program makes after the return see the stores the waker made + * before its bpf_task_wake(). A wake that raced with the timeout, the + * signal or the stop request wins: the waker may have accounted for it. + */ + if (xchg(¤t->bpf_wake_pending, 0)) + ret = 0; + return ret; +} + +/** + * bpf_task_wake - Wake a task parked in bpf_task_park() + * @task: task to wake + * @flags: must be 0 + * + * Marks a wake pending for @task and wakes it if it sleeps interruptibly. + * A task that is not parked keeps the pending wake until its next + * bpf_task_park(), which then returns at once. + * + * Return: 1 when @task was asleep and is now runnable, 0 when it was not, + * -EINVAL when @flags is not 0. + */ +__bpf_kfunc int bpf_task_wake(struct task_struct *task, u64 flags) +{ + if (flags) + return -EINVAL; + + /* + * xchg() is a full barrier. It orders the program's stores before this + * call, its condition, before the word, so a park that consumes the + * word sees them, also when what it consumes is another waker's store + * that overwrote this one. try_to_wake_up() then orders the store + * before its load of the task state, pairing with set_current_state() + * in bpf_task_park(). + */ + xchg(&task->bpf_wake_pending, 1); + return wake_up_state(task, TASK_INTERRUPTIBLE); +} + +__bpf_kfunc_end_defs(); diff --git a/kernel/fork.c b/kernel/fork.c index 8b108ed37152..b66eb25626c9 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1019,6 +1019,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) #ifdef CONFIG_BPF_SYSCALL RCU_INIT_POINTER(tsk->bpf_storage, NULL); tsk->bpf_ctx = NULL; + tsk->bpf_wake_pending = 0; #endif return tsk; -- 2.53.0