From: wujing <realwujing@gmail.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, 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,
wujing <realwujing@gmail.com>,
Qiliang Yuan <yuanql9@chinatelecom.cn>
Subject: [PATCH] bpf/verifier: optimize precision backtracking by skipping precise bits
Date: Thu, 15 Jan 2026 23:04:05 +0800 [thread overview]
Message-ID: <20260115150405.443581-1-realwujing@gmail.com> (raw)
Backtracking is one of the most expensive parts of the verifier. When
marking precision, currently the verifier always triggers the full
__mark_chain_precision even if the target register or stack slot is
already marked as precise.
Since a precise mark in a state implies that all necessary ancestors
have already been backtracked and marked accordingly, we can safely
skip the backtracking process if the bit is already set.
This patch implements early exit logic in:
1. mark_chain_precision: Check if the register is already precise.
2. propagate_precision: Skip registers and stack slots that are already
precise in the current state when propagating from an old state.
This significantly reduces redundant backtracking in complex BPF
programs with frequent state pruning and merges.
Signed-off-by: wujing <realwujing@gmail.com>
Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn>
---
kernel/bpf/verifier.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 6220dde41107..378341e1177f 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -4927,6 +4927,14 @@ static int __mark_chain_precision(struct bpf_verifier_env *env,
int mark_chain_precision(struct bpf_verifier_env *env, int regno)
{
+ struct bpf_reg_state *reg;
+
+ if (regno >= 0) {
+ reg = &env->cur_state->frame[env->cur_state->curframe]->regs[regno];
+ if (reg->precise)
+ return 0;
+ }
+
return __mark_chain_precision(env, env->cur_state, regno, NULL);
}
@@ -19527,19 +19535,23 @@ static int propagate_precision(struct bpf_verifier_env *env,
struct bpf_verifier_state *cur,
bool *changed)
{
- struct bpf_reg_state *state_reg;
- struct bpf_func_state *state;
+ struct bpf_reg_state *state_reg, *cur_reg;
+ struct bpf_func_state *state, *cur_state;
int i, err = 0, fr;
bool first;
for (fr = old->curframe; fr >= 0; fr--) {
state = old->frame[fr];
+ cur_state = cur->frame[fr];
state_reg = state->regs;
first = true;
for (i = 0; i < BPF_REG_FP; i++, state_reg++) {
if (state_reg->type != SCALAR_VALUE ||
!state_reg->precise)
continue;
+ cur_reg = &cur_state->regs[i];
+ if (cur_reg->precise)
+ continue;
if (env->log.level & BPF_LOG_LEVEL2) {
if (first)
verbose(env, "frame %d: propagating r%d", fr, i);
@@ -19557,6 +19569,9 @@ static int propagate_precision(struct bpf_verifier_env *env,
if (state_reg->type != SCALAR_VALUE ||
!state_reg->precise)
continue;
+ cur_reg = &cur_state->stack[i].spilled_ptr;
+ if (cur_reg->precise)
+ continue;
if (env->log.level & BPF_LOG_LEVEL2) {
if (first)
verbose(env, "frame %d: propagating fp%d",
--
2.39.5
next reply other threads:[~2026-01-15 15:04 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-15 15:04 wujing [this message]
2026-01-15 18:52 ` Eduard Zingerman
2026-01-16 4:58 ` [PATCH v2] " Qiliang Yuan
2026-01-17 10:09 ` [PATCH v3] " Qiliang Yuan
2026-01-17 17:12 ` Alexei Starovoitov
2026-01-19 6:20 ` Yonghong Song
2026-01-19 18:43 ` Eduard Zingerman
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=20260115150405.443581-1-realwujing@gmail.com \
--to=realwujing@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--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