Commit 42ea907d authored by Ian Abbott's avatar Ian Abbott Committed by Greg Kroah-Hartman

staging: comedi: allow buffer wraparound in comedi_read()

`comedi_read()` copies data from the acquisition data buffer, which is
cyclic, to the user buffer using a single call to `copy_to_user()`.  It
currently avoids having to deal with wraparound of the cyclic buffer by
limiting the amount it copies (and the amount returned to the user).
Change it to deal with the wraparound using two calls to
`copy_to_user()` if necessary.
Signed-off-by: default avatarIan Abbott <abbotti@mev.co.uk>
Reviewed-by: default avatarH Hartley Sweeten <hsweeten@visionengravers.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 8ea93928
...@@ -2491,11 +2491,11 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes, ...@@ -2491,11 +2491,11 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
add_wait_queue(&async->wait_head, &wait); add_wait_queue(&async->wait_head, &wait);
while (nbytes > 0 && !retval) { while (nbytes > 0 && !retval) {
unsigned int rp, n1, n2;
set_current_state(TASK_INTERRUPTIBLE); set_current_state(TASK_INTERRUPTIBLE);
m = comedi_buf_read_n_available(s); m = comedi_buf_read_n_available(s);
if (async->buf_read_ptr + m > async->prealloc_bufsz)
m = async->prealloc_bufsz - async->buf_read_ptr;
n = min_t(size_t, m, nbytes); n = min_t(size_t, m, nbytes);
if (n == 0) { if (n == 0) {
...@@ -2532,8 +2532,14 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes, ...@@ -2532,8 +2532,14 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
} }
continue; continue;
} }
m = copy_to_user(buf, async->prealloc_buf + rp = async->buf_read_ptr;
async->buf_read_ptr, n); n1 = min(n, async->prealloc_bufsz - rp);
n2 = n - n1;
m = copy_to_user(buf, async->prealloc_buf + rp, n1);
if (m)
m += n2;
else if (n2)
m = copy_to_user(buf + n1, async->prealloc_buf, n2);
if (m) { if (m) {
n -= m; n -= m;
retval = -EFAULT; retval = -EFAULT;
......
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