drivers/base/memory: fix locking for poison accounting lookup
Quoted 35 lines · Miaohe Lin
Quoted 31 lines · Muchun Song
Quoted 22 lines · Miaohe Lin
Quoted 8 lines · Muchun Song
memblk_nr_poison_inc()andmemblk_nr_poison_sub()callfind_memory_block_by_id(), which requiresdevice_hotplug_lockto serialize the xarray lookup against memory block removal. Takedevice_hotplug_lockaround the lookup andnr_hwpoisonupdate so the memory block cannot disappear betweenxa_load()andget_device().Fixes 5033091de814 ("mm/hwpoison: introduce per-memory_block hwpoison counter")Signed-off-by Muchun Song <[email protected]>Thanks for update.
Quoted 9 lines
---
drivers/base/memory.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/base/memory.c +0
-0@@ -1228,23 +1228,29 @@ int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,void memblk_nr_poison_inc (unsigned long pfn)Quoted 8 lines
drivers/base/memory.c int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, · line 1232
Quoted 5 lines
drivers/base/memory.c +2
-1{const unsigned long block_id =pfn_to_block_id (pfn); -struct memory_block *mem =find_memory_block_by_id (block_id); +struct memory_block *mem; +lock_device_hotplug ();
memblk_nr_poison_inc()andmemblk_nr_poison_sub()are both called frommemory_failure()context. I'm afraid ifmemory_failure()is triggered whilelock_device_hotplugis held, it will lead to deadlock. Or am I miss something?Quoted 1 line
I am curious is there any place where
memory_failure()is called with holdinglock_device_hotplug?
Quoted 7 lines
Sorry for dumb scenario, I was a bit too presumptuous. But there might be another possible deadlock:
remove_memory
lock_device_hotplug<-- first called here
try_remove_memory
remove_memory_block_devices
num_poisoned_pages_sub
Passing pfn = -1 here.
Quoted 2 lines
memblk_nr_poison_sub
lock_device_hotplug<-- deadlock here
No. Can’t reach here. No deadlock.
Thanks.
Quoted 5 lines
Hope I'm not mistaken again. :)
Thank.
.
Quoted 46 lines · Muchun Song
Quoted 38 lines · Miaohe Lin
Quoted 31 lines · Muchun Song
Quoted 22 lines · Miaohe Lin
Quoted 8 lines · Muchun Song
memblk_nr_poison_inc()andmemblk_nr_poison_sub()callfind_memory_block_by_id(), which requiresdevice_hotplug_lockto serialize the xarray lookup against memory block removal. Takedevice_hotplug_lockaround the lookup andnr_hwpoisonupdate so the memory block cannot disappear betweenxa_load()andget_device().Fixes 5033091de814 ("mm/hwpoison: introduce per-memory_block hwpoison counter")Signed-off-by Muchun Song <[email protected]>Thanks for update.
Quoted 9 lines
---
drivers/base/memory.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)drivers/base/memory.c +0
-0@@ -1228,23 +1228,29 @@ int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,void memblk_nr_poison_inc (unsigned long pfn)Quoted 8 lines
drivers/base/memory.c int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, · line 1232
Quoted 5 lines
drivers/base/memory.c +2
-1{const unsigned long block_id =pfn_to_block_id (pfn); -struct memory_block *mem =find_memory_block_by_id (block_id); +struct memory_block *mem; +lock_device_hotplug ();
memblk_nr_poison_inc()andmemblk_nr_poison_sub()are both called frommemory_failure()context. I'm afraid ifmemory_failure()is triggered whilelock_device_hotplugis held, it will lead to deadlock. Or am I miss something?Quoted 1 line
I am curious is there any place where
memory_failure()is called with holdinglock_device_hotplug?Quoted 7 lines
Sorry for dumb scenario, I was a bit too presumptuous. But there might be another possible deadlock:
remove_memory
lock_device_hotplug<-- first called here
try_remove_memory
remove_memory_block_devices
num_poisoned_pages_sub
Quoted 6 lines
Passing pfn = -1 here.
Quoted 2 lines
memblk_nr_poison_sub
lock_device_hotplug<-- deadlock hereNo. Can’t reach here. No deadlock.
Right, I missed that. Thanks. But I'm still worried that there might be potential issues. For example, this function could be called while lock_page is held. Acquiring lock_device_hotplug while already holding lock_page might cause problems, though I haven't seen any specific issues yet. Also there might be some other potential scenarios that haven't been considered. Hope I'm just overthinking it. :)
Thanks.
.
Quoted 5 lines · Miaohe Lin
Right, I missed that. Thanks. But I'm still worried that there might be potential issues. For example, this function could be called while
lock_pageis held. Acquiringlock_device_hotplugwhile already holdinglock_pagemight cause problems, though I haven't seen any specific issues yet. Also there might be some other potential scenarios that haven't been considered. Hope I'm just overthinking it. :)
lock_device_hotplug is a mutex lock, and we already take other mutex locks while holding lock_folio in other paths, so I am not sure I see what should be special in this case.
Signature · 2 lines
Oscar Salvador SUSE Labs
Quoted 11 lines · Oscar Salvador
Quoted 5 lines · Miaohe Lin
Right, I missed that. Thanks. But I'm still worried that there might be potential issues. For example, this function could be called while
lock_pageis held. Acquiringlock_device_hotplugwhile already holdinglock_pagemight cause problems, though I haven't seen any specific issues yet. Also there might be some other potential scenarios that haven't been considered. Hope I'm just overthinking it. :)
Quoted 3 lines
lock_device_hotplugis a mutex lock, and we already take other mutex locks while holdinglock_folioin other paths, so I am not sure I see what should be special in this case.
Hi Oscar and Miaohe,
I saw sashiko's report [1] related to folio lock and lock_device_hotplug. Seems it is possible. You can correct me if I am wrong.
[1] https://sashiko.dev/#/patchset/20260428085219.1316047-1-songmuchun%40bytedance.com
We could fix this by calling action_result() without holding folio lock. What do you think?
mm/memory-failure.c +4 -4
@@ -2534,8 +2534,9 @@ int memory_failure(unsigned long pfn, int flags)* Abort on fail: __filemap_remove_folio() assumes unmapped page. */ if (!hwpoison_user_mappings (folio, p, pfn, flags)) { +folio_unlock (folio); res =action_result (pfn,MF_MSG_UNMAP_FAILED ,MF_FAILED ); - goto unlock_page; + goto unlock_mutex; }/*
@@ -2543,16 +2544,15 @@ int memory_failure(unsigned long pfn, int flags)*/ if (folio_test_lru (folio) && !folio_test_swapcache (folio) && folio->mapping ==NULL ) { +folio_unlock (folio); res =action_result (pfn,MF_MSG_TRUNCATED_LRU ,MF_IGNORED ); - goto unlock_page; + goto unlock_mutex; } identify_page_state: res =identify_page_state (pfn, p, page_flags);mutex_unlock (&mf_mutex);return res; -unlock_page: -folio_unlock (folio); unlock_mutex:mutex_unlock (&mf_mutex);return res;
Quoted 5 lines
Signature · 3 lines
-- Oscar Salvador SUSE Labs
Quoted 17 lines · On Wed, 29 Apr 2026 12:18:08 +0800 Muchun Song <[email protected]> wrote:
Quoted 11 lines · Oscar Salvador
Quoted 5 lines · Miaohe Lin
Right, I missed that. Thanks. But I'm still worried that there might be potential issues. For example, this function could be called while
lock_pageis held. Acquiringlock_device_hotplugwhile already holdinglock_pagemight cause problems, though I haven't seen any specific issues yet. Also there might be some other potential scenarios that haven't been considered. Hope I'm just overthinking it. :)Quoted 3 lines
lock_device_hotplugis a mutex lock, and we already take other mutex locks while holdinglock_folioin other paths, so I am not sure I see what should be special in this case.Hi Oscar and Miaohe,
Quoted 8 lines
I saw sashiko's report [1] related to folio lock and
lock_device_hotplug. Seems it is possible. You can correct me if I am wrong.[1] https://sashiko.dev/#/patchset/20260428085219.1316047-1-songmuchun%40bytedance.com
We could fix this by calling
action_result()without holding folio lock. What do you think?
Hello Muchun,
You could end up in memblk_nr_poison_sub() while holding hugetlb_lock spin lock from get_huge_page_for_hwpoison(), right?
Lockdep would flag this as sleeping while atomic when acquiring mutex I think.
Quoted 21 lines · Usama Arif
Quoted 10 lines · On Wed, 29 Apr 2026 12:18:08 +0800 Muchun Song <[email protected]> wrote:
Quoted 7 lines
lock_device_hotplugis a mutex lock, and we already take other mutex locks while holdinglock_folioin other paths, so I am not sure I see what should be special in this case.Hi Oscar and Miaohe,
Quoted 8 lines
I saw sashiko's report [1] related to folio lock and
lock_device_hotplug. Seems it is possible. You can correct me if I am wrong.[1] https://sashiko.dev/#/patchset/20260428085219.1316047-1-songmuchun%40bytedance.com
We could fix this by calling
action_result()without holding folio lock. What do you think?
Quoted 6 lines
Hello Muchun,
You could end up in
memblk_nr_poison_sub()while holdinghugetlb_lockspin lock fromget_huge_page_for_hwpoison(), right?Lockdep would flag this as sleeping while atomic when acquiring mutex I think.
Another thought would be, that we always call the inc/sub from memory failure code while we hold a folio reference and the page is not poisoned yet.
That way, memory offlining cannot continue and the memory block cannot go away.
So we'd let out page reference keep the memory block alive.
Signature · 3 lines
Cheers, David
Quoted 31 lines · David Hildenbrand (Arm)
Quoted 26 lines · Usama Arif
Quoted 10 lines · On Wed, 29 Apr 2026 12:18:08 +0800 Muchun Song <[email protected]> wrote:
Quoted 7 lines
lock_device_hotplugis a mutex lock, and we already take other mutex locks while holdinglock_folioin other paths, so I am not sure I see what should be special in this case.Hi Oscar and Miaohe,
Quoted 8 lines
I saw sashiko's report [1] related to folio lock and
lock_device_hotplug. Seems it is possible. You can correct me if I am wrong.[1] https://sashiko.dev/#/patchset/20260428085219.1316047-1-songmuchun%40bytedance.com
We could fix this by calling
action_result()without holding folio lock. What do you think?Hello Muchun,
You could end up in
memblk_nr_poison_sub()while holdinghugetlb_lockspin lock fromget_huge_page_for_hwpoison(), right?
Quoted 8 lines
Quoted 1 line
Lockdep would flag this as sleeping while atomic when acquiring mutex I think.
Another thought would be, that we always call the inc/sub from memory failure code while we hold a folio reference and the page is not poisoned yet.
That way, memory offlining cannot continue and the memory block cannot go away.
So we'd let out page reference keep the memory block alive.
It seems unnecessary to hold lock_device_hotplug if the user already holds a refcount on the page. I'd like to drop this patch.
Thanks.
Quoted 5 lines
Signature · 4 lines
-- Cheers, David
Andrew Morton
maintainerQuoted 11 lines · On Thu, 30 Apr 2026 15:59:33 +0800 Muchun Song <[email protected]> wrote:
Quoted 8 lines
Quoted 1 line
Lockdep would flag this as sleeping while atomic when acquiring mutex I think.
Another thought would be, that we always call the inc/sub from memory failure code while we hold a folio reference and the page is not poisoned yet.
That way, memory offlining cannot continue and the memory block cannot go away.
So we'd let out page reference keep the memory block alive.
It seems unnecessary to hold
lock_device_hotplugif the user already holds a refcount on the page. I'd like to drop this patch.
Dropped, thanks.
mm-hotfixes-unstable still has
"mm/memory_hotplug: fix memory block reference leak on remove" and
"drivers/base/memory: fix memory block reference leak in poison accounting"
Quoted 11 lines · Andrew Morton
Quoted 5 lines · On Thu, 30 Apr 2026 15:59:33 +0800 Muchun Song <[email protected]> wrote:
Quoted 5 lines
Quoted 1 line
Lockdep would flag this as sleeping while atomic when acquiring mutex I think.
Another thought would be, that we always call the inc/sub from memory failure code while we hold a folio reference and the page is not poisoned yet.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
That way, memory offlining cannot continue and the memory block cannot go away.
So we'd let out page reference keep the memory block alive.
It seems unnecessary to hold
lock_device_hotplugif the user already holds a refcount on the page. I'd like to drop this patch.Dropped, thanks.
Thank you so much for helping me with this.
Thanks,
Muchun.
Quoted 6 lines
mm-hotfixes-unstable still has
"
mm/memory_hotplug: fix memory block reference leak on remove" and
"drivers/base/memory: fix memory block reference leak in poison accounting"