mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: jiazhenyuan <jiazhenyuan@uniontech.com>
To: tytso@mit.edu, adilger.kernel@dilger.ca,
	libaokun@linux.alibaba.com, jack@suse.cz, ojaswin@linux.ibm.com,
	ritesh.list@gmail.com, yi.zhang@huawei.com,
	linux-ext4@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, gouhao@uniontech.com,
	niecheng1@uniontech.com, guanwentao@uniontech.com,
	caina@uniontech.com, zhaoshuang@uniontech.com,
	wangqing@uniontech.com
Subject: [PATCH 2/2] ext4: add KUnit test for ext4_ext_precache() buffer head leak
Date: Fri, 27 Nov 2026 14:49:26 +0800	[thread overview]
Message-ID: <20261127064926.135945-2-jiazhenyuan@uniontech.com> (raw)
In-Reply-To: <20261127064926.135945-1-jiazhenyuan@uniontech.com>

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


  reply	other threads:[~2026-11-27  6:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  6:49 [PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache() jiazhenyuan
2026-11-27  6:49 ` jiazhenyuan [this message]
2026-11-27 14:29 ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127064926.135945-2-jiazhenyuan@uniontech.com \
    --to=jiazhenyuan@uniontech.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=caina@uniontech.com \
    --cc=gouhao@uniontech.com \
    --cc=guanwentao@uniontech.com \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=niecheng1@uniontech.com \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=tytso@mit.edu \
    --cc=wangqing@uniontech.com \
    --cc=yi.zhang@huawei.com \
    --cc=zhaoshuang@uniontech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®