mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Xu Xin (vivo)" <xu.xin@linux.dev>, Qiliang Yuan <odys.yuan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Chengming Zhou <chengming.zhou@linux.dev>,
	Stefan Roesch <shr@devkernel.io>,
	Lorenzo Stoakes <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>, Shuah Khan <shuah@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH v2 1/2] mm/ksm: align smart scan skips with the scan number
Date: Fri, 6 Nov 2026 13:44:31 +0100	[thread overview]
Message-ID: <fb9d1040-7d1e-4b72-b600-f80b6be3a0b3@kernel.org> (raw)
In-Reply-To: <au2hqxeIz0l/DusZ@dgvxl46587.localhost.localdomain>

On 11/6/26 10:45, Xu Xin (vivo) wrote:
> On Thu, Nov 05, 2026 at 04:36:14AM -0500, 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@gmail.com>
>> ---
>>  mm/ksm.c | 24 ++++++++++--------------
>>  1 file changed, 10 insertions(+), 14 deletions(-)
> 
> Brilliant design.
> By using the global ksm_scan.seqnr as a unified time base, this patch eliminates
> the out-of-sync problem of the original per-page skip counters.
> 
> It guarantees that any two pages will be processed in the same scan at least once
> every 8 scans — the least common multiple of all scan periods — which completely
> resolves the permanent de-duplication failure.
> 
> Admittedly, this comes at the cost of shrinking the longest scan interval from 9
> to 8 scans, raising the scan frequency for aged pages by roughly 12.5%. But I
> believe this is a very reasonable trade-off for correctness. So
> 
> Reviewed-by: Xu Xin (vivo) <xu.xin@linux.dev>
> 
> 
> Besides, can we add a extra description about new smart_scan algorithm that we
> missed in Documentation/mm/ksm.rst?

Agreed, that would be nice. Thanks!

-- 
Cheers,

David

  reply	other threads:[~2026-11-06 12:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05  9:36 [PATCH v2 0/2] mm/ksm: keep smart scan from losing duplicate pages Qiliang Yuan
2026-11-05  9:36 ` [PATCH v2 1/2] mm/ksm: align smart scan skips with the scan number Qiliang Yuan
2026-11-05 10:03   ` David Hildenbrand (Arm)
2026-11-06  9:45   ` Xu Xin (vivo)
2026-11-06 12:44     ` David Hildenbrand (Arm) [this message]
2026-11-05  9:36 ` [PATCH v2 2/2] selftests/mm: cover merging KSM pages aged by smart scan Qiliang Yuan
2026-11-05 10:03   ` David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fb9d1040-7d1e-4b72-b600-f80b6be3a0b3@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=odys.yuan@gmail.com \
    --cc=rppt@kernel.org \
    --cc=shr@devkernel.io \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=xu.xin@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®