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: Tejun Heo <tj@kernel.org>
Acked-by: Li Zefan <lizefan@huawei.com>
Acked-by: Michal Hocko <mhocko@suse.cz>
Cc: Matt Helsley <matthltc@us.ibm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Balbir Singh <bsingharora@gmail.com>
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 15c93f9c..abc62ea 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -3205,13 +3205,11 @@
/**
* 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
*
* Advance @it to the next css_set to walk.
*/
-static void cgroup_advance_task_iter(struct cgroup *cgrp,
- struct cgroup_task_iter *it)
+static void cgroup_advance_task_iter(struct cgroup_task_iter *it)
{
struct list_head *l = it->cset_link;
struct cgrp_cset_link *link;
@@ -3220,7 +3218,7 @@
/* Advance to the next non-empty css_set */
do {
l = l->next;
- if (l == &cgrp->cset_links) {
+ if (l == &it->origin_cgrp->cset_links) {
it->cset_link = NULL;
return;
}
@@ -3257,21 +3255,22 @@
cgroup_enable_task_cg_lists();
read_lock(&css_set_lock);
+
+ it->origin_cgrp = cgrp;
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
- * @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 *cgroup_task_iter_next(struct cgroup_task_iter *it)
{
struct task_struct *res;
struct list_head *l = it->task;
@@ -3289,7 +3288,7 @@
* We reached the end of this task list - move on to the
* next cgrp_cset_link.
*/
- cgroup_advance_task_iter(cgrp, it);
+ cgroup_advance_task_iter(it);
} else {
it->task = l;
}
@@ -3298,12 +3297,11 @@
/**
* 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)
+void cgroup_task_iter_end(struct cgroup_task_iter *it)
__releases(css_set_lock)
{
read_unlock(&css_set_lock);
@@ -3409,7 +3407,7 @@
*/
heap->size = 0;
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,
* if he provided one
@@ -3442,7 +3440,7 @@
* the heap and wasn't inserted
*/
}
- cgroup_task_iter_end(scan->cgrp, &it);
+ cgroup_task_iter_end(&it);
if (heap->size) {
for (i = 0; i < heap->size; i++) {
@@ -3664,7 +3662,7 @@
return -ENOMEM;
/* now, populate the array */
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))
break;
/* get tgid or pid for procs or tasks file respectively */
@@ -3675,7 +3673,7 @@
if (pid > 0) /* make sure to only use valid results */
array[n++] = pid;
}
- cgroup_task_iter_end(cgrp, &it);
+ cgroup_task_iter_end(&it);
length = n;
/* now sort & (if procs) strip out duplicates */
sort(array, length, sizeof(pid_t), cmppid, NULL);
@@ -3724,7 +3722,7 @@
cgrp = dentry->d_fsdata;
cgroup_task_iter_start(cgrp, &it);
- while ((tsk = cgroup_task_iter_next(cgrp, &it))) {
+ while ((tsk = cgroup_task_iter_next(&it))) {
switch (tsk->state) {
case TASK_RUNNING:
stats->nr_running++;
@@ -3744,7 +3742,7 @@
break;
}
}
- cgroup_task_iter_end(cgrp, &it);
+ cgroup_task_iter_end(&it);
err:
return ret;