__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 --- 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