[PATCH] cgroup/cpuset: Fix missing effective_xcpus accounting of child partitions

From: Guopeng Zhang

Date: Tue Oct 27 2026 - 08:50:38 EST


From: Guopeng Zhang <zhangguopeng@xxxxxxxxxx>

When a partition root expands its exclusive CPUs, a valid child
partition will be able to claim more CPUs from its parent. As
update_cpumasks_hier() always updates a cpuset before its descendants,
the parent has already been committed when the child partition is
reached. Its effective_cpus is computed with the child's old
effective_xcpus subtracted and no further adjustment is made
after the child commits its new effective_xcpus.

The additional CPUs are thus granted to both the parent and the child
partitions. generate_sched_domains() then emits the warning below
about the overlapping sched domains:

WARNING: kernel/cgroup/cpuset.c:919 at rebuild_sched_domains_locked+0x1ec/0x2e0, CPU#21: bash/1225

Fix that by taking the CPUs newly added to a valid child partition
away from its parent at the commit point of update_cpumasks_hier()
right before the new effective_xcpus is committed. The tasks in the
parent cpuset as well as its other children will be updated if its
effective_cpus is changed.

A new test case is added to test_cpuset_prs.sh to test this patch.

Fixes: e2ffe502ba45 ("cgroup/cpuset: Add cpuset.cpus.exclusive for v2")
Signed-off-by: Guopeng Zhang <zhangguopeng@xxxxxxxxxx>
---
kernel/cgroup/cpuset.c | 25 +++++++++++++++++++
.../selftests/cgroup/test_cpuset_prs.sh | 4 +++
2 files changed, 29 insertions(+)

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 3219f927c35e..ec0ef3ad0726 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -2241,6 +2241,7 @@ static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp,
struct cpuset *parent = parent_cs(cp);
bool remote = is_remote_partition(cp);
bool update_parent = false;
+ bool xcpus_added = false;

old_prs = new_prs = cp->partition_root_state;

@@ -2356,6 +2357,19 @@ static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp,
}

spin_lock_irq(&callback_lock);
+ /*
+ * A child partition may claim more CPUs when its parent expands
+ * the exclusive CPUs. Take the newly granted CPUs away from the
+ * parent which was committed earlier in this walk.
+ */
+ if (!remote && new_prs > 0) {
+ compute_excpus(cp, tmp->addmask);
+ if (cpumask_andnot(tmp->delmask, tmp->addmask,
+ cp->effective_xcpus)) {
+ partition_xcpus_add(new_prs, parent, tmp->delmask);
+ xcpus_added = true;
+ }
+ }
cpumask_copy(cp->effective_cpus, tmp->new_cpus);
cp->partition_root_state = new_prs;
/*
@@ -2375,6 +2389,17 @@ static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp,

cpuset_update_tasks_cpumask(cp, tmp->new_cpus);

+ /*
+ * The parent's effective_cpus may have been changed by
+ * partition_xcpus_add() above. Update the tasks in the parent
+ * cpuset and its other children, if necessary.
+ */
+ if (xcpus_added) {
+ cpumask_copy(tmp->addmask, parent->effective_cpus);
+ cpuset_update_tasks_cpumask(parent, tmp->addmask);
+ update_sibling_cpumasks(parent, cp, tmp);
+ }
+
/*
* On default hierarchy, inherit the CS_SCHED_LOAD_BALANCE
* from parent if current cpuset isn't a valid partition root
diff --git a/tools/testing/selftests/cgroup/test_cpuset_prs.sh b/tools/testing/selftests/cgroup/test_cpuset_prs.sh
index 83ef6c781af3..09e430570815 100755
--- a/tools/testing/selftests/cgroup/test_cpuset_prs.sh
+++ b/tools/testing/selftests/cgroup/test_cpuset_prs.sh
@@ -437,6 +437,10 @@ TEST_MATRIX=(
" CX1-4:P1 CX3-4:P1 . . X1-2 . . . 0 A1:1-2|A2:1-2|XA2: A1:P1|A2:P-1"
" CX1-4:P1 CX3-4:P1 . . X1-2:X1-4 . . . 0 A1:1-2|A2:3-4|XA2:3-4 A1:P1|A2:P1"

+ # The extra CPUs gained by a child partition when its parent expands the
+ # exclusive CPUs have to be taken away from the parent.
+ " C2-6:X2-5:P1 C3-6:X3-6:P1 . . X2-6 . . . 0 A1:2|A2:3-6|XA2:3-6 A1:P1|A2:P1 ."
+
# Taking away all CPUs from parent or itself if there are tasks
# will make the partition invalid.
" C2-3:P1 C3:P1 . . T C2-3 . . 0 A1:2-3|A2:2-3 A1:P1|A2:P-1"
--
2.43.0