Skip to contentSkip to conversation

cleanup for stable_page_flags()

PATCH Applied Jinjiang Tu 3 participants 7 messages Jul 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover cleanup for stable_page_flags()

This series cleans up stable_page_flags(), see the details in the commit message.

Changes since v1:
* add missing "1 << KPF_*" covertions for patch 1
* collect Acked-by for patch 1
* only convert folio-specific flags to folio_test_*() helpers
* move KPF_IDLE cleanup to a separate patch

Jinjiang Tu (3):
fs: stable_page_flags(): use BIT_ULL() for KPF flags
fs: stable_page_flags(): use folio_test_*() helpers
fs: stable_page_flags(): simplify KPF_IDLE handling

 fs/proc/page.c | 56 +++++++++++++++++++++-----------------------------
 1 file changed, 23 insertions(+), 33 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 fs: stable_page_flags(): use BIT_ULL() for KPF flags

The stable_page_flags() function currently sets page flag bits using "1 << KPF_xxx" for various KPF_* definitions. KPF_* may be larger than 32, which will trigger -Wshift-count-overflow warning. All KPF_* values currently used in "1 << KPF_xxx" are smaller than 33, so no warning is triggered with current code.

Replace all occurrences of "1 << KPF_*" with the BIT_ULL() macro to ensure all shifts are performed on a 64-bit unsigned type. This makes the code robust, and safe for future extension.

No functional change is intended.

---
 fs/proc/page.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
fs/proc/page.c +17 -17
index 7d9387143435..ee39b321f1c6 100644
@@ -157,7 +157,7 @@ u64 stable_page_flags(const struct page *page)
 	 * it differentiates a memory hole from a page with no flags
 	 */
 	if (!page)
-		return 1 << KPF_NOPAGE;
+		return BIT_ULL(KPF_NOPAGE);
 
 	snapshot_page(&ps, page);
 	folio = &ps.folio_snapshot;
@@ -170,11 +170,11 @@ u64 stable_page_flags(const struct page *page)
 	 * pseudo flags for the well known (anonymous) memory mapped pages
 	 */
 	if (folio_mapped(folio))
-		u |= 1 << KPF_MMAP;
+		u |= BIT_ULL(KPF_MMAP);
 	if (is_anon) {
-		u |= 1 << KPF_ANON;
+		u |= BIT_ULL(KPF_ANON);
 		if ((mapping & FOLIO_MAPPING_FLAGS) == FOLIO_MAPPING_KSM)
-			u |= 1 << KPF_KSM;
+			u |= BIT_ULL(KPF_KSM);
 	}
 
 	/*
@@ -184,35 +184,35 @@ u64 stable_page_flags(const struct page *page)
 	if (ps.idx == 0)
 		u |= kpf_copy_bit(k, KPF_COMPOUND_HEAD, PG_head);
 	else
-		u |= 1 << KPF_COMPOUND_TAIL;
+		u |= BIT_ULL(KPF_COMPOUND_TAIL);
 	if (folio_test_hugetlb(folio))
-		u |= 1 << KPF_HUGE;
+		u |= BIT_ULL(KPF_HUGE);
 	else if (folio_test_large(folio) &&
 	         folio_test_large_rmappable(folio)) {
 		/* Note: we indicate any THPs here, not just PMD-sized ones */
-		u |= 1 << KPF_THP;
+		u |= BIT_ULL(KPF_THP);
 	} else if (is_huge_zero_pfn(ps.pfn)) {
-		u |= 1 << KPF_ZERO_PAGE;
-		u |= 1 << KPF_THP;
+		u |= BIT_ULL(KPF_ZERO_PAGE);
+		u |= BIT_ULL(KPF_THP);
 	} else if (is_zero_pfn(ps.pfn)) {
-		u |= 1 << KPF_ZERO_PAGE;
+		u |= BIT_ULL(KPF_ZERO_PAGE);
 	}
 
 	if (ps.flags & PAGE_SNAPSHOT_PG_BUDDY)
-		u |= 1 << KPF_BUDDY;
+		u |= BIT_ULL(KPF_BUDDY);
 
 	if (folio_test_offline(folio))
-		u |= 1 << KPF_OFFLINE;
+		u |= BIT_ULL(KPF_OFFLINE);
 	if (folio_test_pgtable(folio))
-		u |= 1 << KPF_PGTABLE;
+		u |= BIT_ULL(KPF_PGTABLE);
 	if (folio_test_slab(folio))
-		u |= 1 << KPF_SLAB;
+		u |= BIT_ULL(KPF_SLAB);
 
 #if defined(CONFIG_PAGE_IDLE_FLAG) && defined(CONFIG_64BIT)
 	u |= kpf_copy_bit(k, KPF_IDLE,          PG_idle);
 #else
 	if (ps.flags & PAGE_SNAPSHOT_PG_IDLE)
-		u |= 1 << KPF_IDLE;
+		u |= BIT_ULL(KPF_IDLE);
 #endif
 
 	u |= kpf_copy_bit(k, KPF_LOCKED,	PG_locked);
@@ -227,14 +227,14 @@ u64 stable_page_flags(const struct page *page)
 
 #define SWAPCACHE ((1 << PG_swapbacked) | (1 << PG_swapcache))
 	if ((k & SWAPCACHE) == SWAPCACHE)
-		u |= 1 << KPF_SWAPCACHE;
+		u |= BIT_ULL(KPF_SWAPCACHE);
 	u |= kpf_copy_bit(k, KPF_SWAPBACKED,	PG_swapbacked);
 
 	u |= kpf_copy_bit(k, KPF_UNEVICTABLE,	PG_unevictable);
 	u |= kpf_copy_bit(k, KPF_MLOCKED,	PG_mlocked);
 
 #ifdef CONFIG_MEMORY_FAILURE
-	if (u & (1 << KPF_HUGE))
+	if (u & BIT_ULL(KPF_HUGE))
 		u |= kpf_copy_bit(k, KPF_HWPOISON,	PG_hwpoison);
 	else
 		u |= kpf_copy_bit(ps.page_snapshot.flags.f, KPF_HWPOISON, PG_hwpoison);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/3 fs: stable_page_flags(): use folio_test_*() helpers

In reply to Jinjiang Tu

Since commit 304daa8132a9 ("maps4: add /proc/kpageflags interface"), /proc/kpageflags directly operates on page->flags to determine page status. Later, commit 177975495914 ("proc: export more page flags in /proc/kpageflags") started using page helper functions when exposing new flags, leading to a mix of both approaches.

For tail pages, the original code did not return corresponding status. commit 0a71649cb724 ("/proc/kpageflags: return KPF_SLAB for slab tail pages") and commit 832fc1de01ae ("/proc/kpageflags: return KPF_BUDDY for "tail" buddy pages") made tail slab/buddy pages also return corresponding status. Then commit dee3d0bef2b0 ("proc: rewrite stable_page_flags()") made all tail pages return the same status as their head page, except for hwpoison and mapped flags. It also cached the folio's flags and operate on the flags directly to avoid concurrency issues if using folio_test_*() helpers.

Since commit 476d87d6a061 ("fs: stable_page_flags(): use snapshot_page()"), we can now safely switch to folio_test_*() helpers instead of directly operating on flags, which is more readable and consistent with the rest of the kernel. Only convert cfolio-specific flags (i.e., anon, ksm, swapcache) to folio_test_*() helpers, which reduces redundant code. Keep others unchanged due to they aren't folio-specific flags or coverting them doesn't cleanup.

No functional change is intended.

---
 fs/proc/page.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
fs/proc/page.c +4 -10
index ee39b321f1c6..b3c1d7a7604d 100644
@@ -148,8 +148,6 @@ u64 stable_page_flags(const struct page *page)
 	const struct folio *folio;
 	struct page_snapshot ps;
 	unsigned long k;
-	unsigned long mapping;
-	bool is_anon;
 	u64 u = 0;
 
 	/*
@@ -161,19 +159,16 @@ u64 stable_page_flags(const struct page *page)
 
 	snapshot_page(&ps, page);
 	folio = &ps.folio_snapshot;
-
 	k = folio->flags.f;
-	mapping = (unsigned long)folio->mapping;
-	is_anon = mapping & FOLIO_MAPPING_ANON;
 
 	/*
 	 * pseudo flags for the well known (anonymous) memory mapped pages
 	 */
 	if (folio_mapped(folio))
 		u |= BIT_ULL(KPF_MMAP);
-	if (is_anon) {
+	if (folio_test_anon(folio)) {
 		u |= BIT_ULL(KPF_ANON);
-		if ((mapping & FOLIO_MAPPING_FLAGS) == FOLIO_MAPPING_KSM)
+		if (folio_test_ksm(folio))
 			u |= BIT_ULL(KPF_KSM);
 	}
 
@@ -225,11 +220,10 @@ u64 stable_page_flags(const struct page *page)
 	u |= kpf_copy_bit(k, KPF_ACTIVE,	PG_active);
 	u |= kpf_copy_bit(k, KPF_RECLAIM,	PG_reclaim);
 
-#define SWAPCACHE ((1 << PG_swapbacked) | (1 << PG_swapcache))
-	if ((k & SWAPCACHE) == SWAPCACHE)
+	if (folio_test_swapcache(folio))
 		u |= BIT_ULL(KPF_SWAPCACHE);
-	u |= kpf_copy_bit(k, KPF_SWAPBACKED,	PG_swapbacked);
 
+	u |= kpf_copy_bit(k, KPF_SWAPBACKED,	PG_swapbacked);
 	u |= kpf_copy_bit(k, KPF_UNEVICTABLE,	PG_unevictable);
 	u |= kpf_copy_bit(k, KPF_MLOCKED,	PG_mlocked);
 
Signature · 1 line
2.43.0
Acked-by on v2 2/3
#3 lore raw

Re: [PATCH v2 2/3] fs: stable_page_flags(): use folio_test_*() helpers

Quoted 24 lines · Jinjiang Tu

Since commit 304daa8132a9 ("maps4: add /proc/kpageflags interface"), /proc/kpageflags directly operates on page->flags to determine page status. Later, commit 177975495914 ("proc: export more page flags in /proc/kpageflags") started using page helper functions when exposing new flags, leading to a mix of both approaches.

For tail pages, the original code did not return corresponding status. commit 0a71649cb724 ("/proc/kpageflags: return KPF_SLAB for slab tail pages") and commit 832fc1de01ae ("/proc/kpageflags: return KPF_BUDDY for "tail" buddy pages") made tail slab/buddy pages also return corresponding status. Then commit dee3d0bef2b0 ("proc: rewrite stable_page_flags()") made all tail pages return the same status as their head page, except for hwpoison and mapped flags. It also cached the folio's flags and operate on the flags directly to avoid concurrency issues if using folio_test_*() helpers.

Since commit 476d87d6a061 ("fs: stable_page_flags(): use snapshot_page()"), we can now safely switch to folio_test_*() helpers instead of directly operating on flags, which is more readable and consistent with the rest of the kernel. Only convert cfolio-specific flags (i.e., anon, ksm, swapcache) to folio_test_*() helpers, which reduces redundant code. Keep others unchanged due to they aren't folio-specific flags or coverting them doesn't cleanup.

Quoted 3 lines

No functional change is intended.

Signature · 3 lines
Cheers,

David
#4 lore raw

PATCH v2 3/3 fs: stable_page_flags(): simplify KPF_IDLE handling

In reply to Jinjiang Tu

For KPF_IDLE, folio_test_idle() check in set_ps_flags() already handles all config cases, so remove the duplicate code in stable_page_flags() that deals with CONFIG_PAGE_IDLE_FLAG for the 64‑bit case.

No functional change is intended.

---
 fs/proc/page.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
fs/proc/page.c +3 -7
index b3c1d7a7604d..260772b20bd9 100644
@@ -196,6 +196,9 @@ u64 stable_page_flags(const struct page *page)
 	if (ps.flags & PAGE_SNAPSHOT_PG_BUDDY)
 		u |= BIT_ULL(KPF_BUDDY);
 
+	if (ps.flags & PAGE_SNAPSHOT_PG_IDLE)
+		u |= BIT_ULL(KPF_IDLE);
+
 	if (folio_test_offline(folio))
 		u |= BIT_ULL(KPF_OFFLINE);
 	if (folio_test_pgtable(folio))
@@ -203,13 +206,6 @@ u64 stable_page_flags(const struct page *page)
 	if (folio_test_slab(folio))
 		u |= BIT_ULL(KPF_SLAB);
 
-#if defined(CONFIG_PAGE_IDLE_FLAG) && defined(CONFIG_64BIT)
-	u |= kpf_copy_bit(k, KPF_IDLE,          PG_idle);
-#else
-	if (ps.flags & PAGE_SNAPSHOT_PG_IDLE)
-		u |= BIT_ULL(KPF_IDLE);
-#endif
-
 	u |= kpf_copy_bit(k, KPF_LOCKED,	PG_locked);
 	u |= kpf_copy_bit(k, KPF_DIRTY,		PG_dirty);
 	u |= kpf_copy_bit(k, KPF_UPTODATE,	PG_uptodate);
Signature · 1 line
2.43.0
Acked-by on v2 3/3
#5 lore raw

Re: [PATCH v2 3/3] fs: stable_page_flags(): simplify KPF_IDLE handling

Quoted 8 lines · Jinjiang Tu

For KPF_IDLE, folio_test_idle() check in set_ps_flags() already handles all config cases, so remove the duplicate code in stable_page_flags() that deals with CONFIG_PAGE_IDLE_FLAG for the 64‑bit case.

No functional change is intended.

---

Signature · 3 lines
Cheers,

David