Re: [PATCH printk v1 07/10] console: add write_atomic interface

From: Andy Shevchenko
Date: Tue Aug 03 2021 - 10:02:30 EST


On Tue, Aug 03, 2021 at 03:18:58PM +0206, John Ogness wrote:
> Add a write_atomic() callback to the console. This is an optional
> function for console drivers. The function must be atomic (including
> NMI safe) for writing to the console.
>
> Console drivers implementing write_atomic() must select the new
> HAVE_ATOMIC_CONSOLE Kconfig option.
>
> Console drivers must still implement the write() callback. The
> write_atomic() callback will only be used in special situations,
> such as when the kernel panics.
>
> Creating an NMI safe write_atomic() that must synchronize with
> write() requires a careful implementation of the console driver. To
> aid with the implementation, a set of console_atomic_*() functions
> are provided:
>
> void console_atomic_lock(unsigned long flags);
> void console_atomic_unlock(unsigned long flags);
>
> These functions synchronize using the printk cpulock and disable
> hardware interrupts.
>
> In order to increase effectiveness, the printk cpulock functions are
> also made more aggressive and now keep interrupts disabled while
> spinning.
>
> CPUs holding the printk cpulock must not spin on any other lock.
> Therefore can_use_console() will now return false if the printk
> cpulock is held in order to avoid calling into console driver code,
> while typically contain spinlocks.
>
> Likewise, console_trylock_spinning() will fail rather than attempt
> to acquire the console_sem (which includes a spinlock in its

...

> #include <linux/atomic.h>
> #include <linux/types.h>
> +#include <linux/printk.h>

Ordered?

...

> + if (!(con->flags & CON_ENABLED)) \
> + continue; \

What about

#define console_is_enabled(con) (!!(con->flags & CON_ENABLED))

or inliner equivalent

static inline bool console_is_enabled(struct console *con)
{
return !!(con->flags & CON_ENABLED);
}

?

--
With Best Regards,
Andy Shevchenko