Re: [PATCH 05/10] btrfs: convert heuristic_collect_sample() to use folios

From: Qu Wenruo

Date: Mon Sep 07 2026 - 02:46:01 EST




在 2026/9/7 07:59, Tal Zussman 写道:
Convert the sampling loop to folios. This removes the last caller of
find_get_page() in btrfs and saves a call to compound_head() per sampled
page. Document that the lookup is not supposed to fail with an ASSERT().

Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>

Reviewed-by: Qu Wenruo <wqu@xxxxxxxx>

Although the existing code doesn't looks particularly well, e.g. it's still fully based on page index, and not handling large folios correctly that well.

E.g. if we hit a large folio, we can handle the whole range inside the folio in one go, without the need to grab example pages by pages.

But that's for future improvement, and shouldn't bother your page->folio interface change.

Thanks,
Qu
---
fs/btrfs/compression.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 979b2ffbd8fc..fa8b92592321 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -1488,7 +1488,7 @@ static bool sample_repeated_patterns(struct heuristic_ws *ws)
static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end,
struct heuristic_ws *ws)
{
- struct page *page;
+ struct folio *folio;
pgoff_t index, index_end;
u32 i, curr_sample_pos;
u8 *in_data;
@@ -1514,8 +1514,10 @@ static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end,
curr_sample_pos = 0;
while (index < index_end) {
- page = find_get_page(inode->i_mapping, index);
- in_data = kmap_local_page(page);
+ folio = filemap_get_folio(inode->i_mapping, index);
+ ASSERT(!IS_ERR(folio));
+ in_data = kmap_local_folio(folio,
+ offset_in_folio(folio, (u64)index << PAGE_SHIFT));
/* Handle case where the start is not aligned to PAGE_SIZE */
i = start % PAGE_SIZE;
while (i < PAGE_SIZE - SAMPLING_READ_SIZE) {
@@ -1529,7 +1531,7 @@ static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end,
curr_sample_pos += SAMPLING_READ_SIZE;
}
kunmap_local(in_data);
- put_page(page);
+ folio_put(folio);
index++;
}