diff mbox series

[32/44] iov_iter: massage calling conventions for first_{iovec,bvec}_segment()

Message ID 20220622041552.737754-32-viro@zeniv.linux.org.uk (mailing list archive)
State New, archived
Headers show
Series [01/44] 9p: handling Rerror without copy_from_iter_full() | expand

Commit Message

Al Viro June 22, 2022, 4:15 a.m. UTC
Pass maxsize by reference, return length via the same.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
---
 lib/iov_iter.c | 37 +++++++++++++++----------------------
 1 file changed, 15 insertions(+), 22 deletions(-)

Comments

Jeff Layton June 28, 2022, 12:06 p.m. UTC | #1
On Wed, 2022-06-22 at 05:15 +0100, Al Viro wrote:
> Pass maxsize by reference, return length via the same.
> 
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> ---
>  lib/iov_iter.c | 37 +++++++++++++++----------------------
>  1 file changed, 15 insertions(+), 22 deletions(-)
> 
> diff --git a/lib/iov_iter.c b/lib/iov_iter.c
> index 0bed684d91d0..fca66ecce7a0 100644
> --- a/lib/iov_iter.c
> +++ b/lib/iov_iter.c
> @@ -1306,26 +1306,22 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
>  	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
>  }
>  
> -static unsigned long found_ubuf_segment(unsigned long addr,
> -					size_t len,
> -					size_t *size, size_t *start)
> +static unsigned long found_ubuf_segment(unsigned long addr, size_t *start)
>  {
>  	*start = addr % PAGE_SIZE;
> -	*size = len;
>  	return addr & PAGE_MASK;
>  }
>  
>  /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
>  static unsigned long first_iovec_segment(const struct iov_iter *i,
> -					 size_t *size, size_t *start,
> -					 size_t maxsize)
> +					 size_t *size, size_t *start)
>  {
>  	size_t skip;
>  	long k;
>  
>  	if (iter_is_ubuf(i)) {
>  		unsigned long addr = (unsigned long)i->ubuf + i->iov_offset;
> -		return found_ubuf_segment(addr, maxsize, size, start);
> +		return found_ubuf_segment(addr, start);
>  	}
>  
>  	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
> @@ -1334,28 +1330,26 @@ static unsigned long first_iovec_segment(const struct iov_iter *i,
>  
>  		if (unlikely(!len))
>  			continue;
> -		if (len > maxsize)
> -			len = maxsize;
> -		return found_ubuf_segment(addr, len, size, start);
> +		if (*size > len)
> +			*size = len;
> +		return found_ubuf_segment(addr, start);
>  	}
>  	BUG(); // if it had been empty, we wouldn't get called
>  }
>  
>  /* must be done on non-empty ITER_BVEC one */
>  static struct page *first_bvec_segment(const struct iov_iter *i,
> -				       size_t *size, size_t *start,
> -				       size_t maxsize)
> +				       size_t *size, size_t *start)
>  {
>  	struct page *page;
>  	size_t skip = i->iov_offset, len;
>  
>  	len = i->bvec->bv_len - skip;
> -	if (len > maxsize)
> -		len = maxsize;
> +	if (*size > len)
> +		*size = len;
>  	skip += i->bvec->bv_offset;
>  	page = i->bvec->bv_page + skip / PAGE_SIZE;
>  	*start = skip % PAGE_SIZE;
> -	*size = len;
>  	return page;
>  }
>  
> @@ -1363,7 +1357,6 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
>  		   struct page ***pages, size_t maxsize,
>  		   unsigned int maxpages, size_t *start)
>  {
> -	size_t len;
>  	int n, res;
>  
>  	if (maxsize > i->count)
> @@ -1382,8 +1375,8 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
>  		if (i->nofault)
>  			gup_flags |= FOLL_NOFAULT;
>  
> -		addr = first_iovec_segment(i, &len, start, maxsize);
> -		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
> +		addr = first_iovec_segment(i, &maxsize, start);
> +		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
>  		if (n > maxpages)
>  			n = maxpages;
>  		if (!*pages) {
> @@ -1394,14 +1387,14 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
>  		res = get_user_pages_fast(addr, n, gup_flags, *pages);
>  		if (unlikely(res <= 0))
>  			return res;
> -		return min_t(size_t, len, res * PAGE_SIZE - *start);
> +		return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
>  	}
>  	if (iov_iter_is_bvec(i)) {
>  		struct page **p;
>  		struct page *page;
>  
> -		page = first_bvec_segment(i, &len, start, maxsize);
> -		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
> +		page = first_bvec_segment(i, &maxsize, start);
> +		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
>  		if (n > maxpages)
>  			n = maxpages;
>  		p = *pages;
> @@ -1412,7 +1405,7 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
>  		}
>  		for (int k = 0; k < n; k++)
>  			get_page(*p++ = page++);
> -		return min_t(size_t, len, n * PAGE_SIZE - *start);
> +		return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
>  	}
>  	if (iov_iter_is_pipe(i))
>  		return pipe_get_pages(i, pages, maxsize, maxpages, start);

Reviewed-by: Jeff Layton <jlayton@kernel.org>
diff mbox series

Patch

diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 0bed684d91d0..fca66ecce7a0 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -1306,26 +1306,22 @@  static ssize_t iter_xarray_get_pages(struct iov_iter *i,
 	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
 }
 
-static unsigned long found_ubuf_segment(unsigned long addr,
-					size_t len,
-					size_t *size, size_t *start)
+static unsigned long found_ubuf_segment(unsigned long addr, size_t *start)
 {
 	*start = addr % PAGE_SIZE;
-	*size = len;
 	return addr & PAGE_MASK;
 }
 
 /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */
 static unsigned long first_iovec_segment(const struct iov_iter *i,
-					 size_t *size, size_t *start,
-					 size_t maxsize)
+					 size_t *size, size_t *start)
 {
 	size_t skip;
 	long k;
 
 	if (iter_is_ubuf(i)) {
 		unsigned long addr = (unsigned long)i->ubuf + i->iov_offset;
-		return found_ubuf_segment(addr, maxsize, size, start);
+		return found_ubuf_segment(addr, start);
 	}
 
 	for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) {
@@ -1334,28 +1330,26 @@  static unsigned long first_iovec_segment(const struct iov_iter *i,
 
 		if (unlikely(!len))
 			continue;
-		if (len > maxsize)
-			len = maxsize;
-		return found_ubuf_segment(addr, len, size, start);
+		if (*size > len)
+			*size = len;
+		return found_ubuf_segment(addr, start);
 	}
 	BUG(); // if it had been empty, we wouldn't get called
 }
 
 /* must be done on non-empty ITER_BVEC one */
 static struct page *first_bvec_segment(const struct iov_iter *i,
-				       size_t *size, size_t *start,
-				       size_t maxsize)
+				       size_t *size, size_t *start)
 {
 	struct page *page;
 	size_t skip = i->iov_offset, len;
 
 	len = i->bvec->bv_len - skip;
-	if (len > maxsize)
-		len = maxsize;
+	if (*size > len)
+		*size = len;
 	skip += i->bvec->bv_offset;
 	page = i->bvec->bv_page + skip / PAGE_SIZE;
 	*start = skip % PAGE_SIZE;
-	*size = len;
 	return page;
 }
 
@@ -1363,7 +1357,6 @@  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		   struct page ***pages, size_t maxsize,
 		   unsigned int maxpages, size_t *start)
 {
-	size_t len;
 	int n, res;
 
 	if (maxsize > i->count)
@@ -1382,8 +1375,8 @@  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		if (i->nofault)
 			gup_flags |= FOLL_NOFAULT;
 
-		addr = first_iovec_segment(i, &len, start, maxsize);
-		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
+		addr = first_iovec_segment(i, &maxsize, start);
+		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
 		if (n > maxpages)
 			n = maxpages;
 		if (!*pages) {
@@ -1394,14 +1387,14 @@  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		res = get_user_pages_fast(addr, n, gup_flags, *pages);
 		if (unlikely(res <= 0))
 			return res;
-		return min_t(size_t, len, res * PAGE_SIZE - *start);
+		return min_t(size_t, maxsize, res * PAGE_SIZE - *start);
 	}
 	if (iov_iter_is_bvec(i)) {
 		struct page **p;
 		struct page *page;
 
-		page = first_bvec_segment(i, &len, start, maxsize);
-		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
+		page = first_bvec_segment(i, &maxsize, start);
+		n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE);
 		if (n > maxpages)
 			n = maxpages;
 		p = *pages;
@@ -1412,7 +1405,7 @@  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		}
 		for (int k = 0; k < n; k++)
 			get_page(*p++ = page++);
-		return min_t(size_t, len, n * PAGE_SIZE - *start);
+		return min_t(size_t, maxsize, n * PAGE_SIZE - *start);
 	}
 	if (iov_iter_is_pipe(i))
 		return pipe_get_pages(i, pages, maxsize, maxpages, start);