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