Skip to content

Commit

Permalink
tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer
Browse files Browse the repository at this point in the history
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 <hch@lst.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Linus Torvalds committed Jan 21, 2021
1 parent 9bb48c8 commit 3b830a9
Showing 14 changed files with 178 additions and 102 deletions.
34 changes: 17 additions & 17 deletions drivers/bluetooth/hci_ldisc.c
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 3 additions & 1 deletion drivers/input/serio/serport.c
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion drivers/net/ppp/ppp_async.c
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion drivers/net/ppp/ppp_synctty.c
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion drivers/tty/n_gsm.c
Original file line number Diff line number Diff line change
@@ -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;
}
60 changes: 41 additions & 19 deletions drivers/tty/n_hdlc.c
Original file line number Diff line number Diff line change
@@ -416,13 +416,19 @@ static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
* Returns the number of bytes returned or error code.
*/
static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
__u8 __user *buf, size_t nr)
__u8 *kbuf, size_t nr,
void **cookie, unsigned long offset)
{
struct n_hdlc *n_hdlc = tty->disc_data;
int ret = 0;
struct n_hdlc_buf *rbuf;
DECLARE_WAITQUEUE(wait, current);

/* Is this a repeated call for an rbuf we already found earlier? */
rbuf = *cookie;
if (rbuf)
goto have_rbuf;

add_wait_queue(&tty->read_wait, &wait);

for (;;) {
@@ -436,25 +442,8 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
set_current_state(TASK_INTERRUPTIBLE);

rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
if (rbuf) {
if (rbuf->count > nr) {
/* too large for caller's buffer */
ret = -EOVERFLOW;
} else {
__set_current_state(TASK_RUNNING);
if (copy_to_user(buf, rbuf->buf, rbuf->count))
ret = -EFAULT;
else
ret = rbuf->count;
}

if (n_hdlc->rx_free_buf_list.count >
DEFAULT_RX_BUF_COUNT)
kfree(rbuf);
else
n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
if (rbuf)
break;
}

/* no data */
if (tty_io_nonblock(tty, file)) {
@@ -473,6 +462,39 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
remove_wait_queue(&tty->read_wait, &wait);
__set_current_state(TASK_RUNNING);

if (!rbuf)
return ret;
*cookie = rbuf;

have_rbuf:
/* Have we used it up entirely? */
if (offset >= rbuf->count)
goto done_with_rbuf;

/* More data to go, but can't copy any more? EOVERFLOW */
ret = -EOVERFLOW;
if (!nr)
goto done_with_rbuf;

/* Copy as much data as possible */
ret = rbuf->count - offset;
if (ret > nr)
ret = nr;
memcpy(kbuf, rbuf->buf+offset, ret);
offset += ret;

/* If we still have data left, we leave the rbuf in the cookie */
if (offset < rbuf->count)
return ret;

done_with_rbuf:
*cookie = NULL;

if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
kfree(rbuf);
else
n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);

return ret;

} /* end of n_hdlc_tty_read() */
3 changes: 2 additions & 1 deletion drivers/tty/n_null.c
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 4 additions & 6 deletions drivers/tty/n_r3964.c
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion drivers/tty/n_tracerouter.c
Original file line number Diff line number Diff line change
@@ -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;
}

4 changes: 3 additions & 1 deletion drivers/tty/n_tracesink.c
Original file line number Diff line number Diff line change
@@ -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;
}

Loading

0 comments on commit 3b830a9

Please sign in to comment.