From: "Jose A. Perez de Azpillaga" <azpijr@gmail.com>
To: David Hildenbrand <david@kernel.org>, Zi Yan <ziy@nvidia.com>,
Andrew Morton <akpm@linux-foundation.org>,
"Zach O'Keefe" <zokeefe@google.com>,
Hugh Dickins <hughd@google.com>, Lorenzo Stoakes <ljs@kernel.org>,
Kiryl Shutsemau <kas@kernel.org>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Dev Jain <dev.jain@arm.com>
Cc: "Liam R. Howlett" <liam@infradead.org>,
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>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
linux-mm@kvack.org, Matthew Wilcox <willy@infradead.org>,
Jan Kara <jack@suse.cz>, Johannes Weiner <hannes@cmpxchg.org>,
Rik van Riel <riel@surriel.com>, Song Liu <songliubraving@fb.com>,
linux-fsdevel@vger.kernel.org,
"Jose A. Perez de Azpillaga" <azpijr@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()
Date: Fri, 27 Nov 2026 10:12:15 +0100 [thread overview]
Message-ID: <20261127091344.113688-3-azpijr@gmail.com> (raw)
In-Reply-To: <20261127091344.113688-1-azpijr@gmail.com>
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 <azpijr@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-27 9:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261127091344.113688-1-azpijr@gmail.com>
2026-11-27 9:12 ` [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd() Jose A. Perez de Azpillaga
2026-11-27 9:12 ` Jose A. Perez de Azpillaga [this message]
2026-11-27 9:12 ` [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked 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=20261127091344.113688-3-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