[RESEND v7 15/29] fs/proc: account PMD swap entries in smaps
From: Usama Arif
Date: Mon Sep 14 2026 - 09:01:44 EST
smaps_pmd_entry() derives a page from a non-present PMD and gives up if it
cannot. A PMD swap entry carries no PFN, so a swapped-out THP is accounted
nowhere: its bytes show up in neither Swap nor SwapPss, and VmSwap silently
loses them.
The slots of a PMD swap entry can have different swap reference counts, so
SwapPss has to be computed one slot at a time rather than divided once.
Factor that loop out of smaps_pte_entry() and call it from both levels,
then return early: a swapped-out THP is not resident and must not be added
to AnonHugePages or Rss.
pagemap needs no equivalent change - it already emits PM_SWAP and
type | offset+idx for a PFN-less softleaf entry - but its
thp_migration_supported() gate answers a different question than the decode
requires, so use pmd_is_valid_softleaf() there instead.
Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
---
fs/proc/task_mmu.c | 42 ++++++++++++++++++++++++++++--------------
1 file changed, 28 insertions(+), 14 deletions(-)
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 24425e2308951..aa3f4d54a8580 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -957,6 +957,27 @@ static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)
#endif
}
+static void smaps_account_swap(struct mem_size_stats *mss,
+ softleaf_t entry, unsigned long size)
+{
+ unsigned long nr_pages = size >> PAGE_SHIFT;
+
+ mss->swap += size;
+ do {
+ int mapcount = swp_swapcount(entry);
+
+ if (mapcount >= 2) {
+ u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
+
+ do_div(pss_delta, mapcount);
+ mss->swap_pss += pss_delta;
+ } else {
+ mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
+ }
+ entry.val++;
+ } while (--nr_pages);
+}
+
static void smaps_pte_entry(pte_t *pte, unsigned long addr,
struct mm_walk *walk)
{
@@ -978,18 +999,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,
const softleaf_t entry = softleaf_from_pte(ptent);
if (softleaf_is_swap(entry)) {
- int mapcount;
-
- mss->swap += PAGE_SIZE;
- mapcount = swp_swapcount(entry);
- if (mapcount >= 2) {
- u64 pss_delta = (u64)PAGE_SIZE << PSS_SHIFT;
-
- do_div(pss_delta, mapcount);
- mss->swap_pss += pss_delta;
- } else {
- mss->swap_pss += (u64)PAGE_SIZE << PSS_SHIFT;
- }
+ smaps_account_swap(mss, entry, PAGE_SIZE);
} else if (softleaf_has_pfn(entry)) {
if (softleaf_is_device_private(entry))
present = true;
@@ -1019,9 +1029,13 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,
if (pmd_present(*pmd)) {
page = vm_normal_page_pmd(vma, addr, *pmd);
present = true;
- } else if (unlikely(thp_migration_supported())) {
+ } else {
const softleaf_t entry = softleaf_from_pmd(*pmd);
+ if (softleaf_is_swap(entry)) {
+ smaps_account_swap(mss, entry, HPAGE_PMD_SIZE);
+ return;
+ }
if (softleaf_has_pfn(entry))
page = softleaf_to_page(entry);
}
@@ -2000,7 +2014,7 @@ static int pagemap_pmd_range_thp(pmd_t *pmdp, unsigned long addr,
flags |= PM_UFFD_WP;
if (pm->show_pfn)
frame = pmd_pfn(pmd) + idx;
- } else if (thp_migration_supported()) {
+ } else if (pmd_is_valid_softleaf(pmd)) {
const softleaf_t entry = softleaf_from_pmd(pmd);
unsigned long offset;
--
2.53.0-Meta