Re: [PATCH 01/32] mm/shmem: count only the swapped pages in the requested range

From: David Hildenbrand (Arm)

Date: Tue Dec 01 2026 - 04:43:11 EST


On 11/30/26 04:41, Zi Yan wrote:
> On Sun Nov 29, 2026 at 5:06 AM EST, David Hildenbrand (Arm) wrote:
>> shmem_partial_swap_usage() counts whole swap entries even when only part
>> of an entry falls within the requested range. Further, when a walk
>> starts inside a large entry, xas_pause() can resume beyond its end and
>> skip subsequent entries.
>>
>> Consequently, we can overcount "Swap" in /proc/<pid>/smaps and
>> smaps_rollup, especially for private writable mappings where the
>> function can be called once per empty PTE, but we can also undercount
>> it.
>>
>> Let's fix it by clamping each entry to the remaining range and advancing
>> "start" to avoid counting pages twice, and by using xas_set() to resume
>> at the first uncounted index after cond_resched_rcu().
>>
>> This has low urgency, as it only affects the "Swap" statistics in
>> /proc/<pid>/smaps and smaps_rollup. No need to CC stable.
>>
>> Fixes: 50f381eccefd ("mm: shmem: extend shmem_partial_swap_usage() to support large folio swap")
>> Reported-by: Chris Mason <mason@xxxxxxxxxx>
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>> ---
>> mm/shmem.c | 24 +++++++++++++++++++++---
>> 1 file changed, 21 insertions(+), 3 deletions(-)
>>
> <snip>
>
>> if (need_resched()) {
>> - xas_pause(&xas);
>> + /* Resume at the first index that is not counted yet. */
>> + xas_set(&xas, start);
>> cond_resched_rcu();
>> }
>> }
>
> This part reminds me of Lorenzo's fix to filemap_cachestat()[1]. But
> there is no fix for racing with collapse/split like [2], doing
> xas_set(&xas, start) regardless need_resched(). It is about smaps, so
> maybe it is tolerable.
>
>
> [1] https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-2-3d2454362e8d@xxxxxxxxxx/
> [2] https://lore.kernel.org/all/20261111-mason-migrate-et-al-fixes-v1-3-3d2454362e8d@xxxxxxxxxx/
>
> After the patch, the xarray walk code looks very similar to
> the filemap_cachestat() one. So
>
> Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
>

Thank!

> Later, we might want to come up with a common helper for them.
Yeah, some pagecache handling code is quite unintuitive ... I think some of that
needs a real rework.

--
Cheers,

David