Skip to content

Commit 28bdebd

Browse files
Gu Bowengregkh
authored andcommitted
mm: fix possible deadlock in kmemleak
commit c873ccb upstream. 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. Link: https://lkml.kernel.org/r/20250822073541.1886469-1-gubowen5@huawei.com Link: https://lore.kernel.org/all/20250731-kmemleak_lock-v1-1-728fd470198f@debian.org/#t [1] Link: https://lore.kernel.org/all/5ca375cd-4a20-4807-b897-68b289626550@redhat.com/ [2] Signed-off-by: Gu Bowen <gubowen5@huawei.com> Reviewed-by: Waiman Long <longman@redhat.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Reviewed-by: Breno Leitao <leitao@debian.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: John Ogness <john.ogness@linutronix.de> Cc: Lu Jialin <lujialin4@huawei.com> Cc: Petr Mladek <pmladek@suse.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 458c3d3 commit 28bdebd

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

mm/kmemleak.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,15 @@ static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
437437
else if (untagged_objp == untagged_ptr || alias)
438438
return object;
439439
else {
440+
/*
441+
* Printk deferring due to the kmemleak_lock held.
442+
* This is done to avoid deadlock.
443+
*/
444+
printk_deferred_enter();
440445
kmemleak_warn("Found object by alias at 0x%08lx\n",
441446
ptr);
442447
dump_object_info(object);
448+
printk_deferred_exit();
443449
break;
444450
}
445451
}
@@ -736,13 +742,19 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
736742
else if (untagged_objp + parent->size <= untagged_ptr)
737743
link = &parent->rb_node.rb_right;
738744
else {
745+
/*
746+
* Printk deferring due to the kmemleak_lock held.
747+
* This is done to avoid deadlock.
748+
*/
749+
printk_deferred_enter();
739750
kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
740751
ptr);
741752
/*
742753
* No need for parent->lock here since "parent" cannot
743754
* be freed while the kmemleak_lock is held.
744755
*/
745756
dump_object_info(parent);
757+
printk_deferred_exit();
746758
return -EEXIST;
747759
}
748760
}
@@ -856,13 +868,8 @@ static void delete_object_part(unsigned long ptr, size_t size,
856868

857869
raw_spin_lock_irqsave(&kmemleak_lock, flags);
858870
object = __find_and_remove_object(ptr, 1, objflags);
859-
if (!object) {
860-
#ifdef DEBUG
861-
kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
862-
ptr, size);
863-
#endif
871+
if (!object)
864872
goto unlock;
865-
}
866873

867874
/*
868875
* 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,
882889

883890
unlock:
884891
raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
885-
if (object)
892+
if (object) {
886893
__delete_object(object);
894+
} else {
895+
#ifdef DEBUG
896+
kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
897+
ptr, size);
898+
#endif
899+
}
887900

888901
out:
889902
if (object_l)

0 commit comments

Comments
 (0)