Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Xu Xin (vivo)" <xu.xin@linux.dev>
Cc: chengming.zhou@linux.dev, david@kernel.org, linux-mm@kvack.org,
	ljs@kernel.org
Subject: Re: [PATCH v2 2/2] mm/ksm: add initialization helpers for scan_get_next_rmap_item()
Date: Mon, 16 Nov 2026 08:01:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-216493@kernel.org> (raw)
In-Reply-To: <20261115080049.132515-1-xu.xin@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm/ksm: uninitialized mm_slot dereference in
  scan_get_next_rmap_item()
--

commit c379de4f41203759cd851dbc9790adb7f3dde0fd
Author: Xu Xin (vivo) <xu.xin@linux.dev>

mm/ksm: add initialization helpers for scan_get_next_rmap_item()

This commit extracts the new scan cycle check into
scan_should_start_new_cycle() and adds scan_get_first_mm() to advance the
scan cursor to the first mm_slot in scan_get_next_rmap_item().

> diff --git a/mm/ksm.c b/mm/ksm.c
> index b81a49c65030b..7d0f4bad53b28 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c

[ ... ]

> @@ -2662,8 +2690,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);
>  
> @@ -2682,17 +2709,8 @@ 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;

[Severity: High]
Does removing mm_slot = ksm_scan.mm_slot leave mm_slot uninitialized when
a scan cycle is already in progress?

mm_slot is now only assigned inside the scan_should_start_new_cycle()
block:

mm/ksm.c:scan_get_next_rmap_item() {
	struct mm_struct *mm;
	struct ksm_mm_slot *mm_slot;
    ...
	if (scan_should_start_new_cycle()) {
    ...
		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;
	}

	slot = &mm_slot->slot;
	mm = slot->mm;
	vma_iter_init(&vmi, mm, ksm_scan.address);

	mmap_read_lock(mm);
    ...
}

On the second call to scan_get_next_rmap_item() in a scan cycle,
scan_should_start_new_cycle() returns false and execution proceeds to
slot = &mm_slot->slot; mm = slot->mm; with an uninitialized mm_slot
pointer, causing a kernel crash in ksmd as well as a
-Wmaybe-uninitialized build warning.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261115075832.132109-1-xu.xin@linux.dev?part=2


      parent reply	other threads:[~2026-11-16  8:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15  7:58 [PATCH v2 0/2] mm/ksm: clean up scan_get_next_rmap_item() initialization Xu Xin (vivo)
2026-11-15  8:00 ` [PATCH v2 1/2] mm/ksm: extract helper functions for scan cycle cleanup Xu Xin (vivo)
2026-11-15  8:00 ` [PATCH v2 2/2] mm/ksm: add initialization helpers for scan_get_next_rmap_item() Xu Xin (vivo)
2026-11-15 15:28   ` kernel test robot
2026-11-16  8:01   ` sashiko-bot [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=sashiko-outbox-216493@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=chengming.zhou@linux.dev \
    --cc=david@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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