Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache()
@ 2026-11-27  6:49 jiazhenyuan
  2026-11-27  6:49 ` [PATCH 2/2] ext4: add KUnit test for ext4_ext_precache() buffer head leak jiazhenyuan
  2026-11-27  6:58 ` [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: jiazhenyuan @ 2026-11-27  6:49 UTC (permalink / raw)
  To: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang, linux-ext4
  Cc: linux-kernel, gouhao, niecheng1, guanwentao, caina, zhaoshuang,
	wangqing

ext4_ext_precache() stores each successfully read child extent block in
path[1..depth].p_bh. During normal traversal, these references are
released as the loop backtracks. If a later read fails, traversal breaks
to out and calls ext4_free_ext_path() before all ancestor references
have been dropped.

path[0].p_depth is not initialized, so ext4_ext_drop_refs() only
releases path[0].p_bh. Since the root block is inline and path[0].p_bh
is NULL, buffer heads held by path[1..depth] are leaked.

Set path[0].p_depth to the inode's extent depth so cleanup covers the
entire traversal stack.

Without this patch, the KUnit test added in the following patch reports:

    # test_ext_precache_read_failure_bh_leak: EXPECTATION FAILED at
    # fs/ext4/extents-test.c:1568
    Expected atomic_read(&bh->b_count) == 2, but
        atomic_read(&bh->b_count) == 3 (0x3)

Fixes: 7869a4a6c5caa ("ext4: add support for extent pre-caching")
Cc: stable@vger.kernel.org
Signed-off-by: jiazhenyuan <jiazhenyuan@uniontech.com>
---
 fs/ext4/extents.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index b65b0261ee5c..4c775aa41459 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -638,6 +638,8 @@ int ext4_ext_precache(struct inode *inode)
 		return -ENOMEM;
 	}
 
+	path[0].p_depth = depth;
+
 	path[0].p_hdr = ext_inode_hdr(inode);
 	ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
 	if (ret)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] ext4: add KUnit test for ext4_ext_precache() buffer head leak
  2026-11-27  6:49 [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() jiazhenyuan
@ 2026-11-27  6:49 ` jiazhenyuan
  2026-11-27  6:58 ` [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: jiazhenyuan @ 2026-11-27  6:49 UTC (permalink / raw)
  To: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang, linux-ext4
  Cc: linux-kernel, gouhao, niecheng1, guanwentao, caina, zhaoshuang,
	wangqing

Add a KUnit regression test that builds a depth-2 extent tree with two
leaves under a shared intermediate index block. Hold a reference to the
index block, make the second leaf read fail, and verify that its
buffer-head reference is released after ext4_ext_precache() returns.

Using a per-block failure flag is needed because a global getblk failure
would fail the first metadata read before any ancestor reference is
held.

Signed-off-by: jiazhenyuan <jiazhenyuan@uniontech.com>
---
 fs/ext4/extents-test.c | 101 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 100 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c
index 6458345ea958..2815156d7b3d 100644
--- a/fs/ext4/extents-test.c
+++ b/fs/ext4/extents-test.c
@@ -82,6 +82,7 @@ static struct kunit_ctx {
 	struct dentry k_dentry;
 
 	bool fail_getblk;
+	ext4_fsblk_t fail_getblk_pblk;
 } k_ctx;
 
 /*
@@ -336,7 +337,9 @@ static struct buffer_head *ext4_ext_getblk_stub(struct inode *inode,
 	struct kunit_meta_block *slot = NULL;
 	int i;
 
-	if (k_ctx.fail_getblk || !k_ctx.meta_blocks)
+	if (k_ctx.fail_getblk || !k_ctx.meta_blocks ||
+	    (k_ctx.fail_getblk_pblk &&
+	     pblk == k_ctx.fail_getblk_pblk))
 		return NULL;
 
 	for (i = 0; i < EXT_MAX_META_BLOCKS; i++) {
@@ -1364,6 +1367,74 @@ static void setup_depth1_two_leaf_tree(struct kunit *test, struct inode *inode)
 	brelse(bh1);
 }
 
+/*
+ * Helper to construct a depth-2 tree with two leaves under a shared
+ * intermediate index block:
+ *   Root (pblk none): index -> 500
+ *   Index 0 (pblk 500): leaf 0 -> 600, leaf 1 -> 601
+ *   Leaf 0 (pblk 600): [10, 15) -> 1000
+ *   Leaf 1 (pblk 601): [40, 45) -> 2000
+ */
+static void setup_depth2_shared_index_tree(struct kunit *test,
+					   struct inode *inode)
+{
+	struct ext4_extent_header *root_eh = ext_inode_hdr(inode);
+	struct ext4_extent_header *index_eh;
+	struct ext4_extent_header *leaf_eh;
+	struct ext4_extent_idx *idx;
+	struct ext4_extent *ex;
+	struct buffer_head *bh;
+
+	ext_reset_tree(inode);
+
+	root_eh->eh_depth = cpu_to_le16(2);
+	root_eh->eh_entries = cpu_to_le16(1);
+	root_eh->eh_max = cpu_to_le16(ext4_ext_space_root_idx_test(inode, 0));
+	idx = EXT_FIRST_INDEX(root_eh);
+	idx->ei_block = cpu_to_le32(10);
+	ext4_idx_store_pblock(idx, EXT_META_BASE_PBLK);
+
+	bh = ext4_ext_getblk_stub(inode, EXT_META_BASE_PBLK, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, bh);
+	index_eh = ext_block_hdr(bh);
+	index_eh->eh_magic = EXT4_EXT_MAGIC;
+	index_eh->eh_depth = cpu_to_le16(1);
+	index_eh->eh_entries = cpu_to_le16(2);
+	index_eh->eh_max = cpu_to_le16(2);
+	idx = EXT_FIRST_INDEX(index_eh);
+	idx[0].ei_block = cpu_to_le32(10);
+	ext4_idx_store_pblock(&idx[0], EXT_META_BASE_PBLK + 100);
+	idx[1].ei_block = cpu_to_le32(40);
+	ext4_idx_store_pblock(&idx[1], EXT_META_BASE_PBLK + 101);
+	brelse(bh);
+
+	bh = ext4_ext_getblk_stub(inode, EXT_META_BASE_PBLK + 100, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, bh);
+	leaf_eh = ext_block_hdr(bh);
+	leaf_eh->eh_magic = EXT4_EXT_MAGIC;
+	leaf_eh->eh_depth = 0;
+	leaf_eh->eh_entries = cpu_to_le16(1);
+	leaf_eh->eh_max = cpu_to_le16(1);
+	ex = EXT_FIRST_EXTENT(leaf_eh);
+	ex->ee_block = cpu_to_le32(10);
+	ex->ee_len = cpu_to_le16(5);
+	ext4_ext_store_pblock(ex, 1000);
+	brelse(bh);
+
+	bh = ext4_ext_getblk_stub(inode, EXT_META_BASE_PBLK + 101, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, bh);
+	leaf_eh = ext_block_hdr(bh);
+	leaf_eh->eh_magic = EXT4_EXT_MAGIC;
+	leaf_eh->eh_depth = 0;
+	leaf_eh->eh_entries = cpu_to_le16(1);
+	leaf_eh->eh_max = cpu_to_le16(1);
+	ex = EXT_FIRST_EXTENT(leaf_eh);
+	ex->ee_block = cpu_to_le32(40);
+	ex->ee_len = cpu_to_le16(5);
+	ext4_ext_store_pblock(ex, 2000);
+	brelse(bh);
+}
+
 /*
  * Verify multi-level (depth 1) extent lookup and tree navigation helpers:
  * ext4_ext_next_allocated_block(), ext4_ext_next_leaf_block(),
@@ -1471,6 +1542,33 @@ static void test_ext_precache(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, ext4_es_pblock(&es), 2100ULL);
 }
 
+/*
+ * Verify that a failed precache traversal drops the buffer-head reference
+ * held for the ancestor extent block. The shared depth-1 index block is
+ * deliberately held by the test; failing the second leaf leaves it
+ * referenced by path[1]. A missing path depth causes ext4_free_ext_path()
+ * to leak that reference.
+ */
+static void test_ext_precache_read_failure_bh_leak(struct kunit *test)
+{
+	struct inode *inode = &k_ctx.k_ei->vfs_inode;
+	struct buffer_head *bh;
+	int err;
+
+	setup_depth2_shared_index_tree(test, inode);
+	bh = ext4_ext_getblk_stub(inode, EXT_META_BASE_PBLK, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, bh);
+	KUNIT_ASSERT_EQ(test, atomic_read(&bh->b_count), 2);
+
+	k_ctx.fail_getblk_pblk = EXT_META_BASE_PBLK + 101;
+	err = ext4_ext_precache(inode);
+	k_ctx.fail_getblk_pblk = 0;
+
+	KUNIT_EXPECT_EQ(test, err, -ENOMEM);
+	KUNIT_EXPECT_EQ(test, atomic_read(&bh->b_count), 2);
+	brelse(bh);
+}
+
 /*
  * Verify ext4_can_extents_be_merged() rules for written/unwritten state,
  * logical and physical contiguity, and maximum length limits.
@@ -3519,6 +3617,7 @@ static struct kunit_case extents_test_cases[] = {
 	KUNIT_CASE(test_find_extent_root_binsearch),
 	KUNIT_CASE(test_find_extent_multilevel_and_navigation),
 	KUNIT_CASE(test_ext_precache),
+	KUNIT_CASE(test_ext_precache_read_failure_bh_leak),
 	KUNIT_CASE(test_can_extents_be_merged),
 	KUNIT_CASE(test_insert_extent_merge_and_nomerge),
 	KUNIT_CASE(test_insert_extent_grow_and_merge_up),
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache()
  2026-11-27  6:49 [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() jiazhenyuan
  2026-11-27  6:49 ` [PATCH 2/2] ext4: add KUnit test for ext4_ext_precache() buffer head leak jiazhenyuan
@ 2026-11-27  6:58 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-27  6:58 UTC (permalink / raw)
  To: jiazhenyuan, linux-ext4; +Cc: tytso

Sashiko has reviewed this 2-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127064926.135945-1-jiazhenyuan@uniontech.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27  6:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  6:49 [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() jiazhenyuan
2026-11-27  6:49 ` [PATCH 2/2] ext4: add KUnit test for ext4_ext_precache() buffer head leak jiazhenyuan
2026-11-27  6:58 ` [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox