cgroup/cpuset: More fix patches
Subsystems (3)
Waiman Long
maintainercover cgroup/cpuset: More fix patches
This patch series contains an additional set of cpuset fix patches.
Waiman Long (5):
selftests/cgroup: Fix "test_cpuset_prs.sh -v" failures cgroup/cpuset: Fix incorrect partition invalidation logic in
cpuset_hotplug_update_tasks()
cgroup/cpuset: Protect current->mems_allowed read in cpuset_fork() cgroup/cpuset: Correctly set cpus_updated in
cpuset_hotplug_update_tasks()
cgroup/cpuset: Correctly set isolated_cpus when switching partition
state
kernel/cgroup/cpuset.c | 66 ++++++++++++++----- .../selftests/cgroup/test_cpuset_prs.sh | 20 ++++-- 2 files changed, 63 insertions(+), 23 deletions(-)
Signature · 1 line
2.56.0
Waiman Long
maintainerPATCH 1/5 selftests/cgroup: Fix "test_cpuset_prs.sh -v" failures
When the -v option is used to run test_cpuset_prs.sh, it activates the use of sched-debug to determine the set of isolated CPUs which will also consider 1-CPU partition as being isolated though they will not be reflected in "cpuset.cpus.isolated". As a result, some of the expected isolated CPUs values have 2 different set of values to correspond to these two ways of determining isolated CPUs.
Commit 6e4834ba8986 ("cgroup/cpuset: Fix incorrect CPU accounting with parent's effective_xcpus expansion") introduces some new test cases which fail when the -v option is used. Update the test cases to make them pass with the -v option as well.
Also remove an obsolete comment in update_parent_effective_cpumask() of cpuset.c.
--- kernel/cgroup/cpuset.c | 6 ------ tools/testing/selftests/cgroup/test_cpuset_prs.sh | 13 ++++++++----- 2 files changed, 8 insertions(+), 11 deletions(-)
kernel/cgroup/cpuset.c +0 -6
@@ -2157,12 +2157,6 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,else part_error =PERR_NOTEXCL ; } -/* -* When both cs and parent remain valid, parent's -* compute_partition_effective_cpumask() call would have removed -* cs's effective_xcpus from the parent's effective_cpus. So -* there is no need to adjust parent's effective_cpus here. -*/ } write_error:
tools/testing/selftests/cgroup/test_cpuset_prs.sh +8 -5
@@ -534,16 +534,19 @@ TEST_MATRIX=( # 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 CX3-6:P1 \ - . . X2-6 . . . 0 A1:2|A2:3-6|XA2:3-6 A1:P1|A2:P1 ." - " C2-6:X2-5:P1 CX3-6:P2 \ - . . X2-6 . . . 0 A1:2|A2:3-6|XA2:3-6 A1:P1|A2:P2 3-6" + " C2-6:X2-5:P1 CX4-6:P1 \ + . . X2-6 . . . 0 A1:2-3|A2:4-6|XA2:4-6 \ + A1:P1|A2:P1 ." + " C2-6:X2-5:P1 CX4-6:P2 \ + . . X2-6 . . . 0 A1:2-3|A2:4-6|XA2:4-6 \ + A1:P1|A2:P2 4-6" # Both expansion and contraction of parent's effective_xcpus will be # correctly handled in child partition and the child can be invalidated # if a populated parent could lose all its effective_cpus. " C2-6:X2-5:P1 CX3,5-6:P2 \ - . . X4-6 . . . 0 A1:4|A2:5-6|XA2:5-6 A1:P1|A2:P2 5-6" + . . X4-6 . . . 0 A1:4|A2:5-6|XA2:5-6 \ + A1:P1|A2:P2 4-6|5-6" " C2-6:X2-5:P1 CX3-6:P2 \ . . T:X4-6 . . . 0 A1:4-6|A2:4-6 A1:P1|A2:P-2 ."
Signature · 1 line
2.56.0
Waiman Long
maintainerPATCH 2/5 cgroup/cpuset: Fix incorrect partition invalidation logic in cpuset_hotplug_update_tasks()
In reply to Waiman LongWhen most of the active CPUs of a system are allocated to sub-partitions below the top_cpuset partition, it is possible that a CPU offline operation may remove the last active CPU in the top_cpuset partition. When that really happens, cpuset_handle_hotplug() will clear the subpartitions_cpus to make sure active CPUs are present in the top partition and let the other sub-partitions get invalidated and then competes for their exclusive CPUs again.
In cpuset_hotplug_update_tasks(), the current code use "cpumask_empty(subpartitions_cpus)" to determine if that partition should be invalidated. That is fine if there is only one sub-partition. As reported by sashiko that when there are more than one sub-partitions and the first sub-partition will get invalidated and then it will try to acquire the exclusive CPUs and become valid again. When this happens, the subpartitions_cpus will no longer be empty. The remaining sub-partitions will assume their exclusive CPUs are safe and proceed as normal. As a result, active CPUs owned by those sub-partitions remain in both top_cpuset.effective_cpus and the sub-partitions' effective_cpus while missing from subpartitions_cpus, breaking partition CPU exclusivity and triggering WARN_ON_ONCE() in generate_sched_domains() and remote_partition_disable().
Fix that by checking if the sub-partitions' effective_xcpus are subset of subpartitions_cpus to determine if they should be invalidated.
--- kernel/cgroup/cpuset.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/cgroup/cpuset.c +4 -4
@@ -4002,9 +4002,9 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)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 )))) { +if (is_remote_partition (cs) && + (!cpumask_subset (cs->effective_xcpus, subpartitions_cpus) || + (cpumask_empty (&new_cpus) &&partition_is_populated (cs,NULL )))) {WRITE_ONCE (cs->prs_err,PERR_HOTPLUG );remote_partition_disable (cs, tmp);compute_effective_cpumask (&new_cpus, cs, parent);
@@ -4021,7 +4021,7 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)if (is_local_partition (cs) && (!is_partition_valid (parent) ||tasks_nocpu_error (parent, cs, &new_cpus) || -cpumask_empty (subpartitions_cpus))) + !cpumask_subset (cs->effective_xcpus, subpartitions_cpus))) partcmd = partcmd_invalidate;/* * On the other hand, an invalid partition root may be transitioned
Signature · 1 line
2.56.0
Waiman Long
maintainerPATCH 3/5 cgroup/cpuset: Protect current->mems_allowed read in cpuset_fork()
In reply to Waiman LongReading mems_allowed of the current task outside of cpuset_mutex critical section should be protected by using mems_allowed_seq seqlock as the nodemask can be updated concurrently leading to a torn read. Such protection is currently not implemented in cpuset_fork() for the !CLONE_INTO_CGROUP case. As a result, the child task may inherit an empty or corrupted mems_allowed nodemask. This violates NUMA memory policy constraints and causes subsequent page allocations to unconditionally fail, potentially leading to crashes or spurious OOM killer invocations for the child task.
Fix that by using a read_mems_allowed_begin/read_mems_allowed_retry() loop to protect the mems_allowed read.
--- kernel/cgroup/cpuset.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/cgroup/cpuset.c +10 -1
@@ -3864,11 +3864,20 @@ static void cpuset_fork(struct task_struct *task)rcu_read_unlock ();if (cs == oldcs) { +unsigned int seq; +if (cs == &top_cpuset)return ;set_cpus_allowed_ptr (task, current->cpus_ptr); - task->mems_allowed = current->mems_allowed; +/* +* A cpuset.mems update can update current->mems_allowed +* concurrently, so retry it again if it really happens. +*/ +do { + seq =read_mems_allowed_begin (); + task->mems_allowed = current->mems_allowed; + }while (read_mems_allowed_retry (seq));return ; }
Signature · 1 line
2.56.0
Waiman Long
maintainerPATCH 4/5 cgroup/cpuset: Correctly set cpus_updated in cpuset_hotplug_update_tasks()
In reply to Waiman LongWhen cpuset_hotplug_update_tasks() is called with the conditions that either remote_partition_disable() or update_parent_effective_cpumask() has to be called, the cs->effective_cpus can be updated such that it matches what is computed in new_cpus. In such case, cpus_updated can be incorrectly set to false leading to stale CPU afinity remained in the tasks not matching the new cpuset value. Fix that by forcing cpus_updated to true whenever one of these partition helpers is being called.
--- kernel/cgroup/cpuset.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +9 -2
@@ -3998,7 +3998,7 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) {static cpumask_t new_cpus;static nodemask_t new_mems; -bool cpus_updated; +bool cpus_updated = false;bool mems_updated;int partcmd =-1 ;struct cpuset *parent;
@@ -4011,12 +4011,18 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)if (!tmp || !cs->partition_root_state) goto update_tasks; +/* +* cpus_updated should be set to true when remote_partition_disable() +* or update_parent_effective_cpumask() is called as cs->effective_cpus +* may have been updated. +*/ if (is_remote_partition (cs) && (!cpumask_subset (cs->effective_xcpus, subpartitions_cpus) || (cpumask_empty (&new_cpus) &&partition_is_populated (cs,NULL )))) {WRITE_ONCE (cs->prs_err,PERR_HOTPLUG );remote_partition_disable (cs, tmp);compute_effective_cpumask (&new_cpus, cs, parent); + cpus_updated = true; }/*
@@ -4041,6 +4047,7 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) partcmd = partcmd_update;if (partcmd >=0 ) { + cpus_updated = true;update_parent_effective_cpumask (cs, partcmd,NULL , tmp);if (is_partition_valid (cs) || (partcmd == partcmd_invalidate)) {compute_effective_cpumask (&new_cpus, cs, parent);
@@ -4049,7 +4056,7 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) } update_tasks: - cpus_updated = !cpumask_equal (&new_cpus, cs->effective_cpus); + cpus_updated |= !cpumask_equal (&new_cpus, cs->effective_cpus); mems_updated = !nodes_equal (new_mems, cs->effective_mems);if (!cpus_updated && !mems_updated) goto unlock;/* Hotplug doesn't affect this cpuset */
Signature · 1 line
2.56.0
Waiman Long
maintainerPATCH 5/5 cgroup/cpuset: Correctly set isolated_cpus when switching partition state
In reply to Waiman LongWhen update_prstate() is called to switch the partition state from root to isolated or vice versa, the whole effective_xcpus is passed to isolated_cpus_update(). However, if there are child partitions present, some of the effective_xcpus are dedicated to those child partitions and their partition states will remain the same or get invalidated in the case of child root partition with isolated parent in the latter update_cpumasks_heir() call. In the latter case, their exclusive CPUs will be returned to their parent.
Therefore isolated_cpus may be set incorrectly with child partitions underneath. Fix that by excluding their exclusive CPUs in computing the correct cpumask of exclusive CPUs used by that partition alone.
Add new test cases into test_cpuset_prs.sh to test for such cases.
--- kernel/cgroup/cpuset.c | 30 +++++++++++++++---- .../selftests/cgroup/test_cpuset_prs.sh | 7 +++++ 2 files changed, 32 insertions(+), 5 deletions(-)
kernel/cgroup/cpuset.c +25 -5
@@ -3050,12 +3050,32 @@ static int update_prstate(struct cpuset *cs, int new_prs) cs->effective_xcpus, NULL)) err =PERR_HKEEPING ; else if (parent_prs == PRS_ISOLATED) - err =PERR_ISOPARENT ; + err =PERR_ISOPARENT ;/* old_prs should be isolated */ -if (!err) - isolcpus_updated = true; -else +if (err) { disable_partition = true; + }else { +struct cgroup_subsys_state *css; +struct cpuset *child; + +/* +* Need to scan child partitions, if present, and +* exclude their effective_xcpus from cs. In the case +* of switching to isolated partition with child root +* partitions, those child partitions will be +* invalidated in update_cpumasks_hier(). +*/ + isolcpus_updated = true; +cpumask_copy (tmpmask.new_cpus, cs->effective_xcpus); +rcu_read_lock (); +cpuset_for_each_child (child, css, cs) { +if (!is_partition_valid (child)) +continue ; +cpumask_andnot (tmpmask.new_cpus, tmpmask.new_cpus, + child->effective_xcpus); + } +rcu_read_unlock (); + } }else {/* * Switching back to member is always allowed even if it
@@ -3089,7 +3109,7 @@ 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, tmpmask.new_cpus);spin_unlock_irq (&callback_lock);/* Force update if partition is disabled & update effective_xcpus */
tools/testing/selftests/cgroup/test_cpuset_prs.sh +7 -0
@@ -550,6 +550,13 @@ TEST_MATRIX=( " C2-6:X2-5:P1 CX3-6:P2 \ . . T:X4-6 . . . 0 A1:4-6|A2:4-6 A1:P1|A2:P-2 ." + # Switch PRS state (root <=> isolated) will properly update cpuset.cpus.isolated + # even if child partitions are present. + " C1-6:P1 C3-6:P1 C5-6:P2 . . P2 . . 0 A1:1-2:A2:3-4:A3:5-6 + A1:P1|A2:P2:A3:P2 3-6" + " C1-6:P1 C3-6:P2 C5-6:P2 . . P1 . . 0 A1:1-2:A2:3-4:A3:5-6 + A1:P1|A2:P1:A3:P2 5-6" + # # old-A1 old-A2 old-A3 old-B1 new-A1 new-A2 new-A3 new-B1 fail ECPUs Pstate ISOLCPUS # ------ ------ ------ ------ ------ ------ ------ ------ ---- ----- ------ --------
Signature · 1 line
2.56.0