Skip to content

Commit adbb0eb

Browse files
maharmstonekdave
authored andcommitted
btrfs: check block group before marking it unused in balance_remap_chunks()
Fix a potential segfault in balance_remap_chunks(): if we quit early because btrfs_inc_block_group_ro() fails, all the remaining items in the chunks list will still have their bg value set to NULL. It's thus not safe to dereference this pointer without checking first. Reported-by: Chris Mason <clm@fb.com> Link: https://lore.kernel.org/linux-btrfs/20260125120717.1578828-1-clm@meta.com/ Fixes: 81e5a45 ("btrfs: allow balancing remap tree") Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Mark Harmstone <mark@harmstone.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 057495c commit adbb0eb

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

fs/btrfs/volumes.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,20 +4277,29 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path
42774277
end:
42784278
while (!list_empty(chunks)) {
42794279
bool is_unused;
4280+
struct btrfs_block_group *bg;
42804281

42814282
rci = list_first_entry(chunks, struct remap_chunk_info, list);
42824283

4283-
spin_lock(&rci->bg->lock);
4284-
is_unused = !btrfs_is_block_group_used(rci->bg);
4285-
spin_unlock(&rci->bg->lock);
4284+
bg = rci->bg;
4285+
if (bg) {
4286+
/*
4287+
* This is a bit racy and the 'used' status can change
4288+
* but this is not a problem as later functions will
4289+
* verify it again.
4290+
*/
4291+
spin_lock(&bg->lock);
4292+
is_unused = !btrfs_is_block_group_used(bg);
4293+
spin_unlock(&bg->lock);
42864294

4287-
if (is_unused)
4288-
btrfs_mark_bg_unused(rci->bg);
4295+
if (is_unused)
4296+
btrfs_mark_bg_unused(bg);
42894297

4290-
if (rci->made_ro)
4291-
btrfs_dec_block_group_ro(rci->bg);
4298+
if (rci->made_ro)
4299+
btrfs_dec_block_group_ro(bg);
42924300

4293-
btrfs_put_block_group(rci->bg);
4301+
btrfs_put_block_group(bg);
4302+
}
42944303

42954304
list_del(&rci->list);
42964305
kfree(rci);

0 commit comments

Comments
 (0)