linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Breno Leitao <leitao@debian.org>
To: John Ogness <john.ogness@linutronix.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>,
	 Gu Bowen <gubowen5@huawei.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Waiman Long <llong@redhat.com>,
	stable@vger.kernel.org,  linux-mm@kvack.org,
	Lu Jialin <lujialin4@huawei.com>
Subject: Re: [PATCH v5] mm: Fix possible deadlock in kmemleak
Date: Mon, 29 Sep 2025 07:40:11 -0700	[thread overview]
Message-ID: <afbwnxlj4rxncrqc2bi7maljsy4vi3xq3a4ads7plvpww4in6n@r6d4yyebofdv> (raw)
In-Reply-To: <84ikh1l5dh.fsf@jogness.linutronix.de>

On Mon, Sep 29, 2025 at 04:13:06PM +0206, John Ogness wrote:
> On 2025-09-26, Breno Leitao <leitao@debian.org> wrote:
> > My concern is when printk() is called with kmemleak_lock held(). Something as:
> >
> > raw_spin_lock_irqsave(&kmemleak_lock, flags);
> >    -> printk()
> >
> > This is instant deadlock when netconsole is enabled. Given that
> > netconsole tries to allocate memory when flushing. Similarly to commit
> > 47b0f6d8f0d2be ("mm/kmemleak: avoid deadlock by moving pr_warn()
> > outside kmemleak_lock").
> 
> Yes, it is a known problem that a caller must not hold any locks that
> are used during console printing. Locking the serial port lock
> (uart_port->lock) and calling printk() also leads to deadlock if that
> port is registered as a serial console.
> 
> This is properly fixed by converting to the new nbcon console API, which
> netconsole is currently working on. But until then something like Breno
> is suggesting will provide a functional workaround.
> 
> Note that printk_deferred_enter/exit() require migration to be
> disabled. If kmemleak_lock() is not always being called in such a
> context, it cannot enable deferring.
> 
> One option is to enable deferring after taking the lock:
> 
> 	void kmemleak_lock(unsigned long *flags) {
> 		raw_spin_lock_irqsave(&kmemleak_lock, flags);
> 		printk_deferred_enter();
> 	}
> 
> printk() always defers in NMI context, so there is no risk if an NMI
> jumped in between locking and deferring and then called printk().
> 
> > The hack above would guarantee that  all printks() inside kmemleak_lock
> > critical area to be deferred, and not executed inline.
> 
> Yes, although I think netconsole is the only console that tries to
> allocate memory. So if this hack is used, it should at least be wrapped
> by an ifdef CONFIG_NETCONSOLE.
> 
> Although it would be preferable if netconsole did not need to allocate
> memory for flushing.

Most (all?) of the allocation is in the skb, where alloc_skb() is done.

In fact, netconsole maintains a pool of 32 skbs that is used when
alloc_skb() fails. And I see cases when that is exhausted (when OOM
causes a lot of messages to be flushed).

If we want to make alloc_skb() out of the TX path, then we probably need
a bigger (configurable?) pool of SKBs.



      reply	other threads:[~2025-09-29 14:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-22  7:35 Gu Bowen
2025-08-26  8:23 ` Breno Leitao
2025-08-26 19:37 ` Catalin Marinas
2025-08-26 23:23 ` Waiman Long
2025-09-19 10:37 ` Breno Leitao
2025-09-25 14:33   ` Catalin Marinas
2025-09-26 14:57     ` Breno Leitao
2025-09-29 14:07       ` John Ogness
2025-09-29 14:40         ` Breno Leitao [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=afbwnxlj4rxncrqc2bi7maljsy4vi3xq3a4ads7plvpww4in6n@r6d4yyebofdv \
    --to=leitao@debian.org \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=gubowen5@huawei.com \
    --cc=john.ogness@linutronix.de \
    --cc=linux-mm@kvack.org \
    --cc=llong@redhat.com \
    --cc=lujialin4@huawei.com \
    --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