]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
pidfs: record exit code and cgroupid at exit
authorChristian Brauner <brauner@kernel.org>
Wed, 5 Mar 2025 10:08:15 +0000 (11:08 +0100)
committerChristian Brauner <brauner@kernel.org>
Wed, 5 Mar 2025 12:26:12 +0000 (13:26 +0100)
Record the exit code and cgroupid in release_task() and stash in struct
pidfs_exit_info so it can be retrieved even after the task has been
reaped.

Link: https://lore.kernel.org/r/20250305-work-pidfs-kill_on_last_close-v3-5-c8c3d8361705@kernel.org
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/internal.h
fs/libfs.c
fs/pidfs.c
include/linux/pidfs.h
kernel/exit.c

index e7f02ae1e0981bd129a995c02f3b7a5defd7c669..c1e6d8b294cb28e375815b309e3530374d339cf0 100644 (file)
@@ -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
index 8444f5cc406415d4fdba45dc059a9d19d13ab881..cf5a267aafe434e3b29e03fc33f88b1f3cb30040 100644 (file)
@@ -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;
index 282511a36fd96e887f180dcd6caefa086e0816a0..c4e6527013e77d07ad9595b9f391348fe3bb73fc 100644 (file)
@@ -458,6 +458,47 @@ 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 referred to by a pidfd then
+ * stashed_dentry_get() 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.
+ */
+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;
+#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;
 
 /*
index 7c830d0dec9a7c098f65f98a9b9f605e9108da96..05e6f8f4a026429486dea36c5f2b5e322bf254e9 100644 (file)
@@ -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 */
index 3485e5fc499e468760302d6776fc8f915362b8bd..9916305e34d3451db517c5bdccecd561a61802db 100644 (file)
@@ -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>
 
@@ -249,6 +250,7 @@ repeat:
        dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
        rcu_read_unlock();
 
+       pidfs_exit(p);
        cgroup_release(p);
 
        write_lock_irq(&tasklist_lock);