linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device()
@ 2019-12-04 13:36 Johannes Thumshirn
  2019-12-04 13:36 ` [PATCH v5 1/2] btrfs: decrement number of open devices after closing the device not before Johannes Thumshirn
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Johannes Thumshirn @ 2019-12-04 13:36 UTC (permalink / raw)
  To: David Sterba
  Cc: Nikolay Borisov, Qu Wenruo, Linux BTRFS Mailinglist, Johannes Thumshirn

This series attempts to remove the BUG_ON()s in btrfs_close_one_device().
Therefore some reorganization of btrfs_close_one_device() was needed, to
avoid the memory allocation.

This series has passed fstests without any deviation from the baseline.

Changes to v4:
- Clear dev_stat_ccnt on removal (Dave)
- Don't clear BTRFS_DEV_STATE_MISSING and BTRFS_DEV_STATE_FS_METADATA as
  they'll be handled elsewhere
- Release extent_io_tree (fstests)

Changes to v3:
- Clear BTRFS_DEV_STATE_WRITEABLE after calling btrfs_close_bdev() so
  btrfs_close_bdev() can call sync_blockdev() and invalidate_bdev() (Nikolay)

Changes to v2:
- Completly different approach to the origianl patchset, instead of handling
  eventual allocation failures.
- Dropped already merged patches for ' btrfs_fs_devices::rotating' and
  'btrfs_fs_devices::seeding'
- Kept the 1st patch of the old series, as it's a nice cleanup

Changes to v1:
- Fixed the decremt of btrfs_fs_devices::seeding.
- In addition to this, I've added two patches changing btrfs_fs_devices::seeding
  and btrfs_fs_devices::rotating to bool, as they are in fact used as booleans.



Johannes Thumshirn (2):
  btrfs: decrement number of open devices after closing the device not
    before
  btrfs: reset device back to allocation state when removing

 fs/btrfs/volumes.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)

-- 
2.20.1 (Apple Git-117)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v5 1/2] btrfs: decrement number of open devices after closing the device not before
  2019-12-04 13:36 [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() Johannes Thumshirn
@ 2019-12-04 13:36 ` Johannes Thumshirn
  2019-12-04 13:36 ` [PATCH v5 2/2] btrfs: reset device back to allocation state when removing Johannes Thumshirn
  2019-12-10 10:10 ` [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() David Sterba
  2 siblings, 0 replies; 4+ messages in thread
From: Johannes Thumshirn @ 2019-12-04 13:36 UTC (permalink / raw)
  To: David Sterba
  Cc: Nikolay Borisov, Qu Wenruo, Linux BTRFS Mailinglist, Johannes Thumshirn

From: Johannes Thumshirn <jthumshirn@suse.de>

In btrfs_close_one_device we're decrementing the number of open devices
before we're calling btrfs_close_bdev().

As there is no intermediate exit between these points in this function it
is technically OK to do so, but it makes the code a bit harder to understand.

Move both operations closer together and move the decrement step after
btrfs_close_bdev().

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/volumes.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index c565650639ee..ae3980ba3a87 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1069,9 +1069,6 @@ static void btrfs_close_one_device(struct btrfs_device *device)
 	struct btrfs_device *new_device;
 	struct rcu_string *name;
 
-	if (device->bdev)
-		fs_devices->open_devices--;
-
 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
 	    device->devid != BTRFS_DEV_REPLACE_DEVID) {
 		list_del_init(&device->dev_alloc_list);
@@ -1082,6 +1079,8 @@ static void btrfs_close_one_device(struct btrfs_device *device)
 		fs_devices->missing_devices--;
 
 	btrfs_close_bdev(device);
+	if (device->bdev)
+		fs_devices->open_devices--;
 
 	new_device = btrfs_alloc_device(NULL, &device->devid,
 					device->uuid);
-- 
2.20.1 (Apple Git-117)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v5 2/2] btrfs: reset device back to allocation state when removing
  2019-12-04 13:36 [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() Johannes Thumshirn
  2019-12-04 13:36 ` [PATCH v5 1/2] btrfs: decrement number of open devices after closing the device not before Johannes Thumshirn
@ 2019-12-04 13:36 ` Johannes Thumshirn
  2019-12-10 10:10 ` [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() David Sterba
  2 siblings, 0 replies; 4+ messages in thread
From: Johannes Thumshirn @ 2019-12-04 13:36 UTC (permalink / raw)
  To: David Sterba
  Cc: Nikolay Borisov, Qu Wenruo, Linux BTRFS Mailinglist, Johannes Thumshirn

From: Johannes Thumshirn <jthumshirn@suse.de>

When closing a device, btrfs_close_one_device() first allocates a new
device, copies the device to close's name, replaces it in the dev_list
with the copy and then finally frees it.

This involves two memory allocation, which can potentially fail. As this
code path is tricky to unwind, the allocation failures where handled by
BUG_ON()s.

But this copying isn't strictly needed, all that is needed is resetting
the device in question to it's state it had after the allocation.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>

---
Changes to v4:
- Clear dev_stat_ccnt on removal (Dave)
- Don't clear BTRFS_DEV_STATE_MISSING and BTRFS_DEV_STATE_FS_METADATA as
  they'll be handled elsewhere
- Release extent_io_tree (fstests)

Changes to v3:
- Clear DEV_STATE_WRITABLE _after_ btrfs_close_bdev() (Nik)
---
 fs/btrfs/volumes.c | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index ae3980ba3a87..eef0b9ed9ea4 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1066,8 +1066,6 @@ static void btrfs_close_bdev(struct btrfs_device *device)
 static void btrfs_close_one_device(struct btrfs_device *device)
 {
 	struct btrfs_fs_devices *fs_devices = device->fs_devices;
-	struct btrfs_device *new_device;
-	struct rcu_string *name;
 
 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
 	    device->devid != BTRFS_DEV_REPLACE_DEVID) {
@@ -1079,25 +1077,21 @@ static void btrfs_close_one_device(struct btrfs_device *device)
 		fs_devices->missing_devices--;
 
 	btrfs_close_bdev(device);
-	if (device->bdev)
+	if (device->bdev) {
 		fs_devices->open_devices--;
-
-	new_device = btrfs_alloc_device(NULL, &device->devid,
-					device->uuid);
-	BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
-
-	/* Safe because we are under uuid_mutex */
-	if (device->name) {
-		name = rcu_string_strdup(device->name->str, GFP_NOFS);
-		BUG_ON(!name); /* -ENOMEM */
-		rcu_assign_pointer(new_device->name, name);
+		device->bdev = NULL;
 	}
+	clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 
-	list_replace_rcu(&device->dev_list, &new_device->dev_list);
-	new_device->fs_devices = device->fs_devices;
+	atomic_set(&device->dev_stats_ccnt, 0);
+	extent_io_tree_release(&device->alloc_state);
 
-	synchronize_rcu();
-	btrfs_free_device(device);
+	/* Verify the device is back in a pristine state  */
+	ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
+	ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
+	ASSERT(list_empty(&device->dev_alloc_list));
+	ASSERT(list_empty(&device->post_commit_list));
+	ASSERT(atomic_read(&device->reada_in_flight) == 0);
 }
 
 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
-- 
2.20.1 (Apple Git-117)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device()
  2019-12-04 13:36 [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() Johannes Thumshirn
  2019-12-04 13:36 ` [PATCH v5 1/2] btrfs: decrement number of open devices after closing the device not before Johannes Thumshirn
  2019-12-04 13:36 ` [PATCH v5 2/2] btrfs: reset device back to allocation state when removing Johannes Thumshirn
@ 2019-12-10 10:10 ` David Sterba
  2 siblings, 0 replies; 4+ messages in thread
From: David Sterba @ 2019-12-10 10:10 UTC (permalink / raw)
  To: Johannes Thumshirn
  Cc: David Sterba, Nikolay Borisov, Qu Wenruo, Linux BTRFS Mailinglist

On Wed, Dec 04, 2019 at 02:36:37PM +0100, Johannes Thumshirn wrote:
> This series attempts to remove the BUG_ON()s in btrfs_close_one_device().
> Therefore some reorganization of btrfs_close_one_device() was needed, to
> avoid the memory allocation.
> 
> This series has passed fstests without any deviation from the baseline.
> 
> Changes to v4:
> - Clear dev_stat_ccnt on removal (Dave)
> - Don't clear BTRFS_DEV_STATE_MISSING and BTRFS_DEV_STATE_FS_METADATA as
>   they'll be handled elsewhere
> - Release extent_io_tree (fstests)

Passed fstests, I don't see any other obvious problems so I'll add it to
misc-next.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-10 10:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-04 13:36 [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() Johannes Thumshirn
2019-12-04 13:36 ` [PATCH v5 1/2] btrfs: decrement number of open devices after closing the device not before Johannes Thumshirn
2019-12-04 13:36 ` [PATCH v5 2/2] btrfs: reset device back to allocation state when removing Johannes Thumshirn
2019-12-10 10:10 ` [PATCH v5 0/2] remove BUG_ON()s in btrfs_close_one_device() David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).