* [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio
[not found] <20261125100058.15200-1-azpijr@gmail.com>
@ 2026-11-25 9:59 ` Jose A. Perez de Azpillaga
2026-11-25 9:59 ` [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked Jose A. Perez de Azpillaga
1 sibling, 0 replies; 2+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25 9:59 UTC (permalink / raw)
To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
Johannes Weiner, Rik van Riel, Song Liu
Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
linux-mm, linux-fsdevel, Jose A. Perez de Azpillaga, stable
try_collapse_pte_mapped_thp() calls set_huge_pmd() with the folio
locked. set_huge_pmd() allocates the missing p4d/pud/pmd tables there,
and on ppc64 with the hash MMU do_set_pmd() also allocates the page
table to deposit.
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 before locking the folio. The mmap lock
keeps the upper levels around, and an unused deposit table is freed
after unlocking the folio.
Fixes: 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
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>
---
mm/khugepaged.c | 79 +++++++++++++++++++++++++++++++++++--------------
1 file changed, 56 insertions(+), 23 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 93d4a6f159e8..e5adf6960a7b 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1822,37 +1822,57 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
}
}
+/*
+ * Allocate the page tables needed to map a PMD at @haddr. Must be called
+ * before locking the folio: allocating with __GFP_FS while holding a folio
+ * lock can deadlock in reclaim.
+ */
+static enum scan_result alloc_huge_pmd_tables(struct mm_struct *mm,
+ unsigned long haddr, pmd_t *pmd, pgtable_t *prealloc_pte)
+{
+ if (!pmd) {
+ pgd_t *pgd = pgd_offset(mm, haddr);
+ p4d_t *p4d;
+ pud_t *pud;
+
+ p4d = p4d_alloc(mm, pgd, haddr);
+ if (!p4d)
+ return SCAN_FAIL;
+ pud = pud_alloc(mm, p4d, haddr);
+ if (!pud)
+ return SCAN_FAIL;
+ if (!pmd_alloc(mm, pud, haddr))
+ return SCAN_FAIL;
+ }
+
+ /* Archs like ppc64 deposit a page table along with the PMD mapping. */
+ if (arch_needs_pgtable_deposit()) {
+ *prealloc_pte = pte_alloc_one(mm);
+ if (!*prealloc_pte)
+ return SCAN_FAIL;
+ }
+ return SCAN_SUCCEED;
+}
+
/* folio must be locked, and mmap_lock must be held */
static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
- pmd_t *pmdp, struct folio *folio, struct page *page)
+ pmd_t *pmdp, struct folio *folio, struct page *page,
+ pgtable_t *prealloc_pte)
{
- struct mm_struct *mm = vma->vm_mm;
struct vm_fault vmf = {
.vma = vma,
.address = addr,
.flags = 0,
+ .pmd = pmdp,
+ .prealloc_pte = *prealloc_pte,
};
- pgd_t *pgdp;
- p4d_t *p4dp;
- pud_t *pudp;
+ vm_fault_t ret;
mmap_assert_locked(vma->vm_mm);
- if (!pmdp) {
- 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);
- if (!pmdp)
- return SCAN_FAIL;
- }
-
- vmf.pmd = pmdp;
- if (do_set_pmd(&vmf, folio, page, /* may_alloc= */ true))
+ ret = do_set_pmd(&vmf, folio, page, /* may_alloc= */ false);
+ *prealloc_pte = vmf.prealloc_pte;
+ if (ret)
return SCAN_FAIL;
folio_get(folio);
@@ -1881,6 +1901,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
unsigned long haddr = addr & HPAGE_PMD_MASK;
unsigned long end = haddr + HPAGE_PMD_SIZE;
struct vm_area_struct *vma = vma_lookup(mm, haddr);
+ pgtable_t prealloc_pte = NULL;
struct folio *folio;
pte_t *start_pte, *pte;
pmd_t *pmd, pgt_pmd;
@@ -1916,10 +1937,18 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
if (userfaultfd_protected(vma))
return SCAN_PTE_UFFD;
+ if (install_pmd) {
+ result = alloc_huge_pmd_tables(mm, haddr, pmd, &prealloc_pte);
+ if (result != SCAN_SUCCEED)
+ goto free_pte;
+ }
+
folio = filemap_lock_folio(vma->vm_file->f_mapping,
linear_page_index(vma, haddr));
- if (IS_ERR(folio))
- return SCAN_PAGE_NULL;
+ if (IS_ERR(folio)) {
+ result = SCAN_PAGE_NULL;
+ goto free_pte;
+ }
if (!is_pmd_order(folio_order(folio))) {
result = SCAN_PAGE_COMPOUND;
@@ -2067,7 +2096,8 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
maybe_install_pmd:
/* step 5: install pmd entry */
result = install_pmd
- ? set_huge_pmd(vma, haddr, pmd, folio, &folio->page)
+ ? set_huge_pmd(vma, haddr, pmd, folio, &folio->page,
+ &prealloc_pte)
: SCAN_SUCCEED;
goto drop_folio;
abort:
@@ -2086,6 +2116,9 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
drop_folio:
folio_unlock(folio);
folio_put(folio);
+free_pte:
+ if (prealloc_pte)
+ pte_free(mm, prealloc_pte);
return result;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked
[not found] <20261125100058.15200-1-azpijr@gmail.com>
2026-11-25 9:59 ` [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio Jose A. Perez de Azpillaga
@ 2026-11-25 9:59 ` Jose A. Perez de Azpillaga
1 sibling, 0 replies; 2+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-25 9:59 UTC (permalink / raw)
To: David Hildenbrand, Matthew Wilcox (Oracle), Jan Kara,
Andrew Morton, Zi Yan, Zach O'Keefe, Lorenzo Stoakes,
Dev Jain, Baolin Wang, Hugh Dickins, Kiryl Shutsemau,
Johannes Weiner, Rik van Riel, Song Liu
Cc: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
Nico Pache, Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
linux-mm, 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. On ppc64 with the hash MMU,
do_set_pmd() allocates the page table to deposit in the same case.
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").
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>
---
mm/memory.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 11cc4039b853..b7e53d7cf6e5 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5986,16 +5986,21 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
}
if (pmd_none(*vmf->pmd)) {
+ /*
+ * 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;
+
if (!needs_fallback && folio_test_pmd_mappable(folio)) {
ret = do_set_pmd(vmf, folio, page, /* may_alloc= */ true);
if (ret != VM_FAULT_FALLBACK)
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;
+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-25 10:01 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261125100058.15200-1-azpijr@gmail.com>
2026-11-25 9:59 ` [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio Jose A. Perez de Azpillaga
2026-11-25 9:59 ` [PATCH 2/4] 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