if (p->binfmt && !try_module_get(p->binfmt->module))
                goto bad_fork_cleanup_put_domain;
 
+       if (pid != &init_struct_pid) {
+               pid = alloc_pid();
+               if (!pid)
+                       goto bad_fork_put_binfmt_module;
+       }
+
        p->did_exec = 0;
        delayacct_tsk_init(p);  /* Must remain after dup_task_struct() */
        copy_flags(clone_flags, p);
        cgroup_exit(p, cgroup_callbacks_done);
 bad_fork_cleanup_delays_binfmt:
        delayacct_tsk_free(p);
+       if (pid != &init_struct_pid)
+               free_pid(pid);
+bad_fork_put_binfmt_module:
        if (p->binfmt)
                module_put(p->binfmt->module);
 bad_fork_cleanup_put_domain:
 {
        struct task_struct *p;
        int trace = 0;
-       struct pid *pid = alloc_pid();
        long nr;
 
-       if (!pid)
-               return -EAGAIN;
-       nr = pid->nr;
        if (unlikely(current->ptrace)) {
                trace = fork_traceflag (clone_flags);
                if (trace)
                        clone_flags |= CLONE_PTRACE;
        }
 
-       p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
+       p = copy_process(clone_flags, stack_start, regs, stack_size,
+                       parent_tidptr, child_tidptr, NULL);
        /*
         * Do this prior waking up the new thread - the thread pointer
         * might get invalid after that point, if the thread exits quickly.
        if (!IS_ERR(p)) {
                struct completion vfork;
 
+               nr = pid_nr(task_pid(p));
+
                if (clone_flags & CLONE_VFORK) {
                        p->vfork_done = &vfork;
                        init_completion(&vfork);
                        }
                }
        } else {
-               free_pid(pid);
                nr = PTR_ERR(p);
        }
        return nr;