kill do_each_thread()
authorOleg Nesterov <oleg@redhat.com>
Thu, 17 Aug 2023 16:37:08 +0000 (18:37 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 21 Aug 2023 20:46:25 +0000 (13:46 -0700)
Eric has pointed out that we still have 3 users of do_each_thread().
Change them to use for_each_process_thread() and kill this helper.

There is a subtle change, after do_each_thread/while_each_thread g == t ==
&init_task, while after for_each_process_thread() they both point to
nowhere, but this doesn't matter.

> Why is for_each_process_thread() better than do_each_thread()?

Say, for_each_process_thread() is rcu safe, do_each_thread() is not.

And certainly

for_each_process_thread(p, t) {
do_something(p, t);
}

looks better than

do_each_thread(p, t) {
do_something(p, t);
} while_each_thread(p, t);

And again, there are only 3 users of this awkward helper left.  It should
have been killed years ago and in fact I thought it had already been
killed.  It uses while_each_thread() which needs some changes.

Link: https://lkml.kernel.org/r/20230817163708.GA8248@redhat.com
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: "Christian Brauner (Microsoft)" <brauner@kernel.org>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Jiri Slaby <jirislaby@kernel.org> # tty/serial
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
arch/ia64/kernel/mca.c
drivers/tty/tty_io.c
fs/fs_struct.c
include/linux/sched/signal.h

index 92ede80..2671688 100644 (file)
@@ -1630,10 +1630,10 @@ default_monarch_init_process(struct notifier_block *self, unsigned long val, voi
        }
        printk("\n\n");
        if (read_trylock(&tasklist_lock)) {
-               do_each_thread (g, t) {
+               for_each_process_thread(g, t) {
                        printk("\nBacktrace of pid %d (%s)\n", t->pid, t->comm);
                        show_stack(t, NULL, KERN_DEFAULT);
-               } while_each_thread (g, t);
+               }
                read_unlock(&tasklist_lock);
        }
        /* FIXME: This will not restore zapped printk locks. */
index 63db04b..27d8e3a 100644 (file)
@@ -3031,7 +3031,7 @@ void __do_SAK(struct tty_struct *tty)
        } while_each_pid_task(session, PIDTYPE_SID, p);
 
        /* Now kill any processes that happen to have the tty open */
-       do_each_thread(g, p) {
+       for_each_process_thread(g, p) {
                if (p->signal->tty == tty) {
                        tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
                                   task_pid_nr(p), p->comm);
@@ -3048,7 +3048,7 @@ void __do_SAK(struct tty_struct *tty)
                                        PIDTYPE_SID);
                }
                task_unlock(p);
-       } while_each_thread(g, p);
+       }
        read_unlock(&tasklist_lock);
        put_pid(session);
 }
index 04b3f5b..64c2d08 100644 (file)
@@ -62,7 +62,7 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
        int count = 0;
 
        read_lock(&tasklist_lock);
-       do_each_thread(g, p) {
+       for_each_process_thread(g, p) {
                task_lock(p);
                fs = p->fs;
                if (fs) {
@@ -79,7 +79,7 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
                        spin_unlock(&fs->lock);
                }
                task_unlock(p);
-       } while_each_thread(g, p);
+       }
        read_unlock(&tasklist_lock);
        while (count--)
                path_put(old_root);
index 669e8cf..0deebe2 100644 (file)
@@ -648,13 +648,6 @@ extern void flush_itimer_signals(void);
 
 extern bool current_is_single_threaded(void);
 
-/*
- * Careful: do_each_thread/while_each_thread is a double loop so
- *          'break' will not work as expected - use goto instead.
- */
-#define do_each_thread(g, t) \
-       for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
-
 #define while_each_thread(g, t) \
        while ((t = next_thread(t)) != g)