BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
	eddyz87@gmail.com, kernel-team@fb.com, martin.lau@kernel.org,
	mason@kernel.org, ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v10 11/24] bpf: Prepare JITed programs for dispatching cleanup pads
Date: Fri, 9 Oct 2026 18:39:50 +0200	[thread overview]
Message-ID: <910e33d6-da80-47c7-bda3-a92e9e530cd1@linux.dev> (raw)
In-Reply-To: <8a00a49d056adef7ace9b597d1f682ea168bac9382001c0b31ce3ebdefbf35ba@mail.kernel.org>



On 10/9/26 7:13 AM, bot+bpf-ci@kernel.org wrote:
>> bpf: Prepare JITed programs for dispatching cleanup pads
> TL;DR: keep_subprog_exits() may mark an exit seen in a never-entered subprog,
> so dead-code removal can miscompute subprog starts and drop main's exit,
> letting JITed main fall off its image end.
>
>> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
>> index 64baee1a37e6..1cc025c45ecd 100644
>> --- a/kernel/bpf/fixups.c
>> +++ b/kernel/bpf/fixups.c
> [ ... ]
>
>> @@ -715,6 +716,32 @@ static void keep_funcs_with_addr_taken(struct bpf_verifier_env *env)
>>   	}
>>   }
>>   
>> +static int keep_subprog_exits(struct bpf_verifier_env *env)
>> +{
>> +	u32 i, j;
>> +
>> +	for (i = 0; i < env->subprog_cnt; i++) {
>> +		bool found = false;
>> +		u32 start;
>> +
>> +		if (!env->subprog_info[i].might_unwind)
>> +			continue;
>> +		start = env->subprog_info[i].start;
>> +		for (j = env->subprog_info[i + 1].start; j-- > start; ) {
>> +			if (env->prog->insnsi[j].code != (BPF_JMP | BPF_EXIT))
>> +				continue;
>> +			env->insn_aux_data[j].seen = env->pass_cnt;
> Can this mark an exit as seen in a subprog the verifier never entered?
>
> might_unwind is set statically while walking the control flow, so it is also
> set on a static subprog that is only called from a path the verifier proves
> dead, for example one guarded by a .rodata constant or a constant register.
> None of that subprog's instructions are seen, but the loop above still marks
> its last exit as seen.
>
> Before this patch a live subprog was always entered at its first
> instruction, and adjust_subprog_starts_after_remove() relies on that.  Now
> the first instruction of a subprog can be dead while a later exit is kept.
> bpf_opt_remove_dead_code() removes each contiguous run of unseen
> instructions even when the run crosses a subprog boundary, so one run can
> cover the end of the previous code and the head of this subprog, and stop at
> the kept exit.
>
> Here is a trace (privileged loader, with bpf_unwind registered and the x86
> cleanup pad support from later patches in the series):
>
>    main:  0: r1 = 0
>           1: if r1 == 0 goto +2   ; always taken
>           2: call P               ; dead
>           3: call A               ; dead
>           4: r0 = 0
>           5: exit
>    P:     6: r0 = 1               ; static, never entered, no unwind
>           7: exit
>    A:     8: call bpf_unwind      ; static, never entered, might_unwind
>           9: r0 = 0
>          10: exit                 ; kept by keep_subprog_exits()
>
> The first dead run, [2,3], is removed correctly, leaving P.start = 4,
> A.start = 6 and the fake subprog at 9.
>
> The next dead run is [4..7]: all of P plus A's call and 'r0 = 0'.  It stops
> at A's kept exit, so off = 4 and cnt = 4.
>
> In adjust_subprog_starts_after_remove(), i is P.  The j loop reaches the fake
> subprog, whose start (9) is not off + cnt (8), so:
>
> 	if (env->subprog_info[j].start != off + cnt)
> 		j--;
>
> gives j = A.  Since j > i, P's entry is dropped and A moves into its slot,
> and then the start adjustment gives A.start = 6 - 4 = 2.  The correct value
> is 4.
>
> The program is now:
>
>    0: r1 = 0
>    1: ja +0
>    2: r0 = 0
>    3: exit
>    4: exit
>
> with subprog starts main = 0 and A = 2, so main has lost its own
> 'r0 = 0; exit'.  After bpf_opt_remove_nops() main is just [r1 = 0].
> jit_subprogs() then JITs func[0] with no exit, so it has no epilogue and no
> ret, and the first run of the program falls off the end of main's image.  If
> main had kept a jump into the reassigned tail, do_jit() would also read
> addrs[] past func[0]->len.
>
> The same miscalculation happens when P is a live subprog with only a dead
> tail (the i == j case, where start != off).
>
> Would it make sense to skip subprogs that were never entered, since they can
> never be on the stack during an unwind, for example:
>
> 	if (!env->insn_aux_data[start].seen)
> 		continue;

Indeed, this is a bug. Will fix.

>
> Alternatively, could adjust_subprog_starts_after_remove() set the start of a
> partially removed subprog to off?
>
> None of the later commits in this series appear to change keep_subprog_exits()
> or adjust_subprog_starts_after_remove().
>
>> +			found = true;
>> +			break;
>> +		}
>> +		if (!found) {
>> +			verbose(env, "subprog %u can be unwound through but has no exit\n", i);
>> +			return -EINVAL;
>> +		}
>> +	}
>> +	return 0;
>> +}
>> +
>
> ---
> 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/37884246994


  reply	other threads:[~2026-10-09 16:39 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-09  4:14 [PATCH bpf-next v10 00/24] bpf: Run exception cleanup landing pads when bpf_unwind() unwinds Yonghong Song
2026-10-09  4:14 ` [PATCH bpf-next v10 01/24] bpf: Pack bpf_insn_aux_data flags into bit fields Yonghong Song
2026-10-09  4:14 ` [PATCH bpf-next v10 02/24] bpf: Accept the compiler's exception cleanup table at program load Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 03/24] bpf: Add the bpf_unwind() and bpf_unwind_resume() kfuncs Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 04/24] bpf: Keep a call site's landing pad in insn_aux_data, add lookups Yonghong Song
2026-10-09  4:28   ` sashiko-bot
2026-10-09 15:10     ` Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 05/24] bpf: Mark covered call sites and check a program can take a table Yonghong Song
2026-10-09  4:34   ` sashiko-bot
2026-10-09 15:13     ` Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 06/24] bpf: Make exception landing pads reachable in the CFG Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 07/24] bpf: Verify an unwind through landing pads and epilogues Yonghong Song
2026-10-09  4:40   ` sashiko-bot
2026-10-09 15:24     ` Yonghong Song
2026-10-09  5:13   ` bot+bpf-ci
2026-10-09 15:25     ` Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 08/24] bpf: Refuse a callback that can unwind Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 09/24] bpf: Refuse a landing pad that does not resume Yonghong Song
2026-10-10 15:08   ` Alexei Starovoitov
2026-10-11 18:26     ` Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 10/24] bpf: Do not use a private stack for a program that can unwind Yonghong Song
2026-10-09  4:45   ` sashiko-bot
2026-10-09 15:38     ` Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 11/24] bpf: Prepare JITed programs for dispatching cleanup pads Yonghong Song
2026-10-09  4:35   ` sashiko-bot
2026-10-09 16:12     ` Yonghong Song
2026-10-09  5:13   ` bot+bpf-ci
2026-10-09 16:39     ` Yonghong Song [this message]
2026-10-09  4:15 ` [PATCH bpf-next v10 12/24] bpf: Dispatch cleanup pads by rewriting return addresses Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 13/24] bpf: Refuse a trampoline that calls a subprog that can unwind Yonghong Song
2026-10-09  4:15 ` [PATCH bpf-next v10 14/24] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
2026-10-09  4:32   ` sashiko-bot
2026-10-09 16:51     ` Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 15/24] bpf, arm64: " Yonghong Song
2026-10-09  4:30   ` sashiko-bot
2026-10-09 16:55     ` Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 16/24] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 17/24] libbpf: Add cleanup_info to bpf_prog_load_opts Yonghong Song
2026-10-09  4:26   ` sashiko-bot
2026-10-09  4:16 ` [PATCH bpf-next v10 18/24] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 19/24] libbpf: Carry the exception cleanup table through the light skeleton Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 20/24] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 21/24] selftests/bpf: Add end-to-end and negative .bpf_cleanup exception tests Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 22/24] selftests/bpf: Add __set_global() and __ret_global() test tags Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 23/24] selftests/bpf: Cover more accepted .bpf_cleanup exception shapes Yonghong Song
2026-10-09  4:16 ` [PATCH bpf-next v10 24/24] selftests/bpf: Load an exception cleanup program from a light skeleton Yonghong Song
2026-10-10 15:15 ` [PATCH bpf-next v10 00/24] bpf: Run exception cleanup landing pads when bpf_unwind() unwinds Alexei Starovoitov
2026-10-11 17:30   ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=910e33d6-da80-47c7-bda3-a92e9e530cd1@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=mason@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox