* [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()
[not found] <20261127091344.113688-1-azpijr@gmail.com>
@ 2026-11-27 9:12 ` Jose A. Perez de Azpillaga
2026-11-27 9:12 ` [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd() Jose A. Perez de Azpillaga
2026-11-27 9:12 ` [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
2 siblings, 0 replies; 3+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-27 9:12 UTC (permalink / raw)
To: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Lorenzo Stoakes, Kiryl Shutsemau, Baolin Wang,
Dev Jain
Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Barry Song, Lance Yang,
Usama Arif, Vlastimil Babka, Mike Rapoport, Michal Hocko,
linux-mm, Matthew Wilcox, Jan Kara, Johannes Weiner, Rik van Riel,
Song Liu, linux-fsdevel, Jose A. Perez de Azpillaga, stable
On ppc64 with the hash MMU, do_set_pmd() allocates the page table to
deposit when the caller did not preallocate one. Both callers that let
it allocate hold the folio lock: set_huge_pmd() in khugepaged never
preallocates, and finish_fault() has no preallocated table when the
page table was removed after __do_fault() checked the PMD.
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").
pte_alloc_one() takes no gfp flags, so allocate the table in a NOFS
scope.
Fixes: 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
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>
---
Changes in v2:
- new, replaces v1 3/4 "mm/memory: never allocate a page table in
do_set_pmd()" (David)
v1: https://lore.kernel.org/20261125100058.15200-4-azpijr@gmail.com
mm/memory.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mm/memory.c b/mm/memory.c
index dfa307a9228c..05d03b32ce52 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5806,9 +5806,17 @@ 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) {
+ unsigned int nofs_flags;
+
if (!may_alloc)
return VM_FAULT_FALLBACK;
+ /*
+ * The folio is locked, and reclaim or compaction with __GFP_FS
+ * could deadlock on it. pte_alloc_one() takes no gfp flags.
+ */
+ nofs_flags = memalloc_nofs_save();
vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
+ memalloc_nofs_restore(nofs_flags);
if (!vmf->prealloc_pte)
return VM_FAULT_OOM;
pgtable_allocated = true;
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()
[not found] <20261127091344.113688-1-azpijr@gmail.com>
2026-11-27 9:12 ` [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd() Jose A. Perez de Azpillaga
@ 2026-11-27 9:12 ` Jose A. Perez de Azpillaga
2026-11-27 9:12 ` [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
2 siblings, 0 replies; 3+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-27 9:12 UTC (permalink / raw)
To: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Lorenzo Stoakes, Kiryl Shutsemau, Baolin Wang,
Dev Jain
Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Barry Song, Lance Yang,
Usama Arif, Vlastimil Babka, Mike Rapoport, Michal Hocko,
linux-mm, Matthew Wilcox, Jan Kara, Johannes Weiner, Rik van Riel,
Song Liu, linux-fsdevel, Jose A. Perez de Azpillaga, stable
try_collapse_pte_mapped_thp() calls set_huge_pmd() with the folio
locked. When there is no PMD table, set_huge_pmd() allocates the
missing p4d/pud/pmd tables there.
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 in a NOFS scope.
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>
---
Changes in v2:
- allocate in a NOFS scope instead of before locking the folio (David)
- the deposit table is handled in do_set_pmd() now
- new subject, was "mm/khugepaged: allocate page tables before locking
the folio"
v1: https://lore.kernel.org/20261125100058.15200-2-azpijr@gmail.com
mm/khugepaged.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 93d4a6f159e8..2ac8b5621db0 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1839,14 +1839,18 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
mmap_assert_locked(vma->vm_mm);
if (!pmdp) {
+ unsigned int nofs_flags;
+
+ /*
+ * The folio is locked, and reclaim or compaction with __GFP_FS
+ * could deadlock on it.
+ */
+ nofs_flags = memalloc_nofs_save();
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);
+ pudp = p4dp ? pud_alloc(mm, p4dp, addr) : NULL;
+ pmdp = pudp ? pmd_alloc(mm, pudp, addr) : NULL;
+ memalloc_nofs_restore(nofs_flags);
if (!pmdp)
return SCAN_FAIL;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked
[not found] <20261127091344.113688-1-azpijr@gmail.com>
2026-11-27 9:12 ` [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd() Jose A. Perez de Azpillaga
2026-11-27 9:12 ` [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd() Jose A. Perez de Azpillaga
@ 2026-11-27 9:12 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 3+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-27 9:12 UTC (permalink / raw)
To: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Lorenzo Stoakes, Kiryl Shutsemau, Baolin Wang,
Dev Jain
Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Barry Song, Lance Yang,
Usama Arif, Vlastimil Babka, Mike Rapoport, Michal Hocko,
linux-mm, Matthew Wilcox, Jan Kara, Johannes Weiner, Rik van Riel,
Song Liu, 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.
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").
If the folio can't be mapped with a PMD, 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>
---
Changes in v2:
- return VM_FAULT_NOPAGE after trying do_set_pmd() (David)
- drop the ppc64 sentence, do_set_pmd() is handled on its own now
v1: https://lore.kernel.org/20261125100058.15200-3-azpijr@gmail.com
mm/memory.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 05d03b32ce52..dd0cb09ba7ef 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -6000,10 +6000,14 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
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;
+ /*
+ * 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;
+ 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] 3+ messages in thread
end of thread, other threads:[~2026-11-27 9:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261127091344.113688-1-azpijr@gmail.com>
2026-11-27 9:12 ` [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd() Jose A. Perez de Azpillaga
2026-11-27 9:12 ` [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd() Jose A. Perez de Azpillaga
2026-11-27 9:12 ` [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox