* [PATCH v3 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()
[not found] <20261201115332.88194-1-azpijr@gmail.com>
@ 2026-12-01 11:53 ` Jose A. Perez de Azpillaga
2026-12-01 11:53 ` [PATCH v3 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd() Jose A. Perez de Azpillaga
2026-12-01 11:53 ` [PATCH v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
2 siblings, 0 replies; 4+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-12-01 11:53 UTC (permalink / raw)
To: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Lorenzo Stoakes, Baolin Wang
Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, 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
ppc64 allocates a page table in do_set_pmd() when it's not preallocated
while callers of it hold the folio lock, e.g. set_huge_pmd() and
finish_fault() (under certain circumstances).
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
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
Changes in v3:
- rename nofs_flags to flags, one-line comment (Lorenzo)
- shorter changelog opener (Lorenzo)
Changes in v2:
- new, replaces v1 3/4 "mm/memory: never allocate a page table in
do_set_pmd()" (David)
v2: https://lore.kernel.org/20261127091344.113688-2-azpijr@gmail.com
v1: https://lore.kernel.org/20261125100058.15200-4-azpijr@gmail.com
mm/memory.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/mm/memory.c b/mm/memory.c
index dfa307a9228c..aab038740a8c 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5806,9 +5806,14 @@ 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 flags;
+
if (!may_alloc)
return VM_FAULT_FALLBACK;
+ /* Folio is locked, so avoid deadlock against concurrent reclaim */
+ flags = memalloc_nofs_save();
vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
+ memalloc_nofs_restore(flags);
if (!vmf->prealloc_pte)
return VM_FAULT_OOM;
pgtable_allocated = true;
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v3 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()
[not found] <20261201115332.88194-1-azpijr@gmail.com>
2026-12-01 11:53 ` [PATCH v3 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd() Jose A. Perez de Azpillaga
@ 2026-12-01 11:53 ` Jose A. Perez de Azpillaga
2026-12-01 11:53 ` [PATCH v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
2 siblings, 0 replies; 4+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-12-01 11:53 UTC (permalink / raw)
To: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Lorenzo Stoakes, Baolin Wang, Kiryl Shutsemau
Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, 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, so 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
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
Changes in v3:
- rename nofs_flags to flags, one-line comment (Lorenzo)
- drop the memcg v1 example from the changelog (Lorenzo)
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"
v2: https://lore.kernel.org/20261127091344.113688-3-azpijr@gmail.com
v1: https://lore.kernel.org/20261125100058.15200-2-azpijr@gmail.com
mm/khugepaged.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 93d4a6f159e8..d7fd5e471783 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1839,14 +1839,15 @@ 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 flags;
+
+ /* Folio is locked, so avoid deadlock against concurrent reclaim */
+ 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(flags);
if (!pmdp)
return SCAN_FAIL;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked
[not found] <20261201115332.88194-1-azpijr@gmail.com>
2026-12-01 11:53 ` [PATCH v3 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd() Jose A. Perez de Azpillaga
2026-12-01 11:53 ` [PATCH v3 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd() Jose A. Perez de Azpillaga
@ 2026-12-01 11:53 ` Jose A. Perez de Azpillaga
2026-12-01 11:56 ` Lorenzo Stoakes (ARM)
2 siblings, 1 reply; 4+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-12-01 11:53 UTC (permalink / raw)
To: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Lorenzo Stoakes, Baolin Wang
Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, Usama Arif, Kiryl Shutsemau, 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() tries to avoid page table allocation in finish_fault() by
pre-allocating one if the PMD is empty.
However, it is no longer safe to assume that a page table found here
(thus avoiding pre-allocation) remains in place, since
retract_page_tables() no longer takes the mmap and VMA locks. If the
table is retracted, finish_fault() allocates one with the folio locked,
which can deadlock in reclaim.
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 retry
preallocates the page table, so it only loops if the table is retracted
again every time.
Fixes: 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")
Cc: stable@vger.kernel.org
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
Changes in v3:
- take Lorenzo's finish_fault() comment
- note at the __do_fault() preallocation that it is best-effort
(Lorenzo)
- reword the changelog opener, mention the NOPAGE retry, drop the
memcg v1 example (Lorenzo)
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
v2: https://lore.kernel.org/20261127091344.113688-4-azpijr@gmail.com
v1: https://lore.kernel.org/20261125100058.15200-3-azpijr@gmail.com
mm/memory.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index aab038740a8c..4f4a11bca741 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5701,6 +5701,8 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
* folio_set_writeback(B)
* folio_unlock(B)
* # flush A, B to clear the writeback
+ *
+ * Best-effort: the page table can be retracted after this check.
*/
if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
@@ -5997,10 +5999,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;
+ /*
+ * Page table retraction can happen after we decided there was
+ * no need to prealloc and the folio is locked, so to avoid
+ * deadlock against concurrent reclaim, just retry the fault.
+ */
+ 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] 4+ messages in thread* Re: [PATCH v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked
2026-12-01 11:53 ` [PATCH v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
@ 2026-12-01 11:56 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-01 11:56 UTC (permalink / raw)
To: Jose A. Perez de Azpillaga
Cc: David Hildenbrand, Zi Yan, Andrew Morton, Zach O'Keefe,
Hugh Dickins, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Vlastimil Babka, Mike Rapoport, Michal Hocko,
linux-mm, Matthew Wilcox, Jan Kara, Johannes Weiner, Rik van Riel,
Song Liu, linux-fsdevel, stable
On Tue, Dec 01, 2026 at 12:53:21PM +0100, Jose A. Perez de Azpillaga wrote:
> __do_fault() tries to avoid page table allocation in finish_fault() by
> pre-allocating one if the PMD is empty.
>
> However, it is no longer safe to assume that a page table found here
> (thus avoiding pre-allocation) remains in place, since
> retract_page_tables() no longer takes the mmap and VMA locks. If the
> table is retracted, finish_fault() allocates one with the folio locked,
> which can deadlock in reclaim.
>
> 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 retry
> preallocates the page table, so it only loops if the table is retracted
> again every time.
>
> Fixes: 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")
> Cc: stable@vger.kernel.org
> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Thanks LGTM so:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> Changes in v3:
> - take Lorenzo's finish_fault() comment
> - note at the __do_fault() preallocation that it is best-effort
> (Lorenzo)
> - reword the changelog opener, mention the NOPAGE retry, drop the
> memcg v1 example (Lorenzo)
>
> 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
>
> v2: https://lore.kernel.org/20261127091344.113688-4-azpijr@gmail.com
> v1: https://lore.kernel.org/20261125100058.15200-3-azpijr@gmail.com
>
> mm/memory.c | 14 ++++++++++----
> 1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index aab038740a8c..4f4a11bca741 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -5701,6 +5701,8 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
> * folio_set_writeback(B)
> * folio_unlock(B)
> * # flush A, B to clear the writeback
> + *
> + * Best-effort: the page table can be retracted after this check.
> */
> if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
> vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
> @@ -5997,10 +5999,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;
> + /*
> + * Page table retraction can happen after we decided there was
> + * no need to prealloc and the folio is locked, so to avoid
> + * deadlock against concurrent reclaim, just retry the fault.
> + */
> + 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
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 4+ messages in thread