bpf: Support BPF_REG_ARENA_SP in the verifier
In Rust &local, &heap and &static are the same type of pointer, and the
same insn dereferences all of them. 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. Spills and all other local variables stay in the r10 stack.
Add a second stack, in arena. r12 (BPF_REG_ARENA_SP) is its stack pointer.
The stack grows up, and r12 points to the end of the frame of the current
function:
f:
r12 += 32 ; the frame of f, 32 bytes in arena
*(u64 *)(r12 - 8) = r1 ; a local variable
r1 = r12
r1 += -8 ; its address, a number (scalar)
call g ; g accesses the variable through r1
exit ; the verifier adds 'r12 += -32' here
The verifier tracks neither r12 nor the content of this stack. It does not
track the rest of arena either. r12 holds the low 32 bits of the address,
and the JIT adds the base of the arena. Thus an access through r12 stays in
arena. But the code at the exit of the prog needs r12 to find the stack
that it must give back. Thus accept r12 in 4 insns only:
r12 += N first insn of a function, N % 16 == 0
rX = r12 number (scalar): user space address
rX = *(size *)(r12 - off) off is not checked against N
*(size *)(r12 - off) = rX or imm
Only the compiler knows N, the size of the frame. The verifier cannot
calculate it, because callees access the frame through numbers. The frames
of a call chain have 32 Kbyte at most.
The reasons for these rules:
in this patch what was rejected, and why
----------------------------- ----------------------------------------
One 'r12 += N', the first 'r12 -= rX': the depth has no bound.
insn of a function. More than one 'r12 += imm': r12 becomes
verifier state.
The verifier tracks nothing Tracked slots: each store through a
in this stack. number invalidates them.
'rX = r12' gives a number. PTR_TO_ARENA: after a store to memory
and a load it is a number. Then one insn
sees two types.
The stack grows up, and the Down, with r12 + off: harder to read.
offsets are negative, as with
r10.
Each run takes a stack, see "bpf: Add stacks for programs to arena". The
verifier adds this code to the main prog. busy, size and base are per-cpu:
entry before each exit
old = busy++ if (r12 & BPF_ARENA_STACK_SLOW)
if (old == 0 && size is sufficient) bpf_arena_stack_put()
r12 = base else
else busy = 0
r12 = bpf_arena_stack_get()
no memory: return 0
The verifier adds this code before gen_prologue() and gen_epilogue() add
theirs. Thus the prologue of the prog type runs before the prog takes
a stack, and the epilogue runs after each exit, also after 'return 0'.
The usual path has 8 insns at the entry and 4 insns at the exit. The kernel
does not pass r12 to a callback. Thus a callback that uses r12 gets the
same code and takes a stack too. To reject r12 in callbacks is not
possible: real schedulers need it.
Progs without r12 get the same code. A prog that uses r12 in one of its
functions pays in each run: up to 22 more x86 insns, no call, no lock
prefix. The cost on x86-64 (Zen 5), in nsec more than the same prog with
r10:
each run, hot caches 0.28 to 2.30
each run, cold caches and TLB 45 to 70
each run that finds the per-cpu stack taken 8.7
iteration of bpf_loop(), the callback uses r12 17.5 (1.13 -> 18.6)
call of a function with a frame, load, store below 0.23
A callback of bpf_loop() always finds the per-cpu stack taken, because the
main prog has it. The verifier also does not inline such a loop.
Limitations:
- The JIT must report bpf_jit_supports_arena_stack(). No JIT does in this
patch.
- The verifier rejects tail calls, because the prog would not give the
stack back.
- It rejects extension progs. The code at the entry uses r2-r5, and an
extension prog has arguments there.
- It rejects BPF_LD_ABS and BPF_LD_IND. They end the function when the
offset is not in the packet, and the prog would not give the stack back.
- It rejects a jump and a gotox to 'r12 += N', because the insn would run
again.
- It rejects a struct_ops prog that has a __ref argument. A run without
a stack is skipped, and nothing would release the argument.
- It rejects a call of the main prog, and a call and a callx of a callback
that takes a stack, because the caller would get the r12 of the callee.
- Nothing can attach to a subprog, because the x86 trampoline does not
keep r12.
- The private stack is off, because the x86 JIT has one register for the
two.
- bpf_throw() does not give the stack back. The stack stays taken until
the arena is freed, for all progs of the arena on that cpu. After 17
throws on a cpu each run there allocates a stack, and each more throw
loses the pages of one stack. Rust progs unwind and do not throw.
may_goto sets is_called in struct bpf_subprog_info for a function that BPF
code calls, and for a callback of a helper other than bpf_loop() and
bpf_for_each_map_elem(). Such a callback can take a stack. A function that
BPF code calls cannot. Give these callbacks a bit of their own,
is_other_cb. may_goto examines the two bits.
The test "invalid src register in LDX" of test_verifier used r12. Use r13.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://patch.msgid.link/20261104012401.1354861-12-alexei.starovoitov@gmail.com
11 files changed