Skip to content

Commit 1e940ff

Browse files
committed
Merge patch series "pidfd: improve uapi when task isn't found"
Christian Brauner <[email protected]> says: 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] where they expect ESRCH to be reported, not EINVAL. I think that's a very resonable assumption. This patch tries to allow userspace to distinguish between (1) and (2). This is racy of course but that shouldn't matter. * patches from https://lore.kernel.org/r/[email protected]: selftest/pidfd: add test for thread-group leader pidfd open for thread pidfd: improve uapi when task isn't found pidfd: remove unneeded NULL check from pidfd_prepare() selftests/pidfd: adapt to recent changes Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
2 parents 0af2f6b + 4fc3f73 commit 1e940ff

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
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 || !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
}

tools/testing/selftests/pidfd/pidfd_info_test.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ TEST_F(pidfd_info, thread_group)
299299
/* Opening a thread as a thread-group leader must fail. */
300300
pidfd_thread = sys_pidfd_open(pid_thread, 0);
301301
ASSERT_LT(pidfd_thread, 0);
302+
ASSERT_EQ(errno, ENOENT);
302303

303304
/* Opening a thread as a PIDFD_THREAD must succeed. */
304305
pidfd_thread = sys_pidfd_open(pid_thread, PIDFD_THREAD);
@@ -362,9 +363,9 @@ TEST_F(pidfd_info, thread_group)
362363
ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
363364
ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
364365
ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
365-
/* The thread-group leader exited successfully. Only the specific thread was SIGKILLed. */
366-
ASSERT_TRUE(WIFEXITED(info.exit_code));
367-
ASSERT_EQ(WEXITSTATUS(info.exit_code), 0);
366+
/* Even though the thread-group exited successfully it will still report the group exit code. */
367+
ASSERT_TRUE(WIFSIGNALED(info.exit_code));
368+
ASSERT_EQ(WTERMSIG(info.exit_code), SIGKILL);
368369

369370
/*
370371
* Retrieve exit information for the thread-group leader via the
@@ -375,9 +376,9 @@ TEST_F(pidfd_info, thread_group)
375376
ASSERT_FALSE(!!(info2.mask & PIDFD_INFO_CREDS));
376377
ASSERT_TRUE(!!(info2.mask & PIDFD_INFO_EXIT));
377378

378-
/* The thread-group leader exited successfully. Only the specific thread was SIGKILLed. */
379-
ASSERT_TRUE(WIFEXITED(info2.exit_code));
380-
ASSERT_EQ(WEXITSTATUS(info2.exit_code), 0);
379+
/* Even though the thread-group exited successfully it will still report the group exit code. */
380+
ASSERT_TRUE(WIFSIGNALED(info2.exit_code));
381+
ASSERT_EQ(WTERMSIG(info2.exit_code), SIGKILL);
381382

382383
/* Retrieve exit information for the thread. */
383384
info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;

0 commit comments

Comments
 (0)