Skip to contentSkip to conversation

cgroup: separate rstat trees

PATCH Superseded JP Kobryn 5 participants 17 messages Mar 19, 2025 – Mar 26, 2025

Subsystems (9)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

JP Kobryn

author
#0 lore raw

v3 cover cgroup: separate rstat trees

The current design of rstat takes the approach that if one subsystem is to be flushed, all other subsystems with pending updates should also be flushed. A flush may be initiated by reading specific stats (like cpu.stat) and other subsystems will be flushed alongside. The complexity of flushing some subsystems has grown to the extent that the overhead of side flushes is unnecessarily delaying the fetching of desired stats.

One big area where the issue comes up is system telemetry, where programs periodically sample cpu stats while the memory controller is enabled. It would be a benefit for programs sampling cpu.stat if the overhead of having to flush memory (and also io) stats was eliminated. It would save cpu cycles for existing stat reader programs and improve scalability in terms of sampling frequency and host volume.

This series changes the approach of "flush all subsystems" to "flush only the requested subsystem". The core design change is moving from a unified model where rstat trees are shared by subsystems to having separate trees for each subsystem. On a per-cpu basis, there will be separate trees for each enabled subsystem if it implements css_rstat_flush and one tree for the base stats. In order to do this, the rstat list pointers were moved off of the cgroup and onto the css. In the transition, these pointer types were changed to cgroup_subsys_state. Finally the API for updated/flush was changed to accept a reference to a css instead of a cgroup. This allows for a specific subsystem to be associated with a given update or flush. The result is that rstat trees will now be made up of css nodes, and a given tree will only contain nodes associated with a specific subsystem.

Since separate trees will now be in use, the locking scheme was adjusted. The global locks were split up in such a way that there are separate locks for the base stats (cgroup::self) and also for each subsystem (memory, io, etc). This allows different subsystems (and base stats) to use rstat in parallel with no contention.

Breaking up the unified tree into separate trees eliminates the overhead and scalability issue explained in the first section, but comes at the expense of additional memory. In an effort to minimize this overhead, new rstat structs are introduced and a conditional allocation is performed. The cgroup_rstat_cpu which originally contained the rstat list pointers and the base stat entities was renamed cgroup_rstat_base_cpu. It is only allocated when the associated css is cgroup::self. As for non-self css's, a new compact struct was added that only contains the rstat list pointers. During initialization, when the given css is associated with an actual subsystem (not cgroup::self), this compact struct is allocated. With this conditional allocation, the change in memory overhead on a per-cpu basis before/after is shown below.

memory overhead before:
sizeof(struct cgroup_rstat_cpu) =~ 144 bytes /* can vary based on config */

nr_cgroups * sizeof(struct cgroup_rstat_cpu)
nr_cgroups * 144 bytes

memory overhead after:
sizeof(struct cgroup_rstat_cpu) == 16 bytes
sizeof(struct cgroup_rstat_base_cpu) =~ 144 bytes

nr_cgroups * (
sizeof(struct cgroup_rstat_base_cpu) +
sizeof(struct cgroup_rstat_cpu) * nr_rstat_controllers
)

nr_cgroups * (144 + 16 * nr_rstat_controllers)

... where nr_rstat_controllers is the number of enabled cgroup controllers that implement css_rstat_flush(). On a host where both memory and io are enabled:

nr_cgroups * (144 + 16 * 2)
nr_cgroups * 176 bytes

This leaves us with an increase in memory overhead of:
32 bytes per cgroup per cpu

Validation was performed by reading some *.stat files of a target parent cgroup while the system was under different workloads. A test program was made to loop 1M times, reading the files cgroup.stat, cpu.stat, io.stat, memory.stat of the parent cgroup each iteration. Using a non-patched kernel as control and this series as experimental, the findings show perf gains when reading stats with this series.

The first experiment consisted of a parent cgroup with memory.swap.max=0 and memory.max=1G. On a 52-cpu machine, 26 child cgroups were created and within each child cgroup a process was spawned to frequently update the memory cgroup stats by creating and then reading a file of size 1T (encouraging reclaim). The test program was run alongside these 26 tasks in parallel. The results showed time and perf gains for the reader test program.

test program elapsed time
control:

real    1m13.663s
user    0m0.948s
sys     1m12.356s

experiment:

real    0m42.498s
user    0m0.764s
sys     0m41.546s

test program perf
control:
31.75% mem_cgroup_css_rstat_flush
5.49% __blkcg_rstat_flush
0.10% cpu_stat_show
0.05% cgroup_base_stat_cputime_show

experiment:
8.60% mem_cgroup_css_rstat_flush
0.15% blkcg_print_stat
0.12% cgroup_base_stat_cputime_show

It's worth noting that memcg uses heuristics to optimize flushing. Depending on the state of updated stats at a given time, a memcg flush may be considered unnecessary and skipped as a result. This opportunity to skip a flush is bypassed when memcg is flushed as a consequence of sharing the tree with another controller.

A second experiment was setup on the same host using a parent cgroup with two child cgroups. The same swap and memory max were used as in the previous experiment. In the two child cgroups, kernel builds were done in parallel, each using "-j 20". The perf comparison is shown below.

test program elapsed time
control:

real    1m22.809s
user    0m1.142s
sys     1m21.138s

experiment:

real    0m42.504s
user    0m1.000s
sys     0m41.220s

test program perf
control:
37.16% mem_cgroup_css_rstat_flush
3.68% __blkcg_rstat_flush
0.09% cpu_stat_show
0.06% cgroup_base_stat_cputime_show

experiment:
2.02% mem_cgroup_css_rstat_flush
0.20% blkcg_print_stat
0.14% cpu_stat_show
0.08% cgroup_base_stat_cputime_show

The final experiment differs from the previous two in that it measures performance from the stat updater perspective. A kernel build was run in a child node with -j 20 on the same host and cgroup setup. A baseline was established by having the build run while no stats were read. The builds were then repeated while stats were constantly being read. In all cases, perf appeared similar in cycles spent on cgroup_rstat_updated() (insignificant compared to the other recorded events). As for the elapsed build times, the results of the different scenarios are shown below, indicating no significant drawbacks of the split tree approach.

control with no readers

real    5m12.307s
user    84m52.037s
sys     3m54.000s

control with constant readers of {memory,io,cpu,cgroup}.stat

real    5m13.209s
user    84m47.949s
sys     4m9.260s

experiment with no readers

real    5m11.961s
user    84m41.750s
sys     3m54.058s

experiment with constant readers of {memory,io,cpu,cgroup}.stat

real    5m12.626s
user    85m0.323s
sys     3m56.167s

changelog
v3:
new bpf kfunc api for updated/flush
rename cgroup_rstat_{updated,flush} and related to "css_rstat_*" check for ss->css_rstat_flush existence where applicable rename locks for base stats
move subsystem locks to cgroup_subsys struct
change cgroup_rstat_boot() to ss_rstat_init(ss) and init locks within change lock helpers to accept css and perform lock selection within fix comments that had outdated lock names
add open css_is_cgroup() helper
rename rstatc to rstatbc to reflect base stats in use rename cgroup_dfl_root_rstat_cpu to root_self_rstat_cpu add comments in early init code to explain deferred allocation misc formatting fixes

v2:
drop the patch creating a new cgroup_rstat struct and related code drop bpf-specific patches. instead just use cgroup::self in bpf progs drop the cpu lock patches. instead select cpu lock in updated_list func relocate the cgroup_rstat_init() call to inside css_create() relocate the cgroup_rstat_exit() cleanup from apply_control_enable()
to css_free_rwork_fn()
v1:

	https://lore.kernel.org/all/[email protected]/

JP Kobryn (4):
cgroup: separate rstat api for bpf programs
cgroup: use separate rstat trees for each subsystem
cgroup: use subsystem-specific rstat locks to avoid contention
cgroup: split up cgroup_rstat_cpu into base stat and non base stat
versions

 block/blk-cgroup.c                            |   6 +-
 include/linux/cgroup-defs.h                   |  80 ++--
 include/linux/cgroup.h                        |  16 +-
 include/trace/events/cgroup.h                 |  10 +-
 kernel/cgroup/cgroup-internal.h               |   6 +-
 kernel/cgroup/cgroup.c                        |  69 +--
 kernel/cgroup/rstat.c                         | 412 +++++++++++-------
 mm/memcontrol.c                               |   4 +-
 .../selftests/bpf/progs/btf_type_tag_percpu.c |   5 +-
 .../bpf/progs/cgroup_hierarchical_stats.c     |   8 +-
 10 files changed, 363 insertions(+), 253 deletions(-)
Signature · 1 line
2.47.1
author
#1 lore raw

PATCH v3 1/4 cgroup: use separate rstat api for bpf programs

The rstat updated/flush API functions are exported as kfuncs so bpf programs can make the same calls that in-kernel code can. Split these API functions into separate in-kernel and bpf versions. Function signatures remain unchanged. The kfuncs are named with the prefix "bpf_". This non-functional change allows for future commits which will modify the signature of the in-kernel API without impacting bpf call sites. The implementations of the kfuncs serve as adapters to the in-kernel API.

---
 include/linux/cgroup.h                        |  3 +++
 kernel/cgroup/rstat.c                         | 19 ++++++++++++++-----
 .../bpf/progs/cgroup_hierarchical_stats.c     |  8 ++++----
 3 files changed, 21 insertions(+), 9 deletions(-)
include/linux/cgroup.h +3 -0
index f8ef47f8a634..13fd82a4336d 100644
@@ -692,6 +692,9 @@ void cgroup_rstat_flush(struct cgroup *cgrp);
 void cgroup_rstat_flush_hold(struct cgroup *cgrp);
 void cgroup_rstat_flush_release(struct cgroup *cgrp);
 
+void bpf_cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
+void bpf_cgroup_rstat_flush(struct cgroup *cgrp);
+
 /*
  * Basic resource stats.
  */
kernel/cgroup/rstat.c +14 -5
index aac91466279f..0d66cfc53061 100644
@@ -82,7 +82,7 @@ void _cgroup_rstat_cpu_unlock(raw_spinlock_t *cpu_lock, int cpu,
  * rstat_cpu->updated_children list.  See the comment on top of
  * cgroup_rstat_cpu definition for details.
  */
-__bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu)
+void cgroup_rstat_updated(struct cgroup *cgrp, int cpu)
 {
 	raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu);
 	unsigned long flags;
@@ -129,6 +129,11 @@ __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu)
 	_cgroup_rstat_cpu_unlock(cpu_lock, cpu, cgrp, flags, true);
 }
 
+__bpf_kfunc void bpf_cgroup_rstat_updated(struct cgroup *cgrp, int cpu)
+{
+	cgroup_rstat_updated(cgrp, cpu);
+}
+
 /**
  * cgroup_rstat_push_children - push children cgroups into the given list
  * @head: current head of the list (= subtree root)
@@ -346,7 +351,7 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp)
  *
  * This function may block.
  */
-__bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp)
+void cgroup_rstat_flush(struct cgroup *cgrp)
 {
 	might_sleep();
 
@@ -355,6 +360,11 @@ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp)
 	__cgroup_rstat_unlock(cgrp, -1);
 }
 
+__bpf_kfunc void bpf_cgroup_rstat_flush(struct cgroup *cgrp)
+{
+	cgroup_rstat_flush(cgrp);
+}
+
 /**
  * cgroup_rstat_flush_hold - flush stats in @cgrp's subtree and hold
  * @cgrp: target cgroup
@@ -644,10 +654,9 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq)
 	cgroup_force_idle_show(seq, &cgrp->bstat);
 }
 
-/* Add bpf kfuncs for cgroup_rstat_updated() and cgroup_rstat_flush() */
 BTF_KFUNCS_START(bpf_rstat_kfunc_ids)
-BTF_ID_FLAGS(func, cgroup_rstat_updated)
-BTF_ID_FLAGS(func, cgroup_rstat_flush, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_cgroup_rstat_updated)
+BTF_ID_FLAGS(func, bpf_cgroup_rstat_flush, KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_rstat_kfunc_ids)
 
 static const struct btf_kfunc_id_set bpf_rstat_kfunc_set = {
tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c +4 -4
index c74362854948..24450dd4d3f3 100644
@@ -37,8 +37,8 @@ struct {
 	__type(value, struct attach_counter);
 } attach_counters SEC(".maps");
 
-extern void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) __ksym;
-extern void cgroup_rstat_flush(struct cgroup *cgrp) __ksym;
+extern void bpf_cgroup_rstat_updated(struct cgroup *cgrp, int cpu) __ksym;
+extern void bpf_cgroup_rstat_flush(struct cgroup *cgrp) __ksym;
 
 static uint64_t cgroup_id(struct cgroup *cgrp)
 {
@@ -75,7 +75,7 @@ int BPF_PROG(counter, struct cgroup *dst_cgrp, struct task_struct *leader,
 	else if (create_percpu_attach_counter(cg_id, 1))
 		return 0;
 
-	cgroup_rstat_updated(dst_cgrp, bpf_get_smp_processor_id());
+	bpf_cgroup_rstat_updated(dst_cgrp, bpf_get_smp_processor_id());
 	return 0;
 }
 
@@ -141,7 +141,7 @@ int BPF_PROG(dumper, struct bpf_iter_meta *meta, struct cgroup *cgrp)
 		return 1;
 
 	/* Flush the stats to make sure we get the most updated numbers */
-	cgroup_rstat_flush(cgrp);
+	bpf_cgroup_rstat_flush(cgrp);
 
 	total_counter = bpf_map_lookup_elem(&attach_counters, &cg_id);
 	if (!total_counter) {
Signature · 1 line
2.47.1
maintainer
#2 lore raw

Re: [PATCH 1/4 v3] cgroup: use separate rstat api for bpf programs

Quoted 7 lines · JP Kobryn

The rstat updated/flush API functions are exported as kfuncs so bpf programs can make the same calls that in-kernel code can. Split these API functions into separate in-kernel and bpf versions. Function signatures remain unchanged. The kfuncs are named with the prefix "bpf_". This non-functional change allows for future commits which will modify the signature of the in-kernel API without impacting bpf call sites. The implementations of the kfuncs serve as adapters to the in-kernel API.

This made me look up
https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations

The series reworks existing kfuncs anyway, is it necessary to have the bpf_ versions? The semantics is changed too from base+all subsystems flush to only base flush (bpf_rstat_flush()).

I'd perhaps do the changes freely w/out taking kfuncs into account and then add possible reconstructive patches towards the end of the series. (I'm not unsure whether the modified btf_type_tag_percpu.c selftest survives the latest unionization of base stats.)

Michal

author
#3 lore raw

Re: [PATCH 1/4 v3] cgroup: use separate rstat api for bpf programs

Quoted 9 lines · Michal Koutný
Quoted 7 lines · JP Kobryn

The rstat updated/flush API functions are exported as kfuncs so bpf programs can make the same calls that in-kernel code can. Split these API functions into separate in-kernel and bpf versions. Function signatures remain unchanged. The kfuncs are named with the prefix "bpf_". This non-functional change allows for future commits which will modify the signature of the in-kernel API without impacting bpf call sites. The implementations of the kfuncs serve as adapters to the in-kernel API.

Quoted 6 lines

This made me look up
https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations

The series reworks existing kfuncs anyway, is it necessary to have the bpf_ versions? The semantics is changed too from base+all subsystems flush to only base flush (bpf_rstat_flush()).

This patch was done based on some conversation in v2 around what to do with the kfuncs (bottom of [0]). It's true the kfunc API deviates from flush-specific-subsystem approach. Dropping this patch is fine by me and I assume it would be ok with Yosry (based on comments at end of [0]), but I'll wait and see if he has any input.

[0] https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

I'd perhaps do the changes freely w/out taking kfuncs into account and then add possible reconstructive patches towards the end of the series. (I'm not unsure whether the modified btf_type_tag_percpu.c selftest survives the latest unionization of base stats.)

Michal

#4 lore raw

Re: [PATCH 1/4 v3] cgroup: use separate rstat api for bpf programs

Quoted 17 lines · JP Kobryn
Quoted 9 lines · Michal Koutný
Quoted 7 lines · JP Kobryn

The rstat updated/flush API functions are exported as kfuncs so bpf programs can make the same calls that in-kernel code can. Split these API functions into separate in-kernel and bpf versions. Function signatures remain unchanged. The kfuncs are named with the prefix "bpf_". This non-functional change allows for future commits which will modify the signature of the in-kernel API without impacting bpf call sites. The implementations of the kfuncs serve as adapters to the in-kernel API.

Quoted 6 lines

This made me look up
https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations

The series reworks existing kfuncs anyway, is it necessary to have the bpf_ versions? The semantics is changed too from base+all subsystems flush to only base flush (bpf_rstat_flush()).

Quoted 5 lines

This patch was done based on some conversation in v2 around what to do with the kfuncs (bottom of [0]). It's true the kfunc API deviates from flush-specific-subsystem approach. Dropping this patch is fine by me and I assume it would be ok with Yosry (based on comments at end of [0]), but I'll wait and see if he has any input.

Yeah I am okay with dropping this as well.

Quoted 2 lines

[0] https://lore.kernel.org/all/[email protected]/

author
#5 lore raw

PATCH v3 2/4 cgroup: use separate rstat trees for each subsystem

In reply to JP Kobryn

Different subsystems may call cgroup_rstat_updated() within the same cgroup, resulting in a tree of pending updates from multiple subsystems. When one of these subsystems is flushed via cgroup_rstat_flushed(), all other subsystems with pending updates on the tree will also be flushed.

Change the paradigm of having a single rstat tree for all subsystems to having separate trees for each subsystem. This separation allows for subsystems to perform flushes without the side effects of other subsystems. As an example, flushing the cpu stats will no longer cause the memory stats to be flushed and vice versa.

In order to achieve subsystem-specific trees, change the tree node type from cgroup to cgroup_subsys_state pointer. Then remove those pointers from the cgroup and instead place them on the css. Finally, change the updated/flush API's to accept a reference to a css instead of a cgroup. This allows a specific subsystem to be associated with an update or flush. Separate rstat trees will now exist for each unique subsystem.

Since updating/flushing will now be done at the subsystem level, there is no longer a need to keep track of updated css nodes at the cgroup level. The list management of these nodes done within the cgroup (rstat_css_list and related) has been removed accordingly. There was also padding in the cgroup to keep rstat_css_list on a cacheline different from rstat_flush_next and the base stats. This padding has also been removed.

---
 block/blk-cgroup.c                            |   4 +-
 include/linux/cgroup-defs.h                   |  41 ++--
 include/linux/cgroup.h                        |  13 +-
 kernel/cgroup/cgroup-internal.h               |   4 +-
 kernel/cgroup/cgroup.c                        |  63 +++---
 kernel/cgroup/rstat.c                         | 212 +++++++++---------
 mm/memcontrol.c                               |   4 +-
 .../selftests/bpf/progs/btf_type_tag_percpu.c |   5 +-
 8 files changed, 177 insertions(+), 169 deletions(-)
block/blk-cgroup.c +2 -2
index 9ed93d91d754..cd9521f4f607 100644
@@ -1201,7 +1201,7 @@ static int blkcg_print_stat(struct seq_file *sf, void *v)
 	if (!seq_css(sf)->parent)
 		blkcg_fill_root_iostats();
 	else
-		cgroup_rstat_flush(blkcg->css.cgroup);
+		css_rstat_flush(&blkcg->css);
 
 	rcu_read_lock();
 	hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
@@ -2186,7 +2186,7 @@ void blk_cgroup_bio_start(struct bio *bio)
 	}
 
 	u64_stats_update_end_irqrestore(&bis->sync, flags);
-	cgroup_rstat_updated(blkcg->css.cgroup, cpu);
+	css_rstat_updated(&blkcg->css, cpu);
 	put_cpu();
 }
 
include/linux/cgroup-defs.h +15 -26

85 lines not on this page · raw message

include/linux/cgroup.h +9 -4
index 13fd82a4336d..4e71ae9858d3 100644
@@ -346,6 +346,11 @@ static inline bool css_is_dying(struct cgroup_subsys_state *css)
 	return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
 }
 
+static inline bool css_is_cgroup(struct cgroup_subsys_state *css)
+{
+	return css->ss == NULL;
+}
+
 static inline void cgroup_get(struct cgroup *cgrp)
 {
 	css_get(&cgrp->self);
@@ -687,10 +692,10 @@ static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
 /*
  * cgroup scalable recursive statistics.
  */
-void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
-void cgroup_rstat_flush(struct cgroup *cgrp);
-void cgroup_rstat_flush_hold(struct cgroup *cgrp);
-void cgroup_rstat_flush_release(struct cgroup *cgrp);
+void css_rstat_updated(struct cgroup_subsys_state *css, int cpu);
+void css_rstat_flush(struct cgroup_subsys_state *css);
+void css_rstat_flush_hold(struct cgroup_subsys_state *css);
+void css_rstat_flush_release(struct cgroup_subsys_state *css);
 
 void bpf_cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
 void bpf_cgroup_rstat_flush(struct cgroup *cgrp);
kernel/cgroup/cgroup-internal.h +2 -2
index c964dd7ff967..d4b75fba9a54 100644
@@ -269,8 +269,8 @@ int cgroup_task_count(const struct cgroup *cgrp);
 /*
  * rstat.c
  */
-int cgroup_rstat_init(struct cgroup *cgrp);
-void cgroup_rstat_exit(struct cgroup *cgrp);
+int css_rstat_init(struct cgroup_subsys_state *css);
+void css_rstat_exit(struct cgroup_subsys_state *css);
 void cgroup_rstat_boot(void);
 void cgroup_base_stat_cputime_show(struct seq_file *seq);
 
kernel/cgroup/cgroup.c +35 -28

180 lines not on this page · raw message

kernel/cgroup/rstat.c +109 -103

457 lines not on this page · raw message

mm/memcontrol.c +2 -2
index 4de6acb9b8ec..fe86d7efe372 100644
@@ -579,7 +579,7 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
 	if (!val)
 		return;
 
-	cgroup_rstat_updated(memcg->css.cgroup, cpu);
+	css_rstat_updated(&memcg->css, cpu);
 	statc = this_cpu_ptr(memcg->vmstats_percpu);
 	for (; statc; statc = statc->parent) {
 		stats_updates = READ_ONCE(statc->stats_updates) + abs(val);
@@ -611,7 +611,7 @@ static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force)
 	if (mem_cgroup_is_root(memcg))
 		WRITE_ONCE(flush_last_time, jiffies_64);
 
-	cgroup_rstat_flush(memcg->css.cgroup);
+	css_rstat_flush(&memcg->css);
 }
 
 /*
tools/testing/selftests/bpf/progs/btf_type_tag_percpu.c +3 -2
index 38f78d9345de..f362f7d41b9e 100644
@@ -45,7 +45,7 @@ int BPF_PROG(test_percpu2, struct bpf_testmod_btf_type_tag_2 *arg)
 SEC("tp_btf/cgroup_mkdir")
 int BPF_PROG(test_percpu_load, struct cgroup *cgrp, const char *path)
 {
-	g = (__u64)cgrp->rstat_cpu->updated_children;
+	g = (__u64)cgrp->self.rstat_cpu->updated_children;
 	return 0;
 }
 
@@ -56,7 +56,8 @@ int BPF_PROG(test_percpu_helper, struct cgroup *cgrp, const char *path)
 	__u32 cpu;
 
 	cpu = bpf_get_smp_processor_id();
-	rstat = (struct cgroup_rstat_cpu *)bpf_per_cpu_ptr(cgrp->rstat_cpu, cpu);
+	rstat = (struct cgroup_rstat_cpu *)bpf_per_cpu_ptr(
+			cgrp->self.rstat_cpu, cpu);
 	if (rstat) {
 		/* READ_ONCE */
 		*(volatile int *)rstat;
Signature · 1 line
2.47.1
maintainer
#6 lore raw

Re: [PATCH 2/4 v3] cgroup: use separate rstat trees for each subsystem

Hello,

Quoted 18 lines · JP Kobryn

Different subsystems may call cgroup_rstat_updated() within the same cgroup, resulting in a tree of pending updates from multiple subsystems. When one of these subsystems is flushed via cgroup_rstat_flushed(), all other subsystems with pending updates on the tree will also be flushed.

Change the paradigm of having a single rstat tree for all subsystems to having separate trees for each subsystem. This separation allows for subsystems to perform flushes without the side effects of other subsystems. As an example, flushing the cpu stats will no longer cause the memory stats to be flushed and vice versa.

In order to achieve subsystem-specific trees, change the tree node type from cgroup to cgroup_subsys_state pointer. Then remove those pointers from the cgroup and instead place them on the css. Finally, change the updated/flush API's to accept a reference to a css instead of a cgroup. This allows a specific subsystem to be associated with an update or flush. Separate rstat trees will now exist for each unique subsystem.

Quoted 6 lines

Since updating/flushing will now be done at the subsystem level, there is no longer a need to keep track of updated css nodes at the cgroup level. The list management of these nodes done within the cgroup (rstat_css_list and related) has been removed accordingly. There was also padding in the cgroup to keep rstat_css_list on a cacheline different from rstat_flush_next and the base stats. This padding has also been removed.

Overall, this looks okay but I think the patch should be split further. There's too much cgroup -> css renames mixed with actual changes which makes it difficult to understand what the actual changes are. Can you please separate it into a patch which makes everything css based but the actual queueing and flushing is still only on the cgroup css and then the next patch to actually split out linking and flushing to each css?

include/linux/cgroup.h static inline bool css_is_dying(struct cgroup_subsys_state *css) · line 352

Quoted 12 lines
include/linux/cgroup.h +4 -0
index 13fd82a4336d..4e71ae9858d3 100644
@@ -346,6 +346,11 @@ static inline bool css_is_dying(struct cgroup_subsys_state *css)
 	return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
 }
 
+static inline bool css_is_cgroup(struct cgroup_subsys_state *css)
+{
+	return css->ss == NULL;
+}

Maybe introduce this in a prep patch and replace existing users?

...

include/linux/cgroup.h static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 6082

Quoted 8 lines
include/linux/cgroup.h +3 -1
@@ -6082,11 +6077,16 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
 	css->flags |= CSS_NO_REF;
 
 	if (early) {
-		/* allocation can't be done safely during early init */
+		/* allocation can't be done safely during early init.
+		 * defer idr and rstat allocations until cgroup_init().
+		 */

Nit: Please use fully winged comment blocks for multilines with captalizations.

Thanks.

Signature · 1 line
tejun
author
#7 lore raw

Re: [PATCH 2/4 v3] cgroup: use separate rstat trees for each subsystem

Quoted 27 lines · Tejun Heo

Hello,

Quoted 18 lines · JP Kobryn

Different subsystems may call cgroup_rstat_updated() within the same cgroup, resulting in a tree of pending updates from multiple subsystems. When one of these subsystems is flushed via cgroup_rstat_flushed(), all other subsystems with pending updates on the tree will also be flushed.

Change the paradigm of having a single rstat tree for all subsystems to having separate trees for each subsystem. This separation allows for subsystems to perform flushes without the side effects of other subsystems. As an example, flushing the cpu stats will no longer cause the memory stats to be flushed and vice versa.

In order to achieve subsystem-specific trees, change the tree node type from cgroup to cgroup_subsys_state pointer. Then remove those pointers from the cgroup and instead place them on the css. Finally, change the updated/flush API's to accept a reference to a css instead of a cgroup. This allows a specific subsystem to be associated with an update or flush. Separate rstat trees will now exist for each unique subsystem.

Quoted 6 lines

Since updating/flushing will now be done at the subsystem level, there is no longer a need to keep track of updated css nodes at the cgroup level. The list management of these nodes done within the cgroup (rstat_css_list and related) has been removed accordingly. There was also padding in the cgroup to keep rstat_css_list on a cacheline different from rstat_flush_next and the base stats. This padding has also been removed.

Quoted 6 lines

Overall, this looks okay but I think the patch should be split further. There's too much cgroup -> css renames mixed with actual changes which makes it difficult to understand what the actual changes are. Can you please separate it into a patch which makes everything css based but the actual queueing and flushing is still only on the cgroup css and then the next patch to actually split out linking and flushing to each css?

Sure, no problem. I did something similar in the RFC series so I'll apply again here.

Quoted 8 lines
Quoted 8 lines
include/linux/cgroup.h +0 -0
index 13fd82a4336d..4e71ae9858d3 100644
@@ -346,6 +346,11 @@ static inline bool css_is_dying(struct cgroup_subsys_state *css)
  	return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
  }
 
Quoted 5 lines

include/linux/cgroup.h static inline bool css_is_dying(struct cgroup_subsys_state *css) · line 352

Quoted 4 lines
include/linux/cgroup.h +4 -0
+static inline bool css_is_cgroup(struct cgroup_subsys_state *css)
+{
+	return css->ss == NULL;
+}

Maybe introduce this in a prep patch and replace existing users?

Makes sense, will do.

Quoted 14 lines

...

include/linux/cgroup.h static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 6082

Quoted 8 lines
include/linux/cgroup.h +3 -1
@@ -6082,11 +6077,16 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
  	css->flags |= CSS_NO_REF;
 
  	if (early) {
-		/* allocation can't be done safely during early init */
+		/* allocation can't be done safely during early init.
+		 * defer idr and rstat allocations until cgroup_init().
+		 */

Nit: Please use fully winged comment blocks for multilines with captalizations.

Thanks.

maintainer
#8 lore raw

Re: [PATCH 2/4 v3] cgroup: use separate rstat trees for each subsystem

In reply to JP Kobryn

kernel/cgroup/cgroup.c static struct static_key_true *cgroup_subsys_on_dfl_key[] = { · line 164

Quoted 8 lines · JP Kobryn
kernel/cgroup/cgroup.c +1 -1
@@ -161,10 +161,12 @@ static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
 };
 #undef SUBSYS
 
-static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);
+static DEFINE_PER_CPU(struct css_rstat_cpu, root_self_rstat_cpu);

root_base_rstat_cpu
cgrp_dfl_root_base_rstat_cpu

(not a big deal, it's only referenced once next to definition)

Quoted 2 lines
kernel/cgroup/rstat.c +0 -0

...

kernel/cgroup/rstat.c __bpf_kfunc void bpf_cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 153

Quoted 5 lines
kernel/cgroup/rstat.c +3 -2
-static struct cgroup *cgroup_rstat_push_children(struct cgroup *head,
-						 struct cgroup *child, int cpu)
+static struct cgroup_subsys_state *cgroup_rstat_push_children(
+		struct cgroup_subsys_state *head,
+		struct cgroup_subsys_state *child, int cpu)

Forgotten rename?

tools/testing/selftests/bpf/progs/btf_type_tag_percpu.c int BPF_PROG(test_percpu2, struct bpf_testmod_btf_type_tag_2 *arg) · line 50

Quoted 12 lines
tools/testing/selftests/bpf/progs/btf_type_tag_percpu.c +1 -1
index 38f78d9345de..f362f7d41b9e 100644
@@ -45,7 +45,7 @@ int BPF_PROG(test_percpu2, struct bpf_testmod_btf_type_tag_2 *arg)
 SEC("tp_btf/cgroup_mkdir")
 int BPF_PROG(test_percpu_load, struct cgroup *cgrp, const char *path)
 {
-       g = (__u64)cgrp->rstat_cpu->updated_children;
+       g = (__u64)cgrp->self.rstat_cpu->updated_children;
        return 0;
 }

There are also some comments above needing an update.

Michal

author
#9 lore raw

PATCH v3 3/4 cgroup: use subsystem-specific rstat locks to avoid contention

In reply to JP Kobryn

It is possible to eliminate contention between subsystems when updating/flushing stats by using subsystem-specific locks. Let the existing rstat locks be dedicated to the base stats and rename them to reflect it. Add similar locks to the cgroup_subsys struct for use with individual subsystems. To make use of the new locks, change the existing lock helper functions to accept a reference to a css and use css->ss to access the locks or use the static locks for base stats when css->ss is NULL.

---
 block/blk-cgroup.c              |   2 +-
 include/linux/cgroup-defs.h     |  12 ++-
 include/trace/events/cgroup.h   |  10 ++-
 kernel/cgroup/cgroup-internal.h |   2 +-
 kernel/cgroup/cgroup.c          |  10 ++-
 kernel/cgroup/rstat.c           | 145 +++++++++++++++++++++-----------
 6 files changed, 122 insertions(+), 59 deletions(-)
block/blk-cgroup.c +1 -1
index cd9521f4f607..34d72bbdd5e5 100644
@@ -1022,7 +1022,7 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu)
 	/*
 	 * For covering concurrent parent blkg update from blkg_release().
 	 *
-	 * When flushing from cgroup, cgroup_rstat_lock is always held, so
+	 * When flushing from cgroup, the subsystem lock is always held, so
 	 * this lock won't cause contention most of time.
 	 */
 	raw_spin_lock_irqsave(&blkg_stat_lock, flags);
include/linux/cgroup-defs.h +10 -2
index 031f55a9ac49..0ffc8438c6d9 100644
@@ -223,7 +223,10 @@ struct cgroup_subsys_state {
 	/*
 	 * A singly-linked list of css structures to be rstat flushed.
 	 * This is a scratch field to be used exclusively by
-	 * cgroup_rstat_flush_locked() and protected by cgroup_rstat_lock.
+	 * cgroup_rstat_flush_locked().
+	 *
+	 * protected by rstat_base_lock when css is cgroup::self
+	 * protected by css->ss->lock otherwise
 	 */
 	struct cgroup_subsys_state *rstat_flush_next;
 };
@@ -391,7 +394,9 @@ struct css_rstat_cpu {
 	 * to the cgroup makes it unnecessary for each per-cpu struct to
 	 * point back to the associated cgroup.
 	 *
-	 * Protected by per-cpu cgroup_rstat_cpu_lock.
+	 * Protected by per-cpu rstat_base_cpu_lock when css->ss == NULL
+	 * otherwise,
+	 * Protected by per-cpu css->ss->rstat_cpu_lock
 	 */
 	struct cgroup_subsys_state *updated_children;	/* terminated by self */
 	struct cgroup_subsys_state *updated_next;	/* NULL if not on list */
@@ -779,6 +784,9 @@ struct cgroup_subsys {
 	 * specifies the mask of subsystems that this one depends on.
 	 */
 	unsigned int depends_on;
+
+	spinlock_t lock;
+	raw_spinlock_t __percpu *percpu_lock;
 };
 
 extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
include/trace/events/cgroup.h +8 -2
index af2755bda6eb..ec3a95bf4981 100644
@@ -231,7 +231,10 @@ DECLARE_EVENT_CLASS(cgroup_rstat,
 		  __entry->cpu, __entry->contended)
 );
 
-/* Related to global: cgroup_rstat_lock */
+/* Related to locks:
+ * rstat_base_lock when handling cgroup::self
+ * css->ss->lock otherwise
+ */
 DEFINE_EVENT(cgroup_rstat, cgroup_rstat_lock_contended,
 
 	TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
@@ -253,7 +256,10 @@ DEFINE_EVENT(cgroup_rstat, cgroup_rstat_unlock,
 	TP_ARGS(cgrp, cpu, contended)
 );
 
-/* Related to per CPU: cgroup_rstat_cpu_lock */
+/* Related to per CPU locks:
+ * rstat_base_cpu_lock when handling cgroup::self
+ * css->ss->cpu_lock otherwise
+ */
 DEFINE_EVENT(cgroup_rstat, cgroup_rstat_cpu_lock_contended,
 
 	TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
kernel/cgroup/cgroup-internal.h +1 -1
index d4b75fba9a54..513bfce3bc23 100644
@@ -271,7 +271,7 @@ int cgroup_task_count(const struct cgroup *cgrp);
  */
 int css_rstat_init(struct cgroup_subsys_state *css);
 void css_rstat_exit(struct cgroup_subsys_state *css);
-void cgroup_rstat_boot(void);
+int ss_rstat_init(struct cgroup_subsys *ss);
 void cgroup_base_stat_cputime_show(struct seq_file *seq);
 
 /*
kernel/cgroup/cgroup.c +7 -3
index 1e21065dec0e..3e8948805f67 100644
@@ -6085,8 +6085,10 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
 		css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
 		BUG_ON(css->id < 0);
 
-		if (ss->css_rstat_flush)
+		if (ss->css_rstat_flush) {
+			BUG_ON(ss_rstat_init(ss));
 			BUG_ON(css_rstat_init(css));
+		}
 	}
 
 	/* Update the init_css_set to contain a subsys
@@ -6163,7 +6165,7 @@ int __init cgroup_init(void)
 	BUG_ON(cgroup_init_cftypes(NULL, cgroup_psi_files));
 	BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
 
-	cgroup_rstat_boot();
+	BUG_ON(ss_rstat_init(NULL));
 
 	get_user_ns(init_cgroup_ns.user_ns);
 
@@ -6193,8 +6195,10 @@ int __init cgroup_init(void)
 						   GFP_KERNEL);
 			BUG_ON(css->id < 0);
 
-			if (ss->css_rstat_flush)
+			if (ss->css_rstat_flush) {
+				BUG_ON(ss_rstat_init(ss));
 				BUG_ON(css_rstat_init(css));
+			}
 		} else {
 			cgroup_init_subsys(ss, false);
 		}
kernel/cgroup/rstat.c +95 -50

284 lines not on this page · raw message

Signature · 1 line
2.47.1
maintainer
#10 lore raw

Re: [PATCH 3/4 v3] cgroup: use subsystem-specific rstat locks to avoid contention

include/linux/cgroup-defs.h struct cgroup_subsys { · line 789

Quoted 7 lines · JP Kobryn
include/linux/cgroup-defs.h +3 -0
@@ -779,6 +784,9 @@ struct cgroup_subsys {
 	 * specifies the mask of subsystems that this one depends on.
 	 */
 	unsigned int depends_on;
+
+	spinlock_t lock;
+	raw_spinlock_t __percpu *percpu_lock;

Please further qualify the names.

include/trace/events/cgroup.h DECLARE_EVENT_CLASS(cgroup_rstat, · line 237

Quoted 5 lines
include/trace/events/cgroup.h +4 -1
-/* Related to global: cgroup_rstat_lock */
+/* Related to locks:
+ * rstat_base_lock when handling cgroup::self
+ * css->ss->lock otherwise
+ */

Comment style, here and in other places.

Thanks.

Signature · 1 line
tejun
maintainer
#11 lore raw

Re: [PATCH 3/4 v3] cgroup: use subsystem-specific rstat locks to avoid contention

In reply to JP Kobryn
Quoted 4 lines · JP Kobryn
kernel/cgroup/rstat.c +0 -0
index a28c00b11736..ffd7ac6bcefc 100644

...

Quoted 5 lines
kernel/cgroup/rstat.c +5 -0
+static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss)
+{
+	if (ss)
+		return &ss->lock;
+

kernel/cgroup/rstat.c static struct css_rstat_cpu *css_rstat_cpu( · line 37

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+	return &rstat_base_lock;
+}
+
+static raw_spinlock_t *ss_rstat_cpu_lock(struct cgroup_subsys *ss, int cpu)
+{
+	if (ss)
+		return per_cpu_ptr(ss->percpu_lock, cpu);
+
+	return per_cpu_ptr(&rstat_base_cpu_lock, cpu);
+}

rstat_ss_lock
rstat_ss_cpu_lock

(judgment call only)

Michal

author
#12 lore raw

PATCH v3 4/4 cgroup: save memory by splitting cgroup_rstat_cpu into compact and full versions

In reply to JP Kobryn

The cgroup_rstat_cpu struct contains rstat node pointers and also the base stat objects. Since ownership of the cgroup_rstat_cpu has shifted from cgroup to cgroup_subsys_state, css's other than cgroup::self are now carrying along these base stat objects which go unused. Eliminate this wasted memory by splitting up cgroup_rstat_cpu into two separate structs.

The cgroup_rstat_cpu struct is modified in a way that it now contains only the rstat node pointers. css's that are associated with a subsystem (memory, io) use this compact struct to participate in rstat without the memory overhead of the base stat objects.

As for css's represented by cgroup::self, a new cgroup_rstat_base_cpu struct is introduced. It contains the new compact cgroup_rstat_cpu struct as its first field followed by the base stat objects.

Because the rstat pointers exist at the same offset (beginning) in both structs, cgroup_subsys_state is modified to contain a union of the two structs. Where css initialization is done, the compact struct is allocated when the css is associated with a subsystem. When the css is not associated with a subsystem, the full struct is allocated. The union allows the existing rstat updated/flush routines to work with any css regardless of subsystem association. The base stats routines however, were modified to access the full struct field in the union.

The change in memory on a per-cpu basis is shown below.

before:

    struct size
    sizeof(cgroup_rstat_cpu) =~ 144 bytes /* can vary based on config */

    per-cpu overhead
    nr_cgroups * (
	sizeof(cgroup_rstat_cpu) * (1 + nr_rstat_subsystems)
    )
    nr_cgroups * (144 * (1 + 2))
    nr_cgroups * 432

    432 bytes per cgroup per cpu

after:
struct sizes
sizeof(cgroup_rstat_base_cpu) =~ 144 bytes
sizeof(cgroup_rstat_cpu) = 16 bytes

per-cpu overhead
nr_cgroups * (
sizeof(cgroup_rstat_base_cpu) + sizeof(cgroup_rstat_cpu) * (nr_rstat_subsystems)
)
nr_cgroups * (144 + 16 * 2)
nr_cgroups * 176

176 bytes per cgroup per cpu

savings:
256 bytes per cgroup per cpu

---
 include/linux/cgroup-defs.h |  41 +++++++++------
 kernel/cgroup/rstat.c       | 100 ++++++++++++++++++++++--------------
 2 files changed, 86 insertions(+), 55 deletions(-)
include/linux/cgroup-defs.h +24 -17
index 0ffc8438c6d9..f9b84e7f718d 100644
@@ -170,7 +170,10 @@ struct cgroup_subsys_state {
 	struct percpu_ref refcnt;
 
 	/* per-cpu recursive resource statistics */
-	struct css_rstat_cpu __percpu *rstat_cpu;
+	union {
+		struct css_rstat_cpu __percpu *rstat_cpu;
+		struct css_rstat_base_cpu __percpu *rstat_base_cpu;
+	};
 
 	/*
 	 * siblings list anchored at the parent's ->children
@@ -358,6 +361,26 @@ struct cgroup_base_stat {
  * resource statistics on top of it - bsync, bstat and last_bstat.
  */
 struct css_rstat_cpu {
+	/*
+	 * Child cgroups with stat updates on this cpu since the last read
+	 * are linked on the parent's ->updated_children through
+	 * ->updated_next.
+	 *
+	 * In addition to being more compact, singly-linked list pointing
+	 * to the cgroup makes it unnecessary for each per-cpu struct to
+	 * point back to the associated cgroup.
+	 *
+	 * Protected by per-cpu rstat_base_cpu_lock when css->ss == NULL
+	 * otherwise,
+	 * Protected by per-cpu css->ss->rstat_cpu_lock
+	 */
+	struct cgroup_subsys_state *updated_children;	/* terminated by self */
+	struct cgroup_subsys_state *updated_next;	/* NULL if not on list */
+};
+
+struct css_rstat_base_cpu {
+	struct css_rstat_cpu rstat_cpu;
+
 	/*
 	 * ->bsync protects ->bstat.  These are the only fields which get
 	 * updated in the hot path.
@@ -384,22 +407,6 @@ struct css_rstat_cpu {
 	 * deltas to propagate to the per-cpu subtree_bstat.
 	 */
 	struct cgroup_base_stat last_subtree_bstat;
-
-	/*
-	 * Child cgroups with stat updates on this cpu since the last read
-	 * are linked on the parent's ->updated_children through
-	 * ->updated_next.
-	 *
-	 * In addition to being more compact, singly-linked list pointing
-	 * to the cgroup makes it unnecessary for each per-cpu struct to
-	 * point back to the associated cgroup.
-	 *
-	 * Protected by per-cpu rstat_base_cpu_lock when css->ss == NULL
-	 * otherwise,
-	 * Protected by per-cpu css->ss->rstat_cpu_lock
-	 */
-	struct cgroup_subsys_state *updated_children;	/* terminated by self */
-	struct cgroup_subsys_state *updated_next;	/* NULL if not on list */
 };
 
 struct cgroup_freezer_state {
kernel/cgroup/rstat.c +62 -38

182 lines not on this page · raw message

Signature · 1 line
2.47.1
maintainer
#13 lore raw

Re: [PATCH 4/4 v3] cgroup: save memory by splitting cgroup_rstat_cpu into compact and full versions

Hello,

Quoted 15 lines · JP Kobryn

The cgroup_rstat_cpu struct contains rstat node pointers and also the base stat objects. Since ownership of the cgroup_rstat_cpu has shifted from cgroup to cgroup_subsys_state, css's other than cgroup::self are now carrying along these base stat objects which go unused. Eliminate this wasted memory by splitting up cgroup_rstat_cpu into two separate structs.

The cgroup_rstat_cpu struct is modified in a way that it now contains only the rstat node pointers. css's that are associated with a subsystem (memory, io) use this compact struct to participate in rstat without the memory overhead of the base stat objects.

As for css's represented by cgroup::self, a new cgroup_rstat_base_cpu struct is introduced. It contains the new compact cgroup_rstat_cpu struct as its first field followed by the base stat objects.

Quoted 8 lines

Because the rstat pointers exist at the same offset (beginning) in both structs, cgroup_subsys_state is modified to contain a union of the two structs. Where css initialization is done, the compact struct is allocated when the css is associated with a subsystem. When the css is not associated with a subsystem, the full struct is allocated. The union allows the existing rstat updated/flush routines to work with any css regardless of subsystem association. The base stats routines however, were modified to access the full struct field in the union.

Can you do this as a part of prep patch? ie. Move the bstat out of rstat_cpu into the containing cgroup before switching to css based structure? It's rather odd to claim memory saving after bloating it up due to patch sequencing.

include/linux/cgroup-defs.h struct cgroup_subsys_state { · line 176

Quoted 13 lines
include/linux/cgroup-defs.h +4 -1
index 0ffc8438c6d9..f9b84e7f718d 100644
@@ -170,7 +170,10 @@ struct cgroup_subsys_state {
 	struct percpu_ref refcnt;
 
 	/* per-cpu recursive resource statistics */
-	struct css_rstat_cpu __percpu *rstat_cpu;
+	union {
+		struct css_rstat_cpu __percpu *rstat_cpu;
+		struct css_rstat_base_cpu __percpu *rstat_base_cpu;
+	};

I have a bit of difficult time following this. All that bstat is is the counters that the cgroup itself carries regardless of the subsystem but they would be collected and flushed the same way. Wouldn't that mean that the cgroup css should carry the same css_rstat_cpu as other csses but would have separate struct to carry the counters? Why is it multiplexed on css_rstat_cpu?

Thanks.

Signature · 1 line
tejun
author
#14 lore raw

Re: [PATCH 4/4 v3] cgroup: save memory by splitting cgroup_rstat_cpu into compact and full versions

Quoted 26 lines · Tejun Heo

Hello,

Quoted 15 lines · JP Kobryn

The cgroup_rstat_cpu struct contains rstat node pointers and also the base stat objects. Since ownership of the cgroup_rstat_cpu has shifted from cgroup to cgroup_subsys_state, css's other than cgroup::self are now carrying along these base stat objects which go unused. Eliminate this wasted memory by splitting up cgroup_rstat_cpu into two separate structs.

The cgroup_rstat_cpu struct is modified in a way that it now contains only the rstat node pointers. css's that are associated with a subsystem (memory, io) use this compact struct to participate in rstat without the memory overhead of the base stat objects.

As for css's represented by cgroup::self, a new cgroup_rstat_base_cpu struct is introduced. It contains the new compact cgroup_rstat_cpu struct as its first field followed by the base stat objects.

Quoted 8 lines

Because the rstat pointers exist at the same offset (beginning) in both structs, cgroup_subsys_state is modified to contain a union of the two structs. Where css initialization is done, the compact struct is allocated when the css is associated with a subsystem. When the css is not associated with a subsystem, the full struct is allocated. The union allows the existing rstat updated/flush routines to work with any css regardless of subsystem association. The base stats routines however, were modified to access the full struct field in the union.

Quoted 4 lines

Can you do this as a part of prep patch? ie. Move the bstat out of rstat_cpu into the containing cgroup before switching to css based structure? It's rather odd to claim memory saving after bloating it up due to patch sequencing.

I think it's possible. I'll give my thoughts in response to your question below.

Quoted 7 lines
Quoted 7 lines
include/linux/cgroup-defs.h +0 -0
index 0ffc8438c6d9..f9b84e7f718d 100644
@@ -170,7 +170,10 @@ struct cgroup_subsys_state {
  	struct percpu_ref refcnt;
 
Quoted 12 lines

include/linux/cgroup-defs.h struct cgroup_subsys_state { · line 176

Quoted 6 lines
include/linux/cgroup-defs.h +4 -1
  	/* per-cpu recursive resource statistics */
-	struct css_rstat_cpu __percpu *rstat_cpu;
+	union {
+		struct css_rstat_cpu __percpu *rstat_cpu;
+		struct css_rstat_base_cpu __percpu *rstat_base_cpu;
+	};

I have a bit of difficult time following this. All that bstat is is the counters that the cgroup itself carries regardless of the subsystem but they would be collected and flushed the same way. Wouldn't that mean that the cgroup css should carry the same css_rstat_cpu as other csses but would have separate struct to carry the counters? Why is it multiplexed on css_rstat_cpu?

Originally the idea came from wanting to avoid having extra objects (even if just one pointer) in the css that would not be used. This setup allowed for what felt like (to me at least) an organized way of keeping the objects used in rstat in the same container (css). You initialize everything within the css and also have consistency with the css-based rstat API's - using the css for updated/flushed allows you to get the needed rstat objects via css. So if we moved the base stat objects to the cgroup, we could where applicable check for css_is_cgroup() and then use css->cgroup to get the base stats which we kind of do anyway. I don't see a problem there. The only complication I think is on the init side, we would have to perform a separate percpu allocation for these base stats in the cgroup. I can give this a try for next rev unless anyone feels otherwise.

In general, I'll wait to see if Yosry, Michal, or Shakeel want any other changes before sending out the next rev.

Quoted 3 lines

Thanks.

maintainer
#15 lore raw

Re: [PATCH 4/4 v3] cgroup: save memory by splitting cgroup_rstat_cpu into compact and full versions

Quoted 2 lines · JP Kobryn

In general, I'll wait to see if Yosry, Michal, or Shakeel want any other changes before sending out the next rev.

Done.

Thanks,
Michal

maintainer
#16 lore raw

Re: [PATCH 4/4 v3] cgroup: save memory by splitting cgroup_rstat_cpu into compact and full versions

In reply to JP Kobryn

On Fri, Mar 21, 2025 at 10:45:29AM -0700, JP Kobryn wrote:
[...]

Quoted 4 lines

In general, I'll wait to see if Yosry, Michal, or Shakeel want any other changes before sending out the next rev.

All good from my side.