Skip to content

Commit 6487330

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
bpf: Add a recursion check to prevent loops in bpf_timer
Do not schedule timer/wq operation on a cpu that is in irq_work callback that is processing async_cmds queue. Otherwise the following loop is possible: bpf_timer_start() -> bpf_async_schedule_op() -> irq_work_queue(). irqrestore -> bpf_async_irq_worker() -> tracepoint -> bpf_timer_start(). Fixes: 1bfbc26 ("bpf: Enable bpf_timer and bpf_wq in any context") Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20260204055147.54960-4-alexei.starovoitov@gmail.com
1 parent 67ee5ad commit 6487330

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

kernel/bpf/helpers.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,9 +1427,23 @@ static int bpf_async_update_prog_callback(struct bpf_async_cb *cb,
14271427
return 0;
14281428
}
14291429

1430+
static DEFINE_PER_CPU(struct bpf_async_cb *, async_cb_running);
1431+
14301432
static int bpf_async_schedule_op(struct bpf_async_cb *cb, enum bpf_async_op op,
14311433
u64 nsec, u32 timer_mode)
14321434
{
1435+
/*
1436+
* Do not schedule another operation on this cpu if it's in irq_work
1437+
* callback that is processing async_cmds queue. Otherwise the following
1438+
* loop is possible:
1439+
* bpf_timer_start() -> bpf_async_schedule_op() -> irq_work_queue().
1440+
* irqrestore -> bpf_async_irq_worker() -> tracepoint -> bpf_timer_start().
1441+
*/
1442+
if (this_cpu_read(async_cb_running) == cb) {
1443+
bpf_async_refcount_put(cb);
1444+
return -EDEADLK;
1445+
}
1446+
14331447
struct bpf_async_cmd *cmd = kmalloc_nolock(sizeof(*cmd), 0, NUMA_NO_NODE);
14341448

14351449
if (!cmd) {
@@ -1628,13 +1642,15 @@ static void bpf_async_irq_worker(struct irq_work *work)
16281642
return;
16291643

16301644
list = llist_reverse_order(list);
1645+
this_cpu_write(async_cb_running, cb);
16311646
llist_for_each_safe(pos, n, list) {
16321647
struct bpf_async_cmd *cmd;
16331648

16341649
cmd = container_of(pos, struct bpf_async_cmd, node);
16351650
bpf_async_process_op(cb, cmd->op, cmd->nsec, cmd->mode);
16361651
kfree_nolock(cmd);
16371652
}
1653+
this_cpu_write(async_cb_running, NULL);
16381654
}
16391655

16401656
static void bpf_async_cancel_and_free(struct bpf_async_kern *async)

0 commit comments

Comments
 (0)