Skip to content

Commit e612b5c

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
bpf, arm64: Add support for lse atomics in bpf_arena
When LSE atomics are available, BPF atomic instructions are implemented as single ARM64 atomic instructions, therefore it is easy to enable these in bpf_arena using the currently available exception handling setup. LL_SC atomics use loops and therefore would need more work to enable in bpf_arena. Enable LSE atomics based instructions in bpf_arena and use the bpf_jit_supports_insn() callback to reject atomics in bpf_arena if LSE atomics are not available. All atomics and arena_atomics selftests are passing: [root@ip-172-31-2-216 bpf]# ./test_progs -a atomics,arena_atomics #3/1 arena_atomics/add:OK #3/2 arena_atomics/sub:OK #3/3 arena_atomics/and:OK #3/4 arena_atomics/or:OK #3/5 arena_atomics/xor:OK #3/6 arena_atomics/cmpxchg:OK #3/7 arena_atomics/xchg:OK #3 arena_atomics:OK #10/1 atomics/add:OK #10/2 atomics/sub:OK #10/3 atomics/and:OK #10/4 atomics/or:OK #10/5 atomics/xor:OK #10/6 atomics/cmpxchg:OK #10/7 atomics/xchg:OK #10 atomics:OK Summary: 2/14 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Puranjay Mohan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 7e2c7a3 commit e612b5c

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,26 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
494494
static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
495495
{
496496
const u8 code = insn->code;
497+
const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
497498
const u8 dst = bpf2a64[insn->dst_reg];
498499
const u8 src = bpf2a64[insn->src_reg];
499500
const u8 tmp = bpf2a64[TMP_REG_1];
500501
const u8 tmp2 = bpf2a64[TMP_REG_2];
501502
const bool isdw = BPF_SIZE(code) == BPF_DW;
503+
const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC;
502504
const s16 off = insn->off;
503-
u8 reg;
505+
u8 reg = dst;
504506

505-
if (!off) {
506-
reg = dst;
507-
} else {
508-
emit_a64_mov_i(1, tmp, off, ctx);
509-
emit(A64_ADD(1, tmp, tmp, dst), ctx);
510-
reg = tmp;
507+
if (off || arena) {
508+
if (off) {
509+
emit_a64_mov_i(1, tmp, off, ctx);
510+
emit(A64_ADD(1, tmp, tmp, dst), ctx);
511+
reg = tmp;
512+
}
513+
if (arena) {
514+
emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx);
515+
reg = tmp;
516+
}
511517
}
512518

513519
switch (insn->imm) {
@@ -576,6 +582,12 @@ static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
576582
u8 reg;
577583
s32 jmp_offset;
578584

585+
if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
586+
/* ll_sc based atomics don't support unsafe pointers yet. */
587+
pr_err_once("unknown atomic opcode %02x\n", code);
588+
return -EINVAL;
589+
}
590+
579591
if (!off) {
580592
reg = dst;
581593
} else {
@@ -777,7 +789,8 @@ static int add_exception_handler(const struct bpf_insn *insn,
777789

778790
if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
779791
BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
780-
BPF_MODE(insn->code) != BPF_PROBE_MEM32)
792+
BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
793+
BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
781794
return 0;
782795

783796
if (!ctx->prog->aux->extable ||
@@ -1474,12 +1487,18 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
14741487

14751488
case BPF_STX | BPF_ATOMIC | BPF_W:
14761489
case BPF_STX | BPF_ATOMIC | BPF_DW:
1490+
case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
1491+
case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
14771492
if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
14781493
ret = emit_lse_atomic(insn, ctx);
14791494
else
14801495
ret = emit_ll_sc_atomic(insn, ctx);
14811496
if (ret)
14821497
return ret;
1498+
1499+
ret = add_exception_handler(insn, ctx, dst);
1500+
if (ret)
1501+
return ret;
14831502
break;
14841503

14851504
default:
@@ -2527,6 +2546,19 @@ bool bpf_jit_supports_arena(void)
25272546
return true;
25282547
}
25292548

2549+
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
2550+
{
2551+
if (!in_arena)
2552+
return true;
2553+
switch (insn->code) {
2554+
case BPF_STX | BPF_ATOMIC | BPF_W:
2555+
case BPF_STX | BPF_ATOMIC | BPF_DW:
2556+
if (!cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
2557+
return false;
2558+
}
2559+
return true;
2560+
}
2561+
25302562
void bpf_jit_free(struct bpf_prog *prog)
25312563
{
25322564
if (prog->jited) {

tools/testing/selftests/bpf/DENYLIST.aarch64

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ fill_link_info/kprobe_multi_link_info # bpf_program__attach_kprobe_mu
1010
fill_link_info/kretprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95
1111
fill_link_info/kprobe_multi_invalid_ubuff # bpf_program__attach_kprobe_multi_opts unexpected error: -95
1212
missed/kprobe_recursion # missed_kprobe_recursion__attach unexpected error: -95 (errno 95)
13-
arena_atomics

0 commit comments

Comments
 (0)