Re: [PATCH 2/3] printk/nbcon/panic: Allow printk kthread to sleep when the system is in panic
From: Andrew Murray
Date: Mon Sep 29 2025 - 04:39:35 EST
On Fri, 26 Sept 2025 at 13:50, Petr Mladek <pmladek@xxxxxxxx> wrote:
>
> The printk kthread might be running when there is a panic in progress.
> But it is not able to acquire the console ownership any longer.
>
> Prevent the desperate attempts to acquire the ownership and allow sleeping
> in panic. It would make it behave the same as when there is any CPU
> in an emergency context.
>
> Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
> ---
> kernel/printk/internal.h | 1 +
> kernel/printk/nbcon.c | 6 ++++--
> kernel/printk/printk.c | 2 +-
> 3 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
> index ef282001f200..6e8578102fb3 100644
> --- a/kernel/printk/internal.h
> +++ b/kernel/printk/internal.h
> @@ -332,6 +332,7 @@ struct printk_message {
> unsigned long dropped;
> };
>
> +bool panic_in_progress(void);
> bool other_cpu_in_panic(void);
> bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
> bool is_extended, bool may_supress);
> diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
> index 08b196e898cd..219ae0c8b5ed 100644
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -1178,7 +1178,8 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex
> * where the context with a higher priority takes over the nbcon console
> * ownership in the middle of a message.
> */
> - if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)))
> + if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)) ||
> + unlikely(panic_in_progress()))
> return false;
>
> cookie = console_srcu_read_lock();
> @@ -1236,7 +1237,8 @@ static int nbcon_kthread_func(void *__console)
> * Block the kthread when the system is in an emergency or panic
> * mode. See nbcon_kthread_should_wakeup() for more details.
> */
> - if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)))
> + if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)) ||
> + unlikely(panic_in_progress()))
> goto wait_for_event;
>
> backlog = false;
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index ebf10352736f..174d42041594 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -345,7 +345,7 @@ static void __up_console_sem(unsigned long ip)
> }
> #define up_console_sem() __up_console_sem(_RET_IP_)
>
> -static bool panic_in_progress(void)
> +bool panic_in_progress(void)
> {
> return unlikely(atomic_read(&panic_cpu) != PANIC_CPU_INVALID);
> }
> --
> 2.51.0
>
Reviewed-by: Andrew Murray <amurray@xxxxxxxxxxxxxxxxxxxx>
Thanks,
Andrew Murray