Skip to contentSkip to conversation

btrfs: fix resource leak in do_walk_down()

PATCH In review Zhen Ni 2 participants 2 messages Nov 4, 2025

Subsystems (2)
lore
Share & tools
lore

Zhen Ni

author
#0 lore raw

PATCH 1/1 btrfs: fix resource leak in do_walk_down()

When check_next_block_uptodate() fails, do_walk_down() returns directly without cleaning up the locked extent buffer allocated earlier, causing memory and lock leaks.

Fix by using the existing out_unlock cleanup path instead of direct return.

Fixes 562d425454e8 ("btrfs: factor out eb uptodate check from do_walk_down()")
---
 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
index dc4ca98c3780..742e476bf815 100644
@@ -5770,7 +5770,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 
 	ret = check_next_block_uptodate(trans, root, path, wc, next);
 	if (ret)
-		return ret;
+		goto out_unlock;
 
 	level--;
 	ASSERT(level == btrfs_header_level(next));
Signature · 1 line
2.20.1
#1 lore raw
Quoted 20 lines · Zhen Ni

When check_next_block_uptodate() fails, do_walk_down() returns directly without cleaning up the locked extent buffer allocated earlier, causing memory and lock leaks.

Fix by using the existing out_unlock cleanup path instead of direct return.

Fixes 562d425454e8 ("btrfs: factor out eb uptodate check from do_walk_down()")
---
 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +0 -0
index dc4ca98c3780..742e476bf815 100644

fs/btrfs/extent-tree.c static noinline int do_walk_down(struct btrfs_trans_handle *trans, · line 5773

Quoted 5 lines
fs/btrfs/extent-tree.c +1 -1
@@ -5770,7 +5770,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
 
        ret = check_next_block_uptodate(trans, root, path, wc, next);
        if (ret)
-               return ret;
+               goto out_unlock;

This is wrong and will cause a double unlock and double free.

When check_next_block_uptodate() returns an error, it has already unlocked and freed the extent buffer.

Thanks.

Quoted 7 lines
fs/btrfs/extent-tree.c +0 -1
        level--;
        ASSERT(level == btrfs_header_level(next));
--

2.20.1