summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-11-15 14:17:31 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2026-11-15 14:17:31 -0800
commit4b15991b3be43e9581aca489ec30a351310bb49d (patch)
treef201b9e4d298e0058f99105e0a8c96787ff816e8
parentf70886e5a510cd7fc2c3aef91c46a14f4485fe63 (diff)
parentacec9637e03ca54bd3c3a0c1a6ad5c6dfa07c0df (diff)
Merge tag 'cgroup-for-7.4-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
Pull cgroup fixes from Tejun Heo: - A file notification racing the file's removal could re-arm the notification timer after it was shut down, corrupting the timer wheel or firing on freed memory. - The v1 freezer could leave an exiting task frozen forever after its cgroup was thawed or removed. - With memcg built without the block cgroup controller, memcg reclaim waited on writeback and could deadlock against a filesystem freeze. memcg now selects the controller. - cpuset partition state could go wrong when CPUs move between nested partitions or go offline: isolated CPUs leaked or went missing, invalid partitions kept exclusive CPUs or came back with no usable CPU, and expanding a remote partition could fail. Separately, a forked child could inherit a corrupted memory node mask. * tag 'cgroup-for-7.4-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup: cgroup/cpuset: Don't revive invalid partition without active CPU on hotplug cgroup/cpuset: Remove redundant WARN_ON_ONCE() in remote_cpus_update() memcg: make MEMCG select BLK_CGROUP cpuset: generalize the validate_partition() interface cpuset: settle isolation state when CPUs are given back to parent cgroup/cpuset: Correctly set isolated_cpus when switching partition state cgroup/cpuset: Protect current->mems_allowed read in cpuset_fork() selftests/cgroup: Fix "test_cpuset_prs.sh -v" failures cpuset: add early empty cpumask check in partition_xcpus_add/del cgroup/cpuset: Reset partition data when an invalid partition stays invalid cgroup/freezer: Fix exiting tasks left frozen forever after rmdir cgroup/freezer: walk exiting tasks in the v1 freezer cgroup: arm the notify timer only while the file exists cgroup/cpuset: Do housekeeping check when isolated child with root parent may get more xcpus cgroup/cpuset: Recompute effective cpumask in update_cpumasks_hier() after calling update_parent_effective_cpumask() cgroup/cpuset: Fix incorrect CPU accounting with parent's effective_xcpus expansion
-rw-r--r--init/Kconfig1
-rw-r--r--kernel/cgroup/cgroup.c22
-rw-r--r--kernel/cgroup/cpuset.c316
-rw-r--r--kernel/cgroup/legacy_freezer.c40
-rwxr-xr-xtools/testing/selftests/cgroup/test_cpuset_prs.sh46
5 files changed, 251 insertions, 174 deletions
diff --git a/init/Kconfig b/init/Kconfig
index 4013781e0a02..c8a7cfa60a6e 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -884,6 +884,7 @@ config MEMCG
select EVENTFD
select SLAB_OBJ_EXT
select VM_EVENT_COUNTERS
+ select BLK_CGROUP if BLOCK
help
Provides control over the memory footprint of tasks in a cgroup.
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index a284225f8dc9..b3657e72db99 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -4806,19 +4806,21 @@ void cgroup_file_notify(struct cgroup_file *cfile)
if (!READ_ONCE(cfile->kn))
return;
- last = READ_ONCE(cfile->notified_at);
- next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
- if (time_in_range(jiffies, last, next)) {
- timer_reduce(&cfile->notify_timer, next);
- if (timer_pending(&cfile->notify_timer))
- return;
- }
+ if (timer_pending(&cfile->notify_timer))
+ return;
spin_lock_irqsave(&cfile->lock, flags);
if (cfile->kn) {
- kn = cfile->kn;
- kernfs_get(kn);
- WRITE_ONCE(cfile->notified_at, jiffies);
+ last = cfile->notified_at;
+ next = last + CGROUP_FILE_NOTIFY_MIN_INTV;
+
+ if (time_in_range(jiffies, last, next)) {
+ timer_reduce(&cfile->notify_timer, next);
+ } else {
+ kn = cfile->kn;
+ kernfs_get(kn);
+ cfile->notified_at = jiffies;
+ }
}
spin_unlock_irqrestore(&cfile->lock, flags);
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 3219f927c35e..db32d330cfbc 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -1244,17 +1244,17 @@ static bool isolated_cpu_update(int new_prs, int cpu)
}
/*
- * isolated_cpus_update - Update the isolated_cpus mask
- * @old_prs: old partition_root_state
- * @new_prs: new partition_root_state
- * @xcpus: exclusive CPUs with state change
+ * isolated_cpus_update - Set the isolation state of @xcpus
+ * @new_prs: partition root state that @xcpus will belong to
+ * @xcpus: exclusive CPUs whose isolation state may change
+ *
+ * CPUs isolated at boot time are kept isolated.
*/
-static void isolated_cpus_update(int old_prs, int new_prs, struct cpumask *xcpus)
+static void isolated_cpus_update(int new_prs, struct cpumask *xcpus)
{
bool updated = false;
int cpu;
- WARN_ON_ONCE(old_prs == new_prs);
lockdep_assert_held(&callback_lock);
lockdep_assert_held(&cpuset_mutex);
for_each_cpu(cpu, xcpus)
@@ -1264,45 +1264,6 @@ static void isolated_cpus_update(int old_prs, int new_prs, struct cpumask *xcpus
}
/*
- * isolated_cpus_release - Remove the relevant CPUs in @xcpus from isolated_cpus
- * @cs: the current cpuset
- * @xcpus: the set of effective xcpus to be considered for removal
- *
- * This function is called when an isolated partition is invalidated or when
- * some of its effective xcpus are to be removed from isolated_cpus due to
- * changes in parent cpuset. The given @xcpus must be modifiable as the function
- * will update its value to determine the right set of CPUs to be removed.
- */
-static void isolated_cpus_release(struct cpuset *cs, struct cpumask *xcpus)
-{
- struct cpuset *ancestor = parent_cs(cs);
- struct cpuset *iancestor = NULL;
-
- /*
- * Search the furthest isolated partition root ancestor available
- * until top_cpuset or a remote partition root is hit.
- */
- for (; ancestor; ancestor = parent_cs(ancestor)) {
- if (ancestor->partition_root_state == PRS_ISOLATED)
- iancestor = ancestor;
- if (is_remote_partition(ancestor))
- break;
- }
-
- /*
- * If a furthest isolated partition acnestor is found, use it to
- * determine which of the CPUs in @xcpus should be removed from
- * isolated_cpus. Otherwise, all of them will be removed.
- */
- if (iancestor && !cpumask_andnot(xcpus, xcpus, iancestor->effective_xcpus))
- return; /* No CPU should be removed from isolated_cpus */
-
- spin_lock_irq(&callback_lock);
- isolated_cpus_update(PRS_ISOLATED, PRS_ROOT, xcpus);
- spin_unlock_irq(&callback_lock);
-}
-
-/*
* partition_xcpus_add - Add new exclusive CPUs to partition
* @new_prs: new partition_root_state
* @parent: parent cpuset
@@ -1315,16 +1276,17 @@ static void partition_xcpus_add(int new_prs, struct cpuset *parent,
{
WARN_ON_ONCE(new_prs < 0);
lockdep_assert_held(&callback_lock);
+
+ if (cpumask_empty(xcpus))
+ return;
+
if (!parent)
parent = &top_cpuset;
-
if (parent == &top_cpuset)
cpumask_or(subpartitions_cpus, subpartitions_cpus, xcpus);
- if (new_prs != parent->partition_root_state)
- isolated_cpus_update(parent->partition_root_state, new_prs,
- xcpus);
+ isolated_cpus_update(new_prs, xcpus);
cpumask_andnot(parent->effective_cpus, parent->effective_cpus, xcpus);
}
@@ -1342,15 +1304,18 @@ static void partition_xcpus_del(int old_prs, struct cpuset *parent,
{
WARN_ON_ONCE(old_prs < 0);
lockdep_assert_held(&callback_lock);
+
+ if (cpumask_empty(xcpus))
+ return;
+
if (!parent)
parent = &top_cpuset;
if (parent == &top_cpuset)
cpumask_andnot(subpartitions_cpus, subpartitions_cpus, xcpus);
- if (old_prs != parent->partition_root_state)
- isolated_cpus_update(old_prs, parent->partition_root_state,
- xcpus);
+ /* CPUs given back, including those of descendants, take parent's state */
+ isolated_cpus_update(parent->partition_root_state, xcpus);
cpumask_or(parent->effective_cpus, parent->effective_cpus, xcpus);
cpumask_and(parent->effective_cpus, parent->effective_cpus, cpu_active_mask);
@@ -1449,6 +1414,48 @@ out:
return res;
}
+/**
+ * validate_partition - Validate a cpuset partition configuration
+ * @cs: The cpuset to validate
+ * @parent: Parent partition of @cs, NULL for remote partition
+ * @new_prs: The proposed new partition root state
+ * @new_excpus: The new effective exclusive CPUs mask to validate
+ * @del: exclusive CPUs to be deleted from @cs, can be NULL
+ *
+ * A remote partition gets its CPUs from top_cpuset, so top_cpuset is used
+ * as the parent when @parent is NULL.
+ *
+ * The housekeeping check is skipped if @parent is not a valid partition
+ * root as @cs can't be a valid local partition in that case.
+ *
+ * Return: PRS error code (0 if valid, non-zero error code if invalid)
+ */
+static enum prs_errcode validate_partition(struct cpuset *cs,
+ struct cpuset *parent, int new_prs,
+ struct cpumask *new_excpus,
+ struct cpumask *del)
+{
+ int parent_prs;
+
+ if (new_prs == PRS_MEMBER)
+ return PERR_NONE;
+
+ if (cpumask_empty(new_excpus))
+ return PERR_INVCPUS;
+
+ if (!parent)
+ parent = &top_cpuset;
+ parent_prs = parent->partition_root_state;
+ if (parent_prs > 0 &&
+ prstate_housekeeping_conflict(new_prs, parent_prs, new_excpus, del))
+ return PERR_HKEEPING;
+
+ if (tasks_nocpu_error(parent, cs, new_excpus))
+ return PERR_NOCPUS;
+
+ return PERR_NONE;
+}
+
/*
* cpuset_update_sd_hk_unlock - Rebuild sched domains, update HK & unlock
*
@@ -1605,6 +1612,7 @@ static void compute_partition_effective_cpumask(struct cpuset *cs,
struct cgroup_subsys_state *css;
struct cpuset *child;
bool populated = partition_is_populated(cs, NULL);
+ static cpumask_t xcpus; /* Protected by cpuset_mutex */
/*
* Check child partition roots to see if they should be invalidated
@@ -1633,14 +1641,15 @@ static void compute_partition_effective_cpumask(struct cpuset *cs,
int parent_prs = cs->partition_root_state;
/*
- * Invalidate child partition and update isolated_cpus
- * if necessary.
+ * Invalidate child partition. Its CPUs still owned by
+ * cs take the isolation state of cs. effective_xcpus
+ * of cs may not be updated yet, so recompute it.
*/
spin_lock_irq(&callback_lock);
WRITE_ONCE(child->prs_err, PERR_NOCPUS);
- if ((old_prs == PRS_ISOLATED) && (parent_prs == PRS_ROOT))
- isolated_cpus_update(PRS_ISOLATED, PRS_ROOT,
- child->effective_xcpus);
+ compute_excpus(cs, &xcpus);
+ cpumask_and(&xcpus, &xcpus, child->effective_xcpus);
+ isolated_cpus_update(parent_prs, &xcpus);
child->partition_root_state = -old_prs;
spin_unlock_irq(&callback_lock);
notify_partition_change(child, old_prs);
@@ -1824,7 +1833,6 @@ static void remote_cpus_update(struct cpuset *cs, struct cpumask *xcpus,
* left in the top cpuset.
*/
if (adding) {
- WARN_ON_ONCE(cpumask_intersects(tmp->addmask, subpartitions_cpus));
if (!capable(CAP_SYS_ADMIN))
WRITE_ONCE(cs->prs_err, PERR_ACCESS);
else if (cpumask_intersects(tmp->addmask, subpartitions_cpus) ||
@@ -1967,18 +1975,10 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
if ((cmd == partcmd_enable) || (cmd == partcmd_enablei)) {
new_prs = (cmd == partcmd_enable) ? PRS_ROOT : PRS_ISOLATED;
- /*
- * Enabling partition root is not allowed if its
- * effective_xcpus is empty.
- */
- if (cpumask_empty(tmp->delmask))
- return PERR_INVCPUS;
-
- if (prstate_housekeeping_conflict(new_prs, parent_prs, tmp->delmask, NULL))
- return PERR_HKEEPING;
-
- if (nocpu)
- return PERR_NOCPUS;
+ part_error = validate_partition(cs, parent, new_prs,
+ tmp->delmask, NULL);
+ if (part_error)
+ return part_error;
/*
* This function will only be called when all the preliminary
@@ -2083,21 +2083,8 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
*
* A partition error happens when parent has tasks and all
* its effective CPUs will have to be distributed out.
- *
- * For an isolated partition with a shrunk root partition parent
- * we may need to return some or all of its isolated CPUs.
*/
nocpu |= cpumask_empty(tmp->delmask);
- if ((parent_prs == PRS_ROOT) && (old_prs == PRS_ISOLATED)) {
- cpumask_clear(tmp->addmask);
- if (nocpu)
- cpumask_copy(tmp->addmask, cs->effective_xcpus);
- else if (!cpumask_subset(cs->effective_xcpus, parent->effective_xcpus))
- cpumask_andnot(tmp->addmask, cs->effective_xcpus,
- parent->effective_xcpus);
- if (!cpumask_empty(tmp->addmask))
- isolated_cpus_release(cs, tmp->addmask);
- }
if (nocpu) {
part_error = PERR_NOCPUS;
if (is_partition_valid(cs))
@@ -2106,11 +2093,34 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
parent->effective_xcpus);
} else if ((parent_prs == PRS_ISOLATED) &&
((old_prs == PRS_ROOT) || (old_prs == -PRS_ROOT))) {
+ /* Parent partition state changed to PRS_ISOLATED */
part_error = PERR_ISOPARENT;
if (old_prs == PRS_ROOT) {
cpumask_copy(tmp->addmask, cs->effective_xcpus);
adding = true;
}
+ } else if (is_partition_valid(cs) &&
+ !cpumask_equal(tmp->delmask, cs->effective_xcpus)) {
+ /*
+ * If there is an expansion of parent's effective_xcpus,
+ * we may take more xcpus from parent. The parent's
+ * effective_cpus should still have active CPUs or nocpu
+ * should have been set. We also need to do a
+ * housekeeping check for an isolated child with root
+ * parent to make sure that it won't exhaust all the
+ * active housekeeping CPUs.
+ */
+ cpumask_andnot(tmp->addmask, cs->effective_xcpus, tmp->delmask);
+ if ((parent_prs == PRS_ROOT) && (old_prs == PRS_ISOLATED) &&
+ prstate_housekeeping_conflict(old_prs, parent_prs,
+ tmp->delmask, tmp->addmask)) {
+ adding = cpumask_and(tmp->addmask, cs->effective_xcpus,
+ tmp->delmask);
+ part_error = PERR_HKEEPING;
+ } else {
+ deleting = cpumask_andnot(tmp->delmask, tmp->delmask,
+ cs->effective_xcpus);
+ }
} else if (is_partition_invalid(cs)) {
struct cgroup_subsys_state *css;
struct cpuset *child;
@@ -2143,12 +2153,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:
@@ -2320,18 +2324,12 @@ update_parent_effective:
/*
* When parent is not a valid partition root,
* child partition roots become invalid too.
- *
- * For a to be invalidated isolated partition,
- * call return_isolated_cpus() to remove the
- * relevant CPUs from isolated_cpus.
+ * The isolation state of their CPUs has been
+ * settled when the parent gave them back.
*/
if (is_partition_valid(cp))
new_prs = -old_prs;
- if (old_prs == PRS_ISOLATED) {
- cpumask_copy(tmp->delmask, cp->effective_xcpus);
- isolated_cpus_release(cp, tmp->delmask);
- }
WRITE_ONCE(cp->prs_err,
is_partition_invalid(parent)
? PERR_INVPARENT : PERR_NOTPART);
@@ -2347,12 +2345,12 @@ get_css:
update_parent_effective_cpumask(cp, partcmd_update, NULL, tmp);
/*
* Capture current partition_root_state as it may have
- * changed. Recompute effective cpumask if changed.
+ * changed. Always recompute effective cpumask as tmp->new_cpus
+ * may be clobbered when update_sibling_cpumasks() is called
+ * within update_parent_effective_cpumask().
*/
new_prs = cp->partition_root_state;
- if (new_prs != old_prs) {
- compute_effective_cpumask(tmp->new_cpus, cp, parent);
- }
+ compute_effective_cpumask(tmp->new_cpus, cp, parent);
}
spin_lock_irq(&callback_lock);
@@ -2464,32 +2462,6 @@ 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
- *
- * 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)
-{
- struct cpuset *parent = parent_cs(cs);
-
- if (cs_is_member(trialcs))
- return PERR_NONE;
-
- if (cpumask_empty(trialcs->effective_xcpus))
- return PERR_INVCPUS;
-
- if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus))
- return PERR_NOCPUS;
-
- return PERR_NONE;
-}
-
-/**
* partition_cpus_change - Handle partition state changes due to CPU mask updates
* @cs: The target cpuset being modified
* @trialcs: The trial cpuset containing proposed configuration changes
@@ -2502,33 +2474,24 @@ static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs,
struct tmpmasks *tmp)
{
enum prs_errcode prs_err;
- int parent_prs = is_remote_partition(cs)
- ? PRS_ROOT : parent_cs(cs)->partition_root_state;
+ struct cpuset *parent = is_remote_partition(cs) ? NULL : parent_cs(cs);
if (cs_is_member(cs))
return;
- prs_err = validate_partition(cs, trialcs);
- if (!prs_err && (parent_prs > 0) &&
- !cpumask_empty(trialcs->effective_xcpus)) {
- int prs = cs->partition_root_state;
-
- /*
- * Check for housekeeping conflict by passing the whole
- * effective_xcpus as well as those CPUs that are to be
- * removed from cs as delmask.
- */
- if (prs > 0)
- cpumask_andnot(tmp->delmask, cs->effective_xcpus,
- trialcs->effective_xcpus);
- else
- cpumask_clear(tmp->delmask);
-
- if (prstate_housekeeping_conflict(prs, parent_prs,
- trialcs->effective_xcpus, tmp->delmask))
- prs_err = PERR_HKEEPING;
- }
+ /*
+ * Check for housekeeping conflict by passing the whole
+ * effective_xcpus as well as those CPUs that are to be
+ * removed from cs as delmask.
+ */
+ if (is_partition_valid(cs))
+ cpumask_andnot(tmp->delmask, cs->effective_xcpus,
+ trialcs->effective_xcpus);
+ else
+ cpumask_clear(tmp->delmask);
+ prs_err = validate_partition(cs, parent, trialcs->partition_root_state,
+ trialcs->effective_xcpus, tmp->delmask);
if (prs_err) {
WRITE_ONCE(cs->prs_err, prs_err);
trialcs->prs_err = prs_err;
@@ -2593,8 +2556,11 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
spin_lock_irq(&callback_lock);
cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus);
- if ((old_prs > 0) && !is_partition_valid(cs))
+ if (old_prs && !is_partition_valid(cs)) {
reset_partition_data(cs);
+ /* effective_xcpus may be cleared, update the subtree too */
+ force = true;
+ }
spin_unlock_irq(&callback_lock);
/* effective_cpus/effective_xcpus will be updated here */
@@ -2658,8 +2624,11 @@ static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs,
spin_lock_irq(&callback_lock);
cpumask_copy(cs->exclusive_cpus, trialcs->exclusive_cpus);
cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus);
- if ((old_prs > 0) && !is_partition_valid(cs))
+ if (old_prs && !is_partition_valid(cs)) {
reset_partition_data(cs);
+ /* effective_xcpus may be cleared, update the subtree too */
+ force = true;
+ }
spin_unlock_irq(&callback_lock);
/*
@@ -3042,12 +3011,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
@@ -3081,7 +3070,7 @@ out:
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(new_prs, tmpmask.new_cpus);
spin_unlock_irq(&callback_lock);
/* Force update if partition is disabled & update effective_xcpus */
@@ -3856,11 +3845,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;
}
@@ -4017,10 +4015,10 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
partcmd = partcmd_invalidate;
/*
* On the other hand, an invalid partition root may be transitioned
- * back to a regular one with a non-empty effective xcpus.
+ * back to a regular one if its effective xcpus has an active CPU.
*/
else if (is_partition_valid(parent) && is_partition_invalid(cs) &&
- !cpumask_empty(cs->effective_xcpus))
+ cpumask_intersects(cs->effective_xcpus, cpu_active_mask))
partcmd = partcmd_update;
if (partcmd >= 0) {
diff --git a/kernel/cgroup/legacy_freezer.c b/kernel/cgroup/legacy_freezer.c
index 8545e0d1ba3d..1792523d119a 100644
--- a/kernel/cgroup/legacy_freezer.c
+++ b/kernel/cgroup/legacy_freezer.c
@@ -265,7 +265,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
rcu_read_unlock();
/* are all tasks frozen? */
- css_task_iter_start(css, 0, &it);
+ css_task_iter_start(css, CSS_TASK_ITER_WITH_DEAD, &it);
while ((task = css_task_iter_next(&it))) {
if (freezing(task) && !frozen(task))
@@ -309,7 +309,7 @@ static void freeze_cgroup(struct freezer *freezer)
struct css_task_iter it;
struct task_struct *task;
- css_task_iter_start(&freezer->css, 0, &it);
+ css_task_iter_start(&freezer->css, CSS_TASK_ITER_WITH_DEAD, &it);
while ((task = css_task_iter_next(&it)))
freeze_task(task);
css_task_iter_end(&it);
@@ -320,7 +320,7 @@ static void unfreeze_cgroup(struct freezer *freezer)
struct css_task_iter it;
struct task_struct *task;
- css_task_iter_start(&freezer->css, 0, &it);
+ css_task_iter_start(&freezer->css, CSS_TASK_ITER_WITH_DEAD, &it);
while ((task = css_task_iter_next(&it)))
__thaw_task(task);
css_task_iter_end(&it);
@@ -382,6 +382,24 @@ static void freezer_change_state(struct freezer *freezer, bool freeze)
* CGROUP_FREEZING_PARENT.
*/
mutex_lock(&freezer_mutex);
+
+ /*
+ * A dead cgroup must not be frozen.
+ *
+ * rmdir thaws the cgroup from freezer_css_killed(), but freezer.state
+ * stays writable until rmdir removes it a moment later. A FROZEN write
+ * in that gap would freeze the cgroup again, and nothing could thaw it
+ * after that.
+ *
+ * A freezer.state write doesn't take cgroup_mutex, but testing
+ * cgroup_is_dead() without it is safe. rmdir marks the cgroup dead
+ * before the thaw, so a write that takes freezer_mutex after the thaw
+ * sees it dead. One that takes it before the thaw may still see the
+ * cgroup alive, but the thaw then undoes its freeze.
+ */
+ if (freeze && data_race(cgroup_is_dead(freezer->css.cgroup)))
+ goto out_unlock;
+
rcu_read_lock();
css_for_each_descendant_pre(pos, &freezer->css) {
struct freezer *pos_f = css_freezer(pos);
@@ -403,10 +421,25 @@ static void freezer_change_state(struct freezer *freezer, bool freeze)
css_put(pos);
}
rcu_read_unlock();
+out_unlock:
mutex_unlock(&freezer_mutex);
cpus_read_unlock();
}
+/**
+ * freezer_css_killed - Thaw a freezer cgroup being removed
+ * @css: css being killed
+ *
+ * Exiting tasks hidden from cgroup.procs can still be in the cgroup when it is
+ * removed, and a frozen one has nothing left to thaw it once freezer.state is
+ * gone. Thaw the cgroup as a THAWED write to it would. A frozen ancestor keeps
+ * the tasks frozen through CGROUP_FREEZING_PARENT until the ancestor is thawed.
+ */
+static void freezer_css_killed(struct cgroup_subsys_state *css)
+{
+ freezer_change_state(css_freezer(css), false);
+}
+
static ssize_t freezer_write(struct kernfs_open_file *of,
char *buf, size_t nbytes, loff_t off)
{
@@ -466,6 +499,7 @@ static struct cftype files[] = {
struct cgroup_subsys freezer_cgrp_subsys = {
.css_alloc = freezer_css_alloc,
.css_online = freezer_css_online,
+ .css_killed = freezer_css_killed,
.css_offline = freezer_css_offline,
.css_free = freezer_css_free,
.attach = freezer_attach,
diff --git a/tools/testing/selftests/cgroup/test_cpuset_prs.sh b/tools/testing/selftests/cgroup/test_cpuset_prs.sh
index 83ef6c781af3..1397ec428c46 100755
--- a/tools/testing/selftests/cgroup/test_cpuset_prs.sh
+++ b/tools/testing/selftests/cgroup/test_cpuset_prs.sh
@@ -137,7 +137,8 @@ LAST_PRESENT_CPU=$(cat /sys/devices/system/cpu/present | sed -e "s/.*[,-]//")
# If isolated CPUs have been reserved at boot time (as shown in
# /sys/devices/system/cpu/isolated), these isolated CPUs should be outside of
# CPUs 0-8 that will be used by this script for testing purpose. If not, some
-# of the tests may fail incorrectly.
+# of the tests may fail incorrectly. The first boot isolated CPU should be
+# 10 or above.
#
# The current set of isolated CPUs (as shown in cpuset.cpus.isolated) should
# be the same as the boot value. If not, wait a bit and retry again just in
@@ -156,7 +157,7 @@ TEST_ISOLCPUS=
[[ -n "$BOOT_ISOLCPUS" ]] && {
FIRST_ISOLCPUS=$(echo $BOOT_ISOLCPUS | sed -e "s/[,-].*//")
echo "Boot isolated CPUs: $BOOT_ISOLCPUS"
- [[ $FIRST_ISOLCPUS -le 8 ]] &&
+ [[ $FIRST_ISOLCPUS -le 9 ]] &&
skip_test "Boot isolated CPUs overlap CPUs to be tested"
}
@@ -433,6 +434,12 @@ TEST_MATRIX=(
# cpuset.cpus is allowed, but those extra CPUs are ignored.
" C2-3:P1 C3:P1 . . . C2-4 . . 0 A1:|A2:2-3 A1:P1|A2:P1"
+ # Clearing cpuset.cpus.exclusive of an invalid partition releases its
+ # exclusive CPUs, including those of its descendants, so a sibling
+ # can claim them.
+ " CX2-3:P1 . . C3 T:O2=0:O3=0:X . . X3 0 XA1:|XB1:3 A1:P-1"
+ " CX2-3:P1 X3 . C3 T:O2=0:O3=0:X . . X3 0 XA1:|XA2:|XB1:3 A1:P-1"
+
# A child cannot recover until its exclusive CPUs are available again.
" 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"
@@ -531,6 +538,31 @@ TEST_MATRIX=(
" C1-4:P1 C3-4:P2 . . T:C4-6 . . . 0 A1:5-6|A2:4 A1:P1|A2:P2 4"
" C1-4:P1 C1-2:P2 . . T:C4-6 . . . 0 A1:4-6|A2:4-6 A1:P1|A2:P-2 ."
+ # 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 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 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 ."
+
+ # 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
# ------ ------ ------ ------ ------ ------ ------ ------ ---- ----- ------ --------
@@ -587,6 +619,7 @@ ISOLCPUS_TEST_MATRIX=(
)
# Test matrix to be run when boot time nohz_full CPUs cover all CPUs except CPUs 0-7
+# This also means that CPU 8 is available and is a nohz_full CPU.
NOHZ_FULL_TEST_MATRIX=(
# old-A1 old-A2 old-A3 old-B1 new-A1 new-A2 new-A3 new-B1 fail ECPUs Pstate ISOLCPUS
# ------ ------ ------ ------ ------ ------ ------ ------ ---- ----- ------ --------
@@ -595,6 +628,10 @@ NOHZ_FULL_TEST_MATRIX=(
" C2-7:P2 . . . C0-4 . . . 0 A1:0-4 A1:P2 0-4"
" C1-7:P2 C5-7:P2 . . . . . C0:P2 0 A1:1-4|A2:5-7|B1:0
A1:P2|A2:P2|B1:P-2 1-7"
+ # An isolated child partition cannot grow to take over the last
+ # housekeeping CPU when its parent expands the exclusive CPUs.
+ " C0-8:X1-8:P1 CX0-8:P2 . . X0-7 . . . 0 A1:0-7|A2:0-7 A1:P1|A2:P-2 ."
+ " C0-8:X0-6:P1 CX0-8:P2 . . X2-8 . . . 0 A1:|A2:2-8 A1:P1|A2:P2 2-8"
)
#
@@ -670,6 +707,11 @@ REMOTE_TEST_MATRIX=(
" C1-4:P1 C5-6 C1-2 . C5 . \
. P1 P1 . O5=0 . p1:3-4|p2:6|c11:1-2|c12:3-4|c21:6|c22:6 \
p1:P1|p2:P1|c11:P1"
+ # Expanding a remote partition should check top cpuset, not its member
+ # parent, for the no CPU left for tasks error.
+ " CX2-3:T . C2-3:X2:P1 . . . \
+ . . X2-3 . . . p1:1,4-7|c11:2-3 \
+ p1:P0|c11:P1"
)
#