From: Ziyang Men <ziyang.meme@gmail.com>
To: kernel-team@meta.com, "Jens Axboe" <axboe@kernel.dk>,
"Tejun Heo" <tj@kernel.org>, "Josef Bacik" <josef@toxicpanda.com>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Shuah Khan" <shuah@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Vincent Guittot <vincent.guittot@linaro.org>,
Ben Segall <bsegall@google.com>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
JP Kobryn <inwardvessel@gmail.com>,
Mykola Lysenko <mykolal@meta.com>,
Ziyang Men <ziyang.meme@gmail.com>,
linux-block@vger.kernel.org, bpf@vger.kernel.org,
cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v3 3/4] block: add BPF kfuncs to read blkcg io.stat
Date: Thu, 20 Aug 2026 14:17:57 -0700 [thread overview]
Message-ID: <20260820211758.3393984-4-ziyang.meme@gmail.com> (raw)
In-Reply-To: <20260820211758.3393984-1-ziyang.meme@gmail.com>
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.
Suggested-by: Shakeel Butt <shakeel.butt@linux.dev>
Suggested-by: Tejun Heo <tj@kernel.org>
Assisted-by: Claude:claude-opus-5
Signed-off-by: Ziyang Men <ziyang.meme@gmail.com>
---
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
diff --git a/MAINTAINERS b/MAINTAINERS
index 2f9472c1a090..87c56e955577 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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
diff --git a/block/Makefile b/block/Makefile
index e7bd320e3d69..572e49988c8e 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -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
diff --git a/block/bpf_blkcg.c b/block/bpf_blkcg.c
new file mode 100644
index 000000000000..d8ab8006bc57
--- /dev/null
+++ b/block/bpf_blkcg.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Block I/O Controller-related BPF kfuncs and auxiliary code
+ */
+
+#include "blk-cgroup.h"
+
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/rculist.h>
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_css_to_blkcg - Cast an io controller css to its block cgroup
+ * @css: io controller css
+ *
+ * Must be called under RCU.
+ *
+ * Return: The block cgroup, or NULL if @css belongs to another controller.
+ */
+__bpf_kfunc struct blkcg *
+bpf_css_to_blkcg(struct cgroup_subsys_state *css)
+{
+ if (unlikely(css->ss != &io_cgrp_subsys))
+ return NULL;
+
+ return css_to_blkcg(css);
+}
+
+struct bpf_iter_blkg {
+ __u64 __opaque[2];
+} __aligned(8);
+
+struct bpf_iter_blkg_kern {
+ struct blkcg *blkcg;
+ struct blkcg_gq *pos;
+} __aligned(8);
+
+/**
+ * bpf_iter_blkg_new - Start iterating a block cgroup's per-device blkgs
+ * @it: iterator to initialize
+ * @blkcg: block cgroup to iterate
+ *
+ * Each blkg holds one device's io.stat counters. Offline blkgs are skipped.
+ * A blkg without a disk can be returned. Root blkgs do not contain the
+ * system-wide statistics shown by root io.stat. Must run under RCU.
+ *
+ * Return: 0 on success.
+ */
+__bpf_kfunc int bpf_iter_blkg_new(struct bpf_iter_blkg *it,
+ struct blkcg *blkcg)
+{
+ struct bpf_iter_blkg_kern *kit = (void *)it;
+
+ BUILD_BUG_ON(sizeof(struct bpf_iter_blkg_kern) > sizeof(struct bpf_iter_blkg));
+ BUILD_BUG_ON(__alignof__(struct bpf_iter_blkg_kern) !=
+ __alignof__(struct bpf_iter_blkg));
+
+ kit->pos = NULL;
+ kit->blkcg = blkcg;
+ return 0;
+}
+
+/**
+ * bpf_iter_blkg_next - Return the next online blkg of the iterated block cgroup
+ * @it: iterator
+ *
+ * Return: the next online blkg, or NULL when the walk is done.
+ */
+__bpf_kfunc struct blkcg_gq *bpf_iter_blkg_next(struct bpf_iter_blkg *it)
+{
+ struct bpf_iter_blkg_kern *kit = (void *)it;
+ struct blkcg_gq *blkg = kit->pos;
+ struct hlist_node *node;
+
+ if (!kit->blkcg)
+ return NULL;
+
+ if (!blkg)
+ node = rcu_dereference(hlist_first_rcu(&kit->blkcg->blkg_list));
+ else
+ node = rcu_dereference(hlist_next_rcu(&blkg->blkcg_node));
+
+ /* Skip offline blkgs, matching io.stat. */
+ while (node) {
+ blkg = hlist_entry(node, struct blkcg_gq, blkcg_node);
+ /* A race only changes whether this blkg is returned. */
+ if (data_race(blkg->online)) {
+ kit->pos = blkg;
+ return blkg;
+ }
+ node = rcu_dereference(hlist_next_rcu(&blkg->blkcg_node));
+ }
+
+ /* The iterator must keep returning NULL after completion. */
+ kit->pos = NULL;
+ kit->blkcg = NULL;
+ return NULL;
+}
+
+/**
+ * bpf_iter_blkg_destroy - Tear down a blkg iterator
+ * @it: iterator
+ */
+__bpf_kfunc void bpf_iter_blkg_destroy(struct bpf_iter_blkg *it)
+{
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_blkcg_kfuncs)
+BTF_ID_FLAGS(func, bpf_css_to_blkcg,
+ KF_RCU | KF_RCU_PROTECTED | KF_RET_NULL)
+
+BTF_ID_FLAGS(func, bpf_iter_blkg_new,
+ KF_ITER_NEW | KF_RCU | KF_RCU_PROTECTED)
+BTF_ID_FLAGS(func, bpf_iter_blkg_next, KF_ITER_NEXT | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_blkg_destroy, KF_ITER_DESTROY)
+BTF_KFUNCS_END(bpf_blkcg_kfuncs)
+
+static const struct btf_kfunc_id_set bpf_blkcg_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &bpf_blkcg_kfuncs,
+};
+
+static int __init bpf_blkcg_init(void)
+{
+ int err;
+
+ err = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
+ &bpf_blkcg_kfunc_set);
+ if (err)
+ pr_warn("error while registering bpf blkcg kfuncs: %d\n", err);
+
+ return err;
+}
+late_initcall(bpf_blkcg_init);
diff --git a/kernel/cgroup/bpf_cgroup.c b/kernel/cgroup/bpf_cgroup.c
index cd28c838dc7b..e253633e8278 100644
--- a/kernel/cgroup/bpf_cgroup.c
+++ b/kernel/cgroup/bpf_cgroup.c
@@ -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
/**
* bpf_css_to_task_group - Cast a CPU controller css to its task group
* @css: CPU controller css
@@ -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);
+ &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);
--
2.53.0-Meta
next prev parent reply other threads:[~2026-08-20 21:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-20 21:17 [PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF Ziyang Men
2026-08-20 21:17 ` [PATCH v3 1/4] cgroup: add BPF kfuncs to read a cpu cgroup's stats Ziyang Men
2026-08-20 21:17 ` [PATCH v3 2/4] selftests/bpf: add cgroup_iter_cpu test for cpu cgroup kfuncs Ziyang Men
2026-08-20 22:19 ` bot+bpf-ci
2026-08-20 21:17 ` Ziyang Men [this message]
2026-08-20 22:19 ` [PATCH v3 3/4] block: add BPF kfuncs to read blkcg io.stat bot+bpf-ci
2026-08-20 21:17 ` [PATCH v3 4/4] selftests/bpf: add test for blkcg io.stat BPF kfuncs Ziyang Men
2026-08-31 22:16 ` [PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF Ziyang Men
2026-09-02 23:10 ` Shakeel Butt
2026-09-03 22:33 ` Ziyang Men
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260820211758.3393984-4-ziyang.meme@gmail.com \
--to=ziyang.meme@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=axboe@kernel.dk \
--cc=bpf@vger.kernel.org \
--cc=bsegall@google.com \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dietmar.eggemann@arm.com \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hannes@cmpxchg.org \
--cc=inwardvessel@gmail.com \
--cc=jolsa@kernel.org \
--cc=josef@toxicpanda.com \
--cc=kernel-team@meta.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=mykolal@meta.com \
--cc=peterz@infradead.org \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®