* [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
` (2 more replies)
0 siblings, 3 replies; 4+ 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] 4+ messages in thread
* Re: [PATCH] mm/hugetlb: serialize subpool release with folio reserve restoration
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
2026-12-02 2:00 ` Andrew Morton
2026-12-02 2:13 ` + mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch added to mm-new branch Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-27 9:37 UTC (permalink / raw)
To: Aveline Noir, Muchun Song, Oscar Salvador, Andrew Morton
Cc: David Hildenbrand, Mike Rapoport, Mike Kravetz, linux-mm
On 11/25/26 9:28 AM, Aveline Noir wrote:
> 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.
>
makes sense to me.
> 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>
LGTM so:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
--
cheers,
jose a. p-a
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/hugetlb: serialize subpool release with folio reserve restoration
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
@ 2026-12-02 2:00 ` Andrew Morton
2026-12-02 2:13 ` + mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch added to mm-new branch Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-02 2:00 UTC (permalink / raw)
To: Aveline Noir
Cc: Muchun Song, Oscar Salvador, David Hildenbrand, Mike Rapoport,
Mike Kravetz, linux-mm
On Wed, 25 Nov 2026 19:28:49 +1100 Aveline Noir <jm5905938@gmail.com> wrote:
> 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.
Well that's not ideal. Is this actually reproducible? The Fixes:
target was 11 years ago!
Seems this happens when a few very rare things are done at the same
time, one of which is hugetlbfs unmount, which nobody is going to do
while there's a huge amount of traffic in flight?
> 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.
Thing is, the worst-case spinlocked-irqs-off region on the final folio
put is now nr-surplus-hugepages * nr-nodes-inspected plus
nr-surplus-pages of freeing.
<ai>
For example, with a 1 TiB minimum and 2 MiB hugepages, N could be
524,288 if that many surplus pages are available. On a 64-node
machine, the upper bound is about 34 million node checks.
</ai>
AI guesstimate: order of a second, possibly several seconds.
That isn't fatal - it's well within the watchdog timeouts. But it
isn't good! On big iron a lot of resources may be piled up on
hugetlb_lock.
I'm not seeing a good solution. There's always our usual cop-out: give
up and punt it to a workqueue. The problem I have with this practice
is that it permits unlimited resource windup - we punt more and more
stuff over to the worker (because we couldn't figure out the locking)
while permitting resource usage to go up and up and up. If <things>
cause the worker to fail to keep up then boom: out of resources.
IOW, if we have resources to release then we should do it right now,
right here synchronously in this thread, ASAP. But not by doing N*M
operations under local_irq_disable()! And that number is just going to
increase as machines get larger.
Ho hum, Your patch replaces awful buggy behavior with undesirable
corner-case behavior so that's an improvement.
^ permalink raw reply [flat|nested] 4+ messages in thread
* + mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch added to mm-new branch
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
2026-12-02 2:00 ` Andrew Morton
@ 2026-12-02 2:13 ` Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-02 2:13 UTC (permalink / raw)
To: mm-commits, rppt, osalvador, muchun.song, david, azpijr,
jm5905938, akpm
The patch titled
Subject: mm/hugetlb: serialize subpool release with folio reserve restoration
has been added to the -mm mm-new branch. Its filename is
mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews. Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Aveline Noir <jm5905938@gmail.com>
Subject: mm/hugetlb: serialize subpool release with folio reserve restoration
Date: Wed, 25 Nov 2026 19:28:49 +1100
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.
Link: https://patch.msgid.link/20261125-hugetlb-subpool-release-v1-1-7398c24d09b0@gmail.com
Fixes: 7ca02d0ae586 ("hugetlbfs: accept subpool min_size mount option and setup accordingly")
Signed-off-by: Aveline Noir <jm5905938@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: David Hildenbrand <david@kernel.org>
Cc: Mike Rapoport <rppt@kernel.org>
mm/hugetlb.c | 158 +++++++++++++++++++++++++++++++++----------------
1 file changed, 107 insertions(+), 51 deletions(-)
--- a/mm/hugetlb.c~mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration
+++ a/mm/hugetlb.c
@@ -128,6 +128,9 @@ struct mutex *hugetlb_fault_mutex_table
/* 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);
@@ -168,7 +171,7 @@ static inline void unlock_or_release_sub
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);
@@ -265,21 +268,17 @@ unlock_ret:
* 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;
@@ -291,6 +290,20 @@ static long hugepage_subpool_put_pages(s
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.
@@ -300,6 +313,28 @@ static long hugepage_subpool_put_pages(s
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));
@@ -1764,6 +1799,9 @@ void free_huge_folio(struct folio *folio
struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
struct folio *surplus_folio = NULL;
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);
@@ -1784,18 +1822,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);
@@ -1809,37 +1851,44 @@ 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);
- return;
+ 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);
- return;
- }
+ 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);
- 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);
+ /*
+ * This is not a surplus folio on this node, but we might have
+ * surplus folios on other nodes that are no longer required,
+ * as reservations are global and surplus folios are per node.
+ * So free another folio instead.
+ */
+ if (h->surplus_huge_pages &&
+ h->free_huge_pages > h->resv_huge_pages)
+ surplus_folio = remove_pool_hugetlb_folio(h,
+ &node_states[N_MEMORY],
+ /* acct_surplus= */ true);
+ if (surplus_folio)
+ list_add(&surplus_folio->lru, &page_list);
+ }
- /*
- * This is not a surplus folio on this node, but we might have surplus
- * folios on other nodes that are no longer required, as reservations
- * are global and surplus folios are per node. So free another folio
- * instead.
- */
- if (h->surplus_huge_pages &&
- h->free_huge_pages > h->resv_huge_pages)
- surplus_folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY],
- /* acct_surplus= */ true);
+ if (free_subpool)
+ collect_unused_surplus_pages(h, spool->min_hpages, &page_list);
spin_unlock_irqrestore(&hugetlb_lock, flags);
+
/* We might be in atomic context, but freeing might need vmemmap pages. */
- if (surplus_folio)
- update_and_free_hugetlb_folio(h, surplus_folio, true);
+ 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);
}
/*
@@ -2522,26 +2571,18 @@ free:
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
@@ -2563,12 +2604,27 @@ static void return_unused_surplus_pages(
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);
_
Patches currently in -mm which might be from jm5905938@gmail.com are
mm-hmm-account-for-subpage-offsets-in-device-private-pmds.patch
mm-damon-sysfs-skip-registration-if-damon-core-init-fails.patch
mm-damon-ops-common-align-mmu-notifier-range-when-clearing-pmd-young-bit.patch
mm-damon-stat-avoid-32-bit-overflow-in-idle-percentiles.patch
mm-damon-pvaddr-skip-folios-already-on-the-migration-target-node.patch
docs-admin-guide-mm-damon-start-fix-grammar.patch
docs-admin-guide-mm-damon-stat-fix-pronoun-agreement.patch
docs-admin-guide-mm-damon-usage-fix-grammar.patch
docs-admin-guide-mm-damon-lru_sort-fix-remaining-grammar.patch
docs-mm-damon-design-fix-grammar-and-typos.patch
docs-mm-damon-api-fix-verb-form.patch
docs-mm-damon-maintainer-profile-fix-subject-verb-agreement.patch
docs-mm-damon-monitoring_intervals_tuning_example-fix-grammar-and-typos.patch
mm-damon-paddr-add-kunit-tests-for-addr_unit-conversion.patch
mm-damon-paddr-add-kunit-tests-for-addr_unit-conversion-fix.patch
mm-damon-paddr-skip-non-lru-folios-as-a-whole-in-pageout.patch
mm-damon-core-skip-apply_probes-when-no-probes-are-installed.patch
selftests-damon-damos_quota-skip-the-last-sample-when-counting-exceeds.patch
mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 2:13 UTC | newest]
Thread overview: 4+ 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
2026-12-02 2:00 ` Andrew Morton
2026-12-02 2:13 ` + mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch added to mm-new branch Andrew Morton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.