diff mbox series

[26/44] unify pipe_get_pages() and pipe_get_pages_alloc()

Message ID 20220622041552.737754-26-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
The differences between those two are
* pipe_get_pages() gets a non-NULL struct page ** value pointing to
preallocated array + array size.
* pipe_get_pages_alloc() gets an address of struct page ** variable that
contains NULL, allocates the array and (on success) stores its address in
that variable.

	Not hard to combine - always pass struct page ***, have
the previous pipe_get_pages_alloc() caller pass ~0U as cap for
array size.

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

Comments

Jeff Layton June 28, 2022, 11:49 a.m. UTC | #1
On Wed, 2022-06-22 at 05:15 +0100, Al Viro wrote:
> 	The differences between those two are
> * pipe_get_pages() gets a non-NULL struct page ** value pointing to
> preallocated array + array size.
> * pipe_get_pages_alloc() gets an address of struct page ** variable that
> contains NULL, allocates the array and (on success) stores its address in
> that variable.
> 
> 	Not hard to combine - always pass struct page ***, have
> the previous pipe_get_pages_alloc() caller pass ~0U as cap for
> array size.
> 
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> ---
>  lib/iov_iter.c | 49 +++++++++++++++++--------------------------------
>  1 file changed, 17 insertions(+), 32 deletions(-)
> 
> diff --git a/lib/iov_iter.c b/lib/iov_iter.c
> index 5c985cf2858e..1c98f2f3a581 100644
> --- a/lib/iov_iter.c
> +++ b/lib/iov_iter.c
> @@ -1187,6 +1187,11 @@ unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
>  }
>  EXPORT_SYMBOL(iov_iter_gap_alignment);
>  
> +static struct page **get_pages_array(size_t n)
> +{
> +	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
> +}
> +
>  static inline ssize_t __pipe_get_pages(struct iov_iter *i,
>  				size_t maxsize,
>  				struct page **pages,
> @@ -1220,10 +1225,11 @@ static inline ssize_t __pipe_get_pages(struct iov_iter *i,
>  }
>  
>  static ssize_t pipe_get_pages(struct iov_iter *i,
> -		   struct page **pages, size_t maxsize, unsigned maxpages,
> +		   struct page ***pages, size_t maxsize, unsigned maxpages,
>  		   size_t *start)
>  {
>  	unsigned int npages, off;
> +	struct page **p;
>  	size_t capacity;
>  
>  	if (!sanity(i))
> @@ -1231,8 +1237,15 @@ static ssize_t pipe_get_pages(struct iov_iter *i,
>  
>  	*start = off = pipe_npages(i, &npages);
>  	capacity = min(npages, maxpages) * PAGE_SIZE - off;
> +	maxsize = min(maxsize, capacity);
> +	p = *pages;
> +	if (!p) {
> +		*pages = p = get_pages_array(DIV_ROUND_UP(maxsize + off, PAGE_SIZE));
> +		if (!p)
> +			return -ENOMEM;
> +	}
>  
> -	return __pipe_get_pages(i, min(maxsize, capacity), pages, off);
> +	return __pipe_get_pages(i, maxsize, p, off);
>  }
>  
>  static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
> @@ -1394,41 +1407,13 @@ ssize_t iov_iter_get_pages(struct iov_iter *i,
>  		return len - *start;
>  	}
>  	if (iov_iter_is_pipe(i))
> -		return pipe_get_pages(i, pages, maxsize, maxpages, start);
> +		return pipe_get_pages(i, &pages, maxsize, maxpages, start);
>  	if (iov_iter_is_xarray(i))
>  		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
>  	return -EFAULT;
>  }
>  EXPORT_SYMBOL(iov_iter_get_pages);
>  
> -static struct page **get_pages_array(size_t n)
> -{
> -	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
> -}
> -
> -static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
> -		   struct page ***pages, size_t maxsize,
> -		   size_t *start)
> -{
> -	struct page **p;
> -	unsigned int npages, off;
> -	ssize_t n;
> -
> -	if (!sanity(i))
> -		return -EFAULT;
> -
> -	*start = off = pipe_npages(i, &npages);
> -	n = npages * PAGE_SIZE - off;
> -	if (maxsize > n)
> -		maxsize = n;
> -	else
> -		npages = DIV_ROUND_UP(maxsize + off, PAGE_SIZE);
> -	*pages = p = get_pages_array(npages);
> -	if (!p)
> -		return -ENOMEM;
> -	return __pipe_get_pages(i, maxsize, p, off);
> -}
> -
>  static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
>  					   struct page ***pages, size_t maxsize,
>  					   size_t *_start_offset)
> @@ -1511,7 +1496,7 @@ static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
>  		return len - *start;
>  	}
>  	if (iov_iter_is_pipe(i))
> -		return pipe_get_pages_alloc(i, pages, maxsize, start);
> +		return pipe_get_pages(i, pages, maxsize, ~0U, start);
>  	if (iov_iter_is_xarray(i))
>  		return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
>  	return -EFAULT;

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

Patch

diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 5c985cf2858e..1c98f2f3a581 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -1187,6 +1187,11 @@  unsigned long iov_iter_gap_alignment(const struct iov_iter *i)
 }
 EXPORT_SYMBOL(iov_iter_gap_alignment);
 
+static struct page **get_pages_array(size_t n)
+{
+	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
+}
+
 static inline ssize_t __pipe_get_pages(struct iov_iter *i,
 				size_t maxsize,
 				struct page **pages,
@@ -1220,10 +1225,11 @@  static inline ssize_t __pipe_get_pages(struct iov_iter *i,
 }
 
 static ssize_t pipe_get_pages(struct iov_iter *i,
-		   struct page **pages, size_t maxsize, unsigned maxpages,
+		   struct page ***pages, size_t maxsize, unsigned maxpages,
 		   size_t *start)
 {
 	unsigned int npages, off;
+	struct page **p;
 	size_t capacity;
 
 	if (!sanity(i))
@@ -1231,8 +1237,15 @@  static ssize_t pipe_get_pages(struct iov_iter *i,
 
 	*start = off = pipe_npages(i, &npages);
 	capacity = min(npages, maxpages) * PAGE_SIZE - off;
+	maxsize = min(maxsize, capacity);
+	p = *pages;
+	if (!p) {
+		*pages = p = get_pages_array(DIV_ROUND_UP(maxsize + off, PAGE_SIZE));
+		if (!p)
+			return -ENOMEM;
+	}
 
-	return __pipe_get_pages(i, min(maxsize, capacity), pages, off);
+	return __pipe_get_pages(i, maxsize, p, off);
 }
 
 static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa,
@@ -1394,41 +1407,13 @@  ssize_t iov_iter_get_pages(struct iov_iter *i,
 		return len - *start;
 	}
 	if (iov_iter_is_pipe(i))
-		return pipe_get_pages(i, pages, maxsize, maxpages, start);
+		return pipe_get_pages(i, &pages, maxsize, maxpages, start);
 	if (iov_iter_is_xarray(i))
 		return iter_xarray_get_pages(i, pages, maxsize, maxpages, start);
 	return -EFAULT;
 }
 EXPORT_SYMBOL(iov_iter_get_pages);
 
-static struct page **get_pages_array(size_t n)
-{
-	return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL);
-}
-
-static ssize_t pipe_get_pages_alloc(struct iov_iter *i,
-		   struct page ***pages, size_t maxsize,
-		   size_t *start)
-{
-	struct page **p;
-	unsigned int npages, off;
-	ssize_t n;
-
-	if (!sanity(i))
-		return -EFAULT;
-
-	*start = off = pipe_npages(i, &npages);
-	n = npages * PAGE_SIZE - off;
-	if (maxsize > n)
-		maxsize = n;
-	else
-		npages = DIV_ROUND_UP(maxsize + off, PAGE_SIZE);
-	*pages = p = get_pages_array(npages);
-	if (!p)
-		return -ENOMEM;
-	return __pipe_get_pages(i, maxsize, p, off);
-}
-
 static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
 					   struct page ***pages, size_t maxsize,
 					   size_t *_start_offset)
@@ -1511,7 +1496,7 @@  static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i,
 		return len - *start;
 	}
 	if (iov_iter_is_pipe(i))
-		return pipe_get_pages_alloc(i, pages, maxsize, start);
+		return pipe_get_pages(i, pages, maxsize, ~0U, start);
 	if (iov_iter_is_xarray(i))
 		return iter_xarray_get_pages_alloc(i, pages, maxsize, start);
 	return -EFAULT;