[PATCH v6 02/12] cpuset: introduce partition_disable()

From: Ridong Chen

Date: Sat Nov 28 2026 - 21:45:00 EST


From: Ridong Chen <chenridong@xxxxxxxxxx>

Add partition_disable() to consolidate updates to key cpuset structures
during partition disablement, including:
1. remote_partition
2. effective_xcpus
3. partition_root_state
4. prs_err

Key operations performed:
1. Cleaning up remote_partition
2. Removing exclusive CPUs via partition_xcpus_del()
3. Recomputing effective exclusive CPUs mask
4. Updating partition state and error status
5. Triggering required scheduler domain rebuilds
6. Sending partition change notifications
7. Propagating the change of the parent's effective_cpus

Only the CPUs actually granted by the parent are given back, as the
parent may have shrunk. An invalid partition, or a partition under an
invalid parent, has already given back its exclusive CPUs, so there is
nothing to delete. A remote partition gets its CPUs from top_cpuset,
which owns all the CPUs.

effective_xcpus is only recomputed when exclusive_cpus is set. With an
empty exclusive_cpus, it is cleared by reset_partition_data() right
after, so the sibling scan in compute_excpus() would be wasted work
under callback_lock.

The parent is derived from @cs, top_cpuset for a remote partition.
When a valid partition gives back its CPUs, the change of the parent's
effective_cpus is propagated to the tasks of the parent and to its
other children. A member or an invalid partition only has its state
updated, so that all the callers can call partition_disable()
directly.

Signed-off-by: Ridong Chen <chenridong@xxxxxxxxxx>
---
kernel/cgroup/cpuset.c | 61 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 37b28159d37a..cfad92cf89d6 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -1764,6 +1764,67 @@ static void __maybe_unused partition_enable(struct cpuset *cs, struct cpuset *pa
notify_partition_change(cs, old_prs);
}

+/**
+ * partition_disable - Disable partition root state for a cpuset
+ * @cs: The cpuset to disable partition for
+ * @new_prs: New partition root state (should be non-positive)
+ * @prs_err: Error code to set if disabling due to validation failure
+ * @tmp: Temporary masks
+ *
+ * A valid partition gives its CPUs back to its parent (top_cpuset for a
+ * remote partition), and the change is propagated down the hierarchy.
+ */
+static void __maybe_unused partition_disable(struct cpuset *cs, int new_prs,
+ enum prs_errcode prs_err,
+ struct tmpmasks *tmp)
+{
+ struct cpuset *parent = is_remote_partition(cs) ? NULL : parent_cs(cs);
+ int old_prs = cs->partition_root_state;
+
+ lockdep_assert_cpuset_lock_held();
+ WARN_ON_ONCE(new_prs > 0);
+ WARN_ON_ONCE(!cpuset_v2());
+ /*
+ * When a CPU is offlined, top_cpuset may end up with no available CPUs,
+ * which should clear subpartitions_cpus. We should not emit a warning for this
+ * scenario: the hierarchy is updated from top to bottom, so subpartitions_cpus
+ * may already be cleared when disabling a remote partition.
+ */
+ if (!parent)
+ WARN_ON_ONCE(!cpumask_subset(cs->effective_xcpus, subpartitions_cpus) &&
+ !cpumask_empty(subpartitions_cpus));
+
+ spin_lock_irq(&callback_lock);
+ cs->remote_partition = false;
+ /* Only give back the CPUs granted by a valid parent, it may have shrunk */
+ if (is_partition_valid(cs) && (!parent || is_partition_valid(parent))) {
+ if (parent)
+ cpumask_and(cs->effective_xcpus, cs->effective_xcpus,
+ parent->effective_xcpus);
+ partition_xcpus_del(old_prs, parent, cs->effective_xcpus);
+ }
+ /*
+ * effective_xcpus may need to be changed. Skip it if exclusive_cpus is
+ * empty, as reset_partition_data() clears it below.
+ */
+ if (!cpumask_empty(cs->exclusive_cpus))
+ compute_excpus(cs, cs->effective_xcpus);
+ partition_state_update(cs, new_prs, prs_err);
+ spin_unlock_irq(&callback_lock);
+ cpuset_force_rebuild();
+ notify_partition_change(cs, old_prs);
+
+ /* A member or an invalid partition owns no CPU, nothing to propagate */
+ if (old_prs <= 0)
+ return;
+
+ /* Propagate changes in the parent's effective_cpus down the hierarchy */
+ if (!parent)
+ parent = &top_cpuset;
+ cpuset_update_tasks_cpumask(parent, tmp->new_cpus);
+ update_sibling_cpumasks(parent, cs, tmp);
+}
+
/*
* remote_partition_enable - Enable current cpuset as a remote partition root
* @cs: the cpuset to update
--
2.34.1