linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] btrfs: use kvzalloc for EXTENT_SAME temporary data
@ 2018-05-17 15:41 David Sterba
  2018-05-17 15:48 ` Nikolay Borisov
  0 siblings, 1 reply; 2+ messages in thread
From: David Sterba @ 2018-05-17 15:41 UTC (permalink / raw)
  To: linux-btrfs; +Cc: fdmanana, nefelim4ag, David Sterba

The dedupe range is 16 MiB, with 4 KiB pages and 8 byte pointers, the
arrays can be 32KiB large. To avoid allocation failures due to
fragmented memory, use the allocation with fallback to vmalloc.

The arrays are allocated and freed only inside btrfs_extent_same and
reused for all the ranges.

Signed-off-by: David Sterba <dsterba@suse.com>
---

Based on git://github.com/kdave/btrfs-devel.git ext/timofey/dedupe-16mb-limit

v2:
- use kvmalloc_array
- use __GFP_ZERO, as there's no kvzalloc_array
- check that there are no other frees missed,
  see https://patchwork.kernel.org/patch/10374941/ for reference

 fs/btrfs/ioctl.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index b572e38b4b64..20751829104d 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -3178,12 +3178,13 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
 	 * locking. We use an array for the page pointers. Size of the array is
 	 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
 	 */
-	cmp.src_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
-	cmp.dst_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
+	cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
+				       GFP_KERNEL | __GFP_ZERO);
+	cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
+				       GFP_KERNEL | __GFP_ZERO);
 	if (!cmp.src_pages || !cmp.dst_pages) {
-		kfree(cmp.src_pages);
-		kfree(cmp.dst_pages);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto out_free;
 	}
 
 	if (same_inode)
@@ -3211,8 +3212,9 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
 	else
 		btrfs_double_inode_unlock(src, dst);
 
-	kfree(cmp.src_pages);
-	kfree(cmp.dst_pages);
+out_free:
+	kvfree(cmp.src_pages);
+	kvfree(cmp.dst_pages);
 
 	return ret;
 }
-- 
2.16.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] btrfs: use kvzalloc for EXTENT_SAME temporary data
  2018-05-17 15:41 [PATCH v2] btrfs: use kvzalloc for EXTENT_SAME temporary data David Sterba
@ 2018-05-17 15:48 ` Nikolay Borisov
  0 siblings, 0 replies; 2+ messages in thread
From: Nikolay Borisov @ 2018-05-17 15:48 UTC (permalink / raw)
  To: David Sterba, linux-btrfs; +Cc: fdmanana, nefelim4ag



On 17.05.2018 18:41, David Sterba wrote:
> The dedupe range is 16 MiB, with 4 KiB pages and 8 byte pointers, the
> arrays can be 32KiB large. To avoid allocation failures due to
> fragmented memory, use the allocation with fallback to vmalloc.
> 
> The arrays are allocated and freed only inside btrfs_extent_same and
> reused for all the ranges.
> 
> Signed-off-by: David Sterba <dsterba@suse.com>

LGTM:

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
> ---
> 
> Based on git://github.com/kdave/btrfs-devel.git ext/timofey/dedupe-16mb-limit
> 
> v2:
> - use kvmalloc_array
> - use __GFP_ZERO, as there's no kvzalloc_array
> - check that there are no other frees missed,
>   see https://patchwork.kernel.org/patch/10374941/ for reference
> 
>  fs/btrfs/ioctl.c | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index b572e38b4b64..20751829104d 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -3178,12 +3178,13 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
>  	 * locking. We use an array for the page pointers. Size of the array is
>  	 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
>  	 */
> -	cmp.src_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
> -	cmp.dst_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
> +	cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
> +				       GFP_KERNEL | __GFP_ZERO);
> +	cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
> +				       GFP_KERNEL | __GFP_ZERO);
>  	if (!cmp.src_pages || !cmp.dst_pages) {
> -		kfree(cmp.src_pages);
> -		kfree(cmp.dst_pages);
> -		return -ENOMEM;
> +		ret = -ENOMEM;
> +		goto out_free;
>  	}
>  
>  	if (same_inode)
> @@ -3211,8 +3212,9 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
>  	else
>  		btrfs_double_inode_unlock(src, dst);
>  
> -	kfree(cmp.src_pages);
> -	kfree(cmp.dst_pages);
> +out_free:
> +	kvfree(cmp.src_pages);
> +	kvfree(cmp.dst_pages);
>  
>  	return ret;
>  }
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-05-17 15:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-17 15:41 [PATCH v2] btrfs: use kvzalloc for EXTENT_SAME temporary data David Sterba
2018-05-17 15:48 ` Nikolay Borisov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).