Skip to content

Commit c17112a

Browse files
committed
Merge tag 'core-process-v5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux
Pull pidfd/waitid updates from Christian Brauner: "This contains two features and various tests. First, it adds support for waiting on process through pidfds by adding the P_PIDFD type to the waitid() syscall. This completes the basic functionality of the pidfd api (cf. [1]). In the meantime we also have a new adition to the userspace projects that make use of the pidfd api. The qt project was nice enough to send a mail pointing out that they have a pr up to switch to the pidfd api (cf. [2]). Second, this tag contains an extension to the waitid() syscall to make it possible to wait on the current process group in a race free manner (even though the actual problem is very unlikely) by specifing 0 together with the P_PGID type. This extension traces back to a discussion on the glibc development mailing list. There are also a range of tests for the features above. Additionally, the test-suite which detected the pidfd-polling race we fixed in [3] is included in this tag" [1] https://lwn.net/Articles/794707/ [2] https://codereview.qt-project.org/c/qt/qtbase/+/108456 [3] commit b191d64 ("pidfd: fix a poll race when setting exit_state") * tag 'core-process-v5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux: waitid: Add support for waiting for the current process group tests: add pidfd poll tests tests: move common definitions and functions into pidfd.h pidfd: add pidfd_wait tests pidfd: add P_PIDFD to waitid()
2 parents 4d856f7 + 821cc7b commit c17112a

12 files changed

Lines changed: 473 additions & 26 deletions

File tree

‎include/linux/pid.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ extern struct pid init_struct_pid;
7272

7373
extern const struct file_operations pidfd_fops;
7474

75+
struct file;
76+
77+
extern struct pid *pidfd_pid(const struct file *file);
78+
7579
static inline struct pid *get_pid(struct pid *pid)
7680
{
7781
if (pid)

‎include/uapi/linux/wait.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define P_ALL 0
1818
#define P_PID 1
1919
#define P_PGID 2
20+
#define P_PIDFD 3
2021

2122

2223
#endif /* _UAPI_LINUX_WAIT_H */

‎kernel/exit.c‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,23 @@ static long do_wait(struct wait_opts *wo)
15541554
return retval;
15551555
}
15561556

1557+
static struct pid *pidfd_get_pid(unsigned int fd)
1558+
{
1559+
struct fd f;
1560+
struct pid *pid;
1561+
1562+
f = fdget(fd);
1563+
if (!f.file)
1564+
return ERR_PTR(-EBADF);
1565+
1566+
pid = pidfd_pid(f.file);
1567+
if (!IS_ERR(pid))
1568+
get_pid(pid);
1569+
1570+
fdput(f);
1571+
return pid;
1572+
}
1573+
15571574
static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
15581575
int options, struct rusage *ru)
15591576
{
@@ -1576,19 +1593,32 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
15761593
type = PIDTYPE_PID;
15771594
if (upid <= 0)
15781595
return -EINVAL;
1596+
1597+
pid = find_get_pid(upid);
15791598
break;
15801599
case P_PGID:
15811600
type = PIDTYPE_PGID;
1582-
if (upid <= 0)
1601+
if (upid < 0)
1602+
return -EINVAL;
1603+
1604+
if (upid)
1605+
pid = find_get_pid(upid);
1606+
else
1607+
pid = get_task_pid(current, PIDTYPE_PGID);
1608+
break;
1609+
case P_PIDFD:
1610+
type = PIDTYPE_PID;
1611+
if (upid < 0)
15831612
return -EINVAL;
1613+
1614+
pid = pidfd_get_pid(upid);
1615+
if (IS_ERR(pid))
1616+
return PTR_ERR(pid);
15841617
break;
15851618
default:
15861619
return -EINVAL;
15871620
}
15881621

1589-
if (type < PIDTYPE_MAX)
1590-
pid = find_get_pid(upid);
1591-
15921622
wo.wo_type = type;
15931623
wo.wo_pid = pid;
15941624
wo.wo_flags = options;

‎kernel/fork.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,14 @@ static inline void rcu_copy_process(struct task_struct *p)
16901690
#endif /* #ifdef CONFIG_TASKS_RCU */
16911691
}
16921692

1693+
struct pid *pidfd_pid(const struct file *file)
1694+
{
1695+
if (file->f_op == &pidfd_fops)
1696+
return file->private_data;
1697+
1698+
return ERR_PTR(-EBADF);
1699+
}
1700+
16931701
static int pidfd_release(struct inode *inode, struct file *file)
16941702
{
16951703
struct pid *pid = file->private_data;

‎kernel/signal.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,8 +3678,11 @@ static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, siginfo_t *info)
36783678

36793679
static struct pid *pidfd_to_pid(const struct file *file)
36803680
{
3681-
if (file->f_op == &pidfd_fops)
3682-
return file->private_data;
3681+
struct pid *pid;
3682+
3683+
pid = pidfd_pid(file);
3684+
if (!IS_ERR(pid))
3685+
return pid;
36833686

36843687
return tgid_pidfd_to_pid(file);
36853688
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pidfd_open_test
2+
pidfd_poll_test
23
pidfd_test
4+
pidfd_wait
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
CFLAGS += -g -I../../../../usr/include/ -lpthread
33

4-
TEST_GEN_PROGS := pidfd_test pidfd_open_test
4+
TEST_GEN_PROGS := pidfd_test pidfd_open_test pidfd_poll_test pidfd_wait
55

66
include ../lib.mk
77

‎tools/testing/selftests/pidfd/pidfd.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616

1717
#include "../kselftest.h"
1818

19+
#ifndef P_PIDFD
20+
#define P_PIDFD 3
21+
#endif
22+
23+
#ifndef CLONE_PIDFD
24+
#define CLONE_PIDFD 0x00001000
25+
#endif
26+
27+
#ifndef __NR_pidfd_open
28+
#define __NR_pidfd_open -1
29+
#endif
30+
31+
#ifndef __NR_pidfd_send_signal
32+
#define __NR_pidfd_send_signal -1
33+
#endif
34+
35+
#ifndef __NR_clone3
36+
#define __NR_clone3 -1
37+
#endif
38+
1939
/*
2040
* The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
2141
* That means, when it wraps around any pid < 300 will be skipped.
@@ -53,5 +73,15 @@ int wait_for_pid(pid_t pid)
5373
return WEXITSTATUS(status);
5474
}
5575

76+
static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
77+
{
78+
return syscall(__NR_pidfd_open, pid, flags);
79+
}
80+
81+
static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
82+
unsigned int flags)
83+
{
84+
return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
85+
}
5686

5787
#endif /* __PIDFD_H */

‎tools/testing/selftests/pidfd/pidfd_open_test.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include "pidfd.h"
2323
#include "../kselftest.h"
2424

25-
static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
26-
{
27-
return syscall(__NR_pidfd_open, pid, flags);
28-
}
29-
3025
static int safe_int(const char *numstr, int *converted)
3126
{
3227
char *err = NULL;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#define _GNU_SOURCE
4+
#include <errno.h>
5+
#include <linux/types.h>
6+
#include <linux/wait.h>
7+
#include <poll.h>
8+
#include <signal.h>
9+
#include <stdbool.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <syscall.h>
14+
#include <sys/wait.h>
15+
#include <unistd.h>
16+
17+
#include "pidfd.h"
18+
#include "../kselftest.h"
19+
20+
static bool timeout;
21+
22+
static void handle_alarm(int sig)
23+
{
24+
timeout = true;
25+
}
26+
27+
int main(int argc, char **argv)
28+
{
29+
struct pollfd fds;
30+
int iter, nevents;
31+
int nr_iterations = 10000;
32+
33+
fds.events = POLLIN;
34+
35+
if (argc > 2)
36+
ksft_exit_fail_msg("Unexpected command line argument\n");
37+
38+
if (argc == 2) {
39+
nr_iterations = atoi(argv[1]);
40+
if (nr_iterations <= 0)
41+
ksft_exit_fail_msg("invalid input parameter %s\n",
42+
argv[1]);
43+
}
44+
45+
ksft_print_msg("running pidfd poll test for %d iterations\n",
46+
nr_iterations);
47+
48+
for (iter = 0; iter < nr_iterations; iter++) {
49+
int pidfd;
50+
int child_pid = fork();
51+
52+
if (child_pid < 0) {
53+
if (errno == EAGAIN) {
54+
iter--;
55+
continue;
56+
}
57+
ksft_exit_fail_msg(
58+
"%s - failed to fork a child process\n",
59+
strerror(errno));
60+
}
61+
62+
if (child_pid == 0) {
63+
/* Child process just sleeps for a min and exits */
64+
sleep(60);
65+
exit(EXIT_SUCCESS);
66+
}
67+
68+
/* Parent kills the child and waits for its death */
69+
pidfd = sys_pidfd_open(child_pid, 0);
70+
if (pidfd < 0)
71+
ksft_exit_fail_msg("%s - pidfd_open failed\n",
72+
strerror(errno));
73+
74+
/* Setup 3 sec alarm - plenty of time */
75+
if (signal(SIGALRM, handle_alarm) == SIG_ERR)
76+
ksft_exit_fail_msg("%s - signal failed\n",
77+
strerror(errno));
78+
alarm(3);
79+
80+
/* Send SIGKILL to the child */
81+
if (sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0))
82+
ksft_exit_fail_msg("%s - pidfd_send_signal failed\n",
83+
strerror(errno));
84+
85+
/* Wait for the death notification */
86+
fds.fd = pidfd;
87+
nevents = poll(&fds, 1, -1);
88+
89+
/* Check for error conditions */
90+
if (nevents < 0)
91+
ksft_exit_fail_msg("%s - poll failed\n",
92+
strerror(errno));
93+
94+
if (nevents != 1)
95+
ksft_exit_fail_msg("unexpected poll result: %d\n",
96+
nevents);
97+
98+
if (!(fds.revents & POLLIN))
99+
ksft_exit_fail_msg(
100+
"unexpected event type received: 0x%x\n",
101+
fds.revents);
102+
103+
if (timeout)
104+
ksft_exit_fail_msg(
105+
"death notification wait timeout\n");
106+
107+
close(pidfd);
108+
/* Wait for child to prevent zombies */
109+
if (waitpid(child_pid, NULL, 0) < 0)
110+
ksft_exit_fail_msg("%s - waitpid failed\n",
111+
strerror(errno));
112+
113+
}
114+
115+
ksft_test_result_pass("pidfd poll test: pass\n");
116+
return ksft_exit_pass();
117+
}

0 commit comments

Comments
 (0)