On Wed, 30 Jul 2025 17:49:14 +0800 Gu Bowen gubowen5@huawei.com wrote:
kmemleak_scan_thread() invokes scan_block() which may invoke a nomal printk() to print warning message. This can cause a deadlock in the scenario reported below:
CPU0 CPU1 ---- ----
lock(kmemleak_lock); lock(&port->lock); lock(kmemleak_lock); lock(console_owner);
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.
Our syztester report the following lockdep error:
====================================================== WARNING: possible circular locking dependency detected 5.10.0-22221-gca646a51dd00 #16 Not tainted
...
Chain exists of: console_owner --> &port->lock --> kmemleak_lock
Cc: stable@vger.kernel.org # 5.10 Signed-off-by: Gu Bowen gubowen5@huawei.com
mm/kmemleak.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 4801751cb6b6..d322897a1de1 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -390,9 +390,11 @@ static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) else if (object->pointer == ptr || alias) return object; else {
__printk_safe_enter(); kmemleak_warn("Found object by alias at 0x%08lx\n", ptr); dump_object_info(object);
} }__printk_safe_exit(); break;
umm,
--- a/mm/kmemleak.c~a +++ a/mm/kmemleak.c @@ -103,6 +103,8 @@ #include <linux/kmemleak.h> #include <linux/memory_hotplug.h>
+#include "../kernel/printk/internal.h" /* __printk_safe_enter */ + /* * Kmemleak configuration and common defines. */
I'm not sure we're allowed to do that. Is there an official way?