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=1796325194; b=f5CJtSsu4pGnTu6FdE3aXyGG4r3WFW2Q2Ajh5mGwlKSWQLWcL3WNm9U4+zFd8CCgcRwN AGjZAkYtkixPXbX9Xluu87rIqynJ1yc2xB3SGR4zHeeGED61XyQXNdqDsfWvBLWAxA55M 5NcPxbapdqSjhmcbzVJ07Ziik+Gop77BiNol72aKccXatB66dAtA0K0/kJRQp/t52qVGj PbXD3qE4o/4+TKmrKKPFzbMdKvGQR3siji5dwTJc8l5w67pos1VJscq8RP3rr5Dz/h94R 1n5nHGhqv7ZSoIg8TADhB4ixVmZMGa1xxLaCGpMGNrTOKmiaWYD1LcyOQNAkml9d9SA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325194; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=CaeriWE4MxCepKayVD7nlSrIZrhxorohLh1r9Q6MxmQ=; b=QhoScKdqSRPKUCo8vZ6DOfBVYGR2uvGRq+5Kp146RSdPtbSkbMWEyN7t1I6fQ0hez2N6 Dx5/+Fix67uyEEEaCbtnOYw+9wNwG9/X8YpuNhO5C0swgBE/nNoo3mqH4FcUtXZNRRMaa OYqwlr0aIDQx9ulMw/pGnjOAo6xKBIH4j6iq5xX5wEryd+RraH+IPEhne+E0wlTu6qjWr yJ57gy2GWgBter5qFnat/UMpD3fKEN+aH67GE8aXlwYfFA6xKk+F3ilJkAxrlYOnC5+SI 8nPdUDxEVSK6tybh6ASn3iKdXbvbvZ7ae0FpY6m4hpku5g5YSv4AAK8LLSG7ubH/9kg== 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=N+lpjIu8; 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 95165606A33A for ; Thu, 3 Dec 2026 19:13:14 +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=N+lpjIu8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13CEC1F0093A; Thu, 3 Dec 2026 19:13:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796325194; bh=CaeriWE4MxCepKayVD7nlSrIZrhxorohLh1r9Q6MxmQ=; h=Date:To:From:In-Reply-To:References:Subject; b=N+lpjIu8yfVZLIgVoQ10ZODlnI80MJpor2wv8Byycwhs9bEURGmmNZz9LRZQXsAB6 voBKHlhzp4By0E62XrQVTCygj6w9ue2s9PLbMCTPi8JbiK5RGThWNHTwXijhhz4xFv kJAkff95Ym4nNgIKhx8cpouY354u9j/Zp81CyGyw= Date: Thu, 03 Dec 2026 11:13:13 -0800 To: mm-commits@vger.kernel.org,david@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261203-hugetlb_fixes_2-v2-4-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-4-9bbeabb555d2@kernel.org> Subject: + mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch added to mm-unstable branch Message-Id: <20261203191314.13CEC1F0093A@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: return the reservation to the subpool on error has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm 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: "David Hildenbrand (Arm)" Subject: mm/hugetlb: return the reservation to the subpool on error Date: Thu, 03 Dec 2026 12:37:27 +0100 When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet on a min_size= mount, alloc_hugetlb_folio() can take the reservation from the subpool minimum, and we perform the reservation for the folio's index in the reserve map. If something goes wrong before adding the folio to the page cache, restore_reserve_on_error() is supposed to undo the reservation in the reserve map, and free_huge_folio() is supposed to return the reservation to the subpool, like we do for CoW mappings already. However, as the folio consumed a global reservation, it has HPG_restore_reserve set. So restore_reserve_on_error() keeps the reservation in the reserve map, and free_huge_folio() restores the global reservation instead of returning it to the subpool. Consequently, the reservation from the subpool minimum is stuck at that index until truncation. As vma_commit_reservation() does not charge the hugetlb cgroup reservation, a later fault at that index will consume it without a cgroup charge. Reproduced with a failing UFFDIO_COPY on a min_size= mount, followed by a fault at the same index, inside a hugetlb cgroup: hugetlb.2MB.rsvd.current: 0 instead of 2097152 Let's fix it by clearing HPG_restore_reserve in shared mappings as well. On success, adding the folio to the page cache clears the flag. Note that we have to clear the flag before vma_commit_reservation() now, such that the race handling can set it again when we raced with another reservation. This has low urgency, as it only affects the subpool minimum and the hugetlb cgroup reservation accounting on min_size= mounts. Link: https://patch.msgid.link/20261203-hugetlb_fixes_2-v2-4-9bbeabb555d2@kernel.org Fixes: 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools") Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Reported-by: Sashiko Cc: Muchun Song Cc: Oscar Salvador Cc: Hongfu Li Cc: Jinmeng Zhou Cc: Mina Almasry Cc: Davidlohr Bueso Cc: Naoya Horiguchi Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Vivek Kasireddy Cc: Mel Gorman Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Jason Gunthorpe Cc: Andy Whitcroft Cc: Roman Gushchin Cc: Barry Song Cc: Andrea Arcangeli Cc: Lorenzo Stoakes Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato Cc: Jann Horn Cc: Guillaume Morin Cc: Yang Shi Cc: James Houghton Cc: Rik van Riel Cc: Harry Yoo Cc: Lance Yang Cc: Axel Rasmussen Cc: Peter Xu Cc: Sasha Levin Cc: Kiryl Shutsemau Cc: Christian Brauner Cc: Steven Rostedt Cc: Hongbo Li Cc: Gregory Price Cc: Zi Yan Cc: Joshua Hahn Cc: Rakie Kim Cc: Ying Huang Cc: Matthew Brost Cc: Byungchul Park Cc: Alistair Popple Cc: Andi Kleen Cc: David Rientjes Cc: Joern Engel Cc: Aneesh Kumar K.V Cc: Gerd Hoffmann Cc: Johannes Weiner Cc: Chris Mason Cc: Jiaqi Yan Cc: Documentation/mm/hugetlbfs_reserv.rst | 4 ++-- mm/hugetlb.c | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) --- a/Documentation/mm/hugetlbfs_reserv.rst~mm-hugetlb-return-the-reservation-to-the-subpool-on-error +++ a/Documentation/mm/hugetlbfs_reserv.rst @@ -257,8 +257,8 @@ compensate. See the section :ref:`Reservation Map Helper Routines ` for more information on these routines. -For CoW mappings, alloc_hugetlb_folio() calls -folio_clear_hugetlb_restore_reserve() if the reservation came from the subpool. +alloc_hugetlb_folio() calls folio_clear_hugetlb_restore_reserve() if the +reservation came from the subpool. This makes free_huge_folio() return the reservation to the subpool on error. --- a/mm/hugetlb.c~mm-hugetlb-return-the-reservation-to-the-subpool-on-error +++ a/mm/hugetlb.c @@ -2724,7 +2724,9 @@ static long vma_del_reservation(struct h * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio. * * For CoW mappings, allocations using a subpool reservation leave the - * flag clear and the reserve map unchanged. free_huge_folio returns the + * flag clear and the reserve map unchanged. The same applies to shared + * mappings, except that case 2 removes the reservation from the reserve map, + * unless updating the reserve map fails. free_huge_folio returns the * reservation to the subpool. */ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, @@ -3202,6 +3204,18 @@ static struct folio *__alloc_hugetlb_fol hugetlb_set_folio_subpool(folio, spool); + /* + * On error, let folio freeing code return the reservation to the + * subpool, unless we consumed a reservation from the reserve map. + * + * In private mappings, the reserve map might not exist or already + * account for another folio at this index. In shared mappings, + * restore_reserve_on_error() will remove the reservation we are about + * to add to the reserve map, unless updating the reserve map fails. + */ + if (map_chg != MAP_CHG_REUSE) + folio_clear_hugetlb_restore_reserve(folio); + if (map_chg != MAP_CHG_ENFORCED) { /* commit() is only needed if the map_chg is not enforced */ retval = vma_commit_reservation(h, vma, addr); @@ -3232,14 +3246,6 @@ static struct folio *__alloc_hugetlb_fol } } - /* - * Return subpool reservations to the subpool on error. A CoW mapping - * might not have a reserve map, or it might already account for - * another folio at this index. - */ - if (map_chg != MAP_CHG_REUSE && vma_is_cow_mapping(vma)) - folio_clear_hugetlb_restore_reserve(folio); - return folio; out_subpool_put: _ Patches currently in -mm which might be from david@kernel.org are mm-mprotect-dont-skip-uffd-marker-install-when-racing-with-pte-table-reclaim.patch mm-userfaultfd-prevent-poison-marker-loss.patch mm-hugetlb-compare-against-persistent-count-when-freeing-lowmem-folios.patch mm-hugetlb-preserve-reservations-when-shrinking-on-highmem.patch mm-hugetlb-account-temporary-folios-separately.patch mm-hugetlb-account-temporary-folios-separately-fix.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation-fix.patch mm-memfd-recheck-the-page-cache-before-allocating-a-hugetlb-folio.patch mm-hugetlb-count-again-if-the-reserve-map-shrank.patch mm-hugetlb-account-restored-reservations-before-freeing-folios.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush.patch mm-userfaultfd-reject-dax-mappings.patch mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation.patch mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.patch mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch mm-memfd-mark-new-hugetlb-folios-migratable.patch mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.patch mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.patch fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch hugetlbfs-fix-integer-overflow-when-punching-holes.patch hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.patch hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.patch mm-hugetlb-serialize-private-faults-with-truncation.patch mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.patch hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.patch mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.patch mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch