taskstats: fill_stats_for_tgid: use for_each_thread()
authorOleg Nesterov <oleg@redhat.com>
Sat, 9 Sep 2023 21:49:51 +0000 (23:49 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 4 Oct 2023 17:41:57 +0000 (10:41 -0700)
do/while_each_thread should be avoided when possible.

Plus I _think_ this change allows to avoid lock_task_sighand() but I am
not sure, I forgot everything about taskstats.  In any case, this code
does not look right in that the same thread can be accounted twice:
taskstats_exit() can account the exiting thread in signal->stats and drop
->siglock but this thread is still on the thread-group list, so
lock_task_sighand() can't help.

Link: https://lkml.kernel.org/r/20230909214951.GA24274@redhat.com
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/taskstats.c

index 8ce3fa0c19e2d7ae7457469da00d184d5834aadc..4354ea231fabd7dc53be2f61fae0cad043d3c81d 100644 (file)
@@ -233,9 +233,8 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
        else
                memset(stats, 0, sizeof(*stats));
 
-       tsk = first;
        start_time = ktime_get_ns();
-       do {
+       for_each_thread(first, tsk) {
                if (tsk->exit_state)
                        continue;
                /*
@@ -258,7 +257,7 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
 
                stats->nvcsw += tsk->nvcsw;
                stats->nivcsw += tsk->nivcsw;
-       } while_each_thread(first, tsk);
+       }
 
        unlock_task_sighand(first, &flags);
        rc = 0;