tty: fix lock order inversion in tty_buffer_flush()
PATCH 1/1 tty: fix lock order inversion in tty_buffer_flush()
tty_buffer_flush() calls ld->ops->flush_buffer() while holding buf->lock. For the N_TTY line discipline, flush_buffer() is n_tty_flush_buffer(), which acquires termios_rwsem.
However, the documented stable lock order (see tty.h) is:
termios_rwsem -> buf->lock
Calling flush_buffer() under buf->lock inverts this order and creates a circular locking dependency detected by lockdep:
&buf->lock -> console_lock -> &port->mutex -> &tty->termios_rwsem
-> &buf->lock
Fix this by moving the flush_buffer() call outside buf->lock. The buffer data has already been discarded at this point, so the ordering change is safe.
--- drivers/tty/tty_buffer.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/tty/tty_buffer.c +7 -3
@@ -244,11 +244,15 @@ void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld) buf->head->read = buf->head->commit; buf->head->lookahead = buf->head->read; -if (ld && ld->ops->flush_buffer) - ld->ops->flush_buffer (tty); -atomic_dec (&buf->priority);mutex_unlock (&buf->lock); +/* +* Call flush_buffer() outside buf->lock: n_tty_flush_buffer() +* acquires termios_rwsem, but the required lock order is +* termios_rwsem -> buf->lock, not the other way around. +*/ +if (ld && ld->ops->flush_buffer) + ld->ops->flush_buffer (tty); }/**
Signature · 1 line
2.55.0
Greg KH
maintainerQuoted 11 lines · Ömer Mete Kaya
tty_buffer_flush()callsld->ops->flush_buffer()while holdingbuf->lock. For theN_TTYline discipline,flush_buffer()isn_tty_flush_buffer(), which acquirestermios_rwsem.However, the documented stable lock order (see
tty.h) is:
termios_rwsem->buf->lockCalling
flush_buffer()underbuf->lockinverts this order and creates a circular locking dependency detected by lockdep:
&buf->lock->console_lock->&port->mutex->&tty->termios_rwsem
Quoted 8 lines
-> &buf->lockFix this by moving the
flush_buffer()call outsidebuf->lock. The buffer data has already been discarded at this point, so the ordering change is safe.
Did syzbot actually test this? How did you test this?
Did you forget an Assisted-by: tag?
Quoted 15 lines
Signed-off-by Ömer Mete Kaya <[email protected]>--- drivers/tty/tty_buffer.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)drivers/tty/tty_buffer.c +0
-2@@ -244,11 +244,15 @@ void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld) buf->head->read = buf->head->commit; buf->head->lookahead = buf->head->read; -if (ld && ld->ops->flush_buffer) - ld->ops->flush_buffer (tty);
drivers/tty/tty_buffer.c void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld) · line 255
Quoted 10 lines
drivers/tty/tty_buffer.c +7
-1-atomic_dec (&buf->priority);mutex_unlock (&buf->lock); +/* +* Call flush_buffer() outside buf->lock: n_tty_flush_buffer() +* acquires termios_rwsem, but the required lock order is +* termios_rwsem -> buf->lock, not the other way around. +*/ +if (ld && ld->ops->flush_buffer) + ld->ops->flush_buffer (tty);
This feels wrong, what is going to break with this change? What commit does this change fix?
thanks,
greg k-h