staging: iio: iio_ring_rip_outer return immediately if rip_lots returns <= 0
Subsystems (3)
The last two days I was casing a bug related sw ringbuffer usage. Reading from an empty buffer caused my system to crash anytime later in all kinds of fashions. The crash happened at a time where no iio related function calls where going on.
It appears that on my nommu development system freeing memory twice can lead to such bugs. And the fact that it does error right away made debugging this issue a pain.
iio_ring_rip_outer() calls rb->access.rip_lots() which defaults in the sw ring buffer case to iio_rip_sw_rb().
iio_rip_sw_rb() returns 0 and frees the local buffer:
if (initial_write_p == initial_read_p) /* No new data available.*/ or if (unlikely(initial_read_p == NULL)) /* No data here as yet */
However iio_ring_rip_outer() only checks for copied/retured < 0. It then carries on copies 0 bytes to user and frees the buffer again.
PATCH 1/1 staging: iio: iio_ring_rip_outer return immediately if rip_lots returns <= 0
From: Michael Hennerich <[email protected]>
--- drivers/staging/iio/industrialio-ring.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/iio/industrialio-ring.c +1 -1
@@ -105,7 +105,7 @@ static ssize_t iio_ring_rip_outer(struct file *filp, char __user *buf,return -EINVAL ; copied = rb->access.rip_lots (rb, count, &data, &dead_offset); -if (copied <0 ) { +if (copied <=0 ) { ret = copied; goto error_ret; }
Signature · 1 line
1.6.0.2
Quoted 3 lines · On 10/07/10 11:41, [email protected] wrote:
From: Michael Hennerich <[email protected]>
Diagnosis of the problem looks right. Sorry about this one, all my test cases only read from the buffer when they get an event so I've never triggered this one. Guess I need another test case!
I think freeing twice is going to cause trouble on any machine, I've just never triggered the case. I'll probably start putting together a more comprehensive test program based around the new generic_buffer program to hammer these corner cases.
Thanks for your hard work. You are nailing a lot of bugs that have been there for quite a while.
There is another issue somewhere in that code to do with very small reads. I triggered it due to a userspace bug a while ago, but haven't had a chance to chase down the cause as yet. Actually it seems fairly plausible that it is another way of triggering the issue you have fixed here.
Great to get some eyes on that ring buffer code, it is a fairly hideous mess!
Quoted 1 line
Signed-off-by Michael Hennerich <[email protected]>
Quoted 17 lines
--- drivers/staging/iio/industrialio-ring.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)drivers/staging/iio/industrialio-ring.c +1
-1@@ -105,7 +105,7 @@ static ssize_t iio_ring_rip_outer(struct file *filp, char __user *buf,return -EINVAL ; copied = rb->access.rip_lots (rb, count, &data, &dead_offset); -if (copied <0 ) { +if (copied <=0 ) { ret = copied; goto error_ret; }