All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Breno Leitao <leitao@debian.org>,
	printk@lists.linux.dev, linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH printk v4 6/8] console: Support unsafe flushing for panic/emergency restarts
Date: Wed, 25 Nov 2026 15:02:51 +0100	[thread overview]
Message-ID: <awbqiycLbPPFU2N3@pathway.suse.cz> (raw)
In-Reply-To: <20261121163557.963029-7-john.ogness@linutronix.de>

On Sat 2026-11-21 17:41:54, John Ogness wrote:
> Introduce a new nbcon-specific console field @allow_unsafe_policy
> to store the unsafe flush policies for a console. The policies are
> represented by bits, specified by enum nbcon_unsafe_flush_flags.
> 
> Define unsafe flush policies for:
> 
> 	panic+restart: This is for kexec and reboot from panic.
> 
> 	emergency+restart: This is for emergency_restart() when in
> 				the emergency state.
> 
> Define a new flush type:
> 
> 	PR_FLUSH_RESTART: For flushing before kexec and reboot.
> 
> Implement the policy check within nbcon_allow_unsafe_takeover().
> 
> No functional change.
> 
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> ---
>  include/linux/console.h | 20 ++++++++++++++++++++
>  include/linux/printk.h  |  3 +++
>  kernel/printk/nbcon.c   |  7 +++++++
>  3 files changed, 30 insertions(+)
> 
> diff --git a/include/linux/console.h b/include/linux/console.h
> index 227a7cf296f60..1afeb90692395 100644
> --- a/include/linux/console.h
> +++ b/include/linux/console.h
> @@ -194,6 +194,23 @@ enum cons_flags {
>  	CON_NBCON_ATOMIC_UNSAFE	= BIT(9),
>  };
>  
> +/**
> + * enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags
> + * @NBCON_PANIC_RESTART_FLUSH:		In panic, unsafe flush before kexec
> + *						or reboot.
> + * @NBCON_EMERGENCY_RESTART_FLUSH:	Unsafe flush before emergency restart.
> + *
> + * NBCON unsafe atomic flushing always occurs at the end of panic() before
> + * the kernel enters an infinite loop. Users may choose to perform unsafe
> + * flushing in additional scenarios where the current kernel is ending in
> + * order to increase the chances of seeing console messages, even if it is
> + * not safe to do so.
> + */
> +enum nbcon_unsafe_flush_flags {
> +	NBCON_PANIC_RESTART_FLUSH	= BIT(0),
> +	NBCON_EMERGENCY_RESTART_FLUSH	= BIT(1),
> +};
> 
>  /**
>   * struct nbcon_state - console state for nbcon consoles
>   * @atom:	Compound of the state fields for atomic operations
> @@ -326,6 +343,7 @@ struct nbcon_write_context {
>   * @data:		Driver private data
>   * @node:		hlist node for the console list
>   *
> + * @allow_unsafe_policy: Unsafe atomic flush policy. See nbcon_unsafe_flush_flags
>   * @nbcon_state:	State for nbcon consoles
>   * @nbcon_seq:		Sequence number of the next record for nbcon to print
>   * @nbcon_device_ctxt:	Context available for non-printing operations
> @@ -461,6 +479,8 @@ struct console {
>  	 */
>  	void (*device_unlock)(struct console *con, unsigned long flags);
>  
> +	unsigned int		allow_unsafe_policy;
> +
>  	atomic_t		__private nbcon_state;
>  	atomic_long_t		__private nbcon_seq;
>  	struct nbcon_context	__private nbcon_device_ctxt;
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index 68cf51db5f0ea..267a0a6fd23cf 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -148,6 +148,8 @@ struct dev_printk_info;
>  /**
>   * enum pr_flush_reason - Reason for a flush
>   * @PR_FLUSH_RUNTIME:	A flush while the kernel is running
> + * @PR_FLUSH_RESTART:	The kernel is ending, but another instance should
> + *			follow (reboot, kexec)
>   * @PR_FLUSH_HANG:	The kernel is ending and will enter an infinite loop
>   *
>   * Flush reasons are used to determine if unsafe atomic flushing should be
> @@ -155,6 +157,7 @@ struct dev_printk_info;
>   */
>  enum pr_flush_reason {
>  	PR_FLUSH_RUNTIME,
> +	PR_FLUSH_RESTART,
>  	PR_FLUSH_HANG,
>  };
>  
> diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
> index 8b7483bd57188..f1789cfbd8b57 100644
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1471,12 +1471,19 @@ enum nbcon_prio nbcon_get_default_prio(void)
>  static bool nbcon_allow_unsafe(struct console *con, enum pr_flush_reason reason)
>  {
>  	enum nbcon_prio prio = nbcon_get_default_prio();
> +	int unsafe_policy = con->allow_unsafe_policy;
>  	bool allow = false;
>  
>  	switch (reason) {
>  	case PR_FLUSH_RUNTIME:
>  		/* Never unsafe flush during runtime. */
>  		break;
> +	case PR_FLUSH_RESTART:
> +		if (prio == NBCON_PRIO_PANIC)
> +			allow = (unsafe_policy & NBCON_PANIC_RESTART_FLUSH);
> +		else if (prio == NBCON_PRIO_EMERGENCY)
> +			allow = (unsafe_policy & NBCON_EMERGENCY_RESTART_FLUSH);
> +		break;

Many different flags are mixed in this function:

    1. CON_NBCON_ATOMIC_UNSAFE from enum cons_flags
    2. NBCON_PRIO_* from enum nbcon_prio
    3. PR_FLUSH_* from enum pr_flush_reason
    4. NBCON_*_FLUSH from enum nbcon_unsafe_flush_flags (flush policy)
7
It is easy to get lost. I wonder if we could do better. ;-)

Ad 1st: CON_NBCON_ATOMIC_UNSAFE is not ideal. But it is done. And
	I am not sure how to make it better.

Ad 2nd: NBCON_PRIO matches nbcon_prio. It look good.

Ad 3rd: PR_FLUSH is not that good. It would be better to
	emphasize that it is a "reason".

	I would prefer to use the longer PR_FLUSH_REASON_ prefix.
	It will be consistent with "enum pr_flush_reason" type name
	and more clear.


Ad 4th: Both NBCON_*_FLUSH and enum nbcon_unsafe_flush_flags are far
	from clear. And they are not consistent. Sigh, it would be
	great to emphasize "unsafe", "flush" and "policy".

	I think about:

	enum nbcon_unsafe_flush_policy {
		NBCON_UFP_PANIC_RESTART	= BIT(0),
		NBCON_UFP_EMERGENCY_RESTART = BIT(1),
	};

	or maybe even better

	enum nbcon_unsafe_flush_policy {
		NBCON_UF_POLICY_PANIC_RESTART	= BIT(0),
		NBCON_UF_POLICY_EMERGENCY_RESTART = BIT(1),
	};

	plus use:

	struct console {
	[...]
		unsigned int		unsafe_flush_policy;
	[...]
	}

	and finally the local variable "uf_policy".

};

What do you think, please?

Otherwise, the patch looks good. Note that we do not need to check
here panic_on_this_cpu() because NBCON_PRIO_PANIC is used only on
the panic_cpu.

Best Regards,
Petr

>  	case PR_FLUSH_HANG:
>  		/* Always unsafe flush on panic hang. */
>  		allow = (prio == NBCON_PRIO_PANIC);
> -- 
> 2.47.3

>  LocalWords:  PRIO

  reply	other threads:[~2026-11-25 14:02 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 16:35 [PATCH printk v4 0/8] Introduce policies for unsafe flushing John Ogness
2026-11-21 16:35 ` [PATCH printk v4 1/8] printk: Introduce and specify flush reasons John Ogness
2026-11-21 16:35 ` [PATCH printk v4 2/8] printk: nbcon: Move unsafe takeover check to higher level John Ogness
2026-11-21 16:35 ` [PATCH printk v4 3/8] printk: Move unsafe takeover check out of console_is_usable() John Ogness
2026-11-24 16:13   ` Petr Mladek
2026-11-26  9:51     ` John Ogness
2026-11-21 16:35 ` [PATCH printk v4 4/8] printk: nbcon: Use @reason instead of global variable to check flush policy John Ogness
2026-11-24 16:27   ` Petr Mladek
2026-11-26  9:53     ` John Ogness
2026-11-21 16:35 ` [PATCH printk v4 5/8] printk: nbcon: Expand unsafe takeover check for general unsafe John Ogness
2026-11-25 10:22   ` Petr Mladek
2026-11-25 13:06     ` Petr Mladek
2026-11-25 14:08     ` Petr Mladek
2026-11-26 10:36     ` John Ogness
2026-11-21 16:35 ` [PATCH printk v4 6/8] console: Support unsafe flushing for panic/emergency restarts John Ogness
2026-11-25 14:02   ` Petr Mladek [this message]
2026-11-26 10:45     ` John Ogness
2026-11-21 16:35 ` [PATCH printk v4 7/8] printk: Add unsafe flush points for panic+kexec and emergency_restart John Ogness
2026-11-21 16:35 ` [PATCH printk v4 8/8] netconsole: Perform unsafe flushing on panic and emergency restart John Ogness
2026-11-21 16:47 ` [PATCH printk v4 0/8] Introduce policies for unsafe flushing sashiko-bot
2026-11-23 22:15 ` Gustavo Luiz Duarte

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=awbqiycLbPPFU2N3@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=john.ogness@linutronix.de \
    --cc=leitao@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=printk@lists.linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=ryan.roberts@arm.com \
    --cc=senozhatsky@chromium.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.