From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796061899; b=BkJrr8DyzdMfDXCqQOyM48sV8te59r6icsQ12P9G1A/nbpMtQv9KXtHnDVBPCSM38dpX GI0c3iQNlRQS1MuuEG1mUkUrAE/koB0tiYtNRwgi9/6TmxBUEshvEXGViQLuYIzXPZuG8 /cZdMcAj7OIz+z7nI7I0OrKurHDDsF46HIBwHIhAafVmWbOGLFOB1g+LL5aWhvG2lM2Dc Rs8ivwy5R8aC2h/mmE2freDrQLXDDA9RQd0N0czI/Vtqa1ts2OjYnQlnf9/TK0knuk4ur /xxgoc5U1+YIDpgqqOaoMdtm0kKD50qxrfBGXCf/zWnn0MdTXfvFUBTAj1rxQESnZIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796061899; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:From:To:Cc:References:Content-Language:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-Scanned-By; bh=y+5xtDHZdGGd0L5RyrykzG0LIrZHrc+CZH/lAFaTmoM=; b=icG8e8c0AaAWkj75ab/V7DVb5M2PNo1EYb9/mcqCi26zQJyQo6qM5EQ2ci45McOTwfEl dGW/4aB+SoBTOWk3Smgsnm4m3n+KA+L4mA4t4ZEsPKanKafZJYjp0ms4Tb8Coc9cXhCpZ tw5kTo6VGLxSTAy7l80XTHD2/BYQ3isqR0/jQElANJntk/EkEHToJX/ObHxX4F0Po3uxn ecxg/MP/TddG7HWJ+APoh65TfLfrQVG46OSHYNs7vb+WYQm1VGVvdPGkVyn7bp249G5S4 DgoxIbX2XTPFsV9pg/iHmIecJGC8VZxdvXiG9ibmhviGop6jNLIlhaoLJqLBKT8ihiw== 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=Vwm+dXDN; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 7C2B3606A325 for ; Mon, 30 Nov 2026 18:04:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7C2B3606A325 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=Vwm+dXDN DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796061898; 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=y+5xtDHZdGGd0L5RyrykzG0LIrZHrc+CZH/lAFaTmoM=; b=Vwm+dXDN2dIIPFkAbvP0D1nEZVxBFNBo9hcwjeUUlvTv6YKcsOCIwhqeK8VTFxnBNQ7MhU Od7+W9LJGq7dYkDjkL4NqkTal2f3G8M72tlFMw1CxT4YmihbIERe1rtXefH/YGsHN25Ofx 5qQpVRxARPqJWD69W93Mn+2P2ngYA5U= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-83-HebVLdZ7NaSmgGnzOjED9g-1; Mon, 30 Nov 2026 18:04:51 +0000 X-MC-Unique: HebVLdZ7NaSmgGnzOjED9g-1 X-Mimecast-MFC-AGG-ID: HebVLdZ7NaSmgGnzOjED9g_1796061889 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B8068180063B; Mon, 30 Nov 2026 18:04:49 +0000 (UTC) Received: from [100.91.18.181] (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8D2E01955F70; Mon, 30 Nov 2026 18:04:48 +0000 (UTC) Message-ID: Date: Mon, 30 Nov 2026 13:04:47 -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 From: Waiman Long 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> <92022370-93ee-4ae7-9afe-2af055f1772b@redhat.com> Content-Language: en-US In-Reply-To: <92022370-93ee-4ae7-9afe-2af055f1772b@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 On 11/30/26 1:02 PM, Waiman Long wrote: > 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 Reviewed-by: Waiman Long