The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "Xu Xin (vivo)" <xu.xin@linux.dev>
Cc: chengming.zhou@linux.dev, david@kernel.org,
	 linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH v3 2/2] mm/ksm: add initialization helpers for scan_get_next_rmap_item()
Date: Mon, 16 Nov 2026 14:14:37 +0000	[thread overview]
Message-ID: <avsOr-03NtFVG-6a@gremlin> (raw)
In-Reply-To: <20261116014655.310486-1-xu.xin@linux.dev>

On Mon, Nov 16, 2026 at 09:46:55AM +0800, Xu Xin (vivo) wrote:
> Extract the new scan cycle check into scan_should_start_new_cycle()
> and add the helper scan_get_first_mm() that advances the scan
> cursor to the first mm_slot.
>
> 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@linux.dev>

Nits below but otherwise LGTM, with those addressed:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

Thanks!

> ---
> Changes in v3:
> - Initialize mm_slot = ksm_scan.mm_slot in the else branch when continuing
>   the current scan cycle, fixing an uninitialized variable warning/bug
>   (kernel test robot).
> - Drop Lorenzo's Reviewed-by tag due to the v2 bug fix.
>
> Changes in v2:
> - Rename scan_begin_new_cycle() to scan_should_start_new_cycle() and keep
>   it simple (David).
> - Keep lru_add_drain_all() in scan_get_next_rmap_item() (David).
> - Use scoped_guard(spinlock, &ksm_mmlist_lock) in scan_get_first_mm()
>   (Lorenzo).
>
>  mm/ksm.c | 47 ++++++++++++++++++++++++++++++++++-------------
>  1 file changed, 34 insertions(+), 13 deletions(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 8db6743ece6d..89a3da4a4874 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -2638,6 +2638,34 @@ static void ksm_reset_unstable_trees(void)
>  		root_unstable_tree[nid] = RB_ROOT;
>  }
>
> +static bool scan_should_start_new_cycle(void)
> +{
> +	/* Check if the cursor points to the head. */

Probably this comment is redundant, I think the code already makes this
clear.

> +	return ksm_scan.mm_slot == &ksm_mm_head;
> +}
> +
> +static struct ksm_mm_slot *scan_get_first_mm(void)
> +{
> +	struct ksm_mm_slot *mm_slot = &ksm_mm_head;
> +	struct mm_slot *slot;
> +
> +	scoped_guard(spinlock, &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

I think you need to update this comment to reflect the fact that you moved the
list_empty() check :)

> +	 * 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;
> @@ -2650,8 +2678,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
>  	if (list_empty(&ksm_mm_head.slot.mm_node))
>  		return NULL;
>
> -	mm_slot = ksm_scan.mm_slot;
> -	if (mm_slot == &ksm_mm_head) {
> +	if (scan_should_start_new_cycle()) {
>  		advisor_start_scan();
>  		trace_ksm_start_scan(ksm_scan.seqnr, ksm_rmap_items);
>
> @@ -2670,21 +2697,15 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)
>  		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;
>  		ksm_scan.rmap_list = &mm_slot->rmap_list;
> +	} else {
> +		/* Continue scanning the current mm. */
> +		mm_slot = ksm_scan.mm_slot;
>  	}
>
>  	slot = &mm_slot->slot;
> --
> 2.34.1
>

--
Cheers, Lorenzo

      reply	other threads:[~2026-11-16 14:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  1:44 [PATCH v3 0/2] mm/ksm: clean up scan_get_next_rmap_item() initialization Xu Xin (vivo)
2026-11-16  1:45 ` [PATCH v3 1/2] mm/ksm: extract helper functions for scan cycle cleanup Xu Xin (vivo)
2026-11-16  1:46 ` [PATCH v3 2/2] mm/ksm: add initialization helpers for scan_get_next_rmap_item() Xu Xin (vivo)
2026-11-16 14:14   ` Lorenzo Stoakes (ARM) [this message]

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=avsOr-03NtFVG-6a@gremlin \
    --to=ljs@kernel.org \
    --cc=chengming.zhou@linux.dev \
    --cc=david@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.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