tty: implement write_iter
Subsystems (7)
Greg Kroah-Hartman
maintainerPATCH 1/6 tty: implement write_iter
From: Linus Torvalds <[email protected]>
This makes the tty layer use the .write_iter() function instead of the traditional .write() functionality.
That allows writev(), but more importantly also makes it possible to enable .splice_write() for ttys, reinstating the "splice to tty" functionality that was lost in commit 36e2c7421f02 ("fs: don't allow splice read/write without explicit ops").
--- drivers/tty/tty_io.c | 48 ++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-)
drivers/tty/tty_io.c +26 -22
@@ -143,9 +143,8 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers */DEFINE_MUTEX (tty_mutex);static ssize_t tty_read (struct file *,char __user *,size_t ,loff_t *); -static ssize_t tty_write (struct file *,const char __user *,size_t ,loff_t *); -ssize_t redirected_tty_write (struct file *,const char __user *, -size_t ,loff_t *); +static ssize_t tty_write (struct kiocb *,struct iov_iter *); +ssize_t redirected_tty_write (struct kiocb *,struct iov_iter *);static __poll_t tty_poll (struct file *,poll_table *);static int tty_open (struct inode *,struct file *);long tty_ioctl (struct file *file,unsigned int cmd,unsigned long arg);
@@ -478,7 +477,8 @@ static void tty_show_fdinfo(struct seq_file *m, struct file *file)static const struct file_operations tty_fops = { .llseek = no_llseek, .read = tty_read, - .write = tty_write, + .write_iter = tty_write, + .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl, .compat_ioctl = tty_compat_ioctl,
@@ -491,7 +491,8 @@ static const struct file_operations tty_fops = {static const struct file_operations console_fops = { .llseek = no_llseek, .read = tty_read, - .write = redirected_tty_write, + .write_iter = redirected_tty_write, + .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl, .compat_ioctl = tty_compat_ioctl,
@@ -607,9 +608,9 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)/* This breaks for file handles being sent over AF_UNIX sockets ? */ list_for_each_entry (priv, &tty->tty_files, list) { filp = priv->file; -if (filp->f_op->write == redirected_tty_write) +if (filp->f_op->write_iter == redirected_tty_write) cons_filp = filp; -if (filp->f_op->write != tty_write) +if (filp->f_op->write_iter != tty_write)continue ; closecount++;__tty_fasync (-1 , filp,0 );/* can't block */
@@ -902,9 +903,9 @@ static inline ssize_t do_tty_write(ssize_t (*write)(struct tty_struct *,struct file *,const unsigned char *,size_t ), struct tty_struct *tty, struct file *file, - const char __user *buf, - size_t count) + struct iov_iter *from) { + size_t count = iov_iter_count(from);ssize_t ret, written =0 ;unsigned int chunk;
@@ -956,14 +957,20 @@ static inline ssize_t do_tty_write(size_t size = count;if (size > chunk) size = chunk; + ret = -EFAULT ; -if (copy_from_user (tty->write_buf, buf, size)) +if (copy_from_iter (tty->write_buf, size, from) != size)break ; + ret =write (tty, file, tty->write_buf, size);if (ret <=0 )break ; + +/* FIXME! Have Al check this! */ +if (ret != size) +iov_iter_revert (from, size-ret); + written += ret; - buf += ret; count -= ret;if (!count)break ;
@@ -1023,9 +1030,9 @@ void tty_write_message(struct tty_struct *tty, char *msg)* write method will not be invoked in parallel for each device. */ -static ssize_t tty_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) +static ssize_t tty_write (struct kiocb *iocb,struct iov_iter *from) { +struct file *file = iocb->ki_filp;struct tty_struct *tty =file_tty (file);struct tty_ldisc *ld;ssize_t ret;
@@ -1038,18 +1045,15 @@ static ssize_t tty_write(struct file *file, const char __user *buf,if (tty->ops->write_room ==NULL )tty_err (tty,"missing write_room method\n" ); ld =tty_ldisc_ref_wait (tty); -if (!ld) -return hung_up_tty_write (file, buf, count, ppos); -if (!ld->ops->write) +if (!ld || !ld->ops->write) ret = -EIO ;else - ret =do_tty_write (ld->ops->write, tty, file, buf, count); + ret =do_tty_write (ld->ops->write, tty, file, from);tty_ldisc_deref (ld);return ret; } -ssize_t redirected_tty_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) +ssize_t redirected_tty_write (struct kiocb *iocb,struct iov_iter *iter) {struct file *p =NULL ;
@@ -1060,11 +1064,11 @@ ssize_t redirected_tty_write(struct file *file, const char __user *buf,if (p) {ssize_t res; - res =vfs_write (p, buf, count, &p->f_pos); + res =vfs_iocb_iter_write (p, iocb, iter);fput (p);return res; } -return tty_write (file, buf, count, ppos); +return tty_write (iocb, iter); }/**
@@ -2293,7 +2297,7 @@ static int tioccons(struct file *file) {if (!capable (CAP_SYS_ADMIN ))return -EPERM ; -if (file->f_op->write == redirected_tty_write) { +if (file->f_op->write_iter == redirected_tty_write) {struct file *f;spin_lock (&redirect_lock); f = redirect;
Signature · 1 line
2.30.0
Greg Kroah-Hartman
maintainerPATCH 3/6 tty: implement read_iter
From: Linus Torvalds <[email protected]>
Now that the ldisc read() function takes kernel pointers, it's fairly straightforward to make the tty file operations use .read_iter() instead of .read().
That automatically gives us vread() and friends, and also makes it possible to do .splice_read() on ttys again.
--- drivers/tty/tty_io.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/tty/tty_io.c +18 -18
@@ -142,7 +142,7 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers *//* Mutex to protect creating and releasing a tty */ DEFINE_MUTEX (tty_mutex); -static ssize_t tty_read (struct file *,char __user *,size_t ,loff_t *); +static ssize_t tty_read (struct kiocb *,struct iov_iter *);static ssize_t tty_write (struct kiocb *,struct iov_iter *);ssize_t redirected_tty_write (struct kiocb *,struct iov_iter *);static __poll_t tty_poll (struct file *,poll_table *);
@@ -476,8 +476,9 @@ static void tty_show_fdinfo(struct seq_file *m, struct file *file)static const struct file_operations tty_fops = { .llseek = no_llseek, - .read = tty_read, + .read_iter = tty_read, .write_iter = tty_write, + .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl,
@@ -490,8 +491,9 @@ static const struct file_operations tty_fops = {static const struct file_operations console_fops = { .llseek = no_llseek, - .read = tty_read, + .read_iter = tty_read, .write_iter = redirected_tty_write, + .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl,
@@ -844,16 +846,17 @@ static void tty_update_time(struct timespec64 *time)* data or clears the cookie. The cookie may be something that the * ldisc maintains state for and needs to free. */ -static int iterate_tty_read (struct tty_ldisc *ld,struct tty_struct *tty,struct file *file, -char __user *buf,size_t count) +static int iterate_tty_read (struct tty_ldisc *ld,struct tty_struct *tty, +struct file *file,struct iov_iter *to) {int retval =0 ;void *cookie =NULL ;unsigned long offset =0 ;char kernel_buf[64 ]; +size_t count =iov_iter_count (to); do { - int size, uncopied; + int size, copied; size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count; size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
@@ -869,10 +872,9 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, structreturn size; } - uncopied =copy_to_user (buf+offset, kernel_buf, size); - size -= uncopied; - offset += size; - count -= size; + copied =copy_to_iter (kernel_buf, size, to); + offset += copied; + count -= copied;/* * If the user copy failed, we still need to do another ->read()
@@ -880,7 +882,7 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, struct* * But make sure size is zeroed. */ -if (unlikely (uncopied)) { +if (unlikely (copied != size)) { count =0 ; retval = -EFAULT ; }
@@ -907,10 +909,10 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, struct* read calls may be outstanding in parallel. */ -static ssize_t tty_read (struct file *file,char __user *buf,size_t count, -loff_t *ppos) +static ssize_t tty_read (struct kiocb *iocb,struct iov_iter *to) {int i; +struct file *file = iocb->ki_filp;struct inode *inode =file_inode (file);struct tty_struct *tty =file_tty (file);struct tty_ldisc *ld;
@@ -923,11 +925,9 @@ static ssize_t tty_read(struct file *file, char __user *buf, size_t count,/* We want to wait for the line discipline to sort out in this situation */ ld =tty_ldisc_ref_wait (tty); -if (!ld) -return hung_up_tty_read (file, buf, count, ppos); i = -EIO ; -if (ld->ops->read) - i =iterate_tty_read (ld, tty, file, buf, count); +if (ld && ld->ops->read) + i =iterate_tty_read (ld, tty, file, to);tty_ldisc_deref (ld);if (i >0 )
@@ -2927,7 +2927,7 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd,static int this_tty (const void *t,struct file *file,unsigned fd) { -if (likely (file->f_op->read != tty_read)) +if (likely (file->f_op->read_iter != tty_read))return 0 ;return file_tty (file) != t ?0 : fd +1 ; }
Signature · 1 line
2.30.0
Greg Kroah-Hartman
maintainerPATCH 4/6 tty: clean up legacy leftovers from n_tty line discipline
In reply to Greg Kroah-HartmanFrom: Linus Torvalds <[email protected]>
Back when the line disciplines did their own direct user accesses, they had to deal with the data copy possibly failing in the middle.
Now that the user copy is done by the tty_io.c code, that failure case no longer exists.
Remove the left-over error handling code that cannot trigger.
--- drivers/tty/n_tty.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/tty/n_tty.c +9 -20
@@ -1955,19 +1955,17 @@ static inline int input_available_p(struct tty_struct *tty, int poll)* read_tail published */ -static int copy_from_read_buf (struct tty_struct *tty, +static void copy_from_read_buf (struct tty_struct *tty,unsigned char **kbp,size_t *nr) {struct n_tty_data *ldata = tty->disc_data; -int retval;size_t n;bool is_eof;size_t head =smp_load_acquire (&ldata->commit_head);size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE -1 ); - retval =0 ; n =min (head - ldata->read_tail,N_TTY_BUF_SIZE - tail); n =min (*nr, n);if (n) {
@@ -1984,7 +1982,6 @@ static int copy_from_read_buf(struct tty_struct *tty, *kbp += n; *nr -= n; } -return retval; }/**
@@ -2010,9 +2007,9 @@ static int copy_from_read_buf(struct tty_struct *tty,* read_tail published */ -static int canon_copy_from_read_buf (struct tty_struct *tty, -unsigned char **kbp, -size_t *nr) +static void canon_copy_from_read_buf (struct tty_struct *tty, +unsigned char **kbp, +size_t *nr) {struct n_tty_data *ldata = tty->disc_data;size_t n, size, more, c;
@@ -2022,7 +2019,7 @@ static int canon_copy_from_read_buf(struct tty_struct *tty,/* N.B. avoid overrun if nr == 0 */ if (!*nr) -return 0 ; +return ; n =min (*nr +1 ,smp_load_acquire (&ldata->canon_head) - ldata->read_tail);
@@ -2069,7 +2066,6 @@ static int canon_copy_from_read_buf(struct tty_struct *tty, ldata->push =0 ;tty_audit_push (); } -return 0 ; } extern ssize_t redirected_tty_write(struct file *, const char __user *,
@@ -2222,24 +2218,17 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, }if (ldata->icanon && !L_EXTPROC (tty)) { - retval =canon_copy_from_read_buf (tty, &kb, &nr); -if (retval) -break ; +canon_copy_from_read_buf (tty, &kb, &nr); }else { -int uncopied; -/* Deal with packet mode. */ if (packet && kb == kbuf) { *kb++ =TIOCPKT_DATA ; nr--; } - uncopied =copy_from_read_buf (tty, &kb, &nr); - uncopied +=copy_from_read_buf (tty, &kb, &nr); -if (uncopied) { - retval = -EFAULT ; -break ; - } +/* See comment above copy_from_read_buf() why twice */ +copy_from_read_buf (tty, &kb, &nr); +copy_from_read_buf (tty, &kb, &nr); }n_tty_check_unthrottle (tty);
Signature · 1 line
2.30.0
Greg Kroah-Hartman
maintainerPATCH 2/6 tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
In reply to Greg Kroah-HartmanFrom: Linus Torvalds <[email protected]>
The tty line discipline .read() function was passed the final user pointer destination as an argument, which doesn't match the 'write()' function, and makes it very inconvenient to do a splice method for ttys.
This is a conversion to use a kernel buffer instead.
NOTE! It does this by passing the tty line discipline ->read() function an additional "cookie" to fill in, and an offset into the cookie data.
The line discipline can fill in the cookie data with its own private information, and then the reader will repeat the read until either the cookie is cleared or it runs out of data.
The only real user of this is N_HDLC, which can use this to handle big packets, even if the kernel buffer is smaller than the whole packet.
--- drivers/bluetooth/hci_ldisc.c | 34 +++++++-------- drivers/input/serio/serport.c | 4 +- drivers/net/ppp/ppp_async.c | 3 +- drivers/net/ppp/ppp_synctty.c | 3 +- drivers/tty/n_gsm.c | 3 +- drivers/tty/n_hdlc.c | 60 +++++++++++++++++-------- drivers/tty/n_null.c | 3 +- drivers/tty/n_r3964.c | 10 ++--- drivers/tty/n_tracerouter.c | 4 +- drivers/tty/n_tracesink.c | 4 +- drivers/tty/n_tty.c | 82 +++++++++++++++-------------------- drivers/tty/tty_io.c | 64 +++++++++++++++++++++++++-- include/linux/tty_ldisc.h | 3 +- net/nfc/nci/uart.c | 3 +- 14 files changed, 178 insertions(+), 102 deletions(-)
drivers/bluetooth/hci_ldisc.c +17 -17
@@ -802,7 +802,8 @@ static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,* We don't provide read/write/poll interface for user space. */ static ssize_t hci_uart_tty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return 0 ; }
@@ -819,29 +820,28 @@ static __poll_t hci_uart_tty_poll(struct tty_struct *tty,return 0 ; } +static struct tty_ldisc_ops hci_uart_ldisc = { + .owner =THIS_MODULE , + .magic =TTY_LDISC_MAGIC , + .name ="n_hci" , + .open = hci_uart_tty_open, + .close = hci_uart_tty_close, + .read = hci_uart_tty_read, + .write = hci_uart_tty_write, + .ioctl = hci_uart_tty_ioctl, + .compat_ioctl = hci_uart_tty_ioctl, + .poll = hci_uart_tty_poll, + .receive_buf = hci_uart_tty_receive, + .write_wakeup = hci_uart_tty_wakeup, +}; +static int __init hci_uart_init (void ) { -static struct tty_ldisc_ops hci_uart_ldisc;int err;BT_INFO ("HCI UART driver ver %s" ,VERSION );/* Register the tty discipline */ - -memset (&hci_uart_ldisc,0 ,sizeof (hci_uart_ldisc)); - hci_uart_ldisc.magic =TTY_LDISC_MAGIC ; - hci_uart_ldisc.name ="n_hci" ; - hci_uart_ldisc.open = hci_uart_tty_open; - hci_uart_ldisc.close = hci_uart_tty_close; - hci_uart_ldisc.read = hci_uart_tty_read; - hci_uart_ldisc.write = hci_uart_tty_write; - hci_uart_ldisc.ioctl = hci_uart_tty_ioctl; - hci_uart_ldisc.compat_ioctl = hci_uart_tty_ioctl; - hci_uart_ldisc.poll = hci_uart_tty_poll; - hci_uart_ldisc.receive_buf = hci_uart_tty_receive; - hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup; - hci_uart_ldisc.owner =THIS_MODULE ; - err =tty_register_ldisc (N_HCI , &hci_uart_ldisc);if (err) {BT_ERR ("HCI line discipline registration failed. (%d)" , err);
drivers/input/serio/serport.c +3 -1
@@ -156,7 +156,9 @@ static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *c* returning 0 characters. */ -static ssize_t serport_ldisc_read (struct tty_struct * tty,struct file * file,unsigned char __user * buf,size_t nr) +static ssize_t serport_ldisc_read (struct tty_struct * tty,struct file * file, +unsigned char *kbuf,size_t nr, +void **cookie,unsigned long offset) {struct serport *serport = (struct serport *) tty->disc_data;struct serio *serio;
drivers/net/ppp/ppp_async.c +2 -1
@@ -259,7 +259,8 @@ static int ppp_asynctty_hangup(struct tty_struct *tty)*/ static ssize_t ppp_asynctty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t count) +unsigned char *buf,size_t count, +void **cookie,unsigned long offset) {return -EAGAIN ; }
drivers/net/ppp/ppp_synctty.c +2 -1
@@ -257,7 +257,8 @@ static int ppp_sync_hangup(struct tty_struct *tty)*/ static ssize_t ppp_sync_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t count) +unsigned char *buf,size_t count, +void **cookie,unsigned long offset) {return -EAGAIN ; }
drivers/tty/n_gsm.c +2 -1
@@ -2557,7 +2557,8 @@ static void gsmld_write_wakeup(struct tty_struct *tty)*/ static ssize_t gsmld_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return -EOPNOTSUPP ; }
drivers/tty/n_hdlc.c +41 -19
85 lines not on this page · raw message
drivers/tty/n_null.c +2 -1
@@ -20,7 +20,8 @@ static void n_null_close(struct tty_struct *tty) }static ssize_t n_null_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return -EOPNOTSUPP ; }
drivers/tty/n_r3964.c +4 -6
@@ -129,7 +129,7 @@ static void remove_client_block(struct r3964_info *pInfo,static int r3964_open (struct tty_struct *tty);static void r3964_close (struct tty_struct *tty);static ssize_t r3964_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr); +void *cookie,unsigned char *buf,size_t nr);static ssize_t r3964_write (struct tty_struct *tty,struct file *file,const unsigned char *buf,size_t nr); static int r3964_ioctl(struct tty_struct *tty, struct file *file,
@@ -1058,7 +1058,8 @@ static void r3964_close(struct tty_struct *tty) }static ssize_t r3964_read (struct tty_struct *tty,struct file *file, -unsigned char __user * buf,size_t nr) +unsigned char *kbuf,size_t nr, +void **cookie,unsigned long offset) {struct r3964_info *pInfo = tty->disc_data;struct r3964_client_info *pClient;
@@ -1109,10 +1110,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,kfree (pMsg);TRACE_M ("r3964_read - msg kfree %p" , pMsg); -if (copy_to_user (buf, &theMsg, ret)) { - ret = -EFAULT ; - goto unlock; - } +memcpy (kbuf, &theMsg, ret);TRACE_PS ("read - return %d" , ret); goto unlock;
drivers/tty/n_tracerouter.c +3 -1
@@ -118,7 +118,9 @@ static void n_tracerouter_close(struct tty_struct *tty)* -EINVAL */ static ssize_t n_tracerouter_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) { +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) +{return -EINVAL ; }
drivers/tty/n_tracesink.c +3 -1
@@ -115,7 +115,9 @@ static void n_tracesink_close(struct tty_struct *tty)* -EINVAL */ static ssize_t n_tracesink_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) { +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) +{return -EINVAL ; }
drivers/tty/n_tty.c +34 -48
202 lines not on this page · raw message
drivers/tty/tty_io.c +61 -3
77 lines not on this page · raw message
include/linux/tty_ldisc.h +2 -1
@@ -185,7 +185,8 @@ struct tty_ldisc_ops {void (*close)(struct tty_struct *);void (*flush_buffer)(struct tty_struct *tty);ssize_t (*read)(struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr); +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset);ssize_t (*write)(struct tty_struct *tty,struct file *file,const unsigned char *buf,size_t nr); int (*ioctl)(struct tty_struct *tty, struct file *file,
net/nfc/nci/uart.c +2 -1
@@ -292,7 +292,8 @@ static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,/* We don't provide read/write/poll interface for user space. */ static ssize_t nci_uart_tty_read (struct tty_struct *tty,struct file *file, -unsigned char __user *buf,size_t nr) +unsigned char *buf,size_t nr, +void **cookie,unsigned long offset) {return 0 ; }
Signature · 1 line
2.30.0
Jiri Slaby
maintainerQuoted 57 lines · Greg Kroah-Hartman
From: Linus Torvalds <[email protected]>
This makes the tty layer use the .write_iter() function instead of the traditional .write() functionality.
That allows
writev(), but more importantly also makes it possible to enable .splice_write() for ttys, reinstating the "splice to tty" functionality that was lost in commit36e2c7421f02("fs: don't allow splice read/write without explicit ops").Fixes 36e2c7421f02 ("fs: don't allow splice read/write without explicit ops")Reported-by Oliver Giles <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Al Viro <[email protected]>Signed-off-by Linus Torvalds <[email protected]>--- drivers/tty/tty_io.c | 48 ++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-)drivers/tty/tty_io.c +6
-5@@ -143,9 +143,8 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers */DEFINE_MUTEX (tty_mutex);static ssize_t tty_read (struct file *,char __user *,size_t ,loff_t *); -static ssize_t tty_write (struct file *,const char __user *,size_t ,loff_t *); -ssize_t redirected_tty_write (struct file *,const char __user *, -size_t ,loff_t *); +static ssize_t tty_write (struct kiocb *,struct iov_iter *); +ssize_t redirected_tty_write (struct kiocb *,struct iov_iter *);static __poll_t tty_poll (struct file *,poll_table *);static int tty_open (struct inode *,struct file *);long tty_ioctl (struct file *file,unsigned int cmd,unsigned long arg);@@ -478,7 +477,8 @@ static void tty_show_fdinfo(struct seq_file *m, struct file *file)static const struct file_operations tty_fops = { .llseek = no_llseek, .read = tty_read, - .write = tty_write, + .write_iter = tty_write, + .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl, .compat_ioctl = tty_compat_ioctl,@@ -491,7 +491,8 @@ static const struct file_operations tty_fops = {static const struct file_operations console_fops = { .llseek = no_llseek, .read = tty_read, - .write = redirected_tty_write, + .write_iter = redirected_tty_write, + .splice_write = iter_file_splice_write, .poll = tty_poll, .unlocked_ioctl = tty_ioctl, .compat_ioctl = tty_compat_ioctl,
drivers/tty/tty_io.c static void __tty_hangup(struct tty_struct *tty, int exit_session) · line 613
Quoted 8 lines
drivers/tty/tty_io.c +2
-2@@ -607,9 +608,9 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)/* This breaks for file handles being sent over AF_UNIX sockets ? */ list_for_each_entry (priv, &tty->tty_files, list) { filp = priv->file; -if (filp->f_op->write == redirected_tty_write) +if (filp->f_op->write_iter == redirected_tty_write) cons_filp = filp; -if (filp->f_op->write != tty_write) +if (filp->f_op->write_iter != tty_write)
This now relies on implicit value of hung_up_tty_fops.write_iter (i.e. NULL), okay.
Quoted 4 lines
drivers/tty/tty_io.c +0
-0continue ; closecount++;__tty_fasync (-1 , filp,0 );/* can't block */ filp->f_op = &hung_up_tty_fops;
Isn't this racy with VFS layer in vfs_write:
if (file->f_op->write)
ret = file->f_op->write(file, buf, count, pos);
else if (file->f_op->write_iter)
ret = new_sync_write(file, buf, count, pos);? hung_up_tty_fops do not set iter_write and tty_fops do not set write. When we switch from one to the other here, right after the 'if', but before the call, what happens? Likely nothing for the ->write case immediately as compilers cache the value, but for ->write_iter, I'm not sure. Anyway, this looks broken to me. (Read on.)
Quoted 38 lines
drivers/tty/tty_io.c +10
-4@@ -956,14 +957,20 @@ static inline ssize_t do_tty_write(size_t size = count;if (size > chunk) size = chunk; + ret = -EFAULT ; -if (copy_from_user (tty->write_buf, buf, size)) +if (copy_from_iter (tty->write_buf, size, from) != size)break ; + ret =write (tty, file, tty->write_buf, size);if (ret <=0 )break ; + +/* FIXME! Have Al check this! */ +if (ret != size) +iov_iter_revert (from, size-ret); + written += ret; - buf += ret; count -= ret;if (!count)break ;@@ -1023,9 +1030,9 @@ void tty_write_message(struct tty_struct *tty, char *msg)* write method will not be invoked in parallel for each device. */ -static ssize_t tty_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) +static ssize_t tty_write (struct kiocb *iocb,struct iov_iter *from) { +struct file *file = iocb->ki_filp;struct tty_struct *tty =file_tty (file);struct tty_ldisc *ld;ssize_t ret;@@ -1038,18 +1045,15 @@ static ssize_t tty_write(struct file *file, const char __user *buf,if (tty->ops->write_room ==NULL )tty_err (tty,"missing write_room method\n" );
drivers/tty/tty_io.c static ssize_t tty_write(struct file *file, const char __user *buf, · line 1052
Quoted 10 lines
drivers/tty/tty_io.c +2
-4ld =tty_ldisc_ref_wait (tty); -if (!ld) -return hung_up_tty_write (file, buf, count, ppos); -if (!ld->ops->write) +if (!ld || !ld->ops->write) ret = -EIO ;else - ret =do_tty_write (ld->ops->write, tty, file, buf, count); + ret =do_tty_write (ld->ops->write, tty, file, from);tty_ldisc_deref (ld);
Ok, here belongs my earlier note: "if ld == NULL => crash here." That is if hangup happens during the ldisc wait, the kernel will crash in tty_ldisc_deref.
Is there a reason not to convert hung_up_tty_fops too and leave the return hung_up_tty_write here intact? This would also solve the comments above.
drivers/tty/tty_io.c static ssize_t tty_write(struct file *file, const char __user *buf, · line 1059
Quoted 9 lines
drivers/tty/tty_io.c +1
-2return ret; } -ssize_t redirected_tty_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) +ssize_t redirected_tty_write (struct kiocb *iocb,struct iov_iter *iter) {struct file *p =NULL ;
thanks,
Signature · 1 line
js
Greg Kroah-Hartman
maintainerPATCH 6/6 tty: teach the n_tty ICANON case about the new "cookie continuations" too
In reply to Greg Kroah-HartmanFrom: Linus Torvalds <[email protected]>
The ICANON case is a bit messy, since it has to look for the line ending, and has special code to then suppress line ending characters if they match the __DISABLED_CHAR. So it actually looks up the line ending even past the point where it knows it won't copy it to the result buffer.
That said, apart from all those odd legacy N_TTY ICANON cases, the actual "should we continue copying" logic isn't really all that complicated or different from the non-canon case. In fact, the lack of "wait for at least N characters" arguably makes the repeat case slightly simpler. It really just boils down to "there's more of the line to be copied".
So add the necessarily trivial logic, and now the N_TTY case will give long result lines even when in canon mode.
--- drivers/tty/n_tty.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/tty/n_tty.c +19 -7
@@ -2009,21 +2009,22 @@ static bool copy_from_read_buf(struct tty_struct *tty,* read_tail published */ -static void canon_copy_from_read_buf (struct tty_struct *tty, +static bool canon_copy_from_read_buf (struct tty_struct *tty,unsigned char **kbp,size_t *nr) {struct n_tty_data *ldata = tty->disc_data;size_t n, size, more, c;size_t eol; -size_t tail; +size_t tail, canon_head;int found =0 ;/* N.B. avoid overrun if nr == 0 */ if (!*nr) -return ; +return false; - n =min (*nr +1 ,smp_load_acquire (&ldata->canon_head) - ldata->read_tail); + canon_head =smp_load_acquire (&ldata->canon_head); + n =min (*nr +1 , canon_head - ldata->read_tail); tail = ldata->read_tail & (N_TTY_BUF_SIZE -1 ); size =min_t (size_t, tail + n,N_TTY_BUF_SIZE );
@@ -2067,7 +2068,11 @@ static void canon_copy_from_read_buf(struct tty_struct *tty,else ldata->push =0 ;tty_audit_push (); +return false; } + +/* No EOL found - do a continuation retry if there is more data */ +return ldata->read_tail != canon_head; } extern ssize_t redirected_tty_write(struct file *, const char __user *,
@@ -2141,8 +2146,13 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,* termios_rwsem, and can just continue to copy data. */ if (*cookie) { -if (copy_from_read_buf (tty, &kb, &nr)) -return kb - kbuf; +if (ldata->icanon && !L_EXTPROC (tty)) { +if (canon_copy_from_read_buf (tty, &kb, &nr)) +return kb - kbuf; + }else { +if (copy_from_read_buf (tty, &kb, &nr)) +return kb - kbuf; + }/* No more data - release locks and stop retries */ n_tty_kick_worker (tty);
@@ -2239,7 +2249,8 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, }if (ldata->icanon && !L_EXTPROC (tty)) { -canon_copy_from_read_buf (tty, &kb, &nr); +if (canon_copy_from_read_buf (tty, &kb, &nr)) + goto more_to_be_read; }else {/* Deal with packet mode. */ if (packet && kb == kbuf) {
@@ -2257,6 +2268,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,* will release them when done. */ if (copy_from_read_buf (tty, &kb, &nr) && kb - kbuf >= minimum) { +more_to_be_read:remove_wait_queue (&tty->read_wait, &wait); *cookie = cookie;return kb - kbuf;
Signature · 1 line
2.30.0
Greg Kroah-Hartman
maintainerPATCH 5/6 tty: teach n_tty line discipline about the new "cookie continuations"
In reply to Greg Kroah-HartmanFrom: Linus Torvalds <[email protected]>
With the conversion to do the tty ldisc read operations in small chunks, the n_tty line discipline became noticeably slower for throughput oriented loads, because rather than read things in up to 2kB chunks, it would return at most 64 bytes per read() system call.
The cost is mainly all in the "do system calls over and over", not really in the new "copy to an extra kernel buffer".
This can be fixed by teaching the n_tty line discipline about the "cookie continuation" model, which the chunking code supports because things like hdlc need to be able to handle packets up to 64kB in size.
Doing that doesn't just get us back to the old performace, but to much better performance: my stupid "copy 10MB of data over a pty" test program is now almost twice as fast as it used to be (going down from 0.1s to 0.054s).
This is entirely because it now creates maximal chunks (which happens to be "one byte less than one page" due to how we do the circular tty buffers).
NOTE! This case only handles the simpler non-icanon case, which is the one where people may care about throughput. I'm going to do the icanon case later too, because while performance isn't a major issue for that, there may be programs that think they'll always get a full line and don't like the 64-byte chunking for that reason.
Such programs are arguably buggy (signals etc can cause random partial results from tty reads anyway), and good programs will handle such partial reads, but expecting everybody to write "good programs" has never been a winning policy for the kernel..
--- drivers/tty/n_tty.c | 52 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-)
drivers/tty/n_tty.c +42 -10
@@ -1943,19 +1943,17 @@ static inline int input_available_p(struct tty_struct *tty, int poll)* Helper function to speed up n_tty_read. It is only called when * ICANON is off; it copies characters straight from the tty queue. * -* It can be profitably called twice; once to drain the space from -* the tail pointer to the (physical) end of the buffer, and once -* to drain the space from the (physical) beginning of the buffer -* to head pointer. -* * Called under the ldata->atomic_read_lock sem * +* Returns true if it successfully copied data, but there is still +* more data to be had. +* * n_tty_read()/consumer path: * caller holds non-exclusive termios_rwsem * read_tail published */ -static void copy_from_read_buf (struct tty_struct *tty, +static bool copy_from_read_buf (struct tty_struct *tty,unsigned char **kbp,size_t *nr)
@@ -1978,10 +1976,14 @@ static void copy_from_read_buf(struct tty_struct *tty,/* Turn single EOF into zero-length read */ if (L_EXTPROC (tty) && ldata->icanon && is_eof && (head == ldata->read_tail)) - n =0 ; +return false; *kbp += n; *nr -= n; + +/* If we have more to copy, let the caller know */ +return head != ldata->read_tail; } +return false; }/**
@@ -2132,6 +2134,25 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,int packet;size_t tail; +/* +* Is this a continuation of a read started earler? +* +* If so, we still hold the atomic_read_lock and the +* termios_rwsem, and can just continue to copy data. +*/ +if (*cookie) { +if (copy_from_read_buf (tty, &kb, &nr)) +return kb - kbuf; + +/* No more data - release locks and stop retries */ +n_tty_kick_worker (tty); +n_tty_check_unthrottle (tty); +up_read (&tty->termios_rwsem); +mutex_unlock (&ldata->atomic_read_lock); + *cookie =NULL ; +return kb - kbuf; + } + c =job_control (tty, file);if (c <0 )return c;
@@ -2226,9 +2247,20 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, nr--; } -/* See comment above copy_from_read_buf() why twice */ -copy_from_read_buf (tty, &kb, &nr); -copy_from_read_buf (tty, &kb, &nr); +/* +* Copy data, and if there is more to be had +* and we have nothing more to wait for, then +* let's mark us for retries. +* +* NOTE! We return here with both the termios_sem +* and atomic_read_lock still held, the retries +* will release them when done. +*/ +if (copy_from_read_buf (tty, &kb, &nr) && kb - kbuf >= minimum) { +remove_wait_queue (&tty->read_wait, &wait); + *cookie = cookie; +return kb - kbuf; + } }n_tty_check_unthrottle (tty);
Signature · 1 line
2.30.0
Jiri Slaby
maintainerRe: [PATCH 3/6] tty: implement read_iter
In reply to Greg Kroah-HartmanQuoted 23 lines · Greg Kroah-Hartman
From: Linus Torvalds <[email protected]>
Now that the ldisc
read()function takes kernel pointers, it's fairly straightforward to make the tty file operations use .read_iter() instead of .read().That automatically gives us
vread()and friends, and also makes it possible to do .splice_read() on ttys again.Fixes 36e2c7421f02 ("fs: don't allow splice read/write without explicit ops")Reported-by Oliver Giles <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Al Viro <[email protected]>Signed-off-by Linus Torvalds <[email protected]>--- drivers/tty/tty_io.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)drivers/tty/tty_io.c +0
-0
...
Quoted 15 lines
drivers/tty/tty_io.c +2
-2@@ -907,10 +909,10 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, struct* read calls may be outstanding in parallel. */ -static ssize_t tty_read (struct file *file,char __user *buf,size_t count, -loff_t *ppos) +static ssize_t tty_read (struct kiocb *iocb,struct iov_iter *to) {int i; +struct file *file = iocb->ki_filp;struct inode *inode =file_inode (file);struct tty_struct *tty =file_tty (file);struct tty_ldisc *ld;@@ -923,11 +925,9 @@ static ssize_t tty_read(struct file *file, char __user *buf, size_t count,/* We want to wait for the line discipline to sort out in this
drivers/tty/tty_io.c static ssize_t tty_read(struct file *file, char __user *buf, size_t count, · line 931
Quoted 10 lines
drivers/tty/tty_io.c +2
-4situation */ ld =tty_ldisc_ref_wait (tty); -if (!ld) -return hung_up_tty_read (file, buf, count, ppos); i = -EIO ; -if (ld->ops->read) - i =iterate_tty_read (ld, tty, file, buf, count); +if (ld && ld->ops->read) + i =iterate_tty_read (ld, tty, file, to);tty_ldisc_deref (ld);
Here we have the same problem as in tty_write.
And also the other one with hung_up_tty_read not converted.
thanks,
Signature · 1 line
js
Jiri Slaby
maintainerRe: [PATCH 2/6] tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
In reply to Greg Kroah-HartmanQuoted 23 lines · Greg Kroah-Hartman
From: Linus Torvalds <[email protected]>
The tty line discipline .read() function was passed the final user pointer destination as an argument, which doesn't match the '
write()' function, and makes it very inconvenient to do a splice method for ttys.This is a conversion to use a kernel buffer instead.
NOTE! It does this by passing the tty line discipline ->read() function an additional "cookie" to fill in, and an offset into the cookie data.
The line discipline can fill in the cookie data with its own private information, and then the reader will repeat the read until either the cookie is cleared or it runs out of data.
The only real user of this is
N_HDLC, which can use this to handle big packets, even if the kernel buffer is smaller than the whole packet.Cc Christoph Hellwig <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Al Viro <[email protected]>Signed-off-by Linus Torvalds <[email protected]>
...
Quoted 19 lines
drivers/tty/tty_io.c +13
-0@@ -833,6 +833,65 @@ static void tty_update_time(struct timespec64 *time) time->tv_sec = sec; } +/* +* Iterate on the ldisc ->read() function until we've gotten all +* the data the ldisc has for us. +* +* The "cookie" is something that the ldisc read function can fill +* in to let us know that there is more data to be had. +* +* We promise to continue to call the ldisc until it stops returning +* data or clears the cookie. The cookie may be something that the +* ldisc maintains state for and needs to free. +*/ +static int iterate_tty_read (struct tty_ldisc *ld,struct tty_struct *tty,struct file *file, +char __user *buf,size_t count)
drivers/tty/tty_io.c static void tty_update_time(struct timespec64 *time) · line 858
Quoted 10 lines
drivers/tty/tty_io.c +10
-0+{ +int retval =0 ; +void *cookie =NULL ; +unsigned long offset =0 ; +char kernel_buf[64 ]; + + do { + int size, uncopied; + + size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count;
Or simply
size = min(count, sizeof(kernel_buf));
Quoted 17 lines
drivers/tty/tty_io.c +17
-0+ size = ld->ops->read (tty, file, kernel_buf, size, &cookie, offset); +if (!size) +break ; + +/* +* A ldisc read error return will override any previously copied +* data (eg -EOVERFLOW from HDLC) +*/ +if (size <0 ) { +memzero_explicit (kernel_buf,sizeof (kernel_buf)); +return size; + } + + uncopied =copy_to_user (buf+offset, kernel_buf, size); + size -= uncopied; + offset += size; + count -= size;
drivers/tty/tty_io.c static void tty_update_time(struct timespec64 *time) · line 885
Quoted 10 lines
drivers/tty/tty_io.c +10
-0+ +/* +* If the user copy failed, we still need to do another ->read() +* call if we had a cookie to let the ldisc clear up. +* +* But make sure size is zeroed. +*/ +if (unlikely (uncopied)) { + count =0 ; + retval = -EFAULT ;
n_hdlc_tty_read will return EOVERFLOW when size is 0, so this EFAULT is never propagated, if I am looking correctly? n_tty seems to be fine (returns zero for zeroed size).
drivers/tty/tty_io.c static void tty_update_time(struct timespec64 *time) · line 892
Quoted 7 lines
drivers/tty/tty_io.c +7
-0+ } + }while (cookie); + +/* We always clear tty buffer in case they contained passwords */ +memzero_explicit (kernel_buf,sizeof (kernel_buf)); +return offset ? offset : retval; +}
thanks,
Signature · 1 line
js
Quoted 4 lines · Jiri Slaby
Ok, here belongs my earlier note: "if ld == NULL => crash here." That is if hangup happens during the ldisc wait, the kernel will crash in
tty_ldisc_deref.
Right you are, good catch.
Quoted 3 lines
Is there a reason not to convert
hung_up_tty_fopstoo and leave the returnhung_up_tty_writehere intact? This would also solve the comments above.
No, no reason. I started out just changing that one tty_write, then noticed that I had to change the redirect case too, but never then got to "yeah, I should have changed the hup case as well".
Greg, do you prefer a new series, or incremental patches?
Linus
Re: [PATCH 2/6] tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
In reply to Jiri SlabyQuoted 4 lines · Jiri Slaby
n_hdlc_tty_readwill return EOVERFLOW when size is 0, so this EFAULT is never propagated, if I am looking correctly?n_ttyseems to be fine (returns zero for zeroed size).
I'll fix that up too.
Greg - same question - do you want an incremental patch, or just a new series?
Linus
Greg Kroah-Hartman
maintainerQuoted 12 lines · Linus Torvalds
Quoted 4 lines · Jiri Slaby
Ok, here belongs my earlier note: "if ld == NULL => crash here." That is if hangup happens during the ldisc wait, the kernel will crash in
tty_ldisc_deref.Right you are, good catch.
Quoted 3 lines
Is there a reason not to convert
hung_up_tty_fopstoo and leave the returnhung_up_tty_writehere intact? This would also solve the comments above.
Quoted 5 lines
No, no reason. I started out just changing that one
tty_write, then noticed that I had to change the redirect case too, but never then got to "yeah, I should have changed the hup case as well".Greg, do you prefer a new series, or incremental patches?
Incremental patches please as these are already in my public branches and I would have to revert them and add new ones but that's messy, so fixes on top is fine.
thanks,
greg k-h
Greg Kroah-Hartman
maintainerRe: [PATCH 2/6] tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
In reply to Linus TorvaldsQuoted 9 lines · Linus Torvalds
Quoted 4 lines · Jiri Slaby
n_hdlc_tty_readwill return EOVERFLOW when size is 0, so this EFAULT is never propagated, if I am looking correctly?n_ttyseems to be fine (returns zero for zeroed size).I'll fix that up too.
Greg - same question - do you want an incremental patch, or just a new series?
Incremental is best, thanks!
Quoted 4 lines · Greg Kroah-Hartman
Incremental patches please as these are already in my public branches and I would have to revert them and add new ones but that's messy, so fixes on top is fine.
Ok. And since I think you put that first tty_write conversion patch in a different branch from the tty_read one, I did the fixup patches for the two as separate patches, even though they really just do the exact same thing.
So here's three patches: the two fixups for the hung_up_tty case, and the EOVERFLOW error case that Jiri also noted. I've also updated the 'tty-splice' branch if you prefer them that way.
And I *should* say that I still haven't tested _any_ of the HDLC changes. I have no idea how to do that, and if somebody can point to a test-case (or better yet, actually has a real life situation where they use it and can test this all) it would be great.
Jiri, any other issues, or any comment of yours I missed? I didn't do the min() thing, I find the explicit conditional more legible myself, but won't complain if somebody else then disagrees and wants to clean it up.
(On the matter of cleanups: when reading through the ICANON handling in canon_copy_from_read_buf(), that code is really completely incomprehensible. I know how it works, and why it does it, but I had to remind myself, because the code just looks crazy and does things like "*nr+1" to walk _past_ the point we actually copy etc. I was very tempted to rewrite that entirely, but wanting to keep my changes minimal and targeted made me not do so).
Linus
From bf6ee858fdff2a1800fd198bbe90034dcd60f3ef Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Thu, 21 Jan 2021 10:04:27 -0800
Subject: [PATCH 1/3] tty: fix up hung_up_tty_write() conversion
In commit "tty: implement write_iter", I left the write_iter conversion of the hung up tty case alone, because I incorrectly thought it didn't matter.
Jiri showed me the errors of my ways, and pointed out the problems with that incomplete conversion. Fix it all up.
--- drivers/tty/tty_io.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/tty/tty_io.c +5 -4
@@ -437,8 +437,7 @@ static ssize_t hung_up_tty_read(struct file *file, char __user *buf,return 0 ; } -static ssize_t hung_up_tty_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) +static ssize_t hung_up_tty_write (struct kiocb *iocb,struct iov_iter *from) {return -EIO ; }
@@ -506,7 +505,7 @@ static const struct file_operations console_fops = {static const struct file_operations hung_up_tty_fops = { .llseek = no_llseek, .read = hung_up_tty_read, - .write = hung_up_tty_write, + .write_iter = hung_up_tty_write, .poll = hung_up_tty_poll, .unlocked_ioctl = hung_up_tty_ioctl, .compat_ioctl = hung_up_tty_compat_ioctl,
@@ -1103,7 +1102,9 @@ static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from)if (tty->ops->write_room ==NULL )tty_err (tty,"missing write_room method\n" ); ld =tty_ldisc_ref_wait (tty); -if (!ld || !ld->ops->write) +if (!ld) +return hung_up_tty_write (iocb, from); +if (!ld->ops->write) ret = -EIO ;else ret =do_tty_write (ld->ops->write, tty, file, from);
Signature · 1 line
2.29.2.157.g1d47791a39
From 1443b92a1ff3a0af5f0e5a177db2d843273a2ca1 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Thu, 21 Jan 2021 10:08:15 -0800
Subject: [PATCH 2/3] tty: fix up hung_up_tty_read() conversion
In commit "tty: implement read_iter", I left the read_iter conversion of the hung up tty case alone, because I incorrectly thought it didn't matter.
Jiri showed me the errors of my ways, and pointed out the problems with that incomplete conversion. Fix it all up.
--- drivers/tty/tty_io.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/tty/tty_io.c +5 -4
@@ -431,8 +431,7 @@ struct tty_driver *tty_find_polling_driver(char *name, int *line)EXPORT_SYMBOL_GPL (tty_find_polling_driver);#endif -static ssize_t hung_up_tty_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) +static ssize_t hung_up_tty_read (struct kiocb *iocb,struct iov_iter *to) {return 0 ; }
@@ -504,7 +503,7 @@ static const struct file_operations console_fops = {static const struct file_operations hung_up_tty_fops = { .llseek = no_llseek, - .read = hung_up_tty_read, + .read_iter = hung_up_tty_read, .write_iter = hung_up_tty_write, .poll = hung_up_tty_poll, .unlocked_ioctl = hung_up_tty_ioctl,
@@ -924,8 +923,10 @@ static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)/* We want to wait for the line discipline to sort out in this situation */ ld =tty_ldisc_ref_wait (tty); +if (!ld) +return hung_up_tty_read (iocb, to); i = -EIO ; -if (ld && ld->ops->read) +if (ld->ops->read) i =iterate_tty_read (ld, tty, file, to);tty_ldisc_deref (ld);
Signature · 1 line
2.29.2.157.g1d47791a39
From 2b3da8cf7ecafe48704f62046fce5da5d17b9e6a Mon Sep 17 00:00:00 2001
From: Linus Torvalds <[email protected]>
Date: Thu, 21 Jan 2021 10:17:25 -0800
Subject: [PATCH 3/3] tty: fix up iterate_tty_read() EOVERFLOW handling
When I converted the tty_ldisc_ops 'read()' function to take a kernel pointer, I was a bit too aggressive about the ldisc returning EOVERFLOW.
Yes, we want to have EOVERFLOW override any partially read data (because the whole point is that the buffer was too small for the whole packet, and we don't want to see partial packets), but it shouldn't override a previous EFAULT.
And in fact, it really is just EOVERFLOW that is special and should throw away any partially read data, not "any error". Admittedly EOVERFLOW is currently the only one that can happen for a continuation read - and if the first read iteration returns an error we won't have this issue.
So this is more of a technicality, but let's just make the intent very explicit, and re-organize the error handling a bit so that this is all clearer.
--- drivers/tty/tty_io.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/tty/tty_io.c +13 -6
@@ -861,13 +861,20 @@ static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,if (!size)break ; -/* -* A ldisc read error return will override any previously copied -* data (eg -EOVERFLOW from HDLC) -*/ if (size <0 ) { -memzero_explicit (kernel_buf,sizeof (kernel_buf)); -return size; +/* Did we have an earlier error (ie -EFAULT)? */ +if (retval) +break ; + retval = size; + +/* +* -EOVERFLOW means we didn't have enough space +* for a whole packet, and we shouldn't return +* a partial result. +*/ +if (retval == -EOVERFLOW ) + offset =0 ; +break ; } copied =copy_to_iter (kernel_buf, size, to);
Signature · 1 line
2.29.2.157.g1d47791a39
Greg Kroah-Hartman
maintainerQuoted 7 lines · Linus Torvalds
Quoted 4 lines · Greg Kroah-Hartman
Incremental patches please as these are already in my public branches and I would have to revert them and add new ones but that's messy, so fixes on top is fine.
Quoted 8 lines
Ok. And since I think you put that first
tty_writeconversion patch in a different branch from thetty_readone, I did the fixup patches for the two as separate patches, even though they really just do the exact same thing.So here's three patches: the two fixups for the
hung_up_ttycase, and the EOVERFLOW error case that Jiri also noted. I've also updated the 'tty-splice' branch if you prefer them that way.
This works, thanks for these. I'll wait for Jiri to review them before applying them to my branches...
greg k-h
Quoted 3 lines · Greg Kroah-Hartman
This works, thanks for these. I'll wait for Jiri to review them before applying them to my branches...
Let's hope Jiri sees them, since he had some email issue earlier..
I'll add his suse address here too.
Linus
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Greg Kroah-Hartman
This works, thanks for these. I'll wait for Jiri to review them before applying them to my branches...
Let's hope Jiri sees them, since he had some email issue earlier..
I'll add his suse address here too.
Thanks, I am fixed and nothing was lost :).
Signature · 2 lines
js suse labs
Quoted 21 lines · Linus Torvalds
Quoted 4 lines · Greg Kroah-Hartman
Incremental patches please as these are already in my public branches and I would have to revert them and add new ones but that's messy, so fixes on top is fine.
Ok. And since I think you put that first
tty_writeconversion patch in a different branch from thetty_readone, I did the fixup patches for the two as separate patches, even though they really just do the exact same thing.So here's three patches: the two fixups for the
hung_up_ttycase, and the EOVERFLOW error case that Jiri also noted. I've also updated the 'tty-splice' branch if you prefer them that way.And I *should* say that I still haven't tested _any_ of the HDLC changes. I have no idea how to do that, and if somebody can point to a test-case (or better yet, actually has a real life situation where they use it and can test this all) it would be great.
Quoted 4 lines
Jiri, any other issues, or any comment of yours I missed? I didn't do the
min()thing, I find the explicit conditional more legible myself, but won't complain if somebody else then disagrees and wants to clean it up.
I cannot find anything else.
All three:
thanks,
Signature · 1 line
js
Greg Kroah-Hartman
maintainerQuoted 27 lines · Jiri Slaby
Quoted 21 lines · Linus Torvalds
Quoted 4 lines · Greg Kroah-Hartman
Incremental patches please as these are already in my public branches and I would have to revert them and add new ones but that's messy, so fixes on top is fine.
Ok. And since I think you put that first
tty_writeconversion patch in a different branch from thetty_readone, I did the fixup patches for the two as separate patches, even though they really just do the exact same thing.So here's three patches: the two fixups for the
hung_up_ttycase, and the EOVERFLOW error case that Jiri also noted. I've also updated the 'tty-splice' branch if you prefer them that way.And I *should* say that I still haven't tested _any_ of the HDLC changes. I have no idea how to do that, and if somebody can point to a test-case (or better yet, actually has a real life situation where they use it and can test this all) it would be great.
Quoted 4 lines
Jiri, any other issues, or any comment of yours I missed? I didn't do the
min()thing, I find the explicit conditional more legible myself, but won't complain if somebody else then disagrees and wants to clean it up.
Quoted 4 lines
I cannot find anything else.
All three:
Reviewed-by Jiri Slaby <[email protected]>
Thanks for the review, I'll go apply these in a bit...
greg k-h