public inbox for linux-doc@vger.kernel.org
 help / color / mirror / Atom feed
From: John Ogness <john.ogness@linutronix.de>
To: hujinfei <3288824963@qq.com>, linux-doc@vger.kernel.org
Cc: pmladek@suse.com, senozhatsky@chromium.org,
	qujingling@huawei.com, zhangjiaji1@huawei.com,
	xushuangxing@huawei.com, rostedt@goodmis.org,
	hujinfei3@huawei.com
Subject: Re: [PATCH] Documentation: printk: warn about lockups from excessive use
Date: Fri, 13 Mar 2026 11:33:16 +0106	[thread overview]
Message-ID: <874imkqb23.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <tencent_D970CEB1BE717D3D5E259943CB4510A99308@qq.com>

On 2026-03-04, hujinfei <3288824963@qq.com> wrote:
> From: hujinfei <hujinfei3@huawei.com>
>
> Add a section 'Avoiding lockups from excessive printk() use' to
> printk-basics.rst, explaining the risk of calling printk() in hot paths
> with slow consoles and suggesting alternatives like ratelimited printing,
> tracepoints, nbcon, and log level filtering.
>
> Signed-off-by: hujinfei <hujinfei3@huawei.com>
> ---
>  Documentation/core-api/printk-basics.rst | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
>
> diff --git a/Documentation/core-api/printk-basics.rst b/Documentation/core-api/printk-basics.rst
> index 2dde24ca7..a9da8c336 100644
> --- a/Documentation/core-api/printk-basics.rst
> +++ b/Documentation/core-api/printk-basics.rst
> @@ -103,6 +103,28 @@ For debugging purposes there are also two conditionally-compiled macros:
>  pr_debug() and pr_devel(), which are compiled-out unless ``DEBUG`` (or
>  also ``CONFIG_DYNAMIC_DEBUG`` in the case of pr_debug()) is defined.
>  
> +Avoiding lockups from excessive printk() use
> +============================================
> +
> +Do not use ``printk()`` in hot paths such as interrupt handlers, timer callbacks,
> +or high-frequency network receive routines. When a slow console (e.g., ``console=ttyS0``)
> +is active, ``printk()`` may synchronously acquire ``console_sem`` and block while
> +flushing messages, potentially disabling interrupts long enough to trigger hard or
> +soft lockup detectors.

I hesitate supporting the addition of this documetation because it is
only relevant for legacy consoles and !PREEMPT_RT. Perhaps we could make
that point clearer. Once all console drivers have been updated to nbcon,
we can then remove this documentation.

> +
> +To avoid this:
> +
> +- Avoid ``printk()`` in hot paths and interrupt contexts.
> +- Use rate-limited variants (e.g., pr_xxx_ratelimited()) or one-time macros (e.g., pr_*_once()).
> +- Assign lower log levels (e.g., ``KERN_DEBUG``) to non-essential messages and filter
> +  console output via ``console_loglevel``.
> +- Use consoles that implement the non-blocking ``nbcon`` API (indicated by ``CON_NBCON``),
> +  which offload message printing to a dedicated kernel thread outside emergency contexts.

If the first paragraph made it clear that legacy consoles are the
problem, the point about using nbcon would not be necessary.

> +  Note that asynchronous printing increases the risk of message loss during crashes;
> +  increasing the kernel log buffer size may help retain more messages.

This last sentence is misleading. Calling printk() immediately logs the
message to the lockless ringbuffer. Upon panic, the buffer is
flushed. The nbcon consoles also transition to atomic printing. The only
messages that might get lost due to mass printk() calling are older
messages. But this problem is not specific to nbcon consoles.

There is also printk_deferred(), which immediately logs to the
ringbuffer and defers the console printing. It is a workaround for
legacy consoles. For nbcon consoles there is no difference between
printk() and printk_deferrred().

And since this is supposed to be general tips for developers, perhaps we
could mention that porting a legacy console driver to nbcon is also a
solution (and, in fact, is the preferred solution).

John Ogness

  parent reply	other threads:[~2026-03-13 10:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-04 14:40 [PATCH] Documentation: printk: warn about lockups from excessive use hujinfei
2026-03-04 21:18 ` Randy Dunlap
2026-03-05  1:49 ` Steven Rostedt
2026-03-13 10:27 ` John Ogness [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-03-05  6:41 hujinfei
2026-03-05 23:25 ` Randy Dunlap
2026-03-10 15:31 ` Petr Mladek

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=874imkqb23.fsf@jogness.linutronix.de \
    --to=john.ogness@linutronix.de \
    --cc=3288824963@qq.com \
    --cc=hujinfei3@huawei.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=qujingling@huawei.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=xushuangxing@huawei.com \
    --cc=zhangjiaji1@huawei.com \
    /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