/*
         * Must be called _before_ exec_mmap() as bprm->mm is
-        * not visible until then. This also enables the update
-        * to be lockless.
+        * not visible until then. Doing it here also ensures
+        * we don't race against replace_mm_exe_file().
         */
        retval = set_mm_exe_file(bprm->mm, bprm->file);
        if (retval)
 
  * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
  *
  * Main users are mmput() and sys_execve(). Callers prevent concurrent
- * invocations: in mmput() nobody alive left, in execve task is single
- * threaded.
+ * invocations: in mmput() nobody alive left, in execve it happens before
+ * the new mm is made visible to anyone.
  *
  * Can only fail if new_exe_file != NULL.
  */
 /**
  * replace_mm_exe_file - replace a reference to the mm's executable file
  *
- * This changes mm's executable file (shown as symlink /proc/[pid]/exe),
- * dealing with concurrent invocation and without grabbing the mmap lock in
- * write mode.
+ * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
  *
  * Main user is sys_prctl(PR_SET_MM_MAP/EXE_FILE).
  */
                        return ret;
        }
 
-       /* set the new file, lockless */
        ret = deny_write_access(new_exe_file);
        if (ret)
                return -EACCES;
        get_file(new_exe_file);
 
-       old_exe_file = xchg(&mm->exe_file, new_exe_file);
+       /* set the new file */
+       mmap_write_lock(mm);
+       old_exe_file = rcu_dereference_raw(mm->exe_file);
+       rcu_assign_pointer(mm->exe_file, new_exe_file);
+       mmap_write_unlock(mm);
+
        if (old_exe_file) {
-               /*
-                * Don't race with dup_mmap() getting the file and disallowing
-                * write access while someone might open the file writable.
-                */
-               mmap_read_lock(mm);
                allow_write_access(old_exe_file);
                fput(old_exe_file);
-               mmap_read_unlock(mm);
        }
        return 0;
 }