A task that called vfork() sleeps until the child has released the mm in mm_release(). But exec_mmap() releases the old mm, then waits for exec_update_lock and only then switches to the new mm. And exit_mm() releases the mm and only then drops it. So the parent is woken up while the child is still on its mm. It wasn't meant to be that way. When mm_release() was added in 2.2 it was called after the task had been switched away from the mm. exec only moved it in front of the switch in 2.5.56 because deactivate_mm() and the write to the child tid address need the old mm (cf. [1]). The release has to stay where it is as it can sleep in a page fault for as long as userspace likes. So take the wakeup out of mm_release(). Wake up the parent in exec_mmap() once the task has switched to its new mm and in exit_mm() once it has dropped the mm. While at it, fix the comment above mm_release(). It hasn't been called after the mm was removed from the task since 2.5.56. Signed-off-by: Christian Brauner (Amutable) --- fs/exec.c | 3 ++- include/linux/sched/mm.h | 4 +++- kernel/exit.c | 7 ++++++- kernel/fork.c | 24 ++++++++++++++---------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 78046ed839646..4d3f78ea0b0e6 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -854,7 +854,6 @@ static int exec_mmap(struct linux_binprm *bprm) if (!exec_state) return -ENOMEM; - /* Notify parent that we're no longer interested in the old VM */ tsk = current; old_mm = current->mm; /* Clean up futexes and release the mm */ @@ -897,6 +896,8 @@ static int exec_mmap(struct linux_binprm *bprm) mmdrop_lazy_tlb(active_mm); } futex_exec_done(tsk); + /* We've left the old mm so a vfork() parent can run again. */ + complete_vfork_done(tsk); return 0; } diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index d7c6a942aa7e0..2adfba6217133 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -158,9 +158,11 @@ extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode); /* * Remove the current tasks stale references to the old mm_struct on exit() and - * exec(). Cleans up futexes as well. + * exec(). Cleans up futexes as well. A parent that sleeps in vfork() is only + * woken up by complete_vfork_done() once the task has left the mm. */ extern void mm_exit_exec_release(struct task_struct *, struct mm_struct *); +void complete_vfork_done(struct task_struct *tsk); #ifdef CONFIG_MEMCG extern void mm_update_next_owner(struct mm_struct *mm); diff --git a/kernel/exit.c b/kernel/exit.c index 38de318a07aaa..6120f0dc3b17d 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -554,8 +554,11 @@ static void exit_mm(void) struct mm_struct *mm = current->mm; mm_exit_exec_release(current, mm); - if (!mm) + if (!mm) { + /* kthread_stop() waits for this. */ + complete_vfork_done(current); return; + } sched_cache_exit_mm(current); @@ -579,6 +582,8 @@ static void exit_mm(void) enter_lazy_tlb(mm, current); local_irq_enable(); task_unlock(current); + /* We've left the mm so a vfork() parent can run again. */ + complete_vfork_done(current); mm_update_next_owner(mm); mmput(mm); if (test_thread_flag(TIF_MEMDIE)) diff --git a/kernel/fork.c b/kernel/fork.c index 5efceb8d58a78..f63133b9e6faf 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1428,10 +1428,21 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) return mm; } -static void complete_vfork_done(struct task_struct *tsk) +/** + * complete_vfork_done - wake up whoever waits for @tsk in vfork() + * @tsk: task that execs or exits + * + * A parent that sleeps in vfork() must not run before the child is off its + * mm. So this is called once @tsk has switched to its new mm in exec or has + * dropped its mm in exit. kthread_stop() uses this completion as well. + */ +void complete_vfork_done(struct task_struct *tsk) { struct completion *vfork; + if (!tsk->vfork_done) + return; + task_lock(tsk); vfork = tsk->vfork_done; if (likely(vfork)) { @@ -1465,8 +1476,8 @@ static int wait_for_vfork_done(struct task_struct *child, * mmput is called whenever we stop holding onto a mm_struct, * error success whatever. * - * mm_release is called after a mm_struct has been removed - * from the current process. + * mm_release is called when the current process is about to + * leave a mm_struct in exec or in exit. It's still on it. * * This difference is important for error handling, when we * only half set up a mm_struct for a new process and need to restore @@ -1498,13 +1509,6 @@ static void mm_release(struct task_struct *tsk, struct mm_struct *mm) } tsk->clear_child_tid = NULL; } - - /* - * All done, finally we can wake up parent and return this mm to him. - * Also kthread_stop() uses this completion for synchronization. - */ - if (tsk->vfork_done) - complete_vfork_done(tsk); } void mm_exit_exec_release(struct task_struct *tsk, struct mm_struct *mm) -- 2.53.0