Commit bf5091c8 authored by David Sterba's avatar David Sterba
Browse files

btrfs: use kvzalloc for EXTENT_SAME temporary data



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.

Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 67b07bd4
Loading
Loading
Loading
Loading
+9 −7
Original line number Original line Diff line number Diff line
@@ -3144,12 +3144,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
	 * 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.
	 * 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.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
	cmp.dst_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
				       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) {
	if (!cmp.src_pages || !cmp.dst_pages) {
		kfree(cmp.src_pages);
		ret = -ENOMEM;
		kfree(cmp.dst_pages);
		goto out_free;
		return -ENOMEM;
	}
	}


	for (i = 0; i < chunk_count; i++) {
	for (i = 0; i < chunk_count; i++) {
@@ -3172,8 +3173,9 @@ out_unlock:
	else
	else
		btrfs_double_inode_unlock(src, dst);
		btrfs_double_inode_unlock(src, dst);


	kfree(cmp.src_pages);
out_free:
	kfree(cmp.dst_pages);
	kvfree(cmp.src_pages);
	kvfree(cmp.dst_pages);


	return ret;
	return ret;
}
}