Re: [PATCH v2 1/2] mm/ksm: align smart scan skips with the scan number
From: David Hildenbrand (Arm)
Date: Thu Nov 05 2026 - 05:17:44 EST
On 11/5/26 10:36, Qiliang Yuan wrote:
> Smart scan skips pages that KSM has failed to de-duplicate for a while.
> Once a page is three scans old, its rmap_item counts down its own number
> of scans to skip, 1, 2, 4 and then 8 as it ages, so an old page is looked
> at every ninth scan.
>
> The unstable tree is rebuilt on every scan, so two identical pages that
> are not in the stable tree yet can only be merged in a scan that looks
> at both. Each countdown runs from when its own page became mergeable, so
> two pages that became mergeable at different times end up on different
> phases and, once both are old, may never be looked at in the same scan
> again. They then stay unmerged for as long as they live. Making a page
> mergeable, waiting for 0 to 20 full scans before doing the same with an
> identical page, 20 times for each wait, and checking the pair after
> another 64 full scans shows this, on two boots per kernel:
>
> before after
> pairs merged (of 420) 329 / 317 420 / 420
> 64M of unique pages skipped 89.0% / 89.1% 87.5% / 87.5%
> ksmd CPU time per scan 6.25 / 6.25 ms 6.88 / 7.03 ms
>
> Pick the skipped scans by the global scan number instead, with periods
> of 2, 4 and 8 scans. The periods being powers of two, every old page is
> looked at on each scan that is a multiple of 8, whatever its age, so any
> two pages meet at least every eighth scan. The longest period goes from
> nine scans to eight, which costs 10-12% more ksmd time on memory that
> never merges.
>
> Fixes: 5e924ff54d08 ("mm/ksm: add "smart" page scanning mode")
> Signed-off-by: Qiliang Yuan <odys.yuan@xxxxxxxxx>
> ---
Makes sense to me, without looking to closely into the details.
Maybe Stefan can give some input?
--
Cheers,
David