diff mbox series

[31/44] iov_iter: first_{iovec,bvec}_segment() - simplify a bit

Message ID 20220622041552.737754-31-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
We return length + offset in page via *size.  Don't bother - the caller
can do that arithmetics just as well; just report the length to it.

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

Comments

Jeff Layton June 28, 2022, 11:58 a.m. UTC | #1
On Wed, 2022-06-22 at 05:15 +0100, Al Viro wrote:
> We return length + offset in page via *size.  Don't bother - the caller
> can do that arithmetics just as well; just report the length to it.
> 
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> ---
>  lib/iov_iter.c | 22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/lib/iov_iter.c b/lib/iov_iter.c
> index 9ef671b101dc..0bed684d91d0 100644
> --- a/lib/iov_iter.c
> +++ b/lib/iov_iter.c
> @@ -1310,7 +1310,7 @@ static unsigned long found_ubuf_segment(unsigned long addr,
>  					size_t len,
>  					size_t *size, size_t *start)
>  {
> -	len += (*start = addr % PAGE_SIZE);
> +	*start = addr % PAGE_SIZE;
>  	*size = len;
>  	return addr & PAGE_MASK;
>  }
> @@ -1354,7 +1354,7 @@ static struct page *first_bvec_segment(const struct iov_iter *i,
>  		len = maxsize;
>  	skip += i->bvec->bv_offset;
>  	page = i->bvec->bv_page + skip / PAGE_SIZE;
> -	len += (*start = skip % PAGE_SIZE);
> +	*start = skip % PAGE_SIZE;
>  	*size = len;
>  	return page;
>  }
> @@ -1383,9 +1383,9 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
>  			gup_flags |= FOLL_NOFAULT;
>  
>  		addr = first_iovec_segment(i, &len, start, maxsize);
> -		if (len > maxpages * PAGE_SIZE)
> -			len = maxpages * PAGE_SIZE;
> -		n = DIV_ROUND_UP(len, PAGE_SIZE);
> +		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
> +		if (n > maxpages)
> +			n = maxpages;
>  		if (!*pages) {
>  			*pages = get_pages_array(n);
>  			if (!*pages)
> @@ -1394,25 +1394,25 @@ 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 (res == n ? len : res * PAGE_SIZE) - *start;
> +		return min_t(size_t, len, res * PAGE_SIZE - *start);
>  	}
>  	if (iov_iter_is_bvec(i)) {
>  		struct page **p;
>  		struct page *page;
>  
>  		page = first_bvec_segment(i, &len, start, maxsize);
> -		if (len > maxpages * PAGE_SIZE)
> -			len = maxpages * PAGE_SIZE;
> -		n = DIV_ROUND_UP(len, PAGE_SIZE);
> +		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
> +		if (n > maxpages)
> +			n = maxpages;
>  		p = *pages;
>  		if (!p) {
>  			*pages = p = get_pages_array(n);
>  			if (!p)
>  				return -ENOMEM;
>  		}
> -		while (n--)
> +		for (int k = 0; k < n; k++)
>  			get_page(*p++ = page++);
> -		return len - *start;
> +		return min_t(size_t, len, 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 9ef671b101dc..0bed684d91d0 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -1310,7 +1310,7 @@  static unsigned long found_ubuf_segment(unsigned long addr,
 					size_t len,
 					size_t *size, size_t *start)
 {
-	len += (*start = addr % PAGE_SIZE);
+	*start = addr % PAGE_SIZE;
 	*size = len;
 	return addr & PAGE_MASK;
 }
@@ -1354,7 +1354,7 @@  static struct page *first_bvec_segment(const struct iov_iter *i,
 		len = maxsize;
 	skip += i->bvec->bv_offset;
 	page = i->bvec->bv_page + skip / PAGE_SIZE;
-	len += (*start = skip % PAGE_SIZE);
+	*start = skip % PAGE_SIZE;
 	*size = len;
 	return page;
 }
@@ -1383,9 +1383,9 @@  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 			gup_flags |= FOLL_NOFAULT;
 
 		addr = first_iovec_segment(i, &len, start, maxsize);
-		if (len > maxpages * PAGE_SIZE)
-			len = maxpages * PAGE_SIZE;
-		n = DIV_ROUND_UP(len, PAGE_SIZE);
+		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
+		if (n > maxpages)
+			n = maxpages;
 		if (!*pages) {
 			*pages = get_pages_array(n);
 			if (!*pages)
@@ -1394,25 +1394,25 @@  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 (res == n ? len : res * PAGE_SIZE) - *start;
+		return min_t(size_t, len, res * PAGE_SIZE - *start);
 	}
 	if (iov_iter_is_bvec(i)) {
 		struct page **p;
 		struct page *page;
 
 		page = first_bvec_segment(i, &len, start, maxsize);
-		if (len > maxpages * PAGE_SIZE)
-			len = maxpages * PAGE_SIZE;
-		n = DIV_ROUND_UP(len, PAGE_SIZE);
+		n = DIV_ROUND_UP(len + *start, PAGE_SIZE);
+		if (n > maxpages)
+			n = maxpages;
 		p = *pages;
 		if (!p) {
 			*pages = p = get_pages_array(n);
 			if (!p)
 				return -ENOMEM;
 		}
-		while (n--)
+		for (int k = 0; k < n; k++)
 			get_page(*p++ = page++);
-		return len - *start;
+		return min_t(size_t, len, n * PAGE_SIZE - *start);
 	}
 	if (iov_iter_is_pipe(i))
 		return pipe_get_pages(i, pages, maxsize, maxpages, start);