From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.230.115 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796007694; b=FJZtYqRmxp72J3eNlSHlqN91KhiJUMsGL4AM1xVxEdyfAKmT+hkx6ASlJWUIVqHKIaz4 LrlE17rssfbAUENCW/YwzmMReIaxN8Gn4f726KSsf7qcalbufua4bq4qYY/nmY8gN+jSb UxaAVTz/0oQeHbsF6i4fBDkdVsaNy4kab2tkn11ThFbK+oLYGxtv+YdMCcsL9QHhN7aLE 1CFnpMszYz+ZWtHDYx8mStuxC0xqDvVIvECzU+Eg2uzPi/+yOOeeo6d0Ev2j8xaEwT+hk eHlgjXkqjVr6ROHjvr9yvJAQkgFonEfhsUximJm+iRVStshj4Zrj4r06jrEJis8+wbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796007694; h=DMARC-Filter:DKIM-Signature:To:Date:Message-Id:Mime-Version: In-Reply-To:From:User-Agent:Content-Type:References:Cc: X-Lms-Return-Path:X-Original-From:Subject:Content-Transfer-Encoding; bh=UeoCFKC6EeP3qCaeIC4sfYbO7xqB85kio3P0BDW2pBY=; b=fy7dq4HpI1xIAlpV3Gnq8hshU/nh1efpTnr+sJ0KsFQ/g52hlLM2/Ikg91x3VFiBALQX AO/SkusAR4mgPkxPCUG0HuYFO07NaqXgiZ2jmdDK1KCZREHduMRJq9NkFXquIQePtEanj w83Uls8NWbLaPYk4qA80wvhVmmmpCqqm2KYF1sqTjUI21mTRw43OyaMxHm6O1ztI4qOqu 0Y9u9dJKLBLyptAfI/gCXzFL4CgmrXW4UPPiV35gTtIf+0Tnd1m24JM8pGg7qqT5APJBP +lFPDzQA7VEa5UKQoy6Q5cHIqKcxRjZFu1goRjgB71aW6Yvqj3WANEjCCOyWdT7QGqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=XE0ykABo; arc=none smtp.remote-ip=209.127.230.115 Received: from va-1-115.ptr.blmpb.com (va-1-115.ptr.blmpb.com [209.127.230.115]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90E11606A31C for ; Mon, 30 Nov 2026 03:01:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 90E11606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=XE0ykABo DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1796007689; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=UeoCFKC6EeP3qCaeIC4sfYbO7xqB85kio3P0BDW2pBY=; b=XE0ykABozopug6nd/d32fbvOL4mKBu3ijjIWbHgsEpBnuMso5615QvJkxUHS3/EgI8MRcY Nkb5J4DlTVT+RDIjjkbrhLBdcgWUKcE+eg755VIDJU25bzW5JgtiJDClslnTymOKeq7yac BJQHcqUoSjC96CyR35wRNMaDNndhx6783T6C2PjObwYF0myY7TdI13Tknkw7JvgWUKBaZT AbZj0BBiqoEe339KXNl0eOpgxsiX/9GO293WL3nMBVggRw1Mzfw5ruANMqPS8TsUGW4K2X FrX98ZqeTkqyvGROipotpcbc6XyMSLwOYYPsasGmvu20PPz4GAWYLn2TD3wzwg== To: "Ridong Chen" , , , , Date: Mon, 30 Nov 2026 11:01:05 +0800 Message-Id: <5731a51e-973c-4135-9d83-cd9a41028299@bytedance.com> 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 In-Reply-To: <628e83e6-fc30-4c02-a067-ee6ad9b74666@linux.dev> From: "Chuyi Zhou" User-Agent: Mozilla Thunderbird Content-Type: text/plain; charset=UTF-8 References: <95bc3ba0a481b2ef09ce8e3258c78b58b6a42a02.1795436486.git.zhouchuyi@bytedance.com> <628e83e6-fc30-4c02-a067-ee6ad9b74666@linux.dev> Cc: X-Lms-Return-Path: X-Original-From: Chuyi Zhou Subject: Re: [PATCH 2/3] cgroup/cpuset: Check v2 partition overlap in a single pass Content-Transfer-Encoding: quoted-printable On 2026-11-28 9:46 a.m., Ridong Chen wrote: >=20 >=20 > On 11/23/2026 8:45 PM, 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. >> >> =C2=A0=C2=A0=C2=A0=C2=A0 Partitions=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Before= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 After >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A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=C2=A0=C2=A0=C2=A0=C2=A0 142.305=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 5.424 >> >> Signed-off-by: Chuyi Zhou >=20 > The numbers look nice. I'm not sure how often this is a bottleneck in > practice, but it's a simple change and a clear win, so I'm all for it. >=20 In production, we have hosts with many root partitions. We use CPU PSI=20 from the workloads in each partition to adjust its CPU allocation at=20 runtime. Those adjustments can trigger scheduling domain rebuilds, which=20 is why we started looking at the rebuild cost. >> --- >> =C2=A0 kernel/cgroup/cpuset-internal.h |=C2=A0 1 - >> =C2=A0 kernel/cgroup/cpuset.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 | 41 +++++++++++++++++---------------- >> =C2=A0 2 files changed, 21 insertions(+), 21 deletions(-) >> >> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-=20 >> 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=20 >> cpuset *cs) >> =C2=A0 } >> =C2=A0 /* >> - * Helper routine for generate_sched_domains(). >> =C2=A0=C2=A0 * Do cpusets a, b have overlapping effective cpus_allowed m= asks? >> =C2=A0=C2=A0 */ >=20 > cpuset1_generate_sched_domains() still uses it, so maybe say that > instead of dropping the line. >=20 > With this change, cpusets_overlap() is only used by > cpuset1_generate_sched_domains(), and the rest of cpuset-v1.c already > calls cpumask_intersects() directly. Maybe just open-code it there and > remove the helper from cpuset-internal.h, either in this patch or as a > separate cleanup. Agreed. I'll add a separate cleanup patch to use cpumask_intersects()=20 directly in the v1 code and remove cpusets_overlap(). Thanks. >=20 >> =C2=A0 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[] =3D { >> =C2=A0 static DEFINE_MUTEX(cpuset_top_mutex); >> =C2=A0 static DEFINE_MUTEX(cpuset_mutex); >> +/* >> + * Scratch mask for task attachment and sched domain generation. Each= =20 >> user >> + * must initialize the mask and hold cpuset_mutex for its entire use. >> + */ >> +static cpumask_var_t cpuset_scratch_mask; >> + >> =C2=A0 /* >> =C2=A0=C2=A0 * File level internal variables below follow one of the fol= lowing=20 >> exclusion >> =C2=A0=C2=A0 * rules. >> @@ -865,10 +871,10 @@ static int generate_sched_domains(cpumask_var_t=20 >> **domains, >> =C2=A0 { >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct cpuset *cp;=C2=A0=C2=A0=C2=A0 /* t= op-down scan of cpusets */ >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct cpuset **csa;=C2=A0=C2=A0=C2=A0 /*= array of all cpuset ptrs */ >> -=C2=A0=C2=A0=C2=A0 int i, j;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = /* indices for partition finding loops */ >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpumask_var_t *doms;=C2=A0=C2=A0=C2=A0 /*= resulting partition; i.e. sched=20 >> domains */ >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 int ndoms =3D 0;=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 /* number of sched domains in result */ >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct cgroup_subsys_state *pos_css; >> +=C2=A0=C2=A0=C2=A0 int i; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!cpuset_v2()) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return cpuset1_ge= nerate_sched_domains(domains, attributes); >> @@ -907,14 +913,16 @@ static int generate_sched_domains(cpumask_var_t=20 >> **domains, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 rcu_read_unlock(); >> -=C2=A0=C2=A0=C2=A0 for (i =3D 0; i < ndoms; i++) { >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 for (j =3D i + 1; j < ndoms;= j++) { >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (= cpusets_overlap(csa[i], csa[j])) >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 /* >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 * Cgroup v2 shouldn't pass down overlapping >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 * partition root cpusets. >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 */ >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 WARN_ON_ONCE(1); >> +=C2=A0=C2=A0=C2=A0 /* Each CPU can belong to only one load-balancing pa= rtition. */ >> +=C2=A0=C2=A0=C2=A0 if (ndoms > 1) { >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct cpumask *seen =3D cpu= set_scratch_mask; >> + >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpumask_copy(seen, csa[0]->e= ffective_cpus); >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 for (i =3D 1; i < ndoms; i++= ) { >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cons= t struct cpumask *cpus =3D csa[i]->effective_cpus; >> + >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WARN= _ON_ONCE(cpumask_intersects(seen, cpus)); >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpum= ask_or(seen, seen, cpus); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } >=20 > A pair of masks overlaps iff some mask intersects the union of the > ones before it, so this is equivalent, and there was only one > WARN_ON_ONCE() before as well. Sharing the scratch mask with > cpuset_attach_task() is fine: both run under cpuset_mutex, overwrite > the mask before reading it, and don't nest. >=20 >=20 >> @@ -3307,13 +3315,6 @@ static void cpuset_cancel_attach(struct=20 >> cgroup_taskset *tset) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 mutex_unlock(&cpuset_mutex); >> =C2=A0 } >> -/* >> - * Protected by cpuset_mutex. cpus_attach is used only by=20 >> cpuset_attach_task() >> - * but we can't allocate it dynamically there.=C2=A0 Define it global a= nd >> - * allocate from cpuset_init(). >> - */ >> -static cpumask_var_t cpus_attach; >> - >> =C2=A0 static void cpuset_attach_task(struct cpuset *cs, struct task_str= uct=20 >> *task) >> =C2=A0 { >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct mm_struct *mm; >> @@ -3321,15 +3322,15 @@ static void cpuset_attach_task(struct cpuset=20 >> *cs, struct task_struct *task) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 lockdep_assert_cpuset_lock_held(); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (cs !=3D &top_cpuset) >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 guarantee_active_cpus(task, = cpus_attach); >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 guarantee_active_cpus(task, = cpuset_scratch_mask); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 else >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpumask_andnot(cpus_attach, = task_cpu_possible_mask(task), >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpumask_andnot(cpuset_scratc= h_mask,=20 >> task_cpu_possible_mask(task), >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 subpartitions_cpus); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * can_attach beforehand should guar= antee that this doesn't >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * fail.=C2=A0 TODO: have a better w= ay to handle failure here >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ >> -=C2=A0=C2=A0=C2=A0 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)= ); >> +=C2=A0=C2=A0=C2=A0 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpuset_scrat= ch_mask)); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (cpuset_v2() && !attach_ctx.mems_updat= ed) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return; >> @@ -3915,7 +3916,7 @@ int __init cpuset_init(void) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpuset1_init(&top_cpuset); >> -=C2=A0=C2=A0=C2=A0 BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))= ; >> +=C2=A0=C2=A0=C2=A0 BUG_ON(!alloc_cpumask_var(&cpuset_scratch_mask, GFP_= KERNEL)); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (housekeeping_enabled(HK_TYPE_DOMAIN_B= OOT)) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cpumask_andnot(is= olated_cpus, cpu_possible_mask, >=20 > Overall, it looks good to me. >=20 > Reviewed-by: Ridong Chen > Thanks. >