Skip to contentSkip to conversation

tty/serial: Process XON/XOFF robustly

PATCH Superseded Ilpo Järvinen 3 participants 12 messages Apr 5, 2022 – Apr 6, 2022

Subsystems (15)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover tty/serial: Process XON/XOFF robustly

XON/XOFF are used for software flow-control on serial lines. XON and XOFF appear as characters within the stream but should be processed as soon as possible.

The characters received by the UART drivers are in intermediate buffers until TTY receives them. In the case where the TTY is not read from, the characters may get stuck into those intermediate buffers until user-space reads from the TTY. Among the characters stuck in the buffers, can also be those XON/XOFF flow control characters. A stuck flow-control character is not very useful.

This patch series addresses the issue by checking if TTY is slow to process characters, that is, eats less than the given amount. If TTY is slow, a lookahead is invoked for the characters that remain in the intermediate buffer(s).

Then at a later time, receive_buf needs to ensure the flow-control actions are not retaken when those same characters get pushed to TTY. The necessary guards for that are added by the first patch of the series.

This patch series fixes an issue but I'm not able to pinpoint to a specific commit id to provide a Fixes tag.

Ilpo Järvinen (2):
tty: Add lookahead param to receive_buf
tty: Implement lookahead to process XON/XOFF timely

 drivers/accessibility/speakup/spk_ttyio.c |  2 +-
 drivers/bluetooth/hci_ldisc.c             |  2 +-
 drivers/char/pcmcia/synclink_cs.c         |  2 +-
 drivers/input/serio/serport.c             |  2 +-
 drivers/isdn/capi/capi.c                  |  2 +-
 drivers/misc/ti-st/st_core.c              |  2 +-
 drivers/net/caif/caif_serial.c            |  4 +-
 drivers/net/can/slcan.c                   |  2 +-
 drivers/net/hamradio/6pack.c              |  2 +-
 drivers/net/hamradio/mkiss.c              |  2 +-
 drivers/net/mctp/mctp-serial.c            |  2 +-
 drivers/net/ppp/ppp_async.c               |  2 +-
 drivers/net/ppp/ppp_synctty.c             |  2 +-
 drivers/net/slip/slip.c                   |  2 +-
 drivers/tty/n_gsm.c                       |  2 +-
 drivers/tty/n_hdlc.c                      |  2 +-
 drivers/tty/n_null.c                      |  2 +-
 drivers/tty/n_tty.c                       | 85 ++++++++++++++++-------
 drivers/tty/serdev/serdev-ttyport.c       |  3 +-
 drivers/tty/synclink_gt.c                 |  2 +-
 drivers/tty/tty_buffer.c                  | 68 ++++++++++++++----
 drivers/tty/tty_io.c                      |  2 +-
 drivers/tty/tty_port.c                    | 27 ++++++-
 drivers/tty/vt/selection.c                |  2 +-
 include/linux/tty_buffer.h                |  1 +
 include/linux/tty_flip.h                  |  2 +-
 include/linux/tty_ldisc.h                 |  6 +-
 include/linux/tty_port.h                  |  3 +-
 net/nfc/nci/uart.c                        |  2 +-
 sound/soc/codecs/cx20442.c                |  2 +-
 sound/soc/ti/ams-delta.c                  |  2 +-
 31 files changed, 174 insertions(+), 69 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 tty: Add lookahead param to receive_buf

After lookahead for XON/XOFF characters is added by the next patch, the receive side needs to ensure the flow-control actions are not retaken later on when those same characters get read by TTY.

Thus, pass lookahead count to receive_buf and skip flow-control character actions if already taken for the character in question. Lookahead count will become live after the next patch.

---
 drivers/accessibility/speakup/spk_ttyio.c |  2 +-
 drivers/bluetooth/hci_ldisc.c             |  2 +-
 drivers/char/pcmcia/synclink_cs.c         |  2 +-
 drivers/input/serio/serport.c             |  2 +-
 drivers/isdn/capi/capi.c                  |  2 +-
 drivers/misc/ti-st/st_core.c              |  2 +-
 drivers/net/caif/caif_serial.c            |  4 +-
 drivers/net/can/slcan.c                   |  2 +-
 drivers/net/hamradio/6pack.c              |  2 +-
 drivers/net/hamradio/mkiss.c              |  2 +-
 drivers/net/mctp/mctp-serial.c            |  2 +-
 drivers/net/ppp/ppp_async.c               |  2 +-
 drivers/net/ppp/ppp_synctty.c             |  2 +-
 drivers/net/slip/slip.c                   |  2 +-
 drivers/tty/n_gsm.c                       |  2 +-
 drivers/tty/n_hdlc.c                      |  2 +-
 drivers/tty/n_null.c                      |  2 +-
 drivers/tty/n_tty.c                       | 50 +++++++++++++----------
 drivers/tty/serdev/serdev-ttyport.c       |  3 +-
 drivers/tty/synclink_gt.c                 |  2 +-
 drivers/tty/tty_buffer.c                  |  8 ++--
 drivers/tty/tty_io.c                      |  2 +-
 drivers/tty/tty_port.c                    |  5 ++-
 drivers/tty/vt/selection.c                |  2 +-
 include/linux/tty_flip.h                  |  2 +-
 include/linux/tty_ldisc.h                 |  4 +-
 include/linux/tty_port.h                  |  2 +-
 net/nfc/nci/uart.c                        |  2 +-
 sound/soc/codecs/cx20442.c                |  2 +-
 sound/soc/ti/ams-delta.c                  |  2 +-
 30 files changed, 66 insertions(+), 56 deletions(-)
drivers/accessibility/speakup/spk_ttyio.c +1 -1
index 08cf8a17754b..86b379c86a49 100644
@@ -73,7 +73,7 @@ static void spk_ttyio_ldisc_close(struct tty_struct *tty)
 
 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
 				  const unsigned char *cp,
-				  const char *fp, int count)
+				  const char *fp, int count, int lookahead_count)
 {
 	struct spk_ldisc_data *ldisc_data = tty->disc_data;
 	struct spk_synth *synth = ldisc_data->synth;
drivers/bluetooth/hci_ldisc.c +1 -1
index f537673ede17..737bd9a01355 100644
@@ -596,7 +596,7 @@ static void hci_uart_tty_wakeup(struct tty_struct *tty)
  * Return Value:    None
  */
 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
-				 const char *flags, int count)
+				 const char *flags, int count, int lookahead_count)
 {
 	struct hci_uart *hu = tty->disc_data;
 
drivers/char/pcmcia/synclink_cs.c +1 -1
index 78baba55a8b5..de9c151cfb12 100644
@@ -501,7 +501,7 @@ static void ldisc_receive_buf(struct tty_struct *tty,
 	ld = tty_ldisc_ref(tty);
 	if (ld) {
 		if (ld->ops->receive_buf)
-			ld->ops->receive_buf(tty, data, flags, count);
+			ld->ops->receive_buf(tty, data, flags, count, 0);
 		tty_ldisc_deref(ld);
 	}
 }
drivers/input/serio/serport.c +1 -1
index 669a728095b8..320aac89e967 100644
@@ -115,7 +115,7 @@ static void serport_ldisc_close(struct tty_struct *tty)
  */
 
 static void serport_ldisc_receive(struct tty_struct *tty,
-		const unsigned char *cp, const char *fp, int count)
+		const unsigned char *cp, const char *fp, int count, int lookahead_count)
 {
 	struct serport *serport = (struct serport*) tty->disc_data;
 	unsigned long flags;
drivers/isdn/capi/capi.c +1 -1
index 0f00be62438d..beb4c78a7219 100644
@@ -454,7 +454,7 @@ static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
 		skb_pull(skb, CAPIMSG_LEN(skb->data));
 		pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
 			 datahandle, skb->len);
-		ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
+		ld->ops->receive_buf(tty, skb->data, NULL, skb->len, 0);
 	} else {
 		printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
 		       errcode);
drivers/misc/ti-st/st_core.c +1 -1
index 7f6976a9f508..b3ef0a593843 100644
@@ -797,7 +797,7 @@ static void st_tty_close(struct tty_struct *tty)
 }
 
 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
-			   const char *tty_flags, int count)
+			   const char *tty_flags, int count, int lookahead_count)
 {
 #ifdef VERBOSE
 	print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
drivers/net/caif/caif_serial.c +2 -2
index 688075859ae4..5ba25b98a445 100644
@@ -159,7 +159,7 @@ static inline void debugfs_tx(struct ser_device *ser, const u8 *data, int size)
 #endif
 
 static void ldisc_receive(struct tty_struct *tty, const u8 *data,
-			const char *flags, int count)
+			const char *flags, int count, int lookahead_count)
 {
 	struct sk_buff *skb = NULL;
 	struct ser_device *ser;
@@ -237,7 +237,7 @@ static int handle_tx(struct ser_device *ser)
 			update_tty_status(ser);
 		} else {
 			tty_wr = len;
-			ldisc_receive(tty, skb->data, NULL, len);
+			ldisc_receive(tty, skb->data, NULL, len, 0);
 		}
 		ser->dev->stats.tx_packets++;
 		ser->dev->stats.tx_bytes += tty_wr;
drivers/net/can/slcan.c +1 -1
index ec294d0c5722..4cfdd65468a2 100644
@@ -471,7 +471,7 @@ static void slc_setup(struct net_device *dev)
 
 static void slcan_receive_buf(struct tty_struct *tty,
 			      const unsigned char *cp, const char *fp,
-			      int count)
+			      int count, int lookahead_count)
 {
 	struct slcan *sl = (struct slcan *) tty->disc_data;
 
drivers/net/hamradio/6pack.c +1 -1
index 45c3c4a1101b..06963e3e57f7 100644
@@ -427,7 +427,7 @@ static void sixpack_write_wakeup(struct tty_struct *tty)
  * and sent on to some IP layer for further processing.
  */
 static void sixpack_receive_buf(struct tty_struct *tty,
-	const unsigned char *cp, const char *fp, int count)
+	const unsigned char *cp, const char *fp, int count, int lookahead_count)
 {
 	struct sixpack *sp;
 	int count1;
drivers/net/hamradio/mkiss.c +1 -1
index c251e04ae047..c8f2f2c98f37 100644
@@ -875,7 +875,7 @@ static int mkiss_ioctl(struct tty_struct *tty, unsigned int cmd,
  * and sent on to the AX.25 layer for further processing.
  */
 static void mkiss_receive_buf(struct tty_struct *tty, const unsigned char *cp,
-	const char *fp, int count)
+	const char *fp, int count, int lookahead_count)
 {
 	struct mkiss *ax = mkiss_get(tty);
 
drivers/net/mctp/mctp-serial.c +1 -1
index 7cd103fd34ef..d5f09b745bad 100644
@@ -390,7 +390,7 @@ static void mctp_serial_push(struct mctp_serial *dev, unsigned char c)
 
 static void mctp_serial_tty_receive_buf(struct tty_struct *tty,
 					const unsigned char *c,
-					const char *f, int len)
+					const char *f, int len, int lookahead_count)
 {
 	struct mctp_serial *dev = tty->disc_data;
 	int i;
drivers/net/ppp/ppp_async.c +1 -1
index 15a179631903..96774392f783 100644
@@ -338,7 +338,7 @@ ppp_asynctty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
 /* May sleep, don't call from interrupt level or with interrupts disabled */
 static void
 ppp_asynctty_receive(struct tty_struct *tty, const unsigned char *buf,
-		  const char *cflags, int count)
+		     const char *cflags, int count, int lookahead_count)
 {
 	struct asyncppp *ap = ap_get(tty);
 	unsigned long flags;
drivers/net/ppp/ppp_synctty.c +1 -1
index 18283b7b94bc..2c60d64caadc 100644
@@ -331,7 +331,7 @@ ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
 /* May sleep, don't call from interrupt level or with interrupts disabled */
 static void
 ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
-		  const char *cflags, int count)
+		 const char *cflags, int count, int lookahead_count)
 {
 	struct syncppp *ap = sp_get(tty);
 	unsigned long flags;
drivers/net/slip/slip.c +1 -1
index 88396ff99f03..3e71932e67ff 100644
@@ -686,7 +686,7 @@ static void sl_setup(struct net_device *dev)
  */
 
 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp,
-		const char *fp, int count)
+			     const char *fp, int count, int lookahead_count)
 {
 	struct slip *sl = tty->disc_data;
 
drivers/tty/n_gsm.c +1 -1
index fa92f727fdf8..8cdef72f7b48 100644
@@ -2500,7 +2500,7 @@ static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
 }
 
 static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
-			      const char *fp, int count)
+			      const char *fp, int count, int lookahead_count)
 {
 	struct gsm_mux *gsm = tty->disc_data;
 	char flags = TTY_NORMAL;
drivers/tty/n_hdlc.c +1 -1
index 94c1ec2dd754..a62e7bbadc4a 100644
@@ -379,7 +379,7 @@ static void n_hdlc_tty_wakeup(struct tty_struct *tty)
  * interpreted as one HDLC frame.
  */
 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
-			       const char *flags, int count)
+			       const char *flags, int count, int lookahead_count)
 {
 	register struct n_hdlc *n_hdlc = tty->disc_data;
 	register struct n_hdlc_buf *buf;
drivers/tty/n_null.c +1 -1
index f913b665af72..a78500865625 100644
@@ -34,7 +34,7 @@ static ssize_t n_null_write(struct tty_struct *tty, struct file *file,
 
 static void n_null_receivebuf(struct tty_struct *tty,
 				 const unsigned char *cp, const char *fp,
-				 int cnt)
+				 int cnt, int lookahead_count)
 {
 }
 
drivers/tty/n_tty.c +29 -21

137 lines not on this page · raw message

drivers/tty/serdev/serdev-ttyport.c +2 -1
index d367803e2044..dbc79c2c2643 100644
@@ -23,7 +23,8 @@ struct serport {
  */
 
 static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
-				const unsigned char *fp, size_t count)
+			       const unsigned char *fp, size_t count,
+			       size_t lookahead_count)
 {
 	struct serdev_controller *ctrl = port->client_data;
 	struct serport *serport = serdev_controller_get_drvdata(ctrl);
drivers/tty/synclink_gt.c +1 -1
index 25c558e65ece..a669e6e13f90 100644
@@ -582,7 +582,7 @@ static void ldisc_receive_buf(struct tty_struct *tty,
 	ld = tty_ldisc_ref(tty);
 	if (ld) {
 		if (ld->ops->receive_buf)
-			ld->ops->receive_buf(tty, data, flags, count);
+			ld->ops->receive_buf(tty, data, flags, count, 0);
 		tty_ldisc_deref(ld);
 	}
 }
drivers/tty/tty_buffer.c +4 -4
index 646510476c30..38863fe8544f 100644
@@ -445,14 +445,14 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
  * Returns: the number of bytes processed.
  */
 int tty_ldisc_receive_buf(struct tty_ldisc *ld, const unsigned char *p,
-			  const char *f, int count)
+			  const char *f, int count, int lookahead_count)
 {
 	if (ld->ops->receive_buf2)
-		count = ld->ops->receive_buf2(ld->tty, p, f, count);
+		count = ld->ops->receive_buf2(ld->tty, p, f, count, lookahead_count);
 	else {
 		count = min_t(int, count, ld->tty->receive_room);
 		if (count && ld->ops->receive_buf)
-			ld->ops->receive_buf(ld->tty, p, f, count);
+			ld->ops->receive_buf(ld->tty, p, f, count, lookahead_count);
 	}
 	return count;
 }
@@ -468,7 +468,7 @@ receive_buf(struct tty_port *port, struct tty_buffer *head, int count)
 	if (~head->flags & TTYB_NORMAL)
 		f = flag_buf_ptr(head, head->read);
 
-	n = port->client_ops->receive_buf(port, p, f, count);
+	n = port->client_ops->receive_buf(port, p, f, count, 0);
 	if (n > 0)
 		memset(p, 0, n);
 	return n;
drivers/tty/tty_io.c +1 -1
index 8fec1d8648f5..cd0b74a5ceb2 100644
@@ -2290,7 +2290,7 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
 		return -EIO;
 	tty_buffer_lock_exclusive(tty->port);
 	if (ld->ops->receive_buf)
-		ld->ops->receive_buf(tty, &ch, &mbz, 1);
+		ld->ops->receive_buf(tty, &ch, &mbz, 1, 0);
 	tty_buffer_unlock_exclusive(tty->port);
 	tty_ldisc_deref(ld);
 	return 0;
drivers/tty/tty_port.c +3 -2
index 880608a65773..0d41ec0e5fee 100644
@@ -22,7 +22,8 @@
 
 static int tty_port_default_receive_buf(struct tty_port *port,
 					const unsigned char *p,
-					const unsigned char *f, size_t count)
+					const unsigned char *f, size_t count,
+					size_t lookahead_count)
 {
 	int ret;
 	struct tty_struct *tty;
@@ -36,7 +37,7 @@ static int tty_port_default_receive_buf(struct tty_port *port,
 	if (!disc)
 		return 0;
 
-	ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
+	ret = tty_ldisc_receive_buf(disc, p, (char *)f, count, lookahead_count);
 
 	tty_ldisc_deref(disc);
 
drivers/tty/vt/selection.c +1 -1
index f7755e73696e..abc3890ecc18 100644
@@ -406,7 +406,7 @@ int paste_selection(struct tty_struct *tty)
 		__set_current_state(TASK_RUNNING);
 		count = vc_sel.buf_len - pasted;
 		count = tty_ldisc_receive_buf(ld, vc_sel.buffer + pasted, NULL,
-					      count);
+					      count, 0);
 		pasted += count;
 	}
 	mutex_unlock(&vc_sel.lock);
include/linux/tty_flip.h +1 -1
index 483d41cbcbb7..3ba7b8c3b9cb 100644
@@ -42,7 +42,7 @@ static inline int tty_insert_flip_string(struct tty_port *port,
 }
 
 int tty_ldisc_receive_buf(struct tty_ldisc *ld, const unsigned char *p,
-		const char *f, int count);
+		const char *f, int count, int lookahead_count);
 
 void tty_buffer_lock_exclusive(struct tty_port *port);
 void tty_buffer_unlock_exclusive(struct tty_port *port);
include/linux/tty_ldisc.h +2 -2
index e85002b56752..496f4755ea05 100644
@@ -224,11 +224,11 @@ struct tty_ldisc_ops {
 	 * The following routines are called from below.
 	 */
 	void	(*receive_buf)(struct tty_struct *tty, const unsigned char *cp,
-			       const char *fp, int count);
+			       const char *fp, int count, int lookahead_count);
 	void	(*write_wakeup)(struct tty_struct *tty);
 	void	(*dcd_change)(struct tty_struct *tty, unsigned int status);
 	int	(*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
-				const char *fp, int count);
+				const char *fp, int count, int lookahead_count);
 
 	struct  module *owner;
 };
include/linux/tty_port.h +1 -1
index 58e9619116b7..1871a6a9cb00 100644
@@ -39,7 +39,7 @@ struct tty_port_operations {
 };
 
 struct tty_port_client_operations {
-	int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t);
+	int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t, size_t);
 	void (*write_wakeup)(struct tty_port *port);
 };
 
net/nfc/nci/uart.c +1 -1
index cc8fa9e36159..b26e295eadeb 100644
@@ -296,7 +296,7 @@ static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
  * Return Value:    None
  */
 static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
-				 const char *flags, int count)
+				 const char *flags, int count, int lookahead_count)
 {
 	struct nci_uart *nu = (void *)tty->disc_data;
 
sound/soc/codecs/cx20442.c +1 -1
index 1af0bf5f1e2f..6cd5da5fe244 100644
@@ -259,7 +259,7 @@ static void v253_hangup(struct tty_struct *tty)
 
 /* Line discipline .receive_buf() */
 static void v253_receive(struct tty_struct *tty, const unsigned char *cp,
-		const char *fp, int count)
+			 const char *fp, int count, int lookahead_count)
 {
 	struct snd_soc_component *component = tty->disc_data;
 	struct cx20442_priv *cx20442;
sound/soc/ti/ams-delta.c +1 -1
index b1a32545babd..a95491ba2309 100644
@@ -337,7 +337,7 @@ static void cx81801_hangup(struct tty_struct *tty)
 
 /* Line discipline .receive_buf() */
 static void cx81801_receive(struct tty_struct *tty, const unsigned char *cp,
-		const char *fp, int count)
+			    const char *fp, int count, int lookahead_count)
 {
 	struct snd_soc_component *component = tty->disc_data;
 	const unsigned char *c;
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] tty: Add lookahead param to receive_buf

Quoted 9 lines · Ilpo Järvinen

After lookahead for XON/XOFF characters is added by the next patch, the receive side needs to ensure the flow-control actions are not retaken later on when those same characters get read by TTY.

Thus, pass lookahead count to receive_buf and skip flow-control character actions if already taken for the character in question. Lookahead count will become live after the next patch.

...

Quoted 12 lines
drivers/tty/n_tty.c +4 -3
-static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
+static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
+				       bool lookahead_done)
 {
 	struct n_tty_data *ldata = tty->disc_data;
 
 	if (I_IXON(tty)) {
 		if (c == START_CHAR(tty)) {
-			start_tty(tty);
-			process_echoes(tty);
+			if (!lookahead_done) {
+				start_tty(tty);

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1240

Quoted 10 lines
drivers/tty/n_tty.c +4 -1
+				process_echoes(tty);
+			}
 			return;
 		}
 		if (c == STOP_CHAR(tty)) {
-			stop_tty(tty);
+			if (!lookahead_done)
+				stop_tty(tty);
 			return;
 		}

Wouldn't be cleaner to inside out the conditionals?

Quoted 1 line

}

	if (I_IXON(tty)) {
		if (lookahead_done) {
			// Can be joined, but I think this is better
			if (c == START_CHAR(tty))
				return;
			if (c == STOP_CHAR(tty))
				return;
		} else {
			if (c == START_CHAR(tty)) {
				start_tty(tty);
				process_echoes(tty);
				return;
			}
			if (c == STOP_CHAR(tty)) {
				stop_tty(tty);
				return;
			}
		}
	}

In my opinion this will show exactly what's going on when we have lookahead_done and when not.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw

Re: [PATCH 1/2] tty: Add lookahead param to receive_buf

Quoted 29 lines · Andy Shevchenko
Quoted 9 lines · Ilpo Järvinen

After lookahead for XON/XOFF characters is added by the next patch, the receive side needs to ensure the flow-control actions are not retaken later on when those same characters get read by TTY.

Thus, pass lookahead count to receive_buf and skip flow-control character actions if already taken for the character in question. Lookahead count will become live after the next patch.

...

Quoted 16 lines
drivers/tty/n_tty.c +6 -3
-static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
+static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
+				       bool lookahead_done)
  {
  	struct n_tty_data *ldata = tty->disc_data;
 
  	if (I_IXON(tty)) {
  		if (c == START_CHAR(tty)) {
-			start_tty(tty);
-			process_echoes(tty);
+			if (!lookahead_done) {
+				start_tty(tty);
+				process_echoes(tty);
+			}
  			return;
  		}
Quoted 8 lines

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1240

Quoted 6 lines
  		if (c == STOP_CHAR(tty)) {
drivers/tty/n_tty.c +2 -1
-			stop_tty(tty);
+			if (!lookahead_done)
+				stop_tty(tty);
  			return;
  		}

Wouldn't be cleaner to inside out the conditionals?

Seconded.

Quoted 3 lines
	if (I_IXON(tty)) {
		if (lookahead_done) {
			// Can be joined, but I think this is better

I would join them, IMO it'd be still easy to read and to follow too.

Quoted 8 lines
			if (c == START_CHAR(tty))
				return;
			if (c == STOP_CHAR(tty))
				return;
		} else {
			if (c == START_CHAR(tty)) {
				start_tty(tty);
				process_echoes(tty);
Quoted 8 lines
				return;
			}
			if (c == STOP_CHAR(tty)) {
				stop_tty(tty);
				return;
			}
		}
	}

thanks,

Signature · 2 lines
js
suse labs
maintainer
#4 lore raw

Re: [PATCH 1/2] tty: Add lookahead param to receive_buf

In reply to Ilpo Järvinen
Quoted 9 lines · Ilpo Järvinen

After lookahead for XON/XOFF characters is added by the next patch, the receive side needs to ensure the flow-control actions are not retaken later on when those same characters get read by TTY.

Thus, pass lookahead count to receive_buf and skip flow-control character actions if already taken for the character in question. Lookahead count will become live after the next patch.

...

include/linux/tty_ldisc.h struct tty_ldisc_ops { · line 231

Quoted 13 lines
include/linux/tty_ldisc.h +2 -2
@@ -224,11 +224,11 @@ struct tty_ldisc_ops {
  	 * The following routines are called from below.
  	 */
  	void	(*receive_buf)(struct tty_struct *tty, const unsigned char *cp,
-			       const char *fp, int count);
+			       const char *fp, int count, int lookahead_count);
  	void	(*write_wakeup)(struct tty_struct *tty);
  	void	(*dcd_change)(struct tty_struct *tty, unsigned int status);
  	int	(*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
-				const char *fp, int count);
+				const char *fp, int count, int lookahead_count);

Please always use unsigned if you don't expect negative numbers. count should be changed to unsigned in long term too.

include/linux/tty_port.h struct tty_port_operations { · line 42

Quoted 10 lines
include/linux/tty_port.h +1 -1
index 58e9619116b7..1871a6a9cb00 100644
@@ -39,7 +39,7 @@ struct tty_port_operations {
  };
 
  struct tty_port_client_operations {
-	int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t);
+	int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t, size_t);

Good, here it is unsigned and even of size_t size (I don't immediately see why, but OK).

thanks,

Signature · 2 lines
js
suse labs
#5 lore raw

Re: [PATCH 1/2] tty: Add lookahead param to receive_buf

Quoted 23 lines · Jiri Slaby
Quoted 9 lines · Ilpo Järvinen

After lookahead for XON/XOFF characters is added by the next patch, the receive side needs to ensure the flow-control actions are not retaken later on when those same characters get read by TTY.

Thus, pass lookahead count to receive_buf and skip flow-control character actions if already taken for the character in question. Lookahead count will become live after the next patch.

...

Quoted 12 lines
include/linux/tty_ldisc.h +1 -1
@@ -224,11 +224,11 @@ struct tty_ldisc_ops {
  	 * The following routines are called from below.
  	 */
  	void	(*receive_buf)(struct tty_struct *tty, const unsigned char
 *cp,
-			       const char *fp, int count);
+			       const char *fp, int count, int
 lookahead_count);
  	void	(*write_wakeup)(struct tty_struct *tty);
  	void	(*dcd_change)(struct tty_struct *tty, unsigned int status);
Quoted 8 lines
Quoted 5 lines
include/linux/tty_ldisc.h +1 -1
  	int	(*receive_buf2)(struct tty_struct *tty, const unsigned char
 *cp,
-				const char *fp, int count);
+				const char *fp, int count, int

lookahead_count);

Please always use unsigned if you don't expect negative numbers. count should be changed to unsigned in long term too.

Ok. I had just used the same types as there were previously. The types are a big mess, int and size_t conversions being done back and forth within that callchain for count. And now that you mentioned unsigned it seems that fp (flag ptr) is also inconsistent.

Quoted 6 lines
Quoted 6 lines
include/linux/tty_port.h +0 -0
index 58e9619116b7..1871a6a9cb00 100644
@@ -39,7 +39,7 @@ struct tty_port_operations {
  };
Quoted 8 lines

include/linux/tty_port.h struct tty_port_operations { · line 43

Quoted 5 lines
include/linux/tty_port.h +1 -1
    struct tty_port_client_operations {
-	int (*receive_buf)(struct tty_port *port, const unsigned char *, const
 unsigned char *, size_t);
+	int (*receive_buf)(struct tty_port *port, const unsigned char *, const
 unsigned char *, size_t, size_t);

Good, here it is unsigned and even of size_t size (I don't immediately see why, but OK).

You mean that the flag ptr is now unsigned char * (which I didn't even touch)?

Your comment leaves me to wonder though, what you now want for the lookahead count (the last arg), size_t or unsigned int? Lookahead count cannot be negative, and AFAICT, count neither because that would mean tty has to return some characters back :-).

...Maybe I'll do another patch post these fix ones to match/correct the types.

Signature · 1 line
 i.
#6 lore raw

PATCH 2/2 tty: Implement lookahead to process XON/XOFF timely

In reply to Ilpo Järvinen

When tty is not read from, XON/XOFF may get stuck into an intermediate buffer. As those characters are there to do software flow-control, it is not very useful. In the case where neither end reads from ttys, the receiving ends might not be able receive the XOFF characters and just keep sending more data to the opposite direction. This problem is almost guaranteed to occur with DMA which sends data in large chunks.

If TTY is slow to process characters, that is, eats less than given amount in receive_buf, invoke lookahead for the rest of the chars to process potential XON/XOFF characters.

The guards necessary for ensuring the XON/XOFF character are processed only once were added by the previous patch. All this patch needs to do on that front is to pass the lookahead count (that can now be non-zero) into port->client_ops->receive_buf().

---
 drivers/tty/n_tty.c        | 55 ++++++++++++++++++++++++---------
 drivers/tty/tty_buffer.c   | 62 ++++++++++++++++++++++++++++++++------
 drivers/tty/tty_port.c     | 22 ++++++++++++++
 include/linux/tty_buffer.h |  1 +
 include/linux/tty_ldisc.h  |  2 ++
 include/linux/tty_port.h   |  1 +
 6 files changed, 119 insertions(+), 24 deletions(-)
drivers/tty/n_tty.c +41 -14
index e1b761740056..749218c6474f 100644
@@ -1220,25 +1220,34 @@ n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
 		process_echoes(tty);
 }
 
+static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
+					 bool lookahead_done)
+{
+	if (!I_IXON(tty))
+		return false;
+
+	if (c == START_CHAR(tty)) {
+		if (!lookahead_done) {
+			start_tty(tty);
+			process_echoes(tty);
+		}
+		return true;
+	}
+	if (c == STOP_CHAR(tty)) {
+		if (!lookahead_done)
+			stop_tty(tty);
+		return true;
+	}
+	return false;
+}
+
 static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
 				       bool lookahead_done)
 {
 	struct n_tty_data *ldata = tty->disc_data;
 
-	if (I_IXON(tty)) {
-		if (c == START_CHAR(tty)) {
-			if (!lookahead_done) {
-				start_tty(tty);
-				process_echoes(tty);
-			}
-			return;
-		}
-		if (c == STOP_CHAR(tty)) {
-			if (!lookahead_done)
-				stop_tty(tty);
-			return;
-		}
-	}
+	if (__n_tty_receive_char_special(tty, c, lookahead_done))
+		return;
 
 	if (L_ISIG(tty)) {
 		if (c == INTR_CHAR(tty)) {
@@ -1450,6 +1459,23 @@ n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
 		n_tty_receive_char_flagged(tty, c, flag);
 }
 
+static void n_tty_lookahead_special(struct tty_struct *tty, const unsigned char *cp,
+				    const char *fp, int count)
+{
+	char flag = TTY_NORMAL;
+
+	if (!I_IXON(tty))
+		return;
+
+	while (count--) {
+		if (fp)
+			flag = *fp++;
+		if (likely(flag == TTY_NORMAL))
+			__n_tty_receive_char_special(tty, *cp, false);
+		cp++;
+	}
+}
+
 static void
 n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
 			   const char *fp, int count)
@@ -2405,6 +2431,7 @@ static struct tty_ldisc_ops n_tty_ops = {
 	.receive_buf     = n_tty_receive_buf,
 	.write_wakeup    = n_tty_write_wakeup,
 	.receive_buf2	 = n_tty_receive_buf2,
+	.lookahead_buf	 = n_tty_lookahead_special,
 };
 
 /**
drivers/tty/tty_buffer.c +52 -10

113 lines not on this page · raw message

drivers/tty/tty_port.c +22 -0
index 0d41ec0e5fee..650f38d67159 100644
@@ -44,6 +44,27 @@ static int tty_port_default_receive_buf(struct tty_port *port,
 	return ret;
 }
 
+static void tty_port_default_lookahead_buf(struct tty_port *port,
+					   const unsigned char *p,
+					   const unsigned char *f, size_t count)
+{
+	struct tty_struct *tty;
+	struct tty_ldisc *disc;
+
+	tty = READ_ONCE(port->itty);
+	if (!tty)
+		return;
+
+	disc = tty_ldisc_ref(tty);
+	if (!disc)
+		return;
+
+	if (disc->ops->lookahead_buf)
+		disc->ops->lookahead_buf(disc->tty, p, f, count);
+
+	tty_ldisc_deref(disc);
+}
+
 static void tty_port_default_wakeup(struct tty_port *port)
 {
 	struct tty_struct *tty = tty_port_tty_get(port);
@@ -56,6 +77,7 @@ static void tty_port_default_wakeup(struct tty_port *port)
 
 const struct tty_port_client_operations tty_port_default_client_ops = {
 	.receive_buf = tty_port_default_receive_buf,
+	.lookahead_buf = tty_port_default_lookahead_buf,
 	.write_wakeup = tty_port_default_wakeup,
 };
 EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
include/linux/tty_buffer.h +1 -0
index 3b9d77604291..1796648c2907 100644
@@ -15,6 +15,7 @@ struct tty_buffer {
 	int used;
 	int size;
 	int commit;
+	int lookahead;		/* Lazy update on recv, can become less than "read" */
 	int read;
 	int flags;
 	/* Data points here */
include/linux/tty_ldisc.h +2 -0
index 496f4755ea05..fc7b4ffe76c6 100644
@@ -229,6 +229,8 @@ struct tty_ldisc_ops {
 	void	(*dcd_change)(struct tty_struct *tty, unsigned int status);
 	int	(*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
 				const char *fp, int count, int lookahead_count);
+	void	(*lookahead_buf)(struct tty_struct *tty, const unsigned char *cp,
+				 const char *fp, int count);
 
 	struct  module *owner;
 };
include/linux/tty_port.h +1 -0
index 1871a6a9cb00..bc768265bf63 100644
@@ -40,6 +40,7 @@ struct tty_port_operations {
 
 struct tty_port_client_operations {
 	int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t, size_t);
+	void (*lookahead_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t);
 	void (*write_wakeup)(struct tty_port *port);
 };
 
Signature · 1 line
2.30.2
#7 lore raw

Re: [PATCH 2/2] tty: Implement lookahead to process XON/XOFF timely

Quoted 16 lines · Ilpo Järvinen

When tty is not read from, XON/XOFF may get stuck into an intermediate buffer. As those characters are there to do software flow-control, it is not very useful. In the case where neither end reads from ttys, the receiving ends might not be able receive the XOFF characters and just keep sending more data to the opposite direction. This problem is almost guaranteed to occur with DMA which sends data in large chunks.

If TTY is slow to process characters, that is, eats less than given amount in receive_buf, invoke lookahead for the rest of the chars to process potential XON/XOFF characters.

The guards necessary for ensuring the XON/XOFF character are processed only once were added by the previous patch. All this patch needs to do on that front is to pass the lookahead count (that can now be non-zero) into port->client_ops->receive_buf().

...

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
+					 bool lookahead_done)
+{
+	if (!I_IXON(tty))
+		return false;
+
+	if (c == START_CHAR(tty)) {
+		if (!lookahead_done) {
+			start_tty(tty);
+			process_echoes(tty);

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1242

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+		}
+		return true;
+	}
+	if (c == STOP_CHAR(tty)) {
+		if (!lookahead_done)
+			stop_tty(tty);
+		return true;
+	}
+	return false;
+}

Looking into this I would first make a preparatory patch that splits out current code into something like

static bool __n_tty_receive_char_special_no_lookahead(struct tty_struct *tty, unsigned char c)

{
	...current code...
}

Then in the patch 1 you add

static bool __n_tty_receive_char_special_lookahead(struct tty_struct *tty, unsigned char c)

{
	...
}

static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
bool lookahead_done)

{
	if (!I_IXON(tty))
		return false;

	if (lookahead_done)
		return _lookahead();

	return _no_lookahead();
}

?

...

/*
* Also start multi-line comments with
* capital letter and finish with
* grammar period.
*/

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw

Re: [PATCH 2/2] tty: Implement lookahead to process XON/XOFF timely

Quoted 51 lines · Andy Shevchenko
Quoted 16 lines · Ilpo Järvinen

When tty is not read from, XON/XOFF may get stuck into an intermediate buffer. As those characters are there to do software flow-control, it is not very useful. In the case where neither end reads from ttys, the receiving ends might not be able receive the XOFF characters and just keep sending more data to the opposite direction. This problem is almost guaranteed to occur with DMA which sends data in large chunks.

If TTY is slow to process characters, that is, eats less than given amount in receive_buf, invoke lookahead for the rest of the chars to process potential XON/XOFF characters.

The guards necessary for ensuring the XON/XOFF character are processed only once were added by the previous patch. All this patch needs to do on that front is to pass the lookahead count (that can now be non-zero) into port->client_ops->receive_buf().

...

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
+					 bool lookahead_done)
+{
+	if (!I_IXON(tty))
+		return false;
+
+	if (c == START_CHAR(tty)) {
+		if (!lookahead_done) {
+			start_tty(tty);
+			process_echoes(tty);

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1242

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+		}
+		return true;
+	}
+	if (c == STOP_CHAR(tty)) {
+		if (!lookahead_done)
+			stop_tty(tty);
+		return true;
+	}
+	return false;
+}

Looking into this I would first make a preparatory patch that splits out current code into something like

static bool __n_tty_receive_char_special_no_lookahead(struct tty_struct *tty, unsigned char c)

{
	...current code...
}

Then in the patch 1 you add

Quoted 7 lines

static bool __n_tty_receive_char_special_lookahead(struct tty_struct *tty, unsigned char c)

{
	...
}

static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
bool lookahead_done)

This should be dubbed better. Maybe n_tty_receive_char_flow_control()?

And I would place the if (I_IXON(tty)) to the caller. I am a bit lost in this pseudo code, so maybe this doesn't make sense in your proposal. I have something like in my mind:

if (I_IXON(tty))
return n_tty_receive_char_flow_control();

Historically, these n_tty_receive* function names were a big mess. Don't produce more of that by simply prepending only "__".

Quoted 9 lines
{
	if (!I_IXON(tty))
		return false;

	if (lookahead_done)
		return _lookahead();

	return _no_lookahead();
}

thanks

Signature · 2 lines
js
suse labs
#9 lore raw

Re: [PATCH 2/2] tty: Implement lookahead to process XON/XOFF timely

Quoted 70 lines · Jiri Slaby
Quoted 55 lines · Andy Shevchenko
Quoted 16 lines · Ilpo Järvinen

When tty is not read from, XON/XOFF may get stuck into an intermediate buffer. As those characters are there to do software flow-control, it is not very useful. In the case where neither end reads from ttys, the receiving ends might not be able receive the XOFF characters and just keep sending more data to the opposite direction. This problem is almost guaranteed to occur with DMA which sends data in large chunks.

If TTY is slow to process characters, that is, eats less than given amount in receive_buf, invoke lookahead for the rest of the chars to process potential XON/XOFF characters.

The guards necessary for ensuring the XON/XOFF character are processed only once were added by the previous patch. All this patch needs to do on that front is to pass the lookahead count (that can now be non-zero) into port->client_ops->receive_buf().

...

Quoted 11 lines

+static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,

drivers/tty/n_tty.c +9 -0
+					 bool lookahead_done)
+{
+	if (!I_IXON(tty))
+		return false;
+
+	if (c == START_CHAR(tty)) {
+		if (!lookahead_done) {
+			start_tty(tty);
+			process_echoes(tty);

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1242

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+		}
+		return true;
+	}
+	if (c == STOP_CHAR(tty)) {
+		if (!lookahead_done)
+			stop_tty(tty);
+		return true;
+	}
+	return false;
+}

Looking into this I would first make a preparatory patch that splits out current code into something like

static bool __n_tty_receive_char_special_no_lookahead(struct tty_struct *tty, unsigned char c)

{
	...current code...
}

Then in the patch 1 you add

static bool __n_tty_receive_char_special_lookahead(struct tty_struct *tty, unsigned char c)

Quoted 7 lines
{
	...
}

static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
bool lookahead_done)

This should be dubbed better. Maybe n_tty_receive_char_flow_control()?

And I would place the if (I_IXON(tty)) to the caller. I am a bit lost in this pseudo code, so maybe this doesn't make sense in your proposal. I have something like in my mind:

Quoted 5 lines

if (I_IXON(tty))
return n_tty_receive_char_flow_control();

Historically, these n_tty_receive* function names were a big mess. Don't produce more of that by simply prepending only "__".

Ok, I'll see what I can do.

Thanks to both of you for the comments.

Signature · 1 line
 i.
#10 lore raw

Re: [PATCH 2/2] tty: Implement lookahead to process XON/XOFF timely

In reply to Jiri Slaby
Quoted 60 lines · Jiri Slaby
Quoted 51 lines · Andy Shevchenko
Quoted 16 lines · Ilpo Järvinen

When tty is not read from, XON/XOFF may get stuck into an intermediate buffer. As those characters are there to do software flow-control, it is not very useful. In the case where neither end reads from ttys, the receiving ends might not be able receive the XOFF characters and just keep sending more data to the opposite direction. This problem is almost guaranteed to occur with DMA which sends data in large chunks.

If TTY is slow to process characters, that is, eats less than given amount in receive_buf, invoke lookahead for the rest of the chars to process potential XON/XOFF characters.

The guards necessary for ensuring the XON/XOFF character are processed only once were added by the previous patch. All this patch needs to do on that front is to pass the lookahead count (that can now be non-zero) into port->client_ops->receive_buf().

...

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
+					 bool lookahead_done)
+{
+	if (!I_IXON(tty))
+		return false;
+
+	if (c == START_CHAR(tty)) {
+		if (!lookahead_done) {
+			start_tty(tty);
+			process_echoes(tty);

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1242

Quoted 10 lines
drivers/tty/n_tty.c +10 -0
+		}
+		return true;
+	}
+	if (c == STOP_CHAR(tty)) {
+		if (!lookahead_done)
+			stop_tty(tty);
+		return true;
+	}
+	return false;
+}

Looking into this I would first make a preparatory patch that splits out current code into something like

static bool __n_tty_receive_char_special_no_lookahead(struct tty_struct *tty, unsigned char c)

{
	...current code...
}

Then in the patch 1 you add

Quoted 7 lines

static bool __n_tty_receive_char_special_lookahead(struct tty_struct *tty, unsigned char c)

{
	...
}

static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
bool lookahead_done)

Quoted 8 lines

This should be dubbed better. Maybe n_tty_receive_char_flow_control()?

And I would place the if (I_IXON(tty)) to the caller. I am a bit lost in this pseudo code, so maybe this doesn't make sense in your proposal. I have something like in my mind:

if (I_IXON(tty))
return n_tty_receive_char_flow_control();

My point to have three helpers which make each change cleaner:

.-> n_tty_receive_char_flow_control_lah()

  |
  |  .-> n_tty_receive_char_flow_control_no_lah()
  |  |

`- + -- n_tty_receive_char_flow_control()

Where no_lah variant can be split as preparatory patch prepending the current series.

And yes, calling I_IXON at the caller seems better.

Quoted 12 lines

Historically, these n_tty_receive* function names were a big mess. Don't produce more of that by simply prepending only "__".

Quoted 9 lines
{
	if (!I_IXON(tty))
		return false;

	if (lookahead_done)
		return _lookahead();

	return _no_lookahead();
}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH 2/2] tty: Implement lookahead to process XON/XOFF timely

Quoted 46 lines · Andy Shevchenko
Quoted 34 lines · Jiri Slaby
Quoted 25 lines · Andy Shevchenko

drivers/tty/n_tty.c n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) · line 1240

Quoted 12 lines · Ilpo Järvinen
drivers/tty/n_tty.c +12 -0
+	if (c == START_CHAR(tty)) {
+		if (!lookahead_done) {
+			start_tty(tty);
+			process_echoes(tty);
+		}
+		return true;
+	}
+	if (c == STOP_CHAR(tty)) {
+		if (!lookahead_done)
+			stop_tty(tty);
+		return true;
+	}

Looking into this I would first make a preparatory patch that splits out current code into something like

static bool __n_tty_receive_char_special_no_lookahead(struct tty_struct *tty, unsigned char c)

{
	...current code...
}

Then in the patch 1 you add

Quoted 7 lines

static bool __n_tty_receive_char_special_lookahead(struct tty_struct *tty, unsigned char c)

{
	...
}

static bool __n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
bool lookahead_done)

Quoted 8 lines

This should be dubbed better. Maybe n_tty_receive_char_flow_control()?

And I would place the if (I_IXON(tty)) to the caller. I am a bit lost in this pseudo code, so maybe this doesn't make sense in your proposal. I have something like in my mind:

if (I_IXON(tty))
return n_tty_receive_char_flow_control();

My point to have three helpers which make each change cleaner:

Quoted 8 lines

.-> n_tty_receive_char_flow_control_lah()

  |
  |  .-> n_tty_receive_char_flow_control_no_lah()
  |  |

`- + -- n_tty_receive_char_flow_control()

Where no_lah variant can be split as preparatory patch prepending the current series.

Another possibility to keep one function, but split it first in a separate patch and then modify.

Quoted 14 lines

And yes, calling I_IXON at the caller seems better.

Quoted 12 lines

Historically, these n_tty_receive* function names were a big mess. Don't produce more of that by simply prepending only "__".

Quoted 9 lines
{
	if (!I_IXON(tty))
		return false;

	if (lookahead_done)
		return _lookahead();

	return _no_lookahead();
}
Signature · 2 lines
With Best Regards,
Andy Shevchenko