From: Oleg Nesterov <oleg@redhat.com>
To: Tycho Andersen <tycho@tycho.pizza>
Cc: Christian Brauner <brauner@kernel.org>,
"Eric W . Biederman" <ebiederm@xmission.com>,
linux-kernel@vger.kernel.org, linux-api@vger.kernel.org,
Tycho Andersen <tandersen@netflix.com>
Subject: Re: [PATCH] pidfd: getfd should always report ESRCH if a task is exiting
Date: Tue, 6 Feb 2024 18:37:22 +0100 [thread overview]
Message-ID: <20240206173722.GA3593@redhat.com> (raw)
In-Reply-To: <20240206164308.62620-1-tycho@tycho.pizza>
On 02/06, Tycho Andersen wrote:
>
> From: Tycho Andersen <tandersen@netflix.com>
>
> We can get EBADF from __pidfd_fget() if a task is currently exiting, which
> might be confusing.
agreed, because EBADF looks as if the "fd" argument was wrong,
> Let's check PF_EXITING, and just report ESRCH if so.
agreed, we can pretend that the task has already exited,
But:
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -688,7 +688,7 @@ static int pidfd_getfd(struct pid *pid, int fd)
> int ret;
>
> task = get_pid_task(pid, PIDTYPE_PID);
> - if (!task)
> + if (!task || task->flags & PF_EXITING)
> return -ESRCH;
This looks racy. Suppose that pidfd_getfd() races with the exiting task.
It is possible that this task sets PF_EXITING and does exit_files()
after the "task->flags & PF_EXITING" check above and before pidfd_getfd()
does __pidfd_fget(), in this case pidfd_getfd() still returns the same
EBADF we want to avoid.
Perhaps we can change pidfd_getfd() to do
if (IS_ERR(file))
return (task->flags & PF_EXITING) ? -ESRCH : PTR_ERR(file);
instead?
This needs a comment to explain the PF_EXITING check. And perhaps another
comment to explain that we can't miss PF_EXITING if the target task has
already passed exit_files, both exit_files() and fget_task() take the same
task_lock(task).
What do you think?
Oleg.
next prev parent reply other threads:[~2024-02-06 17:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-06 16:43 [PATCH] pidfd: getfd should always report ESRCH if a task is exiting Tycho Andersen
2024-02-06 17:37 ` Oleg Nesterov [this message]
2024-02-06 17:55 ` Tycho Andersen
2024-02-06 18:06 ` Oleg Nesterov
2024-02-06 18:09 ` Tycho Andersen
2024-02-06 19:25 ` Oleg Nesterov
2024-02-06 19:35 ` Tycho Andersen
2024-02-07 9:11 ` Christian Brauner
2024-02-07 10:28 ` Oleg Nesterov
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=20240206173722.GA3593@redhat.com \
--to=oleg@redhat.com \
--cc=brauner@kernel.org \
--cc=ebiederm@xmission.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tandersen@netflix.com \
--cc=tycho@tycho.pizza \
/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).