Skip to content

Commit c56b6f3

Browse files
Steve Sistareakpm00
authored andcommitted
mm/hugetlb: fix memfd_pin_folios free_huge_pages leak
memfd_pin_folios followed by unpin_folios fails to restore free_huge_pages if the pages were not already faulted in, because the folio refcount for pages created by memfd_alloc_folio never goes to 0. memfd_pin_folios needs another folio_put to undo the folio_try_get below: memfd_alloc_folio() alloc_hugetlb_folio_nodemask() dequeue_hugetlb_folio_nodemask() dequeue_hugetlb_folio_node_exact() folio_ref_unfreeze(folio, 1); ; adds 1 refcount folio_try_get() ; adds 1 refcount hugetlb_add_to_page_cache() ; adds 512 refcount (on x86) With the fix, after memfd_pin_folios + unpin_folios, the refcount for the (unfaulted) page is 512, which is correct, as the refcount for a faulted unpinned page is 513. Link: https://lkml.kernel.org/r/[email protected] Fixes: 89c1905 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") Signed-off-by: Steve Sistare <[email protected]> Acked-by: Vivek Kasireddy <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Jason Gunthorpe <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Muchun Song <[email protected]> Cc: Peter Xu <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent c225c4f commit c56b6f3

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

mm/gup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3615,7 +3615,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
36153615
pgoff_t start_idx, end_idx, next_idx;
36163616
struct folio *folio = NULL;
36173617
struct folio_batch fbatch;
3618-
struct hstate *h;
3618+
struct hstate *h = NULL;
36193619
long ret = -EINVAL;
36203620

36213621
if (start < 0 || start > end || !max_folios)
@@ -3659,6 +3659,8 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
36593659
&fbatch);
36603660
if (folio) {
36613661
folio_put(folio);
3662+
if (h)
3663+
folio_put(folio);
36623664
folio = NULL;
36633665
}
36643666

0 commit comments

Comments
 (0)