Skip to content

Commit e723608

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Add verifier support for timed may_goto
Implement support in the verifier for replacing may_goto implementation from a counter-based approach to one which samples time on the local CPU to have a bigger loop bound. We implement it by maintaining 16-bytes per-stack frame, and using 8 bytes for maintaining the count for amortizing time sampling, and 8 bytes for the starting timestamp. To minimize overhead, we need to avoid spilling and filling of registers around this sequence, so we push this cost into the time sampling function 'arch_bpf_timed_may_goto'. This is a JIT-specific wrapper around bpf_check_timed_may_goto which returns us the count to store into the stack through BPF_REG_AX. All caller-saved registers (r0-r5) are guaranteed to remain untouched. The loop can be broken by returning count as 0, otherwise we dispatch into the function when the count drops to 0, and the runtime chooses to refresh it (by returning count as BPF_MAX_TIMED_LOOPS) or returning 0 and aborting the loop on next iteration. Since the check for 0 is done right after loading the count from the stack, all subsequent cond_break sequences should immediately break as well, of the same loop or subsequent loops in the program. We pass in the stack_depth of the count (and thus the timestamp, by adding 8 to it) to the arch_bpf_timed_may_goto call so that it can be passed in to bpf_check_timed_may_goto as an argument after r1 is saved, by adding the offset to r10/fp. This adjustment will be arch specific, and the next patch will introduce support for x86. Note that depending on loop complexity, time spent in the loop can be more than the current limit (250 ms), but imposing an upper bound on program runtime is an orthogonal problem which will be addressed when program cancellations are supported. The current time afforded by cond_break may not be enough for cases where BPF programs want to implement locking algorithms inline, and use cond_break as a promise to the verifier that they will eventually terminate. Below are some benchmarking numbers on the time taken per-iteration for an empty loop that counts the number of iterations until cond_break fires. For comparison, we compare it against bpf_for/bpf_repeat which is another way to achieve the same number of spins (BPF_MAX_LOOPS). The hardware used for benchmarking was a Sapphire Rapids Intel server with performance governor enabled, mitigations were enabled. +-----------------------------+--------------+--------------+------------------+ | Loop type | Iterations | Time (ms) | Time/iter (ns) | +-----------------------------|--------------+--------------+------------------+ | may_goto | 8388608 | 3 | 0.36 | | timed_may_goto (count=65535)| 589674932 | 250 | 0.42 | | bpf_for | 8388608 | 10 | 1.19 | +-----------------------------+--------------+--------------+------------------+ This gives a good approximation at low overhead while staying close to the current implementation. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2941e21 commit e723608

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ struct bpf_array {
19871987
*/
19881988
enum {
19891989
BPF_MAX_LOOPS = 8 * 1024 * 1024,
1990+
BPF_MAX_TIMED_LOOPS = 0xffff,
19901991
};
19911992

19921993
#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \

include/linux/filter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,11 @@ struct bpf_prog_stats {
669669
struct u64_stats_sync syncp;
670670
} __aligned(2 * sizeof(u64));
671671

672+
struct bpf_timed_may_goto {
673+
u64 count;
674+
u64 timestamp;
675+
};
676+
672677
struct sk_filter {
673678
refcount_t refcnt;
674679
struct rcu_head rcu;
@@ -1130,8 +1135,11 @@ bool bpf_jit_supports_ptr_xchg(void);
11301135
bool bpf_jit_supports_arena(void);
11311136
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
11321137
bool bpf_jit_supports_private_stack(void);
1138+
bool bpf_jit_supports_timed_may_goto(void);
11331139
u64 bpf_arch_uaddress_limit(void);
11341140
void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie);
1141+
u64 arch_bpf_timed_may_goto(void);
1142+
u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *);
11351143
bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id);
11361144

11371145
static inline bool bpf_dump_raw_ok(const struct cred *cred)

kernel/bpf/core.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,32 @@ void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp,
30693069
{
30703070
}
30713071

3072+
bool __weak bpf_jit_supports_timed_may_goto(void)
3073+
{
3074+
return false;
3075+
}
3076+
3077+
u64 __weak arch_bpf_timed_may_goto(void)
3078+
{
3079+
return 0;
3080+
}
3081+
3082+
u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)
3083+
{
3084+
u64 time = ktime_get_mono_fast_ns();
3085+
3086+
/* Populate the timestamp for this stack frame, and refresh count. */
3087+
if (!p->timestamp) {
3088+
p->timestamp = time;
3089+
return BPF_MAX_TIMED_LOOPS;
3090+
}
3091+
/* Check if we've exhausted our time slice, and zero count. */
3092+
if (time - p->timestamp >= (NSEC_PER_SEC / 4))
3093+
return 0;
3094+
/* Refresh the count for the stack frame. */
3095+
return BPF_MAX_TIMED_LOOPS;
3096+
}
3097+
30723098
/* for configs without MMU or 32-bit */
30733099
__weak const struct bpf_map_ops arena_map_ops;
30743100
__weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)

kernel/bpf/verifier.c

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21572,7 +21572,50 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
2157221572
goto next_insn;
2157321573
}
2157421574

21575-
if (is_may_goto_insn(insn)) {
21575+
if (is_may_goto_insn(insn) && bpf_jit_supports_timed_may_goto()) {
21576+
int stack_off_cnt = -stack_depth - 16;
21577+
21578+
/*
21579+
* Two 8 byte slots, depth-16 stores the count, and
21580+
* depth-8 stores the start timestamp of the loop.
21581+
*
21582+
* The starting value of count is BPF_MAX_TIMED_LOOPS
21583+
* (0xffff). Every iteration loads it and subs it by 1,
21584+
* until the value becomes 0 in AX (thus, 1 in stack),
21585+
* after which we call arch_bpf_timed_may_goto, which
21586+
* either sets AX to 0xffff to keep looping, or to 0
21587+
* upon timeout. AX is then stored into the stack. In
21588+
* the next iteration, we either see 0 and break out, or
21589+
* continue iterating until the next time value is 0
21590+
* after subtraction, rinse and repeat.
21591+
*/
21592+
stack_depth_extra = 16;
21593+
insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_AX, BPF_REG_10, stack_off_cnt);
21594+
if (insn->off >= 0)
21595+
insn_buf[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, insn->off + 5);
21596+
else
21597+
insn_buf[1] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_AX, 0, insn->off - 1);
21598+
insn_buf[2] = BPF_ALU64_IMM(BPF_SUB, BPF_REG_AX, 1);
21599+
insn_buf[3] = BPF_JMP_IMM(BPF_JNE, BPF_REG_AX, 0, 2);
21600+
/*
21601+
* AX is used as an argument to pass in stack_off_cnt
21602+
* (to add to r10/fp), and also as the return value of
21603+
* the call to arch_bpf_timed_may_goto.
21604+
*/
21605+
insn_buf[4] = BPF_MOV64_IMM(BPF_REG_AX, stack_off_cnt);
21606+
insn_buf[5] = BPF_EMIT_CALL(arch_bpf_timed_may_goto);
21607+
insn_buf[6] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt);
21608+
cnt = 7;
21609+
21610+
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
21611+
if (!new_prog)
21612+
return -ENOMEM;
21613+
21614+
delta += cnt - 1;
21615+
env->prog = prog = new_prog;
21616+
insn = new_prog->insnsi + i + delta;
21617+
goto next_insn;
21618+
} else if (is_may_goto_insn(insn)) {
2157621619
int stack_off = -stack_depth - 8;
2157721620

2157821621
stack_depth_extra = 8;
@@ -22113,23 +22156,33 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
2211322156

2211422157
env->prog->aux->stack_depth = subprogs[0].stack_depth;
2211522158
for (i = 0; i < env->subprog_cnt; i++) {
22159+
int delta = bpf_jit_supports_timed_may_goto() ? 2 : 1;
2211622160
int subprog_start = subprogs[i].start;
2211722161
int stack_slots = subprogs[i].stack_extra / 8;
22162+
int slots = delta, cnt = 0;
2211822163

2211922164
if (!stack_slots)
2212022165
continue;
22121-
if (stack_slots > 1) {
22166+
/* We need two slots in case timed may_goto is supported. */
22167+
if (stack_slots > slots) {
2212222168
verbose(env, "verifier bug: stack_slots supports may_goto only\n");
2212322169
return -EFAULT;
2212422170
}
2212522171

22126-
/* Add ST insn to subprog prologue to init extra stack */
22127-
insn_buf[0] = BPF_ST_MEM(BPF_DW, BPF_REG_FP,
22128-
-subprogs[i].stack_depth, BPF_MAX_LOOPS);
22172+
stack_depth = subprogs[i].stack_depth;
22173+
if (bpf_jit_supports_timed_may_goto()) {
22174+
insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
22175+
BPF_MAX_TIMED_LOOPS);
22176+
insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth + 8, 0);
22177+
} else {
22178+
/* Add ST insn to subprog prologue to init extra stack */
22179+
insn_buf[cnt++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, -stack_depth,
22180+
BPF_MAX_LOOPS);
22181+
}
2212922182
/* Copy first actual insn to preserve it */
22130-
insn_buf[1] = env->prog->insnsi[subprog_start];
22183+
insn_buf[cnt++] = env->prog->insnsi[subprog_start];
2213122184

22132-
new_prog = bpf_patch_insn_data(env, subprog_start, insn_buf, 2);
22185+
new_prog = bpf_patch_insn_data(env, subprog_start, insn_buf, cnt);
2213322186
if (!new_prog)
2213422187
return -ENOMEM;
2213522188
env->prog = prog = new_prog;
@@ -22139,7 +22192,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
2213922192
* to insn after BPF_ST that inits may_goto count.
2214022193
* Adjustment will succeed because bpf_patch_insn_data() didn't fail.
2214122194
*/
22142-
WARN_ON(adjust_jmp_off(env->prog, subprog_start, 1));
22195+
WARN_ON(adjust_jmp_off(env->prog, subprog_start, delta));
2214322196
}
2214422197

2214522198
/* Since poke tab is now finalized, publish aux to tracker. */

0 commit comments

Comments
 (0)