From: Waiman Long <longman@redhat.com>
To: Chuyi Zhou <zhouchuyi@bytedance.com>,
tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com,
ridong.chen@linux.dev
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/4] cgroup/cpuset: Check v2 partition overlap in a single pass
Date: Mon, 30 Nov 2026 13:02:21 -0500 [thread overview]
Message-ID: <92022370-93ee-4ae7-9afe-2af055f1772b@redhat.com> (raw)
In-Reply-To: <368f171e21da37609b8c1c3f1aee5461424b7a0b.1796039668.git.zhouchuyi@bytedance.com>
On 11/30/26 7:04 AM, Chuyi Zhou wrote:
> 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>
> Reviewed-by: Ridong Chen <ridong.chen@linux.dev>
> ---
> kernel/cgroup/cpuset-internal.h | 2 +-
> kernel/cgroup/cpuset.c | 41 +++++++++++++++++----------------
> 2 files changed, 22 insertions(+), 21 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
> index 505682493d81..5c80f2798825 100644
> --- a/kernel/cgroup/cpuset-internal.h
> +++ b/kernel/cgroup/cpuset-internal.h
> @@ -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)
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 23348b6b40f6..fb2c08b31b53 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -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;
> @@ -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 <longman@redhat.com>
next prev parent reply other threads:[~2026-11-30 18:02 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 12:04 [PATCH v2 0/4] cgroup/cpuset: Speed up v2 mask updates and domain rebuilds Chuyi Zhou
2026-11-30 12:04 ` [PATCH v2 1/4] cgroup/cpuset: Skip the sched domain attribute allocation on v2 Chuyi Zhou
2026-11-30 17:32 ` Waiman Long
2026-11-30 12:04 ` [PATCH v2 2/4] cgroup/cpuset: Check v2 partition overlap in a single pass Chuyi Zhou
2026-11-30 18:02 ` Waiman Long [this message]
2026-11-30 18:04 ` Waiman Long
2026-11-30 12:04 ` [PATCH v2 3/4] cgroup/cpuset: Drop cpusets_overlap() Chuyi Zhou
2026-11-30 18:03 ` Waiman Long
2026-11-30 18:11 ` Waiman Long
2026-11-30 12:04 ` [PATCH v2 4/4] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs Chuyi Zhou
2026-11-30 18:04 ` Waiman Long
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=92022370-93ee-4ae7-9afe-2af055f1772b@redhat.com \
--to=longman@redhat.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mkoutny@suse.com \
--cc=ridong.chen@linux.dev \
--cc=tj@kernel.org \
--cc=zhouchuyi@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®