Skip to content

Commit b1bcaed

Browse files
Chen Ridonghtejun
authored andcommitted
cpuset: Treat cpusets in attaching as populated
Currently, the check for whether a partition is populated does not account for tasks in the cpuset of attaching. This is a corner case that can leave a task stuck in a partition with no effective CPUs. The race condition occurs as follows: cpu0 cpu1 //cpuset A with cpu N migrate task p to A cpuset_can_attach // with effective cpus // check ok // cpuset_mutex is not held // clear cpuset.cpus.exclusive // making effective cpus empty update_exclusive_cpumask // tasks_nocpu_error check ok // empty effective cpus, partition valid cpuset_attach ... // task p stays in A, with non-effective cpus. To fix this issue, this patch introduces cs_is_populated, which considers tasks in the attaching cpuset. This new helper is used in validate_change and partition_is_populated. Fixes: e2d5990 ("cgroup/cpuset: Allow no-task partition to have empty cpuset.cpus.effective") Signed-off-by: Chen Ridong <[email protected]> Reviewed-by: Waiman Long <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 318e18e commit b1bcaed

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

kernel/cgroup/cpuset.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ static inline bool is_in_v2_mode(void)
356356
(cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
357357
}
358358

359+
static inline bool cpuset_is_populated(struct cpuset *cs)
360+
{
361+
lockdep_assert_held(&cpuset_mutex);
362+
363+
/* Cpusets in the process of attaching should be considered as populated */
364+
return cgroup_is_populated(cs->css.cgroup) ||
365+
cs->attach_in_progress;
366+
}
367+
359368
/**
360369
* partition_is_populated - check if partition has tasks
361370
* @cs: partition root to be checked
@@ -373,19 +382,29 @@ static inline bool is_in_v2_mode(void)
373382
static inline bool partition_is_populated(struct cpuset *cs,
374383
struct cpuset *excluded_child)
375384
{
376-
struct cgroup_subsys_state *css;
377-
struct cpuset *child;
385+
struct cpuset *cp;
386+
struct cgroup_subsys_state *pos_css;
378387

379-
if (cs->css.cgroup->nr_populated_csets)
388+
/*
389+
* We cannot call cs_is_populated(cs) directly, as
390+
* nr_populated_domain_children may include populated
391+
* csets from descendants that are partitions.
392+
*/
393+
if (cs->css.cgroup->nr_populated_csets ||
394+
cs->attach_in_progress)
380395
return true;
381396

382397
rcu_read_lock();
383-
cpuset_for_each_child(child, css, cs) {
384-
if (child == excluded_child)
398+
cpuset_for_each_descendant_pre(cp, pos_css, cs) {
399+
if (cp == cs || cp == excluded_child)
385400
continue;
386-
if (is_partition_valid(child))
401+
402+
if (is_partition_valid(cp)) {
403+
pos_css = css_rightmost_descendant(pos_css);
387404
continue;
388-
if (cgroup_is_populated(child->css.cgroup)) {
405+
}
406+
407+
if (cpuset_is_populated(cp)) {
389408
rcu_read_unlock();
390409
return true;
391410
}
@@ -670,7 +689,7 @@ static int validate_change(struct cpuset *cur, struct cpuset *trial)
670689
* be changed to have empty cpus_allowed or mems_allowed.
671690
*/
672691
ret = -ENOSPC;
673-
if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) {
692+
if (cpuset_is_populated(cur)) {
674693
if (!cpumask_empty(cur->cpus_allowed) &&
675694
cpumask_empty(trial->cpus_allowed))
676695
goto out;

0 commit comments

Comments
 (0)