From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796061749; b=Xi8nccDMbwgnxTNn25sgIzgEQahc6t3hXfNEpJ0ZydmHm/Cgw9SbQy3prOG3r8bUFkDc e3X3/M3Wg8bvnszMBxatKeOllKsruUKCY/8ZpVVcOBpFJ0Vi3yoDh8utmZQTUemxgnFpz ivN1ZjJrlhix+hGoIreIgBS6s2F+m+xNxGA57qoIV0SSret0EgLlpdzJPkdBGShwAYoVr Locd4AWBaGqk57XImy6P1qNqSrajJWInWP9/Gfka9djjbinNirsTwq3OKEGwaeRfjfXVt nftJ1UnnPmSQXK3BthrOyrPyK6ViD6GKAq3oC0RvBFfmcIXbV/gFzUdMay36LnQKFqA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796061749; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-Scanned-By; bh=CMACXnM21gKiCVZcXIW2jX0pcRh/s4kW4FTaFQm5ATU=; b=tre6rruweNycMlWeRIqRrcFBmK5rcPcxKQ2muj/fROCJtEfHC5g8obcUeU0tu3O4GedY 0/Rc0nAJ0JvyNoYvYW/FHuDgxBCDVPyCRBTmIqWqIaysPu2EOIkotPGU+03dvHrg3GQVU HcM0XDbud4VQepbzhVp62bfFH2e/6dqIqiFM7+bZbQjUkUx3JIf5fI3KLiW8f9u/5MYVC JOP/URv5QGILYKieBw+r/JaK9ALhWJS/+oFWtmG2gv0ne9T0L769ymkuNkuqtn6bWA247 Jw6/bL0ZoRweqR6kYdVYWPVuhgIEg8YNo40uZ5RJlekvOTvJs9w8yeU4empV3XfZiNg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LkYdXTaA; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63C00606A31C for ; Mon, 30 Nov 2026 18:02:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 63C00606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LkYdXTaA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796061748; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CMACXnM21gKiCVZcXIW2jX0pcRh/s4kW4FTaFQm5ATU=; b=LkYdXTaA9iiMGP1442hP8AGTXft5uqW/4UjfPYMMz1+bvxR3UpOEZtb47TXuPKYXiOXpXW n/rVrL9PHYhQgjO3HGREOUsD7dVdRpKUAniVnZHKAd3h8BtyWsxHmTxN3Tp1DvpaMFAY/q i1fdoUUd8W+/ufpru2SlsiYjMU95Pro= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-64-9f4zmVj7NH2xPD9d3gBlhQ-1; Mon, 30 Nov 2026 18:02:25 +0000 X-MC-Unique: 9f4zmVj7NH2xPD9d3gBlhQ-1 X-Mimecast-MFC-AGG-ID: 9f4zmVj7NH2xPD9d3gBlhQ_1796061743 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 16FFF195FCC7; Mon, 30 Nov 2026 18:02:23 +0000 (UTC) Received: from [100.91.18.181] (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DAE16343; Mon, 30 Nov 2026 18:02:21 +0000 (UTC) Message-ID: <92022370-93ee-4ae7-9afe-2af055f1772b@redhat.com> Date: Mon, 30 Nov 2026 13:02:21 -0500 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/4] cgroup/cpuset: Check v2 partition overlap in a single pass To: Chuyi Zhou , tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com, ridong.chen@linux.dev Cc: linux-kernel@vger.kernel.org References: <368f171e21da37609b8c1c3f1aee5461424b7a0b.1796039668.git.zhouchuyi@bytedance.com> Content-Language: en-US From: Waiman Long In-Reply-To: <368f171e21da37609b8c1c3f1aee5461424b7a0b.1796039668.git.zhouchuyi@bytedance.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 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 > Reviewed-by: Ridong Chen > --- > 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