Re: [RESEND v7 15/29] fs/proc: account PMD swap entries in smaps

From: Usama Arif

Date: Fri Sep 25 2026 - 09:29:43 EST




On 24/09/2026 21:37, David Hildenbrand (Arm) wrote:
> On 9/14/26 14:28, Usama Arif wrote:
>> 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)
>
> Wouldn't consuming nr_pages be more consistent with other functions that consume
> swap entry ranges?

Yes, done. mss->swap += nr_pages << PAGE_SHIFT; below in the next revision now.


>
>> +{
>> + unsigned long nr_pages = size >> PAGE_SHIFT;
>> +
>> + mss->swap += size;
>> + do {
>> + int mapcount = swp_swapcount(entry);
>
> While at it, can you please call that swapcount?

Done.

>
>> +
>> + 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);
>> +}
>
>
> [...]
>
>> @@ -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;
>>
>
> Interesting, the function already seems to do what we want. Weird that we
> indicate PM_SWAP for migration entries ...


>
> Also, are device_private PMD's handled correctly? *doubt*
>
> [maybe there were patches, not sure]
>

Not in smaps. smaps_account() documents device-private
as fake-present and smaps_pte_entry() does it, but smaps_pmd_entry()
never did, so a PMD-mapped device-private folio got PSS charged
undivided and landed in shared_* even when mapped exactly once.
I have folded the fix for the next revision