Commit c59cd3d8 authored by Tejun Heo's avatar Tejun Heo

cgroup: make cgroup_task_iter remember the cgroup being iterated

Currently all cgroup_task_iter functions require @cgrp to be passed
in, which is superflous and increases chance of usage error.  Make
cgroup_task_iter remember the cgroup being iterated and drop @cgrp
argument from next and end functions.

This patch doesn't introduce any behavior differences.
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 0942eeee
...@@ -891,14 +891,14 @@ css_next_descendant_post(struct cgroup_subsys_state *pos, ...@@ -891,14 +891,14 @@ css_next_descendant_post(struct cgroup_subsys_state *pos,
/* A cgroup_task_iter should be treated as an opaque object */ /* A cgroup_task_iter should be treated as an opaque object */
struct cgroup_task_iter { struct cgroup_task_iter {
struct cgroup *origin_cgrp;
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); void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it);
struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp, struct task_struct *cgroup_task_iter_next(struct cgroup_task_iter *it);
struct cgroup_task_iter *it); void cgroup_task_iter_end(struct cgroup_task_iter *it);
void cgroup_task_iter_end(struct cgroup *cgrp, struct cgroup_task_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);
......
...@@ -3205,13 +3205,11 @@ EXPORT_SYMBOL_GPL(css_next_descendant_post); ...@@ -3205,13 +3205,11 @@ EXPORT_SYMBOL_GPL(css_next_descendant_post);
/** /**
* cgroup_advance_task_iter - advance a task itererator to the next css_set * cgroup_advance_task_iter - advance a task itererator to the next css_set
* @cgrp: the cgroup to walk tasks of
* @it: the iterator to advance * @it: the iterator to advance
* *
* Advance @it to the next css_set to walk. * Advance @it to the next css_set to walk.
*/ */
static void cgroup_advance_task_iter(struct cgroup *cgrp, static void cgroup_advance_task_iter(struct cgroup_task_iter *it)
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;
...@@ -3220,7 +3218,7 @@ static void cgroup_advance_task_iter(struct cgroup *cgrp, ...@@ -3220,7 +3218,7 @@ static void cgroup_advance_task_iter(struct cgroup *cgrp,
/* Advance to the next non-empty css_set */ /* Advance to the next non-empty css_set */
do { do {
l = l->next; l = l->next;
if (l == &cgrp->cset_links) { if (l == &it->origin_cgrp->cset_links) {
it->cset_link = NULL; it->cset_link = NULL;
return; return;
} }
...@@ -3257,21 +3255,22 @@ void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it) ...@@ -3257,21 +3255,22 @@ void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it)
cgroup_enable_task_cg_lists(); cgroup_enable_task_cg_lists();
read_lock(&css_set_lock); read_lock(&css_set_lock);
it->origin_cgrp = cgrp;
it->cset_link = &cgrp->cset_links; it->cset_link = &cgrp->cset_links;
cgroup_advance_task_iter(cgrp, it);
cgroup_advance_task_iter(it);
} }
/** /**
* cgroup_task_iter_next - return the next task for the iterator * cgroup_task_iter_next - return the next task for the iterator
* @cgrp: the cgroup to walk tasks of
* @it: the task iterator being iterated * @it: the task iterator being iterated
* *
* The "next" function for task iteration. @it should have been * The "next" function for task iteration. @it should have been
* initialized via cgroup_task_iter_start(). Returns NULL when the * initialized via cgroup_task_iter_start(). Returns NULL when the
* iteration reaches the end. * iteration reaches the end.
*/ */
struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp, struct task_struct *cgroup_task_iter_next(struct cgroup_task_iter *it)
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;
...@@ -3289,7 +3288,7 @@ struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp, ...@@ -3289,7 +3288,7 @@ struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp,
* We reached the end of this task list - move on to the * We reached the end of this task list - move on to the
* next cgrp_cset_link. * next cgrp_cset_link.
*/ */
cgroup_advance_task_iter(cgrp, it); cgroup_advance_task_iter(it);
} else { } else {
it->task = l; it->task = l;
} }
...@@ -3298,12 +3297,11 @@ struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp, ...@@ -3298,12 +3297,11 @@ struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp,
/** /**
* cgroup_task_iter_end - finish task iteration * cgroup_task_iter_end - finish task iteration
* @cgrp: the cgroup to walk tasks of
* @it: the task iterator to finish * @it: the task iterator to finish
* *
* Finish task iteration started by cgroup_task_iter_start(). * Finish task iteration started by cgroup_task_iter_start().
*/ */
void cgroup_task_iter_end(struct cgroup *cgrp, struct cgroup_task_iter *it) void cgroup_task_iter_end(struct cgroup_task_iter *it)
__releases(css_set_lock) __releases(css_set_lock)
{ {
read_unlock(&css_set_lock); read_unlock(&css_set_lock);
...@@ -3409,7 +3407,7 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan) ...@@ -3409,7 +3407,7 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan)
*/ */
heap->size = 0; heap->size = 0;
cgroup_task_iter_start(scan->cgrp, &it); cgroup_task_iter_start(scan->cgrp, &it);
while ((p = cgroup_task_iter_next(scan->cgrp, &it))) { while ((p = cgroup_task_iter_next(&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
...@@ -3442,7 +3440,7 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan) ...@@ -3442,7 +3440,7 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan)
* the heap and wasn't inserted * the heap and wasn't inserted
*/ */
} }
cgroup_task_iter_end(scan->cgrp, &it); cgroup_task_iter_end(&it);
if (heap->size) { if (heap->size) {
for (i = 0; i < heap->size; i++) { for (i = 0; i < heap->size; i++) {
...@@ -3664,7 +3662,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, ...@@ -3664,7 +3662,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
return -ENOMEM; return -ENOMEM;
/* now, populate the array */ /* now, populate the array */
cgroup_task_iter_start(cgrp, &it); cgroup_task_iter_start(cgrp, &it);
while ((tsk = cgroup_task_iter_next(cgrp, &it))) { while ((tsk = cgroup_task_iter_next(&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 */
...@@ -3675,7 +3673,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, ...@@ -3675,7 +3673,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_task_iter_end(cgrp, &it); cgroup_task_iter_end(&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);
...@@ -3724,7 +3722,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -3724,7 +3722,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
cgrp = dentry->d_fsdata; cgrp = dentry->d_fsdata;
cgroup_task_iter_start(cgrp, &it); cgroup_task_iter_start(cgrp, &it);
while ((tsk = cgroup_task_iter_next(cgrp, &it))) { while ((tsk = cgroup_task_iter_next(&it))) {
switch (tsk->state) { switch (tsk->state) {
case TASK_RUNNING: case TASK_RUNNING:
stats->nr_running++; stats->nr_running++;
...@@ -3744,7 +3742,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) ...@@ -3744,7 +3742,7 @@ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
break; break;
} }
} }
cgroup_task_iter_end(cgrp, &it); cgroup_task_iter_end(&it);
err: err:
return ret; return ret;
......
...@@ -281,7 +281,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css) ...@@ -281,7 +281,7 @@ static void update_if_frozen(struct cgroup_subsys_state *css)
/* are all tasks frozen? */ /* are all tasks frozen? */
cgroup_task_iter_start(css->cgroup, &it); cgroup_task_iter_start(css->cgroup, &it);
while ((task = cgroup_task_iter_next(css->cgroup, &it))) { while ((task = cgroup_task_iter_next(&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_task_iter_end(css->cgroup, &it); cgroup_task_iter_end(&it);
out_unlock: out_unlock:
spin_unlock_irq(&freezer->lock); spin_unlock_irq(&freezer->lock);
} }
...@@ -327,9 +327,9 @@ static void freeze_cgroup(struct freezer *freezer) ...@@ -327,9 +327,9 @@ static void freeze_cgroup(struct freezer *freezer)
struct task_struct *task; struct task_struct *task;
cgroup_task_iter_start(cgroup, &it); cgroup_task_iter_start(cgroup, &it);
while ((task = cgroup_task_iter_next(cgroup, &it))) while ((task = cgroup_task_iter_next(&it)))
freeze_task(task); freeze_task(task);
cgroup_task_iter_end(cgroup, &it); cgroup_task_iter_end(&it);
} }
static void unfreeze_cgroup(struct freezer *freezer) static void unfreeze_cgroup(struct freezer *freezer)
...@@ -339,9 +339,9 @@ static void unfreeze_cgroup(struct freezer *freezer) ...@@ -339,9 +339,9 @@ static void unfreeze_cgroup(struct freezer *freezer)
struct task_struct *task; struct task_struct *task;
cgroup_task_iter_start(cgroup, &it); cgroup_task_iter_start(cgroup, &it);
while ((task = cgroup_task_iter_next(cgroup, &it))) while ((task = cgroup_task_iter_next(&it)))
__thaw_task(task); __thaw_task(task);
cgroup_task_iter_end(cgroup, &it); cgroup_task_iter_end(&it);
} }
/** /**
......
...@@ -1804,7 +1804,7 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, ...@@ -1804,7 +1804,7 @@ static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
struct task_struct *task; struct task_struct *task;
cgroup_task_iter_start(cgroup, &it); cgroup_task_iter_start(cgroup, &it);
while ((task = cgroup_task_iter_next(cgroup, &it))) { while ((task = cgroup_task_iter_next(&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_task_iter_end(cgroup, &it); cgroup_task_iter_end(&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_task_iter_end(cgroup, &it); cgroup_task_iter_end(&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