Skip to content

Commit 3da1814

Browse files
nunojsajic23
authored andcommitted
iio: buffer: make sure O_NONBLOCK is respected
For output buffers, there's no guarantee that the buffer won't be full in the first iteration of the loop in which case we would block independently of userspace passing O_NONBLOCK or not. Fix it by always checking the flag before going to sleep. While at it (and as it's a bit related), refactored the loop so that the stop condition is 'written != n', i.e, run the loop until all data has been copied into the IIO buffers. This makes the code a bit simpler. Fixes: 9eeee3b ("iio: Add output buffer support") Signed-off-by: Nuno Sá <nuno.sa@analog.com> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de> Link: https://lore.kernel.org/r/20230216101452.591805-3-nuno.sa@analog.com Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent b5184a2 commit 3da1814

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

drivers/iio/industrialio-buffer.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,24 @@ static ssize_t iio_buffer_write(struct file *filp, const char __user *buf,
203203
break;
204204
}
205205

206+
if (filp->f_flags & O_NONBLOCK) {
207+
if (!written)
208+
ret = -EAGAIN;
209+
break;
210+
}
211+
206212
wait_woken(&wait, TASK_INTERRUPTIBLE,
207213
MAX_SCHEDULE_TIMEOUT);
208214
continue;
209215
}
210216

211217
ret = rb->access->write(rb, n - written, buf + written);
212-
if (ret == 0 && (filp->f_flags & O_NONBLOCK))
213-
ret = -EAGAIN;
218+
if (ret < 0)
219+
break;
214220

215-
if (ret > 0) {
216-
written += ret;
217-
if (written != n && !(filp->f_flags & O_NONBLOCK))
218-
continue;
219-
}
220-
} while (ret == 0);
221+
written += ret;
222+
223+
} while (written != n);
221224
remove_wait_queue(&rb->pollq, &wait);
222225

223226
return ret < 0 ? ret : written;

0 commit comments

Comments
 (0)