Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Hongfu Li <hongfu.li@linux.dev>
To: muchun.song@linux.dev, osalvador@suse.de, david@kernel.org,
	akpm@linux-foundation.org, steven.sistare@oracle.com,
	vivek.kasireddy@intel.com
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	hongfu.li@linux.dev, Hongfu Li <lihongfu@kylinos.cn>
Subject: [PATCH] mm/hugetlb: fix resv_huge_pages double decrement in memfd error path
Date: Tue, 25 Aug 2026 10:10:13 +0800	[thread overview]
Message-ID: <20260825021013.25672-1-hongfu.li@linux.dev> (raw)

From: Hongfu Li <lihongfu@kylinos.cn>

alloc_hugetlb_folio_reserve() decrements h->resv_huge_pages when
dequeuing a folio, but unlike the use_global_reservation handling in
hugetlb_alloc_folio(), it does not set HPageRestoreReserve on the folio.

Its sole caller memfd_alloc_folio() pre-allocates a reservation via
hugetlb_reserve_pages() before allocating. When hugetlb_add_to_page_cache()
fails, folio_put() drops the folio without HPageRestoreReserve set, so
free_huge_folio() does not restore the reservation. The subsequent
hugetlb_unreserve_pages() on the err_unresv path decrements the counter
a second time, leaving resv_huge_pages off by one for every failed
allocation.

Set HPageRestoreReserve when consuming the reservation in
alloc_hugetlb_folio_reserve(). On the error path, free_huge_folio() then
restores the reservation before hugetlb_unreserve_pages() releases it.
The success path is unaffected, as hugetlb_add_to_page_cache() clears
the flag once the folio is added to the page cache.

Fixes: 26a8ea80929c ("mm/hugetlb: fix memfd_pin_folios resv_huge_pages leak")
Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
---
 mm/hugetlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index d8849196ad9b..fadfe1dbc047 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2178,8 +2178,10 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
 
 	folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, preferred_nid,
 					       nmask);
-	if (folio)
+	if (folio) {
+		folio_set_hugetlb_restore_reserve(folio);
 		h->resv_huge_pages--;
+	}
 
 	spin_unlock_irq(&hugetlb_lock);
 	return folio;
-- 
2.54.0



             reply	other threads:[~2026-08-25  2:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25  2:10 Hongfu Li [this message]
2026-08-25  5:42 ` [PATCH] mm/hugetlb: fix resv_huge_pages double decrement in memfd error path Muchun Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260825021013.25672-1-hongfu.li@linux.dev \
    --to=hongfu.li@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=lihongfu@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    --cc=steven.sistare@oracle.com \
    --cc=vivek.kasireddy@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox