Skip to contentSkip to conversation

mm/hugetlb: various fixes (2)

PATCH In review David Hildenbrand (Arm) 7 participants 134 messages Dec 3 – Dec 4 Page 1 of 2

Subsystems (13)
lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

v2 cover mm/hugetlb: various fixes (2)

Hi,

this is v2 with a bunch more fixes, which I didn't want to send separately to not cause a mess on the list. Most of the new ones are from Sashiko reports, and some found while working on them. Chris reported two more.

This is likely the last hugetlb fixes series I'll be sending; I'll delegate from now on :)

I was able to reproduce and test most of them.

Based on the same base as v1.

- 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

I un-RFCed #34 and dropped the controversial VMA-lock optimization fix.

Nothing here is urgent. A lot are low-priority issues, but we might just fix them in bulk instead of getting random fixes for these things upstream.

---
Changes in v2:
- Collected tags (thanks!).
- Dropped "mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing".
- "mm/hugetlb_cgroup: charge faults to mm->owner"
- Dropped the RFC label.
- Fall back to current->mm like memcg, and also charge reservations to
the mm of the VMA.
- Minor patch description fixups.
- Added new fixes:
- "mm/hugetlb: restore the reservation if vma_needs_reservation() fails"
- "mm/hugetlb: restore the reservation when racing with another reservation"
- "mm/hugetlb: return the reservation to the subpool on error"
- "mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()"
- "mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking"
- "mm/hugetlb: wait for deferred frees before failing an allocation"
- "mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it"
- "hugetlbfs: fix integer overflow when punching holes"
- "hugetlbfs: don't overflow the pseudo VMA address in fallocate()"
- "hugetlbfs: don't overflow the end of the unmap range on 32-bit"
- "hugetlbfs: don't skip private VMAs when unmapping on truncation"
- "mm/hugetlb: serialize private faults with truncation"
- "mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON"
- "hugetlbfs: zero all hugetlbfs-specific fields of new inodes"
- "mm/rmap: don't fail unmapping hugetlb folios from private VMAs"
- Reordered the patches by topic.
- Link to v1: https://patch.msgid.link/[email protected]

To: Muchun Song <[email protected]>
To: Oscar Salvador <[email protected]>
To: Andrew Morton <[email protected]>
To: Hongfu Li <[email protected]>
To: Jinmeng Zhou <[email protected]>
To: Mina Almasry <[email protected]>
To: Davidlohr Bueso <[email protected]>
To: Naoya Horiguchi <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Vlastimil Babka <[email protected]>
To: Mike Rapoport <[email protected]>
To: Michal Hocko <[email protected]>
To: Jonathan Corbet <[email protected]>
To: Shuah Khan <[email protected]>
To: Randy Dunlap <[email protected]>
To: Vivek Kasireddy <[email protected]>
To: Mel Gorman <[email protected]>
To: Hugh Dickins <[email protected]>
To: Baolin Wang <[email protected]>
To: Dave Airlie <[email protected]>
To: Jason Gunthorpe <[email protected]>
To: Andy Whitcroft <[email protected]>
To: Roman Gushchin <[email protected]>
To: Barry Song <[email protected]>
To: Andrea Arcangeli <[email protected]>
To: Lorenzo Stoakes <[email protected]>
To: Suren Baghdasaryan <[email protected]>
To: Shakeel Butt <[email protected]>
To: Pedro Falcato <[email protected]>
To: Jann Horn <[email protected]>
To: Guillaume Morin <[email protected]>
To: Yang Shi <[email protected]>
To: James Houghton <[email protected]>
To: Rik van Riel <[email protected]>
To: Harry Yoo <[email protected]>
To: Lance Yang <[email protected]>
To: Axel Rasmussen <[email protected]>
To: Peter Xu <[email protected]>
To: Sasha Levin <[email protected]>
To: Kiryl Shutsemau <[email protected]>
To: Christian Brauner <[email protected]>
To: "Steven Rostedt (Google)" <[email protected]>
To: Hongbo Li <[email protected]>
To: Gregory Price <[email protected]>
To: Zi Yan <[email protected]>
To: Joshua Hahn <[email protected]>
To: Rakie Kim <[email protected]>
To: Ying Huang <[email protected]>
To: Matthew Brost <[email protected]>
To: Byungchul Park <[email protected]>
To: Alistair Popple <[email protected]>
To: Andi Kleen <[email protected]>
To: David Rientjes <[email protected]>
To: Joern Engel <[email protected]>
To: "Aneesh Kumar K.V" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: Gerd Hoffmann <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: Johannes Weiner <[email protected]>
Cc: Chris Mason <[email protected]>
Cc: Jiaqi Yan <[email protected]>

---
Chris Mason (8):
mm/hugetlb: fix truncated copy result in UFFDIO_COPY
hugetlbfs: fix deadlock between hole punching and memory failure
hugetlbfs: recheck the vma range after the lock wait mm/hugetlb: don't zap non-present entries when unmapping a folio
hugetlbfs: seal SHM_HUGETLB files against growth mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails mm/hugetlb_cgroup: track in-use folios per cgroup mm/hugetlb: resolve protnone faults without a uffd bit

David Hildenbrand (Arm) (27):
mm/hugetlb: charge the copy made at fork to the parent
mm/hugetlb: restore the reservation if vma_needs_reservation() fails mm/hugetlb: restore the reservation when racing with another reservation mm/hugetlb: return the reservation to the subpool on error mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages() mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking
mm/hugetlb: always mark new page cache folios migratable
mm/memfd: mark new hugetlb folios migratable
mm/hugetlb: free surplus folios of other nodes on free
mm/hugetlb: wait for deferred frees before failing an allocation mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs
hugetlbfs: fix integer overflow when punching holes
hugetlbfs: don't zero poisoned pages in a hole punch
hugetlbfs: don't overflow the pseudo VMA address in fallocate()
hugetlbfs: don't overflow the end of the unmap range on 32-bit
hugetlbfs: don't skip private VMAs when unmapping on truncation mm/hugetlb: serialize private faults with truncation
mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON
hugetlbfs: zero all hugetlbfs-specific fields of new inodes
mm/rmap: don't fail unmapping hugetlb folios from private VMAs mm/hugetlb: only check PMD table sharing for PMD-sized folios mm/migrate: check hugetlb PMD sharing in move_pages()
mm/hugetlb: don't share page tables of a VMA during mprotect() mm/hugetlb: count migration entries at fork and unmap
mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared mm/hugetlb_cgroup: charge faults to mm->owner

 Documentation/filesystems/proc.rst    |   3 +-
 Documentation/mm/hugetlbfs_reserv.rst |   4 +-
 fs/hugetlbfs/inode.c                  |  64 ++++---
 fs/proc/task_mmu.c                    |   9 +-
 include/linux/hugetlb.h               |  22 ++-
 include/linux/hugetlb_cgroup.h        |  30 +++-
 include/linux/leafops.h               |  11 ++
 mm/hugetlb.c                          | 330 +++++++++++++++++++++++-----------
 mm/hugetlb_cgroup.c                   |  66 ++++---
 mm/hugetlb_cma.c                      |   1 +
 mm/memfd.c                            |   1 +
 mm/mempolicy.c                        |   2 +-
 mm/migrate.c                          |  23 ++-
 mm/rmap.c                             |  12 +-
 mm/vma.c                              |   2 +-
 tools/testing/vma/include/stubs.h     |   3 +-
 16 files changed, 389 insertions(+), 194 deletions(-)

---
base-commit: c8117bff684306f5c1f959d055cc2b24dd67b1eb
change-id: 20261120-hugetlb_fixes_2-26455c584f4e

Best regards,

Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/35 mm/hugetlb: charge the copy made at fork to the parent

When copying page tables during fork(), the child is not yet assigned to the right memcg. When copying possibly pinned anon hugetlb folios, we must charge them to the parent's memcg, otherwise we can end up allocating more hugetlb folios than allowed.

Let's fix it by charging the copy to the parent's mm, like we do when copying small folios in copy_present_page(), introducing a new helper function alloc_hugetlb_folio_for_fork().

This is a hotfix for v7.4.

---
 mm/hugetlb.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
mm/hugetlb.c +22 -4
index d0e66fb306221..f46c85fc0cd0b 100644
@@ -3103,8 +3103,9 @@ typedef enum {
  * allocation).  New call sites should (probably) never set it to true!!
  * When it's set, the allocation will bypass all vma level reservations.
  */
-struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
-				    unsigned long addr, bool cow_from_owner)
+static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
+		unsigned long addr, bool cow_from_owner,
+		struct mm_struct *memcg_mm)
 {
 	struct hugepage_subpool *spool = subpool_vma(vma);
 	struct hstate *h = hstate_vma(vma);
@@ -3187,7 +3188,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
 		.nodemask = nodemask,
 	};
 
-	folio = hugetlb_alloc_folio(h, &mpoli, vma->vm_mm, alloc_flags);
+	folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
 
 	mpol_cond_put(mpol);
 
@@ -3249,6 +3250,23 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
 	return ERR_PTR(ret);
 }
 
+struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
+		unsigned long addr, bool cow_from_owner)
+{
+	return __alloc_hugetlb_folio(vma, addr, cow_from_owner, vma->vm_mm);
+}
+
+/*
+ * Allocate a folio for a copy in the child VMA during fork(), charged to the
+ * parent's mm: the child is not in its memory cgroup yet.
+ */
+static struct folio *alloc_hugetlb_folio_for_fork(struct vm_area_struct *dst_vma,
+		unsigned long addr, struct mm_struct *src_mm)
+{
+	return __alloc_hugetlb_folio(dst_vma, addr, /* cow_from_owner = */ false,
+				     src_mm);
+}
+
 static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact)
 {
 	if (hugetlb_early_cma(h))
@@ -5110,7 +5128,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
 				spin_unlock(src_ptl);
 				spin_unlock(dst_ptl);
 				/* Do not use reserve as it's private owned */
-				new_folio = alloc_hugetlb_folio(dst_vma, addr, false);
+				new_folio = alloc_hugetlb_folio_for_fork(dst_vma, addr, src);
 				if (IS_ERR(new_folio)) {
 					folio_put(pte_folio);
 					ret = PTR_ERR(new_folio);
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH v2 2/35 mm/hugetlb: restore the reservation if vma_needs_reservation() fails

In reply to David Hildenbrand (Arm)

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.

Fixes 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality")
---
 mm/hugetlb.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
mm/hugetlb.c +20 -19
index f46c85fc0cd0b..bf2265bc6eb71 100644
@@ -2730,21 +2730,27 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
 {
 	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);
@@ -2769,11 +2775,6 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
 				 * 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
Signature · 1 line
2.56.1
maintainer tree notice
#3 lore raw

+ mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch added to mm-unstable branch

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…

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)" <[email protected]>
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.

Fixes 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails +20 -19
@@ -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 [email protected] 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

Reviewed-by on v2 2/35
#4 lore raw

Re: [PATCH v2 02/35] mm/hugetlb: restore the reservation if vma_needs_reservation() fails

In reply to David Hildenbrand (Arm)
Quoted 39 lines · David Hildenbrand (Arm)

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.

Quoted 7 lines

This has low urgency, as the file_region allocation is very unlikely to fail.

Fixes 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#5 lore raw

PATCH v2 3/35 mm/hugetlb: restore the reservation when racing with another reservation

In reply to David Hildenbrand (Arm)

When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet, alloc_hugetlb_folio() can race with hugetlb_reserve_pages() reserving that index, for example, because another process mmap()s the same file without MAP_NORESERVE.

vma_commit_reservation() will then find the racing reservation in the reserve map after allocating the folio, and we consume that reservation
instead: we return our subpool charge, drop a global reservation and uncharge the cgroup reservation of the folio. However, we don't set HPG_restore_reserve on the folio.

If we then hit an error before the folio gets added to the page cache, restore_reserve_on_error() assumes that alloc_hugetlb_folio() performed the reservation in the reserve map and removes it, and free_huge_folio() doesn't restore the global reservation. The reservation of the racing mmap() is then gone, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying alloc_hugetlb_folio() right before vma_commit_reservation() during a failing UFFDIO_COPY, while another process mmap()s the same file: once all unreserved huge pages are in use, that process gets SIGBUS when touching its page.

Let's fix it by setting HPG_restore_reserve, so the error path keeps the reservation in the reserve map and free_huge_folio() restores the global reservation. On success, adding the folio to the page cache clears the flag.

This has low urgency, as it requires an error after racing with another mapping reserving the same index.

Fixes 33039678c8da ("mm/hugetlb: handle races in alloc_huge_page and hugetlb_reserve_pages")
---
 mm/hugetlb.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/hugetlb.c +5 -0
index bf2265bc6eb71..fbe79ae4c0f3c 100644
@@ -3222,6 +3222,11 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
 			hugetlb_cgroup_uncharge_folio_rsvd(
 			    hstate_index(h), pages_per_huge_page(h), folio);
 			spin_unlock_irq(&hugetlb_lock);
+			/*
+			 * We consumed that existing reservation and must
+			 * restore it when freeing the folio due to errors.
+			 */
+			folio_set_hugetlb_restore_reserve(folio);
 		}
 	}
 
Signature · 1 line
2.56.1
maintainer tree notice
#6 lore raw

+ mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: restore the reservation when racing with another reservation has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: restore the reservation when racing with another reservation
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation.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-when-racing-with-another-reservation.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)" <[email protected]>
Subject: mm/hugetlb: restore the reservation when racing with another reservation
Date: Thu, 03 Dec 2026 12:37:26 +0100

When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet, alloc_hugetlb_folio() can race with hugetlb_reserve_pages() reserving that index, for example, because another process mmap()s the same file without MAP_NORESERVE.

vma_commit_reservation() will then find the racing reservation in the reserve map after allocating the folio, and we consume that reservation
instead: we return our subpool charge, drop a global reservation and uncharge the cgroup reservation of the folio. However, we don't set HPG_restore_reserve on the folio.

If we then hit an error before the folio gets added to the page cache, restore_reserve_on_error() assumes that alloc_hugetlb_folio() performed the reservation in the reserve map and removes it, and free_huge_folio() doesn't restore the global reservation. The reservation of the racing mmap() is then gone, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying alloc_hugetlb_folio() right before vma_commit_reservation() during a failing UFFDIO_COPY, while another process mmap()s the same file: once all unreserved huge pages are in use, that process gets SIGBUS when touching its page.

Let's fix it by setting HPG_restore_reserve, so the error path keeps the reservation in the reserve map and free_huge_folio() restores the global reservation. On success, adding the folio to the page cache clears the flag.

This has low urgency, as it requires an error after racing with another mapping reserving the same index.

Fixes 33039678c8da ("mm/hugetlb: handle races in alloc_huge_page and hugetlb_reserve_pages")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |    5 +++++
 1 file changed, 5 insertions(+)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation +5 -0
@@ -3224,6 +3224,11 @@ static struct folio *__alloc_hugetlb_fol
 			hugetlb_cgroup_uncharge_folio_rsvd(
 			    hstate_index(h), pages_per_huge_page(h), folio);
 			spin_unlock_irq(&hugetlb_lock);
+			/*
+			 * We consumed that existing reservation and must
+			 * restore it when freeing the folio due to errors.
+			 */
+			folio_set_hugetlb_restore_reserve(folio);
 		}
 	}
 

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 3/35
#7 lore raw

Re: [PATCH v2 03/35] mm/hugetlb: restore the reservation when racing with another reservation

In reply to David Hildenbrand (Arm)
Quoted 32 lines · David Hildenbrand (Arm)

When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet, alloc_hugetlb_folio() can race with hugetlb_reserve_pages() reserving that index, for example, because another process mmap()s the same file without MAP_NORESERVE.

vma_commit_reservation() will then find the racing reservation in the reserve map after allocating the folio, and we consume that reservation
instead: we return our subpool charge, drop a global reservation and uncharge the cgroup reservation of the folio. However, we don't set HPG_restore_reserve on the folio.

If we then hit an error before the folio gets added to the page cache, restore_reserve_on_error() assumes that alloc_hugetlb_folio() performed the reservation in the reserve map and removes it, and free_huge_folio() doesn't restore the global reservation. The reservation of the racing mmap() is then gone, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying alloc_hugetlb_folio() right before vma_commit_reservation() during a failing UFFDIO_COPY, while another process mmap()s the same file: once all unreserved huge pages are in use, that process gets SIGBUS when touching its page.

Let's fix it by setting HPG_restore_reserve, so the error path keeps the reservation in the reserve map and free_huge_folio() restores the global reservation. On success, adding the folio to the page cache clears the flag.

Quoted 7 lines

This has low urgency, as it requires an error after racing with another mapping reserving the same index.

Fixes 33039678c8da ("mm/hugetlb: handle races in alloc_huge_page and hugetlb_reserve_pages")

Thanks.

Reviewed-by on v2 3/35
#8 lore raw

Re: [PATCH v2 03/35] mm/hugetlb: restore the reservation when racing with another reservation

In reply to David Hildenbrand (Arm)
Quoted 28 lines · David Hildenbrand (Arm)

When we fault in a folio in a shared hugetlb mapping at an index that is not reserved yet, alloc_hugetlb_folio() can race with hugetlb_reserve_pages() reserving that index, for example, because another process mmap()s the same file without MAP_NORESERVE.

vma_commit_reservation() will then find the racing reservation in the reserve map after allocating the folio, and we consume that reservation
instead: we return our subpool charge, drop a global reservation and uncharge the cgroup reservation of the folio. However, we don't set HPG_restore_reserve on the folio.

If we then hit an error before the folio gets added to the page cache, restore_reserve_on_error() assumes that alloc_hugetlb_folio() performed the reservation in the reserve map and removes it, and free_huge_folio() doesn't restore the global reservation. The reservation of the racing mmap() is then gone, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying alloc_hugetlb_folio() right before vma_commit_reservation() during a failing UFFDIO_COPY, while another process mmap()s the same file: once all unreserved huge pages are in use, that process gets SIGBUS when touching its page.

Let's fix it by setting HPG_restore_reserve, so the error path keeps the reservation in the reserve map and free_huge_folio() restores the global reservation. On success, adding the folio to the page cache clears the flag.

Quoted 7 lines

This has low urgency, as it requires an error after racing with another mapping reserving the same index.

Fixes 33039678c8da ("mm/hugetlb: handle races in alloc_huge_page and hugetlb_reserve_pages")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#9 lore raw

PATCH v2 4/35 mm/hugetlb: return the reservation to the subpool on error

In reply to David Hildenbrand (Arm)

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.

Fixes 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools")
---
 Documentation/mm/hugetlbfs_reserv.rst |  4 ++--
 mm/hugetlb.c                          | 24 +++++++++++++++---------
 2 files changed, 17 insertions(+), 11 deletions(-)
Documentation/mm/hugetlbfs_reserv.rst +2 -2
index 4711029bd067d..7c13f79603eee 100644
@@ -257,8 +257,8 @@ compensate.  See the section
 :ref:`Reservation Map Helper Routines <resv_map_helpers>` 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.
 
 
mm/hugetlb.c +15 -9
index fbe79ae4c0f3c..025aae6d0b72e 100644
@@ -2722,7 +2722,9 @@ static long vma_del_reservation(struct hstate *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,
@@ -3200,6 +3202,18 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
 
 	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);
@@ -3230,14 +3244,6 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
 		}
 	}
 
-	/*
-	 * 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:
Signature · 1 line
2.56.1
#10 lore raw

Re: [PATCH v2 04/35] mm/hugetlb: return the reservation to the subpool on error

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] mm/hugetlb: stale CoW-only HPG_restore_reserve comment in hugetlb.h - [Low] mm/hugetlb: inaccurate…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] mm/hugetlb: stale CoW-only HPG_restore_reserve comment in hugetlb.h
- [Low] mm/hugetlb: inaccurate Fixes trailer citing 1c5ecae3a93f --

commit 60d3433873fb76ac98b3288706485f76d8784a94
Author: David Hildenbrand (Arm) <[email protected]>

Quoted 34 lines

mm/hugetlb: return the reservation to the subpool on error

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.

Quoted 7 lines

This has low urgency, as it only affects the subpool minimum and the hugetlb cgroup reservation accounting on min_size= mounts.

Fixes 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools")

[Severity: Low]
Should the Fixes: tag cite d85f69b0b533 ("mm/hugetlb: alloc_huge_page handle areas hole punched by fallocate") instead of 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools")?

In 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools"), alloc_huge_page() ignored a zero return from hugepage_subpool_get_pages(), so unreserved shared faults did not consume subpool minimum reservations or set PagePrivate, and free_huge_page() always put pages to the subpool.

Consuming subpool minimum reservations via gbl_chg == 0 on unreserved shared faults was introduced in d85f69b0b533 ("mm/hugetlb: alloc_huge_page handle areas hole punched by fallocate").

Skipping hugepage_subpool_put_pages() on restore_reserve came in 0919e1b69ab4 ("hugetlbfs: on restore reserve error path retain subpool reservation").

Removing reserve-map entries in restore_reserve_on_error() when HPG_restore_reserve is clear came in 846be08578ed ("mm/hugetlb: expand restore_reserve_on_error functionality").

[ ... ]

Quoted 4 lines
mm/hugetlb.c +0 -0
index fbe79ae4c0f3c..025aae6d0b72e 100644

[ ... ]

Quoted 5 lines
mm/hugetlb.c +1 -0
@@ -3200,6 +3202,18 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
 
 	hugetlb_set_folio_subpool(folio, spool);
 
+	/*

mm/hugetlb.c static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma, · line 3215

Quoted 10 lines
mm/hugetlb.c +10 -0
+	 * 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);

[Severity: Low]
Should the HPG_restore_reserve comment in include/linux/hugetlb.h also be updated to match this change?

Both Documentation/mm/hugetlbfs_reserv.rst and restore_reserve_on_error() were updated, but the comment above enum hugetlb_page_flags still limits clearing the flag to CoW mappings:

include/linux/hugetlb.h (above enum hugetlb_page_flags):
* HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
* allocation time. Cleared when page is fully instantiated. Free
* routine checks flag to restore a reservation on error paths.
* Cleared when allocating for a CoW mapping using a subpool
* reservation, which the free routine returns to the subpool.

Leaving it as-is contradicts __alloc_hugetlb_folio() and Documentation/mm/hugetlbfs_reserv.rst.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer tree notice
#11 lore raw

+ mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
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…

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)" <[email protected]>
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.

Fixes 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 Documentation/mm/hugetlbfs_reserv.rst |    4 ++--
 mm/hugetlb.c                          |   24 +++++++++++++++---------
 2 files changed, 17 insertions(+), 11 deletions(-)
Documentation/mm/hugetlbfs_reserv.rst renamed from Documentation/mm/hugetlbfs_reserv.rst~mm-hugetlb-return-the-reservation-to-the-subpool-on-error +2 -2
@@ -257,8 +257,8 @@ compensate.  See the section
 :ref:`Reservation Map Helper Routines <resv_map_helpers>` 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.
 
 
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-return-the-reservation-to-the-subpool-on-error +15 -9
@@ -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 [email protected] 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

Reviewed-by on v2 4/35
#12 lore raw

Re: [PATCH v2 04/35] mm/hugetlb: return the reservation to the subpool on error

In reply to David Hildenbrand (Arm)
Quoted 32 lines · David Hildenbrand (Arm)

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.

Quoted 7 lines

This has low urgency, as it only affects the subpool minimum and the hugetlb cgroup reservation accounting on min_size= mounts.

Fixes 1c5ecae3a93f ("hugetlbfs: add minimum size accounting to subpools")

with sashiko's points addressed:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#13 lore raw

PATCH v2 5/35 mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()

In reply to David Hildenbrand (Arm)

memfd_alloc_folio() uses hugetlb_reserve_pages() to reserve the index of the folio it is about to allocate. If allocating the folio or adding it to the page cache fails, it undoes that reservation, but only if hugetlb_reserve_pages() reported that it actually performed the reservation.

If the index was already reserved, for example through an mmap() of the same file, memfd_alloc_folio() must not undo that reservation.

However, if hugetlb_reserve_pages() races with another reservation for the same index, region_add_limited() can reserve fewer pages than region_chg() counted. hugetlb_reserve_pages() will then hand back the surplus charges, but still report the number of pages that region_chg() counted.

Consequently, after racing with hugetlbfs_file_mmap() reserving the same index, memfd_alloc_folio() will undo the reservation of the mmap() caller on error, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying hugetlb_reserve_pages() after region_chg() during IOMMU_IOAS_MAP_FILE of a hugetlb memfd, while another process mmap()s the memfd, and failing the page cache insertion with fault injection. Once all unreserved huge pages are in use, the mmap() caller gets SIGBUS on fault.

Let's fix it by only reporting the reservations that we actually added. Other callers are not affected, as they only care about error vs. success, not the actual number of reserved pages.

This has low urgency, as it requires an allocation failure after racing with an mmap() of the same file.

Fixes 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation")
---
 mm/hugetlb.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
mm/hugetlb.c +8 -3
index 025aae6d0b72e..68d90c7297921 100644
@@ -6907,9 +6907,9 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
 /*
  * Update the reservation map for the range [from, to].
  *
- * Returns the number of entries that would be added to the reservation map
- * associated with the range [from, to].  This number is greater or equal to
- * zero. -EINVAL or -ENOMEM is returned in case of any errors.
+ * Returns the number of huge pages that we reserved in the range [from, to],
+ * or a negative error code.  On a later error, callers must only undo
+ * these reservations, not reservations that already existed in the range.
  */
 
 long hugetlb_reserve_pages(struct inode *inode,
@@ -7052,6 +7052,11 @@ long hugetlb_reserve_pages(struct inode *inode,
 			rsv_adjust = hugepage_subpool_put_pages(spool,
 								chg - add);
 			hugetlb_acct_memory(h, -rsv_adjust);
+			/*
+			 * Only report the reservations that we added, such that
+			 * callers will not undo the racing reservations.
+			 */
+			chg = add;
 		} else if (h_cg) {
 			/*
 			 * The file_regions will hold their own reference to
Signature · 1 line
2.56.1
maintainer tree notice
#14 lore raw

+ mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages() has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.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)" <[email protected]>
Subject: mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()
Date: Thu, 03 Dec 2026 12:37:28 +0100

memfd_alloc_folio() uses hugetlb_reserve_pages() to reserve the index of the folio it is about to allocate. If allocating the folio or adding it to the page cache fails, it undoes that reservation, but only if hugetlb_reserve_pages() reported that it actually performed the reservation.

If the index was already reserved, for example through an mmap() of the same file, memfd_alloc_folio() must not undo that reservation.

However, if hugetlb_reserve_pages() races with another reservation for the same index, region_add_limited() can reserve fewer pages than region_chg() counted. hugetlb_reserve_pages() will then hand back the surplus charges, but still report the number of pages that region_chg() counted.

Consequently, after racing with hugetlbfs_file_mmap() reserving the same index, memfd_alloc_folio() will undo the reservation of the mmap() caller on error, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying hugetlb_reserve_pages() after region_chg() during IOMMU_IOAS_MAP_FILE of a hugetlb memfd, while another process mmap()s the memfd, and failing the page cache insertion with fault injection. Once all unreserved huge pages are in use, the mmap() caller gets SIGBUS on fault.

Let's fix it by only reporting the reservations that we actually added. Other callers are not affected, as they only care about error vs. success, not the actual number of reserved pages.

This has low urgency, as it requires an allocation failure after racing with an mmap() of the same file.

Fixes 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages +8 -3
@@ -6882,9 +6882,9 @@ next:
 /*
  * Update the reservation map for the range [from, to].
  *
- * Returns the number of entries that would be added to the reservation map
- * associated with the range [from, to].  This number is greater or equal to
- * zero. -EINVAL or -ENOMEM is returned in case of any errors.
+ * Returns the number of huge pages that we reserved in the range [from, to],
+ * or a negative error code.  On a later error, callers must only undo
+ * these reservations, not reservations that already existed in the range.
  */
 
 long hugetlb_reserve_pages(struct inode *inode,
@@ -7027,6 +7027,11 @@ retry:
 			rsv_adjust = hugepage_subpool_put_pages(spool,
 								chg - add);
 			hugetlb_acct_memory(h, -rsv_adjust);
+			/*
+			 * Only report the reservations that we added, such that
+			 * callers will not undo the racing reservations.
+			 */
+			chg = add;
 		} else if (h_cg) {
 			/*
 			 * The file_regions will hold their own reference to

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 5/35
#15 lore raw

Re: [PATCH v2 05/35] mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()

In reply to David Hildenbrand (Arm)
Quoted 33 lines · David Hildenbrand (Arm)

memfd_alloc_folio() uses hugetlb_reserve_pages() to reserve the index of the folio it is about to allocate. If allocating the folio or adding it to the page cache fails, it undoes that reservation, but only if hugetlb_reserve_pages() reported that it actually performed the reservation.

If the index was already reserved, for example through an mmap() of the same file, memfd_alloc_folio() must not undo that reservation.

However, if hugetlb_reserve_pages() races with another reservation for the same index, region_add_limited() can reserve fewer pages than region_chg() counted. hugetlb_reserve_pages() will then hand back the surplus charges, but still report the number of pages that region_chg() counted.

Consequently, after racing with hugetlbfs_file_mmap() reserving the same index, memfd_alloc_folio() will undo the reservation of the mmap() caller on error, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying hugetlb_reserve_pages() after region_chg() during IOMMU_IOAS_MAP_FILE of a hugetlb memfd, while another process mmap()s the memfd, and failing the page cache insertion with fault injection. Once all unreserved huge pages are in use, the mmap() caller gets SIGBUS on fault.

Let's fix it by only reporting the reservations that we actually added. Other callers are not affected, as they only care about error vs. success, not the actual number of reserved pages.

Quoted 7 lines

This has low urgency, as it requires an allocation failure after racing with an mmap() of the same file.

Fixes 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation")

Thanks.

Reviewed-by on v2 5/35
#16 lore raw

Re: [PATCH v2 05/35] mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages()

In reply to David Hildenbrand (Arm)
Quoted 29 lines · David Hildenbrand (Arm)

memfd_alloc_folio() uses hugetlb_reserve_pages() to reserve the index of the folio it is about to allocate. If allocating the folio or adding it to the page cache fails, it undoes that reservation, but only if hugetlb_reserve_pages() reported that it actually performed the reservation.

If the index was already reserved, for example through an mmap() of the same file, memfd_alloc_folio() must not undo that reservation.

However, if hugetlb_reserve_pages() races with another reservation for the same index, region_add_limited() can reserve fewer pages than region_chg() counted. hugetlb_reserve_pages() will then hand back the surplus charges, but still report the number of pages that region_chg() counted.

Consequently, after racing with hugetlbfs_file_mmap() reserving the same index, memfd_alloc_folio() will undo the reservation of the mmap() caller on error, and a later fault at that index can fail with SIGBUS.

Reproduced by artificially delaying hugetlb_reserve_pages() after region_chg() during IOMMU_IOAS_MAP_FILE of a hugetlb memfd, while another process mmap()s the memfd, and failing the page cache insertion with fault injection. Once all unreserved huge pages are in use, the mmap() caller gets SIGBUS on fault.

Let's fix it by only reporting the reservations that we actually added. Other callers are not affected, as they only care about error vs. success, not the actual number of reserved pages.

Quoted 7 lines

This has low urgency, as it requires an allocation failure after racing with an mmap() of the same file.

Fixes 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#17 lore raw

PATCH v2 6/35 mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking

In reply to David Hildenbrand (Arm)

If a COW fault of the owner of a MAP_PRIVATE hugetlb reservation fails to allocate a folio, the owner keeps the folio, and unmap_ref_private() unmaps it from all other private VMAs that map it.

We mark these VMAs with HPAGE_RESV_UNMAPPED, such that future faults in them fail instead of looking like the data was lost.

However, when a process whose VMA has HPAGE_RESV_UNMAPPED set forks, hugetlb_dup_vma_private() clears vm_private_data of the child's VMA, including HPAGE_RESV_UNMAPPED.

Consequently, faults in the grandchild of the original process can map a page cache folio or a new zeroed folio without any signal, silently losing the data.

Reproduced by forking a child after writing to a folio in a private mapping, exhausting the pool such that the COW fault of the owner fails, and letting the child fork a grandchild after freeing up the pool again:
the child gets SIGBUS when accessing the folio, but the grandchild reads zeroes.

Let's fix it by keeping HPAGE_RESV_UNMAPPED when clearing the private data of the child's VMA.

This has low urgency, as it requires a failed COW fault due to an exhausted pool, followed by a fork(). No need to CC stable.

Fixes 04f2cbe35699 ("hugetlb: guarantee that COW faults for a process that called mmap(MAP_PRIVATE) on hugetlbfs will succeed")
---
 mm/hugetlb.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/hugetlb.c +9 -2
index 68d90c7297921..f64c3293fd109 100644
@@ -1218,7 +1218,7 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma)
 {
 	VM_WARN_ON_ONCE_VMA(!vma_is_hugetlb(vma), vma);
 	/*
-	 * Clear vm_private_data
+	 * Clear vm_private_data, except for HPAGE_RESV_UNMAPPED
 	 * - For shared mappings this is a per-vma semaphore that may be
 	 *   allocated in a subsequent call to hugetlb_vm_op_open.
 	 *   Before clearing, make sure pointer is not associated with vma
@@ -1227,7 +1227,10 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma)
 	 *   been called to allocate a new structure.
 	 * - For MAP_PRIVATE mappings, this is the reserve map which does
 	 *   not apply to children.  Faults generated by the children are
-	 *   not guaranteed to succeed, even if read-only.
+	 *   not guaranteed to succeed, even if read-only.  But keep
+	 *   HPAGE_RESV_UNMAPPED: if the reservation owner unmapped folios
+	 *   from this VMA when its COW fault failed, faults in children must
+	 *   fail as well.
 	 */
 	if (vma->vm_flags & VM_MAYSHARE) {
 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
@@ -1235,7 +1238,11 @@ void hugetlb_dup_vma_private(struct vm_area_struct *vma)
 		if (vma_lock && vma_lock->vma != vma)
 			vma->vm_private_data = NULL;
 	} else {
+		const bool unmapped = is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED);
+
 		vma->vm_private_data = NULL;
+		if (unmapped)
+			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
 	}
 }
 
Signature · 1 line
2.56.1
maintainer tree notice
#18 lore raw

+ mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch

The patch titled
Subject: mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-keep-hpage_resv_unmapped-when-forking.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)" <[email protected]>
Subject: mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking
Date: Thu, 03 Dec 2026 12:37:29 +0100

If a COW fault of the owner of a MAP_PRIVATE hugetlb reservation fails to allocate a folio, the owner keeps the folio, and unmap_ref_private() unmaps it from all other private VMAs that map it.

We mark these VMAs with HPAGE_RESV_UNMAPPED, such that future faults in them fail instead of looking like the data was lost.

However, when a process whose VMA has HPAGE_RESV_UNMAPPED set forks, hugetlb_dup_vma_private() clears vm_private_data of the child's VMA, including HPAGE_RESV_UNMAPPED.

Consequently, faults in the grandchild of the original process can map a page cache folio or a new zeroed folio without any signal, silently losing the data.

Reproduced by forking a child after writing to a folio in a private mapping, exhausting the pool such that the COW fault of the owner fails, and letting the child fork a grandchild after freeing up the pool again:
the child gets SIGBUS when accessing the folio, but the grandchild reads zeroes.

Let's fix it by keeping HPAGE_RESV_UNMAPPED when clearing the private data of the child's VMA.

This has low urgency, as it requires a failed COW fault due to an exhausted pool, followed by a fork(). No need to CC stable.

Fixes 04f2cbe35699 ("hugetlb: guarantee that COW faults for a process that called mmap(MAP_PRIVATE) on hugetlbfs will succeed")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-keep-hpage_resv_unmapped-when-forking +9 -2
@@ -1218,7 +1218,7 @@ void hugetlb_dup_vma_private(struct vm_a
 {
 	VM_WARN_ON_ONCE_VMA(!vma_is_hugetlb(vma), vma);
 	/*
-	 * Clear vm_private_data
+	 * Clear vm_private_data, except for HPAGE_RESV_UNMAPPED
 	 * - For shared mappings this is a per-vma semaphore that may be
 	 *   allocated in a subsequent call to hugetlb_vm_op_open.
 	 *   Before clearing, make sure pointer is not associated with vma
@@ -1227,7 +1227,10 @@ void hugetlb_dup_vma_private(struct vm_a
 	 *   been called to allocate a new structure.
 	 * - For MAP_PRIVATE mappings, this is the reserve map which does
 	 *   not apply to children.  Faults generated by the children are
-	 *   not guaranteed to succeed, even if read-only.
+	 *   not guaranteed to succeed, even if read-only.  But keep
+	 *   HPAGE_RESV_UNMAPPED: if the reservation owner unmapped folios
+	 *   from this VMA when its COW fault failed, faults in children must
+	 *   fail as well.
 	 */
 	if (vma->vm_flags & VM_MAYSHARE) {
 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
@@ -1235,7 +1238,11 @@ void hugetlb_dup_vma_private(struct vm_a
 		if (vma_lock && vma_lock->vma != vma)
 			vma->vm_private_data = NULL;
 	} else {
+		const bool unmapped = is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED);
+
 		vma->vm_private_data = NULL;
+		if (unmapped)
+			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
 	}
 }
 

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 6/35
#19 lore raw

Re: [PATCH v2 06/35] mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking

In reply to David Hildenbrand (Arm)
Quoted 24 lines · David Hildenbrand (Arm)

If a COW fault of the owner of a MAP_PRIVATE hugetlb reservation fails to allocate a folio, the owner keeps the folio, and unmap_ref_private() unmaps it from all other private VMAs that map it.

We mark these VMAs with HPAGE_RESV_UNMAPPED, such that future faults in them fail instead of looking like the data was lost.

However, when a process whose VMA has HPAGE_RESV_UNMAPPED set forks, hugetlb_dup_vma_private() clears vm_private_data of the child's VMA, including HPAGE_RESV_UNMAPPED.

Consequently, faults in the grandchild of the original process can map a page cache folio or a new zeroed folio without any signal, silently losing the data.

Reproduced by forking a child after writing to a folio in a private mapping, exhausting the pool such that the COW fault of the owner fails, and letting the child fork a grandchild after freeing up the pool again:
the child gets SIGBUS when accessing the folio, but the grandchild reads zeroes.

Let's fix it by keeping HPAGE_RESV_UNMAPPED when clearing the private data of the child's VMA.

Quoted 6 lines

This has low urgency, as it requires a failed COW fault due to an exhausted pool, followed by a fork(). No need to CC stable.

Fixes 04f2cbe35699 ("hugetlb: guarantee that COW faults for a process that called mmap(MAP_PRIVATE) on hugetlbfs will succeed")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#20 lore raw

PATCH v2 7/35 mm/hugetlb: always mark new page cache folios migratable

In reply to David Hildenbrand (Arm)

hugetlb_no_page() and hugetlb_mfill_atomic_pte() only set hugetlb_migratable for a new folio once they installed the PTE.

If they back out after adding the new folio to the page cache, for example because the PTE changed or is no longer empty, the folio will stay in the page cache without the migratable flag set.

Later faults will find it as an existing folio and never reset the flag. Such a folio can never be isolated for migration, which breaks memory offlining, alloc_contig_range(), longterm pinning and memory failure handling.

Worse, if the folio is not longterm pinnable, because it resides in ZONE_MOVABLE or CMA, memfd_pin_folios() and the longterm GUP path retry forever on -EAGAIN. memfd_pin_folios() cannot even be killed, which will be fixed independently.

Let's fix it by setting hugetlb folios migratable whenever (1) a new anon folio gets mapped; or (2) a new folio was added to the page cache, like hugetlbfs_fallocate() already does.

This has low urgency.

Fixes cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration")
---
 mm/hugetlb.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
mm/hugetlb.c +7 -9
index f64c3293fd109..662cb87fe740a 100644
@@ -5972,12 +5972,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 {
 	const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE;
 	u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
-	bool new_folio, new_anon_folio = false;
+	bool folio_locked = true, new_anon_folio = false;
 	struct vm_area_struct *vma = vmf->vma;
 	struct mm_struct *mm = vma->vm_mm;
 	struct hstate *h = hstate_vma(vma);
 	vm_fault_t ret = VM_FAULT_SIGBUS;
-	bool folio_locked = true;
 	struct folio *folio;
 	pte_t new_pte;
 
@@ -5997,7 +5996,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 	 * Use page lock to guard against racing truncation
 	 * before we get page_table_lock.
 	 */
-	new_folio = false;
 	folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff);
 	if (IS_ERR(folio)) {
 		if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false))
@@ -6067,7 +6065,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 		}
 		folio_zero_user(folio, vmf->real_address);
 		__folio_mark_uptodate(folio);
-		new_folio = true;
 
 		if (pagecache_folio) {
 			int err = hugetlb_add_to_page_cache(folio, mapping,
@@ -6088,6 +6085,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 				goto out;
 			}
 			filemap_invalidate_unlock_shared(mapping);
+			folio_set_hugetlb_migratable(folio);
 		} else {
 			new_anon_folio = true;
 			folio_lock(folio);
@@ -6170,11 +6168,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 	}
 
 	/*
-	 * Only set hugetlb_migratable in newly allocated pages.  Existing pages
-	 * found in the pagecache may not have hugetlb_migratable if they have
-	 * been isolated for migration.
+	 * We must set hugetlb folios migratable whenever (1) a new anon folio
+	 * gets mapped; or (2) a new folio was added to the pagecache.
 	 */
-	if (new_folio)
+	if (new_anon_folio)
 		folio_set_hugetlb_migratable(folio);
 
 	if (folio_locked)
@@ -6668,6 +6665,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 		if (ret)
 			goto out_release_nounlock;
 		folio_in_pagecache = true;
+		folio_set_hugetlb_migratable(folio);
 	}
 
 	ptl = huge_pte_lock(h, dst_mm, dst_pte);
@@ -6716,7 +6714,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 	/* No need to invalidate - it was non-present before */
 	update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
-	if (!is_continue)
+	if (!folio_in_pagecache)
 		folio_set_hugetlb_migratable(folio);
 	if (vm_shared || is_continue)
 		folio_unlock(folio);
Signature · 1 line
2.56.1
maintainer tree notice
#21 lore raw

+ mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: always mark new page cache folios migratable has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: always mark new page cache folios migratable
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-always-mark-new-page-cache-folios-migratable.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)" <[email protected]>
Subject: mm/hugetlb: always mark new page cache folios migratable
Date: Thu, 03 Dec 2026 12:37:30 +0100

hugetlb_no_page() and hugetlb_mfill_atomic_pte() only set hugetlb_migratable for a new folio once they installed the PTE.

If they back out after adding the new folio to the page cache, for example because the PTE changed or is no longer empty, the folio will stay in the page cache without the migratable flag set.

Later faults will find it as an existing folio and never reset the flag. Such a folio can never be isolated for migration, which breaks memory offlining, alloc_contig_range(), longterm pinning and memory failure handling.

Worse, if the folio is not longterm pinnable, because it resides in ZONE_MOVABLE or CMA, memfd_pin_folios() and the longterm GUP path retry forever on -EAGAIN. memfd_pin_folios() cannot even be killed, which will be fixed independently.

Let's fix it by setting hugetlb folios migratable whenever (1) a new anon folio gets mapped; or (2) a new folio was added to the page cache, like hugetlbfs_fallocate() already does.

This has low urgency.

Fixes cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration")
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-always-mark-new-page-cache-folios-migratable +7 -9
@@ -5947,12 +5947,11 @@ static vm_fault_t hugetlb_no_page(struct
 {
 	const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE;
 	u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
-	bool new_folio, new_anon_folio = false;
+	bool folio_locked = true, new_anon_folio = false;
 	struct vm_area_struct *vma = vmf->vma;
 	struct mm_struct *mm = vma->vm_mm;
 	struct hstate *h = hstate_vma(vma);
 	vm_fault_t ret = VM_FAULT_SIGBUS;
-	bool folio_locked = true;
 	struct folio *folio;
 	pte_t new_pte;
 
@@ -5972,7 +5971,6 @@ static vm_fault_t hugetlb_no_page(struct
 	 * Use page lock to guard against racing truncation
 	 * before we get page_table_lock.
 	 */
-	new_folio = false;
 	folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff);
 	if (IS_ERR(folio)) {
 		if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false))
@@ -6042,7 +6040,6 @@ static vm_fault_t hugetlb_no_page(struct
 		}
 		folio_zero_user(folio, vmf->real_address);
 		__folio_mark_uptodate(folio);
-		new_folio = true;
 
 		if (pagecache_folio) {
 			int err = hugetlb_add_to_page_cache(folio, mapping,
@@ -6063,6 +6060,7 @@ static vm_fault_t hugetlb_no_page(struct
 				goto out;
 			}
 			filemap_invalidate_unlock_shared(mapping);
+			folio_set_hugetlb_migratable(folio);
 		} else {
 			new_anon_folio = true;
 			folio_lock(folio);
@@ -6145,11 +6143,10 @@ static vm_fault_t hugetlb_no_page(struct
 	}
 
 	/*
-	 * Only set hugetlb_migratable in newly allocated pages.  Existing pages
-	 * found in the pagecache may not have hugetlb_migratable if they have
-	 * been isolated for migration.
+	 * We must set hugetlb folios migratable whenever (1) a new anon folio
+	 * gets mapped; or (2) a new folio was added to the pagecache.
 	 */
-	if (new_folio)
+	if (new_anon_folio)
 		folio_set_hugetlb_migratable(folio);
 
 	if (folio_locked)
@@ -6643,6 +6640,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_
 		if (ret)
 			goto out_release_nounlock;
 		folio_in_pagecache = true;
+		folio_set_hugetlb_migratable(folio);
 	}
 
 	ptl = huge_pte_lock(h, dst_mm, dst_pte);
@@ -6691,7 +6689,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_
 	/* No need to invalidate - it was non-present before */
 	update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
-	if (!is_continue)
+	if (!folio_in_pagecache)
 		folio_set_hugetlb_migratable(folio);
 	if (vm_shared || is_continue)
 		folio_unlock(folio);

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 7/35
#22 lore raw

Re: [PATCH v2 07/35] mm/hugetlb: always mark new page cache folios migratable

In reply to David Hildenbrand (Arm)
Quoted 21 lines · David Hildenbrand (Arm)

hugetlb_no_page() and hugetlb_mfill_atomic_pte() only set hugetlb_migratable for a new folio once they installed the PTE.

If they back out after adding the new folio to the page cache, for example because the PTE changed or is no longer empty, the folio will stay in the page cache without the migratable flag set.

Later faults will find it as an existing folio and never reset the flag. Such a folio can never be isolated for migration, which breaks memory offlining, alloc_contig_range(), longterm pinning and memory failure handling.

Worse, if the folio is not longterm pinnable, because it resides in ZONE_MOVABLE or CMA, memfd_pin_folios() and the longterm GUP path retry forever on -EAGAIN. memfd_pin_folios() cannot even be killed, which will be fixed independently.

Let's fix it by setting hugetlb folios migratable whenever (1) a new anon folio gets mapped; or (2) a new folio was added to the page cache, like hugetlbfs_fallocate() already does.

Quoted 7 lines

This has low urgency.

Fixes cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#23 lore raw

PATCH v2 8/35 mm/memfd: mark new hugetlb folios migratable

In reply to David Hildenbrand (Arm)

memfd_alloc_folio() adds a new hugetlb folio to the page cache, but never sets it migratable, unlike hugetlb_no_page() and hugetlbfs_fallocate().

Later faults will find it as an existing folio and never set the migratable flag.

Such a folio can never be isolated for migration, even once it is no longer pinned, which breaks memory offlining, alloc_contig_range() and memory failure handling.

Let's fix it by setting the new folio migratable after adding it to the page cache, like hugetlbfs_fallocate() already does.

This has low urgency.

Fixes 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios")
---
 mm/memfd.c | 1 +
 1 file changed, 1 insertion(+)
mm/memfd.c +1 -0
index 0e02ca69d27cb..6340e8a7c4355 100644
@@ -152,6 +152,7 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
 			}
 
 			hugetlb_set_folio_subpool(folio, subpool_inode(inode));
+			folio_set_hugetlb_migratable(folio);
 			folio_unlock(folio);
 			filemap_invalidate_unlock_shared(memfd->f_mapping);
 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
Signature · 1 line
2.56.1
maintainer tree notice
#24 lore raw

+ mm-memfd-mark-new-hugetlb-folios-migratable.patch added to mm-unstable branch

The patch titled Subject: mm/memfd: mark new hugetlb folios migratable has been added to the -mm mm-unstable branch. Its filename is mm-memfd-mark-new-hugetlb-folios-migratable.patch

The patch titled
Subject: mm/memfd: mark new hugetlb folios migratable
has been added to the -mm mm-unstable branch. Its filename is
mm-memfd-mark-new-hugetlb-folios-migratable.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-memfd-mark-new-hugetlb-folios-migratable.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)" <[email protected]>
Subject: mm/memfd: mark new hugetlb folios migratable
Date: Thu, 03 Dec 2026 12:37:31 +0100

memfd_alloc_folio() adds a new hugetlb folio to the page cache, but never sets it migratable, unlike hugetlb_no_page() and hugetlbfs_fallocate().

Later faults will find it as an existing folio and never set the migratable flag.

Such a folio can never be isolated for migration, even once it is no longer pinned, which breaks memory offlining, alloc_contig_range() and memory failure handling.

Let's fix it by setting the new folio migratable after adding it to the page cache, like hugetlbfs_fallocate() already does.

This has low urgency.

Fixes 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios")
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/memfd.c |    1 +
 1 file changed, 1 insertion(+)
mm/memfd.c renamed from mm/memfd.c~mm-memfd-mark-new-hugetlb-folios-migratable +1 -0
@@ -152,6 +152,7 @@ struct folio *memfd_alloc_folio(struct f
 			}
 
 			hugetlb_set_folio_subpool(folio, subpool_inode(inode));
+			folio_set_hugetlb_migratable(folio);
 			folio_unlock(folio);
 			filemap_invalidate_unlock_shared(memfd->f_mapping);
 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 8/35
#25 lore raw

Re: [PATCH v2 08/35] mm/memfd: mark new hugetlb folios migratable

In reply to David Hildenbrand (Arm)
Quoted 14 lines · David Hildenbrand (Arm)

memfd_alloc_folio() adds a new hugetlb folio to the page cache, but never sets it migratable, unlike hugetlb_no_page() and hugetlbfs_fallocate().

Later faults will find it as an existing folio and never set the migratable flag.

Such a folio can never be isolated for migration, even once it is no longer pinned, which breaks memory offlining, alloc_contig_range() and memory failure handling.

Let's fix it by setting the new folio migratable after adding it to the page cache, like hugetlbfs_fallocate() already does.

Quoted 7 lines

This has low urgency.

Fixes 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#26 lore raw

PATCH v2 9/35 mm/hugetlb: free surplus folios of other nodes on free

In reply to David Hildenbrand (Arm)

Surplus hugetlb folios are accounted per node, but reservations are global. When free_huge_folio() frees a hugetlb folio whose node doesn't have any surplus folios, we currently keep it as a free hugetlb folio.

However, we might then have surplus folios on other nodes that are no longer required for the global reservations.

This can be reproduced with two NUMA nodes, nr_hugepages=1 on node 0 and nr_overcommit_hugepages=1:

(1) Allocate the single hugetlb folio on node 0 from the pool.
(2) With MPOL_BIND to node 1, mmap(MAP_SHARED) a hugetlb memfd that needs a single folio. This will reserve + allocate a surplus folio on node 1.
(3) Free the folio from (1).
(4) With MPOL_BIND to node 0, fault in the folio of the memfd, which consumes the reservation and allocates node 0's free folio, and immediately punch a hole, freeing the folio without restoring the reservation. Node 0 has no surplus folios, so the folio is kept.

The pool ends up with a free surplus folio on node 1 that nobody reserved:

  HugePages_Total:       2
  HugePages_Free:        2
  HugePages_Rsvd:        0
  HugePages_Surp:        1

Let's fix it by freeing a surplus folio of another node when free_huge_folio() keeps a folio whose node has no surplus folios, using the same rule as for the folio's own node: the free folios cover all reservations without it.

With this fix, the surplus folio is already freed in (3), as the two free folios then exceed the single reservation, and we correctly end up with a single free folio on node 0 and no surplus folios.

Note that the freed surplus folio might have been required by a task that was bound to that node. But we don't care about that: reservations are inherently not NUMA aware, and anybody who needs guarantees on specific nodes has to preallocate, like QEMU does, for example.

This has low urgency.

Fixes 7893d1d505d5 ("hugetlb: Try to grow hugetlb pool for MAP_PRIVATE mappings")
---
 mm/hugetlb.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
mm/hugetlb.c +25 -6
index 662cb87fe740a..5984377160d37 100644
@@ -142,6 +142,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
 				unsigned long end, struct folio *folio,
 				zap_flags_t zap_flags);
 static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
+static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
+		nodemask_t *nodes_allowed, bool acct_surplus);
 
 static inline bool subpool_is_free(struct hugepage_subpool *spool)
 {
@@ -1767,6 +1769,7 @@ void free_huge_folio(struct folio *folio)
 	struct hstate *h = folio_hstate(folio);
 	int nid = folio_nid(folio);
 	struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
+	struct folio *surplus_folio = NULL;
 	bool restore_reserve;
 	unsigned long flags;
 
@@ -1815,19 +1818,35 @@ void free_huge_folio(struct folio *folio)
 		folio_clear_hugetlb_temporary(folio);
 		spin_unlock_irqrestore(&hugetlb_lock, flags);
 		update_and_free_hugetlb_folio(h, folio, true);
+		return;
 	} 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);
-	} 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);
+		return;
 	}
+
+	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);
+	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);
 }
 
 /*
Signature · 1 line
2.56.1
#27 lore raw

Re: [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm/hugetlb: free_huge_folio() drops healthy surplus for hwpoisoned folio - [Medium] mm: repeated…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm/hugetlb: free_huge_folio() drops healthy surplus for hwpoisoned folio
- [Medium] mm: repeated O(N_nodes) scan in free_huge_folio() under hugetlb_lock
--

commit 1ff8f928aad331ee2a27cdedef0346559479e7df
Author: David Hildenbrand (Arm) <[email protected]>

mm/hugetlb: free surplus folios of other nodes on free

This commit updates free_huge_folio() so that when a freed folio is enqueued back to a node without surplus folios, it removes and frees a free surplus folio from another node if h->free_huge_pages exceeds h->resv_huge_pages.

Quoted 4 lines
mm/hugetlb.c +0 -0
index 662cb87fe740a..5984377160d37 100644

[ ... ]

Quoted 1 line
mm/hugetlb.c +0 -0
@@ -1815,19 +1818,35 @@ void free_huge_folio(struct folio *folio)

[ ... ]

Quoted 6 lines
mm/hugetlb.c +6 -0
+	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

mm/hugetlb.c void free_huge_folio(struct folio *folio) · line 1849

Quoted 10 lines
mm/hugetlb.c +10 -0
+	 * 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);
+	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);

[Severity: Medium]
Can this free a healthy surplus folio on another node when folio is hwpoisoned?

When memory_failure() hits an anonymous persistent folio on a node with no surplus folios, me_huge_page() calls folio_put() and then __page_handle_poison() -> dissolve_free_hugetlb_folio(). If another node has a free surplus folio backing a reservation, this frees that folio.

dissolve_free_hugetlb_folio() then fails with -EBUSY because available_huge_pages(h) is zero, leaking the healthy subpages. The reservation is also left backed only by the poisoned folio, which dequeue_hugetlb_folio_node_exact() skips, risking SIGBUS on a later fault.

Should this check skip freeing another node's surplus folio when folio_test_hwpoison(folio) is true?

[Severity: Medium]
Can this trigger a repeated O(N_nodes) scan under the IRQ-disabled hugetlb_lock during bulk teardown?

When another node has an in-use surplus folio with an empty freelist and h->free_huge_pages > h->resv_huge_pages, remove_pool_hugetlb_folio() scans all N_MEMORY nodes via for_each_node_mask_to_free() and returns NULL.

Because that condition stays true, every persistent folio freed by folios_put_refs() during munmap() repeats the full node scan. This turns O(M) bulk teardown into O(M * N_nodes) under the global IRQ-disabled hugetlb_lock, increasing lock contention and IRQ-off latency.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#28 lore raw

Re: [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free

Quoted 2 lines · On Thu, 03 Dec 2026 12:29:20 +0000, [email protected] wrote:

Can this free a healthy surplus folio on another node when folio is hwpoisoned?

With this patch, memory_failure() no longer recovers from the error in the program below. One more check fixes that:

- if (h->surplus_huge_pages &&
+ if (h->surplus_huge_pages && !folio_test_hwpoison(folio) &&
h->free_huge_pages > h->resv_huge_pages)

-chris

// Is the healthy part of a broken huge page still saved while another page is // reserved? Needs root, two NUMA nodes (numa=fake=2), nr_hugepages=1 on node 0, // and nr_overcommit_hugepages=1.
#define _GNU_SOURCE
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#ifndef MADV_HWPOISON
#define MADV_HWPOISON 100
#endif
#define SZ (2UL << 20)
static void bus(int s) { }
static void bind_to(int node)

{
	unsigned long mask = 1UL << node;

	if (syscall(SYS_set_mempolicy, 2 /* MPOL_BIND */, &mask, 8 * sizeof(mask)))
		perror("set_mempolicy");
}
static void pool(const char *when)
{
	char l[200]; FILE *f = fopen("/proc/meminfo", "r");

	printf("%s", when);
	while (fgets(l, sizeof(l), f))
		if (!strncmp(l, "HugePages_", 10)) { l[strcspn(l, "\n")] = 0; printf(" %s", l + 10); }
	printf("\n"); fclose(f);
}
int main(void)
{
	char *a, *b; int fd, r;

	signal(SIGBUS, bus);
	bind_to(0);	/* a uses the page of the pool */
	a = mmap(NULL, SZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
	if (a == MAP_FAILED) { perror("a"); return 2; }
	a[0] = 1;
	bind_to(1);	/* b reserves a page, which adds a surplus page on node 1 */
	fd = memfd_create("b", MFD_HUGETLB);
	if (fd < 0 || ftruncate(fd, SZ)) { perror("memfd"); return 2; }
	b = mmap(NULL, SZ, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (b == MAP_FAILED) { perror("b"); return 2; }
	pool("before:");
	r = madvise(a, SZ, MADV_HWPOISON);
	printf("memory error in a: %s\n", r ? strerror(errno) : "recovered");
	pool("after: ");
	return r != 0;
}
maintainer tree notice
#29 lore raw

+ mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: mm/hugetlb: free surplus folios of other nodes on free has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: free surplus folios of other nodes on free
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.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)" <[email protected]>
Subject: mm/hugetlb: free surplus folios of other nodes on free
Date: Thu, 03 Dec 2026 12:37:32 +0100

Surplus hugetlb folios are accounted per node, but reservations are global. When free_huge_folio() frees a hugetlb folio whose node doesn't have any surplus folios, we currently keep it as a free hugetlb folio.

However, we might then have surplus folios on other nodes that are no longer required for the global reservations.

This can be reproduced with two NUMA nodes, nr_hugepages=1 on node 0 and nr_overcommit_hugepages=1:

(1) Allocate the single hugetlb folio on node 0 from the pool.
(2) With MPOL_BIND to node 1, mmap(MAP_SHARED) a hugetlb memfd that needs a single folio. This will reserve + allocate a surplus folio on node 1.
(3) Free the folio from (1).
(4) With MPOL_BIND to node 0, fault in the folio of the memfd, which consumes the reservation and allocates node 0's free folio, and immediately punch a hole, freeing the folio without restoring the reservation. Node 0 has no surplus folios, so the folio is kept.

The pool ends up with a free surplus folio on node 1 that nobody reserved:

  HugePages_Total:       2
  HugePages_Free:        2
  HugePages_Rsvd:        0
  HugePages_Surp:        1

Let's fix it by freeing a surplus folio of another node when free_huge_folio() keeps a folio whose node has no surplus folios, using the same rule as for the folio's own node: the free folios cover all reservations without it.

With this fix, the surplus folio is already freed in (3), as the two free folios then exceed the single reservation, and we correctly end up with a single free folio on node 0 and no surplus folios.

Note that the freed surplus folio might have been required by a task that was bound to that node. But we don't care about that: reservations are inherently not NUMA aware, and anybody who needs guarantees on specific nodes has to preallocate, like QEMU does, for example.

This has low urgency.

Fixes 7893d1d505d5 ("hugetlb: Try to grow hugetlb pool for MAP_PRIVATE mappings")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-free-surplus-folios-of-other-nodes-on-free +25 -6
@@ -142,6 +142,8 @@ static void hugetlb_unmap_range(struct m
 				unsigned long end, struct folio *folio,
 				zap_flags_t zap_flags);
 static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
+static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
+		nodemask_t *nodes_allowed, bool acct_surplus);
 
 static inline bool subpool_is_free(struct hugepage_subpool *spool)
 {
@@ -1767,6 +1769,7 @@ void free_huge_folio(struct folio *folio
 	struct hstate *h = folio_hstate(folio);
 	int nid = folio_nid(folio);
 	struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
+	struct folio *surplus_folio = NULL;
 	bool restore_reserve;
 	unsigned long flags;
 
@@ -1815,19 +1818,35 @@ void free_huge_folio(struct folio *folio
 		folio_clear_hugetlb_temporary(folio);
 		spin_unlock_irqrestore(&hugetlb_lock, flags);
 		update_and_free_hugetlb_folio(h, folio, true);
+		return;
 	} 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);
-	} 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);
+		return;
 	}
+
+	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);
+	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);
 }
 
 /*

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 9/35
#30 lore raw

Re: [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free

In reply to David Hildenbrand (Arm)
Quoted 100 lines · David Hildenbrand (Arm)

Surplus hugetlb folios are accounted per node, but reservations are global. When free_huge_folio() frees a hugetlb folio whose node doesn't have any surplus folios, we currently keep it as a free hugetlb folio.

However, we might then have surplus folios on other nodes that are no longer required for the global reservations.

This can be reproduced with two NUMA nodes, nr_hugepages=1 on node 0 and nr_overcommit_hugepages=1:

(1) Allocate the single hugetlb folio on node 0 from the pool.
(2) With MPOL_BIND to node 1, mmap(MAP_SHARED) a hugetlb memfd that needs a single folio. This will reserve + allocate a surplus folio on node 1.
(3) Free the folio from (1).
(4) With MPOL_BIND to node 0, fault in the folio of the memfd, which consumes the reservation and allocates node 0's free folio, and immediately punch a hole, freeing the folio without restoring the reservation. Node 0 has no surplus folios, so the folio is kept.

The pool ends up with a free surplus folio on node 1 that nobody reserved:

  HugePages_Total:       2
  HugePages_Free:        2
  HugePages_Rsvd:        0
  HugePages_Surp:        1

Let's fix it by freeing a surplus folio of another node when free_huge_folio() keeps a folio whose node has no surplus folios, using the same rule as for the folio's own node: the free folios cover all reservations without it.

With this fix, the surplus folio is already freed in (3), as the two free folios then exceed the single reservation, and we correctly end up with a single free folio on node 0 and no surplus folios.

Note that the freed surplus folio might have been required by a task that was bound to that node. But we don't care about that: reservations are inherently not NUMA aware, and anybody who needs guarantees on specific nodes has to preallocate, like QEMU does, for example.

This has low urgency.

Fixes 7893d1d505d5 ("hugetlb: Try to grow hugetlb pool for MAP_PRIVATE mappings")
---
 mm/hugetlb.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
mm/hugetlb.c +11 -6
index 662cb87fe740a..5984377160d37 100644
@@ -142,6 +142,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
 				unsigned long end, struct folio *folio,
 				zap_flags_t zap_flags);
 static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
+static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
+		nodemask_t *nodes_allowed, bool acct_surplus);
 
 static inline bool subpool_is_free(struct hugepage_subpool *spool)
 {
@@ -1767,6 +1769,7 @@ void free_huge_folio(struct folio *folio)
 	struct hstate *h = folio_hstate(folio);
 	int nid = folio_nid(folio);
 	struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
+	struct folio *surplus_folio = NULL;
 	bool restore_reserve;
 	unsigned long flags;
 
@@ -1815,19 +1818,35 @@ void free_huge_folio(struct folio *folio)
 		folio_clear_hugetlb_temporary(folio);
 		spin_unlock_irqrestore(&hugetlb_lock, flags);
 		update_and_free_hugetlb_folio(h, folio, true);
+		return;
 	} 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);
-	} 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);
+		return;
 	}
+
+	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);
+

mm/hugetlb.c void free_huge_folio(struct folio *folio) · line 1845

Quoted 10 lines
mm/hugetlb.c +10 -0
+	/*
+	 * 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);

with Chris's hwpoison check folded in here:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#31 lore raw

PATCH v2 10/35 mm/hugetlb: wait for deferred frees before failing an allocation

In reply to David Hildenbrand (Arm)

With optimized vmemmap, we defer freeing surplus folios to a workqueue, which restores the vmemmap and frees the folio, or adds it back to the pool if restoring the vmemmap fails.

However, until the worker has run, the memory is neither in the buddy nor in the pool.

Consequently, a task that unmaps surplus folios and immediately faults in new ones can get SIGBUS.

Reproduced by artificially delaying the worker, filling memory with surplus folios, unmapping one and touching a new one: the task gets SIGBUS.

Let's fix it by waiting for deferred frees and retrying once if an allocation fails while frees are pending.

This has low urgency, as it requires overcommitting surplus folios under memory pressure. No need to CC stable.

Fixes b65d4adbc0f0 ("mm: hugetlb: defer freeing of HugeTLB pages")
---
 mm/hugetlb.c | 3 +++
 1 file changed, 3 insertions(+)
mm/hugetlb.c +3 -0
index 5984377160d37..d7ced61b4d2d4 100644
@@ -3218,6 +3218,9 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
 	};
 
 	folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
+	/* Wait for deferred freeing and retry once before giving up. */
+	if (IS_ERR(folio) && flush_work(&free_hpage_work))
+		folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
 
 	mpol_cond_put(mpol);
 
Signature · 1 line
2.56.1
#32 lore raw

Re: [PATCH v2 10/35] mm/hugetlb: wait for deferred frees before failing an allocation

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] mm/hugetlb: missed retry when free_hpage_work finishes before flush_work() - [Medium] mm/hugetlb…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] mm/hugetlb: missed retry when free_hpage_work finishes before flush_work()
- [Medium] mm/hugetlb: futile retry and double reclaim on cgroup charge failure
- [Medium] mm/hugetlb: unconditional flush_work() stalls unrelated allocation failures
- [Medium] mm/hugetlb: flush_work() on !WQ_MEM_RECLAIM wq in __alloc_hugetlb_folio()
--

commit 43bf56053c89d72ce19fd9c0b36b6e5378a398ed
Author: David Hildenbrand (Arm) <[email protected]>

mm/hugetlb: wait for deferred frees before failing an allocation

This commit flushes free_hpage_work in __alloc_hugetlb_folio() and retries hugetlb_alloc_folio() once when the initial allocation returns an error.

mm/hugetlb.c static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma, · line 3225

Quoted 13 lines
mm/hugetlb.c +3 -0
index 5984377160d37..d7ced61b4d2d4 100644
@@ -3218,6 +3218,9 @@ static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
 	};
 
 	folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
+	/* Wait for deferred freeing and retry once before giving up. */
+	if (IS_ERR(folio) && flush_work(&free_hpage_work))
+		folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
 
 	mpol_cond_put(mpol);

[Severity: Medium]
Can gating the retry on flush_work(&free_hpage_work) miss folios freed while the first hugetlb_alloc_folio() attempt was in progress?

hugetlb_alloc_folio() checks h->hugepage_freelists via dequeue_hugetlb_folio() before dropping hugetlb_lock and entering alloc_buddy_hugetlb_folio(), and never rechecks the pool on failure:

CPU0 kworker (free_hpage_workfn)
__alloc_hugetlb_folio()
hugetlb_alloc_folio()
dequeue_hugetlb_folio() -> NULL alloc_buddy_hugetlb_folio()
(direct reclaim/compaction)
__update_and_free_hugetlb_folio()
hugetlb_vmemmap_restore_folio() fails add_hugetlb_folio() (back to pool)
work completes (now idle)
returns ERR_PTR(-ENOSPC)
flush_work(&free_hpage_work) -> false <-- retry skipped

Because flush_work() returns false once the work item is idle, the retry is skipped and the task still gets SIGBUS even though a folio is free in h->hugepage_freelists.

[Severity: Medium]
Should the flush and retry happen only when pool and buddy allocation fail, rather than on every ERR_PTR from hugetlb_alloc_folio()?

free_huge_folio() uncharges hugetlb_cgroup and mem_cgroup synchronously before deferring freeing, so flushing free_hpage_work cannot resolve a cgroup charge failure.

Worse, when a newly allocated surplus folio with optimized vmemmap fails mem_cgroup_charge_hugetlb(), hugetlb_alloc_folio() calls folio_put() before returning ERR_PTR(-ENOSPC). That folio_put() schedules free_hpage_work itself, causing flush_work() to return true and trigger a futile second allocation, memcg reclaim pass, and vmemmap TLB flush.

Moving the wait and retry inside hugetlb_alloc_folio() when alloc_buddy_hugetlb_folio() fails would avoid this and also cover direct callers of hugetlb_alloc_folio().

[Severity: Medium]
Does calling flush_work(&free_hpage_work) unconditionally stall unrelated allocation failures while holding fault and VMA locks?

Expected non-fatal paths such as hugetlb_wp() owner CoW fallback (which falls back to unmap_ref_private() when no spare pages exist) hold hugetlb_fault_mutex_table, hugetlb_vma_lock_read(), and the mmap/VMA lock across __alloc_hugetlb_folio().

With this change, even hstates without vmemmap optimization or overcommit block for the full sequential drain of hpage_freelist in free_hpage_workfn(), and still acquire raw_spin_lock_irq(&pool->lock) in start_flush_work() on every failure once free_hpage_work has run once.

[Severity: Medium]
Could flush_work(&free_hpage_work) here flush a !WQ_MEM_RECLAIM workqueue from a WQ_MEM_RECLAIM worker?

update_and_free_hugetlb_folio() queues free_hpage_work via schedule_work() onto system_percpu_wq, which lacks WQ_MEM_RECLAIM. Meanwhile, mlx5 ODP faults on HugeTLB VMAs run on the WQ_MEM_RECLAIM mlx5_ib_page_fault workqueue:

mlx5_ib_eqe_pf_action() -> ... -> ib_umem_odp_map_dma_and_lock() -> hmm_range_fault_unlocked_timeout() -> hmm_do_fault() -> handle_mm_fault() -> hugetlb_fault() -> __alloc_hugetlb_folio()

If hugetlb_alloc_folio() fails while free_hpage_work is pending, check_flush_dependency() triggers WARN_ONCE and risks a forward-progress deadlock under memory pressure if system_percpu_wq has no idle worker. Moving free_hpage_work to a WQ_MEM_RECLAIM workqueue, or skipping the flush when current_is_workqueue_mem_reclaim() or PF_MEMALLOC is set, would avoid this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
maintainer tree notice
#33 lore raw

+ mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: mm/hugetlb: wait for deferred frees before failing an allocation has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: wait for deferred frees before failing an allocation
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.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)" <[email protected]>
Subject: mm/hugetlb: wait for deferred frees before failing an allocation
Date: Thu, 03 Dec 2026 12:37:33 +0100

With optimized vmemmap, we defer freeing surplus folios to a workqueue, which restores the vmemmap and frees the folio, or adds it back to the pool if restoring the vmemmap fails.

However, until the worker has run, the memory is neither in the buddy nor in the pool.

Consequently, a task that unmaps surplus folios and immediately faults in new ones can get SIGBUS.

Reproduced by artificially delaying the worker, filling memory with surplus folios, unmapping one and touching a new one: the task gets SIGBUS.

Let's fix it by waiting for deferred frees and retrying once if an allocation fails while frees are pending.

This has low urgency, as it requires overcommitting surplus folios under memory pressure. No need to CC stable.

Fixes b65d4adbc0f0 ("mm: hugetlb: defer freeing of HugeTLB pages")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |    3 +++
 1 file changed, 3 insertions(+)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation +3 -0
@@ -3220,6 +3220,9 @@ static struct folio *__alloc_hugetlb_fol
 	};
 
 	folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
+	/* Wait for deferred freeing and retry once before giving up. */
+	if (IS_ERR(folio) && flush_work(&free_hpage_work))
+		folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
 
 	mpol_cond_put(mpol);
 

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 10/35
#34 lore raw

Re: [PATCH v2 10/35] mm/hugetlb: wait for deferred frees before failing an allocation

In reply to David Hildenbrand (Arm)
Quoted 17 lines · David Hildenbrand (Arm)

With optimized vmemmap, we defer freeing surplus folios to a workqueue, which restores the vmemmap and frees the folio, or adds it back to the pool if restoring the vmemmap fails.

However, until the worker has run, the memory is neither in the buddy nor in the pool.

Consequently, a task that unmaps surplus folios and immediately faults in new ones can get SIGBUS.

Reproduced by artificially delaying the worker, filling memory with surplus folios, unmapping one and touching a new one: the task gets SIGBUS.

Let's fix it by waiting for deferred frees and retrying once if an allocation fails while frees are pending.

Quoted 6 lines

This has low urgency, as it requires overcommitting surplus folios under memory pressure. No need to CC stable.

Fixes b65d4adbc0f0 ("mm: hugetlb: defer freeing of HugeTLB pages")

with sashiko's WQ_MEM_RECLAIM point addressed feel free to add:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#35 lore raw

PATCH v2 11/35 mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it

In reply to David Hildenbrand (Arm)

With hugetlb_cma=, hugetlb_cma_reserve() reserves CMA areas for gigantic huge pages. If it ends up not reserving anything, it sets hugetlb_cma_size to 0, as it is the signal to the rest of hugetlb that the hugetlb CMA area exists.

However, if the architecture does not support hugetlb CMA, hugetlb_cma_reserve() only warns and returns, but leaves hugetlb_cma_size as set by the command line.

Consequently, we skip the boot-time allocation of gigantic huge pages, which matters on architectures where gigantic huge pages are allocated at boot, like powerpc with the hash MMU in an LPAR, where the firmware provides 16G pages. Further, we disable demotion of small huge pages and don't clear hugetlb_cma_only, so gigantic huge pages cannot be allocated at all.

This only affects architectures with gigantic huge pages but without hugetlb CMA support, such as sparc64, sh, or powerpc with the hash MMU in an LPAR.

The fix is trivial, so let's just reset hugetlb_cma_size like we do on the other paths that don't reserve anything.

This has low urgency, as it requires passing hugetlb_cma= on one of these architectures. No need to CC stable.

Fixes dbda8feadfa4 ("mm/hugetlb: avoid hardcoding while checking if cma is enabled")
---
 mm/hugetlb_cma.c | 1 +
 1 file changed, 1 insertion(+)
mm/hugetlb_cma.c +1 -0
index 3e5a30571524a..0a63197a9fc9c 100644
@@ -249,6 +249,7 @@ void __init hugetlb_cma_reserve(void)
 	order = arch_hugetlb_cma_order();
 	if (!order) {
 		pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
+		hugetlb_cma_size = 0;
 		return;
 	}
 
Signature · 1 line
2.56.1
maintainer tree notice
#36 lore raw

+ mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.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)" <[email protected]>
Subject: mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it
Date: Thu, 03 Dec 2026 12:37:34 +0100

With hugetlb_cma=, hugetlb_cma_reserve() reserves CMA areas for gigantic huge pages. If it ends up not reserving anything, it sets hugetlb_cma_size to 0, as it is the signal to the rest of hugetlb that the hugetlb CMA area exists.

However, if the architecture does not support hugetlb CMA, hugetlb_cma_reserve() only warns and returns, but leaves hugetlb_cma_size as set by the command line.

Consequently, we skip the boot-time allocation of gigantic huge pages, which matters on architectures where gigantic huge pages are allocated at boot, like powerpc with the hash MMU in an LPAR, where the firmware provides 16G pages. Further, we disable demotion of small huge pages and don't clear hugetlb_cma_only, so gigantic huge pages cannot be allocated at all.

This only affects architectures with gigantic huge pages but without hugetlb CMA support, such as sparc64, sh, or powerpc with the hash MMU in an LPAR.

The fix is trivial, so let's just reset hugetlb_cma_size like we do on the other paths that don't reserve anything.

This has low urgency, as it requires passing hugetlb_cma= on one of these architectures. No need to CC stable.

Fixes dbda8feadfa4 ("mm/hugetlb: avoid hardcoding while checking if cma is enabled")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb_cma.c |    1 +
 1 file changed, 1 insertion(+)
mm/hugetlb_cma.c renamed from mm/hugetlb_cma.c~mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it +1 -0
@@ -249,6 +249,7 @@ void __init hugetlb_cma_reserve(void)
 	order = arch_hugetlb_cma_order();
 	if (!order) {
 		pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
+		hugetlb_cma_size = 0;
 		return;
 	}
 

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 11/35
#37 lore raw

Re: [PATCH v2 11/35] mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it

In reply to David Hildenbrand (Arm)
Quoted 27 lines · David Hildenbrand (Arm)

With hugetlb_cma=, hugetlb_cma_reserve() reserves CMA areas for gigantic huge pages. If it ends up not reserving anything, it sets hugetlb_cma_size to 0, as it is the signal to the rest of hugetlb that the hugetlb CMA area exists.

However, if the architecture does not support hugetlb CMA, hugetlb_cma_reserve() only warns and returns, but leaves hugetlb_cma_size as set by the command line.

Consequently, we skip the boot-time allocation of gigantic huge pages, which matters on architectures where gigantic huge pages are allocated at boot, like powerpc with the hash MMU in an LPAR, where the firmware provides 16G pages. Further, we disable demotion of small huge pages and don't clear hugetlb_cma_only, so gigantic huge pages cannot be allocated at all.

This only affects architectures with gigantic huge pages but without hugetlb CMA support, such as sparc64, sh, or powerpc with the hash MMU in an LPAR.

The fix is trivial, so let's just reset hugetlb_cma_size like we do on the other paths that don't reserve anything.

Quoted 6 lines

This has low urgency, as it requires passing hugetlb_cma= on one of these architectures. No need to CC stable.

Fixes dbda8feadfa4 ("mm/hugetlb: avoid hardcoding while checking if cma is enabled")

Thanks.

Reviewed-by on v2 11/35
#38 lore raw

Re: [PATCH v2 11/35] mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it

In reply to David Hildenbrand (Arm)
Quoted 23 lines · David Hildenbrand (Arm)

With hugetlb_cma=, hugetlb_cma_reserve() reserves CMA areas for gigantic huge pages. If it ends up not reserving anything, it sets hugetlb_cma_size to 0, as it is the signal to the rest of hugetlb that the hugetlb CMA area exists.

However, if the architecture does not support hugetlb CMA, hugetlb_cma_reserve() only warns and returns, but leaves hugetlb_cma_size as set by the command line.

Consequently, we skip the boot-time allocation of gigantic huge pages, which matters on architectures where gigantic huge pages are allocated at boot, like powerpc with the hash MMU in an LPAR, where the firmware provides 16G pages. Further, we disable demotion of small huge pages and don't clear hugetlb_cma_only, so gigantic huge pages cannot be allocated at all.

This only affects architectures with gigantic huge pages but without hugetlb CMA support, such as sparc64, sh, or powerpc with the hash MMU in an LPAR.

The fix is trivial, so let's just reset hugetlb_cma_size like we do on the other paths that don't reserve anything.

Quoted 6 lines

This has low urgency, as it requires passing hugetlb_cma= on one of these architectures. No need to CC stable.

Fixes dbda8feadfa4 ("mm/hugetlb: avoid hardcoding while checking if cma is enabled")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#39 lore raw

PATCH v2 12/35 mm/hugetlb: fix truncated copy result in UFFDIO_COPY

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

Folios that span more bytes than can fit into an unsigned integer (e.g., 16 GiB folios on arm64) are problematic with UFFDIO_COPY: the result from copy_folio_from_user() can be truncated, possibly indicating success instead of an error.

We will then continue with the folio that has stale data, and make it accessible to user space.

For example, if copy_folio_from_user() fails such that the number of bytes not copied is a multiple of 4 GiB, we'll get an effective ret == 0.

Let's fix it by simply testing the return value directly.

This has medium urgency, as it's an information leak.

Fixes 8fb5debc5fcd ("userfaultfd: hugetlbfs: add hugetlb_mcopy_atomic_pte for userfaultfd support")
[ david: cleanup code and description ]
---
 mm/hugetlb.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/hugetlb.c +1 -4
index d7ced61b4d2d4..0d242ebfb07d2 100644
@@ -6606,11 +6606,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 			goto out;
 		}
 
-		ret = copy_folio_from_user(folio, (const void __user *) src_addr,
-					   false);
-
 		/* fallback to copy_from_user outside mmap_lock */
-		if (unlikely(ret)) {
+		if (unlikely(copy_folio_from_user(folio, (const void __user *)src_addr, false))) {
 			ret = -ENOENT;
 			/* Free the allocated folio which may have
 			 * consumed a reservation.
Signature · 1 line
2.56.1
maintainer tree notice
#40 lore raw

+ mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: fix truncated copy result in UFFDIO_COPY has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch

The patch titled
Subject: mm/hugetlb: fix truncated copy result in UFFDIO_COPY
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.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: Chris Mason <[email protected]>
Subject: mm/hugetlb: fix truncated copy result in UFFDIO_COPY
Date: Thu, 03 Dec 2026 12:37:35 +0100

Folios that span more bytes than can fit into an unsigned integer (e.g., 16 GiB folios on arm64) are problematic with UFFDIO_COPY: the result from copy_folio_from_user() can be truncated, possibly indicating success instead of an error.

We will then continue with the folio that has stale data, and make it accessible to user space.

For example, if copy_folio_from_user() fails such that the number of bytes not copied is a multiple of 4 GiB, we'll get an effective ret == 0.

Let's fix it by simply testing the return value directly.

This has medium urgency, as it's an information leak.

[[email protected]: cleanup code and description]
Fixes 8fb5debc5fcd ("userfaultfd: hugetlbfs: add hugetlb_mcopy_atomic_pte for userfaultfd support")
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy +1 -4
@@ -6581,11 +6581,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_
 			goto out;
 		}
 
-		ret = copy_folio_from_user(folio, (const void __user *) src_addr,
-					   false);
-
 		/* fallback to copy_from_user outside mmap_lock */
-		if (unlikely(ret)) {
+		if (unlikely(copy_folio_from_user(folio, (const void __user *)src_addr, false))) {
 			ret = -ENOENT;
 			/* Free the allocated folio which may have
 			 * consumed a reservation.

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch

Reviewed-by on v2 12/35
#41 lore raw

Re: [PATCH v2 12/35] mm/hugetlb: fix truncated copy result in UFFDIO_COPY

In reply to David Hildenbrand (Arm)
Quoted 18 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

Folios that span more bytes than can fit into an unsigned integer (e.g., 16 GiB folios on arm64) are problematic with UFFDIO_COPY: the result from copy_folio_from_user() can be truncated, possibly indicating success instead of an error.

We will then continue with the folio that has stale data, and make it accessible to user space.

For example, if copy_folio_from_user() fails such that the number of bytes not copied is a multiple of 4 GiB, we'll get an effective ret == 0.

Let's fix it by simply testing the return value directly.

This has medium urgency, as it's an information leak.

Quoted 8 lines
Fixes 8fb5debc5fcd ("userfaultfd: hugetlbfs: add hugetlb_mcopy_atomic_pte for userfaultfd support")
[ david: cleanup code and description ]

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#42 lore raw

PATCH v2 13/35 fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs

In reply to David Hildenbrand (Arm)

hugetlb does not support soft-dirty tracking: since commit f96f7a40874d ("mm/hugetlb: fix hugetlb not supporting softdirty tracking"), vma_wants_writenotify() ignores VM_SOFTDIRTY for hugetlb VMAs, and clear_refs never clears the soft-dirty bits of hugetlb entries.

However, clear_refs still clears VM_SOFTDIRTY on hugetlb VMAs. With CONFIG_MEM_SOFT_DIRTY, a FOLL_FORCE write to a private hugetlb mapping without VM_WRITE can then loop in __get_user_pages() until the task is killed, with the mmap_lock held for reading:

__get_user_pages()
retry:
follow_page_mask()
follow_huge_pmd()
can_follow_write_pmd()
pmd_needs_soft_dirty_wp() is true
returns NULL
faultin_page()
hugetlb_wp()
set_huge_ptep_maybe_writable()
no VM_WRITE: the entry stays as it is
returns 0
goto retry

Let's fix it by not clearing VM_SOFTDIRTY on hugetlb VMAs, so we always treat hugetlb pages as soft-dirty.

This has low urgency, as the loop can be killed.

Fixes 052ccfbcc6cd ("mm/hugetlb: support FOLL_FORCE|FOLL_WRITE")
---
 fs/proc/task_mmu.c | 3 +++
 1 file changed, 3 insertions(+)
fs/proc/task_mmu.c +3 -0
index d2a524c6ad4b9..de8eded04e32a 100644
@@ -1802,6 +1802,9 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 
 		if (type == CLEAR_REFS_SOFT_DIRTY) {
 			for_each_vma(vmi, vma) {
+				/* hugetlb does not support soft-dirty tracking. */
+				if (vma_is_hugetlb(vma))
+					continue;
 				if (!(vma->vm_flags & VM_SOFTDIRTY))
 					continue;
 				vm_flags_clear(vma, VM_SOFTDIRTY);
Signature · 1 line
2.56.1
maintainer tree notice
#43 lore raw

+ fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch added to mm-unstable branch

The patch titled Subject: fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs
has been added to the -mm mm-unstable branch. Its filename is
fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.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)" <[email protected]>
Subject: fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs
Date: Thu, 03 Dec 2026 12:37:36 +0100

hugetlb does not support soft-dirty tracking: since commit f96f7a40874d ("mm/hugetlb: fix hugetlb not supporting softdirty tracking"), vma_wants_writenotify() ignores VM_SOFTDIRTY for hugetlb VMAs, and clear_refs never clears the soft-dirty bits of hugetlb entries.

However, clear_refs still clears VM_SOFTDIRTY on hugetlb VMAs. With CONFIG_MEM_SOFT_DIRTY, a FOLL_FORCE write to a private hugetlb mapping without VM_WRITE can then loop in __get_user_pages() until the task is killed, with the mmap_lock held for reading:

__get_user_pages()
retry:
follow_page_mask()
follow_huge_pmd()
can_follow_write_pmd()
pmd_needs_soft_dirty_wp() is true
returns NULL
faultin_page()
hugetlb_wp()
set_huge_ptep_maybe_writable()
no VM_WRITE: the entry stays as it is
returns 0
goto retry

Let's fix it by not clearing VM_SOFTDIRTY on hugetlb VMAs, so we always treat hugetlb pages as soft-dirty.

This has low urgency, as the loop can be killed.

Fixes 052ccfbcc6cd ("mm/hugetlb: support FOLL_FORCE|FOLL_WRITE")
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/proc/task_mmu.c |    3 +++
 1 file changed, 3 insertions(+)
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas +3 -0
@@ -1807,6 +1807,9 @@ static ssize_t clear_refs_write(struct f
 
 		if (type == CLEAR_REFS_SOFT_DIRTY) {
 			for_each_vma(vmi, vma) {
+				/* hugetlb does not support soft-dirty tracking. */
+				if (vma_is_hugetlb(vma))
+					continue;
 				if (!vma_test(vma, VMA_SOFTDIRTY_BIT))
 					continue;
 				vma_start_write(vma);

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 13/35
#44 lore raw

Re: [PATCH v2 13/35] fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs

In reply to David Hildenbrand (Arm)
Quoted 27 lines · David Hildenbrand (Arm)

hugetlb does not support soft-dirty tracking: since commit f96f7a40874d ("mm/hugetlb: fix hugetlb not supporting softdirty tracking"), vma_wants_writenotify() ignores VM_SOFTDIRTY for hugetlb VMAs, and clear_refs never clears the soft-dirty bits of hugetlb entries.

However, clear_refs still clears VM_SOFTDIRTY on hugetlb VMAs. With CONFIG_MEM_SOFT_DIRTY, a FOLL_FORCE write to a private hugetlb mapping without VM_WRITE can then loop in __get_user_pages() until the task is killed, with the mmap_lock held for reading:

__get_user_pages()
retry:
follow_page_mask()
follow_huge_pmd()
can_follow_write_pmd()
pmd_needs_soft_dirty_wp() is true
returns NULL
faultin_page()
hugetlb_wp()
set_huge_ptep_maybe_writable()
no VM_WRITE: the entry stays as it is
returns 0
goto retry

Let's fix it by not clearing VM_SOFTDIRTY on hugetlb VMAs, so we always treat hugetlb pages as soft-dirty.

Quoted 7 lines

This has low urgency, as the loop can be killed.

Fixes 052ccfbcc6cd ("mm/hugetlb: support FOLL_FORCE|FOLL_WRITE")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#45 lore raw

PATCH v2 14/35 hugetlbfs: fix integer overflow when punching holes

In reply to David Hildenbrand (Arm)

hugetlbfs_punch_hole() rounds up the offset to find the first full huge page in the hole, and zeroes the partial huge page before it, starting at the offset.

However, if the offset falls into the last possible huge page below LLONG_MAX, but not at its start, rounding up overflows and hole_start becomes LLONG_MIN.

Consequently, we will zero from the start of that huge page, not from the offset.

Reproduced by allocating that huge page with fallocate(), placing data in it through udmabuf, and punching a hole starting inside it: the data before the hole gets zeroed.

Let's fix it by clamping hole_start to offset + len on overflow, as there are no full huge pages in the hole in that case.

This has low urgency: hugetlbfs supports neither write() nor mmap() of that last huge page, so only something like udmabuf could place data there, which no reasonable use case does. No need to CC stable.

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
---
 fs/hugetlbfs/inode.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +5 -1
index 4a92f31c88e1a..8ac8925d7ad8b 100644
@@ -672,8 +672,12 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
 
 	/*
 	 * hole_start and hole_end indicate the full pages within the hole.
+	 * If rounding up overflows, there are no full pages in the hole.
 	 */
-	hole_start = round_up(offset, hpage_size);
+	if (check_add_overflow(offset, hpage_size - 1, &hole_start))
+		hole_start = offset + len;
+	else
+		hole_start = round_down(hole_start, hpage_size);
 	hole_end = round_down(offset + len, hpage_size);
 
 	inode_lock(inode);
Signature · 1 line
2.56.1
maintainer tree notice
#46 lore raw

+ hugetlbfs-fix-integer-overflow-when-punching-holes.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: fix integer overflow when punching holes has been added to the -mm mm-unstable branch. Its filename is hugetlbfs-fix-integer-overflow-when-punching-holes.patch

The patch titled
Subject: hugetlbfs: fix integer overflow when punching holes
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-fix-integer-overflow-when-punching-holes.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-fix-integer-overflow-when-punching-holes.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)" <[email protected]>
Subject: hugetlbfs: fix integer overflow when punching holes
Date: Thu, 03 Dec 2026 12:37:37 +0100

hugetlbfs_punch_hole() rounds up the offset to find the first full huge page in the hole, and zeroes the partial huge page before it, starting at the offset.

However, if the offset falls into the last possible huge page below LLONG_MAX, but not at its start, rounding up overflows and hole_start becomes LLONG_MIN.

Consequently, we will zero from the start of that huge page, not from the offset.

Reproduced by allocating that huge page with fallocate(), placing data in it through udmabuf, and punching a hole starting inside it: the data before the hole gets zeroed.

Let's fix it by clamping hole_start to offset + len on overflow, as there are no full huge pages in the hole in that case.

This has low urgency: hugetlbfs supports neither write() nor mmap() of that last huge page, so only something like udmabuf could place data there, which no reasonable use case does. No need to CC stable.

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-fix-integer-overflow-when-punching-holes +5 -1
@@ -672,8 +672,12 @@ static long hugetlbfs_punch_hole(struct
 
 	/*
 	 * hole_start and hole_end indicate the full pages within the hole.
+	 * If rounding up overflows, there are no full pages in the hole.
 	 */
-	hole_start = round_up(offset, hpage_size);
+	if (check_add_overflow(offset, hpage_size - 1, &hole_start))
+		hole_start = offset + len;
+	else
+		hole_start = round_down(hole_start, hpage_size);
 	hole_end = round_down(offset + len, hpage_size);
 
 	inode_lock(inode);

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 14/35
#47 lore raw

Re: [PATCH v2 14/35] hugetlbfs: fix integer overflow when punching holes

In reply to David Hildenbrand (Arm)
Quoted 22 lines · David Hildenbrand (Arm)

hugetlbfs_punch_hole() rounds up the offset to find the first full huge page in the hole, and zeroes the partial huge page before it, starting at the offset.

However, if the offset falls into the last possible huge page below LLONG_MAX, but not at its start, rounding up overflows and hole_start becomes LLONG_MIN.

Consequently, we will zero from the start of that huge page, not from the offset.

Reproduced by allocating that huge page with fallocate(), placing data in it through udmabuf, and punching a hole starting inside it: the data before the hole gets zeroed.

Let's fix it by clamping hole_start to offset + len on overflow, as there are no full huge pages in the hole in that case.

Quoted 7 lines

This has low urgency: hugetlbfs supports neither write() nor mmap() of that last huge page, so only something like udmabuf could place data there, which no reasonable use case does. No need to CC stable.

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")

Thanks.

Reviewed-by on v2 14/35
#48 lore raw

Re: [PATCH v2 14/35] hugetlbfs: fix integer overflow when punching holes

In reply to David Hildenbrand (Arm)
Quoted 18 lines · David Hildenbrand (Arm)

hugetlbfs_punch_hole() rounds up the offset to find the first full huge page in the hole, and zeroes the partial huge page before it, starting at the offset.

However, if the offset falls into the last possible huge page below LLONG_MAX, but not at its start, rounding up overflows and hole_start becomes LLONG_MIN.

Consequently, we will zero from the start of that huge page, not from the offset.

Reproduced by allocating that huge page with fallocate(), placing data in it through udmabuf, and punching a hole starting inside it: the data before the hole gets zeroed.

Let's fix it by clamping hole_start to offset + len on overflow, as there are no full huge pages in the hole in that case.

Quoted 7 lines

This has low urgency: hugetlbfs supports neither write() nor mmap() of that last huge page, so only something like udmabuf could place data there, which no reasonable use case does. No need to CC stable.

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")

LGTM:

Reviewed-by Jose A. Perez de Azpillaga <azpijr
Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#49 lore raw

PATCH v2 15/35 hugetlbfs: don't zero poisoned pages in a hole punch

In reply to David Hildenbrand (Arm)

Since commit 8625147cafaa ("hugetlbfs: don't delete error page from pagecache"), a poisoned hugetlb folio stays in the page cache, and we must not access its poisoned pages.

However, hugetlbfs_zero_partial_page() zeroes the partial folio of an unaligned hole punch without checking for poison, which can cause issues like machine-checks.

Let's fix it by simply not zeroing poisoned folios. We don't really care about their content: the folio can no longer be mapped, and read() only returns data up to the first poisoned page.

Only zeroing the pages that are not poisoned would need more thought, as we cannot reliably identify the poisoned pages; that is future work.

Note that a folio that gets poisoned concurrently, after we checked it, can still be zeroed, and sync'ing with memory failure handling is also left as future work.

This has low urgency, as it requires a hole punch on a poisoned folio.

Fixes 8625147cafaa ("hugetlbfs: don't delete error page from pagecache")
---
 fs/hugetlbfs/inode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +2 -1
index 8ac8925d7ad8b..dd0cc0384a181 100644
@@ -656,7 +656,8 @@ static void hugetlbfs_zero_partial_page(struct hstate *h,
 	if (!end)
 		end = huge_page_size(h);
 
-	folio_zero_segment(folio, (size_t)start, (size_t)end);
+	if (!folio_test_hwpoison(folio))
+		folio_zero_segment(folio, (size_t)start, (size_t)end);
 
 	folio_unlock(folio);
 	folio_put(folio);
Signature · 1 line
2.56.1
maintainer tree notice
#50 lore raw

+ hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: don't zero poisoned pages in a hole punch has been added to the -mm mm-unstable branch. Its filename is hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch

The patch titled
Subject: hugetlbfs: don't zero poisoned pages in a hole punch
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.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)" <[email protected]>
Subject: hugetlbfs: don't zero poisoned pages in a hole punch
Date: Thu, 03 Dec 2026 12:37:38 +0100

Since commit 8625147cafaa ("hugetlbfs: don't delete error page from pagecache"), a poisoned hugetlb folio stays in the page cache, and we must not access its poisoned pages.

However, hugetlbfs_zero_partial_page() zeroes the partial folio of an unaligned hole punch without checking for poison, which can cause issues like machine-checks.

Let's fix it by simply not zeroing poisoned folios. We don't really care about their content: the folio can no longer be mapped, and read() only returns data up to the first poisoned page.

Only zeroing the pages that are not poisoned would need more thought, as we cannot reliably identify the poisoned pages; that is future work.

Note that a folio that gets poisoned concurrently, after we checked it, can still be zeroed, and sync'ing with memory failure handling is also left as future work.

This has low urgency, as it requires a hole punch on a poisoned folio.

Fixes 8625147cafaa ("hugetlbfs: don't delete error page from pagecache")
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
 fs/hugetlbfs/inode.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch +2 -1
@@ -656,7 +656,8 @@ static void hugetlbfs_zero_partial_page(
 	if (!end)
 		end = huge_page_size(h);
 
-	folio_zero_segment(folio, (size_t)start, (size_t)end);
+	if (!folio_test_hwpoison(folio))
+		folio_zero_segment(folio, (size_t)start, (size_t)end);
 
 	folio_unlock(folio);
 	folio_put(folio);

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 15/35
#51 lore raw

Re: [PATCH v2 15/35] hugetlbfs: don't zero poisoned pages in a hole punch

In reply to David Hildenbrand (Arm)
Quoted 19 lines · David Hildenbrand (Arm)

Since commit 8625147cafaa ("hugetlbfs: don't delete error page from pagecache"), a poisoned hugetlb folio stays in the page cache, and we must not access its poisoned pages.

However, hugetlbfs_zero_partial_page() zeroes the partial folio of an unaligned hole punch without checking for poison, which can cause issues like machine-checks.

Let's fix it by simply not zeroing poisoned folios. We don't really care about their content: the folio can no longer be mapped, and read() only returns data up to the first poisoned page.

Only zeroing the pages that are not poisoned would need more thought, as we cannot reliably identify the poisoned pages; that is future work.

Note that a folio that gets poisoned concurrently, after we checked it, can still be zeroed, and sync'ing with memory failure handling is also left as future work.

Quoted 8 lines

This has low urgency, as it requires a hole punch on a poisoned folio.

Fixes 8625147cafaa ("hugetlbfs: don't delete error page from pagecache")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#52 lore raw

PATCH v2 16/35 hugetlbfs: fix deadlock between hole punching and memory failure

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

Punching a hole that isn't aligned to the huge page size can deadlock against memory_failure() on a mapped folio at the edge of the hole.

hugetlbfs_punch_hole() locks and zeroes the partial folios with i_mmap_rwsem held for write, following the hugetlbfs locking order documented in mm/rmap.c: i_mmap_rwsem before the folio lock.

However, try_memory_failure_hugetlb() locks the folio first, and then collects the processes to kill under i_mmap_rwsem:

hugetlbfs_punch_hole() try_memory_failure_hugetlb()
====================== ============================
i_mmap_lock_write()
folio_lock()
hugetlbfs_zero_partial_page() hwpoison_user_mappings()
filemap_lock_hugetlb_folio() collect_procs_file()
waits for the folio lock i_mmap_lock_read()
waits for the writer

In fact, the documentation is outdated: hugetlbfs_punch_hole() is the *only* one that still takes i_mmap_rwsem before the folio lock. All other scenarios always take the folio lock before the i_mmap_rwsem.

Only memory failure can deadlock with the hole punch:

* truncation and hole punching of full folios are serialized against it through the inode lock
* unmap_ref_private() holds the lock of an anon folio, not of a page cache folio that the hole punch zeroes
* migration only tries to take the i_mmap_rwsem.

There is no need to zero the partial folios under i_mmap_rwsem. The lock only protects the VMA tree and PMD table sharing, nothing about zeroing; the inode lock and the folio lock are sufficient.

Let's fix it by only holding i_mmap_rwsem while unmapping the full folios of the hole.

Update the documented locking order in mm/rmap.c and the function comment above hugetlb_folio_mapping_lock_write(). hugetlb_folio_mapping_lock_write() could now take the lock unconditionally, but we leave that for a separate change.

The order of the folio lock and i_mmap_rwsem now matches the one for other folios, which is nice.

This has low urgency, as the race shouldn't really happen all that often in practice (never seen).

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
[ david: adjust comment and description ]
---
 fs/hugetlbfs/inode.c | 6 ++----
 mm/hugetlb.c         | 3 +--
 mm/rmap.c            | 4 ++--
 3 files changed, 5 insertions(+), 8 deletions(-)
fs/hugetlbfs/inode.c +2 -4
index dd0cc0384a181..4290e88f23c5b 100644
@@ -689,8 +689,6 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
 		return -EPERM;
 	}
 
-	i_mmap_lock_write(mapping);
-
 	/* If range starts before first full page, zero partial page. */
 	if (offset < hole_start)
 		hugetlbfs_zero_partial_page(h, mapping,
@@ -698,10 +696,12 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
 
 	/* Unmap users of full pages in the hole. */
 	if (hole_end > hole_start) {
+		i_mmap_lock_write(mapping);
 		if (mapping_mapped(mapping))
 			hugetlb_vmdelete_list(mapping,
 					      hole_start >> PAGE_SHIFT,
 					      hole_end >> PAGE_SHIFT, 0);
+		i_mmap_unlock_write(mapping);
 	}
 
 	/* If range extends beyond last full page, zero partial page. */
@@ -709,8 +709,6 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
 		hugetlbfs_zero_partial_page(h, mapping,
 				hole_end, offset + len);
 
-	i_mmap_unlock_write(mapping);
-
 	/* Remove full pages from the file. */
 	if (hole_end > hole_start)
 		remove_inode_hugepages(inode, hole_start, hole_end);
mm/hugetlb.c +1 -2
index 0d242ebfb07d2..72d912f488fdc 100644
@@ -1878,8 +1878,7 @@ void init_new_hugetlb_folio(struct folio *folio)
  * Find and lock address space (mapping) in write mode.
  *
  * Upon entry, the folio is locked which means that folio_mapping() is
- * stable.  Due to locking order, we can only trylock_write.  If we can
- * not get the lock, simply return NULL to caller.
+ * stable. If we cannot get the lock immediately, we simply return NULL.
  */
 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio)
 {
mm/rmap.c +2 -2
index fe354fa6cc43a..f35b671d26514 100644
@@ -49,8 +49,8 @@
  *   hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
  *     vma_lock (hugetlb specific lock for pmd_sharing)
  *       mapping->invalidate_lock (when allocating page cache folios)
- *         mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
- *           folio_lock
+ *         folio_lock
+ *           mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
  */
 
 #include <linux/mm.h>
Signature · 1 line
2.56.1
maintainer tree notice
#53 lore raw

+ hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: fix deadlock between hole punching and memory failure has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: hugetlbfs: fix deadlock between hole punching and memory failure
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.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: Chris Mason <[email protected]>
Subject: hugetlbfs: fix deadlock between hole punching and memory failure
Date: Thu, 03 Dec 2026 12:37:39 +0100

Punching a hole that isn't aligned to the huge page size can deadlock against memory_failure() on a mapped folio at the edge of the hole.

hugetlbfs_punch_hole() locks and zeroes the partial folios with i_mmap_rwsem held for write, following the hugetlbfs locking order documented in mm/rmap.c: i_mmap_rwsem before the folio lock.

However, try_memory_failure_hugetlb() locks the folio first, and then collects the processes to kill under i_mmap_rwsem:

hugetlbfs_punch_hole() try_memory_failure_hugetlb()
====================== ============================
i_mmap_lock_write()
folio_lock()
hugetlbfs_zero_partial_page() hwpoison_user_mappings()
filemap_lock_hugetlb_folio() collect_procs_file()
waits for the folio lock i_mmap_lock_read()
waits for the writer

In fact, the documentation is outdated: hugetlbfs_punch_hole() is the *only* one that still takes i_mmap_rwsem before the folio lock. All other scenarios always take the folio lock before the i_mmap_rwsem.

Only memory failure can deadlock with the hole punch:

* truncation and hole punching of full folios are serialized against it through the inode lock
* unmap_ref_private() holds the lock of an anon folio, not of a page cache folio that the hole punch zeroes
* migration only tries to take the i_mmap_rwsem.

There is no need to zero the partial folios under i_mmap_rwsem. The lock only protects the VMA tree and PMD table sharing, nothing about zeroing; the inode lock and the folio lock are sufficient.

Let's fix it by only holding i_mmap_rwsem while unmapping the full folios of the hole.

Update the documented locking order in mm/rmap.c and the function comment above hugetlb_folio_mapping_lock_write(). hugetlb_folio_mapping_lock_write() could now take the lock unconditionally, but we leave that for a separate change.

The order of the folio lock and i_mmap_rwsem now matches the one for other folios, which is nice.

This has low urgency, as the race shouldn't really happen all that often in practice (never seen).

[[email protected]: adjust comment and description]
Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    6 ++----
 mm/hugetlb.c         |    3 +--
 mm/rmap.c            |    4 ++--
 3 files changed, 5 insertions(+), 8 deletions(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure +2 -4
@@ -689,8 +689,6 @@ static long hugetlbfs_punch_hole(struct
 		return -EPERM;
 	}
 
-	i_mmap_lock_write(mapping);
-
 	/* If range starts before first full page, zero partial page. */
 	if (offset < hole_start)
 		hugetlbfs_zero_partial_page(h, mapping,
@@ -698,10 +696,12 @@ static long hugetlbfs_punch_hole(struct
 
 	/* Unmap users of full pages in the hole. */
 	if (hole_end > hole_start) {
+		i_mmap_lock_write(mapping);
 		if (mapping_mapped(mapping))
 			hugetlb_vmdelete_list(mapping,
 					      hole_start >> PAGE_SHIFT,
 					      hole_end >> PAGE_SHIFT, 0);
+		i_mmap_unlock_write(mapping);
 	}
 
 	/* If range extends beyond last full page, zero partial page. */
@@ -709,8 +709,6 @@ static long hugetlbfs_punch_hole(struct
 		hugetlbfs_zero_partial_page(h, mapping,
 				hole_end, offset + len);
 
-	i_mmap_unlock_write(mapping);
-
 	/* Remove full pages from the file. */
 	if (hole_end > hole_start)
 		remove_inode_hugepages(inode, hole_start, hole_end);
mm/hugetlb.c renamed from mm/hugetlb.c~hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure +1 -2
@@ -1878,8 +1878,7 @@ void init_new_hugetlb_folio(struct folio
  * Find and lock address space (mapping) in write mode.
  *
  * Upon entry, the folio is locked which means that folio_mapping() is
- * stable.  Due to locking order, we can only trylock_write.  If we can
- * not get the lock, simply return NULL to caller.
+ * stable. If we cannot get the lock immediately, we simply return NULL.
  */
 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio)
 {
mm/rmap.c renamed from mm/rmap.c~hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure +2 -2
@@ -49,8 +49,8 @@
  *   hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
  *     vma_lock (hugetlb specific lock for pmd_sharing)
  *       mapping->invalidate_lock (when allocating page cache folios)
- *         mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
- *           folio_lock
+ *         folio_lock
+ *           mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
  */
 
 #include <linux/mm.h>

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch

maintainer Acked-by on v2 16/35
#54 lore raw

Re: [PATCH v2 16/35] hugetlbfs: fix deadlock between hole punching and memory failure

In reply to David Hildenbrand (Arm)
Quoted 52 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

Punching a hole that isn't aligned to the huge page size can deadlock against memory_failure() on a mapped folio at the edge of the hole.

hugetlbfs_punch_hole() locks and zeroes the partial folios with i_mmap_rwsem held for write, following the hugetlbfs locking order documented in mm/rmap.c: i_mmap_rwsem before the folio lock.

However, try_memory_failure_hugetlb() locks the folio first, and then collects the processes to kill under i_mmap_rwsem:

hugetlbfs_punch_hole() try_memory_failure_hugetlb()
====================== ============================
i_mmap_lock_write()
folio_lock()
hugetlbfs_zero_partial_page() hwpoison_user_mappings()
filemap_lock_hugetlb_folio() collect_procs_file()
waits for the folio lock i_mmap_lock_read()
waits for the writer

In fact, the documentation is outdated: hugetlbfs_punch_hole() is the *only* one that still takes i_mmap_rwsem before the folio lock. All other scenarios always take the folio lock before the i_mmap_rwsem.

Only memory failure can deadlock with the hole punch:

* truncation and hole punching of full folios are serialized against it through the inode lock
* unmap_ref_private() holds the lock of an anon folio, not of a page cache folio that the hole punch zeroes
* migration only tries to take the i_mmap_rwsem.

There is no need to zero the partial folios under i_mmap_rwsem. The lock only protects the VMA tree and PMD table sharing, nothing about zeroing; the inode lock and the folio lock are sufficient.

Let's fix it by only holding i_mmap_rwsem while unmapping the full folios of the hole.

Update the documented locking order in mm/rmap.c and the function comment above hugetlb_folio_mapping_lock_write(). hugetlb_folio_mapping_lock_write() could now take the lock unconditionally, but we leave that for a separate change.

The order of the folio lock and i_mmap_rwsem now matches the one for other folios, which is nice.

Quoted 8 lines

This has low urgency, as the race shouldn't really happen all that often in practice (never seen).

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
[ david: adjust comment and description ]

THanks.

Reviewed-by on v2 16/35
#55 lore raw

Re: [PATCH v2 16/35] hugetlbfs: fix deadlock between hole punching and memory failure

In reply to David Hildenbrand (Arm)
Quoted 48 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

Punching a hole that isn't aligned to the huge page size can deadlock against memory_failure() on a mapped folio at the edge of the hole.

hugetlbfs_punch_hole() locks and zeroes the partial folios with i_mmap_rwsem held for write, following the hugetlbfs locking order documented in mm/rmap.c: i_mmap_rwsem before the folio lock.

However, try_memory_failure_hugetlb() locks the folio first, and then collects the processes to kill under i_mmap_rwsem:

hugetlbfs_punch_hole() try_memory_failure_hugetlb()
====================== ============================
i_mmap_lock_write()
folio_lock()
hugetlbfs_zero_partial_page() hwpoison_user_mappings()
filemap_lock_hugetlb_folio() collect_procs_file()
waits for the folio lock i_mmap_lock_read()
waits for the writer

In fact, the documentation is outdated: hugetlbfs_punch_hole() is the *only* one that still takes i_mmap_rwsem before the folio lock. All other scenarios always take the folio lock before the i_mmap_rwsem.

Only memory failure can deadlock with the hole punch:

* truncation and hole punching of full folios are serialized against it through the inode lock
* unmap_ref_private() holds the lock of an anon folio, not of a page cache folio that the hole punch zeroes
* migration only tries to take the i_mmap_rwsem.

There is no need to zero the partial folios under i_mmap_rwsem. The lock only protects the VMA tree and PMD table sharing, nothing about zeroing; the inode lock and the folio lock are sufficient.

Let's fix it by only holding i_mmap_rwsem while unmapping the full folios of the hole.

Update the documented locking order in mm/rmap.c and the function comment above hugetlb_folio_mapping_lock_write(). hugetlb_folio_mapping_lock_write() could now take the lock unconditionally, but we leave that for a separate change.

The order of the folio lock and i_mmap_rwsem now matches the one for other folios, which is nice.

Quoted 8 lines

This has low urgency, as the race shouldn't really happen all that often in practice (never seen).

Fixes 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
[ david: adjust comment and description ]

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#56 lore raw

PATCH v2 17/35 hugetlbfs: don't overflow the pseudo VMA address in fallocate()

In reply to David Hildenbrand (Arm)

hugetlbfs_fallocate() allocates folios through a pseudo VMA that starts at address 0 and file offset 0. It passes the file offset of the folio as the address. The reservation code then derives the reserve map index of the folio from that address. Confusing.

However, on 32-bit, that address overflows for file offsets at or above 4 GiB, while we still add the folio to the page cache at the real index.

Consequently, fallocate() of a single hugetlb folio at offset 4 GiB consumes or restores the reservation at index 0, messing up the reservation accounting on truncation and eviction.

Let's fix it by setting the file offset of the folio in the pseudo VMA instead, and passing address 0.

This has low urgency, as it only affects 32-bit kernels with hugetlb. No need to CC stable.

Fixes 70c3547e36f5 ("hugetlbfs: add hugetlbfs_fallocate()")
---
 fs/hugetlbfs/inode.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
fs/hugetlbfs/inode.c +4 -5
index 4290e88f23c5b..81c5a3efdc4dc 100644
@@ -727,7 +727,6 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 	struct hstate *h = hstate_inode(inode);
 	struct vm_area_struct pseudo_vma;
 	struct mm_struct *mm = current->mm;
-	loff_t hpage_size = huge_page_size(h);
 	unsigned long hpage_shift = huge_page_shift(h);
 	pgoff_t start, index, end;
 	int error;
@@ -774,10 +773,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 	for (index = start; index < end; index++) {
 		/*
 		 * This is supposed to be the vaddr where the page is being
-		 * faulted in, but we have no vaddr here.
+		 * faulted in, but we have no vaddr here. Let the pseudo VMA
+		 * map the folio at address 0 to avoid overflows.
 		 */
+		const unsigned long addr = 0;
 		struct folio *folio;
-		unsigned long addr;
 
 		cond_resched();
 
@@ -790,8 +790,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 			break;
 		}
 
-		/* addr is the offset within the file (zero based) */
-		addr = index * hpage_size;
+		pseudo_vma.vm_pgoff = index << huge_page_order(h);
 
 		/* mutex taken here, fault path and hole punch */
 		hash = hugetlb_fault_mutex_hash(mapping, index);
Signature · 1 line
2.56.1
maintainer tree notice
#57 lore raw

+ hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: don't overflow the pseudo VMA address in fallocate() has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: hugetlbfs: don't overflow the pseudo VMA address in fallocate()
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.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)" <[email protected]>
Subject: hugetlbfs: don't overflow the pseudo VMA address in fallocate()
Date: Thu, 03 Dec 2026 12:37:40 +0100

hugetlbfs_fallocate() allocates folios through a pseudo VMA that starts at address 0 and file offset 0. It passes the file offset of the folio as the address. The reservation code then derives the reserve map index of the folio from that address. Confusing.

However, on 32-bit, that address overflows for file offsets at or above 4 GiB, while we still add the folio to the page cache at the real index.

Consequently, fallocate() of a single hugetlb folio at offset 4 GiB consumes or restores the reservation at index 0, messing up the reservation accounting on truncation and eviction.

Let's fix it by setting the file offset of the folio in the pseudo VMA instead, and passing address 0.

This has low urgency, as it only affects 32-bit kernels with hugetlb. No need to CC stable.

Fixes 70c3547e36f5 ("hugetlbfs: add hugetlbfs_fallocate()")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate +4 -5
@@ -727,7 +727,6 @@ static long hugetlbfs_fallocate(struct f
 	struct hstate *h = hstate_inode(inode);
 	struct vm_area_struct pseudo_vma;
 	struct mm_struct *mm = current->mm;
-	loff_t hpage_size = huge_page_size(h);
 	unsigned long hpage_shift = huge_page_shift(h);
 	pgoff_t start, index, end;
 	int error;
@@ -774,10 +773,11 @@ static long hugetlbfs_fallocate(struct f
 	for (index = start; index < end; index++) {
 		/*
 		 * This is supposed to be the vaddr where the page is being
-		 * faulted in, but we have no vaddr here.
+		 * faulted in, but we have no vaddr here. Let the pseudo VMA
+		 * map the folio at address 0 to avoid overflows.
 		 */
+		const unsigned long addr = 0;
 		struct folio *folio;
-		unsigned long addr;
 
 		cond_resched();
 
@@ -790,8 +790,7 @@ static long hugetlbfs_fallocate(struct f
 			break;
 		}
 
-		/* addr is the offset within the file (zero based) */
-		addr = index * hpage_size;
+		pseudo_vma.vm_pgoff = index << huge_page_order(h);
 
 		/* mutex taken here, fault path and hole punch */
 		hash = hugetlb_fault_mutex_hash(mapping, index);

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 17/35
#58 lore raw

Re: [PATCH v2 17/35] hugetlbfs: don't overflow the pseudo VMA address in fallocate()

In reply to David Hildenbrand (Arm)
Quoted 19 lines · David Hildenbrand (Arm)

hugetlbfs_fallocate() allocates folios through a pseudo VMA that starts at address 0 and file offset 0. It passes the file offset of the folio as the address. The reservation code then derives the reserve map index of the folio from that address. Confusing.

However, on 32-bit, that address overflows for file offsets at or above 4 GiB, while we still add the folio to the page cache at the real index.

Consequently, fallocate() of a single hugetlb folio at offset 4 GiB consumes or restores the reservation at index 0, messing up the reservation accounting on truncation and eviction.

Let's fix it by setting the file offset of the folio in the pseudo VMA instead, and passing address 0.

Quoted 6 lines

This has low urgency, as it only affects 32-bit kernels with hugetlb. No need to CC stable.

Fixes 70c3547e36f5 ("hugetlbfs: add hugetlbfs_fallocate()")

Thanks.

Reviewed-by on v2 17/35
#59 lore raw

Re: [PATCH v2 17/35] hugetlbfs: don't overflow the pseudo VMA address in fallocate()

In reply to David Hildenbrand (Arm)
Quoted 15 lines · David Hildenbrand (Arm)

hugetlbfs_fallocate() allocates folios through a pseudo VMA that starts at address 0 and file offset 0. It passes the file offset of the folio as the address. The reservation code then derives the reserve map index of the folio from that address. Confusing.

However, on 32-bit, that address overflows for file offsets at or above 4 GiB, while we still add the folio to the page cache at the real index.

Consequently, fallocate() of a single hugetlb folio at offset 4 GiB consumes or restores the reservation at index 0, messing up the reservation accounting on truncation and eviction.

Let's fix it by setting the file offset of the folio in the pseudo VMA instead, and passing address 0.

Quoted 6 lines

This has low urgency, as it only affects 32-bit kernels with hugetlb. No need to CC stable.

Fixes 70c3547e36f5 ("hugetlbfs: add hugetlbfs_fallocate()")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#60 lore raw

PATCH v2 18/35 hugetlbfs: don't overflow the end of the unmap range on 32-bit

In reply to David Hildenbrand (Arm)

When truncating a file or punching a hole, hugetlb_vmdelete_list() uses vma_offset_end() to translate the end of the range in the file to an address in the VMA, limited to the end of the VMA.

However, the end of the range can lie far beyond the end of the VMA, and on 32-bit, calculating the address can overflow before we limit it.

Consequently, we might unmap only part of the VMA, or nothing at all. For example, when punching a hole covering the first 4.5 GiB of a file that is mapped by a 1 GiB VMA, we only unmap the first 512 MiB of that VMA. hugetlb_unmap_file_folio() later unmaps the page cache folios, but anonymous folios in private mappings, and for example poison markers, stay in the hole.

Let's fix it by checking whether the range ends beyond the VMA before calculating the address, which also simplifies the code.

This has low urgency, as it requires 32-bit and punching a hole that extends 4 GiB or more past the start of a mapping. No need to CC stable.

Fixes 1bfad99ab425 ("hugetlbfs: hugetlb_vmtruncate_list() needs to take a range to delete")
---
 fs/hugetlbfs/inode.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/hugetlbfs/inode.c +3 -6
index 81c5a3efdc4dc..9b2c61339f830 100644
@@ -363,15 +363,12 @@ static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
 
 static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end)
 {
-	unsigned long t_end;
+	const pgoff_t nr_pages = end - vma->vm_pgoff;
 
-	if (!end)
+	if (!end || nr_pages >= vma_pages(vma))
 		return vma->vm_end;
 
-	t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start;
-	if (t_end > vma->vm_end)
-		t_end = vma->vm_end;
-	return t_end;
+	return vma->vm_start + (nr_pages << PAGE_SHIFT);
 }
 
 /*
Signature · 1 line
2.56.1
#61 lore raw

Re: [PATCH v2 18/35] hugetlbfs: don't overflow the end of the unmap range on 32-bit

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] hugetlbfs: stale 32-bit overflow comment for vma_offset_end() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] hugetlbfs: stale 32-bit overflow comment for vma_offset_end() --

commit 1066e25fedaf1d0622de39c29514e6fdbc96098f
Author: David Hildenbrand (Arm) <[email protected]>

hugetlbfs: don't overflow the end of the unmap range on 32-bit

This commit fixes a 32-bit address overflow in vma_offset_end() by checking whether the unmap range extends past the VMA before shifting the page count.

Quoted 11 lines
fs/hugetlbfs/inode.c +1 -1
index 81c5a3efdc4dc..9b2c61339f830 100644
@@ -363,15 +363,12 @@ static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
 
 static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end)
 {
-	unsigned long t_end;
+	const pgoff_t nr_pages = end - vma->vm_pgoff;
 

fs/hugetlbfs/inode.c static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start) · line 372

Quoted 10 lines
fs/hugetlbfs/inode.c +2 -5
-	if (!end)
+	if (!end || nr_pages >= vma_pages(vma))
 		return vma->vm_end;
 
-	t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start;
-	if (t_end > vma->vm_end)
-		t_end = vma->vm_end;
-	return t_end;
+	return vma->vm_start + (nr_pages << PAGE_SHIFT);
 }

[Severity: Low]
Should the comment immediately above vma_offset_start() be updated as well?

fs/hugetlbfs/inode.c:vma_offset_start()
/*
* Can vma_offset_start/vma_offset_end overflow on 32-bit arches?
* No, because the interval tree returns us only those vmas
* which overlap the truncated area starting at pgoff,
* and no vma on a 32-bit arch can span beyond the 4GB. */

That interval-tree argument only bounds vma_offset_start(), not vma_offset_end(), and now contradicts the nr_pages >= vma_pages(vma) check added here. Leaving it stale could mislead future readers into thinking the overflow guard in vma_offset_end() is redundant.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
maintainer tree notice
#62 lore raw

+ hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: hugetlbfs: don't overflow the end of the unmap range on 32-bit has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: hugetlbfs: don't overflow the end of the unmap range on 32-bit
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.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)" <[email protected]>
Subject: hugetlbfs: don't overflow the end of the unmap range on 32-bit
Date: Thu, 03 Dec 2026 12:37:41 +0100

When truncating a file or punching a hole, hugetlb_vmdelete_list() uses vma_offset_end() to translate the end of the range in the file to an address in the VMA, limited to the end of the VMA.

However, the end of the range can lie far beyond the end of the VMA, and on 32-bit, calculating the address can overflow before we limit it.

Consequently, we might unmap only part of the VMA, or nothing at all. For example, when punching a hole covering the first 4.5 GiB of a file that is mapped by a 1 GiB VMA, we only unmap the first 512 MiB of that VMA. hugetlb_unmap_file_folio() later unmaps the page cache folios, but anonymous folios in private mappings, and for example poison markers, stay in the hole.

Let's fix it by checking whether the range ends beyond the VMA before calculating the address, which also simplifies the code.

This has low urgency, as it requires 32-bit and punching a hole that extends 4 GiB or more past the start of a mapping. No need to CC stable.

Fixes 1bfad99ab425 ("hugetlbfs: hugetlb_vmtruncate_list() needs to take a range to delete")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit +3 -6
@@ -363,15 +363,12 @@ static unsigned long vma_offset_start(st
 
 static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end)
 {
-	unsigned long t_end;
+	const pgoff_t nr_pages = end - vma->vm_pgoff;
 
-	if (!end)
+	if (!end || nr_pages >= vma_pages(vma))
 		return vma->vm_end;
 
-	t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start;
-	if (t_end > vma->vm_end)
-		t_end = vma->vm_end;
-	return t_end;
+	return vma->vm_start + (nr_pages << PAGE_SHIFT);
 }
 
 /*

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 18/35
#63 lore raw

Re: [PATCH v2 18/35] hugetlbfs: don't overflow the end of the unmap range on 32-bit

In reply to David Hildenbrand (Arm)
Quoted 18 lines · David Hildenbrand (Arm)

When truncating a file or punching a hole, hugetlb_vmdelete_list() uses vma_offset_end() to translate the end of the range in the file to an address in the VMA, limited to the end of the VMA.

However, the end of the range can lie far beyond the end of the VMA, and on 32-bit, calculating the address can overflow before we limit it.

Consequently, we might unmap only part of the VMA, or nothing at all. For example, when punching a hole covering the first 4.5 GiB of a file that is mapped by a 1 GiB VMA, we only unmap the first 512 MiB of that VMA. hugetlb_unmap_file_folio() later unmaps the page cache folios, but anonymous folios in private mappings, and for example poison markers, stay in the hole.

Quoted 8 lines

Let's fix it by checking whether the range ends beyond the VMA before calculating the address, which also simplifies the code.

This has low urgency, as it requires 32-bit and punching a hole that extends 4 GiB or more past the start of a mapping. No need to CC stable.

Fixes 1bfad99ab425 ("hugetlbfs: hugetlb_vmtruncate_list() needs to take a range to delete")

With Sashiko's comment resolved:

Thanks.

maintainer
#64 lore raw

Re: [PATCH v2 18/35] hugetlbfs: don't overflow the end of the unmap range on 32-bit

Quoted 21 lines · On Fri, 4 Dec 2026 14:44:20 +0800 Muchun Song <[email protected]> wrote:
Quoted 21 lines · David Hildenbrand (Arm)

When truncating a file or punching a hole, hugetlb_vmdelete_list() uses vma_offset_end() to translate the end of the range in the file to an address in the VMA, limited to the end of the VMA.

However, the end of the range can lie far beyond the end of the VMA, and on 32-bit, calculating the address can overflow before we limit it.

Consequently, we might unmap only part of the VMA, or nothing at all. For example, when punching a hole covering the first 4.5 GiB of a file that is mapped by a 1 GiB VMA, we only unmap the first 512 MiB of that VMA. hugetlb_unmap_file_folio() later unmaps the page cache folios, but anonymous folios in private mappings, and for example poison markers, stay in the hole.

Let's fix it by checking whether the range ends beyond the VMA before calculating the address, which also simplifies the code.

Quoted 7 lines
Quoted 5 lines

This has low urgency, as it requires 32-bit and punching a hole that extends 4 GiB or more past the start of a mapping. No need to CC stable.

Fixes 1bfad99ab425 ("hugetlbfs: hugetlb_vmtruncate_list() needs to take a range to delete")

With Sashiko's comment resolved:

This?

fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix +4 -4
@@ -346,10 +346,10 @@ static bool hugetlb_vma_maps_pfn(struct
 }
 
 /*
- * Can vma_offset_start/vma_offset_end overflow on 32-bit arches?
- * No, because the interval tree returns us only those vmas
- * which overlap the truncated area starting at pgoff,
- * and no vma on a 32-bit arch can span beyond the 4GB.
+ * The interval tree returns only VMAs that overlap the range starting at
+ * start, so vma_offset_start() stays within the VMA on 32-bit.
+ * The end can lie far beyond the VMA.  vma_offset_end() must check the
+ * page offset before converting it to an address.
  */
 static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
 {

_

maintainer
#65 lore raw

Re: [PATCH v2 18/35] hugetlbfs: don't overflow the end of the unmap range on 32-bit

Quoted 30 lines · Andrew Morton
Quoted 24 lines · On Fri, 4 Dec 2026 14:44:20 +0800 Muchun Song <[email protected]> wrote:
Quoted 24 lines · David Hildenbrand (Arm)

When truncating a file or punching a hole, hugetlb_vmdelete_list() uses vma_offset_end() to translate the end of the range in the file to an address in the VMA, limited to the end of the VMA.

However, the end of the range can lie far beyond the end of the VMA, and on 32-bit, calculating the address can overflow before we limit it.

Consequently, we might unmap only part of the VMA, or nothing at all. For example, when punching a hole covering the first 4.5 GiB of a file that is mapped by a 1 GiB VMA, we only unmap the first 512 MiB of that VMA. hugetlb_unmap_file_folio() later unmaps the page cache folios, but anonymous folios in private mappings, and for example poison markers, stay in the hole.

Let's fix it by checking whether the range ends beyond the VMA before calculating the address, which also simplifies the code.

This has low urgency, as it requires 32-bit and punching a hole that extends 4 GiB or more past the start of a mapping. No need to CC stable.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Fixes 1bfad99ab425 ("hugetlbfs: hugetlb_vmtruncate_list() needs to take a range to delete")

With Sashiko's comment resolved:

This?

Definitely.

Quoted 6 lines
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix +0 -0
@@ -346,10 +346,10 @@ static bool hugetlb_vma_maps_pfn(struct
 }
 

fs/hugetlbfs/inode.c static bool hugetlb_vma_maps_pfn(struct · line 353

Quoted 10 lines
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix +4 -4
 /*
- * Can vma_offset_start/vma_offset_end overflow on 32-bit arches?
- * No, because the interval tree returns us only those vmas
- * which overlap the truncated area starting at pgoff,
- * and no vma on a 32-bit arch can span beyond the 4GB.
+ * The interval tree returns only VMAs that overlap the range starting at
+ * start, so vma_offset_start() stays within the VMA on 32-bit.
+ * The end can lie far beyond the VMA.  vma_offset_end() must check the
+ * page offset before converting it to an address.
 */

LGTM.

Thanks.

Quoted 4 lines
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix +0 -0
 static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
 {

_

maintainer tree notice
#66 lore raw

+ hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: hugetlbfs: correct the offset overflow comment has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: hugetlbfs: correct the offset overflow comment
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix.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: Andrew Morton <[email protected]>
Subject: hugetlbfs: correct the offset overflow comment
Date: Thu, 03 Dec 2026 22:52:58 -0800

The comment above vma_offset_start() says that neither offset helper can overflow on 32-bit. David's change to vma_offset_end() fixes the case which disproves that statement. Describe why the start offset stays within the VMA and why the end offset needs an explicit bounds check.

 fs/hugetlbfs/inode.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix +4 -4
@@ -346,10 +346,10 @@ static bool hugetlb_vma_maps_pfn(struct
 }
 
 /*
- * Can vma_offset_start/vma_offset_end overflow on 32-bit arches?
- * No, because the interval tree returns us only those vmas
- * which overlap the truncated area starting at pgoff,
- * and no vma on a 32-bit arch can span beyond the 4GB.
+ * The interval tree returns only VMAs that overlap the range starting at
+ * start, so vma_offset_start() stays within the VMA on 32-bit.
+ * The end can lie far beyond the VMA.  vma_offset_end() must check the
+ * page offset before converting it to an address.
  */
 static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start)
 {

_

Patches currently in -mm which might be from [email protected] are

kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch lib-test_xarray-check-unused-spare-node-after-range-creation.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch selftests-mm-memory-failure-truncate-poisoned-fuse-page-cache-before-teardown.patch selftests-mm-memory-failure-skip-poisoned-dirty-page-cache-on-nilfs2.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup-fix.patch selftests-mm-memory-failure-skip-hard-dirty-page-cache-test-on-cephfs.patch selftests-mm-memory-failure-close-dirty-page-cache-files-before-unlinking-them.patch selftests-mm-memory-failure-extend-page-cache-test-files-with-pwrite.patch selftests-mm-memory-failure-write-the-whole-page-cache-test-file-for-omfs.patch selftests-mm-memory-failure-accept-eio-from-close-after-poisoning-dirty-data.patch xarray-fix-index-jumping-backwards-in-xas_find_marked-fix.patch xarray-fix-index-jumping-backwards-in-xas_find_marked-fix-2.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target-fix.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault-fix.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix.patch ipc-msg-clamp-negative-msg_info-usage-counts.patch squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch lib-atomic64-separate-hashed-locks-by-smp-cache-line.patch nvdimm-btt-separate-map-locks-by-smp-cache-line.patch percpu_ref-replace-the-global-switch-lock-with-hashed-locks-fix.patch afs-do-not-put-error-pointer-after-address-lookup-failure.patch drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch

Reviewed-by on v2 18/35
#67 lore raw

Re: [PATCH v2 18/35] hugetlbfs: don't overflow the end of the unmap range on 32-bit

In reply to David Hildenbrand (Arm)
Quoted 14 lines · David Hildenbrand (Arm)

When truncating a file or punching a hole, hugetlb_vmdelete_list() uses vma_offset_end() to translate the end of the range in the file to an address in the VMA, limited to the end of the VMA.

However, the end of the range can lie far beyond the end of the VMA, and on 32-bit, calculating the address can overflow before we limit it.

Consequently, we might unmap only part of the VMA, or nothing at all. For example, when punching a hole covering the first 4.5 GiB of a file that is mapped by a 1 GiB VMA, we only unmap the first 512 MiB of that VMA. hugetlb_unmap_file_folio() later unmaps the page cache folios, but anonymous folios in private mappings, and for example poison markers, stay in the hole.

Quoted 8 lines

Let's fix it by checking whether the range ends beyond the VMA before calculating the address, which also simplifies the code.

This has low urgency, as it requires 32-bit and punching a hole that extends 4 GiB or more past the start of a mapping. No need to CC stable.

Fixes 1bfad99ab425 ("hugetlbfs: hugetlb_vmtruncate_list() needs to take a range to delete")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#68 lore raw

PATCH v2 19/35 hugetlbfs: recheck the vma range after the lock wait

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

A hole punch or a truncation of a hugetlb file that races with a split of a shared vma can lead to a use-after-free read of a page table, and to an MMU notifier range with end <= start, which trips a WARN_ON_ONCE() in KVM.

hugetlb_unmap_file_folio() computes the address of the folio in a vma and walks the page tables to unmap the folio. As free_pgtables() unlinks a vma from the rmap tree before it frees its page tables, i_mmap_rwsem does not protect page tables outside of the vma, so walking them can result in a use-after-free read.

The rmap tree walk guarantees that a found vma spans our folio. However, when we have to fall back to locking the vma lock first and drop the i_mmap_rwsem, we don't check whether that is still the case after we acquired the vma lock and the i_mmap_rwsem. The following is then possible:

    hugetlb_unmap_file_folio()          __split_vma(), new_below == 0
    ==========================          =============================
    hugetlb_vma_trylock_write() fails
    i_mmap_unlock_write()
    down_write(&vma_lock->rw_sema)
                                        vma_prepare()
                                          i_mmap_lock_write()
                                        vma->vm_end = addr
                                        vma_complete()
                                          i_mmap_unlock_write()
    i_mmap_lock_write()
    vma_lock->vma is not NULL
    vma_offset_start() >= vma->vm_end
    vma_offset_end() == vma->vm_end
    hugetlb_vma_maps_pfn(vma, v_start)
    unmap_hugepage_range(vma, v_start, v_end)

Similarly, a split with new_below == 1, where the vma keeps the upper part and vm_start and vm_pgoff move up, or shrinking the vma with mremap() can leave the vma not covering our folio.

Let's fix it by checking whether the possibly split vma still spans our folio. Add a VM_WARN_ON_ONCE() to highlight the problem that was previously missed.

This has medium urgency, as this is triggerable by unprivileged users.

Fixes 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization")
[ david: adjust comment and description ]
---
 fs/hugetlbfs/inode.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/hugetlbfs/inode.c +5 -3
index 9b2c61339f830..db2af960ee7dc 100644
@@ -439,10 +439,11 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
 		i_mmap_lock_write(mapping);
 
 		vma = vma_lock->vma;
-		if (!vma) {
+		if (!vma || vma_start_pgoff(vma) >= end || vma_end_pgoff(vma) <= start) {
 			/*
-			 * If lock is no longer attached to vma, then just
-			 * unlock, drop our reference and retry looking for
+			 * If the lock is no longer attached to this vma, or if
+			 * the vma changed and no longer covers the folio, then
+			 * just unlock, drop our reference and retry looking for
 			 * other vmas.
 			 */
 			up_write(&vma_lock->rw_sema);
@@ -456,6 +457,7 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
 		 */
 		v_start = vma_offset_start(vma, start);
 		v_end = vma_offset_end(vma, end);
+		VM_WARN_ON_ONCE(v_start >= v_end);
 		if (hugetlb_vma_maps_pfn(vma, v_start, pfn))
 			unmap_hugepage_range(vma, v_start, v_end, NULL,
 					     ZAP_FLAG_DROP_MARKER);
Signature · 1 line
2.56.1
maintainer tree notice
#69 lore raw

+ hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: recheck the vma range after the lock wait has been added to the -mm mm-unstable branch. Its filename is hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch

The patch titled
Subject: hugetlbfs: recheck the vma range after the lock wait
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-recheck-the-vma-range-after-the-lock-wait.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: Chris Mason <[email protected]>
Subject: hugetlbfs: recheck the vma range after the lock wait
Date: Thu, 03 Dec 2026 12:37:42 +0100

A hole punch or a truncation of a hugetlb file that races with a split of a shared vma can lead to a use-after-free read of a page table, and to an MMU notifier range with end <= start, which trips a WARN_ON_ONCE() in KVM.

hugetlb_unmap_file_folio() computes the address of the folio in a vma and walks the page tables to unmap the folio. As free_pgtables() unlinks a vma from the rmap tree before it frees its page tables, i_mmap_rwsem does not protect page tables outside of the vma, so walking them can result in a use-after-free read.

The rmap tree walk guarantees that a found vma spans our folio. However, when we have to fall back to locking the vma lock first and drop the i_mmap_rwsem, we don't check whether that is still the case after we acquired the vma lock and the i_mmap_rwsem. The following is then possible:

    hugetlb_unmap_file_folio()          __split_vma(), new_below == 0
    ==========================          =============================
    hugetlb_vma_trylock_write() fails
    i_mmap_unlock_write()
    down_write(&vma_lock->rw_sema)
                                        vma_prepare()
                                          i_mmap_lock_write()
                                        vma->vm_end = addr
                                        vma_complete()
                                          i_mmap_unlock_write()
    i_mmap_lock_write()
    vma_lock->vma is not NULL
    vma_offset_start() >= vma->vm_end
    vma_offset_end() == vma->vm_end
    hugetlb_vma_maps_pfn(vma, v_start)
    unmap_hugepage_range(vma, v_start, v_end)

Similarly, a split with new_below == 1, where the vma keeps the upper part and vm_start and vm_pgoff move up, or shrinking the vma with mremap() can leave the vma not covering our folio.

Let's fix it by checking whether the possibly split vma still spans our folio. Add a VM_WARN_ON_ONCE() to highlight the problem that was previously missed.

This has medium urgency, as this is triggerable by unprivileged users.

[[email protected]: adjust comment and description]
Fixes 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-recheck-the-vma-range-after-the-lock-wait +5 -3
@@ -439,10 +439,11 @@ retry:
 		i_mmap_lock_write(mapping);
 
 		vma = vma_lock->vma;
-		if (!vma) {
+		if (!vma || vma_start_pgoff(vma) >= end || vma_end_pgoff(vma) <= start) {
 			/*
-			 * If lock is no longer attached to vma, then just
-			 * unlock, drop our reference and retry looking for
+			 * If the lock is no longer attached to this vma, or if
+			 * the vma changed and no longer covers the folio, then
+			 * just unlock, drop our reference and retry looking for
 			 * other vmas.
 			 */
 			up_write(&vma_lock->rw_sema);
@@ -456,6 +457,7 @@ retry:
 		 */
 		v_start = vma_offset_start(vma, start);
 		v_end = vma_offset_end(vma, end);
+		VM_WARN_ON_ONCE(v_start >= v_end);
 		if (hugetlb_vma_maps_pfn(vma, v_start, pfn))
 			unmap_hugepage_range(vma, v_start, v_end, NULL,
 					     ZAP_FLAG_DROP_MARKER);

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch

Reviewed-by on v2 19/35
#70 lore raw

Re: [PATCH v2 19/35] hugetlbfs: recheck the vma range after the lock wait

In reply to David Hildenbrand (Arm)
Quoted 44 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

A hole punch or a truncation of a hugetlb file that races with a split of a shared vma can lead to a use-after-free read of a page table, and to an MMU notifier range with end <= start, which trips a WARN_ON_ONCE() in KVM.

hugetlb_unmap_file_folio() computes the address of the folio in a vma and walks the page tables to unmap the folio. As free_pgtables() unlinks a vma from the rmap tree before it frees its page tables, i_mmap_rwsem does not protect page tables outside of the vma, so walking them can result in a use-after-free read.

The rmap tree walk guarantees that a found vma spans our folio. However, when we have to fall back to locking the vma lock first and drop the i_mmap_rwsem, we don't check whether that is still the case after we acquired the vma lock and the i_mmap_rwsem. The following is then possible:

    hugetlb_unmap_file_folio()          __split_vma(), new_below == 0
    ==========================          =============================
    hugetlb_vma_trylock_write() fails
    i_mmap_unlock_write()
    down_write(&vma_lock->rw_sema)
                                        vma_prepare()
                                          i_mmap_lock_write()
                                        vma->vm_end = addr
                                        vma_complete()
                                          i_mmap_unlock_write()
    i_mmap_lock_write()
    vma_lock->vma is not NULL
    vma_offset_start() >= vma->vm_end
    vma_offset_end() == vma->vm_end
    hugetlb_vma_maps_pfn(vma, v_start)
    unmap_hugepage_range(vma, v_start, v_end)

Similarly, a split with new_below == 1, where the vma keeps the upper part and vm_start and vm_pgoff move up, or shrinking the vma with mremap() can leave the vma not covering our folio.

Let's fix it by checking whether the possibly split vma still spans our folio. Add a VM_WARN_ON_ONCE() to highlight the problem that was previously missed.

Quoted 7 lines

This has medium urgency, as this is triggerable by unprivileged users.

Fixes 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization")
[ david: adjust comment and description ]

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#71 lore raw

PATCH v2 20/35 mm/hugetlb: don't zap non-present entries when unmapping a folio

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

When the owner of a private hugetlb mapping fails to allocate a folio for COW, unmap_ref_private() unmaps the old folio from the forked children, and marks them with HPAGE_RESV_UNMAPPED, so they get a SIGBUS on their next access.

hugetlb_unmap_range() is supposed to only unmap that folio, but it clears non-present page table entries unconditionally.

A child can silently lose its COWed folio while it is getting migrated, or a hwpoison entry or uffd poison marker, without HPAGE_RESV_UNMAPPED getting set. Consequently, it would not SIGBUS on the next access.

Let's fix it by ignoring non-present entries when unmapping a folio. As the folio is locked, migration entries of that folio do not apply.

This has low urgency, as it requires a failing COW allocation in the owner racing with migration or memory failure in the child. Let's not CC stable.

Fixes 8c4894c6bc79 ("hwpoison, hugetlbfs: fix "bad pmd" warning in unmapping hwpoisoned hugepage")
[ david: adjust comment and description ]
---
 mm/hugetlb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/hugetlb.c +8 -0
index 72d912f488fdc..1555a326efe4c 100644
@@ -5479,6 +5479,14 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
 		 * unmapped and its refcount is dropped, so just clear pte here.
 		 */
 		if (unlikely(!pte_present(pte))) {
+			/*
+			 * If we have a folio provided, we only care about
+			 * present page table entries (folio is locked).
+			 */
+			if (folio_provided) {
+				spin_unlock(ptl);
+				continue;
+			}
 			/*
 			 * If the pte was wr-protected by uffd-wp in any of the
 			 * swap forms, meanwhile the caller does not want to
Signature · 1 line
2.56.1
maintainer tree notice
#72 lore raw

+ mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: don't zap non-present entries when unmapping a folio has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: don't zap non-present entries when unmapping a folio
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.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: Chris Mason <[email protected]>
Subject: mm/hugetlb: don't zap non-present entries when unmapping a folio
Date: Thu, 03 Dec 2026 12:37:43 +0100

When the owner of a private hugetlb mapping fails to allocate a folio for COW, unmap_ref_private() unmaps the old folio from the forked children, and marks them with HPAGE_RESV_UNMAPPED, so they get a SIGBUS on their next access.

hugetlb_unmap_range() is supposed to only unmap that folio, but it clears non-present page table entries unconditionally.

A child can silently lose its COWed folio while it is getting migrated, or a hwpoison entry or uffd poison marker, without HPAGE_RESV_UNMAPPED getting set. Consequently, it would not SIGBUS on the next access.

Let's fix it by ignoring non-present entries when unmapping a folio. As the folio is locked, migration entries of that folio do not apply.

This has low urgency, as it requires a failing COW allocation in the owner racing with migration or memory failure in the child. Let's not CC stable.

[[email protected]: adjust comment and description]
Fixes 8c4894c6bc79 ("hwpoison, hugetlbfs: fix "bad pmd" warning in unmapping hwpoisoned hugepage")
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |    8 ++++++++
 1 file changed, 8 insertions(+)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio +8 -0
@@ -5455,6 +5455,14 @@ static void hugetlb_unmap_range(struct m
 		 */
 		if (unlikely(!pte_present(pte))) {
 			/*
+			 * If we have a folio provided, we only care about
+			 * present page table entries (folio is locked).
+			 */
+			if (folio_provided) {
+				spin_unlock(ptl);
+				continue;
+			}
+			/*
 			 * If the pte was wr-protected by uffd-wp in any of the
 			 * swap forms, meanwhile the caller does not want to
 			 * drop the uffd-wp bit in this zap, then replace the

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch

Reviewed-by on v2 20/35
#73 lore raw

Re: [PATCH v2 20/35] mm/hugetlb: don't zap non-present entries when unmapping a folio

In reply to David Hildenbrand (Arm)
Quoted 21 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

When the owner of a private hugetlb mapping fails to allocate a folio for COW, unmap_ref_private() unmaps the old folio from the forked children, and marks them with HPAGE_RESV_UNMAPPED, so they get a SIGBUS on their next access.

hugetlb_unmap_range() is supposed to only unmap that folio, but it clears non-present page table entries unconditionally.

A child can silently lose its COWed folio while it is getting migrated, or a hwpoison entry or uffd poison marker, without HPAGE_RESV_UNMAPPED getting set. Consequently, it would not SIGBUS on the next access.

Let's fix it by ignoring non-present entries when unmapping a folio. As the folio is locked, migration entries of that folio do not apply.

This has low urgency, as it requires a failing COW allocation in the owner racing with migration or memory failure in the child. Let's not CC stable.

Quoted 5 lines
Fixes 8c4894c6bc79 ("hwpoison, hugetlbfs: fix "bad pmd" warning in unmapping hwpoisoned hugepage")
[ david: adjust comment and description ]

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#74 lore raw

PATCH v2 21/35 hugetlbfs: don't skip private VMAs when unmapping on truncation

In reply to David Hildenbrand (Arm)

When truncating a file or punching a hole, hugetlb_vmdelete_list() unmaps the range from all VMAs that map the file. It skips VMAs whose VMA lock it cannot trylock, which is fine for shared VMAs: remove_inode_hugepages() unmaps their page cache folios later.

However, private VMAs that own their reservations have a VMA lock as well, so, for example, a concurrent page fault or MADV_DONTNEED in such a VMA makes us skip that VMA, and nobody unmaps its anonymous folios or, on truncation, its uffd-wp markers later.

Consequently, they stay mapped beyond EOF after a truncation, or inside a punched hole.

We only need the VMA lock here for PMD table sharing, which synchronizes on the shareable lock. We already hold the i_mmap lock in write mode.

Let's fix it like commit da48afefa78d ("hugetlbfs: fix unmapping folios from private VMAs") did for hugetlb_unmap_file_folio(): only take the VMA lock for VMAs with a shareable lock, and always unmap other VMAs only under i_mmap lock.

This has low urgency, as it requires racing with e.g., a page fault in the private VMA.

Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")
---
 fs/hugetlbfs/inode.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
fs/hugetlbfs/inode.c +9 -7
index db2af960ee7dc..5f44236785b5a 100644
@@ -482,10 +482,16 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
 	 */
 	mapping_rmap_tree_foreach(vma, mapping, start,
 				  end ? end - 1 : ULONG_MAX) {
+		const bool need_vma_lock = __vma_shareable_lock(vma);
 		unsigned long v_start;
 		unsigned long v_end;
 
-		if (!hugetlb_vma_trylock_write(vma))
+		/*
+		 * We only need additional VMA locking here for PMD table
+		 * sharing, which synchronizes on the shareable lock. Other VMAs
+		 * never share PMD tables, so we can unmap them without it.
+		 */
+		if (need_vma_lock && !hugetlb_vma_trylock_write(vma))
 			continue;
 
 		v_start = vma_offset_start(vma, start);
@@ -493,12 +499,8 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
 
 		unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);
 
-		/*
-		 * Note that vma lock only exists for shared/non-private
-		 * vmas.  Therefore, lock is not held when calling
-		 * unmap_hugepage_range for private vmas.
-		 */
-		hugetlb_vma_unlock_write(vma);
+		if (need_vma_lock)
+			hugetlb_vma_unlock_write(vma);
 	}
 }
 
Signature · 1 line
2.56.1
#75 lore raw

Re: [PATCH v2 21/35] hugetlbfs: don't skip private VMAs when unmapping on truncation

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] hugetlbfs: skipping private VMA lock in hugetlb_vmdelete_list() races faults --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] hugetlbfs: skipping private VMA lock in hugetlb_vmdelete_list() races faults
--

commit 8e062b4ade15c4eb0dc06a09ad850ccd4f8c9850
Author: David Hildenbrand (Arm) <[email protected]>

hugetlbfs: don't skip private VMAs when unmapping on truncation

This commit changes hugetlb_vmdelete_list() to acquire the hugetlb VMA lock only for VMAs with a shareable lock, so private reservation-owning VMAs are unmapped under i_mmap_rwsem alone instead of being skipped on trylock contention.

Quoted 26 lines
fs/hugetlbfs/inode.c +7 -1
index db2af960ee7dc..5f44236785b5a 100644
@@ -482,10 +482,16 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
 	 */
 	mapping_rmap_tree_foreach(vma, mapping, start,
 				  end ? end - 1 : ULONG_MAX) {
+		const bool need_vma_lock = __vma_shareable_lock(vma);
 		unsigned long v_start;
 		unsigned long v_end;
 
-		if (!hugetlb_vma_trylock_write(vma))
+		/*
+		 * We only need additional VMA locking here for PMD table
+		 * sharing, which synchronizes on the shareable lock. Other VMAs
+		 * never share PMD tables, so we can unmap them without it.
+		 */
+		if (need_vma_lock && !hugetlb_vma_trylock_write(vma))
 			continue;
 
 		v_start = vma_offset_start(vma, start);
@@ -493,12 +499,8 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
 
 		unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);
 

fs/hugetlbfs/inode.c hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start, · line 505

Quoted 10 lines
fs/hugetlbfs/inode.c +2 -6
-		/*
-		 * Note that vma lock only exists for shared/non-private
-		 * vmas.  Therefore, lock is not held when calling
-		 * unmap_hugepage_range for private vmas.
-		 */
-		hugetlb_vma_unlock_write(vma);
+		if (need_vma_lock)
+			hugetlb_vma_unlock_write(vma);
 	}
 }

[Severity: High]
Can skipping resv_map->rw_sema on private reservation-owning VMAs race with concurrent page faults and corrupt h->resv_huge_pages and subpool accounting?

Unlike hugetlb_unmap_file_folio(), hugetlb_vmdelete_list() does not hold hugetlb_fault_mutex_table and unmaps anonymous folios via hugetlb_folio_restore_rsv_on_unmap(). Fault paths do not take i_mmap_rwsem and rely on resv_map->rw_sema to exclude concurrent unmaps.

With a forked private reservation-owning VMA mapping an anonymous folio F1, a CoW write fault can race with ftruncate() or hole punch:

CPU0 (hugetlb_wp) CPU1 (hugetlb_vmdelete_list)
hugetlb_vma_lock_read(vma)
spin_unlock(vmf->ptl)
F2 = alloc_hugetlb_folio(...)
copy_user_large_folio(F2, F1)

                                    unmap_hugepage_range(vma, ...)
                                      clear PTE, drop ptl
                                      hugetlb_folio_restore_rsv_on_unmap()
                                        vma_add_reservation()
                                        hugetlb_set_folio_subpool(F1, NULL)
                                        h->resv_huge_pages++
  spin_lock(vmf->ptl)
  !pte_same()
  restore_reserve_on_error(..., F2)
    vma_del_reservation()  <-- leaks h->resv_huge_pages and subpool quota

Because F2 was allocated with cow_from_owner set, HPG_restore_reserve is clear on F2. restore_reserve_on_error() sees the entry restored by CPU1 and re-marks it consumed in resv_map without undoing h->resv_huge_pages++ or the subpool charge, so hugetlb_vm_op_close() permanently leaks both.

Without resv_map->rw_sema, a concurrent fault during hole punch can also observe vma_add_reservation() before h->resv_huge_pages++ in hugetlb_folio_restore_rsv_on_unmap(), underflowing h->resv_huge_pages to ULONG_MAX or hiding another mapping's reservation.

Finally, because hugetlb_unmap_range() clears the PTE before tlb_finish_mmu() restores the reservation and frees the folio, a concurrent fault during hole punch can fail with VM_FAULT_SIGBUS or race with tlb_flush_rmaps() to leave a duplicate reservation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
author maintainer
#76 lore raw

Re: [PATCH v2 21/35] hugetlbfs: don't skip private VMAs when unmapping on truncation

Quoted 42 lines · On 12/3/26 13:29, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] hugetlbfs: skipping private VMA lock in hugetlb_vmdelete_list() races faults
--

commit 8e062b4ade15c4eb0dc06a09ad850ccd4f8c9850
Author: David Hildenbrand (Arm) <[email protected]>

hugetlbfs: don't skip private VMAs when unmapping on truncation

This commit changes hugetlb_vmdelete_list() to acquire the hugetlb VMA lock only for VMAs with a shareable lock, so private reservation-owning VMAs are unmapped under i_mmap_rwsem alone instead of being skipped on trylock contention.

Quoted 26 lines
fs/hugetlbfs/inode.c +7 -1
index db2af960ee7dc..5f44236785b5a 100644
@@ -482,10 +482,16 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
 	 */
 	mapping_rmap_tree_foreach(vma, mapping, start,
 				  end ? end - 1 : ULONG_MAX) {
+		const bool need_vma_lock = __vma_shareable_lock(vma);
 		unsigned long v_start;
 		unsigned long v_end;
 
-		if (!hugetlb_vma_trylock_write(vma))
+		/*
+		 * We only need additional VMA locking here for PMD table
+		 * sharing, which synchronizes on the shareable lock. Other VMAs
+		 * never share PMD tables, so we can unmap them without it.
+		 */
+		if (need_vma_lock && !hugetlb_vma_trylock_write(vma))
 			continue;
 
 		v_start = vma_offset_start(vma, start);
@@ -493,12 +499,8 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
 
 		unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);
 
Quoted 15 lines

fs/hugetlbfs/inode.c hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start, · line 505

Quoted 10 lines
fs/hugetlbfs/inode.c +2 -6
-		/*
-		 * Note that vma lock only exists for shared/non-private
-		 * vmas.  Therefore, lock is not held when calling
-		 * unmap_hugepage_range for private vmas.
-		 */
-		hugetlb_vma_unlock_write(vma);
+		if (need_vma_lock)
+			hugetlb_vma_unlock_write(vma);
 	}
 }

[Severity: High]
Can skipping resv_map->rw_sema on private reservation-owning VMAs race with concurrent page faults and corrupt h->resv_huge_pages and subpool accounting?

When I had claude check exactly that it said it would be fine. Let me dig again.

Signature · 3 lines
Cheers,

David
#77 lore raw

Re: [PATCH v2 21/35] hugetlbfs: don't skip private VMAs when unmapping on truncation

Quoted 1 line · David Hildenbrand (Arm)

When I had claude check exactly that it said it would be fine. Let me dig again.

claude here setup one thread that touches a private mapping that reserved the only huge page, and another punches holes there. With v2 applied, that gets hundreds of SIGBUS a minute.

-chris

// Does a private hugetlb mapping keep the page it reserved while another thread punches a hole there?
#define _GNU_SOURCE
#include <fcntl.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#define SZ (2UL << 20)
static int fd; static volatile int stop; static long punched; static __thread sigjmp_buf jb;
static void bus(int s) { siglongjmp(jb, 1); }
static void *punch(void *a) { while (!stop) { if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, SZ)) { perror("punch"); exit(2); } punched++; } return NULL; } int main(int argc, char **argv)

{
	int secs = argc > 1 ? atoi(argv[1]) : 60; static volatile long touched, emptied, faults; pthread_t t;
	fd = memfd_create("hp", MFD_HUGETLB); if (fd < 0 || ftruncate(fd, SZ)) { perror("memfd"); return 2; }
	volatile char *p = mmap(NULL, SZ, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	if (p == MAP_FAILED) { perror("mmap"); return 2; }
	struct sigaction sa = { .sa_handler = bus, .sa_flags = SA_NODEFER }; sigaction(SIGBUS, &sa, NULL);
	time_t end = time(NULL) + secs; pthread_create(&t, NULL, punch, NULL);
	while (time(NULL) < end) { if (sigsetjmp(jb, 1)) { faults++; continue; } if (p[0] == 0) emptied++; p[0] = 1; touched++; }
	stop = 1; pthread_join(t, NULL);
	printf("touched %ld, page was gone %ld times, holes %ld, SIGBUS %ld\n%s\n", touched, emptied, punched, faults, faults ? "FAIL: SIGBUS although the mapping reserved its page" : "PASS");
	return faults != 0;
}
maintainer tree notice
#78 lore raw

+ hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: hugetlbfs: don't skip private VMAs when unmapping on truncation has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: hugetlbfs: don't skip private VMAs when unmapping on truncation
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.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)" <[email protected]>
Subject: hugetlbfs: don't skip private VMAs when unmapping on truncation
Date: Thu, 03 Dec 2026 12:37:44 +0100

When truncating a file or punching a hole, hugetlb_vmdelete_list() unmaps the range from all VMAs that map the file. It skips VMAs whose VMA lock it cannot trylock, which is fine for shared VMAs: remove_inode_hugepages() unmaps their page cache folios later.

However, private VMAs that own their reservations have a VMA lock as well, so, for example, a concurrent page fault or MADV_DONTNEED in such a VMA makes us skip that VMA, and nobody unmaps its anonymous folios or, on truncation, its uffd-wp markers later.

Consequently, they stay mapped beyond EOF after a truncation, or inside a punched hole.

We only need the VMA lock here for PMD table sharing, which synchronizes on the shareable lock. We already hold the i_mmap lock in write mode.

Let's fix it like commit da48afefa78d ("hugetlbfs: fix unmapping folios from private VMAs") did for hugetlb_unmap_file_folio(): only take the VMA lock for VMAs with a shareable lock, and always unmap other VMAs only under i_mmap lock.

This has low urgency, as it requires racing with e.g., a page fault in the private VMA.

Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |   16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation +9 -7
@@ -482,10 +482,16 @@ hugetlb_vmdelete_list(struct address_spa
 	 */
 	mapping_rmap_tree_foreach(vma, mapping, start,
 				  end ? end - 1 : ULONG_MAX) {
+		const bool need_vma_lock = __vma_shareable_lock(vma);
 		unsigned long v_start;
 		unsigned long v_end;
 
-		if (!hugetlb_vma_trylock_write(vma))
+		/*
+		 * We only need additional VMA locking here for PMD table
+		 * sharing, which synchronizes on the shareable lock. Other VMAs
+		 * never share PMD tables, so we can unmap them without it.
+		 */
+		if (need_vma_lock && !hugetlb_vma_trylock_write(vma))
 			continue;
 
 		v_start = vma_offset_start(vma, start);
@@ -493,12 +499,8 @@ hugetlb_vmdelete_list(struct address_spa
 
 		unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);
 
-		/*
-		 * Note that vma lock only exists for shared/non-private
-		 * vmas.  Therefore, lock is not held when calling
-		 * unmap_hugepage_range for private vmas.
-		 */
-		hugetlb_vma_unlock_write(vma);
+		if (need_vma_lock)
+			hugetlb_vma_unlock_write(vma);
 	}
 }
 

_

Patches currently in -mm which might be from [email protected] 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

author maintainer
#79 lore raw

PATCH v2 22/35 mm/hugetlb: serialize private faults with truncation

In reply to David Hildenbrand (Arm)

When we fault in a folio in a MAP_PRIVATE hugetlb mapping at an index without a page cache folio, hugetlb_no_page() checks that the index is not beyond EOF, allocates a new anonymous folio and maps it. Truncation first updates i_size under the invalidate lock, and then unmaps the truncated range from all VMAs.

For page cache folios, we recently started rechecking EOF under filemap_invalidate_lock_shared() to handle exactly that.

However, nothing serializes the allocation of anonymous folios with
truncation: if truncation unmaps the range after we checked EOF, but before we mapped the new folio, the folio stays mapped beyond EOF.

Consequently, accessing the truncated range does not raise SIGBUS, and the process keeps the folio mapped until it unmaps it.

Reproduced by artificially delaying hugetlb_no_page() before allocating the folio, while another thread truncates the file: the page beyond EOF stays mapped.

Let's fix it by also rechecking EOF under the invalidate lock for anonymous folios, and holding it until we mapped the folio, such that truncation will unmap it.

Like faults in shared mappings, faults in private mappings now briefly take the invalidate lock in read mode, and might have to wait for a concurrent truncation. Which seems to be the right thing to do.

This has low urgency, as it requires racing a page fault in a private mapping with truncation. No need to CC stable.

Fixes fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
---
 mm/hugetlb.c | 25 +++++++++++++++----------
 mm/rmap.c    |  2 +-
 2 files changed, 16 insertions(+), 11 deletions(-)
mm/hugetlb.c +15 -10
index 1555a326efe4c..105cc2840f988 100644
@@ -6002,6 +6002,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 	const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE;
 	u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
 	bool folio_locked = true, new_anon_folio = false;
+	bool invalidate_locked = false;
 	struct vm_area_struct *vma = vmf->vma;
 	struct mm_struct *mm = vma->vm_mm;
 	struct hstate *h = hstate_vma(vma);
@@ -6061,15 +6062,19 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 			ret = __vmf_anon_prepare(vmf);
 			if (unlikely(ret))
 				goto out;
-		} else {
-			/* Recheck EOF before consuming an inode reservation. */
-			filemap_invalidate_lock_shared(mapping);
-			if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false)) {
-				filemap_invalidate_unlock_shared(mapping);
-				goto out;
-			}
+			ret = VM_FAULT_SIGBUS;
 		}
 
+		/*
+		 * Recheck EOF and keep truncation out until the folio is in the
+		 * page cache, or until we mapped the new anonymous folio:
+		 * truncation unmaps or removes it afterwards.
+		 */
+		filemap_invalidate_lock_shared(mapping);
+		invalidate_locked = true;
+		if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false))
+			goto out;
+
 		folio = alloc_hugetlb_folio(vma, vmf->address, false);
 		if (IS_ERR(folio)) {
 			/*
@@ -6088,8 +6093,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 				ret = vmf_error(PTR_ERR(folio));
 			else
 				ret = 0;
-			if (pagecache_folio)
-				filemap_invalidate_unlock_shared(mapping);
 			goto out;
 		}
 		folio_zero_user(folio, vmf->real_address);
@@ -6110,10 +6113,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 							folio);
 				folio_put(folio);
 				ret = VM_FAULT_SIGBUS;
-				filemap_invalidate_unlock_shared(mapping);
 				goto out;
 			}
 			filemap_invalidate_unlock_shared(mapping);
+			invalidate_locked = false;
 			folio_set_hugetlb_migratable(folio);
 		} else {
 			new_anon_folio = true;
@@ -6212,6 +6215,8 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 	 */
 	spin_unlock(vmf->ptl);
 out:
+	if (invalidate_locked)
+		filemap_invalidate_unlock_shared(mapping);
 	hugetlb_vma_unlock_read(vma);
 
 	/*
mm/rmap.c +1 -1
index f35b671d26514..4193793158bc1 100644
@@ -48,7 +48,7 @@
  * hugetlbfs PageHuge() take locks in this order:
  *   hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
  *     vma_lock (hugetlb specific lock for pmd_sharing)
- *       mapping->invalidate_lock (when allocating page cache folios)
+ *       mapping->invalidate_lock (when allocating folios)
  *         folio_lock
  *           mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
  */
Signature · 1 line
2.56.1
maintainer tree notice
#80 lore raw

+ mm-hugetlb-serialize-private-faults-with-truncation.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: serialize private faults with truncation has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-serialize-private-faults-with-truncation.patch

The patch titled
Subject: mm/hugetlb: serialize private faults with truncation
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-serialize-private-faults-with-truncation.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-private-faults-with-truncation.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)" <[email protected]>
Subject: mm/hugetlb: serialize private faults with truncation
Date: Thu, 03 Dec 2026 12:37:45 +0100

When we fault in a folio in a MAP_PRIVATE hugetlb mapping at an index without a page cache folio, hugetlb_no_page() checks that the index is not beyond EOF, allocates a new anonymous folio and maps it. Truncation first updates i_size under the invalidate lock, and then unmaps the truncated range from all VMAs.

For page cache folios, we recently started rechecking EOF under filemap_invalidate_lock_shared() to handle exactly that.

However, nothing serializes the allocation of anonymous folios with
truncation: if truncation unmaps the range after we checked EOF, but before we mapped the new folio, the folio stays mapped beyond EOF.

Consequently, accessing the truncated range does not raise SIGBUS, and the process keeps the folio mapped until it unmaps it.

Reproduced by artificially delaying hugetlb_no_page() before allocating the folio, while another thread truncates the file: the page beyond EOF stays mapped.

Let's fix it by also rechecking EOF under the invalidate lock for anonymous folios, and holding it until we mapped the folio, such that truncation will unmap it.

Like faults in shared mappings, faults in private mappings now briefly take the invalidate lock in read mode, and might have to wait for a concurrent truncation. Which seems to be the right thing to do.

This has low urgency, as it requires racing a page fault in a private mapping with truncation. No need to CC stable.

Fixes fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   25 +++++++++++++++----------
 mm/rmap.c    |    2 +-
 2 files changed, 16 insertions(+), 11 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-serialize-private-faults-with-truncation +15 -10
@@ -5977,6 +5977,7 @@ static vm_fault_t hugetlb_no_page(struct
 	const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE;
 	u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
 	bool folio_locked = true, new_anon_folio = false;
+	bool invalidate_locked = false;
 	struct vm_area_struct *vma = vmf->vma;
 	struct mm_struct *mm = vma->vm_mm;
 	struct hstate *h = hstate_vma(vma);
@@ -6036,15 +6037,19 @@ static vm_fault_t hugetlb_no_page(struct
 			ret = __vmf_anon_prepare(vmf);
 			if (unlikely(ret))
 				goto out;
-		} else {
-			/* Recheck EOF before consuming an inode reservation. */
-			filemap_invalidate_lock_shared(mapping);
-			if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false)) {
-				filemap_invalidate_unlock_shared(mapping);
-				goto out;
-			}
+			ret = VM_FAULT_SIGBUS;
 		}
 
+		/*
+		 * Recheck EOF and keep truncation out until the folio is in the
+		 * page cache, or until we mapped the new anonymous folio:
+		 * truncation unmaps or removes it afterwards.
+		 */
+		filemap_invalidate_lock_shared(mapping);
+		invalidate_locked = true;
+		if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false))
+			goto out;
+
 		folio = alloc_hugetlb_folio(vma, vmf->address, false);
 		if (IS_ERR(folio)) {
 			/*
@@ -6063,8 +6068,6 @@ static vm_fault_t hugetlb_no_page(struct
 				ret = vmf_error(PTR_ERR(folio));
 			else
 				ret = 0;
-			if (pagecache_folio)
-				filemap_invalidate_unlock_shared(mapping);
 			goto out;
 		}
 		folio_zero_user(folio, vmf->real_address);
@@ -6085,10 +6088,10 @@ static vm_fault_t hugetlb_no_page(struct
 							folio);
 				folio_put(folio);
 				ret = VM_FAULT_SIGBUS;
-				filemap_invalidate_unlock_shared(mapping);
 				goto out;
 			}
 			filemap_invalidate_unlock_shared(mapping);
+			invalidate_locked = false;
 			folio_set_hugetlb_migratable(folio);
 		} else {
 			new_anon_folio = true;
@@ -6187,6 +6190,8 @@ static vm_fault_t hugetlb_no_page(struct
 	 */
 	spin_unlock(vmf->ptl);
 out:
+	if (invalidate_locked)
+		filemap_invalidate_unlock_shared(mapping);
 	hugetlb_vma_unlock_read(vma);
 
 	/*
mm/rmap.c renamed from mm/rmap.c~mm-hugetlb-serialize-private-faults-with-truncation +1 -1
@@ -48,7 +48,7 @@
  * hugetlbfs PageHuge() take locks in this order:
  *   hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
  *     vma_lock (hugetlb specific lock for pmd_sharing)
- *       mapping->invalidate_lock (when allocating page cache folios)
+ *       mapping->invalidate_lock (when allocating folios)
  *         folio_lock
  *           mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
  */

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 22/35
#81 lore raw

Re: [PATCH v2 22/35] mm/hugetlb: serialize private faults with truncation

In reply to David Hildenbrand (Arm)
Quoted 28 lines · David Hildenbrand (Arm)

When we fault in a folio in a MAP_PRIVATE hugetlb mapping at an index without a page cache folio, hugetlb_no_page() checks that the index is not beyond EOF, allocates a new anonymous folio and maps it. Truncation first updates i_size under the invalidate lock, and then unmaps the truncated range from all VMAs.

For page cache folios, we recently started rechecking EOF under filemap_invalidate_lock_shared() to handle exactly that.

However, nothing serializes the allocation of anonymous folios with
truncation: if truncation unmaps the range after we checked EOF, but before we mapped the new folio, the folio stays mapped beyond EOF.

Consequently, accessing the truncated range does not raise SIGBUS, and the process keeps the folio mapped until it unmaps it.

Reproduced by artificially delaying hugetlb_no_page() before allocating the folio, while another thread truncates the file: the page beyond EOF stays mapped.

Let's fix it by also rechecking EOF under the invalidate lock for anonymous folios, and holding it until we mapped the folio, such that truncation will unmap it.

Like faults in shared mappings, faults in private mappings now briefly take the invalidate lock in read mode, and might have to wait for a concurrent truncation. Which seems to be the right thing to do.

Quoted 5 lines

This has low urgency, as it requires racing a page fault in a private mapping with truncation. No need to CC stable.

Fixes fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
#82 lore raw

Re: [PATCH v2 22/35] mm/hugetlb: serialize private faults with truncation

In reply to David Hildenbrand (Arm)

Hi David,

What about the case where a page cache folio already exists? For a write fault in a MAP_PRIVATE VMA, hugetlb_no_page() maps it and calls hugetlb_wp() without the invalidate lock or an EOF recheck. If truncation runs after the lookup, the folio is found unmapped afterwards and the anon copy stays mapped beyond EOF.

Taking the invalidate lock before filemap_lock_hugetlb_folio() for private mappings would cover it, what do you think?

Cheers

author maintainer
#83 lore raw

PATCH v2 23/35 mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON

In reply to David Hildenbrand (Arm)

When UFFDIO_COPY places a new folio into a MAP_SHARED hugetlb mapping, hugetlb_mfill_atomic_pte() checks EOF under filemap_invalidate_lock_shared() and holds it until the folio is mapped, such that a concurrent truncation either unmaps the new folio, or we see the new i_size. For other file-backed mappings, mfill_file_over_size() checks EOF for all userfaultfd operations.

However, we don't check EOF for UFFDIO_COPY into MAP_PRIVATE mappings, where the new folio does not go into the page cache, and we don't check it for UFFDIO_POISON at all.

Consequently, both can install an anonymous folio or a poison marker beyond EOF, even without racing with truncation. Accessing that range then maps the stale folio or reports memory poison instead of raising SIGBUS, and once the file is extended again, the stale folio or poison marker remains instead of a fresh folio getting faulted in.

Reproduced by truncating a file and using UFFDIO_COPY in a private mapping or UFFDIO_POISON on the truncated range: both succeed, and after extending the file again, accessing the page returns the copied data or raises SIGBUS. Further, artificially delaying hugetlb_mfill_atomic_pte() before allocating the folio, while another thread truncates the file, leaves the page beyond EOF mapped.

Let's fix it by checking EOF under the invalidate lock for all UFFDIO_COPY and UFFDIO_POISON, and holding it until we installed the PTE. UFFDIO_CONTINUE maps an existing page cache folio under the fault mutex, which serializes against its removal on truncation.

This has low urgency, as it requires UFFDIO_COPY into a private mapping or UFFDIO_POISON beyond EOF. No need to CC stable.

Fixes fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
Fixes 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs")
---
 mm/hugetlb.c | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)
mm/hugetlb.c +22 -15
index 105cc2840f988..a23d36e7b8e93 100644
@@ -6559,13 +6559,31 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 	bool folio_in_pagecache = false;
 	pte_t dst_ptep;
 
+	/*
+	 * Check EOF and keep truncation out until we mapped the new folio or
+	 * installed the poison marker: truncation unmaps or removes it
+	 * afterwards.
+	 */
+	if (!is_continue) {
+		filemap_invalidate_lock_shared(mapping);
+		if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) {
+			if (*foliop) {
+				folio_put(*foliop);
+				*foliop = NULL;
+			}
+			ret = -EFAULT;
+			goto out;
+		}
+	}
+
 	if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) {
 		ptl = huge_pte_lock(h, dst_mm, dst_pte);
 
 		/* Don't overwrite any existing PTEs (even markers) */
 		if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) {
 			spin_unlock(ptl);
-			return -EEXIST;
+			ret = -EEXIST;
+			goto out;
 		}
 
 		VM_WARN_ON_ONCE(size == PMD_SIZE && hugetlb_pmd_shared(dst_pte));
@@ -6576,19 +6594,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 		update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
 		spin_unlock(ptl);
-		return 0;
-	}
-
-	if (alloc_pagecache_folio) {
-		filemap_invalidate_lock_shared(mapping);
-		if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) {
-			if (*foliop) {
-				folio_put(*foliop);
-				*foliop = NULL;
-			}
-			ret = -EFAULT;
-			goto out;
-		}
+		ret = 0;
+		goto out;
 	}
 
 	if (is_continue) {
@@ -6757,7 +6764,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 	spin_unlock(ptl);
 	ret = 0;
 out:
-	if (alloc_pagecache_folio)
+	if (!is_continue)
 		filemap_invalidate_unlock_shared(mapping);
 	return ret;
 out_release_unlock:
Signature · 1 line
2.56.1
#84 lore raw

Re: [PATCH v2 23/35] mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/hugetlb: MAP_SHARED UFFDIO_POISON can leave poison marker past EOF --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/hugetlb: MAP_SHARED UFFDIO_POISON can leave poison marker past EOF
--

commit d6d01fb801c8f68f26cd739daebebd40d5e13f59
Author: David Hildenbrand (Arm) <[email protected]>

mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON

This commit moves the EOF check under filemap_invalidate_lock_shared() in hugetlb_mfill_atomic_pte() so that all UFFDIO_COPY (including MAP_PRIVATE) and UFFDIO_POISON operations check EOF and hold the invalidate lock until the PTE is installed.

Quoted 57 lines
mm/hugetlb.c +21 -14
index 105cc2840f988..a23d36e7b8e93 100644
@@ -6559,13 +6559,31 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 	bool folio_in_pagecache = false;
 	pte_t dst_ptep;
 
+	/*
+	 * Check EOF and keep truncation out until we mapped the new folio or
+	 * installed the poison marker: truncation unmaps or removes it
+	 * afterwards.
+	 */
+	if (!is_continue) {
+		filemap_invalidate_lock_shared(mapping);
+		if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) {
+			if (*foliop) {
+				folio_put(*foliop);
+				*foliop = NULL;
+			}
+			ret = -EFAULT;
+			goto out;
+		}
+	}
+
 	if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) {
 		ptl = huge_pte_lock(h, dst_mm, dst_pte);
 
 		/* Don't overwrite any existing PTEs (even markers) */
 		if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) {
 			spin_unlock(ptl);
-			return -EEXIST;
+			ret = -EEXIST;
+			goto out;
 		}
 
 		VM_WARN_ON_ONCE(size == PMD_SIZE && hugetlb_pmd_shared(dst_pte));
@@ -6576,19 +6594,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 		update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
 		spin_unlock(ptl);
-		return 0;
-	}
-
-	if (alloc_pagecache_folio) {
-		filemap_invalidate_lock_shared(mapping);
-		if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) {
-			if (*foliop) {
-				folio_put(*foliop);
-				*foliop = NULL;
-			}
-			ret = -EFAULT;
-			goto out;
-		}
+		ret = 0;
+		goto out;
 	}

[ ... ]

mm/hugetlb.c int hugetlb_mfill_atomic_pte(pte_t *dst_pte, · line 6770

Quoted 9 lines
mm/hugetlb.c +1 -1
@@ -6757,7 +6764,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 	spin_unlock(ptl);
 	ret = 0;
 out:
-	if (alloc_pagecache_folio)
+	if (!is_continue)
 		filemap_invalidate_unlock_shared(mapping);
 	return ret;
 out_release_unlock:

[Severity: Medium]
Can a concurrent truncation still leave PTE_MARKER_POISONED installed past EOF on a MAP_SHARED VMA?

mfill_atomic_hugetlb() holds hugetlb_vma_lock_read(dst_vma) across hugetlb_mfill_atomic_pte(), so filemap_invalidate_unlock_shared(mapping) wakes hugetlb_vmtruncate() while the VMA lock is still held in read mode:

CPU0 (UFFDIO_POISON) CPU1 (ftruncate)
mfill_atomic_hugetlb()
hugetlb_vma_lock_read(dst_vma)
hugetlb_mfill_atomic_pte()
filemap_invalidate_lock_shared()
hugetlb_pgoff_valid() == true
set_huge_pte_at(..., POISONED)
hugetlb_vmtruncate()
filemap_invalidate_lock() (waits)
filemap_invalidate_unlock_shared()
i_size_write(inode, offset) filemap_invalidate_unlock() i_mmap_lock_write(mapping) hugetlb_vmdelete_list()
hugetlb_vma_trylock_write()
<-- fails, skips dst_vma
i_mmap_unlock_write(mapping) remove_inode_hugepages()
<-- no folio, marker remains
hugetlb_vma_unlock_read(dst_vma)

Because __vma_shareable_lock(dst_vma) is true for a MAP_SHARED hugetlb VMA, hugetlb_vmdelete_list() calls hugetlb_vma_trylock_write() and skips dst_vma on contention. Unlike MAP_SHARED UFFDIO_COPY, UFFDIO_POISON inserts no page-cache folio, so remove_inode_hugepages() never revisits dst_vma to zap the marker.

Subsequent accesses to the truncated range then hit VM_FAULT_HWPOISON_LARGE instead of an ordinary EOF SIGBUS, and extending the file later leaves the stale poison marker in place instead of faulting in a fresh folio.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
author maintainer
#85 lore raw

Re: [PATCH v2 23/35] mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON

Quoted 5 lines
Quoted 1 line

out_release_unlock:

[Severity: Medium]
Can a concurrent truncation still leave PTE_MARKER_POISONED installed past EOF on a MAP_SHARED VMA?

Yes, and I don't care about that.

Signature · 3 lines
Cheers,

David
maintainer tree notice
#86 lore raw

+ mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.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)" <[email protected]>
Subject: mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON
Date: Thu, 03 Dec 2026 12:37:46 +0100

When UFFDIO_COPY places a new folio into a MAP_SHARED hugetlb mapping, hugetlb_mfill_atomic_pte() checks EOF under filemap_invalidate_lock_shared() and holds it until the folio is mapped, such that a concurrent truncation either unmaps the new folio, or we see the new i_size. For other file-backed mappings, mfill_file_over_size() checks EOF for all userfaultfd operations.

However, we don't check EOF for UFFDIO_COPY into MAP_PRIVATE mappings, where the new folio does not go into the page cache, and we don't check it for UFFDIO_POISON at all.

Consequently, both can install an anonymous folio or a poison marker beyond EOF, even without racing with truncation. Accessing that range then maps the stale folio or reports memory poison instead of raising SIGBUS, and once the file is extended again, the stale folio or poison marker remains instead of a fresh folio getting faulted in.

Reproduced by truncating a file and using UFFDIO_COPY in a private mapping or UFFDIO_POISON on the truncated range: both succeed, and after extending the file again, accessing the page returns the copied data or raises SIGBUS. Further, artificially delaying hugetlb_mfill_atomic_pte() before allocating the folio, while another thread truncates the file, leaves the page beyond EOF mapped.

Let's fix it by checking EOF under the invalidate lock for all UFFDIO_COPY and UFFDIO_POISON, and holding it until we installed the PTE. UFFDIO_CONTINUE maps an existing page cache folio under the fault mutex, which serializes against its removal on truncation.

This has low urgency, as it requires UFFDIO_COPY into a private mapping or UFFDIO_POISON beyond EOF. No need to CC stable.

Fixes fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
Fixes 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison +22 -15
@@ -6534,13 +6534,31 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_
 	bool folio_in_pagecache = false;
 	pte_t dst_ptep;
 
+	/*
+	 * Check EOF and keep truncation out until we mapped the new folio or
+	 * installed the poison marker: truncation unmaps or removes it
+	 * afterwards.
+	 */
+	if (!is_continue) {
+		filemap_invalidate_lock_shared(mapping);
+		if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) {
+			if (*foliop) {
+				folio_put(*foliop);
+				*foliop = NULL;
+			}
+			ret = -EFAULT;
+			goto out;
+		}
+	}
+
 	if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) {
 		ptl = huge_pte_lock(h, dst_mm, dst_pte);
 
 		/* Don't overwrite any existing PTEs (even markers) */
 		if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) {
 			spin_unlock(ptl);
-			return -EEXIST;
+			ret = -EEXIST;
+			goto out;
 		}
 
 		VM_WARN_ON_ONCE(size == PMD_SIZE && hugetlb_pmd_shared(dst_pte));
@@ -6551,19 +6569,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_
 		update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
 		spin_unlock(ptl);
-		return 0;
-	}
-
-	if (alloc_pagecache_folio) {
-		filemap_invalidate_lock_shared(mapping);
-		if (!hugetlb_pgoff_valid(mapping, idx, /* partial= */ false)) {
-			if (*foliop) {
-				folio_put(*foliop);
-				*foliop = NULL;
-			}
-			ret = -EFAULT;
-			goto out;
-		}
+		ret = 0;
+		goto out;
 	}
 
 	if (is_continue) {
@@ -6732,7 +6739,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_
 	spin_unlock(ptl);
 	ret = 0;
 out:
-	if (alloc_pagecache_folio)
+	if (!is_continue)
 		filemap_invalidate_unlock_shared(mapping);
 	return ret;
 out_release_unlock:

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 23/35
#87 lore raw

Re: [PATCH v2 23/35] mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON

In reply to David Hildenbrand (Arm)
Quoted 29 lines · David Hildenbrand (Arm)

When UFFDIO_COPY places a new folio into a MAP_SHARED hugetlb mapping, hugetlb_mfill_atomic_pte() checks EOF under filemap_invalidate_lock_shared() and holds it until the folio is mapped, such that a concurrent truncation either unmaps the new folio, or we see the new i_size. For other file-backed mappings, mfill_file_over_size() checks EOF for all userfaultfd operations.

However, we don't check EOF for UFFDIO_COPY into MAP_PRIVATE mappings, where the new folio does not go into the page cache, and we don't check it for UFFDIO_POISON at all.

Consequently, both can install an anonymous folio or a poison marker beyond EOF, even without racing with truncation. Accessing that range then maps the stale folio or reports memory poison instead of raising SIGBUS, and once the file is extended again, the stale folio or poison marker remains instead of a fresh folio getting faulted in.

Reproduced by truncating a file and using UFFDIO_COPY in a private mapping or UFFDIO_POISON on the truncated range: both succeed, and after extending the file again, accessing the page returns the copied data or raises SIGBUS. Further, artificially delaying hugetlb_mfill_atomic_pte() before allocating the folio, while another thread truncates the file, leaves the page beyond EOF mapped.

Let's fix it by checking EOF under the invalidate lock for all UFFDIO_COPY and UFFDIO_POISON, and holding it until we installed the PTE. UFFDIO_CONTINUE maps an existing page cache folio under the fault mutex, which serializes against its removal on truncation.

Quoted 6 lines

This has low urgency, as it requires UFFDIO_COPY into a private mapping or UFFDIO_POISON beyond EOF. No need to CC stable.

Fixes fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
Fixes 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#88 lore raw

PATCH v2 24/35 hugetlbfs: seal SHM_HUGETLB files against growth

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

The size of a SysV shared memory segment is fixed at shmget(), and do_shmat() relies on that by mapping i_size bytes of the backing file.

However, with SHM_HUGETLB, the hugetlbfs file can be grown through an fd obtained from /proc/<pid>/map_files with ftruncate() or fallocate(). Also, remap_file_pages() with a pgoff beyond the segment can grow it, because hugetlbfs_file_mmap() grows the file to the end of a writable mapping:

shmget(key, 4M, SHM_HUGETLB) i_size = 4M
addr = shmat(id, NULL, 0) remap_file_pages(addr, 2M, 0, 6M >> PAGE_SHIFT, 0)
i_size = 8M
shmat(id, NULL, 0) maps 8M

Later calls to shmat() will map the bigger size.

Using /proc/<pid>/map_files requires CAP_CHECKPOINT_RESTORE, but remap_file_pages() does not require any privileges, so we focus on the latter.

Let's fix it by setting F_SEAL_GROW on the inode when creating it for a segment. remap_file_pages() beyond the segment still succeeds, like for shmem segments, but accessing pages there now correctly results in SIGBUS.

This has low urgency, and likely nobody ever ran into this in practice. Let's not CC stable.

Fixes c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
[ david: adjust description ]
---
 fs/hugetlbfs/inode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +4 -1
index 5f44236785b5a..43d79c9ea8c1b 100644
@@ -1561,8 +1561,11 @@ struct file *hugetlb_file_setup(const char *name, size_t size,
 				    S_IFREG | S_IRWXUGO, 0);
 	if (!inode)
 		goto out;
-	if (creat_flags == HUGETLB_SHMFS_INODE)
+	if (creat_flags == HUGETLB_SHMFS_INODE) {
 		inode->i_flags |= S_PRIVATE;
+		/* The size of a segment is fixed at shmget(). */
+		HUGETLBFS_I(inode)->seals |= F_SEAL_GROW;
+	}
 
 	inode->i_size = size;
 	clear_nlink(inode);
Signature · 1 line
2.56.1
maintainer tree notice
#89 lore raw

+ hugetlbfs-seal-shm_hugetlb-files-against-growth.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: seal SHM_HUGETLB files against growth has been added to the -mm mm-unstable branch. Its filename is hugetlbfs-seal-shm_hugetlb-files-against-growth.patch

The patch titled
Subject: hugetlbfs: seal SHM_HUGETLB files against growth
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-seal-shm_hugetlb-files-against-growth.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-seal-shm_hugetlb-files-against-growth.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: Chris Mason <[email protected]>
Subject: hugetlbfs: seal SHM_HUGETLB files against growth
Date: Thu, 03 Dec 2026 12:37:47 +0100

The size of a SysV shared memory segment is fixed at shmget(), and do_shmat() relies on that by mapping i_size bytes of the backing file.

However, with SHM_HUGETLB, the hugetlbfs file can be grown through an fd obtained from /proc/<pid>/map_files with ftruncate() or fallocate(). Also, remap_file_pages() with a pgoff beyond the segment can grow it, because hugetlbfs_file_mmap() grows the file to the end of a writable mapping:

shmget(key, 4M, SHM_HUGETLB) i_size = 4M
addr = shmat(id, NULL, 0) remap_file_pages(addr, 2M, 0, 6M >> PAGE_SHIFT, 0)
i_size = 8M
shmat(id, NULL, 0) maps 8M

Later calls to shmat() will map the bigger size.

Using /proc/<pid>/map_files requires CAP_CHECKPOINT_RESTORE, but remap_file_pages() does not require any privileges, so we focus on the latter.

Let's fix it by setting F_SEAL_GROW on the inode when creating it for a segment. remap_file_pages() beyond the segment still succeeds, like for shmem segments, but accessing pages there now correctly results in SIGBUS.

This has low urgency, and likely nobody ever ran into this in practice. Let's not CC stable.

[[email protected]: adjust description]
Fixes c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-seal-shm_hugetlb-files-against-growth +4 -1
@@ -1561,8 +1561,11 @@ struct file *hugetlb_file_setup(const ch
 				    S_IFREG | S_IRWXUGO, 0);
 	if (!inode)
 		goto out;
-	if (creat_flags == HUGETLB_SHMFS_INODE)
+	if (creat_flags == HUGETLB_SHMFS_INODE) {
 		inode->i_flags |= S_PRIVATE;
+		/* The size of a segment is fixed at shmget(). */
+		HUGETLBFS_I(inode)->seals |= F_SEAL_GROW;
+	}
 
 	inode->i_size = size;
 	clear_nlink(inode);

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch

maintainer Acked-by on v2 24/35
#90 lore raw

Re: [PATCH v2 24/35] hugetlbfs: seal SHM_HUGETLB files against growth

In reply to David Hildenbrand (Arm)
Quoted 32 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

The size of a SysV shared memory segment is fixed at shmget(), and do_shmat() relies on that by mapping i_size bytes of the backing file.

However, with SHM_HUGETLB, the hugetlbfs file can be grown through an fd obtained from /proc/<pid>/map_files with ftruncate() or fallocate(). Also, remap_file_pages() with a pgoff beyond the segment can grow it, because hugetlbfs_file_mmap() grows the file to the end of a writable mapping:

shmget(key, 4M, SHM_HUGETLB) i_size = 4M
addr = shmat(id, NULL, 0) remap_file_pages(addr, 2M, 0, 6M >> PAGE_SHIFT, 0)

                                           i_size = 8M

shmat(id, NULL, 0) maps 8M

Later calls to shmat() will map the bigger size.

Using /proc/<pid>/map_files requires CAP_CHECKPOINT_RESTORE, but remap_file_pages() does not require any privileges, so we focus on the latter.

Let's fix it by setting F_SEAL_GROW on the inode when creating it for a segment. remap_file_pages() beyond the segment still succeeds, like for shmem segments, but accessing pages there now correctly results in SIGBUS.

Quoted 7 lines

This has low urgency, and likely nobody ever ran into this in practice. Let's not CC stable.

Fixes c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
[ david: adjust description ]

Thanks.

Reviewed-by on v2 24/35
#91 lore raw

Re: [PATCH v2 24/35] hugetlbfs: seal SHM_HUGETLB files against growth

In reply to David Hildenbrand (Arm)
Quoted 28 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

The size of a SysV shared memory segment is fixed at shmget(), and do_shmat() relies on that by mapping i_size bytes of the backing file.

However, with SHM_HUGETLB, the hugetlbfs file can be grown through an fd obtained from /proc/<pid>/map_files with ftruncate() or fallocate(). Also, remap_file_pages() with a pgoff beyond the segment can grow it, because hugetlbfs_file_mmap() grows the file to the end of a writable mapping:

shmget(key, 4M, SHM_HUGETLB) i_size = 4M
addr = shmat(id, NULL, 0) remap_file_pages(addr, 2M, 0, 6M >> PAGE_SHIFT, 0)
i_size = 8M
shmat(id, NULL, 0) maps 8M

Later calls to shmat() will map the bigger size.

Using /proc/<pid>/map_files requires CAP_CHECKPOINT_RESTORE, but remap_file_pages() does not require any privileges, so we focus on the latter.

Let's fix it by setting F_SEAL_GROW on the inode when creating it for a segment. remap_file_pages() beyond the segment still succeeds, like for shmem segments, but accessing pages there now correctly results in SIGBUS.

Quoted 7 lines

This has low urgency, and likely nobody ever ran into this in practice. Let's not CC stable.

Fixes c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
[ david: adjust description ]

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#92 lore raw

PATCH v2 25/35 hugetlbfs: zero all hugetlbfs-specific fields of new inodes

In reply to David Hildenbrand (Arm)

hugetlbfs_get_inode() initializes the seals of new hugetlbfs inodes, and the hugetlbfs inode tracepoints record them.

However, we don't initialize the seals in two cases:

(1) For the root inode through hugetlbfs_get_root()

(2) For an inode that alloc_inode() destroys right away because inode_init_always() failed.

Consequently, when such an inode is evicted or destroyed with the tracepoints enabled, we record stale seals, and KMSAN reports an uninit-value.

Reproduced by closing a hugetlb memfd with seals, and mounting and unmounting hugetlbfs with the hugetlbfs_evict_inode tracepoint enabled:
the root inode reports the seals of the memfd.

Let's fix it by zeroing all hugetlbfs-specific inode fields when allocating the inode in hugetlbfs_alloc_inode().

We can now drop the manual initialization of resv_map in hugetlbfs_get_root().

This has low urgency, as it only affects the tracepoints. No need to CC stable.

Fixes 318580ad7f28 ("hugetlbfs: support tracepoint")
---
 fs/hugetlbfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hugetlbfs/inode.c +1 -1
index 43d79c9ea8c1b..530c18430cbd0 100644
@@ -905,7 +905,6 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,
 		simple_inode_init_ts(inode);
 		inode->i_op = &hugetlbfs_dir_inode_operations;
 		inode->i_fop = &simple_dir_operations;
-		HUGETLBFS_I(inode)->resv_map = NULL;
 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
 		inc_nlink(inode);
 		lockdep_annotate_inode_mutex_key(inode);
@@ -1207,6 +1206,7 @@ static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
 		hugetlbfs_inc_free_inodes(sbinfo);
 		return NULL;
 	}
+	memset_after(p, 0, vfs_inode);
 	return &p->vfs_inode;
 }
 
Signature · 1 line
2.56.1
maintainer tree notice
#93 lore raw

+ hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.patch added to mm-unstable branch

The patch titled Subject: hugetlbfs: zero all hugetlbfs-specific fields of new inodes has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: hugetlbfs: zero all hugetlbfs-specific fields of new inodes
has been added to the -mm mm-unstable branch. Its filename is
hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.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)" <[email protected]>
Subject: hugetlbfs: zero all hugetlbfs-specific fields of new inodes
Date: Thu, 03 Dec 2026 12:37:48 +0100

hugetlbfs_get_inode() initializes the seals of new hugetlbfs inodes, and the hugetlbfs inode tracepoints record them.

However, we don't initialize the seals in two cases:

(1) For the root inode through hugetlbfs_get_root()

(2) For an inode that alloc_inode() destroys right away because inode_init_always() failed.

Consequently, when such an inode is evicted or destroyed with the tracepoints enabled, we record stale seals, and KMSAN reports an uninit-value.

Reproduced by closing a hugetlb memfd with seals, and mounting and unmounting hugetlbfs with the hugetlbfs_evict_inode tracepoint enabled:
the root inode reports the seals of the memfd.

Let's fix it by zeroing all hugetlbfs-specific inode fields when allocating the inode in hugetlbfs_alloc_inode().

We can now drop the manual initialization of resv_map in hugetlbfs_get_root().

This has low urgency, as it only affects the tracepoints. No need to CC stable.

Fixes 318580ad7f28 ("hugetlbfs: support tracepoint")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/hugetlbfs/inode.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes +1 -1
@@ -905,7 +905,6 @@ static struct inode *hugetlbfs_get_root(
 		simple_inode_init_ts(inode);
 		inode->i_op = &hugetlbfs_dir_inode_operations;
 		inode->i_fop = &simple_dir_operations;
-		HUGETLBFS_I(inode)->resv_map = NULL;
 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
 		inc_nlink(inode);
 		lockdep_annotate_inode_mutex_key(inode);
@@ -1207,6 +1206,7 @@ static struct inode *hugetlbfs_alloc_ino
 		hugetlbfs_inc_free_inodes(sbinfo);
 		return NULL;
 	}
+	memset_after(p, 0, vfs_inode);
 	return &p->vfs_inode;
 }
 

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 25/35
#94 lore raw

Re: [PATCH v2 25/35] hugetlbfs: zero all hugetlbfs-specific fields of new inodes

In reply to David Hildenbrand (Arm)
Quoted 28 lines · David Hildenbrand (Arm)

hugetlbfs_get_inode() initializes the seals of new hugetlbfs inodes, and the hugetlbfs inode tracepoints record them.

However, we don't initialize the seals in two cases:

(1) For the root inode through hugetlbfs_get_root()

(2) For an inode that alloc_inode() destroys right away because inode_init_always() failed.

Consequently, when such an inode is evicted or destroyed with the tracepoints enabled, we record stale seals, and KMSAN reports an uninit-value.

Reproduced by closing a hugetlb memfd with seals, and mounting and unmounting hugetlbfs with the hugetlbfs_evict_inode tracepoint enabled:
the root inode reports the seals of the memfd.

Let's fix it by zeroing all hugetlbfs-specific inode fields when allocating the inode in hugetlbfs_alloc_inode().

We can now drop the manual initialization of resv_map in hugetlbfs_get_root().

Quoted 6 lines

This has low urgency, as it only affects the tracepoints. No need to CC stable.

Fixes 318580ad7f28 ("hugetlbfs: support tracepoint")

Thanks.

Reviewed-by on v2 25/35
#95 lore raw

Re: [PATCH v2 25/35] hugetlbfs: zero all hugetlbfs-specific fields of new inodes

In reply to David Hildenbrand (Arm)
Quoted 24 lines · David Hildenbrand (Arm)

hugetlbfs_get_inode() initializes the seals of new hugetlbfs inodes, and the hugetlbfs inode tracepoints record them.

However, we don't initialize the seals in two cases:

(1) For the root inode through hugetlbfs_get_root()

(2) For an inode that alloc_inode() destroys right away because inode_init_always() failed.

Consequently, when such an inode is evicted or destroyed with the tracepoints enabled, we record stale seals, and KMSAN reports an uninit-value.

Reproduced by closing a hugetlb memfd with seals, and mounting and unmounting hugetlbfs with the hugetlbfs_evict_inode tracepoint enabled:
the root inode reports the seals of the memfd.

Let's fix it by zeroing all hugetlbfs-specific inode fields when allocating the inode in hugetlbfs_alloc_inode().

We can now drop the manual initialization of resv_map in hugetlbfs_get_root().

Quoted 6 lines

This has low urgency, as it only affects the tracepoints. No need to CC stable.

Fixes 318580ad7f28 ("hugetlbfs: support tracepoint")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#96 lore raw

PATCH v2 26/35 mm/rmap: don't fail unmapping hugetlb folios from private VMAs

In reply to David Hildenbrand (Arm)

Before unmapping a hugetlb page cache folio, try_to_migrate_one() and try_to_unmap_poisoned_hugetlb_one() try to unshare the PMD table, which requires the hugetlb VMA lock in write mode.

As we already hold the i_mmap_rwsem, we can only trylock it, and give up if that fails.

Since private hugetlb VMAs have a VMA lock as well, the trylock can also fail on private VMAs, for example while another thread is faulting in that VMA. PMD table sharing does not apply in private VMAs, so there is nothing to unshare and we fail for no reason: migration fails more often, and memory failure might leave a poisoned folio mapped.

Let's fix it by only taking the VMA lock and trying to unshare if PMD tables can be shared.

Fortunately, we can drop the anon folio checks now.

This has low urgency, as it requires racing with, for example, a page fault in the private VMA, and migration is usually retried.

Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")
---
 mm/rmap.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/rmap.c +2 -4
index 4193793158bc1..b85ab855a438a 100644
@@ -2049,7 +2049,7 @@ static bool try_to_unmap_poisoned_hugetlb_one(struct folio *folio,
 	 * acquiring vma_lock BEFORE i_mmap_rwsem. We can only try lock here and
 	 * fail if unsuccessful.
 	 */
-	if (!folio_test_anon(folio)) {
+	if (__vma_shareable_lock(vma)) {
 		struct mmu_gather tlb;
 
 		VM_WARN_ON_ONCE(!(flags & TTU_RMAP_LOCKED));
@@ -2580,8 +2580,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 				 PageAnonExclusive(subpage);
 
 		if (folio_test_hugetlb(folio)) {
-			bool anon = folio_test_anon(folio);
-
 			/*
 			 * huge_pmd_unshare may unmap an entire PMD page.
 			 * There is no way of knowing exactly which PMDs may
@@ -2601,7 +2599,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 			 * i_mmap_rwsem.  We can only try lock here and
 			 * fail if unsuccessful.
 			 */
-			if (!anon) {
+			if (__vma_shareable_lock(vma)) {
 				struct mmu_gather tlb;
 
 				VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
Signature · 1 line
2.56.1
maintainer tree notice
#97 lore raw

+ mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.patch added to mm-unstable branch

The patch titled Subject: mm/rmap: don't fail unmapping hugetlb folios from private VMAs has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/rmap: don't fail unmapping hugetlb folios from private VMAs
has been added to the -mm mm-unstable branch. Its filename is
mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.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)" <[email protected]>
Subject: mm/rmap: don't fail unmapping hugetlb folios from private VMAs
Date: Thu, 03 Dec 2026 12:37:49 +0100

Before unmapping a hugetlb page cache folio, try_to_migrate_one() and try_to_unmap_poisoned_hugetlb_one() try to unshare the PMD table, which requires the hugetlb VMA lock in write mode.

As we already hold the i_mmap_rwsem, we can only trylock it, and give up if that fails.

Since private hugetlb VMAs have a VMA lock as well, the trylock can also fail on private VMAs, for example while another thread is faulting in that VMA. PMD table sharing does not apply in private VMAs, so there is nothing to unshare and we fail for no reason: migration fails more often, and memory failure might leave a poisoned folio mapped.

Let's fix it by only taking the VMA lock and trying to unshare if PMD tables can be shared.

Fortunately, we can drop the anon folio checks now.

This has low urgency, as it requires racing with, for example, a page fault in the private VMA, and migration is usually retried.

Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Chris Mason <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/rmap.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/rmap.c renamed from mm/rmap.c~mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas +2 -4
@@ -2049,7 +2049,7 @@ static bool try_to_unmap_poisoned_hugetl
 	 * acquiring vma_lock BEFORE i_mmap_rwsem. We can only try lock here and
 	 * fail if unsuccessful.
 	 */
-	if (!folio_test_anon(folio)) {
+	if (__vma_shareable_lock(vma)) {
 		struct mmu_gather tlb;
 
 		VM_WARN_ON_ONCE(!(flags & TTU_RMAP_LOCKED));
@@ -2580,8 +2580,6 @@ static bool try_to_migrate_one(struct fo
 				 PageAnonExclusive(subpage);
 
 		if (folio_test_hugetlb(folio)) {
-			bool anon = folio_test_anon(folio);
-
 			/*
 			 * huge_pmd_unshare may unmap an entire PMD page.
 			 * There is no way of knowing exactly which PMDs may
@@ -2601,7 +2599,7 @@ static bool try_to_migrate_one(struct fo
 			 * i_mmap_rwsem.  We can only try lock here and
 			 * fail if unsuccessful.
 			 */
-			if (!anon) {
+			if (__vma_shareable_lock(vma)) {
 				struct mmu_gather tlb;
 
 				VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));

_

Patches currently in -mm which might be from [email protected] 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

maintainer Acked-by on v2 26/35
#98 lore raw

Re: [PATCH v2 26/35] mm/rmap: don't fail unmapping hugetlb folios from private VMAs

In reply to David Hildenbrand (Arm)
Quoted 20 lines · David Hildenbrand (Arm)

Before unmapping a hugetlb page cache folio, try_to_migrate_one() and try_to_unmap_poisoned_hugetlb_one() try to unshare the PMD table, which requires the hugetlb VMA lock in write mode.

As we already hold the i_mmap_rwsem, we can only trylock it, and give up if that fails.

Since private hugetlb VMAs have a VMA lock as well, the trylock can also fail on private VMAs, for example while another thread is faulting in that VMA. PMD table sharing does not apply in private VMAs, so there is nothing to unshare and we fail for no reason: migration fails more often, and memory failure might leave a poisoned folio mapped.

Let's fix it by only taking the VMA lock and trying to unshare if PMD tables can be shared.

Quoted 8 lines

Fortunately, we can drop the anon folio checks now.

This has low urgency, as it requires racing with, for example, a page fault in the private VMA, and migration is usually retried.

Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")

Thanks.

Reviewed-by on v2 26/35
#99 lore raw

Re: [PATCH v2 26/35] mm/rmap: don't fail unmapping hugetlb folios from private VMAs

In reply to David Hildenbrand (Arm)
Quoted 16 lines · David Hildenbrand (Arm)

Before unmapping a hugetlb page cache folio, try_to_migrate_one() and try_to_unmap_poisoned_hugetlb_one() try to unshare the PMD table, which requires the hugetlb VMA lock in write mode.

As we already hold the i_mmap_rwsem, we can only trylock it, and give up if that fails.

Since private hugetlb VMAs have a VMA lock as well, the trylock can also fail on private VMAs, for example while another thread is faulting in that VMA. PMD table sharing does not apply in private VMAs, so there is nothing to unshare and we fail for no reason: migration fails more often, and memory failure might leave a poisoned folio mapped.

Let's fix it by only taking the VMA lock and trying to unshare if PMD tables can be shared.

Quoted 8 lines

Fortunately, we can drop the anon folio checks now.

This has low urgency, as it requires racing with, for example, a page fault in the private VMA, and migration is usually retried.

Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#100 lore raw

PATCH v2 27/35 mm/hugetlb: only check PMD table sharing for PMD-sized folios

In reply to David Hildenbrand (Arm)

hugetlb_pmd_shared() reads "pt_share_count" from the ptdesc of the page table that maps a hugetlb folio, independent of the folio size, to tell whether that page table is shared. Only PMD tables have a valid "pt_share_count"; for other folio sizes, such as 1 GiB folios on x86 that are mapped by a PUD table, we read whatever another user of the union left behind.

As a result, such folios might be reported as shared in smaps and as not exclusively mapped in pagemap, and mbind(MPOL_MF_MOVE) and migrate_pages() might skip them.

Let's fix it by adding hugetlb_folio_maybe_mapped_shared(), which combines folio_maybe_mapped_shared(), documented to not care about page table sharing, with a check for PMD table sharing only for PMD-sized folios, and using it in the affected callers.

This has low urgency.

Fixes ca1a47cd3f5f ("mm/hugetlb: fix hugetlb_pmd_shared()")
---
 fs/proc/task_mmu.c      |  6 ++----
 include/linux/hugetlb.h | 16 ++++++++++++++++
 mm/mempolicy.c          |  2 +-
 3 files changed, 19 insertions(+), 5 deletions(-)
fs/proc/task_mmu.c +2 -4
index de8eded04e32a..d9ecb94f4daa5 100644
@@ -1211,8 +1211,7 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
 
 	if (folio) {
 		/* We treat non-present entries as "maybe shared". */
-		if (!present || folio_maybe_mapped_shared(folio) ||
-		    hugetlb_pmd_shared(pte))
+		if (!present || hugetlb_folio_maybe_mapped_shared(folio, pte))
 			mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
 		else
 			mss->private_hugetlb += huge_page_size(hstate_vma(vma));
@@ -2134,8 +2133,7 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
 		if (!folio_test_anon(folio))
 			flags |= PM_FILE;
 
-		if (!folio_maybe_mapped_shared(folio) &&
-		    !hugetlb_pmd_shared(ptep))
+		if (!hugetlb_folio_maybe_mapped_shared(folio, ptep))
 			flags |= PM_MMAP_EXCLUSIVE;
 
 		if (huge_pte_uffd(pte))
include/linux/hugetlb.h +16 -0
index 0d5414392d870..49dad5ae57512 100644
@@ -1386,6 +1386,22 @@ static inline bool hugetlb_pmd_shared(pte_t *pte)
 }
 #endif
 
+#ifdef CONFIG_HUGETLB_PAGE
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+		pte_t *ptep)
+{
+	if (folio_maybe_mapped_shared(folio))
+		return true;
+	return folio_size(folio) == PMD_SIZE && hugetlb_pmd_shared(ptep);
+}
+#else /* !CONFIG_HUGETLB_PAGE */
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+		pte_t *ptep)
+{
+	return true;
+}
+#endif /* CONFIG_HUGETLB_PAGE */
+
 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
 
 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
mm/mempolicy.c +1 -1
index 6f800fb961aec..229efcdb2c7db 100644
@@ -833,7 +833,7 @@ static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask,
 	 * cannot easily detect if a folio is shared.
 	 */
 	if ((flags & MPOL_MF_MOVE_ALL) ||
-	    (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte)))
+	    !hugetlb_folio_maybe_mapped_shared(folio, pte))
 		if (!folio_isolate_hugetlb(folio, qp->pagelist))
 			qp->nr_failed++;
 unlock:
Signature · 1 line
2.56.1
#101 lore raw

Re: [PATCH v2 27/35] mm/hugetlb: only check PMD table sharing for PMD-sized folios

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mm: update folio_maybe_mapped_shared() kernel-doc for hugetlb_pmd_shared() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm: update folio_maybe_mapped_shared() kernel-doc for hugetlb_pmd_shared()
--

commit bede3b675b31c20070ddfc902b25a666f7cf92fc
Author: David Hildenbrand (Arm) <[email protected]>

mm/hugetlb: only check PMD table sharing for PMD-sized folios

This commit adds hugetlb_folio_maybe_mapped_shared() to restrict the hugetlb_pmd_shared() check to PMD-sized folios, and switches the smaps, pagemap, and mempolicy callers to use it.

Quoted 6 lines
include/linux/hugetlb.h +0 -0
index 0d5414392d870..49dad5ae57512 100644
@@ -1386,6 +1386,22 @@ static inline bool hugetlb_pmd_shared(pte_t *pte)
 }

include/linux/hugetlb.h static inline bool hugetlb_pmd_shared(pte_t *pte) · line 1396

Quoted 10 lines
include/linux/hugetlb.h +8 -0
 #endif
 
+#ifdef CONFIG_HUGETLB_PAGE
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+		pte_t *ptep)
+{
+	if (folio_maybe_mapped_shared(folio))
+		return true;
+	return folio_size(folio) == PMD_SIZE && hugetlb_pmd_shared(ptep);
+}

[Severity: Low]
Should the kernel-doc for folio_maybe_mapped_shared() in include/linux/mm.h be updated to point callers to hugetlb_folio_maybe_mapped_shared()?

It currently says:

include/linux/mm.h:folio_maybe_mapped_shared() {
* #. If hugetlb page table sharing applies. Callers might want to check
* hugetlb_pmd_shared().
}

Leaving this unchanged invites future callers to call hugetlb_pmd_shared() directly on non-PMD-sized hugetlb folios and reintroduce the same ptdesc union-aliasing bug.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27
author maintainer
#102 lore raw

Re: [PATCH v2 27/35] mm/hugetlb: only check PMD table sharing for PMD-sized folios

Quoted 23 lines · On 12/3/26 13:29, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm: update folio_maybe_mapped_shared() kernel-doc for hugetlb_pmd_shared()
--

commit bede3b675b31c20070ddfc902b25a666f7cf92fc
Author: David Hildenbrand (Arm) <[email protected]>

mm/hugetlb: only check PMD table sharing for PMD-sized folios

This commit adds hugetlb_folio_maybe_mapped_shared() to restrict the hugetlb_pmd_shared() check to PMD-sized folios, and switches the smaps, pagemap, and mempolicy callers to use it.

Quoted 8 lines
include/linux/hugetlb.h +0 -0
index 0d5414392d870..49dad5ae57512 100644
@@ -1386,6 +1386,22 @@ static inline bool hugetlb_pmd_shared(pte_t *pte)
 }
 #endif
 
Quoted 13 lines

include/linux/hugetlb.h static inline bool hugetlb_pmd_shared(pte_t *pte) · line 1396

Quoted 8 lines
include/linux/hugetlb.h +8 -0
+#ifdef CONFIG_HUGETLB_PAGE
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+		pte_t *ptep)
+{
+	if (folio_maybe_mapped_shared(folio))
+		return true;
+	return folio_size(folio) == PMD_SIZE && hugetlb_pmd_shared(ptep);
+}

[Severity: Low]
Should the kernel-doc for folio_maybe_mapped_shared() in include/linux/mm.h be updated to point callers to hugetlb_folio_maybe_mapped_shared()?

Not required. If I have to resend I might consider it.

Signature · 3 lines
Cheers,

David
maintainer tree notice
#103 lore raw

+ mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: mm/hugetlb: only check PMD table sharing for PMD-sized folios has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: only check PMD table sharing for PMD-sized folios
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.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)" <[email protected]>
Subject: mm/hugetlb: only check PMD table sharing for PMD-sized folios
Date: Thu, 03 Dec 2026 12:37:50 +0100

hugetlb_pmd_shared() reads "pt_share_count" from the ptdesc of the page table that maps a hugetlb folio, independent of the folio size, to tell whether that page table is shared. Only PMD tables have a valid "pt_share_count"; for other folio sizes, such as 1 GiB folios on x86 that are mapped by a PUD table, we read whatever another user of the union left behind.

As a result, such folios might be reported as shared in smaps and as not exclusively mapped in pagemap, and mbind(MPOL_MF_MOVE) and migrate_pages() might skip them.

Let's fix it by adding hugetlb_folio_maybe_mapped_shared(), which combines folio_maybe_mapped_shared(), documented to not care about page table sharing, with a check for PMD table sharing only for PMD-sized folios, and using it in the affected callers.

This has low urgency.

Fixes ca1a47cd3f5f ("mm/hugetlb: fix hugetlb_pmd_shared()")
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 fs/proc/task_mmu.c      |    6 ++----
 include/linux/hugetlb.h |   16 ++++++++++++++++
 mm/mempolicy.c          |    2 +-
 3 files changed, 19 insertions(+), 5 deletions(-)
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios +2 -4
@@ -1216,8 +1216,7 @@ static int smaps_hugetlb_range(pte_t *pt
 
 	if (folio) {
 		/* We treat non-present entries as "maybe shared". */
-		if (!present || folio_maybe_mapped_shared(folio) ||
-		    hugetlb_pmd_shared(pte))
+		if (!present || hugetlb_folio_maybe_mapped_shared(folio, pte))
 			mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
 		else
 			mss->private_hugetlb += huge_page_size(hstate_vma(vma));
@@ -2140,8 +2139,7 @@ static int pagemap_hugetlb_range(pte_t *
 		if (!folio_test_anon(folio))
 			flags |= PM_FILE;
 
-		if (!folio_maybe_mapped_shared(folio) &&
-		    !hugetlb_pmd_shared(ptep))
+		if (!hugetlb_folio_maybe_mapped_shared(folio, ptep))
 			flags |= PM_MMAP_EXCLUSIVE;
 
 		if (huge_pte_uffd(pte))
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios +16 -0
@@ -1386,6 +1386,22 @@ static inline bool hugetlb_pmd_shared(pt
 }
 #endif
 
+#ifdef CONFIG_HUGETLB_PAGE
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+		pte_t *ptep)
+{
+	if (folio_maybe_mapped_shared(folio))
+		return true;
+	return folio_size(folio) == PMD_SIZE && hugetlb_pmd_shared(ptep);
+}
+#else /* !CONFIG_HUGETLB_PAGE */
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+		pte_t *ptep)
+{
+	return true;
+}
+#endif /* CONFIG_HUGETLB_PAGE */
+
 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
 
 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
mm/mempolicy.c renamed from mm/mempolicy.c~mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios +1 -1
@@ -833,7 +833,7 @@ static int queue_folios_hugetlb(pte_t *p
 	 * cannot easily detect if a folio is shared.
 	 */
 	if ((flags & MPOL_MF_MOVE_ALL) ||
-	    (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte)))
+	    !hugetlb_folio_maybe_mapped_shared(folio, pte))
 		if (!folio_isolate_hugetlb(folio, qp->pagelist))
 			qp->nr_failed++;
 unlock:

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 27/35
#104 lore raw

Re: [PATCH v2 27/35] mm/hugetlb: only check PMD table sharing for PMD-sized folios

In reply to David Hildenbrand (Arm)
Quoted 16 lines · David Hildenbrand (Arm)

hugetlb_pmd_shared() reads "pt_share_count" from the ptdesc of the page table that maps a hugetlb folio, independent of the folio size, to tell whether that page table is shared. Only PMD tables have a valid "pt_share_count"; for other folio sizes, such as 1 GiB folios on x86 that are mapped by a PUD table, we read whatever another user of the union left behind.

As a result, such folios might be reported as shared in smaps and as not exclusively mapped in pagemap, and mbind(MPOL_MF_MOVE) and migrate_pages() might skip them.

Let's fix it by adding hugetlb_folio_maybe_mapped_shared(), which combines folio_maybe_mapped_shared(), documented to not care about page table sharing, with a check for PMD table sharing only for PMD-sized folios, and using it in the affected callers.

Quoted 7 lines

This has low urgency.

Fixes ca1a47cd3f5f ("mm/hugetlb: fix hugetlb_pmd_shared()")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#105 lore raw

PATCH v2 28/35 mm/migrate: check hugetlb PMD sharing in move_pages()

In reply to David Hildenbrand (Arm)

Without MPOL_MF_MOVE_ALL, which requires CAP_SYS_NICE, move_pages(2) refuses to migrate folios that might be mapped by other processes.

However, folio_maybe_mapped_shared() does not check for page table sharing. So move_pages(2) with MPOL_MF_MOVE can migrate hugetlb folios that other processes map.

mbind(2) and migrate_pages(2) already handle PMD table sharing since commit 73bdf65ea748 ("migrate: hugetlb: check for hugetlb shared PMD in node migration"), but we missed move_pages(2).

Let's fix it by using hugetlb_folio_maybe_mapped_shared() for hugetlb folios in move_pages(2). Such folios are now reported with -EACCES in the status array.

Rework the code to have separate checks for hugetlb vs. !hugetlb.

This has low urgency.

Fixes e632a938d914 ("mm: migrate: add hugepage migration code to move_pages()")
---
 mm/migrate.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
mm/migrate.c +15 -8
index 0430197fdc04b..194d74152e8e3 100644
@@ -2279,8 +2279,9 @@ static int do_move_pages_to_node(struct list_head *pagelist, int node)
 	return err;
 }
 
-static int __add_folio_for_migration(struct folio *folio, int node,
-		struct list_head *pagelist, bool migrate_all)
+static int __add_folio_for_migration(struct folio *folio,
+		struct folio_walk *fw, int node, struct list_head *pagelist,
+		bool migrate_all)
 {
 	if (is_zero_folio(folio) || is_huge_zero_folio(folio))
 		return -EFAULT;
@@ -2291,13 +2292,19 @@ static int __add_folio_for_migration(struct folio *folio, int node,
 	if (folio_nid(folio) == node)
 		return 0;
 
-	if (folio_maybe_mapped_shared(folio) && !migrate_all)
-		return -EACCES;
-
 	if (folio_test_hugetlb(folio)) {
+		/* fw->ptep aliases fw->pmdp and fw->pudp. */
+		if (!migrate_all && hugetlb_folio_maybe_mapped_shared(folio, fw->ptep))
+			return -EACCES;
 		if (folio_isolate_hugetlb(folio, pagelist))
 			return 1;
-	} else if (folio_isolate_lru(folio)) {
+		return -EBUSY;
+	}
+
+	if (!migrate_all && folio_maybe_mapped_shared(folio))
+		return -EACCES;
+
+	if (folio_isolate_lru(folio)) {
 		list_add_tail(&folio->lru, pagelist);
 		node_stat_mod_folio(folio,
 			NR_ISOLATED_ANON + folio_is_file_lru(folio),
@@ -2332,8 +2339,8 @@ static int add_folio_for_migration(struct mm_struct *mm, const void __user *p,
 	if (vma && vma_migratable(vma)) {
 		folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE);
 		if (folio) {
-			err = __add_folio_for_migration(folio, node, pagelist,
-							migrate_all);
+			err = __add_folio_for_migration(folio, &fw, node,
+							pagelist, migrate_all);
 			folio_walk_end(&fw, vma);
 		} else {
 			err = -ENOENT;
Signature · 1 line
2.56.1
maintainer tree notice
#106 lore raw

+ mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch added to mm-unstable branch

The patch titled Subject: mm/migrate: check hugetlb PMD sharing in move_pages() has been added to the -mm mm-unstable branch. Its filename is mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch

The patch titled
Subject: mm/migrate: check hugetlb PMD sharing in move_pages()
has been added to the -mm mm-unstable branch. Its filename is
mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.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)" <[email protected]>
Subject: mm/migrate: check hugetlb PMD sharing in move_pages()
Date: Thu, 03 Dec 2026 12:37:51 +0100

Without MPOL_MF_MOVE_ALL, which requires CAP_SYS_NICE, move_pages(2) refuses to migrate folios that might be mapped by other processes.

However, folio_maybe_mapped_shared() does not check for page table sharing. So move_pages(2) with MPOL_MF_MOVE can migrate hugetlb folios that other processes map.

mbind(2) and migrate_pages(2) already handle PMD table sharing since commit 73bdf65ea748 ("migrate: hugetlb: check for hugetlb shared PMD in node migration"), but we missed move_pages(2).

Let's fix it by using hugetlb_folio_maybe_mapped_shared() for hugetlb folios in move_pages(2). Such folios are now reported with -EACCES in the status array.

Rework the code to have separate checks for hugetlb vs. !hugetlb.

This has low urgency.

Fixes e632a938d914 ("mm: migrate: add hugepage migration code to move_pages()")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/migrate.c |   23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
mm/migrate.c renamed from mm/migrate.c~mm-migrate-check-hugetlb-pmd-sharing-in-move_pages +15 -8
@@ -2340,8 +2340,9 @@ static int do_move_pages_to_node(struct
 	return err;
 }
 
-static int __add_folio_for_migration(struct folio *folio, int node,
-		struct list_head *pagelist, bool migrate_all)
+static int __add_folio_for_migration(struct folio *folio,
+		struct folio_walk *fw, int node, struct list_head *pagelist,
+		bool migrate_all)
 {
 	if (is_zero_folio(folio) || is_huge_zero_folio(folio))
 		return -EFAULT;
@@ -2352,13 +2353,19 @@ static int __add_folio_for_migration(str
 	if (folio_nid(folio) == node)
 		return 0;
 
-	if (folio_maybe_mapped_shared(folio) && !migrate_all)
-		return -EACCES;
-
 	if (folio_test_hugetlb(folio)) {
+		/* fw->ptep aliases fw->pmdp and fw->pudp. */
+		if (!migrate_all && hugetlb_folio_maybe_mapped_shared(folio, fw->ptep))
+			return -EACCES;
 		if (folio_isolate_hugetlb(folio, pagelist))
 			return 1;
-	} else if (folio_isolate_lru(folio)) {
+		return -EBUSY;
+	}
+
+	if (!migrate_all && folio_maybe_mapped_shared(folio))
+		return -EACCES;
+
+	if (folio_isolate_lru(folio)) {
 		list_add_tail(&folio->lru, pagelist);
 		node_stat_mod_folio(folio,
 			NR_ISOLATED_ANON + folio_is_file_lru(folio),
@@ -2393,8 +2400,8 @@ static int add_folio_for_migration(struc
 	if (vma && vma_migratable(vma)) {
 		folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE);
 		if (folio) {
-			err = __add_folio_for_migration(folio, node, pagelist,
-							migrate_all);
+			err = __add_folio_for_migration(folio, &fw, node,
+							pagelist, migrate_all);
 			folio_walk_end(&fw, vma);
 		} else {
 			err = -ENOENT;

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 28/35
#107 lore raw

Re: [PATCH v2 28/35] mm/migrate: check hugetlb PMD sharing in move_pages()

In reply to David Hildenbrand (Arm)
Quoted 15 lines · David Hildenbrand (Arm)

Without MPOL_MF_MOVE_ALL, which requires CAP_SYS_NICE, move_pages(2) refuses to migrate folios that might be mapped by other processes.

However, folio_maybe_mapped_shared() does not check for page table sharing. So move_pages(2) with MPOL_MF_MOVE can migrate hugetlb folios that other processes map.

mbind(2) and migrate_pages(2) already handle PMD table sharing since commit 73bdf65ea748 ("migrate: hugetlb: check for hugetlb shared PMD in node migration"), but we missed move_pages(2).

Let's fix it by using hugetlb_folio_maybe_mapped_shared() for hugetlb folios in move_pages(2). Such folios are now reported with -EACCES in the status array.

Quoted 8 lines

Rework the code to have separate checks for hugetlb vs. !hugetlb.

This has low urgency.

Fixes e632a938d914 ("mm: migrate: add hugepage migration code to move_pages()")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#108 lore raw

PATCH v2 29/35 mm/hugetlb: don't share page tables of a VMA during mprotect()

In reply to David Hildenbrand (Arm)

When mprotect() of a shared hugetlb mapping races with a hugetlb fault in another process that can share the same PMD table, we can end up with page table entries that don't match the VMA flags, such as writable ones in a PROT_READ mapping or PROT_NONE ones in accessible VMAs.

huge_pmd_share() takes i_mmap_rwsem and shares a PMD table if the flags of the two VMAs match, so while that lock is held the entries in a table have to match the flags of its VMA.

However, mprotect_fixup() stores the new flags without i_mmap_rwsem, and only hugetlb_change_protection() takes it. A fault in between shares a table that still has the old entries, and the walk then unshares the table instead of changing them:

    process A                        process C
    VMA is RW, entries are RW        VMA is R, PUD is empty
    =========================        ======================

mprotect_fixup()
VMA flags = R
huge_pmd_share()
i_mmap_lock_read() flags match, take A's table i_mmap_unlock_read()
hugetlb_change_protection()
i_mmap_lock_write()
huge_pmd_unshare()
clear A's PUD, return 1
skip the entries
VMA is R, entries are RW

mprotect() changes both the flags and the entries while holding the mmap lock in write mode.

Let's fix it by only sharing the page tables of another VMA if we can try-read-lock the mmap lock of its mm. If the trylock fails, we simply try the next VMA, or allocate our own page table.

A trylock is fine, as we hold i_mmap_rwsem, which is usually taken under the mmap lock.

This has medium urgency, as it can result in endless page fault loops if mprotect() races with PMD table sharing and we end up with PROT_NONE entries in accessible VMAs. If the kernel accesses such memory, for example in copy_to_user(), the loop happens in the kernel and the task cannot be killed.

Fixes 39dde65c9940 ("[PATCH] shared page table for hugetlb page")
---
 mm/hugetlb.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
mm/hugetlb.c +11 -3
index a23d36e7b8e93..e529994ee9f73 100644
@@ -7303,15 +7303,23 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
 		if (svma == vma)
 			continue;
 
+		/*
+		 * Stabilize the svma flags, as mprotect() changes flags
+		 * before adjusting page tables.
+		 */
+		if (!mmap_read_trylock(svma->vm_mm))
+			continue;
+
 		saddr = page_table_shareable(svma, vma, addr, idx);
 		if (saddr) {
 			spte = hugetlb_walk(svma, saddr,
 					    vma_mmu_pagesize(svma));
-			if (spte) {
+			if (spte)
 				ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
-				break;
-			}
 		}
+		mmap_read_unlock(svma->vm_mm);
+		if (spte)
+			break;
 	}
 
 	if (!spte)
Signature · 1 line
2.56.1
#109 lore raw

Re: [PATCH v2 29/35] mm/hugetlb: don't share page tables of a VMA during mprotect()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/hugetlb: unconditional mmap_read_trylock() per VMA in huge_pmd_share() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/hugetlb: unconditional mmap_read_trylock() per VMA in huge_pmd_share()
--

commit 7cc2082640f3ff384fba3f898a1612b0c39ddaf2
Author: David Hildenbrand (Arm) <[email protected]>

mm/hugetlb: don't share page tables of a VMA during mprotect()

This commit takes mmap_read_trylock() on each candidate VMA's mm in huge_pmd_share() so PMD page tables are not shared while mprotect() is updating that VMA's flags and page tables.

Quoted 19 lines
mm/hugetlb.c +7 -0
index a23d36e7b8e93..e529994ee9f73 100644
@@ -7303,15 +7303,23 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
 		if (svma == vma)
 			continue;
 
+		/*
+		 * Stabilize the svma flags, as mprotect() changes flags
+		 * before adjusting page tables.
+		 */
+		if (!mmap_read_trylock(svma->vm_mm))
+			continue;
+
 		saddr = page_table_shareable(svma, vma, addr, idx);
 		if (saddr) {
 			spte = hugetlb_walk(svma, saddr,
 					    vma_mmu_pagesize(svma));

mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, · line 7323

Quoted 10 lines
mm/hugetlb.c +4 -3
-			if (spte) {
+			if (spte)
 				ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
-				break;
-			}
 		}
+		mmap_read_unlock(svma->vm_mm);
+		if (spte)
+			break;
 	}

[Severity: Medium]
Does calling mmap_read_trylock(svma->vm_mm) before page_table_shareable() and hugetlb_walk() cause avoidable cacheline bouncing on peer mms?

Each trylock/unlock pair dirties two cachelines in svma->vm_mm (mmap_lock count and owner) and contends with unrelated mmap operations in that process.

Because mapping->i_mmap holds all VMAs on the inode, such as unpopulated VMAs after fork(), concurrent faults in multi-process workloads bounce mmap_lock across CPUs for every skipped VMA.

Could this pre-filter with page_table_shareable() (using READ_ONCE() on svma->vm_flags) and hugetlb_walk() under i_mmap_lock_read(), then take mmap_read_trylock(svma->vm_mm) and re-check svma->vm_flags only once a populated shareable candidate is found?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=29
author maintainer
#110 lore raw

Re: [PATCH v2 29/35] mm/hugetlb: don't share page tables of a VMA during mprotect()

Quoted 4 lines · On 12/3/26 13:29, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/hugetlb: unconditional mmap_read_trylock() per VMA in huge_pmd_share()

Shocking news; almost as if this is what this fix intends to do. lol.

Signature · 3 lines
Cheers,

David
maintainer tree notice
#111 lore raw

+ mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: mm/hugetlb: don't share page tables of a VMA during mprotect() has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: don't share page tables of a VMA during mprotect()
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.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)" <[email protected]>
Subject: mm/hugetlb: don't share page tables of a VMA during mprotect()
Date: Thu, 03 Dec 2026 12:37:52 +0100

When mprotect() of a shared hugetlb mapping races with a hugetlb fault in another process that can share the same PMD table, we can end up with page table entries that don't match the VMA flags, such as writable ones in a PROT_READ mapping or PROT_NONE ones in accessible VMAs.

huge_pmd_share() takes i_mmap_rwsem and shares a PMD table if the flags of the two VMAs match, so while that lock is held the entries in a table have to match the flags of its VMA.

However, mprotect_fixup() stores the new flags without i_mmap_rwsem, and only hugetlb_change_protection() takes it. A fault in between shares a table that still has the old entries, and the walk then unshares the table instead of changing them:

    process A                        process C
    VMA is RW, entries are RW        VMA is R, PUD is empty
    =========================        ======================

mprotect_fixup()
VMA flags = R
huge_pmd_share()
i_mmap_lock_read() flags match, take A's table i_mmap_unlock_read()
hugetlb_change_protection()
i_mmap_lock_write()
huge_pmd_unshare()
clear A's PUD, return 1
skip the entries
VMA is R, entries are RW

mprotect() changes both the flags and the entries while holding the mmap lock in write mode.

Let's fix it by only sharing the page tables of another VMA if we can try-read-lock the mmap lock of its mm. If the trylock fails, we simply try the next VMA, or allocate our own page table.

A trylock is fine, as we hold i_mmap_rwsem, which is usually taken under the mmap lock.

This has medium urgency, as it can result in endless page fault loops if mprotect() races with PMD table sharing and we end up with PROT_NONE entries in accessible VMAs. If the kernel accesses such memory, for example in copy_to_user(), the loop happens in the kernel and the task cannot be killed.

Fixes 39dde65c9940 ("[PATCH] shared page table for hugetlb page")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 mm/hugetlb.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect +11 -3
@@ -7278,15 +7278,23 @@ pte_t *huge_pmd_share(struct mm_struct *
 		if (svma == vma)
 			continue;
 
+		/*
+		 * Stabilize the svma flags, as mprotect() changes flags
+		 * before adjusting page tables.
+		 */
+		if (!mmap_read_trylock(svma->vm_mm))
+			continue;
+
 		saddr = page_table_shareable(svma, vma, addr, idx);
 		if (saddr) {
 			spte = hugetlb_walk(svma, saddr,
 					    vma_mmu_pagesize(svma));
-			if (spte) {
+			if (spte)
 				ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
-				break;
-			}
 		}
+		mmap_read_unlock(svma->vm_mm);
+		if (spte)
+			break;
 	}
 
 	if (!spte)

_

Patches currently in -mm which might be from [email protected] 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

Reviewed-by on v2 29/35
#112 lore raw

Re: [PATCH v2 29/35] mm/hugetlb: don't share page tables of a VMA during mprotect()

In reply to David Hildenbrand (Arm)
Quoted 46 lines · David Hildenbrand (Arm)

When mprotect() of a shared hugetlb mapping races with a hugetlb fault in another process that can share the same PMD table, we can end up with page table entries that don't match the VMA flags, such as writable ones in a PROT_READ mapping or PROT_NONE ones in accessible VMAs.

huge_pmd_share() takes i_mmap_rwsem and shares a PMD table if the flags of the two VMAs match, so while that lock is held the entries in a table have to match the flags of its VMA.

However, mprotect_fixup() stores the new flags without i_mmap_rwsem, and only hugetlb_change_protection() takes it. A fault in between shares a table that still has the old entries, and the walk then unshares the table instead of changing them:

    process A                        process C
    VMA is RW, entries are RW        VMA is R, PUD is empty
    =========================        ======================

mprotect_fixup()
VMA flags = R
huge_pmd_share()
i_mmap_lock_read() flags match, take A's table i_mmap_unlock_read()
hugetlb_change_protection()
i_mmap_lock_write()
huge_pmd_unshare()
clear A's PUD, return 1
skip the entries
VMA is R, entries are RW

mprotect() changes both the flags and the entries while holding the mmap lock in write mode.

Let's fix it by only sharing the page tables of another VMA if we can try-read-lock the mmap lock of its mm. If the trylock fails, we simply try the next VMA, or allocate our own page table.

A trylock is fine, as we hold i_mmap_rwsem, which is usually taken under the mmap lock.

This has medium urgency, as it can result in endless page fault loops if mprotect() races with PMD table sharing and we end up with PROT_NONE entries in accessible VMAs. If the kernel accesses such memory, for example in copy_to_user(), the loop happens in the kernel and the task cannot be killed.

Quoted 4 lines
Fixes 39dde65c9940 ("[PATCH] shared page table for hugetlb page")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#113 lore raw

PATCH v2 30/35 mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails

In reply to David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

If we split a VMA that has shared PMD page tables mapped, and allocating the hugetlb VMA lock for the new VMA fails, a fault in the new VMA can race with a hole punch and access page tables that were already freed.

__split_vma() calls hugetlb_vm_op_open() for the new VMA, which allocates its hugetlb VMA lock in hugetlb_vma_lock_alloc(). If that allocation fails, the new VMA is left without a lock.

A VMA that has shared PMD page tables needs the hugetlb VMA lock, since the lock synchronizes huge_pmd_unshare() with concurrent page table walks, including faults.

want_pmd_share() and page_table_shareable() prevent new sharing without the lock, but a split leaves previously shared page tables in place for the new VMA, and hugetlb_vm_op_open() can't currently return an error if the allocation fails.

This can, for example, happen for an OOM victim. Then, nothing prevents a hole punch from unsharing the page table of the new VMA while a fault in the new VMA is still using it, and the last other sharer from freeing it:

fault in the new VMA hole punch, then munmap()
==================== =========================
hugetlb_vma_lock_read()
no lock, does nothing
huge_pte_alloc()
pointer into shared table
folio_zero_user()
hugetlb_vma_trylock_write()
no lock, returns 1
huge_pmd_unshare(new VMA)
pud_clear()
last sharer frees the table
huge_pte_lock()
set_huge_pte_at()

Let's fix it by unsharing the PMD page tables of a new VMA that has no lock. Also, exit hugetlb_split() early if the original VMA has no lock, as it cannot have shared PMD page tables then.

This has low urgency, as it requires allocating the hugetlb VMA lock to fail during a split, which is practically only possible for OOM victims, and a race with a hole punch.

Fixes 131a79b474e9 ("hugetlb: fix vma lock handling during split vma and range unmapping")

[ david: return early if the original VMA has no lock, adjust comment
and description ]

---
 include/linux/hugetlb.h           |  6 ++++--
 mm/hugetlb.c                      | 17 ++++++++++++++++-
 mm/vma.c                          |  2 +-
 tools/testing/vma/include/stubs.h |  3 ++-
 4 files changed, 23 insertions(+), 5 deletions(-)
include/linux/hugetlb.h +4 -2
index 49dad5ae57512..aea18895e1459 100644
@@ -274,7 +274,8 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
 		unsigned long cp_flags);
 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
 void fixup_hugetlb_reservations(struct vm_area_struct *vma);
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+		unsigned long addr);
 
 unsigned int arch_hugetlb_cma_order(void);
 
@@ -457,7 +458,8 @@ static inline void fixup_hugetlb_reservations(struct vm_area_struct *vma)
 {
 }
 
-static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
+static inline void hugetlb_split(struct vm_area_struct *vma,
+		struct vm_area_struct *new, unsigned long addr) {}
 
 #endif /* !CONFIG_HUGETLB_PAGE */
 
mm/hugetlb.c +16 -1
index e529994ee9f73..1c7259d80615e 100644
@@ -4909,7 +4909,8 @@ static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
 	return 0;
 }
 
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+		unsigned long addr)
 {
 	/*
 	 * PMD sharing is only possible for PUD_SIZE-aligned address ranges
@@ -4920,8 +4921,13 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
 	 * walks (except hardware and gup_fast()).
 	 */
 	vma_assert_write_locked(vma);
+	vma_assert_write_locked(new);
 	i_mmap_assert_write_locked(vma->vm_file->f_mapping);
 
+	/* Shared PMDs require a hugetlb VMA lock. */
+	if (!__vma_shareable_lock(vma))
+		return;
+
 	if (addr & ~PUD_MASK) {
 		unsigned long floor = addr & PUD_MASK;
 		unsigned long ceil = floor + PUD_SIZE;
@@ -4940,6 +4946,15 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
 			hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
 		}
 	}
+
+	/*
+	 * If we failed to allocate the hugetlb VMA lock, unshare everything
+	 * that the new VMA covers.
+	 */
+	if (!__vma_shareable_lock(new))
+		hugetlb_unshare_pmds(new, ALIGN(new->vm_start, PUD_SIZE),
+				     ALIGN_DOWN(new->vm_end, PUD_SIZE),
+				     /* take_locks = */ false);
 }
 
 static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
mm/vma.c +1 -1
index c82e9306b11ac..183bfb8ba4703 100644
@@ -590,7 +590,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	 */
 	vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL);
 	if (vma_is_hugetlb(vma))
-		hugetlb_split(vma, addr);
+		hugetlb_split(vma, new, addr);
 
 	if (new_below) {
 		vma->vm_start = addr;
tools/testing/vma/include/stubs.h +2 -1
index 9f5b8e996abf6..6ddecf587d9be 100644
@@ -466,7 +466,8 @@ static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
 {
 }
 
-static inline void hugetlb_split(struct vm_area_struct *, unsigned long) {}
+static inline void hugetlb_split(struct vm_area_struct *,
+		struct vm_area_struct *, unsigned long) {}
 
 static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
 {
Signature · 1 line
2.56.1
#114 lore raw

Re: [PATCH v2 30/35] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] mm/hugetlb: hugetlb_split() sleeps in atomic context on ARM/PA-RISC - [Medium] mm/hugetlb: ALIGN()…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] mm/hugetlb: hugetlb_split() sleeps in atomic context on ARM/PA-RISC
- [Medium] mm/hugetlb: ALIGN() overflow in hugetlb_split() wraps start to 0 on s390
--

commit 75f9184dc32acb09010de15401fba05ecb4fa173
Author: Chris Mason <[email protected]>

mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails

This commit updates hugetlb_split() to take the newly split VMA, return early when the original VMA has no shareable hugetlb VMA lock, and unshare PMDs across the new VMA if allocating its hugetlb VMA lock failed.

Quoted 4 lines
mm/hugetlb.c +0 -0
index e529994ee9f73..1c7259d80615e 100644

[ ... ]

mm/hugetlb.c void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) · line 4929

Quoted 10 lines
mm/hugetlb.c +4 -0
@@ -4920,8 +4921,13 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
 	 * walks (except hardware and gup_fast()).
 	 */
 	vma_assert_write_locked(vma);
+	vma_assert_write_locked(new);
 	i_mmap_assert_write_locked(vma->vm_file->f_mapping);
 
+	/* Shared PMDs require a hugetlb VMA lock. */
+	if (!__vma_shareable_lock(vma))
+		return;

[Severity: High]
Can hugetlb_split() sleep in atomic context on architectures without PMD sharing, such as ARM32 LPAE and PA-RISC?

In __split_vma(), vma_prepare() holds flush_dcache_mmap_lock(vp->mapping) across hugetlb_split(). On ARM and PA-RISC this expands to xa_lock_irq(&mapping->i_pages), holding a spinlock with IRQs disabled.

Neither architecture enables CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING, yet __vma_shareable_lock(vma) is still true for VM_MAYSHARE VMAs. Both the existing unaligned-split path and the new !__vma_shareable_lock(new) path then reach hugetlb_unshare_pmds() with xa_lock_irq held.

hugetlb_unshare_pmds() calls mmu_notifier_invalidate_range_start() (which calls might_sleep() and sleepable notifiers), hits WARN_ON(arch_irqs_disabled()) in flush_cache_range() on SMP PA-RISC, and takes huge_pte_lock() inside i_pages.xa_lock.

Returning early when !IS_ENABLED(CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING) would avoid this.

[ ... ]

Quoted 4 lines
mm/hugetlb.c +0 -0
@@ -4940,6 +4946,15 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
 			hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
 		}
 	}

mm/hugetlb.c void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) · line 4958

Quoted 10 lines
mm/hugetlb.c +9 -0
+
+	/*
+	 * If we failed to allocate the hugetlb VMA lock, unshare everything
+	 * that the new VMA covers.
+	 */
+	if (!__vma_shareable_lock(new))
+		hugetlb_unshare_pmds(new, ALIGN(new->vm_start, PUD_SIZE),
+				     ALIGN_DOWN(new->vm_end, PUD_SIZE),
+				     /* take_locks = */ false);
 }

[Severity: Medium]
Can ALIGN(new->vm_start, PUD_SIZE) overflow to 0 on 64-bit s390?

On 64-bit s390, TASK_SIZE_MAX is 0xfffffffffffff000, PUD_SIZE is 2 GiB, and HPAGE_SIZE is 1 MiB. Splitting a shared 1 MiB hugetlb VMA in the top 2 GiB when hugetlb_vma_lock_alloc(new) fails wraps ALIGN(new->vm_start, PUD_SIZE) to 0 while ALIGN_DOWN(new->vm_end, PUD_SIZE) evaluates to 0xffffffff80000000.

hugetlb_unshare_pmds() then sees start < end, invalidates MMU notifiers over [0, 0xffffffff80000000), and loops 2^33 - 1 times without cond_resched() under mmap_write_lock and i_mmap_lock_write, causing a soft lockup and RCU stall.

If the process also has an unrelated 2 GiB EDAT2 hugetlb mapping in that range, s390 huge_pte_offset() lacks a pud_leaf() check for PMD_SIZE, so huge_pte_lock() misinterprets the data folio's folio->private as ptdesc->ptl and can crash or corrupt folio->private.

Guarding hugetlb_split() on CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING and checking for ALIGN() wraparound (start < new->vm_start) would prevent this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=30
maintainer tree notice
#115 lore raw

+ mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch added to mm-unstable branch

In reply to David Hildenbrand (Arm)
The patch titled Subject: mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails has been added to the -mm mm-unstable branch. Its filename is…

The patch titled
Subject: mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-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-unshare-pmds-of-a-split-vma-if-allocating-its-lock-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: Chris Mason <[email protected]>
Subject: mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails
Date: Thu, 03 Dec 2026 12:37:53 +0100

If we split a VMA that has shared PMD page tables mapped, and allocating the hugetlb VMA lock for the new VMA fails, a fault in the new VMA can race with a hole punch and access page tables that were already freed.

__split_vma() calls hugetlb_vm_op_open() for the new VMA, which allocates its hugetlb VMA lock in hugetlb_vma_lock_alloc(). If that allocation fails, the new VMA is left without a lock.

A VMA that has shared PMD page tables needs the hugetlb VMA lock, since the lock synchronizes huge_pmd_unshare() with concurrent page table walks, including faults.

want_pmd_share() and page_table_shareable() prevent new sharing without the lock, but a split leaves previously shared page tables in place for the new VMA, and hugetlb_vm_op_open() can't currently return an error if the allocation fails.

This can, for example, happen for an OOM victim. Then, nothing prevents a hole punch from unsharing the page table of the new VMA while a fault in the new VMA is still using it, and the last other sharer from freeing it:

fault in the new VMA hole punch, then munmap()
==================== =========================
hugetlb_vma_lock_read()
no lock, does nothing
huge_pte_alloc()
pointer into shared table
folio_zero_user()
hugetlb_vma_trylock_write()
no lock, returns 1
huge_pmd_unshare(new VMA)
pud_clear()
last sharer frees the table
huge_pte_lock()
set_huge_pte_at()

Let's fix it by unsharing the PMD page tables of a new VMA that has no lock. Also, exit hugetlb_split() early if the original VMA has no lock, as it cannot have shared PMD page tables then.

This has low urgency, as it requires allocating the hugetlb VMA lock to fail during a split, which is practically only possible for OOM victims, and a race with a hole punch.

[[email protected]: return early if the original VMA has no lock, adjust comment and description]
Fixes 131a79b474e9 ("hugetlb: fix vma lock handling during split vma and range unmapping")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 include/linux/hugetlb.h           |    6 ++++--
 mm/hugetlb.c                      |   17 ++++++++++++++++-
 mm/vma.c                          |    2 +-
 tools/testing/vma/include/stubs.h |    3 ++-
 4 files changed, 23 insertions(+), 5 deletions(-)
include/linux/hugetlb.h renamed from include/linux/hugetlb.h~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +4 -2
@@ -274,7 +274,8 @@ long hugetlb_change_protection(struct vm
 		unsigned long cp_flags);
 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
 void fixup_hugetlb_reservations(struct vm_area_struct *vma);
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+		unsigned long addr);
 
 unsigned int arch_hugetlb_cma_order(void);
 
@@ -457,7 +458,8 @@ static inline void fixup_hugetlb_reserva
 {
 }
 
-static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
+static inline void hugetlb_split(struct vm_area_struct *vma,
+		struct vm_area_struct *new, unsigned long addr) {}
 
 #endif /* !CONFIG_HUGETLB_PAGE */
 
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +16 -1
@@ -4909,7 +4909,8 @@ static int hugetlb_vm_op_split(struct vm
 	return 0;
 }
 
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+		unsigned long addr)
 {
 	/*
 	 * PMD sharing is only possible for PUD_SIZE-aligned address ranges
@@ -4920,8 +4921,13 @@ void hugetlb_split(struct vm_area_struct
 	 * walks (except hardware and gup_fast()).
 	 */
 	vma_assert_write_locked(vma);
+	vma_assert_write_locked(new);
 	i_mmap_assert_write_locked(vma->vm_file->f_mapping);
 
+	/* Shared PMDs require a hugetlb VMA lock. */
+	if (!__vma_shareable_lock(vma))
+		return;
+
 	if (addr & ~PUD_MASK) {
 		unsigned long floor = addr & PUD_MASK;
 		unsigned long ceil = floor + PUD_SIZE;
@@ -4940,6 +4946,15 @@ void hugetlb_split(struct vm_area_struct
 			hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
 		}
 	}
+
+	/*
+	 * If we failed to allocate the hugetlb VMA lock, unshare everything
+	 * that the new VMA covers.
+	 */
+	if (!__vma_shareable_lock(new))
+		hugetlb_unshare_pmds(new, ALIGN(new->vm_start, PUD_SIZE),
+				     ALIGN_DOWN(new->vm_end, PUD_SIZE),
+				     /* take_locks = */ false);
 }
 
 static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
mm/vma.c renamed from mm/vma.c~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +1 -1
@@ -590,7 +590,7 @@ __split_vma(struct vma_iterator *vmi, st
 	 */
 	vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL);
 	if (vma_is_hugetlb(vma))
-		hugetlb_split(vma, addr);
+		hugetlb_split(vma, new, addr);
 
 	if (new_below) {
 		vma->vm_start = addr;
tools/testing/vma/include/stubs.h renamed from tools/testing/vma/include/stubs.h~mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails +2 -1
@@ -466,7 +466,8 @@ static inline void vma_adjust_trans_huge
 {
 }
 
-static inline void hugetlb_split(struct vm_area_struct *, unsigned long) {}
+static inline void hugetlb_split(struct vm_area_struct *,
+		struct vm_area_struct *, unsigned long) {}
 
 static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
 {

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch

Reviewed-by on v2 30/35
#116 lore raw

Re: [PATCH v2 30/35] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails

In reply to David Hildenbrand (Arm)
Quoted 47 lines · David Hildenbrand (Arm)

From: Chris Mason <[email protected]>

If we split a VMA that has shared PMD page tables mapped, and allocating the hugetlb VMA lock for the new VMA fails, a fault in the new VMA can race with a hole punch and access page tables that were already freed.

__split_vma() calls hugetlb_vm_op_open() for the new VMA, which allocates its hugetlb VMA lock in hugetlb_vma_lock_alloc(). If that allocation fails, the new VMA is left without a lock.

A VMA that has shared PMD page tables needs the hugetlb VMA lock, since the lock synchronizes huge_pmd_unshare() with concurrent page table walks, including faults.

want_pmd_share() and page_table_shareable() prevent new sharing without the lock, but a split leaves previously shared page tables in place for the new VMA, and hugetlb_vm_op_open() can't currently return an error if the allocation fails.

This can, for example, happen for an OOM victim. Then, nothing prevents a hole punch from unsharing the page table of the new VMA while a fault in the new VMA is still using it, and the last other sharer from freeing it:

fault in the new VMA hole punch, then munmap()
==================== =========================
hugetlb_vma_lock_read()
no lock, does nothing
huge_pte_alloc()
pointer into shared table
folio_zero_user()
hugetlb_vma_trylock_write()
no lock, returns 1
huge_pmd_unshare(new VMA)
pud_clear()
last sharer frees the table
huge_pte_lock()
set_huge_pte_at()

Let's fix it by unsharing the PMD page tables of a new VMA that has no lock. Also, exit hugetlb_split() early if the original VMA has no lock, as it cannot have shared PMD page tables then.

This has low urgency, as it requires allocating the hugetlb VMA lock to fail during a split, which is practically only possible for OOM victims, and a race with a hole punch.

Quoted 6 lines
Fixes 131a79b474e9 ("hugetlb: fix vma lock handling during split vma and range unmapping")

[ david: return early if the original VMA has no lock, adjust comment
and description ]

with sashiko's arm/parisc atomic context point addressed feel free to add:

Signature · 2 lines
cheers,
jose a. p-a
author maintainer
#117 lore raw

PATCH v2 31/35 mm/hugetlb: count migration entries at fork and unmap

In reply to David Hildenbrand (Arm)

HugetlbPages in /proc/<pid>/status can be wrong, and even drop below zero, if we fork() or unmap while mapped hugetlb folios are being migrated.

There is an inconsistency: when installing migration entries we don't adjust the counter, treating them as if they were present. However, when copying or unmapping a migration entry, we don't adjust the counter like we would for a present entry.

Let's fix it by adjusting the counter also for migration entries in these cases. Add pte_is_migration_entry() like we did for other softleaf types already.

This has low urgency and given that this has been around forever and likely nobody cares, let's not CC stable.

Fixes 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status")
---
 include/linux/leafops.h | 11 +++++++++++
 mm/hugetlb.c            |  3 +++
 2 files changed, 14 insertions(+)
include/linux/leafops.h +11 -0
index 4c1476ae32343..c315a3531485e 100644
@@ -587,6 +587,17 @@ static inline bool pte_is_uffd_marker(pte_t pte)
 	return false;
 }
 
+/**
+ * pte_is_migration_entry() - Does this PTE entry encode a migration entry?
+ * @pte: PTE entry.
+ *
+ * Returns: true if the PTE encodes a migration entry, otherwise false.
+ */
+static inline bool pte_is_migration_entry(pte_t pte)
+{
+	return softleaf_is_migration(softleaf_from_pte(pte));
+}
+
 #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_PMD_SOFTLEAVES)
 
 /**
mm/hugetlb.c +3 -0
index 1c7259d80615e..c1fbe66d30332 100644
@@ -5153,6 +5153,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
 			if (!userfaultfd_protected(dst_vma))
 				entry = pte_swp_clear_uffd(entry);
 			set_huge_pte_at(dst, addr, dst_pte, entry, sz);
+			hugetlb_count_add(npages, dst);
 		} else if (unlikely(pte_is_marker(entry))) {
 			const pte_marker marker = copy_pte_marker(softleaf, dst_vma);
 
@@ -5502,6 +5503,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
 				spin_unlock(ptl);
 				continue;
 			}
+			if (pte_is_migration_entry(pte))
+				hugetlb_count_sub(pages_per_huge_page(h), mm);
 			/*
 			 * If the pte was wr-protected by uffd-wp in any of the
 			 * swap forms, meanwhile the caller does not want to
Signature · 1 line
2.56.1
maintainer tree notice
#118 lore raw

+ mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch added to mm-unstable branch

The patch titled Subject: mm/hugetlb: count migration entries at fork and unmap has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch

The patch titled
Subject: mm/hugetlb: count migration entries at fork and unmap
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-count-migration-entries-at-fork-and-unmap.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)" <[email protected]>
Subject: mm/hugetlb: count migration entries at fork and unmap
Date: Thu, 03 Dec 2026 12:37:54 +0100

HugetlbPages in /proc/<pid>/status can be wrong, and even drop below zero, if we fork() or unmap while mapped hugetlb folios are being migrated.

There is an inconsistency: when installing migration entries we don't adjust the counter, treating them as if they were present. However, when copying or unmapping a migration entry, we don't adjust the counter like we would for a present entry.

Let's fix it by adjusting the counter also for migration entries in these cases. Add pte_is_migration_entry() like we did for other softleaf types already.

This has low urgency and given that this has been around forever and likely nobody cares, let's not CC stable.

Fixes 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status")
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Hongfu Li <[email protected]>
Cc Jinmeng Zhou <[email protected]>
Cc Mina Almasry <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Vivek Kasireddy <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Dave Airlie <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Andy Whitcroft <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Barry Song <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Pedro Falcato <[email protected]>
Cc Jann Horn <[email protected]>
Cc Guillaume Morin <[email protected]>
Cc Yang Shi <[email protected]>
Cc James Houghton <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Harry Yoo <[email protected]>
Cc Lance Yang <[email protected]>
Cc Axel Rasmussen <[email protected]>
Cc Peter Xu <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Kiryl Shutsemau <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Hongbo Li <[email protected]>
Cc Gregory Price <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Ying Huang <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Andi Kleen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joern Engel <[email protected]>
Cc Aneesh Kumar K.V <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Jiaqi Yan <[email protected]>
 include/linux/leafops.h |   11 +++++++++++
 mm/hugetlb.c            |    3 +++
 2 files changed, 14 insertions(+)
include/linux/leafops.h renamed from include/linux/leafops.h~mm-hugetlb-count-migration-entries-at-fork-and-unmap +11 -0
@@ -638,6 +638,17 @@ static inline bool pte_is_uffd_marker(pt
 	return false;
 }
 
+/**
+ * pte_is_migration_entry() - Does this PTE entry encode a migration entry?
+ * @pte: PTE entry.
+ *
+ * Returns: true if the PTE encodes a migration entry, otherwise false.
+ */
+static inline bool pte_is_migration_entry(pte_t pte)
+{
+	return softleaf_is_migration(softleaf_from_pte(pte));
+}
+
 #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_PMD_SOFTLEAVES)
 
 /**
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-count-migration-entries-at-fork-and-unmap +3 -0
@@ -5153,6 +5153,7 @@ again:
 			if (!userfaultfd_protected(dst_vma))
 				entry = pte_swp_clear_uffd(entry);
 			set_huge_pte_at(dst, addr, dst_pte, entry, sz);
+			hugetlb_count_add(npages, dst);
 		} else if (unlikely(pte_is_marker(entry))) {
 			const pte_marker marker = copy_pte_marker(softleaf, dst_vma);
 
@@ -5477,6 +5478,8 @@ static void hugetlb_unmap_range(struct m
 				spin_unlock(ptl);
 				continue;
 			}
+			if (pte_is_migration_entry(pte))
+				hugetlb_count_sub(pages_per_huge_page(h), mm);
 			/*
 			 * If the pte was wr-protected by uffd-wp in any of the
 			 * swap forms, meanwhile the caller does not want to

_

Patches currently in -mm which might be from [email protected] 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