cgroup: separate per-subsystem rstat trees
Subsystems (5)
v2 cover cgroup: separate per-subsystem rstat trees
The current rstat model is set up to keep track of cgroup stats on a per-cpu basis. When a stat (of any subsystem) is updated, the updater notes this change using the cgroup_rstat_updated() API call. This change is propagated to the cpu-specific rstat tree, by appending the updated cgroup to the tree (unless it's already on the tree). So for each cpu, an rstat tree will consist of the cgroups that reported one or more updated stats. Later on when a flush is requested via cgroup_rstat_flush(), each per-cpu rstat tree is traversed starting at the requested cgroup and the subsystem-specific flush callbacks (via css_rstat_flush) are invoked along the way. During the flush, the section of the tree starting at the requested cgroup through its descendants are removed.
Using the cgroup struct to represent nodes of change means that the changes represented by a given tree are heterogeneous - the tree can consist of nodes that have changes from different subsystems; i.e. changes in stats from the memory subsystem and the io subsystem can coexist in the same tree. The implication is that when a flush is requested, usually in the context of a single subsystem, all other subsystems need to be flushed along with it. This seems to have become a drawback due to how expensive the flushing of the memory-specific stats have become [0][1]. Another implication is when updates are performed, subsystems may contend with each other over the locks involved.
I've been experimenting with an idea that allows for isolating the updating and flushing of cgroup stats on a per-subsystem basis. The idea was instead of having a per-cpu rstat tree for managing stats across all subsystems, we could split up the per-cpu trees into separate trees for each subsystem. So each cpu would have separate trees for each subsystem. It would allow subsystems to update and flush their stats without any contention or extra overhead from other subsystems. The core change is moving ownership of the the rstat entities from the cgroup struct onto the cgroup_subsystem_state struct.
To complement the ownership change, the lockng scheme was adjusted. The global cgroup_rstat_lock for synchronizing updates and flushes was replaced with subsystem-specific locks (in the cgroup_subsystem struct). An additional global lock was added to allow the base stats pseudo-subsystem to be synchronized in a similar way. The per-cpu locks called cgroup_rstat_cpu_lock have changed to a per-cpu array of locks which is indexed by subsystem id. Following suit, there is also a per-cpu array of locks dedicated to the base subsystem. The dedicated locks for the base stats was added since the base stats have a NULL subsystem so it did not fit the subsystem id index approach.
I reached a point where this started to feel stable in my local testing, so I wanted to share and get feedback on this approach.
[0] https://lore.kernel.org/all/CAOm-9arwY3VLUx5189JAR9J7B=Miad9nQjjet_VNdT3i+J+5FA@mail.gmail.com/
[1] https://github.blog/engineering/debugging-network-stalls-on-kubernetes/
Changelog
v2: updated cover letter and some patch text. no code changes.
JP Kobryn (8):
change cgroup to css in rstat updated and flush api change cgroup to css in rstat internal flush and lock funcs change cgroup to css in rstat init and exit api split rstat from cgroup into separate css separate locking between base css and others isolate base stat flush
remove unneeded rcu list
remove bpf rstat flush from css generic flush
block/blk-cgroup.c | 4 +- include/linux/cgroup-defs.h | 35 ++--- include/linux/cgroup.h | 8 +- kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup.c | 79 ++++++----- kernel/cgroup/rstat.c | 225 +++++++++++++++++++------------- mm/memcontrol.c | 4 +- 7 files changed, 203 insertions(+), 156 deletions(-)
Signature · 1 line
2.47.1
RFC PATCH v2 1/9 cgroup: change cgroup to css in rstat updated and flush api
Change rstat flush and updated API calls to accept a cgroup_subsys_state instead of a cgroup.
--- block/blk-cgroup.c | 4 ++-- include/linux/cgroup.h | 8 ++++---- kernel/cgroup/cgroup.c | 4 ++-- kernel/cgroup/rstat.c | 28 +++++++++++++++++++--------- mm/memcontrol.c | 4 ++-- 5 files changed, 29 insertions(+), 19 deletions(-)
block/blk-cgroup.c +2 -2
@@ -1200,7 +1200,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); +cgroup_rstat_flush (&blkcg->css);rcu_read_lock ();hlist_for_each_entry_rcu (blkg, &blkcg->blkg_list, blkcg_node) {
@@ -2183,7 +2183,7 @@ void blk_cgroup_bio_start(struct bio *bio) }u64_stats_update_end_irqrestore (&bis->sync, flags); -cgroup_rstat_updated (blkcg->css.cgroup, cpu); +cgroup_rstat_updated (&blkcg->css, cpu);put_cpu (); }
include/linux/cgroup.h +4 -4
@@ -687,10 +687,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 cgroup_rstat_updated (struct cgroup_subsys_state *css,int cpu); +void cgroup_rstat_flush (struct cgroup_subsys_state *css); +void cgroup_rstat_flush_hold (struct cgroup_subsys_state *css); +void cgroup_rstat_flush_release (struct cgroup_subsys_state *css);/* * Basic resource stats.
kernel/cgroup/cgroup.c +2 -2
@@ -5465,7 +5465,7 @@ static void css_release_work_fn(struct work_struct *work)/* css release path */ if (!list_empty (&css->rstat_css_node)) { -cgroup_rstat_flush (cgrp); +cgroup_rstat_flush (css);list_del_rcu (&css->rstat_css_node); }
@@ -5493,7 +5493,7 @@ static void css_release_work_fn(struct work_struct *work)/* cgroup release path */ TRACE_CGROUP_PATH (release, cgrp); -cgroup_rstat_flush (cgrp); +cgroup_rstat_flush (css);spin_lock_irq (&css_set_lock); for (tcgrp = cgroup_parent(cgrp); tcgrp;
kernel/cgroup/rstat.c +19 -9
@@ -82,8 +82,9 @@ 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) +__bpf_kfunc void cgroup_rstat_updated(struct cgroup_subsys_state *css, int cpu) { +struct cgroup *cgrp = css->cgroup;raw_spinlock_t *cpu_lock =per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu);unsigned long flags;
@@ -346,8 +347,10 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp)* * This function may block. */ -__bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) +__bpf_kfunc void cgroup_rstat_flush(struct cgroup_subsys_state *css) { +struct cgroup *cgrp = css->cgroup; +might_sleep ();__cgroup_rstat_lock (cgrp,-1 );
@@ -364,9 +367,11 @@ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp)* * This function may block. */ -void cgroup_rstat_flush_hold (struct cgroup *cgrp) +void cgroup_rstat_flush_hold (struct cgroup_subsys_state *css) __acquires(&cgroup_rstat_lock) { +struct cgroup *cgrp = css->cgroup; +might_sleep ();__cgroup_rstat_lock (cgrp,-1 );cgroup_rstat_flush_locked (cgrp);
@@ -376,9 +381,11 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp)* cgroup_rstat_flush_release - release cgroup_rstat_flush_hold() * @cgrp: cgroup used by tracepoint */ -void cgroup_rstat_flush_release (struct cgroup *cgrp) +void cgroup_rstat_flush_release (struct cgroup_subsys_state *css) __releases(&cgroup_rstat_lock) { +struct cgroup *cgrp = css->cgroup; +__cgroup_rstat_unlock (cgrp,-1 ); }
@@ -408,7 +415,7 @@ void cgroup_rstat_exit(struct cgroup *cgrp) {int cpu; -cgroup_rstat_flush (cgrp); +cgroup_rstat_flush (&cgrp->self);/* sanity check */ for_each_possible_cpu (cpu) {
@@ -512,8 +519,10 @@ static void cgroup_base_stat_cputime_account_end(struct cgroup *cgrp, struct cgroup_rstat_cpu *rstatc, unsigned long flags) { +struct cgroup_subsys_state *css = &cgrp->self; +u64_stats_update_end_irqrestore (&rstatc->bsync, flags); -cgroup_rstat_updated (cgrp,smp_processor_id ()); +cgroup_rstat_updated (css,smp_processor_id ());put_cpu_ptr (rstatc); }
@@ -612,16 +621,17 @@ static void cgroup_force_idle_show(struct seq_file *seq, struct cgroup_base_statvoid cgroup_base_stat_cputime_show (struct seq_file *seq) { -struct cgroup *cgrp =seq_css (seq)->cgroup; +struct cgroup_subsys_state *css =seq_css (seq); +struct cgroup *cgrp = css->cgroup;u64 usage, utime, stime, ntime;if (cgroup_parent (cgrp)) { -cgroup_rstat_flush_hold (cgrp); +cgroup_rstat_flush_hold (css); usage = cgrp->bstat.cputime.sum_exec_runtime;cputime_adjust (&cgrp->bstat.cputime, &cgrp->prev_cputime, &utime, &stime); ntime = cgrp->bstat.ntime; -cgroup_rstat_flush_release (cgrp); +cgroup_rstat_flush_release (css); }else {/* cgrp->bstat of root is not actually used, reuse it */ root_cgroup_cputime (&cgrp->bstat);
mm/memcontrol.c +2 -2
@@ -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); +cgroup_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); +cgroup_rstat_flush (&memcg->css); }/*
Signature · 1 line
2.47.1
kernel test robot
build botRe: [RFC PATCH 1/9 v2] cgroup: change cgroup to css in rstat updated and flush api
kernel test robot noticed the following build warnings:
Hi JP,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on tj-cgroup/for-next]
[also build test WARNING on akpm-mm/mm-everything axboe-block/for-next linus/master v6.13-rc5 next-20241220] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/JP-Kobryn/cgroup-change-cgroup-to-css-in-rstat-updated-and-flush-api/20250103-095314 base: https://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next patch link: https://lore.kernel.org/r/20250103015020.78547-2-inwardvessel%40gmail.com patch subject: [RFC PATCH 1/9 v2] cgroup: change cgroup to css in rstat updated and flush api config: i386-buildonly-randconfig-003-20250103 (https://download.01.org/0day-ci/archive/20250103/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250103/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/cgroup/rstat.c:86: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_updated' >> kernel/cgroup/rstat.c:86: warning: Excess function parameter 'cgrp' description in 'cgroup_rstat_updated' >> kernel/cgroup/rstat.c:351: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_flush' >> kernel/cgroup/rstat.c:351: warning: Excess function parameter 'cgrp' description in 'cgroup_rstat_flush'
kernel/cgroup/rstat.c:372: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_flush_hold' kernel/cgroup/rstat.c:372: warning: Excess function parameter 'cgrp' description in 'cgroup_rstat_flush_hold' kernel/cgroup/rstat.c:386: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_flush_release' kernel/cgroup/rstat.c:386: warning: Excess function parameter 'cgrp' description in 'cgroup_rstat_flush_release'
vim +86 kernel/cgroup/rstat.c
21c38a3bd4ee3f kernel/cgroup/rstat.c Jesper Dangaard Brouer 2024-05-01 75
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 76 /**
6162cef0f741c7 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 77 * cgroup_rstat_updated - keep track of updated rstat_cpu
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 78 * @cgrp: target cgroup
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 79 * @cpu: cpu on which rstat_cpu was updated
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 80 *
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 81 * @cgrp's rstat_cpu on @cpu was updated. Put it on the parent's matching
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 82 * rstat_cpu->updated_children list. See the comment on top of
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 83 * cgroup_rstat_cpu definition for details.
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 84 */
3801ea247f0018 kernel/cgroup/rstat.c JP Kobryn 2025-01-02 85 __bpf_kfunc void cgroup_rstat_updated(struct cgroup_subsys_state *css, int cpu)
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 @86 {
3801ea247f0018 kernel/cgroup/rstat.c JP Kobryn 2025-01-02 87 struct cgroup *cgrp = css->cgroup;
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 88 raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu);
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 89 unsigned long flags;
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 90
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 91 /*
d8ef4b38cb69d9 kernel/cgroup/rstat.c Tejun Heo 2020-04-09 92 * Speculative already-on-list test. This may race leading to
d8ef4b38cb69d9 kernel/cgroup/rstat.c Tejun Heo 2020-04-09 93 * temporary inaccuracies, which is fine.
d8ef4b38cb69d9 kernel/cgroup/rstat.c Tejun Heo 2020-04-09 94 *
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 95 * Because @parent's updated_children is terminated with @parent
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 96 * instead of NULL, we can tell whether @cgrp is on the list by
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 97 * testing the next pointer for NULL.
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 98 */
eda09706b240ca kernel/cgroup/rstat.c Michal Koutný 2021-11-03 99 if (data_race(cgroup_rstat_cpu(cgrp, cpu)->updated_next))
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 100 return;
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 101
21c38a3bd4ee3f kernel/cgroup/rstat.c Jesper Dangaard Brouer 2024-05-01 102 flags = _cgroup_rstat_cpu_lock(cpu_lock, cpu, cgrp, true);
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 103
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 104 /* put @cgrp and all ancestors on the corresponding updated lists */
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 105 while (true) {
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 106 struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(cgrp, cpu);
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 107 struct cgroup *parent = cgroup_parent(cgrp);
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 108 struct cgroup_rstat_cpu *prstatc;
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 109
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 110 /*
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 111 * Both additions and removals are bottom-up. If a cgroup
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 112 * is already in the tree, all ancestors are.
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 113 */
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 114 if (rstatc->updated_next)
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 115 break;
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 116
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 117 /* Root has no parent to link it to, but mark it busy */
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 118 if (!parent) {
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 119 rstatc->updated_next = cgrp;
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 120 break;
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 121 }
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 122
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 123 prstatc = cgroup_rstat_cpu(parent, cpu);
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 124 rstatc->updated_next = prstatc->updated_children;
c58632b3631cb2 kernel/cgroup/rstat.c Tejun Heo 2018-04-26 125 prstatc->updated_children = cgrp;
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 126
dc26532aed0ab2 kernel/cgroup/rstat.c Johannes Weiner 2021-04-29 127 cgrp = parent;
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 128 }
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 129
21c38a3bd4ee3f kernel/cgroup/rstat.c Jesper Dangaard Brouer 2024-05-01 130 _cgroup_rstat_cpu_unlock(cpu_lock, cpu, cgrp, flags, true);
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 131 }
041cd640b2f3c5 kernel/cgroup/stat.c Tejun Heo 2017-09-25 132 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v2 2/9 cgroup: change cgroup to css in rstat internal flush and lock funcs
In reply to JP KobrynChange internal rstat functions to accept a cgroup_subsys_state instead of a cgroup.
--- kernel/cgroup/rstat.c | 45 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-)
kernel/cgroup/rstat.c +23 -22
@@ -201,8 +201,10 @@ static struct cgroup *cgroup_rstat_push_children(struct cgroup *head,* within the children list and terminated by the parent cgroup. An exception * here is the cgroup root whose updated_next can be self terminated. */ -static struct cgroup *cgroup_rstat_updated_list (struct cgroup *root,int cpu) +static struct cgroup *cgroup_rstat_updated_list (struct cgroup_subsys_state *root_css, +int cpu) { +struct cgroup *root = root_css->cgroup;raw_spinlock_t *cpu_lock =per_cpu_ptr (&cgroup_rstat_cpu_lock, cpu);struct cgroup_rstat_cpu *rstatc =cgroup_rstat_cpu (root, cpu);struct cgroup *head =NULL , *parent, *child;
@@ -280,9 +282,11 @@ __bpf_hook_end();* value -1 is used when obtaining the main lock else this is the CPU * number processed last. */ -static inline void __cgroup_rstat_lock (struct cgroup *cgrp,int cpu_in_loop) +static inline void __cgroup_rstat_lock (struct cgroup_subsys_state *css, +int cpu_in_loop) __acquires(&cgroup_rstat_lock) { +struct cgroup *cgrp = css->cgroup;bool contended; contended = !spin_trylock_irq (&cgroup_rstat_lock);
@@ -293,15 +297,18 @@ static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int cpu_in_loop)trace_cgroup_rstat_locked (cgrp, cpu_in_loop, contended); } -static inline void __cgroup_rstat_unlock (struct cgroup *cgrp,int cpu_in_loop) +static inline void __cgroup_rstat_unlock (struct cgroup_subsys_state *css, +int cpu_in_loop) __releases(&cgroup_rstat_lock) { +struct cgroup *cgrp = css->cgroup; +trace_cgroup_rstat_unlock (cgrp, cpu_in_loop, false);spin_unlock_irq (&cgroup_rstat_lock); }/* see cgroup_rstat_flush() */ -static void cgroup_rstat_flush_locked (struct cgroup *cgrp) +static void cgroup_rstat_flush_locked (struct cgroup_subsys_state *css) __releases(&cgroup_rstat_lock) __acquires(&cgroup_rstat_lock) {int cpu;
@@ -309,27 +316,27 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp)lockdep_assert_held (&cgroup_rstat_lock);for_each_possible_cpu (cpu) { -struct cgroup *pos =cgroup_rstat_updated_list (cgrp, cpu); +struct cgroup *pos =cgroup_rstat_updated_list (css, cpu);for (; pos; pos = pos->rstat_flush_next) { -struct cgroup_subsys_state *css; +struct cgroup_subsys_state *css_iter;cgroup_base_stat_flush (pos, cpu);bpf_rstat_flush (pos,cgroup_parent (pos), cpu);rcu_read_lock (); -list_for_each_entry_rcu (css, &pos->rstat_css_list, +list_for_each_entry_rcu (css_iter, &pos->rstat_css_list, rstat_css_node) - css->ss->css_rstat_flush (css, cpu); + css_iter->ss->css_rstat_flush (css_iter, cpu);rcu_read_unlock (); }/* play nice and yield if necessary */ if (need_resched () ||spin_needbreak (&cgroup_rstat_lock)) { -__cgroup_rstat_unlock (cgrp, cpu); +__cgroup_rstat_unlock (css, cpu);if (!cond_resched ())cpu_relax (); -__cgroup_rstat_lock (cgrp, cpu); +__cgroup_rstat_lock (css, cpu); } } }
@@ -349,13 +356,11 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp)*/ __bpf_kfunc void cgroup_rstat_flush(struct cgroup_subsys_state *css) { -struct cgroup *cgrp = css->cgroup; -might_sleep (); -__cgroup_rstat_lock (cgrp,-1 ); -cgroup_rstat_flush_locked (cgrp); -__cgroup_rstat_unlock (cgrp,-1 ); +__cgroup_rstat_lock (css,-1 ); +cgroup_rstat_flush_locked (css); +__cgroup_rstat_unlock (css,-1 ); }/**
@@ -370,11 +375,9 @@ __bpf_kfunc void cgroup_rstat_flush(struct cgroup_subsys_state *css)void cgroup_rstat_flush_hold (struct cgroup_subsys_state *css) __acquires(&cgroup_rstat_lock) { -struct cgroup *cgrp = css->cgroup; -might_sleep (); -__cgroup_rstat_lock (cgrp,-1 ); -cgroup_rstat_flush_locked (cgrp); +__cgroup_rstat_lock (css,-1 ); +cgroup_rstat_flush_locked (css); }/**
@@ -384,9 +387,7 @@ void cgroup_rstat_flush_hold(struct cgroup_subsys_state *css)void cgroup_rstat_flush_release (struct cgroup_subsys_state *css) __releases(&cgroup_rstat_lock) { -struct cgroup *cgrp = css->cgroup; - -__cgroup_rstat_unlock (cgrp,-1 ); +__cgroup_rstat_unlock (css,-1 ); }int cgroup_rstat_init (struct cgroup *cgrp)
Signature · 1 line
2.47.1
RFC PATCH v2 3/9 cgroup: change cgroup to css in rstat init and exit api
In reply to JP KobrynChange rstat init and exit API calls to accept a cgroup_subsys_state instead of a cgroup.
--- kernel/cgroup/cgroup-internal.h | 4 ++-- kernel/cgroup/cgroup.c | 16 ++++++++++------ kernel/cgroup/rstat.c | 6 ++++-- 3 files changed, 16 insertions(+), 10 deletions(-)
kernel/cgroup/cgroup-internal.h +2 -2
@@ -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 cgroup_rstat_init (struct cgroup_subsys_state *css); +void cgroup_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 +10 -6
@@ -1358,7 +1358,7 @@ static void cgroup_destroy_root(struct cgroup_root *root)cgroup_unlock (); -cgroup_rstat_exit (cgrp); +cgroup_rstat_exit (&cgrp->self);kernfs_destroy_root (root->kf_root);cgroup_free_root (root); }
@@ -2132,7 +2132,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)if (ret) goto destroy_root; - ret =cgroup_rstat_init (root_cgrp); + ret =cgroup_rstat_init (&root_cgrp->self);if (ret) goto destroy_root;
@@ -2174,7 +2174,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) goto out; exit_stats: -cgroup_rstat_exit (root_cgrp); +cgroup_rstat_exit (&root_cgrp->self); destroy_root:kernfs_destroy_root (root->kf_root); root->kf_root =NULL ;
@@ -5435,7 +5435,7 @@ static void css_free_rwork_fn(struct work_struct *work)cgroup_put (cgroup_parent (cgrp));kernfs_put (cgrp->kn);psi_cgroup_free (cgrp); -cgroup_rstat_exit (cgrp); +cgroup_rstat_exit (css);kfree (cgrp); }else {/*
@@ -5686,7 +5686,11 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,if (ret) goto out_free_cgrp; - ret =cgroup_rstat_init (cgrp); +/* init self cgroup early so css->cgroup is valid within cgroup_rstat_init() +* note that this will go away in a subsequent patch in this series +*/ + cgrp->self.cgroup = cgrp; + ret =cgroup_rstat_init (&cgrp->self);if (ret) goto out_cancel_ref;
@@ -5779,7 +5783,7 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name, out_kernfs_remove:kernfs_remove (cgrp->kn); out_stat_exit: -cgroup_rstat_exit (cgrp); +cgroup_rstat_exit (&cgrp->self); out_cancel_ref:percpu_ref_exit (&cgrp->self.refcnt); out_free_cgrp:
kernel/cgroup/rstat.c +4 -2
@@ -390,8 +390,9 @@ void cgroup_rstat_flush_release(struct cgroup_subsys_state *css)__cgroup_rstat_unlock (css,-1 ); } -int cgroup_rstat_init (struct cgroup *cgrp) +int cgroup_rstat_init (struct cgroup_subsys_state *css) { +struct cgroup *cgrp = css->cgroup;int cpu;/* the root cgrp has rstat_cpu preallocated */
@@ -412,8 +413,9 @@ int cgroup_rstat_init(struct cgroup *cgrp)return 0 ; } -void cgroup_rstat_exit (struct cgroup *cgrp) +void cgroup_rstat_exit (struct cgroup_subsys_state *css) { +struct cgroup *cgrp = css->cgroup;int cpu;cgroup_rstat_flush (&cgrp->self);
Signature · 1 line
2.47.1
RFC PATCH v2 4/9 cgroup: split rstat from cgroup into separate css
In reply to JP KobrynMove the rstat entities off of the cgroup struct and onto the cgroup_subsys_state struct. Adjust related code to reflect this new ownership.
--- include/linux/cgroup-defs.h | 40 ++++++++-------- kernel/cgroup/cgroup.c | 65 ++++++++++++++++++-------- kernel/cgroup/rstat.c | 92 ++++++++++++++++++------------------- 3 files changed, 111 insertions(+), 86 deletions(-)
include/linux/cgroup-defs.h +20 -20
@@ -180,6 +180,24 @@ struct cgroup_subsys_state {struct list_head sibling;struct list_head children; +/* per-cpu recursive resource statistics */ +struct cgroup_rstat_cpu __percpu *rstat_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. +*/ +CACHELINE_PADDING (_pad_); + +/* +* A singly-linked list of cgroup 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. +*/ +struct cgroup_subsys_state *rstat_flush_next; +/* flush target list anchored at cgrp->rstat_css_list */ struct list_head rstat_css_node;
@@ -389,8 +407,8 @@ struct cgroup_rstat_cpu {* * 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;/* terminated by self cgroup */ +struct cgroup_subsys_state *updated_next;/* NULL iff not on the list */ };struct cgroup_freezer_state {
@@ -516,24 +534,6 @@ 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 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. -*/ -CACHELINE_PADDING (_pad_); - -/* -* A singly-linked list of cgroup 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. -*/ -struct cgroup *rstat_flush_next; -/* cgroup basic resource statistics */ struct cgroup_base_stat last_bstat;struct cgroup_base_stat bstat;
kernel/cgroup/cgroup.c +45 -20
173 lines not on this page · raw message
kernel/cgroup/rstat.c +46 -46
227 lines not on this page · raw message
Signature · 1 line
2.47.1
RFC PATCH v2 5/9 cgroup: separate locking between base css and others
In reply to JP KobrynSeparate locks can be used to eliminate contention between subsystems that make use of rstat. The base stats also get their own lock. Where applicable, check for the existence of a subsystem pointer to determine if the given cgroup_subsys_state is the base css or not for deciding which lock to take.
--- include/linux/cgroup-defs.h | 2 + kernel/cgroup/rstat.c | 92 +++++++++++++++++++++++++------------ 2 files changed, 65 insertions(+), 29 deletions(-)
include/linux/cgroup-defs.h +2 -0
@@ -790,6 +790,8 @@ struct cgroup_subsys {* specifies the mask of subsystems that this one depends on. */ unsigned int depends_on; + +spinlock_t rstat_lock; };extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
kernel/cgroup/rstat.c +63 -29
197 lines not on this page · raw message
Signature · 1 line
2.47.1
kernel test robot
build botRe: [RFC PATCH 5/9 v2] cgroup: separate locking between base css and others
kernel test robot noticed the following build warnings:
Hi JP,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on tj-cgroup/for-next]
[also build test WARNING on akpm-mm/mm-everything axboe-block/for-next linus/master v6.13-rc5 next-20241220] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/JP-Kobryn/cgroup-change-cgroup-to-css-in-rstat-updated-and-flush-api/20250103-095314 base: https://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next patch link: https://lore.kernel.org/r/20250103015020.78547-6-inwardvessel%40gmail.com patch subject: [RFC PATCH 5/9 v2] cgroup: separate locking between base css and others config: i386-buildonly-randconfig-003-20250103 (https://download.01.org/0day-ci/archive/20250103/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250103/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
kernel/cgroup/rstat.c:88: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_updated' kernel/cgroup/rstat.c:88: warning: Excess function parameter 'cgrp' description in 'cgroup_rstat_updated' kernel/cgroup/rstat.c:378: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_flush' kernel/cgroup/rstat.c:378: warning: Excess function parameter 'cgrp' description in 'cgroup_rstat_flush'
>> kernel/cgroup/rstat.c:408: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_base_flush_hold' >> kernel/cgroup/rstat.c:408: warning: expecting prototype for cgroup_rstat_flush_hold(). Prototype was for cgroup_rstat_base_flush_hold() instead >> kernel/cgroup/rstat.c:420: warning: Function parameter or struct member 'css' not described in 'cgroup_rstat_base_flush_release' >> kernel/cgroup/rstat.c:420: warning: expecting prototype for cgroup_rstat_flush_release(). Prototype was for cgroup_rstat_base_flush_release() instead
vim +408 kernel/cgroup/rstat.c
0fa294fb1985c0 Tejun Heo 2018-04-26 396
6162cef0f741c7 Tejun Heo 2018-04-26 397 /**
2ca11b0e043be6 Yang Li 2021-05-26 398 * cgroup_rstat_flush_hold - flush stats in @cgrp's subtree and hold
6162cef0f741c7 Tejun Heo 2018-04-26 399 * @cgrp: target cgroup
6162cef0f741c7 Tejun Heo 2018-04-26 400 *
6162cef0f741c7 Tejun Heo 2018-04-26 401 * Flush stats in @cgrp's subtree and prevent further flushes. Must be
6162cef0f741c7 Tejun Heo 2018-04-26 402 * paired with cgroup_rstat_flush_release().
0fa294fb1985c0 Tejun Heo 2018-04-26 403 *
0fa294fb1985c0 Tejun Heo 2018-04-26 404 * This function may block.
6162cef0f741c7 Tejun Heo 2018-04-26 405 */
d0b90e95826c89 JP Kobryn 2025-01-02 406 static void cgroup_rstat_base_flush_hold(struct cgroup_subsys_state *css)
d0b90e95826c89 JP Kobryn 2025-01-02 407 __acquires(&cgroup_rstat_base_lock)
6162cef0f741c7 Tejun Heo 2018-04-26 @408 {
0fa294fb1985c0 Tejun Heo 2018-04-26 409 might_sleep();
d0b90e95826c89 JP Kobryn 2025-01-02 410 __cgroup_rstat_lock(css, &cgroup_rstat_base_lock, -1);
6f15a01d35d342 JP Kobryn 2025-01-02 411 cgroup_rstat_flush_locked(css);
6162cef0f741c7 Tejun Heo 2018-04-26 412 }
6162cef0f741c7 Tejun Heo 2018-04-26 413
6162cef0f741c7 Tejun Heo 2018-04-26 414 /**
6162cef0f741c7 Tejun Heo 2018-04-26 415 * cgroup_rstat_flush_release - release cgroup_rstat_flush_hold()
97a46a66ad7d91 Jesper Dangaard Brouer 2024-04-17 416 * @cgrp: cgroup used by tracepoint
6162cef0f741c7 Tejun Heo 2018-04-26 417 */
d0b90e95826c89 JP Kobryn 2025-01-02 418 static void cgroup_rstat_base_flush_release(struct cgroup_subsys_state *css)
d0b90e95826c89 JP Kobryn 2025-01-02 419 __releases(&cgroup_rstat_base_lock)
6162cef0f741c7 Tejun Heo 2018-04-26 @420 {
d0b90e95826c89 JP Kobryn 2025-01-02 421 __cgroup_rstat_unlock(css, &cgroup_rstat_base_lock, -1);
6162cef0f741c7 Tejun Heo 2018-04-26 422 }
6162cef0f741c7 Tejun Heo 2018-04-26 423 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v2 6/9 cgroup: isolate base stat flush
In reply to JP KobrynRemove the base stat flushing from the generic css flushing routine. Provide a direct way to flush the base stats and make use of it when the cpu stats are read.
--- kernel/cgroup/rstat.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/cgroup/rstat.c +9 -2
@@ -320,6 +320,14 @@ static inline void __cgroup_rstat_unlock(struct cgroup_subsys_state *css,spin_unlock_irq (lock); } +static void cgroup_rstat_base_flush_locked (struct cgroup *cgrp) +{ +int cpu; + +for_each_possible_cpu (cpu) +cgroup_base_stat_flush (cgrp, cpu); +} +/* see cgroup_rstat_flush() */ static void cgroup_rstat_flush_locked(struct cgroup_subsys_state *css) __releases(&css->ss->rstat_lock) __acquires(&css->ss->rstat_lock)
@@ -341,7 +349,6 @@ static void cgroup_rstat_flush_locked(struct cgroup_subsys_state *css)for (; pos; pos = pos->rstat_flush_next) {struct cgroup_subsys_state *css_iter; -cgroup_base_stat_flush (pos->cgroup, cpu);bpf_rstat_flush (pos->cgroup,cgroup_parent (pos->cgroup), cpu);rcu_read_lock ();
@@ -408,7 +415,7 @@ static void cgroup_rstat_base_flush_hold(struct cgroup_subsys_state *css) {might_sleep ();__cgroup_rstat_lock (css, &cgroup_rstat_base_lock,-1 ); -cgroup_rstat_flush_locked (css); +cgroup_rstat_base_flush_locked (css->cgroup); }/**
Signature · 1 line
2.47.1
RFC PATCH v2 7/9 cgroup: remove unneeded rcu list
In reply to JP KobrynSince the cgroup_subsystem_state now owns the rstat tree, the list management previously done on the cgroup to keep track of which subsystem states are participating in rstat is no longer needed.
--- include/linux/cgroup-defs.h | 11 ----------- kernel/cgroup/cgroup.c | 20 +------------------- kernel/cgroup/rstat.c | 9 +-------- 3 files changed, 2 insertions(+), 38 deletions(-)
include/linux/cgroup-defs.h +0 -11
@@ -182,14 +182,6 @@ struct cgroup_subsys_state {/* per-cpu recursive resource statistics */ struct cgroup_rstat_cpu __percpu *rstat_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. -*/ -CACHELINE_PADDING (_pad_);/* * A singly-linked list of cgroup structures to be rstat flushed.
@@ -198,9 +190,6 @@ struct cgroup_subsys_state {*/ struct cgroup_subsys_state *rstat_flush_next; -/* 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().
kernel/cgroup/cgroup.c +1 -19
@@ -1826,7 +1826,6 @@ int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)struct cgroup_root *src_root = ss->root;struct cgroup *scgrp = &src_root->cgrp;struct cgroup_subsys_state *css =cgroup_css (scgrp, ss); -struct cgroup_subsys_state *dcss =cgroup_css (dcgrp, ss);struct css_set *cset, *cset_pos;struct css_task_iter *it;
@@ -1864,13 +1863,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, - &dcss->rstat_css_list); - } -/* default hierarchy doesn't enable controllers by default */ dst_root->subsys_mask |=1 << ssid;if (dst_root == &cgrp_dfl_root) {
@@ -5491,11 +5483,7 @@ static void css_release_work_fn(struct work_struct *work)if (ss) {struct cgroup *parent_cgrp; -/* css release path */ -if (!list_empty (&css->rstat_css_node)) { -cgroup_rstat_flush (css); -list_del_rcu (&css->rstat_css_node); - } +cgroup_rstat_flush (css);cgroup_idr_replace (&ss->css_idr,NULL , css->id);if (ss->css_released)
@@ -5569,8 +5557,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_list); -INIT_LIST_HEAD (&css->rstat_css_node); css->serial_nr = css_serial_nr_next++;atomic_set (&css->online_cnt,0 );
@@ -5579,9 +5565,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, &css->rstat_css_list); -BUG_ON (cgroup_css (cgrp, ss)); }
@@ -5687,7 +5670,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);
kernel/cgroup/rstat.c +1 -8
@@ -347,15 +347,8 @@ static void cgroup_rstat_flush_locked(struct cgroup_subsys_state *css)struct cgroup_subsys_state *pos =cgroup_rstat_updated_list (css, cpu);for (; pos; pos = pos->rstat_flush_next) { -struct cgroup_subsys_state *css_iter; -bpf_rstat_flush (pos->cgroup,cgroup_parent (pos->cgroup), cpu); - -rcu_read_lock (); -list_for_each_entry_rcu (css_iter, &pos->rstat_css_list, - rstat_css_node) - css_iter->ss->css_rstat_flush (css_iter, cpu); -rcu_read_unlock (); + pos->ss->css_rstat_flush (pos, cpu); }/* play nice and yield if necessary */
Signature · 1 line
2.47.1
RFC PATCH v2 8/9 cgroup: remove bpf rstat flush from css generic flush
In reply to JP KobrynRemove the bpf-specific flush call from the generic subsystem flush. Leave it up to bpf programs to manually flush any subsystems desired by using the kfunc cgroup_rstat_flush().
--- kernel/cgroup/rstat.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/cgroup/rstat.c +1 -3
@@ -346,10 +346,8 @@ static void cgroup_rstat_flush_locked(struct cgroup_subsys_state *css)for_each_possible_cpu (cpu) {struct cgroup_subsys_state *pos =cgroup_rstat_updated_list (css, cpu); -for (; pos; pos = pos->rstat_flush_next) { -bpf_rstat_flush (pos->cgroup,cgroup_parent (pos->cgroup), cpu); +for (; pos; pos = pos->rstat_flush_next) pos->ss->css_rstat_flush (pos, cpu); - }/* play nice and yield if necessary */ if (need_resched () ||spin_needbreak (lock)) {
Signature · 1 line
2.47.1
RFC PATCH v2 9/9 cgroup: avoid allocating rstat when flush func not present
In reply to JP KobrynIf a given subsystem is not the base type and does not have a flush func, do not perform any rstat allocation.
--- kernel/cgroup/rstat.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/cgroup/rstat.c +6 -0
@@ -423,6 +423,9 @@ int cgroup_rstat_init(struct cgroup_subsys_state *css) {int cpu; +if (css->ss && !css->ss->css_rstat_flush) +return 0 ; +/* the root cgrp css has rstat_cpu preallocated */ if (!css->rstat_cpu) { css->rstat_cpu = alloc_percpu(struct cgroup_rstat_cpu);
@@ -445,6 +448,9 @@ void cgroup_rstat_exit(struct cgroup_subsys_state *css) {int cpu; +if (css->ss && !css->ss->css_rstat_flush) +return ; +cgroup_rstat_flush (css);/* sanity check */
Signature · 1 line
2.47.1
Tejun Heo
maintainerHello,
On Thu, Jan 02, 2025 at 05:50:11PM -0800, JP Kobryn wrote:
...
Quoted 2 lines
I reached a point where this started to feel stable in my local testing, so I wanted to share and get feedback on this approach.
The rationale for using one tree to track all subsystems was that if one subsys has been active (e.g. memory), it's likely that other subsyses have been active too (e.g. cpu) and thus we might as well flush the whole thing together. The approach can be useful for reducing the amount of work done when e.g. there are a lot of cgroups which are only active periodically but has drawbacks when one subsystem's stats are read a lot more actively than others as you pointed out.
Intuitions go only so far and it's difficult to judge whether splitting the trees would be a good idea without data. Can you please provide some numbers along with rationales for the test setups?
Thanks.
Signature · 1 line
tejun
Shakeel Butt
maintainerQuoted 7 lines · Tejun Heo
Hello,
On Thu, Jan 02, 2025 at 05:50:11PM -0800, JP Kobryn wrote:
...Quoted 2 lines
I reached a point where this started to feel stable in my local testing, so I wanted to share and get feedback on this approach.
Quoted 7 lines
The rationale for using one tree to track all subsystems was that if one subsys has been active (e.g. memory), it's likely that other subsyses have been active too (e.g. cpu) and thus we might as well flush the whole thing together. The approach can be useful for reducing the amount of work done when e.g. there are a lot of cgroups which are only active periodically but has drawbacks when one subsystem's stats are read a lot more actively than others as you pointed out.
I wanted to add two more points to above: (1) One subsystem (memory) has in-kernel stats consumer with strict latency/performance requirement and
(2) the flush cost of memory stats have drastically increased due to more than 100 stats it has to maintain.
Quoted 4 lines
Intuitions go only so far and it's difficult to judge whether splitting the trees would be a good idea without data. Can you please provide some numbers along with rationales for the test setups?
Here I think the supportive data we can show is the (1) non-memory stats readers not needing to spend time on memory stats flushing and (2) with per-subsystem update tree, have we increased the cost of update tree insertion in general?
Anything else you think will be needed?
Thanks Tejun for taking a look.
Tejun Heo
maintainerHello,
On Mon, Jan 13, 2025 at 10:39:02AM -0800, Shakeel Butt wrote:
...
Quoted 6 lines
Here I think the supportive data we can show is the (1) non-memory stats readers not needing to spend time on memory stats flushing and (2) with per-subsystem update tree, have we increased the cost of update tree insertion in general?
Anything else you think will be needed?
Anything that shows:
1. It doesn't cause noticeable regressions for generic use cases.
2. It has noticeable benefits in the targeted use cases.
The above is a bit willy-nilly but 2 should be fairly straightforward. For 1, the main worry would be whether high(er) frequency monitoring using something like systemd-cgtop or below becomes noticeably more expensive on a large and busy system.
Thanks.
Signature · 1 line
tejun
Quoted 42 lines · JP Kobryn
The current rstat model is set up to keep track of cgroup stats on a per-cpu basis. When a stat (of any subsystem) is updated, the updater notes this change using the
cgroup_rstat_updated()API call. This change is propagated to the cpu-specific rstat tree, by appending the updated cgroup to the tree (unless it's already on the tree). So for each cpu, an rstat tree will consist of the cgroups that reported one or more updated stats. Later on when a flush is requested viacgroup_rstat_flush(), each per-cpu rstat tree is traversed starting at the requested cgroup and the subsystem-specific flush callbacks (viacss_rstat_flush) are invoked along the way. During the flush, the section of the tree starting at the requested cgroup through its descendants are removed.Using the cgroup struct to represent nodes of change means that the changes represented by a given tree are heterogeneous - the tree can consist of nodes that have changes from different subsystems; i.e. changes in stats from the memory subsystem and the io subsystem can coexist in the same tree. The implication is that when a flush is requested, usually in the context of a single subsystem, all other subsystems need to be flushed along with it. This seems to have become a drawback due to how expensive the flushing of the memory-specific stats have become [0][1]. Another implication is when updates are performed, subsystems may contend with each other over the locks involved.
I've been experimenting with an idea that allows for isolating the updating and flushing of cgroup stats on a per-subsystem basis. The idea was instead of having a per-cpu rstat tree for managing stats across all subsystems, we could split up the per-cpu trees into separate trees for each subsystem. So each cpu would have separate trees for each subsystem. It would allow subsystems to update and flush their stats without any contention or extra overhead from other subsystems. The core change is moving ownership of the the rstat entities from the cgroup struct onto the
cgroup_subsystem_statestruct.To complement the ownership change, the lockng scheme was adjusted. The global
cgroup_rstat_lockfor synchronizing updates and flushes was replaced with subsystem-specific locks (in thecgroup_subsystemstruct). An additional global lock was added to allow the base stats pseudo-subsystem to be synchronized in a similar way. The per-cpu locks calledcgroup_rstat_cpu_lockhave changed to a per-cpu array of locks which is indexed by subsystem id. Following suit, there is also a per-cpu array of locks dedicated to the base subsystem. The dedicated locks for the base stats was added since the base stats have a NULL subsystem so it did not fit the subsystem id index approach.
Quoted 2 lines
I reached a point where this started to feel stable in my local testing, so I wanted to share and get feedback on this approach.
I remember discussing this with Shakeel and Michal Koutný in LPC two
years ago. I suggested it multiple times over the last few years, most
recently in: https://lore.kernel.org/lkml/CAJD7tkbpFu8z1HaUgkaE6bup_fsD39QLPmgNyOnaTrm+hZ_9hA@mail.gmail.com/.
I think it conceptually makes sense, and I took a stab at it when I was working on fixing the hard lockups due to atomic flushing, but the system I was working on was using cgroup v1, so different subsystems had different hierarchies (and hence different trees) anyway, so it wouldn't have helped.
This is especially true for the MM subsystem, which apparently flushes most often and has the most expensive flushes, so other subsystems are probably being unnecessarily taxed.
Quoted 29 lines
[0] https://lore.kernel.org/all/CAOm-9arwY3VLUx5189JAR9J7B=Miad9nQjjet_VNdT3i+J+5FA@mail.gmail.com/
[1] https://github.blog/engineering/debugging-network-stalls-on-kubernetes/Changelog
v2: updated cover letter and some patch text. no code changes.JP Kobryn (8):
change cgroup to css in rstat updated and flush api change cgroup to css in rstat internal flush and lock funcs change cgroup to css in rstat init and exit api split rstat from cgroup into separate css separate locking between base css and others isolate base stat flush
remove unneeded rcu list
remove bpf rstat flush from css generic flushblock/blk-cgroup.c | 4 +- include/linux/cgroup-defs.h | 35 ++--- include/linux/cgroup.h | 8 +- kernel/cgroup/cgroup-internal.h | 4 +- kernel/cgroup/cgroup.c | 79 ++++++----- kernel/cgroup/rstat.c | 225 +++++++++++++++++++------------- mm/memcontrol.c | 4 +- 7 files changed, 203 insertions(+), 156 deletions(-)--
2.47.1