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 <[email protected]>
To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
To: Steven Rostedt <[email protected]>
To: Masami Hiramatsu <[email protected]>
To: Lorenzo Stoakes <[email protected]>
Cc: "Liam R. Howlett" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Mathieu Desnoyers <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Nico Pache <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Dev Jain <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Lance Yang <[email protected]>
Cc: Usama Arif <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Joshua Hahn <[email protected]>
Cc: Rakie Kim <[email protected]>
Cc: Byungchul Park <[email protected]>
Cc: Gregory Price <[email protected]>
Cc: Ying Huang <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
 include/linux/mm.h             |  4 +---
 include/linux/page-flags.h     | 21 ++++++++++++++++++---
 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, 24 insertions(+), 20 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 786f8a47cea6d..b5b60f3ad58dc 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3071,9 +3071,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_swapcache(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 ee7abb3000c11..9b585e68127a2 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -1210,6 +1210,23 @@ 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.
+ *
+ * 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_swapcache(folio) &&
+              !folio_test_hugetlb(folio);
+}
 /**
  * folio_has_private - Determine if folio has private stuff
  * @folio: The folio to be checked
@@ -1219,9 +1236,7 @@ static __always_inline void 
__ClearPageAnonExclusive(struct page *page)
  */
 static inline int folio_has_private(const struct folio *folio)
 {
-       return (!!folio->private && !folio_test_swapcache(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 fb9abec40ec79..5425ef7bbae6e 100644
--- a/include/trace/events/pagemap.h
+++ b/include/trace/events/pagemap.h
@@ -22,9 +22,7 @@
        (folio_test_swapcache(folio)    ? PAGEMAP_SWAPCACHE  : 0) | \
        (folio_test_swapbacked(folio)   ? PAGEMAP_SWAPBACKED : 0) | \
        (folio_test_mappedtodisk(folio) ? PAGEMAP_MAPPEDDISK : 0) | \
-       (folio_test_private(folio) && \
-        !folio_test_swapcache(folio) && \
-        !folio_test_hugetlb(folio)     ? PAGEMAP_BUFFERS    : 0) \
+       (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 546b37ccce37f..d085fd09466ef 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4810,9 +4810,7 @@ static int split_huge_pages_pid(int pid, unsigned long 
vaddr_start,
                 * will try to drop it before split and then check if the folio
                 * can be split or not. So skip the check here.
                 */
-               if (!(folio_test_private(folio) &&
-                     !folio_test_swapcache(folio) &&
-                     !folio_test_hugetlb(folio)) &&
+               if (!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 f14e7bfee14bd..666a7365e16e8 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_swapcache(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 4022d6c381896..e03a1c46bce01 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2705,10 +2705,7 @@ bool filemap_dirty_folio(struct address_space *mapping, 
struct folio *folio)
        if (folio_test_set_dirty(folio))
                return false;
 
-       __folio_mark_dirty(folio, mapping,
-                          !(folio_test_private(folio) &&
-                            !folio_test_swapcache(folio) &&
-                            !folio_test_hugetlb(folio)));
+       __folio_mark_dirty(folio, mapping, !folio_test_fs_private(folio));
 
        if (mapping->host) {
                /* !PageAnon && !swapper_space */
diff --git a/mm/vmscan.c b/mm/vmscan.c
index ac275eec086b9..29051c0f89881 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -955,8 +955,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_swapcache(folio) &&
-             !folio_test_hugetlb(folio)))
+       if (!folio_test_fs_private(folio))
                return;
 
        mapping = folio_mapping(folio);

-- 
2.53.0


Reply via email to