From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org, memxor@gmail.com
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Shuah Khan <shuah@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>,
Mykyta Yatsenko <yatsenko@meta.com>,
Taegu Ha <hataegu0826@gmail.com>,
Amery Hung <ameryhung@gmail.com>,
Paul Chaignon <paul.chaignon@gmail.com>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
llvm@lists.linux.dev
Subject: [PATCH net-next v1] selftests/bpf: Fix insn count expectations in chained global subprogs test
Date: Thu, 13 Aug 2026 13:16:29 +0800 [thread overview]
Message-ID: <20260813051635.121670-1-jiayuan.chen@linux.dev> (raw)
The test asserts exact per-subprog insns_self/insns_total values and the
aggregate processed count, but these depend on compiler codegen. The
expected values match -mcpu=v3 output; under -mcpu=v4 clang emits a
sign-extending load in global_good() instead of a load plus two shifts,
so the subprog is verified in 3 insns instead of 5 and the test fails on
the cpuv4 flavor.
Rewrite the involved subprogs as naked functions with hand-written
instruction sequences using only v1 instructions, so the verified insn
counts no longer depend on cpu version or compiler codegen.
Fixes: c2e6c7de8830 ("bpf: Show more useful info in stack depth stats")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
.../bpf/progs/verifier_global_subprogs.c | 56 +++++++++++++++----
1 file changed, 45 insertions(+), 11 deletions(-)
diff --git a/tools/testing/selftests/bpf/progs/verifier_global_subprogs.c b/tools/testing/selftests/bpf/progs/verifier_global_subprogs.c
index 7b65eea97ebc..3352dc177ec1 100644
--- a/tools/testing/selftests/bpf/progs/verifier_global_subprogs.c
+++ b/tools/testing/selftests/bpf/progs/verifier_global_subprogs.c
@@ -25,9 +25,22 @@ __noinline long global_bad(void)
return arr[unkn_idx]; /* BOOM */
}
-__noinline long global_good(void)
+/* Insn counts asserted in chained_global_func_calls_success() depend on
+ * the exact instruction sequences of this function and its callers, so
+ * they are written in inline asm to be independent of compiler codegen.
+ */
+__naked __noinline long global_good(void)
{
- return arr[0];
+ /* return (long)arr[0]; 5 insns (ld_imm64 counts as one) */
+ asm volatile (
+ "r1 = %[arr] ll;"
+ "r0 = *(u32 *)(r1 + 0);"
+ "r0 <<= 32;"
+ "r0 s>>= 32;"
+ "exit;"
+ :
+ : __imm_addr(arr)
+ : __clobber_all);
}
__noinline long global_calls_bad(void)
@@ -35,9 +48,13 @@ __noinline long global_calls_bad(void)
return global_good() + global_bad() /* does BOOM indirectly */;
}
-__noinline long global_calls_good_only(void)
+__naked __noinline long global_calls_good_only(void)
{
- return global_good();
+ /* return global_good(); 2 insns */
+ asm volatile (
+ "call global_good;"
+ "exit;"
+ ::: __clobber_all);
}
__noinline long global_dead(void)
@@ -56,13 +73,30 @@ __msg("subprog 0 (chained_global_func_calls_success) main insns_self 7 insns_tot
__msg("subprog {{[0-9]+}} (global_calls_good_only) global insns_self 2 insns_total 2 stack")
__msg("subprog {{[0-9]+}} (global_good) global insns_self 5 insns_total 5 stack")
__msg("processed 14 insns")
-int chained_global_func_calls_success(void)
-{
- int sum = 0;
-
- if (call_dead_subprog)
- sum += global_dead();
- return global_calls_good_only() + sum;
+__naked int chained_global_func_calls_success(void)
+{
+ /* int sum = 0;
+ * if (call_dead_subprog)
+ * sum += global_dead();
+ * return global_calls_good_only() + sum;
+ *
+ * call_dead_subprog is known false, so 7 insns are processed:
+ * the branch to l0 is always taken and global_dead() is dead code.
+ */
+ asm volatile (
+ "r6 = 0;"
+ "r1 = %[call_dead_subprog] ll;"
+ "r1 = *(u8 *)(r1 + 0);"
+ "if r1 == 0 goto l0_%=;"
+ "call global_dead;"
+ "r6 = r0;"
+ "l0_%=:"
+ "call global_calls_good_only;"
+ "r0 += r6;"
+ "exit;"
+ :
+ : __imm_addr(call_dead_subprog)
+ : __clobber_all);
}
SEC("?raw_tp")
--
2.43.0
reply other threads:[~2026-08-13 5:17 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260813051635.121670-1-jiayuan.chen@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hataegu0826@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=justinstitt@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=paul.chaignon@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yatsenko@meta.com \
--cc=yonghong.song@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox