]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
bpf: Migrate bpf_task_work to kmalloc_nolock
authorMykyta Yatsenko <yatsenko@meta.com>
Mon, 30 Mar 2026 22:27:56 +0000 (15:27 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 2 Apr 2026 16:31:42 +0000 (09:31 -0700)
commit90f51ebff242e9446a9bbd3ce1a04d50c30e8b96
treef8c13bdd197761d7e07e101ab4eec8828d0de234
parentf7601044020ac1c55675ee59422ef75b25a31bed
bpf: Migrate bpf_task_work to kmalloc_nolock

Replace bpf_mem_alloc/bpf_mem_free with
kmalloc_nolock/kfree_rcu for bpf_task_work_ctx.

Replace guard(rcu_tasks_trace)() with guard(rcu)() in
bpf_task_work_irq(). The function only accesses ctx struct members
(not map values), so tasks trace protection is not needed - regular
RCU is sufficient since ctx is freed via kfree_rcu. The guard in
bpf_task_work_callback() remains as tasks trace since it accesses map
values from process context.

Sleepable BPF programs hold rcu_read_lock_trace but not
regular rcu_read_lock. Since kfree_rcu
waits for a regular RCU grace period, the ctx memory can be freed
while a sleepable program is still running. Add scoped_guard(rcu)
around the pointer read and refcount tryget in
bpf_task_work_acquire_ctx to close this race window.

Since kfree_rcu uses call_rcu internally which is not safe from
NMI context, defer destruction via irq_work when irqs are disabled.

For the lost-cmpxchg path the ctx was never published, so
kfree_nolock is safe.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20260330-kmalloc_special-v2-1-c90403f92ff0@meta.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/helpers.c