From: John Ogness <john.ogness@linutronix.de>
To: Breno Leitao <leitao@debian.org>,
Catalin Marinas <catalin.marinas@arm.com>
Cc: 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 16:13:06 +0206 [thread overview]
Message-ID: <84ikh1l5dh.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <kuq7guzalpqj5bxe2vt6s3kirrq4sg5ozwcim6ewnzpxhuxm4l@yfgb44nbcisz>
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.
John Ogness
next prev parent reply other threads:[~2025-09-29 14:07 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 [this message]
2025-09-29 14:40 ` Breno Leitao
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=84ikh1l5dh.fsf@jogness.linutronix.de \
--to=john.ogness@linutronix.de \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=gregkh@linuxfoundation.org \
--cc=gubowen5@huawei.com \
--cc=leitao@debian.org \
--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