Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/hugetlb: serialize subpool release with folio reserve restoration
@ 2026-11-25  8:28 Aveline Noir
  2026-11-27  9:37 ` Jose A. Perez de Azpillaga
  0 siblings, 1 reply; 2+ messages in thread
From: Aveline Noir @ 2026-11-25  8:28 UTC (permalink / raw)
  To: Muchun Song, Oscar Salvador, Andrew Morton
  Cc: David Hildenbrand, Mike Rapoport, Mike Kravetz, linux-mm

Freeing the last folio of an unmounted min_size subpool can invoke
hugetlb_acct_memory() from atomic folio_put() context, where its IRQ
and bulk-freeing paths may enable interrupts, reschedule, or allocate
vmemmap pages.

In addition, the subpool minimum is released before the folio's
reservation is restored, transiently underflowing resv_huge_pages. A
concurrent allocation can consume the refilled subpool reservation, or
unmount can release it, before the global counter is restored. Under the
available_huge_pages() clamp, this underflow also prevents collecting
unused surplus folios.

For positive minima, take hugetlb_lock before dropping spool->lock and
restore the folio reservation, release the minimum, and collect surplus
folios in a single transaction. Keep subpool accounting outside
hugetlb_lock, leave size-only and zero-minimum puts unnested, free
collected folios outside the locks via the atomic-safe path (which
defers HVO work), and skip zero-sized minimum releases that need no
global accounting.

The ordering came from 1c5ecae3a93f; the commit below made it reachable
by enabling min_size and releasing its reservation on subpool teardown.

Fixes: 7ca02d0ae586 ("hugetlbfs: accept subpool min_size mount option and setup accordingly")
Signed-off-by: Aveline Noir <jm5905938@gmail.com>
---
Tests passed in x86_64 KVM with KASAN
---
 mm/hugetlb.c | 123 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 90 insertions(+), 33 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index d0e66fb30622..05c34ca44a15 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -128,6 +128,9 @@ struct mutex *hugetlb_fault_mutex_table __ro_after_init;
 
 /* Forward declaration */
 static int hugetlb_acct_memory(struct hstate *h, long delta);
+static void collect_unused_surplus_pages(struct hstate *h,
+					 unsigned long unused_resv_pages,
+					 struct list_head *page_list);
 static unsigned int allowed_mems_nr(struct hstate *h);
 static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
 static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
@@ -166,7 +169,7 @@ static inline void unlock_or_release_subpool(struct hugepage_subpool *spool,
 	spin_unlock_irqrestore(&spool->lock, irq_flags);
 
 	if (free_subpool) {
-		if (spool->min_hpages != -1)
+		if (spool->min_hpages > 0)
 			hugetlb_acct_memory(spool->hstate,
 						-spool->min_hpages);
 		kfree(spool);
@@ -263,21 +266,17 @@ static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
  * The return value may only be different than the passed value (delta)
  * in the case where a subpool minimum size must be maintained.
  */
-static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
-				       long delta)
+static long __hugepage_subpool_put_pages(struct hugepage_subpool *spool,
+					 long delta)
 {
 	long ret = delta;
-	unsigned long flags;
-
-	if (!spool)
-		return delta;
 
-	spin_lock_irqsave(&spool->lock, flags);
+	lockdep_assert_held(&spool->lock);
 
 	if (spool->max_hpages != -1)		/* maximum size accounting */
 		spool->used_hpages -= delta;
 
-	 /* minimum size accounting */
+	/* minimum size accounting */
 	if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
 		if (spool->rsv_hpages + delta <= spool->min_hpages)
 			ret = 0;
@@ -289,6 +288,20 @@ static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
 			spool->rsv_hpages = spool->min_hpages;
 	}
 
+	return ret;
+}
+
+static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
+				       long delta)
+{
+	long ret;
+	unsigned long flags;
+
+	if (!spool)
+		return delta;
+
+	spin_lock_irqsave(&spool->lock, flags);
+	ret = __hugepage_subpool_put_pages(spool, delta);
 	/*
 	 * If hugetlbfs_put_super couldn't free spool due to an outstanding
 	 * quota reference, free it now.
@@ -298,6 +311,28 @@ static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
 	return ret;
 }
 
+/*
+ * Return with hugetlb_lock held.  Take it before dropping spool->lock so
+ * a refilled subpool reservation cannot be consumed
+ * before free_huge_folio() restores the global reservation.  Keep subpool
+ * accounting and waiting for spool->lock outside the global critical section.
+ * The caller owns final subpool release if *free_subpool is set.
+ */
+static bool hugepage_subpool_put_folio(struct hugepage_subpool *spool,
+				       unsigned long *flags, bool *free_subpool)
+{
+	bool restore_reserve;
+
+	lockdep_assert_not_held(&hugetlb_lock);
+	spin_lock_irqsave(&spool->lock, *flags);
+	restore_reserve = !__hugepage_subpool_put_pages(spool, 1);
+	*free_subpool = subpool_is_free(spool);
+	spin_lock(&hugetlb_lock);
+	spin_unlock(&spool->lock);
+
+	return restore_reserve;
+}
+
 static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
 {
 	return subpool_inode(file_inode(vma->vm_file));
@@ -1761,6 +1796,9 @@ void free_huge_folio(struct folio *folio)
 	int nid = folio_nid(folio);
 	struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
 	bool restore_reserve;
+	bool free_subpool = false;
+	struct folio *free_folio, *tmp;
+	LIST_HEAD(page_list);
 	unsigned long flags;
 
 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
@@ -1781,18 +1819,22 @@ void free_huge_folio(struct folio *folio)
 	 * reservation, do not call hugepage_subpool_put_pages() as this will
 	 * remove the reserved page from the subpool.
 	 */
-	if (!restore_reserve) {
+	if (!restore_reserve && spool && spool->min_hpages > 0) {
 		/*
 		 * A return code of zero implies that the subpool will be
 		 * under its minimum size if the reservation is not restored
 		 * after page is free.  Therefore, force restore_reserve
 		 * operation.
 		 */
-		if (hugepage_subpool_put_pages(spool, 1) == 0)
-			restore_reserve = true;
+		restore_reserve = hugepage_subpool_put_folio(spool, &flags,
+							     &free_subpool);
+	} else {
+		if (!restore_reserve)
+			hugepage_subpool_put_pages(spool, 1);
+		spin_lock_irqsave(&hugetlb_lock, flags);
 	}
 
-	spin_lock_irqsave(&hugetlb_lock, flags);
+	lockdep_assert_held(&hugetlb_lock);
 	folio_clear_hugetlb_migratable(folio);
 	hugetlb_cgroup_uncharge_folio(hstate_index(h),
 				     pages_per_huge_page(h), folio);
@@ -1806,21 +1848,29 @@ void free_huge_folio(struct folio *folio)
 	if (folio_test_hugetlb_temporary(folio)) {
 		remove_hugetlb_folio(h, folio, false);
 		folio_clear_hugetlb_temporary(folio);
-		spin_unlock_irqrestore(&hugetlb_lock, flags);
-		update_and_free_hugetlb_folio(h, folio, true);
+		list_add(&folio->lru, &page_list);
 	} else if (h->surplus_huge_pages_node[nid] &&
 		   h->free_huge_pages >= h->resv_huge_pages) {
 		/* Free this surplus folio that is not required globally. */
 		remove_hugetlb_folio(h, folio, true);
-		spin_unlock_irqrestore(&hugetlb_lock, flags);
-		update_and_free_hugetlb_folio(h, folio, true);
+		list_add(&folio->lru, &page_list);
 	} else {
 		mod_node_page_state(folio_pgdat(folio), NR_HUGETLB,
 				    -(long)pages_per_huge_page(h));
 		arch_clear_hugetlb_flags(folio);
 		enqueue_hugetlb_folio(h, folio);
-		spin_unlock_irqrestore(&hugetlb_lock, flags);
 	}
+
+	if (free_subpool)
+		collect_unused_surplus_pages(h, spool->min_hpages, &page_list);
+	spin_unlock_irqrestore(&hugetlb_lock, flags);
+
+	list_for_each_entry_safe(free_folio, tmp, &page_list, lru) {
+		list_del(&free_folio->lru);
+		update_and_free_hugetlb_folio(h, free_folio, true);
+	}
+	if (free_subpool)
+		kfree(spool);
 }
 
 /*
@@ -2502,26 +2552,18 @@ static int gather_surplus_pages(struct hstate *h, long delta)
 	return ret;
 }
 
-/*
- * This routine has two main purposes:
- * 1) Decrement the reservation count (resv_huge_pages) by the value passed
- *    in unused_resv_pages.  This corresponds to the prior adjustments made
- *    to the associated reservation map.
- * 2) Free any unused surplus pages that may have been allocated to satisfy
- *    the reservation.  As many as unused_resv_pages may be freed.
- */
-static void return_unused_surplus_pages(struct hstate *h,
-					unsigned long unused_resv_pages)
+static void collect_unused_surplus_pages(struct hstate *h,
+					 unsigned long unused_resv_pages,
+					 struct list_head *page_list)
 {
 	unsigned long nr_pages;
-	LIST_HEAD(page_list);
 
 	lockdep_assert_held(&hugetlb_lock);
 	/* Uncommit the reservation */
 	h->resv_huge_pages -= unused_resv_pages;
 
 	if (hstate_is_gigantic_no_runtime(h))
-		goto out;
+		return;
 
 	/*
 	 * Part (or even all) of the reservation could have been backed
@@ -2543,12 +2585,27 @@ static void return_unused_surplus_pages(struct hstate *h,
 
 		folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY], 1);
 		if (!folio)
-			goto out;
+			break;
 
-		list_add(&folio->lru, &page_list);
+		list_add(&folio->lru, page_list);
 	}
+}
+
+/*
+ * This routine has two main purposes:
+ * 1) Decrement the reservation count (resv_huge_pages) by the value passed
+ *    in unused_resv_pages.  This corresponds to the prior adjustments made
+ *    to the associated reservation map.
+ * 2) Free any unused surplus pages that may have been allocated to satisfy
+ *    the reservation.  As many as unused_resv_pages may be freed.
+ */
+static void return_unused_surplus_pages(struct hstate *h,
+					unsigned long unused_resv_pages)
+{
+	LIST_HEAD(page_list);
+
+	collect_unused_surplus_pages(h, unused_resv_pages, &page_list);
 
-out:
 	spin_unlock_irq(&hugetlb_lock);
 	update_and_free_pages_bulk(h, &page_list);
 	spin_lock_irq(&hugetlb_lock);

---
base-commit: c8117bff684306f5c1f959d055cc2b24dd67b1eb
change-id: 20261125-hugetlb-subpool-release-ba8e9e7005dd

-- 
Thanks,
Aveline Noir



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27  9:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  8:28 [PATCH] mm/hugetlb: serialize subpool release with folio reserve restoration Aveline Noir
2026-11-27  9:37 ` Jose A. Perez de Azpillaga

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox