__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 Reviewed-by: David Hildenbrand (Arm) Signed-off-by: Jose A. Perez de Azpillaga --- 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