[PATCH v4 10/12] f2fs: cache: support fault injection

From: Chao Yu

Date: Thu Aug 27 2026 - 08:29:14 EST


From: Chao Yu <chao@xxxxxxxxxx>

This patch adds fault injection support for metadata cache allocations
to improve error-path test coverage.
- it integrates entry allocations with FAULT_KALLOC

Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
---
fs/f2fs/cache.c | 24 ++++++++++++++----------
1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/fs/f2fs/cache.c b/fs/f2fs/cache.c
index 058e62c44152..2a98479bb3db 100644
--- a/fs/f2fs/cache.c
+++ b/fs/f2fs/cache.c
@@ -176,23 +176,27 @@ 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 (index == ULONG_MAX)
return ERR_PTR(-ERANGE);

- 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;
--
2.49.0