cgroup: add @flags to css_task_iter_start() and implement CSS_TASK_ITER_PROCS
authorTejun Heo <tj@kernel.org>
Mon, 15 May 2017 13:34:01 +0000 (09:34 -0400)
committerTejun Heo <tj@kernel.org>
Fri, 21 Jul 2017 15:14:51 +0000 (11:14 -0400)
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: Tejun Heo <tj@kernel.org>
include/linux/cgroup.h
kernel/cgroup/cgroup-v1.c
kernel/cgroup/cgroup.c
kernel/cgroup/cpuset.c
kernel/cgroup/freezer.c
mm/memcontrol.c
net/core/netclassid_cgroup.c

index 308b10797a5440360a427b77f499619a380fe92a..cae5831ae6504f610015917164191b24ccd5095a 100644 (file)
 #define CGROUP_WEIGHT_DFL              100
 #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 */
 struct css_task_iter {
        struct cgroup_subsys            *ss;
+       unsigned int                    flags;
 
        struct list_head                *cset_pos;
        struct list_head                *cset_head;
@@ -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 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 task_struct *css_task_iter_next(struct css_task_iter *it);
 void css_task_iter_end(struct css_task_iter *it);
index 60f72475863eab040fccb672e5d5f242ed6e786c..167aaab04bf97cb2101e9ce14f6226d7c42140da 100644 (file)
@@ -121,7 +121,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
         * ->can_attach() fails.
         */
        do {
-               css_task_iter_start(&from->self, &it);
+               css_task_iter_start(&from->self, 0, &it);
                task = css_task_iter_next(&it);
                if (task)
                        get_task_struct(task);
@@ -373,7 +373,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
        if (!array)
                return -ENOMEM;
        /* 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))) {
                if (unlikely(n == length))
                        break;
@@ -749,7 +749,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
        }
        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))) {
                switch (tsk->state) {
                case TASK_RUNNING:
index e3bda0752501114969390afeaad6e02ea7505415..3c5a37a9a89227a8554686ae57a5691f32a49c08 100644 (file)
@@ -3643,6 +3643,7 @@ static void css_task_iter_advance(struct css_task_iter *it)
        lockdep_assert_held(&css_set_lock);
        WARN_ON_ONCE(!l);
 
+repeat:
        /*
         * Advance iterator to find next entry.  cset->tasks is consumed
         * 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)
                css_task_iter_advance_css_set(it);
        else
                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: the css to walk tasks of
+ * @flags: CSS_TASK_ITER_* flags
  * @it: the task iterator to use
  *
  * 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)
  * returns NULL.  On completion of iteration, css_task_iter_end() must be
  * 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)
 {
        /* no one should try to iterate before mounting cgroups */
@@ -3680,6 +3688,7 @@ void css_task_iter_start(struct cgroup_subsys_state *css,
        spin_lock_irq(&css_set_lock);
 
        it->ss = css->ss;
+       it->flags = flags;
 
        if (it->ss)
                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)
 {
        struct kernfs_open_file *of = s->private;
        struct css_task_iter *it = of->priv;
-       struct task_struct *task;
-
-       do {
-               task = css_task_iter_next(it);
-       } while (task && !thread_group_leader(task));
 
-       return task;
+       return css_task_iter_next(it);
 }
 
 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)
                        return ERR_PTR(-ENOMEM);
                of->priv = it;
-               css_task_iter_start(&cgrp->self, it);
+               css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS, it);
        } else if (!(*pos)++) {
                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);
@@ -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)
 {
-       seq_printf(s, "%d\n", task_tgid_vnr(v));
+       seq_printf(s, "%d\n", task_pid_vnr(v));
        return 0;
 }
 
index ca8376e5008c7c16ca91175e3ad1021ec69e2777..252d70c9a49b13aae41121d0a4dc0c03e912dc7b 100644 (file)
@@ -861,7 +861,7 @@ static void update_tasks_cpumask(struct cpuset *cs)
        struct css_task_iter it;
        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)))
                set_cpus_allowed_ptr(task, cs->effective_cpus);
        css_task_iter_end(&it);
@@ -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()
         * 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))) {
                struct mm_struct *mm;
                bool migrate;
@@ -1284,7 +1284,7 @@ static void update_tasks_flags(struct cpuset *cs)
        struct css_task_iter it;
        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)))
                cpuset_update_task_spread_flag(cs, task);
        css_task_iter_end(&it);
index 1b72d56edce5e2d9253c2c03f203e81aa76bd16c..08236798d17315622d73540d62a89dcbafdc5c77 100644 (file)
@@ -268,7 +268,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
        rcu_read_unlock();
 
        /* are all tasks frozen? */
-       css_task_iter_start(css, &it);
+       css_task_iter_start(css, 0, &it);
 
        while ((task = css_task_iter_next(&it))) {
                if (freezing(task)) {
@@ -320,7 +320,7 @@ static void freeze_cgroup(struct freezer *freezer)
        struct css_task_iter it;
        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)))
                freeze_task(task);
        css_task_iter_end(&it);
@@ -331,7 +331,7 @@ static void unfreeze_cgroup(struct freezer *freezer)
        struct css_task_iter it;
        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)))
                __thaw_task(task);
        css_task_iter_end(&it);
index 3df3c04d73ab08e3bbb663f25b2e195b396d2149..2b2f071f914b5143c45b0192bfefe7daaaa86539 100644 (file)
@@ -917,7 +917,7 @@ int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
                struct css_task_iter it;
                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)))
                        ret = fn(task, arg);
                css_task_iter_end(&it);
index 029a61ac6cdd8a0b4dd54d2be3c5bdf047a82cb0..5e4f04004a49a38ba9d8f2535aa293eeab898155 100644 (file)
@@ -100,7 +100,7 @@ static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
 
        cs->classid = (u32)value;
 
-       css_task_iter_start(css, &it);
+       css_task_iter_start(css, 0, &it);
        while ((p = css_task_iter_next(&it))) {
                task_lock(p);
                iterate_fd(p->files, 0, update_classid_sock,