Commit bc2fb7ed authored by Tejun Heo's avatar Tejun Heo

cgroup: add @flags to css_task_iter_start() and implement CSS_TASK_ITER_PROCS

css_task_iter currently always walks all tasks.  With the scheduled
cgroup v2 thread support, the iterator would need to handle multiple
types of iteration.  As a preparation, add @flags to
css_task_iter_start() and implement CSS_TASK_ITER_PROCS.  If the flag
is not specified, it walks all tasks as before.  When asserted, the
iterator only walks the group leaders.

For now, the only user of the flag is cgroup v2 "cgroup.procs" file
which no longer needs to skip non-leader tasks in cgroup_procs_next().
Note that cgroup v1 "cgroup.procs" can't use the group leader walk as
v1 "cgroup.procs" doesn't mean "list all thread group leaders in the
cgroup" but "list all thread group id's with any threads in the
cgroup".

While at it, update cgroup_procs_show() to use task_pid_vnr() instead
of task_tgid_vnr().  As the iteration guarantees that the function
only sees group leaders, this doesn't change the output and will allow
sharing the function for thread iteration.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
parent 715c809d
...@@ -36,9 +36,13 @@ ...@@ -36,9 +36,13 @@
#define CGROUP_WEIGHT_DFL 100 #define CGROUP_WEIGHT_DFL 100
#define CGROUP_WEIGHT_MAX 10000 #define CGROUP_WEIGHT_MAX 10000
/* walk only threadgroup leaders */
#define CSS_TASK_ITER_PROCS (1U << 0)
/* a css_task_iter should be treated as an opaque object */ /* a css_task_iter should be treated as an opaque object */
struct css_task_iter { struct css_task_iter {
struct cgroup_subsys *ss; struct cgroup_subsys *ss;
unsigned int flags;
struct list_head *cset_pos; struct list_head *cset_pos;
struct list_head *cset_head; struct list_head *cset_head;
...@@ -129,7 +133,7 @@ struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, ...@@ -129,7 +133,7 @@ struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
struct cgroup_subsys_state **dst_cssp); struct cgroup_subsys_state **dst_cssp);
void css_task_iter_start(struct cgroup_subsys_state *css, void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
struct css_task_iter *it); struct css_task_iter *it);
struct task_struct *css_task_iter_next(struct css_task_iter *it); struct task_struct *css_task_iter_next(struct css_task_iter *it);
void css_task_iter_end(struct css_task_iter *it); void css_task_iter_end(struct css_task_iter *it);
......
...@@ -121,7 +121,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) ...@@ -121,7 +121,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
* ->can_attach() fails. * ->can_attach() fails.
*/ */
do { do {
css_task_iter_start(&from->self, &it); css_task_iter_start(&from->self, 0, &it);
task = css_task_iter_next(&it); task = css_task_iter_next(&it);
if (task) if (task)
get_task_struct(task); get_task_struct(task);
...@@ -373,7 +373,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, ...@@ -373,7 +373,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
if (!array) if (!array)
return -ENOMEM; return -ENOMEM;
/* now, populate the array */ /* now, populate the array */
css_task_iter_start(&cgrp->self, &it); css_task_iter_start(&cgrp->self, 0, &it);
while ((tsk = css_task_iter_next(&it))) { while ((tsk = css_task_iter_next(&it))) {
if (unlikely(n == length)) if (unlikely(n == length))
break; break;
...@@ -749,7 +749,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -749,7 +749,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
} }
rcu_read_unlock(); rcu_read_unlock();
css_task_iter_start(&cgrp->self, &it); css_task_iter_start(&cgrp->self, 0, &it);
while ((tsk = css_task_iter_next(&it))) { while ((tsk = css_task_iter_next(&it))) {
switch (tsk->state) { switch (tsk->state) {
case TASK_RUNNING: case TASK_RUNNING:
......
...@@ -3643,6 +3643,7 @@ static void css_task_iter_advance(struct css_task_iter *it) ...@@ -3643,6 +3643,7 @@ static void css_task_iter_advance(struct css_task_iter *it)
lockdep_assert_held(&css_set_lock); lockdep_assert_held(&css_set_lock);
WARN_ON_ONCE(!l); WARN_ON_ONCE(!l);
repeat:
/* /*
* Advance iterator to find next entry. cset->tasks is consumed * Advance iterator to find next entry. cset->tasks is consumed
* first and then ->mg_tasks. After ->mg_tasks, we move onto the * first and then ->mg_tasks. After ->mg_tasks, we move onto the
...@@ -3657,11 +3658,18 @@ static void css_task_iter_advance(struct css_task_iter *it) ...@@ -3657,11 +3658,18 @@ static void css_task_iter_advance(struct css_task_iter *it)
css_task_iter_advance_css_set(it); css_task_iter_advance_css_set(it);
else else
it->task_pos = l; it->task_pos = l;
/* if PROCS, skip over tasks which aren't group leaders */
if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos &&
!thread_group_leader(list_entry(it->task_pos, struct task_struct,
cg_list)))
goto repeat;
} }
/** /**
* css_task_iter_start - initiate task iteration * css_task_iter_start - initiate task iteration
* @css: the css to walk tasks of * @css: the css to walk tasks of
* @flags: CSS_TASK_ITER_* flags
* @it: the task iterator to use * @it: the task iterator to use
* *
* Initiate iteration through the tasks of @css. The caller can call * Initiate iteration through the tasks of @css. The caller can call
...@@ -3669,7 +3677,7 @@ static void css_task_iter_advance(struct css_task_iter *it) ...@@ -3669,7 +3677,7 @@ static void css_task_iter_advance(struct css_task_iter *it)
* returns NULL. On completion of iteration, css_task_iter_end() must be * returns NULL. On completion of iteration, css_task_iter_end() must be
* called. * called.
*/ */
void css_task_iter_start(struct cgroup_subsys_state *css, void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
struct css_task_iter *it) struct css_task_iter *it)
{ {
/* no one should try to iterate before mounting cgroups */ /* no one should try to iterate before mounting cgroups */
...@@ -3680,6 +3688,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css, ...@@ -3680,6 +3688,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css,
spin_lock_irq(&css_set_lock); spin_lock_irq(&css_set_lock);
it->ss = css->ss; it->ss = css->ss;
it->flags = flags;
if (it->ss) if (it->ss)
it->cset_pos = &css->cgroup->e_csets[css->ss->id]; it->cset_pos = &css->cgroup->e_csets[css->ss->id];
...@@ -3753,13 +3762,8 @@ static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) ...@@ -3753,13 +3762,8 @@ static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
{ {
struct kernfs_open_file *of = s->private; struct kernfs_open_file *of = s->private;
struct css_task_iter *it = of->priv; struct css_task_iter *it = of->priv;
struct task_struct *task;
do { return css_task_iter_next(it);
task = css_task_iter_next(it);
} while (task && !thread_group_leader(task));
return task;
} }
static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
...@@ -3780,10 +3784,10 @@ static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) ...@@ -3780,10 +3784,10 @@ static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
if (!it) if (!it)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
of->priv = it; of->priv = it;
css_task_iter_start(&cgrp->self, it); css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS, it);
} else if (!(*pos)++) { } else if (!(*pos)++) {
css_task_iter_end(it); css_task_iter_end(it);
css_task_iter_start(&cgrp->self, it); css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS, it);
} }
return cgroup_procs_next(s, NULL, NULL); return cgroup_procs_next(s, NULL, NULL);
...@@ -3791,7 +3795,7 @@ static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) ...@@ -3791,7 +3795,7 @@ static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
static int cgroup_procs_show(struct seq_file *s, void *v) static int cgroup_procs_show(struct seq_file *s, void *v)
{ {
seq_printf(s, "%d\n", task_tgid_vnr(v)); seq_printf(s, "%d\n", task_pid_vnr(v));
return 0; return 0;
} }
......
...@@ -861,7 +861,7 @@ static void update_tasks_cpumask(struct cpuset *cs) ...@@ -861,7 +861,7 @@ static void update_tasks_cpumask(struct cpuset *cs)
struct css_task_iter it; struct css_task_iter it;
struct task_struct *task; struct task_struct *task;
css_task_iter_start(&cs->css, &it); css_task_iter_start(&cs->css, 0, &it);
while ((task = css_task_iter_next(&it))) while ((task = css_task_iter_next(&it)))
set_cpus_allowed_ptr(task, cs->effective_cpus); set_cpus_allowed_ptr(task, cs->effective_cpus);
css_task_iter_end(&it); css_task_iter_end(&it);
...@@ -1091,7 +1091,7 @@ static void update_tasks_nodemask(struct cpuset *cs) ...@@ -1091,7 +1091,7 @@ static void update_tasks_nodemask(struct cpuset *cs)
* It's ok if we rebind the same mm twice; mpol_rebind_mm() * It's ok if we rebind the same mm twice; mpol_rebind_mm()
* is idempotent. Also migrate pages in each mm to new nodes. * is idempotent. Also migrate pages in each mm to new nodes.
*/ */
css_task_iter_start(&cs->css, &it); css_task_iter_start(&cs->css, 0, &it);
while ((task = css_task_iter_next(&it))) { while ((task = css_task_iter_next(&it))) {
struct mm_struct *mm; struct mm_struct *mm;
bool migrate; bool migrate;
...@@ -1284,7 +1284,7 @@ static void update_tasks_flags(struct cpuset *cs) ...@@ -1284,7 +1284,7 @@ static void update_tasks_flags(struct cpuset *cs)
struct css_task_iter it; struct css_task_iter it;
struct task_struct *task; struct task_struct *task;
css_task_iter_start(&cs->css, &it); css_task_iter_start(&cs->css, 0, &it);
while ((task = css_task_iter_next(&it))) while ((task = css_task_iter_next(&it)))
cpuset_update_task_spread_flag(cs, task); cpuset_update_task_spread_flag(cs, task);
css_task_iter_end(&it); css_task_iter_end(&it);
......
...@@ -268,7 +268,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css) ...@@ -268,7 +268,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
rcu_read_unlock(); rcu_read_unlock();
/* are all tasks frozen? */ /* are all tasks frozen? */
css_task_iter_start(css, &it); css_task_iter_start(css, 0, &it);
while ((task = css_task_iter_next(&it))) { while ((task = css_task_iter_next(&it))) {
if (freezing(task)) { if (freezing(task)) {
...@@ -320,7 +320,7 @@ static void freeze_cgroup(struct freezer *freezer) ...@@ -320,7 +320,7 @@ static void freeze_cgroup(struct freezer *freezer)
struct css_task_iter it; struct css_task_iter it;
struct task_struct *task; struct task_struct *task;
css_task_iter_start(&freezer->css, &it); css_task_iter_start(&freezer->css, 0, &it);
while ((task = css_task_iter_next(&it))) while ((task = css_task_iter_next(&it)))
freeze_task(task); freeze_task(task);
css_task_iter_end(&it); css_task_iter_end(&it);
...@@ -331,7 +331,7 @@ static void unfreeze_cgroup(struct freezer *freezer) ...@@ -331,7 +331,7 @@ static void unfreeze_cgroup(struct freezer *freezer)
struct css_task_iter it; struct css_task_iter it;
struct task_struct *task; struct task_struct *task;
css_task_iter_start(&freezer->css, &it); css_task_iter_start(&freezer->css, 0, &it);
while ((task = css_task_iter_next(&it))) while ((task = css_task_iter_next(&it)))
__thaw_task(task); __thaw_task(task);
css_task_iter_end(&it); css_task_iter_end(&it);
......
...@@ -917,7 +917,7 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, ...@@ -917,7 +917,7 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
struct css_task_iter it; struct css_task_iter it;
struct task_struct *task; struct task_struct *task;
css_task_iter_start(&iter->css, &it); css_task_iter_start(&iter->css, 0, &it);
while (!ret && (task = css_task_iter_next(&it))) while (!ret && (task = css_task_iter_next(&it)))
ret = fn(task, arg); ret = fn(task, arg);
css_task_iter_end(&it); css_task_iter_end(&it);
......
...@@ -100,7 +100,7 @@ static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft, ...@@ -100,7 +100,7 @@ static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
cs->classid = (u32)value; cs->classid = (u32)value;
css_task_iter_start(css, &it); css_task_iter_start(css, 0, &it);
while ((p = css_task_iter_next(&it))) { while ((p = css_task_iter_next(&it))) {
task_lock(p); task_lock(p);
iterate_fd(p->files, 0, update_classid_sock, iterate_fd(p->files, 0, update_classid_sock,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment