tty/hvc_console: fix console lock ordering with spinlock
PATCH 1/1 tty/hvc_console: fix console lock ordering with spinlock
hvc_remove() takes a spin lock first then acquires the console semaphore. This situation can easily lead to a deadlock scenario where we call scheduler with spin lock held.
--- drivers/tty/hvc/hvc_console.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/hvc/hvc_console.c +2 -2
@@ -920,17 +920,17 @@ int hvc_remove(struct hvc_struct *hp) tty =tty_port_tty_get (&hp->port); +console_lock ();spin_lock_irqsave (&hp->lock, flags);if (hp->index <MAX_NR_HVC_CONSOLES ) { -console_lock (); vtermnos[hp->index] =-1 ; cons_ops[hp->index] =NULL ; -console_unlock (); }/* Don't whack hp->irq because tty_hangup() will need to free the irq. */ spin_unlock_irqrestore (&hp->lock, flags); +console_unlock ();/* * We 'put' the instance that was grabbed when the kref instance
Signature · 1 line
1.8.3.1
Quoted 3 lines · Denis Kirjanov
hvc_remove()takes a spin lock first then acquires the console semaphore. This situation can easily lead to a deadlock scenario where we call scheduler with spin lock held.
Have you actually hit the deadlock? Because that code's been like that for years and I've never received a bug report.
Quoted 11 lines
drivers/tty/hvc/hvc_console.c +1
-0@@ -920,17 +920,17 @@ int hvc_remove(struct hvc_struct *hp) tty =tty_port_tty_get (&hp->port); +console_lock ();spin_lock_irqsave (&hp->lock, flags);if (hp->index <MAX_NR_HVC_CONSOLES ) {
drivers/tty/hvc/hvc_console.c int hvc_remove(struct hvc_struct *hp) · line 933
Quoted 10 lines
drivers/tty/hvc/hvc_console.c +1
-2-console_lock (); vtermnos[hp->index] =-1 ; cons_ops[hp->index] =NULL ; -console_unlock (); }/* Don't whack hp->irq because tty_hangup() will need to free the irq. */ spin_unlock_irqrestore (&hp->lock, flags); +console_unlock ();
I get that you're trying to do the minimal change, but I don't think the result is ideal. If this isn't a console hvc then we take both locks but do nothing.
So what about:
if (hp->index < MAX_NR_HVC_CONSOLES) {
console_lock();
spin_lock_irqsave(&hp->lock, flags);
vtermnos[hp->index] = -1;
cons_ops[hp->index] = NULL;
spin_unlock_irqrestore(&hp->lock, flags);
console_unlock();
}cheers
Quoted 8 lines · Michael Ellerman
Quoted 3 lines · Denis Kirjanov
hvc_remove()takes a spin lock first then acquires the console semaphore. This situation can easily lead to a deadlock scenario where we call scheduler with spin lock held.Have you actually hit the deadlock? Because that code's been like that for years and I've never received a bug report.
Nope, I didn't. I've found the bug in the code while looking at the lockdep output
Quoted 31 lines
Quoted 14 lines
drivers/tty/hvc/hvc_console.c +1
-1@@ -920,17 +920,17 @@ int hvc_remove(struct hvc_struct *hp) tty =tty_port_tty_get (&hp->port); +console_lock ();spin_lock_irqsave (&hp->lock, flags);if (hp->index <MAX_NR_HVC_CONSOLES ) { -console_lock ();drivers/tty/hvc/hvc_console.c int hvc_remove(struct hvc_struct *hp) · line 934
Quoted 10 lines
drivers/tty/hvc/hvc_console.c +1
-1vtermnos[hp->index] =-1 ; cons_ops[hp->index] =NULL ; -console_unlock (); }/* Don't whack hp->irq because tty_hangup() will need to free the irq. */ spin_unlock_irqrestore (&hp->lock, flags); +console_unlock ();I get that you're trying to do the minimal change, but I don't think the result is ideal. If this isn't a console hvc then we take both locks but do nothing.
So what about:
Quoted 8 lines
if (hp->index < MAX_NR_HVC_CONSOLES) {
console_lock();
spin_lock_irqsave(&hp->lock, flags);
vtermnos[hp->index] = -1;
cons_ops[hp->index] = NULL;
spin_unlock_irqrestore(&hp->lock, flags);
console_unlock();
}Are you sure that we don't corrupt the hp->index between hvc_poll in interrupt context and hvc_remoev?
Quoted 3 lines
cheers