dm-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/13] Zone write plugging fixes and cleanup
@ 2024-04-30 12:51 Damien Le Moal
  2024-04-30 12:51 ` [PATCH 01/13] dm: Check that a zoned table leads to a valid mapped device Damien Le Moal
                   ` (13 more replies)
  0 siblings, 14 replies; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Jens, Mike,

With more testing of zone write plugging on more device setups,
including weird/test setups (with scsi debug and null_blk), several
issues were identified. This patch series addresses them and cleanup the
code a little to try to make it more obvious.

The first patch is a DM modification to not expose zoned devices
composed solely of conventional zones as zoned block devices. The second
patch is a fix partly related to this to expose a correct max open zones
limit for devices with no limits (which DM devices are as the max
open/active limit is not propagated to the mapped device as there is no
easy way to do that).

Patches 3 to 9 are bug fixes. The most serious problem among theses was
detected with tests using scsi_debug zoned devices and is fixed in
patch 7.

Patches 10 to 13 improve and cleanup the code.

Damien Le Moal (13):
  dm: Check that a zoned table leads to a valid mapped device
  block: Exclude conventional zones when faking max open limit
  block: Fix zone write plug initialization from blk_revalidate_zone_cb()
  block: Fix reference counting for zone write plugs in error state
  block: Hold a reference on zone write plugs to schedule submission
  block: Unhash a zone write plug only if needed
  block: Do not remove zone write plugs still in use
  block: Fix flush request sector restore
  block: Fix handling of non-empty flush write requests to zones
  block: Improve blk_zone_write_plug_bio_merged()
  block: Improve zone write request completion handling
  block: Simplify blk_zone_write_plug_bio_endio()
  block: Simplify zone write plug BIO abort

 block/blk-flush.c     |   3 +-
 block/blk-mq.c        |  12 +-
 block/blk-zoned.c     | 269 +++++++++++++++++++++++++++---------------
 block/blk.h           |  12 +-
 drivers/md/dm-table.c |   3 +-
 drivers/md/dm-zone.c  |  53 +++++++++
 6 files changed, 243 insertions(+), 109 deletions(-)

-- 
2.44.0


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

* [PATCH 01/13] dm: Check that a zoned table leads to a valid mapped device
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 12:51 ` [PATCH 02/13] block: Exclude conventional zones when faking max open limit Damien Le Moal
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Using targets such as dm-linear, a mapped device can be created to
contain only conventional zones. Such device should not be treated as
zoned as it does not contain any mandatory sequential write required
zone. Since such device can be randomly written, we can modify
dm_set_zones_restrictions() to set the mapped device zoned queue limit
to false to expose it as a regular block device. The function
dm_check_zoned() does this after counting the number of conventional
zones of the mapped device and comparing it to the total number of zones
reported. The special dm_check_zoned_cb() report zones callback function
is used to count conventional zones.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 drivers/md/dm-table.c |  3 ++-
 drivers/md/dm-zone.c  | 53 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 41f1d731ae5a..2c6fbd87363f 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -2042,7 +2042,8 @@ int dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 		r = dm_set_zones_restrictions(t, q);
 		if (r)
 			return r;
-		if (!static_key_enabled(&zoned_enabled.key))
+		if (blk_queue_is_zoned(q) &&
+		    !static_key_enabled(&zoned_enabled.key))
 			static_branch_enable(&zoned_enabled);
 	}
 
diff --git a/drivers/md/dm-zone.c b/drivers/md/dm-zone.c
index d17ae4486a6a..3071f67d72aa 100644
--- a/drivers/md/dm-zone.c
+++ b/drivers/md/dm-zone.c
@@ -145,6 +145,48 @@ bool dm_is_zone_write(struct mapped_device *md, struct bio *bio)
 	}
 }
 
+/*
+ * Count conventional zones of a mapped zoned device. If the device
+ * only has conventional zones, do not expose it as zoned.
+ */
+static int dm_check_zoned_cb(struct blk_zone *zone, unsigned int idx,
+			     void *data)
+{
+	unsigned int *nr_conv_zones = data;
+
+	if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
+		(*nr_conv_zones)++;
+
+	return 0;
+}
+
+static int dm_check_zoned(struct mapped_device *md, struct dm_table *t)
+{
+	struct gendisk *disk = md->disk;
+	unsigned int nr_conv_zones = 0;
+	int ret;
+
+	/* Revalidate only if something changed. */
+	md->zone_revalidate_map = t;
+	ret = dm_blk_report_zones(disk, 0, UINT_MAX,
+				  dm_check_zoned_cb, &nr_conv_zones);
+	md->zone_revalidate_map = NULL;
+	if (ret < 0) {
+		DMERR("Check zoned failed %d", ret);
+		return ret;
+	}
+
+	if (nr_conv_zones >= ret) {
+		disk->queue->limits.max_open_zones = 0;
+		disk->queue->limits.max_active_zones = 0;
+		disk->queue->limits.zoned = false;
+		clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
+		disk->nr_zones = 0;
+	}
+
+	return 0;
+}
+
 /*
  * Revalidate the zones of a mapped device to initialize resource necessary
  * for zone append emulation. Note that we cannot simply use the block layer
@@ -208,6 +250,7 @@ static bool dm_table_supports_zone_append(struct dm_table *t)
 int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q)
 {
 	struct mapped_device *md = t->md;
+	int ret;
 
 	/*
 	 * Check if zone append is natively supported, and if not, set the
@@ -224,6 +267,16 @@ int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q)
 	if (!get_capacity(md->disk))
 		return 0;
 
+	/*
+	 * Check that the mapped device will indeed be zoned, that is, that it
+	 * has sequential write required zones.
+	 */
+	ret = dm_check_zoned(md, t);
+	if (ret)
+		return ret;
+	if (!blk_queue_is_zoned(q))
+		return 0;
+
 	if (!md->disk->nr_zones) {
 		DMINFO("%s using %s zone append",
 		       md->disk->disk_name,
-- 
2.44.0


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

* [PATCH 02/13] block: Exclude conventional zones when faking max open limit
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
  2024-04-30 12:51 ` [PATCH 01/13] dm: Check that a zoned table leads to a valid mapped device Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:24   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb() Damien Le Moal
                   ` (11 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

For a device that has no limits for the maximum number of open and
active zones, we default to using the number of zones, limited to
BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE (128), for the maximum number of open
zones indicated to the user. However, for a device that has conventional
zones and less zones than BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE, we should
not account conventional zones and set the limit to the number of
sequential write required zones. Furthermore, for cases where the limit
is equal to the number of sequential write required zones, we can
advertize a limit of 0 to indicate "no limits".

Fix this by moving the zone write plug mempool resizing from
disk_revalidate_zone_resources() to disk_update_zone_resources() where
we can safely compute the number of conventional zones and update the
limits.

Fixes: 843283e96e5a ("block: Fake max open zones limit when there is no limit")
Reported-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index bad68277c0b2..6cf3e319513c 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -1513,10 +1513,6 @@ static int disk_revalidate_zone_resources(struct gendisk *disk,
 	if (!disk->zone_wplugs_hash)
 		return disk_alloc_zone_resources(disk, pool_size);
 
-	/* Resize the zone write plug memory pool if needed. */
-	if (disk->zone_wplugs_pool->min_nr != pool_size)
-		return mempool_resize(disk->zone_wplugs_pool, pool_size);
-
 	return 0;
 }
 
@@ -1536,27 +1532,51 @@ static int disk_update_zone_resources(struct gendisk *disk,
 				      struct blk_revalidate_zone_args *args)
 {
 	struct request_queue *q = disk->queue;
+	unsigned int nr_seq_zones, nr_conv_zones = 0;
+	unsigned int pool_size;
 	struct queue_limits lim;
 
 	disk->nr_zones = args->nr_zones;
 	disk->zone_capacity = args->zone_capacity;
 	swap(disk->conv_zones_bitmap, args->conv_zones_bitmap);
+	if (disk->conv_zones_bitmap)
+		nr_conv_zones = bitmap_weight(disk->conv_zones_bitmap,
+					      disk->nr_zones);
+	if (nr_conv_zones >= disk->nr_zones) {
+		pr_warn("%s: Invalid number of conventional zones %u / %u\n",
+			disk->disk_name, nr_conv_zones, disk->nr_zones);
+		return -ENODEV;
+	}
+
+	if (!disk->zone_wplugs_pool)
+		return 0;
 
 	/*
-	 * If the device has no limit on the maximum number of open and active
+	 * If the device has no limits on the maximum number of open and active
 	 * zones, set its max open zone limit to the mempool size to indicate
 	 * to the user that there is a potential performance impact due to
 	 * dynamic zone write plug allocation when simultaneously writing to
 	 * more zones than the size of the mempool.
 	 */
-	if (disk->zone_wplugs_pool) {
-		lim = queue_limits_start_update(q);
-		if (!lim.max_open_zones && !lim.max_active_zones)
-			lim.max_open_zones = disk->zone_wplugs_pool->min_nr;
-		return queue_limits_commit_update(q, &lim);
+	lim = queue_limits_start_update(q);
+
+	nr_seq_zones = disk->nr_zones - nr_conv_zones;
+	pool_size = max(lim.max_open_zones, lim.max_active_zones);
+	if (!pool_size)
+		pool_size = min(BLK_ZONE_WPLUG_DEFAULT_POOL_SIZE, nr_seq_zones);
+
+	/* Resize the zone write plug memory pool if needed. */
+	if (disk->zone_wplugs_pool->min_nr != pool_size)
+		mempool_resize(disk->zone_wplugs_pool, pool_size);
+
+	if (!lim.max_open_zones && !lim.max_active_zones) {
+		if (pool_size < nr_seq_zones)
+			lim.max_open_zones = pool_size;
+		else
+			lim.max_open_zones = 0;
 	}
 
-	return 0;
+	return queue_limits_commit_update(q, &lim);
 }
 
 /*
-- 
2.44.0


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

* [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb()
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
  2024-04-30 12:51 ` [PATCH 01/13] dm: Check that a zoned table leads to a valid mapped device Damien Le Moal
  2024-04-30 12:51 ` [PATCH 02/13] block: Exclude conventional zones when faking max open limit Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:26   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 04/13] block: Fix reference counting for zone write plugs in error state Damien Le Moal
                   ` (10 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

When revalidating the zones of a zoned block device,
blk_revalidate_zone_cb() must allocate a zone write plug for any
sequential write required zone that is not empty nor full. However, the
current code tests the latter case by comparing the zone write pointer
offset to the zone size instead of the zone capacity. Furthermore,
disk_get_and_lock_zone_wplug() is called with a sector argument equal to
the zone start instead of the current zone write pointer position.
This commit fixes both issues by calling disk_get_and_lock_zone_wplug()
for a zone that is not empty and with a write pointer offset lower than
the zone capacity and use the zone capacity sector as the sector
argument for disk_get_and_lock_zone_wplug().

Fixes: dd291d77cc90 ("block: Introduce zone write plugging")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 6cf3e319513c..e92ae0729cf8 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -1666,10 +1666,11 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
 		 * empty nor full. So make sure we have a zone write plug for
 		 * such zone if the device has a zone write plug hash table.
 		 */
+		if (!disk->zone_wplugs_hash)
+			break;
 		wp_offset = blk_zone_wp_offset(zone);
-		if (disk->zone_wplugs_hash &&
-		    wp_offset && wp_offset < zone_sectors) {
-			zwplug = disk_get_and_lock_zone_wplug(disk, zone->start,
+		if (wp_offset && wp_offset < zone->capacity) {
+			zwplug = disk_get_and_lock_zone_wplug(disk, zone->wp,
 							      GFP_NOIO, &flags);
 			if (!zwplug)
 				return -ENOMEM;
-- 
2.44.0


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

* [PATCH 04/13] block: Fix reference counting for zone write plugs in error state
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (2 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb() Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:28   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 05/13] block: Hold a reference on zone write plugs to schedule submission Damien Le Moal
                   ` (9 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

When zone is reset or finished, disk_zone_wplug_set_wp_offset() is
called to update the zone write plug write pointer offset and to clear
the zone error state (BLK_ZONE_WPLUG_ERROR flag) if it is set.
However, this processing is missing dropping the reference to the zone
write plug that was taken in disk_zone_wplug_set_error() when the error
flag was first set. Furthermore, the error state handling must release
the zone write plug lock to first execute a report zones command. When
the report zone races with a reset or finish operation that clears the
error, we can end up decrementing the zone write plug reference count
twice: once in disk_zone_wplug_set_wp_offset() for the reset/finish
operation and one more time in disk_zone_wplugs_work() once
disk_zone_wplug_handle_error() completes.

Fix this by introducing disk_zone_wplug_clear_error() as the symmetric
function of disk_zone_wplug_set_error(). disk_zone_wplug_clear_error()
decrements the zone write plug reference count obtained in
disk_zone_wplug_set_error() only if the error handling has not started
yet, that is, only if disk_zone_wplugs_work() has not yet taken the zone
write plug off the error list. This ensure that either
disk_zone_wplug_clear_error() or disk_zone_wplugs_work() drop the zone
write plug reference count.

Fixes: dd291d77cc90 ("block: Introduce zone write plugging")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 74 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 48 insertions(+), 26 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index e92ae0729cf8..9bded29592e0 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -658,6 +658,53 @@ static void disk_zone_wplug_abort_unaligned(struct gendisk *disk,
 	bio_list_merge(&zwplug->bio_list, &bl);
 }
 
+static inline void disk_zone_wplug_set_error(struct gendisk *disk,
+					     struct blk_zone_wplug *zwplug)
+{
+	if (!(zwplug->flags & BLK_ZONE_WPLUG_ERROR)) {
+		unsigned long flags;
+
+		/*
+		 * At this point, we already have a reference on the zone write
+		 * plug. However, since we are going to add the plug to the disk
+		 * zone write plugs work list, increase its reference count.
+		 * This reference will be dropped in disk_zone_wplugs_work()
+		 * once the error state is handled, or
+		 * in disk_zone_wplug_clear_error() if the zone is reset or
+		 * finished.
+		 */
+		zwplug->flags |= BLK_ZONE_WPLUG_ERROR;
+		atomic_inc(&zwplug->ref);
+
+		spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
+		list_add_tail(&zwplug->link, &disk->zone_wplugs_err_list);
+		spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
+	}
+}
+
+static inline void disk_zone_wplug_clear_error(struct gendisk *disk,
+					       struct blk_zone_wplug *zwplug)
+{
+	if (zwplug->flags & BLK_ZONE_WPLUG_ERROR) {
+		unsigned long flags;
+
+		/*
+		 * We are racing with the error handling work which drops
+		 * the reference on the zone write plug after handling the error
+		 * state. So remove the plug from the error list and drop its
+		 * reference count only if the error handling has not yet
+		 * started, that is, if the zone write plug is still listed.
+		 */
+		spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
+		if (!list_empty(&zwplug->link)) {
+			list_del_init(&zwplug->link);
+			zwplug->flags &= ~BLK_ZONE_WPLUG_ERROR;
+			disk_put_zone_wplug(zwplug);
+		}
+		spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
+	}
+}
+
 /*
  * Set a zone write plug write pointer offset to either 0 (zone reset case)
  * or to the zone size (zone finish case). This aborts all plugged BIOs, which
@@ -691,12 +738,7 @@ static void disk_zone_wplug_set_wp_offset(struct gendisk *disk,
 	 * in a good state. So clear the error flag and decrement the
 	 * error count if we were in error state.
 	 */
-	if (zwplug->flags & BLK_ZONE_WPLUG_ERROR) {
-		zwplug->flags &= ~BLK_ZONE_WPLUG_ERROR;
-		spin_lock(&disk->zone_wplugs_lock);
-		list_del_init(&zwplug->link);
-		spin_unlock(&disk->zone_wplugs_lock);
-	}
+	disk_zone_wplug_clear_error(disk, zwplug);
 
 	/*
 	 * The zone write plug now has no BIO plugged: remove it from the
@@ -885,26 +927,6 @@ void blk_zone_write_plug_attempt_merge(struct request *req)
 	spin_unlock_irqrestore(&zwplug->lock, flags);
 }
 
-static inline void disk_zone_wplug_set_error(struct gendisk *disk,
-					     struct blk_zone_wplug *zwplug)
-{
-	if (!(zwplug->flags & BLK_ZONE_WPLUG_ERROR)) {
-		unsigned long flags;
-
-		/*
-		 * Increase the plug reference count. The reference will be
-		 * dropped in disk_zone_wplugs_work() once the error state
-		 * is handled.
-		 */
-		zwplug->flags |= BLK_ZONE_WPLUG_ERROR;
-		atomic_inc(&zwplug->ref);
-
-		spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
-		list_add_tail(&zwplug->link, &disk->zone_wplugs_err_list);
-		spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
-	}
-}
-
 /*
  * Check and prepare a BIO for submission by incrementing the write pointer
  * offset of its zone write plug and changing zone append operations into
-- 
2.44.0


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

* [PATCH 05/13] block: Hold a reference on zone write plugs to schedule submission
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (3 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 04/13] block: Fix reference counting for zone write plugs in error state Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:30   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 06/13] block: Unhash a zone write plug only if needed Damien Le Moal
                   ` (8 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Since a zone write plug BIO work is a field of struct blk_zone_wplug, we
must ensure that a zone write plug is never freed when its BIO
submission work is queued or running. Do this by holding a reference on
the zone write plug when the submission work is scheduled for execution
with queue_work() and releasing the reference at the end of the
execution of the work function blk_zone_wplug_bio_work().
The helper function disk_zone_wplug_schedule_bio_work() is introduced to
get a reference on a zone write plug and queue its work. This helper is
used in disk_zone_wplug_unplug_bio() and disk_zone_wplug_handle_error().

Fixes: dd291d77cc90 ("block: Introduce zone write plugging")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 9bded29592e0..03555ea64774 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -1131,6 +1131,19 @@ bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
 }
 EXPORT_SYMBOL_GPL(blk_zone_plug_bio);
 
+static void disk_zone_wplug_schedule_bio_work(struct gendisk *disk,
+					      struct blk_zone_wplug *zwplug)
+{
+	/*
+	 * Take a reference on the zone write plug and schedule the submission
+	 * of the next plugged BIO. blk_zone_wplug_bio_work() will release the
+	 * reference we take here.
+	 */
+	WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED));
+	atomic_inc(&zwplug->ref);
+	queue_work(disk->zone_wplugs_wq, &zwplug->bio_work);
+}
+
 static void disk_zone_wplug_unplug_bio(struct gendisk *disk,
 				       struct blk_zone_wplug *zwplug)
 {
@@ -1150,8 +1163,8 @@ static void disk_zone_wplug_unplug_bio(struct gendisk *disk,
 
 	/* Schedule submission of the next plugged BIO if we have one. */
 	if (!bio_list_empty(&zwplug->bio_list)) {
+		disk_zone_wplug_schedule_bio_work(disk, zwplug);
 		spin_unlock_irqrestore(&zwplug->lock, flags);
-		queue_work(disk->zone_wplugs_wq, &zwplug->bio_work);
 		return;
 	}
 
@@ -1251,14 +1264,14 @@ static void blk_zone_wplug_bio_work(struct work_struct *work)
 	if (!bio) {
 		zwplug->flags &= ~BLK_ZONE_WPLUG_PLUGGED;
 		spin_unlock_irqrestore(&zwplug->lock, flags);
-		return;
+		goto put_zwplug;
 	}
 
 	if (!blk_zone_wplug_prepare_bio(zwplug, bio)) {
 		/* Error recovery will decide what to do with the BIO. */
 		bio_list_add_head(&zwplug->bio_list, bio);
 		spin_unlock_irqrestore(&zwplug->lock, flags);
-		return;
+		goto put_zwplug;
 	}
 
 	spin_unlock_irqrestore(&zwplug->lock, flags);
@@ -1274,6 +1287,10 @@ static void blk_zone_wplug_bio_work(struct work_struct *work)
 	 */
 	if (bdev->bd_has_submit_bio)
 		blk_queue_exit(bdev->bd_disk->queue);
+
+put_zwplug:
+	/* Drop the reference we took in disk_zone_wplug_schedule_bio_work(). */
+	disk_put_zone_wplug(zwplug);
 }
 
 static unsigned int blk_zone_wp_offset(struct blk_zone *zone)
@@ -1353,8 +1370,7 @@ static void disk_zone_wplug_handle_error(struct gendisk *disk,
 
 	/* Restart BIO submission if we still have any BIO left. */
 	if (!bio_list_empty(&zwplug->bio_list)) {
-		WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_PLUGGED));
-		queue_work(disk->zone_wplugs_wq, &zwplug->bio_work);
+		disk_zone_wplug_schedule_bio_work(disk, zwplug);
 		goto unlock;
 	}
 
-- 
2.44.0


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

* [PATCH 06/13] block: Unhash a zone write plug only if needed
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (4 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 05/13] block: Hold a reference on zone write plugs to schedule submission Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:31   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 07/13] block: Do not remove zone write plugs still in use Damien Le Moal
                   ` (7 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Fix disk_remove_zone_wplug() to ensure that a zone write plug already
removed from a disk hash table of zone write plugs is not removed
again. Do this by checking the BLK_ZONE_WPLUG_UNHASHED flag of the plug
and calling hlist_del_init_rcu() only if the flag is not set.

Furthermore, since BIO completions can happen at any time, that is,
decrementing of the zone write plug reference count can happen at any
time, make sure to use disk_put_zone_wplug() instead of atomic_dec() to
ensure that the zone write plug is freed when its last reference is
dropped. In order to do this, disk_remove_zone_wplug() is moved after
the definition of disk_put_zone_wplug(). disk_should_remove_zone_wplug()
is moved as well to keep it together with disk_remove_zone_wplug().

To be consistent with this change, add a check in disk_put_zone_wplug()
to ensure that a zone write plug being freed was already removed from
the disk hash table.

Fixes: dd291d77cc90 ("block: Introduce zone write plugging")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 54 +++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 23 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 03555ea64774..82e540dad900 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -476,29 +476,6 @@ static bool disk_insert_zone_wplug(struct gendisk *disk,
 	return true;
 }
 
-static void disk_remove_zone_wplug(struct gendisk *disk,
-				   struct blk_zone_wplug *zwplug)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
-	zwplug->flags |= BLK_ZONE_WPLUG_UNHASHED;
-	atomic_dec(&zwplug->ref);
-	hlist_del_init_rcu(&zwplug->node);
-	spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
-}
-
-static inline bool disk_should_remove_zone_wplug(struct gendisk *disk,
-						 struct blk_zone_wplug *zwplug)
-{
-	/* If the zone is still busy, the plug cannot be removed. */
-	if (zwplug->flags & BLK_ZONE_WPLUG_BUSY)
-		return false;
-
-	/* We can remove zone write plugs for zones that are empty or full. */
-	return !zwplug->wp_offset || zwplug->wp_offset >= disk->zone_capacity;
-}
-
 static struct blk_zone_wplug *disk_get_zone_wplug(struct gendisk *disk,
 						  sector_t sector)
 {
@@ -534,11 +511,42 @@ static inline void disk_put_zone_wplug(struct blk_zone_wplug *zwplug)
 	if (atomic_dec_and_test(&zwplug->ref)) {
 		WARN_ON_ONCE(!bio_list_empty(&zwplug->bio_list));
 		WARN_ON_ONCE(!list_empty(&zwplug->link));
+		WARN_ON_ONCE(!(zwplug->flags & BLK_ZONE_WPLUG_UNHASHED));
 
 		call_rcu(&zwplug->rcu_head, disk_free_zone_wplug_rcu);
 	}
 }
 
+static inline bool disk_should_remove_zone_wplug(struct gendisk *disk,
+						 struct blk_zone_wplug *zwplug)
+{
+	/* If the zone is still busy, the plug cannot be removed. */
+	if (zwplug->flags & BLK_ZONE_WPLUG_BUSY)
+		return false;
+
+	/* We can remove zone write plugs for zones that are empty or full. */
+	return !zwplug->wp_offset || zwplug->wp_offset >= disk->zone_capacity;
+}
+
+static void disk_remove_zone_wplug(struct gendisk *disk,
+				   struct blk_zone_wplug *zwplug)
+{
+	if (!(zwplug->flags & BLK_ZONE_WPLUG_UNHASHED)) {
+		unsigned long flags;
+
+		/*
+		 * Mark the zone write plug as unhashed and drop the extra
+		 * reference we took when the plug was inserted in the hash
+		 * table.
+		 */
+		zwplug->flags |= BLK_ZONE_WPLUG_UNHASHED;
+		spin_lock_irqsave(&disk->zone_wplugs_lock, flags);
+		hlist_del_init_rcu(&zwplug->node);
+		spin_unlock_irqrestore(&disk->zone_wplugs_lock, flags);
+		disk_put_zone_wplug(zwplug);
+	}
+}
+
 static void blk_zone_wplug_bio_work(struct work_struct *work);
 
 /*
-- 
2.44.0


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

* [PATCH 07/13] block: Do not remove zone write plugs still in use
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (5 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 06/13] block: Unhash a zone write plug only if needed Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:34   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 08/13] block: Fix flush request sector restore Damien Le Moal
                   ` (6 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Large write BIOs that span a zone boundary are split in
blk_mq_submit_bio() before being passed to blk_zone_plug_bio() for zone
write plugging. Such split BIO will be chained with one fragment
targeting one zone and the remainder of the BIO tergetting the next
zone. The two BIOs can be executed in parallel, without a predetermine
order relative to eachother and their completion may be reversed: the
remainder first completing and the first fragment then completing. In
such case, bio_endio() will not immediately execute
blk_zone_write_plug_bio_endio() for the parent BIO (the remainder of the
split BIO) as the BIOs are chained. blk_zone_write_plug_bio_endio() for
the parent BIO will be executed only once the first fragment completes.

In the case of a device with small zones and very large BIOs, uch
completion pattern can lead to disk_should_remove_zone_wplug() to return
true for the zone of the parent BIO when the parent BIO request
completes and blk_zone_write_plug_complete_request() is executed. This
triggers the removal of the zone write plug from the hash table using
disk_remove_zone_wplug(). With the zone write plug of the parent BIO
missing, the call to disk_get_zone_wplug() in
blk_zone_write_plug_bio_endio() returns NULL and triggers a warning.

This patterns can be recreated fairly easily using a scsi_debug device
with small zone and btrfs. E.g.

modprobe scsi_debug delay=0 dev_size_mb=1024 sector_size=4096 \
	zbc=host-managed zone_cap_mb=3 zone_nr_conv=0 zone_size_mb=4
mkfs.btrfs -f -O zoned /dev/sda
mount -t btrfs /dev/sda /mnt
fio --name=wrtest --rw=randwrite --direct=1 --ioengine=libaio \
	--bs=4k --iodepth=16 --size=1M --directory=/mnt --time_based \
	--runtime=10
umount /dev/sda

Will result in the warning:

[   29.035538] WARNING: CPU: 3 PID: 37 at block/blk-zoned.c:1207 blk_zone_write_plug_bio_endio+0xee/0x1e0
...
[   29.058682] Call Trace:
[   29.059095]  <TASK>
[   29.059473]  ? __warn+0x80/0x120
[   29.059983]  ? blk_zone_write_plug_bio_endio+0xee/0x1e0
[   29.060728]  ? report_bug+0x160/0x190
[   29.061283]  ? handle_bug+0x36/0x70
[   29.061830]  ? exc_invalid_op+0x17/0x60
[   29.062399]  ? asm_exc_invalid_op+0x1a/0x20
[   29.063025]  ? blk_zone_write_plug_bio_endio+0xee/0x1e0
[   29.063760]  bio_endio+0xb7/0x150
[   29.064280]  btrfs_clone_write_end_io+0x2b/0x60 [btrfs]
[   29.065049]  blk_update_request+0x17c/0x500
[   29.065666]  scsi_end_request+0x27/0x1a0 [scsi_mod]
[   29.066356]  scsi_io_completion+0x5b/0x690 [scsi_mod]
[   29.067077]  blk_complete_reqs+0x3a/0x50
[   29.067692]  __do_softirq+0xcf/0x2b3
[   29.068248]  ? sort_range+0x20/0x20
[   29.068791]  run_ksoftirqd+0x1c/0x30
[   29.069339]  smpboot_thread_fn+0xcc/0x1b0
[   29.069936]  kthread+0xcf/0x100
[   29.070438]  ? kthread_complete_and_exit+0x20/0x20
[   29.071314]  ret_from_fork+0x31/0x50
[   29.071873]  ? kthread_complete_and_exit+0x20/0x20
[   29.072563]  ret_from_fork_asm+0x11/0x20
[   29.073146]  </TASK>

either when fio executes or when unmount is executed.

Fix this by modifying disk_should_remove_zone_wplug() to check that the
reference count to a zone write plug is not larger than 2, that is, that
the only references left on the zone are the caller held reference
(blk_zone_write_plug_complete_request()) and the initial extra reference
for the zone write plug taken when it was initialized (and that is
dropped when the zone write plug is removed from the hash table).

To be consistent with this change, make sure to drop the request or BIO
held reference to the zone write plug before calling
disk_zone_wplug_unplug_bio(). All references are also dropped using
disk_put_zone_wplug() instead of atomic_dec() to ensure that the zone
write plug is freed if it needs to be.

Comments are also improved to clarify zone write plugs reference
handling.

Reported-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Fixes: dd291d77cc90 ("block: Introduce zone write plugging")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 82e540dad900..5792e3b160c9 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -520,8 +520,9 @@ static inline void disk_put_zone_wplug(struct blk_zone_wplug *zwplug)
 static inline bool disk_should_remove_zone_wplug(struct gendisk *disk,
 						 struct blk_zone_wplug *zwplug)
 {
-	/* If the zone is still busy, the plug cannot be removed. */
-	if (zwplug->flags & BLK_ZONE_WPLUG_BUSY)
+	/* If the zone write plug is still busy, it cannot be removed. */
+	if ((zwplug->flags & BLK_ZONE_WPLUG_BUSY) ||
+	    atomic_read(&zwplug->ref) > 2)
 		return false;
 
 	/* We can remove zone write plugs for zones that are empty or full. */
@@ -891,8 +892,9 @@ void blk_zone_write_plug_attempt_merge(struct request *req)
 	struct bio *bio;
 
 	/*
-	 * Completion of this request needs to be handled with
-	 * blk_zone_write_plug_complete_request().
+	 * Indicate that completion of this request needs to be handled with
+	 * blk_zone_write_plug_complete_request(), which will drop the reference
+	 * on the zone write plug we took above on entry to this function.
 	 */
 	req->rq_flags |= RQF_ZONE_WRITE_PLUGGING;
 
@@ -1221,6 +1223,9 @@ void blk_zone_write_plug_bio_endio(struct bio *bio)
 		spin_unlock_irqrestore(&zwplug->lock, flags);
 	}
 
+	/* Drop the reference we took when the BIO was issued. */
+	disk_put_zone_wplug(zwplug);
+
 	/*
 	 * For BIO-based devices, blk_zone_write_plug_complete_request()
 	 * is not called. So we need to schedule execution of the next
@@ -1229,8 +1234,7 @@ void blk_zone_write_plug_bio_endio(struct bio *bio)
 	if (bio->bi_bdev->bd_has_submit_bio)
 		disk_zone_wplug_unplug_bio(disk, zwplug);
 
-	/* Drop the reference we took when the BIO was issued. */
-	atomic_dec(&zwplug->ref);
+	/* Drop the reference we took when entering this function. */
 	disk_put_zone_wplug(zwplug);
 }
 
@@ -1244,13 +1248,15 @@ void blk_zone_write_plug_complete_request(struct request *req)
 
 	req->rq_flags &= ~RQF_ZONE_WRITE_PLUGGING;
 
-	disk_zone_wplug_unplug_bio(disk, zwplug);
-
 	/*
 	 * Drop the reference we took when the request was initialized in
 	 * blk_zone_write_plug_attempt_merge().
 	 */
-	atomic_dec(&zwplug->ref);
+	disk_put_zone_wplug(zwplug);
+
+	disk_zone_wplug_unplug_bio(disk, zwplug);
+
+	/* Drop the reference we took when entering this function. */
 	disk_put_zone_wplug(zwplug);
 }
 
-- 
2.44.0


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

* [PATCH 08/13] block: Fix flush request sector restore
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (6 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 07/13] block: Do not remove zone write plugs still in use Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:26   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 09/13] block: Fix handling of non-empty flush write requests to zones Damien Le Moal
                   ` (5 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Make sure that a request bio is not NULL before trying to restore the
request start sector.

Reported-by: Yi Zhang <yi.zhang@redhat.com>
Fixes: 6f8fd758de63 ("block: Restore sector of flush requests")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-flush.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/block/blk-flush.c b/block/blk-flush.c
index 2f58ae018464..c17cf8ed8113 100644
--- a/block/blk-flush.c
+++ b/block/blk-flush.c
@@ -130,7 +130,8 @@ static void blk_flush_restore_request(struct request *rq)
 	 * original @rq->bio.  Restore it.
 	 */
 	rq->bio = rq->biotail;
-	rq->__sector = rq->bio->bi_iter.bi_sector;
+	if (rq->bio)
+		rq->__sector = rq->bio->bi_iter.bi_sector;
 
 	/* make @rq a normal request */
 	rq->rq_flags &= ~RQF_FLUSH_SEQ;
-- 
2.44.0


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

* [PATCH 09/13] block: Fix handling of non-empty flush write requests to zones
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (7 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 08/13] block: Fix flush request sector restore Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:34   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 10/13] block: Improve blk_zone_write_plug_bio_merged() Damien Le Moal
                   ` (4 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Zone write plugging ignores empty (no data) flush operations but handles
flush BIOs that have data to ensure that the flush machinery generated
write is processed in order. However, the call to
blk_zone_write_plug_attempt_merge() which sets a request
RQF_ZONE_WRITE_PLUGGING flag is called after blk_insert_flush(), thus
missing indicating that a non empty flush request completion needs
handling by zone write plugging.

Fix this by moving the call to blk_zone_write_plug_attempt_merge()
before blk_insert_flush(). And while at it, rename that function as
blk_zone_write_plug_init_request() to be clear that it is not just about
merging plugged BIOs in the request. While at it, also add a WARN_ONCE()
check that the zone write plug for the request is not NULL.

Fixes: dd291d77cc90 ("block: Introduce zone write plugging")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-mq.c    |  6 +++---
 block/blk-zoned.c | 12 ++++++++----
 block/blk.h       |  4 ++--
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index 434d45219e23..0fae9bd0ecd4 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -3001,12 +3001,12 @@ void blk_mq_submit_bio(struct bio *bio)
 		return;
 	}
 
+	if (bio_zone_write_plugging(bio))
+		blk_zone_write_plug_init_request(rq);
+
 	if (op_is_flush(bio->bi_opf) && blk_insert_flush(rq))
 		return;
 
-	if (bio_zone_write_plugging(bio))
-		blk_zone_write_plug_attempt_merge(rq);
-
 	if (plug) {
 		blk_add_rq_to_plug(plug, rq);
 		return;
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 5792e3b160c9..b551fe4e684f 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -855,8 +855,9 @@ void blk_zone_write_plug_bio_merged(struct bio *bio)
 
 	/*
 	 * If the BIO was already plugged, then we were called through
-	 * blk_zone_write_plug_attempt_merge() -> blk_attempt_bio_merge().
-	 * For this case, blk_zone_write_plug_attempt_merge() will handle the
+	 * blk_zone_write_plug_init_request() -> blk_attempt_bio_merge().
+	 * For this case, we already hold a reference on the zone write plug for
+	 * the BIO and blk_zone_write_plug_init_request() will handle the
 	 * zone write pointer offset update.
 	 */
 	if (bio_flagged(bio, BIO_ZONE_WRITE_PLUGGING))
@@ -880,7 +881,7 @@ void blk_zone_write_plug_bio_merged(struct bio *bio)
  * already went through zone write plugging (either a new BIO or one that was
  * unplugged).
  */
-void blk_zone_write_plug_attempt_merge(struct request *req)
+void blk_zone_write_plug_init_request(struct request *req)
 {
 	sector_t req_back_sector = blk_rq_pos(req) + blk_rq_sectors(req);
 	struct request_queue *q = req->q;
@@ -891,6 +892,9 @@ void blk_zone_write_plug_attempt_merge(struct request *req)
 	unsigned long flags;
 	struct bio *bio;
 
+	if (WARN_ON_ONCE(!zwplug))
+		return;
+
 	/*
 	 * Indicate that completion of this request needs to be handled with
 	 * blk_zone_write_plug_complete_request(), which will drop the reference
@@ -1250,7 +1254,7 @@ void blk_zone_write_plug_complete_request(struct request *req)
 
 	/*
 	 * Drop the reference we took when the request was initialized in
-	 * blk_zone_write_plug_attempt_merge().
+	 * blk_zone_write_plug_init_request().
 	 */
 	disk_put_zone_wplug(zwplug);
 
diff --git a/block/blk.h b/block/blk.h
index 1140c4a0be03..8a62b861453c 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -427,7 +427,7 @@ static inline bool bio_is_zone_append(struct bio *bio)
 		bio_flagged(bio, BIO_EMULATES_ZONE_APPEND);
 }
 void blk_zone_write_plug_bio_merged(struct bio *bio);
-void blk_zone_write_plug_attempt_merge(struct request *rq);
+void blk_zone_write_plug_init_request(struct request *rq);
 static inline void blk_zone_update_request_bio(struct request *rq,
 					       struct bio *bio)
 {
@@ -481,7 +481,7 @@ static inline bool bio_is_zone_append(struct bio *bio)
 static inline void blk_zone_write_plug_bio_merged(struct bio *bio)
 {
 }
-static inline void blk_zone_write_plug_attempt_merge(struct request *rq)
+static inline void blk_zone_write_plug_init_request(struct request *rq)
 {
 }
 static inline void blk_zone_update_request_bio(struct request *rq,
-- 
2.44.0


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

* [PATCH 10/13] block: Improve blk_zone_write_plug_bio_merged()
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (8 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 09/13] block: Fix handling of non-empty flush write requests to zones Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:35   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 11/13] block: Improve zone write request completion handling Damien Le Moal
                   ` (3 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Improve blk_zone_write_plug_bio_merged() to check that we succefully get
a reference on the zone write plugi of the merged BIO, as expected since
for a merge we already have at least one request and one BIO referencing
the zone write plug. Comments in this function are also improved to
better explain the references to the BIO zone write plug.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index b551fe4e684f..d962ba7c9ae1 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -866,11 +866,16 @@ void blk_zone_write_plug_bio_merged(struct bio *bio)
 	bio_set_flag(bio, BIO_ZONE_WRITE_PLUGGING);
 
 	/*
-	 * Increase the plug reference count and advance the zone write
-	 * pointer offset.
+	 * Get a reference on the zone write plug of the target zone and advance
+	 * the zone write pointer offset. Given that this is a merge, we already
+	 * have at least one request and one BIO referencing the zone write
+	 * plug. So this should not fail.
 	 */
 	zwplug = disk_get_zone_wplug(bio->bi_bdev->bd_disk,
 				     bio->bi_iter.bi_sector);
+	if (WARN_ON_ONCE(!zwplug))
+		return;
+
 	spin_lock_irqsave(&zwplug->lock, flags);
 	zwplug->wp_offset += bio_sectors(bio);
 	spin_unlock_irqrestore(&zwplug->lock, flags);
-- 
2.44.0


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

* [PATCH 11/13] block: Improve zone write request completion handling
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (9 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 10/13] block: Improve blk_zone_write_plug_bio_merged() Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:39   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 12/13] block: Simplify blk_zone_write_plug_bio_endio() Damien Le Moal
                   ` (2 subsequent siblings)
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

blk_zone_complete_request() must be called to handle the completion of a
zone write request handled with zone write plugging. This function is
called from blk_complete_request(), blk_update_request() and also in
blk_mq_submit_bio() error path. Improve this by moving this function
call into blk_mq_finish_request() as all requests are processed with
this function when they complete as well as when they are freed without
being executed. This also improves blk_update_request() used by scsi
devices as these may repeatedly call this function to handle partial
completions.

To be consistent with this change, blk_zone_complete_request() is
renamed to blk_zone_finish_request() and
blk_zone_write_plug_complete_request() is renamed to
blk_zone_write_plug_finish_request().

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-mq.c    | 6 ++----
 block/blk-zoned.c | 9 +++++----
 block/blk.h       | 8 ++++----
 3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index 0fae9bd0ecd4..9f677ea85a52 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -691,6 +691,8 @@ static void blk_mq_finish_request(struct request *rq)
 {
 	struct request_queue *q = rq->q;
 
+	blk_zone_finish_request(rq);
+
 	if (rq->rq_flags & RQF_USE_SCHED) {
 		q->elevator->type->ops.finish_request(rq);
 		/*
@@ -828,8 +830,6 @@ static void blk_complete_request(struct request *req)
 		bio = next;
 	} while (bio);
 
-	blk_zone_complete_request(req);
-
 	/*
 	 * Reset counters so that the request stacking driver
 	 * can find how many bytes remain in the request
@@ -940,7 +940,6 @@ bool blk_update_request(struct request *req, blk_status_t error,
 	 * completely done
 	 */
 	if (!req->bio) {
-		blk_zone_complete_request(req);
 		/*
 		 * Reset counters so that the request stacking driver
 		 * can find how many bytes remain in the request
@@ -2996,7 +2995,6 @@ void blk_mq_submit_bio(struct bio *bio)
 	if (ret != BLK_STS_OK) {
 		bio->bi_status = ret;
 		bio_endio(bio);
-		blk_zone_complete_request(rq);
 		blk_mq_free_request(rq);
 		return;
 	}
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index d962ba7c9ae1..0047fe66f22d 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -902,7 +902,7 @@ void blk_zone_write_plug_init_request(struct request *req)
 
 	/*
 	 * Indicate that completion of this request needs to be handled with
-	 * blk_zone_write_plug_complete_request(), which will drop the reference
+	 * blk_zone_write_plug_finish_request(), which will drop the reference
 	 * on the zone write plug we took above on entry to this function.
 	 */
 	req->rq_flags |= RQF_ZONE_WRITE_PLUGGING;
@@ -1236,7 +1236,7 @@ void blk_zone_write_plug_bio_endio(struct bio *bio)
 	disk_put_zone_wplug(zwplug);
 
 	/*
-	 * For BIO-based devices, blk_zone_write_plug_complete_request()
+	 * For BIO-based devices, blk_zone_write_plug_finish_request()
 	 * is not called. So we need to schedule execution of the next
 	 * plugged BIO here.
 	 */
@@ -1247,11 +1247,12 @@ void blk_zone_write_plug_bio_endio(struct bio *bio)
 	disk_put_zone_wplug(zwplug);
 }
 
-void blk_zone_write_plug_complete_request(struct request *req)
+void blk_zone_write_plug_finish_request(struct request *req)
 {
 	struct gendisk *disk = req->q->disk;
-	struct blk_zone_wplug *zwplug = disk_get_zone_wplug(disk, req->__sector);
+	struct blk_zone_wplug *zwplug;
 
+	zwplug = disk_get_zone_wplug(disk, req->__sector);
 	if (WARN_ON_ONCE(!zwplug))
 		return;
 
diff --git a/block/blk.h b/block/blk.h
index 8a62b861453c..ee4f782d1496 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -453,11 +453,11 @@ static inline void blk_zone_bio_endio(struct bio *bio)
 		blk_zone_write_plug_bio_endio(bio);
 }
 
-void blk_zone_write_plug_complete_request(struct request *rq);
-static inline void blk_zone_complete_request(struct request *rq)
+void blk_zone_write_plug_finish_request(struct request *rq);
+static inline void blk_zone_finish_request(struct request *rq)
 {
 	if (rq->rq_flags & RQF_ZONE_WRITE_PLUGGING)
-		blk_zone_write_plug_complete_request(rq);
+		blk_zone_write_plug_finish_request(rq);
 }
 int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
 		unsigned long arg);
@@ -491,7 +491,7 @@ static inline void blk_zone_update_request_bio(struct request *rq,
 static inline void blk_zone_bio_endio(struct bio *bio)
 {
 }
-static inline void blk_zone_complete_request(struct request *rq)
+static inline void blk_zone_finish_request(struct request *rq)
 {
 }
 static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
-- 
2.44.0


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

* [PATCH 12/13] block: Simplify blk_zone_write_plug_bio_endio()
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (10 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 11/13] block: Improve zone write request completion handling Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:36   ` Christoph Hellwig
  2024-04-30 12:51 ` [PATCH 13/13] block: Simplify zone write plug BIO abort Damien Le Moal
  2024-04-30 17:00 ` [PATCH 00/13] Zone write plugging fixes and cleanup Johannes Thumshirn
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

We already have the disk variable obtained from the bio when calling
disk_get_zone_wplug(). So use that variable instead of dereferencing the
bio bdev again for the disk argument of disk_get_zone_wplug().

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 0047fe66f22d..c819e3cc7a20 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -1203,8 +1203,7 @@ void blk_zone_write_plug_bio_endio(struct bio *bio)
 {
 	struct gendisk *disk = bio->bi_bdev->bd_disk;
 	struct blk_zone_wplug *zwplug =
-		disk_get_zone_wplug(bio->bi_bdev->bd_disk,
-				    bio->bi_iter.bi_sector);
+		disk_get_zone_wplug(disk, bio->bi_iter.bi_sector);
 	unsigned long flags;
 
 	if (WARN_ON_ONCE(!zwplug))
-- 
2.44.0


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

* [PATCH 13/13] block: Simplify zone write plug BIO abort
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (11 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 12/13] block: Simplify blk_zone_write_plug_bio_endio() Damien Le Moal
@ 2024-04-30 12:51 ` Damien Le Moal
  2024-04-30 15:36   ` Christoph Hellwig
  2024-04-30 17:00 ` [PATCH 00/13] Zone write plugging fixes and cleanup Johannes Thumshirn
  13 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 12:51 UTC (permalink / raw)
  To: linux-block, Jens Axboe, dm-devel, Mike Snitzer

When BIOs plugged in a zone write plug are aborted,
blk_zone_wplug_bio_io_error() clears the BIO BIO_ZONE_WRITE_PLUGGING
flag so that bio_io_error(bio) does not end up calling
blk_zone_write_plug_bio_endio() and we thus need to manually drop the
reference on the zone write plug held by the aborted BIO.

Move the call to disk_put_zone_wplug() that is alwasy following the call
to blk_zone_wplug_bio_io_error() inside that function to simplify the
code.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index c819e3cc7a20..ed180fdf66f4 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -616,12 +616,14 @@ static struct blk_zone_wplug *disk_get_and_lock_zone_wplug(struct gendisk *disk,
 	return zwplug;
 }
 
-static inline void blk_zone_wplug_bio_io_error(struct bio *bio)
+static inline void blk_zone_wplug_bio_io_error(struct blk_zone_wplug *zwplug,
+					       struct bio *bio)
 {
-	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
+	struct request_queue *q = zwplug->disk->queue;
 
 	bio_clear_flag(bio, BIO_ZONE_WRITE_PLUGGING);
 	bio_io_error(bio);
+	disk_put_zone_wplug(zwplug);
 	blk_queue_exit(q);
 }
 
@@ -632,10 +634,8 @@ static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug)
 {
 	struct bio *bio;
 
-	while ((bio = bio_list_pop(&zwplug->bio_list))) {
-		blk_zone_wplug_bio_io_error(bio);
-		disk_put_zone_wplug(zwplug);
-	}
+	while ((bio = bio_list_pop(&zwplug->bio_list)))
+		blk_zone_wplug_bio_io_error(zwplug, bio);
 }
 
 /*
@@ -655,8 +655,7 @@ static void disk_zone_wplug_abort_unaligned(struct gendisk *disk,
 		if (wp_offset >= zone_capacity ||
 		    (bio_op(bio) != REQ_OP_ZONE_APPEND &&
 		     bio_offset_from_zone_start(bio) != wp_offset)) {
-			blk_zone_wplug_bio_io_error(bio);
-			disk_put_zone_wplug(zwplug);
+			blk_zone_wplug_bio_io_error(zwplug, bio);
 			continue;
 		}
 
-- 
2.44.0


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

* Re: [PATCH 02/13] block: Exclude conventional zones when faking max open limit
  2024-04-30 12:51 ` [PATCH 02/13] block: Exclude conventional zones when faking max open limit Damien Le Moal
@ 2024-04-30 15:24   ` Christoph Hellwig
  2024-04-30 19:04     ` Jens Axboe
  0 siblings, 1 reply; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:24 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On Tue, Apr 30, 2024 at 09:51:20PM +0900, Damien Le Moal wrote:
> +	/* Resize the zone write plug memory pool if needed. */
> +	if (disk->zone_wplugs_pool->min_nr != pool_size)
> +		mempool_resize(disk->zone_wplugs_pool, pool_size);

No need for the if here, mempool_resize is a no-op if called for
the current value.

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb()
  2024-04-30 12:51 ` [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb() Damien Le Moal
@ 2024-04-30 15:26   ` Christoph Hellwig
  2024-04-30 22:50     ` Damien Le Moal
  0 siblings, 1 reply; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:26 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

Although I suspect that at some point blk_revalidate_zone_cb should
grow separate helpers for the conventional and sequential required cases
as the amount of code in that switch statement is getting a bit out of
bounds..


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

* Re: [PATCH 08/13] block: Fix flush request sector restore
  2024-04-30 12:51 ` [PATCH 08/13] block: Fix flush request sector restore Damien Le Moal
@ 2024-04-30 15:26   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:26 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On Tue, Apr 30, 2024 at 09:51:26PM +0900, Damien Le Moal wrote:
> Make sure that a request bio is not NULL before trying to restore the
> request start sector.

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


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

* Re: [PATCH 04/13] block: Fix reference counting for zone write plugs in error state
  2024-04-30 12:51 ` [PATCH 04/13] block: Fix reference counting for zone write plugs in error state Damien Le Moal
@ 2024-04-30 15:28   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:28 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

>  
> +static inline void disk_zone_wplug_set_error(struct gendisk *disk,
> +					     struct blk_zone_wplug *zwplug)
> +{
> +	if (!(zwplug->flags & BLK_ZONE_WPLUG_ERROR)) {

Can you just do an early return if the flag is not set instead of
all the extra indentation?

> +static inline void disk_zone_wplug_clear_error(struct gendisk *disk,
> +					       struct blk_zone_wplug *zwplug)
> +{
> +	if (zwplug->flags & BLK_ZONE_WPLUG_ERROR) {

Same here.

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH 05/13] block: Hold a reference on zone write plugs to schedule submission
  2024-04-30 12:51 ` [PATCH 05/13] block: Hold a reference on zone write plugs to schedule submission Damien Le Moal
@ 2024-04-30 15:30   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:30 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH 06/13] block: Unhash a zone write plug only if needed
  2024-04-30 12:51 ` [PATCH 06/13] block: Unhash a zone write plug only if needed Damien Le Moal
@ 2024-04-30 15:31   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:31 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


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

* Re: [PATCH 07/13] block: Do not remove zone write plugs still in use
  2024-04-30 12:51 ` [PATCH 07/13] block: Do not remove zone write plugs still in use Damien Le Moal
@ 2024-04-30 15:34   ` Christoph Hellwig
  2024-04-30 23:06     ` Damien Le Moal
  0 siblings, 1 reply; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:34 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On Tue, Apr 30, 2024 at 09:51:25PM +0900, Damien Le Moal wrote:
> Large write BIOs that span a zone boundary are split in
> blk_mq_submit_bio() before being passed to blk_zone_plug_bio() for zone
> write plugging. Such split BIO will be chained with one fragment
> targeting one zone and the remainder of the BIO tergetting the next

s/tergetting/targetting/

> Fix this by modifying disk_should_remove_zone_wplug() to check that the
> reference count to a zone write plug is not larger than 2, that is, that
> the only references left on the zone are the caller held reference
> (blk_zone_write_plug_complete_request()) and the initial extra reference
> for the zone write plug taken when it was initialized (and that is
> dropped when the zone write plug is removed from the hash table).

How is this atomic_read() based check not racy?


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

* Re: [PATCH 09/13] block: Fix handling of non-empty flush write requests to zones
  2024-04-30 12:51 ` [PATCH 09/13] block: Fix handling of non-empty flush write requests to zones Damien Le Moal
@ 2024-04-30 15:34   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:34 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH 10/13] block: Improve blk_zone_write_plug_bio_merged()
  2024-04-30 12:51 ` [PATCH 10/13] block: Improve blk_zone_write_plug_bio_merged() Damien Le Moal
@ 2024-04-30 15:35   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:35 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On Tue, Apr 30, 2024 at 09:51:28PM +0900, Damien Le Moal wrote:
> Improve blk_zone_write_plug_bio_merged() to check that we succefully get
> a reference on the zone write plugi of the merged BIO, as expected since

s/plug/plug/ ?

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH 12/13] block: Simplify blk_zone_write_plug_bio_endio()
  2024-04-30 12:51 ` [PATCH 12/13] block: Simplify blk_zone_write_plug_bio_endio() Damien Le Moal
@ 2024-04-30 15:36   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:36 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On Tue, Apr 30, 2024 at 09:51:30PM +0900, Damien Le Moal wrote:
> We already have the disk variable obtained from the bio when calling
> disk_get_zone_wplug(). So use that variable instead of dereferencing the
> bio bdev again for the disk argument of disk_get_zone_wplug().

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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

* Re: [PATCH 13/13] block: Simplify zone write plug BIO abort
  2024-04-30 12:51 ` [PATCH 13/13] block: Simplify zone write plug BIO abort Damien Le Moal
@ 2024-04-30 15:36   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:36 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


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

* Re: [PATCH 11/13] block: Improve zone write request completion handling
  2024-04-30 12:51 ` [PATCH 11/13] block: Improve zone write request completion handling Damien Le Moal
@ 2024-04-30 15:39   ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-04-30 15:39 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


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

* Re: [PATCH 00/13] Zone write plugging fixes and cleanup
  2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
                   ` (12 preceding siblings ...)
  2024-04-30 12:51 ` [PATCH 13/13] block: Simplify zone write plug BIO abort Damien Le Moal
@ 2024-04-30 17:00 ` Johannes Thumshirn
  13 siblings, 0 replies; 33+ messages in thread
From: Johannes Thumshirn @ 2024-04-30 17:00 UTC (permalink / raw)
  To: Damien Le Moal, linux-block, Jens Axboe, dm-devel, Mike Snitzer

Apart from Christoph's comments:
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

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

* Re: [PATCH 02/13] block: Exclude conventional zones when faking max open limit
  2024-04-30 15:24   ` Christoph Hellwig
@ 2024-04-30 19:04     ` Jens Axboe
  2024-05-01  4:57       ` Christoph Hellwig
  0 siblings, 1 reply; 33+ messages in thread
From: Jens Axboe @ 2024-04-30 19:04 UTC (permalink / raw)
  To: Christoph Hellwig, Damien Le Moal; +Cc: linux-block, dm-devel, Mike Snitzer

On 4/30/24 9:24 AM, Christoph Hellwig wrote:
> On Tue, Apr 30, 2024 at 09:51:20PM +0900, Damien Le Moal wrote:
>> +	/* Resize the zone write plug memory pool if needed. */
>> +	if (disk->zone_wplugs_pool->min_nr != pool_size)
>> +		mempool_resize(disk->zone_wplugs_pool, pool_size);
> 
> No need for the if here, mempool_resize is a no-op if called for
> the current value.

Still cheaper than the function call though, so I think that's
the right way to do it.

-- 
Jens Axboe



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

* Re: [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb()
  2024-04-30 15:26   ` Christoph Hellwig
@ 2024-04-30 22:50     ` Damien Le Moal
  2024-04-30 23:54       ` Damien Le Moal
  0 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 22:50 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On 5/1/24 00:26, Christoph Hellwig wrote:
> Looks good:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 
> Although I suspect that at some point blk_revalidate_zone_cb should
> grow separate helpers for the conventional and sequential required cases
> as the amount of code in that switch statement is getting a bit out of
> bounds..

Yep, will clean that up later (next cycle).

-- 
Damien Le Moal
Western Digital Research


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

* Re: [PATCH 07/13] block: Do not remove zone write plugs still in use
  2024-04-30 15:34   ` Christoph Hellwig
@ 2024-04-30 23:06     ` Damien Le Moal
  2024-04-30 23:55       ` Damien Le Moal
  0 siblings, 1 reply; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 23:06 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On 5/1/24 00:34, Christoph Hellwig wrote:
>> Fix this by modifying disk_should_remove_zone_wplug() to check that the
>> reference count to a zone write plug is not larger than 2, that is, that
>> the only references left on the zone are the caller held reference
>> (blk_zone_write_plug_complete_request()) and the initial extra reference
>> for the zone write plug taken when it was initialized (and that is
>> dropped when the zone write plug is removed from the hash table).
> 
> How is this atomic_read() based check not racy?

Because of how references work:
1) A valid and unused zone write plug has a ref count of 1
2) A function using a write plug always has a reference on it, so if the plug is
valid and unused, the ref count is always 2
3) Any plugged BIO and in-flight BIOs and requests hold a reference on the plug.
So if the plug is used for BIOs, the reference count is always at least 2, and
when a function is using the plug the refcount is always at least 3

Based on this, all callers of disk_should_remove_zone_wplug() will always see a
refcount of 2 if the plug is unused, or more than 2 if the plug is being used to
handle BIOs. Most of the time, checking for the BUSY (PLUGGED) flag catches the
later case. But as explained in the commit message, chained BIOs due to splits
can lead to bio_endio() execution order to change and to calls to
blk_zone_write_plug_bio_endio() to be done after
blk_zone_write_plug_finish_request() calls disk_zone_wplug_unplug_bio().
Checking that the plug refcount is not more than 2 tells us reliably that BIOs
are still holding references on the plug and that the plug should not be removed
until all BIOs completions are handled.

Does this answer your question ?

-- 
Damien Le Moal
Western Digital Research


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

* Re: [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb()
  2024-04-30 22:50     ` Damien Le Moal
@ 2024-04-30 23:54       ` Damien Le Moal
  0 siblings, 0 replies; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 23:54 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On 5/1/24 07:50, Damien Le Moal wrote:
> On 5/1/24 00:26, Christoph Hellwig wrote:
>> Looks good:
>>
>> Reviewed-by: Christoph Hellwig <hch@lst.de>
>>
>> Although I suspect that at some point blk_revalidate_zone_cb should
>> grow separate helpers for the conventional and sequential required cases
>> as the amount of code in that switch statement is getting a bit out of
>> bounds..
> 
> Yep, will clean that up later (next cycle).

I did it now. It is a nice cleanup. I added a patch to the series for that.

-- 
Damien Le Moal
Western Digital Research


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

* Re: [PATCH 07/13] block: Do not remove zone write plugs still in use
  2024-04-30 23:06     ` Damien Le Moal
@ 2024-04-30 23:55       ` Damien Le Moal
  0 siblings, 0 replies; 33+ messages in thread
From: Damien Le Moal @ 2024-04-30 23:55 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-block, Jens Axboe, dm-devel, Mike Snitzer

On 5/1/24 08:06, Damien Le Moal wrote:
> On 5/1/24 00:34, Christoph Hellwig wrote:
>>> Fix this by modifying disk_should_remove_zone_wplug() to check that the
>>> reference count to a zone write plug is not larger than 2, that is, that
>>> the only references left on the zone are the caller held reference
>>> (blk_zone_write_plug_complete_request()) and the initial extra reference
>>> for the zone write plug taken when it was initialized (and that is
>>> dropped when the zone write plug is removed from the hash table).
>>
>> How is this atomic_read() based check not racy?
> 
> Because of how references work:
> 1) A valid and unused zone write plug has a ref count of 1
> 2) A function using a write plug always has a reference on it, so if the plug is
> valid and unused, the ref count is always 2
> 3) Any plugged BIO and in-flight BIOs and requests hold a reference on the plug.
> So if the plug is used for BIOs, the reference count is always at least 2, and
> when a function is using the plug the refcount is always at least 3
> 
> Based on this, all callers of disk_should_remove_zone_wplug() will always see a
> refcount of 2 if the plug is unused, or more than 2 if the plug is being used to
> handle BIOs. Most of the time, checking for the BUSY (PLUGGED) flag catches the
> later case. But as explained in the commit message, chained BIOs due to splits
> can lead to bio_endio() execution order to change and to calls to
> blk_zone_write_plug_bio_endio() to be done after
> blk_zone_write_plug_finish_request() calls disk_zone_wplug_unplug_bio().
> Checking that the plug refcount is not more than 2 tells us reliably that BIOs
> are still holding references on the plug and that the plug should not be removed
> until all BIOs completions are handled.
> 
> Does this answer your question ?

I modified the patch to add a comment in disk_should_remove_zone_wplug()
explaining the above.

-- 
Damien Le Moal
Western Digital Research


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

* Re: [PATCH 02/13] block: Exclude conventional zones when faking max open limit
  2024-04-30 19:04     ` Jens Axboe
@ 2024-05-01  4:57       ` Christoph Hellwig
  0 siblings, 0 replies; 33+ messages in thread
From: Christoph Hellwig @ 2024-05-01  4:57 UTC (permalink / raw)
  To: Jens Axboe
  Cc: Christoph Hellwig, Damien Le Moal, linux-block, dm-devel, Mike Snitzer

On Tue, Apr 30, 2024 at 01:04:51PM -0600, Jens Axboe wrote:
> On 4/30/24 9:24 AM, Christoph Hellwig wrote:
> > On Tue, Apr 30, 2024 at 09:51:20PM +0900, Damien Le Moal wrote:
> >> +	/* Resize the zone write plug memory pool if needed. */
> >> +	if (disk->zone_wplugs_pool->min_nr != pool_size)
> >> +		mempool_resize(disk->zone_wplugs_pool, pool_size);
> > 
> > No need for the if here, mempool_resize is a no-op if called for
> > the current value.
> 
> Still cheaper than the function call though, so I think that's
> the right way to do it.

It is only called during device probing and resize.  Try to avoid
the call and spinlock there is the poster definition of premature
micro-optimization..


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

end of thread, other threads:[~2024-05-01  4:57 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-30 12:51 [PATCH 00/13] Zone write plugging fixes and cleanup Damien Le Moal
2024-04-30 12:51 ` [PATCH 01/13] dm: Check that a zoned table leads to a valid mapped device Damien Le Moal
2024-04-30 12:51 ` [PATCH 02/13] block: Exclude conventional zones when faking max open limit Damien Le Moal
2024-04-30 15:24   ` Christoph Hellwig
2024-04-30 19:04     ` Jens Axboe
2024-05-01  4:57       ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 03/13] block: Fix zone write plug initialization from blk_revalidate_zone_cb() Damien Le Moal
2024-04-30 15:26   ` Christoph Hellwig
2024-04-30 22:50     ` Damien Le Moal
2024-04-30 23:54       ` Damien Le Moal
2024-04-30 12:51 ` [PATCH 04/13] block: Fix reference counting for zone write plugs in error state Damien Le Moal
2024-04-30 15:28   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 05/13] block: Hold a reference on zone write plugs to schedule submission Damien Le Moal
2024-04-30 15:30   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 06/13] block: Unhash a zone write plug only if needed Damien Le Moal
2024-04-30 15:31   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 07/13] block: Do not remove zone write plugs still in use Damien Le Moal
2024-04-30 15:34   ` Christoph Hellwig
2024-04-30 23:06     ` Damien Le Moal
2024-04-30 23:55       ` Damien Le Moal
2024-04-30 12:51 ` [PATCH 08/13] block: Fix flush request sector restore Damien Le Moal
2024-04-30 15:26   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 09/13] block: Fix handling of non-empty flush write requests to zones Damien Le Moal
2024-04-30 15:34   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 10/13] block: Improve blk_zone_write_plug_bio_merged() Damien Le Moal
2024-04-30 15:35   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 11/13] block: Improve zone write request completion handling Damien Le Moal
2024-04-30 15:39   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 12/13] block: Simplify blk_zone_write_plug_bio_endio() Damien Le Moal
2024-04-30 15:36   ` Christoph Hellwig
2024-04-30 12:51 ` [PATCH 13/13] block: Simplify zone write plug BIO abort Damien Le Moal
2024-04-30 15:36   ` Christoph Hellwig
2024-04-30 17:00 ` [PATCH 00/13] Zone write plugging fixes and cleanup Johannes Thumshirn

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).