[PATCH v3 4/4] btrfs: avoid allocating unnecessary page pointers

Qu Wenruo
2020-08-25T05:48:37Z

Commit 142349f541d0 ("btrfs: lower the dirty balance poll interval")
introduced one limit which is definitely suspicious:

- ensure we always have 8 pages allocated
The 8 lower limit looks pretty strange, this means even we're just
writing 4K, we will allocate page pointers for 8 pages no matter what.
To me, this 8 pages look more like a upper limit.

This 8 pages upper limit looks so incorrect that my eyes alawys correct
it into "min(, 8)" other than "max(, 8)".

This patch will use a fixed size (SZ_64K) other than page numbers to
setup the upper limit.
Also, with comment added to show why we need a upper limit.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/file.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 67d2368a8fa6..de6d1c313042 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1561,7 +1561,14 @@ static int calc_nr_pages(loff_t pos, struct iov_iter *iov)

nr_pages = min(nr_pages, current->nr_dirtied_pause -
current->nr_dirtied);
- nr_pages = max(nr_pages, 8);
+
+ /*
+ * Limit the batch to 64K, too large batch may lead to higher memory
+ * pressure and increase the possibility of short-copy.
+ * With more and more short-copy, the benefit of batch copy would be
+ * hugely reduced, as we will fall back to page-by-page copy.
+ */
+ nr_pages = min(nr_pages, SZ_64K / PAGE_SIZE);
return nr_pages;
}

--
2.28.0