Skip to contentSkip to conversation

fs/proc: fix KPF_KSM reported for all anonymous pages

PATCH Superseded Jinjiang Tu 4 participants 6 messages Jun 26

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs/proc: fix KPF_KSM reported for all anonymous pages

Reading /proc/kpageflags for any anonymous page returns KPF_KSM set, even when KSM is not in use. As a result, tools misclassify all anonymous pages as KSM merged.

In stable_page_flags(), if the page is anonymous, then use (mapping & FOLIO_MAPPING_KSM) check to identify if the anonymous page is KSM page. However, FOLIO_MAPPING_KSM is FOLIO_MAPPING_ANON | FOLIO_MAPPING_ANON_KSM, (mapping & FOLIO_MAPPING_KSM) check returns true for all nonymous pages.

To fix it, use FOLIO_MAPPING_ANON_KSM instead.

Fixes dee3d0bef2b0 ("proc: rewrite stable_page_flags()")

---
Changelog in v2:
* use FOLIO_MAPPING_KSM to match explictly instead of use FOLIO_MAPPING_ANON_KSM
only.

 fs/proc/page.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/page.c +1 -1
index f9b2c2c906cd..7d9387143435 100644
@@ -173,7 +173,7 @@ u64 stable_page_flags(const struct page *page)
 		u |= 1 << KPF_MMAP;
 	if (is_anon) {
 		u |= 1 << KPF_ANON;
-		if (mapping & FOLIO_MAPPING_KSM)
+		if ((mapping & FOLIO_MAPPING_FLAGS) == FOLIO_MAPPING_KSM)
 			u |= 1 << KPF_KSM;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines

Reading /proc/kpageflags for any anonymous page returns KPF_KSM set, even when KSM is not in use. As a result, tools misclassify all anonymous pages as KSM merged.

In stable_page_flags(), if the page is anonymous, then use (mapping & FOLIO_MAPPING_KSM) check to identify if the anonymous page is KSM page. However, FOLIO_MAPPING_KSM is FOLIO_MAPPING_ANON | FOLIO_MAPPING_ANON_KSM, (mapping & FOLIO_MAPPING_KSM) check returns true for all nonymous pages.

"returns true for all nonymous pages."

should %s/nonymous/anonymous ?

Quoted 28 lines

To fix it, use FOLIO_MAPPING_ANON_KSM instead.

Fixes dee3d0bef2b0 ("proc: rewrite stable_page_flags()")

---
Changelog in v2:
* use FOLIO_MAPPING_KSM to match explictly instead of use FOLIO_MAPPING_ANON_KSM
only.

 fs/proc/page.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/page.c +1 -1
index f9b2c2c906cd..7d9387143435 100644
@@ -173,7 +173,7 @@ u64 stable_page_flags(const struct page *page)
         u |= 1 << KPF_MMAP;
     if (is_anon) {
         u |= 1 << KPF_ANON;
-        if (mapping & FOLIO_MAPPING_KSM)
+        if ((mapping & FOLIO_MAPPING_FLAGS) == FOLIO_MAPPING_KSM)
             u |= 1 << KPF_KSM;
     }
 
Signature · 1 line
2.43.0
#2 lore raw
Quoted 11 lines · 在 2026/6/26 10:09, [email protected] 写道:
Quoted 8 lines

Reading /proc/kpageflags for any anonymous page returns KPF_KSM set, even when KSM is not in use. As a result, tools misclassify all anonymous pages as KSM merged.

In stable_page_flags(), if the page is anonymous, then use (mapping & FOLIO_MAPPING_KSM) check to identify if the anonymous page is KSM page. However, FOLIO_MAPPING_KSM is FOLIO_MAPPING_ANON | FOLIO_MAPPING_ANON_KSM, (mapping & FOLIO_MAPPING_KSM) check returns true for all nonymous pages.

"returns true for all nonymous pages."

should %s/nonymous/anonymous ?

Ah, yes

Quoted 28 lines
Quoted 28 lines

To fix it, use FOLIO_MAPPING_ANON_KSM instead.

Fixes dee3d0bef2b0 ("proc: rewrite stable_page_flags()")

---
Changelog in v2:
* use FOLIO_MAPPING_KSM to match explictly instead of use FOLIO_MAPPING_ANON_KSM
only.

  fs/proc/page.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/page.c +1 -1
index f9b2c2c906cd..7d9387143435 100644
@@ -173,7 +173,7 @@ u64 stable_page_flags(const struct page *page)
          u |= 1 << KPF_MMAP;
      if (is_anon) {
          u |= 1 << KPF_ANON;
-        if (mapping & FOLIO_MAPPING_KSM)
+        if ((mapping & FOLIO_MAPPING_FLAGS) == FOLIO_MAPPING_KSM)
              u |= 1 << KPF_KSM;
      }
 
Signature · 1 line
2.43.0
Acked-by on v2 1/1
#3 lore raw
In reply to Jinjiang Tu
Quoted 9 lines · Jinjiang Tu

Reading /proc/kpageflags for any anonymous page returns KPF_KSM set, even when KSM is not in use. As a result, tools misclassify all anonymous pages as KSM merged.

In stable_page_flags(), if the page is anonymous, then use (mapping & FOLIO_MAPPING_KSM) check to identify if the anonymous page is KSM page. However, FOLIO_MAPPING_KSM is FOLIO_MAPPING_ANON | FOLIO_MAPPING_ANON_KSM, (mapping & FOLIO_MAPPING_KSM) check returns true for all nonymous pages.

Quoted 5 lines

To fix it, use FOLIO_MAPPING_ANON_KSM instead.

Fixes dee3d0bef2b0 ("proc: rewrite stable_page_flags()")

---

With spelling fix

And I'd appreciate if someone could look into cleaning up the code to switch back to folio helpers now that we have folio+page snapshots.

Signature · 3 lines
Cheers,

David
#4 lore raw
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 9 lines · Jinjiang Tu

Reading /proc/kpageflags for any anonymous page returns KPF_KSM set, even when KSM is not in use. As a result, tools misclassify all anonymous pages as KSM merged.

In stable_page_flags(), if the page is anonymous, then use (mapping & FOLIO_MAPPING_KSM) check to identify if the anonymous page is KSM page. However, FOLIO_MAPPING_KSM is FOLIO_MAPPING_ANON | FOLIO_MAPPING_ANON_KSM, (mapping & FOLIO_MAPPING_KSM) check returns true for all nonymous pages.

Quoted 8 lines
Quoted 5 lines

To fix it, use FOLIO_MAPPING_ANON_KSM instead.

Fixes dee3d0bef2b0 ("proc: rewrite stable_page_flags()")

---

With spelling fix

Thanks.

Quoted 2 lines

And I'd appreciate if someone could look into cleaning up the code to switch back to folio helpers now that we have folio+page snapshots.

I would like to do it.

Quoted 1 line
Acked-by on v2 1/1
#5 lore raw
In reply to Jinjiang Tu
Quoted 13 lines · Jinjiang Tu

Reading /proc/kpageflags for any anonymous page returns KPF_KSM set, even when KSM is not in use. As a result, tools misclassify all anonymous pages as KSM merged.

In stable_page_flags(), if the page is anonymous, then use (mapping & FOLIO_MAPPING_KSM) check to identify if the anonymous page is KSM page. However, FOLIO_MAPPING_KSM is FOLIO_MAPPING_ANON | FOLIO_MAPPING_ANON_KSM, (mapping & FOLIO_MAPPING_KSM) check returns true for all nonymous pages.

To fix it, use FOLIO_MAPPING_ANON_KSM instead.

Fixes dee3d0bef2b0 ("proc: rewrite stable_page_flags()")
Quoted 8 lines

---
Changelog in v2:
* use FOLIO_MAPPING_KSM to match explictly instead of use FOLIO_MAPPING_ANON_KSM
only.

 fs/proc/page.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

LGTM. Thanks.

Signature · 2 lines
Best Regards,
Yan, Zi