[PATCH bpf-next v2] cgroup/rstat: disable preemption in css_rstat_updated()
From: Karl Mehltretter
Date: Mon Nov 23 2026 - 22:17:01 EST
BPF programs can call css_rstat_updated() with preemption enabled.
The kfunc calls __css_rstat_updated(), which requires preemption to
be disabled while it queues an rstat update. A cgroup iterator can
hit the lockdep assertion on both RT and non-RT kernels. The
cgroup_hierarchical_stats selftest can also hit it on PREEMPT_RT:
spin_lock_irqsave() on trace_cgroup_path_lock leaves preemption and
interrupts enabled there.
Disable preemption in the BPF wrapper after validating the CPU and
before calling __css_rstat_updated().
Fixes: 36df6e3dbd7e ("cgroup: make css_rstat_updated nmi safe")
Reported-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
Closes: https://lore.kernel.org/r/20251119111402.153727-1-jiayuan.chen@xxxxxxxxx/
Signed-off-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
---
Assisted by an LLM.
v2 places the preemption guard in the kfunc as Alexei requested:
https://lore.kernel.org/r/DN26CP5BB6O7.23ALW6CZUBYSD@xxxxxxxxx/
Since 8817005efbdf split out the BPF-only wrapper, v2 does not add
preemption operations to the in-kernel callers discussed when Jiayuan
proposed the earlier kernel-side version in November 2025:
https://lore.kernel.org/r/20251121040655.89584-1-jiayuan.chen@xxxxxxxxx/
v1: https://lore.kernel.org/r/20261121082843.44614-1-kmehltretter@xxxxxxxxx/
Tested in x86-64 KVM guests with lockdep on bpf-next 9867c61abbd4
(base and v2) and 2bf3351c5f89 (v2). Both tests passed on all 24 boots.
Boots with an rstat warning base v2 rebased v2
PREEMPT_RT, cgroup_hierarchical_stats 2/2 0/2 0/2
PREEMPT_RT, iter/cgroup 2/2 0/2 0/2
PREEMPT (!RT), cgroup_hierarchical_stats 0/2 0/2 0/2
PREEMPT (!RT), iter/cgroup 2/2 0/2 0/2
kernel/cgroup/rstat.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c
index de816a43db9f..d72fe721cea0 100644
--- a/kernel/cgroup/rstat.c
+++ b/kernel/cgroup/rstat.c
@@ -131,6 +131,7 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
if (unlikely(cpu < 0 || cpu >= nr_cpu_ids || !cpu_possible(cpu)))
return;
+ guard(preempt)();
__css_rstat_updated(css, cpu);
}
base-commit: 2bf3351c5f894697e8443eeef5333d4c4ce90cab