Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH] btrfs: zoned: do not limit delalloc size to fs_info->max_extent_size
@ 2023-06-12 15:10 Naohiro Aota
  2023-06-12 22:22 ` David Sterba
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Naohiro Aota @ 2023-06-12 15:10 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Naohiro Aota

There is an issue writing out huge buffered data with the compressed mount
option on zoned mode. For example, when you buffered write 16GB data and
call "sync" command on "mount -o compress" file-system on a zoned device,
it takes more than 3 minutes to finish the sync, invoking the hung_task
timeout.

Before the patch:

    + xfs_io -f -c 'pwrite -b 8M 0 16G' /mnt/test/foo
    wrote 17179869184/17179869184 bytes at offset 0
    16.000 GiB, 2048 ops; 0:00:23.74 (690.013 MiB/sec and 86.2516 ops/sec)

    real    0m23.770s
    user    0m0.005s
    sys     0m23.599s
    + sync

    real    3m55.921s
    user    0m0.000s
    sys     0m0.134s

After the patch:

    + xfs_io -f -c 'pwrite -b 8M 0 16G' /mnt/test/foo
    wrote 17179869184/17179869184 bytes at offset 0
    16.000 GiB, 2048 ops; 0:00:28.11 (582.648 MiB/sec and 72.8311 ops/sec)

    real    0m28.146s
    user    0m0.004s
    sys     0m27.951s
    + sync

    real    0m12.310s
    user    0m0.000s
    sys     0m0.048s

This slow "sync" comes from inefficient async chunk building due to
needlessly limited delalloc size.

find_lock_delalloc_range() looks for pages for the delayed allocation at
most fs_info->max_extent_size in its size. For the non-compress write case,
that range directly corresponds to num_bytes at cow_file_range() (= size of
allocation). So, limiting the size to the max_extent_size seems no harm as
we will split the extent even if we can have a larger allocation.

However, things are different with the compression case. The
run_delalloc_compressed() divides the delalloc range into 512 KB sized
chunks and queues a worker for each chunk. The device of the above test
case has 672 KB zone_append_max_bytes, which is equal to
fs_info->max_extent_size. Thus, one run_delalloc_compressed() call can
build only two chunks at most, which is really smaller than
BTRFS_MAX_EXTENT_SIZE / 512KB = 256, making it inefficient.

Also, as 672 KB is not aligned to 512 KB, it is creating ceil(16G / 672K) *
2 = 49934 async chunks for the above case. OTOH, with BTRFS_MAX_EXTENT_SIZE
delalloced, we will create 32768 chunks, which is 34% reduced.

This patch reverts the delalloc size to BTRFS_MAX_EXTENT_SIZE, as it does
not directly corresponds to the size of one extent. Instead, this patch
will limit the allocation size at cow_file_range() for the zoned mode.

As shown above, with this patch applied, the "sync" time is reduced from
almost 4 minutes to 12 seconds.

Fixes: f7b12a62f008 ("btrfs: replace BTRFS_MAX_EXTENT_SIZE with fs_info->max_extent_size")
CC: stable@vger.kernel.org # 6.1+
---
 fs/btrfs/extent-tree.c |  3 +++
 fs/btrfs/extent_io.c   | 11 ++++++++---
 fs/btrfs/inode.c       |  5 ++++-
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 911908ea5f6f..e6944c4db18b 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4452,6 +4452,9 @@ int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
 	bool for_treelog = (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
 	bool for_data_reloc = (btrfs_is_data_reloc_root(root) && is_data);
 
+	if (btrfs_is_zoned(fs_info))
+		ASSERT(num_bytes <= fs_info->max_extent_size);
+
 	flags = get_alloc_profile_by_root(root, is_data);
 again:
 	WARN_ON(num_bytes < fs_info->sectorsize);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index a91d5ad27984..bd86d9fc2b31 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -371,12 +371,17 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
 				    struct page *locked_page, u64 *start,
 				    u64 *end)
 {
-	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
 	struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
 	const u64 orig_start = *start;
 	const u64 orig_end = *end;
-	/* The sanity tests may not set a valid fs_info. */
-	u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE;
+	/*
+	 * We don't use fs_info->max_extent_size here. The delalloc range will
+	 * not directly corresponds to the size of an extent. The allocation
+	 * size will be capped by either cow_file_range() (only for zoned) or
+	 * run_delalloc_compressed(). We can give large enough size to collect
+	 * delalloc pages.
+	 */
+	u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
 	u64 delalloc_start;
 	u64 delalloc_end;
 	bool found;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 27bb1298f73c..3823ecc836c2 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1494,7 +1494,10 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
 	while (num_bytes > 0) {
 		struct btrfs_ordered_extent *ordered;
 
-		cur_alloc_size = num_bytes;
+		if (btrfs_is_zoned(fs_info))
+			cur_alloc_size = min_t(u64, num_bytes, fs_info->max_extent_size);
+		else
+			cur_alloc_size = num_bytes;
 		ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
 					   min_alloc_size, 0, alloc_hint,
 					   &ins, 1, 1);
-- 
2.41.0


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

end of thread, other threads:[~2023-06-19  4:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-12 15:10 [PATCH] btrfs: zoned: do not limit delalloc size to fs_info->max_extent_size Naohiro Aota
2023-06-12 22:22 ` David Sterba
2023-06-13  3:35   ` Naohiro Aota
2023-06-13  5:17 ` Christoph Hellwig
2023-06-13  5:53   ` Naohiro Aota
2023-06-14  1:59     ` Naohiro Aota
2023-06-14  5:44       ` hch
2023-06-19  4:35         ` Naohiro Aota
2023-06-14  2:05 ` Naohiro Aota
2023-06-14 22:08   ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox