Re: [EXT.MSG] Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
From: Mike Bazov
Date: Mon Nov 23 2026 - 07:49:11 EST
Done. The array is not faster than walking the chain
bench -w2 -d12, M/s, median of 10 runs [min-max], lazy preemption,
x86-64 VM with 4 vCPUs, bpf-next 32b66b94c86f, in boot order.
The existing benchmarks:
trig-fentry trig-rawtp
bpf-next 39.36 [33.73-39.72] 40.02 [37.00-40.78]
series 40.10 [39.43-41.41] 39.63 [38.99-40.06]
array 38.97 [38.19-39.17] 39.19 [38.58-39.90]
bpf-next #2 39.74 [39.17-40.12] 41.12 [40.14-41.68]
series #2 40.68 [39.68-41.58] 39.81 [39.33-40.49]
array #2 39.89 [39.48-40.29] 39.49 [38.85-40.34]
I added two benchmarks to bench, with three programs nested: the
measured program starts inside the third one.
driver (raw_tp) calls bpf_ktime_get_boot_ns()
fentry on that helper calls bpf_ktime_get_tai_ns()
fentry on that one runs the loop that fires the measured program
trig-fentry-nest3 trig-rawtp-nest3
bpf-next 39.88 [39.42-40.52] 41.44 [40.93-42.68]
series 41.25 [39.95-41.95] 39.62 [38.62-40.50]
array 39.59 [38.15-41.53] 38.22 [36.37-39.02]
bpf-next #2 40.62 [39.79-41.13] 42.84 [41.96-43.50]
series #2 41.48 [40.09-41.94] 41.10 [39.70-41.63]
array #2 41.06 [39.73-41.80] 39.67 [39.07-40.08]
The array is patches 1 and 2 plus this in place of patch 3, with two
fields in task_struct
const struct bpf_prog *bpf_recur_progs[4];
unsigned int bpf_recur_depth;
static inline bool bpf_prog_get_recursion_context(struct bpf_prog *prog)
{
+ struct task_struct *tsk = current;
+ unsigned int depth, i;
+ bool ok;
+
+ if (prog->recur_per_cpu)
+ return bpf_prog_get_recursion_percpu(prog); /* the old body */
+
+ depth = READ_ONCE(tsk->bpf_recur_depth);
+ ok = depth < ARRAY_SIZE(tsk->bpf_recur_progs);
+ for (i = 0; ok && i < depth; i++)
+ ok = READ_ONCE(tsk->bpf_recur_progs[i]) != prog;
+ WRITE_ONCE(tsk->bpf_recur_depth, depth + 1);
+ if (ok)
+ WRITE_ONCE(tsk->bpf_recur_progs[depth], prog);
+ return ok;
}
static inline void bpf_prog_put_recursion_context(struct bpf_prog *prog)
{
+ if (prog->recur_per_cpu) {
+ bpf_prog_put_recursion_percpu(prog); /* the old body */
+ return;
+ }
+ WRITE_ONCE(current->bpf_recur_depth,
+ READ_ONCE(current->bpf_recur_depth) - 1);
}