f2fs: fix read extent cache merge and add debug interface
v2 cover f2fs: fix read extent cache merge and add debug interface
From: Yongpeng Yang <[email protected]>
This series fixes a read extent cache merge bug and adds a sysfs-based debug interface for inspecting the in-memory extent tree, which is otherwise invisible to userspace (fsck can only see the on-disk i_ext, never the in-memory rbtree).
Changes since v1 (RESEND [1]):
- Patch 1 (was 1/5) reuse the result of __is_back_mergeable().
- Dropped [PATCH RESEND 2/5] "only initialize largest extent without extent_node at inode init": no benefit was observed in practice.
- Patch 2 (was 3/5) unchanged.
- Dropped [PATCH RESEND 4/5] "add extent_access_mode to track extent cache access patterns": on phones the read extent node memory budget is small, and no hit-ratio improvement was observed in performance tests.
- Patch 3 (was 5/5) -- replace the ioctl with a write-only per-sb sysfs entry extent_cache_debug. Writing a file descriptor to it resolves the inode in the writer's context, dumps the largest extent and every rbtree node (fofs/blk/len) to the kernel log via f2fs_info(); inspect with "dmesg | grep ino=<ino>". A matching f2fs_io extent_cache subcommand is added in f2fs-tools.
[1] https://lore.kernel.org/all/[email protected]/
Patch 1: fix extent merge failure when largest extent is not in rb-tree.
When the largest extent exists only as a hint with no rbtree node (e.g. after shrink eviction), a newly inserted extent that is mergeable with it is not merged, leaving a gap that fiemap reports as separate extents. Merge the new extent with the largest before the normal lookup.
Patch 2: punch largest extent instead of dropping it entirely on
overlap. Keep the larger remaining left/right portion after a partial overlap instead of discarding the whole largest extent, preserving cache coverage for truncate/overwrite.
Patch 3: dump read extent cache via sysfs for debugging.
Yongpeng Yang (3):
f2fs: fix extent merge failure when largest extent is not in rb-tree
f2fs: punch largest extent instead of dropping it entirely on overlap
f2fs: dump read extent cache via sysfs for debugging
fs/f2fs/extent_cache.c | 100 ++++++++++++++++++++++++++++++++++++++--- fs/f2fs/f2fs.h | 1 + fs/f2fs/sysfs.c | 18 +++++++ 3 files changed, 112 insertions(+), 7 deletions(-)
--
2.43.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
PATCH v2 1/3 f2fs: fix extent merge failure when largest extent is not in rb-tree
From: Yongpeng Yang <[email protected]>
The following scenario can cause fiemap to report incorrect extents:
$ mkfs.f2fs /dev/vdb -f
$ mount -o mode=lfs /dev/vdb /mnt/f2fs/
$ dd if=/dev/urandom of=data bs=4K count=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
$ shrink all extent
$ dd if=/dev/urandom of=data bs=4K count=150 seek=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
Fiemap: offset = 0 len = 1000000
logical addr. physical addr. length flags
0 0000000000000000 00000002868d4000 000000000036a000 00001000
1 0000000000000000 0000000286c3e000 0000000000096000 00001001The root cause is that when the largest extent is not in the extent tree, mergeable extents are not merged, causing f2fs_map_blocks to misjudge and output an incorrect extent list.
Fix this by allowing the extent being inserted to merge with the largest extent. When updating the extent tree range, if the new extent can be front-merged or back-merged with the largest extent and the largest extent is not in the rb-tree, merge them before the normal lookup.
The merge logic only applies to the read extent cache, whose largest extent may be cached without a corresponding rb-tree node. Reuse the front/back mergeable check results instead of recomputing them.
---
v2:
- Reuse the result of __is_back_mergeable().
--- fs/f2fs/extent_cache.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
fs/f2fs/extent_cache.c +24 -0
@@ -711,6 +711,30 @@ static void __update_extent_tree_range(struct inode *inode,__drop_largest_extent (et, fofs, len); } +if (type ==EX_READ && et->largest.len !=0 ) { +bool front_merge =__is_front_mergeable (tei, &et->largest, type); +bool back_merge =__is_back_mergeable (tei, &et->largest, type); + +if (front_merge || back_merge) { +/* 0. try to merge with largest extent. */ + en =__lookup_extent_node_ret (&et->root, + et->cached_en, et->largest.fofs, + &prev_en, &next_en, + &insert_p, &insert_parent, + &leftmost); +if (!en) { +if (back_merge) { + tei->fofs = et->largest.fofs; + tei->blk = et->largest.blk; + fofs = tei->fofs; + } + tei->len += et->largest.len; + len = tei->len; + end = fofs + len; + } + } + } +/* 1. lookup first extent node in range [fofs, fofs + len - 1] */ en = __lookup_extent_node_ret(&et->root, et->cached_en, fofs,
Signature · 8 lines
2.43.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
PATCH v2 2/3 f2fs: punch largest extent instead of dropping it entirely on overlap
In reply to Yongpeng YangFrom: Yongpeng Yang <[email protected]>
Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.
This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.
--- fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +24 -7
@@ -406,14 +406,31 @@ static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,return count; } -static void __drop_largest_extent (struct extent_tree *et, +static void __punch_largest_extent (struct extent_tree *et,pgoff_t fofs,unsigned int len) { -if (fofs < (pgoff_t )et->largest.fofs + et->largest.len && - fofs + len > et->largest.fofs) { - et->largest.len =0 ; - et->largest_updated = true; +unsigned int largest_end, punch_end; +unsigned int left_len, right_len; + +if (fofs >= (pgoff_t )et->largest.fofs + et->largest.len || + fofs + len <= et->largest.fofs) +return ; + +/* Punch [fofs, fofs + len) from largest extent. */ + largest_end = et->largest.fofs + et->largest.len; + punch_end = fofs + len; + + left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs :0 ; + right_len = largest_end > punch_end ? largest_end - punch_end :0 ; + +if (left_len >= right_len) { + et->largest.len = left_len; + }else { + et->largest.blk += punch_end - et->largest.fofs; + et->largest.fofs = punch_end; + et->largest.len = right_len; } + et->largest_updated = true; }void f2fs_init_read_extent_tree (struct inode *inode,struct folio *ifolio)
@@ -705,10 +722,10 @@ static void __update_extent_tree_range(struct inode *inode, dei.len =0 ;/* -* drop largest extent before lookup, in case it's already +* punch largest extent before lookup, in case it's already * been shrunk from extent tree */ -__drop_largest_extent (et, fofs, len); +__punch_largest_extent (et, fofs, len); }if (type ==EX_READ && et->largest.len !=0 ) {
Signature · 8 lines
2.43.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
PATCH v2 3/3 f2fs: dump read extent cache via sysfs for debugging
In reply to Yongpeng YangFrom: Yongpeng Yang <[email protected]>
Add a write-only per-sb sysfs entry "extent_cache_debug": writing a file descriptor resolves the inode in the writer's context, dumps the largest extent and every rbtree node (fofs/blk/len) to the kernel log via f2fs_info(). A matching f2fs_io "extent_cache" subcommand is added in f2fs-tools.
--- fs/f2fs/extent_cache.c | 45 ++++++++++++++++++++++++++++++++++++++++++ fs/f2fs/f2fs.h | 1 + fs/f2fs/sysfs.c | 18 +++++++++++++++++ 3 files changed, 64 insertions(+)
fs/f2fs/extent_cache.c +45 -0
@@ -13,6 +13,7 @@*/ #include <linux/fs.h> +#include <linux/file.h> #include <linux/f2fs_fs.h> #include "f2fs.h"
@@ -1268,6 +1269,50 @@ static void __init_extent_tree_info(struct extent_tree_info *eti)atomic_set (&eti->total_ext_node,0 ); } +/* Dump the read extent cache of the inode behind @fd to the kernel log. */ +int f2fs_dump_read_extent_cache (struct f2fs_sb_info *sbi,int fd) +{ +struct fd f =fdget (fd); +struct inode *inode; +struct extent_tree *et; +struct rb_node *node; +struct extent_node *en; +unsigned int count =0 ; +int ret =0 ; + +if (fd_empty (f)) +return -EBADF ; + + inode =file_inode (fd_file (f)); +if (inode->i_sb != sbi->sb) { +fdput (f); +return -EINVAL ; + } + + et =F2FS_I (inode)->extent_tree[EX_READ ]; +if (!et) { +f2fs_info (sbi,"ino=%llu: read extent cache disabled" , + (unsigned long long )inode->i_ino); + goto out; + } + +read_lock (&et->lock); +f2fs_info (sbi,"ino=%llu largest: fofs=%u blk=%u len=%u node_count=%u" , + (unsigned long long )inode->i_ino, et->largest.fofs, + et->largest.blk, et->largest.len, +atomic_read (&et->node_cnt)); +for (node =rb_first_cached (&et->root); node; node =rb_next (node)) { + en = rb_entry(node, struct extent_node, rb_node); +f2fs_info (sbi," [%u] fofs=%u blk=%u len=%u" , + count, en->ei.fofs, en->ei.blk, en->ei.len); + count++; + } +read_unlock (&et->lock); +out: +fdput (f); +return ret; +} +void f2fs_init_extent_cache_info (struct f2fs_sb_info *sbi) {__init_extent_tree_info (&sbi->extent_tree[EX_READ ]);
fs/f2fs/f2fs.h +1 -0
@@ -4648,6 +4648,7 @@ void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn, pgoff_t fofs, block_t blkaddr, unsigned int len);unsigned int f2fs_shrink_read_extent_tree (struct f2fs_sb_info *sbi,int nr_shrink); +int f2fs_dump_read_extent_cache (struct f2fs_sb_info *sbi,int fd);/* block age extent cache ops */ void f2fs_init_age_extent_tree (struct inode *inode);
fs/f2fs/sysfs.c +18 -0
@@ -1368,6 +1368,23 @@ F2FS_GENERAL_RO_ATTR(avg_vblocks);F2FS_GENERAL_RO_ATTR (defrag_blocks);#endif +static ssize_t extent_cache_debug_store (struct f2fs_attr *a, +struct f2fs_sb_info *sbi,const char *buf,size_t count) +{ +int fd, ret; + + ret =kstrtoint (buf,0 , &fd); +if (ret <0 ) +return ret; + ret =f2fs_dump_read_extent_cache (sbi, fd); +if (ret <0 ) +return ret; +return count; +} + +static struct f2fs_attr f2fs_attr_extent_cache_debug =__ATTR ( + extent_cache_debug,0200 ,NULL , extent_cache_debug_store); +#ifdef CONFIG_FS_ENCRYPTION F2FS_FEATURE_RO_ATTR (encryption);F2FS_FEATURE_RO_ATTR (test_dummy_encryption_v2);
@@ -1491,6 +1508,7 @@ static struct attribute *f2fs_attrs[] = {ATTR_LIST (avg_vblocks),ATTR_LIST (defrag_blocks), #endif +ATTR_LIST (extent_cache_debug), #ifdef CONFIG_BLK_DEV_ZONEDATTR_LIST (unusable_blocks_per_sec), ATTR_LIST(max_open_zones),
Signature · 8 lines
2.43.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 54 lines · Yongpeng Yang
From: Yongpeng Yang <[email protected]>
This series fixes a read extent cache merge bug and adds a sysfs-based debug interface for inspecting the in-memory extent tree, which is otherwise invisible to userspace (fsck can only see the on-disk
i_ext, never the in-memory rbtree).Changes since v1 (RESEND [1]):
- Patch 1 (was 1/5) reuse the result of
__is_back_mergeable().- Dropped [PATCH RESEND 2/5] "only initialize largest extent without
extent_nodeat inode init": no benefit was observed in practice.- Patch 2 (was 3/5) unchanged.
- Dropped [PATCH RESEND 4/5] "add
extent_access_modeto track extent cache access patterns": on phones the read extent node memory budget is small, and no hit-ratio improvement was observed in performance tests.- Patch 3 (was 5/5) -- replace the ioctl with a write-only per-sb sysfs entry
extent_cache_debug. Writing a file descriptor to it resolves the inode in the writer's context, dumps the largest extent and every rbtree node (fofs/blk/len) to the kernel log viaf2fs_info(); inspect with "dmesg | grep ino=<ino>". A matchingf2fs_ioextent_cachesubcommand is added in f2fs-tools.[1] https://lore.kernel.org/all/[email protected]/
Patch 1: fix extent merge failure when largest extent is not in rb-tree.
When the largest extent exists only as a hint with no rbtree node (e.g. after shrink eviction), a newly inserted extent that is mergeable with it is not merged, leaving a gap that fiemap reports as separate extents. Merge the new extent with the largest before the normal lookup.Patch 2: punch largest extent instead of dropping it entirely on
overlap. Keep the larger remaining left/right portion after a partial overlap instead of discarding the whole largest extent, preserving cache coverage for truncate/overwrite.Patch 3: dump read extent cache via sysfs for debugging.
Yongpeng Yang (3):
f2fs: fix extent merge failure when largest extent is not in rb-tree
f2fs: punch largest extent instead of dropping it entirely on overlap
f2fs: dump read extent cache via sysfs for debuggingfs/f2fs/extent_cache.c | 100 ++++++++++++++++++++++++++++++++++++++--- fs/f2fs/f2fs.h | 1 + fs/f2fs/sysfs.c | 18 +++++++ 3 files changed, 112 insertions(+), 7 deletions(-)
Quoted 8 lines
--
2.43.0_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
ping
Thanks,
Yongpeng
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Yongpeng,
Sorry, too busy these days, so I have to add this serial and inline extent into my TODO review list. : P
Let me know if you have urgent fix that needs to upstream.
Quoted 64 lines · Yongpeng Yang
Quoted 54 lines · Yongpeng Yang
From: Yongpeng Yang <[email protected]>
This series fixes a read extent cache merge bug and adds a sysfs-based debug interface for inspecting the in-memory extent tree, which is otherwise invisible to userspace (fsck can only see the on-disk
i_ext, never the in-memory rbtree).Changes since v1 (RESEND [1]):
- Patch 1 (was 1/5) reuse the result of
__is_back_mergeable().- Dropped [PATCH RESEND 2/5] "only initialize largest extent without
extent_nodeat inode init": no benefit was observed in practice.- Patch 2 (was 3/5) unchanged.
- Dropped [PATCH RESEND 4/5] "add
extent_access_modeto track extent cache access patterns": on phones the read extent node memory budget is small, and no hit-ratio improvement was observed in performance tests.- Patch 3 (was 5/5) -- replace the ioctl with a write-only per-sb sysfs entry
extent_cache_debug. Writing a file descriptor to it resolves the inode in the writer's context, dumps the largest extent and every rbtree node (fofs/blk/len) to the kernel log viaf2fs_info(); inspect with "dmesg | grep ino=<ino>". A matchingf2fs_ioextent_cachesubcommand is added in f2fs-tools.[1] https://lore.kernel.org/all/[email protected]/
Patch 1: fix extent merge failure when largest extent is not in rb-tree.
When the largest extent exists only as a hint with no rbtree node (e.g. after shrink eviction), a newly inserted extent that is mergeable with it is not merged, leaving a gap that fiemap reports as separate extents. Merge the new extent with the largest before the normal lookup.Patch 2: punch largest extent instead of dropping it entirely on
overlap. Keep the larger remaining left/right portion after a partial overlap instead of discarding the whole largest extent, preserving cache coverage for truncate/overwrite.Patch 3: dump read extent cache via sysfs for debugging.
Yongpeng Yang (3):
f2fs: fix extent merge failure when largest extent is not in rb-tree
f2fs: punch largest extent instead of dropping it entirely on overlap
f2fs: dump read extent cache via sysfs for debuggingfs/f2fs/extent_cache.c | 100 ++++++++++++++++++++++++++++++++++++++--- fs/f2fs/f2fs.h | 1 + fs/f2fs/sysfs.c | 18 +++++++ 3 files changed, 112 insertions(+), 7 deletions(-)Quoted 8 lines
--
2.43.0_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 5 lines
ping
Thanks,
Yongpeng
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 6 lines · On 8/28/26 20:31, Chao Yu via Linux-f2fs-devel wrote:
Yongpeng,
Sorry, too busy these days, so I have to add this serial and inline extent into my TODO review list. : P
Let me know if you have urgent fix that needs to upstream.
I'm not in a hurry. :)
Thanks,
Yongpeng
Quoted 75 lines · Yongpeng Yang
Quoted 65 lines · Yongpeng Yang
Quoted 50 lines
From: Yongpeng Yang <[email protected]>
This series fixes a read extent cache merge bug and adds a sysfs-based debug interface for inspecting the in-memory extent tree, which is otherwise invisible to userspace (fsck can only see the on-disk
i_ext, never the in-memory rbtree).Changes since v1 (RESEND [1]):
- Patch 1 (was 1/5) reuse the result of
__is_back_mergeable().- Dropped [PATCH RESEND 2/5] "only initialize largest extent without
extent_nodeat inode init": no benefit was observed in practice.- Patch 2 (was 3/5) unchanged.
- Dropped [PATCH RESEND 4/5] "add
extent_access_modeto track extent cache access patterns": on phones the read extent node memory budget is small, and no hit-ratio improvement was observed in performance tests.- Patch 3 (was 5/5) -- replace the ioctl with a write-only per-sb sysfs entry
extent_cache_debug. Writing a file descriptor to it resolves the inode in the writer's context, dumps the largest extent and every rbtree node (fofs/blk/len) to the kernel log viaf2fs_info(); inspect with "dmesg | grep ino=<ino>". A matchingf2fs_ioextent_cachesubcommand is added in f2fs-tools.[1] https://lore.kernel.org/all/20260612115839.2065903-1-
[email protected]/Patch 1: fix extent merge failure when largest extent is not in rb-tree. When the largest extent exists only as a hint with no rbtree node (e.g. after shrink eviction), a newly inserted extent that is mergeable with it is not merged, leaving a gap that fiemap reports as separate extents. Merge the new extent with the largest before the normal lookup.
Patch 2: punch largest extent instead of dropping it entirely on overlap. Keep the larger remaining left/right portion after a partial overlap instead of discarding the whole largest extent, preserving cache coverage for truncate/overwrite.
Patch 3: dump read extent cache via sysfs for debugging.
Yongpeng Yang (3):
f2fs: fix extent merge failure when largest extent is not in rb-tree
f2fs: punch largest extent instead of dropping it entirely on overlap
f2fs: dump read extent cache via sysfs for debuggingQuoted 13 lines
fs/f2fs/extent_cache.c| 100 ++++++++++++++++++++++++++++++++++++++--- fs/f2fs/f2fs.h | 1 +
fs/f2fs/sysfs.c | 18 +++++++3 files changed, 112 insertions(+), 7 deletions(-)Signature · 7 lines
2.43.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-develQuoted 5 lines
ping
Thanks,
Yongpeng
Quoted 4 lines
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel