diff --git a/kernel/runfd.c b/kernel/runfd.c
index 843f7f52fa95..56149dd3b4be 100644
--- a/kernel/runfd.c
+++ b/kernel/runfd.c
@@ -69,6 +69,8 @@ void runfd_put(struct run *c)
        runfd_free(c);
 }
 
+DEFINE_FREE(runfd_put, struct run *, runfd_put(_T))
+
 static __poll_t runfd_poll(struct file *file, poll_table *wait)
 {
        struct run *run = file->private_data;
@@ -241,17 +243,21 @@ static long runfd_share_nsfd(struct run *c, unsigned long 
arg)
  *
  * ``waitid(P_RUNFD, run_fd, infop, options, NULL)`` waits freezably until
  * ``run_fd`` reaches ``RUNFD_DEAD`` (unless ``WNOHANG`` or ``O_NONBLOCK`` is
- * set) and writes init's exit information into ``infop`` without consuming it.
+ * set) and writes init's exit information into ``infop``. The information
+ * remains available to other waiters. Init's parent reaps the process
+ * separately once it becomes waitable.
+ *
  * ``options`` must include ``WEXITED`` and may include ``WNOHANG`` and
- * ``WNOWAIT``; ``rusage`` must be ``NULL``, and the caller must run in an
- * ancestor active PID namespace.
+ * ``WNOWAIT``. ``WNOWAIT`` leaves the behavior unchanged. ``rusage`` must be
+ * ``NULL``, and the caller must run in an ancestor active PID namespace.
+ * ``IORING_OP_WAITID`` does not support ``P_RUNFD``.
  */
 long runfd_waitid(int upid, struct waitid_info *infop, int options,
                  struct rusage *ru)
 {
        struct pid_namespace *pid_ns;
        unsigned int f_flags;
-       struct run *run;
+       struct run *run __free(runfd_put) = NULL;
        bool nonblock;
        int status;
        kuid_t uid;
@@ -264,13 +270,19 @@ long runfd_waitid(int upid, struct waitid_info *infop, 
int options,
        if (!(options & WEXITED))
                return -EINVAL;
 
-       CLASS(fd, f)(upid);
-       if (fd_empty(f) || !runfd_is_file(fd_file(f)))
-               return -EBADF;
+       {
+               CLASS(fd, f)(upid);
+
+               if (fd_empty(f) || !runfd_is_file(fd_file(f)))
+                       return -EBADF;
+
+               f_flags = READ_ONCE(fd_file(f)->f_flags);
+               run = fd_file(f)->private_data;
+               refcount_inc(&run->usage);
+               /* Don't pin the file across the wait: release may kill init. */
+       }
 
-       f_flags = READ_ONCE(fd_file(f)->f_flags);
        nonblock = (options & WNOHANG) || (f_flags & O_NONBLOCK);
-       run = fd_file(f)->private_data;
        pid_ns = task_active_pid_ns(current);
 
        if (!pidns_is_ancestor(run->ns->pid_ns_for_children, pid_ns))

Reply via email to