[PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF

Ziyang Men posted 4 patches 1 month, 1 week ago
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
[PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF
Posted by Ziyang Men 1 month, 1 week ago
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/20260818002450.3071325-1-ziyang.meme@gmail.com/
blkcg v2: https://lore.kernel.org/all/20260817214205.723267-1-ziyang.meme@gmail.com/

===
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
-- 
2.53.0-Meta
Re: [PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF
Posted by Ziyang Men 3 weeks, 6 days ago
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

On Thu, Aug 20, 2026 at 02:17:54PM -0700, Ziyang Men wrote:
>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/20260818002450.3071325-1-ziyang.meme@gmail.com/
>blkcg v2: https://lore.kernel.org/all/20260817214205.723267-1-ziyang.meme@gmail.com/
>
>===
>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
>-- 
>2.53.0-Meta
Re: [PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF
Posted by Shakeel Butt 3 weeks, 4 days ago
On Mon, Aug 31, 2026 at 03:16:10PM -0700, Ziyang Men wrote:
> 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.
Re: [PATCH v3 0/4] cgroup: expose cpu.stat and io.stat to BPF
Posted by Ziyang Men 3 weeks, 3 days ago
On Wed, Sep 02, 2026 at 04:10:01PM -0700, Shakeel Butt wrote:
>On Mon, Aug 31, 2026 at 03:16:10PM -0700, Ziyang Men wrote:
>> 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