Skip to content

Commit 8cf4b73

Browse files
committed
pidfd: improve uapi when task isn't found
We currently report EINVAL whenever a struct pid has no tasked attached anymore thereby conflating two concepts: (1) The task has already been reaped. (2) The caller requested a pidfd for a thread-group leader but the pid actually references a struct pid that isn't used as a thread-group leader. This is causing issues for non-threaded workloads as in [1]. This patch tries to allow userspace to distinguish between (1) and (2). This is racy of course but that shouldn't matter. Link: systemd/systemd#36982 [1] Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Oleg Nesterov <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 1b09094 commit 8cf4b73

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

kernel/fork.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,10 +2108,27 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
21082108
*/
21092109
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
21102110
{
2111-
bool thread = flags & PIDFD_THREAD;
2111+
int err = 0;
21122112

2113-
if (!pid_has_task(pid, thread ? PIDTYPE_PID : PIDTYPE_TGID))
2114-
return -EINVAL;
2113+
if (!(flags & PIDFD_THREAD)) {
2114+
/*
2115+
* If this is struct pid isn't used as a thread-group
2116+
* leader pid but the caller requested to create a
2117+
* thread-group leader pidfd then report ENOENT to the
2118+
* caller as a hint.
2119+
*/
2120+
if (!pid_has_task(pid, PIDTYPE_TGID))
2121+
err = -ENOENT;
2122+
}
2123+
2124+
/*
2125+
* If this wasn't a thread-group leader struct pid or the task
2126+
* got reaped in the meantime report -ESRCH to userspace.
2127+
*/
2128+
if (!pid_has_task(pid, PIDTYPE_PID))
2129+
err = -ESRCH;
2130+
if (err)
2131+
return err;
21152132

21162133
return __pidfd_prepare(pid, flags, ret);
21172134
}

0 commit comments

Comments
 (0)