From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.214 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795920341; b=JxL2JvPCzZnyqPcsvVUDeyvuQb/68sM6s4edso6+2q53X9HG9gOIBFrQBfSnrBypEgfm yXjvXrtVEq5+jSLyujS6O4Y3ZZWiLXCaxj+ZVpqQjEKt2rV2qr8v2mXGnZ2EYRg/DOxvH i/Jekz1gjJ9Lb11sTP1dBOB+YwUQzF75zqOPVpxG0LgzaqIvVUaq3X4R77E12qEd/MEYW YwAsyNHTMtRb1p+OIwo72h0Q0qFJrEP6LzIjUvtMPgRcTufQdjevLakJa3N36FHTSdxKu GeRFVgssvdczcOECs4W9B2j+bt9arjjJUtjY43QQ8C3nO/wTyMmxafcEmkRAebapecw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795920341; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Wm9GNmJorZBI1d0sgRhWArixYcn0I8lZRgz6B2pXHiw=; b=LR3jO9yfkD7SpyLflbmrvSIiy9dKUsk+nlCyB3yLx6bPEiQ5wsQJFrtAhCdqLvv2Xuqi PoQX5Hm184U5hhEnFYqljV99pzcHIDJPn9Ide5dWQzqzPt3mwfL5UHV0NOYD1Fea99kju enPCJ/MfKrakebmVEaL5UV9MW7lBPp45C66sMZiIHMXD+Ie0A6oCgnvfiUooJL4mw04kj 6xUO0U9F+YKJZGLg8zETzvWHTVjMJAOy4sAJzGl6m5mcW7vIBMknPMVcBFzt27Hxd43+a PXsDzxHcztCVvN5xZqUhNZkB5zwcvZMdh9bVZaEcw2XfHMLAcghR/1Ht51g388ggk8A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZfwTFkA3; arc=none smtp.remote-ip=95.215.58.214 Received: from mta1.migadu.com (out-214.mta1.migadu.com [95.215.58.214]) (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 A4416606A31C for ; Sun, 29 Nov 2026 02:45:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A4416606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZfwTFkA3 X-Envelope-To: cgroups@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=0sZuzm/zLf1WqhzNP0kAjmynrDyNMvHzIupoFxoshco=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795920338; v=1; x=1796525138; b=ZfwTFkA3oWR6XlQ/udR8sSKlpLqcjnlIwdDf23Y3jr/VlklWvyjCSS0k+3lXhNwM2GCdrG/5 jJ9Z3/vjEd6hKiRYCilI/vr7Qqbna2Flf2T8YVORi5eiGJtofgBHlzo2rL8VVkpvD5haKF5H9QK NF6nZZlG+jpQGSaqriRadulA= X-Envelope-To: cgroups@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 7aa009cfce6589a3; Sun, 29 Nov 2026 02:45:25 +0000 X-Mizu-Trace-ID: 7aa009cfce6589a3 X-Migadu-Flow: FLOW_OUT From: Ridong Chen To: Waiman Long , Tejun Heo , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= Cc: Ridong Chen , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Ridong Chen Subject: [PATCH v6 11/12] cpuset: introduce local_partition_update() Date: Sun, 29 Nov 2026 10:44:30 +0800 Message-Id: <20261129024431.2485721-12-ridong.chen@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261129024431.2485721-1-ridong.chen@linux.dev> References: <20261129024431.2485721-1-ridong.chen@linux.dev> X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ridong Chen Factor the partcmd_update handling without a new cpumask out of update_parent_effective_cpumask() into local_partition_update(), and the one with a new cpumask, used by partition_cpus_change(), into __local_partition_update(). __local_partition_update() validates the new effective exclusive CPUs with validate_local_partition(), then: - if the validation fails, a valid partition is invalidated with partition_disable() and its load balance flag is updated, while an invalid partition only has its error code updated. The error code is kept if the parent is not a valid partition root, e.g. for a remote partition invalidated by hotplug, as the cpuset can't become a valid local partition anyway. - if the validation passes, an invalid partition becomes valid again with partition_enable(), and a valid partition has its CPUs updated with partition_update(). An invalid partition owns no CPU, so tmp->delmask is cleared for it. Otherwise the housekeeping check would count its CPUs as released and skip the check for the remaining housekeeping CPUs, as the old partcmd_update path did not pass a delmask for an invalid partition either. local_partition_update() computes the new effective exclusive CPUs with compute_excpus() as before. As in the old partcmd_update path, the sibling exclusivity check is only done for an invalid partition with an empty cpuset.cpus.exclusive, as a valid partition has passed it already. cpuset_hotplug_update_tasks() now calls local_partition_update() for both the invalidation and the revival of a local partition, instead of open coding the invalidation conditions. The only exception is when subpartitions_cpus becomes empty because no CPU is left for top_cpuset, in which case the local partition is disabled directly with PERR_HOTPLUG. As before, an invalid partition is only revived if it has an active CPU in its exclusive CPUs, so it doesn't take CPUs back right after top_cpuset runs out of non-partition CPUs. A remote partition only needs the hotplug invalidation check, so jump to update_tasks right after it. As partition_disable() gives the CPUs back to the parent and updates effective_cpus of the cpuset directly, effective_cpus is saved before any partition state change and the new effective cpumask is compared against it. Otherwise the tasks of a partition invalidated by hotplug may be left with only the offlined CPUs. In update_cpumasks_hier(), the effective cpumask is always recomputed after local_partition_update(), as tmp->new_cpus may be reused by it. effective_cpus of the child is only updated after that, so an unvalidated cpumask is never exposed to its readers. Signed-off-by: Ridong Chen --- kernel/cgroup/cpuset.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 149 insertions(+), 56 deletions(-) diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index a32bf85e48ad..26061bb44798 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -2090,6 +2090,109 @@ static int local_partition_enable(struct cpuset *cs, return 0; } +/** + * __local_partition_update - Update local partition configuration + * @cs: Target cpuset to update + * @xcpus: New exclusive CPU mask + * @excpus: New effective exclusive CPU mask + * @tmp: Temporary mask storage for intermediate calculations + * @excl_check: Flag to enable exclusivity validation + * + * Handles updates to local CPU partition configurations by validating + * changes, managing state transitions, and propagating updates through + * the cpuset hierarchy. + * + * Note on exclusivity checking: Exclusivity validation is required when + * transitioning from an invalid to valid partition state. However, when + * updating cpus_allowed or exclusive_cpus, exclusivity should have already + * been verified by validate_change(). In such cases, excl_check must be + * false since the cs cpumasks are not yet updated. + * + * Return: Partition error code (PERR_NONE indicates success) + */ +static int __local_partition_update(struct cpuset *cs, struct cpumask *xcpus, + struct cpumask *excpus, struct tmpmasks *tmp, + bool excl_check) +{ + struct cpuset *parent = parent_cs(cs); + int part_error = PERR_NONE; /* Partition error? */ + int old_prs, new_prs; + + lockdep_assert_cpuset_lock_held(); + /* For local partition only */ + if (WARN_ON_ONCE(is_remote_partition(cs) || cs_is_member(cs))) + return PERR_NONE; + + old_prs = cs->partition_root_state; + /* + * If new_prs < 0, it might transition to valid partition state. + * Use absolute value for validation checks. + */ + new_prs = old_prs < 0 ? -old_prs : old_prs; + cpumask_andnot(tmp->addmask, excpus, cs->effective_xcpus); + /* + * An invalid partition doesn't own any CPU, so it gives nothing back + * and the housekeeping check must not count any CPU as released. + */ + if (is_partition_valid(cs)) + cpumask_andnot(tmp->delmask, cs->effective_xcpus, excpus); + else + cpumask_clear(tmp->delmask); + part_error = validate_local_partition(cs, new_prs, excpus, + excl_check, tmp); + if (part_error) { + /* + * An invalid partition only needs to update its error code. + * Keep the error code if the parent is not a valid partition + * root, e.g. for an invalidated remote partition, as the + * cpuset can't become a valid local partition anyway. + */ + if (is_partition_valid(cs)) { + partition_disable(cs, -new_prs, part_error, tmp); + update_partition_sd_lb(cs, old_prs); + } else if (is_partition_valid(parent)) { + WRITE_ONCE(cs->prs_err, part_error); + } + return part_error; + } + + /* Nothing changes, return PERR_NONE */ + if (new_prs == old_prs && + (cpumask_empty(tmp->addmask) && cpumask_empty(tmp->delmask))) + return PERR_NONE; + + /* + * If partition was previously invalid but now passes checks, + * re-enable it and update related flags. + * Otherwise, partition state doesn't change, only cpumasks change. + */ + if (is_partition_invalid(cs)) { + partition_enable(cs, parent, new_prs, excpus); + update_partition_sd_lb(cs, old_prs); + } else { + partition_update(cs, new_prs, xcpus, excpus, tmp); + } + + cpuset_update_tasks_cpumask(parent, tmp->addmask); + update_sibling_cpumasks(parent, cs, tmp); + return PERR_NONE; +} + +static int local_partition_update(struct cpuset *cs, struct tmpmasks *tmp) +{ + /* + * A valid partition has passed the exclusivity check already. An + * invalid partition has to pass it before becoming valid again when + * cpuset.cpus.exclusive is empty. + */ + bool excl_check = is_partition_invalid(cs) && + cpumask_empty(cs->exclusive_cpus); + + compute_excpus(cs, tmp->new_cpus); + return __local_partition_update(cs, NULL, tmp->new_cpus, tmp, + excl_check); +} + /** * update_parent_effective_cpumask - update effective_cpus mask of parent cpuset * @cs: The cpuset that requests change in partition root state @@ -2127,9 +2230,9 @@ static int local_partition_enable(struct cpuset *cs, * check for error and so partition_root_state and prs_err will be updated * directly. */ -static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, - struct cpumask *newmask, - struct tmpmasks *tmp) +static int __maybe_unused update_parent_effective_cpumask(struct cpuset *cs, int cmd, + struct cpumask *newmask, + struct tmpmasks *tmp) { struct cpuset *parent = parent_cs(cs); int adding; /* Adding cpus to parent's effective_cpus */ @@ -2149,23 +2252,6 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, adding = deleting = false; old_prs = new_prs = cs->partition_root_state; - if (cmd == partcmd_invalidate) { - if (is_partition_invalid(cs)) - return 0; - - /* - * Make the current partition invalid. - */ - if (is_partition_valid(parent)) - adding = cpumask_and(tmp->addmask, - cs->effective_xcpus, - parent->effective_xcpus); - if (old_prs > 0) - new_prs = -old_prs; - - goto write_error; - } - /* * The parent must be a partition root. * The new cpumask, if present, or the current cpus_allowed must @@ -2525,12 +2611,12 @@ static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp, rcu_read_unlock(); if (update_parent) { - update_parent_effective_cpumask(cp, partcmd_update, NULL, tmp); + local_partition_update(cp, tmp); /* * Capture current partition_root_state as it may have * changed. Always recompute effective cpumask as tmp->new_cpus * may be clobbered when update_sibling_cpumasks() is called - * within update_parent_effective_cpumask(). + * within local_partition_update(). */ new_prs = cp->partition_root_state; compute_effective_cpumask(tmp->new_cpus, cp, parent); @@ -2689,8 +2775,8 @@ static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs, trialcs->effective_xcpus, tmp); } else { if (!trialcs->prs_err) - update_parent_effective_cpumask(cs, partcmd_update, - trialcs->effective_xcpus, tmp); + __local_partition_update(cs, trialcs->exclusive_cpus, + trialcs->effective_xcpus, tmp, false); else if (is_partition_valid(cs)) partition_disable(cs, -cs->partition_root_state, trialcs->prs_err, tmp); @@ -4144,60 +4230,67 @@ void cpuset_force_rebuild(void) */ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) { - static cpumask_t new_cpus; + static cpumask_t new_cpus, old_cpus; static nodemask_t new_mems; bool cpus_updated; bool mems_updated; - int partcmd = -1; struct cpuset *parent; wait_attach_done_lock(); parent = parent_cs(cs); + /* + * Partition state changes below may update effective_cpus directly, + * save it to check if tasks need to be updated. + */ + cpumask_copy(&old_cpus, cs->effective_cpus); compute_effective_cpumask(&new_cpus, cs, parent); compute_effective_nodemask(&new_mems, cs, parent); if (!tmp || !cs->partition_root_state) goto update_tasks; - if (is_remote_partition(cs) && (cpumask_empty(subpartitions_cpus) || - (cpumask_empty(&new_cpus) && - partition_is_populated(cs, NULL)))) { - partition_disable(cs, -cs->partition_root_state, PERR_HOTPLUG, - tmp); - compute_effective_cpumask(&new_cpus, cs, parent); + /* + * Compute effective_cpus for valid partition root, may invalidate + * child partition roots if necessary. + */ + if (is_remote_partition(cs)) { + if (cpumask_empty(subpartitions_cpus) || + (cpumask_empty(&new_cpus) && + partition_is_populated(cs, NULL))) { + partition_disable(cs, -cs->partition_root_state, + PERR_HOTPLUG, tmp); + compute_effective_cpumask(&new_cpus, cs, parent); + } + goto update_tasks; } /* - * Force the partition to become invalid if either one of - * the following conditions hold: - * 1) empty effective cpus but not valid empty partition. - * 2) parent is invalid or doesn't grant any cpus to child - * partitions. - * 3) subpartitions_cpus is empty. + * The subpartitions_cpus may be cleared if no CPUs remain available for + * the top_cpuset. In this case, disable the local partition directly. + * Otherwise, local_partition_update() will handle the automatic + * transition between valid and invalid partition states. */ - if (is_local_partition(cs) && - (!is_partition_valid(parent) || - tasks_nocpu_error(parent, cs, &new_cpus) || - cpumask_empty(subpartitions_cpus))) - partcmd = partcmd_invalidate; + if (is_local_partition(cs) && cpumask_empty(subpartitions_cpus)) + partition_disable(cs, -cs->partition_root_state, PERR_HOTPLUG, + tmp); /* - * On the other hand, an invalid partition root may be transitioned - * back to a regular one if its effective xcpus has an active CPU. + * Only try to revive an invalid partition if it has an active CPU, + * or it may take back CPUs that other partitions need when top_cpuset + * has just run out of non-partition CPUs. */ - else if (is_partition_valid(parent) && is_partition_invalid(cs) && - cpumask_intersects(cs->effective_xcpus, cpu_active_mask)) - partcmd = partcmd_update; + else if (is_partition_valid(cs) || + cpumask_intersects(user_xcpus(cs), cpu_active_mask)) + local_partition_update(cs, tmp); - if (partcmd >= 0) { - update_parent_effective_cpumask(cs, partcmd, NULL, tmp); - if (is_partition_valid(cs) || (partcmd == partcmd_invalidate)) { - compute_effective_cpumask(&new_cpus, cs, parent); - cpuset_force_rebuild(); - } - } + /* + * Recompute effective CPU mask after partition state update: + * - For valid partitions: calculate partition-specific effective CPUs + * - For invalid partitions: compute member effective CPU mask + */ + compute_effective_cpumask(&new_cpus, cs, parent); update_tasks: - cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus); + cpus_updated = !cpumask_equal(&new_cpus, &old_cpus); mems_updated = !nodes_equal(new_mems, cs->effective_mems); if (!cpus_updated && !mems_updated) goto unlock; /* Hotplug doesn't affect this cpuset */ -- 2.34.1