cgroup: separate rstat trees
Subsystems (8)
v5 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 causing noticeable delays in reading the 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 that implements css_rstat_flush plus one tree dedicated to 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 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 issues explained in the first section, but comes at the cost of additional memory. Originally, each cgroup contained an instance of the cgroup_rstat_cpu. The design change of moving to css-based trees calls for each css having the rstat per-cpu objects instead. Moving these objects to every css is where this overhead is created. In an effort to minimize this, the cgroup_rstat_cpu struct was split into two separate structs. One is the cgroup_rstat_base_cpu struct which only contains the per-cpu base stat objects used in rstat. The other is the css_rstat_cpu struct which contains the minimum amount of pointers needed for a css to participate in rstat. Since only the cgroup::self css is associated with the base stats, an instance of the cgroup_rstat_base_cpu struct is placed on the cgroup. Meanwhile an instance of the css_rstat_cpu is placed on the cgroup_subsys_state. This allows for all css's to participate in rstat while avoiding the unnecessary inclusion of the base stats. The base stat objects will only exist once per-cgroup regardless of however many subsystems are enabled. With this division of rstat list pointers and base stats, the change in memory overhead on a per-cpu basis before/after is shown below.
memory overhead before:
nr_cgroups * sizeof(struct cgroup_rstat_cpu)
where
sizeof(struct cgroup_rstat_cpu) = 144 bytes /* config-dependent */
resulting in
nr_cgroups * 144 bytes
memory overhead after:
nr_cgroups * ( sizeof(struct cgroup_rstat_base_cpu) + sizeof(struct css_rstat_cpu) * (1 + nr_rstat_controllers) )
where
sizeof(struct cgroup_rstat_base_cpu) = 128 bytes sizeof(struct css_rstat_cpu) = 16 bytes
the constant "1" accounts for the cgroup::self css nr_rstat_controllers = number of controllers defining css_rstat_flush
when both memory and io are enabled
nr_rstat_controllers = 2
resulting in
nr_cgroups * (128 + 16 * (1 + 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 1m29.929s user 0m0.933s sys 1m28.525s
experiment:
real 1m3.604s user 0m0.828s sys 1m2.497s
test program perf
control:
29.47% mem_cgroup_css_rstat_flush
5.09% __blkcg_rstat_flush
0.07% cpu_stat_show
experiment:
6.89% mem_cgroup_css_rstat_flush
0.31% blkcg_print_stat
0.07% cpu_stat_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. 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 1m59.647s user 0m1.263s sys 1m57.511s
experiment:
real 1m0.328s user 0m1.077s sys 0m58.834s
test program perf
control:
35.69% mem_cgroup_css_rstat_flush
4.49% __blkcg_rstat_flush
0.07% cpu_stat_show
0.05% cgroup_base_stat_cputime_show
experiment:
2.04% mem_cgroup_css_rstat_flush
0.18% blkcg_print_stat
0.09% cpu_stat_show
0.09% 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, showing no significant drawbacks of the split tree approach.
control with no readers
real 5m11.548s user 84m45.072s sys 3m52.069s
control with constant readers of {memory,io,cpu,cgroup}.stat
real 5m13.619s user 85m1.847s sys 4m5.379s
experiment with no readers
real 5m12.557s user 84m54.966s sys 3m53.383s
experiment with constant readers of {memory,io,cpu,cgroup}.stat
real 5m12.548s user 84m56.313s sys 3m54.955s
changelog
v5:
new patch for using css_is_cgroup() in more places
new patch adding is_css_rstat() helper
new patch documenting circumstances behind where css_rstat_init occurs check if css is cgroup early in css_rstat_flush()
remove ss->css_rstat_flush check in flush loop
fix css_rstat_flush where "pos" should be used instead of "css" change lockdep text in __css_rstat_lock/unlock()
remove unnecessary base lock init in ss_rstat_init()
guard against invalid css in css_rstat_updated/flush() guard against invalid css in css_rstat_init/exit()
call css_rstat_updated/flush and css_rstat_init/exit unconditionally consolidate calls to css_rstat_exit() into one (aside from error cases) eliminate call to css_rstat_init() in cgroup_init() for ss->early_init move comment changes to matching commits where applicable fix comment with mention of stale function css_rstat_flush_locked() fix comment referring to "cgroup" where "css" should be used
v4:
drop bpf api patch
drop cgroup_rstat_cpu split and union patch,
replace with patch for moving base stats into new struct
new patch for renaming rstat api's from cgroup_* to css_* new patch for adding css_is_cgroup() helper
rename ss->lock and ss->cpu_lock to ss->rstat_ss_lock and
ss->rstat_ss_cpu_lock respectively
rename root_self_stat_cpu to root_base_rstat_cpu rename cgroup_rstat_push_children to css_rstat_push_children format comments for consistency in wings and capitalization update comments in bpf selftests
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 (5):
cgroup: use helper for distingushing css in callbacks
cgroup: use separate rstat trees for each subsystem
cgroup: use subsystem-specific rstat locks to avoid contention
cgroup: helper for checking rstat participation of css
cgroup: document the rstat per-cpu initialization
block/blk-cgroup.c | 2 +- include/linux/cgroup-defs.h | 78 +++-- include/trace/events/cgroup.h | 12 +- kernel/cgroup/cgroup-internal.h | 2 +- kernel/cgroup/cgroup.c | 41 +-- kernel/cgroup/rstat.c | 310 +++++++++++------- .../selftests/bpf/progs/btf_type_tag_percpu.c | 18 +- 7 files changed, 289 insertions(+), 174 deletions(-)
Signature · 1 line
2.47.1
PATCH v5 1/5 cgroup: use helper for distingushing css in callbacks
The callbacks used for cleaning up css's check whether the css is associated with a subsystem or not. Instead of just checking the ss pointer, use the helper functions to better show the intention.
--- kernel/cgroup/cgroup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +2 -2
@@ -5406,7 +5406,7 @@ static void css_free_rwork_fn(struct work_struct *work)percpu_ref_exit (&css->refcnt); -if (ss) { +if (!css_is_cgroup (css)) {/* css free path */ struct cgroup_subsys_state *parent = css->parent;int id = css->id;
@@ -5460,7 +5460,7 @@ static void css_release_work_fn(struct work_struct *work) css->flags |=CSS_RELEASED ;list_del_rcu (&css->sibling); -if (ss) { +if (!css_is_cgroup (css)) {struct cgroup *parent_cgrp;/* css release path */
Signature · 1 line
2.47.1
Re: [PATCH v5 1/5] cgroup: use helper for distingushing css in callbacks
Quoted 5 lines · JP Kobryn
The callbacks used for cleaning up css's check whether the css is associated with a subsystem or not. Instead of just checking the ss pointer, use the helper functions to better show the intention.
Signed-off-by JP Kobryn <[email protected]>
Re: [PATCH v5 1/5] cgroup: use helper for distingushing css in callbacks
In reply to JP KobrynQuoted 5 lines · JP Kobryn
The callbacks used for cleaning up css's check whether the css is associated with a subsystem or not. Instead of just checking the ss pointer, use the helper functions to better show the intention.
Signed-off-by JP Kobryn <[email protected]>
I still think this should be renamed and potentially reimplemented to (also?) check css->cgroup->self, but anyway:
Quoted 30 lines
--- kernel/cgroup/cgroup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/cgroup/cgroup.c +2
-2@@ -5406,7 +5406,7 @@ static void css_free_rwork_fn(struct work_struct *work)percpu_ref_exit (&css->refcnt); -if (ss) { +if (!css_is_cgroup (css)) {/* css free path */ struct cgroup_subsys_state *parent = css->parent;int id = css->id;@@ -5460,7 +5460,7 @@ static void css_release_work_fn(struct work_struct *work) css->flags |=CSS_RELEASED ;list_del_rcu (&css->sibling); -if (ss) { +if (!css_is_cgroup (css)) {struct cgroup *parent_cgrp;/* css release path */ Signature · 1 line
2.47.1
Re: [PATCH v5 1/5] cgroup: use helper for distingushing css in callbacks
Quoted 9 lines · Yosry Ahmed
Quoted 5 lines · JP Kobryn
The callbacks used for cleaning up css's check whether the css is associated with a subsystem or not. Instead of just checking the ss pointer, use the helper functions to better show the intention.
Signed-off-by JP Kobryn <[email protected]>I still think this should be renamed and potentially reimplemented to (also?) check
css->cgroup->self, but anyway:
I'll make that impl change and rename within this patch next rev.
PATCH v5 2/5 cgroup: use separate rstat trees for each subsystem
In reply to JP KobrynDifferent 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 update/flush functions to make use of the different node type (css). These changes allow 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.
Conditional guards for checking validity of a given css were placed within css_rstat_updated/flush() to prevent undefined behavior occuring from kfunc usage in bpf programs. Guards were also placed within css_rstat_init/exit() in order to help consolidate calls to them. At call sites for all four functions, the existing guards were removed.
--- include/linux/cgroup-defs.h | 46 ++-- kernel/cgroup/cgroup.c | 34 +-- kernel/cgroup/rstat.c | 200 ++++++++++-------- .../selftests/bpf/progs/btf_type_tag_percpu.c | 18 +- 4 files changed, 160 insertions(+), 138 deletions(-)
include/linux/cgroup-defs.h +21 -25
@@ -169,6 +169,8 @@ struct cgroup_subsys_state {/* reference count - access via css_[try]get() and css_put() */ struct percpu_ref refcnt; +struct css_rstat_cpu __percpu *rstat_cpu; +/* * siblings list anchored at the parent's ->children *
@@ -177,9 +179,6 @@ struct cgroup_subsys_state {struct list_head sibling;struct list_head children; -/* flush target list anchored at cgrp->rstat_css_list */ -struct list_head rstat_css_node; -/* * PI: Subsys-unique ID. 0 is unused and root is always 1. The * matching css can be looked up using css_from_id().
@@ -219,6 +218,13 @@ struct cgroup_subsys_state {* Protected by cgroup_mutex. */ int nr_descendants; + +/* +* A singly-linked list of css structures to be rstat flushed. +* This is a scratch field to be used exclusively by +* css_rstat_flush() and protected by cgroup_rstat_lock. +*/ +struct cgroup_subsys_state *rstat_flush_next; };/*
@@ -329,10 +335,10 @@ struct cgroup_base_stat {/* * rstat - cgroup scalable recursive statistics. Accounting is done -* per-cpu in cgroup_rstat_cpu which is then lazily propagated up the +* per-cpu in css_rstat_cpu which is then lazily propagated up the * hierarchy on reads. * -* When a stat gets updated, the cgroup_rstat_cpu and its ancestors are +* When a stat gets updated, the css_rstat_cpu and its ancestors are * linked into the updated tree. On the following read, propagation only * considers and consumes the updated tree. This makes reading O(the * number of descendants which have been active since last read) instead of
@@ -346,20 +352,20 @@ struct cgroup_base_stat {* This struct hosts both the fields which implement the above - * updated_children and updated_next. */ -struct cgroup_rstat_cpu { +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. +* ->updated_next. updated_children is terminated by its container css. * -* 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. +* In addition to being more compact, singly-linked list pointing to +* the css makes it unnecessary for each per-cpu struct to point back +* to the associated css. * * Protected by per-cpu cgroup_rstat_cpu_lock. */ -struct cgroup *updated_children;/* terminated by self cgroup */ -struct cgroup *updated_next;/* NULL iff not on the list */ +struct cgroup_subsys_state *updated_children; +struct cgroup_subsys_state *updated_next;/* NULL if not on the list */ };/*
@@ -521,25 +527,15 @@ struct cgroup {struct cgroup *dom_cgrp;struct cgroup *old_dom_cgrp;/* used while enabling threaded */ -/* per-cpu recursive resource statistics */ -struct cgroup_rstat_cpu __percpu *rstat_cpu;struct cgroup_rstat_base_cpu __percpu *rstat_base_cpu; -struct list_head rstat_css_list;/* -* Add padding to separate the read mostly rstat_cpu and -* rstat_css_list into a different cacheline from the following -* rstat_flush_next and *bstat fields which can have frequent updates. +* Add padding to keep the read mostly rstat per-cpu pointer on a +* different cacheline than the following *bstat fields which can have +* frequent updates. */ CACHELINE_PADDING (_pad_); -/* -* A singly-linked list of cgroup structures to be rstat flushed. -* This is a scratch field to be used exclusively by -* css_rstat_flush_locked() and protected by cgroup_rstat_lock. -*/ -struct cgroup *rstat_flush_next; -/* cgroup basic resource statistics */ struct cgroup_base_stat last_bstat;struct cgroup_base_stat bstat;
kernel/cgroup/cgroup.c +11 -23
@@ -161,12 +161,12 @@ static struct static_key_true *cgroup_subsys_on_dfl_key[] = { };#undef SUBSYS -static DEFINE_PER_CPU(struct cgroup_rstat_cpu, root_rstat_cpu); +static DEFINE_PER_CPU(struct css_rstat_cpu, root_rstat_cpu); static DEFINE_PER_CPU(struct cgroup_rstat_base_cpu, root_rstat_base_cpu);/* the default hierarchy */ struct cgroup_root cgrp_dfl_root = { - .cgrp.rstat_cpu = &root_rstat_cpu, + .cgrp.self.rstat_cpu = &root_rstat_cpu, .cgrp.rstat_base_cpu = &root_rstat_base_cpu, };EXPORT_SYMBOL_GPL (cgrp_dfl_root);
@@ -1362,7 +1362,6 @@ static void cgroup_destroy_root(struct cgroup_root *root)cgroup_unlock (); -css_rstat_exit (&cgrp->self);kernfs_destroy_root (root->kf_root);cgroup_free_root (root); }
@@ -1867,13 +1866,6 @@ int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) }spin_unlock_irq (&css_set_lock); -if (ss->css_rstat_flush) { -list_del_rcu (&css->rstat_css_node); -synchronize_rcu (); -list_add_rcu (&css->rstat_css_node, - &dcgrp->rstat_css_list); - } -/* default hierarchy doesn't enable controllers by default */ dst_root->subsys_mask |=1 << ssid;if (dst_root == &cgrp_dfl_root) {
@@ -2056,7 +2048,6 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp) cgrp->dom_cgrp = cgrp; cgrp->max_descendants =INT_MAX ; cgrp->max_depth =INT_MAX ; -INIT_LIST_HEAD (&cgrp->rstat_css_list);prev_cputime_init (&cgrp->prev_cputime);for_each_subsys (ss, ssid)
@@ -5405,6 +5396,7 @@ static void css_free_rwork_fn(struct work_struct *work)struct cgroup *cgrp = css->cgroup;percpu_ref_exit (&css->refcnt); +css_rstat_exit (css);if (!css_is_cgroup (css)) {/* css free path */
@@ -5435,7 +5427,6 @@ static void css_free_rwork_fn(struct work_struct *work)cgroup_put (cgroup_parent (cgrp));kernfs_put (cgrp->kn);psi_cgroup_free (cgrp); -css_rstat_exit (css);kfree (cgrp); }else {/*
@@ -5463,11 +5454,7 @@ static void css_release_work_fn(struct work_struct *work)if (!css_is_cgroup (css)) {struct cgroup *parent_cgrp; -/* css release path */ -if (!list_empty (&css->rstat_css_node)) { -css_rstat_flush (css); -list_del_rcu (&css->rstat_css_node); - } +css_rstat_flush (css);cgroup_idr_replace (&ss->css_idr,NULL , css->id);if (ss->css_released)
@@ -5493,7 +5480,7 @@ static void css_release_work_fn(struct work_struct *work)/* cgroup release path */ TRACE_CGROUP_PATH (release, cgrp); -css_rstat_flush (css); +css_rstat_flush (&cgrp->self);spin_lock_irq (&css_set_lock); for (tcgrp = cgroup_parent(cgrp); tcgrp;
@@ -5541,7 +5528,6 @@ static void init_and_link_css(struct cgroup_subsys_state *css, css->id =-1 ;INIT_LIST_HEAD (&css->sibling);INIT_LIST_HEAD (&css->children); -INIT_LIST_HEAD (&css->rstat_css_node); css->serial_nr = css_serial_nr_next++;atomic_set (&css->online_cnt,0 );
@@ -5550,9 +5536,6 @@ static void init_and_link_css(struct cgroup_subsys_state *css,css_get (css->parent); } -if (ss->css_rstat_flush) -list_add_rcu (&css->rstat_css_node, &cgrp->rstat_css_list); -BUG_ON (cgroup_css (cgrp, ss)); }
@@ -5645,6 +5628,10 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, goto err_free_css; css->id = err; + err =css_rstat_init (css); +if (err) + goto err_free_css; +/* @css is ready to be brought online now, make it visible */ list_add_tail_rcu (&css->sibling, &parent_css->children);cgroup_idr_replace (&ss->css_idr, css, css->id);
@@ -5658,7 +5645,6 @@ static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, err_list_del:list_del_rcu (&css->sibling); err_free_css: -list_del_rcu (&css->rstat_css_node);INIT_RCU_WORK (&css->destroy_rwork, css_free_rwork_fn);queue_rcu_work (cgroup_destroy_wq, &css->destroy_rwork);return ERR_PTR (err);
@@ -6101,6 +6087,8 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) }else { css->id =cgroup_idr_alloc (&ss->css_idr, css,1 ,2 ,GFP_KERNEL );BUG_ON (css->id <0 ); + +BUG_ON (css_rstat_init (css)); }/* Update the init_css_set to contain a subsys
kernel/cgroup/rstat.c +116 -84
366 lines not on this page · raw message
tools/testing/selftests/bpf/progs/btf_type_tag_percpu.c +12 -6
@@ -30,22 +30,27 @@ int BPF_PROG(test_percpu2, struct bpf_testmod_btf_type_tag_2 *arg)/* trace_cgroup_mkdir(struct cgroup *cgrp, const char *path) * -* struct cgroup_rstat_cpu { +* struct css_rstat_cpu { * ... -* struct cgroup *updated_children; +* struct cgroup_subsys_state *updated_children; * ... * }; * -* struct cgroup { +* struct cgroup_subsys_state { +* ... +* struct css_rstat_cpu __percpu *rstat_cpu; * ... -* struct cgroup_rstat_cpu __percpu *rstat_cpu; +* }; +* +* struct cgroup { +* struct cgroup_subsys_state self; * ... * }; */ 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 +61,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
Re: [PATCH v5 2/5] cgroup: use separate rstat trees for each subsystem
Quoted 36 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 viacgroup_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_statepointer. Then remove those pointers from the cgroup and instead place them on the css. Finally, change update/flush functions to make use of the different node type (css). These changes allow 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_listand related) has been removed accordingly.Conditional guards for checking validity of a given css were placed within css_rstat_updated/flush() to prevent undefined behavior occuring from kfunc usage in bpf programs. Guards were also placed within css_rstat_init/exit() in order to help consolidate calls to them. At call sites for all four functions, the existing guards were removed.
Signed-off-by JP Kobryn <[email protected]>--- include/linux/cgroup-defs.h | 46 ++-- kernel/cgroup/cgroup.c | 34 +-- kernel/cgroup/rstat.c | 200 ++++++++++-------- .../selftests/bpf/progs/btf_type_tag_percpu.c | 18 +- 4 files changed, 160 insertions(+), 138 deletions(-)
[..]
kernel/cgroup/cgroup.c static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 6091
Quoted 6 lines
kernel/cgroup/cgroup.c +2
-0@@ -6101,6 +6087,8 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) }else { css->id =cgroup_idr_alloc (&ss->css_idr, css,1 ,2 ,GFP_KERNEL );BUG_ON (css->id <0 ); + +BUG_ON (css_rstat_init (css));
We call css_rstat_init() here for subsys css's that are not early initialized, and in cgroup_setup_root() self css's. We can probably move both calls into cgroup_init() as I mentioned earlier?
Also, I think this version just skips calling css_rstat_init() for early initialized subsys css's, without adding the patch that you talked about earlier which protects against early initialized subsystems using rstat.
Quoted 3 lines
kernel/cgroup/cgroup.c +0
-0}/* Update the init_css_set to contain a subsys
[..]
kernel/cgroup/rstat.c static struct cgroup *cgroup_rstat_push_children(struct cgroup *head, · line 228
Quoted 7 lines
kernel/cgroup/rstat.c +1
-2@@ -217,31 +225,32 @@ static struct cgroup *cgroup_rstat_push_children(struct cgroup *head, }/** -* cgroup_rstat_updated_list - return a list of updated cgroups to be flushed -* @root: root of the cgroup subtree to traverse +* css_rstat_updated_list - return a list of updated cgroups to be flushed
css's?
Quoted 19 lines
kernel/cgroup/rstat.c +4
-3+* @root: root of the css subtree to traverse * @cpu: target cpu * Return: A singly linked list of cgroups to be flushed * * Walks the updated rstat_cpu tree on @cpu from @root. During traversal, -* each returned cgroup is unlinked from the updated tree. +* each returned css is unlinked from the updated tree. * * The only ordering guarantee is that, for a parent and a child pair * covered by a given traversal, the child is before its parent in * the list. * * Note that updated_children is self terminated and points to a list of -* child cgroups if not empty. Whereas updated_next is like a sibling link -* within the children list and terminated by the parent cgroup. An exception +* child css's if not empty. Whereas updated_next is like a sibling link +* within the children list and terminated by the parent css. An exception * here is the cgroup root whose updated_next can be self terminated. */
[..]
Quoted 7 lines
kernel/cgroup/rstat.c +1
-1@@ -383,32 +395,45 @@ __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)int css_rstat_init (struct cgroup_subsys_state *css) { -struct cgroup *cgrp = css->cgroup; +struct cgroup *cgrp;int cpu;
kernel/cgroup/rstat.c __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css) · line 403
Quoted 10 lines
kernel/cgroup/rstat.c +3
-6+bool is_cgroup =css_is_cgroup (css); -/* the root cgrp has rstat_cpu preallocated */ -if (!cgrp->rstat_cpu) { - cgrp->rstat_cpu = alloc_percpu(struct cgroup_rstat_cpu); -if (!cgrp->rstat_cpu) -return -ENOMEM ; - } +if (is_cgroup) { + cgrp = css->cgroup;
You can keep 'cgrp' initialized at the top of the function to avoid the extra level of indentation here, right?
kernel/cgroup/rstat.c __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css) · line 412
Quoted 12 lines
kernel/cgroup/rstat.c +7
-3-if (!cgrp->rstat_base_cpu) { - cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu); +/* the root cgrp has rstat_base_cpu preallocated */ if (!cgrp->rstat_base_cpu) { -free_percpu (cgrp->rstat_cpu); + cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu); +if (!cgrp->rstat_base_cpu) +return -ENOMEM ; + } + }else if (css->ss->css_rstat_flush ==NULL ) +return 0 ;
We can probably just do this at the beginning of the function to be able to use the helper:
if (!css_is_cgroup(css) && css->ss->css_rstat_flush == NULL) return 0;
Also, when the return value of css_is_cgroup() is cached as is_cgroup it makes me hate the function name even more, because 'is_cgroup' is very confusing for a css in my opinion since they all represent cgroups.
I really think this should be css_is_self() (or css_is_self_cgroup()) and the variable names would be 'is_self'.
Quoted 32 lines
kernel/cgroup/rstat.c +17
-5+ +/* the root cgrp's self css has rstat_cpu preallocated */ +if (!css->rstat_cpu) { + css->rstat_cpu = alloc_percpu(struct css_rstat_cpu); +if (!css->rstat_cpu) { +if (is_cgroup) +free_percpu (cgrp->rstat_base_cpu); +return -ENOMEM ; } }/* ->updated_children list is self terminated */ for_each_possible_cpu (cpu) { -struct cgroup_rstat_cpu *rstatc =cgroup_rstat_cpu (cgrp, cpu); -struct cgroup_rstat_base_cpu *rstatbc = -cgroup_rstat_base_cpu (cgrp, cpu); +struct css_rstat_cpu *rstatc =css_rstat_cpu (css, cpu); - rstatc->updated_children = cgrp; -u64_stats_init (&rstatbc->bsync); + rstatc->updated_children = css; + +if (is_cgroup) { +struct cgroup_rstat_base_cpu *rstatbc; + + rstatbc =cgroup_rstat_base_cpu (cgrp, cpu); +u64_stats_init (&rstatbc->bsync); + } }return 0 ;
[..]
Re: [PATCH v5 2/5] cgroup: use separate rstat trees for each subsystem
Quoted 1 line · Yosry Ahmed
On Fri, May 02, 2025 at 05:12:19PM -0700, JP Kobryn wrote:
[..]
Quoted 10 lines
kernel/cgroup/cgroup.c static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 6091
Quoted 6 lines
kernel/cgroup/cgroup.c +2
-0@@ -6101,6 +6087,8 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) }else { css->id =cgroup_idr_alloc (&ss->css_idr, css,1 ,2 ,GFP_KERNEL );BUG_ON (css->id <0 ); + +BUG_ON (css_rstat_init (css));We call
css_rstat_init()here for subsys css's that are not early initialized, and incgroup_setup_root()self css's. We can probably move both calls intocgroup_init()as I mentioned earlier?
I think it should stay here for two reasons. The first is because it
must precede the call to online_css(). There is a stated assumption that css->css_online() is called (within online_css()) "after cgrp
successfully completed all allocations" in the admin-guide. There is an
example of this assumption in memcg_cgroup_css_online() in which the
work for periodic flushing is enqueued. So if online_css() is called
before css_rstat_init() there would be a race between flushing and initializing rstat for the css. The second reason is that calling css_rstat_init() on a subsystem css
must also follow the call to init_and_link_css(), or else the css will
not have any subsystem association at the point of rstat init. It would
result in a subsystem css appearing to be the cgroup::self css since the cgroup_subsys_state::ss field would be NULL.
Quoted 5 lines
Also, I think this version just skips calling
css_rstat_init()for early initialized subsys css's, without adding the patch that you talked about earlier which protects against early initialized subsystems using rstat.
Right, that was a pre-existing constraint but I'll create that patch and prepend it to this series in the next rev.
Re: [PATCH v5 2/5] cgroup: use separate rstat trees for each subsystem
In reply to Yosry AhmedQuoted 1 line · Yosry Ahmed
On Fri, May 02, 2025 at 05:12:19PM -0700, JP Kobryn wrote:
[..]
Quoted 9 lines
Quoted 9 lines
kernel/cgroup/rstat.c +2
-1@@ -383,32 +395,45 @@ __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)int css_rstat_init (struct cgroup_subsys_state *css) { -struct cgroup *cgrp = css->cgroup; +struct cgroup *cgrp;int cpu; +bool is_cgroup =css_is_cgroup (css);
Quoted 12 lines
kernel/cgroup/rstat.c __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css) · line 403
Quoted 8 lines
kernel/cgroup/rstat.c +2
-6-/* the root cgrp has rstat_cpu preallocated */ -if (!cgrp->rstat_cpu) { - cgrp->rstat_cpu = alloc_percpu(struct cgroup_rstat_cpu); -if (!cgrp->rstat_cpu) -return -ENOMEM ; - } +if (is_cgroup) { + cgrp = css->cgroup;You can keep 'cgrp' initialized at the top of the function to avoid the extra level of indentation here, right?
I can move this initialization to the top but the indentation of this conditional branch will remain. See more thoughts below related to this block.
Quoted 16 lines
kernel/cgroup/rstat.c __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css) · line 412
Quoted 12 lines
kernel/cgroup/rstat.c +7
-3-if (!cgrp->rstat_base_cpu) { - cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu); +/* the root cgrp has rstat_base_cpu preallocated */ if (!cgrp->rstat_base_cpu) { -free_percpu (cgrp->rstat_cpu); + cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu); +if (!cgrp->rstat_base_cpu) +return -ENOMEM ; + } + }else if (css->ss->css_rstat_flush ==NULL ) +return 0 ;We can probably just do this at the beginning of the function to be able to use the helper:
Quoted 2 lines
if (!css_is_cgroup(css) && css->ss->css_rstat_flush == NULL) return 0;
If we did this we would be wasting extra memory allocating the base stats for every subsystem css. The intention behind the way I wrote it was to optimize the memory overhead needed for split rstat trees.
PATCH v5 3/5 cgroup: use subsystem-specific rstat locks to avoid contention
In reply to JP KobrynIt 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 cgroup base stats and rename them to reflect that. Add similar locks to the cgroup_subsys struct for use with individual subsystems.
Lock initialization is done in the new function ss_rstat_init(ss) which replaces cgroup_rstat_boot(void). If NULL is passed to this function, the global base stat locks will be initialized. Otherwise, the subsystem locks will be initialized.
Change the existing lock helper functions to accept a reference to a css. Then within these functions, conditionally select the appropriate locks based on the subsystem affiliation of the given css. Add helper functions for this selection routine to avoid repeated code.
--- block/blk-cgroup.c | 2 +- include/linux/cgroup-defs.h | 10 +++- include/trace/events/cgroup.h | 12 +++- kernel/cgroup/cgroup-internal.h | 2 +- kernel/cgroup/cgroup.c | 3 +- kernel/cgroup/rstat.c | 103 ++++++++++++++++++++++---------- 6 files changed, 95 insertions(+), 37 deletions(-)
block/blk-cgroup.c +1 -1
@@ -1074,7 +1074,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 +8 -2
@@ -222,7 +222,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 -* css_rstat_flush() and protected by cgroup_rstat_lock. +* css_rstat_flush(). +* +* Protected by rstat_base_lock when css is cgroup::self. +* Protected by css->ss->rstat_ss_lock otherwise. */ struct cgroup_subsys_state *rstat_flush_next; };
@@ -362,7 +365,7 @@ struct css_rstat_cpu {* the css makes it unnecessary for each per-cpu struct to point back * to the associated css. * -* Protected by per-cpu cgroup_rstat_cpu_lock. +* Protected by per-cpu css->ss->rstat_ss_cpu_lock. */ struct cgroup_subsys_state *updated_children;struct cgroup_subsys_state *updated_next;/* NULL if not on the list */
@@ -792,6 +795,9 @@ struct cgroup_subsys {* specifies the mask of subsystems that this one depends on. */ unsigned int depends_on; + +spinlock_t rstat_ss_lock; +raw_spinlock_t __percpu *rstat_ss_cpu_lock; };extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
include/trace/events/cgroup.h +10 -2
@@ -231,7 +231,11 @@ DECLARE_EVENT_CLASS(cgroup_rstat, __entry->cpu, __entry->contended) ); -/* Related to global: cgroup_rstat_lock */ +/* +* Related to locks: +* global rstat_base_lock for base stats +* cgroup_subsys::rstat_ss_lock for subsystem stats +*/ DEFINE_EVENT(cgroup_rstat, cgroup_rstat_lock_contended, TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),
@@ -253,7 +257,11 @@ 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: +* global rstat_base_cpu_lock for base stats +* cgroup_subsys::rstat_ss_cpu_lock for subsystem stats +*/ 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
@@ -272,7 +272,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 +2 -1
@@ -6088,6 +6088,7 @@ 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 ); +BUG_ON (ss_rstat_init (ss));BUG_ON (css_rstat_init (css)); }
@@ -6165,7 +6166,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);
kernel/cgroup/rstat.c +73 -30
224 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH v5 3/5] cgroup: use subsystem-specific rstat locks to avoid contention
Quoted 31 lines · 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 cgroup base stats and rename them to reflect that. Add similar locks to the
cgroup_subsysstruct for use with individual subsystems.Lock initialization is done in the new function
ss_rstat_init(ss)which replacescgroup_rstat_boot(void). If NULL is passed to this function, the global base stat locks will be initialized. Otherwise, the subsystem locks will be initialized.Change the existing lock helper functions to accept a reference to a css. Then within these functions, conditionally select the appropriate locks based on the subsystem affiliation of the given css. Add helper functions for this selection routine to avoid repeated code.
Signed-off-by JP Kobryn <[email protected]>--- block/blk-cgroup.c | 2 +- include/linux/cgroup-defs.h | 10 +++- include/trace/events/cgroup.h | 12 +++- kernel/cgroup/cgroup-internal.h | 2 +- kernel/cgroup/cgroup.c | 3 +- kernel/cgroup/rstat.c | 103 ++++++++++++++++++++++---------- 6 files changed, 95 insertions(+), 37 deletions(-)block/blk-cgroup.c +0
-0
block/blk-cgroup.c static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu) · line 1077
Quoted 5 lines
block/blk-cgroup.c +1
-1@@ -1074,7 +1074,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
nit: subsystem rstat lock?
Quoted 86 lines
block/blk-cgroup.c +0
-0* this lock won't cause contention most of time. */ raw_spin_lock_irqsave (&blkg_stat_lock, flags);include/linux/cgroup-defs.h +8
-2@@ -222,7 +222,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 -* css_rstat_flush() and protected by cgroup_rstat_lock. +* css_rstat_flush(). +* +* Protected by rstat_base_lock when css is cgroup::self. +* Protected by css->ss->rstat_ss_lock otherwise. */ struct cgroup_subsys_state *rstat_flush_next; };@@ -362,7 +365,7 @@ struct css_rstat_cpu {* the css makes it unnecessary for each per-cpu struct to point back * to the associated css. * -* Protected by per-cpu cgroup_rstat_cpu_lock. +* Protected by per-cpu css->ss->rstat_ss_cpu_lock. */ struct cgroup_subsys_state *updated_children;struct cgroup_subsys_state *updated_next;/* NULL if not on the list */ @@ -792,6 +795,9 @@ struct cgroup_subsys {* specifies the mask of subsystems that this one depends on. */ unsigned int depends_on; + +spinlock_t rstat_ss_lock; +raw_spinlock_t __percpu *rstat_ss_cpu_lock; };extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;include/trace/events/cgroup.h +10
-2@@ -231,7 +231,11 @@ DECLARE_EVENT_CLASS(cgroup_rstat, __entry->cpu, __entry->contended) ); -/* Related to global: cgroup_rstat_lock */ +/* +* Related to locks: +* global rstat_base_lock for base stats +* cgroup_subsys::rstat_ss_lock for subsystem stats +*/ DEFINE_EVENT(cgroup_rstat, cgroup_rstat_lock_contended, TP_PROTO(struct cgroup *cgrp, int cpu, bool contended),@@ -253,7 +257,11 @@ 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: +* global rstat_base_cpu_lock for base stats +* cgroup_subsys::rstat_ss_cpu_lock for subsystem stats +*/ 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@@ -272,7 +272,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 +0
-0
kernel/cgroup/cgroup.c static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) · line 6092
Quoted 5 lines
kernel/cgroup/cgroup.c +1
-0@@ -6088,6 +6088,7 @@ 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 ); +BUG_ON (ss_rstat_init (ss));BUG_ON (css_rstat_init (css));
If we move the css_rstat_init() call to cgroup_init(), let's also move ss_rstat_init().
Quoted 239 lines
kernel/cgroup/cgroup.c +1
-1}@@ -6165,7 +6166,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);kernel/cgroup/rstat.c +65
-29@@ -9,8 +9,8 @@#include <trace/events/cgroup.h> -static DEFINE_SPINLOCK (cgroup_rstat_lock); -static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); +static DEFINE_SPINLOCK (rstat_base_lock); +static DEFINE_PER_CPU(raw_spinlock_t, rstat_base_cpu_lock);static void cgroup_base_stat_flush (struct cgroup *cgrp,int cpu);@@ -26,8 +26,24 @@ static struct cgroup_rstat_base_cpu *cgroup_rstat_base_cpu(return per_cpu_ptr (cgrp->rstat_base_cpu, cpu); } +static spinlock_t *ss_rstat_lock (struct cgroup_subsys *ss) +{ +if (ss) +return &ss->rstat_ss_lock; + +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->rstat_ss_cpu_lock, cpu); + +return per_cpu_ptr (&rstat_base_cpu_lock, cpu); +} +/* -* Helper functions for rstat per CPU lock (cgroup_rstat_cpu_lock). +* Helper functions for rstat per CPU locks. * * This makes it easier to diagnose locking issues and contention in * production environments. The parameter @fast_path determine the @@ -35,21 +51,23 @@ static struct cgroup_rstat_base_cpu *cgroup_rstat_base_cpu(* operations without handling high-frequency fast-path "update" events. */ static __always_inline -unsigned long _css_rstat_cpu_lock (raw_spinlock_t *cpu_lock,int cpu, -struct cgroup_subsys_state *css,const bool fast_path) +unsigned long _css_rstat_cpu_lock (struct cgroup_subsys_state *css,int cpu, +const bool fast_path) {struct cgroup *cgrp = css->cgroup; +raw_spinlock_t *cpu_lock;unsigned long flags;bool contended;/* -* The _irqsave() is needed because cgroup_rstat_lock is -* spinlock_t which is a sleeping lock on PREEMPT_RT. Acquiring -* this lock with the _irq() suffix only disables interrupts on -* a non-PREEMPT_RT kernel. The raw_spinlock_t below disables -* interrupts on both configurations. The _irqsave() ensures -* that interrupts are always disabled and later restored. +* The _irqsave() is needed because the locks used for flushing are +* spinlock_t which is a sleeping lock on PREEMPT_RT. Acquiring this lock +* with the _irq() suffix only disables interrupts on a non-PREEMPT_RT +* kernel. The raw_spinlock_t below disables interrupts on both +* configurations. The _irqsave() ensures that interrupts are always +* disabled and later restored. */ + cpu_lock =ss_rstat_cpu_lock (css->ss, cpu); contended = !raw_spin_trylock_irqsave (cpu_lock, flags);if (contended) {if (fast_path)@@ -69,17 +87,18 @@ unsigned long _css_rstat_cpu_lock(raw_spinlock_t *cpu_lock, int cpu, }static __always_inline -void _css_rstat_cpu_unlock(raw_spinlock_t *cpu_lock, int cpu, -struct cgroup_subsys_state *css,unsigned long flags, -const bool fast_path) +void _css_rstat_cpu_unlock(struct cgroup_subsys_state *css, int cpu, +unsigned long flags,const bool fast_path) {struct cgroup *cgrp = css->cgroup; +raw_spinlock_t *cpu_lock;if (fast_path)trace_cgroup_rstat_cpu_unlock_fastpath (cgrp, cpu, false);else trace_cgroup_rstat_cpu_unlock (cgrp, cpu, false); + cpu_lock =ss_rstat_cpu_lock (css->ss, cpu);raw_spin_unlock_irqrestore (cpu_lock, flags); }@@ -94,7 +113,6 @@ void _css_rstat_cpu_unlock(raw_spinlock_t *cpu_lock, int cpu,*/ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) { -raw_spinlock_t *cpu_lock =per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu);unsigned long flags;/* @@ -115,7 +133,7 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)if (data_race (css_rstat_cpu (css, cpu)->updated_next))return ; - flags =_css_rstat_cpu_lock (cpu_lock, cpu, css, true); + flags =_css_rstat_cpu_lock (css, cpu, true);/* put @css and all ancestors on the corresponding updated lists */ while (true) {@@ -143,7 +161,7 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu) css = parent; } -_css_rstat_cpu_unlock (cpu_lock, cpu, css, flags, true); +_css_rstat_cpu_unlock (css, cpu, flags, true); }/** @@ -171,11 +189,11 @@ static struct cgroup_subsys_state *css_rstat_push_children( child->rstat_flush_next =NULL ;/* -* The cgroup_rstat_lock must be held for the whole duration from +* The subsystem rstat lock must be held for the whole duration from * here as the rstat_flush_next list is being constructed to when * it is consumed later in css_rstat_flush(). */ -lockdep_assert_held (&cgroup_rstat_lock); +lockdep_assert_held (ss_rstat_lock (head->ss));/* * Notation: -> updated_next pointer @@ -245,12 +263,11 @@ static struct cgroup_subsys_state *css_rstat_push_children(static struct cgroup_subsys_state *css_rstat_updated_list (struct cgroup_subsys_state *root,int cpu) { -raw_spinlock_t *cpu_lock =per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu);struct css_rstat_cpu *rstatc =css_rstat_cpu (root, cpu);struct cgroup_subsys_state *head =NULL , *parent, *child;unsigned long flags; - flags =_css_rstat_cpu_lock (cpu_lock, cpu, root, false); + flags =_css_rstat_cpu_lock (root, cpu, false);/* Return NULL if this subtree is not on-list */ if (!rstatc->updated_next)@@ -287,7 +304,7 @@ static struct cgroup_subsys_state *css_rstat_updated_list(if (child != root) head =css_rstat_push_children (head, child, cpu); unlock_ret: -_css_rstat_cpu_unlock (cpu_lock, cpu, root, flags, false); +_css_rstat_cpu_unlock (root, cpu, flags, false);return head; }@@ -314,7 +331,7 @@ __weak noinline void bpf_rstat_flush(struct cgroup *cgrp,__bpf_hook_end ();/* -* Helper functions for locking cgroup_rstat_lock. +* Helper functions for locking. * * This makes it easier to diagnose locking issues and contention in * production environments. The parameter @cpu_in_loop indicate lock @@ -324,27 +341,31 @@ __bpf_hook_end();*/ static inline void __css_rstat_lock (struct cgroup_subsys_state *css,int cpu_in_loop) - __acquires(&cgroup_rstat_lock) + __acquires(ss_rstat_lock(css->ss)) {struct cgroup *cgrp = css->cgroup; +spinlock_t *lock;bool contended; - contended = !spin_trylock_irq (&cgroup_rstat_lock); + lock =ss_rstat_lock (css->ss); + contended = !spin_trylock_irq (lock);if (contended) {trace_cgroup_rstat_lock_contended (cgrp, cpu_in_loop, contended); -spin_lock_irq (&cgroup_rstat_lock); +spin_lock_irq (lock); }trace_cgroup_rstat_locked (cgrp, cpu_in_loop, contended); }static inline void __css_rstat_unlock (struct cgroup_subsys_state *css,int cpu_in_loop) - __releases(&cgroup_rstat_lock) + __releases(ss_rstat_lock(css->ss)) {struct cgroup *cgrp = css->cgroup; +spinlock_t *lock; + lock =ss_rstat_lock (css->ss);trace_cgroup_rstat_unlock (cgrp, cpu_in_loop, false); -spin_unlock_irq (&cgroup_rstat_lock); +spin_unlock_irq (lock); }/** @@ -468,12 +489,34 @@ void css_rstat_exit(struct cgroup_subsys_state *css) css->rstat_cpu =NULL ; } -void __init cgroup_rstat_boot (void ) +/** +* ss_rstat_init - subsystem-specific rstat initialization +* @ss: target subsystem +* +* If @ss is NULL, the static locks associated with the base stats +* are initialized. If @ss is non-NULL, the subsystem-specific locks +* are initialized. +*/ +int __init ss_rstat_init (struct cgroup_subsys *ss) {int cpu; +if (!ss) { +for_each_possible_cpu (cpu) +raw_spin_lock_init (per_cpu_ptr (&rstat_base_cpu_lock, cpu)); + +return 0 ; + } +
kernel/cgroup/rstat.c void css_rstat_exit(struct cgroup_subsys_state *css) · line 519
Quoted 10 lines
kernel/cgroup/rstat.c +8
-1+spin_lock_init (&ss->rstat_ss_lock); + ss->rstat_ss_cpu_lock =alloc_percpu (raw_spinlock_t); +if (!ss->rstat_ss_cpu_lock) +return -ENOMEM ; +for_each_possible_cpu (cpu) -raw_spin_lock_init (per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu)); +raw_spin_lock_init (per_cpu_ptr (ss->rstat_ss_cpu_lock, cpu)); + +return 0 ;
We can probably get away with a single per-CPU loop and unify some more code:
int __init ss_rstat_init(struct cgroup_subsys *ss) {
int cpu;
if (ss) {
ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
if (!ss->rstat_ss_cpu_lock)
return -ENOMEM;
}
spin_lock_init(ss_rstat_lock(ss));
for_each_possible_cpu(cpu)
raw_spin_lock_init(ss_rstat_cpu_lock(ss, cpu));
return 0;
}Does this work?
PATCH v5 4/5 cgroup: helper for checking rstat participation of css
In reply to JP KobrynThere are a few places where a conditional check is performed to validate a given css on its rstat participation. This new helper tries to make the code more readable where this check is performed.
--- kernel/cgroup/rstat.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
kernel/cgroup/rstat.c +14 -3
@@ -14,6 +14,17 @@ static DEFINE_PER_CPU(raw_spinlock_t, rstat_base_cpu_lock);static void cgroup_base_stat_flush (struct cgroup *cgrp,int cpu); +/* +* Determines whether a given css can participate in rstat. +* css's that are cgroup::self use rstat for base stats. +* Other css's associated with a subsystem use rstat when they +* define the ss->css_rstat_flush callback. +*/ +static inline bool is_rstat_css (struct cgroup_subsys_state *css) +{ +return css_is_cgroup (css) || css->ss->css_rstat_flush !=NULL ; +} +static struct css_rstat_cpu *css_rstat_cpu (struct cgroup_subsys_state *css,int cpu) {
@@ -119,7 +130,7 @@ __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)* Since bpf programs can call this function, prevent access to * uninitialized rstat pointers. */ -if (!css_is_cgroup (css) && css->ss->css_rstat_flush ==NULL ) +if (!is_rstat_css (css))return ;/*
@@ -390,7 +401,7 @@ __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)* Since bpf programs can call this function, prevent access to * uninitialized rstat pointers. */ -if (!is_cgroup && css->ss->css_rstat_flush ==NULL ) +if (!is_rstat_css (css))return ;might_sleep ();
@@ -464,7 +475,7 @@ void css_rstat_exit(struct cgroup_subsys_state *css) {int cpu; -if (!css_is_cgroup (css) && css->ss->css_rstat_flush ==NULL ) +if (!is_rstat_css (css))return ;css_rstat_flush (css);
Signature · 1 line
2.47.1
Re: [PATCH v5 4/5] cgroup: helper for checking rstat participation of css
Quoted 14 lines · JP Kobryn
There are a few places where a conditional check is performed to validate a given css on its rstat participation. This new helper tries to make the code more readable where this check is performed.
Signed-off-by JP Kobryn <[email protected]>--- kernel/cgroup/rstat.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)kernel/cgroup/rstat.c +0
-0
kernel/cgroup/rstat.c static DEFINE_PER_CPU(raw_spinlock_t, rstat_base_cpu_lock); · line 23
Quoted 10 lines
kernel/cgroup/rstat.c +7
-0@@ -14,6 +14,17 @@ static DEFINE_PER_CPU(raw_spinlock_t, rstat_base_cpu_lock);static void cgroup_base_stat_flush (struct cgroup *cgrp,int cpu); +/* +* Determines whether a given css can participate in rstat. +* css's that are cgroup::self use rstat for base stats. +* Other css's associated with a subsystem use rstat when they +* define the ss->css_rstat_flush callback. +*/ +static inline bool is_rstat_css (struct cgroup_subsys_state *css)
css_uses_rstat() is probably a better name.
PATCH v5 5/5 cgroup: document the rstat per-cpu initialization
In reply to JP KobrynThe calls to css_rstat_init() occur at different places depending on the context. Document the conditions that determine which point of initialization is used.
--- include/linux/cgroup-defs.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
include/linux/cgroup-defs.h +24 -0
@@ -169,6 +169,21 @@ struct cgroup_subsys_state {/* reference count - access via css_[try]get() and css_put() */ struct percpu_ref refcnt; +/* +* Depending on the context, this field is initialized +* via css_rstat_init() at different places: +* +* when css is associated with cgroup::self +* when css->cgroup is the root cgroup +* cgroup_init() +* when css->cgroup is not the root cgroup +* cgroup_create() +* when css is associated with a subsystem +* when css->cgroup is the root cgroup +* cgroup_init_subsys() in the non-early path +* when css->cgroup is not the root cgroup +* css_create() +*/ struct css_rstat_cpu __percpu *rstat_cpu;/*
@@ -530,6 +545,15 @@ struct cgroup {struct cgroup *dom_cgrp;struct cgroup *old_dom_cgrp;/* used while enabling threaded */ +/* +* Depending on the context, this field is initialized via +* css_rstat_init() at different places: +* +* when cgroup is the root cgroup +* cgroup_setup_root() +* otherwise +* cgroup_create() +*/ struct cgroup_rstat_base_cpu __percpu *rstat_base_cpu;/*
Signature · 1 line
2.47.1