mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cgroup/cpuset: Fix missing effective_xcpus accounting of child partitions
@ 2026-10-27 12:38 Guopeng Zhang
  2026-10-27 13:44 ` Guopeng Zhang
  2026-10-28 15:30 ` Waiman Long
  0 siblings, 2 replies; 4+ messages in thread
From: Guopeng Zhang @ 2026-10-27 12:38 UTC (permalink / raw)
  To: longman
  Cc: ridong.chen, tj, hannes, mkoutny, shuah, cgroups, linux-kernel,
	linux-kselftest, Guopeng Zhang

From: Guopeng Zhang <zhangguopeng@kylinos.cn>

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@kylinos.cn>
---
 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-28 15:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-27 12:38 [PATCH] cgroup/cpuset: Fix missing effective_xcpus accounting of child partitions Guopeng Zhang
2026-10-27 13:44 ` Guopeng Zhang
2026-10-28  5:51   ` Guopeng Zhang
2026-10-28 15:30 ` Waiman Long

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®