Skip to content

Commit cc30042

Browse files
minatorvalds
authored andcommitted
hugetlb, userfaultfd: fix reservation restore on userfaultfd error
Currently in the is_continue case in hugetlb_mcopy_atomic_pte(), if we bail out using "goto out_release_unlock;" in the cases where idx >= size, or !huge_pte_none(), the code will detect that new_pagecache_page == false, and so call restore_reserve_on_error(). In this case I see restore_reserve_on_error() delete the reservation, and the following call to remove_inode_hugepages() will increment h->resv_hugepages causing a 100% reproducible leak. We should treat the is_continue case similar to adding a page into the pagecache and set new_pagecache_page to true, to indicate that there is no reservation to restore on the error path, and we need not call restore_reserve_on_error(). Rename new_pagecache_page to page_in_pagecache to make that clear. Link: https://lkml.kernel.org/r/20211117193825.378528-1-almasrymina@google.com Fixes: c7b1850 ("hugetlb: don't pass page cache pages to restore_reserve_on_error") Signed-off-by: Mina Almasry <almasrymina@google.com> Reported-by: James Houghton <jthoughton@google.com> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com> Cc: Wei Xu <weixugc@google.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent afe041c commit cc30042

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

mm/hugetlb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5736,13 +5736,14 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
57365736
int ret = -ENOMEM;
57375737
struct page *page;
57385738
int writable;
5739-
bool new_pagecache_page = false;
5739+
bool page_in_pagecache = false;
57405740

57415741
if (is_continue) {
57425742
ret = -EFAULT;
57435743
page = find_lock_page(mapping, idx);
57445744
if (!page)
57455745
goto out;
5746+
page_in_pagecache = true;
57465747
} else if (!*pagep) {
57475748
/* If a page already exists, then it's UFFDIO_COPY for
57485749
* a non-missing case. Return -EEXIST.
@@ -5830,7 +5831,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
58305831
ret = huge_add_to_page_cache(page, mapping, idx);
58315832
if (ret)
58325833
goto out_release_nounlock;
5833-
new_pagecache_page = true;
5834+
page_in_pagecache = true;
58345835
}
58355836

58365837
ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
@@ -5894,7 +5895,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
58945895
if (vm_shared || is_continue)
58955896
unlock_page(page);
58965897
out_release_nounlock:
5897-
if (!new_pagecache_page)
5898+
if (!page_in_pagecache)
58985899
restore_reserve_on_error(h, dst_vma, dst_addr, page);
58995900
put_page(page);
59005901
goto out;

0 commit comments

Comments
 (0)