[PATCH] f2fs: batch block invalidation in f2fs_do_zero_range()

From: Seongjae Jeong

Date: Sat Sep 26 2026 - 05:47:04 EST


f2fs_do_zero_range() calls f2fs_invalidate_blocks() for each mapped
data block. Each call acquires the SIT sentry write lock and updates
segment information.

Batch contiguous physical block addresses into a single invalidation
call. A NEW_ADDR slot does not end a run if the subsequent physical
addresses are contiguous.

In a microbenchmark on an F2FS loop device, a fresh 64 MiB file was
written and fsynced before timing fallocate with FALLOC_FL_ZERO_RANGE
flag over the whole file. Across 20 runs, mean syscall time
decreased from 16.436 ms to 10.011 ms (39%).

Signed-off-by: Seongjae Jeong <jsjlee1020@xxxxxxxxx>
---
fs/f2fs/file.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index b0bdc7a977b9..c227b7603731 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1713,6 +1713,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
unsigned int ofs_in_node = dn->ofs_in_node;
blkcnt_t count = 0;
int ret;
+ block_t blkstart = 0;
+ unsigned int blklen = 0;

for (; index < end; index++, dn->ofs_in_node++) {
if (f2fs_data_blkaddr(dn) == NULL_ADDR)
@@ -1739,16 +1741,30 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
if (dn->data_blkaddr == NEW_ADDR)
continue;

+ /*
+ * Flush before validation so a repeated block address
+ * can be detected through the SIT bitmap.
+ */
+ if (blklen && blkstart + blklen != dn->data_blkaddr) {
+ f2fs_invalidate_blocks(sbi, blkstart, blklen);
+ blklen = 0;
+ }
+
if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
DATA_GENERIC_ENHANCE)) {
ret = -EFSCORRUPTED;
break;
}

- f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
+ if (!blklen)
+ blkstart = dn->data_blkaddr;
+ blklen++;
f2fs_set_data_blkaddr(dn, NEW_ADDR);
}

+ if (blklen)
+ f2fs_invalidate_blocks(sbi, blkstart, blklen);
+
if (index > start) {
f2fs_update_read_extent_cache_range(dn, start, 0,
index - start);
--
2.53.0