btrfs: fix extent state leaks after device replace
cover btrfs: fix extent state leaks after device replace
From: Filipe Manana <[email protected]>
This fixes a recent regression (on its way to Linus' tree) that results in leaking extent state records in the allocation io tree of a device used as a source device for a device replace. Also unexport btrfs_free_device().
Filipe Manana (2):
btrfs: fix leak of source device allocation state after device replace
btrfs: make btrfs_free_device() static
fs/btrfs/volumes.c | 3 ++- fs/btrfs/volumes.h | 1 - 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/2 btrfs: fix leak of source device allocation state after device replace
From: Filipe Manana <[email protected]>
When a device replace finishes, the source device is freed by calling btrfs_free_device() at btrfs_rm_dev_replace_free_srcdev(), but the allocation state, tracked in the device's alloc_state io tree, is never freed.
This is a regression recently introduced by commit f0bb5474cff0 ("btrfs:
remove redundant release of btrfs_device::alloc_state"), which removed a call to extent_io_tree_release() from btrfs_free_device(), with the rationale that btrfs_close_one_device() already releases the allocation state from a device and btrfs_close_one_device() is always called before a device is freed with btrfs_free_device(). However that is not true for the device replace case, as btrfs_free_device() is called without any previous call to btrfs_close_one_device().
The issue is trivial to reproduce, for example, by running test btrfs/027 from fstests:
$ ./check btrfs/027 $ rmmod btrfs $ dmesg (...) [84519.395485] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg started [84519.466224] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg finished [84519.552251] BTRFS info (device sdc): scrub: started on devid 1 [84519.552277] BTRFS info (device sdc): scrub: started on devid 2 [84519.552332] BTRFS info (device sdc): scrub: started on devid 3 [84519.552705] BTRFS info (device sdc): scrub: started on devid 4 [84519.604261] BTRFS info (device sdc): scrub: finished on devid 4 with status: 0 [84519.609374] BTRFS info (device sdc): scrub: finished on devid 3 with status: 0 [84519.610818] BTRFS info (device sdc): scrub: finished on devid 1 with status: 0 [84519.610927] BTRFS info (device sdc): scrub: finished on devid 2 with status: 0 [84559.503795] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 [84559.506764] BTRFS: state leak: start 1048576 end 1347420159 state 1 in tree 1 refs 1 [84559.510294] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1
So fix this by adding back the call to extent_io_tree_release() at btrfs_free_device().
--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)
fs/btrfs/volumes.c +1 -0
@@ -395,6 +395,7 @@ void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name); +extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device); }
Signature · 1 line
2.35.1
PATCH 2/2 btrfs: make btrfs_free_device() static
In reply to [email protected]From: Filipe Manana <[email protected]>
The function btrfs_free_device() is never used outside of volumes.c, so make it static and remove its prototype declaration at volumes.h.
--- fs/btrfs/volumes.c | 2 +- fs/btrfs/volumes.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/volumes.c +1 -1
@@ -391,7 +391,7 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,return fs_devs; } -void btrfs_free_device (struct btrfs_device *device) +static void btrfs_free_device (struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name);
fs/btrfs/volumes.h +0 -1
@@ -617,7 +617,6 @@ struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, const u64 *devid, const u8 *uuid, const char *path);void btrfs_put_dev_args_from_path (struct btrfs_dev_lookup_args *args); -void btrfs_free_device (struct btrfs_device *device);int btrfs_rm_device (struct btrfs_fs_info *fs_info,struct btrfs_dev_lookup_args *args,struct block_device **bdev,fmode_t *mode);
Signature · 1 line
2.35.1
Re: [PATCH 1/2] btrfs: fix leak of source device allocation state after device replace
In reply to [email protected]Quoted 37 lines · On 2023/4/27 01:13, [email protected] wrote:
From: Filipe Manana <[email protected]>
When a device replace finishes, the source device is freed by calling
btrfs_free_device()atbtrfs_rm_dev_replace_free_srcdev(), but the allocation state, tracked in the device'salloc_stateio tree, is never freed.This is a regression recently introduced by commit
f0bb5474cff0("btrfs:
remove redundant release ofbtrfs_device::alloc_state"), which removed a call toextent_io_tree_release()frombtrfs_free_device(), with the rationale thatbtrfs_close_one_device()already releases the allocation state from a device andbtrfs_close_one_device()is always called before a device is freed withbtrfs_free_device(). However that is not true for the device replace case, asbtrfs_free_device()is called without any previous call tobtrfs_close_one_device().The issue is trivial to reproduce, for example, by running test btrfs/027 from fstests:
$ ./check btrfs/027 $ rmmod btrfs $ dmesg (...) [84519.395485] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg started [84519.466224] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg finished [84519.552251] BTRFS info (device sdc): scrub: started on devid 1 [84519.552277] BTRFS info (device sdc): scrub: started on devid 2 [84519.552332] BTRFS info (device sdc): scrub: started on devid 3 [84519.552705] BTRFS info (device sdc): scrub: started on devid 4 [84519.604261] BTRFS info (device sdc): scrub: finished on devid 4 with status: 0 [84519.609374] BTRFS info (device sdc): scrub: finished on devid 3 with status: 0 [84519.610818] BTRFS info (device sdc): scrub: finished on devid 1 with status: 0 [84519.610927] BTRFS info (device sdc): scrub: finished on devid 2 with status: 0 [84559.503795] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 [84559.506764] BTRFS: state leak: start 1048576 end 1347420159 state 1 in tree 1 refs 1 [84559.510294] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1
Quoted 5 lines
So fix this by adding back the call to
extent_io_tree_release()atbtrfs_free_device().Fixes f0bb5474cff0 ("btrfs: remove redundant release of btrfs_device::alloc_state")Signed-off-by Filipe Manana <[email protected]>
Thanks,
Qu
Quoted 16 lines
--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)fs/btrfs/volumes.c +1
-0@@ -395,6 +395,7 @@ void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name); +extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device); }
Re: [PATCH 2/2] btrfs: make btrfs_free_device() static
In reply to [email protected]Quoted 6 lines · On 2023/4/27 01:13, [email protected] wrote:
From: Filipe Manana <[email protected]>
The function
btrfs_free_device()is never used outside ofvolumes.c, so make it static and remove its prototype declaration atvolumes.h.Signed-off-by Filipe Manana <[email protected]>
Thanks,
Qu
Quoted 30 lines
--- fs/btrfs/volumes.c | 2 +- fs/btrfs/volumes.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-)fs/btrfs/volumes.c +1
-1@@ -391,7 +391,7 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,return fs_devs; } -void btrfs_free_device (struct btrfs_device *device) +static void btrfs_free_device (struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name);fs/btrfs/volumes.h +0
-1@@ -617,7 +617,6 @@ struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, const u64 *devid, const u8 *uuid, const char *path);void btrfs_put_dev_args_from_path (struct btrfs_dev_lookup_args *args); -void btrfs_free_device (struct btrfs_device *device);int btrfs_rm_device (struct btrfs_fs_info *fs_info,struct btrfs_dev_lookup_args *args,struct block_device **bdev,fmode_t *mode);
Re: [PATCH 1/2] btrfs: fix leak of source device allocation state after device replace
In reply to [email protected]Quoted 16 lines · On 27/04/2023 01:13, [email protected] wrote:
From: Filipe Manana <[email protected]>
When a device replace finishes, the source device is freed by calling
btrfs_free_device()atbtrfs_rm_dev_replace_free_srcdev(), but the allocation state, tracked in the device'salloc_stateio tree, is never freed.This is a regression recently introduced by commit
f0bb5474cff0("btrfs:
remove redundant release ofbtrfs_device::alloc_state"), which removed a call toextent_io_tree_release()frombtrfs_free_device(), with the rationale thatbtrfs_close_one_device()already releases the allocation state from a device andbtrfs_close_one_device()is always called before a device is freed withbtrfs_free_device(). However that is not true for the device replace case, asbtrfs_free_device()is called without any previous call tobtrfs_close_one_device().
Quoted 5 lines
The issue is trivial to reproduce, for example, by running test btrfs/027 from fstests:
$ ./check btrfs/027 $ rmmod btrfs
Ah, module reload is a useful way to verify. I have now enabled it in the fstests by setting TEST_FS_MODULE_RELOAD=1, and I am able to reproduce the issue.
Quoted 30 lines
$ dmesg (...) [84519.395485] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg started [84519.466224] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg finished [84519.552251] BTRFS info (device sdc): scrub: started on devid 1 [84519.552277] BTRFS info (device sdc): scrub: started on devid 2 [84519.552332] BTRFS info (device sdc): scrub: started on devid 3 [84519.552705] BTRFS info (device sdc): scrub: started on devid 4 [84519.604261] BTRFS info (device sdc): scrub: finished on devid 4 with status: 0 [84519.609374] BTRFS info (device sdc): scrub: finished on devid 3 with status: 0 [84519.610818] BTRFS info (device sdc): scrub: finished on devid 1 with status: 0 [84519.610927] BTRFS info (device sdc): scrub: finished on devid 2 with status: 0 [84559.503795] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 [84559.506764] BTRFS: state leak: start 1048576 end 1347420159 state 1 in tree 1 refs 1 [84559.510294] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1So fix this by adding back the call to
extent_io_tree_release()atbtrfs_free_device().Fixes f0bb5474cff0 ("btrfs: remove redundant release of btrfs_device::alloc_state")Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)fs/btrfs/volumes.c +0
-0
fs/btrfs/volumes.c void btrfs_free_device(struct btrfs_device *device) · line 401
Quoted 7 lines
fs/btrfs/volumes.c +1
-0@@ -395,6 +395,7 @@ void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name); +extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device); }
Hmm. Is this fix incomplete? It does not address the concern raised in f0bb5474cff0. Why don't we also do this...
-----
fs/btrfs/volumes.c +0 -1
@@ -1133,7 +1133,6 @@ static void btrfs_close_one_device(struct btrfs_device *device) device->fs_info =NULL ;atomic_set (&device->dev_stats_ccnt,0 ); -extent_io_tree_release (&device->alloc_state);/* * Reset the flush error record. We might have a transient
flush error
-----
Thanks, Anand
Re: [PATCH 2/2] btrfs: make btrfs_free_device() static
In reply to [email protected]Quoted 6 lines · On 27/04/2023 01:13, [email protected] wrote:
From: Filipe Manana <[email protected]>
The function
btrfs_free_device()is never used outside ofvolumes.c, so make it static and remove its prototype declaration atvolumes.h.Signed-off-by Filipe Manana <[email protected]>
Re: [PATCH 1/2] btrfs: fix leak of source device allocation state after device replace
In reply to Anand JainQuoted 53 lines · Anand Jain
Quoted 16 lines · On 27/04/2023 01:13, [email protected] wrote:
From: Filipe Manana <[email protected]>
When a device replace finishes, the source device is freed by calling
btrfs_free_device()atbtrfs_rm_dev_replace_free_srcdev(), but the allocation state, tracked in the device'salloc_stateio tree, is never freed.This is a regression recently introduced by commit
f0bb5474cff0("btrfs:
remove redundant release ofbtrfs_device::alloc_state"), which removed a call toextent_io_tree_release()frombtrfs_free_device(), with the rationale thatbtrfs_close_one_device()already releases the allocation state from a device andbtrfs_close_one_device()is always called before a device is freed withbtrfs_free_device(). However that is not true for the device replace case, asbtrfs_free_device()is called without any previous call tobtrfs_close_one_device().Quoted 5 lines
The issue is trivial to reproduce, for example, by running test btrfs/027 from fstests:
$ ./check btrfs/027 $ rmmod btrfsAh, module reload is a useful way to verify. I have now enabled it in the fstests by setting TEST_FS_MODULE_RELOAD=1, and I am able to reproduce the issue.
Quoted 25 lines
$ dmesg (...) [84519.395485] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg started [84519.466224] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg finished [84519.552251] BTRFS info (device sdc): scrub: started on devid 1 [84519.552277] BTRFS info (device sdc): scrub: started on devid 2 [84519.552332] BTRFS info (device sdc): scrub: started on devid 3 [84519.552705] BTRFS info (device sdc): scrub: started on devid 4 [84519.604261] BTRFS info (device sdc): scrub: finished on devid 4 with status: 0 [84519.609374] BTRFS info (device sdc): scrub: finished on devid 3 with status: 0 [84519.610818] BTRFS info (device sdc): scrub: finished on devid 1 with status: 0 [84519.610927] BTRFS info (device sdc): scrub: finished on devid 2 with status: 0 [84559.503795] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 [84559.506764] BTRFS: state leak: start 1048576 end 1347420159 state 1 in tree 1 refs 1 [84559.510294] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1So fix this by adding back the call to
extent_io_tree_release()atbtrfs_free_device().Fixes f0bb5474cff0 ("btrfs: remove redundant release of btrfs_device::alloc_state")Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)
Quoted 16 lines
fs/btrfs/volumes.c void btrfs_free_device(struct btrfs_device *device) · line 401
Quoted 12 lines
fs/btrfs/volumes.c +1
-0@@ -395,6 +395,7 @@ void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name); +extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device); }Hmm. Is this fix incomplete? It does not address the concern raised in
f0bb5474cff0. Why don't we also do this...
What's the concern exactly?
One extra call to extent_io_tree_release() that's actually needed?
fs/btrfs/volumes.c static void btrfs_close_one_device(struct · line 1137
Quoted 12 lines
-----
fs/btrfs/volumes.c +0
-1@@ -1133,7 +1133,6 @@ static void btrfs_close_one_device(struct btrfs_device *device) device->fs_info = NULL;atomic_set (&device->dev_stats_ccnt,0 ); -extent_io_tree_release (&device->alloc_state);
This will change semantics a bit.
Removing this, will make us hold on to more memory for a longer period when fs_devices->opened > 1, so btrfs_free_device() / free_fs_devices() will be called potentially much later.
I'd rather keep the behaviour we had unless there's a stronger reason other than removing 1 line of code.
Thanks.
Quoted 9 lines
fs/btrfs/volumes.c +0
-0/* * Reset the flush error record. We might have a transient flush error
-----Thanks, Anand
Quoted 9 lines · On Wed, Apr 26, 2023 at 06:12:59PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
This fixes a recent regression (on its way to Linus' tree) that results in leaking extent state records in the allocation io tree of a device used as a source device for a device replace. Also unexport
btrfs_free_device().Filipe Manana (2):
btrfs: fix leak of source device allocation state after device replace
btrfs: makebtrfs_free_device()static
Added to misc-next, thanks.
Re: [PATCH 1/2] btrfs: fix leak of source device allocation state after device replace
In reply to Filipe MananaQuoted 71 lines · Filipe Manana
Quoted 53 lines · Anand Jain
Quoted 16 lines · On 27/04/2023 01:13, [email protected] wrote:
From: Filipe Manana <[email protected]>
When a device replace finishes, the source device is freed by calling
btrfs_free_device()atbtrfs_rm_dev_replace_free_srcdev(), but the allocation state, tracked in the device'salloc_stateio tree, is never freed.This is a regression recently introduced by commit
f0bb5474cff0("btrfs:
remove redundant release ofbtrfs_device::alloc_state"), which removed a call toextent_io_tree_release()frombtrfs_free_device(), with the rationale thatbtrfs_close_one_device()already releases the allocation state from a device andbtrfs_close_one_device()is always called before a device is freed withbtrfs_free_device(). However that is not true for the device replace case, asbtrfs_free_device()is called without any previous call tobtrfs_close_one_device().Quoted 5 lines
The issue is trivial to reproduce, for example, by running test btrfs/027 from fstests:
$ ./check btrfs/027 $ rmmod btrfsAh, module reload is a useful way to verify. I have now enabled it in the fstests by setting TEST_FS_MODULE_RELOAD=1, and I am able to reproduce the issue.
Quoted 25 lines
$ dmesg (...) [84519.395485] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg started [84519.466224] BTRFS info (device sdc): dev_replace from <missing disk> (devid 2) to /dev/sdg finished [84519.552251] BTRFS info (device sdc): scrub: started on devid 1 [84519.552277] BTRFS info (device sdc): scrub: started on devid 2 [84519.552332] BTRFS info (device sdc): scrub: started on devid 3 [84519.552705] BTRFS info (device sdc): scrub: started on devid 4 [84519.604261] BTRFS info (device sdc): scrub: finished on devid 4 with status: 0 [84519.609374] BTRFS info (device sdc): scrub: finished on devid 3 with status: 0 [84519.610818] BTRFS info (device sdc): scrub: finished on devid 1 with status: 0 [84519.610927] BTRFS info (device sdc): scrub: finished on devid 2 with status: 0 [84559.503795] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 [84559.506764] BTRFS: state leak: start 1048576 end 1347420159 state 1 in tree 1 refs 1 [84559.510294] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1So fix this by adding back the call to
extent_io_tree_release()atbtrfs_free_device().Fixes f0bb5474cff0 ("btrfs: remove redundant release of btrfs_device::alloc_state")Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+)Quoted 16 lines
fs/btrfs/volumes.c void btrfs_free_device(struct btrfs_device *device) · line 401
Quoted 12 lines
fs/btrfs/volumes.c +1
-0@@ -395,6 +395,7 @@ void btrfs_free_device(struct btrfs_device *device) {WARN_ON (!list_empty (&device->post_commit_list));rcu_string_free (device->name); +extent_io_tree_release (&device->alloc_state);btrfs_destroy_dev_zone_info (device);kfree (device); }Hmm. Is this fix incomplete? It does not address the concern raised in
f0bb5474cff0. Why don't we also do this...
Quoted 2 lines
What's the concern exactly?
One extra call toextent_io_tree_release()that's actually needed?
I'm looking for a solution that meets the need without leading to a situation where extent_io_tree_release() is invoked twice elsewhere.
Quoted 19 lines
fs/btrfs/volumes.c static void btrfs_close_one_device(struct · line 1137
Quoted 13 lines
-----
fs/btrfs/volumes.c +0
-1@@ -1133,7 +1133,6 @@ static void btrfs_close_one_device(struct btrfs_device *device) device->fs_info = NULL;atomic_set (&device->dev_stats_ccnt,0 ); -extent_io_tree_release (&device->alloc_state);This will change semantics a bit.
Removing this, will make us hold on to more memory for a longer period whenfs_devices->opened> 1, sobtrfs_free_device()/free_fs_devices()will be called potentially much later.
Quoted 2 lines
I'd rather keep the behaviour we had unless there's a stronger reason other than removing 1 line of code.
I'm trying to simplify by merging steps in either btrfs_rm_dev_replace_remove_srcdev() or btrfs_rm_dev_replace_free_srcdev() with btrfs_close_one_device().
This could beautifully address the issue. Let's see if it works.
Thanks, Anand
Quoted 14 lines
Thanks.
Quoted 9 lines
fs/btrfs/volumes.c +0
-0/* * Reset the flush error record. We might have a transient flush error
-----Thanks, Anand
Quoted 7 lines · David Sterba
Quoted 6 lines · On Wed, Apr 26, 2023 at 06:12:59PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
This fixes a recent regression (on its way to Linus' tree) that results in leaking extent state records in the allocation io tree of a device used as a source device for a device replace. Also unexport
btrfs_free_device().
Quoted 7 lines
Quoted 3 lines
Filipe Manana (2):
btrfs: fix leak of source device allocation state after device replace
btrfs: makebtrfs_free_device()staticAdded to misc-next, thanks.
Oh, I hope you saw the discussions in the patch 1/2.
We can address both calling extent_io_tree_release() twice and the leak after device replace. Or no need of it?
Quoted 14 lines · Anand Jain
Quoted 11 lines · David Sterba
Quoted 9 lines · On Wed, Apr 26, 2023 at 06:12:59PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
This fixes a recent regression (on its way to Linus' tree) that results in leaking extent state records in the allocation io tree of a device used as a source device for a device replace. Also unexport
btrfs_free_device().Filipe Manana (2):
btrfs: fix leak of source device allocation state after device replace
btrfs: makebtrfs_free_device()static
Quoted 5 lines
Quoted 1 line
Added to misc-next, thanks.
Oh, I hope you saw the discussions in the patch 1/2.
We can address both callingextent_io_tree_release()twice and the leak after device replace. Or no need of it?
Yes I saw the discussion, I'd rather revert back to the known behaviour, this does not seem to be worth the optimization, apparently it's easy to miss some cases.