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=1796325189; b=qK4n9GW1JJ9KUzzzFb7pk7h+0+YOGAdYE/b+LaXIzr2qCctgWTThkxENyRW+F1FP6jZ0 Ql/H7fd7E8w7SegaxYoLrNZoKggVtzYvJe/slqUtWI0lQbKLUMUWWvrZUtvTGfERQyG5T zMogf4Zs2OSjrcgmVpzRFz0nOhxZwDsKPYxv2+ShAWDuwZ+RBmNnwMk11cgeWZbRKYoKf BUFV3Dh9adj3tIsGfNlDA+vZqzRHqdOMulZ0oPk2qz5Q5/FxE9xM5MAsYbQJE8+LFBKRZ 99Ir6/lEFlQ87yp98srb/5t6CbeC/WNBwbKqDVqquNmqUCVsXIuqgwwWdAd8e6ZTL6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325189; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=U0jtguS+c3854nBuYffF5tp9fBNqrC4g66z9claSuwg=; b=bVxtKbDaIH9MEf3ViBn13c6ynhU7G2VeqJoURNdzx25Il6M6rrfTO+xo7cO5usD0pmKS jY3v92agStdKHq1Rrzohc9u5wwtq8lDC4jyWMXdNgvaND7cHrrZuHnZffbLJyXDQCzKG/ QpOzkFO5mX38osoiV9mK7D0Yzv37O/nPrQ+Ax42rcR5LWhPQeo0duh/VOHhZcgHV5a33l Eq7WopGamOUXUk1OqsPt8VI/0F6geA/DjY7SK3WeL27NTcQRlWIWwRVevlEXhPTmeAA+U iCRwng0EtdV3oqWS0tsKHgsnHqLVSbiZ/7RBGryUSBUa1VD0f3WbFVih6B3cooaKxGg== 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=vEbnpfcH; 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 8DDA6606A322 for ; Thu, 3 Dec 2026 19:13:09 +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=vEbnpfcH Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4557C1F00A1D; Thu, 3 Dec 2026 19:13:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796325189; bh=U0jtguS+c3854nBuYffF5tp9fBNqrC4g66z9claSuwg=; h=Date:To:From:In-Reply-To:References:Subject; b=vEbnpfcHzps95hc/nzMQXvSbLk4aad/A+wJ/YemtZFQ7ciqdpCm8FPDoMJ9S2VL1z 0lUQv1ksygb4L6Qx0JOV6RC48eMcTli1+Ce6f5nvREO+XVAIgTT4uElG844w7ZMGkM 1QKSdA+YdJlN1MKaqJtYGtLTzlFAQ/JBdfFeaHHM= Date: Thu, 03 Dec 2026 11:13:08 -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-2-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-2-9bbeabb555d2@kernel.org> Subject: + mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch added to mm-unstable branch Message-Id: <20261203191309.4557C1F00A1D@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: restore the reservation if vma_needs_reservation() fails has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.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: restore the reservation if vma_needs_reservation() fails Date: Thu, 03 Dec 2026 12:37:25 +0100 Patch series "mm/hugetlb: various fixes (2)", v2. Most of the new ones are from Sashiko reports, and some found while working on them. Chris reported two more. I was able to reproduce and test most of them. - Patch #1: hotfix for v7.4 (already in mm-hotfixes-stable) - Patch #2 - #6: reservation handling fixes - Patch #7 - #11: folio allocation and freeing fixes - Patch #12 - #13: userfaultfd and soft-dirty fixes - Patch #14 - #25: truncation, hole punching, fallocate() and inode fixes - Patch #26 - #30: PMD table sharing and VMA lock fixes - Patch #31 - #33: accounting fixes - Patch #34: a hugetlb cgroup fix - Patch #35: a cleanup / hardening patch This patch (of 34): When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet, for example because the mapping was created with MAP_NORESERVE, alloc_hugetlb_folio() is responsible for performing the hugetlb 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() will call vma_needs_reservation(), which can fail to allocate a file_region in corner cases. In that case, we will leave the reserve map unchanged and free the folio without HPG_restore_reserve. Consequently, nothing backs the reservation. The same happens on a min_size= mount if alloc_hugetlb_folio() took the reservation from the subpool minimum: the folio then has HPG_restore_reserve, but we clear it when vma_needs_reservation() fails. When we later try again to allocate+populate the page cache, we will consume the reservation for that index in the reserve map, although we never accounted for a reservation. This will underflow resv_huge_pages and the subpool usage. Reproduced both cases with fault-injection, failing the file_region allocation in restore_reserve_on_error() when the first UFFDIO_COPY attempt into a MAP_SHARED|MAP_NORESERVE mapping fails: (1) memfd, after the retry: HugePages_Rsvd: 0 -> 18446744073709551615 (2) hugetlbfs mount with min_size=, after unmounting: HugePages_Rsvd: 0 -> 18446744073709551615 Let's fix it by setting or keeping HPG_restore_reserve for folios in shared mappings in this unlikely error case, such that free_huge_folio() will restore the reservation. Restructure the code to make it at least a little bit easier to understand. This has low urgency, as the file_region allocation is very unlikely to fail. Link: https://lore.kernel.org/20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org Link: https://patch.msgid.link/20261203-hugetlb_fixes_2-v2-2-9bbeabb555d2@kernel.org Fixes: 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality") 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: mm/hugetlb.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) --- a/mm/hugetlb.c~mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails +++ a/mm/hugetlb.c @@ -2732,21 +2732,27 @@ void restore_reserve_on_error(struct hst { long rc = vma_needs_reservation(h, vma, address); - if (folio_test_hugetlb_restore_reserve(folio)) { - if (unlikely(rc < 0)) - /* - * Rare out of memory condition in reserve map - * manipulation. Clear hugetlb_restore_reserve so - * that global reserve count will not be incremented - * by free_huge_folio. This will make it appear - * as though the reservation for this folio was - * consumed. This may prevent the task from - * faulting in the folio at a later time. This - * is better than inconsistent global huge page - * accounting of reserve counts. - */ + if (unlikely(rc < 0)) { + /* + * Rare out of memory condition in reserve map manipulation. + * + * In shared mappings, the reserve map has the reservation for + * this index, so let folio freeing code restore it. + * + * Otherwise, don't restore the reservation, making it appear + * as though it was consumed. This may prevent the task from + * faulting in the folio at a later time, but is better than + * inconsistent global huge page accounting of reserve counts. + */ + if (vma->vm_flags & VM_MAYSHARE) + folio_set_hugetlb_restore_reserve(folio); + else folio_clear_hugetlb_restore_reserve(folio); - else if (rc) + return; + } + + if (folio_test_hugetlb_restore_reserve(folio)) { + if (rc) (void)vma_add_reservation(h, vma, address); else vma_end_reservation(h, vma, address); @@ -2771,11 +2777,6 @@ void restore_reserve_on_error(struct hst * on a subsequent allocation. */ folio_set_hugetlb_restore_reserve(folio); - } else if (rc < 0) { - /* - * Region cache allocation can fail even if an entry exists. - * Leave the flag and reserve map unchanged. - */ } else { /* * No reservation present, do nothing _ 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