Skip to content

Commit

Permalink
userfaultfd: hugetlbfs: fix __mcopy_atomic_hugetlb retry/error proces…
Browse files Browse the repository at this point in the history
…sing

The new routine copy_huge_page_from_user() uses kmap_atomic() to map
PAGE_SIZE pages.  However, this prevents page faults in the subsequent
call to copy_from_user().  This is OK in the case where the routine is
copied with mmap_sema held.  However, in another case we want to allow
page faults.  So, add a new argument allow_pagefault to indicate if the
routine should allow page faults.

[dan.carpenter@oracle.com: unmap the correct pointer]
  Link: http://lkml.kernel.org/r/20170113082608.GA3548@mwanda
[akpm@linux-foundation.org: kunmap() takes a page*, per Hugh]
Link: http://lkml.kernel.org/r/20161216144821.5183-20-aarcange@redhat.com
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Cc: Hillf Danton <hillf.zj@alibaba-inc.com>
Cc: Michael Rapoport <RAPOPORT@il.ibm.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Pavel Emelyanov <xemul@parallels.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Mike Kravetz authored and Linus Torvalds committed Feb 23, 2017
1 parent 60d4d2d commit 810a56b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2426,7 +2426,8 @@ extern void copy_user_huge_page(struct page *dst, struct page *src,
unsigned int pages_per_huge_page);
extern long copy_huge_page_from_user(struct page *dst_page,
const void __user *usr_src,
unsigned int pages_per_huge_page);
unsigned int pages_per_huge_page,
bool allow_pagefault);
#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */

extern struct page_ext_operations debug_guardpage_ops;
Expand Down
2 changes: 1 addition & 1 deletion mm/hugetlb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3973,7 +3973,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,

ret = copy_huge_page_from_user(page,
(const void __user *) src_addr,
pages_per_huge_page(h));
pages_per_huge_page(h), false);

/* fallback to copy_from_user outside mmap_sem */
if (unlikely(ret)) {
Expand Down
13 changes: 10 additions & 3 deletions mm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -4155,19 +4155,26 @@ void copy_user_huge_page(struct page *dst, struct page *src,

long copy_huge_page_from_user(struct page *dst_page,
const void __user *usr_src,
unsigned int pages_per_huge_page)
unsigned int pages_per_huge_page,
bool allow_pagefault)
{
void *src = (void *)usr_src;
void *page_kaddr;
unsigned long i, rc = 0;
unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;

for (i = 0; i < pages_per_huge_page; i++) {
page_kaddr = kmap_atomic(dst_page + i);
if (allow_pagefault)
page_kaddr = kmap(dst_page + i);
else
page_kaddr = kmap_atomic(dst_page + i);
rc = copy_from_user(page_kaddr,
(const void __user *)(src + i * PAGE_SIZE),
PAGE_SIZE);
kunmap_atomic(page_kaddr);
if (allow_pagefault)
kunmap(dst_page + i);
else
kunmap_atomic(page_kaddr);

ret_val -= (PAGE_SIZE - rc);
if (rc)
Expand Down
2 changes: 1 addition & 1 deletion mm/userfaultfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,

err = copy_huge_page_from_user(page,
(const void __user *)src_addr,
pages_per_huge_page(h));
pages_per_huge_page(h), true);
if (unlikely(err)) {
err = -EFAULT;
goto out;
Expand Down

0 comments on commit 810a56b

Please sign in to comment.