Re: [PATCH v10 1/2] printk: Make printk() completely async

From: Petr Mladek
Date: Fri Aug 19 2016 - 05:55:05 EST


On Fri 2016-08-19 15:32:36, Sergey Senozhatsky wrote:
> On (08/18/16 12:56), Petr Mladek wrote:
> > The advantage of the printk_func trick is that it is transparent.
> > You do not need to modify any existing functions used by WARN()/BUG()
> > macros.
>
> good point.
>
> so something like below, perhaps. I'm less sure about
> deferred BUG()/BUG_ON():
>
> #define DEFERRED_BUG() do { \
> printk_deferred_enter(); \
> BUG(); \
> printk_deferred_exit(); \
> } while (0) \
>
> #define DEFERRED_BUG_ON(condition) do { \
> printk_deferred_enter(); \
> BUG_ON(condition); \
> printk_deferred_exit(); \
> } while (0)
>
> depending on .config BUG() may never return back -- passing control
> to do_exit(), so printk_deferred_exit() won't be executed. thus we
> probably need to have a per-cpu variable that would indicate that
> we are in deferred_bug. hm... but do we really need deferred BUG()
> in the first place?

Good question. I am not aware of any BUG_ON() that would be called from
wake_up_process() but it is hard to check everything.

A conservative approach would be to force synchronous printk from
BUG_ON().


> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
>
> ---
>
> include/asm-generic/bug.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++
> include/linux/printk.h | 6 ++++++
> kernel/printk/internal.h | 18 +----------------
> kernel/printk/printk.c | 42 ++++++++++++++++++++++++++++++++++-----
> 4 files changed, 94 insertions(+), 22 deletions(-)
>
> diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
> index 6f96247..d72ee1e 100644
> --- a/include/asm-generic/bug.h
> +++ b/include/asm-generic/bug.h
> @@ -60,6 +60,10 @@ struct bug_entry {
> * significant issues that need prompt attention if they should ever
> * appear at runtime. Use the versions with printk format strings
> * to provide better diagnostics.
> + *
> + * DEFERRED_WARN macros call printk_deferred() to print the messages
> + * and are meant to be used from the contexts where direct printk()
> + * can deadlock the system.
> */
> #ifndef __WARN_TAINT
> extern __printf(3, 4)
> @@ -145,6 +149,52 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
> unlikely(__ret_warn_once); \
> })
>
> +#define DEFERRED_WARN_ON(condition) ({ \
> + int __ret_warn_on = !!(condition); \
> + if (unlikely(__ret_warn_on)) { \
> + printk_deferred_enter(); \
> + __WARN(); \
> + printk_deferred_exit(); \
> + } \
> + unlikely(__ret_warn_on); \
> +})

This looks reasonable to me.


> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 0d3e026..6e260a0 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
>
> +void printk_deferred_enter(void)
> +{
> + preempt_disable();
> + this_cpu_write(printk_func, vprintk_deferred);
> +}
> +EXPORT_SYMBOL(printk_deferred_enter);
> +
> +void printk_deferred_exit(void)
> +{
> + this_cpu_write(printk_func, vprintk_default);
> + preempt_enable();
> +}
> +EXPORT_SYMBOL(printk_deferred_exit);
> +

This is racy with printk_nmi_enter() and printk_nmi_exit().
It need to work both ways. It must keep printk_deferred()
even when DEFERRED_WARN_ON() is called in nmi context.
Also it must keep printk_deferred() when the DEFERRED_WARN_ON()
is interrupted by an nmi.

It think that best solution would be to allow nesting.
What about replacing "printk_func" per-CPU variable
with a per-CPU atomic counter. Then we could just
check the counter in vprintk_emit() to see if it is
deferred or not.

The approach with printk_func() was more generic. We thought
that it might be used even for a transparent early_printk().
But it still might be solved even with the per-CPU atomic
counter. We could fallback to the early_printk when a
global flag is set or so.

Best Regards,
Petr