Skip to contentSkip to conversation

mm: free unused page table allocated by do_set_pmd()

PATCH Applied David Hildenbrand (Arm) 5 participants 13 messages Nov 6 – Nov 10

Subsystems (5)
lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

v2 cover mm: free unused page table allocated by do_set_pmd()

This isn't super urgent, so it can wait for 7.5.

---
Changes in v2:
- Free the table in do_set_pmd() instead.
- Add "mm: don't allocate a page table under RCU in do_set_pmd()"
- Link to v1: https://patch.msgid.link/[email protected]

---

David Hildenbrand (Arm) (2):
mm: free unused page table allocated by do_set_pmd()
mm: don't allocate a page table under RCU in do_set_pmd()

 include/linux/mm.h |  3 ++-
 mm/filemap.c       |  2 +-
 mm/khugepaged.c    |  2 +-
 mm/memory.c        | 16 +++++++++++++---
 4 files changed, 17 insertions(+), 6 deletions(-)

---

base-commit: f44fd601f72e09e44c667b9234c9cd469856679c

change-id: 20261105-khugepaged_set_pmd-60a02f718e64

--

Cheers,

David

author maintainer
#1 lore raw

PATCH v2 1/2 mm: free unused page table allocated by do_set_pmd()

do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a deposited page table, such as PowerPC with the hash MMU. If the PMD is no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK without consuming that table.

Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent userfaultfd operation can install a PTE table before taking the folio lock. If that races with set_huge_pmd(), the unused preallocated table is lost when its local vm_fault goes out of scope.

Track whether do_set_pmd() allocated the table and free it after dropping the PMD lock if it was not consumed. Preserve any preallocated table supplied by the caller for PTE fallback. Successful PMD installation consumes the table and clears the pointer.

Returning without a preallocated table matches the existing behavior on architectures that do not need a deposited table. All callers already handle that case.

Fixes 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")
---
 mm/memory.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/memory.c +6 -0
index becf53a2a0944..421064aef3cf0 100644
@@ -5687,6 +5687,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	struct vm_area_struct *vma = vmf->vma;
 	bool write = vmf->flags & FAULT_FLAG_WRITE;
 	unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
+	bool pgtable_allocated = false;
 	pmd_t entry;
 	vm_fault_t ret = VM_FAULT_FALLBACK;
 
@@ -5725,6 +5726,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
+		pgtable_allocated = true;
 	}
 
 	vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
@@ -5755,6 +5757,10 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	count_vm_event(THP_FILE_MAPPED);
 out:
 	spin_unlock(vmf->ptl);
+	if (pgtable_allocated && vmf->prealloc_pte) {
+		pte_free(vma->vm_mm, vmf->prealloc_pte);
+		vmf->prealloc_pte = NULL;
+	}
 	return ret;
 }
 #else
Signature · 1 line
2.43.0
reviewer Acked-by on v2 1/2
#2 lore raw
Quoted 19 lines · On Fri, 06 Nov 2026 13:36:12 +0100 "David Hildenbrand (Arm)"…

do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a deposited page table, such as PowerPC with the hash MMU. If the PMD is no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK without consuming that table.

Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent userfaultfd operation can install a PTE table before taking the folio lock. If that races with set_huge_pmd(), the unused preallocated table is lost when its local vm_fault goes out of scope.

Track whether do_set_pmd() allocated the table and free it after dropping the PMD lock if it was not consumed. Preserve any preallocated table supplied by the caller for PTE fallback. Successful PMD installation consumes the table and clears the pointer.

Returning without a preallocated table matches the existing behavior on architectures that do not need a deposited table. All callers already handle that case.

Quoted 7 lines
Fixes 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")
---
 mm/memory.c | 6 ++++++
 1 file changed, 6 insertions(+)

Reminded me of lazy pagetable deposit series :)

Quoted 37 lines
mm/memory.c +6 -0
index becf53a2a0944..421064aef3cf0 100644
@@ -5687,6 +5687,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	struct vm_area_struct *vma = vmf->vma;
 	bool write = vmf->flags & FAULT_FLAG_WRITE;
 	unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
+	bool pgtable_allocated = false;
 	pmd_t entry;
 	vm_fault_t ret = VM_FAULT_FALLBACK;
 
@@ -5725,6 +5726,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
+		pgtable_allocated = true;
 	}
 
 	vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
@@ -5755,6 +5757,10 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	count_vm_event(THP_FILE_MAPPED);
 out:
 	spin_unlock(vmf->ptl);
+	if (pgtable_allocated && vmf->prealloc_pte) {
+		pte_free(vma->vm_mm, vmf->prealloc_pte);
+		vmf->prealloc_pte = NULL;
+	}
 	return ret;
 }
 #else
Signature · 2 lines
-- 
2.43.0
author maintainer
#3 lore raw
Quoted 29 lines · Usama Arif
Quoted 19 lines · On Fri, 06 Nov 2026 13:36:12 +0100 "David Hildenbrand (Arm)"…

do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a deposited page table, such as PowerPC with the hash MMU. If the PMD is no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK without consuming that table.

Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent userfaultfd operation can install a PTE table before taking the folio lock. If that races with set_huge_pmd(), the unused preallocated table is lost when its local vm_fault goes out of scope.

Track whether do_set_pmd() allocated the table and free it after dropping the PMD lock if it was not consumed. Preserve any preallocated table supplied by the caller for PTE fallback. Successful PMD installation consumes the table and clears the pointer.

Returning without a preallocated table matches the existing behavior on architectures that do not need a deposited table. All callers already handle that case.

Quoted 7 lines
Fixes 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")
---
 mm/memory.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 3 lines

Reminded me of lazy pagetable deposit series :)

Not just you. haha :)

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 1/2
#4 lore raw
In reply to David Hildenbrand (Arm)
Quoted 51 lines · David Hildenbrand (Arm)

do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a deposited page table, such as PowerPC with the hash MMU. If the PMD is no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK without consuming that table.

Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent userfaultfd operation can install a PTE table before taking the folio lock. If that races with set_huge_pmd(), the unused preallocated table is lost when its local vm_fault goes out of scope.

Track whether do_set_pmd() allocated the table and free it after dropping the PMD lock if it was not consumed. Preserve any preallocated table supplied by the caller for PTE fallback. Successful PMD installation consumes the table and clears the pointer.

Returning without a preallocated table matches the existing behavior on architectures that do not need a deposited table. All callers already handle that case.

Fixes 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")

---
mm/memory.c | 6 ++++++

1 file changed, 6 insertions(+)
mm/memory.c +2 -0
index becf53a2a0944..421064aef3cf0 100644
@@ -5687,6 +5687,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	struct vm_area_struct *vma = vmf->vma;
 	bool write = vmf->flags & FAULT_FLAG_WRITE;
 	unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
+	bool pgtable_allocated = false;
 	pmd_t entry;
 	vm_fault_t ret = VM_FAULT_FALLBACK;
 
@@ -5725,6 +5726,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
+		pgtable_allocated = true;
 	}
 
 	vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
@@ -5755,6 +5757,10 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	count_vm_event(THP_FILE_MAPPED);

out:

	spin_unlock(vmf->ptl);

mm/memory.c vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa · line 5760

Quoted 1 line
mm/memory.c +1 -0
+	if (pgtable_allocated && vmf->prealloc_pte) {

Took me a sec ... to see why we can't just check vmf->prealloc_pte here
...

Emm ... as it turns out, the caller might have supplied it for fallback :D

LGTM1

Quoted 11 lines
mm/memory.c +3 -0
+		pte_free(vma->vm_mm, vmf->prealloc_pte);
+		vmf->prealloc_pte = NULL;
+	}
	return ret;
}

#else

Signature · 1 line
2.43.0
author maintainer
#5 lore raw
Quoted 58 lines · Lance Yang
Quoted 51 lines · David Hildenbrand (Arm)

do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a deposited page table, such as PowerPC with the hash MMU. If the PMD is no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK without consuming that table.

Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent userfaultfd operation can install a PTE table before taking the folio lock. If that races with set_huge_pmd(), the unused preallocated table is lost when its local vm_fault goes out of scope.

Track whether do_set_pmd() allocated the table and free it after dropping the PMD lock if it was not consumed. Preserve any preallocated table supplied by the caller for PTE fallback. Successful PMD installation consumes the table and clears the pointer.

Returning without a preallocated table matches the existing behavior on architectures that do not need a deposited table. All callers already handle that case.

Fixes 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")

---
mm/memory.c | 6 ++++++

1 file changed, 6 insertions(+)
mm/memory.c +2 -0
index becf53a2a0944..421064aef3cf0 100644
@@ -5687,6 +5687,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	struct vm_area_struct *vma = vmf->vma;
 	bool write = vmf->flags & FAULT_FLAG_WRITE;
 	unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
+	bool pgtable_allocated = false;
 	pmd_t entry;
 	vm_fault_t ret = VM_FAULT_FALLBACK;
 
@@ -5725,6 +5726,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
+		pgtable_allocated = true;
 	}
 
 	vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
@@ -5755,6 +5757,10 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	count_vm_event(THP_FILE_MAPPED);

out:

	spin_unlock(vmf->ptl);

mm/memory.c vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa · line 5760

Quoted 1 line
mm/memory.c +1 -0
+	if (pgtable_allocated && vmf->prealloc_pte) {

Took me a sec ... to see why we can't just check vmf->prealloc_pte here
...

Quoted 2 lines

Emm ... as it turns out, the caller might have supplied it for fallback :D

Yeah, it's all ... complicated :)

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 1/2
#6 lore raw
In reply to David Hildenbrand (Arm)
Quoted 19 lines · David Hildenbrand (Arm)

do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a deposited page table, such as PowerPC with the hash MMU. If the PMD is no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK without consuming that table.

Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent userfaultfd operation can install a PTE table before taking the folio lock. If that races with set_huge_pmd(), the unused preallocated table is lost when its local vm_fault goes out of scope.

Track whether do_set_pmd() allocated the table and free it after dropping the PMD lock if it was not consumed. Preserve any preallocated table supplied by the caller for PTE fallback. Successful PMD installation consumes the table and clears the pointer.

Returning without a preallocated table matches the existing behavior on architectures that do not need a deposited table. All callers already handle that case.

Quoted 5 lines
Fixes 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")

---

LGTM.

author maintainer
#7 lore raw

PATCH v2 2/2 mm: don't allocate a page table under RCU in do_set_pmd()

In reply to David Hildenbrand (Arm)

do_fault_around() skips PTE table preallocation when the PMD already points to a PTE table. A concurrent collapse can then retract that table and clear the PMD before filemap_map_pages() locks the new PMD-sized folio.

filemap_map_pmd() can then call do_set_pmd() with an empty PMD and no preallocated table. On PowerPC hash MMU, do_set_pmd() then tries allocating a PTE table to deposit. But filemap_map_pmd() calls do_set_pmd() under RCU, so allocating from that context is not good.

This race became possible when retract_page_tables() stopped taking the mmap and VMA locks. Previously, those locks excluded retraction while the fault was in progress.

Add a may_alloc argument to do_set_pmd() and pass false from filemap_map_pmd(), so we can fail with VM_FAULT_FALLBACK instead of trying to allocate.

This issue should be fairly hard to hit in practice.

Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")
---
 include/linux/mm.h |  3 ++-
 mm/filemap.c       |  2 +-
 mm/khugepaged.c    |  2 +-
 mm/memory.c        | 10 +++++++---
 4 files changed, 11 insertions(+), 6 deletions(-)
include/linux/mm.h +2 -1
index a2dd76cc5bbfe..bd144fec5f5da 100644
@@ -2156,7 +2156,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
 	return pte;
 }
 
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page);
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
+		bool may_alloc);
 void set_pte_range(struct vm_fault *vmf, struct folio *folio,
 		struct page *page, unsigned int nr, unsigned long addr);
 
mm/filemap.c +1 -1
index 86297841f9955..9871b4f32a916 100644
@@ -3745,7 +3745,7 @@ static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,
 
 	if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) {
 		struct page *page = folio_file_page(folio, start);
-		vm_fault_t ret = do_set_pmd(vmf, folio, page);
+		vm_fault_t ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ false);
 		if (!ret) {
 			/* The page is mapped successfully, reference consumed. */
 			folio_unlock(folio);
mm/khugepaged.c +1 -1
index 8d9f05a39afb3..5836e08abc984 100644
@@ -1826,7 +1826,7 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
 	}
 
 	vmf.pmd = pmdp;
-	if (do_set_pmd(&vmf, folio, page))
+	if (do_set_pmd(&vmf, folio, page, /* may_alloc= */ true))
 		return SCAN_FAIL;
 
 	folio_get(folio);
mm/memory.c +7 -3
index 421064aef3cf0..6eeed43b05a19 100644
@@ -5682,7 +5682,8 @@ static void deposit_prealloc_pte(struct vm_fault *vmf)
 	vmf->prealloc_pte = NULL;
 }
 
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
+		bool may_alloc)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	bool write = vmf->flags & FAULT_FLAG_WRITE;
@@ -5723,6 +5724,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	 * related to pte entry. Use the preallocated table for that.
 	 */
 	if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
+		if (!may_alloc)
+			return VM_FAULT_FALLBACK;
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
@@ -5764,7 +5767,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	return ret;
 }
 #else
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
+		bool may_alloc)
 {
 	return VM_FAULT_FALLBACK;
 }
@@ -5888,7 +5892,7 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 
 	if (pmd_none(*vmf->pmd)) {
 		if (!needs_fallback && folio_test_pmd_mappable(folio)) {
-			ret = do_set_pmd(vmf, folio, page);
+			ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ true);
 			if (ret != VM_FAULT_FALLBACK)
 				return ret;
 		}
Signature · 1 line
2.43.0
reviewer Acked-by on v2 2/2
#8 lore raw

Re: [PATCH v2 2/2] mm: don't allocate a page table under RCU in do_set_pmd()

Quoted 13 lines · On Fri, 06 Nov 2026 13:36:13 +0100 "David Hildenbrand (Arm)"…

do_fault_around() skips PTE table preallocation when the PMD already points to a PTE table. A concurrent collapse can then retract that table and clear the PMD before filemap_map_pages() locks the new PMD-sized folio.

filemap_map_pmd() can then call do_set_pmd() with an empty PMD and no preallocated table. On PowerPC hash MMU, do_set_pmd() then tries allocating a PTE table to deposit. But filemap_map_pmd() calls do_set_pmd() under RCU, so allocating from that context is not good.

This race became possible when retract_page_tables() stopped taking the mmap and VMA locks. Previously, those locks excluded retraction while the fault was in progress.

Quoted 6 lines

Add a may_alloc argument to do_set_pmd() and pass false from filemap_map_pmd(), so we can fail with VM_FAULT_FALLBACK instead of trying to allocate.

This issue should be fairly hard to hit in practice.

Yes, I imagine it was very difficult to reproduce, doing a sleeping pte_alloc_one() inside filemap_map_pmd()'s RCU read-side section when a concurrent retract_page_tables() empties the PMD. Only possible on PowerPC.

Quoted 4 lines
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---

Quoted 93 lines
 include/linux/mm.h |  3 ++-
 mm/filemap.c       |  2 +-
 mm/khugepaged.c    |  2 +-
 mm/memory.c        | 10 +++++++---
 4 files changed, 11 insertions(+), 6 deletions(-)
include/linux/mm.h +2 -1
index a2dd76cc5bbfe..bd144fec5f5da 100644
@@ -2156,7 +2156,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
 	return pte;
 }
 
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page);
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
+		bool may_alloc);
 void set_pte_range(struct vm_fault *vmf, struct folio *folio,
 		struct page *page, unsigned int nr, unsigned long addr);
 
mm/filemap.c +1 -1
index 86297841f9955..9871b4f32a916 100644
@@ -3745,7 +3745,7 @@ static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,
 
 	if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) {
 		struct page *page = folio_file_page(folio, start);
-		vm_fault_t ret = do_set_pmd(vmf, folio, page);
+		vm_fault_t ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ false);
 		if (!ret) {
 			/* The page is mapped successfully, reference consumed. */
 			folio_unlock(folio);
mm/khugepaged.c +1 -1
index 8d9f05a39afb3..5836e08abc984 100644
@@ -1826,7 +1826,7 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
 	}
 
 	vmf.pmd = pmdp;
-	if (do_set_pmd(&vmf, folio, page))
+	if (do_set_pmd(&vmf, folio, page, /* may_alloc= */ true))
 		return SCAN_FAIL;
 
 	folio_get(folio);
mm/memory.c +7 -3
index 421064aef3cf0..6eeed43b05a19 100644
@@ -5682,7 +5682,8 @@ static void deposit_prealloc_pte(struct vm_fault *vmf)
 	vmf->prealloc_pte = NULL;
 }
 
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
+		bool may_alloc)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	bool write = vmf->flags & FAULT_FLAG_WRITE;
@@ -5723,6 +5724,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	 * related to pte entry. Use the preallocated table for that.
 	 */
 	if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
+		if (!may_alloc)
+			return VM_FAULT_FALLBACK;
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
@@ -5764,7 +5767,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 	return ret;
 }
 #else
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
+		bool may_alloc)
 {
 	return VM_FAULT_FALLBACK;
 }
@@ -5888,7 +5892,7 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 
 	if (pmd_none(*vmf->pmd)) {
 		if (!needs_fallback && folio_test_pmd_mappable(folio)) {
-			ret = do_set_pmd(vmf, folio, page);
+			ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ true);
 			if (ret != VM_FAULT_FALLBACK)
 				return ret;
 		}
Signature · 2 lines
-- 
2.43.0
author maintainer
#9 lore raw

Re: [PATCH v2 2/2] mm: don't allocate a page table under RCU in do_set_pmd()

Quoted 19 lines · Usama Arif
Quoted 17 lines · On Fri, 06 Nov 2026 13:36:13 +0100 "David Hildenbrand (Arm)"…

do_fault_around() skips PTE table preallocation when the PMD already points to a PTE table. A concurrent collapse can then retract that table and clear the PMD before filemap_map_pages() locks the new PMD-sized folio.

filemap_map_pmd() can then call do_set_pmd() with an empty PMD and no preallocated table. On PowerPC hash MMU, do_set_pmd() then tries allocating a PTE table to deposit. But filemap_map_pmd() calls do_set_pmd() under RCU, so allocating from that context is not good.

This race became possible when retract_page_tables() stopped taking the mmap and VMA locks. Previously, those locks excluded retraction while the fault was in progress.

Add a may_alloc argument to do_set_pmd() and pass false from filemap_map_pmd(), so we can fail with VM_FAULT_FALLBACK instead of trying to allocate.

Quoted 7 lines
Quoted 2 lines

This issue should be fairly hard to hit in practice.

Yes, I imagine it was very difficult to reproduce, doing a sleeping pte_alloc_one() inside filemap_map_pmd()'s RCU read-side section when a concurrent retract_page_tables() empties the PMD. Only possible on PowerPC.

Yes. Did I express how much I hate us having to support this ppc handling?

I'll buy you a double digit number of beers if you manage to rip that out ;)

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 2/2
#10 lore raw

Re: [PATCH v2 2/2] mm: don't allocate a page table under RCU in do_set_pmd()

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

do_fault_around() skips PTE table preallocation when the PMD already points to a PTE table. A concurrent collapse can then retract that table and clear the PMD before filemap_map_pages() locks the new PMD-sized folio.

Right, having a PTE table when we check in do_fault_around() doesn't mean it'll still be there later ;)

Quoted 5 lines

filemap_map_pmd() can then call do_set_pmd() with an empty PMD and no preallocated table. On PowerPC hash MMU, do_set_pmd() then tries allocating a PTE table to deposit. But filemap_map_pmd() calls do_set_pmd() under RCU, so allocating from that context is not good.

Quoted 7 lines

This race became possible when retract_page_tables() stopped taking the mmap and VMA locks. Previously, those locks excluded retraction while the fault was in progress.

Add a may_alloc argument to do_set_pmd() and pass false from filemap_map_pmd(), so we can fail with VM_FAULT_FALLBACK instead of trying to allocate.

YES, just fall back and let the regular fault handling take over after leaving RCU.

Quoted 6 lines

This issue should be fairly hard to hit in practice.

Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---

LGTM1

Cheers, Lance

reviewer Reviewed-by on v2 2/2
#11 lore raw

Re: [PATCH v2 2/2] mm: don't allocate a page table under RCU in do_set_pmd()

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

do_fault_around() skips PTE table preallocation when the PMD already points to a PTE table. A concurrent collapse can then retract that table and clear the PMD before filemap_map_pages() locks the new PMD-sized folio.

filemap_map_pmd() can then call do_set_pmd() with an empty PMD and no preallocated table. On PowerPC hash MMU, do_set_pmd() then tries allocating a PTE table to deposit. But filemap_map_pmd() calls do_set_pmd() under RCU, so allocating from that context is not good.

This race became possible when retract_page_tables() stopped taking the mmap and VMA locks. Previously, those locks excluded retraction while the fault was in progress.

Add a may_alloc argument to do_set_pmd() and pass false from filemap_map_pmd(), so we can fail with VM_FAULT_FALLBACK instead of trying to allocate.

Quoted 5 lines

This issue should be fairly hard to hit in practice.

Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author maintainer Applied
#12 lore raw
In reply to David Hildenbrand (Arm)
Quoted 1 line · David Hildenbrand (Arm)

This isn't super urgent, so it can wait for 7.5.

Applied, thanks!

tree: mm/core.git
branch: core-7.5.misc

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.

[1/2] mm: free unused page table allocated by do_set_pmd()

      https://git.kernel.org/mm/core/c/e5111d38
[2/2] mm: don't allocate a page table under RCU in do_set_pmd()
      https://git.kernel.org/mm/core/c/bd4d4f8d
Signature · 3 lines
Cheers,

David