Commit 6c776766 authored by David Howells's avatar David Howells Committed by Al Viro

iov_iter: Fix iter_xarray_get_pages{,_alloc}()

The maths at the end of iter_xarray_get_pages() to calculate the actual
size doesn't work under some circumstances, such as when it's been asked to
extract a partial single page.  Various terms of the equation cancel out
and you end up with actual == offset.  The same issue exists in
iter_xarray_get_pages_alloc().

Fix these to just use min() to select the lesser amount from between the
amount of page content transcribed into the buffer, minus the offset, and
the size limit specified.

This doesn't appear to have caused a problem yet upstream because network
filesystems aren't getting the pages from an xarray iterator, but rather
passing it directly to the socket, which just iterates over it.  Cachefiles
*does* do DIO from one to/from ext4/xfs/btrfs/etc. but it always asks for
whole pages to be written or read.

Fixes: 7ff50620 ("iov_iter: Add ITER_XARRAY")
Reported-by: default avatarJeff Layton <jlayton@kernel.org>
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
cc: Alexander Viro <viro@zeniv.linux.org.uk>
cc: Dominique Martinet <asmadeus@codewreck.org>
cc: Mike Marshall <hubcap@omnibond.com>
cc: Gao Xiang <xiang@kernel.org>
cc: linux-afs@lists.infradead.org
cc: v9fs-developer@lists.sourceforge.net
cc: devel@lists.orangefs.org
cc: linux-erofs@lists.ozlabs.org
cc: linux-cachefs@redhat.com
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent f2906aa8
...@@ -1434,7 +1434,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i, ...@@ -1434,7 +1434,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
{ {
unsigned nr, offset; unsigned nr, offset;
pgoff_t index, count; pgoff_t index, count;
size_t size = maxsize, actual; size_t size = maxsize;
loff_t pos; loff_t pos;
if (!size || !maxpages) if (!size || !maxpages)
...@@ -1461,13 +1461,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i, ...@@ -1461,13 +1461,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
if (nr == 0) if (nr == 0)
return 0; return 0;
actual = PAGE_SIZE * nr; return min(nr * PAGE_SIZE - offset, maxsize);
actual -= offset;
if (nr == count && size > 0) {
unsigned last_offset = (nr > 1) ? 0 : offset;
actual -= PAGE_SIZE - (last_offset + size);
}
return actual;
} }
/* must be done on non-empty ITER_IOVEC one */ /* must be done on non-empty ITER_IOVEC one */
...@@ -1602,7 +1596,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i, ...@@ -1602,7 +1596,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
struct page **p; struct page **p;
unsigned nr, offset; unsigned nr, offset;
pgoff_t index, count; pgoff_t index, count;
size_t size = maxsize, actual; size_t size = maxsize;
loff_t pos; loff_t pos;
if (!size) if (!size)
...@@ -1631,13 +1625,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i, ...@@ -1631,13 +1625,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
if (nr == 0) if (nr == 0)
return 0; return 0;
actual = PAGE_SIZE * nr; return min(nr * PAGE_SIZE - offset, maxsize);
actual -= offset;
if (nr == count && size > 0) {
unsigned last_offset = (nr > 1) ? 0 : offset;
actual -= PAGE_SIZE - (last_offset + size);
}
return actual;
} }
ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
......
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