Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] mm: don't allocate page tables with the folio locked
@ 2026-11-25  9:59 Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio Jose A. Perez de Azpillaga
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25  9:59 UTC (permalink / raw)
  To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel, Jose A. Perez de Azpillaga

David asked for the remaining places that allocate with __GFP_FS while
holding a folio lock [1]. The fault path and khugepaged still allocate
page tables that way, and collapse_file() calls filemap_release_folio()
and swaps in shmem folios with folio locks held.

Patch 1 allocates the page tables khugepaged needs before locking the
folio.
Patch 2 makes finish_fault() retry the fault instead of allocating when
the page table went away after __do_fault() checked the PMD.
Patch 3 drops the allocation from do_set_pmd(), no caller needs it anymore.
Patch 4 runs the part of collapse_file() that holds folio locks in a NOFS
scope, like __folio_split().

I don't have a real deadlock for patch 4, it just does what [2] does for
splitting, so I left it without a stable tag.

Based on mm/core.git for-next, patch 3 needs the do_set_pmd() changes
there.

Tested on x86_64 and on ppc64le with the hash MMU in QEMU.

[1] https://lore.kernel.org/66e5f9e3-17d4-4635-8919-c67ed562f9f0@kernel.org
[2] https://lore.kernel.org/20261124-migrate_reclaim_deadlock-v2-1-03cc147e1e36@kernel.org

Jose A. Perez de Azpillaga (4):
  mm/khugepaged: allocate page tables before locking the folio
  mm/memory: don't allocate in finish_fault() with the folio locked
  mm/memory: never allocate a page table in do_set_pmd()
  mm/khugepaged: use NOFS while collapse_file() holds folio locks

 include/linux/mm.h |  3 +-
 mm/filemap.c       |  2 +-
 mm/khugepaged.c    | 91 ++++++++++++++++++++++++++++++++++------------
 mm/memory.c        | 39 ++++++++------------
 4 files changed, 85 insertions(+), 50 deletions(-)


base-commit: 98f6b507621822477db7ec7cef8519de0dd243c4
-- 
2.56.1



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio
  2026-11-25  9:59 [PATCH 0/4] mm: don't allocate page tables with the folio locked Jose A. Perez de Azpillaga
@ 2026-11-25  9:59 ` Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25  9:59 UTC (permalink / raw)
  To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel, Jose A. Perez de Azpillaga, stable

try_collapse_pte_mapped_thp() calls set_huge_pmd() with the folio
locked. set_huge_pmd() allocates the missing p4d/pud/pmd tables there,
and on ppc64 with the hash MMU do_set_pmd() also allocates the page
table to deposit.

Allocating with __GFP_FS while holding a folio lock can deadlock in
reclaim. For example, memcg reclaim on cgroup v1 waits for writeback
that may be waiting for the folio lock we hold.

Allocate these page tables before locking the folio. The mmap lock
keeps the upper levels around, and an unused deposit table is freed
after unlocking the folio.

Fixes: 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
Fixes: f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")
Cc: stable@vger.kernel.org
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
 mm/khugepaged.c | 79 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 56 insertions(+), 23 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 93d4a6f159e8..e5adf6960a7b 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1822,37 +1822,57 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 	}
 }
 
+/*
+ * Allocate the page tables needed to map a PMD at @haddr. Must be called
+ * before locking the folio: allocating with __GFP_FS while holding a folio
+ * lock can deadlock in reclaim.
+ */
+static enum scan_result alloc_huge_pmd_tables(struct mm_struct *mm,
+		unsigned long haddr, pmd_t *pmd, pgtable_t *prealloc_pte)
+{
+	if (!pmd) {
+		pgd_t *pgd = pgd_offset(mm, haddr);
+		p4d_t *p4d;
+		pud_t *pud;
+
+		p4d = p4d_alloc(mm, pgd, haddr);
+		if (!p4d)
+			return SCAN_FAIL;
+		pud = pud_alloc(mm, p4d, haddr);
+		if (!pud)
+			return SCAN_FAIL;
+		if (!pmd_alloc(mm, pud, haddr))
+			return SCAN_FAIL;
+	}
+
+	/* Archs like ppc64 deposit a page table along with the PMD mapping. */
+	if (arch_needs_pgtable_deposit()) {
+		*prealloc_pte = pte_alloc_one(mm);
+		if (!*prealloc_pte)
+			return SCAN_FAIL;
+	}
+	return SCAN_SUCCEED;
+}
+
 /* folio must be locked, and mmap_lock must be held */
 static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
-		pmd_t *pmdp, struct folio *folio, struct page *page)
+		pmd_t *pmdp, struct folio *folio, struct page *page,
+		pgtable_t *prealloc_pte)
 {
-	struct mm_struct *mm = vma->vm_mm;
 	struct vm_fault vmf = {
 		.vma = vma,
 		.address = addr,
 		.flags = 0,
+		.pmd = pmdp,
+		.prealloc_pte = *prealloc_pte,
 	};
-	pgd_t *pgdp;
-	p4d_t *p4dp;
-	pud_t *pudp;
+	vm_fault_t ret;
 
 	mmap_assert_locked(vma->vm_mm);
 
-	if (!pmdp) {
-		pgdp = pgd_offset(mm, addr);
-		p4dp = p4d_alloc(mm, pgdp, addr);
-		if (!p4dp)
-			return SCAN_FAIL;
-		pudp = pud_alloc(mm, p4dp, addr);
-		if (!pudp)
-			return SCAN_FAIL;
-		pmdp = pmd_alloc(mm, pudp, addr);
-		if (!pmdp)
-			return SCAN_FAIL;
-	}
-
-	vmf.pmd = pmdp;
-	if (do_set_pmd(&vmf, folio, page, /* may_alloc= */ true))
+	ret = do_set_pmd(&vmf, folio, page, /* may_alloc= */ false);
+	*prealloc_pte = vmf.prealloc_pte;
+	if (ret)
 		return SCAN_FAIL;
 
 	folio_get(folio);
@@ -1881,6 +1901,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
 	unsigned long haddr = addr & HPAGE_PMD_MASK;
 	unsigned long end = haddr + HPAGE_PMD_SIZE;
 	struct vm_area_struct *vma = vma_lookup(mm, haddr);
+	pgtable_t prealloc_pte = NULL;
 	struct folio *folio;
 	pte_t *start_pte, *pte;
 	pmd_t *pmd, pgt_pmd;
@@ -1916,10 +1937,18 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
 	if (userfaultfd_protected(vma))
 		return SCAN_PTE_UFFD;
 
+	if (install_pmd) {
+		result = alloc_huge_pmd_tables(mm, haddr, pmd, &prealloc_pte);
+		if (result != SCAN_SUCCEED)
+			goto free_pte;
+	}
+
 	folio = filemap_lock_folio(vma->vm_file->f_mapping,
 			       linear_page_index(vma, haddr));
-	if (IS_ERR(folio))
-		return SCAN_PAGE_NULL;
+	if (IS_ERR(folio)) {
+		result = SCAN_PAGE_NULL;
+		goto free_pte;
+	}
 
 	if (!is_pmd_order(folio_order(folio))) {
 		result = SCAN_PAGE_COMPOUND;
@@ -2067,7 +2096,8 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
 maybe_install_pmd:
 	/* step 5: install pmd entry */
 	result = install_pmd
-			? set_huge_pmd(vma, haddr, pmd, folio, &folio->page)
+			? set_huge_pmd(vma, haddr, pmd, folio, &folio->page,
+				       &prealloc_pte)
 			: SCAN_SUCCEED;
 	goto drop_folio;
 abort:
@@ -2086,6 +2116,9 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
 drop_folio:
 	folio_unlock(folio);
 	folio_put(folio);
+free_pte:
+	if (prealloc_pte)
+		pte_free(mm, prealloc_pte);
 	return result;
 }
 
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked
  2026-11-25  9:59 [PATCH 0/4] mm: don't allocate page tables with the folio locked Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio Jose A. Perez de Azpillaga
@ 2026-11-25  9:59 ` Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 3/4] mm/memory: never allocate a page table in do_set_pmd() Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks Jose A. Perez de Azpillaga
  3 siblings, 0 replies; 8+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25  9:59 UTC (permalink / raw)
  To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel, Jose A. Perez de Azpillaga, stable

__do_fault() preallocates a page table only when the PMD is empty, so
that finish_fault() does not have to allocate one with the folio locked.

Since retract_page_tables() no longer takes the mmap and VMA locks, the
page table can be removed while the fault waits in ->fault().
finish_fault() then finds an empty PMD without a preallocated table, and
calls pte_alloc() with the folio locked. On ppc64 with the hash MMU,
do_set_pmd() allocates the page table to deposit in the same case.

Allocating with __GFP_FS while holding a folio lock can deadlock in
reclaim, for example the memcg reclaim deadlock on cgroup v1 described
in commit 63f3655f9501 ("mm, memcg: fix reclaim deadlock with
writeback").

Return VM_FAULT_NOPAGE instead, as we already do when
pte_offset_map_lock() fails. The retried fault preallocates the page
table before locking the folio.

Fixes: 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")
Cc: stable@vger.kernel.org
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
 mm/memory.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 11cc4039b853..b7e53d7cf6e5 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5986,16 +5986,21 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 	}
 
 	if (pmd_none(*vmf->pmd)) {
+		/*
+		 * The page table was removed after __do_fault() found it, so
+		 * nothing was preallocated. Allocating with the folio locked
+		 * can deadlock in reclaim, so retry the fault instead.
+		 */
+		if (!vmf->prealloc_pte)
+			return VM_FAULT_NOPAGE;
+
 		if (!needs_fallback && folio_test_pmd_mappable(folio)) {
 			ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ true);
 			if (ret != VM_FAULT_FALLBACK)
 				return ret;
 		}
 
-		if (vmf->prealloc_pte)
-			pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
-		else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
-			return VM_FAULT_OOM;
+		pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
 	}
 
 	nr_pages = folio_nr_pages(folio);
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/4] mm/memory: never allocate a page table in do_set_pmd()
  2026-11-25  9:59 [PATCH 0/4] mm: don't allocate page tables with the folio locked Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
@ 2026-11-25  9:59 ` Jose A. Perez de Azpillaga
  2026-11-25  9:59 ` [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks Jose A. Perez de Azpillaga
  3 siblings, 0 replies; 8+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25  9:59 UTC (permalink / raw)
  To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel, Jose A. Perez de Azpillaga

do_set_pmd() is always called with the folio locked, so it should not
allocate the page table to deposit. finish_fault() and khugepaged now
always pass one, and filemap_map_pmd() already passes may_alloc=false.

Drop may_alloc and fall back when no page table was preallocated.

No functional change intended.

Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
 include/linux/mm.h |  3 +--
 mm/filemap.c       |  2 +-
 mm/khugepaged.c    |  2 +-
 mm/memory.c        | 26 +++++++-------------------
 4 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index d09173a94e9c..5e7044b6ff87 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2160,8 +2160,7 @@ 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,
-		bool may_alloc);
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page);
 void set_pte_range(struct vm_fault *vmf, struct folio *folio,
 		struct page *page, unsigned int nr, unsigned long addr);
 
diff --git a/mm/filemap.c b/mm/filemap.c
index d730b0ea4667..b47c25808814 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3751,7 +3751,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, /* may_alloc= */ false);
+		vm_fault_t ret = do_set_pmd(vmf, folio, page);
 		if (!ret) {
 			/* The page is mapped successfully, reference consumed. */
 			folio_unlock(folio);
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index e5adf6960a7b..6d2431a20bc9 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1870,7 +1870,7 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
 
 	mmap_assert_locked(vma->vm_mm);
 
-	ret = do_set_pmd(&vmf, folio, page, /* may_alloc= */ false);
+	ret = do_set_pmd(&vmf, folio, page);
 	*prealloc_pte = vmf.prealloc_pte;
 	if (ret)
 		return SCAN_FAIL;
diff --git a/mm/memory.c b/mm/memory.c
index b7e53d7cf6e5..8c7c6be5495a 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5758,13 +5758,11 @@ 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,
-		bool may_alloc)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page)
 {
 	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;
 
@@ -5803,16 +5801,11 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
 
 	/*
 	 * Archs like ppc64 need additional space to store information
-	 * related to pte entry. Use the preallocated table for that.
+	 * related to pte entry. Use the table the caller preallocated
+	 * before locking the folio 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;
-		pgtable_allocated = true;
-	}
+	if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte)
+		return ret;
 
 	vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
 	if (unlikely(!pmd_none(*vmf->pmd)))
@@ -5842,15 +5835,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
-vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page,
-		bool may_alloc)
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *page)
 {
 	return VM_FAULT_FALLBACK;
 }
@@ -5995,7 +5983,7 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 			return VM_FAULT_NOPAGE;
 
 		if (!needs_fallback && folio_test_pmd_mappable(folio)) {
-			ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ true);
+			ret = do_set_pmd(vmf, folio, page);
 			if (ret != VM_FAULT_FALLBACK)
 				return ret;
 		}
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
  2026-11-25  9:59 [PATCH 0/4] mm: don't allocate page tables with the folio locked Jose A. Perez de Azpillaga
                   ` (2 preceding siblings ...)
  2026-11-25  9:59 ` [PATCH 3/4] mm/memory: never allocate a page table in do_set_pmd() Jose A. Perez de Azpillaga
@ 2026-11-25  9:59 ` Jose A. Perez de Azpillaga
  2026-11-25 10:55   ` David Hildenbrand (Arm)
  3 siblings, 1 reply; 8+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25  9:59 UTC (permalink / raw)
  To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel, Jose A. Perez de Azpillaga

collapse_file() keeps the folios it collapses locked until it is done.
With those locks held, it calls filemap_release_folio() with GFP_KERNEL,
and for shmem it swaps folios in with the mapping gfp, which includes
__GFP_FS.

As when splitting folios, allocating with __GFP_FS while holding folio
locks can deadlock in direct reclaim or compaction, and __GFP_FS also
lets filemap_release_folio() wait on the filesystem.

Run the part that holds folio locks in a NOFS scope, as __folio_split()
does. The huge folio is still allocated before that.

Fixes: f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem pages")
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
 mm/khugepaged.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 6d2431a20bc9..c3c733d3a32b 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2302,6 +2302,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	LIST_HEAD(pagelist);
 	XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
 	enum scan_result result = SCAN_SUCCEED;
+	unsigned int nofs_flags;
 	int nr_none = 0;
 	bool do_readahead = false;
 	bool readahead_done = false;
@@ -2320,6 +2321,13 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		goto out;
 	new_pfn = folio_pfn(new_folio);
 
+	/*
+	 * We allocate while holding folio locks below, when swapping in shmem
+	 * folios and in filemap_release_folio(). Allocating with __GFP_FS
+	 * there can deadlock in reclaim or compaction.
+	 */
+	nofs_flags = memalloc_nofs_save();
+
 	mapping_set_update(&xas, mapping);
 
 retry:
@@ -2698,7 +2706,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 		folio_put_refs(folio, 2 + folio_nr_pages(folio));
 	}
 
-	goto out;
+	goto out_nofs;
 
 rollback:
 	/* Something went wrong: roll back page cache changes */
@@ -2744,6 +2752,8 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	}
 out_put:
 	folio_put(new_folio);
+out_nofs:
+	memalloc_nofs_restore(nofs_flags);
 out:
 	VM_BUG_ON(!list_empty(&pagelist));
 	trace_mm_khugepaged_collapse_file(mm, new_pfn, index, addr, is_shmem, file, HPAGE_PMD_NR, result);
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
  2026-11-25  9:59 ` [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks Jose A. Perez de Azpillaga
@ 2026-11-25 10:55   ` David Hildenbrand (Arm)
  2026-11-25 11:12     ` Jose A. Perez de Azpillaga
  0 siblings, 1 reply; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 10:55 UTC (permalink / raw)
  To: Jose A. Perez de Azpillaga, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel

On 11/25/26 10:59, Jose A. Perez de Azpillaga wrote:
> collapse_file() keeps the folios it collapses locked until it is done.
> With those locks held, it calls filemap_release_folio() with GFP_KERNEL,
> and for shmem it swaps folios in with the mapping gfp, which includes
> __GFP_FS.
> 
> As when splitting folios, allocating with __GFP_FS while holding folio
> locks can deadlock in direct reclaim or compaction, and __GFP_FS also
> lets filemap_release_folio() wait on the filesystem.

How is that possible with my fixes?

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
  2026-11-25 10:55   ` David Hildenbrand (Arm)
@ 2026-11-25 11:12     ` Jose A. Perez de Azpillaga
  2026-11-25 11:15       ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 8+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25 11:12 UTC (permalink / raw)
  To: David Hildenbrand (Arm), Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel

On 11/25/26 11:55 AM, David Hildenbrand (Arm) wrote:
> On 11/25/26 10:59, Jose A. Perez de Azpillaga wrote:
>> collapse_file() keeps the folios it collapses locked until it is done.
>> With those locks held, it calls filemap_release_folio() with GFP_KERNEL,
>> and for shmem it swaps folios in with the mapping gfp, which includes
>> __GFP_FS.
>>
>> As when splitting folios, allocating with __GFP_FS while holding folio
>> locks can deadlock in direct reclaim or compaction, and __GFP_FS also
>> lets filemap_release_folio() wait on the filesystem.
> How is that possible with my fixes?
> 

it isn't, sorry. I'll drop 4/4.

-- 
cheers,
jose a. p-a


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
  2026-11-25 11:12     ` Jose A. Perez de Azpillaga
@ 2026-11-25 11:15       ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 11:15 UTC (permalink / raw)
  To: Jose A. Perez de Azpillaga, Matthew Wilcox (Oracle), Jan Kara,
	Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
	Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
	Johannes Weiner, Rik van Riel, Song Liu
  Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-mm, linux-fsdevel

On 11/25/26 12:12, Jose A. Perez de Azpillaga wrote:
> On 11/25/26 11:55 AM, David Hildenbrand (Arm) wrote:
>> On 11/25/26 10:59, Jose A. Perez de Azpillaga wrote:
>>> collapse_file() keeps the folios it collapses locked until it is done.
>>> With those locks held, it calls filemap_release_folio() with GFP_KERNEL,
>>> and for shmem it swaps folios in with the mapping gfp, which includes
>>> __GFP_FS.
>>>
>>> As when splitting folios, allocating with __GFP_FS while holding folio
>>> locks can deadlock in direct reclaim or compaction, and __GFP_FS also
>>> lets filemap_release_folio() wait on the filesystem.
>> How is that possible with my fixes?
>>
> 
> it isn't, sorry. I'll drop 4/4.

So there are no other allocations in there besides the one done by splitting?

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-25 11:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  9:59 [PATCH 0/4] mm: don't allocate page tables with the folio locked Jose A. Perez de Azpillaga
2026-11-25  9:59 ` [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio Jose A. Perez de Azpillaga
2026-11-25  9:59 ` [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
2026-11-25  9:59 ` [PATCH 3/4] mm/memory: never allocate a page table in do_set_pmd() Jose A. Perez de Azpillaga
2026-11-25  9:59 ` [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks Jose A. Perez de Azpillaga
2026-11-25 10:55   ` David Hildenbrand (Arm)
2026-11-25 11:12     ` Jose A. Perez de Azpillaga
2026-11-25 11:15       ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox