Skip to content

Commit 17f1b08

Browse files
committed
pidfs: ensure consistent ENOENT/ESRCH reporting
In a prior patch series we tried to cleanly differentiate between: (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. as this was causing issues for non-threaded workloads. But there's cases where the current simple logic is wrong. Specifically, if the pid was a leader pid and the check races with __unhash_process(). Stabilize this by using the pidfd waitqueue lock. Link: https://lore.kernel.org/[email protected] Reviewed-by: Oleg Nesterov <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 35c9701 commit 17f1b08

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

kernel/fork.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,28 +2108,26 @@ 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-
int err = 0;
2112-
2113-
if (!(flags & PIDFD_THREAD)) {
2111+
/*
2112+
* While holding the pidfd waitqueue lock removing the task
2113+
* linkage for the thread-group leader pid (PIDTYPE_TGID) isn't
2114+
* possible. Thus, if there's still task linkage for PIDTYPE_PID
2115+
* not having thread-group leader linkage for the pid means it
2116+
* wasn't a thread-group leader in the first place.
2117+
*/
2118+
scoped_guard(spinlock_irq, &pid->wait_pidfd.lock) {
2119+
/* Task has already been reaped. */
2120+
if (!pid_has_task(pid, PIDTYPE_PID))
2121+
return -ESRCH;
21142122
/*
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.
2123+
* If this struct pid isn't used as a thread-group
2124+
* leader but the caller requested to create a
2125+
* thread-group leader pidfd then report ENOENT.
21192126
*/
2120-
if (!pid_has_task(pid, PIDTYPE_TGID))
2121-
err = -ENOENT;
2127+
if (!(flags & PIDFD_THREAD) && !pid_has_task(pid, PIDTYPE_TGID))
2128+
return -ENOENT;
21222129
}
21232130

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;
2132-
21332131
return __pidfd_prepare(pid, flags, ret);
21342132
}
21352133

0 commit comments

Comments
 (0)