*/
 int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
 {
-       bool thread = flags & PIDFD_THREAD;
+       int err = 0;
 
-       if (!pid_has_task(pid, thread ? PIDTYPE_PID : PIDTYPE_TGID))
-               return -EINVAL;
+       if (!(flags & PIDFD_THREAD)) {
+               /*
+                * If this is struct pid isn't used as a thread-group
+                * leader pid but the caller requested to create a
+                * thread-group leader pidfd then report ENOENT to the
+                * caller as a hint.
+                */
+               if (!pid_has_task(pid, PIDTYPE_TGID))
+                       err = -ENOENT;
+       }
+
+       /*
+        * If this wasn't a thread-group leader struct pid or the task
+        * got reaped in the meantime report -ESRCH to userspace.
+        */
+       if (!pid_has_task(pid, PIDTYPE_PID))
+               err = -ESRCH;
+       if (err)
+               return err;
 
        return __pidfd_prepare(pid, flags, ret);
 }