* [PATCH v5] mm: Fix possible deadlock in kmemleak
@ 2025-08-22 7:35 Gu Bowen
2025-08-26 8:23 ` Breno Leitao
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Gu Bowen @ 2025-08-22 7:35 UTC (permalink / raw)
To: Catalin Marinas, Andrew Morton, Greg Kroah-Hartman, Waiman Long
Cc: stable, linux-mm, Breno Leitao, John Ogness, Lu Jialin, Gu Bowen
There are some AA deadlock issues in kmemleak, similar to the situation
reported by Breno [1]. The deadlock path is as follows:
mem_pool_alloc()
-> raw_spin_lock_irqsave(&kmemleak_lock, flags);
-> pr_warn()
-> netconsole subsystem
-> netpoll
-> __alloc_skb
-> __create_object
-> raw_spin_lock_irqsave(&kmemleak_lock, flags);
To solve this problem, switch to printk_safe mode before printing warning
message, this will redirect all printk()-s to a special per-CPU buffer,
which will be flushed later from a safe context (irq work), and this
deadlock problem can be avoided. The proper API to use should be
printk_deferred_enter()/printk_deferred_exit() [2]. Another way is to
place the warn print after kmemleak is released.
[1]
https://lore.kernel.org/all/20250731-kmemleak_lock-v1-1-728fd470198f@debian.org/#t
[2]
https://lore.kernel.org/all/5ca375cd-4a20-4807-b897-68b289626550@redhat.com/
====================
Signed-off-by: Gu Bowen <gubowen5@huawei.com>
---
mm/kmemleak.c | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 84265983f239..1ac56ceb29b6 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -437,9 +437,15 @@ static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
else if (untagged_objp == untagged_ptr || alias)
return object;
else {
+ /*
+ * Printk deferring due to the kmemleak_lock held.
+ * This is done to avoid deadlock.
+ */
+ printk_deferred_enter();
kmemleak_warn("Found object by alias at 0x%08lx\n",
ptr);
dump_object_info(object);
+ printk_deferred_exit();
break;
}
}
@@ -736,6 +742,11 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
else if (untagged_objp + parent->size <= untagged_ptr)
link = &parent->rb_node.rb_right;
else {
+ /*
+ * Printk deferring due to the kmemleak_lock held.
+ * This is done to avoid deadlock.
+ */
+ printk_deferred_enter();
kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
ptr);
/*
@@ -743,6 +754,7 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
* be freed while the kmemleak_lock is held.
*/
dump_object_info(parent);
+ printk_deferred_exit();
return -EEXIST;
}
}
@@ -856,13 +868,8 @@ static void delete_object_part(unsigned long ptr, size_t size,
raw_spin_lock_irqsave(&kmemleak_lock, flags);
object = __find_and_remove_object(ptr, 1, objflags);
- if (!object) {
-#ifdef DEBUG
- kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
- ptr, size);
-#endif
+ if (!object)
goto unlock;
- }
/*
* Create one or two objects that may result from the memory block
@@ -882,8 +889,14 @@ static void delete_object_part(unsigned long ptr, size_t size,
unlock:
raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
- if (object)
+ if (object) {
__delete_object(object);
+ } else {
+#ifdef DEBUG
+ kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
+ ptr, size);
+#endif
+ }
out:
if (object_l)
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v5] mm: Fix possible deadlock in kmemleak
2025-08-22 7:35 [PATCH v5] mm: Fix possible deadlock in kmemleak Gu Bowen
@ 2025-08-26 8:23 ` Breno Leitao
2025-08-26 19:37 ` Catalin Marinas
2025-08-26 23:23 ` Waiman Long
2 siblings, 0 replies; 4+ messages in thread
From: Breno Leitao @ 2025-08-26 8:23 UTC (permalink / raw)
To: Gu Bowen
Cc: Catalin Marinas, Andrew Morton, Greg Kroah-Hartman, Waiman Long,
stable, linux-mm, John Ogness, Lu Jialin
On Fri, Aug 22, 2025 at 03:35:41PM +0800, Gu Bowen wrote:
> There are some AA deadlock issues in kmemleak, similar to the situation
> reported by Breno [1]. The deadlock path is as follows:
>
> mem_pool_alloc()
> -> raw_spin_lock_irqsave(&kmemleak_lock, flags);
> -> pr_warn()
> -> netconsole subsystem
> -> netpoll
> -> __alloc_skb
> -> __create_object
> -> raw_spin_lock_irqsave(&kmemleak_lock, flags);
>
> To solve this problem, switch to printk_safe mode before printing warning
> message, this will redirect all printk()-s to a special per-CPU buffer,
> which will be flushed later from a safe context (irq work), and this
> deadlock problem can be avoided. The proper API to use should be
> printk_deferred_enter()/printk_deferred_exit() [2]. Another way is to
> place the warn print after kmemleak is released.
>
> [1]
> https://lore.kernel.org/all/20250731-kmemleak_lock-v1-1-728fd470198f@debian.org/#t
> [2]
> https://lore.kernel.org/all/5ca375cd-4a20-4807-b897-68b289626550@redhat.com/
> ====================
>
> Signed-off-by: Gu Bowen <gubowen5@huawei.com>
Reviewed-by: Breno Leitao <leitao@debian.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v5] mm: Fix possible deadlock in kmemleak
2025-08-22 7:35 [PATCH v5] mm: Fix possible deadlock in kmemleak Gu Bowen
2025-08-26 8:23 ` Breno Leitao
@ 2025-08-26 19:37 ` Catalin Marinas
2025-08-26 23:23 ` Waiman Long
2 siblings, 0 replies; 4+ messages in thread
From: Catalin Marinas @ 2025-08-26 19:37 UTC (permalink / raw)
To: Gu Bowen
Cc: Andrew Morton, Greg Kroah-Hartman, Waiman Long, stable, linux-mm,
Breno Leitao, John Ogness, Lu Jialin
On Fri, Aug 22, 2025 at 03:35:41PM +0800, Gu Bowen wrote:
> There are some AA deadlock issues in kmemleak, similar to the situation
> reported by Breno [1]. The deadlock path is as follows:
>
> mem_pool_alloc()
> -> raw_spin_lock_irqsave(&kmemleak_lock, flags);
> -> pr_warn()
> -> netconsole subsystem
> -> netpoll
> -> __alloc_skb
> -> __create_object
> -> raw_spin_lock_irqsave(&kmemleak_lock, flags);
>
> To solve this problem, switch to printk_safe mode before printing warning
> message, this will redirect all printk()-s to a special per-CPU buffer,
> which will be flushed later from a safe context (irq work), and this
> deadlock problem can be avoided. The proper API to use should be
> printk_deferred_enter()/printk_deferred_exit() [2]. Another way is to
> place the warn print after kmemleak is released.
>
> [1]
> https://lore.kernel.org/all/20250731-kmemleak_lock-v1-1-728fd470198f@debian.org/#t
> [2]
> https://lore.kernel.org/all/5ca375cd-4a20-4807-b897-68b289626550@redhat.com/
> ====================
>
> Signed-off-by: Gu Bowen <gubowen5@huawei.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v5] mm: Fix possible deadlock in kmemleak
2025-08-22 7:35 [PATCH v5] mm: Fix possible deadlock in kmemleak Gu Bowen
2025-08-26 8:23 ` Breno Leitao
2025-08-26 19:37 ` Catalin Marinas
@ 2025-08-26 23:23 ` Waiman Long
2 siblings, 0 replies; 4+ messages in thread
From: Waiman Long @ 2025-08-26 23:23 UTC (permalink / raw)
To: Gu Bowen, Catalin Marinas, Andrew Morton, Greg Kroah-Hartman,
Waiman Long
Cc: stable, linux-mm, Breno Leitao, John Ogness, Lu Jialin
On 8/22/25 3:35 AM, Gu Bowen wrote:
> There are some AA deadlock issues in kmemleak, similar to the situation
> reported by Breno [1]. The deadlock path is as follows:
>
> mem_pool_alloc()
> -> raw_spin_lock_irqsave(&kmemleak_lock, flags);
> -> pr_warn()
> -> netconsole subsystem
> -> netpoll
> -> __alloc_skb
> -> __create_object
> -> raw_spin_lock_irqsave(&kmemleak_lock, flags);
>
> To solve this problem, switch to printk_safe mode before printing warning
> message, this will redirect all printk()-s to a special per-CPU buffer,
> which will be flushed later from a safe context (irq work), and this
> deadlock problem can be avoided. The proper API to use should be
> printk_deferred_enter()/printk_deferred_exit() [2]. Another way is to
> place the warn print after kmemleak is released.
>
> [1]
> https://lore.kernel.org/all/20250731-kmemleak_lock-v1-1-728fd470198f@debian.org/#t
> [2]
> https://lore.kernel.org/all/5ca375cd-4a20-4807-b897-68b289626550@redhat.com/
> ====================
>
> Signed-off-by: Gu Bowen <gubowen5@huawei.com>
> ---
> mm/kmemleak.c | 27 ++++++++++++++++++++-------
> 1 file changed, 20 insertions(+), 7 deletions(-)
>
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index 84265983f239..1ac56ceb29b6 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -437,9 +437,15 @@ static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
> else if (untagged_objp == untagged_ptr || alias)
> return object;
> else {
> + /*
> + * Printk deferring due to the kmemleak_lock held.
> + * This is done to avoid deadlock.
> + */
> + printk_deferred_enter();
> kmemleak_warn("Found object by alias at 0x%08lx\n",
> ptr);
> dump_object_info(object);
> + printk_deferred_exit();
> break;
> }
> }
> @@ -736,6 +742,11 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
> else if (untagged_objp + parent->size <= untagged_ptr)
> link = &parent->rb_node.rb_right;
> else {
> + /*
> + * Printk deferring due to the kmemleak_lock held.
> + * This is done to avoid deadlock.
> + */
> + printk_deferred_enter();
> kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
> ptr);
> /*
> @@ -743,6 +754,7 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
> * be freed while the kmemleak_lock is held.
> */
> dump_object_info(parent);
> + printk_deferred_exit();
> return -EEXIST;
> }
> }
> @@ -856,13 +868,8 @@ static void delete_object_part(unsigned long ptr, size_t size,
>
> raw_spin_lock_irqsave(&kmemleak_lock, flags);
> object = __find_and_remove_object(ptr, 1, objflags);
> - if (!object) {
> -#ifdef DEBUG
> - kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
> - ptr, size);
> -#endif
> + if (!object)
> goto unlock;
> - }
>
> /*
> * Create one or two objects that may result from the memory block
> @@ -882,8 +889,14 @@ static void delete_object_part(unsigned long ptr, size_t size,
>
> unlock:
> raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
> - if (object)
> + if (object) {
> __delete_object(object);
> + } else {
> +#ifdef DEBUG
> + kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
> + ptr, size);
> +#endif
> + }
>
> out:
> if (object_l)
Reviewed-by: Waiman Long <longman@redhat.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-08-26 23:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-22 7:35 [PATCH v5] mm: Fix possible deadlock in kmemleak Gu Bowen
2025-08-26 8:23 ` Breno Leitao
2025-08-26 19:37 ` Catalin Marinas
2025-08-26 23:23 ` Waiman Long
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).