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 2/8] mm/khugepaged: sanitize the folio passed to file folio collapse logic
Date: Wed, 11 Nov 2026 17:51:15 +0000 [thread overview]
Message-ID: <20261111175121.1328383-3-pfalcato@suse.de> (raw)
In-Reply-To: <20261111175121.1328383-1-pfalcato@suse.de>
At that point, the folio (which is taken from the i_pages xarray
directly) can assume 3 different kinds of values:
1) NULL. This is the complete absence of value from the xarray.
2) An xa_value. This is generally a shadow entry in the page cache, or a
swap entry in shmem page cache.
3) A real struct folio*.
This is confusing. Collapse NULL and xa_value into NULL right at the top
of the loop. Collapse logic generally doesn't care whether something is
fully absent, or whether there's some sort of underlying shadow entry.
This simplifies a bit of reading logic in prepare_collapse_file().
No functional change intended.
Signed-off-by: Pedro Falcato <pfalcato@suse.de>
---
mm/khugepaged.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 0fe19acf470b..ac148db2a4a0 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2225,6 +2225,19 @@ struct collapse_file_state {
bool readahead_done : 1;
};
+/**
+ * real_folio() - Convert a page cache folio * into a real folio *
+ * @folio: struct folio * to convert.
+ *
+ * Return: NULL if the passed folio is invalid (XA value, NULL), otherwise @folio.
+ */
+static inline struct folio *real_folio(struct folio *folio)
+{
+ if (!folio || xa_is_value(folio))
+ return NULL;
+ return folio;
+}
+
/**
* prepare_collapse_file_folio() - Prepare the collapsing of a single file folio
* @index: folio index in the page cache
@@ -2249,7 +2262,7 @@ prepare_collapse_file_folio(pgoff_t index, struct collapse_file_state *state)
struct folio *folio = state->folio;
if (is_shmem) {
- if (xa_is_value(folio) || !folio_test_uptodate(folio)) {
+ if (!folio || !folio_test_uptodate(folio)) {
xas_unlock_irq(state->xas);
/* swap in or instantiate fallocated page */
if (shmem_get_folio(mapping->host, index, 0,
@@ -2267,7 +2280,7 @@ prepare_collapse_file_folio(pgoff_t index, struct collapse_file_state *state)
goto xa_locked;
}
} else { /* !is_shmem */
- if (!folio || xa_is_value(folio)) {
+ if (!folio) {
state->do_readahead = !state->readahead_done;
result = SCAN_FAIL;
goto xa_unlocked;
@@ -2420,8 +2433,12 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
continue;
}
- /* At this point folio can be NULL, or a value. */
- state.folio = folio;
+ /*
+ * At this point folio can be a value. Make values NULL. The
+ * value itself isn't interesting here. Collapse only cares
+ * about the absence of a folio.
+ */
+ state.folio = real_folio(folio);
result = prepare_collapse_file_folio(index, &state);
folio = state.folio;
if (result != SCAN_SUCCEED)
--
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 ` Pedro Falcato [this message]
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 ` [PATCH v2 6/8] mm/khugepaged: hoist isolation into collapse_isolate_folio() Pedro Falcato
2026-11-11 17:51 ` [PATCH v2 7/8] mm/khugepaged: hoist more code " 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-3-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®