Re: [RFC][PATCH 7/7] printk: new printk() recursion detection

From: Petr Mladek
Date: Thu Sep 29 2016 - 09:20:34 EST


On Tue 2016-09-27 23:22:37, Sergey Senozhatsky wrote:
> We can't use alt_printk when we are flushing alt_printk buffers to logbuf,
> because otherwise we would just flush messages back to alt_printk buffers.
> At the same time, this opens a door for potential deadlock, should flushing
> endup in recursive printk() call:
>
> irq_work_run()
> alt_printk_flush_line()
> printk()
> WARN_ON()
> ^^^^^^^^^
> depending on circumstances this can result in printk-deadlock.
>
> To cope with it, we can use alt_printk context ->entry_count:
> - __alt_printk_flush() sets ->entry_count to 1
> - every printk() call from alt_printk_flush_foo() calls
> alt_printk_enter() and alt_printk_exit()
> - alt_printk_enter() increments ->entry_count and tests the value:
> - if it's `==2' then we are in normal alt_printk_flush()->printk() path
> - if it's `>=3' then we are in recursion. The first time we detect
> recursion (->entry_count == 3) we switch from default printk() to
> alt_printk(), to avoid possible deadlocks.
> - in alt_printk_exit() we test if we were in normal printk() path or
> in printk() recursion and rollback to default printk() if so;
> and decrement ->entry_count.

I am sorry but I do not understand this much. printk() should set the
alternative implementation in the critical section by default.
Why do we need to handle this so specially?

Is it because of flushing in NMI context when panicing? I would call
vprintk_emit() directly from the flush_line() function in this case.
Then all other possible error printk's will get redirected to the
NMI buffer which is good enouh.

Best Regards,
Petr