[PATCH bpf-next v3 2/3] bpf: Limit nesting of mmap_read_trylock() in BPF helpers
From: Mike Bazov via B4 Relay
Date: Fri Nov 20 2026 - 06:00:08 EST
From: Mike Bazov <mike.bazov@xxxxxxxxx>
bpf_get_stack() and bpf_get_stackid() with BPF_F_USER_BUILD_ID, and
bpf_find_vma(), take mmap_read_trylock() on the current mm. That fires
the mmap_lock tracepoints, so a program on one of them that calls these
helpers runs the tracepoint again from inside itself:
prog 1 on mmap_lock_acquire_returned
bpf_get_stack(BPF_F_USER_STACK | BPF_F_USER_BUILD_ID)
mmap_read_trylock()
prog 2 on mmap_lock_acquire_returned
bpf_get_stack(...)
mmap_read_trylock()
...
The recursion check of each program only stops it from running inside
itself, so this nests as deep as there are programs attached, and each
level runs all programs that aren't on the stack yet: the work per
tracepoint hit grows with the factorial of the number of programs. With
irqs disabled, the per-CPU irq_work already allows only one level; with
irqs enabled nothing limits it.
Count the levels per CPU with irqs enabled too and fail with -EBUSY past
three, the same depth as the other per-CPU nesting limits of BPF
tracing. The helpers then fall back as they do when the trylock fails:
bpf_get_stack() and bpf_get_stackid() return IPs instead of build IDs,
and bpf_find_vma() returns -EBUSY.
The counter is per CPU, and programs with irqs enabled can be
preempted, so a preempted holder counts against the other tasks on its
CPU. They can hit the limit early and get the fallback; they never get
wrong data.
Suggested-by: Alexei Starovoitov <ast@xxxxxxxxxx>
Signed-off-by: Mike Bazov <mike.bazov@xxxxxxxxx>
---
kernel/bpf/mmap_unlock_work.h | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/kernel/bpf/mmap_unlock_work.h b/kernel/bpf/mmap_unlock_work.h
index 1834db20b..02af564e6 100644
--- a/kernel/bpf/mmap_unlock_work.h
+++ b/kernel/bpf/mmap_unlock_work.h
@@ -13,6 +13,7 @@ struct mmap_unlock_irq_work {
struct irq_work irq_work;
struct mm_struct *mm;
atomic_t active;
+ int nest_level;
};
DECLARE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
@@ -28,8 +29,16 @@ static inline struct mmap_unlock_irq_work *bpf_mmap_unlock_guard_get(void)
{
struct mmap_unlock_irq_work *work;
- if (!irqs_disabled())
- return NULL;
+ if (!irqs_disabled()) {
+ /*
+ * mmap_read_trylock() fires the mmap_lock tracepoints, and
+ * programs on them can get here again.
+ */
+ if (this_cpu_inc_return(mmap_unlock_work.nest_level) <= 3)
+ return NULL;
+ this_cpu_dec(mmap_unlock_work.nest_level);
+ return ERR_PTR(-EBUSY);
+ }
/*
* PREEMPT_RT does not allow to trylock mmap sem in interrupt
@@ -51,12 +60,15 @@ bpf_mmap_unlock_guard_put(struct mmap_unlock_irq_work *work)
{
if (work)
atomic_set_release(&work->active, 0);
+ else
+ this_cpu_dec(mmap_unlock_work.nest_level);
}
static inline void bpf_mmap_unlock_mm(struct mmap_unlock_irq_work *work, struct mm_struct *mm)
{
if (!work) {
mmap_read_unlock(mm);
+ bpf_mmap_unlock_guard_put(NULL);
} else {
work->mm = mm;
--
2.43.0