Skip to content

Commit fc560a2

Browse files
committed
cpuset: replace cpuset->stack_list with cpuset_for_each_descendant_pre()
Implement cpuset_for_each_descendant_pre() and replace the cpuset-specific tree walking using cpuset->stack_list with it. Signed-off-by: Tejun Heo <tj@kernel.org> Reviewed-by: Michal Hocko <mhocko@suse.cz> Acked-by: Li Zefan <lizefan@huawei.com>
1 parent 5d21cc2 commit fc560a2

1 file changed

Lines changed: 48 additions & 75 deletions

File tree

‎kernel/cpuset.c‎

Lines changed: 48 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ struct cpuset {
103103
/* for custom sched domain */
104104
int relax_domain_level;
105105

106-
/* used for walking a cpuset hierarchy */
107-
struct list_head stack_list;
108-
109106
struct work_struct hotplug_work;
110107
};
111108

@@ -207,6 +204,20 @@ static struct cpuset top_cpuset = {
207204
cgroup_for_each_child((pos_cgrp), (parent_cs)->css.cgroup) \
208205
if (is_cpuset_online(((child_cs) = cgroup_cs((pos_cgrp)))))
209206

207+
/**
208+
* cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
209+
* @des_cs: loop cursor pointing to the current descendant
210+
* @pos_cgrp: used for iteration
211+
* @root_cs: target cpuset to walk ancestor of
212+
*
213+
* Walk @des_cs through the online descendants of @root_cs. Must be used
214+
* with RCU read locked. The caller may modify @pos_cgrp by calling
215+
* cgroup_rightmost_descendant() to skip subtree.
216+
*/
217+
#define cpuset_for_each_descendant_pre(des_cs, pos_cgrp, root_cs) \
218+
cgroup_for_each_descendant_pre((pos_cgrp), (root_cs)->css.cgroup) \
219+
if (is_cpuset_online(((des_cs) = cgroup_cs((pos_cgrp)))))
220+
210221
/*
211222
* There are two global mutexes guarding cpuset structures - cpuset_mutex
212223
* and callback_mutex. The latter may nest inside the former. We also
@@ -507,31 +518,24 @@ update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
507518
return;
508519
}
509520

510-
static void
511-
update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
521+
static void update_domain_attr_tree(struct sched_domain_attr *dattr,
522+
struct cpuset *root_cs)
512523
{
513-
LIST_HEAD(q);
514-
515-
list_add(&c->stack_list, &q);
516-
while (!list_empty(&q)) {
517-
struct cpuset *cp;
518-
struct cgroup *cont;
519-
struct cpuset *child;
520-
521-
cp = list_first_entry(&q, struct cpuset, stack_list);
522-
list_del(q.next);
524+
struct cpuset *cp;
525+
struct cgroup *pos_cgrp;
523526

524-
if (cpumask_empty(cp->cpus_allowed))
527+
rcu_read_lock();
528+
cpuset_for_each_descendant_pre(cp, pos_cgrp, root_cs) {
529+
/* skip the whole subtree if @cp doesn't have any CPU */
530+
if (cpumask_empty(cp->cpus_allowed)) {
531+
pos_cgrp = cgroup_rightmost_descendant(pos_cgrp);
525532
continue;
533+
}
526534

527535
if (is_sched_load_balance(cp))
528536
update_domain_attr(dattr, cp);
529-
530-
rcu_read_lock();
531-
cpuset_for_each_child(child, cont, cp)
532-
list_add_tail(&child->stack_list, &q);
533-
rcu_read_unlock();
534537
}
538+
rcu_read_unlock();
535539
}
536540

537541
/*
@@ -591,7 +595,6 @@ update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
591595
static int generate_sched_domains(cpumask_var_t **domains,
592596
struct sched_domain_attr **attributes)
593597
{
594-
LIST_HEAD(q); /* queue of cpusets to be scanned */
595598
struct cpuset *cp; /* scans q */
596599
struct cpuset **csa; /* array of all cpuset ptrs */
597600
int csn; /* how many cpuset ptrs in csa so far */
@@ -600,6 +603,7 @@ static int generate_sched_domains(cpumask_var_t **domains,
600603
struct sched_domain_attr *dattr; /* attributes for custom domains */
601604
int ndoms = 0; /* number of sched domains in result */
602605
int nslot; /* next empty doms[] struct cpumask slot */
606+
struct cgroup *pos_cgrp;
603607

604608
doms = NULL;
605609
dattr = NULL;
@@ -627,33 +631,27 @@ static int generate_sched_domains(cpumask_var_t **domains,
627631
goto done;
628632
csn = 0;
629633

630-
list_add(&top_cpuset.stack_list, &q);
631-
while (!list_empty(&q)) {
632-
struct cgroup *cont;
633-
struct cpuset *child; /* scans child cpusets of cp */
634-
635-
cp = list_first_entry(&q, struct cpuset, stack_list);
636-
list_del(q.next);
637-
638-
if (cpumask_empty(cp->cpus_allowed))
639-
continue;
640-
634+
rcu_read_lock();
635+
cpuset_for_each_descendant_pre(cp, pos_cgrp, &top_cpuset) {
641636
/*
642-
* All child cpusets contain a subset of the parent's cpus, so
643-
* just skip them, and then we call update_domain_attr_tree()
644-
* to calc relax_domain_level of the corresponding sched
645-
* domain.
637+
* Continue traversing beyond @cp iff @cp has some CPUs and
638+
* isn't load balancing. The former is obvious. The
639+
* latter: All child cpusets contain a subset of the
640+
* parent's cpus, so just skip them, and then we call
641+
* update_domain_attr_tree() to calc relax_domain_level of
642+
* the corresponding sched domain.
646643
*/
647-
if (is_sched_load_balance(cp)) {
648-
csa[csn++] = cp;
644+
if (!cpumask_empty(cp->cpus_allowed) &&
645+
!is_sched_load_balance(cp))
649646
continue;
650-
}
651647

652-
rcu_read_lock();
653-
cpuset_for_each_child(child, cont, cp)
654-
list_add_tail(&child->stack_list, &q);
655-
rcu_read_unlock();
656-
}
648+
if (is_sched_load_balance(cp))
649+
csa[csn++] = cp;
650+
651+
/* skip @cp's subtree */
652+
pos_cgrp = cgroup_rightmost_descendant(pos_cgrp);
653+
}
654+
rcu_read_unlock();
657655

658656
for (i = 0; i < csn; i++)
659657
csa[i]->pn = i;
@@ -2068,31 +2066,6 @@ static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
20682066
move_member_tasks_to_cpuset(cs, parent);
20692067
}
20702068

2071-
/*
2072-
* Helper function to traverse cpusets.
2073-
* It can be used to walk the cpuset tree from top to bottom, completing
2074-
* one layer before dropping down to the next (thus always processing a
2075-
* node before any of its children).
2076-
*/
2077-
static struct cpuset *cpuset_next(struct list_head *queue)
2078-
{
2079-
struct cpuset *cp;
2080-
struct cpuset *child; /* scans child cpusets of cp */
2081-
struct cgroup *cont;
2082-
2083-
if (list_empty(queue))
2084-
return NULL;
2085-
2086-
cp = list_first_entry(queue, struct cpuset, stack_list);
2087-
list_del(queue->next);
2088-
rcu_read_lock();
2089-
cpuset_for_each_child(child, cont, cp)
2090-
list_add_tail(&child->stack_list, queue);
2091-
rcu_read_unlock();
2092-
2093-
return cp;
2094-
}
2095-
20962069
/**
20972070
* cpuset_propagate_hotplug_workfn - propagate CPU/memory hotplug to a cpuset
20982071
* @cs: cpuset in interest
@@ -2229,12 +2202,12 @@ static void cpuset_hotplug_workfn(struct work_struct *work)
22292202
/* if cpus or mems went down, we need to propagate to descendants */
22302203
if (cpus_offlined || mems_offlined) {
22312204
struct cpuset *cs;
2232-
LIST_HEAD(queue);
2205+
struct cgroup *pos_cgrp;
22332206

2234-
list_add_tail(&top_cpuset.stack_list, &queue);
2235-
while ((cs = cpuset_next(&queue)))
2236-
if (cs != &top_cpuset)
2237-
schedule_cpuset_propagate_hotplug(cs);
2207+
rcu_read_lock();
2208+
cpuset_for_each_descendant_pre(cs, pos_cgrp, &top_cpuset)
2209+
schedule_cpuset_propagate_hotplug(cs);
2210+
rcu_read_unlock();
22382211
}
22392212

22402213
mutex_unlock(&cpuset_mutex);

0 commit comments

Comments
 (0)