* [PATCH bpf-next] bpf, arm32: build constants inline instead of using a literal pool
@ 2026-11-11 12:16 Ramin Moussavi
2026-11-20 14:01 ` [PATCH bpf-next v2] " Ramin Moussavi
0 siblings, 1 reply; 3+ messages in thread
From: Ramin Moussavi @ 2026-11-11 12:16 UTC (permalink / raw)
To: Russell King, Puranjay Mohan
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf,
linux-arm-kernel, linux-kernel
ARMv5 and ARMv6 have no MOVW/MOVT, so emit_mov_i_no8m() put every
constant that is not an imm8m into a literal pool behind the epilogue and
loaded it with LDR rX, [pc, #off]. That offset field is 12 bits and the
pool sits at the end of the image, so from roughly 4 KB of JITed code on,
the load no longer reaches it.
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".
Build the constant in the register instead: a single MVN when the
inverted value is an imm8m, otherwise one MOV/ORR per non-zero byte,
since a byte at a byte-aligned position is always an imm8m. The arm64
JIT does the same in emit_a64_mov_i64() with MOVZ/MOVN and MOVK. Without
a pool there is no offset to overflow, so programs of any size are
translated, and FLAG_IMM_OVERFLOW, ctx->imms, ctx->imm_count and
ctx->epilogue_bytes go away with it.
It costs up to four instructions per hard constant instead of one. The
pool was not much cheaper: imm_offset() counts duplicates on the sizing
pass, so every use already took 4 bytes of LDR plus 4 bytes of slot.
The 4 KB ceiling is low enough to be hit by the kernel itself. An ARMv5
kernel fails to boot when CONFIG_BPF_JIT_ALWAYS_ON=y and
CONFIG_NET_PTP_CLASSIFY=y are set together: the first leaves no
interpreter to fall back to, the second makes sock_init() build the PTP
classifier, which needs 5124 bytes of ARM code. bpf_prog_create() then
returns -ENOTSUPP and ptp_classifier_init() turns that into a BUG_ON.
Neither option is unusual - NET_PTP_CLASSIFY is selected by
PTP_1588_CLOCK, which defaults to ETHERNET, which defaults to y:
kernel BUG at net/core/ptp_classifier.c:223!
PC is at ptp_classifier_init+0x3c/0x70
ptp_classifier_init from sock_init+0xac/0xf0
seccomp reaches the JIT the same way, so filters of that size are likely
rejected there as well; that was not measured.
ARMv7 and later are unaffected, the MOVW/MOVT path is unchanged.
Tested on versatile_defconfig (ARM926EJ-S) under qemu with
CONFIG_BPF_JIT_ALWAYS_ON=y: the kernel boots again and SO_ATTACH_FILTER
accepts classic filters up to BPF_MAXINSNS, all of which the JIT now
translates. That is the classic limit of 4096 instructions; eBPF
programs loaded through bpf() can be far larger and were not part of the
test. The emitted sequences were verified separately by executing them
for 6.2 million values.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Ramin Moussavi <ramin.moussavi@yacoub.de>
---
arch/arm/net/bpf_jit_32.c | 114 +++++++++-----------------------------
arch/arm/net/bpf_jit_32.h | 3 +
2 files changed, 29 insertions(+), 88 deletions(-)
diff --git a/arch/arm/net/bpf_jit_32.c b/arch/arm/net/bpf_jit_32.c
index 9ede81afbc50..d3b5d7745068 100644
--- a/arch/arm/net/bpf_jit_32.c
+++ b/arch/arm/net/bpf_jit_32.c
@@ -130,8 +130,6 @@ enum {
#define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */
#define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */
-#define FLAG_IMM_OVERFLOW (1 << 0)
-
/*
* Map eBPF registers to ARM 32bit registers or stack scratch space.
*
@@ -193,10 +191,6 @@ static const s8 bpf2a32[][2] = {
* offsets : array of eBPF instruction offsets in
* JITed code.
* target : final JITed code.
- * epilogue_bytes : no of bytes used in epilogue.
- * imm_count : no of immediate counts used for global
- * variables.
- * imms : array of global variable addresses.
*/
struct jit_ctx {
@@ -209,11 +203,6 @@ struct jit_ctx {
u32 *offsets;
u32 *target;
u32 stack_size;
-#if __LINUX_ARM_ARCH__ < 7
- u16 epilogue_bytes;
- u16 imm_count;
- u32 *imms;
-#endif
};
/*
@@ -404,53 +393,6 @@ static void jit_fill_hole(void *area, unsigned int size)
#define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE)
#define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
-#if __LINUX_ARM_ARCH__ < 7
-
-static u16 imm_offset(u32 k, struct jit_ctx *ctx)
-{
- unsigned int i = 0, offset;
- u16 imm;
-
- /* on the "fake" run we just count them (duplicates included) */
- if (ctx->target == NULL) {
- ctx->imm_count++;
- return 0;
- }
-
- while ((i < ctx->imm_count) && ctx->imms[i]) {
- if (ctx->imms[i] == k)
- break;
- i++;
- }
-
- if (ctx->imms[i] == 0)
- ctx->imms[i] = k;
-
- /* constants go just after the epilogue */
- offset = ctx->offsets[ctx->prog->len - 1] * 4;
- offset += ctx->prologue_bytes;
- offset += ctx->epilogue_bytes;
- offset += i * 4;
-
- ctx->target[offset / 4] = k;
-
- /* PC in ARM mode == address of the instruction + 8 */
- imm = offset - (8 + ctx->idx * 4);
-
- if (imm & ~0xfff) {
- /*
- * literal pool is too far, signal it into flags. we
- * can only detect it on the second pass unfortunately.
- */
- ctx->flags |= FLAG_IMM_OVERFLOW;
- return 0;
- }
-
- return imm;
-}
-
-#endif /* __LINUX_ARM_ARCH__ */
-
static inline int bpf2a32_offset(int bpf_to, int bpf_from,
const struct jit_ctx *ctx) {
int to, from;
@@ -469,7 +411,30 @@ static inline int bpf2a32_offset(int bpf_to, int bpf_from,
static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
{
#if __LINUX_ARM_ARCH__ < 7
- emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
+ /* A single MVN if the inverted value is an imm8m, else one
+ * MOV/ORR per non-zero byte -- a byte is always an imm8m.
+ */
+ int imm12 = imm8m(~val);
+ unsigned int shift;
+ bool first = true;
+
+ if (imm12 >= 0) {
+ emit(ARM_MVN_I(rd, imm12), ctx);
+ return;
+ }
+ 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);
#else
emit(ARM_MOVW(rd, val & 0xffff), ctx);
if (val > 0xffff)
@@ -2091,14 +2056,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
pr_err_once("unknown opcode %02x\n", code);
return -EINVAL;
}
-
- if (ctx->flags & FLAG_IMM_OVERFLOW)
- /*
- * this instruction generated an overflow when
- * trying to access the literal pool, so
- * delegate this filter to the kernel interpreter.
- */
- return -1;
return 0;
}
@@ -2192,21 +2149,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
ctx.epilogue_offset = ctx.idx;
-#if __LINUX_ARM_ARCH__ < 7
- tmp_idx = ctx.idx;
build_epilogue(&ctx);
- ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
-
- ctx.idx += ctx.imm_count;
- if (ctx.imm_count) {
- ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
- if (ctx.imms == NULL)
- goto out_off;
- }
-#else
- /* there's nothing about the epilogue on ARMv7 */
- build_epilogue(&ctx);
-#endif
/* Now we can get the actual image size of the JITed arm code.
* Currently, we are not considering the THUMB-2 instructions
* for jit, although it can decrease the size of the image.
@@ -2224,7 +2167,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
* we must fall back to the interpretation
*/
if (header == NULL)
- goto out_imms;
+ goto out_off;
/* 2.) Actual pass to generate final JIT code */
ctx.target = (u32 *) image_ptr;
@@ -2254,11 +2197,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
prog->jited = 1;
prog->jited_len = image_size;
-out_imms:
-#if __LINUX_ARM_ARCH__ < 7
- if (ctx.imm_count)
- kfree(ctx.imms);
-#endif
out_off:
kfree(ctx.offsets);
@@ -2267,6 +2205,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
out_free:
image_ptr = NULL;
bpf_jit_binary_free(header);
- goto out_imms;
+ goto out_off;
}
diff --git a/arch/arm/net/bpf_jit_32.h b/arch/arm/net/bpf_jit_32.h
index 438f0e1f91a0..76d83b486ded 100644
--- a/arch/arm/net/bpf_jit_32.h
+++ b/arch/arm/net/bpf_jit_32.h
@@ -104,6 +104,7 @@
#define ARM_INST_MOV_I 0x03a00000
#define ARM_INST_MOVW 0x03000000
#define ARM_INST_MOVT 0x03400000
+#define ARM_INST_MVN_I 0x03e00000
#define ARM_INST_MUL 0x00000090
@@ -231,6 +232,8 @@
#define ARM_MOVT(rd, imm) \
(ARM_INST_MOVT | ((imm) >> 12) << 16 | (rd) << 12 | ((imm) & 0x0fff))
+#define ARM_MVN_I(rd, imm) _AL3_I(ARM_INST_MVN, rd, 0, imm)
+
#define ARM_MUL(rd, rm, rn) (ARM_INST_MUL | (rd) << 16 | (rm) << 8 | (rn))
#define ARM_POP(regs) (ARM_INST_POP | (regs))
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH bpf-next v2] bpf, arm32: build constants inline instead of using a literal pool
2026-11-11 12:16 [PATCH bpf-next] bpf, arm32: build constants inline instead of using a literal pool Ramin Moussavi
@ 2026-11-20 14:01 ` Ramin Moussavi
2026-11-20 19:12 ` Alexei Starovoitov
0 siblings, 1 reply; 3+ messages in thread
From: Ramin Moussavi @ 2026-11-20 14:01 UTC (permalink / raw)
To: Russell King, Puranjay Mohan
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf,
linux-arm-kernel, linux-kernel
ARMv5 and ARMv6 have no MOVW/MOVT, so emit_mov_i_no8m() put every
constant that is not an imm8m into a literal pool behind the epilogue and
loaded it with LDR rX, [pc, #off]. That offset field is 12 bits and the
pool sits at the end of the image, so from roughly 4 KB of JITed code on,
the load no longer reaches it.
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".
Build the constant in the register instead: a single MVN when the
inverted value is an imm8m, otherwise one MOV/ORR per non-zero byte,
since a byte at a byte-aligned position is always an imm8m. The arm64
JIT does the same in emit_a64_mov_i64() with MOVZ/MOVN and MOVK. Without
a pool there is no offset to overflow, so programs of any size are
translated, and FLAG_IMM_OVERFLOW with ctx->flags, which held nothing
else, ctx->imms, ctx->imm_count and ctx->epilogue_bytes go away with it.
It costs up to four instructions per hard constant instead of one. The
pool was not much cheaper: imm_offset() counts duplicates on the sizing
pass, so every use already took 4 bytes of LDR plus 4 bytes of slot.
The 4 KB ceiling is low enough to be hit by the kernel itself. An ARMv5
kernel fails to boot when CONFIG_BPF_JIT_ALWAYS_ON=y and
CONFIG_NET_PTP_CLASSIFY=y are set together: the first leaves no
interpreter to fall back to, the second makes sock_init() build the PTP
classifier, which needs 5124 bytes of ARM code. bpf_prog_create() then
returns -ENOTSUPP and ptp_classifier_init() turns that into a BUG_ON.
Neither option is unusual - NET_PTP_CLASSIFY is selected by
PTP_1588_CLOCK, which defaults to ETHERNET, which defaults to y:
kernel BUG at net/core/ptp_classifier.c:223!
PC is at ptp_classifier_init+0x3c/0x70
ptp_classifier_init from sock_init+0xac/0xf0
seccomp reaches the JIT the same way, so filters of that size are likely
rejected there as well; that was not measured.
ARMv7 and later are unaffected, the MOVW/MOVT path is unchanged.
Tested on versatile_defconfig (ARM926EJ-S) under qemu with
CONFIG_BPF_JIT_ALWAYS_ON=y: the kernel boots again and SO_ATTACH_FILTER
accepts classic filters up to BPF_MAXINSNS, all of which the JIT now
translates. That is the classic limit of 4096 instructions; eBPF
programs loaded through bpf() can be far larger and were not part of the
test. The emitted sequences were verified separately by executing them
for 6.2 million values.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Ramin Moussavi <ramin.moussavi@yacoub.de>
---
v2:
- rebased onto bpf-next, which now has commit 0af298e62e5c ("treewide:
refresh kmalloc_obj() conversions") touching the ctx.imms allocation
this patch removes
- drop the then unused ctx->flags as well (Sashiko review)
- multi-line comment in emit_mov_i_no8m() in the usual style (Sashiko)
v1: https://lore.kernel.org/all/20261111121621.665533-1-ramin.moussavi@yacoub.de/
arch/arm/net/bpf_jit_32.c | 116 +++++++++-----------------------------
arch/arm/net/bpf_jit_32.h | 3 +
2 files changed, 30 insertions(+), 89 deletions(-)
diff --git a/arch/arm/net/bpf_jit_32.c b/arch/arm/net/bpf_jit_32.c
index aa240e3de6e7..58d066adea32 100644
--- a/arch/arm/net/bpf_jit_32.c
+++ b/arch/arm/net/bpf_jit_32.c
@@ -130,8 +130,6 @@ enum {
#define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */
#define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */
-#define FLAG_IMM_OVERFLOW (1 << 0)
-
/*
* Map eBPF registers to ARM 32bit registers or stack scratch space.
*
@@ -193,10 +191,6 @@ static const s8 bpf2a32[][2] = {
* offsets : array of eBPF instruction offsets in
* JITed code.
* target : final JITed code.
- * epilogue_bytes : no of bytes used in epilogue.
- * imm_count : no of immediate counts used for global
- * variables.
- * imms : array of global variable addresses.
*/
struct jit_ctx {
@@ -205,15 +199,9 @@ struct jit_ctx {
unsigned int prologue_bytes;
unsigned int epilogue_offset;
unsigned int cpu_architecture;
- u32 flags;
u32 *offsets;
u32 *target;
u32 stack_size;
-#if __LINUX_ARM_ARCH__ < 7
- u16 epilogue_bytes;
- u16 imm_count;
- u32 *imms;
-#endif
};
/*
@@ -404,53 +392,6 @@ static void jit_fill_hole(void *area, unsigned int size)
#define _STACK_SIZE (ctx->prog->aux->stack_depth + SCRATCH_SIZE)
#define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
-#if __LINUX_ARM_ARCH__ < 7
-
-static u16 imm_offset(u32 k, struct jit_ctx *ctx)
-{
- unsigned int i = 0, offset;
- u16 imm;
-
- /* on the "fake" run we just count them (duplicates included) */
- if (ctx->target == NULL) {
- ctx->imm_count++;
- return 0;
- }
-
- while ((i < ctx->imm_count) && ctx->imms[i]) {
- if (ctx->imms[i] == k)
- break;
- i++;
- }
-
- if (ctx->imms[i] == 0)
- ctx->imms[i] = k;
-
- /* constants go just after the epilogue */
- offset = ctx->offsets[ctx->prog->len - 1] * 4;
- offset += ctx->prologue_bytes;
- offset += ctx->epilogue_bytes;
- offset += i * 4;
-
- ctx->target[offset / 4] = k;
-
- /* PC in ARM mode == address of the instruction + 8 */
- imm = offset - (8 + ctx->idx * 4);
-
- if (imm & ~0xfff) {
- /*
- * literal pool is too far, signal it into flags. we
- * can only detect it on the second pass unfortunately.
- */
- ctx->flags |= FLAG_IMM_OVERFLOW;
- return 0;
- }
-
- return imm;
-}
-
-#endif /* __LINUX_ARM_ARCH__ */
-
static inline int bpf2a32_offset(int bpf_to, int bpf_from,
const struct jit_ctx *ctx) {
int to, from;
@@ -469,7 +410,31 @@ static inline int bpf2a32_offset(int bpf_to, int bpf_from,
static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
{
#if __LINUX_ARM_ARCH__ < 7
- emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
+ /*
+ * A single MVN if the inverted value is an imm8m, else one
+ * MOV/ORR per non-zero byte -- a byte is always an imm8m.
+ */
+ int imm12 = imm8m(~val);
+ unsigned int shift;
+ bool first = true;
+
+ if (imm12 >= 0) {
+ emit(ARM_MVN_I(rd, imm12), ctx);
+ return;
+ }
+ 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);
#else
emit(ARM_MOVW(rd, val & 0xffff), ctx);
if (val > 0xffff)
@@ -2091,14 +2056,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
pr_err_once("unknown opcode %02x\n", code);
return -EINVAL;
}
-
- if (ctx->flags & FLAG_IMM_OVERFLOW)
- /*
- * this instruction generated an overflow when
- * trying to access the literal pool, so
- * delegate this filter to the kernel interpreter.
- */
- return -1;
return 0;
}
@@ -2192,21 +2149,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
ctx.epilogue_offset = ctx.idx;
-#if __LINUX_ARM_ARCH__ < 7
- tmp_idx = ctx.idx;
- build_epilogue(&ctx);
- ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
-
- ctx.idx += ctx.imm_count;
- if (ctx.imm_count) {
- ctx.imms = kzalloc_objs(u32, ctx.imm_count);
- if (ctx.imms == NULL)
- goto out_off;
- }
-#else
- /* there's nothing about the epilogue on ARMv7 */
build_epilogue(&ctx);
-#endif
/* Now we can get the actual image size of the JITed arm code.
* Currently, we are not considering the THUMB-2 instructions
* for jit, although it can decrease the size of the image.
@@ -2224,7 +2167,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
* we must fall back to the interpretation
*/
if (header == NULL)
- goto out_imms;
+ goto out_off;
/* 2.) Actual pass to generate final JIT code */
ctx.target = (u32 *) image_ptr;
@@ -2254,11 +2197,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
prog->jited = 1;
prog->jited_len = image_size;
-out_imms:
-#if __LINUX_ARM_ARCH__ < 7
- if (ctx.imm_count)
- kfree(ctx.imms);
-#endif
out_off:
kfree(ctx.offsets);
@@ -2267,6 +2205,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
out_free:
image_ptr = NULL;
bpf_jit_binary_free(header);
- goto out_imms;
+ goto out_off;
}
diff --git a/arch/arm/net/bpf_jit_32.h b/arch/arm/net/bpf_jit_32.h
index 438f0e1f91a0..76d83b486ded 100644
--- a/arch/arm/net/bpf_jit_32.h
+++ b/arch/arm/net/bpf_jit_32.h
@@ -104,6 +104,7 @@
#define ARM_INST_MOV_I 0x03a00000
#define ARM_INST_MOVW 0x03000000
#define ARM_INST_MOVT 0x03400000
+#define ARM_INST_MVN_I 0x03e00000
#define ARM_INST_MUL 0x00000090
@@ -231,6 +232,8 @@
#define ARM_MOVT(rd, imm) \
(ARM_INST_MOVT | ((imm) >> 12) << 16 | (rd) << 12 | ((imm) & 0x0fff))
+#define ARM_MVN_I(rd, imm) _AL3_I(ARM_INST_MVN, rd, 0, imm)
+
#define ARM_MUL(rd, rm, rn) (ARM_INST_MUL | (rd) << 16 | (rm) << 8 | (rn))
#define ARM_POP(regs) (ARM_INST_POP | (regs))
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next v2] bpf, arm32: build constants inline instead of using a literal pool
2026-11-20 14:01 ` [PATCH bpf-next v2] " Ramin Moussavi
@ 2026-11-20 19:12 ` Alexei Starovoitov
0 siblings, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-20 19:12 UTC (permalink / raw)
To: Ramin Moussavi, Russell King, Puranjay Mohan
Cc: Daniel Borkmann, Andrii Nakryiko, bpf, linux-arm-kernel,
linux-kernel
On Fri, Nov 20, 2026 at 06:01 AM Ramin Moussavi <ramin.moussavi@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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-20 19:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11 12:16 [PATCH bpf-next] bpf, arm32: build constants inline instead of using a literal pool Ramin Moussavi
2026-11-20 14:01 ` [PATCH bpf-next v2] " Ramin Moussavi
2026-11-20 19:12 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox