[PATCH v3 10/14] mm/page-flags: introduce folio_test_fs_private()

From: Zi Yan

Date: Mon Sep 07 2026 - 23:02:54 EST


folio_test_fs_private() wraps folio->private != NULL check and excludes
swapcache and hugetlb folios, since swapcache uses swp_entry_t overlapping
with folio->private and hugetlb sets its own flags in folio->private.
Replace open code with the helper, since core MM does this check
frequently.

No functional change intended.

Assisted-by: Claude:claude-opus-4-8
Assisted-by: Codex:gpt-5
Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
To: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
To: David Hildenbrand <david@xxxxxxxxxx>
To: Steven Rostedt <rostedt@xxxxxxxxxxx>
To: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
To: Lorenzo Stoakes <ljs@xxxxxxxxxx>
Cc: "Liam R. Howlett" <liam@xxxxxxxxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxxxxx>
Cc: Mike Rapoport <rppt@xxxxxxxxxx>
Cc: Suren Baghdasaryan <surenb@xxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxx>
Cc: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: Zi Yan <ziy@xxxxxxxxxx>
Cc: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Cc: Nico Pache <nico.pache@xxxxxxxxx>
Cc: Ryan Roberts <ryan.roberts@xxxxxxx>
Cc: Dev Jain <dev.jain@xxxxxxx>
Cc: Barry Song <baohua@xxxxxxxxxx>
Cc: Lance Yang <lance.yang@xxxxxxxxx>
Cc: Usama Arif <usama.arif@xxxxxxxxx>
Cc: Matthew Brost <matthew.brost@xxxxxxxxx>
Cc: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>
Cc: Rakie Kim <rakie.kim@xxxxxx>
Cc: Byungchul Park <byungchul@xxxxxx>
Cc: Gregory Price <gourry@xxxxxxxxxx>
Cc: Ying Huang <ying.huang@xxxxxxxxxxxxxxxxx>
Cc: Alistair Popple <apopple@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: linux-fsdevel@xxxxxxxxxxxxxxx
Cc: linux-mm@xxxxxxxxx
Cc: linux-trace-kernel@xxxxxxxxxxxxxxx
---
include/linux/mm.h | 4 +---
include/linux/page-flags.h | 26 +++++++++++++++++++++++---
include/trace/events/pagemap.h | 4 +---
mm/huge_memory.c | 4 +---
mm/migrate.c | 3 +--
mm/page-writeback.c | 5 +----
mm/vmscan.c | 3 +--
7 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 5eb8a62fafb56..c861525cda83f 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3053,9 +3053,7 @@ static inline int folio_expected_ref_count(const struct folio *folio)
* One reference from filesystem private data.
* Use data_race() since folio might not be locked.
*/
- ref_count += data_race(folio_test_private(folio)) &&
- !folio_test_hugetlb(folio) &&
- !folio_test_swapbacked(folio);
+ ref_count += data_race(folio_test_fs_private(folio));
}

/* One reference per page table mapping. */
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 9cb4d64739798..ce7fccd90367b 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -1210,6 +1210,28 @@ static __always_inline void __ClearPageAnonExclusive(struct page *page)
(0xffUL /* order */ | 1UL << PG_has_hwpoisoned | \
1UL << PG_large_rmappable | 1UL << PG_partially_mapped)

+/**
+ * folio_test_fs_private - check if the folio has filesystem private data
+ * @folio: The folio to check.
+ *
+ * Use this in code that may encounter swapcache or hugetlb folios but only
+ * wants to detect filesystem private data. Swapcache stores swp_entry_t in
+ * folio->swap, a union with folio->private, and hugetlb stores its own flags
+ * in folio->private; both are excluded.
+ *
+ * NOTE: For swapcache, folio->swap.val PG_swapcache are not set as a whole,
+ * so folio_test_swapcache() is not reliable to exclude swapcache.
+ * Use folio_test_swapbacked() instead, since it remains set when a folio is
+ * added to/removed from swapcache.
+ *
+ * Return: true if folio->private is set and the folio is neither swapcache
+ * nor hugetlb.
+ */
+static inline bool folio_test_fs_private(const struct folio *folio)
+{
+ return folio_test_private(folio) && !folio_test_swapbacked(folio) &&
+ !folio_test_hugetlb(folio);
+}
/**
* folio_has_private - Determine if folio has private stuff
* @folio: The folio to be checked
@@ -1219,9 +1241,7 @@ static __always_inline void __ClearPageAnonExclusive(struct page *page)
*/
static inline int folio_has_private(const struct folio *folio)
{
- return (!!folio->private && !folio_test_swapbacked(folio) &&
- !folio_test_hugetlb(folio)) ||
- folio_test_private_2(folio);
+ return folio_test_fs_private(folio) || folio_test_private_2(folio);
}

#undef PF_ANY
diff --git a/include/trace/events/pagemap.h b/include/trace/events/pagemap.h
index 8193f91216822..547a550b81ee1 100644
--- a/include/trace/events/pagemap.h
+++ b/include/trace/events/pagemap.h
@@ -23,9 +23,7 @@
(folio_test_swapbacked(folio) ? PAGEMAP_SWAPBACKED : 0) | \
(folio_test_mappedtodisk(folio) ? PAGEMAP_MAPPEDDISK : 0) | \
/* data_race() is used to read folio->private locklessly */ \
- (data_race(folio_test_private(folio)) && \
- !folio_test_swapbacked(folio) && \
- !folio_test_hugetlb(folio) ? PAGEMAP_BUFFERS : 0) \
+ (data_race(folio_test_fs_private(folio)) ? PAGEMAP_BUFFERS : 0) \
)

TRACE_EVENT(mm_lru_insertion,
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 9b2a9d0794a61..4cb7d9bc47544 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4833,9 +4833,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
* can be split or not. So skip the check here.
* data_race() is used to read folio->private locklessly.
*/
- if (!(data_race(folio_test_private(folio)) &&
- !folio_test_swapbacked(folio) &&
- !folio_test_hugetlb(folio)) &&
+ if (!data_race(folio_test_fs_private(folio)) &&
folio_expected_ref_count(folio) != folio_ref_count(folio))
goto next;

diff --git a/mm/migrate.c b/mm/migrate.c
index f6befd3ff1c46..2e133d533c8da 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1327,8 +1327,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,
* free the metadata, so the page can be freed.
*/
if (!src->mapping) {
- if (folio_test_private(src) && !folio_test_swapbacked(src) &&
- !folio_test_hugetlb(src)) {
+ if (folio_test_fs_private(src)) {
try_to_free_buffers(src);
goto out;
}
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 02ad49b10be07..e7389020d2a56 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2706,10 +2706,7 @@ bool filemap_dirty_folio(struct address_space *mapping, struct folio *folio)
return false;

/* data_race() is used to read folio->private locklessly */
- __folio_mark_dirty(folio, mapping,
- !(data_race(folio_test_private(folio)) &&
- !folio_test_swapbacked(folio) &&
- !folio_test_hugetlb(folio)));
+ __folio_mark_dirty(folio, mapping, !data_race(folio_test_fs_private(folio)));

if (mapping->host) {
/* !PageAnon && !swapper_space */
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9348ebf9de882..47352bafc5b6f 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -978,8 +978,7 @@ static void folio_check_dirty_writeback(struct folio *folio,
*writeback = folio_test_writeback(folio);

/* Verify dirty/writeback state if the filesystem supports it */
- if (!(folio_test_private(folio) && !folio_test_swapbacked(folio) &&
- !folio_test_hugetlb(folio)))
+ if (!folio_test_fs_private(folio))
return;

mapping = folio_mapping(folio);

--
2.53.0