f2fs: introduce metadata cache
Subsystems (2)
Re: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
In reply to Daeho JeongQuoted 149 lines · Daeho Jeong
Quoted 27 lines · On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel…
This patch migrates compressed cluster caching from the fake VFS inode page cache (
sbi->compress_inode) to compress cache (sbi->compress_blocks).It converts compression caching and decompression paths to use compress cache APIs, uses
entry->inofor per-inode invalidation, and removessbi->compress_inode.Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/cache.c | 14 ++-- fs/f2fs/cache.h | 1 + fs/f2fs/compress.c | 158 ++++++++++++++++++---------------------- fs/f2fs/debug.c | 9 ++- fs/f2fs/f2fs.h | 8 +- fs/f2fs/inode.c | 49 +------------ fs/f2fs/node.c | 2 +- fs/f2fs/shrinker.c | 3 +- fs/f2fs/super.c | 15 ++-- include/linux/f2fs_fs.h | 1 - 10 files changed, 98 insertions(+), 162 deletions(-)fs/f2fs/cache.c +0
-0fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 6 lines
fs/f2fs/cache.c +1
-1@@ -417,13 +417,12 @@ static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,spin_unlock (&cache->list_lock); } -static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Quoted 110 lines
fs/f2fs/cache.c +7
-5{ -f2fs_lock_cache (entry); -if (entry->cache) -f2fs_do_truncate_cache (entry, drop_dirty); -f2fs_unlock_cache (entry); +if (!entry->cache) +return ; +f2fs_do_truncate_cache (entry, drop_dirty); } static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,@@ -435,8 +434,9 @@ static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,if (IS_ERR (entry))return ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); -f2fs_put_cache (entry, false); +f2fs_put_cache (entry, true); } void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,@@ -478,7 +478,9 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache, index = entry->index +1 ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); +f2fs_unlock_cache (entry); }f2fs_cache_gang_release (entries, nr);fs/f2fs/cache.h +1
-0@@ -168,6 +168,7 @@ unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache, unsigned int max_items, int tag);void f2fs_cache_gang_release (struct f2fs_cached_block **entries,unsigned int nr_entries); +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty);int f2fs_writeback_cache (struct f2fs_cached_block_list *cache,bool sync);void f2fs_cache_wait_on_all_writeback (struct f2fs_cached_block_list *cache); void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,fs/f2fs/compress.c +10
-27@@ -1918,30 +1918,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,return compressed ? i -1 : i; } -const struct address_space_operations f2fs_compress_aops = { - .release_folio = f2fs_release_folio, - .invalidate_folio = f2fs_invalidate_folio, - .migrate_folio = filemap_migrate_folio, -}; - -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi) -{ -return sbi->compress_inode->i_mapping; -} -void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len) { -if (!sbi->compress_inode) +if (!test_opt (sbi,COMPRESS_CACHE ))return ; -invalidate_mapping_pages (COMPRESS_MAPPING (sbi), blkaddr, blkaddr + len -1 ); + +f2fs_drop_cache_range (COMPRESS_CACHE (sbi), blkaddr, len, false); }static void f2fs_cache_compressed_page (struct f2fs_sb_info *sbi,struct folio *folio,nid_t ino,block_t blkaddr) { -struct folio *cfolio; -int ret; +struct f2fs_cached_block *entry;if (!test_opt (sbi,COMPRESS_CACHE ))return ;@@ -1952,49 +1941,43 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,if (!f2fs_available_free_memory (sbi,COMPRESS_PAGE ))return ; - cfolio =filemap_get_folio (COMPRESS_MAPPING (sbi), blkaddr); -if (!IS_ERR (cfolio)) { -f2fs_folio_put (cfolio, false); + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_put_cache (entry, false);return ; } - cfolio =filemap_alloc_folio (__GFP_NOWARN | __GFP_IO,0 ,NULL ); -if (!cfolio) -return ; - - ret =filemap_add_folio (COMPRESS_MAPPING (sbi), cfolio, - blkaddr,GFP_NOFS ); -if (ret) { -f2fs_folio_put (cfolio, false); + entry =f2fs_grab_cache (COMPRESS_CACHE (sbi), blkaddr, +F2FS_CACHE_LOCK_CREATE ); +if (IS_ERR (entry))return ; - }
Quoted 7 lines
fs/f2fs/compress.c static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, · line 1956
Quoted 7 lines
fs/f2fs/compress.c +2
-5-folio_set_f2fs_data (cfolio, ino); - -memcpy (folio_address (cfolio),folio_address (folio),PAGE_SIZE ); -folio_mark_uptodate (cfolio); -f2fs_folio_put (cfolio, true); + entry->ino = ino; +memcpy (cache_address (entry),folio_address (folio),PAGE_SIZE );
Use sbi->blocksize or F2FS_BLKSIZE instead?
Thanks,
Quoted 455 lines
Quoted 35 lines
fs/f2fs/compress.c +10
-7+f2fs_cache_set_uptodate (entry); +f2fs_put_cache (entry, true); }bool f2fs_load_compressed_folio (struct f2fs_sb_info *sbi,struct folio *folio,block_t blkaddr) { -struct folio *cfolio; +struct f2fs_cached_block *entry;bool hitted = false;if (!test_opt (sbi,COMPRESS_CACHE ))return false; - cfolio =f2fs_filemap_get_folio (COMPRESS_MAPPING (sbi), - blkaddr,FGP_LOCK |FGP_NOWAIT ,GFP_NOFS ); -if (!IS_ERR (cfolio)) { -if (folio_test_uptodate (cfolio)) { + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_lock_cache (entry); +if (f2fs_is_compress_cache (entry) && +f2fs_cache_test_uptodate (entry)) {atomic_inc (&sbi->compress_page_hit);memcpy (folio_address (folio), -folio_address (cfolio),folio_size (folio)); +cache_address (entry),folio_size (folio)); hitted = true; } -f2fs_folio_put (cfolio, true); +f2fs_put_cache (entry, true); }return hitted;fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 1986
Quoted 2 lines
fs/f2fs/compress.c +0
-0@@ -2002,71 +1985,70 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,void f2fs_invalidate_compress_pages (struct f2fs_sb_info *sbi,nid_t ino)This is O(N) Full-tree scan on inode eviction.
WDYT to improve it?
- Per-inode tracking
- Re-evaluate eviction policy: Since compress cache blocks are
indexed by physical block address (blkaddr) and hold immutable disk data, the cached blocks remain valid even after the inode is closed/evicted.
We should evaluate whether we actually need to synchronously purge
clean compress cache entries upon inode eviction, or if we can simply let the memory shrinker reclaim them via LRU under memory pressure.fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2010
Quoted 13 lines
fs/f2fs/compress.c +6
-5{ -struct address_space *mapping =COMPRESS_MAPPING (sbi); -struct folio_batch fbatch; -pgoff_t index =0 ; -pgoff_t end =MAX_BLKADDR (sbi); +struct f2fs_cached_block_list *cache =COMPRESS_CACHE (sbi); +struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ]; +unsigned long flags; +pgoff_t index =0 , end =ULONG_MAX ; +int nr; +int i; -if (!mapping->nrpages)Why did you remove this line?
Quoted 42 lines
fs/f2fs/compress.c +23
-15+if (!test_opt (sbi,COMPRESS_CACHE ))return ; - -folio_batch_init (&fbatch); - - do { - unsigned int nr, i; - - nr = filemap_get_folios(mapping, &index, end - 1, &fbatch); - if (!nr) +next: +spin_lock_irqsave (&cache->tree_lock, flags); + nr =radix_tree_gang_lookup (&cache->root, (void **)entries, index, +min ((unsigned long )F2FS_ONSTACK_CACHES , end - index)); +if (!nr) + goto out_unlock; + +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; + +if (entry->index >= end) { + nr = i;break ; + } +f2fs_cache_get (entry); + } +out_unlock: +spin_unlock_irqrestore (&cache->tree_lock, flags); +if (!nr) +return ; +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; - for (i = 0; i < nr; i++) { - struct folio *folio = fbatch.folios[i]; + index = entry->index +1 ; - folio_lock(folio); - if (folio->mapping != mapping) { - folio_unlock(folio); - continue; - }fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 10 lines
fs/f2fs/compress.c +2
-7+f2fs_lock_cache (entry); -if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {How is this possible?
or if (!entry->cache)?Thanks,
Quoted 331 lines
fs/f2fs/compress.c +17
-22+f2fs_unlock_cache (entry); +continue ; } -folio_batch_release (&fbatch); +if (entry->ino != ino) { +f2fs_unlock_cache (entry); +continue ; + } + +f2fs_truncate_cache (entry, false); +f2fs_unlock_cache (entry); + } +f2fs_cache_gang_release (entries, nr); + +if (index < end) {cond_resched (); - }while (index < end); + goto next; + } } -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { -struct inode *inode; -if (!test_opt (sbi,COMPRESS_CACHE )) -return 0 ; - - inode =f2fs_iget (sbi->sb,F2FS_COMPRESS_INO (sbi)); -if (IS_ERR (inode)) -return PTR_ERR (inode); - sbi->compress_inode = inode; +return ; sbi->compress_percent =COMPRESS_PERCENT ; sbi->compress_watermark =COMPRESS_WATERMARK ; -atomic_set (&sbi->compress_page_hit,0 ); - -return 0 ; -} - -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) -{ -if (!sbi->compress_inode) -return ; -iput (sbi->compress_inode); - sbi->compress_inode =NULL ; }int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi)fs/f2fs/debug.c +5
-4@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi) si->node_caches =NODE_CACHE (sbi)->num_entries; si->meta_caches =META_CACHE (sbi)->num_entries;#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { - si->compress_pages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { + si->compress_pages =COMPRESS_CACHE (sbi)->num_entries; si->compress_page_hit =atomic_read (&sbi->compress_page_hit); }#endif @@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->page_mem += (unsigned long long )NODE_CACHE (sbi)->num_entries <<PAGE_SHIFT ; si->cache_mem +=NODE_CACHE (sbi)->num_entries *sizeof (struct f2fs_cached_block );#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { -unsigned long npages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { +unsigned long npages =COMPRESS_CACHE (sbi)->num_entries; si->page_mem += (unsigned long long )npages <<PAGE_SHIFT ; + si->cache_mem += npages *sizeof (struct f2fs_cached_block ); }#endif }fs/f2fs/f2fs.h +2
-6@@ -2078,7 +2078,6 @@ struct f2fs_sb_info {u32 compr_new_inode;/* For compressed block cache */ -struct inode *compress_inode;/* cache compressed blocks */ unsigned int compress_percent;/* cache page percentage */ unsigned int compress_watermark;/* cache page watermark */ atomic_t compress_page_hit;/* cache hit count */ @@ -4821,13 +4820,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,int f2fs_init_compress_ctx (struct compress_ctx *cc);void f2fs_destroy_compress_ctx (struct compress_ctx *cc,bool reuse);void f2fs_init_compress_info (struct f2fs_sb_info *sbi); -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi); -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi); +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi);int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi);void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi);int __init f2fs_init_compress_cache (void );void f2fs_destroy_compress_cache (void ); -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi);void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len); bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,@@ -4876,8 +4873,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)static inline unsigned int f2fs_cluster_blocks_are_contiguous (struct dnode_of_data *dn,unsigned int ofs_in_node) {return 0 ; }static inline bool f2fs_sanity_check_cluster (struct dnode_of_data *dn) {return false; } -static inline int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) {return 0 ; } -static inline void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) { } +static inline void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { }static inline int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi) {return 0 ; }static inline void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi) { }static inline int __init f2fs_init_compress_cache (void ) {return 0 ; }fs/f2fs/inode.c +4
-45@@ -573,15 +573,6 @@ static int do_read_inode(struct inode *inode)return 0 ; } -static bool is_meta_ino (struct f2fs_sb_info *sbi,unsigned int ino) -{ -#ifdef CONFIG_F2FS_FS_COMPRESSION -if (test_opt (sbi,COMPRESS_CACHE ) && ino ==F2FS_COMPRESS_INO (sbi)) -return true; -#endif -return false; -} -struct inode *f2fs_iget (struct super_block *sb,unsigned long ino) {struct f2fs_sb_info *sbi =F2FS_SB (sb);@@ -593,42 +584,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return ERR_PTR (-ENOMEM );if (!(inode_state_read_once (inode) &I_NEW )) { -if (is_meta_ino (sbi, ino)) { -f2fs_err (sbi,"inaccessible inode: %lu, run fsck to repair" , ino); -set_sbi_flag (sbi,SBI_NEED_FSCK ); - ret = -EFSCORRUPTED ; -trace_f2fs_iget_exit (inode, ret); -iput (inode); -f2fs_handle_error (sbi,ERROR_CORRUPTED_INODE ); -fserror_report_file_metadata (inode, ret,GFP_NOFS ); -return ERR_PTR (ret); - } -trace_f2fs_iget (inode);return inode; } -if (is_meta_ino (sbi, ino)) - goto make_now; - ret =do_read_inode (inode);if (ret) goto bad_inode; -make_now: +f2fs_set_inode_flags (inode); -if (ino ==F2FS_COMPRESS_INO (sbi)) { -#ifdef CONFIG_F2FS_FS_COMPRESSION - inode->i_mapping->a_ops = &f2fs_compress_aops; -/* -* generic_error_remove_folio only truncates pages of regular -* inode -*/ - inode->i_mode |=S_IFREG ; -#endif -mapping_set_gfp_mask (inode->i_mapping, -GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); - }else if (S_ISREG (inode->i_mode)) { +if (S_ISREG (inode->i_mode)) { inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops;@@ -879,7 +845,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)/* * Return true, if we shouldn't go through post_evict_inode. */ -static bool f2fs_pre_evict_inode (struct inode *inode) +static void f2fs_pre_evict_inode (struct inode *inode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode);@@ -905,17 +871,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -if (inode->i_ino ==F2FS_COMPRESS_INO (sbi)) -return true; -f2fs_bug_on (sbi,get_dirty_pages (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);if (!IS_DEVICE_ALIASING (inode))f2fs_destroy_extent_tree (inode); - -return false; }static void f2fs_delete_inode (struct inode *inode)@@ -1077,15 +1038,13 @@ static void f2fs_post_evict_inode(struct inode *inode)*/ void f2fs_evict_inode (struct inode *inode) { -if (f2fs_pre_evict_inode (inode)) - goto clear_out; +f2fs_pre_evict_inode (inode);if (!inode->i_nlink && !is_bad_inode (inode))f2fs_delete_inode (inode);f2fs_post_evict_inode (inode); -clear_out:fscrypt_put_encryption_info (inode);clear_inode (inode); }fs/f2fs/node.c +1
-1@@ -119,7 +119,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)* exceed threshold, deny caching compress page. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_MAPPING (sbi)->nrpages < + (COMPRESS_CACHE (sbi)->num_entries < free_ram * sbi->compress_percent /100 );#else res = false;fs/f2fs/shrinker.c +2
-1@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,static unsigned long __count_cache (struct f2fs_sb_info *sbi) {return sbi->meta_blocks.num_entries + - sbi->node_blocks.num_entries; + sbi->node_blocks.num_entries + + sbi->compress_blocks.num_entries; } unsigned long f2fs_shrink_count(struct shrinker *shrink,fs/f2fs/super.c +5
-10@@ -2050,8 +2050,6 @@ static void f2fs_put_super(struct super_block *sb)f2fs_bug_on (sbi, sbi->fsync_node_num); -f2fs_destroy_compress_inode (sbi); -f2fs_destroy_cache (COMPRESS_CACHE (sbi));f2fs_destroy_cache (NODE_CACHE (sbi));f2fs_destroy_cache (META_CACHE (sbi));@@ -5285,6 +5283,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_init_fsync_node_info (sbi); +f2fs_init_compress_cache_context (sbi); +/* setup checkpoint request control and start checkpoint issue thread */ f2fs_init_ckpt_req_control (sbi); if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&@@ -5363,13 +5363,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) goto free_ino_entry; } - err =f2fs_init_compress_inode (sbi); -if (err) - goto free_root_inode; - err =f2fs_register_sysfs (sbi);if (err) - goto free_compress_inode; + goto free_root_inode; sbi->umount_lock_holder = current;#ifdef CONFIG_QUOTA @@ -5537,8 +5533,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* evict some inodes being cached by GC */ evict_inodes (sb);f2fs_unregister_sysfs (sbi); -free_compress_inode: -f2fs_destroy_compress_inode (sbi); free_root_inode:dput (sb->s_root); sb->s_root =NULL ;@@ -5655,7 +5649,8 @@ static void kill_f2fs_super(struct super_block *sb)* compress inode cache. */ if (test_opt (sbi,COMPRESS_CACHE )) -truncate_inode_pages_final (COMPRESS_MAPPING (sbi)); +f2fs_invalidate_compress_pages_range (sbi, +0 ,UINT_MAX );#endif if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||include/linux/f2fs_fs.h +0
-2@@ -34,7 +34,6 @@#define F2FS_RESERVED_NODE_NUM 3#define F2FS_ROOT_INO (sbi) ((sbi)->root_ino_num) -#define F2FS_COMPRESS_INO (sbi) (NM_I(sbi)->max_nid)#define F2FS_MAX_QUOTAS 3 --2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 150 lines · Daeho Jeong
Quoted 149 lines · Daeho Jeong
Quoted 27 lines · On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel…
This patch migrates compressed cluster caching from the fake VFS inode page cache (
sbi->compress_inode) to compress cache (sbi->compress_blocks).It converts compression caching and decompression paths to use compress cache APIs, uses
entry->inofor per-inode invalidation, and removessbi->compress_inode.Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/cache.c | 14 ++-- fs/f2fs/cache.h | 1 + fs/f2fs/compress.c | 158 ++++++++++++++++++---------------------- fs/f2fs/debug.c | 9 ++- fs/f2fs/f2fs.h | 8 +- fs/f2fs/inode.c | 49 +------------ fs/f2fs/node.c | 2 +- fs/f2fs/shrinker.c | 3 +- fs/f2fs/super.c | 15 ++-- include/linux/f2fs_fs.h | 1 - 10 files changed, 98 insertions(+), 162 deletions(-)fs/f2fs/cache.c +0
-0fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 6 lines
fs/f2fs/cache.c +1
-1@@ -417,13 +417,12 @@ static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,spin_unlock (&cache->list_lock); } -static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Quoted 110 lines
fs/f2fs/cache.c +7
-5{ -f2fs_lock_cache (entry); -if (entry->cache) -f2fs_do_truncate_cache (entry, drop_dirty); -f2fs_unlock_cache (entry); +if (!entry->cache) +return ; +f2fs_do_truncate_cache (entry, drop_dirty); } static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,@@ -435,8 +434,9 @@ static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,if (IS_ERR (entry))return ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); -f2fs_put_cache (entry, false); +f2fs_put_cache (entry, true); } void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,@@ -478,7 +478,9 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache, index = entry->index +1 ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); +f2fs_unlock_cache (entry); }f2fs_cache_gang_release (entries, nr);fs/f2fs/cache.h +1
-0@@ -168,6 +168,7 @@ unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache, unsigned int max_items, int tag);void f2fs_cache_gang_release (struct f2fs_cached_block **entries,unsigned int nr_entries); +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty);int f2fs_writeback_cache (struct f2fs_cached_block_list *cache,bool sync);void f2fs_cache_wait_on_all_writeback (struct f2fs_cached_block_list *cache); void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,fs/f2fs/compress.c +10
-27@@ -1918,30 +1918,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,return compressed ? i -1 : i; } -const struct address_space_operations f2fs_compress_aops = { - .release_folio = f2fs_release_folio, - .invalidate_folio = f2fs_invalidate_folio, - .migrate_folio = filemap_migrate_folio, -}; - -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi) -{ -return sbi->compress_inode->i_mapping; -} -void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len) { -if (!sbi->compress_inode) +if (!test_opt (sbi,COMPRESS_CACHE ))return ; -invalidate_mapping_pages (COMPRESS_MAPPING (sbi), blkaddr, blkaddr + len -1 ); + +f2fs_drop_cache_range (COMPRESS_CACHE (sbi), blkaddr, len, false); }static void f2fs_cache_compressed_page (struct f2fs_sb_info *sbi,struct folio *folio,nid_t ino,block_t blkaddr) { -struct folio *cfolio; -int ret; +struct f2fs_cached_block *entry;if (!test_opt (sbi,COMPRESS_CACHE ))return ;@@ -1952,49 +1941,43 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,if (!f2fs_available_free_memory (sbi,COMPRESS_PAGE ))return ; - cfolio =filemap_get_folio (COMPRESS_MAPPING (sbi), blkaddr); -if (!IS_ERR (cfolio)) { -f2fs_folio_put (cfolio, false); + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_put_cache (entry, false);return ; } - cfolio =filemap_alloc_folio (__GFP_NOWARN | __GFP_IO,0 ,NULL ); -if (!cfolio) -return ; - - ret =filemap_add_folio (COMPRESS_MAPPING (sbi), cfolio, - blkaddr,GFP_NOFS ); -if (ret) { -f2fs_folio_put (cfolio, false); + entry =f2fs_grab_cache (COMPRESS_CACHE (sbi), blkaddr, +F2FS_CACHE_LOCK_CREATE ); +if (IS_ERR (entry))return ; - }
Quoted 9 lines
Quoted 7 lines
fs/f2fs/compress.c static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, · line 1956
Quoted 7 lines
fs/f2fs/compress.c +2
-5-folio_set_f2fs_data (cfolio, ino); - -memcpy (folio_address (cfolio),folio_address (folio),PAGE_SIZE ); -folio_mark_uptodate (cfolio); -f2fs_folio_put (cfolio, true); + entry->ino = ino; +memcpy (cache_address (entry),folio_address (folio),PAGE_SIZE );Use
sbi->blocksizeorF2FS_BLKSIZEinstead?
Sure.
Thanks,
Quoted 458 lines
Thanks,
Quoted 455 lines
Quoted 35 lines
fs/f2fs/compress.c +10
-7+f2fs_cache_set_uptodate (entry); +f2fs_put_cache (entry, true); }bool f2fs_load_compressed_folio (struct f2fs_sb_info *sbi,struct folio *folio,block_t blkaddr) { -struct folio *cfolio; +struct f2fs_cached_block *entry;bool hitted = false;if (!test_opt (sbi,COMPRESS_CACHE ))return false; - cfolio =f2fs_filemap_get_folio (COMPRESS_MAPPING (sbi), - blkaddr,FGP_LOCK |FGP_NOWAIT ,GFP_NOFS ); -if (!IS_ERR (cfolio)) { -if (folio_test_uptodate (cfolio)) { + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_lock_cache (entry); +if (f2fs_is_compress_cache (entry) && +f2fs_cache_test_uptodate (entry)) {atomic_inc (&sbi->compress_page_hit);memcpy (folio_address (folio), -folio_address (cfolio),folio_size (folio)); +cache_address (entry),folio_size (folio)); hitted = true; } -f2fs_folio_put (cfolio, true); +f2fs_put_cache (entry, true); }return hitted;fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 1986
Quoted 2 lines
fs/f2fs/compress.c +0
-0@@ -2002,71 +1985,70 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,void f2fs_invalidate_compress_pages (struct f2fs_sb_info *sbi,nid_t ino)This is O(N) Full-tree scan on inode eviction.
WDYT to improve it?
- Per-inode tracking
- Re-evaluate eviction policy: Since compress cache blocks are
indexed by physical block address (blkaddr) and hold immutable disk data, the cached blocks remain valid even after the inode is closed/evicted.
We should evaluate whether we actually need to synchronously purge
clean compress cache entries upon inode eviction, or if we can simply let the memory shrinker reclaim them via LRU under memory pressure.fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2010
Quoted 13 lines
fs/f2fs/compress.c +6
-5{ -struct address_space *mapping =COMPRESS_MAPPING (sbi); -struct folio_batch fbatch; -pgoff_t index =0 ; -pgoff_t end =MAX_BLKADDR (sbi); +struct f2fs_cached_block_list *cache =COMPRESS_CACHE (sbi); +struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ]; +unsigned long flags; +pgoff_t index =0 , end =ULONG_MAX ; +int nr; +int i; -if (!mapping->nrpages)Why did you remove this line?
Quoted 42 lines
fs/f2fs/compress.c +23
-15+if (!test_opt (sbi,COMPRESS_CACHE ))return ; - -folio_batch_init (&fbatch); - - do { - unsigned int nr, i; - - nr = filemap_get_folios(mapping, &index, end - 1, &fbatch); - if (!nr) +next: +spin_lock_irqsave (&cache->tree_lock, flags); + nr =radix_tree_gang_lookup (&cache->root, (void **)entries, index, +min ((unsigned long )F2FS_ONSTACK_CACHES , end - index)); +if (!nr) + goto out_unlock; + +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; + +if (entry->index >= end) { + nr = i;break ; + } +f2fs_cache_get (entry); + } +out_unlock: +spin_unlock_irqrestore (&cache->tree_lock, flags); +if (!nr) +return ; +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; - for (i = 0; i < nr; i++) { - struct folio *folio = fbatch.folios[i]; + index = entry->index +1 ; - folio_lock(folio); - if (folio->mapping != mapping) { - folio_unlock(folio); - continue; - }fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 10 lines
fs/f2fs/compress.c +2
-7+f2fs_lock_cache (entry); -if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {How is this possible?
or if (!entry->cache)?Thanks,
Quoted 331 lines
fs/f2fs/compress.c +17
-22+f2fs_unlock_cache (entry); +continue ; } -folio_batch_release (&fbatch); +if (entry->ino != ino) { +f2fs_unlock_cache (entry); +continue ; + } + +f2fs_truncate_cache (entry, false); +f2fs_unlock_cache (entry); + } +f2fs_cache_gang_release (entries, nr); + +if (index < end) {cond_resched (); - }while (index < end); + goto next; + } } -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { -struct inode *inode; -if (!test_opt (sbi,COMPRESS_CACHE )) -return 0 ; - - inode =f2fs_iget (sbi->sb,F2FS_COMPRESS_INO (sbi)); -if (IS_ERR (inode)) -return PTR_ERR (inode); - sbi->compress_inode = inode; +return ; sbi->compress_percent =COMPRESS_PERCENT ; sbi->compress_watermark =COMPRESS_WATERMARK ; -atomic_set (&sbi->compress_page_hit,0 ); - -return 0 ; -} - -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) -{ -if (!sbi->compress_inode) -return ; -iput (sbi->compress_inode); - sbi->compress_inode =NULL ; }int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi)fs/f2fs/debug.c +5
-4@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi) si->node_caches =NODE_CACHE (sbi)->num_entries; si->meta_caches =META_CACHE (sbi)->num_entries;#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { - si->compress_pages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { + si->compress_pages =COMPRESS_CACHE (sbi)->num_entries; si->compress_page_hit =atomic_read (&sbi->compress_page_hit); }#endif @@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->page_mem += (unsigned long long )NODE_CACHE (sbi)->num_entries <<PAGE_SHIFT ; si->cache_mem +=NODE_CACHE (sbi)->num_entries *sizeof (struct f2fs_cached_block );#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { -unsigned long npages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { +unsigned long npages =COMPRESS_CACHE (sbi)->num_entries; si->page_mem += (unsigned long long )npages <<PAGE_SHIFT ; + si->cache_mem += npages *sizeof (struct f2fs_cached_block ); }#endif }fs/f2fs/f2fs.h +2
-6@@ -2078,7 +2078,6 @@ struct f2fs_sb_info {u32 compr_new_inode;/* For compressed block cache */ -struct inode *compress_inode;/* cache compressed blocks */ unsigned int compress_percent;/* cache page percentage */ unsigned int compress_watermark;/* cache page watermark */ atomic_t compress_page_hit;/* cache hit count */ @@ -4821,13 +4820,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,int f2fs_init_compress_ctx (struct compress_ctx *cc);void f2fs_destroy_compress_ctx (struct compress_ctx *cc,bool reuse);void f2fs_init_compress_info (struct f2fs_sb_info *sbi); -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi); -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi); +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi);int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi);void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi);int __init f2fs_init_compress_cache (void );void f2fs_destroy_compress_cache (void ); -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi);void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len); bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,@@ -4876,8 +4873,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)static inline unsigned int f2fs_cluster_blocks_are_contiguous (struct dnode_of_data *dn,unsigned int ofs_in_node) {return 0 ; }static inline bool f2fs_sanity_check_cluster (struct dnode_of_data *dn) {return false; } -static inline int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) {return 0 ; } -static inline void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) { } +static inline void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { }static inline int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi) {return 0 ; }static inline void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi) { }static inline int __init f2fs_init_compress_cache (void ) {return 0 ; }fs/f2fs/inode.c +4
-45@@ -573,15 +573,6 @@ static int do_read_inode(struct inode *inode)return 0 ; } -static bool is_meta_ino (struct f2fs_sb_info *sbi,unsigned int ino) -{ -#ifdef CONFIG_F2FS_FS_COMPRESSION -if (test_opt (sbi,COMPRESS_CACHE ) && ino ==F2FS_COMPRESS_INO (sbi)) -return true; -#endif -return false; -} -struct inode *f2fs_iget (struct super_block *sb,unsigned long ino) {struct f2fs_sb_info *sbi =F2FS_SB (sb);@@ -593,42 +584,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return ERR_PTR (-ENOMEM );if (!(inode_state_read_once (inode) &I_NEW )) { -if (is_meta_ino (sbi, ino)) { -f2fs_err (sbi,"inaccessible inode: %lu, run fsck to repair" , ino); -set_sbi_flag (sbi,SBI_NEED_FSCK ); - ret = -EFSCORRUPTED ; -trace_f2fs_iget_exit (inode, ret); -iput (inode); -f2fs_handle_error (sbi,ERROR_CORRUPTED_INODE ); -fserror_report_file_metadata (inode, ret,GFP_NOFS ); -return ERR_PTR (ret); - } -trace_f2fs_iget (inode);return inode; } -if (is_meta_ino (sbi, ino)) - goto make_now; - ret =do_read_inode (inode);if (ret) goto bad_inode; -make_now: +f2fs_set_inode_flags (inode); -if (ino ==F2FS_COMPRESS_INO (sbi)) { -#ifdef CONFIG_F2FS_FS_COMPRESSION - inode->i_mapping->a_ops = &f2fs_compress_aops; -/* -* generic_error_remove_folio only truncates pages of regular -* inode -*/ - inode->i_mode |=S_IFREG ; -#endif -mapping_set_gfp_mask (inode->i_mapping, -GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); - }else if (S_ISREG (inode->i_mode)) { +if (S_ISREG (inode->i_mode)) { inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops;@@ -879,7 +845,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)/* * Return true, if we shouldn't go through post_evict_inode. */ -static bool f2fs_pre_evict_inode (struct inode *inode) +static void f2fs_pre_evict_inode (struct inode *inode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode);@@ -905,17 +871,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -if (inode->i_ino ==F2FS_COMPRESS_INO (sbi)) -return true; -f2fs_bug_on (sbi,get_dirty_pages (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);if (!IS_DEVICE_ALIASING (inode))f2fs_destroy_extent_tree (inode); - -return false; }static void f2fs_delete_inode (struct inode *inode)@@ -1077,15 +1038,13 @@ static void f2fs_post_evict_inode(struct inode *inode)*/ void f2fs_evict_inode (struct inode *inode) { -if (f2fs_pre_evict_inode (inode)) - goto clear_out; +f2fs_pre_evict_inode (inode);if (!inode->i_nlink && !is_bad_inode (inode))f2fs_delete_inode (inode);f2fs_post_evict_inode (inode); -clear_out:fscrypt_put_encryption_info (inode);clear_inode (inode); }fs/f2fs/node.c +1
-1@@ -119,7 +119,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)* exceed threshold, deny caching compress page. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_MAPPING (sbi)->nrpages < + (COMPRESS_CACHE (sbi)->num_entries < free_ram * sbi->compress_percent /100 );#else res = false;fs/f2fs/shrinker.c +2
-1@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,static unsigned long __count_cache (struct f2fs_sb_info *sbi) {return sbi->meta_blocks.num_entries + - sbi->node_blocks.num_entries; + sbi->node_blocks.num_entries + + sbi->compress_blocks.num_entries; } unsigned long f2fs_shrink_count(struct shrinker *shrink,fs/f2fs/super.c +5
-10@@ -2050,8 +2050,6 @@ static void f2fs_put_super(struct super_block *sb)f2fs_bug_on (sbi, sbi->fsync_node_num); -f2fs_destroy_compress_inode (sbi); -f2fs_destroy_cache (COMPRESS_CACHE (sbi));f2fs_destroy_cache (NODE_CACHE (sbi));f2fs_destroy_cache (META_CACHE (sbi));@@ -5285,6 +5283,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_init_fsync_node_info (sbi); +f2fs_init_compress_cache_context (sbi); +/* setup checkpoint request control and start checkpoint issue thread */ f2fs_init_ckpt_req_control (sbi); if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&@@ -5363,13 +5363,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) goto free_ino_entry; } - err =f2fs_init_compress_inode (sbi); -if (err) - goto free_root_inode; - err =f2fs_register_sysfs (sbi);if (err) - goto free_compress_inode; + goto free_root_inode; sbi->umount_lock_holder = current;#ifdef CONFIG_QUOTA @@ -5537,8 +5533,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* evict some inodes being cached by GC */ evict_inodes (sb);f2fs_unregister_sysfs (sbi); -free_compress_inode: -f2fs_destroy_compress_inode (sbi); free_root_inode:dput (sb->s_root); sb->s_root =NULL ;@@ -5655,7 +5649,8 @@ static void kill_f2fs_super(struct super_block *sb)* compress inode cache. */ if (test_opt (sbi,COMPRESS_CACHE )) -truncate_inode_pages_final (COMPRESS_MAPPING (sbi)); +f2fs_invalidate_compress_pages_range (sbi, +0 ,UINT_MAX );#endif if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||include/linux/f2fs_fs.h +0
-2@@ -34,7 +34,6 @@#define F2FS_RESERVED_NODE_NUM 3#define F2FS_ROOT_INO (sbi) ((sbi)->root_ino_num) -#define F2FS_COMPRESS_INO (sbi) (NM_I(sbi)->max_nid)#define F2FS_MAX_QUOTAS 3 --2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
In reply to Daeho JeongQuoted 32 lines · Daeho Jeong
Quoted 30 lines · On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel…
This patch migrates compressed cluster caching from the fake VFS inode page cache (
sbi->compress_inode) to compress cache (sbi->compress_blocks).It converts compression caching and decompression paths to use compress cache APIs, uses
entry->inofor per-inode invalidation, and removessbi->compress_inode.Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/cache.c | 14 ++-- fs/f2fs/cache.h | 1 + fs/f2fs/compress.c | 158 ++++++++++++++++++---------------------- fs/f2fs/debug.c | 9 ++- fs/f2fs/f2fs.h | 8 +- fs/f2fs/inode.c | 49 +------------ fs/f2fs/node.c | 2 +- fs/f2fs/shrinker.c | 3 +- fs/f2fs/super.c | 15 ++-- include/linux/f2fs_fs.h | 1 - 10 files changed, 98 insertions(+), 162 deletions(-)fs/f2fs/cache.c +0
-0@@ -417,13 +417,12 @@ static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,spin_unlock (&cache->list_lock); }
Quoted 5 lines
fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 3 lines
fs/f2fs/cache.c +1
-1-static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?
Actually, I'v relocated the lock into callers of f2fs_truncate_cache(), in order to support check entry->ino, entry->cache w/ the lock in f2fs_invalidate_compress_pages(), please take a look.
Quoted 158 lines
Quoted 153 lines
fs/f2fs/cache.c +7
-5{ -f2fs_lock_cache (entry); -if (entry->cache) -f2fs_do_truncate_cache (entry, drop_dirty); -f2fs_unlock_cache (entry); +if (!entry->cache) +return ; +f2fs_do_truncate_cache (entry, drop_dirty); } static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,@@ -435,8 +434,9 @@ static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,if (IS_ERR (entry))return ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); -f2fs_put_cache (entry, false); +f2fs_put_cache (entry, true); } void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,@@ -478,7 +478,9 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache, index = entry->index +1 ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); +f2fs_unlock_cache (entry); }f2fs_cache_gang_release (entries, nr);fs/f2fs/cache.h +1
-0@@ -168,6 +168,7 @@ unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache, unsigned int max_items, int tag);void f2fs_cache_gang_release (struct f2fs_cached_block **entries,unsigned int nr_entries); +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty);int f2fs_writeback_cache (struct f2fs_cached_block_list *cache,bool sync);void f2fs_cache_wait_on_all_writeback (struct f2fs_cached_block_list *cache); void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,fs/f2fs/compress.c +22
-39@@ -1918,30 +1918,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,return compressed ? i -1 : i; } -const struct address_space_operations f2fs_compress_aops = { - .release_folio = f2fs_release_folio, - .invalidate_folio = f2fs_invalidate_folio, - .migrate_folio = filemap_migrate_folio, -}; - -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi) -{ -return sbi->compress_inode->i_mapping; -} -void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len) { -if (!sbi->compress_inode) +if (!test_opt (sbi,COMPRESS_CACHE ))return ; -invalidate_mapping_pages (COMPRESS_MAPPING (sbi), blkaddr, blkaddr + len -1 ); + +f2fs_drop_cache_range (COMPRESS_CACHE (sbi), blkaddr, len, false); }static void f2fs_cache_compressed_page (struct f2fs_sb_info *sbi,struct folio *folio,nid_t ino,block_t blkaddr) { -struct folio *cfolio; -int ret; +struct f2fs_cached_block *entry;if (!test_opt (sbi,COMPRESS_CACHE ))return ;@@ -1952,49 +1941,43 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,if (!f2fs_available_free_memory (sbi,COMPRESS_PAGE ))return ; - cfolio =filemap_get_folio (COMPRESS_MAPPING (sbi), blkaddr); -if (!IS_ERR (cfolio)) { -f2fs_folio_put (cfolio, false); + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_put_cache (entry, false);return ; } - cfolio =filemap_alloc_folio (__GFP_NOWARN | __GFP_IO,0 ,NULL ); -if (!cfolio) -return ; - - ret =filemap_add_folio (COMPRESS_MAPPING (sbi), cfolio, - blkaddr,GFP_NOFS ); -if (ret) { -f2fs_folio_put (cfolio, false); + entry =f2fs_grab_cache (COMPRESS_CACHE (sbi), blkaddr, +F2FS_CACHE_LOCK_CREATE ); +if (IS_ERR (entry))return ; - } -folio_set_f2fs_data (cfolio, ino); - -memcpy (folio_address (cfolio),folio_address (folio),PAGE_SIZE ); -folio_mark_uptodate (cfolio); -f2fs_folio_put (cfolio, true); + entry->ino = ino; +memcpy (cache_address (entry),folio_address (folio),PAGE_SIZE ); +f2fs_cache_set_uptodate (entry); +f2fs_put_cache (entry, true); }bool f2fs_load_compressed_folio (struct f2fs_sb_info *sbi,struct folio *folio,block_t blkaddr) { -struct folio *cfolio; +struct f2fs_cached_block *entry;bool hitted = false;if (!test_opt (sbi,COMPRESS_CACHE ))return false; - cfolio =f2fs_filemap_get_folio (COMPRESS_MAPPING (sbi), - blkaddr,FGP_LOCK |FGP_NOWAIT ,GFP_NOFS ); -if (!IS_ERR (cfolio)) { -if (folio_test_uptodate (cfolio)) { + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_lock_cache (entry); +if (f2fs_is_compress_cache (entry) && +f2fs_cache_test_uptodate (entry)) {atomic_inc (&sbi->compress_page_hit);memcpy (folio_address (folio), -folio_address (cfolio),folio_size (folio)); +cache_address (entry),folio_size (folio)); hitted = true; } -f2fs_folio_put (cfolio, true); +f2fs_put_cache (entry, true); }return hitted;fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 1986
Quoted 2 lines
fs/f2fs/compress.c +0
-0@@ -2002,71 +1985,70 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,void f2fs_invalidate_compress_pages (struct f2fs_sb_info *sbi,nid_t ino)This is O(N) Full-tree scan on inode eviction.
Quoted 6 lines
WDYT to improve it?
- Per-inode tracking
- Re-evaluate eviction policy: Since compress cache blocks are
indexed by physical block address (blkaddr) and hold immutable disk data, the cached blocks remain valid even after the inode is closed/evicted.
So you mean like extent cache policy, right? cache still keep valid before unlink/umount.
Quoted 3 lines
We should evaluate whether we actually need to synchronously purge
clean compress cache entries upon inode eviction, or if we can simply let the memory shrinker reclaim them via LRU under memory pressure.
I think your idea is good, also I'm good to improve compress cache, actually, in this patch I just want to do minimum replacement in between folio and internal cache, what do you think of improving it w/ a separated patch a bit later?
Quoted 13 lines
Quoted 13 lines
fs/f2fs/compress.c +6
-4{ -struct address_space *mapping =COMPRESS_MAPPING (sbi); -struct folio_batch fbatch; -pgoff_t index =0 ; -pgoff_t end =MAX_BLKADDR (sbi); +struct f2fs_cached_block_list *cache =COMPRESS_CACHE (sbi); +struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ]; +unsigned long flags; +pgoff_t index =0 , end =ULONG_MAX ; +int nr; +int i;
Quoted 3 lines
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2010
Quoted 1 line
fs/f2fs/compress.c +0
-1-if (!mapping->nrpages)Why did you remove this line?
Because compress_inode's mapping is not existing?
Quoted 45 lines
Quoted 45 lines
fs/f2fs/compress.c +24
-15+if (!test_opt (sbi,COMPRESS_CACHE ))return ; - -folio_batch_init (&fbatch); - - do { - unsigned int nr, i; - - nr = filemap_get_folios(mapping, &index, end - 1, &fbatch); - if (!nr) +next: +spin_lock_irqsave (&cache->tree_lock, flags); + nr =radix_tree_gang_lookup (&cache->root, (void **)entries, index, +min ((unsigned long )F2FS_ONSTACK_CACHES , end - index)); +if (!nr) + goto out_unlock; + +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; + +if (entry->index >= end) { + nr = i;break ; + } +f2fs_cache_get (entry); + } +out_unlock: +spin_unlock_irqrestore (&cache->tree_lock, flags); +if (!nr) +return ; +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; - for (i = 0; i < nr; i++) { - struct folio *folio = fbatch.folios[i]; + index = entry->index +1 ; - folio_lock(folio); - if (folio->mapping != mapping) { - folio_unlock(folio); - continue; - } +f2fs_lock_cache (entry);
Quoted 11 lines
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 8 lines
fs/f2fs/compress.c +1
-7-if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {How is this possible?
or if (!entry->cache)?
Looks like a impossible case, let me chaneg it to a f2fs_bug_on().
Thanks,
Quoted 334 lines
Thanks,
Quoted 331 lines
fs/f2fs/compress.c +17
-22+f2fs_unlock_cache (entry); +continue ; } -folio_batch_release (&fbatch); +if (entry->ino != ino) { +f2fs_unlock_cache (entry); +continue ; + } + +f2fs_truncate_cache (entry, false); +f2fs_unlock_cache (entry); + } +f2fs_cache_gang_release (entries, nr); + +if (index < end) {cond_resched (); - }while (index < end); + goto next; + } } -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { -struct inode *inode; -if (!test_opt (sbi,COMPRESS_CACHE )) -return 0 ; - - inode =f2fs_iget (sbi->sb,F2FS_COMPRESS_INO (sbi)); -if (IS_ERR (inode)) -return PTR_ERR (inode); - sbi->compress_inode = inode; +return ; sbi->compress_percent =COMPRESS_PERCENT ; sbi->compress_watermark =COMPRESS_WATERMARK ; -atomic_set (&sbi->compress_page_hit,0 ); - -return 0 ; -} - -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) -{ -if (!sbi->compress_inode) -return ; -iput (sbi->compress_inode); - sbi->compress_inode =NULL ; }int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi)fs/f2fs/debug.c +5
-4@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi) si->node_caches =NODE_CACHE (sbi)->num_entries; si->meta_caches =META_CACHE (sbi)->num_entries;#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { - si->compress_pages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { + si->compress_pages =COMPRESS_CACHE (sbi)->num_entries; si->compress_page_hit =atomic_read (&sbi->compress_page_hit); }#endif @@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->page_mem += (unsigned long long )NODE_CACHE (sbi)->num_entries <<PAGE_SHIFT ; si->cache_mem +=NODE_CACHE (sbi)->num_entries *sizeof (struct f2fs_cached_block );#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { -unsigned long npages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { +unsigned long npages =COMPRESS_CACHE (sbi)->num_entries; si->page_mem += (unsigned long long )npages <<PAGE_SHIFT ; + si->cache_mem += npages *sizeof (struct f2fs_cached_block ); }#endif }fs/f2fs/f2fs.h +2
-6@@ -2078,7 +2078,6 @@ struct f2fs_sb_info {u32 compr_new_inode;/* For compressed block cache */ -struct inode *compress_inode;/* cache compressed blocks */ unsigned int compress_percent;/* cache page percentage */ unsigned int compress_watermark;/* cache page watermark */ atomic_t compress_page_hit;/* cache hit count */ @@ -4821,13 +4820,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,int f2fs_init_compress_ctx (struct compress_ctx *cc);void f2fs_destroy_compress_ctx (struct compress_ctx *cc,bool reuse);void f2fs_init_compress_info (struct f2fs_sb_info *sbi); -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi); -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi); +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi);int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi);void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi);int __init f2fs_init_compress_cache (void );void f2fs_destroy_compress_cache (void ); -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi);void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len); bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,@@ -4876,8 +4873,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)static inline unsigned int f2fs_cluster_blocks_are_contiguous (struct dnode_of_data *dn,unsigned int ofs_in_node) {return 0 ; }static inline bool f2fs_sanity_check_cluster (struct dnode_of_data *dn) {return false; } -static inline int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) {return 0 ; } -static inline void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) { } +static inline void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { }static inline int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi) {return 0 ; }static inline void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi) { }static inline int __init f2fs_init_compress_cache (void ) {return 0 ; }fs/f2fs/inode.c +4
-45@@ -573,15 +573,6 @@ static int do_read_inode(struct inode *inode)return 0 ; } -static bool is_meta_ino (struct f2fs_sb_info *sbi,unsigned int ino) -{ -#ifdef CONFIG_F2FS_FS_COMPRESSION -if (test_opt (sbi,COMPRESS_CACHE ) && ino ==F2FS_COMPRESS_INO (sbi)) -return true; -#endif -return false; -} -struct inode *f2fs_iget (struct super_block *sb,unsigned long ino) {struct f2fs_sb_info *sbi =F2FS_SB (sb);@@ -593,42 +584,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return ERR_PTR (-ENOMEM );if (!(inode_state_read_once (inode) &I_NEW )) { -if (is_meta_ino (sbi, ino)) { -f2fs_err (sbi,"inaccessible inode: %lu, run fsck to repair" , ino); -set_sbi_flag (sbi,SBI_NEED_FSCK ); - ret = -EFSCORRUPTED ; -trace_f2fs_iget_exit (inode, ret); -iput (inode); -f2fs_handle_error (sbi,ERROR_CORRUPTED_INODE ); -fserror_report_file_metadata (inode, ret,GFP_NOFS ); -return ERR_PTR (ret); - } -trace_f2fs_iget (inode);return inode; } -if (is_meta_ino (sbi, ino)) - goto make_now; - ret =do_read_inode (inode);if (ret) goto bad_inode; -make_now: +f2fs_set_inode_flags (inode); -if (ino ==F2FS_COMPRESS_INO (sbi)) { -#ifdef CONFIG_F2FS_FS_COMPRESSION - inode->i_mapping->a_ops = &f2fs_compress_aops; -/* -* generic_error_remove_folio only truncates pages of regular -* inode -*/ - inode->i_mode |=S_IFREG ; -#endif -mapping_set_gfp_mask (inode->i_mapping, -GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); - }else if (S_ISREG (inode->i_mode)) { +if (S_ISREG (inode->i_mode)) { inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops;@@ -879,7 +845,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)/* * Return true, if we shouldn't go through post_evict_inode. */ -static bool f2fs_pre_evict_inode (struct inode *inode) +static void f2fs_pre_evict_inode (struct inode *inode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode);@@ -905,17 +871,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -if (inode->i_ino ==F2FS_COMPRESS_INO (sbi)) -return true; -f2fs_bug_on (sbi,get_dirty_pages (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);if (!IS_DEVICE_ALIASING (inode))f2fs_destroy_extent_tree (inode); - -return false; }static void f2fs_delete_inode (struct inode *inode)@@ -1077,15 +1038,13 @@ static void f2fs_post_evict_inode(struct inode *inode)*/ void f2fs_evict_inode (struct inode *inode) { -if (f2fs_pre_evict_inode (inode)) - goto clear_out; +f2fs_pre_evict_inode (inode);if (!inode->i_nlink && !is_bad_inode (inode))f2fs_delete_inode (inode);f2fs_post_evict_inode (inode); -clear_out:fscrypt_put_encryption_info (inode);clear_inode (inode); }fs/f2fs/node.c +1
-1@@ -119,7 +119,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)* exceed threshold, deny caching compress page. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_MAPPING (sbi)->nrpages < + (COMPRESS_CACHE (sbi)->num_entries < free_ram * sbi->compress_percent /100 );#else res = false;fs/f2fs/shrinker.c +2
-1@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,static unsigned long __count_cache (struct f2fs_sb_info *sbi) {return sbi->meta_blocks.num_entries + - sbi->node_blocks.num_entries; + sbi->node_blocks.num_entries + + sbi->compress_blocks.num_entries; } unsigned long f2fs_shrink_count(struct shrinker *shrink,fs/f2fs/super.c +5
-10@@ -2050,8 +2050,6 @@ static void f2fs_put_super(struct super_block *sb)f2fs_bug_on (sbi, sbi->fsync_node_num); -f2fs_destroy_compress_inode (sbi); -f2fs_destroy_cache (COMPRESS_CACHE (sbi));f2fs_destroy_cache (NODE_CACHE (sbi));f2fs_destroy_cache (META_CACHE (sbi));@@ -5285,6 +5283,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_init_fsync_node_info (sbi); +f2fs_init_compress_cache_context (sbi); +/* setup checkpoint request control and start checkpoint issue thread */ f2fs_init_ckpt_req_control (sbi); if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&@@ -5363,13 +5363,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) goto free_ino_entry; } - err =f2fs_init_compress_inode (sbi); -if (err) - goto free_root_inode; - err =f2fs_register_sysfs (sbi);if (err) - goto free_compress_inode; + goto free_root_inode; sbi->umount_lock_holder = current;#ifdef CONFIG_QUOTA @@ -5537,8 +5533,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* evict some inodes being cached by GC */ evict_inodes (sb);f2fs_unregister_sysfs (sbi); -free_compress_inode: -f2fs_destroy_compress_inode (sbi); free_root_inode:dput (sb->s_root); sb->s_root =NULL ;@@ -5655,7 +5649,8 @@ static void kill_f2fs_super(struct super_block *sb)* compress inode cache. */ if (test_opt (sbi,COMPRESS_CACHE )) -truncate_inode_pages_final (COMPRESS_MAPPING (sbi)); +f2fs_invalidate_compress_pages_range (sbi, +0 ,UINT_MAX );#endif if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||include/linux/f2fs_fs.h +0
-2@@ -34,7 +34,6 @@#define F2FS_RESERVED_NODE_NUM 3#define F2FS_ROOT_INO (sbi) ((sbi)->root_ino_num) -#define F2FS_COMPRESS_INO (sbi) (NM_I(sbi)->max_nid)#define F2FS_MAX_QUOTAS 3 --2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Re: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 212 lines · Chao Yu
Quoted 32 lines · Daeho Jeong
Quoted 30 lines · On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel…
This patch migrates compressed cluster caching from the fake VFS inode page cache (
sbi->compress_inode) to compress cache (sbi->compress_blocks).It converts compression caching and decompression paths to use compress cache APIs, uses
entry->inofor per-inode invalidation, and removessbi->compress_inode.Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/cache.c | 14 ++-- fs/f2fs/cache.h | 1 + fs/f2fs/compress.c | 158 ++++++++++++++++++---------------------- fs/f2fs/debug.c | 9 ++- fs/f2fs/f2fs.h | 8 +- fs/f2fs/inode.c | 49 +------------ fs/f2fs/node.c | 2 +- fs/f2fs/shrinker.c | 3 +- fs/f2fs/super.c | 15 ++-- include/linux/f2fs_fs.h | 1 - 10 files changed, 98 insertions(+), 162 deletions(-)fs/f2fs/cache.c +0
-0@@ -417,13 +417,12 @@ static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,spin_unlock (&cache->list_lock); }Quoted 5 lines
fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 3 lines
fs/f2fs/cache.c +1
-1-static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Actually, I'v relocated the lock into callers of
f2fs_truncate_cache(), in order to support checkentry->ino,entry->cachew/ the lock inf2fs_invalidate_compress_pages(), please take a look.Quoted 158 lines
Quoted 153 lines
fs/f2fs/cache.c +7
-5{ -f2fs_lock_cache (entry); -if (entry->cache) -f2fs_do_truncate_cache (entry, drop_dirty); -f2fs_unlock_cache (entry); +if (!entry->cache) +return ; +f2fs_do_truncate_cache (entry, drop_dirty); } static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,@@ -435,8 +434,9 @@ static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,if (IS_ERR (entry))return ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); -f2fs_put_cache (entry, false); +f2fs_put_cache (entry, true); } void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,@@ -478,7 +478,9 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache, index = entry->index +1 ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); +f2fs_unlock_cache (entry); }f2fs_cache_gang_release (entries, nr);fs/f2fs/cache.h +1
-0@@ -168,6 +168,7 @@ unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache, unsigned int max_items, int tag);void f2fs_cache_gang_release (struct f2fs_cached_block **entries,unsigned int nr_entries); +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty);int f2fs_writeback_cache (struct f2fs_cached_block_list *cache,bool sync);void f2fs_cache_wait_on_all_writeback (struct f2fs_cached_block_list *cache); void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,fs/f2fs/compress.c +22
-39@@ -1918,30 +1918,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,return compressed ? i -1 : i; } -const struct address_space_operations f2fs_compress_aops = { - .release_folio = f2fs_release_folio, - .invalidate_folio = f2fs_invalidate_folio, - .migrate_folio = filemap_migrate_folio, -}; - -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi) -{ -return sbi->compress_inode->i_mapping; -} -void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len) { -if (!sbi->compress_inode) +if (!test_opt (sbi,COMPRESS_CACHE ))return ; -invalidate_mapping_pages (COMPRESS_MAPPING (sbi), blkaddr, blkaddr + len -1 ); + +f2fs_drop_cache_range (COMPRESS_CACHE (sbi), blkaddr, len, false); }static void f2fs_cache_compressed_page (struct f2fs_sb_info *sbi,struct folio *folio,nid_t ino,block_t blkaddr) { -struct folio *cfolio; -int ret; +struct f2fs_cached_block *entry;if (!test_opt (sbi,COMPRESS_CACHE ))return ;@@ -1952,49 +1941,43 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,if (!f2fs_available_free_memory (sbi,COMPRESS_PAGE ))return ; - cfolio =filemap_get_folio (COMPRESS_MAPPING (sbi), blkaddr); -if (!IS_ERR (cfolio)) { -f2fs_folio_put (cfolio, false); + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_put_cache (entry, false);return ; } - cfolio =filemap_alloc_folio (__GFP_NOWARN | __GFP_IO,0 ,NULL ); -if (!cfolio) -return ; - - ret =filemap_add_folio (COMPRESS_MAPPING (sbi), cfolio, - blkaddr,GFP_NOFS ); -if (ret) { -f2fs_folio_put (cfolio, false); + entry =f2fs_grab_cache (COMPRESS_CACHE (sbi), blkaddr, +F2FS_CACHE_LOCK_CREATE ); +if (IS_ERR (entry))return ; - } -folio_set_f2fs_data (cfolio, ino); - -memcpy (folio_address (cfolio),folio_address (folio),PAGE_SIZE ); -folio_mark_uptodate (cfolio); -f2fs_folio_put (cfolio, true); + entry->ino = ino; +memcpy (cache_address (entry),folio_address (folio),PAGE_SIZE ); +f2fs_cache_set_uptodate (entry); +f2fs_put_cache (entry, true); }bool f2fs_load_compressed_folio (struct f2fs_sb_info *sbi,struct folio *folio,block_t blkaddr) { -struct folio *cfolio; +struct f2fs_cached_block *entry;bool hitted = false;if (!test_opt (sbi,COMPRESS_CACHE ))return false; - cfolio =f2fs_filemap_get_folio (COMPRESS_MAPPING (sbi), - blkaddr,FGP_LOCK |FGP_NOWAIT ,GFP_NOFS ); -if (!IS_ERR (cfolio)) { -if (folio_test_uptodate (cfolio)) { + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_lock_cache (entry); +if (f2fs_is_compress_cache (entry) && +f2fs_cache_test_uptodate (entry)) {atomic_inc (&sbi->compress_page_hit);memcpy (folio_address (folio), -folio_address (cfolio),folio_size (folio)); +cache_address (entry),folio_size (folio)); hitted = true; } -f2fs_folio_put (cfolio, true); +f2fs_put_cache (entry, true); }return hitted;fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 1986
Quoted 2 lines
fs/f2fs/compress.c +0
-0@@ -2002,71 +1985,70 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,void f2fs_invalidate_compress_pages (struct f2fs_sb_info *sbi,nid_t ino)This is O(N) Full-tree scan on inode eviction.
Quoted 6 lines
WDYT to improve it?
- Per-inode tracking
- Re-evaluate eviction policy: Since compress cache blocks are
indexed by physical block address (blkaddr) and hold immutable disk data, the cached blocks remain valid even after the inode is closed/evicted.So you mean like extent cache policy, right? cache still keep valid before unlink/umount.
Quoted 7 lines
Quoted 3 lines
We should evaluate whether we actually need to synchronously purge
clean compress cache entries upon inode eviction, or if we can simply let the memory shrinker reclaim them via LRU under memory pressure.I think your idea is good, also I'm good to improve compress cache, actually, in this patch I just want to do minimum replacement in between folio and internal cache, what do you think of improving it w/ a separated patch a bit later?
Makes sense. We can add it to the TO-DO list.
Thanks,
Quoted 416 lines
Quoted 14 lines
Quoted 14 lines
fs/f2fs/compress.c +6
-4{ -struct address_space *mapping =COMPRESS_MAPPING (sbi); -struct folio_batch fbatch; -pgoff_t index =0 ; -pgoff_t end =MAX_BLKADDR (sbi); +struct f2fs_cached_block_list *cache =COMPRESS_CACHE (sbi); +struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ]; +unsigned long flags; +pgoff_t index =0 , end =ULONG_MAX ; +int nr; +int i;Quoted 3 lines
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2010
Quoted 1 line
fs/f2fs/compress.c +0
-1-if (!mapping->nrpages)Why did you remove this line?
Because compress_inode's mapping is not existing?
Quoted 45 lines
Quoted 45 lines
fs/f2fs/compress.c +24
-15+if (!test_opt (sbi,COMPRESS_CACHE ))return ; - -folio_batch_init (&fbatch); - - do { - unsigned int nr, i; - - nr = filemap_get_folios(mapping, &index, end - 1, &fbatch); - if (!nr) +next: +spin_lock_irqsave (&cache->tree_lock, flags); + nr =radix_tree_gang_lookup (&cache->root, (void **)entries, index, +min ((unsigned long )F2FS_ONSTACK_CACHES , end - index)); +if (!nr) + goto out_unlock; + +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; + +if (entry->index >= end) { + nr = i;break ; + } +f2fs_cache_get (entry); + } +out_unlock: +spin_unlock_irqrestore (&cache->tree_lock, flags); +if (!nr) +return ; +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; - for (i = 0; i < nr; i++) { - struct folio *folio = fbatch.folios[i]; + index = entry->index +1 ; - folio_lock(folio); - if (folio->mapping != mapping) { - folio_unlock(folio); - continue; - } +f2fs_lock_cache (entry);Quoted 11 lines
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 8 lines
fs/f2fs/compress.c +1
-7-if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {How is this possible?
or if (!entry->cache)?Looks like a impossible case, let me chaneg it to a
f2fs_bug_on().Thanks,
Quoted 334 lines
Thanks,
Quoted 331 lines
fs/f2fs/compress.c +17
-22+f2fs_unlock_cache (entry); +continue ; } -folio_batch_release (&fbatch); +if (entry->ino != ino) { +f2fs_unlock_cache (entry); +continue ; + } + +f2fs_truncate_cache (entry, false); +f2fs_unlock_cache (entry); + } +f2fs_cache_gang_release (entries, nr); + +if (index < end) {cond_resched (); - }while (index < end); + goto next; + } } -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { -struct inode *inode; -if (!test_opt (sbi,COMPRESS_CACHE )) -return 0 ; - - inode =f2fs_iget (sbi->sb,F2FS_COMPRESS_INO (sbi)); -if (IS_ERR (inode)) -return PTR_ERR (inode); - sbi->compress_inode = inode; +return ; sbi->compress_percent =COMPRESS_PERCENT ; sbi->compress_watermark =COMPRESS_WATERMARK ; -atomic_set (&sbi->compress_page_hit,0 ); - -return 0 ; -} - -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) -{ -if (!sbi->compress_inode) -return ; -iput (sbi->compress_inode); - sbi->compress_inode =NULL ; }int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi)fs/f2fs/debug.c +5
-4@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi) si->node_caches =NODE_CACHE (sbi)->num_entries; si->meta_caches =META_CACHE (sbi)->num_entries;#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { - si->compress_pages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { + si->compress_pages =COMPRESS_CACHE (sbi)->num_entries; si->compress_page_hit =atomic_read (&sbi->compress_page_hit); }#endif @@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->page_mem += (unsigned long long )NODE_CACHE (sbi)->num_entries <<PAGE_SHIFT ; si->cache_mem +=NODE_CACHE (sbi)->num_entries *sizeof (struct f2fs_cached_block );#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { -unsigned long npages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { +unsigned long npages =COMPRESS_CACHE (sbi)->num_entries; si->page_mem += (unsigned long long )npages <<PAGE_SHIFT ; + si->cache_mem += npages *sizeof (struct f2fs_cached_block ); }#endif }fs/f2fs/f2fs.h +2
-6@@ -2078,7 +2078,6 @@ struct f2fs_sb_info {u32 compr_new_inode;/* For compressed block cache */ -struct inode *compress_inode;/* cache compressed blocks */ unsigned int compress_percent;/* cache page percentage */ unsigned int compress_watermark;/* cache page watermark */ atomic_t compress_page_hit;/* cache hit count */ @@ -4821,13 +4820,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,int f2fs_init_compress_ctx (struct compress_ctx *cc);void f2fs_destroy_compress_ctx (struct compress_ctx *cc,bool reuse);void f2fs_init_compress_info (struct f2fs_sb_info *sbi); -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi); -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi); +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi);int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi);void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi);int __init f2fs_init_compress_cache (void );void f2fs_destroy_compress_cache (void ); -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi);void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len); bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,@@ -4876,8 +4873,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)static inline unsigned int f2fs_cluster_blocks_are_contiguous (struct dnode_of_data *dn,unsigned int ofs_in_node) {return 0 ; }static inline bool f2fs_sanity_check_cluster (struct dnode_of_data *dn) {return false; } -static inline int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) {return 0 ; } -static inline void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) { } +static inline void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { }static inline int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi) {return 0 ; }static inline void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi) { }static inline int __init f2fs_init_compress_cache (void ) {return 0 ; }fs/f2fs/inode.c +4
-45@@ -573,15 +573,6 @@ static int do_read_inode(struct inode *inode)return 0 ; } -static bool is_meta_ino (struct f2fs_sb_info *sbi,unsigned int ino) -{ -#ifdef CONFIG_F2FS_FS_COMPRESSION -if (test_opt (sbi,COMPRESS_CACHE ) && ino ==F2FS_COMPRESS_INO (sbi)) -return true; -#endif -return false; -} -struct inode *f2fs_iget (struct super_block *sb,unsigned long ino) {struct f2fs_sb_info *sbi =F2FS_SB (sb);@@ -593,42 +584,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return ERR_PTR (-ENOMEM );if (!(inode_state_read_once (inode) &I_NEW )) { -if (is_meta_ino (sbi, ino)) { -f2fs_err (sbi,"inaccessible inode: %lu, run fsck to repair" , ino); -set_sbi_flag (sbi,SBI_NEED_FSCK ); - ret = -EFSCORRUPTED ; -trace_f2fs_iget_exit (inode, ret); -iput (inode); -f2fs_handle_error (sbi,ERROR_CORRUPTED_INODE ); -fserror_report_file_metadata (inode, ret,GFP_NOFS ); -return ERR_PTR (ret); - } -trace_f2fs_iget (inode);return inode; } -if (is_meta_ino (sbi, ino)) - goto make_now; - ret =do_read_inode (inode);if (ret) goto bad_inode; -make_now: +f2fs_set_inode_flags (inode); -if (ino ==F2FS_COMPRESS_INO (sbi)) { -#ifdef CONFIG_F2FS_FS_COMPRESSION - inode->i_mapping->a_ops = &f2fs_compress_aops; -/* -* generic_error_remove_folio only truncates pages of regular -* inode -*/ - inode->i_mode |=S_IFREG ; -#endif -mapping_set_gfp_mask (inode->i_mapping, -GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); - }else if (S_ISREG (inode->i_mode)) { +if (S_ISREG (inode->i_mode)) { inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops;@@ -879,7 +845,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)/* * Return true, if we shouldn't go through post_evict_inode. */ -static bool f2fs_pre_evict_inode (struct inode *inode) +static void f2fs_pre_evict_inode (struct inode *inode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode);@@ -905,17 +871,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -if (inode->i_ino ==F2FS_COMPRESS_INO (sbi)) -return true; -f2fs_bug_on (sbi,get_dirty_pages (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);if (!IS_DEVICE_ALIASING (inode))f2fs_destroy_extent_tree (inode); - -return false; }static void f2fs_delete_inode (struct inode *inode)@@ -1077,15 +1038,13 @@ static void f2fs_post_evict_inode(struct inode *inode)*/ void f2fs_evict_inode (struct inode *inode) { -if (f2fs_pre_evict_inode (inode)) - goto clear_out; +f2fs_pre_evict_inode (inode);if (!inode->i_nlink && !is_bad_inode (inode))f2fs_delete_inode (inode);f2fs_post_evict_inode (inode); -clear_out:fscrypt_put_encryption_info (inode);clear_inode (inode); }fs/f2fs/node.c +1
-1@@ -119,7 +119,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)* exceed threshold, deny caching compress page. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_MAPPING (sbi)->nrpages < + (COMPRESS_CACHE (sbi)->num_entries < free_ram * sbi->compress_percent /100 );#else res = false;fs/f2fs/shrinker.c +2
-1@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,static unsigned long __count_cache (struct f2fs_sb_info *sbi) {return sbi->meta_blocks.num_entries + - sbi->node_blocks.num_entries; + sbi->node_blocks.num_entries + + sbi->compress_blocks.num_entries; } unsigned long f2fs_shrink_count(struct shrinker *shrink,fs/f2fs/super.c +5
-10@@ -2050,8 +2050,6 @@ static void f2fs_put_super(struct super_block *sb)f2fs_bug_on (sbi, sbi->fsync_node_num); -f2fs_destroy_compress_inode (sbi); -f2fs_destroy_cache (COMPRESS_CACHE (sbi));f2fs_destroy_cache (NODE_CACHE (sbi));f2fs_destroy_cache (META_CACHE (sbi));@@ -5285,6 +5283,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_init_fsync_node_info (sbi); +f2fs_init_compress_cache_context (sbi); +/* setup checkpoint request control and start checkpoint issue thread */ f2fs_init_ckpt_req_control (sbi); if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&@@ -5363,13 +5363,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) goto free_ino_entry; } - err =f2fs_init_compress_inode (sbi); -if (err) - goto free_root_inode; - err =f2fs_register_sysfs (sbi);if (err) - goto free_compress_inode; + goto free_root_inode; sbi->umount_lock_holder = current;#ifdef CONFIG_QUOTA @@ -5537,8 +5533,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* evict some inodes being cached by GC */ evict_inodes (sb);f2fs_unregister_sysfs (sbi); -free_compress_inode: -f2fs_destroy_compress_inode (sbi); free_root_inode:dput (sb->s_root); sb->s_root =NULL ;@@ -5655,7 +5649,8 @@ static void kill_f2fs_super(struct super_block *sb)* compress inode cache. */ if (test_opt (sbi,COMPRESS_CACHE )) -truncate_inode_pages_final (COMPRESS_MAPPING (sbi)); +f2fs_invalidate_compress_pages_range (sbi, +0 ,UINT_MAX );#endif if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||include/linux/f2fs_fs.h +0
-2@@ -34,7 +34,6 @@#define F2FS_RESERVED_NODE_NUM 3#define F2FS_ROOT_INO (sbi) ((sbi)->root_ino_num) -#define F2FS_COMPRESS_INO (sbi) (NM_I(sbi)->max_nid)#define F2FS_MAX_QUOTAS 3 --2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
In reply to Daeho JeongQuoted 217 lines · Daeho Jeong
Quoted 27 lines · On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel…
This patch migrates compressed cluster caching from the fake VFS inode page cache (
sbi->compress_inode) to compress cache (sbi->compress_blocks).It converts compression caching and decompression paths to use compress cache APIs, uses
entry->inofor per-inode invalidation, and removessbi->compress_inode.Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/cache.c | 14 ++-- fs/f2fs/cache.h | 1 + fs/f2fs/compress.c | 158 ++++++++++++++++++---------------------- fs/f2fs/debug.c | 9 ++- fs/f2fs/f2fs.h | 8 +- fs/f2fs/inode.c | 49 +------------ fs/f2fs/node.c | 2 +- fs/f2fs/shrinker.c | 3 +- fs/f2fs/super.c | 15 ++-- include/linux/f2fs_fs.h | 1 - 10 files changed, 98 insertions(+), 162 deletions(-)fs/f2fs/cache.c +0
-0fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 6 lines
fs/f2fs/cache.c +1
-1@@ -417,13 +417,12 @@ static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,spin_unlock (&cache->list_lock); } -static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Quoted 152 lines
fs/f2fs/cache.c +7
-5{ -f2fs_lock_cache (entry); -if (entry->cache) -f2fs_do_truncate_cache (entry, drop_dirty); -f2fs_unlock_cache (entry); +if (!entry->cache) +return ; +f2fs_do_truncate_cache (entry, drop_dirty); } static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,@@ -435,8 +434,9 @@ static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,if (IS_ERR (entry))return ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); -f2fs_put_cache (entry, false); +f2fs_put_cache (entry, true); } void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,@@ -478,7 +478,9 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache, index = entry->index +1 ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); +f2fs_unlock_cache (entry); }f2fs_cache_gang_release (entries, nr);fs/f2fs/cache.h +1
-0@@ -168,6 +168,7 @@ unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache, unsigned int max_items, int tag);void f2fs_cache_gang_release (struct f2fs_cached_block **entries,unsigned int nr_entries); +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty);int f2fs_writeback_cache (struct f2fs_cached_block_list *cache,bool sync);void f2fs_cache_wait_on_all_writeback (struct f2fs_cached_block_list *cache); void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,fs/f2fs/compress.c +22
-39@@ -1918,30 +1918,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,return compressed ? i -1 : i; } -const struct address_space_operations f2fs_compress_aops = { - .release_folio = f2fs_release_folio, - .invalidate_folio = f2fs_invalidate_folio, - .migrate_folio = filemap_migrate_folio, -}; - -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi) -{ -return sbi->compress_inode->i_mapping; -} -void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len) { -if (!sbi->compress_inode) +if (!test_opt (sbi,COMPRESS_CACHE ))return ; -invalidate_mapping_pages (COMPRESS_MAPPING (sbi), blkaddr, blkaddr + len -1 ); + +f2fs_drop_cache_range (COMPRESS_CACHE (sbi), blkaddr, len, false); }static void f2fs_cache_compressed_page (struct f2fs_sb_info *sbi,struct folio *folio,nid_t ino,block_t blkaddr) { -struct folio *cfolio; -int ret; +struct f2fs_cached_block *entry;if (!test_opt (sbi,COMPRESS_CACHE ))return ;@@ -1952,49 +1941,43 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,if (!f2fs_available_free_memory (sbi,COMPRESS_PAGE ))return ; - cfolio =filemap_get_folio (COMPRESS_MAPPING (sbi), blkaddr); -if (!IS_ERR (cfolio)) { -f2fs_folio_put (cfolio, false); + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_put_cache (entry, false);return ; } - cfolio =filemap_alloc_folio (__GFP_NOWARN | __GFP_IO,0 ,NULL ); -if (!cfolio) -return ; - - ret =filemap_add_folio (COMPRESS_MAPPING (sbi), cfolio, - blkaddr,GFP_NOFS ); -if (ret) { -f2fs_folio_put (cfolio, false); + entry =f2fs_grab_cache (COMPRESS_CACHE (sbi), blkaddr, +F2FS_CACHE_LOCK_CREATE ); +if (IS_ERR (entry))return ; - } -folio_set_f2fs_data (cfolio, ino); - -memcpy (folio_address (cfolio),folio_address (folio),PAGE_SIZE ); -folio_mark_uptodate (cfolio); -f2fs_folio_put (cfolio, true); + entry->ino = ino; +memcpy (cache_address (entry),folio_address (folio),PAGE_SIZE ); +f2fs_cache_set_uptodate (entry); +f2fs_put_cache (entry, true); }bool f2fs_load_compressed_folio (struct f2fs_sb_info *sbi,struct folio *folio,block_t blkaddr) { -struct folio *cfolio; +struct f2fs_cached_block *entry;bool hitted = false;if (!test_opt (sbi,COMPRESS_CACHE ))return false; - cfolio =f2fs_filemap_get_folio (COMPRESS_MAPPING (sbi), - blkaddr,FGP_LOCK |FGP_NOWAIT ,GFP_NOFS ); -if (!IS_ERR (cfolio)) { -if (folio_test_uptodate (cfolio)) { + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_lock_cache (entry); +if (f2fs_is_compress_cache (entry) && +f2fs_cache_test_uptodate (entry)) {atomic_inc (&sbi->compress_page_hit);memcpy (folio_address (folio), -folio_address (cfolio),folio_size (folio)); +cache_address (entry),folio_size (folio)); hitted = true; } -f2fs_folio_put (cfolio, true); +f2fs_put_cache (entry, true); }return hitted;fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 1986
Quoted 2 lines
fs/f2fs/compress.c +0
-0@@ -2002,71 +1985,70 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,void f2fs_invalidate_compress_pages (struct f2fs_sb_info *sbi,nid_t ino)This is O(N) Full-tree scan on inode eviction.
WDYT to improve it?
- Per-inode tracking
- Re-evaluate eviction policy: Since compress cache blocks are
indexed by physical block address (blkaddr) and hold immutable disk data, the cached blocks remain valid even after the inode is closed/evicted.
We should evaluate whether we actually need to synchronously purge
clean compress cache entries upon inode eviction, or if we can simply let the memory shrinker reclaim them via LRU under memory pressure.Quoted 12 lines
fs/f2fs/compress.c +6
-4{ -struct address_space *mapping =COMPRESS_MAPPING (sbi); -struct folio_batch fbatch; -pgoff_t index =0 ; -pgoff_t end =MAX_BLKADDR (sbi); +struct f2fs_cached_block_list *cache =COMPRESS_CACHE (sbi); +struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ]; +unsigned long flags; +pgoff_t index =0 , end =ULONG_MAX ; +int nr; +int i;
Quoted 3 lines
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2010
Quoted 1 line
fs/f2fs/compress.c +0
-1-if (!mapping->nrpages)Why did you remove this line?
Oh, I see your point, we need to check cache->num_entries here.
Thanks,
Quoted 390 lines
Quoted 43 lines
fs/f2fs/compress.c +23
-15+if (!test_opt (sbi,COMPRESS_CACHE ))return ; - -folio_batch_init (&fbatch); - - do { - unsigned int nr, i; - - nr = filemap_get_folios(mapping, &index, end - 1, &fbatch); - if (!nr) +next: +spin_lock_irqsave (&cache->tree_lock, flags); + nr =radix_tree_gang_lookup (&cache->root, (void **)entries, index, +min ((unsigned long )F2FS_ONSTACK_CACHES , end - index)); +if (!nr) + goto out_unlock; + +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; + +if (entry->index >= end) { + nr = i;break ; + } +f2fs_cache_get (entry); + } +out_unlock: +spin_unlock_irqrestore (&cache->tree_lock, flags); +if (!nr) +return ; +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; - for (i = 0; i < nr; i++) { - struct folio *folio = fbatch.folios[i]; + index = entry->index +1 ; - folio_lock(folio); - if (folio->mapping != mapping) { - folio_unlock(folio); - continue; - }fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 10 lines
fs/f2fs/compress.c +2
-7+f2fs_lock_cache (entry); -if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {How is this possible?
or if (!entry->cache)?Thanks,
Quoted 331 lines
fs/f2fs/compress.c +17
-22+f2fs_unlock_cache (entry); +continue ; } -folio_batch_release (&fbatch); +if (entry->ino != ino) { +f2fs_unlock_cache (entry); +continue ; + } + +f2fs_truncate_cache (entry, false); +f2fs_unlock_cache (entry); + } +f2fs_cache_gang_release (entries, nr); + +if (index < end) {cond_resched (); - }while (index < end); + goto next; + } } -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { -struct inode *inode; -if (!test_opt (sbi,COMPRESS_CACHE )) -return 0 ; - - inode =f2fs_iget (sbi->sb,F2FS_COMPRESS_INO (sbi)); -if (IS_ERR (inode)) -return PTR_ERR (inode); - sbi->compress_inode = inode; +return ; sbi->compress_percent =COMPRESS_PERCENT ; sbi->compress_watermark =COMPRESS_WATERMARK ; -atomic_set (&sbi->compress_page_hit,0 ); - -return 0 ; -} - -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) -{ -if (!sbi->compress_inode) -return ; -iput (sbi->compress_inode); - sbi->compress_inode =NULL ; }int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi)fs/f2fs/debug.c +5
-4@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi) si->node_caches =NODE_CACHE (sbi)->num_entries; si->meta_caches =META_CACHE (sbi)->num_entries;#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { - si->compress_pages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { + si->compress_pages =COMPRESS_CACHE (sbi)->num_entries; si->compress_page_hit =atomic_read (&sbi->compress_page_hit); }#endif @@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->page_mem += (unsigned long long )NODE_CACHE (sbi)->num_entries <<PAGE_SHIFT ; si->cache_mem +=NODE_CACHE (sbi)->num_entries *sizeof (struct f2fs_cached_block );#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { -unsigned long npages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { +unsigned long npages =COMPRESS_CACHE (sbi)->num_entries; si->page_mem += (unsigned long long )npages <<PAGE_SHIFT ; + si->cache_mem += npages *sizeof (struct f2fs_cached_block ); }#endif }fs/f2fs/f2fs.h +2
-6@@ -2078,7 +2078,6 @@ struct f2fs_sb_info {u32 compr_new_inode;/* For compressed block cache */ -struct inode *compress_inode;/* cache compressed blocks */ unsigned int compress_percent;/* cache page percentage */ unsigned int compress_watermark;/* cache page watermark */ atomic_t compress_page_hit;/* cache hit count */ @@ -4821,13 +4820,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,int f2fs_init_compress_ctx (struct compress_ctx *cc);void f2fs_destroy_compress_ctx (struct compress_ctx *cc,bool reuse);void f2fs_init_compress_info (struct f2fs_sb_info *sbi); -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi); -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi); +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi);int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi);void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi);int __init f2fs_init_compress_cache (void );void f2fs_destroy_compress_cache (void ); -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi);void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len); bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,@@ -4876,8 +4873,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)static inline unsigned int f2fs_cluster_blocks_are_contiguous (struct dnode_of_data *dn,unsigned int ofs_in_node) {return 0 ; }static inline bool f2fs_sanity_check_cluster (struct dnode_of_data *dn) {return false; } -static inline int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) {return 0 ; } -static inline void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) { } +static inline void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { }static inline int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi) {return 0 ; }static inline void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi) { }static inline int __init f2fs_init_compress_cache (void ) {return 0 ; }fs/f2fs/inode.c +4
-45@@ -573,15 +573,6 @@ static int do_read_inode(struct inode *inode)return 0 ; } -static bool is_meta_ino (struct f2fs_sb_info *sbi,unsigned int ino) -{ -#ifdef CONFIG_F2FS_FS_COMPRESSION -if (test_opt (sbi,COMPRESS_CACHE ) && ino ==F2FS_COMPRESS_INO (sbi)) -return true; -#endif -return false; -} -struct inode *f2fs_iget (struct super_block *sb,unsigned long ino) {struct f2fs_sb_info *sbi =F2FS_SB (sb);@@ -593,42 +584,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return ERR_PTR (-ENOMEM );if (!(inode_state_read_once (inode) &I_NEW )) { -if (is_meta_ino (sbi, ino)) { -f2fs_err (sbi,"inaccessible inode: %lu, run fsck to repair" , ino); -set_sbi_flag (sbi,SBI_NEED_FSCK ); - ret = -EFSCORRUPTED ; -trace_f2fs_iget_exit (inode, ret); -iput (inode); -f2fs_handle_error (sbi,ERROR_CORRUPTED_INODE ); -fserror_report_file_metadata (inode, ret,GFP_NOFS ); -return ERR_PTR (ret); - } -trace_f2fs_iget (inode);return inode; } -if (is_meta_ino (sbi, ino)) - goto make_now; - ret =do_read_inode (inode);if (ret) goto bad_inode; -make_now: +f2fs_set_inode_flags (inode); -if (ino ==F2FS_COMPRESS_INO (sbi)) { -#ifdef CONFIG_F2FS_FS_COMPRESSION - inode->i_mapping->a_ops = &f2fs_compress_aops; -/* -* generic_error_remove_folio only truncates pages of regular -* inode -*/ - inode->i_mode |=S_IFREG ; -#endif -mapping_set_gfp_mask (inode->i_mapping, -GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); - }else if (S_ISREG (inode->i_mode)) { +if (S_ISREG (inode->i_mode)) { inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops;@@ -879,7 +845,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)/* * Return true, if we shouldn't go through post_evict_inode. */ -static bool f2fs_pre_evict_inode (struct inode *inode) +static void f2fs_pre_evict_inode (struct inode *inode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode);@@ -905,17 +871,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -if (inode->i_ino ==F2FS_COMPRESS_INO (sbi)) -return true; -f2fs_bug_on (sbi,get_dirty_pages (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);if (!IS_DEVICE_ALIASING (inode))f2fs_destroy_extent_tree (inode); - -return false; }static void f2fs_delete_inode (struct inode *inode)@@ -1077,15 +1038,13 @@ static void f2fs_post_evict_inode(struct inode *inode)*/ void f2fs_evict_inode (struct inode *inode) { -if (f2fs_pre_evict_inode (inode)) - goto clear_out; +f2fs_pre_evict_inode (inode);if (!inode->i_nlink && !is_bad_inode (inode))f2fs_delete_inode (inode);f2fs_post_evict_inode (inode); -clear_out:fscrypt_put_encryption_info (inode);clear_inode (inode); }fs/f2fs/node.c +1
-1@@ -119,7 +119,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)* exceed threshold, deny caching compress page. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_MAPPING (sbi)->nrpages < + (COMPRESS_CACHE (sbi)->num_entries < free_ram * sbi->compress_percent /100 );#else res = false;fs/f2fs/shrinker.c +2
-1@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,static unsigned long __count_cache (struct f2fs_sb_info *sbi) {return sbi->meta_blocks.num_entries + - sbi->node_blocks.num_entries; + sbi->node_blocks.num_entries + + sbi->compress_blocks.num_entries; } unsigned long f2fs_shrink_count(struct shrinker *shrink,fs/f2fs/super.c +5
-10@@ -2050,8 +2050,6 @@ static void f2fs_put_super(struct super_block *sb)f2fs_bug_on (sbi, sbi->fsync_node_num); -f2fs_destroy_compress_inode (sbi); -f2fs_destroy_cache (COMPRESS_CACHE (sbi));f2fs_destroy_cache (NODE_CACHE (sbi));f2fs_destroy_cache (META_CACHE (sbi));@@ -5285,6 +5283,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_init_fsync_node_info (sbi); +f2fs_init_compress_cache_context (sbi); +/* setup checkpoint request control and start checkpoint issue thread */ f2fs_init_ckpt_req_control (sbi); if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&@@ -5363,13 +5363,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) goto free_ino_entry; } - err =f2fs_init_compress_inode (sbi); -if (err) - goto free_root_inode; - err =f2fs_register_sysfs (sbi);if (err) - goto free_compress_inode; + goto free_root_inode; sbi->umount_lock_holder = current;#ifdef CONFIG_QUOTA @@ -5537,8 +5533,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* evict some inodes being cached by GC */ evict_inodes (sb);f2fs_unregister_sysfs (sbi); -free_compress_inode: -f2fs_destroy_compress_inode (sbi); free_root_inode:dput (sb->s_root); sb->s_root =NULL ;@@ -5655,7 +5649,8 @@ static void kill_f2fs_super(struct super_block *sb)* compress inode cache. */ if (test_opt (sbi,COMPRESS_CACHE )) -truncate_inode_pages_final (COMPRESS_MAPPING (sbi)); +f2fs_invalidate_compress_pages_range (sbi, +0 ,UINT_MAX );#endif if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||include/linux/f2fs_fs.h +0
-2@@ -34,7 +34,6 @@#define F2FS_RESERVED_NODE_NUM 3#define F2FS_ROOT_INO (sbi) ((sbi)->root_ino_num) -#define F2FS_COMPRESS_INO (sbi) (NM_I(sbi)->max_nid)#define F2FS_MAX_QUOTAS 3 --2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Re: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 219 lines · Chao Yu
Quoted 217 lines · Daeho Jeong
Quoted 27 lines · On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel…
This patch migrates compressed cluster caching from the fake VFS inode page cache (
sbi->compress_inode) to compress cache (sbi->compress_blocks).It converts compression caching and decompression paths to use compress cache APIs, uses
entry->inofor per-inode invalidation, and removessbi->compress_inode.Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/cache.c | 14 ++-- fs/f2fs/cache.h | 1 + fs/f2fs/compress.c | 158 ++++++++++++++++++---------------------- fs/f2fs/debug.c | 9 ++- fs/f2fs/f2fs.h | 8 +- fs/f2fs/inode.c | 49 +------------ fs/f2fs/node.c | 2 +- fs/f2fs/shrinker.c | 3 +- fs/f2fs/super.c | 15 ++-- include/linux/f2fs_fs.h | 1 - 10 files changed, 98 insertions(+), 162 deletions(-)fs/f2fs/cache.c +0
-0fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 6 lines
fs/f2fs/cache.c +1
-1@@ -417,13 +417,12 @@ static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,spin_unlock (&cache->list_lock); } -static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Quoted 152 lines
fs/f2fs/cache.c +7
-5{ -f2fs_lock_cache (entry); -if (entry->cache) -f2fs_do_truncate_cache (entry, drop_dirty); -f2fs_unlock_cache (entry); +if (!entry->cache) +return ; +f2fs_do_truncate_cache (entry, drop_dirty); } static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,@@ -435,8 +434,9 @@ static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,if (IS_ERR (entry))return ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); -f2fs_put_cache (entry, false); +f2fs_put_cache (entry, true); } void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,@@ -478,7 +478,9 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache, index = entry->index +1 ; +f2fs_lock_cache (entry);f2fs_truncate_cache (entry, drop_dirty); +f2fs_unlock_cache (entry); }f2fs_cache_gang_release (entries, nr);fs/f2fs/cache.h +1
-0@@ -168,6 +168,7 @@ unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache, unsigned int max_items, int tag);void f2fs_cache_gang_release (struct f2fs_cached_block **entries,unsigned int nr_entries); +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty);int f2fs_writeback_cache (struct f2fs_cached_block_list *cache,bool sync);void f2fs_cache_wait_on_all_writeback (struct f2fs_cached_block_list *cache); void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,fs/f2fs/compress.c +22
-39@@ -1918,30 +1918,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,return compressed ? i -1 : i; } -const struct address_space_operations f2fs_compress_aops = { - .release_folio = f2fs_release_folio, - .invalidate_folio = f2fs_invalidate_folio, - .migrate_folio = filemap_migrate_folio, -}; - -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi) -{ -return sbi->compress_inode->i_mapping; -} -void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len) { -if (!sbi->compress_inode) +if (!test_opt (sbi,COMPRESS_CACHE ))return ; -invalidate_mapping_pages (COMPRESS_MAPPING (sbi), blkaddr, blkaddr + len -1 ); + +f2fs_drop_cache_range (COMPRESS_CACHE (sbi), blkaddr, len, false); }static void f2fs_cache_compressed_page (struct f2fs_sb_info *sbi,struct folio *folio,nid_t ino,block_t blkaddr) { -struct folio *cfolio; -int ret; +struct f2fs_cached_block *entry;if (!test_opt (sbi,COMPRESS_CACHE ))return ;@@ -1952,49 +1941,43 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,if (!f2fs_available_free_memory (sbi,COMPRESS_PAGE ))return ; - cfolio =filemap_get_folio (COMPRESS_MAPPING (sbi), blkaddr); -if (!IS_ERR (cfolio)) { -f2fs_folio_put (cfolio, false); + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_put_cache (entry, false);return ; } - cfolio =filemap_alloc_folio (__GFP_NOWARN | __GFP_IO,0 ,NULL ); -if (!cfolio) -return ; - - ret =filemap_add_folio (COMPRESS_MAPPING (sbi), cfolio, - blkaddr,GFP_NOFS ); -if (ret) { -f2fs_folio_put (cfolio, false); + entry =f2fs_grab_cache (COMPRESS_CACHE (sbi), blkaddr, +F2FS_CACHE_LOCK_CREATE ); +if (IS_ERR (entry))return ; - } -folio_set_f2fs_data (cfolio, ino); - -memcpy (folio_address (cfolio),folio_address (folio),PAGE_SIZE ); -folio_mark_uptodate (cfolio); -f2fs_folio_put (cfolio, true); + entry->ino = ino; +memcpy (cache_address (entry),folio_address (folio),PAGE_SIZE ); +f2fs_cache_set_uptodate (entry); +f2fs_put_cache (entry, true); }bool f2fs_load_compressed_folio (struct f2fs_sb_info *sbi,struct folio *folio,block_t blkaddr) { -struct folio *cfolio; +struct f2fs_cached_block *entry;bool hitted = false;if (!test_opt (sbi,COMPRESS_CACHE ))return false; - cfolio =f2fs_filemap_get_folio (COMPRESS_MAPPING (sbi), - blkaddr,FGP_LOCK |FGP_NOWAIT ,GFP_NOFS ); -if (!IS_ERR (cfolio)) { -if (folio_test_uptodate (cfolio)) { + entry =f2fs_find_cache (COMPRESS_CACHE (sbi), blkaddr); +if (!IS_ERR (entry)) { +f2fs_lock_cache (entry); +if (f2fs_is_compress_cache (entry) && +f2fs_cache_test_uptodate (entry)) {atomic_inc (&sbi->compress_page_hit);memcpy (folio_address (folio), -folio_address (cfolio),folio_size (folio)); +cache_address (entry),folio_size (folio)); hitted = true; } -f2fs_folio_put (cfolio, true); +f2fs_put_cache (entry, true); }return hitted;fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 1986
Quoted 2 lines
fs/f2fs/compress.c +0
-0@@ -2002,71 +1985,70 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,void f2fs_invalidate_compress_pages (struct f2fs_sb_info *sbi,nid_t ino)This is O(N) Full-tree scan on inode eviction.
WDYT to improve it?
- Per-inode tracking
- Re-evaluate eviction policy: Since compress cache blocks are
indexed by physical block address (blkaddr) and hold immutable disk data, the cached blocks remain valid even after the inode is closed/evicted.
We should evaluate whether we actually need to synchronously purge
clean compress cache entries upon inode eviction, or if we can simply let the memory shrinker reclaim them via LRU under memory pressure.Quoted 12 lines
fs/f2fs/compress.c +6
-4{ -struct address_space *mapping =COMPRESS_MAPPING (sbi); -struct folio_batch fbatch; -pgoff_t index =0 ; -pgoff_t end =MAX_BLKADDR (sbi); +struct f2fs_cached_block_list *cache =COMPRESS_CACHE (sbi); +struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES ]; +unsigned long flags; +pgoff_t index =0 , end =ULONG_MAX ; +int nr; +int i;
Quoted 5 lines
Quoted 3 lines
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2010
Quoted 1 line
fs/f2fs/compress.c +0
-1-if (!mapping->nrpages)Why did you remove this line?
Oh, I see your point, we need to check
cache->num_entrieshere.
That's right.
Thanks,
Quoted 394 lines
Thanks,
Quoted 390 lines
Quoted 43 lines
fs/f2fs/compress.c +23
-15+if (!test_opt (sbi,COMPRESS_CACHE ))return ; - -folio_batch_init (&fbatch); - - do { - unsigned int nr, i; - - nr = filemap_get_folios(mapping, &index, end - 1, &fbatch); - if (!nr) +next: +spin_lock_irqsave (&cache->tree_lock, flags); + nr =radix_tree_gang_lookup (&cache->root, (void **)entries, index, +min ((unsigned long )F2FS_ONSTACK_CACHES , end - index)); +if (!nr) + goto out_unlock; + +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; + +if (entry->index >= end) { + nr = i;break ; + } +f2fs_cache_get (entry); + } +out_unlock: +spin_unlock_irqrestore (&cache->tree_lock, flags); +if (!nr) +return ; +for (i =0 ; i < nr; i++) { +struct f2fs_cached_block *entry = entries[i]; - for (i = 0; i < nr; i++) { - struct folio *folio = fbatch.folios[i]; + index = entry->index +1 ; - folio_lock(folio); - if (folio->mapping != mapping) { - folio_unlock(folio); - continue; - }fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 10 lines
fs/f2fs/compress.c +2
-7+f2fs_lock_cache (entry); -if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {How is this possible?
or if (!entry->cache)?Thanks,
Quoted 331 lines
fs/f2fs/compress.c +17
-22+f2fs_unlock_cache (entry); +continue ; } -folio_batch_release (&fbatch); +if (entry->ino != ino) { +f2fs_unlock_cache (entry); +continue ; + } + +f2fs_truncate_cache (entry, false); +f2fs_unlock_cache (entry); + } +f2fs_cache_gang_release (entries, nr); + +if (index < end) {cond_resched (); - }while (index < end); + goto next; + } } -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { -struct inode *inode; -if (!test_opt (sbi,COMPRESS_CACHE )) -return 0 ; - - inode =f2fs_iget (sbi->sb,F2FS_COMPRESS_INO (sbi)); -if (IS_ERR (inode)) -return PTR_ERR (inode); - sbi->compress_inode = inode; +return ; sbi->compress_percent =COMPRESS_PERCENT ; sbi->compress_watermark =COMPRESS_WATERMARK ; -atomic_set (&sbi->compress_page_hit,0 ); - -return 0 ; -} - -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) -{ -if (!sbi->compress_inode) -return ; -iput (sbi->compress_inode); - sbi->compress_inode =NULL ; }int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi)fs/f2fs/debug.c +5
-4@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi) si->node_caches =NODE_CACHE (sbi)->num_entries; si->meta_caches =META_CACHE (sbi)->num_entries;#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { - si->compress_pages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { + si->compress_pages =COMPRESS_CACHE (sbi)->num_entries; si->compress_page_hit =atomic_read (&sbi->compress_page_hit); }#endif @@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi) si->page_mem += (unsigned long long )NODE_CACHE (sbi)->num_entries <<PAGE_SHIFT ; si->cache_mem +=NODE_CACHE (sbi)->num_entries *sizeof (struct f2fs_cached_block );#ifdef CONFIG_F2FS_FS_COMPRESSION -if (sbi->compress_inode) { -unsigned long npages =COMPRESS_MAPPING (sbi)->nrpages; +if (test_opt (sbi,COMPRESS_CACHE )) { +unsigned long npages =COMPRESS_CACHE (sbi)->num_entries; si->page_mem += (unsigned long long )npages <<PAGE_SHIFT ; + si->cache_mem += npages *sizeof (struct f2fs_cached_block ); }#endif }fs/f2fs/f2fs.h +2
-6@@ -2078,7 +2078,6 @@ struct f2fs_sb_info {u32 compr_new_inode;/* For compressed block cache */ -struct inode *compress_inode;/* cache compressed blocks */ unsigned int compress_percent;/* cache page percentage */ unsigned int compress_watermark;/* cache page watermark */ atomic_t compress_page_hit;/* cache hit count */ @@ -4821,13 +4820,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,int f2fs_init_compress_ctx (struct compress_ctx *cc);void f2fs_destroy_compress_ctx (struct compress_ctx *cc,bool reuse);void f2fs_init_compress_info (struct f2fs_sb_info *sbi); -int f2fs_init_compress_inode (struct f2fs_sb_info *sbi); -void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi); +void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi);int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi);void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi);int __init f2fs_init_compress_cache (void );void f2fs_destroy_compress_cache (void ); -struct address_space *COMPRESS_MAPPING (struct f2fs_sb_info *sbi);void f2fs_invalidate_compress_pages_range (struct f2fs_sb_info *sbi,block_t blkaddr,unsigned int len); bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,@@ -4876,8 +4873,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)static inline unsigned int f2fs_cluster_blocks_are_contiguous (struct dnode_of_data *dn,unsigned int ofs_in_node) {return 0 ; }static inline bool f2fs_sanity_check_cluster (struct dnode_of_data *dn) {return false; } -static inline int f2fs_init_compress_inode (struct f2fs_sb_info *sbi) {return 0 ; } -static inline void f2fs_destroy_compress_inode (struct f2fs_sb_info *sbi) { } +static inline void f2fs_init_compress_cache_context (struct f2fs_sb_info *sbi) { }static inline int f2fs_init_page_array_cache (struct f2fs_sb_info *sbi) {return 0 ; }static inline void f2fs_destroy_page_array_cache (struct f2fs_sb_info *sbi) { }static inline int __init f2fs_init_compress_cache (void ) {return 0 ; }fs/f2fs/inode.c +4
-45@@ -573,15 +573,6 @@ static int do_read_inode(struct inode *inode)return 0 ; } -static bool is_meta_ino (struct f2fs_sb_info *sbi,unsigned int ino) -{ -#ifdef CONFIG_F2FS_FS_COMPRESSION -if (test_opt (sbi,COMPRESS_CACHE ) && ino ==F2FS_COMPRESS_INO (sbi)) -return true; -#endif -return false; -} -struct inode *f2fs_iget (struct super_block *sb,unsigned long ino) {struct f2fs_sb_info *sbi =F2FS_SB (sb);@@ -593,42 +584,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return ERR_PTR (-ENOMEM );if (!(inode_state_read_once (inode) &I_NEW )) { -if (is_meta_ino (sbi, ino)) { -f2fs_err (sbi,"inaccessible inode: %lu, run fsck to repair" , ino); -set_sbi_flag (sbi,SBI_NEED_FSCK ); - ret = -EFSCORRUPTED ; -trace_f2fs_iget_exit (inode, ret); -iput (inode); -f2fs_handle_error (sbi,ERROR_CORRUPTED_INODE ); -fserror_report_file_metadata (inode, ret,GFP_NOFS ); -return ERR_PTR (ret); - } -trace_f2fs_iget (inode);return inode; } -if (is_meta_ino (sbi, ino)) - goto make_now; - ret =do_read_inode (inode);if (ret) goto bad_inode; -make_now: +f2fs_set_inode_flags (inode); -if (ino ==F2FS_COMPRESS_INO (sbi)) { -#ifdef CONFIG_F2FS_FS_COMPRESSION - inode->i_mapping->a_ops = &f2fs_compress_aops; -/* -* generic_error_remove_folio only truncates pages of regular -* inode -*/ - inode->i_mode |=S_IFREG ; -#endif -mapping_set_gfp_mask (inode->i_mapping, -GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); - }else if (S_ISREG (inode->i_mode)) { +if (S_ISREG (inode->i_mode)) { inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops;@@ -879,7 +845,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)/* * Return true, if we shouldn't go through post_evict_inode. */ -static bool f2fs_pre_evict_inode (struct inode *inode) +static void f2fs_pre_evict_inode (struct inode *inode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode);@@ -905,17 +871,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -if (inode->i_ino ==F2FS_COMPRESS_INO (sbi)) -return true; -f2fs_bug_on (sbi,get_dirty_pages (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);if (!IS_DEVICE_ALIASING (inode))f2fs_destroy_extent_tree (inode); - -return false; }static void f2fs_delete_inode (struct inode *inode)@@ -1077,15 +1038,13 @@ static void f2fs_post_evict_inode(struct inode *inode)*/ void f2fs_evict_inode (struct inode *inode) { -if (f2fs_pre_evict_inode (inode)) - goto clear_out; +f2fs_pre_evict_inode (inode);if (!inode->i_nlink && !is_bad_inode (inode))f2fs_delete_inode (inode);f2fs_post_evict_inode (inode); -clear_out:fscrypt_put_encryption_info (inode);clear_inode (inode); }fs/f2fs/node.c +1
-1@@ -119,7 +119,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)* exceed threshold, deny caching compress page. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_MAPPING (sbi)->nrpages < + (COMPRESS_CACHE (sbi)->num_entries < free_ram * sbi->compress_percent /100 );#else res = false;fs/f2fs/shrinker.c +2
-1@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,static unsigned long __count_cache (struct f2fs_sb_info *sbi) {return sbi->meta_blocks.num_entries + - sbi->node_blocks.num_entries; + sbi->node_blocks.num_entries + + sbi->compress_blocks.num_entries; } unsigned long f2fs_shrink_count(struct shrinker *shrink,fs/f2fs/super.c +5
-10@@ -2050,8 +2050,6 @@ static void f2fs_put_super(struct super_block *sb)f2fs_bug_on (sbi, sbi->fsync_node_num); -f2fs_destroy_compress_inode (sbi); -f2fs_destroy_cache (COMPRESS_CACHE (sbi));f2fs_destroy_cache (NODE_CACHE (sbi));f2fs_destroy_cache (META_CACHE (sbi));@@ -5285,6 +5283,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)f2fs_init_fsync_node_info (sbi); +f2fs_init_compress_cache_context (sbi); +/* setup checkpoint request control and start checkpoint issue thread */ f2fs_init_ckpt_req_control (sbi); if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&@@ -5363,13 +5363,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) goto free_ino_entry; } - err =f2fs_init_compress_inode (sbi); -if (err) - goto free_root_inode; - err =f2fs_register_sysfs (sbi);if (err) - goto free_compress_inode; + goto free_root_inode; sbi->umount_lock_holder = current;#ifdef CONFIG_QUOTA @@ -5537,8 +5533,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)/* evict some inodes being cached by GC */ evict_inodes (sb);f2fs_unregister_sysfs (sbi); -free_compress_inode: -f2fs_destroy_compress_inode (sbi); free_root_inode:dput (sb->s_root); sb->s_root =NULL ;@@ -5655,7 +5649,8 @@ static void kill_f2fs_super(struct super_block *sb)* compress inode cache. */ if (test_opt (sbi,COMPRESS_CACHE )) -truncate_inode_pages_final (COMPRESS_MAPPING (sbi)); +f2fs_invalidate_compress_pages_range (sbi, +0 ,UINT_MAX );#endif if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||include/linux/f2fs_fs.h +0
-2@@ -34,7 +34,6 @@#define F2FS_RESERVED_NODE_NUM 3#define F2FS_ROOT_INO (sbi) ((sbi)->root_ino_num) -#define F2FS_COMPRESS_INO (sbi) (NM_I(sbi)->max_nid)#define F2FS_MAX_QUOTAS 3 --2.49.0
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
In reply to Daeho JeongQuoted 5 lines · Daeho Jeong
fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 3 lines
fs/f2fs/cache.c +1
-1-static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?
Or, I guess you mean we need a cleanup here?
What about:
void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty)
{
if (!entry->cache)
return;
f2fs_do_truncate_cache(entry, drop_dirty);
}static void f2fs_truncate_cache_locked(struct f2fs_cached_block *entry, bool drop_dirty)
{
f2fs_lock_cache(entry);
f2fs_truncate_cache(entry, drop_dirty);
f2fs_unlock_cache(entry);
}f2fs_truncate_cache() was exported for using in other .c file, in the meantime, static f2fs_truncate_cache_locked() can be used inside cache.c.
Thanks,
Re: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 19 lines · Chao Yu
Quoted 5 lines · Daeho Jeong
fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 3 lines
fs/f2fs/cache.c +1
-1-static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Or, I guess you mean we need a cleanup here?
What about:
void
f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty){ if (!entry->cache) return; f2fs_do_truncate_cache(entry, drop_dirty); }static void
f2fs_truncate_cache_locked(struct f2fs_cached_block *entry, bool drop_dirty)
Quoted 8 lines
{ f2fs_lock_cache(entry); f2fs_truncate_cache(entry, drop_dirty); f2fs_unlock_cache(entry); }
f2fs_truncate_cache()was exported for using in other .c file, in the meantime, staticf2fs_truncate_cache_locked()can be used insidecache.c.
Splitting them into two helpers (one with locking, one without) makes total sense.
However, in the Linux kernel convention, having _locked or __ usually indicates that the caller is expected to ALREADY hold the lock (e.g. __list_add(), __folio_mark_dirty()).
If an exported function is named f2fs_truncate_cache() without __, callers would naturally assume it handles locking internally.
So I'd suggest following the standard kernel pattern:
/* Caller must hold f2fs_lock_cache(entry) */
void __f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty) {
if (!entry->cache)
return;f2fs_do_truncate_cache(entry, drop_dirty);
}
/* Safe wrapper that acquires entry lock internally */
void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty) {f2fs_lock_cache(entry);__f2fs_truncate_cache(entry, drop_dirty); f2fs_unlock_cache(entry);
}
Then compress.c (which already holds the lock to check entry->ino) can call __f2fs_truncate_cache(), while other callers can safely use f2fs_truncate_cache().
What do you think?
Thanks,
Quoted 2 lines
Thanks,
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 57 lines · Daeho Jeong
Quoted 19 lines · Chao Yu
Quoted 5 lines · Daeho Jeong
fs/f2fs/cache.c static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry, · line 421
Quoted 3 lines
fs/f2fs/cache.c +1
-1-static void f2fs_truncate_cache (struct f2fs_cached_block *entry, +void f2fs_truncate_cache (struct f2fs_cached_block *entry,bool drop_dirty)->
__f2fs_truncate_cache()without locking?Or, I guess you mean we need a cleanup here?
What about:
void
f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty){ if (!entry->cache) return; f2fs_do_truncate_cache(entry, drop_dirty); }static void
f2fs_truncate_cache_locked(struct f2fs_cached_block *entry, bool drop_dirty)Quoted 8 lines
{ f2fs_lock_cache(entry); f2fs_truncate_cache(entry, drop_dirty); f2fs_unlock_cache(entry); }
f2fs_truncate_cache()was exported for using in other .c file, in the meantime, staticf2fs_truncate_cache_locked()can be used insidecache.c.Splitting them into two helpers (one with locking, one without) makes total sense.
However, in the Linux kernel convention, having
_lockedor__usually indicates that the caller is expected to ALREADY hold the lock (e.g.__list_add(),__folio_mark_dirty()).If an exported function is named
f2fs_truncate_cache()without__, callers would naturally assume it handles locking internally.So I'd suggest following the standard kernel pattern:
/* Caller must hold
f2fs_lock_cache(entry)*/
void__f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty){
if (!entry->cache)
return;f2fs_do_truncate_cache(entry, drop_dirty);
}/* Safe wrapper that acquires entry lock internally */
voidf2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty){f2fs_lock_cache(entry);__f2fs_truncate_cache(entry, drop_dirty);f2fs_unlock_cache(entry);
}
Quoted 3 lines
Then
compress.c(which already holds the lock to checkentry->ino) can call__f2fs_truncate_cache(), while other callers can safely usef2fs_truncate_cache().
Okay, I see.
My concern is previously we don't export __f2fs functions in *.h, see f2fs.h, but since it has "f2fs" keyword in prefix, so it won't pollute global name space, so I guess it's fine, anyway, let me update.
Thanks,
Quoted 7 lines
What do you think?
Thanks,
Quoted 2 lines
Thanks,
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
In reply to Daeho JeongQuoted 12 lines · Daeho Jeong
On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel
fs/f2fs/compress.c bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, · line 2024
Quoted 10 lines
fs/f2fs/compress.c +2
-7+f2fs_lock_cache (entry); -if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {
Quoted 2 lines
How is this possible?
or if (!entry->cache)?
I suffer a panic from f2fs_bug_on(sbi, !f2fs_is_compress_cache(entry)) here, it may suffer race condition w/ shrinker.
Thanks,
Chao Yu
maintainerRe: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 13 lines · Chao Yu
Quoted 12 lines · Daeho Jeong
On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel
Quoted 10 lines
+
f2fs_lock_cache(entry);- if (ino !=
folio_get_f2fs_data(folio)) { -folio_unlock(folio);
- continue;
- }
-
-generic_error_remove_folio(mapping, folio); -folio_unlock(folio);
+ if (!f2fs_is_compress_cache(entry)) {
Quoted 5 lines
Quoted 2 lines
How is this possible?
or if (!entry->cache)?I suffer a panic from
f2fs_bug_on(sbi, !f2fs_is_compress_cache(entry))here, it may suffer race condition w/ shrinker.
I changed like this, because:
1. we will never update entry->ino after initialization, so it's safe to check w/o lock.
2. f2fs_truncate_cache() has checked entry->cache by itself, so it doesn't need to check w/ f2fs_is_compress_cache() outside __f2fs_truncate_cache().
for (i = 0; i < nr; i++) {
struct f2fs_cached_block *entry = entries[i];
index = entry->index + 1;
if (entry->ino != ino)
continue;
f2fs_truncate_cache(entry, false);
}Let me know if you have any concern about this.
Thanks,
Re: [f2fs-dev] [PATCH v3 09/12] f2fs: cache: use compress cache
Quoted 38 lines · Chao Yu
Quoted 13 lines · Chao Yu
Quoted 12 lines · Daeho Jeong
On Tue, Aug 25, 2026 at 6:03 AM Chao Yu via Linux-f2fs-devel
Quoted 10 lines
fs/f2fs/compress.c +2
-7+f2fs_lock_cache (entry); -if (ino !=folio_get_f2fs_data (folio)) { -folio_unlock (folio); -continue ; - } - -generic_error_remove_folio (mapping, folio); -folio_unlock (folio); +if (!f2fs_is_compress_cache (entry)) {Quoted 5 lines
Quoted 2 lines
How is this possible?
or if (!entry->cache)?I suffer a panic from
f2fs_bug_on(sbi, !f2fs_is_compress_cache(entry))here, it may suffer race condition w/ shrinker.I changed like this, because:
1. we will never updateentry->inoafter initialization, so it's safe to check w/o lock.
2.f2fs_truncate_cache()has checkedentry->cacheby itself, so it doesn't need to check w/f2fs_is_compress_cache()outside__f2fs_truncate_cache().for (i = 0; i < nr; i++) { struct f2fs_cached_block *entry = entries[i]; index = entry->index + 1; if (entry->ino != ino) continue; f2fs_truncate_cache(entry, false); }
Quoted 1 line
Let me know if you have any concern about this.
Hi Chao,
This looks much cleaner and straightforward.
I have no further concerns on this part.
Thanks,
Quoted 3 lines
Thanks,
Chao Yu
maintainerPATCH v3 10/12 f2fs: cache: support fault injection
In reply to Chao YuThis patch adds fault injection support for metadata cache allocations to improve error-path test coverage.
- it integrates entry allocations with FAULT_KALLOC
--- fs/f2fs/cache.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
fs/f2fs/cache.c +14 -10
@@ -176,20 +176,24 @@ static struct f2fs_cached_block *f2fs_create_cache(struct f2fs_cached_block_list *cache, unsigned long index, bool nofail) { + struct f2fs_sb_info *sbi = cache->sbi;struct f2fs_cached_block *entry;unsigned int flags =GFP_NOFS ; -if (nofail) +if (nofail) { flags |= __GFP_NOFAIL; - - entry =kzalloc_obj (*entry, flags); -if (!entry) -return ERR_PTR (-ENOMEM ); - - entry->data =kmalloc (cache->sbi->blocksize, flags); -if (!entry->data) { -kfree (entry); -return ERR_PTR (-ENOMEM ); + entry =kzalloc_obj (*entry, flags); + entry->data =kmalloc (sbi->blocksize, flags); + }else { + entry =f2fs_kzalloc (sbi,sizeof (*entry), flags); +if (!entry) +return ERR_PTR (-ENOMEM ); + + entry->data =f2fs_kmalloc (sbi, sbi->blocksize, flags); +if (!entry->data) { +kfree (entry); +return ERR_PTR (-ENOMEM ); + } } entry->index = index;
Signature · 1 line
2.49.0
Chao Yu
maintainerPATCH v3 11/12 f2fs: cache: introduce tracepoints
In reply to Chao YuThis patch introduces ftrace tracepoints to observe and profile metadata cache operations:
- trace_f2fs_cache_set_dirty to trace marking a cached block dirty
- trace_f2fs_write_cache to trace single block writeback submission
- trace_f2fs_write_caches to tracesbatch writeback and sync sessions
--- fs/f2fs/cache.c | 3 ++ fs/f2fs/checkpoint.c | 6 ++++ fs/f2fs/segment.c | 3 ++ include/trace/events/f2fs.h | 71 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+)
fs/f2fs/cache.c +3 -0
@@ -15,6 +15,7 @@#include "f2fs.h" #include "cache.h" #include "node.h" +#include <trace/events/f2fs.h> #include "segment.h" void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
@@ -76,6 +77,8 @@ bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry)enum count_type type =IS_META_CACHE (cache) ?F2FS_DIRTY_META :F2FS_DIRTY_NODES ; +trace_f2fs_cache_set_dirty (entry, +IS_META_CACHE (cache) ?META :NODE );f2fs_cache_update_tag (entry,F2FS_CACHE_TAG_NONE ,F2FS_CACHE_TAG_DIRTY );inc_page_count (cache->sbi, type);
fs/f2fs/checkpoint.c +6 -0
@@ -551,6 +551,8 @@ static bool __f2fs_write_meta_cache(struct f2fs_cached_block *entry, {struct f2fs_sb_info *sbi = entry->cache->sbi; +trace_f2fs_write_cache (entry,META ); +if (unlikely (f2fs_cp_error (sbi))) {if (is_sbi_flag_set (sbi,SBI_IS_CLOSE )) {f2fs_force_clear_cache_dirty (entry);
@@ -609,6 +611,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,struct blk_plug plug;bool background = nr_to_write !=LONG_MAX ; +trace_f2fs_write_caches (sbi, nr_to_write,0 ,META ); +blk_start_plug (&plug); while ((nr = f2fs_cache_gang_lookup_tag(META_CACHE(sbi), entries,
@@ -664,6 +668,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,blk_finish_plug (&plug); +trace_f2fs_write_caches (sbi, nr_to_write, nwritten,META ); +return nwritten; }
fs/f2fs/segment.c +3 -0
@@ -4171,6 +4171,9 @@ void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio) {struct f2fs_summary sum; +if (fio->is_cache) +trace_f2fs_write_cache (fio->cache_entry,NODE ); +set_summary (&sum, nid,0 ,0 );do_write_page (&sum, fio);
include/trace/events/f2fs.h +71 -0
83 lines not on this page · raw message
Signature · 1 line
2.49.0
Chao Yu
maintainerPATCH v3 12/12 f2fs: cache: show per-cache usage in debugfs
In reply to Chao YuThis patch exposes per-cache memory usage (entry struct memory vs. cached block buffer memory) for meta, node, and compress caches under the memory section in debugfs:
- meta entry: xxx KB, meta cache: xxx KB
- node entry: xxx KB, node cache: xxx KB
- compress entry: xxx KB, compress cache: xxx KB
--- fs/f2fs/cache.h | 1 + fs/f2fs/debug.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- fs/f2fs/f2fs.h | 2 ++ 3 files changed, 43 insertions(+), 9 deletions(-)
fs/f2fs/cache.h +1 -0
@@ -38,6 +38,7 @@ enum f2fs_cache_type {F2FS_META_CACHE ,F2FS_NODE_CACHE ,F2FS_COMPRESS_CACHE , + NR_CACHE_TYPES, };/* Main cache control structure (per sb_info) */
fs/f2fs/debug.c +40 -9
63 lines not on this page · raw message
fs/f2fs/f2fs.h +2 -0
@@ -4457,6 +4457,8 @@ struct f2fs_stat_info {unsigned int block_count[2 ];unsigned int inplace_count;unsigned long long base_mem, cache_mem, page_mem; +unsigned long long cache_entry_mem[NR_CACHE_TYPES ]; +unsigned long long cache_data_mem[NR_CACHE_TYPES ];struct f2fs_dev_stats *dev_stats; };
Signature · 1 line
2.49.0