linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Gu Bowen <gubowen5@huawei.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	stable@vger.kernel.org, linux-mm@kvack.org,
	Lu Jialin <lujialin4@huawei.com>,
	Waiman Long <longman@redhat.com>,
	Breno Leitao <leitao@debian.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	John Ogness <john.ogness@linutronix.de>
Subject: Re: [PATCH] mm: Fix possible deadlock in console_trylock_spinning
Date: Mon, 11 Aug 2025 16:33:31 +0200	[thread overview]
Message-ID: <aJn_Oxp3kKUF-fuR@pathway.suse.cz> (raw)
In-Reply-To: <20250801165228.6c2a009c0fe439ddc438217e@linux-foundation.org>

On Fri 2025-08-01 16:52:28, Andrew Morton wrote:
> 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?

The official way is to use printk_deferred_enter()/exit().

Note that the API is using a per-CPU variable. It must be called
with CPU migration disabled. The comment suggests disabling IRQs.
But it should be enough to disable preemption.

Best Regards,
Petr


      reply	other threads:[~2025-08-11 14:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-30  9:49 [PATCH] mm: Fix possible deadlock in console_trylock_spinning Gu Bowen
2025-08-01 22:33 ` Andrew Morton
2025-08-02  3:09   ` Waiman Long
2025-08-02  3:53     ` Andrew Morton
2025-08-04 12:08       ` Catalin Marinas
2025-08-04 12:34         ` Breno Leitao
2025-08-04 18:28           ` Catalin Marinas
2025-08-05  7:44             ` Gu Bowen
2025-08-06 15:34     ` John Ogness
2025-08-07  3:22       ` Gu Bowen
2025-08-01 23:52 ` Andrew Morton
2025-08-11 14:33   ` Petr Mladek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aJn_Oxp3kKUF-fuR@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=gubowen5@huawei.com \
    --cc=john.ogness@linutronix.de \
    --cc=leitao@debian.org \
    --cc=linux-mm@kvack.org \
    --cc=longman@redhat.com \
    --cc=lujialin4@huawei.com \
    --cc=rostedt@goodmis.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).