Linux block layer
 help / color / mirror / Atom feed
From: Damien Le Moal <dlemoal@kernel.org>
To: Jens Axboe <axboe@kernel.dk>, linux-block@vger.kernel.org
Cc: Christoph Hellwig <hch@lst.de>
Subject: [PATCH v7 09/16] block: serialize zone revalidation
Date: Tue,  8 Sep 2026 17:57:38 +0900	[thread overview]
Message-ID: <20260908085745.1082697-10-dlemoal@kernel.org> (raw)
In-Reply-To: <20260908085745.1082697-1-dlemoal@kernel.org>

The zone related fields of struct gendisk can be modified by
blk_revalidate_disk_zones() either on the first scan of the disk, or
during user triggered scans or device revalidation, if some
characteristics of the disk has changed (e.g. the disk capacity). Changes
to these fields are always done with the disk request queue frozen so that
BIO processing does not see any inconsistent state of the zones. This
implies a contract that reading these zone related fields must always be
done while holding a usage count on the request queue of the disk.

However, increasing the usage count of the disk request queue cannot be
done from the context of blk_revalidate_disk_zones() itself, as that would
prevent freezing the disk queue and result in a deadlock. This prevents
blk_revalidate_disk_zones() from consulting the zone related fields of
struct gendisk to detect, for instance, a change in the number of zones of
the disk. For such case, we want to detect the change, take appropriate
measures and revalidate exclusively revalidate the zones to avoid
concurrent revalidation calls to see the same change while corrections are
already on-going.

A simple solution to avoid this issue is to introduce a mutex to serialize
calls to blk_revalidate_disk_zones() and ensure only a single context at a
time can modify the zone related fields of a gendisk. In preparation for
handling disk capacity revalidation in blk_revalidate_disk_zones(), do so
with the mutex zone_revalidate_mutex. This mutex is initialized in
disk_init_zone_resources(), destroyed in disk_release_zone_resources() and
taken and released only in blk_revalidate_disk_zones() to serialize the
execution of this function.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 block/blk-zoned.c      | 17 +++++++++++++++--
 include/linux/blkdev.h |  9 ++++++---
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 70a687bb371f..676620ed93de 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -1963,6 +1963,7 @@ static int disk_zone_wplugs_worker(void *data)
 
 void disk_init_zone_resources(struct gendisk *disk)
 {
+	mutex_init(&disk->zone_revalidate_mutex);
 	atomic_set(&disk->nr_zone_wplugs, 0);
 	spin_lock_init(&disk->zone_wplugs_hash_lock);
 	spin_lock_init(&disk->zone_wplugs_list_lock);
@@ -2120,6 +2121,7 @@ void disk_release_zone_resources(struct gendisk *disk)
 	disk->zone_capacity = 0;
 	disk->last_zone_capacity = 0;
 	disk->nr_zones = 0;
+	mutex_destroy(&disk->zone_revalidate_mutex);
 }
 
 struct blk_revalidate_zone_args {
@@ -2461,6 +2463,12 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
 		return -ENODEV;
 	}
 
+	/*
+	 * Serialize calls to this function so that we can safely look at and
+	 * eventually change the disk zone information.
+	 */
+	mutex_lock(&disk->zone_revalidate_mutex);
+
 	/*
 	 * Allocate zone resources if they are needed and we have not done
 	 * so yet, and initialize the revalidation arguments passed to report
@@ -2472,14 +2480,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
 		ret = disk_alloc_zone_resources(disk, args.capacity);
 		if (ret) {
 			memalloc_noio_restore(noio_flag);
-			return ret;
+			goto unlock;
 		}
 	}
 
 	ret = disk_init_revalidate_args(disk, &args);
 	if (ret) {
 		memalloc_noio_restore(noio_flag);
-		return ret;
+		goto unlock;
 	}
 
 	ret = disk->fops->report_zones(disk, 0, UINT_MAX, &rep_args);
@@ -2496,6 +2504,8 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
 	if (ret)
 		goto free_args;
 
+	mutex_unlock(&disk->zone_revalidate_mutex);
+
 	return 0;
 
 free_args:
@@ -2503,6 +2513,9 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
 
 	kfree(args.zones_state);
 
+unlock:
+	mutex_unlock(&disk->zone_revalidate_mutex);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones);
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 6a765146a2d3..8252c896e3ea 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -191,10 +191,13 @@ struct gendisk {
 #ifdef CONFIG_BLK_DEV_ZONED
 	/*
 	 * Zoned block device information. Reads of this information must be
-	 * protected with blk_queue_enter() / blk_queue_exit(). Modifying this
-	 * information is only allowed while no requests are being processed.
-	 * See also blk_mq_freeze_queue() and blk_mq_unfreeze_queue().
+	 * protected with blk_queue_enter() / blk_queue_exit() or by holding a
+	 * lock on zone_revalidate_mutex. blk_revalidate_disk_zones() may modify
+	 * this information while no requests are being processed (disk queue
+	 * frozen with blk_mq_freeze_queue()) and while holding a lock on
+	 * zone_revalidate_mutex.
 	 */
+	struct mutex		zone_revalidate_mutex;
 	unsigned int		nr_zones;
 	unsigned int		zone_capacity;
 	unsigned int		last_zone_capacity;
-- 
2.55.0


  parent reply	other threads:[~2026-09-08  8:58 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-08  8:57 [PATCH v7 00/16] Improve handling of offline and read-only zones Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 01/16] block: remove disk_free_zone_resources() Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 02/16] block: improve capacity handling during zone revalidation Damien Le Moal
2026-09-09  8:51   ` Hannes Reinecke
2026-09-10  5:20   ` Christoph Hellwig
2026-09-08  8:57 ` [PATCH v7 03/16] block: refactor disk_revalidate_zone_resources() Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 04/16] block: refactor disk_update_zone_resources() Damien Le Moal
2026-09-09  9:09   ` Hannes Reinecke
2026-09-10  5:20   ` Christoph Hellwig
2026-09-08  8:57 ` [PATCH v7 05/16] block: remember a zone type regardless of its condition Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 06/16] block: refactor bdev_zone_is_seq() Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 07/16] block: improve blkdev_get_zone_info() Damien Le Moal
2026-09-09  9:11   ` Hannes Reinecke
2026-09-10  5:21   ` Christoph Hellwig
2026-09-08  8:57 ` [PATCH v7 08/16] block: introduce disk_for_all_zone_wplugs() Damien Le Moal
2026-09-08  8:57 ` Damien Le Moal [this message]
2026-09-09  9:20   ` [PATCH v7 09/16] block: serialize zone revalidation Hannes Reinecke
2026-09-10  5:21   ` Christoph Hellwig
2026-09-08  8:57 ` [PATCH v7 10/16] block: drop all zone write plugs on capacity changes Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 11/16] block: retry zone revalidation on capacity change Damien Le Moal
2026-09-09  9:26   ` Hannes Reinecke
2026-09-10  5:24   ` Christoph Hellwig
2026-09-10  5:26     ` Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 12/16] block: propagate readonly and offline conditions to zone write plugs Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 13/16] block: always treat offline and read-only zones as dead Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 14/16] block: fail zone management operations to read-only and offline zones Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 15/16] block: allow read-only and offline conventional zones Damien Le Moal
2026-09-08  8:57 ` [PATCH v7 16/16] block: simplify disk_zone_set_cond() Damien Le Moal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260908085745.1082697-10-dlemoal@kernel.org \
    --to=dlemoal@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox