* [PATCH 0/3] cgroup/cpuset: Speed up v2 mask updates and domain rebuilds
@ 2026-11-23 12:45 Chuyi Zhou
2026-11-23 12:45 ` [PATCH 1/3] cgroup/cpuset: Skip the sched domain attribute allocation on v2 Chuyi Zhou
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Chuyi Zhou @ 2026-11-23 12:45 UTC (permalink / raw)
To: longman, tj, hannes, mkoutny, ridong.chen; +Cc: linux-kernel, Chuyi Zhou
We run high-density container workloads on large bare-metal hosts using
cgroup v2. We may configure many root partitions on a single host.
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 (3):
cgroup/cpuset: Skip the sched domain attribute allocation on v2
cgroup/cpuset: Check v2 partition overlap in a single pass
cgroup/cpuset: Skip sibling checks for v2 members without exclusive
CPUs
kernel/cgroup/cpuset-internal.h | 1 -
kernel/cgroup/cpuset.c | 74 +++++++++++++++++----------------
2 files changed, 39 insertions(+), 36 deletions(-)
base-commit: a6ca40399708c4322e87448f6c47da7192d72895
--
2.20.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] cgroup/cpuset: Skip the sched domain attribute allocation on v2
2026-11-23 12:45 [PATCH 0/3] cgroup/cpuset: Speed up v2 mask updates and domain rebuilds Chuyi Zhou
@ 2026-11-23 12:45 ` Chuyi Zhou
2026-11-23 12:45 ` [PATCH 2/3] cgroup/cpuset: Check v2 partition overlap in a single pass Chuyi Zhou
2026-11-23 12:45 ` [PATCH 3/3] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs Chuyi Zhou
2 siblings, 0 replies; 4+ messages in thread
From: Chuyi Zhou @ 2026-11-23 12:45 UTC (permalink / raw)
To: longman, tj, hannes, mkoutny, ridong.chen; +Cc: linux-kernel, 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 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.
Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
---
kernel/cgroup/cpuset.c | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index c7b3a734cda8..633fd05f513e 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -867,7 +867,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;
@@ -875,7 +874,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 */
@@ -926,15 +924,8 @@ generate_doms:
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++) {
/*
@@ -946,8 +937,6 @@ generate_doms:
housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT));
else
cpumask_copy(doms[i], csa[i]->effective_cpus);
- if (dattr)
- dattr[i] = SD_ATTR_INIT;
}
done:
@@ -960,8 +949,12 @@ done:
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;
}
--
2.20.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/3] cgroup/cpuset: Check v2 partition overlap in a single pass
2026-11-23 12:45 [PATCH 0/3] cgroup/cpuset: Speed up v2 mask updates and domain rebuilds Chuyi Zhou
2026-11-23 12:45 ` [PATCH 1/3] cgroup/cpuset: Skip the sched domain attribute allocation on v2 Chuyi Zhou
@ 2026-11-23 12:45 ` Chuyi Zhou
2026-11-23 12:45 ` [PATCH 3/3] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs Chuyi Zhou
2 siblings, 0 replies; 4+ messages in thread
From: Chuyi Zhou @ 2026-11-23 12:45 UTC (permalink / raw)
To: longman, tj, hannes, mkoutny, ridong.chen; +Cc: linux-kernel, 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_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
Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
---
kernel/cgroup/cpuset-internal.h | 1 -
kernel/cgroup/cpuset.c | 41 +++++++++++++++++----------------
2 files changed, 21 insertions(+), 21 deletions(-)
diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
index 1ba94ffaff9d..42abe241a251 100644
--- a/kernel/cgroup/cpuset-internal.h
+++ b/kernel/cgroup/cpuset-internal.h
@@ -257,7 +257,6 @@ static inline int is_spread_slab(const struct cpuset *cs)
}
/*
- * Helper routine for generate_sched_domains().
* Do cpusets a, b have overlapping effective cpus_allowed masks?
*/
static inline int cpusets_overlap(struct cpuset *a, struct cpuset *b)
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 633fd05f513e..2e09abb9e864 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -119,6 +119,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.
@@ -865,10 +871,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);
@@ -907,14 +913,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);
}
}
@@ -3307,13 +3315,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;
@@ -3321,15 +3322,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;
@@ -3915,7 +3916,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,
--
2.20.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 3/3] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs
2026-11-23 12:45 [PATCH 0/3] cgroup/cpuset: Speed up v2 mask updates and domain rebuilds Chuyi Zhou
2026-11-23 12:45 ` [PATCH 1/3] cgroup/cpuset: Skip the sched domain attribute allocation on v2 Chuyi Zhou
2026-11-23 12:45 ` [PATCH 2/3] cgroup/cpuset: Check v2 partition overlap in a single pass Chuyi Zhou
@ 2026-11-23 12:45 ` Chuyi Zhou
2 siblings, 0 replies; 4+ messages in thread
From: Chuyi Zhou @ 2026-11-23 12:45 UTC (permalink / raw)
To: longman, tj, hannes, mkoutny, ridong.chen; +Cc: linux-kernel, Chuyi Zhou
Changing 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
Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
---
kernel/cgroup/cpuset.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 2e09abb9e864..6c47cdb4a244 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -804,12 +804,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;
@@ -819,6 +828,7 @@ int validate_change(struct cpuset *cur, struct cpuset *trial)
goto out;
}
+valid:
ret = 0;
out:
rcu_read_unlock();
--
2.20.1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 12:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 12:45 [PATCH 0/3] cgroup/cpuset: Speed up v2 mask updates and domain rebuilds Chuyi Zhou
2026-11-23 12:45 ` [PATCH 1/3] cgroup/cpuset: Skip the sched domain attribute allocation on v2 Chuyi Zhou
2026-11-23 12:45 ` [PATCH 2/3] cgroup/cpuset: Check v2 partition overlap in a single pass Chuyi Zhou
2026-11-23 12:45 ` [PATCH 3/3] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs Chuyi Zhou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®