* [PATCH 1/2] f2fs: refactor f2fs_evict_inode having three major parts
@ 2026-08-07 22:02 Jaegeuk Kim
2026-08-07 22:02 ` [PATCH 2/2] f2fs: call __add_ino_entry out of the eviction path Jaegeuk Kim
0 siblings, 1 reply; 2+ messages in thread
From: Jaegeuk Kim @ 2026-08-07 22:02 UTC (permalink / raw)
To: linux-kernel, linux-f2fs-devel; +Cc: Jaegeuk Kim
1. f2fs_pre_evict_inode()
: drop all in-memory structures
2. f2fs_delete_inode()
: truncate inode blocks, if it was unlinked.
3. f2fs_post_evict_inode()
: update inode records for future access
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
fs/f2fs/inode.c | 136 ++++++++++++++++++++++++++++--------------------
1 file changed, 81 insertions(+), 55 deletions(-)
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index c95e0b126da4..553b1e338aa1 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -855,15 +855,12 @@ void f2fs_remove_donate_inode(struct inode *inode)
}
/*
- * Called at the last iput() if i_nlink is zero
+ * Return true, if we shouldn't go through post_evict_inode.
*/
-void f2fs_evict_inode(struct inode *inode)
+static bool f2fs_pre_evict_inode(struct inode *inode)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct f2fs_inode_info *fi = F2FS_I(inode);
- nid_t xnid = fi->i_xattr_nid;
- int err = 0;
- bool freeze_protected = false;
f2fs_abort_atomic_write(inode, true);
@@ -883,13 +880,13 @@ void f2fs_evict_inode(struct inode *inode)
truncate_inode_pages_final(&inode->i_data);
if ((inode->i_nlink || is_bad_inode(inode)) &&
- test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))
+ test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))
f2fs_invalidate_compress_pages(sbi, inode->i_ino);
if (inode->i_ino == F2FS_NODE_INO(sbi) ||
- inode->i_ino == F2FS_META_INO(sbi) ||
- inode->i_ino == F2FS_COMPRESS_INO(sbi))
- goto out_clear;
+ inode->i_ino == F2FS_META_INO(sbi) ||
+ inode->i_ino == F2FS_COMPRESS_INO(sbi))
+ return true;
f2fs_bug_on(sbi, get_dirty_pages(inode));
f2fs_remove_dirty_inode(inode);
@@ -898,14 +895,18 @@ void f2fs_evict_inode(struct inode *inode)
if (!IS_DEVICE_ALIASING(inode))
f2fs_destroy_extent_tree(inode);
- if (inode->i_nlink || is_bad_inode(inode))
- goto no_delete;
+ return false;
+}
- err = f2fs_dquot_initialize(inode);
- if (err) {
- err = 0;
+static void f2fs_delete_inode(struct inode *inode)
+{
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ bool freeze_protected = false;
+ struct f2fs_lock_context lc;
+ int err = 0;
+
+ if (f2fs_dquot_initialize(inode))
set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
- }
f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
@@ -924,30 +925,30 @@ void f2fs_evict_inode(struct inode *inode)
if (time_to_inject(sbi, FAULT_EVICT_INODE))
err = -EIO;
- if (!err) {
- struct f2fs_lock_context lc;
-
- f2fs_lock_op(sbi, &lc);
- err = f2fs_remove_inode_page(inode);
- f2fs_unlock_op(sbi, &lc);
- if (err == -ENOENT) {
- err = 0;
-
- /*
- * in fuzzed image, another node may has the same
- * block address as inode's, if it was truncated
- * previously, truncation of inode node will fail.
- */
- if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
- f2fs_warn(F2FS_I_SB(inode),
- "f2fs_evict_inode: inconsistent node id, ino:%llu",
- inode->i_ino);
- f2fs_inode_synced(inode);
- set_sbi_flag(sbi, SBI_NEED_FSCK);
- }
+ if (err)
+ goto error_check;
+
+ f2fs_lock_op(sbi, &lc);
+ err = f2fs_remove_inode_page(inode);
+ f2fs_unlock_op(sbi, &lc);
+
+ if (err == -ENOENT) {
+ err = 0;
+
+ /*
+ * in fuzzed image, another node may has the same
+ * block address as inode's, if it was truncated
+ * previously, truncation of inode node will fail.
+ */
+ if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
+ f2fs_warn(F2FS_I_SB(inode),
+ "f2fs_evict_inode: inconsistent node id, ino:%llu",
+ inode->i_ino);
+ f2fs_inode_synced(inode);
+ set_sbi_flag(sbi, SBI_NEED_FSCK);
}
}
-
+error_check:
/* give more chances, if ENOMEM case */
if (err == -ENOMEM) {
err = 0;
@@ -957,27 +958,37 @@ void f2fs_evict_inode(struct inode *inode)
if (IS_DEVICE_ALIASING(inode))
f2fs_destroy_extent_tree(inode);
- if (err) {
- f2fs_update_inode_page(inode);
- if (dquot_initialize_needed(inode))
- set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
+ if (!err)
+ goto unfreeze_out;
- /*
- * If both f2fs_truncate() and f2fs_update_inode_page() failed
- * due to fuzzed corrupted inode, call f2fs_inode_synced() to
- * avoid triggering later f2fs_bug_on().
- */
- if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
- f2fs_warn(sbi,
- "f2fs_evict_inode: inode is dirty, ino:%llu",
- inode->i_ino);
- f2fs_inode_synced(inode);
- set_sbi_flag(sbi, SBI_NEED_FSCK);
- }
+ f2fs_update_inode_page(inode);
+
+ if (dquot_initialize_needed(inode))
+ set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
+
+ /*
+ * If both f2fs_truncate() and f2fs_update_inode_page() failed
+ * due to fuzzed corrupted inode, call f2fs_inode_synced() to
+ * avoid triggering later f2fs_bug_on().
+ */
+ if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
+ f2fs_warn(sbi,
+ "f2fs_evict_inode: inode is dirty, ino:%llu",
+ inode->i_ino);
+ f2fs_inode_synced(inode);
+ set_sbi_flag(sbi, SBI_NEED_FSCK);
}
+unfreeze_out:
if (freeze_protected)
sb_end_intwrite(inode->i_sb);
-no_delete:
+}
+
+static void f2fs_post_evict_inode(struct inode *inode)
+{
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ struct f2fs_inode_info *fi = F2FS_I(inode);
+ nid_t xnid = fi->i_xattr_nid;
+
dquot_drop(inode);
stat_dec_inline_xattr(inode);
@@ -1019,7 +1030,22 @@ void f2fs_evict_inode(struct inode *inode)
* In that case, f2fs_check_nid_range() is enough to give a clue.
*/
}
-out_clear:
+}
+
+/*
+ * Called at the last iput() if i_nlink is zero
+ */
+void f2fs_evict_inode(struct inode *inode)
+{
+ if (f2fs_pre_evict_inode(inode))
+ goto clear_out;
+
+ 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);
}
--
2.55.0.654.g21b8a5bc05-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 2/2] f2fs: call __add_ino_entry out of the eviction path
2026-08-07 22:02 [PATCH 1/2] f2fs: refactor f2fs_evict_inode having three major parts Jaegeuk Kim
@ 2026-08-07 22:02 ` Jaegeuk Kim
0 siblings, 0 replies; 2+ messages in thread
From: Jaegeuk Kim @ 2026-08-07 22:02 UTC (permalink / raw)
To: linux-kernel, linux-f2fs-devel; +Cc: Jaegeuk Kim
The f2fs_evict_inode() can be called during the direct reclaim path, but
__add_ino_entry requires allocating some memory. Since we don't need to
do that in that context, let's migrate it in other workqueue context.
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
fs/f2fs/checkpoint.c | 11 ++++++
fs/f2fs/data.c | 13 ++++++-
fs/f2fs/f2fs.h | 4 ++
fs/f2fs/inode.c | 91 +++++++++++++++++++++++++++++++++++++++++---
fs/f2fs/super.c | 9 ++++-
5 files changed, 121 insertions(+), 7 deletions(-)
diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c
index 9da51d2a7af7..e2b27fa8941f 100644
--- a/fs/f2fs/checkpoint.c
+++ b/fs/f2fs/checkpoint.c
@@ -766,6 +766,15 @@ static void __remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
spin_unlock(&im->ino_lock);
}
+static void f2fs_wait_for_inode_record(struct f2fs_sb_info *sbi, int mode)
+{
+ if (mode != APPEND_INO && mode != UPDATE_INO)
+ return;
+
+ /* Let's wait for some pending updates for APPEND_INO and UPDATE_INO. */
+ flush_workqueue(sbi->evict_wq);
+}
+
void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type)
{
struct inode_management *im = &sbi->im[type];
@@ -853,6 +862,8 @@ void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all)
for (i = all ? ORPHAN_INO : FLUSH_INO; i <= FLUSH_INO; i++) {
struct inode_management *im = &sbi->im[i];
+ f2fs_wait_for_inode_record(sbi, i);
+
spin_lock(&im->ino_lock);
list_for_each_entry_safe(e, tmp, &im->ino_list, list) {
list_del(&e->list);
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index c219ea76a3a7..6ae0eb37d20f 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -4558,13 +4558,24 @@ int f2fs_init_wq(struct f2fs_sb_info *sbi)
{
sbi->wq = alloc_workqueue("f2fs_wq", WQ_UNBOUND | WQ_HIGHPRI,
num_online_cpus());
- return sbi->wq ? 0 : -ENOMEM;
+ if (!sbi->wq)
+ return -ENOMEM;
+
+ sbi->evict_wq = alloc_workqueue("f2fs_evict_wq",
+ WQ_UNBOUND | WQ_HIGHPRI, num_online_cpus());
+ if (!sbi->evict_wq) {
+ destroy_workqueue(sbi->wq);
+ return -ENOMEM;
+ }
+ return 0;
}
void f2fs_destroy_wq(struct f2fs_sb_info *sbi)
{
if (sbi->wq)
destroy_workqueue(sbi->wq);
+ if (sbi->evict_wq)
+ destroy_workqueue(sbi->evict_wq);
}
int __init f2fs_init_bio_entry_cache(void)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 1f8c4bb7c5cb..5ae2d347c3ce 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2020,6 +2020,8 @@ struct f2fs_sb_info {
struct workqueue_struct *wq; /* bio completion workqueue */
+ struct workqueue_struct *evict_wq; /* inode eviction workqueue */
+
/*
* If we are in irq context, let's update error information into
* on-disk superblock in the work.
@@ -3885,6 +3887,8 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
void f2fs_remove_donate_inode(struct inode *inode);
void f2fs_evict_inode(struct inode *inode);
void f2fs_handle_failed_inode(struct inode *inode, struct f2fs_lock_context *lc);
+int f2fs_init_evict_inode_work(void);
+void f2fs_destroy_evict_inode_work(void);
/*
* namei.c
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 553b1e338aa1..ca812d7d2bee 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -24,6 +24,18 @@
extern const struct address_space_operations f2fs_compress_aops;
#endif
+#define NUM_PREALLOC_EVICT_INODE_WORK 8
+
+static struct kmem_cache *evict_inode_work_cache;
+static mempool_t *evict_inode_work_pool;
+
+struct evict_inode_work {
+ struct work_struct work;
+ struct f2fs_sb_info *sbi;
+ nid_t ino;
+ unsigned int add_ino_entry_bits;
+};
+
void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
{
if (is_inode_flag_set(inode, FI_NEW_INODE))
@@ -637,6 +649,9 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
inode->i_fop = &f2fs_dir_operations;
inode->i_mapping->a_ops = &f2fs_dblock_aops;
mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
+
+ /* Let's prepare APPEND/UPDATE_INO before future access. */
+ flush_workqueue(sbi->evict_wq);
} else if (S_ISLNK(inode->i_mode)) {
if (file_is_encrypt(inode))
inode->i_op = &f2fs_encrypted_symlink_inode_operations;
@@ -854,6 +869,25 @@ void f2fs_remove_donate_inode(struct inode *inode)
spin_unlock(&sbi->inode_lock[DONATE_INODE]);
}
+static void f2fs_record_inode_state(struct f2fs_sb_info *sbi, nid_t ino,
+ unsigned int bits)
+{
+ if (bits & BIT(APPEND_INO))
+ f2fs_add_ino_entry(sbi, ino, APPEND_INO);
+ if (bits & BIT(UPDATE_INO))
+ f2fs_add_ino_entry(sbi, ino, UPDATE_INO);
+}
+
+static void f2fs_evict_inode_work(struct work_struct *work)
+{
+ struct evict_inode_work *ew =
+ container_of(work, struct evict_inode_work, work);
+
+ f2fs_record_inode_state(ew->sbi, ew->ino, ew->add_ino_entry_bits);
+
+ mempool_free(ew, evict_inode_work_pool);
+}
+
/*
* Return true, if we shouldn't go through post_evict_inode.
*/
@@ -988,6 +1022,7 @@ static void f2fs_post_evict_inode(struct inode *inode)
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct f2fs_inode_info *fi = F2FS_I(inode);
nid_t xnid = fi->i_xattr_nid;
+ unsigned int record_bits = 0;
dquot_drop(inode);
@@ -1014,12 +1049,32 @@ static void f2fs_post_evict_inode(struct inode *inode)
inode->i_ino);
if (xnid)
invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
- if (inode->i_nlink) {
- if (is_inode_flag_set(inode, FI_APPEND_WRITE))
- f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
- if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
- f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
+
+ if (!inode->i_nlink)
+ goto skip_record;
+
+ if (is_inode_flag_set(inode, FI_APPEND_WRITE))
+ record_bits = BIT(APPEND_INO);
+ if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
+ record_bits = BIT(UPDATE_INO);
+
+ if (!record_bits)
+ goto skip_record;
+
+ /* Let's do this in workqueue out of the direct reclaim path. */
+ if (current_is_kswapd()) {
+ f2fs_record_inode_state(sbi, inode->i_ino, record_bits);
+ } else {
+ struct evict_inode_work *ew =
+ mempool_alloc(evict_inode_work_pool, GFP_NOFS);
+
+ ew->sbi = sbi;
+ ew->ino = inode->i_ino;
+ ew->add_ino_entry_bits = record_bits;
+ INIT_WORK(&ew->work, f2fs_evict_inode_work);
+ queue_work(sbi->evict_wq, &ew->work);
}
+skip_record:
if (is_inode_flag_set(inode, FI_FREE_NID)) {
f2fs_alloc_nid_failed(sbi, inode->i_ino);
clear_inode_flag(inode, FI_FREE_NID);
@@ -1105,3 +1160,29 @@ void f2fs_handle_failed_inode(struct inode *inode, struct f2fs_lock_context *lc)
/* iput will drop the inode object */
iput(inode);
}
+
+int __init f2fs_init_evict_inode_work(void)
+{
+ evict_inode_work_cache =
+ kmem_cache_create("f2fs_evict_inode_work",
+ sizeof(struct evict_inode_work), 0, 0, NULL);
+ if (!evict_inode_work_cache)
+ goto fail;
+ evict_inode_work_pool =
+ mempool_create_slab_pool(NUM_PREALLOC_EVICT_INODE_WORK,
+ evict_inode_work_cache);
+ if (!evict_inode_work_pool)
+ goto fail_free_cache;
+ return 0;
+
+fail_free_cache:
+ kmem_cache_destroy(evict_inode_work_cache);
+fail:
+ return -ENOMEM;
+}
+
+void f2fs_destroy_evict_inode_work(void)
+{
+ mempool_destroy(evict_inode_work_pool);
+ kmem_cache_destroy(evict_inode_work_cache);
+}
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index f8f4a7bac1ab..8feae93d35b4 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -5764,10 +5764,16 @@ static int __init init_f2fs_fs(void)
err = f2fs_init_xattr_cache();
if (err)
goto free_casefold_cache;
- err = register_filesystem(&f2fs_fs_type);
+ err = f2fs_init_evict_inode_work();
if (err)
goto free_xattr_cache;
+ err = register_filesystem(&f2fs_fs_type);
+ if (err)
+ goto free_evict_inode_cache;
return 0;
+
+free_evict_inode_cache:
+ f2fs_destroy_evict_inode_work();
free_xattr_cache:
f2fs_destroy_xattr_cache();
free_casefold_cache:
@@ -5810,6 +5816,7 @@ static int __init init_f2fs_fs(void)
static void __exit exit_f2fs_fs(void)
{
unregister_filesystem(&f2fs_fs_type);
+ f2fs_destroy_evict_inode_work();
f2fs_destroy_xattr_cache();
f2fs_destroy_casefold_cache();
f2fs_destroy_compress_cache();
--
2.55.0.654.g21b8a5bc05-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-08-07 22:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-07 22:02 [PATCH 1/2] f2fs: refactor f2fs_evict_inode having three major parts Jaegeuk Kim
2026-08-07 22:02 ` [PATCH 2/2] f2fs: call __add_ino_entry out of the eviction path Jaegeuk Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox