From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796177597; b=r8bQiUloj9PIP+IKP9HXzmAa9s0c1in5c1zOixRpB80dd2a8joY8q8Kl+2sOMUGXFadC EkLCAmnWLEIHDQbFCYVs36qg2JaMm+/0DVi+/ctlphuzSqfdiCT1pjyJWpQbVMB9QfXUQ +Fpl5QLEe0xBDVbBnX1FzbSZ+ZlU2YogQ/osZOoDVNKRF09QKAQ7/BR66OrESZGXG4ZTb NLIphBipRgwbaaYH8PMH3Gyvc8KcmEscSAbcCbWxlt9jyNz0E0y/lblTRKrwCsUvFTcCY sHxvoO/MaYP869oBuLb+9xTJuqpuSh7j8t8Qibs+CdBMtFciCke8G2W9eDu8GaBw09g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796177597; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=PqZ5n1ehWB2YOoCT7GFGRATxXzmMzB5b7E9nIyuDs5Q=; b=tnWI0/krjDKtlrVk1uBdKlFg7L2iZUeyuyROISBYK/d/fCdQkMyWioZqGbrUalotla3h Lb/h8JhbYQ4Oa+Z/fOrfV++PXn1UT7jQsYkt9QAegGkw3VGCe0aV4nrvDW5bM+RZxFAAZ 3XS07H92BwsuFiRyAJBuALIrKoUndJkAyOP8I0BKG9dWAp6mOaUu11S3Cqcjlg7fXmK5K fCvggQEPReknY0mvSI56wfV4raiOvHUOgbkSADBZzupq4m37Kh/2ffqNLT02Hx2yDEcwN SySVDHM+2Fm2OtXXwbLml4nfK4mHc+EAeT8bxz/W2i+fRIc//umsHOqmSvDSROP6pBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=NFJpCfV5; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CCC3606A31A for ; Wed, 2 Dec 2026 02:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=NFJpCfV5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 074DB1F000C1; Wed, 2 Dec 2026 02:13:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796177597; bh=PqZ5n1ehWB2YOoCT7GFGRATxXzmMzB5b7E9nIyuDs5Q=; h=Date:To:From:In-Reply-To:References:Subject; b=NFJpCfV5dQA75x5cjvxee3faukVrFX32dComFSQ+eaImzJnUxfVtM/xPyThmp3f1z BI6jWSzeO5bM7gpgOjrLVDp3/dSjDYpK1DHSYov2S/gad+UHq9YRtk655yYq4qeEbc 3ya80BvNj3lVVjW3M63nXJIJ8kXbZosb/tAO2x1U= Date: Tue, 01 Dec 2026 18:13:16 -0800 To: mm-commits@vger.kernel.org,rppt@kernel.org,osalvador@suse.de, muchun.song@linux.dev,david@kernel.org,azpijr@gmail.com,jm5905938@gmail.com, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261125-hugetlb-subpool-release-v1-1-7398c24d09b0@gmail.com> References: <20261125-hugetlb-subpool-release-v1-1-7398c24d09b0@gmail.com> Subject: + mm-hugetlb-serialize-subpool-release-with-folio-reserve-restoration.patch added to mm-new branch Message-Id: <20261202021317.074DB1F000C1@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 Signed-off-by: Andrew Morton Reviewed-by: Jose A. Perez de Azpillaga Cc: Muchun Song Cc: Oscar Salvador Cc: David Hildenbrand Cc: Mike Rapoport 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