[PATCH] introduce for_other_threads(p, t)

Oleg Nesterov posted 1 patch 2 years, 2 months ago
fs/exec.c                    |  3 +--
include/linux/sched/signal.h |  3 +++
kernel/signal.c              | 11 ++++-------
3 files changed, 8 insertions(+), 9 deletions(-)
[PATCH] introduce for_other_threads(p, t)
Posted by Oleg Nesterov 2 years, 2 months ago
Cosmetic, but imho it makes the usage look more clear and simple, the
new helper doesn't require to initialize "t".

After this change while_each_thread() has only 3 users, and it is only
used in the do/while loops.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 fs/exec.c                    |  3 +--
 include/linux/sched/signal.h |  3 +++
 kernel/signal.c              | 11 ++++-------
 3 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 6518e33ea813..e5bdce5e36ee 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1580,11 +1580,10 @@ static void check_unsafe_exec(struct linux_binprm *bprm)
 	 * will be able to manipulate the current directory, etc.
 	 * It would be nice to force an unshare instead...
 	 */
-	t = p;
 	n_fs = 1;
 	spin_lock(&p->fs->lock);
 	rcu_read_lock();
-	while_each_thread(p, t) {
+	for_other_threads(p, t) {
 		if (t->fs == p->fs)
 			n_fs++;
 	}
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index d7fa3ca2fa53..134a2c0bc283 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -655,6 +655,9 @@ extern bool current_is_single_threaded(void);
 #define while_each_thread(g, t) \
 	while ((t = next_thread(t)) != g)
 
+#define for_other_threads(p, t)	\
+	for (t = p; (t = next_thread(t)) != p; )
+
 #define __for_each_thread(signal, t)	\
 	list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
 
diff --git a/kernel/signal.c b/kernel/signal.c
index ccfc3ded5672..f5781a54d89d 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -1376,12 +1376,12 @@ int force_sig_info(struct kernel_siginfo *info)
  */
 int zap_other_threads(struct task_struct *p)
 {
-	struct task_struct *t = p;
+	struct task_struct *t;
 	int count = 0;
 
 	p->signal->group_stop_count = 0;
 
-	while_each_thread(p, t) {
+	for_other_threads(p, t) {
 		task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
 		/* Don't require de_thread to wait for the vhost_worker */
 		if ((t->flags & (PF_IO_WORKER | PF_USER_WORKER)) != PF_USER_WORKER)
@@ -2457,12 +2457,10 @@ static bool do_signal_stop(int signr)
 			sig->group_exit_code = signr;
 
 		sig->group_stop_count = 0;
-
 		if (task_set_jobctl_pending(current, signr | gstop))
 			sig->group_stop_count++;
 
-		t = current;
-		while_each_thread(current, t) {
+		for_other_threads(current, t) {
 			/*
 			 * Setting state to TASK_STOPPED for a group
 			 * stop is always done with the siglock held,
@@ -2958,8 +2956,7 @@ static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
 	if (sigisemptyset(&retarget))
 		return;
 
-	t = tsk;
-	while_each_thread(tsk, t) {
+	for_other_threads(tsk, t) {
 		if (t->flags & PF_EXITING)
 			continue;
 
-- 
2.25.1.362.g51ebf55
Re: [PATCH] introduce for_other_threads(p, t)
Posted by Christian Brauner 2 years, 2 months ago
On Mon, Oct 30, 2023 at 04:57:10PM +0100, Oleg Nesterov wrote:
> Cosmetic, but imho it makes the usage look more clear and simple, the
> new helper doesn't require to initialize "t".
> 
> After this change while_each_thread() has only 3 users, and it is only
> used in the do/while loops.
> 
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> ---

Looks good to me,
Reviewed-by: Christian Brauner <brauner@kernel.org>