cgroup/cpuset: Speed up v2 mask updates and domain rebuilds
Subsystems (2)
v2 cover cgroup/cpuset: Speed up v2 mask updates and domain rebuilds
Changes in v2:
- Rebase onto cgroup/for-7.5.
- Patch 2: Name cpuset1_generate_sched_domains() as the remaining caller of cpusets_overlap() in the helper comment (Ridong).
- New patch 3: Drop cpusets_overlap() and call cpumask_intersects() directly in cpuset1_generate_sched_domains() (Ridong). The sibling-scan optimization from v1 is now patch 4.
- Add Ridong's Reviewed-by to patches 1 and 2.
v1: https://lore.kernel.org/lkml/[email protected]/
We run high-density container workloads on large bare-metal hosts using cgroup v2. We may configure many root partitions on a single host. We resize them at runtime based on each partition's CPU PSI.
Changing a member's cpuset.cpus scans its siblings under cpuset_mutex, so the cost of a write grows with the number of children under the same parent. Creating or resizing a root partition can trigger a scheduling domain rebuild. The rebuild checks every pair of partition CPU masks for overlap, so the number of checks grows quadratically with the number of partitions.
This series speeds up cpuset.cpus updates and scheduling domain rebuilds on these hosts. The timings below are from a dual-socket Xeon Platinum 8582C with 240 online CPUs.
1. All v2 partitions use the default scheduling domain attributes. Passing NULL saves one array allocation per rebuild and one attribute initialization per partition.
2. Check each partition's effective CPUs against the union of the masks already seen to detect overlap in a single pass. With 128 root partitions, median time in generate_sched_domains() fell from 42.9 us to 3.14 us.
3. Skip the sibling scan when a member's cpuset.cpus.exclusive stays empty. With 2,048 children under one parent and one sleeping task in the target cpuset, median write latency for cpuset.cpus fell from 24.1 us to 0.85 us.
Thanks,
Chuyi
Chuyi Zhou (4):
cgroup/cpuset: Skip the sched domain attribute allocation on v2 cgroup/cpuset: Check v2 partition overlap in a single pass cgroup/cpuset: Drop cpusets_overlap()
cgroup/cpuset: Skip sibling checks for v2 members without exclusive
CPUs
kernel/cgroup/cpuset-internal.h | 9 ---- kernel/cgroup/cpuset-v1.c | 3 +- kernel/cgroup/cpuset.c | 74 +++++++++++++++++---------------- 3 files changed, 41 insertions(+), 45 deletions(-)
base-commit: 3aeec6cbfda2c7e08ea465a346be28ee8d1fef58
Signature · 1 line
2.20.1
PATCH v2 1/4 cgroup/cpuset: Skip the sched domain attribute allocation on v2
Cgroup v2 cpusets use the default scheduling domain attributes for every partition. The scheduler treats a NULL attribute pointer as the default both when matching existing domains and when building new ones.
generate_sched_domains() allocates an attribute array for v2 and fills each entry with SD_ATTR_INIT, even in the single-partition case.
Return NULL for the v2 attributes. For N > 0 partitions, this saves one memory allocation and N attribute initializations per rebuild.
--- kernel/cgroup/cpuset.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
kernel/cgroup/cpuset.c +7 -14
@@ -868,7 +868,6 @@ static int generate_sched_domains(cpumask_var_t **domains,struct cpuset **csa;/* array of all cpuset ptrs */ int i, j;/* indices for partition finding loops */ cpumask_var_t *doms;/* resulting partition; i.e. sched domains */ -struct sched_domain_attr *dattr;/* attributes for custom domains */ int ndoms =0 ;/* number of sched domains in result */ struct cgroup_subsys_state *pos_css;
@@ -876,7 +875,6 @@ static int generate_sched_domains(cpumask_var_t **domains,return cpuset1_generate_sched_domains (domains, attributes); doms =NULL ; - dattr =NULL ; csa =NULL ;/* Special case for the 99% of systems with one, full, sched domain */
@@ -927,15 +925,8 @@ static int generate_sched_domains(cpumask_var_t **domains, goto done;/* -* The rest of the code, including the scheduler, can deal with -* dattr==NULL case. No need to abort if alloc fails. -*/ - dattr = kmalloc_objs(struct sched_domain_attr, ndoms); - -/* -* Cgroup v2 doesn't support domain attributes, just set all of them -* to SD_ATTR_INIT. Also non-isolating partition root CPUs are a -* subset of HK_TYPE_DOMAIN_BOOT housekeeping CPUs. +* Non-isolating child partition root CPUs are a subset of +* HK_TYPE_DOMAIN_BOOT housekeeping CPUs. */ for (i =0 ; i < ndoms; i++) {/*
@@ -947,8 +938,6 @@ static int generate_sched_domains(cpumask_var_t **domains, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)); else cpumask_copy(doms[i], csa[i]->effective_cpus); -if (dattr) - dattr[i] =SD_ATTR_INIT ; } done:
@@ -961,8 +950,12 @@ static int generate_sched_domains(cpumask_var_t **domains,if (doms ==NULL ) ndoms =1 ; +/* +* Cgroup v2 uses the default domain attributes. The scheduler treats +* a NULL attribute array as SD_ATTR_INIT for every partition. +*/ *domains = doms; - *attributes = dattr; + *attributes =NULL ;return ndoms; }
Signature · 1 line
2.20.1
Re: [PATCH v2 1/4] cgroup/cpuset: Skip the sched domain attribute allocation on v2
Quoted 67 lines · Chuyi Zhou
Cgroup v2 cpusets use the default scheduling domain attributes for every partition. The scheduler treats a NULL attribute pointer as the default both when matching existing domains and when building new ones.
generate_sched_domains()allocates an attribute array for v2 and fills each entry withSD_ATTR_INIT, even in the single-partition case.Return NULL for the v2 attributes. For N > 0 partitions, this saves one memory allocation and N attribute initializations per rebuild.
Signed-off-by Chuyi Zhou <[email protected]>Reviewed-by Ridong Chen <[email protected]>--- kernel/cgroup/cpuset.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)kernel/cgroup/cpuset.c +2
-13@@ -868,7 +868,6 @@ static int generate_sched_domains(cpumask_var_t **domains,struct cpuset **csa;/* array of all cpuset ptrs */ int i, j;/* indices for partition finding loops */ cpumask_var_t *doms;/* resulting partition; i.e. sched domains */ -struct sched_domain_attr *dattr;/* attributes for custom domains */ int ndoms =0 ;/* number of sched domains in result */ struct cgroup_subsys_state *pos_css;@@ -876,7 +875,6 @@ static int generate_sched_domains(cpumask_var_t **domains,return cpuset1_generate_sched_domains (domains, attributes); doms =NULL ; - dattr =NULL ; csa =NULL ;/* Special case for the 99% of systems with one, full, sched domain */ @@ -927,15 +925,8 @@ static int generate_sched_domains(cpumask_var_t **domains, goto done;/* -* The rest of the code, including the scheduler, can deal with -* dattr==NULL case. No need to abort if alloc fails. -*/ - dattr = kmalloc_objs(struct sched_domain_attr, ndoms); - -/* -* Cgroup v2 doesn't support domain attributes, just set all of them -* to SD_ATTR_INIT. Also non-isolating partition root CPUs are a -* subset of HK_TYPE_DOMAIN_BOOT housekeeping CPUs. +* Non-isolating child partition root CPUs are a subset of +* HK_TYPE_DOMAIN_BOOT housekeeping CPUs. */ for (i =0 ; i < ndoms; i++) {/* @@ -947,8 +938,6 @@ static int generate_sched_domains(cpumask_var_t **domains, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)); else cpumask_copy(doms[i], csa[i]->effective_cpus); -if (dattr) - dattr[i] =SD_ATTR_INIT ; } done:@@ -961,8 +950,12 @@ static int generate_sched_domains(cpumask_var_t **domains,if (doms ==NULL ) ndoms =1 ;
kernel/cgroup/cpuset.c static int generate_sched_domains(cpumask_var_t **domains, · line 961
Quoted 10 lines
kernel/cgroup/cpuset.c +5
-1+/* +* Cgroup v2 uses the default domain attributes. The scheduler treats +* a NULL attribute array as SD_ATTR_INIT for every partition. +*/ *domains = doms; - *attributes = dattr; + *attributes =NULL ;return ndoms; }
The cpuset code is usually considered a slow path and so we don't pay too much attention on performance. It looks like we will have to pay a bit more attention going forward.
The code looks good to me.
PATCH v2 2/4 cgroup/cpuset: Check v2 partition overlap in a single pass
In reply to Chuyi Zhougenerate_sched_domains() checks that the effective CPU masks of cgroup v2 root partitions with load balancing enabled are disjoint. It compares every pair of nonempty masks and warns on overlap. For P masks, this takes P * (P - 1) / 2 intersection tests.
Copy the first mask into a scratch mask. Check each remaining mask for overlap with the scratch mask before adding its CPUs. This takes one intersection test and one union per remaining partition.
Reuse the scratch mask allocated at boot for task attachment. Both users hold cpuset_mutex and overwrite the mask before reading it, so the check needs no additional allocation.
On a dual-socket Xeon Platinum 8582C with 240 online CPUs, changing a child partition's cpuset.cpus triggered a domain rebuild. The table shows median time in generate_sched_domains() in microseconds. The partition count includes the top partition.
Partitions Before After
2 0.524 0.520
8 0.775 0.657
32 3.734 1.222
64 11.875 1.797
128 42.936 3.138
192 93.781 4.700
238 142.305 5.424--- kernel/cgroup/cpuset-internal.h | 2 +- kernel/cgroup/cpuset.c | 41 +++++++++++++++++---------------- 2 files changed, 22 insertions(+), 21 deletions(-)
kernel/cgroup/cpuset-internal.h +1 -1
@@ -258,7 +258,7 @@ static inline int is_spread_slab(const struct cpuset *cs) }/* -* Helper routine for generate_sched_domains(). +* Helper routine for cpuset1_generate_sched_domains(). * Do cpusets a, b have overlapping effective cpus_allowed masks? */ static inline int cpusets_overlap (struct cpuset *a,struct cpuset *b)
kernel/cgroup/cpuset.c +21 -20
86 lines not on this page · raw message
Signature · 1 line
2.20.1
Waiman Long
maintainerRe: [PATCH v2 2/4] cgroup/cpuset: Check v2 partition overlap in a single pass
Quoted 135 lines · Chuyi Zhou
generate_sched_domains()checks that the effective CPU masks of cgroup v2 root partitions with load balancing enabled are disjoint. It compares every pair of nonempty masks and warns on overlap. For P masks, this takes P * (P - 1) / 2 intersection tests.Copy the first mask into a scratch mask. Check each remaining mask for overlap with the scratch mask before adding its CPUs. This takes one intersection test and one union per remaining partition.
Reuse the scratch mask allocated at boot for task attachment. Both users hold
cpuset_mutexand overwrite the mask before reading it, so the check needs no additional allocation.On a dual-socket Xeon Platinum 8582C with 240 online CPUs, changing a child partition's cpuset.cpus triggered a domain rebuild. The table shows median time in
generate_sched_domains()in microseconds. The partition count includes the top partition.Partitions Before After 2 0.524 0.520 8 0.775 0.657 32 3.734 1.222 64 11.875 1.797 128 42.936 3.138 192 93.781 4.700 238 142.305 5.424Signed-off-by Chuyi Zhou <[email protected]>Reviewed-by Ridong Chen <[email protected]>--- kernel/cgroup/cpuset-internal.h | 2 +- kernel/cgroup/cpuset.c | 41 +++++++++++++++++---------------- 2 files changed, 22 insertions(+), 21 deletions(-)kernel/cgroup/cpuset-internal.h +1
-1@@ -258,7 +258,7 @@ static inline int is_spread_slab(const struct cpuset *cs) }/* -* Helper routine for generate_sched_domains(). +* Helper routine for cpuset1_generate_sched_domains(). * Do cpusets a, b have overlapping effective cpus_allowed masks? */ static inline int cpusets_overlap (struct cpuset *a,struct cpuset *b)kernel/cgroup/cpuset.c +20
-19@@ -120,6 +120,12 @@ static const char * const perr_strings[] = {static DEFINE_MUTEX (cpuset_top_mutex);static DEFINE_MUTEX (cpuset_mutex); +/* +* Scratch mask for task attachment and sched domain generation. Each user +* must initialize the mask and hold cpuset_mutex for its entire use. +*/ +static cpumask_var_t cpuset_scratch_mask; +/* * File level internal variables below follow one of the following exclusion * rules. @@ -866,10 +872,10 @@ static int generate_sched_domains(cpumask_var_t **domains, {struct cpuset *cp;/* top-down scan of cpusets */ struct cpuset **csa;/* array of all cpuset ptrs */ -int i, j;/* indices for partition finding loops */ cpumask_var_t *doms;/* resulting partition; i.e. sched domains */ int ndoms =0 ;/* number of sched domains in result */ struct cgroup_subsys_state *pos_css; +int i;if (!cpuset_v2 ())return cpuset1_generate_sched_domains (domains, attributes);@@ -908,14 +914,16 @@ static int generate_sched_domains(cpumask_var_t **domains, }rcu_read_unlock (); -for (i =0 ; i < ndoms; i++) { -for (j = i +1 ; j < ndoms; j++) { -if (cpusets_overlap (csa[i], csa[j])) -/* -* Cgroup v2 shouldn't pass down overlapping -* partition root cpusets. -*/ -WARN_ON_ONCE (1 ); +/* Each CPU can belong to only one load-balancing partition. */ +if (ndoms >1 ) { +struct cpumask *seen = cpuset_scratch_mask; + +cpumask_copy (seen, csa[0 ]->effective_cpus); +for (i =1 ; i < ndoms; i++) { +const struct cpumask *cpus = csa[i]->effective_cpus; + +WARN_ON_ONCE (cpumask_intersects (seen, cpus)); +cpumask_or (seen, seen, cpus); } }@@ -3306,13 +3314,6 @@ static void cpuset_cancel_attach(struct cgroup_taskset *tset)mutex_unlock (&cpuset_mutex); } -/* -* Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach_task() -* but we can't allocate it dynamically there. Define it global and -* allocate from cpuset_init(). -*/ -static cpumask_var_t cpus_attach; -static void cpuset_attach_task (struct cpuset *cs,struct task_struct *task) {struct mm_struct *mm;@@ -3320,15 +3321,15 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)lockdep_assert_cpuset_lock_held ();if (cs != &top_cpuset) -guarantee_active_cpus (task, cpus_attach); +guarantee_active_cpus (task, cpuset_scratch_mask);else -cpumask_andnot (cpus_attach,task_cpu_possible_mask (task), +cpumask_andnot (cpuset_scratch_mask,task_cpu_possible_mask (task), subpartitions_cpus);/* * can_attach beforehand should guarantee that this doesn't * fail. TODO: have a better way to handle failure here */ -WARN_ON_ONCE (set_cpus_allowed_ptr (task, cpus_attach)); +WARN_ON_ONCE (set_cpus_allowed_ptr (task, cpuset_scratch_mask));if (cpuset_v2 () && !attach_ctx.mems_updated)return ;
kernel/cgroup/cpuset.c int __init cpuset_init(void) · line 3930
Quoted 8 lines
kernel/cgroup/cpuset.c +1
-1@@ -3923,7 +3924,7 @@ int __init cpuset_init(void)cpuset1_init (&top_cpuset); -BUG_ON (!alloc_cpumask_var (&cpus_attach,GFP_KERNEL )); +BUG_ON (!alloc_cpumask_var (&cpuset_scratch_mask,GFP_KERNEL ));if (housekeeping_enabled (HK_TYPE_DOMAIN_BOOT )) cpumask_andnot(isolated_cpus, cpu_possible_mask,
Waiman Long <[email protected]>
Re: [PATCH v2 2/4] cgroup/cpuset: Check v2 partition overlap in a single pass
Quoted 133 lines · Waiman Long
Quoted 132 lines · Chuyi Zhou
generate_sched_domains()checks that the effective CPU masks of cgroup v2 root partitions with load balancing enabled are disjoint. It compares every pair of nonempty masks and warns on overlap. For P masks, this takes P * (P - 1) / 2 intersection tests.Copy the first mask into a scratch mask. Check each remaining mask for overlap with the scratch mask before adding its CPUs. This takes one intersection test and one union per remaining partition.
Reuse the scratch mask allocated at boot for task attachment. Both users hold
cpuset_mutexand overwrite the mask before reading it, so the check needs no additional allocation.On a dual-socket Xeon Platinum 8582C with 240 online CPUs, changing a child partition's cpuset.cpus triggered a domain rebuild. The table shows median time in
generate_sched_domains()in microseconds. The partition count includes the top partition.Partitions Before After
2 0.524 0.520
8 0.775 0.657
32 3.734 1.222
64 11.875 1.797
128 42.936 3.138
192 93.781 4.700
238 142.305 5.424Signed-off-by Chuyi Zhou <[email protected]>Reviewed-by Ridong Chen <[email protected]>---
kernel/cgroup/cpuset-internal.h| 2 +- kernel/cgroup/cpuset.c | 41 +++++++++++++++++----------------2 files changed, 22 insertions(+), 21 deletions(-)kernel/cgroup/cpuset-internal.h +1
-1@@ -258,7 +258,7 @@ static inline int is_spread_slab(const struct cpuset *cs) } /* - * Helper routine for generate_sched_domains(). + * Helper routine for cpuset1_generate_sched_domains(). * Do cpusets a, b have overlapping effective cpus_allowed masks? */ static inline int cpusets_overlap(struct cpuset *a, struct cpuset *b)kernel/cgroup/cpuset.c +0
-0@@ -120,6 +120,12 @@ static const char * const perr_strings[] = { static DEFINE_MUTEX(cpuset_top_mutex); static DEFINE_MUTEX(cpuset_mutex);+/*
+ * Scratch mask for task attachment and sched domain generation. Each user
+ * must initialize the mask and holdcpuset_mutexfor its entire use.
+ */kernel/cgroup/cpuset.c +2
-0+static cpumask_var_t cpuset_scratch_mask; +/*
* File level internal variables below follow one of the following exclusion
* rules.kernel/cgroup/cpuset.c +0
-0@@ -866,10 +872,10 @@ static int generate_sched_domains(cpumask_var_t **domains, {struct cpuset *cp; /* top-down scan of cpusets */ struct cpuset **csa; /* array of all cpuset ptrs */
- int i, j; /* indices for partition finding loops */cpumask_var_t*doms; /* resulting partition; i.e. sched domains */ int ndoms = 0; /* number of sched domains in result */ structcgroup_subsys_state*pos_css;
+ int i;
if (!cpuset_v2())
returncpuset1_generate_sched_domains(domains, attributes);kernel/cgroup/cpuset.c +10
-7@@ -908,14 +914,16 @@ static int generate_sched_domains(cpumask_var_t **domains, } rcu_read_unlock(); - for (i = 0; i < ndoms; i++) { - for (j = i + 1; j < ndoms; j++) { - if (cpusets_overlap(csa[i], csa[j])) - /* - * Cgroup v2 shouldn't pass down overlapping - * partition root cpusets. - */ - WARN_ON_ONCE(1); + /* Each CPU can belong to only one load-balancing partition. */ + if (ndoms > 1) { + struct cpumask *seen = cpuset_scratch_mask; + + cpumask_copy(seen, csa[0]->effective_cpus); + for (i = 1; i < ndoms; i++) { + const struct cpumask *cpus = csa[i]->effective_cpus; + + WARN_ON_ONCE(cpumask_intersects(seen, cpus)); + cpumask_or(seen, seen, cpus); } }@@ -3306,13 +3314,6 @@ static void
cpuset_cancel_attach(struct cgroup_taskset *tset)mutex_unlock(&cpuset_mutex);
}
-/*
- * Protected bycpuset_mutex.cpus_attachis used only bycpuset_attach_task()
- * but we can't allocate it dynamically there. Define it global and
- * allocate fromcpuset_init().
- */kernel/cgroup/cpuset.c +0
-2-static cpumask_var_t cpus_attach; -static void
cpuset_attach_task(struct cpuset *cs, struct task_struct *task){
structmm_struct*mm;kernel/cgroup/cpuset.c +1
-1@@ -3320,15 +3321,15 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task) lockdep_assert_cpuset_lock_held(); if (cs != &top_cpuset) - guarantee_active_cpus(task, cpus_attach); + guarantee_active_cpus(task, cpuset_scratch_mask);else
- cpumask_andnot(cpus_attach,task_cpu_possible_mask(task), + cpumask_andnot(cpuset_scratch_mask,task_cpu_possible_mask(task),
subpartitions_cpus);
/*
*can_attachbeforehand should guarantee that this doesn't
* fail. TODO: have a better way to handle failure here */
-WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); +WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpuset_scratch_mask)); if (cpuset_v2()&& !attach_ctx.mems_updated)
Quoted 8 lines
kernel/cgroup/cpuset.c int __init cpuset_init(void) · line 3928
Quoted 7 lines
return;
kernel/cgroup/cpuset.c +1
-0@@ -3923,7 +3924,7 @@ int __init cpuset_init(void) cpuset1_init(&top_cpuset); - BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)); + BUG_ON(!alloc_cpumask_var(&cpuset_scratch_mask, GFP_KERNEL)); if (housekeeping_enabled(HK_TYPE_DOMAIN_BOOT)) cpumask_andnot(isolated_cpus, cpu_possible_mask,Waiman Long <[email protected]>
PATCH v2 3/4 cgroup/cpuset: Drop cpusets_overlap()
In reply to Chuyi Zhoucpuset1_generate_sched_domains() is the only caller of cpusets_overlap(). Call cpumask_intersects() directly on the two effective CPU masks and remove the wrapper from the shared cpuset header.
--- kernel/cgroup/cpuset-internal.h | 9 --------- kernel/cgroup/cpuset-v1.c | 3 ++- 2 files changed, 2 insertions(+), 10 deletions(-)
kernel/cgroup/cpuset-internal.h +0 -9
@@ -257,15 +257,6 @@ static inline int is_spread_slab(const struct cpuset *cs)return test_bit (CS_SPREAD_SLAB , &cs->flags); } -/* -* Helper routine for cpuset1_generate_sched_domains(). -* Do cpusets a, b have overlapping effective cpus_allowed masks? -*/ -static inline int cpusets_overlap (struct cpuset *a,struct cpuset *b) -{ -return cpumask_intersects (a->effective_cpus, b->effective_cpus); -} -static inline int nr_cpusets (void ) {/* jump label reference count + the top-level cpuset */
kernel/cgroup/cpuset-v1.c +2 -1
@@ -756,7 +756,8 @@ int cpuset1_generate_sched_domains(cpumask_var_t **domains,/* Merge overlapping cpusets */ for (i =0 ; i < csn; i++) {for (j = i +1 ; j < csn; j++) { -if (cpusets_overlap (csa[i], csa[j])) +if (cpumask_intersects (csa[i]->effective_cpus, + csa[j]->effective_cpus))uf_union (&csa[i]->node, &csa[j]->node); } }
Signature · 1 line
2.20.1
Waiman Long
maintainerRe: [PATCH v2 3/4] cgroup/cpuset: Drop cpusets_overlap()
Quoted 36 lines · Chuyi Zhou
cpuset1_generate_sched_domains()is the only caller ofcpusets_overlap(). Callcpumask_intersects()directly on the two effective CPU masks and remove the wrapper from the shared cpuset header.Suggested-by Ridong Chen <[email protected]>Signed-off-by Chuyi Zhou <[email protected]>--- kernel/cgroup/cpuset-internal.h | 9 --------- kernel/cgroup/cpuset-v1.c | 3 ++- 2 files changed, 2 insertions(+), 10 deletions(-)kernel/cgroup/cpuset-internal.h +0
-9@@ -257,15 +257,6 @@ static inline int is_spread_slab(const struct cpuset *cs)return test_bit (CS_SPREAD_SLAB , &cs->flags); } -/* -* Helper routine for cpuset1_generate_sched_domains(). -* Do cpusets a, b have overlapping effective cpus_allowed masks? -*/ -static inline int cpusets_overlap (struct cpuset *a,struct cpuset *b) -{ -return cpumask_intersects (a->effective_cpus, b->effective_cpus); -} -static inline int nr_cpusets (void ) {/* jump label reference count + the top-level cpuset */ kernel/cgroup/cpuset-v1.c +0
-0
kernel/cgroup/cpuset-v1.c int cpuset1_generate_sched_domains(cpumask_var_t **domains, · line 763
Quoted 9 lines
kernel/cgroup/cpuset-v1.c +2
-1@@ -756,7 +756,8 @@ int cpuset1_generate_sched_domains(cpumask_var_t **domains,/* Merge overlapping cpusets */ for (i =0 ; i < csn; i++) {for (j = i +1 ; j < csn; j++) { -if (cpusets_overlap (csa[i], csa[j])) +if (cpumask_intersects (csa[i]->effective_cpus, + csa[j]->effective_cpus))uf_union (&csa[i]->node, &csa[j]->node); } }
Waiman Long <[email protected]>
Re: [PATCH v2 3/4] cgroup/cpuset: Drop cpusets_overlap()
Quoted 41 lines · Waiman Long
Quoted 39 lines · Chuyi Zhou
cpuset1_generate_sched_domains()is the only caller ofcpusets_overlap(). Callcpumask_intersects()directly on the two effective CPU masks and remove the wrapper from the shared cpuset header.Suggested-by Ridong Chen <[email protected]>Signed-off-by Chuyi Zhou <[email protected]>---
kernel/cgroup/cpuset-internal.h| 9 ---------
kernel/cgroup/cpuset-v1.c | 3 ++-2 files changed, 2 insertions(+), 10 deletions(-)kernel/cgroup/cpuset-internal.h +0
-0@@ -257,15 +257,6 @@ static inline int is_spread_slab(const struct cpuset *cs) return test_bit(CS_SPREAD_SLAB, &cs->flags); }-/*
- * Helper routine forcpuset1_generate_sched_domains().
- * Do cpusets a, b have overlapping effectivecpus_allowedmasks?
- */kernel/cgroup/cpuset-internal.h +0
-5-static inline int cpusets_overlap (struct cpuset *a,struct cpuset *b) -{ - return cpumask_intersects(a->effective_cpus, b->effective_cpus); -} -static inline int
nr_cpusets(void)
{
/* jump label reference count + the top-level cpuset */kernel/cgroup/cpuset-v1.c +0
-0@@ -756,7 +756,8 @@ int cpuset1_generate_sched_domains(cpumask_var_t **domains, /* Merge overlapping cpusets */ for (i = 0; i < csn; i++) {
Quoted 8 lines
Quoted 7 lines
kernel/cgroup/cpuset-v1.c +2
-1for (j = i + 1; j < csn; j++) { - if (cpusets_overlap(csa[i], csa[j])) + if (cpumask_intersects(csa[i]->effective_cpus, + csa[j]->effective_cpus)) uf_union(&csa[i]->node, &csa[j]->node); }}
Waiman Long <[email protected]>
Re: [PATCH v2 3/4] cgroup/cpuset: Drop cpusets_overlap()
In reply to Chuyi ZhouQuoted 36 lines · Chuyi Zhou
cpuset1_generate_sched_domains()is the only caller ofcpusets_overlap(). Callcpumask_intersects()directly on the two effective CPU masks and remove the wrapper from the shared cpuset header.Suggested-by Ridong Chen <[email protected]>Signed-off-by Chuyi Zhou <[email protected]>--- kernel/cgroup/cpuset-internal.h | 9 --------- kernel/cgroup/cpuset-v1.c | 3 ++- 2 files changed, 2 insertions(+), 10 deletions(-)kernel/cgroup/cpuset-internal.h +0
-9@@ -257,15 +257,6 @@ static inline int is_spread_slab(const struct cpuset *cs)return test_bit (CS_SPREAD_SLAB , &cs->flags); } -/* -* Helper routine for cpuset1_generate_sched_domains(). -* Do cpusets a, b have overlapping effective cpus_allowed masks? -*/ -static inline int cpusets_overlap (struct cpuset *a,struct cpuset *b) -{ -return cpumask_intersects (a->effective_cpus, b->effective_cpus); -} -static inline int nr_cpusets (void ) {/* jump label reference count + the top-level cpuset */ kernel/cgroup/cpuset-v1.c +0
-0
kernel/cgroup/cpuset-v1.c int cpuset1_generate_sched_domains(cpumask_var_t **domains, · line 763
Quoted 9 lines
kernel/cgroup/cpuset-v1.c +2
-1@@ -756,7 +756,8 @@ int cpuset1_generate_sched_domains(cpumask_var_t **domains,/* Merge overlapping cpusets */ for (i =0 ; i < csn; i++) {for (j = i +1 ; j < csn; j++) { -if (cpusets_overlap (csa[i], csa[j])) +if (cpumask_intersects (csa[i]->effective_cpus, + csa[j]->effective_cpus))uf_union (&csa[i]->node, &csa[j]->node); } }
Thanks.
Signature · 2 lines
Best regards Ridong
PATCH v2 4/4 cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs
In reply to Chuyi ZhouChanging cpuset.cpus on a cgroup v2 member calls validate_change(), which walks all siblings under cpuset_mutex to check CPU and memory exclusivity. The cost of changing one member's CPU mask grows with the number of children under its parent.
Skip the walk when the member's cpuset.cpus.exclusive stays empty. An empty exclusive mask cannot intersect a sibling's exclusive mask. The check for a sibling whose allowed CPUs are all in the member's exclusive mask applies only when that mask changes. Only the top cpuset is memory-exclusive on v2, so the walk can also be skipped when changing cpuset.mems on these members.
On a dual-socket Xeon Platinum 8582C with 240 online CPUs, the target cpuset contained one sleeping task. A writer pinned to CPU 1 alternated its cpuset.cpus between 2 and 2-3. Each table entry gives the median latency of 256 write() calls.
Children Before (ns) After (ns)
1 861 859
64 1425 856
512 5184 855
2048 24056 853--- kernel/cgroup/cpuset.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/cgroup/cpuset.c +11 -1
@@ -805,12 +805,21 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) !cpuset_cpumask_can_shrink(cur->effective_cpus, user_xcpus(trial))) goto out; + xcpus_changed = !cpumask_equal (cur->exclusive_cpus, trial->exclusive_cpus); + +/* +* On v2 an unchanged, empty exclusive CPU mask cannot conflict with +* a sibling's CPU masks. Only the top cpuset is memory-exclusive. +*/ +if (cpuset_v2 () &&cs_is_member (cur) && !xcpus_changed && +cpumask_empty (trial->exclusive_cpus)) + goto valid; +/* * If either I or some sibling (!= me) is exclusive, we can't * overlap. exclusive_cpus cannot overlap with each other if set. */ ret = -EINVAL ; - xcpus_changed = !cpumask_equal (cur->exclusive_cpus, trial->exclusive_cpus);cpuset_for_each_child (c, css, par) {if (c == cur)continue ;
@@ -820,6 +829,7 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) goto out; } +valid: ret =0 ; out:rcu_read_unlock ();
Signature · 1 line
2.20.1
Re: [PATCH v2 4/4] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs
Quoted 56 lines · Chuyi Zhou
Changing cpuset.cpus on a cgroup v2 member calls
validate_change(), which walks all siblings undercpuset_mutexto check CPU and memory exclusivity. The cost of changing one member's CPU mask grows with the number of children under its parent.Skip the walk when the member's cpuset.cpus.exclusive stays empty. An empty exclusive mask cannot intersect a sibling's exclusive mask. The check for a sibling whose allowed CPUs are all in the member's exclusive mask applies only when that mask changes. Only the top cpuset is memory-exclusive on v2, so the walk can also be skipped when changing cpuset.mems on these members.
On a dual-socket Xeon Platinum 8582C with 240 online CPUs, the target cpuset contained one sleeping task. A writer pinned to CPU 1 alternated its cpuset.cpus between 2 and 2-3. Each table entry gives the median latency of 256
write()calls.Children Before (ns) After (ns) 1 861 859 64 1425 856 512 5184 855 2048 24056 853Signed-off-by Chuyi Zhou <[email protected]>--- kernel/cgroup/cpuset.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)kernel/cgroup/cpuset.c +10
-1@@ -805,12 +805,21 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) !cpuset_cpumask_can_shrink(cur->effective_cpus, user_xcpus(trial))) goto out; + xcpus_changed = !cpumask_equal (cur->exclusive_cpus, trial->exclusive_cpus); + +/* +* On v2 an unchanged, empty exclusive CPU mask cannot conflict with +* a sibling's CPU masks. Only the top cpuset is memory-exclusive. +*/ +if (cpuset_v2 () &&cs_is_member (cur) && !xcpus_changed && +cpumask_empty (trial->exclusive_cpus)) + goto valid; +/* * If either I or some sibling (!= me) is exclusive, we can't * overlap. exclusive_cpus cannot overlap with each other if set. */ ret = -EINVAL ; - xcpus_changed = !cpumask_equal (cur->exclusive_cpus, trial->exclusive_cpus);cpuset_for_each_child (c, css, par) {if (c == cur)continue ;
kernel/cgroup/cpuset.c int validate_change(struct cpuset *cur, struct cpuset *trial) · line 835
Quoted 7 lines
kernel/cgroup/cpuset.c +1
-0@@ -820,6 +829,7 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) goto out; } +valid: ret =0 ; out:rcu_read_unlock ();
Ridong Chen
reviewerRe: [PATCH v2 4/4] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs
In reply to Chuyi ZhouQuoted 56 lines · Chuyi Zhou
Changing cpuset.cpus on a cgroup v2 member calls
validate_change(), which walks all siblings undercpuset_mutexto check CPU and memory exclusivity. The cost of changing one member's CPU mask grows with the number of children under its parent.Skip the walk when the member's cpuset.cpus.exclusive stays empty. An empty exclusive mask cannot intersect a sibling's exclusive mask. The check for a sibling whose allowed CPUs are all in the member's exclusive mask applies only when that mask changes. Only the top cpuset is memory-exclusive on v2, so the walk can also be skipped when changing cpuset.mems on these members.
On a dual-socket Xeon Platinum 8582C with 240 online CPUs, the target cpuset contained one sleeping task. A writer pinned to CPU 1 alternated its cpuset.cpus between 2 and 2-3. Each table entry gives the median latency of 256
write()calls.Children Before (ns) After (ns) 1 861 859 64 1425 856 512 5184 855 2048 24056 853Signed-off-by Chuyi Zhou <[email protected]>--- kernel/cgroup/cpuset.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)kernel/cgroup/cpuset.c +10
-1@@ -805,12 +805,21 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) !cpuset_cpumask_can_shrink(cur->effective_cpus, user_xcpus(trial))) goto out; + xcpus_changed = !cpumask_equal (cur->exclusive_cpus, trial->exclusive_cpus); + +/* +* On v2 an unchanged, empty exclusive CPU mask cannot conflict with +* a sibling's CPU masks. Only the top cpuset is memory-exclusive. +*/ +if (cpuset_v2 () &&cs_is_member (cur) && !xcpus_changed && +cpumask_empty (trial->exclusive_cpus)) + goto valid; +/* * If either I or some sibling (!= me) is exclusive, we can't * overlap. exclusive_cpus cannot overlap with each other if set. */ ret = -EINVAL ; - xcpus_changed = !cpumask_equal (cur->exclusive_cpus, trial->exclusive_cpus);cpuset_for_each_child (c, css, par) {if (c == cur)continue ;
kernel/cgroup/cpuset.c int validate_change(struct cpuset *cur, struct cpuset *trial) · line 835
Quoted 7 lines
kernel/cgroup/cpuset.c +1
-0@@ -820,6 +829,7 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) goto out; } +valid: ret =0 ; out:rcu_read_unlock ();
Hi Chuyi,
Thanks for the patch. I think the condition can be simplified, and the sibling walk could be restructured a bit.
Since commit 85ad3963de96 ("cgroup/cpuset: Remove CS_CPU_EXCLUSIVE handling code from v2"), the v2 part of cpus_excl_conflict() only looks at trial->exclusive_cpus:
xcpus_changed && cpumask_subset(sibling->cpus_allowed, trial->exclusive_cpus) cpumask_intersects(trial->exclusive_cpus, sibling->exclusive_cpus)
If trial->exclusive_cpus is empty, neither of them can be true, whether or not exclusive_cpus has changed. The subset check also requires a non-empty sibling->cpus_allowed, and a non-empty mask cannot be a subset of an empty one. So !xcpus_changed is not needed.
cs_is_member(cur) is not needed either. Nothing in the loop depends on the partition state of @cur. For a partition root, conflicts with siblings are handled by compute_trialcs_excpus() and partition_cpus_change(), which invalidate the partition instead of failing the write. Dropping the check would let partition roots with an empty cpuset.cpus.exclusive skip the walk as well.
On the memory side, mems_excl_conflict() never fires on v2: only top_cpuset can be CS_MEM_EXCLUSIVE there, and it has no siblings.
So the v1 and v2 sibling rules no longer share anything. How about moving the walk into one helper that hands v1 off to cpuset-v1.c and does the v2 checks by default, with the empty check as an early return? Something like:
In cpuset.c:
```
static bool siblings_excl_conflict(struct cpuset *cur, struct cpuset *trial)
{
struct cgroup_subsys_state *css;
struct cpuset *c;
bool xcpus_changed;
if (!cpuset_v2())
return cpuset1_siblings_excl_conflict(cur, trial);
/* An empty exclusive_cpus cannot conflict with any sibling */
if (cpumask_empty(trial->exclusive_cpus))
return false;
xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial->exclusive_cpus);
cpuset_for_each_child(c, css, parent_cs(cur)) {
if (c == cur)
continue;
if (xcpus_changed && !cpumask_empty(c->cpus_allowed) &&
cpumask_subset(c->cpus_allowed, trial->exclusive_cpus))
return true;
if (cpumask_intersects(trial->exclusive_cpus, c->exclusive_cpus))
return true;
}
return false;
}
```In cpuset-v1.c:
```
bool cpuset1_siblings_excl_conflict(struct cpuset *cur, struct cpuset *trial)
{
struct cgroup_subsys_state *css;
struct cpuset *c;
cpuset_for_each_child(c, css, parent_cs(cur)) {
if (c == cur)
continue;
if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
return true;
if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
nodes_intersects(trial->mems_allowed, c->mems_allowed))
return true;
}
return false;
}
```Signature · 2 lines
Best regards Ridong
Re: [PATCH v2 4/4] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs
Quoted 150 lines · Ridong Chen
Quoted 64 lines · Chuyi Zhou
Changing cpuset.cpus on a cgroup v2 member calls
validate_change(), which walks all siblings undercpuset_mutexto check CPU and memory exclusivity. The cost of changing one member's CPU mask grows with the number of children under its parent.Skip the walk when the member's cpuset.cpus.exclusive stays empty. An empty exclusive mask cannot intersect a sibling's exclusive mask. The check for a sibling whose allowed CPUs are all in the member's exclusive mask applies only when that mask changes. Only the top cpuset is memory-exclusive on v2, so the walk can also be skipped when changing cpuset.mems on these members.
On a dual-socket Xeon Platinum 8582C with 240 online CPUs, the target cpuset contained one sleeping task. A writer pinned to CPU 1 alternated its cpuset.cpus between 2 and 2-3. Each table entry gives the median latency of 256
write()calls.Children Before (ns) After (ns)
1 861 859
64 1425 856
512 5184 855
2048 24056 853Signed-off-by Chuyi Zhou <[email protected]>---
kernel/cgroup/cpuset.c| 12 +++++++++++-1 file changed, 11 insertions(+), 1 deletion(-)kernel/cgroup/cpuset.c +10
-1@@ -805,12 +805,21 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) !cpuset_cpumask_can_shrink(cur->effective_cpus, user_xcpus(trial))) goto out; + xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial- >exclusive_cpus); + + /* + * On v2 an unchanged, empty exclusive CPU mask cannot conflict with + * a sibling's CPU masks. Only the top cpuset is memory-exclusive. + */ + if (cpuset_v2() && cs_is_member(cur) && !xcpus_changed && + cpumask_empty(trial->exclusive_cpus)) + goto valid; + /* * If either I or some sibling (!= me) is exclusive, we can't * overlap. exclusive_cpus cannot overlap with each other if set. */ ret = -EINVAL; - xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial- >exclusive_cpus);
cpuset_for_each_child(c, css, par){
if (c == cur)
continue;kernel/cgroup/cpuset.c +1
-0@@ -820,6 +829,7 @@ int validate_change(struct cpuset *cur, struct cpuset *trial) goto out; } +valid: ret = 0;Quoted 2 lines
out:
rcu_read_unlock();Hi Chuyi,
Thanks for the patch. I think the condition can be simplified, and the sibling walk could be restructured a bit.
Since commit
85ad3963de96("cgroup/cpuset: RemoveCS_CPU_EXCLUSIVEhandling code from v2"), the v2 part ofcpus_excl_conflict()only looks attrial->exclusive_cpus:
xcpus_changed&& cpumask_subset(sibling->cpus_allowed, trial-Quoted 1 line
exclusive_cpus)
cpumask_intersects(trial->exclusive_cpus, sibling->exclusive_cpus)If
trial->exclusive_cpusis empty, neither of them can be true, whether or notexclusive_cpushas changed. The subset check also requires a non-emptysibling->cpus_allowed, and a non-empty mask cannot be a subset of an empty one. So !xcpus_changed is not needed.
cs_is_member(cur)is not needed either. Nothing in the loop depends on the partition state of @cur. For a partition root, conflicts with siblings are handled bycompute_trialcs_excpus()andpartition_cpus_change(), which invalidate the partition instead of failing the write. Dropping the check would let partition roots with an empty cpuset.cpus.exclusive skip the walk as well.On the memory side,
mems_excl_conflict()never fires on v2: onlytop_cpusetcan beCS_MEM_EXCLUSIVEthere, and it has no siblings.So the v1 and v2 sibling rules no longer share anything. How about moving the walk into one helper that hands v1 off to
cpuset-v1.cand does the v2 checks by default, with the empty check as an early return? Something like:In
cpuset.c:``` static bool siblings_excl_conflict(struct cpuset *cur, struct cpuset *trial) { struct cgroup_subsys_state *css; struct cpuset *c; bool xcpus_changed; if (!cpuset_v2()) return cpuset1_siblings_excl_conflict(cur, trial); /* An empty exclusive_cpus cannot conflict with any sibling */ if (cpumask_empty(trial->exclusive_cpus)) return false; xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial- >exclusive_cpus); cpuset_for_each_child(c, css, parent_cs(cur)) { if (c == cur) continue; if (xcpus_changed && !cpumask_empty(c->cpus_allowed) && cpumask_subset(c->cpus_allowed, trial->exclusive_cpus)) return true; if (cpumask_intersects(trial->exclusive_cpus, c->exclusive_cpus)) return true; } return false; } ```In
cpuset-v1.c:``` bool cpuset1_siblings_excl_conflict(struct cpuset *cur, struct cpuset *trial) { struct cgroup_subsys_state *css; struct cpuset *c; cpuset_for_each_child(c, css, parent_cs(cur)) { if (c == cur) continue; if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Quoted 8 lines
return true; if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && nodes_intersects(trial->mems_allowed, c->mems_allowed)) return true; } return false; } ```
Hi Ridong,
Thanks for pointing this out. I agree that an empty trial->exclusive_cpus is enough to skip the v2 sibling walk, even when the mask has changed or the cpuset is a partition root.
Would you mind if I picked this up and sent follow-up patches to simplify the v2 check and move the v1 sibling walk into cpuset-v1.c as you suggested?
Since you also sketched out the implementation, would you prefer a Suggested-by or a Co-developed-by tag? If you prefer Co-developed-by, could you also send your Signed-off-by?
Thanks,
Chuyi
Quoted 6 lines
Chuyi Zhou (4):
cgroup/cpuset: Skip the sched domain attribute allocation on v2 cgroup/cpuset: Check v2 partition overlap in a single pass cgroup/cpuset: Dropcpusets_overlap()
cgroup/cpuset: Skip sibling checks for v2 members without exclusive
CPUs
Applied 1-4 to cgroup/for-7.5 with the following tags added:
Thanks.
--
tejun