Skip to content

Commit

Permalink
VM: skip the stack guard page lookup in get_user_pages only for mlock
Browse files Browse the repository at this point in the history
The logic in __get_user_pages() used to skip the stack guard page lookup
whenever the caller wasn't interested in seeing what the actual page
was.  But Michel Lespinasse points out that there are cases where we
don't care about the physical page itself (so 'pages' may be NULL), but
do want to make sure a page is mapped into the virtual address space.

So using the existence of the "pages" array as an indication of whether
to look up the guard page or not isn't actually so great, and we really
should just use the FOLL_MLOCK bit.  But because that bit was only set
for the VM_LOCKED case (and not all vma's necessarily have it, even for
mlock()), we couldn't do that originally.

Fix that by moving the VM_LOCKED check deeper into the call-chain, which
actually simplifies many things.  Now mlock() gets simpler, and we can
also check for FOLL_MLOCK in __get_user_pages() and the code ends up
much more straightforward.

Reported-and-reviewed-by: Michel Lespinasse <[email protected]>
Cc: [email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
torvalds committed May 5, 2011
1 parent 5895198 commit a1fde08
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
7 changes: 3 additions & 4 deletions mm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
*/
mark_page_accessed(page);
}
if (flags & FOLL_MLOCK) {
if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
/*
* The preliminary mapping check is mainly to avoid the
* pointless overhead of lock_page on the ZERO_PAGE
Expand Down Expand Up @@ -1552,10 +1552,9 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
}

/*
* If we don't actually want the page itself,
* and it's the stack guard page, just skip it.
* For mlock, just skip the stack guard page.
*/
if (!pages && stack_guard_page(vma, start))
if ((gup_flags & FOLL_MLOCK) && stack_guard_page(vma, start))
goto next_page;

do {
Expand Down
5 changes: 1 addition & 4 deletions mm/mlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static long __mlock_vma_pages_range(struct vm_area_struct *vma,
VM_BUG_ON(end > vma->vm_end);
VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));

gup_flags = FOLL_TOUCH;
gup_flags = FOLL_TOUCH | FOLL_MLOCK;
/*
* We want to touch writable mappings with a write fault in order
* to break COW, except for shared mappings because these don't COW
Expand All @@ -178,9 +178,6 @@ static long __mlock_vma_pages_range(struct vm_area_struct *vma,
if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
gup_flags |= FOLL_FORCE;

if (vma->vm_flags & VM_LOCKED)
gup_flags |= FOLL_MLOCK;

return __get_user_pages(current, mm, addr, nr_pages, gup_flags,
NULL, NULL, nonblocking);
}
Expand Down

0 comments on commit a1fde08

Please sign in to comment.