Linux CXL
 help / color / mirror / Atom feed
* [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration
@ 2026-05-14  9:26 Georgi Djakov
  2026-05-14 10:08 ` Richard Cheng
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Georgi Djakov @ 2026-05-14  9:26 UTC (permalink / raw)
  To: akpm, david
  Cc: linux-mm, linux-cxl, osalvador, gregkh, rafael, georgi.djakov,
	djakov

If __add_memory_block() fails at xa_store() (under memory pressure
for example), device_unregister() is called, which eventually
triggers memory_block_release() with mem->altmap still set, causing
a WARN_ON(mem->altmap). This was triggered by modifying virtio-mem
driver.

Fix this by delaying the assignment of mem->altmap until after
__add_memory_block() has succeeded.

Fixes: 1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block")
Signed-off-by: Georgi Djakov <georgi.djakov@oss.qualcomm.com>
---

v1: https://patch.msgid.link/20260513130900.2204499-1-georgi.djakov@oss.qualcomm.com

 drivers/base/memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 11d57cfa8d72..78e9600e6d45 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -807,7 +807,6 @@ static int add_memory_block(unsigned long block_id, int nid, unsigned long state
 	mem->start_section_nr = block_id * sections_per_block;
 	mem->state = state;
 	mem->nid = nid;
-	mem->altmap = altmap;
 	INIT_LIST_HEAD(&mem->group_next);
 
 #ifndef CONFIG_NUMA
@@ -825,6 +824,8 @@ static int add_memory_block(unsigned long block_id, int nid, unsigned long state
 	if (ret)
 		return ret;
 
+	mem->altmap = altmap;
+
 	if (group) {
 		mem->group = group;
 		list_add(&mem->group_next, &group->memory_blocks);

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

* Re: [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration
  2026-05-14  9:26 [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration Georgi Djakov
@ 2026-05-14 10:08 ` Richard Cheng
  2026-05-17 20:06 ` Oscar Salvador (SUSE)
  2026-05-19 20:25 ` Andrew Morton
  2 siblings, 0 replies; 6+ messages in thread
From: Richard Cheng @ 2026-05-14 10:08 UTC (permalink / raw)
  To: Georgi Djakov
  Cc: akpm, david, linux-mm, linux-cxl, osalvador, gregkh, rafael,
	djakov

On Thu, May 14, 2026 at 02:26:57AM +0800, Georgi Djakov wrote:
> If __add_memory_block() fails at xa_store() (under memory pressure
> for example), device_unregister() is called, which eventually
> triggers memory_block_release() with mem->altmap still set, causing
> a WARN_ON(mem->altmap). This was triggered by modifying virtio-mem
> driver.
>

I see you mentioned about some out of tree virtio_mem patches that
can trigger the bug, mind to paste the tree link here ?
Just curious.

Best regards,
Richard Cheng.
 
> Fix this by delaying the assignment of mem->altmap until after
> __add_memory_block() has succeeded.
> 
> Fixes: 1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block")
> Signed-off-by: Georgi Djakov <georgi.djakov@oss.qualcomm.com>
> ---
> 
> v1: https://patch.msgid.link/20260513130900.2204499-1-georgi.djakov@oss.qualcomm.com
> 
>  drivers/base/memory.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 11d57cfa8d72..78e9600e6d45 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -807,7 +807,6 @@ static int add_memory_block(unsigned long block_id, int nid, unsigned long state
>  	mem->start_section_nr = block_id * sections_per_block;
>  	mem->state = state;
>  	mem->nid = nid;
> -	mem->altmap = altmap;
>  	INIT_LIST_HEAD(&mem->group_next);
>  
>  #ifndef CONFIG_NUMA
> @@ -825,6 +824,8 @@ static int add_memory_block(unsigned long block_id, int nid, unsigned long state
>  	if (ret)
>  		return ret;
>  
> +	mem->altmap = altmap;
> +
>  	if (group) {
>  		mem->group = group;
>  		list_add(&mem->group_next, &group->memory_blocks);
> 

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

* Re: [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration
  2026-05-14  9:26 [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration Georgi Djakov
  2026-05-14 10:08 ` Richard Cheng
@ 2026-05-17 20:06 ` Oscar Salvador (SUSE)
  2026-05-19 20:25 ` Andrew Morton
  2 siblings, 0 replies; 6+ messages in thread
From: Oscar Salvador (SUSE) @ 2026-05-17 20:06 UTC (permalink / raw)
  To: Georgi Djakov
  Cc: akpm, david, linux-mm, linux-cxl, osalvador, gregkh, rafael,
	djakov

On Thu, May 14, 2026 at 02:26:57AM -0700, Georgi Djakov wrote:
> If __add_memory_block() fails at xa_store() (under memory pressure
> for example), device_unregister() is called, which eventually
> triggers memory_block_release() with mem->altmap still set, causing
> a WARN_ON(mem->altmap). This was triggered by modifying virtio-mem
> driver.
> 
> Fix this by delaying the assignment of mem->altmap until after
> __add_memory_block() has succeeded.
> 
> Fixes: 1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block")
> Signed-off-by: Georgi Djakov <georgi.djakov@oss.qualcomm.com>

Acked-by: Oscar Salvador (SUSE) <osalvador@kernel.org>



-- 
Oscar Salvador
SUSE Labs

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

* Re: [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration
  2026-05-14  9:26 [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration Georgi Djakov
  2026-05-14 10:08 ` Richard Cheng
  2026-05-17 20:06 ` Oscar Salvador (SUSE)
@ 2026-05-19 20:25 ` Andrew Morton
  2026-05-19 20:57   ` Georgi Djakov
  2 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2026-05-19 20:25 UTC (permalink / raw)
  To: Georgi Djakov
  Cc: david, linux-mm, linux-cxl, osalvador, gregkh, rafael, djakov,
	Mike Rapoport, Vishal Verma

On Thu, 14 May 2026 02:26:57 -0700 Georgi Djakov <georgi.djakov@oss.qualcomm.com> wrote:

> If __add_memory_block() fails at xa_store() (under memory pressure
> for example), device_unregister() is called, which eventually
> triggers memory_block_release() with mem->altmap still set, causing
> a WARN_ON(mem->altmap).

That isn't good.

> This was triggered by modifying virtio-mem
> driver.
> 
> Fix this by delaying the assignment of mem->altmap until after
> __add_memory_block() has succeeded.
> 
> Fixes: 1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block")

Patch is nice and simple so I'll add a cc:stable to this, even though
the issue seems rather minor.

btw, AI review had no issue with your change but might have found a bug
in create_altmaps_and_memory_blocks():

	https://sashiko.dev/#/patchset/20260514092657.3057141-1-georgi.djakov@oss.qualcomm.com



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

* Re: [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration
  2026-05-19 20:25 ` Andrew Morton
@ 2026-05-19 20:57   ` Georgi Djakov
  2026-05-19 21:08     ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Georgi Djakov @ 2026-05-19 20:57 UTC (permalink / raw)
  To: Andrew Morton, Georgi Djakov
  Cc: david, linux-mm, linux-cxl, osalvador, gregkh, rafael,
	Mike Rapoport, Vishal Verma

On 5/19/26 11:25 PM, Andrew Morton wrote:
> On Thu, 14 May 2026 02:26:57 -0700 Georgi Djakov <georgi.djakov@oss.qualcomm.com> wrote:
> 
>> If __add_memory_block() fails at xa_store() (under memory pressure
>> for example), device_unregister() is called, which eventually
>> triggers memory_block_release() with mem->altmap still set, causing
>> a WARN_ON(mem->altmap).
> 
> That isn't good.
> 
>> This was triggered by modifying virtio-mem
>> driver.
>>
>> Fix this by delaying the assignment of mem->altmap until after
>> __add_memory_block() has succeeded.
>>
>> Fixes: 1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block")
> 
> Patch is nice and simple so I'll add a cc:stable to this, even though
> the issue seems rather minor.
> 
> btw, AI review had no issue with your change but might have found a bug
> in create_altmaps_and_memory_blocks():
> 
> 	https://sashiko.dev/#/patchset/20260514092657.3057141-1-georgi.djakov@oss.qualcomm.com

Yes, i have hit exactly this bug actually (shortly after this WARN_ON, we crash)
and was going to send a fix, but noticed that someone already did:
https://lore.kernel.org/lkml/20260428081855.1249045-3-songmuchun@bytedance.com/

I can respond with my RB on that one if you haven't applied it yet.

BR,
Georgi

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

* Re: [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration
  2026-05-19 20:57   ` Georgi Djakov
@ 2026-05-19 21:08     ` Andrew Morton
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2026-05-19 21:08 UTC (permalink / raw)
  To: Georgi Djakov
  Cc: Georgi Djakov, david, linux-mm, linux-cxl, osalvador, gregkh,
	rafael, Mike Rapoport, Vishal Verma

On Tue, 19 May 2026 23:57:52 +0300 Georgi Djakov <djakov@kernel.org> wrote:

> On 5/19/26 11:25 PM, Andrew Morton wrote:
> > On Thu, 14 May 2026 02:26:57 -0700 Georgi Djakov <georgi.djakov@oss.qualcomm.com> wrote:
> > 
> >> If __add_memory_block() fails at xa_store() (under memory pressure
> >> for example), device_unregister() is called, which eventually
> >> triggers memory_block_release() with mem->altmap still set, causing
> >> a WARN_ON(mem->altmap).
> > 
> > That isn't good.
> > 
> >> This was triggered by modifying virtio-mem
> >> driver.
> >>
> >> Fix this by delaying the assignment of mem->altmap until after
> >> __add_memory_block() has succeeded.
> >>
> >> Fixes: 1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block")
> > 
> > Patch is nice and simple so I'll add a cc:stable to this, even though
> > the issue seems rather minor.
> > 
> > btw, AI review had no issue with your change but might have found a bug
> > in create_altmaps_and_memory_blocks():
> > 
> > 	https://sashiko.dev/#/patchset/20260514092657.3057141-1-georgi.djakov@oss.qualcomm.com
> 
> Yes, i have hit exactly this bug actually (shortly after this WARN_ON, we crash)
> and was going to send a fix, but noticed that someone already did:
> https://lore.kernel.org/lkml/20260428081855.1249045-3-songmuchun@bytedance.com/
> 
> I can respond with my RB on that one if you haven't applied it yet.

I have indeed applied it, but a R-b isn't a problem and would be
welcomed.  One of the many reasons why I retain patches in quilt form
for a few weeks before gitifying them.

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

end of thread, other threads:[~2026-05-19 21:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-14  9:26 [PATCH v2] drivers/base/memory: set mem->altmap after successful device registration Georgi Djakov
2026-05-14 10:08 ` Richard Cheng
2026-05-17 20:06 ` Oscar Salvador (SUSE)
2026-05-19 20:25 ` Andrew Morton
2026-05-19 20:57   ` Georgi Djakov
2026-05-19 21:08     ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox