bpf: Use dedicated workqueue for freeing BPF programs
Emil Tsalapatis
reviewerPATCH v2 1/1 bpf: Use dedicated workqueue for freeing BPF programs
BPF program freeing currently runs from the system workqueue. With BPF program pack allocations, freeing the last program in a pack can take pack_mutex and call into execmem/vmalloc code to change page permissions. That path may queue a vmalloc purge helper to the same CPU and wait for it to finish.
If enough other BPF program free workers are already active on that CPU, they can all block on pack_mutex and consume the system workqueue active slots. The worker holding pack_mutex then waits for the vmalloc purge work, but that purge work cannot run because the queue is full of pack_mutex waiters:
[ 24.646667] INFO: task kworker/0:4:1291 blocked for more than 10 seconds. [ 24.646669] Not tainted 7.3.0-00001-gdeaa120a9b25-dirty #11 [ 24.646670] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 24.646671] task:kworker/0:4 state:D stack:0 pid:1291 tgid:1291 ppid:2 task_flags:0x4208060 flags:0x00080000 [ 24.646675] Workqueue: events bpf_prog_free_deferred [ 24.646678] Call Trace: [ 24.646680] <TASK> [ 24.646686] __schedule+0x5bc/0x1430 [ 24.646702] schedule+0x3f/0x100 [ 24.646706] schedule_timeout+0x106/0x110 [ 24.646711] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646713] ? mark_held_locks+0x49/0x80 [ 24.646720] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646723] ? lockdep_hardirqs_on_prepare+0xdc/0x190 [ 24.646726] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646732] __wait_for_common+0xc1/0x1d0 [ 24.646735] ? __pfx_schedule_timeout+0x10/0x10 [ 24.646748] ? __flush_work+0x1f8/0x550 [ 24.646751] __flush_work+0x309/0x550 [ 24.646759] ? __pfx_wq_barrier_func+0x10/0x10 [ 24.646783] __purge_vmap_area_lazy+0x3e4/0x440 [ 24.646796] _vm_unmap_aliases+0x2d5/0x330 [ 24.646810] change_page_attr_set_clr+0xb9/0x580 [ 24.646817] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646833] set_memory_nx+0x3b/0x50 [ 24.646838] __execmem_cache_free+0x35/0xb0 [ 24.646848] execmem_free+0x98/0x160 [ 24.646864] bpf_prog_pack_free+0x152/0x190 [ 24.646871] bpf_jit_binary_pack_free+0x17/0x90 [ 24.646876] bpf_jit_free+0x5e/0x130 [ 24.646882] process_one_work+0x289/0x790 [ 24.646886] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646888] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646902] worker_thread+0x1b3/0x370 [ 24.646908] ? __pfx_worker_thread+0x10/0x10 [ 24.646911] kthread+0xf7/0x130 [ 24.646914] ? __pfx_kthread+0x10/0x10 [ 24.646920] ret_from_fork+0x293/0x300 [ 24.646923] ? __pfx_kthread+0x10/0x10 [ 24.646927] ret_from_fork_asm+0x1a/0x30 [ 24.646947] </TASK>
Move deferred freeing of BPF programs, program images, and trampoline images to a dedicated workqueue to avoid this deadlock.
---
v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/):
- Rebase onto the current bpf tree and move RCU-delayed program-image freeing to the dedicated workqueue after the upstream symbol-lifetime change. (Alexei, Andrii)
- Move deferred trampoline-image freeing to the same workqueue. (Alexei, Andrii)
- Set max_active to 1 to limit workers waiting on pack_mutex. (Alexei)
- Correct the Fixes: tag to 72210662c5a2 ("mm: vmalloc: offload free_vmap_area_lock lock"). (Alexei)
- Drop the standalone stress selftest and send a single patch. (Andrii)
--- kernel/bpf/core.c | 21 +++++++++++++++++++-- kernel/bpf/trampoline.c | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-)
kernel/bpf/core.c +19 -2
@@ -24,6 +24,7 @@#include <linux/skbuff.h> #include <linux/static_call.h> #include <linux/vmalloc.h> +#include <linux/workqueue.h> #include <linux/prandom.h> #include <linux/bpf.h> #include <linux/btf.h>
@@ -3143,6 +3144,17 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)kfree (aux->used_btfs); } +struct workqueue_struct *bpf_prog_free_wq__ro_after_init ; + +static int __init bpf_prog_free_wq_init (void ) +{ + bpf_prog_free_wq =alloc_workqueue ("bpf_prog_free" ,WQ_UNBOUND ,1 ); +if (!bpf_prog_free_wq) +panic ("Failed to allocate BPF program free workqueue\n" ); +return 0 ; +} +early_initcall (bpf_prog_free_wq_init); +static void bpf_prog_free_images (struct work_struct *work) {struct bpf_prog_aux *aux;
@@ -3198,7 +3210,7 @@ static void bpf_prog_free_deferred(struct work_struct *work)/* The symbols are looked up under rcu_read_lock() */ INIT_RCU_WORK (&aux->rwork, bpf_prog_free_images);if (ksyms) -queue_rcu_work (system_percpu_wq, &aux->rwork); +queue_rcu_work (bpf_prog_free_wq, &aux->rwork);else bpf_prog_free_images (&aux->rwork.work); }
@@ -3211,7 +3223,12 @@ void bpf_prog_free(struct bpf_prog *fp)bpf_prog_put (aux->dst_prog);bpf_token_put (aux->token);INIT_WORK (&aux->work, bpf_prog_free_deferred); -schedule_work (&aux->work); +/* +* A pack_mutex holder may wait for vmalloc work on system_percpu_wq. +* Keep program frees off that queue so mutex waiters cannot exhaust +* the active slots needed by the vmalloc work. +*/ +queue_work (bpf_prog_free_wq, &aux->work); }/* RNG for unprivileged user space with separated state from prandom_u32(). */
kernel/bpf/trampoline.c +3 -1
@@ -577,6 +577,8 @@ static void __bpf_tramp_image_put_deferred(struct work_struct *work)bpf_trampoline_put (tr); } +extern struct workqueue_struct *bpf_prog_free_wq__ro_after_init ; +/* callback, fexit step 3 or fentry step 2 */ static void __bpf_tramp_image_put_rcu (struct rcu_head *rcu) {
@@ -584,7 +586,7 @@ static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) im = container_of(rcu, struct bpf_tramp_image, rcu);INIT_WORK (&im->work, __bpf_tramp_image_put_deferred); -schedule_work (&im->work); +queue_work (bpf_prog_free_wq, &im->work); }/* callback, fexit step 2. Called after percpu_ref_kill confirms. */
Signature · 1 line
2.54.0
[email protected]
AI reviewTL;DR: Design question: the new bpf_prog_free workqueue uses max_active=1, so slow frees (rtnl_lock, cgroup_lock) may block all other BPF frees, delaying memory release. Would the default or…
Quoted 1 line
bpf: Use dedicated workqueue for freeing BPF programs
TL;DR: Design question: the new bpf_prog_free workqueue uses max_active=1, so slow frees (rtnl_lock, cgroup_lock) may block all other BPF frees, delaying memory release. Would the default or WQ_PERCPU fit better?
Quoted 3 lines
kernel/bpf/core.c +0
-0
[ ... ]
kernel/bpf/core.c static void bpf_free_used_btfs(struct bpf_prog_aux *aux) · line 3151
Quoted 9 lines
kernel/bpf/core.c +5
-0@@ -3143,6 +3144,17 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)kfree (aux->used_btfs); } +struct workqueue_struct *bpf_prog_free_wq__ro_after_init ; + +static int __init bpf_prog_free_wq_init (void ) +{ + bpf_prog_free_wq =alloc_workqueue ("bpf_prog_free" ,WQ_UNBOUND ,1 );
Is max_active=1 intended here? With WQ_UNBOUND, max_active is split across NUMA nodes, with a per-node minimum of 1, so a single-node machine runs exactly one queued item at a time.
Before this patch these items ran on system_percpu_wq with up to WQ_DFL_ACTIVE (1024) active items per CPU. Several of the work functions sleep on locks unrelated to pack_mutex:
bpf_prog_free_deferred()
bpf_prog_dev_bound_destroy()
rtnl_lock()
bpf_prog_free_deferred()
bpf_cgroup_atype_put()
cgroup_lock()
__bpf_tramp_image_put_deferred()
trampoline_lock(tr)
bpf_prog_pack_free()
__purge_vmap_area_lazy()
flush_work()
While one of these sleeps, every other queued free waits behind it, including the cBPF frees done on process exit with a seccomp filter (seccomp_filter_free() -> bpf_prog_destroy() -> bpf_prog_free()) and socket/tun/cls_bpf filter frees. The memory, map/BTF/module references and JIT charge are only released once the queue drains.
The vmalloc purge helpers are queued with schedule_work_on() on system_percpu_wq, not on bpf_prog_free_wq, so a default max_active of 0 would also avoid the deadlock. The cgroup_bpf_destroy_wq in kernel/bpf/cgroup.c uses alloc_workqueue(..., WQ_PERCPU, 1), which keeps one active item per CPU rather than one per node.
The commit message doesn't say why system-wide serialization was chosen. Would 0 (the default) or WQ_PERCPU be a better fit here?
kernel/bpf/core.c static void bpf_free_used_btfs(struct bpf_prog_aux *aux)
Quoted 5 lines
kernel/bpf/core.c +0
-0if (!bpf_prog_free_wq)panic ("Failed to allocate BPF program free workqueue\n" );return 0 ; }early_initcall (bpf_prog_free_wq_init);
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/42089627629
Emil Tsalapatis
reviewerQuoted 52 lines · On Thu, Nov 5, 2026 at 7:32 PM <[email protected]> wrote:
Quoted 2 lines
bpf: Use dedicated workqueue for freeing BPF programs
TL;DR: Design question: the new
bpf_prog_freeworkqueue uses max_active=1, so slow frees (rtnl_lock,cgroup_lock) may block all other BPF frees, delaying memory release. Would the default orWQ_PERCPUfit better?Quoted 3 lines
kernel/bpf/core.c +0
-0[ ... ]
kernel/bpf/core.c static void bpf_free_used_btfs(struct bpf_prog_aux *aux) · line 3151
Quoted 9 lines
kernel/bpf/core.c +5
-0@@ -3143,6 +3144,17 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)kfree (aux->used_btfs); } +struct workqueue_struct *bpf_prog_free_wq__ro_after_init ; + +static int __init bpf_prog_free_wq_init (void ) +{ + bpf_prog_free_wq =alloc_workqueue ("bpf_prog_free" ,WQ_UNBOUND ,1 );Is max_active=1 intended here? With
WQ_UNBOUND,max_activeis split across NUMA nodes, with a per-node minimum of 1, so a single-node machine runs exactly one queued item at a time.Before this patch these items ran on
system_percpu_wqwith up toWQ_DFL_ACTIVE(1024) active items per CPU. Several of the work functions sleep on locks unrelated topack_mutex:
bpf_prog_free_deferred()
bpf_prog_dev_bound_destroy()rtnl_lock()
bpf_prog_free_deferred()
bpf_cgroup_atype_put()
cgroup_lock()
__bpf_tramp_image_put_deferred()
trampoline_lock(tr)
bpf_prog_pack_free()
__purge_vmap_area_lazy()
flush_work()While one of these sleeps, every other queued free waits behind it, including the cBPF frees done on process exit with a seccomp filter (
seccomp_filter_free()->bpf_prog_destroy()->bpf_prog_free()) and socket/tun/cls_bpf filter frees. The memory, map/BTF/module references and JIT charge are only released once the queue drains.
Quoted 9 lines
The vmalloc purge helpers are queued with
schedule_work_on()onsystem_percpu_wq, not onbpf_prog_free_wq, so a defaultmax_activeof 0 would also avoid the deadlock. Thecgroup_bpf_destroy_wqinkernel/bpf/cgroup.cusesalloc_workqueue(..., WQ_PERCPU, 1), which keeps one active item per CPU rather than one per node.The commit message doesn't say why system-wide serialization was chosen. Would 0 (the default) or
WQ_PERCPUbe a better fit here?
I think WQ_PERCPU is a valid tradeoff between 0 and WQ_UNBOUND/1. We don't want unnecessary contention, but we also don't want spurious implicit contention between unrelated tasks (e.g., some work item waiting on the RTNL lock is blocking
unrelated free operations). The fact that the items get queued to the CPU queueing
the item is also fine and an improvement over sending it to a per-NUMA queue.
@Alexei wdyt?
Quoted 14 lines
kernel/bpf/core.c static void bpf_free_used_btfs(struct bpf_prog_aux *aux)
Quoted 5 lines
kernel/bpf/core.c +0
-0if (!bpf_prog_free_wq)panic ("Failed to allocate BPF program free workqueue\n" );return 0 ; }early_initcall (bpf_prog_free_wq_init);[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/42089627629
Quoted 119 lines · Emil Tsalapatis
BPF program freeing currently runs from the system workqueue. With BPF program pack allocations, freeing the last program in a pack can take
pack_mutexand call into execmem/vmalloc code to change page permissions. That path may queue a vmalloc purge helper to the same CPU and wait for it to finish.If enough other BPF program free workers are already active on that CPU, they can all block on
pack_mutexand consume the system workqueue active slots. The worker holdingpack_mutexthen waits for the vmalloc purge work, but that purge work cannot run because the queue is full ofpack_mutexwaiters:[ 24.646667] INFO: task kworker/0:4:1291 blocked for more than 10 seconds. [ 24.646669] Not tainted 7.3.0-00001-gdeaa120a9b25-dirty #11 [ 24.646670] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 24.646671] task:kworker/0:4 state:D stack:0 pid:1291 tgid:1291 ppid:2 task_flags:0x4208060 flags:0x00080000 [ 24.646675] Workqueue: events bpf_prog_free_deferred [ 24.646678] Call Trace: [ 24.646680] <TASK> [ 24.646686] __schedule+0x5bc/0x1430 [ 24.646702] schedule+0x3f/0x100 [ 24.646706] schedule_timeout+0x106/0x110 [ 24.646711] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646713] ? mark_held_locks+0x49/0x80 [ 24.646720] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646723] ? lockdep_hardirqs_on_prepare+0xdc/0x190 [ 24.646726] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646732] __wait_for_common+0xc1/0x1d0 [ 24.646735] ? __pfx_schedule_timeout+0x10/0x10 [ 24.646748] ? __flush_work+0x1f8/0x550 [ 24.646751] __flush_work+0x309/0x550 [ 24.646759] ? __pfx_wq_barrier_func+0x10/0x10 [ 24.646783] __purge_vmap_area_lazy+0x3e4/0x440 [ 24.646796] _vm_unmap_aliases+0x2d5/0x330 [ 24.646810] change_page_attr_set_clr+0xb9/0x580 [ 24.646817] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646833] set_memory_nx+0x3b/0x50 [ 24.646838] __execmem_cache_free+0x35/0xb0 [ 24.646848] execmem_free+0x98/0x160 [ 24.646864] bpf_prog_pack_free+0x152/0x190 [ 24.646871] bpf_jit_binary_pack_free+0x17/0x90 [ 24.646876] bpf_jit_free+0x5e/0x130 [ 24.646882] process_one_work+0x289/0x790 [ 24.646886] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646888] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646902] worker_thread+0x1b3/0x370 [ 24.646908] ? __pfx_worker_thread+0x10/0x10 [ 24.646911] kthread+0xf7/0x130 [ 24.646914] ? __pfx_kthread+0x10/0x10 [ 24.646920] ret_from_fork+0x293/0x300 [ 24.646923] ? __pfx_kthread+0x10/0x10 [ 24.646927] ret_from_fork_asm+0x1a/0x30 [ 24.646947] </TASK>Move deferred freeing of BPF programs, program images, and trampoline images to a dedicated workqueue to avoid this deadlock.
Fixes 72210662c5a2 ("mm: vmalloc: offload free_vmap_area_lock lock")Reported-by Andrii Nakryiko <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>---
v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/):- Rebase onto the current bpf tree and move RCU-delayed program-image freeing to the dedicated workqueue after the upstream symbol-lifetime change. (Alexei, Andrii)
- Move deferred trampoline-image freeing to the same workqueue. (Alexei, Andrii)
- Setmax_activeto 1 to limit workers waiting onpack_mutex. (Alexei)
- Correct theFixes:tag to72210662c5a2("mm: vmalloc: offloadfree_vmap_area_locklock"). (Alexei)
- Drop the standalone stress selftest and send a single patch. (Andrii)--- kernel/bpf/core.c | 21 +++++++++++++++++++-- kernel/bpf/trampoline.c | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-)kernel/bpf/core.c +13
-1@@ -24,6 +24,7 @@#include <linux/skbuff.h> #include <linux/static_call.h> #include <linux/vmalloc.h> +#include <linux/workqueue.h> #include <linux/prandom.h> #include <linux/bpf.h> #include <linux/btf.h> @@ -3143,6 +3144,17 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)kfree (aux->used_btfs); } +struct workqueue_struct *bpf_prog_free_wq__ro_after_init ; + +static int __init bpf_prog_free_wq_init (void ) +{ + bpf_prog_free_wq =alloc_workqueue ("bpf_prog_free" ,WQ_UNBOUND ,1 ); +if (!bpf_prog_free_wq) +panic ("Failed to allocate BPF program free workqueue\n" ); +return 0 ; +} +early_initcall (bpf_prog_free_wq_init); +static void bpf_prog_free_images (struct work_struct *work) {struct bpf_prog_aux *aux;@@ -3198,7 +3210,7 @@ static void bpf_prog_free_deferred(struct work_struct *work)/* The symbols are looked up under rcu_read_lock() */ INIT_RCU_WORK (&aux->rwork, bpf_prog_free_images);if (ksyms) -queue_rcu_work (system_percpu_wq, &aux->rwork); +queue_rcu_work (bpf_prog_free_wq, &aux->rwork);else bpf_prog_free_images (&aux->rwork.work); }
kernel/bpf/core.c void bpf_prog_free(struct bpf_prog *fp) · line 3230
Quoted 9 lines
kernel/bpf/core.c +5
-1@@ -3211,7 +3223,12 @@ void bpf_prog_free(struct bpf_prog *fp)bpf_prog_put (aux->dst_prog);bpf_token_put (aux->token);INIT_WORK (&aux->work, bpf_prog_free_deferred); -schedule_work (&aux->work); +/* +* A pack_mutex holder may wait for vmalloc work on system_percpu_wq. +* Keep program frees off that queue so mutex waiters cannot exhaust +* the active slots needed by the vmalloc work. +*/
very specific pack_mutex reference is more confusing than helpful, imo. should we keep it generic and say that bpf_prog freeing goes through its own workqueue to not accidentally block or slowdown system queue or something along those lines?
kernel/bpf/trampoline.c static void __bpf_tramp_image_put_deferred(struct work_struct *work) · line 580
Quoted 13 lines
kernel/bpf/core.c +1
-0+queue_work (bpf_prog_free_wq, &aux->work); }/* RNG for unprivileged user space with separated state from prandom_u32(). */ kernel/bpf/trampoline.c +1
-0@@ -577,6 +577,8 @@ static void __bpf_tramp_image_put_deferred(struct work_struct *work)bpf_trampoline_put (tr); } +extern struct workqueue_struct *bpf_prog_free_wq__ro_after_init ;
this extern should be in include/linux/bpf.h, please move it there
pw-bot: cr
Quoted 16 lines
kernel/bpf/trampoline.c +2
-2+/* callback, fexit step 3 or fentry step 2 */ static void __bpf_tramp_image_put_rcu (struct rcu_head *rcu) {@@ -584,7 +586,7 @@ static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) im = container_of(rcu, struct bpf_tramp_image, rcu);INIT_WORK (&im->work, __bpf_tramp_image_put_deferred); -schedule_work (&im->work); +queue_work (bpf_prog_free_wq, &im->work); }/* callback, fexit step 2. Called after percpu_ref_kill confirms. */ --2.54.0
Emil Tsalapatis
reviewerQuoted 127 lines · Andrii Nakryiko
Quoted 125 lines · Emil Tsalapatis
BPF program freeing currently runs from the system workqueue. With BPF program pack allocations, freeing the last program in a pack can take
pack_mutexand call into execmem/vmalloc code to change page permissions. That path may queue a vmalloc purge helper to the same CPU and wait for it to finish.If enough other BPF program free workers are already active on that CPU, they can all block on
pack_mutexand consume the system workqueue active slots. The worker holdingpack_mutexthen waits for the vmalloc purge work, but that purge work cannot run because the queue is full ofpack_mutexwaiters:[ 24.646667] INFO: task kworker/0:4:1291 blocked for more than 10 seconds. [ 24.646669] Not tainted 7.3.0-00001-gdeaa120a9b25-dirty #11 [ 24.646670] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 24.646671] task:kworker/0:4 state:D stack:0 pid:1291 tgid:1291 ppid:2 task_flags:0x4208060 flags:0x00080000 [ 24.646675] Workqueue: events bpf_prog_free_deferred [ 24.646678] Call Trace: [ 24.646680] <TASK> [ 24.646686] __schedule+0x5bc/0x1430 [ 24.646702] schedule+0x3f/0x100 [ 24.646706] schedule_timeout+0x106/0x110 [ 24.646711] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646713] ? mark_held_locks+0x49/0x80 [ 24.646720] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646723] ? lockdep_hardirqs_on_prepare+0xdc/0x190 [ 24.646726] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646732] __wait_for_common+0xc1/0x1d0 [ 24.646735] ? __pfx_schedule_timeout+0x10/0x10 [ 24.646748] ? __flush_work+0x1f8/0x550 [ 24.646751] __flush_work+0x309/0x550 [ 24.646759] ? __pfx_wq_barrier_func+0x10/0x10 [ 24.646783] __purge_vmap_area_lazy+0x3e4/0x440 [ 24.646796] _vm_unmap_aliases+0x2d5/0x330 [ 24.646810] change_page_attr_set_clr+0xb9/0x580 [ 24.646817] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646833] set_memory_nx+0x3b/0x50 [ 24.646838] __execmem_cache_free+0x35/0xb0 [ 24.646848] execmem_free+0x98/0x160 [ 24.646864] bpf_prog_pack_free+0x152/0x190 [ 24.646871] bpf_jit_binary_pack_free+0x17/0x90 [ 24.646876] bpf_jit_free+0x5e/0x130 [ 24.646882] process_one_work+0x289/0x790 [ 24.646886] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646888] ? srso_alias_return_thunk+0x5/0xfbef5 [ 24.646902] worker_thread+0x1b3/0x370 [ 24.646908] ? __pfx_worker_thread+0x10/0x10 [ 24.646911] kthread+0xf7/0x130 [ 24.646914] ? __pfx_kthread+0x10/0x10 [ 24.646920] ret_from_fork+0x293/0x300 [ 24.646923] ? __pfx_kthread+0x10/0x10 [ 24.646927] ret_from_fork_asm+0x1a/0x30 [ 24.646947] </TASK>Move deferred freeing of BPF programs, program images, and trampoline images to a dedicated workqueue to avoid this deadlock.
Fixes 72210662c5a2 ("mm: vmalloc: offload free_vmap_area_lock lock")Reported-by Andrii Nakryiko <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>---
v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/):- Rebase onto the current bpf tree and move RCU-delayed program-image freeing to the dedicated workqueue after the upstream symbol-lifetime change. (Alexei, Andrii)
- Move deferred trampoline-image freeing to the same workqueue. (Alexei, Andrii)
- Setmax_activeto 1 to limit workers waiting onpack_mutex. (Alexei)
- Correct theFixes:tag to72210662c5a2("mm: vmalloc: offloadfree_vmap_area_locklock"). (Alexei)
- Drop the standalone stress selftest and send a single patch. (Andrii)--- kernel/bpf/core.c | 21 +++++++++++++++++++-- kernel/bpf/trampoline.c | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-)kernel/bpf/core.c +15
-2@@ -24,6 +24,7 @@#include <linux/skbuff.h> #include <linux/static_call.h> #include <linux/vmalloc.h> +#include <linux/workqueue.h> #include <linux/prandom.h> #include <linux/bpf.h> #include <linux/btf.h> @@ -3143,6 +3144,17 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)kfree (aux->used_btfs); } +struct workqueue_struct *bpf_prog_free_wq__ro_after_init ; + +static int __init bpf_prog_free_wq_init (void ) +{ + bpf_prog_free_wq =alloc_workqueue ("bpf_prog_free" ,WQ_UNBOUND ,1 ); +if (!bpf_prog_free_wq) +panic ("Failed to allocate BPF program free workqueue\n" ); +return 0 ; +} +early_initcall (bpf_prog_free_wq_init); +static void bpf_prog_free_images (struct work_struct *work) {struct bpf_prog_aux *aux;@@ -3198,7 +3210,7 @@ static void bpf_prog_free_deferred(struct work_struct *work)/* The symbols are looked up under rcu_read_lock() */ INIT_RCU_WORK (&aux->rwork, bpf_prog_free_images);if (ksyms) -queue_rcu_work (system_percpu_wq, &aux->rwork); +queue_rcu_work (bpf_prog_free_wq, &aux->rwork);else bpf_prog_free_images (&aux->rwork.work); }@@ -3211,7 +3223,12 @@ void bpf_prog_free(struct bpf_prog *fp)bpf_prog_put (aux->dst_prog);bpf_token_put (aux->token);INIT_WORK (&aux->work, bpf_prog_free_deferred); -schedule_work (&aux->work); +/* +* A pack_mutex holder may wait for vmalloc work on system_percpu_wq.
Quoted 9 lines
kernel/bpf/core.c void bpf_prog_free(struct bpf_prog *fp) · line 3230
Quoted 3 lines
kernel/bpf/core.c +3
-0+* Keep program frees off that queue so mutex waiters cannot exhaust +* the active slots needed by the vmalloc work. +*/ very specific
pack_mutexreference is more confusing than helpful, imo. should we keep it generic and say thatbpf_progfreeing goes through its own workqueue to not accidentally block or slowdown system queue or something along those lines?
Fair, I will adjust the wording.
Quoted 12 lines
Quoted 12 lines
kernel/bpf/core.c +1
-0+queue_work (bpf_prog_free_wq, &aux->work); }/* RNG for unprivileged user space with separated state from prandom_u32(). */ kernel/bpf/trampoline.c +0
-0@@ -577,6 +577,8 @@ static void __bpf_tramp_image_put_deferred(struct work_struct *work)bpf_trampoline_put (tr); }
Quoted 4 lines
kernel/bpf/trampoline.c static void __bpf_tramp_image_put_deferred(struct work_struct *work) · line 580
Quoted 1 line
kernel/bpf/trampoline.c +1
-0+extern struct workqueue_struct *bpf_prog_free_wq__ro_after_init ;this extern should be in
include/linux/bpf.h, please move it there
Ack.
Quoted 18 lines
pw-bot: cr
Quoted 16 lines
kernel/bpf/trampoline.c +2
-2+/* callback, fexit step 3 or fentry step 2 */ static void __bpf_tramp_image_put_rcu (struct rcu_head *rcu) {@@ -584,7 +586,7 @@ static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) im = container_of(rcu, struct bpf_tramp_image, rcu);INIT_WORK (&im->work, __bpf_tramp_image_put_deferred); -schedule_work (&im->work); +queue_work (bpf_prog_free_wq, &im->work); }/* callback, fexit step 2. Called after percpu_ref_kill confirms. */ --2.54.0