mm/memfd: don't unreserve hugetlb pages on -EEXIST in error path
Subsystems (2)
PATCH 1/1 mm/memfd: don't unreserve hugetlb pages on -EEXIST in error path
From: Hongfu Li <[email protected]>
If hugetlb_add_to_page_cache() fails with -EEXIST, a concurrent fault has already instantiated the folio in the page cache, and the reservation now belongs to that folio. Calling hugetlb_unreserve_pages() in that case incorrectly removes the region backing the cached folio, and a later truncate or inode eviction passes a negative (chg - freed) into hugepage_subpool_put_pages(), corrupting subpool and resv_huge_pages accounting.
Skip the unreserve on -EEXIST; failures other than -EEXIST leave the reservation unconsumed and still unreserve it.
--- mm/memfd.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/memfd.c +6 -0
@@ -128,6 +128,12 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)if (err) {folio_put (folio); +/* +* On -EEXIST, a concurrent fault has cached the folio, +* which now owns the reservation; don't unreserve. +*/ +if (err == -EEXIST ) +return ERR_PTR (err); goto err_unresv; }
Signature · 1 line
2.54.0
Andrew Morton
maintainerQuoted 10 lines · On Mon, 31 Aug 2026 17:06:31 +0800 Hongfu Li <[email protected]> wrote:
From: Hongfu Li <[email protected]>
If
hugetlb_add_to_page_cache()fails with-EEXIST, a concurrent fault has already instantiated the folio in the page cache, and the reservation now belongs to that folio. Callinghugetlb_unreserve_pages()in that case incorrectly removes the region backing the cached folio, and a later truncate or inode eviction passes a negative (chg - freed) intohugepage_subpool_put_pages(), corrupting subpool andresv_huge_pagesaccounting.
Quoted 2 lines
Skip the unreserve on
-EEXIST; failures other than-EEXISTleave the reservation unconsumed and still unreserve it.
Thanks.
AI review might have found another bug and it suggests that your proposal only partially fixes this bug:
https://sashiko.dev/#/patchset/[email protected]
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Mon, 31 Aug 2026 17:06:31 +0800 Hongfu Li <[email protected]> wrote:
From: Hongfu Li <[email protected]>
If
hugetlb_add_to_page_cache()fails with-EEXIST, a concurrent fault has already instantiated the folio in the page cache, and the reservation now belongs to that folio. Callinghugetlb_unreserve_pages()in that case incorrectly removes the region backing the cached folio, and a later truncate or inode eviction passes a negative (chg - freed) intohugepage_subpool_put_pages(), corrupting subpool andresv_huge_pagesaccounting.
Quoted 8 lines
Quoted 2 lines
Skip the unreserve on
-EEXIST; failures other than-EEXISTleave the reservation unconsumed and still unreserve it.Thanks.
AI review might have found another bug and it suggests that your proposal only partially fixes this bug:
https://sashiko.dev/#/patchset/[email protected]
Thank you for the reminder.
I have carefully gone through the Sashiko review report. The issue it pointed out is valid. I will address this remaining problem and send out a v2 patch shortly.
Signature · 2 lines
Best regards, Hongfu