btrfs: fix potential segfault in balance_remap_chunks()
PATCH 1/1 btrfs: fix potential segfault 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.
--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
fs/btrfs/volumes.c +10 -8
@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path 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); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock); -if (is_unused) -btrfs_mark_bg_unused (rci->bg); +if (is_unused) +btrfs_mark_bg_unused (rci->bg); -if (rci->made_ro) -btrfs_dec_block_group_ro (rci->bg); +if (rci->made_ro) +btrfs_dec_block_group_ro (rci->bg); -btrfs_put_block_group (rci->bg); +btrfs_put_block_group (rci->bg); + }list_del (&rci->list);kfree (rci);
Signature · 1 line
2.52.0
Quoted 35 lines · Mark Harmstone
Fix a potential segfault in
balance_remap_chunks(): if we quit early becausebtrfs_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 <[email protected]>Fixes 81e5a4551c32 ("btrfs: allow balancing remap tree")Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)fs/btrfs/volumes.c +6
-6@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path 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); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock); -if (is_unused) -btrfs_mark_bg_unused (rci->bg); +if (is_unused) +btrfs_mark_bg_unused (rci->bg); -if (rci->made_ro)
fs/btrfs/volumes.c static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path · line 4306
Quoted 10 lines
fs/btrfs/volumes.c +4
-2-btrfs_dec_block_group_ro (rci->bg); +if (rci->made_ro) +btrfs_dec_block_group_ro (rci->bg); -btrfs_put_block_group (rci->bg); +btrfs_put_block_group (rci->bg); + }list_del (&rci->list);kfree (rci);
Not necessarily a hot path here, but this could be made way more readable if you'd have a local bg variable, sth like this:
fs/btrfs/volumes.c +0 -0
@@ -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);
Quoted 21 lines · Mark Harmstone
Fix a potential segfault in
balance_remap_chunks(): if we quit early becausebtrfs_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 <[email protected]>Fixes 81e5a4551c32 ("btrfs: allow balancing remap tree")Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)fs/btrfs/volumes.c +0
-0@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path rci = list_first_entry(chunks, struct remap_chunk_info, list);
fs/btrfs/volumes.c static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path · line 4296
Quoted 10 lines
fs/btrfs/volumes.c +4
-5-spin_lock (&rci->bg->lock); - is_unused = !btrfs_is_block_group_used (rci->bg); -spin_unlock (&rci->bg->lock); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock); -if (is_unused) -btrfs_mark_bg_unused (rci->bg);
Not related to the patch but isn't this pattern inherently racy?
The "used" is read under lock but then btrfs_mark_bg_unused() is outside of the lock so the status can change. This can be seen it more places, but they seem to be related to the remap tree feature.
Quoted 30 lines · David Sterba
Quoted 29 lines · Mark Harmstone
Fix a potential segfault in
balance_remap_chunks(): if we quit early becausebtrfs_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 <[email protected]>Fixes 81e5a4551c32 ("btrfs: allow balancing remap tree")Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)fs/btrfs/volumes.c +4
-3@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path 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); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock);
Quoted 7 lines
fs/btrfs/volumes.c static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path · line 4296
Quoted 2 lines
fs/btrfs/volumes.c +0
-2-if (is_unused) -btrfs_mark_bg_unused (rci->bg);Not related to the patch but isn't this pattern inherently racy?
The "used" is read under lock but then
btrfs_mark_bg_unused()is outside of the lock so the status can change. This can be seen it more places, but they seem to be related to the remap tree feature.
Good catch, it is! But I /think/ it's not something to worry about (too much), because btrfs_delete_unused_bgs() will do checks on it's own as well, so if the "unused" state changes, nothing bad will happen.
Quoted 31 lines · David Sterba
Quoted 29 lines · Mark Harmstone
Fix a potential segfault in
balance_remap_chunks(): if we quit early becausebtrfs_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 <[email protected]>Fixes 81e5a4551c32 ("btrfs: allow balancing remap tree")Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)fs/btrfs/volumes.c +4
-3@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path 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); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock);
Quoted 8 lines
fs/btrfs/volumes.c static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path · line 4296
Quoted 2 lines
fs/btrfs/volumes.c +0
-2-if (is_unused) -btrfs_mark_bg_unused (rci->bg);Not related to the patch but isn't this pattern inherently racy?
The "used" is read under lock but then
btrfs_mark_bg_unused()is outside of the lock so the status can change. This can be seen it more places, but they seem to be related to the remap tree feature.
It's not a problem since when attempting to delete unused bgs we skip any if they are actually used.
It's done not just for this type of race but also for the case where after added to the unused list, it becomes used before the cleaner kthread runs to delete unused bgs.
Quoted 1 line
Quoted 41 lines · Filipe Manana
Quoted 31 lines · David Sterba
Quoted 29 lines · Mark Harmstone
Fix a potential segfault in
balance_remap_chunks(): if we quit early becausebtrfs_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 <[email protected]>Fixes 81e5a4551c32 ("btrfs: allow balancing remap tree")Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)fs/btrfs/volumes.c +4
-3@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path 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); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock);Quoted 8 lines
fs/btrfs/volumes.c static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path · line 4296
Quoted 2 lines
fs/btrfs/volumes.c +0
-2-if (is_unused) -btrfs_mark_bg_unused (rci->bg);Not related to the patch but isn't this pattern inherently racy?
The "used" is read under lock but then
btrfs_mark_bg_unused()is outside of the lock so the status can change. This can be seen it more places, but they seem to be related to the remap tree feature.
Quoted 5 lines
It's not a problem since when attempting to delete unused bgs we skip any if they are actually used.
It's done not just for this type of race but also for the case where after added to the unused list, it becomes used before the cleaner kthread runs to delete unused bgs.
Yes, the unused list means less "these BGs are definitely unused", more "these BGs are worth considering as maybe unused".
Johannes, your suggestion that we add a bg variable is a good one - I'll send another patch doing that.
Quoted 42 lines · Mark Harmstone
Quoted 41 lines · Filipe Manana
Quoted 39 lines · David Sterba
Quoted 21 lines · Mark Harmstone
Fix a potential segfault in
balance_remap_chunks(): if we quit early becausebtrfs_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 <[email protected]>Fixes 81e5a4551c32 ("btrfs: allow balancing remap tree")Signed-off-by Mark Harmstone <[email protected]>--- fs/btrfs/volumes.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)fs/btrfs/volumes.c +0
-0@@ -4288,17 +4288,19 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path rci = list_first_entry(chunks, struct remap_chunk_info, list);fs/btrfs/volumes.c static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path · line 4296
Quoted 10 lines
fs/btrfs/volumes.c +4
-5-spin_lock (&rci->bg->lock); - is_unused = !btrfs_is_block_group_used (rci->bg); -spin_unlock (&rci->bg->lock); +if (rci->bg) { +spin_lock (&rci->bg->lock); + is_unused = !btrfs_is_block_group_used (rci->bg); +spin_unlock (&rci->bg->lock); -if (is_unused) -btrfs_mark_bg_unused (rci->bg);Not related to the patch but isn't this pattern inherently racy?
The "used" is read under lock but then
btrfs_mark_bg_unused()is outside of the lock so the status can change. This can be seen it more places, but they seem to be related to the remap tree feature.
Quoted 8 lines
Quoted 5 lines
It's not a problem since when attempting to delete unused bgs we skip any if they are actually used.
It's done not just for this type of race but also for the case where after added to the unused list, it becomes used before the cleaner kthread runs to delete unused bgs.Yes, the unused list means less "these BGs are definitely unused", more "these BGs are worth considering as maybe unused".
So if there's a question or need for clarification in a patch thread please add it as a comment (commit updated).