From: Pedro Falcato <pfalcato@suse.de>
To: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>, Zi Yan <ziy@nvidia.com>
Cc: Pedro Falcato <pfalcato@suse.de>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Lorenzo Stoakes <ljs@kernel.org>,
Kiryl Shutsemau <kas@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 6/8] mm/khugepaged: hoist isolation into collapse_isolate_folio()
Date: Wed, 11 Nov 2026 17:51:19 +0000 [thread overview]
Message-ID: <20261111175121.1328383-7-pfalcato@suse.de> (raw)
In-Reply-To: <20261111175121.1328383-1-pfalcato@suse.de>
Move unwindy folio isolation logic from the main loop to
collapse_isolate_folio().
As a small side effect, try_to_unmap_flush() is no longer called for
errors prior to try_to_unmap(). This should be fine as there are other
mechanisms in place to flush it before references/locks are dropped.
Aside from that, no functional changes intended.
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
mm/khugepaged.c | 118 +++++++++++++++++++++++++++---------------------
1 file changed, 66 insertions(+), 52 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 236df9be7e27..1e58ae5b3e74 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2354,6 +2354,69 @@ prepare_collapse_file_folio(pgoff_t index, struct collapse_file_state *state)
return result;
}
+static enum scan_result collapse_isolate_folio(struct collapse_file_state *state)
+{
+ struct folio *folio = state->folio;
+ enum scan_result result;
+
+ /*
+ * The folio must be locked, so we can drop the i_pages lock
+ * without racing with truncate.
+ */
+ VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
+
+ /* make sure the folio is up to date */
+ if (unlikely(!folio_test_uptodate(folio))) {
+ result = SCAN_FAIL;
+ goto out_unlock;
+ }
+
+ /*
+ * If file was truncated then extended, or hole-punched, before
+ * we locked the first folio, then a THP might be there already.
+ * This will be discovered on the first iteration.
+ */
+ if (is_pmd_order(folio_order(folio))) {
+ result = SCAN_PTE_MAPPED_HUGEPAGE;
+ goto out_unlock;
+ }
+
+ if (folio_mapping(folio) != state->mapping) {
+ result = SCAN_TRUNCATED;
+ goto out_unlock;
+ }
+
+ if (!state->is_shmem && (folio_test_dirty(folio) ||
+ folio_test_writeback(folio))) {
+ /*
+ * khugepaged only works on clean file-backed folios,
+ * so this folio is dirty because it hasn't been flushed
+ * since first write.
+ */
+ result = SCAN_PAGE_DIRTY_OR_WRITEBACK;
+ goto out_unlock;
+ }
+
+ if (!folio_isolate_lru(folio)) {
+ result = SCAN_DEL_PAGE_LRU;
+ goto out_unlock;
+ }
+
+ if (!filemap_release_folio(folio, GFP_KERNEL)) {
+ result = SCAN_PAGE_HAS_PRIVATE;
+ folio_putback_lru(folio);
+ goto out_unlock;
+ }
+
+ if (folio_mapped(folio))
+ try_to_unmap(folio, TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
+ return SCAN_SUCCEED;
+out_unlock:
+ folio_unlock(folio);
+ folio_put(folio);
+ return result;
+}
+
/**
* collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
*
@@ -2474,61 +2537,12 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
folio = state.folio;
if (result != SCAN_SUCCEED)
goto xa_unlocked;
- /*
- * The folio must be locked, so we can drop the i_pages lock
- * without racing with truncate.
- */
- VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
-
- /* make sure the folio is up to date */
- if (unlikely(!folio_test_uptodate(folio))) {
- result = SCAN_FAIL;
- goto out_unlock;
- }
- /*
- * If file was truncated then extended, or hole-punched, before
- * we locked the first folio, then a THP might be there already.
- * This will be discovered on the first iteration.
- */
- if (is_pmd_order(folio_order(folio))) {
- result = SCAN_PTE_MAPPED_HUGEPAGE;
- goto out_unlock;
- }
-
- if (folio_mapping(folio) != mapping) {
- result = SCAN_TRUNCATED;
- goto out_unlock;
- }
-
- if (!is_shmem && (folio_test_dirty(folio) ||
- folio_test_writeback(folio))) {
- /*
- * khugepaged only works on clean file-backed folios,
- * so this folio is dirty because it hasn't been flushed
- * since first write.
- */
- result = SCAN_PAGE_DIRTY_OR_WRITEBACK;
- goto out_unlock;
- }
-
- if (!folio_isolate_lru(folio)) {
- result = SCAN_DEL_PAGE_LRU;
- goto out_unlock;
- }
-
- if (!filemap_release_folio(folio, GFP_KERNEL)) {
- result = SCAN_PAGE_HAS_PRIVATE;
- folio_putback_lru(folio);
- goto out_unlock;
- }
-
- if (folio_mapped(folio))
- try_to_unmap(folio,
- TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
+ result = collapse_isolate_folio(&state);
+ if (result != SCAN_SUCCEED)
+ goto xa_unlocked;
xas_lock_irq(&xas);
-
VM_BUG_ON_FOLIO(folio != xa_load(xas.xa, index), folio);
/*
--
2.56.0
next prev parent reply other threads:[~2026-11-11 17:51 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 17:51 [PATCH v2 0/8] mm/khugepaged: collapse_file() cleanups Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 1/8] mm/khugepaged: separate out windy folio logic from collapse_file Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 2/8] mm/khugepaged: sanitize the folio passed to file folio collapse logic Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 3/8] mm/khugepaged: factor out page cache folio reading Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 4/8] mm/khugepaged: factor out and simplify dirty/writeback handling Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 5/8] mm/khugepaged: simplify prepare folio locking and exit paths Pedro Falcato
2026-11-11 17:51 ` Pedro Falcato [this message]
2026-11-11 17:51 ` [PATCH v2 7/8] mm/khugepaged: hoist more code into collapse_isolate_folio() Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 8/8] mm/khugepaged: flesh out try_to_unmap_flush() comment Pedro Falcato
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=20261111175121.1328383-7-pfalcato@suse.de \
--to=pfalcato@suse.de \
--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=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=nico.pache@linux.dev \
--cc=ryan.roberts@arm.com \
--cc=usama.arif@linux.dev \
--cc=ziy@nvidia.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
all inboxes | Powered by JetHome®