All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/1] selftests/bpf: cover shift counts over all operand register pairs
@ 2026-12-02 22:18 Ramin Moussavi
  2026-12-02 22:18 ` [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across " Ramin Moussavi
  0 siblings, 1 reply; 3+ messages in thread
From: Ramin Moussavi @ 2026-12-02 22:18 UTC (permalink / raw)
  To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Dave Thaler

I measured what the 32-bit JITs do with a shift whose count comes from
a register, and the answer differs from the interpreter on four of the
six targets I could run.

Method: one small program per case, loaded as BPF_PROG_TYPE_SOCKET_FILTER
and run through BPF_PROG_TEST_RUN, twice per boot -- once with
/proc/sys/net/core/bpf_jit_enable at 0 and once at 1.  Same kernel, same
program text; everything that differs between the two passes is the JIT.
bpf-next at fcd1857c35ebe, under qemu.

Two parts of the tree say different things about a count beyond the
width, and the older one is the one the JITs follow.

kernel/bpf/verifier.c has carried "Shifts greater than 31 or 63 are
undefined.  This includes shifts by a negative number" since 2017; it
sits in is_safe_to_compute_dst_reg_range() today.  The ISA document
says the opposite, and says why it was changed: commit 8819495a754e7
("bpf, docs: Shift operations are defined to use a mask") added "Shift
operations use a mask of 0x3F (63) for 64-bit operations and 0x1F (31)
for 32-bit operations", with the reasoning "since otherwise shifting by
a value out of range (like negative) is undefined".  The interpreter
has done the masking all along, on every target I measured.

Neither side rejects such a program: the predicate returns false, the
caller marks the destination unknown, and the count reaching the shift
is whatever the register holds.

So either the 2023 definition is what a JIT owes, and four of the
targets below are wrong, or "undefined" still holds and the sentence in
the ISA document is the thing to fix.  The measurements are the same
under both readings.

  1) ALU64, count 64

        r6 = 0x0123456789abcdef
        r7 = 64
        r6 >>= r7

     64 & 63 == 0, so the value must come out unchanged.

        interpreter, everywhere 0x0123456789abcdef
        mips32, arc, powerpc32  0x0123456789abcdef
        x86-32, riscv32         0x0000000001234567   shifted by 32
        arm32                   0x0000000000000000

  2) ALU64, count -1

        r6 = 0xfedcba9876543210
        r7 = -1
        r6 s>>= r7

     -1 & 63 == 63, so all sign bits.

        interpreter, everywhere         0xffffffffffffffff
        mips32, arc, powerpc32, x86-32  0xffffffffffffffff
        riscv32                         0xfffffffffdb97530  shifted by 31
        arm32                           0xffffffff00000000

  3) ALU32, count 32

        r6 = 0x89abcdef
        r7 = 32
        w6 >>= w7               (and s>>=)

                                        >>=             s>>=
        interpreter, everywhere         0x89abcdef      0x89abcdef
        mips32, arc, x86-32, riscv32    0x89abcdef      0x89abcdef
        powerpc32, arm32                0x00000000      0xffffffff

What I think should change in jit_insns
======================================

The existing shift_counts subtest already runs counts beyond the width
-- 32, 33, 63, 64, 65, 127, -1, -3 and 2^32+3 -- so it catches the
plain masking failure on arm32, x86-32 and powerpc32 today.  What it
does not vary is the registers: the value is always r1 and the count is
always r2.  That hides two things.

Sweep the operand registers instead of fixing them.  Two reasons, and
the second one is the one that surprised me:

  a) Different pairs compile to different code, and the JITs do not
     agree on which.  arm32 keeps r0, r1 and r6 in hardware registers
     and spills the rest to a stack scratch area; x86-32 keeps every
     BPF register on the stack; powerpc32 keeps them all in registers.
     shift_counts puts the count in r2, so on arm32 it only ever
     exercises the spilled path, and nothing says whether the other one
     behaves the same.

  b) rd == rs is a case of its own, and on powerpc32 it is the only
     64-bit failure there is:

        powerpc32, alu64 >>= and s>>=, count -1
              rs: 0123456789
           rd 0: J.........
           rd 1: .J........
           ...
           rd 9: .........J

        (J = JIT differs from the interpreter, over all 100 pairs)

     Ten of a hundred for each of the two operations, exactly the
     diagonal -- every other 64-bit case on that target is correct.
     Both shifts are open coded in the destination registers, and both
     write the low word as the second of eight instructions:

        1  SUBFIC(_R0, src_reg, 32)
        2  SRW(dst_reg, src2_reg, src_reg)       <- writes dst_reg
        3  ADDI(tmp_reg, src_reg, 32)            <- reads src_reg
        ...
        7  SRW(dst_reg_h, src2_reg_h, src_reg)   <- reads src_reg

     src2_reg defaults to dst_reg, so with rd == rs that one register
     is value, count and destination at once.  Step 2 overwrites the
     count, and steps 3 and 7 read what is left.

     That this is an ordering mistake and not something the aliasing
     forces can be read off the same file: <<= is open coded the same
     way, but writes the low word last, after src_reg is read for the
     last time -- and it is correct for every pair.  arm32 and x86-32
     cannot have the problem at all, because they load the operands
     into scratch registers and write the destination back only at the
     end.

     A fixed r1/r2 reaches none of this.

A hundred pairs times three counts is still a small test.  The patch
below adds the sweep.  jit_insns/shift_counts is already red on arm32,
x86-32 and powerpc32 for part of the counts, so no green subtest turns
red -- the cases the sweep adds are failures in the JITs, not in the
test.  The probe used for the measurements is a single self-contained
C file with no libbpf dependency; happy to post it too.

Ramin Moussavi (1):
  selftests/bpf: Check shift counts across operand register pairs

 .../selftests/bpf/prog_tests/jit_insns.c      | 68 ++++++++++++++++++-
 1 file changed, 66 insertions(+), 2 deletions(-)


base-commit: fcd1857c35ebe058dde310cbe7d350682e008037
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across operand register pairs
  2026-12-02 22:18 [PATCH bpf-next 0/1] selftests/bpf: cover shift counts over all operand register pairs Ramin Moussavi
@ 2026-12-02 22:18 ` Ramin Moussavi
  2026-12-02 22:21   ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Ramin Moussavi @ 2026-12-02 22:18 UTC (permalink / raw)
  To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Dave Thaler

jit_insns covers the masking of shift counts in shift_counts, but every
case uses the same two registers, r1 for the value and r2 for the count.
A 32-bit JIT does not treat all BPF registers alike -- arm32 keeps r0,
r1 and r6 in hardware registers and spills the rest to a stack scratch
area -- so one fixed pair reaches the shift through one path, and the
others go untested.  The register pair sweep that would cover them,
alu_reg_pairs, masks the count to 0..31 before it starts.

Run the counts over all pairs as well.  Three counts and one value are
enough: the pairs are what is being varied, and 32, 64 and -1 each take
a different branch in the JITs I looked at.  That is 300 more cases per
operation and width, 18 more program loads in all.  Name the pair in
the failure message, since with rd == rs the two inputs are the same
value and nothing else tells the cases apart.

Measured on bpf-next under qemu with the JIT enabled:

                                        arm32   x86-32   ppc32
    alu*_*_x counts beyond the width      112       24      47
    alu*_*_x counts over register pairs  1220      300     520

rd == rs earns the cases on its own.  On ppc32 every one of the 47 the
existing test finds is a 32-bit shift; the only 64-bit failures are in
>>= and s>>=, on the diagonal, 10 pairs each.  That JIT shifts into the
destination registers and writes the low word before it has read the
count for the last time, which can only go wrong when the two are the
same register.  No fixed pair reaches it.

x86-32 is the other case: the sweep finds no new kind of failure there,
only the same 64-bit one in all 100 pairs.

Signed-off-by: Ramin Moussavi <ramin.moussavi@yacoub.de>
---
 .../selftests/bpf/prog_tests/jit_insns.c      | 68 ++++++++++++++++++-
 1 file changed, 66 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/jit_insns.c b/tools/testing/selftests/bpf/prog_tests/jit_insns.c
index 0aaab23a2eef8..4d67e7db38a1b 100644
--- a/tools/testing/selftests/bpf/prog_tests/jit_insns.c
+++ b/tools/testing/selftests/bpf/prog_tests/jit_insns.c
@@ -2,6 +2,7 @@
 /* Copyright (c) 2026 Isovalent */
 
 #include <endian.h>
+#include <stdarg.h>
 #include <stdint.h>
 #include <test_progs.h>
 #include <bpf/btf.h>
@@ -29,6 +30,7 @@ struct batch {
 	struct sweep_ctx ctx;
 	__u64 exp[NR_CASES][4];
 	__u8 nr_out[NR_CASES];
+	char note[NR_CASES][12];
 	int nr;
 	char desc[80];
 };
@@ -102,8 +104,9 @@ static void batch_flush(struct batch *b)
 			for (j = 0; j < b->nr_out[k]; j++) {
 				if (b->ctx.out[k][j] == b->exp[k][j])
 					continue;
-				snprintf(buf, sizeof(buf), "%s: in0=0x%llx in1=0x%llx out%d",
-					 b->desc, b->ctx.in[k][0], b->ctx.in[k][1], j);
+				snprintf(buf, sizeof(buf), "%s: %s%sin0=0x%llx in1=0x%llx out%d",
+					 b->desc, b->note[k], b->note[k][0] ? " " : "",
+					 b->ctx.in[k][0], b->ctx.in[k][1], j);
 				ASSERT_EQ(b->ctx.out[k][j], b->exp[k][j], buf);
 			}
 		}
@@ -124,10 +127,23 @@ static int batch_case(struct batch *b, int rc, __u64 in0, __u64 in1)
 	b->ctx.in[k][0] = in0;
 	b->ctx.in[k][1] = in1;
 	b->nr_out[k] = 0;
+	b->note[k][0] = 0;
 	EMIT(b, BPF_LDX_MEM(BPF_DW, rc, BPF_REG_10, CTX_SPILL));
 	return k;
 }
 
+/* Name the case in its failure message, where the operands alone do not say
+ * which one it was
+ */
+__printf(3, 4) static void batch_note(struct batch *b, int k, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vsnprintf(b->note[k], sizeof(b->note[k]), fmt, ap);
+	va_end(ap);
+}
+
 static void batch_expect(struct batch *b, int k, int j, __u64 val)
 {
 	b->exp[k][j] = val;
@@ -826,6 +842,53 @@ static void test_shifts(void)
 	batch_fini(&b);
 }
 
+/*
+ * The same counts across all pairs of operand registers. A JIT that keeps
+ * only some of the BPF registers in hardware registers reaches the shift
+ * through two different paths, and the masking has to be in both.
+ */
+static void shift_count_pairs(struct batch *b, int op, bool alu32)
+{
+	static const __s64 counts[] = { 32, 64, -1 };
+	int code = (alu32 ? BPF_ALU : BPF_ALU64) | op | BPF_X;
+	__u64 val = 0x0123456789abcdefULL;
+	int bits = alu32 ? 32 : 64;
+	int c, rd, rs, rc, k;
+
+	snprintf(b->desc, sizeof(b->desc), "alu%d_%s_x counts over register pairs",
+		 bits, alu_name(op));
+	for (c = 0; c < ARRAY_SIZE(counts); c++) {
+		__u64 cnt = alu32 ? (__u32)counts[c] : counts[c];
+		__u64 reg = val, self = cnt, res, same;
+
+		if (alu32) {
+			reg = op == BPF_ARSH ? (__u64)(__s32)reg : (__u32)reg;
+			self = op == BPF_ARSH ? (__u64)(__s32)self : (__u32)self;
+		}
+		alu_result(&res, reg, cnt & (bits - 1), op, 0);
+		/* With one register for both operands the count is the value */
+		alu_result(&same, self, cnt & (bits - 1), op, 0);
+		if (alu32) {
+			res = (__u32)res;
+			same = (__u32)same;
+		}
+		for (rd = BPF_REG_0; rd <= BPF_REG_9; rd++) {
+			for (rs = BPF_REG_0; rs <= BPF_REG_9; rs++) {
+				rc = scratch_reg(rd, rs);
+				k = batch_case(b, rc, rd == rs ? counts[c] : val, counts[c]);
+				batch_note(b, k, "r%d,r%d", rd, rs);
+				LD_IN(b, k, 0, rc, rd);
+				if (rs != rd)
+					LD_IN(b, k, 1, rc, rs);
+				EMIT(b, BPF_RAW_INSN(code, rd, rs, 0, 0));
+				ST_OUT(b, k, 0, rc, rd);
+				batch_expect(b, k, 0, rd == rs ? same : res);
+			}
+		}
+		batch_flush(b);
+	}
+}
+
 /*
  * Shifts by register with counts at and above the width and negative ones,
  * which the ISA masks by the width. Apart in a subtest of its own, as not
@@ -869,6 +932,7 @@ static void test_shift_counts(void)
 				}
 			}
 			batch_flush(&b);
+			shift_count_pairs(&b, op, f);
 		}
 	}
 	batch_fini(&b);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across operand register pairs
  2026-12-02 22:18 ` [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across " Ramin Moussavi
@ 2026-12-02 22:21   ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 22:21 UTC (permalink / raw)
  To: Ramin Moussavi, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/bpf: non-standard multi-line comment style above
  batch_note()
--

commit f23b98c932cf4075dd7ccf4be0a85595080b26b9
Author: Ramin Moussavi <ramin.moussavi@yacoub.de>

selftests/bpf: Check shift counts across operand register pairs

This commit extends the shift_counts test in jit_insns to test out-of-range
and negative shift counts across all operand register pairs and adds a
per-case note field to distinguish register pairs in failure messages.

> diff --git a/tools/testing/selftests/bpf/prog_tests/jit_insns.c b/tools/testing/selftests/bpf/prog_tests/jit_insns.c
> index 0aaab23a2eef8..4d67e7db38a1b 100644
> --- a/tools/testing/selftests/bpf/prog_tests/jit_insns.c
> +++ b/tools/testing/selftests/bpf/prog_tests/jit_insns.c

[ ... ]

> @@ -124,10 +127,23 @@ static int batch_case(struct batch *b, int rc, __u64 in0, __u64 in1)
>  	b->ctx.in[k][0] = in0;
>  	b->ctx.in[k][1] = in1;
>  	b->nr_out[k] = 0;
> +	b->note[k][0] = 0;
>  	EMIT(b, BPF_LDX_MEM(BPF_DW, rc, BPF_REG_10, CTX_SPILL));
>  	return k;
>  }
>  
> +/* Name the case in its failure message, where the operands alone do not say
> + * which one it was
> + */
> +__printf(3, 4) static void batch_note(struct batch *b, int k, const char *fmt, ...)

[Severity: Low]
This isn't a bug, but BPF multi-line comment style puts the opening /* on
its own line with the comment text starting on the next line, matching the
comment above shift_count_pairs().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202221801.3863568-1-ramin.moussavi@yacoub.de?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 22:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 22:18 [PATCH bpf-next 0/1] selftests/bpf: cover shift counts over all operand register pairs Ramin Moussavi
2026-12-02 22:18 ` [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across " Ramin Moussavi
2026-12-02 22:21   ` sashiko-bot

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.