* [PATCH v2] btrfs: fix potential segfault in balance_remap_chunks()
@ 2026-03-09 18:50 Mark Harmstone
2026-03-10 7:22 ` Johannes Thumshirn
2026-03-16 9:19 ` David Sterba
0 siblings, 2 replies; 3+ messages in thread
From: Mark Harmstone @ 2026-03-09 18:50 UTC (permalink / raw)
To: linux-btrfs, Johannes.Thumshirn; +Cc: Mark Harmstone, Chris Mason
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.
Link: https://lore.kernel.org/linux-btrfs/20260125120717.1578828-1-clm@meta.com/
Reported-by: Chris Mason <clm@fb.com>
Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
Signed-off-by: Mark Harmstone <mark@harmstone.com>
---
fs/btrfs/volumes.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 95accc9361bd26..aff286d9df4aa0 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -4285,20 +4285,24 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path
end:
while (!list_empty(chunks)) {
bool is_unused;
+ struct btrfs_block_group *bg;
rci = list_first_entry(chunks, struct remap_chunk_info, list);
- spin_lock(&rci->bg->lock);
- is_unused = !btrfs_is_block_group_used(rci->bg);
- spin_unlock(&rci->bg->lock);
+ bg = rci->bg;
+ if (bg) {
+ spin_lock(&bg->lock);
+ is_unused = !btrfs_is_block_group_used(bg);
+ spin_unlock(&bg->lock);
- if (is_unused)
- btrfs_mark_bg_unused(rci->bg);
+ if (is_unused)
+ btrfs_mark_bg_unused(bg);
- if (rci->made_ro)
- btrfs_dec_block_group_ro(rci->bg);
+ if (rci->made_ro)
+ btrfs_dec_block_group_ro(bg);
- btrfs_put_block_group(rci->bg);
+ btrfs_put_block_group(bg);
+ }
list_del(&rci->list);
kfree(rci);
--
2.52.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] btrfs: fix potential segfault in balance_remap_chunks()
2026-03-09 18:50 [PATCH v2] btrfs: fix potential segfault in balance_remap_chunks() Mark Harmstone
@ 2026-03-10 7:22 ` Johannes Thumshirn
2026-03-16 9:19 ` David Sterba
1 sibling, 0 replies; 3+ messages in thread
From: Johannes Thumshirn @ 2026-03-10 7:22 UTC (permalink / raw)
To: Mark Harmstone, linux-btrfs@vger.kernel.org; +Cc: Chris Mason
Looks good,
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] btrfs: fix potential segfault in balance_remap_chunks()
2026-03-09 18:50 [PATCH v2] btrfs: fix potential segfault in balance_remap_chunks() Mark Harmstone
2026-03-10 7:22 ` Johannes Thumshirn
@ 2026-03-16 9:19 ` David Sterba
1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2026-03-16 9:19 UTC (permalink / raw)
To: Mark Harmstone; +Cc: linux-btrfs, Johannes.Thumshirn, Chris Mason
Subject changed to "btrfs: check block group before marking it unused in
balance_remap_chunks()"
On Mon, Mar 09, 2026 at 06:50:37PM +0000, Mark Harmstone wrote:
> 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.
>
> Link: https://lore.kernel.org/linux-btrfs/20260125120717.1578828-1-clm@meta.com/
> Reported-by: Chris Mason <clm@fb.com>
> Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
> Signed-off-by: Mark Harmstone <mark@harmstone.com>
> ---
> fs/btrfs/volumes.c | 20 ++++++++++++--------
> 1 file changed, 12 insertions(+), 8 deletions(-)
>
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 95accc9361bd26..aff286d9df4aa0 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -4285,20 +4285,24 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path
> end:
> while (!list_empty(chunks)) {
> bool is_unused;
> + struct btrfs_block_group *bg;
>
> rci = list_first_entry(chunks, struct remap_chunk_info, list);
>
> - spin_lock(&rci->bg->lock);
> - is_unused = !btrfs_is_block_group_used(rci->bg);
> - spin_unlock(&rci->bg->lock);
> + bg = rci->bg;
> + if (bg) {
> + spin_lock(&bg->lock);
> + is_unused = !btrfs_is_block_group_used(bg);
> + spin_unlock(&bg->lock);
>
> - if (is_unused)
> - btrfs_mark_bg_unused(rci->bg);
> + if (is_unused)
> + btrfs_mark_bg_unused(bg);
>
> - if (rci->made_ro)
> - btrfs_dec_block_group_ro(rci->bg);
> + if (rci->made_ro)
> + btrfs_dec_block_group_ro(bg);
>
> - btrfs_put_block_group(rci->bg);
> + btrfs_put_block_group(bg);
> + }
>
> list_del(&rci->list);
> kfree(rci);
> --
> 2.52.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-03-16 9:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-09 18:50 [PATCH v2] btrfs: fix potential segfault in balance_remap_chunks() Mark Harmstone
2026-03-10 7:22 ` Johannes Thumshirn
2026-03-16 9:19 ` David Sterba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox