bpf: Fix loop detection for re-arming async callbacks
Subsystems (4)
v2 cover bpf: Fix loop detection for re-arming async callbacks
Changelog:
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Patch 1: also fix push_callback_call(), which numbered a new entry from the caller frame. When the callback re-arms from a subprog that frame is not frame 0 and its count is 0, so every entry was numbered 1, is_state_visited() never saw a difference, and a loop in such a callback was still rejected.
- Patch 2: loop_cb() now re-arms through a __noinline subprog, so the caller frame at bpf_timer_set_callback() is not the callback's own frame. The v1 test re-armed from frame 0 and passed without the push_callback_call() fix.
push_async_cb() sets in_async_callback_fn and async_entry_cnt on the callback's own frame, which is frame 0 of the fresh state it starts, and setup_func_entry() copies neither, so a subprog called by the callback carries neither. Two places read them from the innermost frame instead.
is_state_visited() skips the infinite loop check when two states differ in async_entry_cnt, since seeing the same state on a second entry into an async callback is not a loop. push_callback_call() assigns that count as the caller's plus one.
A callback which re-arms itself and calls a subprog therefore has its two entries compared at a loop inside that subprog, where the innermost frame is the subprog's, and both entries are numbered 1 anyway, so it is rejected:
infinite loop detected at insn 57
Patch 1 reads both from frame 0 in both places, which push_async_cb() makes the callback's frame at any call depth. A loop within a single entry still has a matching count and is still caught.
Patch 2 covers both directions: a timer callback which re-arms through bpf_timer_set_callback() from a subprog and reaches a bounded loop through another one, which fails to load without patch 1, and a callback which never returns, which must still be rejected either way. Nothing covered an async callback before, so neither direction was tested.
Puranjay Mohan (2):
bpf: Look at frame 0 when telling async callback entries apart selftests/bpf: Add timer tests for a re-arming callback with a loop
kernel/bpf/states.c | 4 +- kernel/bpf/verifier.c | 2 +- .../testing/selftests/bpf/prog_tests/timer.c | 33 ++++++++++ tools/testing/selftests/bpf/progs/timer.c | 60 ++++++++++++++++++- .../selftests/bpf/progs/timer_failure.c | 29 +++++++++ 5 files changed, 124 insertions(+), 4 deletions(-)
base-commit: 4f3a5eae895b9995e93425a75235d8f1f3268caa
Signature · 1 line
2.53.0-Meta
PATCH v2 1/2 bpf: Look at frame 0 when telling async callback entries apart
push_async_cb() sets in_async_callback_fn and async_entry_cnt on the callback's own frame, which is frame 0 of the fresh state it starts, and setup_func_entry() copies neither, so a subprog called by the callback carries neither. Two places read them from the innermost frame instead.
is_state_visited() skips the infinite loop check when two states differ in async_entry_cnt, because seeing the same state on a second entry into an async callback is not a loop. A callback which re-arms itself and calls a subprog therefore compares the two entries at a loop inside that subprog, where the innermost frame is the subprog's and has no flag set, and the state is rejected:
infinite loop detected at insn 60
push_callback_call() numbers a new entry as the caller's count plus one. When the callback re-arms itself from a subprog, the caller is that subprog's frame with a count of 0, so every entry is numbered 1, the check above never sees a difference, and a loop anywhere in such a callback is rejected the same way.
Read the flag and the count from frame 0 in both places. A loop within a single entry still has a matching count and is still caught, and frame 0 of a non-async state does not have the flag set, so nothing else changes.
The two other readers are already correct: check_return_code() reads frame[0], and the one in do_check() is reached only after an early return for curframe != 0.
--- kernel/bpf/states.c | 4 ++-- kernel/bpf/verifier.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
kernel/bpf/states.c +2 -2
@@ -1273,10 +1273,10 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)continue ;if (sl->state.branches) { -struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; +struct bpf_func_state *frame = sl->state.frame[0 ];if (frame->in_async_callback_fn && - frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { + frame->async_entry_cnt != cur->frame[0 ]->async_entry_cnt) {/* Different async_entry_cnt means that the verifier is * processing another entry into async callback. * Seeing the same state is not an indication of infinite
kernel/bpf/verifier.c +1 -1
@@ -10936,7 +10936,7 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *insif (IS_ERR (async_cb))return PTR_ERR (async_cb); callee = async_cb->frame[0 ]; - callee->async_entry_cnt = caller->async_entry_cnt +1 ; + callee->async_entry_cnt = state->frame[0 ]->async_entry_cnt +1 ;/* Convert bpf_timer_set_callback() args into timer callback args */ err =set_callee_state_cb (env, caller, callee, insn_idx);
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH bpf-next v2 1/2] bpf: Look at frame 0 when telling async callback entries apart
Is the attribution here accurate? Looking at the verifier code, push_async_cb() doesn't directly set either field. It kzalloc()s frame 0 and calls init_func_state() on it, which initializes…
Quoted 4 lines
push_async_cb()setsin_async_callback_fnandasync_entry_cnton the callback's own frame, which is frame 0 of the fresh state it starts, andsetup_func_entry()copies neither, so a subprog called by the callback carries neither.
Is the attribution here accurate? Looking at the verifier code, push_async_cb() doesn't directly set either field. It kzalloc()s frame 0 and calls init_func_state() on it, which initializes callsite, frameno, subprogno, callback_ret_range and registers.
async_entry_cnt is actually set by push_callback_call() after push_async_cb() returns:
callee = async_cb->frame[0];
callee->async_entry_cnt = state->frame[0]->async_entry_cnt + 1;And in_async_callback_fn is set by the set_callee_state_cb() callbacks:set_timer_callback_state(), set_task_work_schedule_callback_state() and set_rcu_callback_state(), each with:
callee->in_async_callback_fn = true;
Quoted 3 lines
The two other readers are already correct:
check_return_code()reads frame[0], and the one indo_check()is reached only after an early return for curframe != 0.
Doesn't this leave out print_verifier_state()? In kernel/bpf/log.c, it reads the flag:
if (state->in_async_callback_fn)
verbose(env, " async_cb");print_verifier_state() only affects log output and reads the flag from the frame it is printing, so leaving it unchanged is fine. The reader credited to do_check() is actually in process_bpf_exit_full(), which do_check_insn() calls.
Would naming the functions that actually set the fields (push_callback_call() and the set_*_callback_state() callbacks) and covering the log.c reader make the changelog more accurate? The conclusions in the message hold, and the fix itself is correct, but for a subtle verifier change it would help to have the attribution match the code.
---
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/36029511690
PATCH v2 2/2 selftests/bpf: Add timer tests for a re-arming callback with a loop
In reply to Puranjay MohanExisting timer callbacks either do not re-arm through bpf_timer_set_callback(), which is what starts another async callback entry, or do not reach a loop once they do. Cover that: loop_cb() re-arms itself and reaches a bounded loop through sum_to(), which is static and not inlined, so the loop is walked in a frame below the callback's rather than in a separately verified global subprog.
The re-arm goes through rearm(), also static and not inlined, so the caller frame at bpf_timer_set_callback() is not the callback's own frame and carries no async_entry_cnt of its own. That covers both frames the preceding fix corrects; without it the program is rejected with "infinite loop detected".
Telling async callback entries apart must not stop the verifier catching a real loop in a callback, and no existing test covers that either, so also check that a callback which never returns is still rejected.
--- .../testing/selftests/bpf/prog_tests/timer.c | 33 ++++++++++ tools/testing/selftests/bpf/progs/timer.c | 60 ++++++++++++++++++- .../selftests/bpf/progs/timer_failure.c | 29 +++++++++ 3 files changed, 121 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/timer.c +33 -0
@@ -262,6 +262,34 @@ static int timer_cancel_async(struct timer *timer_skel)return 0 ; } +/* +* A timer callback which re-arms itself and reaches a loop through a call: +* the two entries into the callback meet at the loop, in a frame of its own. +*/ +static int timer_loop_rearm (struct timer *timer_skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +int err, prog_fd, i; + + err =timer__attach (timer_skel); +if (!ASSERT_OK (err,"timer_attach" )) +return err; + + timer_skel->bss->loop_rearm =1 ; + + prog_fd =bpf_program__fd (timer_skel->progs.test_loop_rearm); + err =bpf_prog_test_run_opts (prog_fd, &topts); +if (!ASSERT_OK (err,"test_run" )) +return err; + +for (i =0 ; i <100 && timer_skel->bss->loop_sum <120 *2 ; i++) +usleep (1000 ); + +timer__detach (timer_skel); +ASSERT_EQ (timer_skel->bss->loop_sum,120 *2 ,"loop_sum" ); +return 0 ; +} +static void test_timer (int (*timer_test_fn)(struct timer *timer_skel)) {struct timer *timer_skel =NULL ;
@@ -287,6 +315,11 @@ void serial_test_timer(void)RUN_TESTS (timer_failure); } +void serial_test_timer_loop_rearm (void ) +{ +test_timer (timer_loop_rearm); +} +void serial_test_timer_stress (void ) {test_timer (timer_stress);
tools/testing/selftests/bpf/progs/timer.c +59 -1
80 lines not on this page · raw message
tools/testing/selftests/bpf/progs/timer_failure.c +29 -0
@@ -66,3 +66,32 @@ long BPF_PROG2(test_bad_ret, int, a)return 0 ; } + +/* +* A real loop inside an async callback must still be rejected: both entries +* into the callback have the same async_entry_cnt, so telling entries apart +* does not apply here. +*/ +static int timer_cb_infinite_loop (void *map,int *key,struct elem *val) +{ +for (;;) {} + +return 0 ; +} + +SEC("fentry/bpf_fentry_test1") +__failure __msg("infinite loop detected") +long BPF_PROG2(test_infinite_loop_cb, int, a) +{ +struct bpf_timer *timer; +int key =0 ; + + timer =bpf_map_lookup_elem (&timer_map, &key); +if (timer) { +bpf_timer_init (timer, &timer_map,CLOCK_BOOTTIME ); +bpf_timer_set_callback (timer, timer_cb_infinite_loop); +bpf_timer_start (timer,1000 ,0 ); + } + +return 0 ; +}
Signature · 1 line
2.53.0-Meta
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 13 lines · On Thu, 24 Sep 2026 09:26:05 -0700 you wrote:
Changelog:
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Patch 1: also fixpush_callback_call(), which numbered a new entry from the caller frame. When the callback re-arms from a subprog that frame is not frame 0 and its count is 0, so every entry was numbered 1,is_state_visited()never saw a difference, and a loop in such a callback was still rejected.
- Patch 2:loop_cb()now re-arms through a__noinlinesubprog, so the caller frame atbpf_timer_set_callback()is not the callback's own frame. The v1 test re-armed from frame 0 and passed without thepush_callback_call()fix.
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,v2,1/2] bpf: Look at frame 0 when telling async callback entries apart
https://git.kernel.org/bpf/bpf-next/c/e55220162aae - [bpf-next,v2,2/2] selftests/bpf: Add timer tests for a re-arming callback with a loop https://git.kernel.org/bpf/bpf-next/c/84bff37db2ad
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html