pidfs: provide information after task has been reaped
Christian Brauner
maintainerv2 cover pidfs: provide information after task has been reaped
Hey,
Various tools need access to information about a process/task even after it has already been reaped. For example, systemd's journal logs and uses such information as the cgroup id and exit status to deal with processes that have been sent via SCM_PIDFD or SCM_PEERPIDFD. By the time the pidfd is received the process might have already been reaped.
This series aims to provide information by extending the PIDFD_GET_INFO ioctl to retrieve the exit code and cgroup id. There might be other stuff that we would want in the future.
Pidfd
polling allows waiting on either task exit or for a task to have been reaped. The contract for PIDFD_INFO_EXIT is simply that EPOLLHUP must be observed before exit information can be retrieved, i.e., exit information is only provided once the task has been reaped.
Note, that if a thread-group leader exits before other threads in the thread-group then exit information will only be available once the thread-group is empty. This aligns with wait() as well, where reaping of a thread-group leader that exited before the thread-group was empty is delayed until the thread-group is empty.
With PIDFD_INFO_EXIT autoreaping might actually become usable because it means a parent can ignore SIGCHLD or set SA_NOCLDWAIT and simply use pidfd polling and PIDFD_INFO_EXIT to get get status information for its children. The kernel will autocleanup right away instead of delaying.
---
Changes in v2:
- Call pidfs_exit() from release_task().
- Don't provide exit information once the task has exited but once the
task has been reaped. This makes for simpler semantics. Thus, call
pidfs_exit() from release_task().
- Link to v1: https://lore.kernel.org/r/20250228-work-pidfs-kill_on_last_close-v1-0-5bd7e6bb428e@kernel.org
---
Christian Brauner (15):
pidfs: switch to copy_struct_to_user()
pidfd: rely on automatic cleanup in __pidfd_prepare()
pidfs: move setting flags into pidfs_alloc_file()
pidfs: add inode allocation
pidfs: record exit code and cgroupid at exit
pidfs: allow to retrieve exit information selftests/pidfd: fix header inclusion pidfs/selftests: ensure correct headers for ioctl handling selftests/pidfd: move more defines to common header selftests/pidfd: add first PIDFD_INFO_EXIT selftest selftests/pidfd: add second PIDFD_INFO_EXIT selftest selftests/pidfd: add third PIDFD_INFO_EXIT selftest selftests/pidfd: add fourth PIDFD_INFO_EXIT selftest selftests/pidfd: add fifth PIDFD_INFO_EXIT selftest selftests/pidfd: add sixth PIDFD_INFO_EXIT selftest
fs/internal.h | 1 + fs/libfs.c | 4 +- fs/pidfs.c | 182 +++++++++-- include/linux/pidfs.h | 1 + include/uapi/linux/pidfd.h | 3 +- kernel/exit.c | 2 + kernel/fork.c | 15 +- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 +- tools/testing/selftests/pidfd/pidfd.h | 86 +++++ tools/testing/selftests/pidfd/pidfd_fdinfo_test.c | 1 + tools/testing/selftests/pidfd/pidfd_info_test.c | 372 ++++++++++++++++++++++ tools/testing/selftests/pidfd/pidfd_open_test.c | 26 -- tools/testing/selftests/pidfd/pidfd_setns_test.c | 45 --- 14 files changed, 636 insertions(+), 105 deletions(-)
---
base-commit: b1e809e7f64ad47dd232ff072d8ef59c1fe414c5
change-id: 20250227-work-pidfs-kill_on_last_close-a23ddf21db47
Christian Brauner
maintainerPATCH v2 1/15 pidfs: switch to copy_struct_to_user()
We have a helper that deals with all the required logic.
--- fs/pidfs.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
fs/pidfs.c +1 -4
@@ -276,10 +276,7 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long* userspace knows about will be copied. If userspace provides a new * struct, only the bits that the kernel knows about will be copied. */ -if (copy_to_user (uinfo, &kinfo,min (usize,sizeof (kinfo)))) -return -EFAULT ; - -return 0 ; +return copy_struct_to_user (uinfo, usize, &kinfo,sizeof (kinfo),NULL ); }static bool pidfs_ioctl_valid (unsigned int cmd)
Signature · 1 line
2.47.2
Re: [PATCH v2 01/15] pidfs: switch to copy_struct_to_user()
Quoted 14 lines · Christian Brauner
We have a helper that deals with all the required logic.
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)fs/pidfs.c +0
-0@@ -276,10 +276,7 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long* userspace knows about will be copied. If userspace provides a new
fs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 282
Quoted 10 lines
fs/pidfs.c +1
-4* struct, only the bits that the kernel knows about will be copied. */ -if (copy_to_user (uinfo, &kinfo,min (usize,sizeof (kinfo)))) -return -EFAULT ; - -return 0 ; +return copy_struct_to_user (uinfo, usize, &kinfo,sizeof (kinfo),NULL ); }static bool pidfs_ioctl_valid (unsigned int cmd)
Christian Brauner
maintainerPATCH v2 2/15 pidfd: rely on automatic cleanup in __pidfd_prepare()
In reply to Christian BraunerRely on scope-based cleanup for the allocated file descriptor.
--- kernel/fork.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
kernel/fork.c +4 -6
@@ -2032,25 +2032,23 @@ static inline void rcu_copy_process(struct task_struct *p)*/ static int __pidfd_prepare (struct pid *pid,unsigned int flags,struct file **ret) { -int pidfd;struct file *pidfd_file; - pidfd =get_unused_fd_flags (O_CLOEXEC ); +CLASS (get_unused_fd, pidfd)(O_CLOEXEC );if (pidfd <0 )return pidfd; pidfd_file =pidfs_alloc_file (pid, flags |O_RDWR ); -if (IS_ERR (pidfd_file)) { -put_unused_fd (pidfd); +if (IS_ERR (pidfd_file))return PTR_ERR (pidfd_file); - } +/* * anon_inode_getfile() ignores everything outside of the * O_ACCMODE | O_NONBLOCK mask, set PIDFD_THREAD manually. */ pidfd_file->f_flags |= (flags &PIDFD_THREAD ); *ret = pidfd_file; -return pidfd; +return take_fd (pidfd); }/**
Signature · 1 line
2.47.2
Re: [PATCH v2 02/15] pidfd: rely on automatic cleanup in __pidfd_prepare()
Quoted 32 lines · Christian Brauner
Rely on scope-based cleanup for the allocated file descriptor.
Signed-off-by Christian Brauner <[email protected]>--- kernel/fork.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)kernel/fork.c +3
-5@@ -2032,25 +2032,23 @@ static inline void rcu_copy_process(struct task_struct *p)*/ static int __pidfd_prepare (struct pid *pid,unsigned int flags,struct file **ret) { -int pidfd;struct file *pidfd_file; - pidfd =get_unused_fd_flags (O_CLOEXEC ); +CLASS (get_unused_fd, pidfd)(O_CLOEXEC );if (pidfd <0 )return pidfd; pidfd_file =pidfs_alloc_file (pid, flags |O_RDWR ); -if (IS_ERR (pidfd_file)) { -put_unused_fd (pidfd); +if (IS_ERR (pidfd_file))return PTR_ERR (pidfd_file); - } +/*
kernel/fork.c static inline void rcu_copy_process(struct task_struct *p) · line 2054
Quoted 10 lines
kernel/fork.c +1
-1* anon_inode_getfile() ignores everything outside of the * O_ACCMODE | O_NONBLOCK mask, set PIDFD_THREAD manually. */ pidfd_file->f_flags |= (flags &PIDFD_THREAD ); *ret = pidfd_file; -return pidfd; +return take_fd (pidfd); }/**
I'll Ack this, but I will say that I find the result to be less readable than what was there before.
Christian Brauner
maintainerPATCH v2 3/15 pidfs: move setting flags into pidfs_alloc_file()
In reply to Christian BraunerInstead od adding it into __pidfd_prepare() place it where the actual file allocation happens and update the outdated comment.
--- fs/pidfs.c | 4 ++++ kernel/fork.c | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-)
fs/pidfs.c +4 -0
@@ -696,6 +696,10 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)return ERR_PTR (ret); pidfd_file =dentry_open (&path, flags,current_cred ()); +/* Raise PIDFD_THREAD explicitly as do_dentry_open() strips it. */ +if (!IS_ERR (pidfd_file)) + pidfd_file->f_flags |= (flags &PIDFD_THREAD ); +path_put (&path);return pidfd_file; }
kernel/fork.c +0 -5
@@ -2042,11 +2042,6 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **reif (IS_ERR (pidfd_file))return PTR_ERR (pidfd_file); -/* -* anon_inode_getfile() ignores everything outside of the -* O_ACCMODE | O_NONBLOCK mask, set PIDFD_THREAD manually. -*/ - pidfd_file->f_flags |= (flags &PIDFD_THREAD ); *ret = pidfd_file;return take_fd (pidfd); }
Signature · 1 line
2.47.2
Re: [PATCH v2 03/15] pidfs: move setting flags into pidfs_alloc_file()
Quoted 31 lines · Christian Brauner
Instead od adding it into
__pidfd_prepare()place it where the actual file allocation happens and update the outdated comment.Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 4 ++++ kernel/fork.c | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-)fs/pidfs.c +4
-0@@ -696,6 +696,10 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)return ERR_PTR (ret); pidfd_file =dentry_open (&path, flags,current_cred ()); +/* Raise PIDFD_THREAD explicitly as do_dentry_open() strips it. */ +if (!IS_ERR (pidfd_file)) + pidfd_file->f_flags |= (flags &PIDFD_THREAD ); +path_put (&path);return pidfd_file; }kernel/fork.c +0
-0@@ -2042,11 +2042,6 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **reif (IS_ERR (pidfd_file))
kernel/fork.c static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re · line 2047
Quoted 10 lines
kernel/fork.c +0
-5return PTR_ERR (pidfd_file); -/* -* anon_inode_getfile() ignores everything outside of the -* O_ACCMODE | O_NONBLOCK mask, set PIDFD_THREAD manually. -*/ - pidfd_file->f_flags |= (flags &PIDFD_THREAD ); *ret = pidfd_file;return take_fd (pidfd); }
Christian Brauner
maintainerPATCH v2 4/15 pidfs: add inode allocation
In reply to Christian Brauner--- fs/pidfs.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
fs/pidfs.c +52 -0
@@ -24,6 +24,27 @@#include "internal.h" #include "mount.h" +static struct kmem_cache *pidfs_cachep__ro_after_init ; + +/* +* Stashes information that userspace needs to access even after the +* process has been reaped. +*/ +struct pidfs_exit_info { +__u64 cgroupid; +__u64 exit_code; +}; + +struct pidfs_inode { +struct pidfs_exit_info exit_info; +struct inode vfs_inode; +}; + +static inline struct pidfs_inode *pidfs_i (struct inode *inode) +{ + return container_of(inode, struct pidfs_inode, vfs_inode); +} +static struct rb_root pidfs_ino_tree =RB_ROOT ;#if BITS_PER_LONG ==32
@@ -492,9 +513,29 @@ static void pidfs_evict_inode(struct inode *inode)put_pid (pid); } +static struct inode *pidfs_alloc_inode (struct super_block *sb) +{ +struct pidfs_inode *pi; + + pi =alloc_inode_sb (sb, pidfs_cachep,GFP_KERNEL ); +if (!pi) +return NULL ; + +memset (&pi->exit_info,0 ,sizeof (pi->exit_info)); + +return &pi->vfs_inode; +} + +static void pidfs_free_inode (struct inode *inode) +{ +kmem_cache_free (pidfs_cachep,pidfs_i (inode)); +} +static const struct super_operations pidfs_sops = { + .alloc_inode = pidfs_alloc_inode, .drop_inode = generic_delete_inode, .evict_inode = pidfs_evict_inode, + .free_inode = pidfs_free_inode, .statfs = simple_statfs, };
@@ -704,8 +745,19 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)return pidfd_file; } +static void pidfs_inode_init_once (void *data) +{ +struct pidfs_inode *pi = data; + +inode_init_once (&pi->vfs_inode); +} +void __init pidfs_init (void ) { + pidfs_cachep =kmem_cache_create ("pidfs_cache" ,sizeof (struct pidfs_inode ),0 , + (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT | +SLAB_ACCOUNT |SLAB_PANIC ), + pidfs_inode_init_once); pidfs_mnt =kern_mount (&pidfs_type);if (IS_ERR (pidfs_mnt))panic ("Failed to mount pidfs pseudo filesystem" );
Signature · 1 line
2.47.2
Re: [PATCH v2 04/15] pidfs: add inode allocation
Quoted 12 lines · Christian Brauner
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)fs/pidfs.c +0
-0@@ -24,6 +24,27 @@#include "internal.h" #include "mount.h"
fs/pidfs.c line 35
Quoted 10 lines
fs/pidfs.c +9
-0+static struct kmem_cache *pidfs_cachep__ro_after_init ; + +/* +* Stashes information that userspace needs to access even after the +* process has been reaped. +*/ +struct pidfs_exit_info { +__u64 cgroupid; +__u64 exit_code;
This is changed to __s32 in the next patch. Might as well do that here.
Quoted 56 lines
fs/pidfs.c +38
-0+}; + +struct pidfs_inode { +struct pidfs_exit_info exit_info; +struct inode vfs_inode; +}; + +static inline struct pidfs_inode *pidfs_i (struct inode *inode) +{ + return container_of(inode, struct pidfs_inode, vfs_inode); +} +static struct rb_root pidfs_ino_tree =RB_ROOT ;#if BITS_PER_LONG ==32 @@ -492,9 +513,29 @@ static void pidfs_evict_inode(struct inode *inode)put_pid (pid); } +static struct inode *pidfs_alloc_inode (struct super_block *sb) +{ +struct pidfs_inode *pi; + + pi =alloc_inode_sb (sb, pidfs_cachep,GFP_KERNEL ); +if (!pi) +return NULL ; + +memset (&pi->exit_info,0 ,sizeof (pi->exit_info)); + +return &pi->vfs_inode; +} + +static void pidfs_free_inode (struct inode *inode) +{ +kmem_cache_free (pidfs_cachep,pidfs_i (inode)); +} +static const struct super_operations pidfs_sops = { + .alloc_inode = pidfs_alloc_inode, .drop_inode = generic_delete_inode, .evict_inode = pidfs_evict_inode, + .free_inode = pidfs_free_inode, .statfs = simple_statfs, };@@ -704,8 +745,19 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)return pidfd_file; } +static void pidfs_inode_init_once (void *data) +{ +struct pidfs_inode *pi = data; + +inode_init_once (&pi->vfs_inode); +}
fs/pidfs.c struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags) · line 763
Quoted 10 lines
fs/pidfs.c +5
-0+void __init pidfs_init (void ) { + pidfs_cachep =kmem_cache_create ("pidfs_cache" ,sizeof (struct pidfs_inode ),0 , + (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT | +SLAB_ACCOUNT |SLAB_PANIC ), + pidfs_inode_init_once); pidfs_mnt =kern_mount (&pidfs_type);if (IS_ERR (pidfs_mnt))panic ("Failed to mount pidfs pseudo filesystem" );
Christian Brauner
maintainerPATCH v2 5/15 pidfs: record exit code and cgroupid at exit
In reply to Christian BraunerRecord the exit code and cgroupid in do_exit() and stash in struct pidfs_exit_info so it can be retrieved even after the task has been reaped.
--- fs/internal.h | 1 + fs/libfs.c | 4 ++-- fs/pidfs.c | 41 ++++++++++++++++++++++++++++++++++++++++- include/linux/pidfs.h | 1 + kernel/exit.c | 2 ++ 5 files changed, 46 insertions(+), 3 deletions(-)
fs/internal.h +1 -0
@@ -325,6 +325,7 @@ struct stashed_operations {int path_from_stashed (struct dentry **stashed,struct vfsmount *mnt,void *data,struct path *path);void stashed_dentry_prune (struct dentry *dentry); +struct dentry *stashed_dentry_get (struct dentry **stashed);/** * path_mounted - check whether path is mounted * @path: path to check
fs/libfs.c +2 -2
@@ -2113,7 +2113,7 @@ struct timespec64 simple_inode_init_ts(struct inode *inode) }EXPORT_SYMBOL (simple_inode_init_ts); -static inline struct dentry *get_stashed_dentry (struct dentry **stashed) +struct dentry *stashed_dentry_get (struct dentry **stashed) {struct dentry *dentry;
@@ -2215,7 +2215,7 @@ int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info;/* See if dentry can be reused. */ - path->dentry =get_stashed_dentry (stashed); + path->dentry =stashed_dentry_get (stashed);if (path->dentry) { sops->put_data (data); goto out_path;
fs/pidfs.c +40 -1
53 lines not on this page · raw message
include/linux/pidfs.h +1 -0
@@ -6,6 +6,7 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags);void __init pidfs_init (void );void pidfs_add_pid (struct pid *pid);void pidfs_remove_pid (struct pid *pid); +void pidfs_exit (struct task_struct *tsk);extern const struct dentry_operations pidfs_dentry_operations;#endif /* _LINUX_PID_FS_H */
kernel/exit.c +2 -0
@@ -69,6 +69,7 @@#include <linux/sysfs.h> #include <linux/user_events.h> #include <linux/uaccess.h> +#include <linux/pidfs.h> #include <uapi/linux/wait.h>
@@ -254,6 +255,7 @@ void release_task(struct task_struct *p)write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); +pidfs_exit (p);__exit_signal (p);/*
Signature · 1 line
2.47.2
Re: [PATCH v2 05/15] pidfs: record exit code and cgroupid at exit
Quoted 52 lines · Christian Brauner
Record the exit code and cgroupid in
do_exit()and stash in structpidfs_exit_infoso it can be retrieved even after the task has been reaped.Signed-off-by Christian Brauner <[email protected]>--- fs/internal.h | 1 + fs/libfs.c | 4 ++-- fs/pidfs.c | 41 ++++++++++++++++++++++++++++++++++++++++- include/linux/pidfs.h | 1 + kernel/exit.c | 2 ++ 5 files changed, 46 insertions(+), 3 deletions(-)fs/internal.h +1
-0@@ -325,6 +325,7 @@ struct stashed_operations {int path_from_stashed (struct dentry **stashed,struct vfsmount *mnt,void *data,struct path *path);void stashed_dentry_prune (struct dentry *dentry); +struct dentry *stashed_dentry_get (struct dentry **stashed);/** * path_mounted - check whether path is mounted * @path: path to check fs/libfs.c +2
-2@@ -2113,7 +2113,7 @@ struct timespec64 simple_inode_init_ts(struct inode *inode) }EXPORT_SYMBOL (simple_inode_init_ts); -static inline struct dentry *get_stashed_dentry (struct dentry **stashed) +struct dentry *stashed_dentry_get (struct dentry **stashed) {struct dentry *dentry;@@ -2215,7 +2215,7 @@ int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info;/* See if dentry can be reused. */ - path->dentry =get_stashed_dentry (stashed); + path->dentry =stashed_dentry_get (stashed);if (path->dentry) { sops->put_data (data); goto out_path;fs/pidfs.c +0
-0
fs/pidfs.c static struct kmem_cache *pidfs_cachep __ro_after_init; · line 35
Quoted 5 lines
fs/pidfs.c +1
-1@@ -32,7 +32,7 @@ static struct kmem_cache *pidfs_cachep __ro_after_init;*/ struct pidfs_exit_info {__u64 cgroupid; -__u64 exit_code; +__s32 exit_code;
^^^
The above delta should be folded into the previous patch.
Quoted 14 lines
fs/pidfs.c +7
-0};struct pidfs_inode {@@ -458,6 +458,45 @@ struct pid *pidfd_pid(const struct file *file)return file_inode (file)->i_private; } +/* +* We're called from release_task(). We know there's at least one +* reference to struct pid being held that won't be released until the +* task has been reaped which cannot happen until we're out of +* release_task(). +* +* If this struct pid is refered to by a pidfd then stashed_dentry_get()
fs/pidfs.c struct pid *pidfd_pid(const struct file *file) · line 477
Quoted 10 lines
fs/pidfs.c +10
-0+* will return the dentry and inode for that struct pid. Since we've +* taken a reference on it there's now an additional reference from the +* exit path on it. Which is fine. We're going to put it again in a +* second and we know that the pid is kept alive anyway. +* +* Worst case is that we've filled in the info and immediately free the +* dentry and inode afterwards since the pidfd has been closed. Since +* pidfs_exit() currently is placed after exit_task_work() we know that +* it cannot be us aka the exiting task holding a pidfd to ourselves. +*/
That is a subtle interaction. Thanks for the comment!
Quoted 51 lines
fs/pidfs.c +22
-0+void pidfs_exit (struct task_struct *tsk) +{ +struct dentry *dentry; + + dentry =stashed_dentry_get (&task_pid (tsk)->stashed); +if (dentry) { +struct inode *inode =d_inode (dentry); +struct pidfs_exit_info *exit_info = &pidfs_i (inode)->exit_info; +#ifdef CONFIG_CGROUPS +struct cgroup *cgrp; + +rcu_read_lock (); + cgrp =task_dfl_cgroup (tsk); + exit_info->cgroupid =cgroup_id (cgrp); +rcu_read_unlock (); +#endif + exit_info->exit_code = tsk->exit_code; + +dput (dentry); + } +} +static struct vfsmount *pidfs_mnt__ro_after_init ;/* include/linux/pidfs.h +1
-0@@ -6,6 +6,7 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags);void __init pidfs_init (void );void pidfs_add_pid (struct pid *pid);void pidfs_remove_pid (struct pid *pid); +void pidfs_exit (struct task_struct *tsk);extern const struct dentry_operations pidfs_dentry_operations;#endif /* _LINUX_PID_FS_H */ kernel/exit.c +1
-0@@ -69,6 +69,7 @@#include <linux/sysfs.h> #include <linux/user_events.h> #include <linux/uaccess.h> +#include <linux/pidfs.h> #include <uapi/linux/wait.h>
kernel/exit.c void release_task(struct task_struct *p) · line 261
Quoted 7 lines
kernel/exit.c +1
-0@@ -254,6 +255,7 @@ void release_task(struct task_struct *p)write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); +pidfs_exit (p);__exit_signal (p);/*
Re: [PATCH v2 05/15] pidfs: record exit code and cgroupid at exit
In reply to Christian BraunerI will read this series later, but I see nothing wrong after a quick glance.
Minor nit below...
Quoted 2 lines · Christian Brauner
Record the exit code and cgroupid in
do_exit()
^^^^^^^^^^^^
this is no longer true. In release_task().
kernel/exit.c void release_task(struct task_struct *p) · line 259
Quoted 6 lines
kernel/exit.c +1
-0@@ -254,6 +255,7 @@ void release_task(struct task_struct *p)write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); +pidfs_exit (p);__exit_signal (p);
And the next patch rightly moves pidfs_exit() up outside of tasklist.
Why not call it before write_lock_irq(&tasklist_lock) from the very beginning?
Oleg.
Christian Brauner
maintainerRe: [PATCH v2 05/15] pidfs: record exit code and cgroupid at exit
Quoted 9 lines · Oleg Nesterov
I will read this series later, but I see nothing wrong after a quick glance.
Minor nit below...
Quoted 2 lines · Christian Brauner
Record the exit code and cgroupid in
do_exit()^^^^^^^^^^^^this is no longer true. In
release_task().
Yes, thanks for catching this!
Quoted 10 lines
kernel/exit.c void release_task(struct task_struct *p) · line 259
Quoted 7 lines
kernel/exit.c +1
-0@@ -254,6 +255,7 @@ void release_task(struct task_struct *p)write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); +pidfs_exit (p);__exit_signal (p);And the next patch rightly moves
pidfs_exit()up outside of tasklist.
Quoted 2 lines
Why not call it before
write_lock_irq(&tasklist_lock)from the very beginning?
Yes, sorry that was my intention. pidfs_exit() can sleep due to dput(). Thanks!
Christian Brauner
maintainerPATCH v2 6/15 pidfs: allow to retrieve exit information
In reply to Christian BraunerSome tools like systemd's jounral need to retrieve the exit and cgroup information after a process has already been reaped. This can e.g., happen when retrieving a pidfd via SCM_PIDFD or SCM_PEERPIDFD.
--- fs/pidfs.c | 88 ++++++++++++++++++++++++++++++++++++---------- include/uapi/linux/pidfd.h | 3 +- kernel/exit.c | 2 +- 3 files changed, 73 insertions(+), 20 deletions(-)
fs/pidfs.c +70 -18
@@ -36,7 +36,8 @@ struct pidfs_exit_info { };struct pidfs_inode { -struct pidfs_exit_info exit_info; +struct pidfs_exit_info __pei; +struct pidfs_exit_info *exit_info;struct inode vfs_inode; };
@@ -228,17 +229,28 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)return poll_flags; } -static long pidfd_info (struct task_struct *task,unsigned int cmd,unsigned long arg) +static inline bool current_in_pidns (struct pid *pid) +{ +const struct pid_namespace *ns =task_active_pid_ns (current); + +if (ns->level <= pid->level) +return pid->numbers[ns->level].ns == ns; + +return false; +} + +static long pidfd_info (struct file *file,unsigned int cmd,unsigned long arg) {struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg; +struct pid *pid =pidfd_pid (file);size_t usize =_IOC_SIZE (cmd);struct pidfd_info kinfo = {}; +struct pidfs_exit_info *exit_info; +struct inode *inode =file_inode (file);struct user_namespace *user_ns; +struct task_struct *task;const struct cred *c;__u64 mask; -#ifdef CONFIG_CGROUPS -struct cgroup *cgrp; -#endif if (!uinfo)return -EINVAL ;
@@ -248,6 +260,37 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (copy_from_user (&mask, &uinfo->mask,sizeof (mask)))return -EFAULT ; + task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ; + } + +if (mask &PIDFD_INFO_EXIT ) { + exit_info =READ_ONCE (pidfs_i (inode)->exit_info); +if (exit_info) { +#ifdef CONFIG_CGROUPS + kinfo.cgroupid = exit_info->cgroupid; + kinfo.mask |=PIDFD_INFO_EXIT |PIDFD_INFO_CGROUPID ; +#endif + kinfo.exit_code = exit_info->exit_code; + } + } + +/* +* If the task has already been reaped only exit information +* can be provided. It's entirely possible that the task has +* already been reaped but we managed to grab a reference to it +* before that. So a full set of information about @task doesn't +* mean it hasn't been waited upon. Similarly, a full set of +* information doesn't mean that the task hasn't already exited. +*/ +if (!task) + goto copy_out; + c =get_task_cred (task);if (!c)return -ESRCH ;
@@ -267,11 +310,15 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longput_cred (c);#ifdef CONFIG_CGROUPS -rcu_read_lock (); - cgrp =task_dfl_cgroup (task); - kinfo.cgroupid =cgroup_id (cgrp); - kinfo.mask |=PIDFD_INFO_CGROUPID ; -rcu_read_unlock (); +if (!kinfo.cgroupid) { +struct cgroup *cgrp; + +rcu_read_lock (); + cgrp =task_dfl_cgroup (task); + kinfo.cgroupid =cgroup_id (cgrp); + kinfo.mask |=PIDFD_INFO_CGROUPID ; +rcu_read_unlock (); + }#endif /*
@@ -291,6 +338,7 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (kinfo.pid ==0 || kinfo.tgid ==0 || (kinfo.ppid ==0 && kinfo.pid !=1 ))return -ESRCH ; +copy_out:/* * If userspace and the kernel have the same struct size it can just * be copied. If userspace provides an older struct, only the bits that
@@ -325,7 +373,6 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct task_struct *task __free(put_task) = NULL; struct nsproxy *nsp __free(put_nsproxy) = NULL; -struct pid *pid =pidfd_pid (file);struct ns_common *ns_common =NULL ;struct pid_namespace *pid_ns;
@@ -340,13 +387,13 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return put_user (file_inode (file)->i_generation, argp); } - task =get_pid_task (pid,PIDTYPE_PID ); -if (!task) -return -ESRCH ; -/* Extensible IOCTL that does not open namespace FDs, take a shortcut */ if (_IOC_NR (cmd) ==_IOC_NR (PIDFD_GET_INFO )) -return pidfd_info (task, cmd, arg); +return pidfd_info (file, cmd, arg); + + task =get_pid_task (pidfd_pid (file),PIDTYPE_PID ); +if (!task) +return -ESRCH ;if (arg)return -EINVAL ;
@@ -479,10 +526,12 @@ void pidfs_exit(struct task_struct *tsk) {struct dentry *dentry; +might_sleep (); + dentry =stashed_dentry_get (&task_pid (tsk)->stashed);if (dentry) {struct inode *inode =d_inode (dentry); -struct pidfs_exit_info *exit_info = &pidfs_i (inode)->exit_info; +struct pidfs_exit_info *exit_info = &pidfs_i (inode)->__pei;#ifdef CONFIG_CGROUPS struct cgroup *cgrp;
@@ -493,6 +542,8 @@ void pidfs_exit(struct task_struct *tsk)#endif exit_info->exit_code = tsk->exit_code; +/* Ensure that PIDFD_GET_INFO sees either all or nothing. */ +smp_store_release (&pidfs_i (inode)->exit_info, &pidfs_i (inode)->__pei);dput (dentry); } }
@@ -560,7 +611,8 @@ static struct inode *pidfs_alloc_inode(struct super_block *sb)if (!pi)return NULL ; -memset (&pi->exit_info,0 ,sizeof (pi->exit_info)); +memset (&pi->__pei,0 ,sizeof (pi->__pei)); + pi->exit_info =NULL ;return &pi->vfs_inode; }
include/uapi/linux/pidfd.h +2 -1
@@ -20,6 +20,7 @@#define PIDFD_INFO_PID (1UL << 0)/* Always returned, even if not requested */ #define PIDFD_INFO_CREDS (1UL << 1)/* Always returned, even if not requested */ #define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ +#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */ #define PIDFD_INFO_SIZE_VER0 64/* sizeof first published struct */
@@ -86,7 +87,7 @@ struct pidfd_info {__u32 sgid;__u32 fsuid;__u32 fsgid; -__u32 spare0[1 ]; +__s32 exit_code; };#define PIDFS_IOCTL_MAGIC 0xFF
kernel/exit.c +1 -1
@@ -250,12 +250,12 @@ void release_task(struct task_struct *p)dec_rlimit_ucounts (task_ucounts (p),UCOUNT_RLIMIT_NPROC ,1 );rcu_read_unlock (); +pidfs_exit (p);cgroup_release (p);write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); -pidfs_exit (p);__exit_signal (p);/*
Signature · 1 line
2.47.2
Re: [PATCH v2 06/15] pidfs: allow to retrieve exit information
Quoted 211 lines · Christian Brauner
Some tools like systemd's jounral need to retrieve the exit and cgroup information after a process has already been reaped. This can e.g., happen when retrieving a pidfd via
SCM_PIDFDorSCM_PEERPIDFD.Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 88 ++++++++++++++++++++++++++++++++++++---------- include/uapi/linux/pidfd.h | 3 +- kernel/exit.c | 2 +- 3 files changed, 73 insertions(+), 20 deletions(-)fs/pidfs.c +70
-18@@ -36,7 +36,8 @@ struct pidfs_exit_info { };struct pidfs_inode { -struct pidfs_exit_info exit_info; +struct pidfs_exit_info __pei; +struct pidfs_exit_info *exit_info;struct inode vfs_inode; };@@ -228,17 +229,28 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)return poll_flags; } -static long pidfd_info (struct task_struct *task,unsigned int cmd,unsigned long arg) +static inline bool current_in_pidns (struct pid *pid) +{ +const struct pid_namespace *ns =task_active_pid_ns (current); + +if (ns->level <= pid->level) +return pid->numbers[ns->level].ns == ns; + +return false; +} + +static long pidfd_info (struct file *file,unsigned int cmd,unsigned long arg) {struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg; +struct pid *pid =pidfd_pid (file);size_t usize =_IOC_SIZE (cmd);struct pidfd_info kinfo = {}; +struct pidfs_exit_info *exit_info; +struct inode *inode =file_inode (file);struct user_namespace *user_ns; +struct task_struct *task;const struct cred *c;__u64 mask; -#ifdef CONFIG_CGROUPS -struct cgroup *cgrp; -#endif if (!uinfo)return -EINVAL ;@@ -248,6 +260,37 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (copy_from_user (&mask, &uinfo->mask,sizeof (mask)))return -EFAULT ; + task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ; + } + +if (mask &PIDFD_INFO_EXIT ) { + exit_info =READ_ONCE (pidfs_i (inode)->exit_info); +if (exit_info) { +#ifdef CONFIG_CGROUPS + kinfo.cgroupid = exit_info->cgroupid; + kinfo.mask |=PIDFD_INFO_EXIT |PIDFD_INFO_CGROUPID ; +#endif + kinfo.exit_code = exit_info->exit_code; + } + } + +/* +* If the task has already been reaped only exit information +* can be provided. It's entirely possible that the task has +* already been reaped but we managed to grab a reference to it +* before that. So a full set of information about @task doesn't +* mean it hasn't been waited upon. Similarly, a full set of +* information doesn't mean that the task hasn't already exited. +*/ +if (!task) + goto copy_out; + c =get_task_cred (task);if (!c)return -ESRCH ;@@ -267,11 +310,15 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longput_cred (c);#ifdef CONFIG_CGROUPS -rcu_read_lock (); - cgrp =task_dfl_cgroup (task); - kinfo.cgroupid =cgroup_id (cgrp); - kinfo.mask |=PIDFD_INFO_CGROUPID ; -rcu_read_unlock (); +if (!kinfo.cgroupid) { +struct cgroup *cgrp; + +rcu_read_lock (); + cgrp =task_dfl_cgroup (task); + kinfo.cgroupid =cgroup_id (cgrp); + kinfo.mask |=PIDFD_INFO_CGROUPID ; +rcu_read_unlock (); + }#endif /* @@ -291,6 +338,7 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (kinfo.pid ==0 || kinfo.tgid ==0 || (kinfo.ppid ==0 && kinfo.pid !=1 ))return -ESRCH ; +copy_out:/* * If userspace and the kernel have the same struct size it can just * be copied. If userspace provides an older struct, only the bits that @@ -325,7 +373,6 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct task_struct *task __free(put_task) = NULL; struct nsproxy *nsp __free(put_nsproxy) = NULL; -struct pid *pid =pidfd_pid (file);struct ns_common *ns_common =NULL ;struct pid_namespace *pid_ns;@@ -340,13 +387,13 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return put_user (file_inode (file)->i_generation, argp); } - task =get_pid_task (pid,PIDTYPE_PID ); -if (!task) -return -ESRCH ; -/* Extensible IOCTL that does not open namespace FDs, take a shortcut */ if (_IOC_NR (cmd) ==_IOC_NR (PIDFD_GET_INFO )) -return pidfd_info (task, cmd, arg); +return pidfd_info (file, cmd, arg); + + task =get_pid_task (pidfd_pid (file),PIDTYPE_PID ); +if (!task) +return -ESRCH ;if (arg)return -EINVAL ;@@ -479,10 +526,12 @@ void pidfs_exit(struct task_struct *tsk) {struct dentry *dentry; +might_sleep (); + dentry =stashed_dentry_get (&task_pid (tsk)->stashed);if (dentry) {struct inode *inode =d_inode (dentry); -struct pidfs_exit_info *exit_info = &pidfs_i (inode)->exit_info; +struct pidfs_exit_info *exit_info = &pidfs_i (inode)->__pei;#ifdef CONFIG_CGROUPS struct cgroup *cgrp;@@ -493,6 +542,8 @@ void pidfs_exit(struct task_struct *tsk)#endif exit_info->exit_code = tsk->exit_code; +/* Ensure that PIDFD_GET_INFO sees either all or nothing. */ +smp_store_release (&pidfs_i (inode)->exit_info, &pidfs_i (inode)->__pei);dput (dentry); } }@@ -560,7 +611,8 @@ static struct inode *pidfs_alloc_inode(struct super_block *sb)if (!pi)return NULL ; -memset (&pi->exit_info,0 ,sizeof (pi->exit_info)); +memset (&pi->__pei,0 ,sizeof (pi->__pei)); + pi->exit_info =NULL ;return &pi->vfs_inode; }include/uapi/linux/pidfd.h +2
-1@@ -20,6 +20,7 @@#define PIDFD_INFO_PID (1UL << 0)/* Always returned, even if not requested */ #define PIDFD_INFO_CREDS (1UL << 1)/* Always returned, even if not requested */ #define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ +#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */ #define PIDFD_INFO_SIZE_VER0 64/* sizeof first published struct */ @@ -86,7 +87,7 @@ struct pidfd_info {__u32 sgid;__u32 fsuid;__u32 fsgid; -__u32 spare0[1 ]; +__s32 exit_code; };#define PIDFS_IOCTL_MAGIC 0xFFkernel/exit.c +0
-0
kernel/exit.c void release_task(struct task_struct *p) · line 258
Quoted 10 lines
kernel/exit.c +1
-1@@ -250,12 +250,12 @@ void release_task(struct task_struct *p)dec_rlimit_ucounts (task_ucounts (p),UCOUNT_RLIMIT_NPROC ,1 );rcu_read_unlock (); +pidfs_exit (p);cgroup_release (p);write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); -pidfs_exit (p);
Why move this after you just added it?
kernel/exit.c void release_task(struct task_struct *p) · line 261
Quoted 4 lines
kernel/exit.c +0
-0__exit_signal (p);/*
Everything else looks fine though. Assuming you fix the nit above, you can add:
Christian Brauner
maintainerRe: [PATCH v2 06/15] pidfs: allow to retrieve exit information
Quoted 218 lines · Jeff Layton
Quoted 217 lines · Christian Brauner
Some tools like systemd's jounral need to retrieve the exit and cgroup information after a process has already been reaped. This can e.g., happen when retrieving a pidfd via
SCM_PIDFDorSCM_PEERPIDFD.Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 88 ++++++++++++++++++++++++++++++++++++---------- include/uapi/linux/pidfd.h | 3 +- kernel/exit.c | 2 +- 3 files changed, 73 insertions(+), 20 deletions(-)fs/pidfs.c +70
-18@@ -36,7 +36,8 @@ struct pidfs_exit_info { };struct pidfs_inode { -struct pidfs_exit_info exit_info; +struct pidfs_exit_info __pei; +struct pidfs_exit_info *exit_info;struct inode vfs_inode; };@@ -228,17 +229,28 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)return poll_flags; } -static long pidfd_info (struct task_struct *task,unsigned int cmd,unsigned long arg) +static inline bool current_in_pidns (struct pid *pid) +{ +const struct pid_namespace *ns =task_active_pid_ns (current); + +if (ns->level <= pid->level) +return pid->numbers[ns->level].ns == ns; + +return false; +} + +static long pidfd_info (struct file *file,unsigned int cmd,unsigned long arg) {struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg; +struct pid *pid =pidfd_pid (file);size_t usize =_IOC_SIZE (cmd);struct pidfd_info kinfo = {}; +struct pidfs_exit_info *exit_info; +struct inode *inode =file_inode (file);struct user_namespace *user_ns; +struct task_struct *task;const struct cred *c;__u64 mask; -#ifdef CONFIG_CGROUPS -struct cgroup *cgrp; -#endif if (!uinfo)return -EINVAL ;@@ -248,6 +260,37 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (copy_from_user (&mask, &uinfo->mask,sizeof (mask)))return -EFAULT ; + task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ; + } + +if (mask &PIDFD_INFO_EXIT ) { + exit_info =READ_ONCE (pidfs_i (inode)->exit_info); +if (exit_info) { +#ifdef CONFIG_CGROUPS + kinfo.cgroupid = exit_info->cgroupid; + kinfo.mask |=PIDFD_INFO_EXIT |PIDFD_INFO_CGROUPID ; +#endif + kinfo.exit_code = exit_info->exit_code; + } + } + +/* +* If the task has already been reaped only exit information +* can be provided. It's entirely possible that the task has +* already been reaped but we managed to grab a reference to it +* before that. So a full set of information about @task doesn't +* mean it hasn't been waited upon. Similarly, a full set of +* information doesn't mean that the task hasn't already exited. +*/ +if (!task) + goto copy_out; + c =get_task_cred (task);if (!c)return -ESRCH ;@@ -267,11 +310,15 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longput_cred (c);#ifdef CONFIG_CGROUPS -rcu_read_lock (); - cgrp =task_dfl_cgroup (task); - kinfo.cgroupid =cgroup_id (cgrp); - kinfo.mask |=PIDFD_INFO_CGROUPID ; -rcu_read_unlock (); +if (!kinfo.cgroupid) { +struct cgroup *cgrp; + +rcu_read_lock (); + cgrp =task_dfl_cgroup (task); + kinfo.cgroupid =cgroup_id (cgrp); + kinfo.mask |=PIDFD_INFO_CGROUPID ; +rcu_read_unlock (); + }#endif /* @@ -291,6 +338,7 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (kinfo.pid ==0 || kinfo.tgid ==0 || (kinfo.ppid ==0 && kinfo.pid !=1 ))return -ESRCH ; +copy_out:/* * If userspace and the kernel have the same struct size it can just * be copied. If userspace provides an older struct, only the bits that @@ -325,7 +373,6 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct task_struct *task __free(put_task) = NULL; struct nsproxy *nsp __free(put_nsproxy) = NULL; -struct pid *pid =pidfd_pid (file);struct ns_common *ns_common =NULL ;struct pid_namespace *pid_ns;@@ -340,13 +387,13 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)return put_user (file_inode (file)->i_generation, argp); } - task =get_pid_task (pid,PIDTYPE_PID ); -if (!task) -return -ESRCH ; -/* Extensible IOCTL that does not open namespace FDs, take a shortcut */ if (_IOC_NR (cmd) ==_IOC_NR (PIDFD_GET_INFO )) -return pidfd_info (task, cmd, arg); +return pidfd_info (file, cmd, arg); + + task =get_pid_task (pidfd_pid (file),PIDTYPE_PID ); +if (!task) +return -ESRCH ;if (arg)return -EINVAL ;@@ -479,10 +526,12 @@ void pidfs_exit(struct task_struct *tsk) {struct dentry *dentry; +might_sleep (); + dentry =stashed_dentry_get (&task_pid (tsk)->stashed);if (dentry) {struct inode *inode =d_inode (dentry); -struct pidfs_exit_info *exit_info = &pidfs_i (inode)->exit_info; +struct pidfs_exit_info *exit_info = &pidfs_i (inode)->__pei;#ifdef CONFIG_CGROUPS struct cgroup *cgrp;@@ -493,6 +542,8 @@ void pidfs_exit(struct task_struct *tsk)#endif exit_info->exit_code = tsk->exit_code; +/* Ensure that PIDFD_GET_INFO sees either all or nothing. */ +smp_store_release (&pidfs_i (inode)->exit_info, &pidfs_i (inode)->__pei);dput (dentry); } }@@ -560,7 +611,8 @@ static struct inode *pidfs_alloc_inode(struct super_block *sb)if (!pi)return NULL ; -memset (&pi->exit_info,0 ,sizeof (pi->exit_info)); +memset (&pi->__pei,0 ,sizeof (pi->__pei)); + pi->exit_info =NULL ;return &pi->vfs_inode; }include/uapi/linux/pidfd.h +2
-1@@ -20,6 +20,7 @@#define PIDFD_INFO_PID (1UL << 0)/* Always returned, even if not requested */ #define PIDFD_INFO_CREDS (1UL << 1)/* Always returned, even if not requested */ #define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ +#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */ #define PIDFD_INFO_SIZE_VER0 64/* sizeof first published struct */ @@ -86,7 +87,7 @@ struct pidfd_info {__u32 sgid;__u32 fsuid;__u32 fsgid; -__u32 spare0[1 ]; +__s32 exit_code; };#define PIDFS_IOCTL_MAGIC 0xFFkernel/exit.c +1
-0@@ -250,12 +250,12 @@ void release_task(struct task_struct *p)dec_rlimit_ucounts (task_ucounts (p),UCOUNT_RLIMIT_NPROC ,1 );rcu_read_unlock (); +pidfs_exit (p);cgroup_release (p);
Quoted 6 lines
kernel/exit.c void release_task(struct task_struct *p) · line 258
Quoted 4 lines
kernel/exit.c +0
-1write_lock_irq (&tasklist_lock);ptrace_release_task (p); thread_pid =get_pid (p->thread_pid); -pidfs_exit (p);Why move this after you just added it?
Ah sorry that should've been done in the previous patch. pidfs_exit() needs to be called outside of tasklist_lock as it can sleep due to dput().
Quoted 10 lines
kernel/exit.c void release_task(struct task_struct *p) · line 261
Quoted 5 lines
kernel/exit.c +0
-0__exit_signal (p);/* Everything else looks fine though. Assuming you fix the nit above, you can add:
Reviewed-by Jeff Layton <[email protected]>
Re: [PATCH v2 06/15] pidfs: allow to retrieve exit information
In reply to Christian BraunerQuoted 13 lines · Christian Brauner
fs/pidfs.c +8
-0@@ -248,6 +260,37 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (copy_from_user (&mask, &uinfo->mask,sizeof (mask)))return -EFAULT ; + task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ; + }
fs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 280
Quoted 10 lines
fs/pidfs.c +10
-0+ +if (mask &PIDFD_INFO_EXIT ) { + exit_info =READ_ONCE (pidfs_i (inode)->exit_info); +if (exit_info) { +#ifdef CONFIG_CGROUPS + kinfo.cgroupid = exit_info->cgroupid; + kinfo.mask |=PIDFD_INFO_EXIT |PIDFD_INFO_CGROUPID ; +#endif + kinfo.exit_code = exit_info->exit_code; + }
Confused... so, without CONFIG_CGROUPS pidfd_info() will never report PIDFD_INFO_EXIT in kinfo.mask ?
include/uapi/linux/pidfd.h line 23
Quoted 7 lines
include/uapi/linux/pidfd.h +1
-0@@ -20,6 +20,7 @@#define PIDFD_INFO_PID (1UL << 0)/* Always returned, even if not requested */ #define PIDFD_INFO_CREDS (1UL << 1)/* Always returned, even if not requested */ #define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ +#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The comment doesn't match the "if (mask & PIDFD_INFO_EXIT)" check above...
Oleg.
Christian Brauner
maintainerRe: [PATCH v2 06/15] pidfs: allow to retrieve exit information
Quoted 19 lines · Oleg Nesterov
Quoted 18 lines · Christian Brauner
fs/pidfs.c +13
-0@@ -248,6 +260,37 @@ static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned longif (copy_from_user (&mask, &uinfo->mask,sizeof (mask)))return -EFAULT ; + task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ; + } + +if (mask &PIDFD_INFO_EXIT ) { + exit_info =READ_ONCE (pidfs_i (inode)->exit_info); +if (exit_info) { +#ifdef CONFIG_CGROUPS
Quoted 8 lines
fs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 280
Quoted 5 lines
fs/pidfs.c +5
-0+ kinfo.cgroupid = exit_info->cgroupid; + kinfo.mask |=PIDFD_INFO_EXIT |PIDFD_INFO_CGROUPID ; +#endif + kinfo.exit_code = exit_info->exit_code; + }Confused... so, without
CONFIG_CGROUPSpidfd_info()will never reportPIDFD_INFO_EXITin kinfo.mask ?
Fixed.
Quoted 11 lines
include/uapi/linux/pidfd.h line 23
Quoted 8 lines
include/uapi/linux/pidfd.h +1
-0@@ -20,6 +20,7 @@#define PIDFD_INFO_PID (1UL << 0)/* Always returned, even if not requested */ #define PIDFD_INFO_CREDS (1UL << 1)/* Always returned, even if not requested */ #define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ +#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^The comment doesn't match the "if (mask &
PIDFD_INFO_EXIT)" check above...
Also fixed.
Re: [PATCH v2 06/15] pidfs: allow to retrieve exit information
In reply to Christian Braunerfs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 269
Quoted 8 lines · Christian Brauner
fs/pidfs.c +7
-0+ task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ;
Damn ;) could you explain the current_in_pidns() check to me ? I am puzzled...
Oleg.
Christian Brauner
maintainerRe: [PATCH v2 06/15] pidfs: allow to retrieve exit information
Quoted 12 lines · Oleg Nesterov
fs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 269
Quoted 8 lines · Christian Brauner
fs/pidfs.c +7
-0+ task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ;Damn ;) could you explain the
current_in_pidns()check to me ? I am puzzled...
So we currently restrict interactions with pidfd by pid namespace hierarchy. Meaning that we ensure that the pidfd is part of the caller's pid namespace hierarchy. So this check is similar to:
pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
{
struct upid *upid;
pid_t nr = 0;
if (pid && ns->level <= pid->level) {
upid = &pid->numbers[ns->level];
if (upid->ns == ns)
nr = upid->nr;
}
return nr;
}IOW, we verify that the caller's pid namespace can be found within the pid namespace hierarchy that the struct pid had a pid numbers allocated in.
Only that by the time we perform this check the pid numbers have already been freed so we can't use that function directly. But the pid namespace hierarchy is still alive as that won't be released until the pidfd has put the reference on struct @pid. So we can use current_in_pidns().
Is that understandable?
Re: [PATCH v2 06/15] pidfs: allow to retrieve exit information
Quoted 15 lines · Christian Brauner
Quoted 12 lines · Oleg Nesterov
fs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 269
Quoted 8 lines · Christian Brauner
fs/pidfs.c +7
-0+ task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ;Damn ;) could you explain the
current_in_pidns()check to me ? I am puzzled...
Quoted 3 lines
So we currently restrict interactions with pidfd by pid namespace hierarchy. Meaning that we ensure that the pidfd is part of the caller's pid namespace hierarchy.
Well this is clear... but sorry I still can't understand.
Why do we check current_in_pidns() only if get_pid_task(PIDTYPE_PID) returns NULL?
And, unless (quite possibly) I am totally confused, if task != NULL but current_in_pidns() would return false, then
kinfo.pid = task_pid_vnr(task);
below will set kinfo.pid = 0, and pidfd_info() will return -ESRCH anyway?
Quoted 15 lines
So this check is similar to:
pid_tpid_nr_ns(struct pid *pid, struct pid_namespace *ns){ struct upid *upid; pid_t nr = 0; if (pid && ns->level <= pid->level) { upid = &pid->numbers[ns->level]; if (upid->ns == ns) nr = upid->nr; } return nr; }
Quoted 2 lines
Only that by the time we perform this check the pid numbers have already been freed so we can't use that function directly.
Confused again... Yes, the [u]pid numbers can be already "freed" in that upid->nr can be already idr_remove()'ed, but
Quoted 3 lines
But the pid namespace
hierarchy is still alive as that won't be released until the pidfd has put the reference on struct @pid.
Yes, so I still don't undestand, sorry :/
IOW. Why not check current_in_pidns() at the start? and do task = get_pid_task() later, right before
if (!task) goto copy_out;
?
Oleg.
Christian Brauner
maintainerRe: [PATCH v2 06/15] pidfs: allow to retrieve exit information
Quoted 16 lines · Oleg Nesterov
Quoted 15 lines · Christian Brauner
Quoted 12 lines · Oleg Nesterov
fs/pidfs.c static long pidfd_info(struct task_struct *task, unsigned int cmd, unsigned long · line 269
Quoted 8 lines · Christian Brauner
fs/pidfs.c +7
-0+ task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { +if (!(mask &PIDFD_INFO_EXIT )) +return -ESRCH ; + +if (!current_in_pidns (pid)) +return -ESRCH ;Damn ;) could you explain the
current_in_pidns()check to me ? I am puzzled...
Quoted 5 lines
Quoted 3 lines
So we currently restrict interactions with pidfd by pid namespace hierarchy. Meaning that we ensure that the pidfd is part of the caller's pid namespace hierarchy.
Well this is clear... but sorry I still can't understand.
Ok, it sounded like you wanted me to explain what current_in_pidns() does not why it's placed where it is placed. :)
Quoted 3 lines
Why do we check
current_in_pidns()only ifget_pid_task(PIDTYPE_PID)returns NULL?
Because if task != NULL we already catch it kinfo.pid and since we can't skip the call to task_pid_vnr() it seemed redundant to do that check twice. But if we do it before get_task_pid() it makes more sense ofc.
Quoted 7 lines
And, unless (quite possibly) I am totally confused, if task != NULL but
current_in_pidns()would return false, thenkinfo.pid = task_pid_vnr(task);below will set kinfo.pid = 0, and
pidfd_info()will return-ESRCHanyway?
Yes.
Quoted 22 lines
Quoted 16 lines
So this check is similar to:
pid_tpid_nr_ns(struct pid *pid, struct pid_namespace *ns){ struct upid *upid; pid_t nr = 0; if (pid && ns->level <= pid->level) { upid = &pid->numbers[ns->level]; if (upid->ns == ns) nr = upid->nr; } return nr; }Quoted 2 lines
Only that by the time we perform this check the pid numbers have already been freed so we can't use that function directly.
Confused again... Yes, the [u]pid numbers can be already "freed" in that
upid->nrcan be already idr_remove()'ed, but
Quoted 8 lines
Quoted 3 lines
But the pid namespace
hierarchy is still alive as that won't be released until the pidfd has put the reference on struct @pid.Yes, so I still don't undestand, sorry :/
IOW. Why not check
current_in_pidns()at the start? and do task =get_pid_task()later, right before
Sure, that's a good suggestion.
Quoted 8 lines
if (!task) goto copy_out;?
Oleg.
Christian Brauner
maintainerPATCH v2 7/15 selftests/pidfd: fix header inclusion
In reply to Christian BraunerEnsure that necessary defines are present.
--- tools/testing/selftests/pidfd/pidfd_fdinfo_test.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/pidfd/pidfd_fdinfo_test.c +1 -0
@@ -13,6 +13,7 @@#include <syscall.h> #include <sys/wait.h> #include <sys/mman.h> +#include <sys/mount.h> #include "pidfd.h" #include "../kselftest.h"
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 8/15 pidfs/selftests: ensure correct headers for ioctl handling
In reply to Christian Brauner--- tools/testing/selftests/pidfd/pidfd_setns_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/pidfd/pidfd_setns_test.c +1 -1
@@ -16,7 +16,7 @@#include <unistd.h> #include <sys/socket.h> #include <sys/stat.h> -#include <linux/ioctl.h> +#include <sys/ioctl.h> #include "pidfd.h" #include "../kselftest_harness.h"
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 9/15 selftests/pidfd: move more defines to common header
In reply to Christian Brauner--- tools/testing/selftests/pidfd/pidfd.h | 78 ++++++++++++++++++++++++ tools/testing/selftests/pidfd/pidfd_open_test.c | 26 -------- tools/testing/selftests/pidfd/pidfd_setns_test.c | 45 -------------- 3 files changed, 78 insertions(+), 71 deletions(-)
tools/testing/selftests/pidfd/pidfd.h +78 -0
@@ -12,6 +12,7 @@#include <stdlib.h> #include <string.h> #include <syscall.h> +#include <sys/ioctl.h> #include <sys/types.h> #include <sys/wait.h>
@@ -66,6 +67,83 @@#define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP#endif +#ifndef PIDFS_IOCTL_MAGIC +#define PIDFS_IOCTL_MAGIC 0xFF +#endif + +#ifndef PIDFD_GET_CGROUP_NAMESPACE +#define PIDFD_GET_CGROUP_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 1) +#endif + +#ifndef PIDFD_GET_IPC_NAMESPACE +#define PIDFD_GET_IPC_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 2) +#endif + +#ifndef PIDFD_GET_MNT_NAMESPACE +#define PIDFD_GET_MNT_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 3) +#endif + +#ifndef PIDFD_GET_NET_NAMESPACE +#define PIDFD_GET_NET_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 4) +#endif + +#ifndef PIDFD_GET_PID_NAMESPACE +#define PIDFD_GET_PID_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 5) +#endif + +#ifndef PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE +#define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 6) +#endif + +#ifndef PIDFD_GET_TIME_NAMESPACE +#define PIDFD_GET_TIME_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 7) +#endif + +#ifndef PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE +#define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8) +#endif + +#ifndef PIDFD_GET_USER_NAMESPACE +#define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9) +#endif + +#ifndef PIDFD_GET_UTS_NAMESPACE +#define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10) +#endif + +#ifndef PIDFD_GET_INFO +#define PIDFD_GET_INFO _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info) +#endif + +#ifndef PIDFD_INFO_PID +#define PIDFD_INFO_PID (1UL << 0)/* Always returned, even if not requested */ +#endif + +#ifndef PIDFD_INFO_CREDS +#define PIDFD_INFO_CREDS (1UL << 1)/* Always returned, even if not requested */ +#endif + +#ifndef PIDFD_INFO_CGROUPID +#define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ +#endif + +struct pidfd_info { +__u64 mask; +__u64 cgroupid; +__u32 pid; +__u32 tgid; +__u32 ppid; +__u32 ruid; +__u32 rgid; +__u32 euid; +__u32 egid; +__u32 suid; +__u32 sgid; +__u32 fsuid; +__u32 fsgid; +__u32 spare0[1 ]; +}; +/* * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c * That means, when it wraps around any pid < 300 will be skipped.
tools/testing/selftests/pidfd/pidfd_open_test.c +0 -26
@@ -22,32 +22,6 @@#include "pidfd.h" #include "../kselftest.h" -#ifndef PIDFS_IOCTL_MAGIC -#define PIDFS_IOCTL_MAGIC 0xFF -#endif - -#ifndef PIDFD_GET_INFO -#define PIDFD_GET_INFO _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info) -#define PIDFD_INFO_CGROUPID (1UL << 0) - -struct pidfd_info { -__u64 mask; -__u64 cgroupid; -__u32 pid; -__u32 tgid; -__u32 ppid; -__u32 ruid; -__u32 rgid; -__u32 euid; -__u32 egid; -__u32 suid; -__u32 sgid; -__u32 fsuid; -__u32 fsgid; -__u32 spare0[1 ]; -}; -#endif -static int safe_int (const char *numstr,int *converted) {char *err =NULL ;
tools/testing/selftests/pidfd/pidfd_setns_test.c +0 -45
55 lines not on this page · raw message
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 10/15 selftests/pidfd: add first PIDFD_INFO_EXIT selftest
In reply to Christian BraunerAdd a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 +- tools/testing/selftests/pidfd/pidfd.h | 6 +- tools/testing/selftests/pidfd/pidfd_info_test.c | 146 ++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-)
tools/testing/selftests/pidfd/.gitignore +1 -0
@@ -8,3 +8,4 @@ pidfd_getfd_test pidfd_setns_test pidfd_file_handle_test pidfd_bind_mount +pidfd_info_test
tools/testing/selftests/pidfd/Makefile +1 -1
@@ -3,7 +3,7 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ - pidfd_file_handle_test pidfd_bind_mount + pidfd_file_handle_test pidfd_bind_mount pidfd_info_test include ../lib.mk
tools/testing/selftests/pidfd/pidfd.h +5 -1
@@ -127,6 +127,10 @@#define PIDFD_INFO_CGROUPID (1UL << 2)/* Always returned if available, even if not requested */ #endif +#ifndef PIDFD_INFO_EXIT +#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */ +#endif +struct pidfd_info {__u64 mask;__u64 cgroupid;
@@ -141,7 +145,7 @@ struct pidfd_info {__u32 sgid;__u32 fsuid;__u32 fsgid; -__u32 spare0[1 ]; +__s32 exit_code; };/*
tools/testing/selftests/pidfd/pidfd_info_test.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 11/15 selftests/pidfd: add second PIDFD_INFO_EXIT selftest
In reply to Christian BraunerAdd a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/pidfd_info_test.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
tools/testing/selftests/pidfd/pidfd_info_test.c +18 -0
@@ -143,4 +143,22 @@ TEST_F(pidfd_info, sigkill_exit)ASSERT_FALSE (!!(info.mask &PIDFD_INFO_EXIT )); } +TEST_F(pidfd_info, sigkill_reaped) +{ +struct pidfd_info info = { + .mask =PIDFD_INFO_CGROUPID , + }; + +/* Process has already been reaped and PIDFD_INFO_EXIT hasn't been set. */ +ASSERT_NE (ioctl (self->child_pidfd2,PIDFD_GET_INFO , &info),0 ); +ASSERT_EQ (errno,ESRCH ); + + info.mask =PIDFD_INFO_CGROUPID |PIDFD_INFO_EXIT ; +ASSERT_EQ (ioctl (self->child_pidfd2,PIDFD_GET_INFO , &info),0 ); +ASSERT_FALSE (!!(info.mask &PIDFD_INFO_CREDS )); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_EXIT )); +ASSERT_TRUE (WIFSIGNALED (info.exit_code)); +ASSERT_EQ (WTERMSIG (info.exit_code),SIGKILL ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 12/15 selftests/pidfd: add third PIDFD_INFO_EXIT selftest
In reply to Christian BraunerAdd a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/pidfd_info_test.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
tools/testing/selftests/pidfd/pidfd_info_test.c +16 -0
@@ -161,4 +161,20 @@ TEST_F(pidfd_info, sigkill_reaped)ASSERT_EQ (WTERMSIG (info.exit_code),SIGKILL ); } +TEST_F(pidfd_info, success_exit) +{ +struct pidfd_info info = { + .mask =PIDFD_INFO_CGROUPID , + }; + +/* Process has exited but not been reaped so this must work. */ +ASSERT_EQ (ioctl (self->child_pidfd3,PIDFD_GET_INFO , &info),0 ); + + info.mask =PIDFD_INFO_CGROUPID |PIDFD_INFO_EXIT ; +ASSERT_EQ (ioctl (self->child_pidfd3,PIDFD_GET_INFO , &info),0 ); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_CREDS )); +/* Process has exited but not been reaped, so no PIDFD_INFO_EXIT information yet. */ +ASSERT_FALSE (!!(info.mask &PIDFD_INFO_EXIT )); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 13/15 selftests/pidfd: add fourth PIDFD_INFO_EXIT selftest
In reply to Christian BraunerAdd a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/pidfd_info_test.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
tools/testing/selftests/pidfd/pidfd_info_test.c +18 -0
@@ -177,4 +177,22 @@ TEST_F(pidfd_info, success_exit)ASSERT_FALSE (!!(info.mask &PIDFD_INFO_EXIT )); } +TEST_F(pidfd_info, success_reaped) +{ +struct pidfd_info info = { + .mask =PIDFD_INFO_CGROUPID , + }; + +/* Process has already been reaped and PIDFD_INFO_EXIT hasn't been set. */ +ASSERT_NE (ioctl (self->child_pidfd4,PIDFD_GET_INFO , &info),0 ); +ASSERT_EQ (errno,ESRCH ); + + info.mask =PIDFD_INFO_CGROUPID |PIDFD_INFO_EXIT ; +ASSERT_EQ (ioctl (self->child_pidfd4,PIDFD_GET_INFO , &info),0 ); +ASSERT_FALSE (!!(info.mask &PIDFD_INFO_CREDS )); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_EXIT )); +ASSERT_TRUE (WIFEXITED (info.exit_code)); +ASSERT_EQ (WEXITSTATUS (info.exit_code),0 ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 14/15 selftests/pidfd: add fifth PIDFD_INFO_EXIT selftest
In reply to Christian BraunerAdd a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/pidfd_info_test.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/testing/selftests/pidfd/pidfd_info_test.c +23 -0
@@ -195,4 +195,27 @@ TEST_F(pidfd_info, success_reaped)ASSERT_EQ (WEXITSTATUS (info.exit_code),0 ); } +TEST_F(pidfd_info, success_reaped_poll) +{ +struct pidfd_info info = { + .mask =PIDFD_INFO_CGROUPID |PIDFD_INFO_EXIT , + }; +struct pollfd fds = {}; +int nevents; + + fds.events =POLLIN ; + fds.fd = self->child_pidfd2; + + nevents =poll (&fds,1 ,-1 ); +ASSERT_EQ (nevents,1 ); +ASSERT_TRUE (!!(fds.revents &POLLIN )); +ASSERT_TRUE (!!(fds.revents &POLLHUP )); + +ASSERT_EQ (ioctl (self->child_pidfd2,PIDFD_GET_INFO , &info),0 ); +ASSERT_FALSE (!!(info.mask &PIDFD_INFO_CREDS )); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_EXIT )); +ASSERT_TRUE (WIFSIGNALED (info.exit_code)); +ASSERT_EQ (WTERMSIG (info.exit_code),SIGKILL ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH v2 15/15 selftests/pidfd: add sixth PIDFD_INFO_EXIT selftest
In reply to Christian BraunerAdd a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/pidfd.h | 4 + tools/testing/selftests/pidfd/pidfd_info_test.c | 151 ++++++++++++++++++++++++ 2 files changed, 155 insertions(+)
tools/testing/selftests/pidfd/pidfd.h +4 -0
@@ -131,6 +131,10 @@#define PIDFD_INFO_EXIT (1UL << 3)/* Always returned if available, even if not requested */ #endif +#ifndef PIDFD_THREAD +#define PIDFD_THREAD O_EXCL +#endif +struct pidfd_info {__u64 mask;__u64 cgroupid;
tools/testing/selftests/pidfd/pidfd_info_test.c +151 -0
155 lines not on this page · raw message
Signature · 1 line
2.47.2
Christian Brauner
maintainer[PATCH v2 17/16] selftests/pidfd: test multi-threaded exec with PPIDFD_INFO_EXIT
Add a selftest for PIDFD_INFO_EXIT behavior.
--- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 + tools/testing/selftests/pidfd/pidfd.h | 7 ++ .../selftests/pidfd/pidfd_exec_helper.c | 12 +++ .../testing/selftests/pidfd/pidfd_info_test.c | 102 ++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 tools/testing/selftests/pidfd/pidfd_exec_helper.c
tools/testing/selftests/pidfd/.gitignore +1 -0
@@ -9,3 +9,4 @@ pidfd_setns_test pidfd_file_handle_test pidfd_bind_mount pidfd_info_test +pidfd_exec_helper
tools/testing/selftests/pidfd/Makefile +2 -0
@@ -5,5 +5,7 @@ TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ pidfd_file_handle_test pidfd_bind_mount pidfd_info_test +TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper + include ../lib.mk
tools/testing/selftests/pidfd/pidfd.h +7 -0
@@ -254,4 +254,11 @@ static inline ssize_t write_nointr(int fd, const void *buf, size_t count) return ret; } +static inline int sys_execveat (int dirfd,const char *pathname, +char *const argv[],char *const envp[], +int flags) +{ +return syscall (__NR_execveat, dirfd, pathname, argv, envp, flags); +} +#endif /* __PIDFD_H */
tools/testing/selftests/pidfd/pidfd_exec_helper.c new file +12 -0
@@ -0,0 +1,12 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +int main (int argc,char *argv[]) +{ +if (pause ()) +_exit (EXIT_FAILURE ); + +_exit (EXIT_SUCCESS ); +}
tools/testing/selftests/pidfd/pidfd_info_test.c +102 -0
106 lines not on this page · raw message
Signature · 1 line
2.47.2