public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: Christian Brauner <brauner@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, Jeff Layton <jlayton@kernel.org>,
	Lennart Poettering <lennart@poettering.net>,
	Daan De Meyer <daan.j.demeyer@gmail.com>,
	Mike Yuan <me@yhndnzj.com>
Subject: Re: [PATCH RFC v2 1/3] pidfs: improve multi-threaded exec and premature thread-group leader exit polling
Date: Wed, 19 Mar 2025 15:00:52 +0100	[thread overview]
Message-ID: <20250319140052.GC26879@redhat.com> (raw)
In-Reply-To: <20250318-work-pidfs-thread_group-v2-1-2677898ffa2e@kernel.org>

On 03/18, Christian Brauner wrote:
>
> @@ -746,8 +751,23 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
>  	 * sub-thread or delay_group_leader(), wake up the
>  	 * PIDFD_THREAD waiters.
>  	 */
> -	if (!thread_group_empty(tsk))
> -		do_notify_pidfd(tsk);
> +	if (!thread_group_empty(tsk)) {
> +		if (delay_group_leader(tsk)) {
> +			struct pid *pid;
> +
> +			/*
> +			 * This is a thread-group leader exiting before
> +			 * all of its subthreads have exited allow pidfd
> +			 * polling to detect this case and delay exit
> +			 * notification until the last thread has
> +			 * exited.
> +			 */
> +			pid = task_pid(tsk);
> +			WRITE_ONCE(pid->delayed_leader, 1);

This is racy, tsk->exit_state is already set so pidfd_poll() can see
task->exit_state && !pid->delayed_leader.

But this is minor. I can't understand all these complications,
probably because I barely slept tonight ;) I will re-read this patch
again tomorrow, but could you explain why we can't simply use the
trivial patch below?

Oleg.
---

diff --git a/fs/pidfs.c b/fs/pidfs.c
index d980f779c213..8a95920aed98 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -210,7 +210,6 @@ static void pidfd_show_fdinfo(struct seq_file *m, struct file *f)
 static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
 {
 	struct pid *pid = pidfd_pid(file);
-	bool thread = file->f_flags & PIDFD_THREAD;
 	struct task_struct *task;
 	__poll_t poll_flags = 0;
 
@@ -223,7 +222,7 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
 	task = pid_task(pid, PIDTYPE_PID);
 	if (!task)
 		poll_flags = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
-	else if (task->exit_state && (thread || thread_group_empty(task)))
+	else if (task->exit_state && !delay_group_leader(task))
 		poll_flags = EPOLLIN | EPOLLRDNORM;
 
 	return poll_flags;
diff --git a/kernel/exit.c b/kernel/exit.c
index 9916305e34d3..356ca41d313b 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -746,7 +746,7 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
 	 * sub-thread or delay_group_leader(), wake up the
 	 * PIDFD_THREAD waiters.
 	 */
-	if (!thread_group_empty(tsk))
+	if (!delay_group_leader(tsk))
 		do_notify_pidfd(tsk);
 
 	if (unlikely(tsk->ptrace)) {


  reply	other threads:[~2025-03-19 14:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-18  9:52 [PATCH RFC v2 0/3] pidfs: handle multi-threaded exec and premature thread-group leader exit Christian Brauner
2025-03-18  9:52 ` [PATCH RFC v2 1/3] pidfs: improve multi-threaded exec and premature thread-group leader exit polling Christian Brauner
2025-03-19 14:00   ` Oleg Nesterov [this message]
2025-03-19 15:39     ` Christian Brauner
2025-03-18  9:52 ` [PATCH RFC v2 2/3] selftests/pidfd: first test for multi-threaded exec polling Christian Brauner
2025-03-18  9:52 ` [PATCH RFC v2 3/3] selftests/pidfd: second " Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250319140052.GC26879@redhat.com \
    --to=oleg@redhat.com \
    --cc=brauner@kernel.org \
    --cc=daan.j.demeyer@gmail.com \
    --cc=jlayton@kernel.org \
    --cc=lennart@poettering.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=me@yhndnzj.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox