Skip to contentSkip to conversation

cgroup/rstat: disable preemption in css_rstat_updated()

PATCH Applied Karl Mehltretter 4 participants 5 messages Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cgroup/rstat: disable preemption in css_rstat_updated()

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")

---
Assisted by an LLM.

v2 places the preemption guard in the kfunc as Alexei requested:
https://lore.kernel.org/r/[email protected]/
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/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/

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(+)
kernel/cgroup/rstat.c +1 -0
index de816a43db9f..d72fe721cea0 100644
@@ -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

#1 lore raw
Quoted 18 lines · Karl Mehltretter

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")

---
Assisted by an LLM.

Quoted 8 lines

v2 places the preemption guard in the kfunc as Alexei requested:
https://lore.kernel.org/r/[email protected]/
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/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/

I suggest you target cgroup-tree instread of bpf-tree if you need revise, because you only modified pure cgroup file.

Since your v2 does the same operation as my previous v1 which was denied by cgroup maintainer, I also suggest you wait their replies ( As time goes by, situation can change).

I still insist on kernel-side modification just like this patch because we should prevent 'wrong' bpf code from triggering WARNING.

Quoted 26 lines

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(+)
kernel/cgroup/rstat.c +1 -0
index de816a43db9f..d72fe721cea0 100644
@@ -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

#2 lore raw
Quoted 33 lines · Jiayuan Chen
Quoted 18 lines · Karl Mehltretter

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")

---
Assisted by an LLM.

Quoted 8 lines

v2 places the preemption guard in the kfunc as Alexei requested:
https://lore.kernel.org/r/[email protected]/
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/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/

I suggest you target cgroup-tree instread of bpf-tree if you need revise, because you only modified pure cgroup file.

Quoted 7 lines

Since your v2 does the same operation as my previous v1 which was denied by cgroup maintainer, I also suggest you wait their replies ( As time goes by, situation can change).

I still insist on kernel-side modification just like this patch because we should prevent 'wrong' bpf code from triggering WARNING.

After the commit 8817005efbdfd all the in-kernel usage has moved to __css_rstat_updated(), so I think this patch is fine.

Acked-by on v2 1/1
#3 lore raw
In reply to Karl Mehltretter
Quoted 8 lines · Karl Mehltretter

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.

Quoted 7 lines

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")