Re: [PATCH 2/2] mm/ksm: add initialization helpers for scan_get_next_rmap_item()
From: Lorenzo Stoakes (ARM)
Date: Fri Nov 13 2026 - 09:22:47 EST
On Thu, Nov 12, 2026 at 08:59:59PM +0800, Xu Xin (vivo) wrote:
> Extract the new scan cycle detection (including draining per-CPU LRU
> caches) and advancing the scan cursor to the first mm_slot into
> scan_begin_new_cycle() and scan_get_first_mm().
>
> This keeps the exact execution order intact while making
> scan_get_next_rmap_item() cleaner and easier to follow.
>
> No functional change intended.
>
> Signed-off-by: Xu Xin (vivo) <xu.xin@xxxxxxxxx>
Again, thanks for doing this! LGTM and:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---
> mm/ksm.c | 70 +++++++++++++++++++++++++++++++++++++-------------------
> 1 file changed, 46 insertions(+), 24 deletions(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 49977bcf04f7..e79e64372e49 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -2640,6 +2640,49 @@ static void ksm_reset_unstable_trees(void)
> root_unstable_tree[nid] = RB_ROOT;
> }
>
> +static bool scan_begin_new_cycle(void)
> +{
> + /* Check if the cursor points to the head. */
> + if (ksm_scan.mm_slot != &ksm_mm_head)
> + return false;
> +
> + /*
> + * A number of pages can hang around indefinitely in per-cpu
> + * LRU cache, raised page count preventing write_protect_page
> + * from merging them. Though it doesn't really matter much,
> + * it is puzzling to see some stuck in pages_volatile until
> + * other activity jostles them out, and they also prevented
> + * LTP's KSM test from succeeding deterministically; so drain
> + * them here (here rather than on entry to ksm_do_scan(),
> + * so we don't IPI too often when pages_to_scan is set low).
> + */
> + lru_add_drain_all();
The sashiko finding is silly, this reorders it after advisor_start_scan() which
calls... ktime_get(), yeah it's zero delta :)
> + return true;
> +}
> +
> +static struct ksm_mm_slot *scan_get_first_mm(void)
> +{
> + struct ksm_mm_slot *mm_slot = ksm_scan.mm_slot;
> + struct mm_slot *slot;
> +
> + spin_lock(&ksm_mmlist_lock);
> +
> + slot = list_entry(mm_slot->slot.mm_node.next, struct mm_slot, mm_node);
> + mm_slot = mm_slot_entry(slot, struct ksm_mm_slot, slot);
> + ksm_scan.mm_slot = mm_slot;
> +
> + spin_unlock(&ksm_mmlist_lock);
NIT: optional, but this could be:
scoped_guard(mutex, &ksm_mmlist_lock) {
slot = list_entry(mm_slot->slot.mm_node.next, struct mm_slot, mm_node);
mm_slot = mm_slot_entry(slot, struct ksm_mm_slot, slot);
ksm_scan.mm_slot = mm_slot;
}
> +
> + /*
> + * Although we tested list_empty() above, a racing __ksm_exit
> + * of the last mm on the list may have removed it since then.
> + */
> + if (mm_slot == &ksm_mm_head)
> + return NULL;
> +
> + return mm_slot;
> +}
> +
> static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
> {
> struct mm_struct *mm;
> @@ -2653,36 +2696,15 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
> return NULL;
>
> mm_slot = ksm_scan.mm_slot;
> - if (mm_slot == &ksm_mm_head) {
> + if (scan_begin_new_cycle()) {
> advisor_start_scan();
> trace_ksm_start_scan(ksm_scan.seqnr, ksm_rmap_items);
>
> - /*
> - * A number of pages can hang around indefinitely in per-cpu
> - * LRU cache, raised page count preventing write_protect_page
> - * from merging them. Though it doesn't really matter much,
> - * it is puzzling to see some stuck in pages_volatile until
> - * other activity jostles them out, and they also prevented
> - * LTP's KSM test from succeeding deterministically; so drain
> - * them here (here rather than on entry to ksm_do_scan(),
> - * so we don't IPI too often when pages_to_scan is set low).
> - */
> - lru_add_drain_all();
> -
> ksm_prune_migrate_nodes();
> ksm_reset_unstable_trees();
>
> - spin_lock(&ksm_mmlist_lock);
> - slot = list_entry(mm_slot->slot.mm_node.next,
> - struct mm_slot, mm_node);
> - mm_slot = mm_slot_entry(slot, struct ksm_mm_slot, slot);
> - ksm_scan.mm_slot = mm_slot;
> - spin_unlock(&ksm_mmlist_lock);
> - /*
> - * Although we tested list_empty() above, a racing __ksm_exit
> - * of the last mm on the list may have removed it since then.
> - */
> - if (mm_slot == &ksm_mm_head)
> + mm_slot = scan_get_first_mm();
> + if (!mm_slot)
> return NULL;
> next_mm:
> ksm_scan.address = 0;
> --
> 2.34.1
>
--
Cheers, Lorenzo