pidfd: waiting on processes through pidfds
Subsystems (5)
Christian Brauner
maintainercover pidfd: waiting on processes through pidfds
Hey everyone,
/* v1 */
This adds the ability to wait on processes using pidfds. This is one of the few missing pieces to make it possible to manage processes using only pidfds.
Please note the following major changes (More details can be found in the individual commit changelogs.):
- Add the new type P_PIDFD to waitid() instead of a new dedicated pidfd_wait() syscall.
This is the same approach we discussed a few months ago and still prefer over adding a dedicated syscall.
- Adapt the tests to the new type P_PIDFD for waitid().
- Remove struct waitid_info patch.
This will be sent out as a separate patch.
- Remove CLONE_WAIT_PID patch.
This will be sent out as a separate patch.
The core patch for waitid is pleasantly small. The largest change is caused by adding proper tests for waitid(P_PIDFD).
/* v0 */
Thanks!
Christian
Christian Brauner (2):
pidfd: add P_PIDFD to waitid()
pidfd: add pidfd_wait tests
include/linux/pid.h | 4 + include/uapi/linux/wait.h | 1 + kernel/exit.c | 25 ++- kernel/fork.c | 8 + kernel/signal.c | 7 +- tools/testing/selftests/pidfd/pidfd.h | 25 +++ tools/testing/selftests/pidfd/pidfd_test.c | 14 -- tools/testing/selftests/pidfd/pidfd_wait.c | 245 +++++++++++++++++++++ 8 files changed, 311 insertions(+), 18 deletions(-) create mode 100644 tools/testing/selftests/pidfd/pidfd_wait.c
Signature · 1 line
2.22.0
Christian Brauner
maintainerPATCH 1/2 pidfd: add P_PIDFD to waitid()
This adds the P_PIDFD type to waitid().
One of the last remaining bits for the pidfd api is to make it possible to wait on pidfds. With P_PIDFD added to waitid() the parts of userspace that want to use the pidfd api to exclusively manage processes can do so now.
One of the things this will unblock in the future is the ability to make it possible to retrieve the exit status via waitid(P_PIDFD) for non-parent processes if handed a _suitable_ pidfd that has this feature set. This is similar to what you can do on FreeBSD with kqueue(). It might even end up being possible to wait on a process as a non-parent if an appropriate property is enabled on the pidfd.
With P_PIDFD no scoping of the process identified by the pidfd is possible, i.e. it explicitly blocks things such as wait4(-1), wait4(0), waitid(P_ALL), waitid(P_PGID) etc. It only allows for semantics equivalent to wait4(pid), waitid(P_PID). Users that need scoping should rely on pid-based wait*() syscalls for now.
---
v1:
- Linus Torvalds <[email protected]>:
- use flag as discussed before, not a dedicated pidfd_wait() syscall
- Oleg Nesterov <[email protected]>:
- use flag as discussed before, not a dedicated pidfd_wait() syscall
--- include/linux/pid.h | 4 ++++ include/uapi/linux/wait.h | 1 + kernel/exit.c | 25 +++++++++++++++++++++++-- kernel/fork.c | 8 ++++++++ kernel/signal.c | 7 +++++-- 5 files changed, 41 insertions(+), 4 deletions(-)
include/linux/pid.h +4 -0
@@ -72,6 +72,10 @@ extern struct pid init_struct_pid;extern const struct file_operations pidfd_fops; +struct file ; + +extern struct pid *pidfd_pid (const struct file *file); +static inline struct pid *get_pid (struct pid *pid) {if (pid)
include/uapi/linux/wait.h +1 -0
@@ -17,6 +17,7 @@#define P_ALL 0#define P_PID 1#define P_PGID 2 +#define P_PIDFD 3#endif /* _UAPI_LINUX_WAIT_H */
kernel/exit.c +23 -2
@@ -1555,6 +1555,7 @@ static long do_wait(struct wait_opts *wo)static long kernel_waitid (int which,pid_t upid,struct waitid_info *infop,int options,struct rusage *ru) { +struct fd f;struct wait_opts wo;struct pid *pid =NULL ;enum pid_type type;
@@ -1580,11 +1581,27 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,if (upid <=0 )return -EINVAL ;break ; +case P_PIDFD : + type =PIDTYPE_PID ; +if (upid <0 ) +return -EINVAL ; + + f =fdget (upid); +if (!f.file) +return -EBADF ; + + pid =pidfd_pid (f.file); +if (IS_ERR (pid)) { +fdput (f); +return PTR_ERR (pid); + } + +break ;default :return -EINVAL ; } -if (type <PIDTYPE_MAX ) +if (type <PIDTYPE_MAX && !pid) pid =find_get_pid (upid); wo.wo_type = type;
@@ -1594,7 +1611,11 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, wo.wo_rusage = ru; ret =do_wait (&wo); -put_pid (pid); +if (which ==P_PIDFD ) +fdput (f); +else +put_pid (pid); +return ret; }
kernel/fork.c +8 -0
@@ -1690,6 +1690,14 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +struct pid *pidfd_pid (const struct file *file) +{ +if (file->f_op == &pidfd_fops) +return file->private_data; + +return ERR_PTR (-EBADF ); +} +static int pidfd_release (struct inode *inode,struct file *file) {struct pid *pid = file->private_data;
kernel/signal.c +5 -2
@@ -3672,8 +3672,11 @@ static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, siginfo_t *info)static struct pid *pidfd_to_pid (const struct file *file) { -if (file->f_op == &pidfd_fops) -return file->private_data; +struct pid *pid; + + pid =pidfd_pid (file); +if (!IS_ERR (pid)) +return pid;return tgid_pidfd_to_pid (file); }
Signature · 1 line
2.22.0
Re: [PATCH v1 1/2] pidfd: add P_PIDFD to waitid()
kernel/exit.c static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, · line 1605
Quoted 4 lines · Christian Brauner
kernel/exit.c +1
-1-if (type <PIDTYPE_MAX ) +if (type <PIDTYPE_MAX && !pid) pid =find_get_pid (upid);
So now we have four cases in the switch statement, and two of them do *not* want that "find_get_pid()" call.
Honestly, let's just move that whole thing into the switch statement for the two cases that do want it. Particulartly since I think the "upid == 0" case for P_PGID will prefer it that way anyway.
Let's not check 'type' in two different places in two completely different ways.
Ok?
Linus
Christian Brauner
maintainerRe: [PATCH v1 1/2] pidfd: add P_PIDFD to waitid()
Quoted 14 lines · Linus Torvalds
kernel/exit.c static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, · line 1605
Quoted 4 lines · Christian Brauner
kernel/exit.c +1
-1-if (type <PIDTYPE_MAX ) +if (type <PIDTYPE_MAX && !pid) pid =find_get_pid (upid);So now we have four cases in the switch statement, and two of them do *not* want that "
find_get_pid()" call.Honestly, let's just move that whole thing into the switch statement for the two cases that do want it. Particulartly since I think the "upid == 0" case for
P_PGIDwill prefer it that way anyway.
Quoted 6 lines
Let's not check 'type' in two different places in two completely different ways.
Ok?
Linus
Ok, will resend with that changed.
Christian
Christian Brauner
maintainerPATCH 2/2 pidfd: add pidfd_wait tests
In reply to Christian BraunerAdd tests for pidfd_wait() and CLONE_WAIT_PID:
- test that waitid(P_PIDFD) can wait on a pidfd
- test that waitid(P_PIDFD) can wait on a pidfd and return siginfo_t
- test that waitid(P_PIDFD) works with WEXITED
- test that waitid(P_PIDFD) works with WSTOPPED
- test that waitid(P_PIDFD) works with WUNTRACED
- test that waitid(P_PIDFD) works with WCONTINUED
- test that waitid(P_PIDFD) works with WNOWAIT
- test that waitid(P_PIDFD)works with WNOHANG
---
v1:
- Christian Brauner <[email protected]>:
- adapt tests to new P_PIDFD concept
--- tools/testing/selftests/pidfd/pidfd.h | 25 +++ tools/testing/selftests/pidfd/pidfd_test.c | 14 -- tools/testing/selftests/pidfd/pidfd_wait.c | 245 +++++++++++++++++++++ 3 files changed, 270 insertions(+), 14 deletions(-) create mode 100644 tools/testing/selftests/pidfd/pidfd_wait.c
tools/testing/selftests/pidfd/pidfd.h +25 -0
@@ -16,6 +16,26 @@#include "../kselftest.h" +#ifndef P_PIDFD +#define P_PIDFD 3 +#endif + +#ifndef CLONE_PIDFD +#define CLONE_PIDFD 0x00001000 +#endif + +#ifndef __NR_pidfd_open +#define __NR_pidfd_open -1 +#endif + +#ifndef __NR_pidfd_send_signal +#define __NR_pidfd_send_signal -1 +#endif + +#ifndef __NR_clone3 +#define __NR_clone3 -1 +#endif +/* * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c * That means, when it wraps around any pid < 300 will be skipped.
@@ -53,5 +73,10 @@ int wait_for_pid(pid_t pid) return WEXITSTATUS(status); } +static inline int sys_pidfd_send_signal (int pidfd,int sig,siginfo_t *info, +unsigned int flags) +{ +return syscall (__NR_pidfd_send_signal, pidfd, sig, info, flags); +}#endif /* __PIDFD_H */
tools/testing/selftests/pidfd/pidfd_test.c +0 -14
@@ -21,20 +21,12 @@#include "pidfd.h" #include "../kselftest.h" -#ifndef __NR_pidfd_send_signal -#define __NR_pidfd_send_signal -1 -#endif -#define str (s) _str(s)#define _str (s) #s#define CHILD_THREAD_MIN_WAIT 3/* seconds */ #define MAX_EVENTS 5 -#ifndef CLONE_PIDFD -#define CLONE_PIDFD 0x00001000 -#endif -static pid_t pidfd_clone (int flags,int *pidfd,int (*fn)(void *)) {size_t stack_size =1024 ;
@@ -47,12 +39,6 @@ static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))#endif } -static inline int sys_pidfd_send_signal (int pidfd,int sig,siginfo_t *info, -unsigned int flags) -{ -return syscall (__NR_pidfd_send_signal, pidfd, sig, info, flags); -} -static int signal_received;static void set_signal_received_on_sigusr1 (int sig)
tools/testing/selftests/pidfd/pidfd_wait.c new file +245 -0
245 lines not on this page · raw message
Signature · 1 line
2.22.0
Hi Christian,
just fyi, can't read this patch till Monday
Quoted 46 lines · Christian Brauner
Hey everyone,
/* v1 */
This adds the ability to wait on processes using pidfds. This is one of the few missing pieces to make it possible to manage processes using only pidfds.Please note the following major changes (More details can be found in the individual commit changelogs.):
- Add the new typeP_PIDFDtowaitid()instead of a new dedicatedpidfd_wait()syscall.
This is the same approach we discussed a few months ago and still prefer over adding a dedicated syscall.
- Adapt the tests to the new typeP_PIDFDforwaitid().
- Remove structwaitid_infopatch.
This will be sent out as a separate patch.
- RemoveCLONE_WAIT_PIDpatch.
This will be sent out as a separate patch.The core patch for waitid is pleasantly small. The largest change is caused by adding proper tests for
waitid(P_PIDFD)./* v0 */
Thanks!
ChristianChristian Brauner (2):
pidfd: addP_PIDFDtowaitid()
pidfd: addpidfd_waittestsinclude/linux/pid.h | 4 + include/uapi/linux/wait.h | 1 + kernel/exit.c | 25 ++- kernel/fork.c | 8 + kernel/signal.c | 7 +- tools/testing/selftests/pidfd/pidfd.h | 25 +++ tools/testing/selftests/pidfd/pidfd_test.c | 14 -- tools/testing/selftests/pidfd/pidfd_wait.c | 245 +++++++++++++++++++++ 8 files changed, 311 insertions(+), 18 deletions(-) create mode 100644 tools/testing/selftests/pidfd/pidfd_wait.cSignature · 1 line
2.22.0