[PATCH 1/2] ext4: fix buffer head leak in ext4_ext_precache()

From: jiazhenyuan

Date: Fri Nov 27 2026 - 01:51:33 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: jiazhenyuan <jiazhenyuan@xxxxxxxxxxxxx>
---
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