BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: wujing <realwujing@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>,
	 Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>,
	bpf@vger.kernel.org,  linux-kernel@vger.kernel.org,
	Qiliang Yuan <yuanql9@chinatelecom.cn>
Subject: Re: [PATCH] bpf/verifier: optimize ID mapping reset in states_equal
Date: Thu, 15 Jan 2026 12:36:02 -0800	[thread overview]
Message-ID: <fe21bddcc1c46ecd18a28cf76db4de78c5ef314e.camel@gmail.com> (raw)
In-Reply-To: <20260115144946.439069-1-realwujing@gmail.com>

On Thu, 2026-01-15 at 22:49 +0800, wujing wrote:
> The verifier uses an ID mapping table (struct bpf_idmap) during state
> equivalence checks. Currently, reset_idmap_scratch performs a full memset
> on the entire map in every call.
> 
> The table size is exactly 4800 bytes (approx. 4.7KB), calculated as:
> - MAX_BPF_REG = 11
> - MAX_BPF_STACK = 512
> - BPF_REG_SIZE = 8
> - MAX_CALL_FRAMES = 8
> - BPF_ID_MAP_SIZE = (11 + 512 / 8) * 8 = 600 entries
> - Each entry (struct bpf_id_pair) is 8 bytes (two u32 fields)
> - Total size = 600 * 8 = 4800 bytes
> 
> For complex programs with many pruning points, this constant large memset
> introduces significant CPU overhead and cache pressure, especially when
> only a few IDs are actually used.
> 
> This patch optimizes the reset logic by:
> 1. Adding 'map_cnt' to bpf_idmap to track used slots.
> 2. Updating 'map_cnt' in check_ids to record the high-water mark.
> 3. Making reset_idmap_scratch perform a partial memset based on 'map_cnt'.
> 
> This improves pruning performance and reduces redundant memory writes.
> 
> Signed-off-by: wujing <realwujing@gmail.com>
                 ^^^^^^
		 Please use your full name.
> Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn>
> ---

I think this is an ok change.
Could you please collect some stats using 'perf stat' for some big selftest?

>  include/linux/bpf_verifier.h |  1 +
>  kernel/bpf/verifier.c        | 10 ++++++++--
>  2 files changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
> index 130bcbd66f60..562f7e63be29 100644
> --- a/include/linux/bpf_verifier.h
> +++ b/include/linux/bpf_verifier.h
> @@ -692,6 +692,7 @@ struct bpf_id_pair {
>  
>  struct bpf_idmap {
>  	u32 tmp_id_gen;
> +	u32 map_cnt;
>  	struct bpf_id_pair map[BPF_ID_MAP_SIZE];
>  };
>  
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 37ce3990c9ad..6220dde41107 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -18954,6 +18954,7 @@ static bool check_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap)
>  			/* Reached an empty slot; haven't seen this id before */
>  			map[i].old = old_id;
>  			map[i].cur = cur_id;
> +			idmap->map_cnt = i + 1;
>  			return true;
>  		}
>  		if (map[i].old == old_id)
> @@ -19471,8 +19472,13 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat
>  
>  static void reset_idmap_scratch(struct bpf_verifier_env *env)
>  {
> -	env->idmap_scratch.tmp_id_gen = env->id_gen;
> -	memset(&env->idmap_scratch.map, 0, sizeof(env->idmap_scratch.map));
> +	struct bpf_idmap *idmap = &env->idmap_scratch;
> +
> +	idmap->tmp_id_gen = env->id_gen;
> +	if (idmap->map_cnt) {

Nit: this condition is not really necessary.

> +		memset(idmap->map, 0, idmap->map_cnt * sizeof(struct bpf_id_pair));
> +		idmap->map_cnt = 0;
> +	}
>  }
>  
>  static bool states_equal(struct bpf_verifier_env *env,

  reply	other threads:[~2026-01-15 20:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-15 14:49 [PATCH] bpf/verifier: optimize ID mapping reset in states_equal wujing
2026-01-15 20:36 ` Eduard Zingerman [this message]
2026-01-15 22:19   ` Andrii Nakryiko
2026-01-16  9:08     ` [PATCH v2] " Qiliang Yuan
2026-01-19 19:42       ` Eduard Zingerman
2026-01-20  1:56         ` [PATCH v3] " Qiliang Yuan
2026-01-20  2:09           ` Alexei Starovoitov
2026-01-20  2:32             ` [PATCH v4] " Qiliang Yuan
2026-01-20 19:40               ` patchwork-bot+netdevbpf
2026-01-16  9:29     ` [PATCH v2] " Qiliang Yuan

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=fe21bddcc1c46ecd18a28cf76db4de78c5ef314e.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=realwujing@gmail.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    --cc=yuanql9@chinatelecom.cn \
    /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