Skip to content

Commit 8c12ccf

Browse files
kkdwvdKernel Patches Daemon
authored andcommitted
bpf, x86: Add support for signed arena loads
Currently, signed load instructions into arena memory are unsupported. The compiler is free to generate these, and on GCC-14 we see a corresponding error when it happens. The hurdle in supporting them is deciding which unused opcode to use to mark them for the JIT's own consumption. After much thinking, it appears 0xc0 / BPF_NOSPEC can be combined with load instructions to identify signed arena loads. Use this to recognize and JIT them appropriately, and remove the verifier side limitation on the program if the JIT supports them. Co-developed-by: Puranjay Mohan <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Puranjay Mohan <[email protected]>
1 parent 2b3c471 commit 8c12ccf

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,6 +3064,11 @@ bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
30643064
if (!bpf_atomic_is_load_store(insn) &&
30653065
!cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
30663066
return false;
3067+
break;
3068+
case BPF_LDX | BPF_MEMSX | BPF_B:
3069+
case BPF_LDX | BPF_MEMSX | BPF_H:
3070+
case BPF_LDX | BPF_MEMSX | BPF_W:
3071+
return false;
30673072
}
30683073
return true;
30693074
}

arch/s390/net/bpf_jit_comp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,11 @@ bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
29672967
case BPF_STX | BPF_ATOMIC | BPF_DW:
29682968
if (bpf_atomic_is_load_store(insn))
29692969
return false;
2970+
break;
2971+
case BPF_LDX | BPF_MEMSX | BPF_B:
2972+
case BPF_LDX | BPF_MEMSX | BPF_H:
2973+
case BPF_LDX | BPF_MEMSX | BPF_W:
2974+
return false;
29702975
}
29712976
return true;
29722977
}

arch/x86/net/bpf_jit_comp.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,11 +1152,38 @@ static void emit_ldx_index(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, u32 i
11521152
*pprog = prog;
11531153
}
11541154

1155+
static void emit_ldsx_index(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, u32 index_reg, int off)
1156+
{
1157+
u8 *prog = *pprog;
1158+
1159+
switch (size) {
1160+
case BPF_B:
1161+
/* movsx rax, byte ptr [rax + r12 + off] */
1162+
EMIT3(add_3mod(0x48, src_reg, dst_reg, index_reg), 0x0F, 0xBE);
1163+
break;
1164+
case BPF_H:
1165+
/* movsx rax, word ptr [rax + r12 + off] */
1166+
EMIT3(add_3mod(0x48, src_reg, dst_reg, index_reg), 0x0F, 0xBF);
1167+
break;
1168+
case BPF_W:
1169+
/* movsx rax, dword ptr [rax + r12 + off] */
1170+
EMIT2(add_3mod(0x48, src_reg, dst_reg, index_reg), 0x63);
1171+
break;
1172+
}
1173+
emit_insn_suffix_SIB(&prog, src_reg, dst_reg, index_reg, off);
1174+
*pprog = prog;
1175+
}
1176+
11551177
static void emit_ldx_r12(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
11561178
{
11571179
emit_ldx_index(pprog, size, dst_reg, src_reg, X86_REG_R12, off);
11581180
}
11591181

1182+
static void emit_ldsx_r12(u8 **prog, u32 size, u32 dst_reg, u32 src_reg, int off)
1183+
{
1184+
emit_ldsx_index(prog, size, dst_reg, src_reg, X86_REG_R12, off);
1185+
}
1186+
11601187
/* STX: *(u8*)(dst_reg + off) = src_reg */
11611188
static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
11621189
{
@@ -2109,13 +2136,19 @@ st: if (is_imm8(insn->off))
21092136
case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
21102137
case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
21112138
case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
2139+
case BPF_LDX | BPF_PROBE_MEM32SX | BPF_B:
2140+
case BPF_LDX | BPF_PROBE_MEM32SX | BPF_H:
2141+
case BPF_LDX | BPF_PROBE_MEM32SX | BPF_W:
21122142
case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
21132143
case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
21142144
case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
21152145
case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
21162146
start_of_ldx = prog;
21172147
if (BPF_CLASS(insn->code) == BPF_LDX)
2118-
emit_ldx_r12(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
2148+
if (BPF_MODE(insn->code) == BPF_PROBE_MEM32SX)
2149+
emit_ldsx_r12(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
2150+
else
2151+
emit_ldx_r12(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
21192152
else
21202153
emit_stx_r12(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
21212154
populate_extable:

include/linux/filter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ struct ctl_table_header;
7878
/* unused opcode to mark special atomic instruction */
7979
#define BPF_PROBE_ATOMIC 0xe0
8080

81+
/* unused opcode to mark special ldsx instruction. Same as BPF_NOSPEC */
82+
#define BPF_PROBE_MEM32SX 0xc0
83+
8184
/* unused opcode to mark call to interpreter with arguments */
8285
#define BPF_CALL_ARGS 0xe0
8386

kernel/bpf/verifier.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21422,10 +21422,14 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
2142221422
continue;
2142321423
case PTR_TO_ARENA:
2142421424
if (BPF_MODE(insn->code) == BPF_MEMSX) {
21425-
verbose(env, "sign extending loads from arena are not supported yet\n");
21426-
return -EOPNOTSUPP;
21425+
if (!bpf_jit_supports_insn(insn, true)) {
21426+
verbose(env, "sign extending loads from arena are not supported yet\n");
21427+
return -EOPNOTSUPP;
21428+
}
21429+
insn->code = BPF_CLASS(insn->code) | BPF_PROBE_MEM32SX | BPF_SIZE(insn->code);
21430+
} else {
21431+
insn->code = BPF_CLASS(insn->code) | BPF_PROBE_MEM32 | BPF_SIZE(insn->code);
2142721432
}
21428-
insn->code = BPF_CLASS(insn->code) | BPF_PROBE_MEM32 | BPF_SIZE(insn->code);
2142921433
env->prog->aux->num_exentries++;
2143021434
continue;
2143121435
default:
@@ -21631,6 +21635,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
2163121635
if (BPF_CLASS(insn->code) == BPF_LDX &&
2163221636
(BPF_MODE(insn->code) == BPF_PROBE_MEM ||
2163321637
BPF_MODE(insn->code) == BPF_PROBE_MEM32 ||
21638+
BPF_MODE(insn->code) == BPF_PROBE_MEM32SX ||
2163421639
BPF_MODE(insn->code) == BPF_PROBE_MEMSX))
2163521640
num_exentries++;
2163621641
if ((BPF_CLASS(insn->code) == BPF_STX ||

0 commit comments

Comments
 (0)