From 5ffd2c37cb7a53d52099e5ed1fd7ccbc9e358791 Mon Sep 17 00:00:00 2001 From: Oleg Nesterov Date: Thu, 17 Aug 2023 18:37:08 +0200 Subject: kill do_each_thread() 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 Reviewed-by: Kees Cook Cc: "Christian Brauner (Microsoft)" Cc: Eric W. Biederman Cc: Jiri Slaby # tty/serial Signed-off-by: Andrew Morton --- include/linux/sched/signal.h | 7 ------- 1 file changed, 7 deletions(-) (limited to 'include/linux/sched') diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index 669e8cff40c7..0deebe2ab07d 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -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) -- cgit v1.2.3 From dce8f8ed1de1d9d6d27c5ccd202ce4ec163b100c Mon Sep 17 00:00:00 2001 From: Oleg Nesterov Date: Wed, 23 Aug 2023 19:08:06 +0200 Subject: document while_each_thread(), change first_tid() to use for_each_thread() Add the comment to explain that while_each_thread(g,t) is not rcu-safe unless g is stable (e.g. current). Even if g is a group leader and thus can't exit before t, t or another sub-thread can exec and remove g from the thread_group list. The only lockless user of while_each_thread() is first_tid() and it is fine in that it can't loop forever, yet for_each_thread() looks better and I am going to change while_each_thread/next_thread. Link: https://lkml.kernel.org/r/20230823170806.GA11724@redhat.com Signed-off-by: Oleg Nesterov Cc: Eric W. Biederman Signed-off-by: Andrew Morton --- include/linux/sched/signal.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include/linux/sched') diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index 0deebe2ab07d..0014d3adaf84 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -648,6 +648,10 @@ extern void flush_itimer_signals(void); extern bool current_is_single_threaded(void); +/* + * Without tasklist/siglock it is only rcu-safe if g can't exit/exec, + * otherwise next_thread(t) will never reach g after list_del_rcu(g). + */ #define while_each_thread(g, t) \ while ((t = next_thread(t)) != g) -- cgit v1.2.3