From: Alexei Starovoitov A Rust function that gets &T does not know where T is. T can be on the heap, a static, or a local variable of the caller. The same insn accesses all three. The heap and the statics of a Rust prog are in arena. Thus a local variable whose address is taken must be in arena too, and the prog needs a second stack there. Introduce BPF_REG_ARENA_SP, r12, the stack pointer of the stack in arena. BPF_REG_AX stays the last register and becomes r13: register before after ---------------- ------ ----- BPF_REG_PARAMS r11 r11 BPF_REG_ARENA_SP r12 BPF_REG_AX r12 r13 This patch only reserves the number. The verifier continues to reject insns with r12. BPF_REG_AX is visible in xlated insns. Adjust the selftests that match it there. Signed-off-by: Alexei Starovoitov --- include/linux/filter.h | 5 +- kernel/bpf/core.c | 3 +- .../selftests/bpf/prog_tests/ctx_rewrite.c | 14 ++-- .../bpf/progs/verifier_bpf_fastcall.c | 24 +++---- .../selftests/bpf/progs/verifier_may_goto_1.c | 12 ++-- .../selftests/bpf/progs/verifier_sdiv.c | 64 +++++++++---------- 6 files changed, 62 insertions(+), 60 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index 98254742ae37..f57916e95b94 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -60,8 +60,9 @@ struct ctl_table_header; /* Kernel hidden auxiliary/helper register. */ #define BPF_REG_PARAMS MAX_BPF_REG -#define BPF_REG_AX (MAX_BPF_REG + 1) -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2) +#define BPF_REG_ARENA_SP (MAX_BPF_REG + 1) +#define BPF_REG_AX (MAX_BPF_REG + 2) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 3) #define MAX_BPF_JIT_REG MAX_BPF_EXT_REG /* unused opcode to mark special call to bpf_tail_call() helper */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 472efced9e40..94ee44ad88f7 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1368,7 +1368,8 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from, u32 imm_rnd = get_random_u32(); int off; - BUILD_BUG_ON(BPF_REG_PARAMS + 2 != MAX_BPF_JIT_REG); + BUILD_BUG_ON(BPF_REG_PARAMS + 3 != MAX_BPF_JIT_REG); + BUILD_BUG_ON(BPF_REG_ARENA_SP + 2 != MAX_BPF_JIT_REG); BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); /* Constraints on AX register: diff --git a/tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c b/tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c index 2c3124092b73..2d0bb771626d 100644 --- a/tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c +++ b/tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c @@ -69,19 +69,19 @@ static struct test_case test_cases[] = { #if defined(__x86_64__) || defined(__aarch64__) { N(SCHED_CLS, struct __sk_buff, tstamp), - .read = "r12 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" - "if w12 & 0x4 goto pc+1;" + .read = "r13 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" + "if w13 & 0x4 goto pc+1;" "goto pc+4;" - "if w12 & 0x3 goto pc+1;" + "if w13 & 0x3 goto pc+1;" "goto pc+2;" "$dst = 0;" "goto pc+1;" "$dst = *(u64 *)($ctx + sk_buff::tstamp);", - .write = "r12 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" - "if w12 & 0x4 goto pc+1;" + .write = "r13 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" + "if w13 & 0x4 goto pc+1;" "goto pc+2;" - "w12 &= -4;" - "*(u8 *)($ctx + sk_buff::__mono_tc_offset) = r12;" + "w13 &= -4;" + "*(u8 *)($ctx + sk_buff::__mono_tc_offset) = r13;" "*(u64 *)($ctx + sk_buff::tstamp) = $src;", }, #endif diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c index e0c389102db9..5cd503e87df9 100644 --- a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +++ b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c @@ -913,13 +913,13 @@ __xlated("...") __xlated("4: r0 = &(void __percpu *)(r0)") __xlated("...") /* may_goto expansion starts */ -__xlated("6: r12 = *(u64 *)(r10 -24)") -__xlated("7: if r12 == 0x0 goto pc+6") -__xlated("8: r12 -= 1") -__xlated("9: if r12 != 0x0 goto pc+2") -__xlated("10: r12 = -24") +__xlated("6: r13 = *(u64 *)(r10 -24)") +__xlated("7: if r13 == 0x0 goto pc+6") +__xlated("8: r13 -= 1") +__xlated("9: if r13 != 0x0 goto pc+2") +__xlated("10: r13 = -24") __xlated("11: call unknown") -__xlated("12: *(u64 *)(r10 -24) = r12") +__xlated("12: *(u64 *)(r10 -24) = r13") /* may_goto expansion ends */ __xlated("13: *(u64 *)(r10 -8) = r1") __xlated("14: exit") @@ -954,13 +954,13 @@ __xlated("1: *(u64 *)(r10 -16) =") __xlated("2: r1 = 1") __xlated("3: call bpf_get_smp_processor_id") /* may_goto expansion starts */ -__xlated("4: r12 = *(u64 *)(r10 -24)") -__xlated("5: if r12 == 0x0 goto pc+6") -__xlated("6: r12 -= 1") -__xlated("7: if r12 != 0x0 goto pc+2") -__xlated("8: r12 = -24") +__xlated("4: r13 = *(u64 *)(r10 -24)") +__xlated("5: if r13 == 0x0 goto pc+6") +__xlated("6: r13 -= 1") +__xlated("7: if r13 != 0x0 goto pc+2") +__xlated("8: r13 = -24") __xlated("9: call unknown") -__xlated("10: *(u64 *)(r10 -24) = r12") +__xlated("10: *(u64 *)(r10 -24) = r13") /* may_goto expansion ends */ __xlated("11: *(u64 *)(r10 -8) = r1") __xlated("12: exit") diff --git a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c index 6fe602c8c7f3..49753e89cce9 100644 --- a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +++ b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c @@ -89,13 +89,13 @@ __arch_riscv64 __arch_loongarch __xlated("0: *(u64 *)(r10 -16) = 65535") __xlated("1: *(u64 *)(r10 -8) = 0") -__xlated("2: r12 = *(u64 *)(r10 -16)") -__xlated("3: if r12 == 0x0 goto pc+6") -__xlated("4: r12 -= 1") -__xlated("5: if r12 != 0x0 goto pc+2") -__xlated("6: r12 = -16") +__xlated("2: r13 = *(u64 *)(r10 -16)") +__xlated("3: if r13 == 0x0 goto pc+6") +__xlated("4: r13 -= 1") +__xlated("5: if r13 != 0x0 goto pc+2") +__xlated("6: r13 = -16") __xlated("7: call unknown") -__xlated("8: *(u64 *)(r10 -16) = r12") +__xlated("8: *(u64 *)(r10 -16) = r13") __xlated("9: r0 = 1") __xlated("10: r0 = 2") __xlated("11: exit") diff --git a/tools/testing/selftests/bpf/progs/verifier_sdiv.c b/tools/testing/selftests/bpf/progs/verifier_sdiv.c index d6cbe1e27fd6..0efbfa726764 100644 --- a/tools/testing/selftests/bpf/progs/verifier_sdiv.c +++ b/tools/testing/selftests/bpf/progs/verifier_sdiv.c @@ -778,10 +778,10 @@ __arch_x86_64 __xlated("0: r2 = 0x8000000000000000") __xlated("2: r3 = -1") __xlated("3: r4 = r2") -__xlated("4: r12 = r3") -__xlated("5: r12 += 1") -__xlated("6: if r12 > 0x1 goto pc+4") -__xlated("7: if r12 == 0x0 goto pc+1") +__xlated("4: r13 = r3") +__xlated("5: r13 += 1") +__xlated("6: if r13 > 0x1 goto pc+4") +__xlated("7: if r13 == 0x0 goto pc+1") __xlated("8: r2 = 0") __xlated("9: r2 = -r2") __xlated("10: goto pc+1") @@ -812,10 +812,10 @@ __success __retval(-5) __arch_x86_64 __xlated("0: r2 = 5") __xlated("1: r3 = -1") -__xlated("2: r12 = r3") -__xlated("3: r12 += 1") -__xlated("4: if r12 > 0x1 goto pc+4") -__xlated("5: if r12 == 0x0 goto pc+1") +__xlated("2: r13 = r3") +__xlated("3: r13 += 1") +__xlated("4: if r13 > 0x1 goto pc+4") +__xlated("5: if r13 == 0x0 goto pc+1") __xlated("6: r2 = 0") __xlated("7: r2 = -r2") __xlated("8: goto pc+1") @@ -890,10 +890,10 @@ __arch_x86_64 __xlated("0: w2 = -2147483648") __xlated("1: w3 = -1") __xlated("2: w4 = w2") -__xlated("3: r12 = r3") -__xlated("4: w12 += 1") -__xlated("5: if w12 > 0x1 goto pc+4") -__xlated("6: if w12 == 0x0 goto pc+1") +__xlated("3: r13 = r3") +__xlated("4: w13 += 1") +__xlated("5: if w13 > 0x1 goto pc+4") +__xlated("6: if w13 == 0x0 goto pc+1") __xlated("7: w2 = 0") __xlated("8: w2 = -w2") __xlated("9: goto pc+1") @@ -925,10 +925,10 @@ __arch_x86_64 __xlated("0: w2 = -5") __xlated("1: w3 = -1") __xlated("2: w4 = w2") -__xlated("3: r12 = r3") -__xlated("4: w12 += 1") -__xlated("5: if w12 > 0x1 goto pc+4") -__xlated("6: if w12 == 0x0 goto pc+1") +__xlated("3: r13 = r3") +__xlated("4: w13 += 1") +__xlated("5: if w13 > 0x1 goto pc+4") +__xlated("6: if w13 == 0x0 goto pc+1") __xlated("7: w2 = 0") __xlated("8: w2 = -w2") __xlated("9: goto pc+1") @@ -1004,10 +1004,10 @@ __arch_x86_64 __xlated("0: r2 = 0x8000000000000000") __xlated("2: r3 = -1") __xlated("3: r4 = r2") -__xlated("4: r12 = r3") -__xlated("5: r12 += 1") -__xlated("6: if r12 > 0x1 goto pc+3") -__xlated("7: if r12 == 0x1 goto pc+3") +__xlated("4: r13 = r3") +__xlated("5: r13 += 1") +__xlated("6: if r13 > 0x1 goto pc+3") +__xlated("7: if r13 == 0x1 goto pc+3") __xlated("8: w2 = 0") __xlated("9: goto pc+1") __xlated("10: r2 s%= r3") @@ -1034,10 +1034,10 @@ __arch_x86_64 __xlated("0: r2 = 5") __xlated("1: r3 = -1") __xlated("2: r4 = r2") -__xlated("3: r12 = r3") -__xlated("4: r12 += 1") -__xlated("5: if r12 > 0x1 goto pc+3") -__xlated("6: if r12 == 0x1 goto pc+3") +__xlated("3: r13 = r3") +__xlated("4: r13 += 1") +__xlated("5: if r13 > 0x1 goto pc+3") +__xlated("6: if r13 == 0x1 goto pc+3") __xlated("7: w2 = 0") __xlated("8: goto pc+1") __xlated("9: r2 s%= r3") @@ -1108,10 +1108,10 @@ __arch_x86_64 __xlated("0: w2 = -2147483648") __xlated("1: w3 = -1") __xlated("2: w4 = w2") -__xlated("3: r12 = r3") -__xlated("4: w12 += 1") -__xlated("5: if w12 > 0x1 goto pc+3") -__xlated("6: if w12 == 0x1 goto pc+4") +__xlated("3: r13 = r3") +__xlated("4: w13 += 1") +__xlated("5: if w13 > 0x1 goto pc+3") +__xlated("6: if w13 == 0x1 goto pc+4") __xlated("7: w2 = 0") __xlated("8: goto pc+1") __xlated("9: w2 s%= w3") @@ -1140,10 +1140,10 @@ __arch_x86_64 __xlated("0: w2 = -5") __xlated("1: w3 = -1") __xlated("2: w4 = w2") -__xlated("3: r12 = r3") -__xlated("4: w12 += 1") -__xlated("5: if w12 > 0x1 goto pc+3") -__xlated("6: if w12 == 0x1 goto pc+4") +__xlated("3: r13 = r3") +__xlated("4: w13 += 1") +__xlated("5: if w13 > 0x1 goto pc+3") +__xlated("6: if w13 == 0x1 goto pc+4") __xlated("7: w2 = 0") __xlated("8: goto pc+1") __xlated("9: w2 s%= w3") -- 2.55.0