Re: [PATCH v4] lib/spinlock_debug.c: prevent a recursive cycle in the debug code

From: Byungchul Park
Date: Fri Jan 29 2016 - 07:16:14 EST


On Fri, Jan 29, 2016 at 01:05:00PM +0900, Sergey Senozhatsky wrote:
> then this will explode:
>
> printk
> spin_lock
> >> coding error <<
> spin_unlock
> printk
> spin_lock
> printk
> spin_lock
> printk
> spin_lock
> ... boom
>
> vprintk_emit() recursion detection code will not work for logbuf_lock here.
> because the only criteria how vprintk_emit() can detect a recursion is via
> static `logbuf_cpu' which is set to UINT_MAX right before it
> raw_spin_unlock(&logbuf_lock). so from vprintk_emit() POV the logbuf_lock is
> already unlocked. which is not true.
>
>
> in case of memory corruption I don't think we must care, 'coding error case'
> is _probably/may be_ something that can be improved, but I'm not really 100%
> sure... and this still doesn't explain your console_sem.lock case.

Hello, I found the case this bad thing can happen. So the thought occurred
struck me that we need a patch, similar to my v3 patch, even though the
consideration of logbug_lock in the v3 patch may not be necessary now.

cpu0
====
printk
console_trylock
console_unlock
up_console_sem
up
raw_spin_lock_irqsave(&sem->lock, flags)
__up
wake_up_process
try_to_wake_up
raw_spin_lock_irqsave(&p->pi_lock)
__spin_lock_debug
spin_dump // once it happened
printk
console_trylock
raw_spin_lock_irqsave(&sem->lock, flags)

<=== DEADLOCK

cpu1
====
printk
console_trylock
raw_spin_lock_irqsave(&sem->lock, flags)
__spin_lock_debug
spin_dump
printk
...

<=== repeat the recursive cycle infinitely

This was the my v3 patch.
-----8<-----