mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm/ksm: mark migration stores with WRITE_ONCE()
@ 2026-08-22 16:38 Chengfeng Ye
  2026-08-23 13:10 ` xu.xin16
  2026-08-24 10:39 ` David Hildenbrand (Arm)
  0 siblings, 2 replies; 4+ messages in thread
From: Chengfeng Ye @ 2026-08-22 16:38 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Xu Xin, Chengming Zhou, Hugh Dickins
  Cc: linux-mm, linux-kernel, Chengfeng Ye

ksm_get_folio() deliberately samples stable_node->kpfn and
folio->mapping without taking the folio lock because the KSM folio may be
migrated concurrently.  folio_migrate_ksm() updates the same state using
plain assignments.

The reader can load the old kpfn, then the migrator can store the new kpfn,
execute smp_wmb(), and clear the old folio's mapping before the reader
checks that mapping.  Thus the initial kpfn load can overlap its update and
the subsequent mapping load can overlap the clear, with no common lock.
This leaves marked READ_ONCE() accesses racing with plain stores.

The kernel reported:

  BUG: KCSAN: data-race in folio_migrate_ksm / ksm_get_folio

  read (marked) to 0xffff8ce401421330 of 8 bytes by task 48 on cpu 3:
   ksm_get_folio+0x7f/0x2a0
   ksm_scan_thread+0x1635/0x3330
   kthread+0x1af/0x1f0

  write to 0xffff8ce401421330 of 8 bytes by task 102 on cpu 1:
   folio_migrate_ksm+0x6a/0xd0
   folio_migrate_flags+0x193/0x420
   __migrate_folio.isra.0+0x162/0x1a0
   migrate_folio+0x4c/0x70
   move_to_new_folio+0xd6/0x170

Use WRITE_ONCE() for both stores to pair them with the existing lockless
reads.  This preserves the existing smp_wmb()/smp_rmb() migration protocol
and control flow while preventing compiler transformations of the shared
accesses.

Signed-off-by: Chengfeng Ye <nicoyip.dev@gmail.com>
---
 mm/ksm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index b4142746777e..bec6fea0fdb4 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1116,7 +1116,8 @@ static inline void folio_set_stable_node(struct folio *folio,
 					 struct ksm_stable_node *stable_node)
 {
 	VM_WARN_ON_FOLIO(folio_test_anon(folio) && PageAnonExclusive(&folio->page), folio);
-	folio->mapping = (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM);
+	WRITE_ONCE(folio->mapping,
+		   (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM));
 }
 
 #ifdef CONFIG_SYSFS
@@ -3318,7 +3319,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio)
 	stable_node = folio_stable_node(folio);
 	if (stable_node) {
 		VM_BUG_ON_FOLIO(stable_node->kpfn != folio_pfn(folio), folio);
-		stable_node->kpfn = folio_pfn(newfolio);
+		WRITE_ONCE(stable_node->kpfn, folio_pfn(newfolio));
 		/*
 		 * newfolio->mapping was set in advance; now we need smp_wmb()
 		 * to make sure that the new stable_node->kpfn is visible
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/ksm: mark migration stores with WRITE_ONCE()
  2026-08-22 16:38 [PATCH] mm/ksm: mark migration stores with WRITE_ONCE() Chengfeng Ye
@ 2026-08-23 13:10 ` xu.xin16
  2026-08-24 10:39 ` David Hildenbrand (Arm)
  1 sibling, 0 replies; 4+ messages in thread
From: xu.xin16 @ 2026-08-23 13:10 UTC (permalink / raw)
  To: nicoyip.dev
  Cc: akpm, david, chengming.zhou, hughd, linux-mm, linux-kernel, nicoyip.dev

> Use WRITE_ONCE() for both stores to pair them with the existing lockless
> reads.  This preserves the existing smp_wmb()/smp_rmb() migration protocol
> and control flow while preventing compiler transformations of the shared
> accesses.
> 
> Signed-off-by: Chengfeng Ye <nicoyip.dev@gmail.com>
> ---
>  mm/ksm.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/ksm.c b/mm/ksm.c
> index b4142746777e..bec6fea0fdb4 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -1116,7 +1116,8 @@ static inline void folio_set_stable_node(struct folio *folio,
>  					 struct ksm_stable_node *stable_node)
>  {
>  	VM_WARN_ON_FOLIO(folio_test_anon(folio) && PageAnonExclusive(&folio->page), folio);
> -	folio->mapping = (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM);
> +	WRITE_ONCE(folio->mapping,
> +		   (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM));
>  }

This is good.

>  
>  #ifdef CONFIG_SYSFS
> @@ -3318,7 +3319,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio)
>  	stable_node = folio_stable_node(folio);
>  	if (stable_node) {
>  		VM_BUG_ON_FOLIO(stable_node->kpfn != folio_pfn(folio), folio);
> -		stable_node->kpfn = folio_pfn(newfolio);
> +		WRITE_ONCE(stable_node->kpfn, folio_pfn(newfolio));
>  		/*
>  		 * newfolio->mapping was set in advance; now we need smp_wmb()
>  		 * to make sure that the new stable_node->kpfn is visible
> -- 
> 2.43.0
> 

There are other places where READ_ONCE is not used like in ksm_check_stable_tree() and
stable_node_dup_remove_range(), but these two function belong to MEM_OFFLINE, and
I think there should be no races between migrate and ksm_memory_callback of MEM_OFFLINE.

So Basically it looks good to me.

Acked-by: Xu Xin <xu.xin16@zte.com.cn>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/ksm: mark migration stores with WRITE_ONCE()
  2026-08-22 16:38 [PATCH] mm/ksm: mark migration stores with WRITE_ONCE() Chengfeng Ye
  2026-08-23 13:10 ` xu.xin16
@ 2026-08-24 10:39 ` David Hildenbrand (Arm)
  2026-08-24 11:26   ` Chengfeng Ye
  1 sibling, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-08-24 10:39 UTC (permalink / raw)
  To: Chengfeng Ye, Andrew Morton, Xu Xin, Chengming Zhou, Hugh Dickins
  Cc: linux-mm, linux-kernel

On 8/22/26 18:38, Chengfeng Ye wrote:
> ksm_get_folio() deliberately samples stable_node->kpfn and
> folio->mapping without taking the folio lock because the KSM folio may be
> migrated concurrently.  folio_migrate_ksm() updates the same state using
> plain assignments.
> 
> The reader can load the old kpfn, then the migrator can store the new kpfn,
> execute smp_wmb(), and clear the old folio's mapping before the reader
> checks that mapping.  Thus the initial kpfn load can overlap its update and
> the subsequent mapping load can overlap the clear, with no common lock.
> This leaves marked READ_ONCE() accesses racing with plain stores.
> 
> The kernel reported:
> 
>   BUG: KCSAN: data-race in folio_migrate_ksm / ksm_get_folio
> 
>   read (marked) to 0xffff8ce401421330 of 8 bytes by task 48 on cpu 3:
>    ksm_get_folio+0x7f/0x2a0
>    ksm_scan_thread+0x1635/0x3330
>    kthread+0x1af/0x1f0
> 
>   write to 0xffff8ce401421330 of 8 bytes by task 102 on cpu 1:
>    folio_migrate_ksm+0x6a/0xd0
>    folio_migrate_flags+0x193/0x420
>    __migrate_folio.isra.0+0x162/0x1a0
>    migrate_folio+0x4c/0x70
>    move_to_new_folio+0xd6/0x170
> 
> Use WRITE_ONCE() for both stores to pair them with the existing lockless
> reads.  This preserves the existing smp_wmb()/smp_rmb() migration protocol
> and control flow while preventing compiler transformations of the shared
> accesses.
> 

We want a Fixes: tag, can you dig?

I am not convinced CC stable is warranted ... but certainly wouldn't hurt here.

> Signed-off-by: Chengfeng Ye <nicoyip.dev@gmail.com>
> ---
>  mm/ksm.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/ksm.c b/mm/ksm.c
> index b4142746777e..bec6fea0fdb4 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -1116,7 +1116,8 @@ static inline void folio_set_stable_node(struct folio *folio,
>  					 struct ksm_stable_node *stable_node)
>  {
>  	VM_WARN_ON_FOLIO(folio_test_anon(folio) && PageAnonExclusive(&folio->page), folio);
> -	folio->mapping = (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM);
> +	WRITE_ONCE(folio->mapping,
> +		   (void *)((unsigned long)stable_node | FOLIO_MAPPING_KSM));

That also matches what we do in folio_move_anon_rmap()/__folio_set_anon().

>  }
>  
>  #ifdef CONFIG_SYSFS
> @@ -3318,7 +3319,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio)
>  	stable_node = folio_stable_node(folio);
>  	if (stable_node) {
>  		VM_BUG_ON_FOLIO(stable_node->kpfn != folio_pfn(folio), folio);
> -		stable_node->kpfn = folio_pfn(newfolio);
> +		WRITE_ONCE(stable_node->kpfn, folio_pfn(newfolio));

That makes sense as well!

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/ksm: mark migration stores with WRITE_ONCE()
  2026-08-24 10:39 ` David Hildenbrand (Arm)
@ 2026-08-24 11:26   ` Chengfeng Ye
  0 siblings, 0 replies; 4+ messages in thread
From: Chengfeng Ye @ 2026-08-24 11:26 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Andrew Morton, Xu Xin, Chengming Zhou, Hugh Dickins, linux-mm,
	linux-kernel

On Mon, Aug 24, 2026 at 6:39 PM David Hildenbrand (Arm)
<david@kernel.org> wrote:
> We want a Fixes: tag, can you dig?
>
> I am not convinced CC stable is warranted ... but certainly wouldn't hurt here.

No problem, v2 is sent to add the CC stable and a fix tag. Thanks all
for your review!

Best regards,
Chengfeng

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-24 11:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-22 16:38 [PATCH] mm/ksm: mark migration stores with WRITE_ONCE() Chengfeng Ye
2026-08-23 13:10 ` xu.xin16
2026-08-24 10:39 ` David Hildenbrand (Arm)
2026-08-24 11:26   ` Chengfeng Ye

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®