Skip to contentSkip to conversation

tty: discard flip-buffer input for TCSAFLUSH

PATCH In review Hungyu Lin 2 participants 4 messages Oct 27 TTY layer and serial drivers

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v2 1/1 tty: discard flip-buffer input for TCSAFLUSH

set_termios() handles TERMIOS_FLUSH by clearing only the line discipline's input buffer. Data already committed to the tty flip buffers can be delivered after that flush, leaving old input readable after a successful tcsetattr(TCSAFLUSH).

This can be reproduced with a noncanonical PTY: complete an 8192-byte master write, wait until the slave reports 4095 unread bytes, and apply TCSAFLUSH. The remaining 4097 bytes are still readable afterwards. Small amounts of pending input can also survive if they have not reached the line discipline yet.

Use tty_buffer_flush() with the referenced line discipline to discard both receive queues under the existing buffer lock. If the line discipline has a flush_buffer callback, also unthrottle the tty after releasing that lock, as TCIFLUSH does. Otherwise a paused input source or sleeping PTY writer can remain stopped after the queues are emptied.

---
Changes in v2:
- Unthrottle after flushing when the line discipline has a flush_buffer callback, matching TCIFLUSH and addressing Sashiko's review.
- Add local blocked-writer and serial-type N_TTY flow-control validation; rerun the original functional and sudo/PAM tests. Test harnesses are kept outside this patch.

 drivers/tty/tty_ioctl.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/tty/tty_ioctl.c +5 -2
index 90c70d8d14e3..be764f04bf3a 100644
@@ -496,8 +496,11 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
 
 		ld = tty_ldisc_ref(tty);
 		if (ld != NULL) {
-			if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
-				ld->ops->flush_buffer(tty);
+			if (opt & TERMIOS_FLUSH) {
+				tty_buffer_flush(tty, ld);
+				if (ld->ops->flush_buffer)
+					tty_unthrottle(tty);
+			}
 			tty_ldisc_deref(ld);
 		}
 
Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

set_termios() handles TERMIOS_FLUSH by clearing only the line discipline's input buffer. Data already committed to the tty flip buffers can be delivered after that flush, leaving old input readable after a successful tcsetattr(TCSAFLUSH).

This can be reproduced with a noncanonical PTY: complete an 8192-byte master write, wait until the slave reports 4095 unread bytes, and apply TCSAFLUSH. The remaining 4097 bytes are still readable afterwards. Small amounts of pending input can also survive if they have not reached the line discipline yet.

Quoted 7 lines

Use tty_buffer_flush() with the referenced line discipline to discard both receive queues under the existing buffer lock. If the line discipline has a flush_buffer callback, also unthrottle the tty after releasing that lock, as TCIFLUSH does. Otherwise a paused input source or sleeping PTY writer can remain stopped after the queues are emptied.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw
Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Please disregard v2 for now. Further testing found regressions with PPP/SLIP and a locking issue with VT selection paste.

I'm still investigating and don't have a replacement ready.

Thanks,
Hungyu

author
#3 lore raw
In reply to Hungyu Lin
Quoted 1 line

I'm still investigating and don't have a replacement ready.

Fixing this properly involves more changes than I expected, so I won't be pursuing it further. Please disregard both v1 and v2.

Thanks,
Hungyu