[PATCH bpf-next v2] bpf, arm32: build constants inline instead of using a literal pool

Alexei Starovoitov alexei.starovoitov at gmail.com
Fri Nov 20 11:12:28 PST 2026


On Fri, Nov 20, 2026 at 06:01 AM Ramin Moussavi <ramin.moussavi at yacoub.de> wrote:
> Commit 0b59d8806a31 ("ARM: net: delegate filter to kernel interpreter
> when imm_offset() return value can't fit into 12bits.") handles that by
> setting FLAG_IMM_OVERFLOW and dropping the whole program to the
> interpreter, and already noted that multiple pools "would better be done
> as a feature instead of fix".

Not always. imm is u16 in imm_offset():

	imm = offset - (8 + ctx->idx * 4);

	if (imm & ~0xfff) {

Take a classic filter that starts with 'div #3'.
Put 64 KB of code without hard constants after it:

  offset - (8 + ctx->idx * 4)   0x10008
  imm                           0x8, the check passes
  ldr ip, [pc, #8]              loads an insn, not jit_udiv32
  blx ip                        jumps to the value of that insn

So the patch fixes a crash too.
Pls say that in the log and add a Fixes tag for 0b59d8806a31.

> ARMv7 and later are unaffected, the MOVW/MOVT path is unchanged.

Only when the kernel is built without v6.
The v6 line wins in arch/arm/Makefile:

	cpp-$(CONFIG_CPU_32v7)		:=-D__LINUX_ARM_ARCH__=7
	cpp-$(CONFIG_CPU_32v6)		:=-D__LINUX_ARM_ARCH__=6

imx_v6_v7_defconfig and omap2plus_defconfig enable both.
With the JIT on, an i.MX6 runs the new code. Pls fix the log.

> +	for (shift = 0; shift < 32; shift += 8) {
> +		u32 part = val & (0xffU << shift);
> +
> +		if (!part)
> +			continue;
> +		if (first)
> +			emit(ARM_MOV_I(rd, imm8m(part)), ctx);
> +		else
> +			emit(ARM_ORR_I(rd, rd, imm8m(part)), ctx);
> +		first = false;
> +	}
> +	if (first)
> +		emit(ARM_MOV_I(rd, 0), ctx);

The log says that arm64 does the same. Not for small negative values.
arm64 emits one MOVN. Here they get the longest sequence.
For 'r2 += -264' val is 0xfffffef8 and ~val is 0x107, not an imm8m:

  version      insns for 0xfffffef8
  -----------  ---------------------------------
  today        ldr r8, [pc, #off]
  patch        mov, orr, orr, orr
  diff below   mvn r8, #0x7; bic r8, r8, #0x100

The last 'if (first)' is dead code.
emit_mov_i() is the only caller and imm8m(0) is 0.

Here is emit_mov_i_no8m() on top of your patch. I didn't compile it:

diff --git a/arch/arm/net/bpf_jit_32.c b/arch/arm/net/bpf_jit_32.c
index 58d066a..28a2c5e 100644
--- a/arch/arm/net/bpf_jit_32.c
+++ b/arch/arm/net/bpf_jit_32.c
@@ -411,30 +411,30 @@ static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
 {
 #if __LINUX_ARM_ARCH__ < 7
 	/*
-	 * A single MVN if the inverted value is an imm8m, else one
-	 * MOV/ORR per non-zero byte -- a byte is always an imm8m.
+	 * A single MVN if the inverted value is an imm8m. Else MOV + ORR set
+	 * the bytes of val, or MVN + BIC clear the bytes of ~val.
 	 */
+	u32 op = ARM_MOV_I(rd, 0), next = ARM_ORR_I(rd, rd, 0);
 	int imm12 = imm8m(~val);
 	unsigned int shift;
-	bool first = true;
 
 	if (imm12 >= 0) {
 		emit(ARM_MVN_I(rd, imm12), ctx);
 		return;
 	}
+	if (val >= 0xffff0000) {
+		op = ARM_MVN_I(rd, 0);
+		next = ARM_BIC_I(rd, rd, 0);
+		val = ~val;
+	}
 	for (shift = 0; shift < 32; shift += 8) {
 		u32 part = val & (0xffU << shift);
 
 		if (!part)
 			continue;
-		if (first)
-			emit(ARM_MOV_I(rd, imm8m(part)), ctx);
-		else
-			emit(ARM_ORR_I(rd, rd, imm8m(part)), ctx);
-		first = false;
+		emit(op | imm8m(part), ctx);
+		op = next;
 	}
-	if (first)
-		emit(ARM_MOV_I(rd, 0), ctx);
 #else
 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
 	if (val > 0xffff)

pw-bot: cr



More information about the linux-arm-kernel mailing list