Skip to contentSkip to conversation

cgroup: expose cpu.stat and io.stat to BPF

PATCH In review Ziyang Men 4 participants 12 messages Aug 20 – Sep 3

Subsystems (5)
lore
Share & tools
lore

Ziyang Men

author
#0 lore raw

v3 cover cgroup: expose cpu.stat and io.stat to BPF

Collecting cgroup statistics is expensive: the existing method is to open and parse a cgroup file for every cgroup of interest. The memory controller already has an efficient alternative through BPF; this series extends that model to the CPU and block I/O controllers.

Patch 1 adds the CPU kfuncs, and patch 2 adds their selftest. Patch 3 adds the blkcg kfuncs, and patch 4 adds their selftest.

This v3 combines the two previously posted v2 series:
CPU v2: https://lore.kernel.org/all/[email protected]/
blkcg v2: https://lore.kernel.org/all/[email protected]/

===
Changes since CPU v2:
- Rename bpf_cpu.c to bpf_cgroup.c and use unlikely() and container_of() for the task_group conversion.
- Remove bpf_css_flush_rstat() and register the existing css_rstat_flush() in the common kfunc set.
- Remove the SLEEPABLE mark bpf_cgroup_base_stat().

Changes since blkcg v2:
- Add bpf_cgroup_css() and bpf_css_release() to keep a controller css alive across the sleepable rstat flush.
- Add an RCU-protected checked css-to-blkcg conversion and make the blkg iterator take the typed blkcg pointer.
- Use the existing css_rstat_flush() and remove bpf_blkcg_flush_stats().
- Do not expose root io.stat values.

===
Changes since CPU v1:
- Make the rstat flush generic, move it to rstat.c, and make it take a css.
- Expose 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 scheduler changes.

Built with LLVM. Pass test on v7.2-rc5.

Ziyang Men (4):
cgroup: add BPF kfuncs to read a cpu cgroup's stats selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs
block: add BPF kfuncs to read blkcg io.stat selftests/bpf: add test for blkcg io.stat BPF kfuncs

 MAINTAINERS                                   |   1 +
 block/Makefile                                |   3 +
 block/bpf_blkcg.c                             | 138 ++++++++++
 kernel/cgroup/Makefile                        |   2 +
 kernel/cgroup/bpf_cgroup.c                    | 100 +++++++
 kernel/cgroup/rstat.c                         |  54 +++-
 tools/testing/selftests/bpf/cgroup_iter_cpu.h |  22 ++
 tools/testing/selftests/bpf/cgroup_iter_io.h  |  17 ++
 tools/testing/selftests/bpf/config            |   4 +
 .../bpf/prog_tests/cgroup_iter_cpu.c          | 259 ++++++++++++++++++
 .../selftests/bpf/prog_tests/cgroup_iter_io.c | 254 +++++++++++++++++
 .../selftests/bpf/progs/cgroup_iter_cpu.c     | 113 ++++++++
 .../selftests/bpf/progs/cgroup_iter_io.c      |  74 +++++
 13 files changed, 1037 insertions(+), 4 deletions(-)
 create mode 100644 block/bpf_blkcg.c
 create mode 100644 kernel/cgroup/bpf_cgroup.c
 create mode 100644 tools/testing/selftests/bpf/cgroup_iter_cpu.h
 create mode 100644 tools/testing/selftests/bpf/cgroup_iter_io.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_io.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_io.c

base-commit: 3b5f4b83c4abc0c9b0a7b9e2b44e816611b7f2ec

Signature · 1 line
2.53.0-Meta
author
#1 lore raw

PATCH v3 1/4 cgroup: add BPF kfuncs to read a cpu cgroup's stats

Collecting cgroup statistics is expensive because the existing method opens and parses a cgroup file. memcg already provides an efficient BPF interface; extend that model to the CPU controller.

Register css_rstat_flush() as a common kfunc and add bpf_cgroup_base_stat(). The latter returns cgroup_base_stat after the same cputime adjustment used by cpu.stat.

The BPF program reads the plain CFS bandwidth counters directly. Add bpf_css_to_task_group() to check the controller and give the verifier a typed task_group pointer for bpf_per_cpu_ptr().

css_rstat_flush() may reschedule and requires a sleepable program. bpf_cgroup_base_stat() only takes locks and is not marked sleepable, but those locks are not NMI-safe.

Assisted-by Claude:claude-opus-5
---
 kernel/cgroup/Makefile     |  2 ++
 kernel/cgroup/bpf_cgroup.c | 58 ++++++++++++++++++++++++++++++++++++++
 kernel/cgroup/rstat.c      | 54 ++++++++++++++++++++++++++++++++---
 3 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 kernel/cgroup/bpf_cgroup.c
kernel/cgroup/Makefile +2 -0
index ede31601a363..29f29228865b 100644
@@ -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_cgroup.o
+
 obj-$(CONFIG_CGROUP_FREEZER) += legacy_freezer.o
 obj-$(CONFIG_CGROUP_PIDS) += pids.o
 obj-$(CONFIG_CGROUP_RDMA) += rdma.o
kernel/cgroup/bpf_cgroup.c new file +58 -0

58 lines not on this page · raw message

kernel/cgroup/rstat.c +50 -4

67 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
#2 lore raw

Re: [PATCH v3 1/4] cgroup: add BPF kfuncs to read a cpu cgroup's stats

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The `bpf_cgroup_base_stat` kfunc is exposed to all BPF program types but unsafely acquires a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The bpf_cgroup_base_stat kfunc is exposed to all BPF program types but unsafely acquires a non-NMI-safe lock and unconditionally enables hardware interrupts, leading to IRQ state corruption, NMI deadlocks, and PREEMPT_RT panics. --

--- Patch [1]: [PATCH v3 1/4] cgroup: add BPF kfuncs to read a cpu cgroup's stats --- commit 41558082c4ece37f910c5109f3537477a7ba8820
Author: Ziyang Men <[email protected]>

cgroup: add BPF kfuncs to read a cpu cgroup's stats

This commit adds BPF kfuncs for reading CPU cgroup statistics. It registers css_rstat_flush() and bpf_cgroup_base_stat(), allowing BPF programs to read CFS bandwidth counters directly.

Quoted 4 lines
kernel/cgroup/rstat.c +0 -0
index de816a43db9f0..5db72504a8a5a 100644

[ ... ]

Quoted 15 lines
kernel/cgroup/rstat.c +10 -1
@@ -752,10 +752,49 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq)
 	cgroup_force_idle_show(seq, &bstat);
 }
 
-/* Add bpf kfuncs for css_rstat_updated() and css_rstat_flush() */
+#ifdef CONFIG_BPF_SYSCALL
+
+__bpf_kfunc_start_defs();
+
+/**
+ * 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.

kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 774

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+ */
+__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);

[Severity: High]
Does this code unconditionally enable hardware interrupts when called from arbitrary BPF contexts?

Since this kfunc is registered under BPF_PROG_TYPE_UNSPEC without context checks, it can be called from tracing programs (like kprobes or perf_events) that may run in NMI context or with interrupts already disabled.

If it executes in an interrupt-disabled context, __css_rstat_unlock() will unconditionally re-enable hardware interrupts and violate the caller's locking assumptions:

kernel/cgroup/rstat.c:__css_rstat_unlock() {
	...
	spin_unlock_irq(lock);
}

Also, could calling this from an atomic tracing context cause a panic on PREEMPT_RT, since spinlock_t is a sleeping lock there?

Quoted 4 lines
kernel/cgroup/rstat.c +4 -0
+	} else {
+		root_cgroup_cputime(out);
+	}
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v3 2/4 selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs

In reply to Ziyang Men

Add 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.

Assisted-by Claude:claude-opus-5
---
 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
new file mode 100644
index 000000000000..74599a5c0e4d
@@ -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
index ea7044f30adc..482b40dde2f9 100644
@@ -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
#4 lore raw

Re: [PATCH v3 2/4] selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs

This isn't a bug, but CONFIG_CGROUP_SCHED already appears in all five config.<arch> fragments (config.x86_64:30, config.aarch64:26, config.s390x:19, config.ppc64el:17, config.riscv64:17), and…
Quoted 5 lines
tools/testing/selftests/bpf/config +0 -0
index ea7044f30adc..482b40dde2f9 100644

tools/testing/selftests/bpf/config CONFIG_BPF_STREAM_PARSER=y · line 19

Quoted 9 lines
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

This isn't a bug, but CONFIG_CGROUP_SCHED already appears in all five config.<arch> fragments (config.x86_64:30, config.aarch64:26, config.s390x:19, config.ppc64el:17, config.riscv64:17), and CONFIG_CFS_BANDWIDTH already appears in config.x86_64:24.

Now that they are in the common config, should the per-arch copies be removed so each option lives in one place?

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +0 -0
new file mode 100644
index 000000000000..cd7e92ababfb

[ ... ]

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +2 -0
@@ -0,0 +1,259 @@
+void test_cgroup_iter_cpu(void)
+{

[ ... ]

Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +22 -0
+	/*
+	 * cpu.stat cputime grows on every tick a task in the cgroup runs, so
+	 * stop them all before comparing
+	 */
+	if (test__start_subtest("cgroup_iter_cpu__match")) {
+		struct cpu_query filev = {};
+		int i, stable = 0;
+
+		kill(hog, SIGSTOP);
+		waitpid(hog, NULL, WUNTRACED);
+		if (!ASSERT_OK(join_root_cgroup(), "join_root_cgroup"))
+			goto cleanup_hog;
+
+		/*
+		 * The period timer keeps adding to nr_periods for a while
+		 * after the hog stops
+		 */
+		for (i = 0; i < 20; i++) {
+			struct cpu_query before = {}, after = {};
+
+			if (!ASSERT_OK(parse_stats(cgroup_fd, &before, have_bw), "cpu.stat") ||
+			    !ASSERT_OK(read_stats(link), "read stats") ||

tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c line 227

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c new file +10 -0
+			    !ASSERT_OK(parse_stats(cgroup_fd, &after, have_bw), "cpu.stat"))
+				goto cleanup_hog;
+
+			if (!memcmp(&before, &after, sizeof(before))) {
+				filev = before;
+				stable = 1;
+				break;
+			}
+			usleep(100000);
+		}

This isn't a bug, but could the 20 iterations and usleep(100000) get names the way cgroup_iter_memcg.c names MEMCG_STAT_RETRIES / MEMCG_STAT_RETRY_DELAY_US, so the relationship to the 100ms cpu.max period is visible?

[ ... ]

---
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/32419919743

author
#5 lore raw

PATCH v3 3/4 block: add BPF kfuncs to read blkcg io.stat

In reply to Ziyang Men

Collecting cgroup statistics is expensive because the existing method opens and parses a cgroup file for every cgroup. memcg already provides an efficient BPF interface; extend that model to the block controller.

Add bpf_cgroup_css() and bpf_css_release() to acquire a controller's css from a cgroup. The reference keeps the css alive across the sleepable css_rstat_flush().

Add bpf_css_to_blkcg() as a checked RCU-protected css-to-blkcg conversion and an open-coded iterator for the per-device blkgs.

Assisted-by Claude:claude-opus-5
---
 MAINTAINERS                |   1 +
 block/Makefile             |   3 +
 block/bpf_blkcg.c          | 138 +++++++++++++++++++++++++++++++++++++
 kernel/cgroup/bpf_cgroup.c |  62 ++++++++++++++---
 4 files changed, 194 insertions(+), 10 deletions(-)
 create mode 100644 block/bpf_blkcg.c
MAINTAINERS +1 -0
index 2f9472c1a090..87c56e955577 100644
@@ -6617,6 +6617,7 @@ F:	block/blk-cgroup.c
 F:	block/blk-iocost.c
 F:	block/blk-iolatency.c
 F:	block/blk-throttle.c
+F:	block/bpf_blkcg.c
 F:	include/linux/blk-cgroup.h
 
 CONTROL GROUP - CPUSET
block/Makefile +3 -0
index e7bd320e3d69..572e49988c8e 100644
@@ -17,6 +17,9 @@ obj-$(CONFIG_BLK_ERROR_INJECTION) += error-injection.o
 obj-$(CONFIG_BLK_DEV_BSG_COMMON) += bsg.o
 obj-$(CONFIG_BLK_DEV_BSGLIB)	+= bsg-lib.o
 obj-$(CONFIG_BLK_CGROUP)	+= blk-cgroup.o
+ifdef CONFIG_BPF_SYSCALL
+obj-$(CONFIG_BLK_CGROUP)	+= bpf_blkcg.o
+endif
 obj-$(CONFIG_BLK_CGROUP_RWSTAT)	+= blk-cgroup-rwstat.o
 obj-$(CONFIG_BLK_CGROUP_FC_APPID) += blk-cgroup-fc-appid.o
 obj-$(CONFIG_BLK_DEV_THROTTLING)	+= blk-throttle.o
block/bpf_blkcg.c new file +138 -0

138 lines not on this page · raw message

kernel/cgroup/bpf_cgroup.c +52 -10

93 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
#6 lore raw

Re: [PATCH v3 3/4] block: add BPF kfuncs to read blkcg io.stat

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Calling bpf_css_release() from NMI context can trigger a deadlock. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Calling bpf_css_release() from NMI context can trigger a deadlock. --

--- Patch [3]: [PATCH v3 3/4] block: add BPF kfuncs to read blkcg io.stat --- commit d71e52907eeb75ca126ab0da4391bc18134b90e2
Author: Ziyang Men <[email protected]>

block: add BPF kfuncs to read blkcg io.stat

This commit adds BPF kfuncs to acquire and release a controller's css from a cgroup. It also adds a kfunc to cast an io controller css to its block cgroup, along with an open-coded iterator for the per-device blkgs.

Quoted 4 lines
kernel/cgroup/bpf_cgroup.c +0 -0
index cd28c838dc7b6..e253633e82782 100644

[ ... ]

kernel/cgroup/bpf_cgroup.c line 52

Quoted 8 lines
kernel/cgroup/bpf_cgroup.c +8 -0
+/**
+ * bpf_css_release - Release a css reference
+ * @css: css to release
+ */
+__bpf_kfunc void bpf_css_release(struct cgroup_subsys_state *css)
+{
+	css_put(css);
+}

[Severity: High]
Can this trigger a deadlock if called from NMI context?

Since these kfuncs are registered under BPF_PROG_TYPE_UNSPEC, they can be called by NMI-context BPF programs such as perf events.

If a BPF program drops the last reference to the css here, css_put() will invoke the percpu_ref release callback, css_release(), directly in the NMI context.

css_release() unconditionally calls queue_work(), which internally acquires the workqueue pool's raw spinlock. If the NMI interrupted a context that already held this lock, would this instantly deadlock the system?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [PATCH v3 3/4] block: add BPF kfuncs to read blkcg io.stat

In reply to Ziyang Men
This isn't a bug, but the patch adds two controller-agnostic kfuncs (bpf_cgroup_css and bpf_css_release) in kernel/cgroup/bpf_cgroup.c, along with a three-way rename (bpf_cpu_cgroup_kfunc_ids to…
Quoted 53 lines
kernel/cgroup/bpf_cgroup.c +39 -1
index cd28c838dc7b..e253633e8278 100644
@@ -8,12 +8,50 @@
 #include <linux/bpf.h>
 #include <linux/btf_ids.h>
 #include <linux/cgroup.h>
+#include <linux/rcupdate.h>
 
+#ifdef CONFIG_CGROUP_SCHED
 #include "../sched/sched.h"
+#endif
 
-#ifdef CONFIG_CGROUP_SCHED
 __bpf_kfunc_start_defs();
 
+/**
+ * bpf_cgroup_css - Get a reference to one controller's css
+ * @cgrp: cgroup to look in
+ * @ssid: controller ID
+ *
+ * The returned css must be released with bpf_css_release().
+ *
+ * Return: The referenced css, or NULL.
+ */
+__bpf_kfunc struct cgroup_subsys_state *
+bpf_cgroup_css(struct cgroup *cgrp, int ssid)
+{
+	struct cgroup_subsys_state *css;
+
+	if (unlikely(ssid < 0 || ssid >= CGROUP_SUBSYS_COUNT))
+		return NULL;
+
+	rcu_read_lock();
+	css = rcu_dereference(cgrp->subsys[ssid]);
+	if (css && !css_tryget(css))
+		css = NULL;
+	rcu_read_unlock();
+
+	return css;
+}
+
+/**
+ * bpf_css_release - Release a css reference
+ * @css: css to release
+ */
+__bpf_kfunc void bpf_css_release(struct cgroup_subsys_state *css)
+{
+	css_put(css);
+}
+
+#ifdef CONFIG_CGROUP_SCHED

[ ... ]

Quoted 33 lines
kernel/cgroup/bpf_cgroup.c +10 -6
@@ -30,29 +68,33 @@ bpf_css_to_task_group(struct cgroup_subsys_state *css)
 
 	return container_of(css, struct task_group, css);
 }
+#endif /* CONFIG_CGROUP_SCHED */
 
 __bpf_kfunc_end_defs();
 
-BTF_KFUNCS_START(bpf_cpu_cgroup_kfunc_ids)
+BTF_KFUNCS_START(bpf_cgroup_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_cgroup_css, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_css_release, KF_RELEASE)
+#ifdef CONFIG_CGROUP_SCHED
 BTF_ID_FLAGS(func, bpf_css_to_task_group,
 	     KF_RCU | KF_RCU_PROTECTED | KF_RET_NULL)
-BTF_KFUNCS_END(bpf_cpu_cgroup_kfunc_ids)
+#endif
+BTF_KFUNCS_END(bpf_cgroup_kfunc_ids)
 
-static const struct btf_kfunc_id_set bpf_cpu_cgroup_kfunc_set = {
+static const struct btf_kfunc_id_set bpf_cgroup_kfunc_set = {
 	.owner		= THIS_MODULE,
-	.set		= &bpf_cpu_cgroup_kfunc_ids,
+	.set		= &bpf_cgroup_kfunc_ids,
 };
 
-static int __init bpf_cpu_cgroup_kfunc_init(void)
+static int __init bpf_cgroup_kfunc_init(void)
 {
 	int err;
 
 	err = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
-					&bpf_cpu_cgroup_kfunc_set);

kernel/cgroup/bpf_cgroup.c bpf_css_to_task_group(struct cgroup_subsys_state *css) · line 100

Quoted 10 lines
kernel/cgroup/bpf_cgroup.c +3 -3
+					&bpf_cgroup_kfunc_set);
 	if (err)
-		pr_warn("error while registering cpu cgroup kfuncs: %d\n", err);
+		pr_warn("error while registering cgroup kfuncs: %d\n", err);
 
 	return err;
 }
-late_initcall(bpf_cpu_cgroup_kfunc_init);
-#endif /* CONFIG_CGROUP_SCHED */
+late_initcall(bpf_cgroup_kfunc_init);

This isn't a bug, but the patch adds two controller-agnostic kfuncs (bpf_cgroup_css and bpf_css_release) in kernel/cgroup/bpf_cgroup.c, along with a three-way rename (bpf_cpu_cgroup_kfunc_ids to bpf_cgroup_kfunc_ids) and an ifdef restructure of the whole file.

The immediately preceding commit in this series (382c3bd4 "cgroup: add BPF kfuncs to read a cpu cgroup's stats") already touched this same file under a "cgroup:" prefix.

Would it be worth splitting the generic css acquire/release and the rename into a separate "cgroup:" patch, so this one stays scoped to the block changes?

---
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/32419919743

author
#8 lore raw

PATCH v3 4/4 selftests/bpf: add test for blkcg io.stat BPF kfuncs

In reply to Ziyang Men

Add cgroup_iter_io to test the blkcg io.stat BPF kfuncs. The BPF program acquires the I/O css, flushes its statistics, converts it to a typed blkcg under RCU, walks the blkgs, and releases the css reference.

The test performs O_DIRECT I/O on a private loop device. It checks the device ID and counters against io.stat.

Assisted-by Claude:claude-opus-5
---
 tools/testing/selftests/bpf/cgroup_iter_io.h  |  17 ++
 tools/testing/selftests/bpf/config            |   1 +
 .../selftests/bpf/prog_tests/cgroup_iter_io.c | 254 ++++++++++++++++++
 .../selftests/bpf/progs/cgroup_iter_io.c      |  74 +++++
 4 files changed, 346 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/cgroup_iter_io.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_io.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_io.c
tools/testing/selftests/bpf/cgroup_iter_io.h new file +17 -0
new file mode 100644
index 000000000000..f4bbaaccdf71
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#ifndef __CGROUP_ITER_IO_H
+#define __CGROUP_ITER_IO_H
+
+struct io_query {
+	/* one device's io.stat counters */
+	__u64 rbytes;
+	__u64 wbytes;
+	__u64 rios;
+	__u64 wios;
+	__u64 dbytes;
+	__u64 dios;
+	__u64 dev;	/* dev_t of the device the counters belong to */
+};
+
+#endif /* __CGROUP_ITER_IO_H */
tools/testing/selftests/bpf/config +1 -0
index 482b40dde2f9..0e10d625de6e 100644
@@ -1,3 +1,4 @@
+CONFIG_BLK_CGROUP=y
 CONFIG_BLK_DEV_LOOP=y
 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC=y
 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC=1
tools/testing/selftests/bpf/prog_tests/cgroup_iter_io.c new file +254 -0

254 lines not on this page · raw message

tools/testing/selftests/bpf/progs/cgroup_iter_io.c new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
author
#9 lore raw
In reply to Ziyang Men

Hi Reviewers,

Just want to follow-up on this patch series. Please let me know the possible issues and I will fix them.

Thanks very much!

Best,
Ziyang

Quoted 72 lines · Ziyang Men

Collecting cgroup statistics is expensive: the existing method is to open and parse a cgroup file for every cgroup of interest. The memory controller already has an efficient alternative through BPF; this series extends that model to the CPU and block I/O controllers.

Patch 1 adds the CPU kfuncs, and patch 2 adds their selftest. Patch 3 adds the blkcg kfuncs, and patch 4 adds their selftest.

This v3 combines the two previously posted v2 series:
CPU v2: https://lore.kernel.org/all/[email protected]/
blkcg v2: https://lore.kernel.org/all/[email protected]/

===
Changes since CPU v2:
- Rename bpf_cpu.c to bpf_cgroup.c and use unlikely() and container_of() for the task_group conversion.
- Remove bpf_css_flush_rstat() and register the existing css_rstat_flush() in the common kfunc set.
- Remove the SLEEPABLE mark bpf_cgroup_base_stat().

Changes since blkcg v2:
- Add bpf_cgroup_css() and bpf_css_release() to keep a controller css alive across the sleepable rstat flush.
- Add an RCU-protected checked css-to-blkcg conversion and make the blkg iterator take the typed blkcg pointer.
- Use the existing css_rstat_flush() and remove bpf_blkcg_flush_stats().
- Do not expose root io.stat values.

===
Changes since CPU v1:
- Make the rstat flush generic, move it to rstat.c, and make it take a css.
- Expose 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 scheduler changes.

Built with LLVM. Pass test on v7.2-rc5.

Ziyang Men (4):
cgroup: add BPF kfuncs to read a cpu cgroup's stats selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs
block: add BPF kfuncs to read blkcg io.stat selftests/bpf: add test for blkcg io.stat BPF kfuncs

MAINTAINERS                                   |   1 +
block/Makefile                                |   3 +
block/bpf_blkcg.c                             | 138 ++++++++++
kernel/cgroup/Makefile                        |   2 +
kernel/cgroup/bpf_cgroup.c                    | 100 +++++++
kernel/cgroup/rstat.c                         |  54 +++-
tools/testing/selftests/bpf/cgroup_iter_cpu.h |  22 ++
tools/testing/selftests/bpf/cgroup_iter_io.h  |  17 ++
tools/testing/selftests/bpf/config            |   4 +
.../bpf/prog_tests/cgroup_iter_cpu.c          | 259 ++++++++++++++++++
.../selftests/bpf/prog_tests/cgroup_iter_io.c | 254 +++++++++++++++++
.../selftests/bpf/progs/cgroup_iter_cpu.c     | 113 ++++++++
.../selftests/bpf/progs/cgroup_iter_io.c      |  74 +++++
13 files changed, 1037 insertions(+), 4 deletions(-)

create mode 100644 block/bpf_blkcg.c
create mode 100644 kernel/cgroup/bpf_cgroup.c
create mode 100644 tools/testing/selftests/bpf/cgroup_iter_cpu.h create mode 100644 tools/testing/selftests/bpf/cgroup_iter_io.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_cpu.c create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_io.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_cpu.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_io.c

base-commit: 3b5f4b83c4abc0c9b0a7b9e2b44e816611b7f2ec

Signature · 1 line
2.53.0-Meta
#10 lore raw
Quoted 5 lines · Ziyang Men

Hi Reviewers,

Just want to follow-up on this patch series. Please let me know the possible issues and I will fix them.

AI bots have raised real concerns. Please address those and then resubmit.

author
#11 lore raw
Quoted 9 lines · Shakeel Butt
Quoted 5 lines · Ziyang Men

Hi Reviewers,

Just want to follow-up on this patch series. Please let me know the possible issues and I will fix them.

AI bots have raised real concerns. Please address those and then resubmit.

Hi Shakeel,

I have reviewed the AI bots suggestions, and it seems the main issue is to decide whether we want some kfuncs to be NMI safe, e.g.,

- The bpf_cgroup_base_stat(), which calls the cputime_adjust() and in turn acquires raw_spin_lock_irqsave().
- The bpf_cgroup_css() and bpf_css_release() to get/put the css.

The simply solution is to mark them SLEEPABLE.

Hi Eduard, what's your idea about this? Do we want to mark them SLEEPABLE or there are better ways stand in the mid?

Thanks,
Ziyang