Skip to content

Commit a9d7de0

Browse files
committed
Merge patch series "pidfs: ensure consistent ENOENT/ESRCH reporting"
Christian Brauner <[email protected]> says: 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. * patches from https://lore.kernel.org/[email protected]: pidfs: ensure consistent ENOENT/ESRCH reporting exit: move wake_up_all() pidfd waiters into __unhash_process() Link: https://lore.kernel.org/[email protected] Signed-off-by: Christian Brauner <[email protected]>
2 parents 1e940ff + 17f1b08 commit a9d7de0

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

kernel/exit.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,13 @@ struct release_task_post {
133133
static void __unhash_process(struct release_task_post *post, struct task_struct *p,
134134
bool group_dead)
135135
{
136+
struct pid *pid = task_pid(p);
137+
136138
nr_threads--;
139+
137140
detach_pid(post->pids, p, PIDTYPE_PID);
141+
wake_up_all(&pid->wait_pidfd);
142+
138143
if (group_dead) {
139144
detach_pid(post->pids, p, PIDTYPE_TGID);
140145
detach_pid(post->pids, p, PIDTYPE_PGID);

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

kernel/pid.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,6 @@ static void __change_pid(struct pid **pids, struct task_struct *task,
359359
hlist_del_rcu(&task->pid_links[type]);
360360
*pid_ptr = new;
361361

362-
if (type == PIDTYPE_PID) {
363-
WARN_ON_ONCE(pid_has_task(pid, PIDTYPE_PID));
364-
wake_up_all(&pid->wait_pidfd);
365-
}
366-
367362
for (tmp = PIDTYPE_MAX; --tmp >= 0; )
368363
if (pid_has_task(pid, tmp))
369364
return;

0 commit comments

Comments
 (0)