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" [[email protected]: remove explicit call to free_huge_folio(), per Matthew] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] 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]> Suggested-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 ce645b9 commit dc677b5

File tree

2 files changed

+2
-5
lines changed

2 files changed

+2
-5
lines changed

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)