Re: [PATCH printk v2 22/26] printk: nbcon: Implement emergency sections

From: Petr Mladek
Date: Fri Mar 01 2024 - 08:28:44 EST


On Sun 2024-02-18 20:03:22, John Ogness wrote:
> From: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
>
> In emergency situations (something has gone wrong but the
> system continues to operate), usually important information
> (such as a backtrace) is generated via printk(). Each
> individual printk record has little meaning. It is the
> collection of printk messages that is most often needed by
> developers and users.
>
> In order to help ensure that the collection of printk messages
> in an emergency situation are all stored to the ringbuffer as
> quickly as possible, disable console output for that CPU while
> it is in the emergency situation. When exiting the emergency
> situation, trigger the consoles to be flushed.
>
> Add per-CPU emergency nesting tracking because an emergency
> can arise while in an emergency situation.
>
> Add functions to mark the beginning and end of emergency
> sections where the urgent messages are generated.
>
> Do not print if the current CPU is in an emergency state.
>
> Trigger console flushing when exiting all emergency nesting.
>
> Note that the emergency state is not system-wide. While one CPU
> is in an emergency state, another CPU may continue to print
> console messages.
>
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2404,16 +2404,29 @@ asmlinkage int vprintk_emit(int facility, int level,
> * printing of all remaining records to all consoles so that
> * this context can return as soon as possible. Hopefully
> * another printk() caller will take over the printing.
> + *
> + * Also, nbcon_get_default_prio() requires migration disabled.
> */
> preempt_disable();
> +
> /*
> - * Try to acquire and then immediately release the console
> - * semaphore. The release will print out buffers. With the
> - * spinning variant, this context tries to take over the
> - * printing from another printing context.
> + * Do not emit for EMERGENCY priority. The console will be
> + * explicitly flushed when exiting the emergency section.
> */
> - if (console_trylock_spinning())
> - console_unlock();
> + if (nbcon_get_default_prio() == NBCON_PRIO_EMERGENCY) {
> + do_trylock_unlock = false;

This would cause calling defer_console_output() in this printk().
I think that we do not want it. It is done later by
nbcon_cpu_emergency_exit(). I think that we want something like:


/*
* Try to acquire and then immediately release the
* console semaphore. The release will print out
* buffers. With the spinning variant, this context
* tries to take over the printing from another
* printing context.
*
* Skip it in EMERGENCY priority. The console will be
* explicitly flushed when exiting the emergency section.
*/
if (nbcon_get_default_prio() != NBCON_PRIO_EMERGENCY) {
if (console_trylock_spinning())
console_unlock();
}


> + } else {
> + /*
> + * Try to acquire and then immediately release the
> + * console semaphore. The release will print out
> + * buffers. With the spinning variant, this context
> + * tries to take over the printing from another
> + * printing context.
> + */
> + if (console_trylock_spinning())
> + console_unlock();
> + }
> +
> preempt_enable();
> }

Otherwise, it looks good.

Best Regards,
Petr