[PATCH] vt: drop console_lock before calling __do_SAK() in vc_SAK()
From: Jiayi Zhang
Date: Wed Dec 02 2026 - 13:12:38 EST
vc_SAK() calls __do_SAK() on the console's tty with console_lock held.
__do_SAK() flushes the tty, and tty_buffer_flush() takes the buffer
lock of the tty. flush_to_ldisc() holds that buffer lock while it
passes input to the line discipline, and n_tty calls con_flush_chars(),
which takes console_lock, after each batch of input:
vc_SAK() flush_to_ldisc()
console_lock() mutex_lock(&buf->lock)
__do_SAK() n_tty_receive_buf_common()
tty_ldisc_flush() con_flush_chars()
tty_buffer_flush() console_lock()
mutex_lock(&buf->lock)
So a SAK on a virtual console that is receiving input can deadlock.
syzbot reports this dependency (console_lock -> &buf->lock) as part of
two cycles, one through termios_rwsem and n_tty's receive path, and one
through the serial port mutex.
Take a reference to the tty with tty_port_tty_get() while holding
console_lock, drop console_lock for __do_SAK(), and take it again for
reset_vc(). Commit 8b6312f4dcc1 ("[PATCH] vt: refactor console SAK
processing") held the lock across __do_SAK() so that nothing else
changes the console during SAK. __do_SAK() does not need it: it
flushes the tty and kills its users, and do_SAK_work() calls it
without the console lock for every other tty. The modes that SAK
resets are reset by reset_vc(), which still runs under the lock after
the processes have been signalled, so a change made in between is
undone. The reference keeps the tty alive and delays con_cleanup(), so
vt_disallocate() cannot free the console meanwhile. It also resolves
the old FIXME: vc_SAK() used vc->port.tty without holding a reference.
Fixes: 42fd552e8647 ("fix serial buffer memory leak")
Reported-by: syzbot+52cf91760dcb1dac6376@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=52cf91760dcb1dac6376
Reported-by: syzbot+d84934b4184d7f9a1aed@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=d84934b4184d7f9a1aed
Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
---
Tested in QEMU with the syzbot C reproducers of both reports and their
kernel configs. Both trigger rarely here, so the numbers are small:
- tty_buffer_flush report: without the patch 1 of 4 instances
reported the deadlock; with the patch none of 11 did.
- n_tty_flush_buffer report, with one tcsetattr() on /dev/ttyS0 added
to the reproducer (a getty on the serial console does this on the
syzbot machines; it records the serial half of that cycle): without
the patch 1 of 5; with the patch none of 9.
An earlier patch for the second report moved the ldisc flush in
tty_buffer_flush() out of buf->lock:
https://lore.kernel.org/all/20260804140251.8197-1-omermetekaya0@xxxxxxxxx/
This patch removes the console_lock -> buf->lock dependency in
vc_SAK() instead.
AI tools assisted with the analysis, the code and the changelog.
drivers/tty/vt/vt_ioctl.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
index ecefe4a..62a52df 100644
--- a/drivers/tty/vt/vt_ioctl.c
+++ b/drivers/tty/vt/vt_ioctl.c
@@ -986,17 +986,25 @@ void vc_SAK(struct work_struct *work)
struct vc_data *vc;
struct tty_struct *tty;
- guard(console_lock)();
+ console_lock();
vc = vc_con->d;
- if (!vc)
+ if (!vc) {
+ console_unlock();
return;
+ }
- /* FIXME: review tty ref counting */
- tty = vc->port.tty;
+ tty = tty_port_tty_get(&vc->port);
/* SAK should also work in all raw modes and reset them properly. */
- if (tty)
+ if (tty) {
+ console_unlock();
__do_SAK(tty);
- reset_vc(vc);
+ tty_kref_put(tty);
+ console_lock();
+ vc = vc_con->d;
+ }
+ if (vc)
+ reset_vc(vc);
+ console_unlock();
}
#ifdef CONFIG_COMPAT
--
2.53.0