btrfs: fix zoned sub_group leak on space_info sysfs failure
PATCH v2 1/1 btrfs: fix zoned sub_group leak on space_info sysfs failure
On zoned filesystems, create_space_info() creates space_info sub_groups for the primary DATA and METADATA space infos.
Before this change, the sub_group was created before registering the parent space_info in sysfs. If the sub_group registration succeeded but the parent btrfs_sysfs_add_space_info_type() call failed, the parent space_info was released through its kobject release path while the already published sub_group was left behind. The sub_group also kept its logical parent pointer, which then pointed at freed memory.
Fix this by making btrfs_sysfs_remove_space_info() responsible for removing any sub_groups before removing the space_info itself. Then register the parent space_info before creating zoned sub_groups. With that order, a parent registration failure has no sub_groups to unwind; if sub_group creation fails, btrfs_sysfs_remove_space_info() can tear down the parent and any sub_groups created so far.
This also keeps check_removing_space_info() as a pure checker by moving sub_group removal into the common sysfs removal helper.
---
v2:
- Make btrfs_sysfs_remove_space_info() remove sub_groups before removing the space_info itself, as suggested by Naohiro.
- Register the parent space_info before creating zoned sub_groups in create_space_info().
- Use btrfs_sysfs_remove_space_info() to unwind the parent when sub_group creation fails.
- Remove sub_group removal from check_removing_space_info(), keeping it as a pure checker.
- Use ARRAY_SIZE(space_info->sub_group) instead of BTRFS_SPACE_INFO_SUB_GROUP_MAX.
fs/btrfs/block-group.c | 5 +---- fs/btrfs/space-info.c | 14 +++++++------- fs/btrfs/sysfs.c | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-)
fs/btrfs/block-group.c +1 -4
@@ -4615,11 +4615,8 @@ static void check_removing_space_info(struct btrfs_space_info *space_info)if (space_info->subgroup_id ==BTRFS_SUB_GROUP_PRIMARY ) {/* This is a top space_info, proceed with its children first. */ for (int i =0 ; i <BTRFS_SPACE_INFO_SUB_GROUP_MAX ; i++) { -if (space_info->sub_group[i]) { +if (space_info->sub_group[i])check_removing_space_info (space_info->sub_group[i]); -btrfs_sysfs_remove_space_info (space_info->sub_group[i]); - space_info->sub_group[i] =NULL ; - } } }
fs/btrfs/space-info.c +7 -7
@@ -304,6 +304,10 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags)init_space_info (info, space_info, flags); + ret =btrfs_sysfs_add_space_info_type (space_info); +if (ret) +return ret; +if (btrfs_is_zoned (info)) {if (flags &BTRFS_BLOCK_GROUP_DATA ) ret = create_space_info_sub_group(space_info, flags,
@@ -315,21 +319,17 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags) 0);if (ret) - goto out_free; + goto out_remove; } - ret =btrfs_sysfs_add_space_info_type (space_info); -if (ret) -return ret; -list_add (&space_info->list, &info->space_info);if (flags &BTRFS_BLOCK_GROUP_DATA ) info->data_sinfo = space_info;return ret; -out_free: -kfree (space_info); +out_remove: +btrfs_sysfs_remove_space_info (space_info);return ret; }
fs/btrfs/sysfs.c +16 -0
@@ -1885,6 +1885,22 @@ void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) {int i; +for (i =0 ; i <ARRAY_SIZE (space_info->sub_group); i++) { +struct btrfs_space_info *sub_group; + + sub_group = space_info->sub_group[i]; +if (!sub_group) +continue ; + +/* +* Remove sub-groups before removing their parent space_info. +* Clear the pointer first so recursive removal cannot leave a +* stale child pointer behind. +*/ + space_info->sub_group[i] =NULL ; +btrfs_sysfs_remove_space_info (sub_group); + } +for (i =0 ; i <BTRFS_NR_RAID_TYPES ; i++) {struct kobject *kobj;
Signature · 1 line
2.43.0
Quoted 5 lines · Guangshuo Li
fs/btrfs/sysfs.c +0
-0
fs/btrfs/sysfs.c void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) · line 1893
Quoted 9 lines
fs/btrfs/sysfs.c +6
-0@@ -1885,6 +1885,22 @@ void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) {int i; +for (i =0 ; i <ARRAY_SIZE (space_info->sub_group); i++) { +struct btrfs_space_info *sub_group; + + sub_group = space_info->sub_group[i]; +if (!sub_group) +continue ;
I think you can break here instead of continue. space_info->sub_group is not expected to have holes.
fs/btrfs/sysfs.c void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) · line 1901
Quoted 8 lines
fs/btrfs/sysfs.c +8
-0+ +/* +* Remove sub-groups before removing their parent space_info. +* Clear the pointer first so recursive removal cannot leave a +* stale child pointer behind. +*/ + space_info->sub_group[i] =NULL ; +btrfs_sysfs_remove_space_info (sub_group);
That comment IMHO doesn't add any value.
Hi Johannes,
Thank you for the review.
Quoted 30 lines · Johannes Thumshirn
Quoted 5 lines · Guangshuo Li
fs/btrfs/sysfs.c +0
-0fs/btrfs/sysfs.c void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) · line 1893
Quoted 9 lines
fs/btrfs/sysfs.c +6
-0@@ -1885,6 +1885,22 @@ void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) {int i; +for (i =0 ; i <ARRAY_SIZE (space_info->sub_group); i++) { +struct btrfs_space_info *sub_group; + + sub_group = space_info->sub_group[i]; +if (!sub_group) +continue ;I think you can break here instead of continue.
space_info->sub_groupis not expected to have holes.fs/btrfs/sysfs.c void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) · line 1901
Quoted 8 lines
fs/btrfs/sysfs.c +8
-0+ +/* +* Remove sub-groups before removing their parent space_info. +* Clear the pointer first so recursive removal cannot leave a +* stale child pointer behind. +*/ + space_info->sub_group[i] =NULL ; +btrfs_sysfs_remove_space_info (sub_group);
Quoted 4 lines
That comment IMHO doesn't add any value.
I agree, sub_group[] is not expected to have holes, so using break here is better than continue. I will also drop the comment as suggested.
I will send a v3 with these changes.
Thanks,
Guangshuo