From: Steve Sistare <steven.sistare@xxxxxxxxxx> commit ce645b9fdc78ec5d28067286e92871ddae6817d5 upstream. If memfd_pin_folios tries to create a hugetlb page, but someone else already did, then folio gets the value -EEXIST here: folio = memfd_alloc_folio(memfd, start_idx); if (IS_ERR(folio)) { ret = PTR_ERR(folio); if (ret != -EEXIST) goto err; then on the next trip through the "while start_idx" loop we panic here: if (folio) { folio_put(folio); To fix, set the folio to NULL on error. Link: https://lkml.kernel.org/r/1725373521-451395-6-git-send-email-steven.sistare@xxxxxxxxxx Fixes: 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") Signed-off-by: Steve Sistare <steven.sistare@xxxxxxxxxx> Acked-by: Vivek Kasireddy <vivek.kasireddy@xxxxxxxxx> Cc: David Hildenbrand <david@xxxxxxxxxx> Cc: Jason Gunthorpe <jgg@xxxxxxxxxx> Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx> Cc: Muchun Song <muchun.song@xxxxxxxxx> Cc: Peter Xu <peterx@xxxxxxxxxx> Cc: <stable@xxxxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- mm/gup.c | 1 + 1 file changed, 1 insertion(+) --- a/mm/gup.c +++ b/mm/gup.c @@ -3705,6 +3705,7 @@ long memfd_pin_folios(struct file *memfd ret = PTR_ERR(folio); if (ret != -EEXIST) goto err; + folio = NULL; } } } Patches currently in stable-queue which might be from steven.sistare@xxxxxxxxxx are queue-6.11/mm-gup-fix-memfd_pin_folios-alloc-race-panic.patch queue-6.11/mm-hugetlb-fix-memfd_pin_folios-resv_huge_pages-leak.patch queue-6.11/mm-hugetlb-simplify-refs-in-memfd_alloc_folio.patch queue-6.11/mm-hugetlb-fix-memfd_pin_folios-free_huge_pages-leak.patch queue-6.11/mm-filemap-fix-filemap_get_folios_contig-thp-panic.patch queue-6.11/mm-gup-fix-memfd_pin_folios-hugetlb-page-allocation.patch