Commit 32f13521 authored by Peter Hurley's avatar Peter Hurley Committed by Greg Kroah-Hartman

n_tty: Line copy to user buffer in canonical mode

Instead of pushing one char per loop, pre-compute the data length
to copy and copy all at once.
Signed-off-by: default avatarPeter Hurley <peter@hurleysoftware.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 88bb0de3
...@@ -74,6 +74,13 @@ ...@@ -74,6 +74,13 @@
#define ECHO_OP_SET_CANON_COL 0x81 #define ECHO_OP_SET_CANON_COL 0x81
#define ECHO_OP_ERASE_TAB 0x82 #define ECHO_OP_ERASE_TAB 0x82
#undef N_TTY_TRACE
#ifdef N_TTY_TRACE
# define n_tty_trace(f, args...) trace_printk(f, ##args)
#else
# define n_tty_trace(f, args...)
#endif
struct n_tty_data { struct n_tty_data {
unsigned int column; unsigned int column;
unsigned long overrun_time; unsigned long overrun_time;
...@@ -1748,35 +1755,84 @@ static int copy_from_read_buf(struct tty_struct *tty, ...@@ -1748,35 +1755,84 @@ static int copy_from_read_buf(struct tty_struct *tty,
} }
/** /**
* canon_copy_to_user - copy read data in canonical mode * canon_copy_from_read_buf - copy read data in canonical mode
* @tty: terminal device * @tty: terminal device
* @b: user data * @b: user data
* @nr: size of data * @nr: size of data
* *
* Helper function for n_tty_read. It is only called when ICANON is on; * Helper function for n_tty_read. It is only called when ICANON is on;
* it copies characters one at a time from the read buffer to the user * it copies one line of input up to and including the line-delimiting
* space buffer. * character into the user-space buffer.
* *
* Called under the atomic_read_lock mutex * Called under the atomic_read_lock mutex
*/ */
static int canon_copy_to_user(struct tty_struct *tty, static int canon_copy_from_read_buf(struct tty_struct *tty,
unsigned char __user **b, unsigned char __user **b,
size_t *nr) size_t *nr)
{ {
struct n_tty_data *ldata = tty->disc_data; struct n_tty_data *ldata = tty->disc_data;
unsigned long flags; unsigned long flags;
int eol, c; size_t n, size, more, c;
unsigned long eol;
int ret, tail, found = 0;
/* N.B. avoid overrun if nr == 0 */ /* N.B. avoid overrun if nr == 0 */
raw_spin_lock_irqsave(&ldata->read_lock, flags); raw_spin_lock_irqsave(&ldata->read_lock, flags);
while (*nr && ldata->read_cnt) {
eol = test_and_clear_bit(ldata->read_tail, ldata->read_flags); n = min_t(size_t, *nr, ldata->read_cnt);
c = ldata->read_buf[ldata->read_tail]; if (!n) {
ldata->read_tail = (ldata->read_tail+1) & (N_TTY_BUF_SIZE-1); raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
ldata->read_cnt--; return 0;
if (eol) { }
tail = ldata->read_tail;
size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
n_tty_trace("%s: nr:%zu tail:%d n:%zu size:%zu\n",
__func__, *nr, tail, n, size);
eol = find_next_bit(ldata->read_flags, size, tail);
more = n - (size - tail);
if (eol == N_TTY_BUF_SIZE && more) {
/* scan wrapped without finding set bit */
eol = find_next_bit(ldata->read_flags, more, 0);
if (eol != more)
found = 1;
} else if (eol != size)
found = 1;
size = N_TTY_BUF_SIZE - tail;
n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
c = n;
if (found && ldata->read_buf[eol] == __DISABLED_CHAR)
n--;
n_tty_trace("%s: eol:%lu found:%d n:%zu c:%zu size:%zu more:%zu\n",
__func__, eol, found, n, c, size, more);
raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
if (n > size) {
ret = copy_to_user(*b, &ldata->read_buf[tail], size);
if (ret)
return -EFAULT;
ret = copy_to_user(*b + size, ldata->read_buf, n - size);
} else
ret = copy_to_user(*b, &ldata->read_buf[tail], n);
if (ret)
return -EFAULT;
*b += n;
*nr -= n;
raw_spin_lock_irqsave(&ldata->read_lock, flags);
ldata->read_tail = (ldata->read_tail + c) & (N_TTY_BUF_SIZE - 1);
ldata->read_cnt -= c;
if (found) {
__clear_bit(eol, ldata->read_flags);
/* this test should be redundant: /* this test should be redundant:
* we shouldn't be reading data if * we shouldn't be reading data if
* canon_data is 0 * canon_data is 0
...@@ -1786,20 +1842,8 @@ static int canon_copy_to_user(struct tty_struct *tty, ...@@ -1786,20 +1842,8 @@ static int canon_copy_to_user(struct tty_struct *tty,
} }
raw_spin_unlock_irqrestore(&ldata->read_lock, flags); raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
if (!eol || (c != __DISABLED_CHAR)) { if (found)
if (tty_put_user(tty, c, *b))
return -EFAULT;
*b += 1;
*nr -= 1;
}
if (eol) {
tty_audit_push(tty); tty_audit_push(tty);
return 0;
}
raw_spin_lock_irqsave(&ldata->read_lock, flags);
}
raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
return 0; return 0;
} }
...@@ -1972,7 +2016,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, ...@@ -1972,7 +2016,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
} }
if (ldata->icanon && !L_EXTPROC(tty)) { if (ldata->icanon && !L_EXTPROC(tty)) {
retval = canon_copy_to_user(tty, &b, &nr); retval = canon_copy_from_read_buf(tty, &b, &nr);
if (retval) if (retval)
break; break;
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment