From: Hungyu Lin <dennylin0707@gmail.com>
To: ast@kernel.org, bpf@vger.kernel.org
Cc: daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com,
memxor@gmail.com, martin.lau@linux.dev, song@kernel.org,
yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com,
ihor.solodrai@linux.dev, shuah@kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Hungyu Lin <dennylin0707@gmail.com>
Subject: [PATCH bpf-next] bpf: Use 64-bit writes when inlining bpf_loop
Date: Wed, 4 Nov 2026 03:27:00 +0000 [thread overview]
Message-ID: <20261104032700.55296-1-dennylin0707@gmail.com> (raw)
The error path in inline_bpf_loop() writes -E2BIG with BPF_MOV32_IMM(),
zero-extending it to 0x00000000fffffff9 instead of the 64-bit negative
value returned by the helper. A caller keeping the result in a long
therefore misses the error when testing ret < 0.
Use BPF_MOV64_IMM() for the error return and loop-counter initialization.
Neither write needs truncation, and this avoids the extra zero extensions
required by the s390 and RISC-V JITs.
Keep the helper result in a long before checking it in the existing
bpf_loop test, so its over-limit case catches the wrong upper bits.
Drop the now redundant architecture-specific expectations from
patch_list_insert_in_front().
Fixes: 1ade23711971 ("bpf: Inline calls to bpf_loop when callback is known")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/DMM0E6AA3KB6.28SYB9X6ZYIHL@gmail.com/
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
kernel/bpf/fixups.c | 4 ++--
tools/testing/selftests/bpf/progs/bpf_loop.c | 8 +++++---
.../selftests/bpf/progs/verifier_patch_list.c | 18 ------------------
3 files changed, 7 insertions(+), 23 deletions(-)
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 6b719a28be68..321624a4b2b3 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -2783,7 +2783,7 @@ static int inline_bpf_loop(struct bpf_verifier_env *env, int position,
* expected number of iterations is too big.
*/
insn_buf[cnt++] = BPF_JMP_IMM(BPF_JLE, BPF_REG_1, BPF_MAX_LOOPS, 2);
- insn_buf[cnt++] = BPF_MOV32_IMM(BPF_REG_0, -E2BIG);
+ insn_buf[cnt++] = BPF_MOV64_IMM(BPF_REG_0, -E2BIG);
insn_buf[cnt++] = BPF_JMP_IMM(BPF_JA, 0, 0, 16);
/* spill R6, R7, R8 to use these as loop vars */
insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, r6_offset);
@@ -2791,7 +2791,7 @@ static int inline_bpf_loop(struct bpf_verifier_env *env, int position,
insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_8, r8_offset);
/* initialize loop vars */
insn_buf[cnt++] = BPF_MOV64_REG(reg_loop_max, BPF_REG_1);
- insn_buf[cnt++] = BPF_MOV32_IMM(reg_loop_cnt, 0);
+ insn_buf[cnt++] = BPF_MOV64_IMM(reg_loop_cnt, 0);
insn_buf[cnt++] = BPF_MOV64_REG(reg_loop_ctx, BPF_REG_3);
/* loop header,
* if reg_loop_cnt >= reg_loop_max skip the loop body
diff --git a/tools/testing/selftests/bpf/progs/bpf_loop.c b/tools/testing/selftests/bpf/progs/bpf_loop.c
index 1d194455b109..7a8ae1110c0a 100644
--- a/tools/testing/selftests/bpf/progs/bpf_loop.c
+++ b/tools/testing/selftests/bpf/progs/bpf_loop.c
@@ -66,14 +66,16 @@ SEC("fentry/" SYS_PREFIX "sys_nanosleep")
int test_prog(void *ctx)
{
struct callback_ctx data = {};
+ long ret;
if (bpf_get_current_pid_tgid() >> 32 != pid)
return 0;
- nr_loops_returned = bpf_loop(nr_loops, callback, &data, 0);
+ ret = bpf_loop(nr_loops, callback, &data, 0);
+ nr_loops_returned = ret;
- if (nr_loops_returned < 0)
- err = nr_loops_returned;
+ if (ret < 0)
+ err = ret;
else
g_output = data.output;
diff --git a/tools/testing/selftests/bpf/progs/verifier_patch_list.c b/tools/testing/selftests/bpf/progs/verifier_patch_list.c
index a9ad1dc56c3a..581b75b7b5c5 100644
--- a/tools/testing/selftests/bpf/progs/verifier_patch_list.c
+++ b/tools/testing/selftests/bpf/progs/verifier_patch_list.c
@@ -298,23 +298,6 @@ __arch_arm64
__arch_riscv64
__arch_loongarch
__success
-/* The verifier adds two zero extensions for these JITs. */
-#if defined(__TARGET_ARCH_s390) || defined(__TARGET_ARCH_riscv)
-__xlated("25: call pc+7")
-__xlated("...")
-__xlated("32: exit")
-__xlated("33: r0 = ")
-__xlated("35: r0 = &(void __percpu *)(r0)")
-__xlated("36: r0 = *(u64 *)(r0 +0)")
-__xlated("37: if r0 != 0x0 goto pc+2")
-__xlated("38: r0 = 1")
-__xlated("39: exit")
-__xlated("40: r1 += -1")
-__xlated("...")
-__xlated("51: if r1 != 0x0 goto pc-12")
-__xlated("52: r0 = 0")
-__xlated("53: exit")
-#else
__xlated("23: call pc+7")
__xlated("...")
__xlated("30: exit")
@@ -329,7 +312,6 @@ __xlated("...")
__xlated("49: if r1 != 0x0 goto pc-12")
__xlated("50: r0 = 0")
__xlated("51: exit")
-#endif
__naked void patch_list_insert_in_front(void)
{
asm volatile (
base-commit: 8fb43ffc3327d12d57d3fbe6f4820b5a522de0d1
--
2.43.0
reply other threads:[~2026-11-04 3:27 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=20261104032700.55296-1-dennylin0707@gmail.com \
--to=dennylin0707@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=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--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
all inboxes | Powered by JetHome®