Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
mmap locking API: don't check locking if the mm isn't live yet
Browse files Browse the repository at this point in the history
In preparation for adding a mmap_assert_locked() check in
__get_user_pages(), teach the mmap_assert_*locked() helpers that it's fine
to operate on an mm without locking in the middle of execve() as long as
it hasn't been installed on a process yet.

Existing code paths that do this are (reverse callgraph):

  get_user_pages_remote
    get_arg_page
      copy_strings
      copy_string_kernel
      remove_arg_zero
    tomoyo_dump_page
      tomoyo_print_bprm
      tomoyo_scan_bprm
      tomoyo_environ

Link: https://lkml.kernel.org/r/CAG48ez03YJG9JU_6tGiMcaVjuTyRE_o4LEQ7901b5ZoCnNAjcg@mail.gmail.com
Signed-off-by: Jann Horn <[email protected]>
Cc: "Eric W . Biederman" <[email protected]>
Cc: Jason Gunthorpe <[email protected]>
Cc: John Hubbard <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: Michel Lespinasse <[email protected]>
Cc: Sakari Ailus <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Stephen Rothwell <[email protected]>
  • Loading branch information
thejh authored and sfrothwell committed Oct 12, 2020
1 parent 28d300c commit 093e982
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions fs/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,14 @@ static int exec_mmap(struct mm_struct *mm)
}
}

#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
/*
* From here on, the mm may be accessed concurrently, and proper locking
* is required for things like get_user_pages_remote().
*/
mm->mmap_lock_required = 1;
#endif

task_lock(tsk);
membarrier_exec_mmap(mm);

Expand Down
10 changes: 10 additions & 0 deletions include/linux/mm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ struct mm_struct {
#ifdef CONFIG_IOMMU_SUPPORT
u32 pasid;
#endif

#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
/*
* Notes whether this mm has been installed on a process yet.
* If not, only the task going through execve() can access this
* mm, and no locking is needed around get_user_pages_remote().
* This flag is only used for debug checks.
*/
bool mmap_lock_required;
#endif
} __randomize_layout;

/*
Expand Down
16 changes: 12 additions & 4 deletions include/linux/mmap_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)

static inline void mmap_assert_locked(struct mm_struct *mm)
{
lockdep_assert_held(&mm->mmap_lock);
VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
if (mm->mmap_lock_required) {
lockdep_assert_held(&mm->mmap_lock);
VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
}
#endif
}

static inline void mmap_assert_write_locked(struct mm_struct *mm)
{
lockdep_assert_held_write(&mm->mmap_lock);
VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
if (mm->mmap_lock_required) {
lockdep_assert_held_write(&mm->mmap_lock);
VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
}
#endif
}

static inline int mmap_lock_is_contended(struct mm_struct *mm)
Expand Down

0 comments on commit 093e982

Please sign in to comment.