cgroup: expose cpu.stat to BPF
Subsystems (4)
v2 cover cgroup: expose cpu.stat to BPF
As the title suggests, this series adds bpf kfuncs for the cgroup CPU controller.
Motivation:
Collecting cgroup statistics is expensive: the existing method is to open and parse a cgroup file. memcg already has an efficient alternative through BPF; this series extends that idea to cpu.
Design:
- Add bpf_css_flush_rstat() and bpf_cgroup_base_stat() to cgroup rstat. The second kfunc returns the raw cgroup_base_stat after the same cputime adjustment used by cpu.stat.
- Leave reading the CFS bandwidth counters to the BPF program. They are plain fields of tg->cfs_bandwidth, so they need no kernel code.
- Add bpf_css_to_task_group(), which returns a checked RCU pointer of the task_group*. This is usefule to compute the throttled time in bpf side since the bpf_per_cpu_ptr() requires a verifier-known struct task_group * and a pointer carrying the MEM_PERCPU property. This use the convention that task_group embeds its css at offset zero, so no scheduler helper or scheduler source change is needed.
===
Changes since v1:
- Make the rstat flush generic, move it to rstat.c, and take a css.
- Return cgroup_base_stat directly instead of repacking its CPU times.
- Replace the throttled-time aggregation kfunc with a checked RCU cast in the CPU cgroup BPF code. The cast preserves the verifier type needed for the BPF program to perform the per-CPU sum itself.
- Remove the changes in the scheduler part.
Tested on VM with v7.2-rc5.
Ziyang Men (2):
cgroup: add BPF kfuncs to read a cpu cgroup's stats selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs
kernel/cgroup/Makefile | 2 + kernel/cgroup/bpf_cpu.c | 61 +++++ kernel/cgroup/rstat.c | 59 +++- tools/testing/selftests/bpf/cgroup_iter_cpu.h | 22 ++ tools/testing/selftests/bpf/config | 3 + .../bpf/prog_tests/cgroup_iter_cpu.c | 259 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_iter_cpu.c | 113 ++++++++ 7 files changed, 517 insertions(+), 2 deletions(-) create mode 100644 kernel/cgroup/bpf_cpu.c create mode 100644 tools/testing/selftests/bpf/cgroup_iter_cpu.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c
Signature · 1 line
2.53.0-Meta
PATCH v2 1/2 cgroup: add BPF kfuncs to read a cpu cgroup's stats
Collecting cgroup statistics is expensive: the existing method is to open and parse a cgroup file. memcg already has an efficient alternative through BPF; this series extends that idea to cpu.
Expose the CPU controller's per-cgroup statistics to BPF, following the memory controller kfuncs in mm/bpf_memcontrol.c.
Design:
- Add bpf_css_flush_rstat() and bpf_cgroup_base_stat() to cgroup rstat. The second kfunc returns the raw cgroup_base_stat after the same cputime adjustment used by cpu.stat.
- Leave reading the CFS bandwidth counters to the BPF program. They are plain fields of tg->cfs_bandwidth, so they need no kernel code.
- Add bpf_css_to_task_group(), which returns a checked RCU pointer of the task_group*. This is usefule to compute the throttled time in bpf side since the bpf_per_cpu_ptr() requires a verifier-known struct task_group * and a pointer carrying the MEM_PERCPU property. This use the convention that task_group embeds its css at offset zero, so no scheduler helper or scheduler source change is needed.
--- kernel/cgroup/Makefile | 2 ++ kernel/cgroup/bpf_cpu.c | 61 +++++++++++++++++++++++++++++++++++++++++ kernel/cgroup/rstat.c | 59 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 kernel/cgroup/bpf_cpu.c
kernel/cgroup/Makefile +2 -0
@@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 obj-y := cgroup.o rstat.o namespace.o cgroup-v1.o freezer.o +obj-$(CONFIG_BPF_SYSCALL) += bpf_cpu.o + obj-$(CONFIG_CGROUP_FREEZER) += legacy_freezer.o obj-$(CONFIG_CGROUP_PIDS) += pids.o obj-$(CONFIG_CGROUP_RDMA) += rdma.o
kernel/cgroup/bpf_cpu.c new file +61 -0
61 lines not on this page · raw message
kernel/cgroup/rstat.c +57 -2
70 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
This isn't a bug, but there are language errors in the third Design bullet: "usefule" should be "useful", and "This use" should be "This uses". Also "in bpf side" is typically written as "on the BPF…
Quoted 9 lines
cgroup: add BPF kfuncs to read a cpu cgroup's stats
Collecting cgroup statistics is expensive: the existing method is to open and parse a cgroup file. memcg already has an efficient alternative through BPF; this series extends that idea to cpu.
Expose the CPU controller's per-cgroup statistics to BPF, following the memory controller kfuncs in
mm/bpf_memcontrol.c.
Quoted 12 lines
Design:
- Addbpf_css_flush_rstat()andbpf_cgroup_base_stat()to cgroup rstat. The second kfunc returns the rawcgroup_base_statafter the same cputime adjustment used by cpu.stat.
- Leave reading the CFS bandwidth counters to the BPF program. They are plain fields oftg->cfs_bandwidth, so they need no kernel code.
- Addbpf_css_to_task_group(), which returns a checked RCU pointer of the task_group*. This is usefule to compute the throttled time in bpf side since thebpf_per_cpu_ptr()requires a verifier-known structtask_group* and a pointer carrying theMEM_PERCPUproperty. This use the convention thattask_groupembeds its css at offset zero, so no scheduler helper or scheduler source change is needed.
This isn't a bug, but there are language errors in the third Design
bullet: "usefule" should be "useful", and "This use" should be "This uses". Also "in bpf side" is typically written as "on the BPF side" in kernel documentation.
Quoted 13 lines
kernel/cgroup/Makefile +2
-0@@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 obj-y := cgroup.o rstat.o namespace.o cgroup-v1.o freezer.o +obj-$(CONFIG_BPF_SYSCALL) += bpf_cpu.o + obj-$(CONFIG_CGROUP_FREEZER) += legacy_freezer.o obj-$(CONFIG_CGROUP_PIDS) += pids.o obj-$(CONFIG_CGROUP_RDMA) += rdma.o
[ ... ]
Quoted 10 lines
kernel/cgroup/rstat.c +2
-0@@ -752,6 +752,54 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq)cgroup_force_idle_show (seq, &bstat); } +#ifdef CONFIG_BPF_SYSCALL +
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 766
Quoted 10 lines
kernel/cgroup/rstat.c +10
-0+__bpf_kfunc_start_defs (); + +/** +* bpf_css_flush_rstat - Flush a cgroup subsystem's rstat data +* @css: cgroup subsystem state to flush +*/ +__bpf_kfunc void bpf_css_flush_rstat(struct cgroup_subsys_state *css) +{ +css_rstat_flush (css); +}
This isn't a bug, but is bpf_css_flush_rstat() needed?
css_rstat_flush() is already marked __bpf_kfunc and registered with KF_SLEEPABLE at rstat.c:409 and :806. Adding 'BTF_ID_FLAGS(func, css_rstat_flush, KF_SLEEPABLE)' to bpf_rstat_common_kfunc_ids below would expose the same operation to BPF without introducing a second name for it and without needing a wrapper.
Quoted 36 lines
kernel/cgroup/rstat.c +36
-0+ +/** +* bpf_cgroup_base_stat - Read a cgroup's base statistics +* @cgrp: cgroup to read from +* @out: zero-initialized output in nanoseconds +* +* CPU time is adjusted as for cpu.stat. +*/ +__bpf_kfunc void bpf_cgroup_base_stat(struct cgroup *cgrp, +struct cgroup_base_stat *out) +{ +if (cgroup_parent (cgrp)) { +__css_rstat_lock (&cgrp->self,-1 ); + *out = cgrp->bstat; +cputime_adjust (&cgrp->bstat.cputime, &cgrp->prev_cputime, + &out->cputime.utime, &out->cputime.stime); +__css_rstat_unlock (&cgrp->self,-1 ); + }else { +root_cgroup_cputime (out); + } +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (bpf_rstat_common_kfunc_ids) +BTF_ID_FLAGS (func ,bpf_css_flush_rstat ,KF_SLEEPABLE ) +BTF_ID_FLAGS (func, bpf_cgroup_base_stat,KF_SLEEPABLE ) +BTF_KFUNCS_END (bpf_rstat_common_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_rstat_common_kfunc_set = { + .owner =THIS_MODULE , + .set = &bpf_rstat_common_kfunc_ids, +}; + +#endif /* CONFIG_BPF_SYSCALL */ +
[ ... ]
Quoted 5 lines
static int
__initbpf_rstat_kfunc_init(void)
{kernel/cgroup/rstat.c +1
-2-return register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , - &bpf_rstat_kfunc_set); +int ret;
kernel/cgroup/rstat.c static const struct btf_kfunc_id_set bpf_rstat_kfunc_set = { · line 826
Quoted 10 lines
kernel/cgroup/rstat.c +8
-0+ + ret =register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , + &bpf_rstat_kfunc_set); +#ifdef CONFIG_BPF_SYSCALL + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , + &bpf_rstat_common_kfunc_set); +#endif +return ret; }late_initcall (bpf_rstat_kfunc_init);
This isn't a bug, but are these #ifdef CONFIG_BPF_SYSCALL guards necessary?
The existing kfuncs in this file (css_rstat_flush at line 409, BTF_KFUNCS_START at 804, register_btf_kfunc_id_set at 818) have no guards, and rstat.o is obj-y, so they compile fine with CONFIG_BPF_SYSCALL=n via the no-op stubs in include/linux/btf.h:652. The cited precedent mm/bpf_memcontrol.c uses 'obj-$(CONFIG_MEMCG) += bpf_memcontrol.o' with no preprocessor conditionals anywhere in the .c file. Documentation/process/coding-style.rst:1166-1175 asks to avoid preprocessor conditionals in .c files and prefer compiling out entire functions via header stubs. Would dropping the guards and using 'obj-$(CONFIG_CGROUP_SCHED) += bpf_cpu.o' in the Makefile work here?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/32085852351
Tejun Heo
maintainerRe: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
In reply to Ziyang MenQuoted 1 line · Ziyang Men
+++
b/kernel/cgroup/bpf_cpu.c
Probably not the best file name. bpf_cgroup.c or maybe just put it in cgroup.c?
Quoted 7 lines
+/**
+ *bpf_css_to_task_group- Cast a CPU controller css to its task group
+ * @css: CPU controller css
+ *
+ * Must be called under RCU.
+ * A C cast does not give the verifier atask_grouppointer. This kfunc
+ * preserves thetask_groupand per-CPU types needed to readcfs_rq.
The fact that this is used for per-CPU types now probably won't age well if this grows more usages in the future.
kernel/cgroup/bpf_cpu.c line 30
Quoted 7 lines
+ *
+ * Return: The task group, or NULL if @css belongs to another controller.
+ */kernel/cgroup/bpf_cpu.c new file +4
-0+__bpf_kfunc struct task_group * +bpf_css_to_task_group (struct cgroup_subsys_state *css) +{ +if (css->ss != &cpu_cgrp_subsys)
unlikely()?
kernel/cgroup/bpf_cpu.c line 34
Quoted 4 lines
kernel/cgroup/bpf_cpu.c new file +4
-0+return NULL ; + +/* task_group embeds css at offset zero. */ +return (struct task_group *)css;
container_of()?
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 766
Quoted 8 lines
kernel/cgroup/rstat.c +8
-0+/** +* bpf_css_flush_rstat - Flush a cgroup subsystem's rstat data +* @css: cgroup subsystem state to flush +*/ +__bpf_kfunc void bpf_css_flush_rstat(struct cgroup_subsys_state *css) +{ +css_rstat_flush (css); +}
Why is this necessary? Isn't css_rstat_flush() already exposed as a kfunc?
Quoted 16 lines
kernel/cgroup/rstat.c +16
-0+/** +* bpf_cgroup_base_stat - Read a cgroup's base statistics +* @cgrp: cgroup to read from +* @out: zero-initialized output in nanoseconds +* +* CPU time is adjusted as for cpu.stat. +*/ +__bpf_kfunc void bpf_cgroup_base_stat(struct cgroup *cgrp, +struct cgroup_base_stat *out) +{ +if (cgroup_parent (cgrp)) { +__css_rstat_lock (&cgrp->self,-1 ); + *out = cgrp->bstat; +cputime_adjust (&cgrp->bstat.cputime, &cgrp->prev_cputime, + &out->cputime.utime, &out->cputime.stime); +__css_rstat_unlock (&cgrp->self,-1 );
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 793
Quoted 10 lines
kernel/cgroup/rstat.c +10
-0+ }else { +root_cgroup_cputime (out); + } +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START(bpf_rstat_common_kfunc_ids) +BTF_ID_FLAGS(func, bpf_css_flush_rstat, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_cgroup_base_stat, KF_SLEEPABLE)
Why are these SLEEPABLE?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
Hi Tejun,
Quoted 6 lines · Tejun Heo
Quoted 1 line · Ziyang Men
+++
b/kernel/cgroup/bpf_cpu.cProbably not the best file name.
bpf_cgroup.cor maybe just put it incgroup.c?
bpf_cgroup.c sounds good, I will rename to it.
Quoted 11 lines
Quoted 7 lines
+/**
+ *bpf_css_to_task_group- Cast a CPU controller css to its task group
+ * @css: CPU controller css
+ *
+ * Must be called under RCU.
+ * A C cast does not give the verifier atask_grouppointer. This kfunc
+ * preserves thetask_groupand per-CPU types needed to readcfs_rq.The fact that this is used for per-CPU types now probably won't age well if this grows more usages in the future.
Yes the major usage of this function is to enable the bpf side to compute the throttled_time meanwhile not touch the codes in scheduler.
Quoted 10 lines
kernel/cgroup/bpf_cpu.c line 30
Quoted 7 lines
+ *
+ * Return: The task group, or NULL if @css belongs to another controller.
+ */kernel/cgroup/bpf_cpu.c new file +4
-0+__bpf_kfunc struct task_group * +bpf_css_to_task_group (struct cgroup_subsys_state *css) +{ +if (css->ss != &cpu_cgrp_subsys)
unlikely()?
Ok. Added.
Quoted 7 lines
kernel/cgroup/bpf_cpu.c line 34
Quoted 4 lines
kernel/cgroup/bpf_cpu.c new file +4
-0+return NULL ; + +/* task_group embeds css at offset zero. */ +return (struct task_group *)css;
container_of()?
Ok. Added.
Quoted 11 lines
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 766
Quoted 8 lines
kernel/cgroup/rstat.c +8
-0+/** +* bpf_css_flush_rstat - Flush a cgroup subsystem's rstat data +* @css: cgroup subsystem state to flush +*/ +__bpf_kfunc void bpf_css_flush_rstat(struct cgroup_subsys_state *css) +{ +css_rstat_flush (css); +}Why is this necessary? Isn't
css_rstat_flush()already exposed as a kfunc?
Ok. Will remove it.
Quoted 20 lines
Quoted 20 lines
kernel/cgroup/rstat.c +20
-0+/** +* bpf_cgroup_base_stat - Read a cgroup's base statistics +* @cgrp: cgroup to read from +* @out: zero-initialized output in nanoseconds +* +* CPU time is adjusted as for cpu.stat. +*/ +__bpf_kfunc void bpf_cgroup_base_stat(struct cgroup *cgrp, +struct cgroup_base_stat *out) +{ +if (cgroup_parent (cgrp)) { +__css_rstat_lock (&cgrp->self,-1 ); + *out = cgrp->bstat; +cputime_adjust (&cgrp->bstat.cputime, &cgrp->prev_cputime, + &out->cputime.utime, &out->cputime.stime); +__css_rstat_unlock (&cgrp->self,-1 ); + }else { +root_cgroup_cputime (out); + } +}
Quoted 9 lines
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 793
Quoted 6 lines
kernel/cgroup/rstat.c +6
-0+ +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START(bpf_rstat_common_kfunc_ids) +BTF_ID_FLAGS(func, bpf_css_flush_rstat, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_cgroup_base_stat, KF_SLEEPABLE)Why are these SLEEPABLE?
The css_rstat_flush() calls might_sleep() and cond_resched(). The bpf_cgroup_base_stat() takes an rstat spinlock_t, which can sleep on PREEMPT_RT.
So both marked as SLEEPABLE.
Quoted 4 lines
Thanks.
Signature · 1 line
tejun
Please let me know your concerns. Thanks!
Best,
Ziyang
Tejun Heo
maintainerRe: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
Quoted 8 lines · Ziyang Men
Quoted 6 lines
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 793
Quoted 3 lines
kernel/cgroup/rstat.c +3
-0+BTF_KFUNCS_START(bpf_rstat_common_kfunc_ids) +BTF_ID_FLAGS(func, bpf_css_flush_rstat, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_cgroup_base_stat, KF_SLEEPABLE)Why are these SLEEPABLE?
The
css_rstat_flush()callsmight_sleep()andcond_resched().
I see.
Quoted 2 lines
The
bpf_cgroup_base_stat()takes an rstatspinlock_t, which can sleep onPREEMPT_RT.
Is this actually required? This doesn't really make sense to me. Shouldn't what SLEEPABLE mean change on RT kernels instead?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
Hi Tejun,
Quoted 10 lines · Tejun Heo
Quoted 8 lines · Ziyang Men
Quoted 6 lines
kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 793
Quoted 3 lines
kernel/cgroup/rstat.c +3
-0+BTF_KFUNCS_START(bpf_rstat_common_kfunc_ids) +BTF_ID_FLAGS(func, bpf_css_flush_rstat, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_cgroup_base_stat, KF_SLEEPABLE)Why are these SLEEPABLE?
The
css_rstat_flush()callsmight_sleep()andcond_resched().
Quoted 7 lines
I see.
Quoted 2 lines
The
bpf_cgroup_base_stat()takes an rstatspinlock_t, which can sleep onPREEMPT_RT.Is this actually required? This doesn't really make sense to me. Shouldn't what SLEEPABLE mean change on RT kernels instead?
Oh sorry, I didn't notice that.
I might be wrong: this function calls the cputime_adjust(), which in turn acquires raw_spin_lock_irqsave(), so there would be NMI deadlock in the perf_event program. The __css_rstat_lock() take the spin_lock_irq() as well. So maybe a SLEEPABLE tag is still necessary?
Please let me know your concerns. Thanks!
Best,
Ziyang
Quoted 5 lines
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerRe: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
Hello,
Quoted 6 lines · Ziyang Men
Quoted 5 lines
Quoted 2 lines
The
bpf_cgroup_base_stat()takes an rstatspinlock_t, which can sleep onPREEMPT_RT.Is this actually required? This doesn't really make sense to me. Shouldn't what SLEEPABLE mean change on RT kernels instead?
Quoted 6 lines
Oh sorry, I didn't notice that.
I might be wrong: this function calls the
cputime_adjust(), which in turn acquiresraw_spin_lock_irqsave(), so there would be NMI deadlock in theperf_eventprogram. The__css_rstat_lock()take thespin_lock_irq()as well. So maybe a SLEEPABLE tag is still necessary?
I think this is for BPF folks to answer. I don't think SLEEPABLE is needed because of RT but yeah it wouldn't be safe to be called from nmi context.
Thanks.
Signature · 1 line
tejun
Re: [PATCH v2 1/2] cgroup: add BPF kfuncs to read a cpu cgroup's stats
Hi Tejun,
Quoted 11 lines · Tejun Heo
Hello,
Quoted 8 lines · Ziyang Men
Quoted 5 lines
Quoted 2 lines
The
bpf_cgroup_base_stat()takes an rstatspinlock_t, which can sleep onPREEMPT_RT.Is this actually required? This doesn't really make sense to me. Shouldn't what SLEEPABLE mean change on RT kernels instead?
Oh sorry, I didn't notice that.
Quoted 7 lines
Quoted 4 lines
I might be wrong: this function calls the
cputime_adjust(), which in turn acquiresraw_spin_lock_irqsave(), so there would be NMI deadlock in theperf_eventprogram. The__css_rstat_lock()take thespin_lock_irq()as well. So maybe a SLEEPABLE tag is still necessary?I think this is for BPF folks to answer. I don't think SLEEPABLE is needed because of RT but yeah it wouldn't be safe to be called from nmi context.
Ok I see, let's remove the SLEEPABLE for now.
Thanks,
Ziyang
Quoted 5 lines
Thanks.
Signature · 1 line
tejun
PATCH v2 2/2 selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs
In reply to Ziyang MenAdd cgroup_iter_cpu, a selftest for the CPU controller BPF kfuncs.
The userspace side runs a CPU hog in a test cgroup with cpu.max settled then:
- checks the CPU-time and throttling counters are nonzero,
- compares whether all values the program read are same as those reading from cgroup file.
Enable CONFIG_CGROUP_SCHED, CONFIG_FAIR_GROUP_SCHED and CONFIG_CFS_BANDWIDTH in the selftest config.
Tested on VM with v7.2-rc5.
--- tools/testing/selftests/bpf/cgroup_iter_cpu.h | 22 ++ tools/testing/selftests/bpf/config | 3 + .../bpf/prog_tests/cgroup_iter_cpu.c | 259 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_iter_cpu.c | 113 ++++++++ 4 files changed, 397 insertions(+) create mode 100644 tools/testing/selftests/bpf/cgroup_iter_cpu.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c
tools/testing/selftests/bpf/cgroup_iter_cpu.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#ifndef __CGROUP_ITER_CPU_H +#define __CGROUP_ITER_CPU_H + +struct cpu_query { +/* base cpu time, from cpu.stat */ +__u64 usage_usec; +__u64 user_usec; +__u64 system_usec; +__u64 nice_usec; +__u64 forceidle_usec; +/* CFS bandwidth throttling, from cpu.stat and cpu.stat.local */ +__u64 nr_periods; +__u64 nr_throttled; +__u64 throttled_usec; +__u64 nr_bursts; +__u64 burst_usec; +__u64 throttled_self_usec; +}; + +#endif /* __CGROUP_ITER_CPU_H */
tools/testing/selftests/bpf/config +3 -0
@@ -11,6 +11,9 @@ CONFIG_BPF_STREAM_PARSER=y CONFIG_BPF_SYSCALL=y # CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set CONFIG_CGROUP_BPF=y +CONFIG_CGROUP_SCHED=y +CONFIG_FAIR_GROUP_SCHED=y +CONFIG_CFS_BANDWIDTH=y CONFIG_CRYPTO_HMAC=y CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_USER_API=y
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +259 -0
259 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c new file +113 -0
113 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 2/2] selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs
This isn't a bug, but could the changelog body be split into paragraphs so it's clearer what state the cgroup is put in and which values are compared?
Quoted 15 lines
commit
0dbd10c9717fb724b55b28346bbbbe9e5e570bc6Author: Ziyang Men <[email protected]> Date: Mon Aug 17 17:24:50 2026 -0700selftests/bpf: add
cgroup_iter_cputest for cpu cgroup kfuncsAdd
cgroup_iter_cpu, a selftest for the CPU controller BPF kfuncs.The userspace side runs a CPU hog in a test cgroup with cpu.max settled then:
- checks the CPU-time and throttling counters are nonzero,
- compares whether all values the program read are same as those reading from cgroup file.
Quoted 8 lines
Enable
CONFIG_CGROUP_SCHED,CONFIG_FAIR_GROUP_SCHEDandCONFIG_CFS_BANDWIDTHin the selftest config.Tested on VM with v7.2-rc5.
Suggested-by Shakeel Butt <[email protected]>Assisted-by Claude:claude-opus-5Signed-off-by Ziyang Men <[email protected]>
This isn't a bug, but could the changelog body be split into paragraphs so it's clearer what state the cgroup is put in and which values are compared?
The sentence "with cpu.max settled" doesn't say what quota is written (the test writes "10000 100000"), and "compares whether all values the program read are same as those reading from cgroup file" is missing words that would indicate which side is being read from where.
Rewording might help, something like: "runs a CPU hog in a test cgroup with a cpu.max quota set, then checks that the CPU-time and throttling counters are nonzero and that every value the BPF program reports matches the corresponding value read from cpu.stat and cpu.stat.local."
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +0
-0
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +7
-0+/* Read cgroup file @name into @buf. */ +static int read_cgroup_file (int cgroup_fd,const char *name,char *buf, +size_t size) +{ +ssize_t n; +int fd; +
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c line 47
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +10
-0+ fd =openat (cgroup_fd, name,O_RDONLY ); +if (fd <0 ) +return -1 ; + n =read (fd, buf, size -1 ); +close (fd); +if (n <=0 ) +return -1 ; + buf[n] ='\0' ; +return 0 ; +}
This isn't a bug, but since cgroup_helpers.h already has write_cgroup_file() but no reader, would read_cgroup_file() be better placed next to it in cgroup_helpers.c so other tests can use it?
It's a generic "read any cgroup file into a buffer" helper with nothing cpu-specific in it.
Quoted 3 lines
tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c new file +0
-0
[ ... ]
Quoted 18 lines
tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c new file +18
-0+if (tg &&bpf_core_field_exists (tg->cfs_rq) && + bpf_core_field_exists(struct cfs_rq, throttled_clock_self_time)) { + __u32 mask_bytes = bpf_core_type_size(struct cpumask); +__u32 full_words = mask_bytes /sizeof (__u64); +int *cpu; + +if (full_words) +bpf_for_each (bits, cpu, + (const __u64 *)&__cpu_possible_mask, + full_words) + throttled_self +=read_throttled_self (tg, *cpu); + +if (mask_bytes & (sizeof (__u64) -1 )) { +__u32 tail =0 ; +const void *src = (const char *)&__cpu_possible_mask + + full_words *sizeof (__u64); +int bit; +
tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c line 106
Quoted 10 lines
tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c new file +10
-0+if (!bpf_probe_read_kernel (&tail,sizeof (tail), src)) + bpf_for(bit, 0, 32) +if (tail & (1U << bit)) { +__u32 tail_cpu = full_words *64 + bit; + + throttled_self += +read_throttled_self (tg, tail_cpu); + } + } + }
This isn't a bug, but since struct cpumask is an array of unsigned long, sizeof() is always a multiple of 8 on 64-bit, so the mask_bytes & (sizeof(__u64) - 1) tail path never fires there - is 32-bit coverage the intent here, and if so would a comment saying so help?
The if (full_words) guard also looks droppable given bpf_for_each(bits, ..., 0) is already a valid no-op (verifier_bits_iter.c:zero_words).
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/32085852351