btrfs: fix memory leak of fs_devices in degraded seed device path
PATCH 1/1 btrfs: fix memory leak of fs_devices in degraded seed device path
In open_seed_devices(), when find_fsid() fails and we're in DEGRADED mode, a new fs_devices is allocated via alloc_fs_devices() but is never added to the seed_list before returning. This contrasts with the normal path where fs_devices is properly added via list_add().
If any error occurs later in read_one_dev() or btrfs_read_chunk_tree(), the cleanup code iterates seed_list to free seed devices, but this orphaned fs_devices is never found and never freed, causing a memory leak. Any devices allocated via add_missing_dev() and attached to this fs_devices are also leaked.
Fix this by adding the newly allocated fs_devices to seed_list in the degraded path, consistent with the normal path.
--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)
fs/btrfs/volumes.c +1 -0
@@ -7128,6 +7128,7 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, fs_devices->seeding = true; fs_devices->opened =1 ; +list_add (&fs_devices->seed_list, &fs_info->fs_devices->seed_list);return fs_devices; }
Signature · 1 line
2.43.0
Quoted 11 lines · Deepanshu Kartikey
In
open_seed_devices(), whenfind_fsid()fails and we're in DEGRADED mode, a newfs_devicesis allocated viaalloc_fs_devices()but is never added to theseed_listbefore returning. This contrasts with the normal path wherefs_devicesis properly added vialist_add().If any error occurs later in
read_one_dev()orbtrfs_read_chunk_tree(), the cleanup code iteratesseed_listto free seed devices, but this orphanedfs_devicesis never found and never freed, causing a memory leak. Any devices allocated viaadd_missing_dev()and attached to thisfs_devicesare also leaked.
Quoted 8 lines
Fix this by adding the newly allocated
fs_devicestoseed_listin the degraded path, consistent with the normal path.Fixes 5f37583569442 ("Btrfs: move the missing device to its own fs device list")Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
Quoted 11 lines · Deepanshu Kartikey
In
open_seed_devices(), whenfind_fsid()fails and we're in DEGRADED mode, a newfs_devicesis allocated viaalloc_fs_devices()but is never added to theseed_listbefore returning. This contrasts with the normal path wherefs_devicesis properly added vialist_add().If any error occurs later in
read_one_dev()orbtrfs_read_chunk_tree(), the cleanup code iteratesseed_listto free seed devices, but this orphanedfs_devicesis never found and never freed, causing a memory leak. Any devices allocated viaadd_missing_dev()and attached to thisfs_devicesare also leaked.
Quoted 8 lines
Fix this by adding the newly allocated
fs_devicestoseed_listin the degraded path, consistent with the normal path.Fixes 5f37583569442 ("Btrfs: move the missing device to its own fs device list")Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
Thanks,
Qu
Quoted 16 lines
--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)fs/btrfs/volumes.c +1
-0@@ -7128,6 +7128,7 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, fs_devices->seeding = true; fs_devices->opened =1 ; +list_add (&fs_devices->seed_list, &fs_info->fs_devices->seed_list);return fs_devices; }