Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'tty-splice' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux into tty-next

Fixes both the "splice/sendfile to a tty" and "splice/sendfile from a
tty" regression from 5.10.

* 'tty-splice' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux:
tty: teach the n_tty ICANON case about the new "cookie continuations" too
tty: teach n_tty line discipline about the new "cookie continuations"
tty: clean up legacy leftovers from n_tty line discipline
tty: implement read_iter
tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
tty: implement write_iter

+264 -151
+17 -17
drivers/bluetooth/hci_ldisc.c
··· 802 802 * We don't provide read/write/poll interface for user space. 803 803 */ 804 804 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file, 805 - unsigned char __user *buf, size_t nr) 805 + unsigned char *buf, size_t nr, 806 + void **cookie, unsigned long offset) 806 807 { 807 808 return 0; 808 809 } ··· 820 819 return 0; 821 820 } 822 821 822 + static struct tty_ldisc_ops hci_uart_ldisc = { 823 + .owner = THIS_MODULE, 824 + .magic = TTY_LDISC_MAGIC, 825 + .name = "n_hci", 826 + .open = hci_uart_tty_open, 827 + .close = hci_uart_tty_close, 828 + .read = hci_uart_tty_read, 829 + .write = hci_uart_tty_write, 830 + .ioctl = hci_uart_tty_ioctl, 831 + .compat_ioctl = hci_uart_tty_ioctl, 832 + .poll = hci_uart_tty_poll, 833 + .receive_buf = hci_uart_tty_receive, 834 + .write_wakeup = hci_uart_tty_wakeup, 835 + }; 836 + 823 837 static int __init hci_uart_init(void) 824 838 { 825 - static struct tty_ldisc_ops hci_uart_ldisc; 826 839 int err; 827 840 828 841 BT_INFO("HCI UART driver ver %s", VERSION); 829 842 830 843 /* Register the tty discipline */ 831 - 832 - memset(&hci_uart_ldisc, 0, sizeof(hci_uart_ldisc)); 833 - hci_uart_ldisc.magic = TTY_LDISC_MAGIC; 834 - hci_uart_ldisc.name = "n_hci"; 835 - hci_uart_ldisc.open = hci_uart_tty_open; 836 - hci_uart_ldisc.close = hci_uart_tty_close; 837 - hci_uart_ldisc.read = hci_uart_tty_read; 838 - hci_uart_ldisc.write = hci_uart_tty_write; 839 - hci_uart_ldisc.ioctl = hci_uart_tty_ioctl; 840 - hci_uart_ldisc.compat_ioctl = hci_uart_tty_ioctl; 841 - hci_uart_ldisc.poll = hci_uart_tty_poll; 842 - hci_uart_ldisc.receive_buf = hci_uart_tty_receive; 843 - hci_uart_ldisc.write_wakeup = hci_uart_tty_wakeup; 844 - hci_uart_ldisc.owner = THIS_MODULE; 845 - 846 844 err = tty_register_ldisc(N_HCI, &hci_uart_ldisc); 847 845 if (err) { 848 846 BT_ERR("HCI line discipline registration failed. (%d)", err);
+3 -1
drivers/input/serio/serport.c
··· 156 156 * returning 0 characters. 157 157 */ 158 158 159 - static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr) 159 + static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, 160 + unsigned char *kbuf, size_t nr, 161 + void **cookie, unsigned long offset) 160 162 { 161 163 struct serport *serport = (struct serport*) tty->disc_data; 162 164 struct serio *serio;
+2 -1
drivers/net/ppp/ppp_async.c
··· 259 259 */ 260 260 static ssize_t 261 261 ppp_asynctty_read(struct tty_struct *tty, struct file *file, 262 - unsigned char __user *buf, size_t count) 262 + unsigned char *buf, size_t count, 263 + void **cookie, unsigned long offset) 263 264 { 264 265 return -EAGAIN; 265 266 }
+2 -1
drivers/net/ppp/ppp_synctty.c
··· 257 257 */ 258 258 static ssize_t 259 259 ppp_sync_read(struct tty_struct *tty, struct file *file, 260 - unsigned char __user *buf, size_t count) 260 + unsigned char *buf, size_t count, 261 + void **cookie, unsigned long offset) 261 262 { 262 263 return -EAGAIN; 263 264 }
+2 -1
drivers/tty/n_gsm.c
··· 2559 2559 */ 2560 2560 2561 2561 static ssize_t gsmld_read(struct tty_struct *tty, struct file *file, 2562 - unsigned char __user *buf, size_t nr) 2562 + unsigned char *buf, size_t nr, 2563 + void **cookie, unsigned long offset) 2563 2564 { 2564 2565 return -EOPNOTSUPP; 2565 2566 }
+41 -19
drivers/tty/n_hdlc.c
··· 416 416 * Returns the number of bytes returned or error code. 417 417 */ 418 418 static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, 419 - __u8 __user *buf, size_t nr) 419 + __u8 *kbuf, size_t nr, 420 + void **cookie, unsigned long offset) 420 421 { 421 422 struct n_hdlc *n_hdlc = tty->disc_data; 422 423 int ret = 0; 423 424 struct n_hdlc_buf *rbuf; 424 425 DECLARE_WAITQUEUE(wait, current); 426 + 427 + /* Is this a repeated call for an rbuf we already found earlier? */ 428 + rbuf = *cookie; 429 + if (rbuf) 430 + goto have_rbuf; 425 431 426 432 add_wait_queue(&tty->read_wait, &wait); 427 433 ··· 442 436 set_current_state(TASK_INTERRUPTIBLE); 443 437 444 438 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); 445 - if (rbuf) { 446 - if (rbuf->count > nr) { 447 - /* too large for caller's buffer */ 448 - ret = -EOVERFLOW; 449 - } else { 450 - __set_current_state(TASK_RUNNING); 451 - if (copy_to_user(buf, rbuf->buf, rbuf->count)) 452 - ret = -EFAULT; 453 - else 454 - ret = rbuf->count; 455 - } 456 - 457 - if (n_hdlc->rx_free_buf_list.count > 458 - DEFAULT_RX_BUF_COUNT) 459 - kfree(rbuf); 460 - else 461 - n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf); 439 + if (rbuf) 462 440 break; 463 - } 464 441 465 442 /* no data */ 466 443 if (tty_io_nonblock(tty, file)) { ··· 461 472 462 473 remove_wait_queue(&tty->read_wait, &wait); 463 474 __set_current_state(TASK_RUNNING); 475 + 476 + if (!rbuf) 477 + return ret; 478 + *cookie = rbuf; 479 + 480 + have_rbuf: 481 + /* Have we used it up entirely? */ 482 + if (offset >= rbuf->count) 483 + goto done_with_rbuf; 484 + 485 + /* More data to go, but can't copy any more? EOVERFLOW */ 486 + ret = -EOVERFLOW; 487 + if (!nr) 488 + goto done_with_rbuf; 489 + 490 + /* Copy as much data as possible */ 491 + ret = rbuf->count - offset; 492 + if (ret > nr) 493 + ret = nr; 494 + memcpy(kbuf, rbuf->buf+offset, ret); 495 + offset += ret; 496 + 497 + /* If we still have data left, we leave the rbuf in the cookie */ 498 + if (offset < rbuf->count) 499 + return ret; 500 + 501 + done_with_rbuf: 502 + *cookie = NULL; 503 + 504 + if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT) 505 + kfree(rbuf); 506 + else 507 + n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf); 464 508 465 509 return ret; 466 510
+2 -1
drivers/tty/n_null.c
··· 20 20 } 21 21 22 22 static ssize_t n_null_read(struct tty_struct *tty, struct file *file, 23 - unsigned char __user * buf, size_t nr) 23 + unsigned char *buf, size_t nr, 24 + void **cookie, unsigned long offset) 24 25 { 25 26 return -EOPNOTSUPP; 26 27 }
+4 -6
drivers/tty/n_r3964.c
··· 129 129 static int r3964_open(struct tty_struct *tty); 130 130 static void r3964_close(struct tty_struct *tty); 131 131 static ssize_t r3964_read(struct tty_struct *tty, struct file *file, 132 - unsigned char __user * buf, size_t nr); 132 + void *cookie, unsigned char *buf, size_t nr); 133 133 static ssize_t r3964_write(struct tty_struct *tty, struct file *file, 134 134 const unsigned char *buf, size_t nr); 135 135 static int r3964_ioctl(struct tty_struct *tty, struct file *file, ··· 1058 1058 } 1059 1059 1060 1060 static ssize_t r3964_read(struct tty_struct *tty, struct file *file, 1061 - unsigned char __user * buf, size_t nr) 1061 + unsigned char *kbuf, size_t nr, 1062 + void **cookie, unsigned long offset) 1062 1063 { 1063 1064 struct r3964_info *pInfo = tty->disc_data; 1064 1065 struct r3964_client_info *pClient; ··· 1110 1109 kfree(pMsg); 1111 1110 TRACE_M("r3964_read - msg kfree %p", pMsg); 1112 1111 1113 - if (copy_to_user(buf, &theMsg, ret)) { 1114 - ret = -EFAULT; 1115 - goto unlock; 1116 - } 1112 + memcpy(kbuf, &theMsg, ret); 1117 1113 1118 1114 TRACE_PS("read - return %d", ret); 1119 1115 goto unlock;
+3 -1
drivers/tty/n_tracerouter.c
··· 118 118 * -EINVAL 119 119 */ 120 120 static ssize_t n_tracerouter_read(struct tty_struct *tty, struct file *file, 121 - unsigned char __user *buf, size_t nr) { 121 + unsigned char *buf, size_t nr, 122 + void **cookie, unsigned long offset) 123 + { 122 124 return -EINVAL; 123 125 } 124 126
+3 -1
drivers/tty/n_tracesink.c
··· 115 115 * -EINVAL 116 116 */ 117 117 static ssize_t n_tracesink_read(struct tty_struct *tty, struct file *file, 118 - unsigned char __user *buf, size_t nr) { 118 + unsigned char *buf, size_t nr, 119 + void **cookie, unsigned long offset) 120 + { 119 121 return -EINVAL; 120 122 } 121 123
+85 -66
drivers/tty/n_tty.c
··· 164 164 memset(buffer, 0x00, size); 165 165 } 166 166 167 - static int tty_copy_to_user(struct tty_struct *tty, void __user *to, 168 - size_t tail, size_t n) 167 + static void tty_copy(struct tty_struct *tty, void *to, size_t tail, size_t n) 169 168 { 170 169 struct n_tty_data *ldata = tty->disc_data; 171 170 size_t size = N_TTY_BUF_SIZE - tail; 172 171 void *from = read_buf_addr(ldata, tail); 173 - int uncopied; 174 172 175 173 if (n > size) { 176 174 tty_audit_add_data(tty, from, size); 177 - uncopied = copy_to_user(to, from, size); 178 - zero_buffer(tty, from, size - uncopied); 179 - if (uncopied) 180 - return uncopied; 175 + memcpy(to, from, size); 176 + zero_buffer(tty, from, size); 181 177 to += size; 182 178 n -= size; 183 179 from = ldata->read_buf; 184 180 } 185 181 186 182 tty_audit_add_data(tty, from, n); 187 - uncopied = copy_to_user(to, from, n); 188 - zero_buffer(tty, from, n - uncopied); 189 - return uncopied; 183 + memcpy(to, from, n); 184 + zero_buffer(tty, from, n); 190 185 } 191 186 192 187 /** ··· 1941 1946 /** 1942 1947 * copy_from_read_buf - copy read data directly 1943 1948 * @tty: terminal device 1944 - * @b: user data 1949 + * @kbp: data 1945 1950 * @nr: size of data 1946 1951 * 1947 1952 * Helper function to speed up n_tty_read. It is only called when 1948 - * ICANON is off; it copies characters straight from the tty queue to 1949 - * user space directly. It can be profitably called twice; once to 1950 - * drain the space from the tail pointer to the (physical) end of the 1951 - * buffer, and once to drain the space from the (physical) beginning of 1952 - * the buffer to head pointer. 1953 + * ICANON is off; it copies characters straight from the tty queue. 1953 1954 * 1954 1955 * Called under the ldata->atomic_read_lock sem 1956 + * 1957 + * Returns true if it successfully copied data, but there is still 1958 + * more data to be had. 1955 1959 * 1956 1960 * n_tty_read()/consumer path: 1957 1961 * caller holds non-exclusive termios_rwsem 1958 1962 * read_tail published 1959 1963 */ 1960 1964 1961 - static int copy_from_read_buf(struct tty_struct *tty, 1962 - unsigned char __user **b, 1965 + static bool copy_from_read_buf(struct tty_struct *tty, 1966 + unsigned char **kbp, 1963 1967 size_t *nr) 1964 1968 1965 1969 { 1966 1970 struct n_tty_data *ldata = tty->disc_data; 1967 - int retval; 1968 1971 size_t n; 1969 1972 bool is_eof; 1970 1973 size_t head = smp_load_acquire(&ldata->commit_head); 1971 1974 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); 1972 1975 1973 - retval = 0; 1974 1976 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail); 1975 1977 n = min(*nr, n); 1976 1978 if (n) { 1977 1979 unsigned char *from = read_buf_addr(ldata, tail); 1978 - retval = copy_to_user(*b, from, n); 1979 - n -= retval; 1980 + memcpy(*kbp, from, n); 1980 1981 is_eof = n == 1 && *from == EOF_CHAR(tty); 1981 1982 tty_audit_add_data(tty, from, n); 1982 1983 zero_buffer(tty, from, n); ··· 1980 1989 /* Turn single EOF into zero-length read */ 1981 1990 if (L_EXTPROC(tty) && ldata->icanon && is_eof && 1982 1991 (head == ldata->read_tail)) 1983 - n = 0; 1984 - *b += n; 1992 + return false; 1993 + *kbp += n; 1985 1994 *nr -= n; 1995 + 1996 + /* If we have more to copy, let the caller know */ 1997 + return head != ldata->read_tail; 1986 1998 } 1987 - return retval; 1999 + return false; 1988 2000 } 1989 2001 1990 2002 /** 1991 2003 * canon_copy_from_read_buf - copy read data in canonical mode 1992 2004 * @tty: terminal device 1993 - * @b: user data 2005 + * @kbp: data 1994 2006 * @nr: size of data 1995 2007 * 1996 2008 * Helper function for n_tty_read. It is only called when ICANON is on; 1997 2009 * it copies one line of input up to and including the line-delimiting 1998 - * character into the user-space buffer. 2010 + * character into the result buffer. 1999 2011 * 2000 2012 * NB: When termios is changed from non-canonical to canonical mode and 2001 2013 * the read buffer contains data, n_tty_set_termios() simulates an EOF ··· 2013 2019 * read_tail published 2014 2020 */ 2015 2021 2016 - static int canon_copy_from_read_buf(struct tty_struct *tty, 2017 - unsigned char __user **b, 2018 - size_t *nr) 2022 + static bool canon_copy_from_read_buf(struct tty_struct *tty, 2023 + unsigned char **kbp, 2024 + size_t *nr) 2019 2025 { 2020 2026 struct n_tty_data *ldata = tty->disc_data; 2021 2027 size_t n, size, more, c; 2022 2028 size_t eol; 2023 - size_t tail; 2024 - int ret, found = 0; 2029 + size_t tail, canon_head; 2030 + int found = 0; 2025 2031 2026 2032 /* N.B. avoid overrun if nr == 0 */ 2027 2033 if (!*nr) 2028 - return 0; 2034 + return false; 2029 2035 2030 - n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail); 2036 + canon_head = smp_load_acquire(&ldata->canon_head); 2037 + n = min(*nr + 1, canon_head - ldata->read_tail); 2031 2038 2032 2039 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); 2033 2040 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE); ··· 2058 2063 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n", 2059 2064 __func__, eol, found, n, c, tail, more); 2060 2065 2061 - ret = tty_copy_to_user(tty, *b, tail, n); 2062 - if (ret) 2063 - return -EFAULT; 2064 - *b += n; 2066 + tty_copy(tty, *kbp, tail, n); 2067 + *kbp += n; 2065 2068 *nr -= n; 2066 2069 2067 2070 if (found) ··· 2072 2079 else 2073 2080 ldata->push = 0; 2074 2081 tty_audit_push(); 2082 + return false; 2075 2083 } 2076 - return 0; 2084 + 2085 + /* No EOL found - do a continuation retry if there is more data */ 2086 + return ldata->read_tail != canon_head; 2077 2087 } 2078 2088 2079 2089 extern ssize_t redirected_tty_write(struct file *, const char __user *, ··· 2130 2134 */ 2131 2135 2132 2136 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, 2133 - unsigned char __user *buf, size_t nr) 2137 + unsigned char *kbuf, size_t nr, 2138 + void **cookie, unsigned long offset) 2134 2139 { 2135 2140 struct n_tty_data *ldata = tty->disc_data; 2136 - unsigned char __user *b = buf; 2141 + unsigned char *kb = kbuf; 2137 2142 DEFINE_WAIT_FUNC(wait, woken_wake_function); 2138 2143 int c; 2139 2144 int minimum, time; ··· 2142 2145 long timeout; 2143 2146 int packet; 2144 2147 size_t tail; 2148 + 2149 + /* 2150 + * Is this a continuation of a read started earler? 2151 + * 2152 + * If so, we still hold the atomic_read_lock and the 2153 + * termios_rwsem, and can just continue to copy data. 2154 + */ 2155 + if (*cookie) { 2156 + if (ldata->icanon && !L_EXTPROC(tty)) { 2157 + if (canon_copy_from_read_buf(tty, &kb, &nr)) 2158 + return kb - kbuf; 2159 + } else { 2160 + if (copy_from_read_buf(tty, &kb, &nr)) 2161 + return kb - kbuf; 2162 + } 2163 + 2164 + /* No more data - release locks and stop retries */ 2165 + n_tty_kick_worker(tty); 2166 + n_tty_check_unthrottle(tty); 2167 + up_read(&tty->termios_rwsem); 2168 + mutex_unlock(&ldata->atomic_read_lock); 2169 + *cookie = NULL; 2170 + return kb - kbuf; 2171 + } 2145 2172 2146 2173 c = job_control(tty, file); 2147 2174 if (c < 0) ··· 2204 2183 /* First test for status change. */ 2205 2184 if (packet && tty->link->ctrl_status) { 2206 2185 unsigned char cs; 2207 - if (b != buf) 2186 + if (kb != kbuf) 2208 2187 break; 2209 2188 spin_lock_irq(&tty->link->ctrl_lock); 2210 2189 cs = tty->link->ctrl_status; 2211 2190 tty->link->ctrl_status = 0; 2212 2191 spin_unlock_irq(&tty->link->ctrl_lock); 2213 - if (put_user(cs, b)) { 2214 - retval = -EFAULT; 2215 - break; 2216 - } 2217 - b++; 2192 + *kb++ = cs; 2218 2193 nr--; 2219 2194 break; 2220 2195 } ··· 2253 2236 } 2254 2237 2255 2238 if (ldata->icanon && !L_EXTPROC(tty)) { 2256 - retval = canon_copy_from_read_buf(tty, &b, &nr); 2257 - if (retval) 2258 - break; 2239 + if (canon_copy_from_read_buf(tty, &kb, &nr)) 2240 + goto more_to_be_read; 2259 2241 } else { 2260 - int uncopied; 2261 - 2262 2242 /* Deal with packet mode. */ 2263 - if (packet && b == buf) { 2264 - if (put_user(TIOCPKT_DATA, b)) { 2265 - retval = -EFAULT; 2266 - break; 2267 - } 2268 - b++; 2243 + if (packet && kb == kbuf) { 2244 + *kb++ = TIOCPKT_DATA; 2269 2245 nr--; 2270 2246 } 2271 2247 2272 - uncopied = copy_from_read_buf(tty, &b, &nr); 2273 - uncopied += copy_from_read_buf(tty, &b, &nr); 2274 - if (uncopied) { 2275 - retval = -EFAULT; 2276 - break; 2248 + /* 2249 + * Copy data, and if there is more to be had 2250 + * and we have nothing more to wait for, then 2251 + * let's mark us for retries. 2252 + * 2253 + * NOTE! We return here with both the termios_sem 2254 + * and atomic_read_lock still held, the retries 2255 + * will release them when done. 2256 + */ 2257 + if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) { 2258 + more_to_be_read: 2259 + remove_wait_queue(&tty->read_wait, &wait); 2260 + *cookie = cookie; 2261 + return kb - kbuf; 2277 2262 } 2278 2263 } 2279 2264 2280 2265 n_tty_check_unthrottle(tty); 2281 2266 2282 - if (b - buf >= minimum) 2267 + if (kb - kbuf >= minimum) 2283 2268 break; 2284 2269 if (time) 2285 2270 timeout = time; ··· 2293 2274 remove_wait_queue(&tty->read_wait, &wait); 2294 2275 mutex_unlock(&ldata->atomic_read_lock); 2295 2276 2296 - if (b - buf) 2297 - retval = b - buf; 2277 + if (kb - kbuf) 2278 + retval = kb - kbuf; 2298 2279 2299 2280 return retval; 2300 2281 }
+96 -34
drivers/tty/tty_io.c
··· 142 142 /* Mutex to protect creating and releasing a tty */ 143 143 DEFINE_MUTEX(tty_mutex); 144 144 145 - static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); 146 - static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); 147 - ssize_t redirected_tty_write(struct file *, const char __user *, 148 - size_t, loff_t *); 145 + static ssize_t tty_read(struct kiocb *, struct iov_iter *); 146 + static ssize_t tty_write(struct kiocb *, struct iov_iter *); 147 + ssize_t redirected_tty_write(struct kiocb *, struct iov_iter *); 149 148 static __poll_t tty_poll(struct file *, poll_table *); 150 149 static int tty_open(struct inode *, struct file *); 151 150 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); ··· 476 477 477 478 static const struct file_operations tty_fops = { 478 479 .llseek = no_llseek, 479 - .read = tty_read, 480 - .write = tty_write, 480 + .read_iter = tty_read, 481 + .write_iter = tty_write, 482 + .splice_read = generic_file_splice_read, 483 + .splice_write = iter_file_splice_write, 481 484 .poll = tty_poll, 482 485 .unlocked_ioctl = tty_ioctl, 483 486 .compat_ioctl = tty_compat_ioctl, ··· 491 490 492 491 static const struct file_operations console_fops = { 493 492 .llseek = no_llseek, 494 - .read = tty_read, 495 - .write = redirected_tty_write, 493 + .read_iter = tty_read, 494 + .write_iter = redirected_tty_write, 495 + .splice_read = generic_file_splice_read, 496 + .splice_write = iter_file_splice_write, 496 497 .poll = tty_poll, 497 498 .unlocked_ioctl = tty_ioctl, 498 499 .compat_ioctl = tty_compat_ioctl, ··· 626 623 /* This breaks for file handles being sent over AF_UNIX sockets ? */ 627 624 list_for_each_entry(priv, &tty->tty_files, list) { 628 625 filp = priv->file; 629 - if (filp->f_op->write == redirected_tty_write) 626 + if (filp->f_op->write_iter == redirected_tty_write) 630 627 cons_filp = filp; 631 - if (filp->f_op->write != tty_write) 628 + if (filp->f_op->write_iter != tty_write) 632 629 continue; 633 630 closecount++; 634 631 __tty_fasync(-1, filp, 0); /* can't block */ ··· 851 848 time->tv_sec = sec; 852 849 } 853 850 851 + /* 852 + * Iterate on the ldisc ->read() function until we've gotten all 853 + * the data the ldisc has for us. 854 + * 855 + * The "cookie" is something that the ldisc read function can fill 856 + * in to let us know that there is more data to be had. 857 + * 858 + * We promise to continue to call the ldisc until it stops returning 859 + * data or clears the cookie. The cookie may be something that the 860 + * ldisc maintains state for and needs to free. 861 + */ 862 + static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, 863 + struct file *file, struct iov_iter *to) 864 + { 865 + int retval = 0; 866 + void *cookie = NULL; 867 + unsigned long offset = 0; 868 + char kernel_buf[64]; 869 + size_t count = iov_iter_count(to); 870 + 871 + do { 872 + int size, copied; 873 + 874 + size = count > sizeof(kernel_buf) ? sizeof(kernel_buf) : count; 875 + size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); 876 + if (!size) 877 + break; 878 + 879 + /* 880 + * A ldisc read error return will override any previously copied 881 + * data (eg -EOVERFLOW from HDLC) 882 + */ 883 + if (size < 0) { 884 + memzero_explicit(kernel_buf, sizeof(kernel_buf)); 885 + return size; 886 + } 887 + 888 + copied = copy_to_iter(kernel_buf, size, to); 889 + offset += copied; 890 + count -= copied; 891 + 892 + /* 893 + * If the user copy failed, we still need to do another ->read() 894 + * call if we had a cookie to let the ldisc clear up. 895 + * 896 + * But make sure size is zeroed. 897 + */ 898 + if (unlikely(copied != size)) { 899 + count = 0; 900 + retval = -EFAULT; 901 + } 902 + } while (cookie); 903 + 904 + /* We always clear tty buffer in case they contained passwords */ 905 + memzero_explicit(kernel_buf, sizeof(kernel_buf)); 906 + return offset ? offset : retval; 907 + } 908 + 909 + 854 910 /** 855 911 * tty_read - read method for tty device files 856 912 * @file: pointer to tty file ··· 925 863 * read calls may be outstanding in parallel. 926 864 */ 927 865 928 - static ssize_t tty_read(struct file *file, char __user *buf, size_t count, 929 - loff_t *ppos) 866 + static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to) 930 867 { 931 868 int i; 869 + struct file *file = iocb->ki_filp; 932 870 struct inode *inode = file_inode(file); 933 871 struct tty_struct *tty = file_tty(file); 934 872 struct tty_ldisc *ld; ··· 941 879 /* We want to wait for the line discipline to sort out in this 942 880 situation */ 943 881 ld = tty_ldisc_ref_wait(tty); 944 - if (!ld) 945 - return hung_up_tty_read(file, buf, count, ppos); 946 - if (ld->ops->read) 947 - i = ld->ops->read(tty, file, buf, count); 948 - else 949 - i = -EIO; 882 + i = -EIO; 883 + if (ld && ld->ops->read) 884 + i = iterate_tty_read(ld, tty, file, to); 950 885 tty_ldisc_deref(ld); 951 886 952 887 if (i > 0) ··· 977 918 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), 978 919 struct tty_struct *tty, 979 920 struct file *file, 980 - const char __user *buf, 981 - size_t count) 921 + struct iov_iter *from) 982 922 { 923 + size_t count = iov_iter_count(from); 983 924 ssize_t ret, written = 0; 984 925 unsigned int chunk; 985 926 ··· 1031 972 size_t size = count; 1032 973 if (size > chunk) 1033 974 size = chunk; 975 + 1034 976 ret = -EFAULT; 1035 - if (copy_from_user(tty->write_buf, buf, size)) 977 + if (copy_from_iter(tty->write_buf, size, from) != size) 1036 978 break; 979 + 1037 980 ret = write(tty, file, tty->write_buf, size); 1038 981 if (ret <= 0) 1039 982 break; 983 + 984 + /* FIXME! Have Al check this! */ 985 + if (ret != size) 986 + iov_iter_revert(from, size-ret); 987 + 1040 988 written += ret; 1041 - buf += ret; 1042 989 count -= ret; 1043 990 if (!count) 1044 991 break; ··· 1104 1039 * write method will not be invoked in parallel for each device. 1105 1040 */ 1106 1041 1107 - static ssize_t tty_write(struct file *file, const char __user *buf, 1108 - size_t count, loff_t *ppos) 1042 + static ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from) 1109 1043 { 1044 + struct file *file = iocb->ki_filp; 1110 1045 struct tty_struct *tty = file_tty(file); 1111 1046 struct tty_ldisc *ld; 1112 1047 ssize_t ret; ··· 1119 1054 if (tty->ops->write_room == NULL) 1120 1055 tty_err(tty, "missing write_room method\n"); 1121 1056 ld = tty_ldisc_ref_wait(tty); 1122 - if (!ld) 1123 - return hung_up_tty_write(file, buf, count, ppos); 1124 - if (!ld->ops->write) 1057 + if (!ld || !ld->ops->write) 1125 1058 ret = -EIO; 1126 1059 else 1127 - ret = do_tty_write(ld->ops->write, tty, file, buf, count); 1060 + ret = do_tty_write(ld->ops->write, tty, file, from); 1128 1061 tty_ldisc_deref(ld); 1129 1062 return ret; 1130 1063 } 1131 1064 1132 - ssize_t redirected_tty_write(struct file *file, const char __user *buf, 1133 - size_t count, loff_t *ppos) 1065 + ssize_t redirected_tty_write(struct kiocb *iocb, struct iov_iter *iter) 1134 1066 { 1135 1067 struct file *p = NULL; 1136 1068 ··· 1138 1076 1139 1077 if (p) { 1140 1078 ssize_t res; 1141 - res = vfs_write(p, buf, count, &p->f_pos); 1079 + res = vfs_iocb_iter_write(p, iocb, iter); 1142 1080 fput(p); 1143 1081 return res; 1144 1082 } 1145 - return tty_write(file, buf, count, ppos); 1083 + return tty_write(iocb, iter); 1146 1084 } 1147 1085 1148 1086 /* ··· 2394 2332 { 2395 2333 if (!capable(CAP_SYS_ADMIN)) 2396 2334 return -EPERM; 2397 - if (file->f_op->write == redirected_tty_write) { 2335 + if (file->f_op->write_iter == redirected_tty_write) { 2398 2336 struct file *f; 2399 2337 spin_lock(&redirect_lock); 2400 2338 f = redirect; ··· 2987 2925 2988 2926 static int this_tty(const void *t, struct file *file, unsigned fd) 2989 2927 { 2990 - if (likely(file->f_op->read != tty_read)) 2928 + if (likely(file->f_op->read_iter != tty_read)) 2991 2929 return 0; 2992 2930 return file_tty(file) != t ? 0 : fd + 1; 2993 2931 }
+2 -1
include/linux/tty_ldisc.h
··· 185 185 void (*close)(struct tty_struct *); 186 186 void (*flush_buffer)(struct tty_struct *tty); 187 187 ssize_t (*read)(struct tty_struct *tty, struct file *file, 188 - unsigned char __user *buf, size_t nr); 188 + unsigned char *buf, size_t nr, 189 + void **cookie, unsigned long offset); 189 190 ssize_t (*write)(struct tty_struct *tty, struct file *file, 190 191 const unsigned char *buf, size_t nr); 191 192 int (*ioctl)(struct tty_struct *tty, struct file *file,
+2 -1
net/nfc/nci/uart.c
··· 292 292 293 293 /* We don't provide read/write/poll interface for user space. */ 294 294 static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file, 295 - unsigned char __user *buf, size_t nr) 295 + unsigned char *buf, size_t nr, 296 + void **cookie, unsigned long offset) 296 297 { 297 298 return 0; 298 299 }