Commit 0942eeee authored by Tejun Heo's avatar Tejun Heo

cgroup: rename cgroup_iter to cgroup_task_iter

cgroup now has multiple iterators and it's quite confusing to have
something which walks over tasks of a single cgroup named cgroup_iter.
Let's rename it to cgroup_task_iter.

While at it, reformat / update comments and replace the overview
comment above the interface function decls with proper function
comments.  Such overview can be useful but function comments should be
more than enough here.

This is pure rename and doesn't introduce any functional changes.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarLi Zefan <lizefan@huawei.com>
Acked-by: default avatarMichal Hocko <mhocko@suse.cz>
Cc: Matt Helsley <matthltc@us.ibm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Balbir Singh <bsingharora@gmail.com>
parent d515876e
...@@ -889,31 +889,16 @@ css_next_descendant_post(struct cgroup_subsys_state *pos, ...@@ -889,31 +889,16 @@ css_next_descendant_post(struct cgroup_subsys_state *pos,
for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \ for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
(pos) = css_next_descendant_post((pos), (css))) (pos) = css_next_descendant_post((pos), (css)))
/* A cgroup_iter should be treated as an opaque object */ /* A cgroup_task_iter should be treated as an opaque object */
struct cgroup_iter { struct cgroup_task_iter {
struct list_head *cset_link; struct list_head *cset_link;
struct list_head *task; struct list_head *task;
}; };
/* void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it);
* To iterate across the tasks in a cgroup: struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp,
* struct cgroup_task_iter *it);
* 1) call cgroup_iter_start to initialize an iterator void cgroup_task_iter_end(struct cgroup *cgrp, struct cgroup_task_iter *it);
*
* 2) call cgroup_iter_next() to retrieve member tasks until it
* returns NULL or until you want to end the iteration
*
* 3) call cgroup_iter_end() to destroy the iterator.
*
* Or, call cgroup_scan_tasks() to iterate through every task in a
* cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
* the test_task() callback, but not while calling the process_task()
* callback.
*/
void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
struct cgroup_iter *it);
void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
int cgroup_scan_tasks(struct cgroup_scanner *scan); int cgroup_scan_tasks(struct cgroup_scanner *scan);
int cgroup_attach_task_all(struct task_struct *from, struct task_struct *); int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from); int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
......
...@@ -367,9 +367,11 @@ static struct cgrp_cset_link init_cgrp_cset_link; ...@@ -367,9 +367,11 @@ static struct cgrp_cset_link init_cgrp_cset_link;
static int cgroup_init_idr(struct cgroup_subsys *ss, static int cgroup_init_idr(struct cgroup_subsys *ss,
struct cgroup_subsys_state *css); struct cgroup_subsys_state *css);
/* css_set_lock protects the list of css_set objects, and the /*
* chain of tasks off each css_set. Nests outside task->alloc_lock * css_set_lock protects the list of css_set objects, and the chain of
* due to cgroup_iter_start() */ * tasks off each css_set. Nests outside task->alloc_lock due to
* cgroup_task_iter_start().
*/
static DEFINE_RWLOCK(css_set_lock); static DEFINE_RWLOCK(css_set_lock);
static int css_set_count; static int css_set_count;
...@@ -394,10 +396,12 @@ static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) ...@@ -394,10 +396,12 @@ static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
return key; return key;
} }
/* We don't maintain the lists running through each css_set to its /*
* task until after the first call to cgroup_iter_start(). This * We don't maintain the lists running through each css_set to its task
* reduces the fork()/exit() overhead for people who have cgroups * until after the first call to cgroup_task_iter_start(). This reduces
* compiled into their kernel but not actually in use */ * the fork()/exit() overhead for people who have cgroups compiled into
* their kernel but not actually in use.
*/
static int use_task_css_set_links __read_mostly; static int use_task_css_set_links __read_mostly;
static void __put_css_set(struct css_set *cset, int taskexit) static void __put_css_set(struct css_set *cset, int taskexit)
...@@ -2982,10 +2986,10 @@ int cgroup_task_count(const struct cgroup *cgrp) ...@@ -2982,10 +2986,10 @@ int cgroup_task_count(const struct cgroup *cgrp)
} }
/* /*
* To reduce the fork() overhead for systems that are not actually * To reduce the fork() overhead for systems that are not actually using
* using their cgroups capability, we don't maintain the lists running * their cgroups capability, we don't maintain the lists running through
* through each css_set to its tasks until we see the list actually * each css_set to its tasks until we see the list actually used - in other
* used - in other words after the first call to cgroup_iter_start(). * words after the first call to cgroup_task_iter_start().
*/ */
static void cgroup_enable_task_cg_lists(void) static void cgroup_enable_task_cg_lists(void)
{ {
...@@ -3199,11 +3203,15 @@ css_next_descendant_post(struct cgroup_subsys_state *pos, ...@@ -3199,11 +3203,15 @@ css_next_descendant_post(struct cgroup_subsys_state *pos,
} }
EXPORT_SYMBOL_GPL(css_next_descendant_post); EXPORT_SYMBOL_GPL(css_next_descendant_post);
/* /**
* Advance a list_head iterator. The iterator should be positioned at * cgroup_advance_task_iter - advance a task itererator to the next css_set
* the start of a css_set * @cgrp: the cgroup to walk tasks of
* @it: the iterator to advance
*
* Advance @it to the next css_set to walk.
*/ */
static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) static void cgroup_advance_task_iter(struct cgroup *cgrp,
struct cgroup_task_iter *it)
{ {
struct list_head *l = it->cset_link; struct list_head *l = it->cset_link;
struct cgrp_cset_link *link; struct cgrp_cset_link *link;
...@@ -3223,7 +3231,21 @@ static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) ...@@ -3223,7 +3231,21 @@ static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
it->task = cset->tasks.next; it->task = cset->tasks.next;
} }
void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) /**
* cgroup_task_iter_start - initiate task iteration
* @cgrp: the cgroup to walk tasks of
* @it: the task iterator to use
*
* Initiate iteration through the tasks of @cgrp. The caller can call
* cgroup_task_iter_next() to walk through the tasks until the function
* returns NULL. On completion of iteration, cgroup_task_iter_end() must
* be called.
*
* Note that this function acquires a lock which is released when the
* iteration finishes. The caller can't sleep while iteration is in
* progress.
*/
void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it)
__acquires(css_set_lock) __acquires(css_set_lock)
{ {
/* /*
...@@ -3236,11 +3258,20 @@ void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) ...@@ -3236,11 +3258,20 @@ void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
read_lock(&css_set_lock); read_lock(&css_set_lock);
it->cset_link = &cgrp->cset_links; it->cset_link = &cgrp->cset_links;
cgroup_advance_iter(cgrp, it); cgroup_advance_task_iter(cgrp, it);
} }
struct task_struct *cgroup_iter_next(struct cgroup *cgrp, /**
struct cgroup_iter *it) * cgroup_task_iter_next - return the next task for the iterator
* @cgrp: the cgroup to walk tasks of
* @it: the task iterator being iterated
*
* The "next" function for task iteration. @it should have been
* initialized via cgroup_task_iter_start(). Returns NULL when the
* iteration reaches the end.
*/
struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp,
struct cgroup_task_iter *it)
{ {
struct task_struct *res; struct task_struct *res;
struct list_head *l = it->task; struct list_head *l = it->task;
...@@ -3254,16 +3285,25 @@ struct task_struct *cgroup_iter_next(struct cgroup *cgrp, ...@@ -3254,16 +3285,25 @@ struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
l = l->next; l = l->next;
link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
if (l == &link->cset->tasks) { if (l == &link->cset->tasks) {
/* We reached the end of this task list - move on to /*
* the next cg_cgroup_link */ * We reached the end of this task list - move on to the
cgroup_advance_iter(cgrp, it); * next cgrp_cset_link.
*/
cgroup_advance_task_iter(cgrp, it);
} else { } else {
it->task = l; it->task = l;
} }
return res; return res;
} }
void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) /**
* cgroup_task_iter_end - finish task iteration
* @cgrp: the cgroup to walk tasks of
* @it: the task iterator to finish
*
* Finish task iteration started by cgroup_task_iter_start().
*/
void cgroup_task_iter_end(struct cgroup *cgrp, struct cgroup_task_iter *it)
__releases(css_set_lock) __releases(css_set_lock)
{ {
read_unlock(&css_set_lock); read_unlock(&css_set_lock);
...@@ -3312,7 +3352,7 @@ static inline int started_after(void *p1, void *p2) ...@@ -3312,7 +3352,7 @@ static inline int started_after(void *p1, void *p2)
* Iterate through all the tasks in a cgroup, calling test_task() for each, * Iterate through all the tasks in a cgroup, calling test_task() for each,
* and if it returns true, call process_task() for it also. * and if it returns true, call process_task() for it also.
* The test_task pointer may be NULL, meaning always true (select all tasks). * The test_task pointer may be NULL, meaning always true (select all tasks).
* Effectively duplicates cgroup_iter_{start,next,end}() * Effectively duplicates cgroup_task_iter_{start,next,end}()
* but does not lock css_set_lock for the call to process_task(). * but does not lock css_set_lock for the call to process_task().
* The struct cgroup_scanner may be embedded in any structure of the caller's * The struct cgroup_scanner may be embedded in any structure of the caller's
* creation. * creation.
...@@ -3333,7 +3373,7 @@ static inline int started_after(void *p1, void *p2) ...@@ -3333,7 +3373,7 @@ static inline int started_after(void *p1, void *p2)
int cgroup_scan_tasks(struct cgroup_scanner *scan) int cgroup_scan_tasks(struct cgroup_scanner *scan)
{ {
int retval, i; int retval, i;
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *p, *dropped; struct task_struct *p, *dropped;
/* Never dereference latest_task, since it's not refcounted */ /* Never dereference latest_task, since it's not refcounted */
struct task_struct *latest_task = NULL; struct task_struct *latest_task = NULL;
...@@ -3368,8 +3408,8 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan) ...@@ -3368,8 +3408,8 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan)
* guarantees forward progress and that we don't miss any tasks. * guarantees forward progress and that we don't miss any tasks.
*/ */
heap->size = 0; heap->size = 0;
cgroup_iter_start(scan->cgrp, &it); cgroup_task_iter_start(scan->cgrp, &it);
while ((p = cgroup_iter_next(scan->cgrp, &it))) { while ((p = cgroup_task_iter_next(scan->cgrp, &it))) {
/* /*
* Only affect tasks that qualify per the caller's callback, * Only affect tasks that qualify per the caller's callback,
* if he provided one * if he provided one
...@@ -3402,7 +3442,7 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan) ...@@ -3402,7 +3442,7 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan)
* the heap and wasn't inserted * the heap and wasn't inserted
*/ */
} }
cgroup_iter_end(scan->cgrp, &it); cgroup_task_iter_end(scan->cgrp, &it);
if (heap->size) { if (heap->size) {
for (i = 0; i < heap->size; i++) { for (i = 0; i < heap->size; i++) {
...@@ -3608,7 +3648,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, ...@@ -3608,7 +3648,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
pid_t *array; pid_t *array;
int length; int length;
int pid, n = 0; /* used for populating the array */ int pid, n = 0; /* used for populating the array */
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *tsk; struct task_struct *tsk;
struct cgroup_pidlist *l; struct cgroup_pidlist *l;
...@@ -3623,8 +3663,8 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, ...@@ -3623,8 +3663,8 @@ 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 */
cgroup_iter_start(cgrp, &it); cgroup_task_iter_start(cgrp, &it);
while ((tsk = cgroup_iter_next(cgrp, &it))) { while ((tsk = cgroup_task_iter_next(cgrp, &it))) {
if (unlikely(n == length)) if (unlikely(n == length))
break; break;
/* get tgid or pid for procs or tasks file respectively */ /* get tgid or pid for procs or tasks file respectively */
...@@ -3635,7 +3675,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, ...@@ -3635,7 +3675,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
if (pid > 0) /* make sure to only use valid results */ if (pid > 0) /* make sure to only use valid results */
array[n++] = pid; array[n++] = pid;
} }
cgroup_iter_end(cgrp, &it); cgroup_task_iter_end(cgrp, &it);
length = n; length = n;
/* now sort & (if procs) strip out duplicates */ /* now sort & (if procs) strip out duplicates */
sort(array, length, sizeof(pid_t), cmppid, NULL); sort(array, length, sizeof(pid_t), cmppid, NULL);
...@@ -3669,7 +3709,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -3669,7 +3709,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
{ {
int ret = -EINVAL; int ret = -EINVAL;
struct cgroup *cgrp; struct cgroup *cgrp;
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *tsk; struct task_struct *tsk;
/* /*
...@@ -3683,8 +3723,8 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -3683,8 +3723,8 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
ret = 0; ret = 0;
cgrp = dentry->d_fsdata; cgrp = dentry->d_fsdata;
cgroup_iter_start(cgrp, &it); cgroup_task_iter_start(cgrp, &it);
while ((tsk = cgroup_iter_next(cgrp, &it))) { while ((tsk = cgroup_task_iter_next(cgrp, &it))) {
switch (tsk->state) { switch (tsk->state) {
case TASK_RUNNING: case TASK_RUNNING:
stats->nr_running++; stats->nr_running++;
...@@ -3704,7 +3744,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -3704,7 +3744,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
break; break;
} }
} }
cgroup_iter_end(cgrp, &it); cgroup_task_iter_end(cgrp, &it);
err: err:
return ret; return ret;
...@@ -5137,7 +5177,7 @@ void cgroup_fork(struct task_struct *child) ...@@ -5137,7 +5177,7 @@ void cgroup_fork(struct task_struct *child)
* Adds the task to the list running through its css_set if necessary and * Adds the task to the list running through its css_set if necessary and
* call the subsystem fork() callbacks. Has to be after the task is * call the subsystem fork() callbacks. Has to be after the task is
* visible on the task list in case we race with the first call to * visible on the task list in case we race with the first call to
* cgroup_iter_start() - to guarantee that the new task ends up on its * cgroup_task_iter_start() - to guarantee that the new task ends up on its
* list. * list.
*/ */
void cgroup_post_fork(struct task_struct *child) void cgroup_post_fork(struct task_struct *child)
......
...@@ -258,7 +258,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css) ...@@ -258,7 +258,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
{ {
struct freezer *freezer = css_freezer(css); struct freezer *freezer = css_freezer(css);
struct cgroup_subsys_state *pos; struct cgroup_subsys_state *pos;
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *task; struct task_struct *task;
WARN_ON_ONCE(!rcu_read_lock_held()); WARN_ON_ONCE(!rcu_read_lock_held());
...@@ -279,9 +279,9 @@ static void update_if_frozen(struct cgroup_subsys_state *css) ...@@ -279,9 +279,9 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
} }
/* are all tasks frozen? */ /* are all tasks frozen? */
cgroup_iter_start(css->cgroup, &it); cgroup_task_iter_start(css->cgroup, &it);
while ((task = cgroup_iter_next(css->cgroup, &it))) { while ((task = cgroup_task_iter_next(css->cgroup, &it))) {
if (freezing(task)) { if (freezing(task)) {
/* /*
* freezer_should_skip() indicates that the task * freezer_should_skip() indicates that the task
...@@ -296,7 +296,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css) ...@@ -296,7 +296,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
freezer->state |= CGROUP_FROZEN; freezer->state |= CGROUP_FROZEN;
out_iter_end: out_iter_end:
cgroup_iter_end(css->cgroup, &it); cgroup_task_iter_end(css->cgroup, &it);
out_unlock: out_unlock:
spin_unlock_irq(&freezer->lock); spin_unlock_irq(&freezer->lock);
} }
...@@ -323,25 +323,25 @@ static int freezer_read(struct cgroup_subsys_state *css, struct cftype *cft, ...@@ -323,25 +323,25 @@ static int freezer_read(struct cgroup_subsys_state *css, struct cftype *cft,
static void freeze_cgroup(struct freezer *freezer) static void freeze_cgroup(struct freezer *freezer)
{ {
struct cgroup *cgroup = freezer->css.cgroup; struct cgroup *cgroup = freezer->css.cgroup;
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *task; struct task_struct *task;
cgroup_iter_start(cgroup, &it); cgroup_task_iter_start(cgroup, &it);
while ((task = cgroup_iter_next(cgroup, &it))) while ((task = cgroup_task_iter_next(cgroup, &it)))
freeze_task(task); freeze_task(task);
cgroup_iter_end(cgroup, &it); cgroup_task_iter_end(cgroup, &it);
} }
static void unfreeze_cgroup(struct freezer *freezer) static void unfreeze_cgroup(struct freezer *freezer)
{ {
struct cgroup *cgroup = freezer->css.cgroup; struct cgroup *cgroup = freezer->css.cgroup;
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *task; struct task_struct *task;
cgroup_iter_start(cgroup, &it); cgroup_task_iter_start(cgroup, &it);
while ((task = cgroup_iter_next(cgroup, &it))) while ((task = cgroup_task_iter_next(cgroup, &it)))
__thaw_task(task); __thaw_task(task);
cgroup_iter_end(cgroup, &it); cgroup_task_iter_end(cgroup, &it);
} }
/** /**
......
...@@ -1800,11 +1800,11 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, ...@@ -1800,11 +1800,11 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1; totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
for_each_mem_cgroup_tree(iter, memcg) { for_each_mem_cgroup_tree(iter, memcg) {
struct cgroup *cgroup = iter->css.cgroup; struct cgroup *cgroup = iter->css.cgroup;
struct cgroup_iter it; struct cgroup_task_iter it;
struct task_struct *task; struct task_struct *task;
cgroup_iter_start(cgroup, &it); cgroup_task_iter_start(cgroup, &it);
while ((task = cgroup_iter_next(cgroup, &it))) { while ((task = cgroup_task_iter_next(cgroup, &it))) {
switch (oom_scan_process_thread(task, totalpages, NULL, switch (oom_scan_process_thread(task, totalpages, NULL,
false)) { false)) {
case OOM_SCAN_SELECT: case OOM_SCAN_SELECT:
...@@ -1817,7 +1817,7 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, ...@@ -1817,7 +1817,7 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
case OOM_SCAN_CONTINUE: case OOM_SCAN_CONTINUE:
continue; continue;
case OOM_SCAN_ABORT: case OOM_SCAN_ABORT:
cgroup_iter_end(cgroup, &it); cgroup_task_iter_end(cgroup, &it);
mem_cgroup_iter_break(memcg, iter); mem_cgroup_iter_break(memcg, iter);
if (chosen) if (chosen)
put_task_struct(chosen); put_task_struct(chosen);
...@@ -1834,7 +1834,7 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, ...@@ -1834,7 +1834,7 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
get_task_struct(chosen); get_task_struct(chosen);
} }
} }
cgroup_iter_end(cgroup, &it); cgroup_task_iter_end(cgroup, &it);
} }
if (!chosen) if (!chosen)
......
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