Skip to content

Commit dc677b5

Browse files
Steve Sistareakpm00
authored andcommitted
mm/hugetlb: simplify refs in memfd_alloc_folio
The folio_try_get in memfd_alloc_folio is not necessary. Delete it, and delete the matching folio_put in memfd_pin_folios. This also avoids leaking a ref if the memfd_alloc_folio call to hugetlb_add_to_page_cache fails. That error path is also broken in a second way -- when its folio_put causes the ref to become 0, it will implicitly call free_huge_folio, but then the path *explicitly* calls free_huge_folio. Delete the latter. This is a continuation of the fix "mm/hugetlb: fix memfd_pin_folios free_huge_pages leak" [steven.sistare@oracle.com: remove explicit call to free_huge_folio(), per Matthew] Link: https://lkml.kernel.org/r/Zti-7nPVMcGgpcbi@casper.infradead.org Link: https://lkml.kernel.org/r/1725481920-82506-1-git-send-email-steven.sistare@oracle.com Link: https://lkml.kernel.org/r/1725478868-61732-1-git-send-email-steven.sistare@oracle.com Fixes: 89c1905 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") Signed-off-by: Steve Sistare <steven.sistare@oracle.com> Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Cc: David Hildenbrand <david@redhat.com> Cc: Jason Gunthorpe <jgg@nvidia.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Muchun Song <muchun.song@linux.dev> Cc: Peter Xu <peterx@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ce645b9 commit dc677b5

2 files changed

Lines changed: 2 additions & 5 deletions

File tree

mm/gup.c

Lines changed: 1 addition & 3 deletions
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 = NULL;
3618+
struct hstate *h;
36193619
long ret = -EINVAL;
36203620

36213621
if (start < 0 || start > end || !max_folios)
@@ -3659,8 +3659,6 @@ 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);
36643662
folio = NULL;
36653663
}
36663664

mm/memfd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
8989
numa_node_id(),
9090
NULL,
9191
gfp_mask);
92-
if (folio && folio_try_get(folio)) {
92+
if (folio) {
9393
err = hugetlb_add_to_page_cache(folio,
9494
memfd->f_mapping,
9595
idx);
9696
if (err) {
9797
folio_put(folio);
98-
free_huge_folio(folio);
9998
return ERR_PTR(err);
10099
}
101100
folio_unlock(folio);

0 commit comments

Comments
 (0)