From: Guopeng Zhang <guopeng.zhang@linux.dev>
To: Waiman Long <longman@redhat.com>, Ridong Chen <ridong.chen@linux.dev>
Cc: "Tejun Heo" <tj@kernel.org>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
"Guopeng Zhang" <zhangguopeng@kylinos.cn>
Subject: [PATCH v4 2/7] cgroup/cpuset: Account for child CPU ownership in partition changes
Date: Thu, 10 Sep 2026 17:45:41 +0800 [thread overview]
Message-ID: <20260910094546.5852-3-guopeng.zhang@linux.dev> (raw)
In-Reply-To: <20260910094546.5852-1-guopeng.zhang@linux.dev>
From: Guopeng Zhang <zhangguopeng@kylinos.cn>
effective_xcpus includes CPUs granted to valid child partitions. A change
to the parent must not apply its isolation state or housekeeping checks to
CPUs which remain owned by those children.
For example, on a cgroup v2 system with CPUs 0-3 online:
cd /sys/fs/cgroup
echo +cpuset > cgroup.subtree_control
mkdir type-repro
echo 1-3 > type-repro/cpuset.cpus
echo isolated > type-repro/cpuset.cpus.partition
echo +cpuset > type-repro/cgroup.subtree_control
mkdir type-repro/child
echo 2-3 > type-repro/child/cpuset.cpus
echo isolated > type-repro/child/cpuset.cpus.partition
echo root > type-repro/cpuset.cpus.partition
cat cpuset.cpus.isolated
The isolated mask should still contain CPUs 2-3 after the parent becomes a
root partition. Without this change, those CPUs are removed even though
the child remains isolated.
Compute the CPUs owned directly by a partition by excluding CPUs granted
to valid children. When validating a trial parent mask, exclude only CPUs
granted to children that will remain valid under that mask. Reuse the child
validation rules so PERR_INVCPUS and PERR_NOCPUS are handled consistently.
Use the directly owned mask for root/isolated type changes and housekeeping
validation. If a root child returns the last housekeeping CPU to an
isolated parent, invalidate the outermost isolated ancestor so the child
can still become a member without violating the housekeeping constraint.
Force the hierarchy update from the invalidated ancestor. Otherwise an
unchanged member cpuset can cause its subtree to be skipped, leaving task
CPU masks or descendant partition states stale.
Link: https://sashiko.dev/#/patchset/20260820124202.517160-1-guopeng.zhang%40linux.dev?part=6
Link: https://sashiko.dev/#/patchset/20260828095643.13395-1-guopeng.zhang@linux.dev?part=1
Link: https://sashiko.dev/#/patchset/20260902102615.79189-1-guopeng.zhang@linux.dev?part=2
Fixes: 4a74e418881f ("cgroup/cpuset: Check partition conflict with housekeeping setup")
Fixes: 11e5f407b64a ("cgroup/cpuset: Keep track of CPUs in isolated partitions")
Fixes: 103b08709e8a ("cgroup/cpuset: Fail if isolated and nohz_full don't leave any housekeeping")
Fixes: b1034a690129 ("cgroup/cpuset: Ensure domain isolated CPUs stay in root or isolated partition")
Signed-off-by: Guopeng Zhang <zhangguopeng@kylinos.cn>
---
kernel/cgroup/cpuset.c | 142 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 132 insertions(+), 10 deletions(-)
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index a2514fcb1144..994ddb79272d 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -2184,6 +2184,44 @@ static void compute_partition_effective_cpumask(struct cpuset *cs,
rcu_read_unlock();
}
+/*
+ * Compute CPUs owned directly by a partition under @parent_xcpus by excluding
+ * CPUs granted to children that remain valid under that mask.
+ */
+static void compute_partition_owned_cpumask(struct cpuset *cs,
+ const struct cpumask *parent_xcpus,
+ struct cpumask *owned_cpus,
+ struct cpumask *remaining_ecpus)
+{
+ struct cgroup_subsys_state *css;
+ struct cpuset *child;
+ bool populated = partition_is_populated(cs, NULL);
+
+ lockdep_assert_held(&cpuset_mutex);
+ cpumask_copy(owned_cpus, parent_xcpus);
+ cpumask_and(remaining_ecpus, parent_xcpus, cpu_active_mask);
+
+ rcu_read_lock();
+ cpuset_for_each_child(child, css, cs) {
+ if (!is_partition_valid(child))
+ continue;
+
+ /*
+ * A child that would become invalid under the proposed
+ * configuration cannot retain ownership of its CPUs.
+ */
+ if (cs_partition_error(child, parent_xcpus,
+ remaining_ecpus, populated))
+ continue;
+
+ cpumask_andnot(owned_cpus, owned_cpus,
+ child->effective_xcpus);
+ cpumask_andnot(remaining_ecpus, remaining_ecpus,
+ child->effective_xcpus);
+ }
+ rcu_read_unlock();
+}
+
/*
* update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
* @cs: the cpuset to consider
@@ -2424,13 +2462,18 @@ static int parse_cpuset_cpulist(const char *buf, struct cpumask *out_mask)
* validate_partition - Validate a cpuset partition configuration
* @cs: The cpuset to validate
* @trialcs: The trial cpuset containing proposed configuration changes
+ * @owned_cpus: Scratch mask for CPUs owned directly by the trial partition
+ * @remaining_ecpus: Scratch mask used to predict valid child partitions
*
* If any validation check fails, the appropriate error code is set in the
* cpuset's prs_err field.
*
* Return: PRS error code (0 if valid, non-zero error code if invalid)
*/
-static enum prs_errcode validate_partition(struct cpuset *cs, struct cpuset *trialcs)
+static enum prs_errcode validate_partition(struct cpuset *cs,
+ struct cpuset *trialcs,
+ struct cpumask *owned_cpus,
+ struct cpumask *remaining_ecpus)
{
struct cpuset *parent = parent_cs(cs);
@@ -2440,8 +2483,10 @@ static enum prs_errcode validate_partition(struct cpuset *cs, struct cpuset *tri
if (cpumask_empty(trialcs->effective_xcpus))
return PERR_INVCPUS;
+ compute_partition_owned_cpumask(cs, trialcs->effective_xcpus,
+ owned_cpus, remaining_ecpus);
if (prstate_housekeeping_conflict(trialcs->partition_root_state,
- trialcs->effective_xcpus))
+ owned_cpus))
return PERR_HKEEPING;
if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus))
@@ -2467,7 +2512,8 @@ static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs,
if (cs_is_member(cs))
return;
- prs_err = validate_partition(cs, trialcs);
+ prs_err = validate_partition(cs, trialcs, tmp->new_cpus,
+ tmp->addmask);
if (prs_err) {
WRITE_ONCE(cs->prs_err, prs_err);
trialcs->prs_err = prs_err;
@@ -2946,6 +2992,49 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
return err;
}
+/*
+ * Invalidate the highest isolated partition that contains @cs.
+ *
+ * A root partition returning CPUs to an isolated parent can consume the last
+ * housekeeping CPU. Invalidating the highest isolated ancestor lets the
+ * subsequent hierarchy update propagate invalidation down the chain and
+ * return the CPUs to a root partition.
+ */
+static struct cpuset *invalidate_isolated_ancestor(struct cpuset *cs,
+ struct tmpmasks *tmp)
+{
+ struct cpuset *ancestor = parent_cs(cs);
+ struct cpuset *parent;
+ int err;
+
+ lockdep_assert_held(&cpuset_mutex);
+ if (WARN_ON_ONCE(!ancestor))
+ return NULL;
+
+ while ((ancestor != &top_cpuset) &&
+ !is_remote_partition(ancestor)) {
+ parent = parent_cs(ancestor);
+ if (!parent ||
+ parent->partition_root_state != PRS_ISOLATED)
+ break;
+ ancestor = parent;
+ }
+
+ if (WARN_ON_ONCE(ancestor == &top_cpuset))
+ return NULL;
+
+ WRITE_ONCE(ancestor->prs_err, PERR_HKEEPING);
+ if (is_remote_partition(ancestor)) {
+ remote_partition_disable(ancestor, tmp);
+ } else {
+ err = update_parent_effective_cpumask(ancestor,
+ partcmd_invalidate, NULL, tmp);
+ WARN_ON_ONCE(err);
+ }
+
+ return ancestor;
+}
+
/**
* update_prstate - update partition_root_state
* @cs: the cpuset to update
@@ -2958,6 +3047,8 @@ static int update_prstate(struct cpuset *cs, int new_prs)
{
int err = PERR_NONE, old_prs = cs->partition_root_state;
struct cpuset *parent = parent_cs(cs);
+ struct cpuset *invalidated = NULL;
+ struct cpumask *isolcpus_update_cpus = cs->effective_xcpus;
struct tmpmasks tmpmask;
bool isolcpus_updated = false;
@@ -3014,19 +3105,38 @@ static int update_prstate(struct cpuset *cs, int new_prs)
} else if (old_prs && new_prs) {
/*
* A change in load balance state only, no change in cpumasks.
- * Need to update isolated_cpus.
+ * Need to update isolated_cpus for CPUs owned by this partition,
+ * excluding CPUs distributed to valid child partitions.
*/
+ compute_partition_owned_cpumask(cs, cs->effective_xcpus,
+ tmpmask.new_cpus,
+ tmpmask.addmask);
if (((new_prs == PRS_ISOLATED) &&
- !isolated_cpus_can_update(cs->effective_xcpus, NULL)) ||
- prstate_housekeeping_conflict(new_prs, cs->effective_xcpus))
+ !isolated_cpus_can_update(tmpmask.new_cpus, NULL)) ||
+ prstate_housekeeping_conflict(new_prs, tmpmask.new_cpus)) {
err = PERR_HKEEPING;
- else
+ } else {
+ /*
+ * Only directly owned CPUs change isolation state for a
+ * successful root <-> isolated type change.
+ */
+ isolcpus_update_cpus = tmpmask.new_cpus;
isolcpus_updated = true;
+ }
} else {
/*
* Switching back to member is always allowed even if it
- * disables child partitions.
+ * disables child partitions. If returning CPUs to an isolated
+ * parent would consume the last housekeeping CPU, invalidate
+ * the outermost isolated ancestor and return its CPUs instead.
*/
+ if (old_prs == PRS_ROOT &&
+ parent->partition_root_state == PRS_ISOLATED &&
+ !isolated_cpus_can_update(cs->effective_xcpus, NULL))
+ invalidated = invalidate_isolated_ancestor(cs, &tmpmask);
+ if (invalidated)
+ goto out;
+
if (is_remote_partition(cs))
remote_partition_disable(cs, &tmpmask);
else
@@ -3054,11 +3164,23 @@ static int update_prstate(struct cpuset *cs, int new_prs)
if (!is_partition_valid(cs))
reset_partition_data(cs);
else if (isolcpus_updated)
- isolated_cpus_update(old_prs, new_prs, cs->effective_xcpus);
+ isolated_cpus_update(old_prs, new_prs,
+ isolcpus_update_cpus);
spin_unlock_irq(&callback_lock);
/* Force update if switching back to member & update effective_xcpus */
- update_cpumasks_hier(cs, &tmpmask, !new_prs);
+ if (invalidated) {
+ /*
+ * Ancestor invalidation changes the partition hierarchy. Force the
+ * traversal so an unchanged member cpuset does not cause its subtree
+ * to be skipped.
+ */
+ update_cpumasks_hier(invalidated, &tmpmask, true);
+ update_partition_sd_lb(invalidated, PRS_ISOLATED);
+ notify_partition_change(invalidated, PRS_ISOLATED);
+ } else {
+ update_cpumasks_hier(cs, &tmpmask, !new_prs);
+ }
/* A newly created partition must have effective_xcpus set */
WARN_ON_ONCE(!old_prs && (new_prs > 0)
--
2.43.0
next prev parent reply other threads:[~2026-09-10 9:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-10 9:45 [PATCH v4 0/7] cgroup/cpuset: Fix partition transitions and invalidation Guopeng Zhang
2026-09-10 9:45 ` [PATCH v4 1/7] cgroup/cpuset: Factor out child partition validation Guopeng Zhang
2026-09-10 9:45 ` Guopeng Zhang [this message]
2026-09-10 9:45 ` [PATCH v4 3/7] cgroup/cpuset: Release CPUs when type-change validation fails Guopeng Zhang
2026-09-10 9:45 ` [PATCH v4 4/7] cgroup/cpuset: Fix child invalidation after parent CPU changes Guopeng Zhang
2026-09-10 9:45 ` [PATCH v4 5/7] cgroup/cpuset: Fix isolation accounting on propagated invalidation Guopeng Zhang
2026-09-10 9:45 ` [PATCH v4 6/7] cgroup/cpuset: Publish cpus_allowed before partition updates Guopeng Zhang
2026-09-10 9:45 ` [PATCH v4 7/7] cgroup/cpuset: Publish exclusive_cpus " Guopeng Zhang
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=20260910094546.5852-3-guopeng.zhang@linux.dev \
--to=guopeng.zhang@linux.dev \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mkoutny@suse.com \
--cc=ridong.chen@linux.dev \
--cc=tj@kernel.org \
--cc=zhangguopeng@kylinos.cn \
/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®