mm: Fix possible deadlock in console_trylock_spinning
Subsystems (3)
PATCH v3 1/1 mm: Fix possible deadlock in console_trylock_spinning
Our syztester report the lockdep WARNING [1]. 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. The proper API to use should be printk_deferred_enter()/printk_deferred_exit() if we want to deferred the printing [2].
This patch also fixes other similar case that need to use the printk deferring [3].
[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/all/[email protected]/
[3]
https://lore.kernel.org/all/[email protected]/
====================
--- mm/kmemleak.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
mm/kmemleak.c +12 -0
@@ -390,9 +390,15 @@ static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)else if (object->pointer == 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 ; } }
@@ -632,6 +638,11 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,else if (parent->pointer + parent->size <= 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);/*
@@ -639,6 +650,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,* be freed while the kmemleak_lock is held. */ dump_object_info (parent); +printk_deferred_exit ();kmem_cache_free (object_cache, object); object =NULL ; goto out;
Signature · 1 line
2.25.1
Quoted 29 lines · Gu Bowen
Our syztester report the lockdep WARNING [1].
kmemleak_scan_thread()invokesscan_block()which may invoke a nomalprintk()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_safemode 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() if we want to deferred the printing [2].This patch also fixes other similar case that need to use the printk deferring [3].
[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/all/[email protected]/
[3]
https://lore.kernel.org/all/[email protected]/
====================
Quoted 2 lines
Cc [email protected] # 5.10Signed-off-by Gu Bowen <[email protected]>
Andrew Morton
maintainermm/kmemleak.c
Quoted 25 lines · On Wed, 13 Aug 2025 16:53:10 +0800 Gu Bowen <[email protected]> wrote:
Our syztester report the lockdep WARNING [1].
kmemleak_scan_thread()invokesscan_block()which may invoke a nomalprintk()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_safemode 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() if we want to deferred the printing [2].This patch also fixes other similar case that need to use the printk deferring [3].
...
mm/kmemleak.c +0
-0
I'm not sure which kernel version this was against, but kmemleak.c has changed quite a lot.
Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 9 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of console_lock in v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/. Therefore, theoretically, this issue existed before the refactoring of console_lock.
Best Regards,
Guber
Catalin Marinas
maintainerQuoted 9 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 7 lines
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.
Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
Signature · 1 line
Catalin
Greg Kroah-Hartman
maintainerQuoted 18 lines · Catalin Marinas
Quoted 9 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 7 lines
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.
Quoted 5 lines
Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
Why not take the upstream changes instead?
Catalin Marinas
maintainerQuoted 19 lines · Greg Kroah-Hartman
Quoted 18 lines · Catalin Marinas
Quoted 16 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.
Quoted 7 lines
Quoted 5 lines
Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
Why not take the upstream changes instead?
Gu reckons there are 40 patches -
https://lore.kernel.org/all/[email protected]/
I haven't checked what ended in mainline and whether we could do with fewer backports.
Signature · 1 line
Catalin
Greg Kroah-Hartman
maintainerQuoted 23 lines · Catalin Marinas
Quoted 22 lines · Greg Kroah-Hartman
Quoted 21 lines · Catalin Marinas
Quoted 9 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 7 lines
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
Why not take the upstream changes instead?
Gu reckons there are 40 patches -
https://lore.kernel.org/all/[email protected]/
40 really isn't that much overall, we've taken way more for much smaller issues :)
Quoted 2 lines
I haven't checked what ended in mainline and whether we could do with fewer backports.
I'll leave that all up to the people who are still wanting these older kernels.
thanks,
greg k-h
Catalin Marinas
maintainerQuoted 27 lines · Greg Kroah-Hartman
Quoted 26 lines · Catalin Marinas
Quoted 25 lines · Greg Kroah-Hartman
Quoted 23 lines · Catalin Marinas
Quoted 9 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 7 lines
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Why not take the upstream changes instead?
Gu reckons there are 40 patches -
https://lore.kernel.org/all/[email protected]/40 really isn't that much overall, we've taken way more for much smaller issues :)
TBH, I'm not sure it's worth it. That's a potential deadlock on a rare error condition (a kmemleak bug or something wrong with the sites calling the kmemleak API).
Quoted 5 lines
Quoted 2 lines
I haven't checked what ended in mainline and whether we could do with fewer backports.
I'll leave that all up to the people who are still wanting these older kernels.
Good point. Thanks for the advice ;).
Signature · 1 line
Catalin
Quoted 18 lines · Catalin Marinas
Quoted 9 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 7 lines
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.
Quoted 6 lines
Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
I have discovered that I made a mistake, this fix patch should be merged into the mainline. Since we have identified two types of deadlocks, the AA deadlock [1] and the ABBA deadlock[2], the latter's deadlock path no longer exists in the mainline due to the 40 patches that refactored console_lock. However, the AA deadlock issue persists, so I believe this fix should be applied to the mainline.
[1] https://lore.kernel.org/all/[email protected]/#t
[2] https://lore.kernel.org/all/[email protected]/
Best Regards,
Guber
Greg Kroah-Hartman
maintainerQuoted 35 lines · Gu Bowen
Quoted 18 lines · Catalin Marinas
Quoted 9 lines · Gu Bowen
Quoted 7 lines · Andrew Morton
I'm not sure which kernel version this was against, but
kmemleak.chas changed quite a lot.Could we please see a patch against a latest kernel version? Linus mainline will suit.
Thanks.
Quoted 7 lines
I discovered this issue in kernel version 5.10. Afterwards, I reviewed the code of the mainline version and found that this deadlock path no longer exists due to the refactoring of
console_lockin v6.2-rc1. For details on the refactoring, you can refer to this link :
https://lore.kernel.org/all/[email protected]/.
Therefore, theoretically, this issue existed before the refactoring ofconsole_lock.Quoted 6 lines
Oh, so you can no longer hit this issue with mainline. This wasn't mentioned (or I missed it) in the commit log.
So this would be a stable-only fix that does not have a correspondent upstream. Adding Greg for his opinion.
I have discovered that I made a mistake, this fix patch should be merged into the mainline. Since we have identified two types of deadlocks, the AA deadlock [1] and the ABBA deadlock[2], the latter's deadlock path no longer exists in the mainline due to the 40 patches that refactored
console_lock. However, the AA deadlock issue persists, so I believe this fix should be applied to the mainline.
Quoted 2 lines
[1] https://lore.kernel.org/all/[email protected]/#t
[2] https://lore.kernel.org/all/[email protected]/
Pleasae submit it as a normal patch then.
thanks,
greg k-h