All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH v2 bpf-next 2/7] bpf: Check many main programs in one set of insns
Date: Wed, 02 Dec 2026 22:15:00 +0000	[thread overview]
Message-ID: <sashiko-outbox-246684@kernel.org> (raw)
In-Reply-To: <20261202212743.647706-3-alexei.starovoitov@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] bpf: missing sleepable_async_cb propagation in
  mark_sleepable_async_cb()
- [Low] bpf: unreturned env->mem charges in bpf_precision_free() across
  main progs
--

commit e92d07122046139421d58da32ed44b43dd2f289e
Author: Alexei Starovoitov <ast@kernel.org>

bpf: Check many main programs in one set of insns

This commit extends the BPF verifier to verify multiple main program entry
points within a single shared instruction image.

> diff --git a/kernel/bpf/precision.c b/kernel/bpf/precision.c
> index 0a2923505308b..725de358f5ca8 100644
> --- a/kernel/bpf/precision.c
> +++ b/kernel/bpf/precision.c
> @@ -1826,6 +1826,8 @@ void bpf_precision_free(struct bpf_verifier_env *env)
>  
>  	if (!prec)
>  		return;
> +	if (prec->data_deps)
> +		env->mem -= 18 * prec->words * sizeof(long) + 5 * env->prog->len * sizeof(u16);
>  	kvfree(prec->func);
>  	kvfree(prec->loops);
>  	kvfree(prec->hops_to_err);

[Severity: Low]
As reported by bot+bpf-ci@kernel.org on v1 [1], does bpf_precision_free()
leave bytes charged to env->mem across main programs when prec->seen is
allocated or when alloc_masks() fails before setting prec->data_deps?

In bpf_failing_path_was_here(), growing prec->seen charges env->mem +=
size * sizeof(*set), and alloc_masks() leaves 5 * env->prog->len *
sizeof(u16) charged for prec->hops_to_err when bpf_check_limits() or
kvcalloc() fails with prec->data_deps still NULL.  Because do_check_mains()
carries env->mem across entries, these unreturned charges can accumulate
and cause later main programs in the load to fail with -E2BIG.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index b753d6c7a00b5..2c6dc44f4e74e 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c

[ ... ]

> @@ -25328,14 +25478,50 @@ static int do_check_all(struct bpf_verifier_env *env)
>  		restart_log(env, &log);
>  	if (err && (!path_err || guess) && (limit || (env->for_log && !env->err)))
>  		verbose(env, "the limit on cost or memory ended pass %u\n", env->pass);
> -	if (!err && env->sleepable_async_cb)
> -		mark_sleepable_async_cb(env);
>  	bpf_precision_free(env);
>  	free_snapshot(env, initial);
>  	free_snapshot(env, good_pass);
>  	return err;
>  }
>  
> +/*
> + * A main prog that is proven is not checked again. The ones after it must not add a site that its
> + * states went past, or take away a site at which they were cancelled.
> + */
> +static int check_proven_mains(struct bpf_verifier_env *env)
> +{
> +	struct bpf_insn_aux_data *aux = env->insn_aux_data;
> +	int i;
> +
> +	for (i = 0; i < env->prog->len; i++) {
> +		if (aux[i].needs_guard && aux[i].no_guard_edges) {
> +			verbose(env, "loop at insn %d needs may_goto for some programs only\n", i);
> +			return -EINVAL;
> +		}
> +	}
> +	return 0;
> +}
> +
> +/* Each main prog has passes of its own. They start from what the progs before it wrote. */
> +static int do_check_mains(struct bpf_verifier_env *env)
> +{
> +	u64 cost = env->cost, max_cost = env->max_cost, max_mem = env->max_mem;
> +	int err = 0;
> +
> +	for (; env->pass_main < env->main_cnt && !err; env->pass_main++) {
> +		env->cost = cost;
> +		env->max_cost = max_cost;
> +		env->max_mem = max_mem;
> +		bpf_flow_new_main(env);
> +		err = do_check_all(env);
> +		if (!err && env->main_cnt > 1)
> +			err = check_proven_mains(env);
> +	}
> +	if (!err && env->sleepable_async_cb)
> +		mark_sleepable_async_cb(env);
> +	return err;
> +}

[Severity: Medium]
Can a non-sleepable struct_ops entry miss sleepable_async_cb and map
sleepable_refcnt increments when the first entry is sleepable?

can_share_subprogs() allows a struct_ops load to mix sleepable and
non-sleepable entries.  When the first entry is sleepable (env->prog->sleepable
is true) and the load schedules a sleepable async callback,
mark_sleepable_async_cb() returns early before setting
env->prog->aux->sleepable_async_cb = true:

kernel/bpf/verifier.c:mark_sleepable_async_cb() {
	if (bpf_prog_runs_sleepable(env->prog))
		return;

	env->prog->aux->sleepable_async_cb = true;
    ...
}

finish_entry() then copies from->sleepable_async_cb (false) into each
entry's aux, so for any non-sleepable entry bpf_prog_runs_sleepable(entry)
is false.  As a result, finish_entry() skips incrementing
map->sleepable_refcnt for that entry's used_maps and the entry is freed via
call_rcu() instead of call_rcu_tasks_trace(), allowing map->sleepable_refcnt
to drop to zero and skip RCU Tasks Trace synchronization while the
non-sleepable entry and its sleepable async callback remain active.

[1] https://lore.kernel.org/r/a0639de449fd4f8ca45a82936271218bb746b417c8aeeefa92cdd3258174303f@mail.kernel.org

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202212743.647706-1-alexei.starovoitov@gmail.com?part=2

  reply	other threads:[~2026-12-02 22:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 21:27 [PATCH v2 bpf-next 0/7] bpf: Share subprogs between programs Alexei Starovoitov
2026-12-02 21:27 ` [PATCH v2 bpf-next 1/7] bpf: Prepare for many main programs in one load Alexei Starovoitov
2026-12-02 22:20   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 2/7] bpf: Check many main programs in one set of insns Alexei Starovoitov
2026-12-02 22:15   ` sashiko-bot [this message]
2026-12-02 22:35   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 3/7] bpf: Generate landing pads for many main programs Alexei Starovoitov
2026-12-02 22:21   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 4/7] bpf: Take entries at BPF_PROG_LOAD Alexei Starovoitov
2026-12-02 22:20   ` bot+bpf-ci
2026-12-02 21:27 ` [PATCH v2 bpf-next 5/7] libbpf, bpftool: Share subprogs between programs Alexei Starovoitov
2026-12-02 21:27 ` [PATCH v2 bpf-next 6/7] libbpf: Share subprogs in light skeletons Alexei Starovoitov
2026-12-02 21:27 ` [PATCH v2 bpf-next 7/7] selftests/bpf: Test programs that share subprogs Alexei Starovoitov

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=sashiko-outbox-246684@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.