From: "Jose A. Perez de Azpillaga" <azpijr@gmail.com>
To: David Hildenbrand <david@kernel.org>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
Jan Kara <jack@suse.cz>,
Andrew Morton <akpm@linux-foundation.org>,
Zi Yan <ziy@nvidia.com>, "Zach O'Keefe" <zokeefe@google.com>,
Lorenzo Stoakes <ljs@kernel.org>, Dev Jain <dev.jain@arm.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Hugh Dickins <hughd@google.com>, Kiryl Shutsemau <kas@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Rik van Riel <riel@surriel.com>, Song Liu <songliubraving@fb.com>
Cc: "Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
"Jose A. Perez de Azpillaga" <azpijr@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio
Date: Wed, 25 Nov 2026 10:59:44 +0100 [thread overview]
Message-ID: <20261125100058.15200-2-azpijr@gmail.com> (raw)
In-Reply-To: <20261125100058.15200-1-azpijr@gmail.com>
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
next prev parent reply other threads:[~2026-11-25 10:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 9:59 [PATCH 0/4] mm: don't allocate page tables with the folio locked Jose A. Perez de Azpillaga
2026-11-25 9:59 ` Jose A. Perez de Azpillaga [this message]
2026-11-25 9:59 ` [PATCH 2/4] mm/memory: don't allocate in finish_fault() " Jose A. Perez de Azpillaga
2026-11-25 9:59 ` [PATCH 3/4] mm/memory: never allocate a page table in do_set_pmd() Jose A. Perez de Azpillaga
2026-11-25 9:59 ` [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks Jose A. Perez de Azpillaga
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125100058.15200-2-azpijr@gmail.com \
--to=azpijr@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=nico.pache@linux.dev \
--cc=riel@surriel.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=songliubraving@fb.com \
--cc=stable@vger.kernel.org \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=willy@infradead.org \
--cc=ziy@nvidia.com \
--cc=zokeefe@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox