Skip to content

Commit 734d451

Browse files
lizf-oshtejun
authored andcommitted
cpuset: update cs->effective_{cpus, mems} when config changes
We're going to have separate user-configured masks and effective ones. Eventually configured masks can only be changed by writing cpuset.cpus and cpuset.mems, and they won't be restricted by parent cpuset. While effective masks reflect cpu/memory hotplug and hierachical restriction, and these are the real masks that apply to the tasks in the cpuset. We calculate effective mask this way: - top cpuset's effective_mask == online_mask, otherwise - cpuset's effective_mask == configured_mask & parent effective_mask, if the result is empty, it inherits parent effective mask. Those behavior changes are for default hierarchy only. For legacy hierarchy, effective_mask and configured_mask are the same, so we won't break old interfaces. To make cs->effective_{cpus,mems} to be effective masks, we need to - update the effective masks at hotplug - update the effective masks at config change - take on ancestor's mask when the effective mask is empty The second item is done here. We don't need to treat root_cs specially in update_cpumasks_hier(). This won't introduce behavior change. v3: - add a WARN_ON() to check if effective masks are the same with configured masks on legacy hierarchy. - pass trialcs->cpus_allowed to update_cpumasks_hier() and add a comment for it. Similar change for update_nodemasks_hier(). Suggested by Tejun. v2: - revise the comment in update_{cpu,node}masks_hier(), suggested by Tejun. - fix to use @cp instead of @cs in these two functions. Signed-off-by: Li Zefan <lizefan@huawei.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 1344ab9 commit 734d451

1 file changed

Lines changed: 54 additions & 34 deletions

File tree

‎kernel/cpuset.c‎

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -855,36 +855,45 @@ static void update_tasks_cpumask(struct cpuset *cs)
855855
}
856856

857857
/*
858-
* update_tasks_cpumask_hier - Update the cpumasks of tasks in the hierarchy.
859-
* @root_cs: the root cpuset of the hierarchy
860-
* @update_root: update root cpuset or not?
858+
* update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
859+
* @cs: the cpuset to consider
860+
* @new_cpus: temp variable for calculating new effective_cpus
861+
*
862+
* When congifured cpumask is changed, the effective cpumasks of this cpuset
863+
* and all its descendants need to be updated.
861864
*
862-
* This will update cpumasks of tasks in @root_cs and all other empty cpusets
863-
* which take on cpumask of @root_cs.
865+
* On legacy hierachy, effective_cpus will be the same with cpu_allowed.
864866
*
865867
* Called with cpuset_mutex held
866868
*/
867-
static void update_tasks_cpumask_hier(struct cpuset *root_cs, bool update_root)
869+
static void update_cpumasks_hier(struct cpuset *cs, struct cpumask *new_cpus)
868870
{
869871
struct cpuset *cp;
870872
struct cgroup_subsys_state *pos_css;
871873

872874
rcu_read_lock();
873-
cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
874-
if (cp == root_cs) {
875-
if (!update_root)
876-
continue;
877-
} else {
878-
/* skip the whole subtree if @cp have some CPU */
879-
if (!cpumask_empty(cp->cpus_allowed)) {
880-
pos_css = css_rightmost_descendant(pos_css);
881-
continue;
882-
}
875+
cpuset_for_each_descendant_pre(cp, pos_css, cs) {
876+
struct cpuset *parent = parent_cs(cp);
877+
878+
cpumask_and(new_cpus, cp->cpus_allowed, parent->effective_cpus);
879+
880+
/* Skip the whole subtree if the cpumask remains the same. */
881+
if (cpumask_equal(new_cpus, cp->effective_cpus)) {
882+
pos_css = css_rightmost_descendant(pos_css);
883+
continue;
883884
}
885+
884886
if (!css_tryget_online(&cp->css))
885887
continue;
886888
rcu_read_unlock();
887889

890+
mutex_lock(&callback_mutex);
891+
cpumask_copy(cp->effective_cpus, new_cpus);
892+
mutex_unlock(&callback_mutex);
893+
894+
WARN_ON(!cgroup_on_dfl(cp->css.cgroup) &&
895+
!cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
896+
888897
update_tasks_cpumask(cp);
889898

890899
rcu_read_lock();
@@ -940,7 +949,8 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
940949
cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
941950
mutex_unlock(&callback_mutex);
942951

943-
update_tasks_cpumask_hier(cs, true);
952+
/* use trialcs->cpus_allowed as a temp variable */
953+
update_cpumasks_hier(cs, trialcs->cpus_allowed);
944954

945955
if (is_load_balanced)
946956
rebuild_sched_domains_locked();
@@ -1091,36 +1101,45 @@ static void update_tasks_nodemask(struct cpuset *cs)
10911101
}
10921102

10931103
/*
1094-
* update_tasks_nodemask_hier - Update the nodemasks of tasks in the hierarchy.
1095-
* @cs: the root cpuset of the hierarchy
1096-
* @update_root: update the root cpuset or not?
1104+
* update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1105+
* @cs: the cpuset to consider
1106+
* @new_mems: a temp variable for calculating new effective_mems
1107+
*
1108+
* When configured nodemask is changed, the effective nodemasks of this cpuset
1109+
* and all its descendants need to be updated.
10971110
*
1098-
* This will update nodemasks of tasks in @root_cs and all other empty cpusets
1099-
* which take on nodemask of @root_cs.
1111+
* On legacy hiearchy, effective_mems will be the same with mems_allowed.
11001112
*
11011113
* Called with cpuset_mutex held
11021114
*/
1103-
static void update_tasks_nodemask_hier(struct cpuset *root_cs, bool update_root)
1115+
static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
11041116
{
11051117
struct cpuset *cp;
11061118
struct cgroup_subsys_state *pos_css;
11071119

11081120
rcu_read_lock();
1109-
cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
1110-
if (cp == root_cs) {
1111-
if (!update_root)
1112-
continue;
1113-
} else {
1114-
/* skip the whole subtree if @cp have some CPU */
1115-
if (!nodes_empty(cp->mems_allowed)) {
1116-
pos_css = css_rightmost_descendant(pos_css);
1117-
continue;
1118-
}
1121+
cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1122+
struct cpuset *parent = parent_cs(cp);
1123+
1124+
nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
1125+
1126+
/* Skip the whole subtree if the nodemask remains the same. */
1127+
if (nodes_equal(*new_mems, cp->effective_mems)) {
1128+
pos_css = css_rightmost_descendant(pos_css);
1129+
continue;
11191130
}
1131+
11201132
if (!css_tryget_online(&cp->css))
11211133
continue;
11221134
rcu_read_unlock();
11231135

1136+
mutex_lock(&callback_mutex);
1137+
cp->effective_mems = *new_mems;
1138+
mutex_unlock(&callback_mutex);
1139+
1140+
WARN_ON(!cgroup_on_dfl(cp->css.cgroup) &&
1141+
nodes_equal(cp->mems_allowed, cp->effective_mems));
1142+
11241143
update_tasks_nodemask(cp);
11251144

11261145
rcu_read_lock();
@@ -1188,7 +1207,8 @@ static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
11881207
cs->mems_allowed = trialcs->mems_allowed;
11891208
mutex_unlock(&callback_mutex);
11901209

1191-
update_tasks_nodemask_hier(cs, true);
1210+
/* use trialcs->mems_allowed as a temp variable */
1211+
update_nodemasks_hier(cs, &cs->mems_allowed);
11921212
done:
11931213
return retval;
11941214
}

0 commit comments

Comments
 (0)