linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tycho Andersen <tycho@tycho.pizza>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Christian Brauner <brauner@kernel.org>,
	linux-kernel@vger.kernel.org, linux-api@vger.kernel.org,
	Tycho Andersen <tandersen@netflix.com>
Subject: Re: [PATCH v3 1/3] pidfd: allow pidfd_open() on non-thread-group leaders
Date: Tue, 23 Jan 2024 14:10:15 -0700	[thread overview]
Message-ID: <ZbArN3EYRfhrNs3o@tycho.pizza> (raw)
In-Reply-To: <20240123195608.GB9978@redhat.com>

On Tue, Jan 23, 2024 at 08:56:08PM +0100, Oleg Nesterov wrote:
> Too late for me, but I don't understand this patch after a quick glance.
> perhaps I missed something...

Thanks for taking a look.

> On 01/23, Tycho Andersen wrote:
> >
> > @@ -256,6 +256,17 @@ void release_task(struct task_struct *p)
> >  	write_lock_irq(&tasklist_lock);
> >  	ptrace_release_task(p);
> >  	thread_pid = get_pid(p->thread_pid);
> > +
> > +	/*
> > +	 * If we're not the leader, notify any waiters on our pidfds. Note that
> > +	 * we don't want to notify the leader until /everyone/ in the thread
> > +	 * group is dead, viz. the condition below.
> > +	 *
> > +	 * We have to do this here, since __exit_signal() will
> > +	 * __unhash_processes(), and break do_notify_pidfd()'s lookup.
> > +	 */
> > +	if (!thread_group_leader(p))
> > +		do_notify_pidfd(p);
> 
> This doesn't look consistent.
> 
> If the task is a group leader do_notify_pidfd() is called by exit_notify()
> when it becomes a zombie (if no other threads), before it is reaped by its
> parent (unless autoreap).

There is another path, also in release_task(), that I was trying to
mirror since it deals explicitly with sub-threads but,

> If it is a sub-thread, it is called by release_task() above. Note that a
> sub-thread can become a zombie too if it is traced.

I didn't know about this.

> >  	__exit_signal(p);
> 
> and,  do_notify_pidfd() is called before __exit_signal() which does
> __unhash_process() -> detach_pid(PIDTYPE_PID).
> 
> Doesn't this mean that pidfd_poll() can hang? thread_group_exited()
> won't return true after do_notify_pidfd() above, not to mention that
> thread_group_empty() is not possible if !thread_group_leader().

I was wondering about this too, but the test_non_tgl_poll_exit test in
the next patch tests exactly this and works as expected.

> So. When do we want to do do_notify_pidfd() ? Whe the task (leader or not)
> becomes a zombie (passes exit_notify) or when it is reaped by release_task?

It seems like we'd want it when exit_notify() is called in principle,
since that's when the pid actually dies. When it is reaped is "mostly
unrelated". Something like,

1. in the "normal" exit_notify() paths via do_notify_parent()
2. if none of those cases are true (aka the final else in
   exit_notify()) and the thread is not ptraced
3. via release_task() finally if this was the thread group leader and
   it died before some sub-thread

then in pidfd_poll(), we can do:

    if (!tsk || (tsk->exit_state >= 0) || thread_group_exited())
        do_notify_pidfd();

?

> Either way pidfd_poll() needs more changes with this patch and it can't
> use thread_group_exited(). If do_notify_pidfd() is called by release_task()
> after __exit_signal(), it can just check pid_has_task(PIDTYPE_PID).

I suppose this is why my test works, since pid_task(PIDTYPE_PID) is null
after release_task(). But if we want it to happen earlier, we'll have
to do something like the above.

Tycho

  reply	other threads:[~2024-01-23 21:10 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-23 15:34 [PATCH v3 0/3] pidfds for non thread group leaders Tycho Andersen
2024-01-23 15:34 ` [PATCH v3 1/3] pidfd: allow pidfd_open() on non-thread-group leaders Tycho Andersen
2024-01-23 19:56   ` Oleg Nesterov
2024-01-23 21:10     ` Tycho Andersen [this message]
2024-01-23 22:22       ` Oleg Nesterov
2024-01-24  1:25         ` Oleg Nesterov
2024-01-25 14:08       ` Oleg Nesterov
2024-01-25 17:17         ` Christian Brauner
2024-01-25 17:51           ` Oleg Nesterov
2024-01-25 18:03             ` Tycho Andersen
2024-01-25 18:25               ` Oleg Nesterov
2024-01-25 18:30                 ` Oleg Nesterov
2024-01-25 18:36                   ` Tycho Andersen
2024-01-26  9:49                     ` Christian Brauner
2024-01-26  9:42             ` Christian Brauner
2024-01-26 14:33               ` Oleg Nesterov
2024-01-26  9:47         ` Christian Brauner
2024-01-26 14:33           ` Oleg Nesterov
2024-01-27 14:26             ` Christian Brauner
2024-01-26 21:50         ` Tycho Andersen
2024-01-27 10:54           ` Oleg Nesterov
2024-01-27 14:33             ` Christian Brauner
2024-01-27 15:55             ` Tycho Andersen
2024-01-27 16:31               ` Oleg Nesterov
2024-01-27 17:20                 ` Tycho Andersen
2024-01-27 19:31                   ` Oleg Nesterov
2024-01-27 20:44                     ` Tycho Andersen
2024-01-27 21:10                       ` Oleg Nesterov
2024-01-29 11:23                         ` [RFC PATCH] pidfd: implement PIDFD_THREAD flag for pidfd_open() Oleg Nesterov
2024-01-29 13:41                           ` Christian Brauner
2024-01-29 14:31                             ` Tycho Andersen
2024-01-29 15:14                               ` Christian Brauner
2024-01-30 11:21                             ` Oleg Nesterov
2024-01-31 18:11                           ` Andy Lutomirski
2024-01-31 18:48                             ` Oleg Nesterov
2024-01-31 19:14                               ` Oleg Nesterov
2024-01-31 19:24                                 ` Andy Lutomirski
2024-01-31 19:46                                   ` Christian Brauner
2024-01-31 19:50                                     ` Andy Lutomirski
2024-02-01 13:30                                       ` Christian Brauner
2024-02-01 13:39                                         ` Christian Brauner
2024-02-01 19:33                                           ` Andy Lutomirski
2024-01-23 15:34 ` [PATCH v3 2/3] selftests/pidfd: add non-thread-group leader tests Tycho Andersen
2024-01-23 15:34 ` [PATCH v3 3/3] clone: allow CLONE_THREAD | CLONE_PIDFD together Tycho Andersen

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=ZbArN3EYRfhrNs3o@tycho.pizza \
    --to=tycho@tycho.pizza \
    --cc=brauner@kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=tandersen@netflix.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;
as well as URLs for NNTP newsgroup(s).