From patchwork Thu May 30 05:40:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 13679763 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29CD3143881; Thu, 30 May 2024 05:40:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047641; cv=none; b=jME1TmsHmu4VWNsQ9kcjWLmkHXUFd8j0xjG3hWtolc679P0ZiW8BJ3VYOr26L58S0xdNK5QJ/WqXxMAuar5W35+OjRGiiGBenvgm5BKNpGOPEissABhXeVtDQ68pLb1gNjTYNq+/J300uTzk775mMJKfH4B5qqD2LXArwGz6Pho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047641; c=relaxed/simple; bh=zq1flJzo4D0ydTL1aUlxX2XBynuRm4rgunyI1ebwP08=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pNa4UGzuRPqys4VPR1vxNiMrktVGiEVLhNOEu/MnmhZxwZ4q+Qdkm1W28/FGF6UClgmsc61CujCKsmrFyhfqt26ohq+pSUSHhEG4+AgTQ8IZZi6MTZWpSqn/Eu4cSUDlGzLQ5A5AhxoYT70gH+BS+SsIn2obtKmWU3xm7/bxiCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WLp1E3Eu; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WLp1E3Eu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E27B3C32782; Thu, 30 May 2024 05:40:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717047640; bh=zq1flJzo4D0ydTL1aUlxX2XBynuRm4rgunyI1ebwP08=; h=From:To:Subject:Date:In-Reply-To:References:From; b=WLp1E3EuhJDAd7CakbVSPLFrQkRvLxHC+AELe/GqRIX4v0etrO3Hl+mSXrpjftjIc lHDr7fbkmB9nHc+jaHv7N0AdwjHBwBJ2UtNlyWq/+Gt+NT8FO13F2Fyf1jESZqAODq 6Os1mIPolaUd72ojF65lAaPrqXq51s3shGqPDp/EZKGs9uND3FBj9F/9tNpSZoGc+c LxdjkaJOJoKOTq1etStk7IWwoLoKWfVqkFPPPlkzAAH43VjXzPQ7MO9tvKHpE0nUke XF/dwfqrXqkgWoqy2p4CfYku/nj4mtFyp3rqjkLBgj/uwqsomyf+ag51uT+n2uoZLE HHud5JYTVe1Zg== From: Damien Le Moal To: Jens Axboe , linux-block@vger.kernel.org, dm-devel@lists.linux.dev, Mike Snitzer , Mikulas Patocka Subject: [PATCH 1/4] null_blk: Do not allow runt zone with zone capacity smaller then zone size Date: Thu, 30 May 2024 14:40:32 +0900 Message-ID: <20240530054035.491497-2-dlemoal@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240530054035.491497-1-dlemoal@kernel.org> References: <20240530054035.491497-1-dlemoal@kernel.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A zoned device with a smaller last zone together with a zone capacity smaller than the zone size does make any sense as that does not correspond to any possible setup for a real device: 1) For ZNS and zoned UFS devices, all zones are always the same size. 2) For SMR HDDs, all zones always have the same capacity. In other words, if we have a smaller last runt zone, then this zone capacity should always be equal to the zone size. Add a check in null_init_zoned_dev() to prevent a configuration to have both a smaller zone size and a zone capacity smaller than the zone size. Signed-off-by: Damien Le Moal Reviewed-by: Niklas Cassel Reviewed-by: Bart Van Assche Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke --- drivers/block/null_blk/zoned.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/block/null_blk/zoned.c b/drivers/block/null_blk/zoned.c index 79c8e5e99f7f..f118d304f310 100644 --- a/drivers/block/null_blk/zoned.c +++ b/drivers/block/null_blk/zoned.c @@ -74,6 +74,17 @@ int null_init_zoned_dev(struct nullb_device *dev, return -EINVAL; } + /* + * If a smaller zone capacity was requested, do not allow a smaller last + * zone at the same time as such zone configuration does not correspond + * to any real zoned device. + */ + if (dev->zone_capacity != dev->zone_size && + dev->size & (dev->zone_size - 1)) { + pr_err("A smaller last zone is not allowed with zone capacity smaller than zone size.\n"); + return -EINVAL; + } + zone_capacity_sects = mb_to_sects(dev->zone_capacity); dev_capacity_sects = mb_to_sects(dev->size); dev->zone_size_sects = mb_to_sects(dev->zone_size); From patchwork Thu May 30 05:40:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 13679764 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26FD2143881; Thu, 30 May 2024 05:40:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047642; cv=none; b=O9adZjRG7e4R0UfsVXOoj4A2cBxhE1NlxIp34YyjGQZeizB8arOhhhmI3iZUgSGzoFRXgFgvCjfHAafRrVhUO8YXf9/J7dk0EqnwsIIjVWowzRvLSUc7fqE1Oyrp2ntqvCGuaxKE7yezn6cpM/fGLfXs6gc10XNPCyBRxqUkPyU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047642; c=relaxed/simple; bh=PdWwzmYj0FalkU2p3VQavwmK6ssWnPRHTkGVetRA6oE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=srWaTmvNRvM8nVZSEnYAGrQd4/TMh0ZzE6Sjc16EymPuXtOB/a6D5p9EQl/QgOdP8UFP9nCNU+u6Vl4tWWGJ7ccCdVbgfvet/oVMkEZAC4FCKD20FQhpf+oK85XBRSKE7ScDzrtAZJOvoi0zmb0sTyS0IzUJ5lcUb+WCPJy6L80= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MhiNy0fM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MhiNy0fM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0346FC3277B; Thu, 30 May 2024 05:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717047641; bh=PdWwzmYj0FalkU2p3VQavwmK6ssWnPRHTkGVetRA6oE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=MhiNy0fMo/5SPPz/ebLCpn6dDGVoN8dsvTtf3BUr7eUi/+Am36P3vpIr2gSwlJc5K yfS/koeL0iunR6q4v/8/YO0Fcbpa+oDrUqkqAAtliR7DPPubMhDnsIl15DBiDbESL/ 7k4egnDXPRnmqHr2F4jc8P7CpJ2L0itIInOsRY5+Gf+PMY+KNkqGwG6chXnPasyBd6 bSkYfJ5TmyUgFotB5NAMcPh5Yx9bEWdagX0HF7YiT/PBTzx7R3HeLxoIKbF/hqdWJs jVv0zrEE1opN6tok3R02H3p2UQu1UBY1U/avMPihHZ9Mpruls3sE3eCvpDfW4ic0/d n2fMU7pMILkow== From: Damien Le Moal To: Jens Axboe , linux-block@vger.kernel.org, dm-devel@lists.linux.dev, Mike Snitzer , Mikulas Patocka Subject: [PATCH 2/4] block: Fix validation of zoned device with a runt zone Date: Thu, 30 May 2024 14:40:33 +0900 Message-ID: <20240530054035.491497-3-dlemoal@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240530054035.491497-1-dlemoal@kernel.org> References: <20240530054035.491497-1-dlemoal@kernel.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Commit ecfe43b11b02 ("block: Remember zone capacity when revalidating zones") introduced checks to ensure that the capacity of the zones of a zoned device is constant for all zones. However, this check ignores the possibility that a zoned device has a smaller last zone with a size not equal to the capacity of other zones. Such device correspond in practice to an SMR drive with a smaller last zone and all zones with a capacity equal to the zone size, leading to the last zone capacity being different than the capacity of other zones. Correctly handle such device by fixing the check for the constant zone capacity in blk_revalidate_seq_zone() using the new helper function disk_zone_is_last(). This helper function is also used in blk_revalidate_zone_cb() when checking the zone size. Fixes: ecfe43b11b02 ("block: Remember zone capacity when revalidating zones") Signed-off-by: Damien Le Moal Reviewed-by: Niklas Cassel Reviewed-by: Bart Van Assche Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke --- block/blk-zoned.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/block/blk-zoned.c b/block/blk-zoned.c index 03aa4eead39e..402a50a1ac4d 100644 --- a/block/blk-zoned.c +++ b/block/blk-zoned.c @@ -450,6 +450,11 @@ static inline bool disk_zone_is_conv(struct gendisk *disk, sector_t sector) return test_bit(disk_zone_no(disk, sector), disk->conv_zones_bitmap); } +static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone) +{ + return zone->start + zone->len >= get_capacity(disk); +} + static bool disk_insert_zone_wplug(struct gendisk *disk, struct blk_zone_wplug *zwplug) { @@ -1693,11 +1698,13 @@ static int blk_revalidate_seq_zone(struct blk_zone *zone, unsigned int idx, /* * Remember the capacity of the first sequential zone and check - * if it is constant for all zones. + * if it is constant for all zones, ignoring the last zone as it can be + * smaller. */ if (!args->zone_capacity) args->zone_capacity = zone->capacity; - if (zone->capacity != args->zone_capacity) { + if (!disk_zone_is_last(disk, zone) && + zone->capacity != args->zone_capacity) { pr_warn("%s: Invalid variable zone capacity\n", disk->disk_name); return -ENODEV; @@ -1732,7 +1739,6 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx, { struct blk_revalidate_zone_args *args = data; struct gendisk *disk = args->disk; - sector_t capacity = get_capacity(disk); sector_t zone_sectors = disk->queue->limits.chunk_sectors; int ret; @@ -1743,7 +1749,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx, return -ENODEV; } - if (zone->start >= capacity || !zone->len) { + if (zone->start >= get_capacity(disk) || !zone->len) { pr_warn("%s: Invalid zone start %llu, length %llu\n", disk->disk_name, zone->start, zone->len); return -ENODEV; @@ -1753,7 +1759,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx, * All zones must have the same size, with the exception on an eventual * smaller last zone. */ - if (zone->start + zone->len < capacity) { + if (!disk_zone_is_last(disk, zone)) { if (zone->len != zone_sectors) { pr_warn("%s: Invalid zoned device with non constant zone size\n", disk->disk_name); From patchwork Thu May 30 05:40:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 13679765 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCC2A143881; Thu, 30 May 2024 05:40:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047643; cv=none; b=gapKstVrlYSzZPbMS/2CCXShhdgfgDjU2Ca1JEhxpUC/BdzRcBbzCv3wKAJcWzrydEUHHI/EpDO7tan2KELwBKmR63Kr9HU9ZaFF5bOa1Y9K+f1rvIwU5yF1P6Jf5r+9MX+fNcxN7/517gk9JGdMXZoALrQL06DUHPehIPBtsIg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047643; c=relaxed/simple; bh=qcEDmH6KYVTMfxHj5ZiKg9fjO9GRTu6Mxt5BFF1PvrY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qXw0vYKr6hjv5qagPBqRyB++rtTnZKZJK34zmOtre80raPrjsGZTua6dTEWkuBct85MeZ5LF+UIaKSbpnWpixoK+D+De2Uw+J0r2/gEUp5UzwfgnYJ3aEiwNN+PaYsHiOZ1XiM7Q0eZsX0Al+jKAL0dPHaMMFRvzI1CdsDTcnEA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OPGPUsfw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OPGPUsfw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AC69C32782; Thu, 30 May 2024 05:40:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717047642; bh=qcEDmH6KYVTMfxHj5ZiKg9fjO9GRTu6Mxt5BFF1PvrY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=OPGPUsfwcYC4N6lv8uNckxawZJVXxaK4dqxMJffCIqxxdieDmhJKPAMlBkzNV915C p4N/yUL1CyXfyPxWr21ROWhTRj7QhGezPpdmgiSM1gbkynbTzzCH6hYR1A4rLm2pa6 6AipE9EFX61oXtbUSYFxxx1L+ZNNrlu+QFy+canFsz2k3mcwkB8eV6M19A7a0BaeYF MZSaoGLMvqdZStmtiRYSKqxbV4KDclyWmW/tY8PxvPrKkfENttuyqn4bWs3Y5+4v7n /lzu3nFY4sm+maHJlWsuW9RYB/bBTQChOg6GHlbM2jMbjRsatTxFNQUoQHRZ1lJzz/ i25JYeY+YMTWg== From: Damien Le Moal To: Jens Axboe , linux-block@vger.kernel.org, dm-devel@lists.linux.dev, Mike Snitzer , Mikulas Patocka Subject: [PATCH 3/4] block: Fix zone write plugging handling of devices with a runt zone Date: Thu, 30 May 2024 14:40:34 +0900 Message-ID: <20240530054035.491497-4-dlemoal@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240530054035.491497-1-dlemoal@kernel.org> References: <20240530054035.491497-1-dlemoal@kernel.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A zoned device may have a last sequential write required zone that is smaller than other zones. However, all tests to check if a zone write plug write offset exceeds the zone capacity use the same capacity value stored in the gendisk zone_capacity field. This is incorrect for a zoned device with a last runt (smaller) zone. Add the new field last_zone_capacity to struct gendisk to store the capacity of the last zone of the device. blk_revalidate_seq_zone() and blk_revalidate_conv_zone() are both modified to get this value when disk_zone_is_last() returns true. Similarly to zone_capacity, the value is first stored using the last_zone_capacity field of struct blk_revalidate_zone_args. Once zone revalidation of all zones is done, this is used to set the gendisk last_zone_capacity field. The checks to determine if a zone is full or if a sector offset in a zone exceeds the zone capacity in disk_should_remove_zone_wplug(), disk_zone_wplug_abort_unaligned(), blk_zone_write_plug_init_request(), and blk_zone_wplug_prepare_bio() are modified to use the new helper functions disk_zone_is_full() and disk_zone_wplug_is_full(). disk_zone_is_full() uses the zone index to determine if the zone being tested is the last one of the disk and uses the either the disk zone_capacity or last_zone_capacity accordingly. Fixes: dd291d77cc90 ("block: Introduce zone write plugging") Signed-off-by: Damien Le Moal Reviewed-by: Niklas Cassel Reviewed-by: Bart Van Assche Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke --- block/blk-zoned.c | 35 +++++++++++++++++++++++++++-------- include/linux/blkdev.h | 1 + 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/block/blk-zoned.c b/block/blk-zoned.c index 402a50a1ac4d..52abebf56027 100644 --- a/block/blk-zoned.c +++ b/block/blk-zoned.c @@ -455,6 +455,20 @@ static bool disk_zone_is_last(struct gendisk *disk, struct blk_zone *zone) return zone->start + zone->len >= get_capacity(disk); } +static bool disk_zone_is_full(struct gendisk *disk, + unsigned int zno, unsigned int offset_in_zone) +{ + if (zno < disk->nr_zones - 1) + return offset_in_zone >= disk->zone_capacity; + return offset_in_zone >= disk->last_zone_capacity; +} + +static bool disk_zone_wplug_is_full(struct gendisk *disk, + struct blk_zone_wplug *zwplug) +{ + return disk_zone_is_full(disk, zwplug->zone_no, zwplug->wp_offset); +} + static bool disk_insert_zone_wplug(struct gendisk *disk, struct blk_zone_wplug *zwplug) { @@ -548,7 +562,7 @@ static inline bool disk_should_remove_zone_wplug(struct gendisk *disk, 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; + return !zwplug->wp_offset || disk_zone_wplug_is_full(disk, zwplug); } static void disk_remove_zone_wplug(struct gendisk *disk, @@ -669,13 +683,12 @@ static void disk_zone_wplug_abort(struct blk_zone_wplug *zwplug) static void disk_zone_wplug_abort_unaligned(struct gendisk *disk, struct blk_zone_wplug *zwplug) { - unsigned int zone_capacity = disk->zone_capacity; unsigned int wp_offset = zwplug->wp_offset; struct bio_list bl = BIO_EMPTY_LIST; struct bio *bio; while ((bio = bio_list_pop(&zwplug->bio_list))) { - if (wp_offset >= zone_capacity || + if (disk_zone_is_full(disk, zwplug->zone_no, wp_offset) || (bio_op(bio) != REQ_OP_ZONE_APPEND && bio_offset_from_zone_start(bio) != wp_offset)) { blk_zone_wplug_bio_io_error(zwplug, bio); @@ -914,7 +927,6 @@ 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; struct gendisk *disk = q->disk; - unsigned int zone_capacity = disk->zone_capacity; struct blk_zone_wplug *zwplug = disk_get_zone_wplug(disk, blk_rq_pos(req)); unsigned long flags; @@ -938,7 +950,7 @@ void blk_zone_write_plug_init_request(struct request *req) * into the back of the request. */ spin_lock_irqsave(&zwplug->lock, flags); - while (zwplug->wp_offset < zone_capacity) { + while (!disk_zone_wplug_is_full(disk, zwplug)) { bio = bio_list_peek(&zwplug->bio_list); if (!bio) break; @@ -984,7 +996,7 @@ static bool blk_zone_wplug_prepare_bio(struct blk_zone_wplug *zwplug, * We know such BIO will fail, and that would potentially overflow our * write pointer offset beyond the end of the zone. */ - if (zwplug->wp_offset >= disk->zone_capacity) + if (disk_zone_wplug_is_full(disk, zwplug)) goto err; if (bio_op(bio) == REQ_OP_ZONE_APPEND) { @@ -1561,6 +1573,7 @@ void disk_free_zone_resources(struct gendisk *disk) kfree(disk->conv_zones_bitmap); disk->conv_zones_bitmap = NULL; disk->zone_capacity = 0; + disk->last_zone_capacity = 0; disk->nr_zones = 0; } @@ -1605,6 +1618,7 @@ struct blk_revalidate_zone_args { unsigned long *conv_zones_bitmap; unsigned int nr_zones; unsigned int zone_capacity; + unsigned int last_zone_capacity; sector_t sector; }; @@ -1622,6 +1636,7 @@ static int disk_update_zone_resources(struct gendisk *disk, disk->nr_zones = args->nr_zones; disk->zone_capacity = args->zone_capacity; + disk->last_zone_capacity = args->last_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, @@ -1673,6 +1688,9 @@ static int blk_revalidate_conv_zone(struct blk_zone *zone, unsigned int idx, return -ENODEV; } + if (disk_zone_is_last(disk, zone)) + args->last_zone_capacity = zone->capacity; + if (!disk_need_zone_resources(disk)) return 0; @@ -1703,8 +1721,9 @@ static int blk_revalidate_seq_zone(struct blk_zone *zone, unsigned int idx, */ if (!args->zone_capacity) args->zone_capacity = zone->capacity; - if (!disk_zone_is_last(disk, zone) && - zone->capacity != args->zone_capacity) { + if (disk_zone_is_last(disk, zone)) { + args->last_zone_capacity = zone->capacity; + } else if (zone->capacity != args->zone_capacity) { pr_warn("%s: Invalid variable zone capacity\n", disk->disk_name); return -ENODEV; diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index aefdda9f4ec7..24c36929920b 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -186,6 +186,7 @@ struct gendisk { */ unsigned int nr_zones; unsigned int zone_capacity; + unsigned int last_zone_capacity; unsigned long *conv_zones_bitmap; unsigned int zone_wplugs_hash_bits; spinlock_t zone_wplugs_lock; From patchwork Thu May 30 05:40:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 13679766 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 437FB143881; Thu, 30 May 2024 05:40:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047644; cv=none; b=Uf9b2PZXbEHQAu1hY1l//5Rv7fd4jGa5/Pee6e8auXOWyXKbnaJPnzZfcOnw3QgFDwv4SQEaPUWyMAReK36qJnLh2oq9NsCccIffo52KeDROviTZJXnNUzyavqcYaDe4ZKnKM/mxilZwhdFcaMw7j7+fcEcUOX+dWn38y6fRKA0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717047644; c=relaxed/simple; bh=HFl4eMBEoveqXT/43h+zgVnr0XF6YpmSMOkRil/ZZk8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WqvlZRVMz7kCUFD9j0z/0D60XDpuSt1M+r1VEw2e4imOKQCuNESCReVFvW+pohdPov1h6MTzP4mSrQ7Lei7o6RVMUqH5oyt+MiEG2yM0Ivh7vvcNpFrDWJyNkB4ulIR7Vg0voQ3FfOXfuzL/p7klqyrcg0zdDNx8eVvW0PPiIyM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Zaq/1Rzw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Zaq/1Rzw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 126EAC3277B; Thu, 30 May 2024 05:40:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717047643; bh=HFl4eMBEoveqXT/43h+zgVnr0XF6YpmSMOkRil/ZZk8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Zaq/1RzwOvVxhnUVmipiMzCgw4Tuha3meLX1Dv5j97wRlANmwPO+/syea54d+58Nj 3MHxZrIZr2c2WB8//HJvJhY7+6xJZQ75TiBNGi85RZPE8GPxFOTKsy83hXuZIfLxWr waIW+TFg/r3aACG3tx8wqjfTXUGS5iqRElCdwFdiEbEpDwdBvQ4yj/g3RIxtnBjhPv F9nYwUjalkCbH8BsjSGqb5GF4KQZSR5R2A/bkuyTU2WflC332FwWyTWmntX4KM9bFb r8WKC3QGhiM1krJ4YteAHSCZtiXFfmqjGNBJBmieTOaFQmrxUujRy0uSfOdmI+AgNq WrXLl2h/mMLRw== From: Damien Le Moal To: Jens Axboe , linux-block@vger.kernel.org, dm-devel@lists.linux.dev, Mike Snitzer , Mikulas Patocka Subject: [PATCH 4/4] dm: Improve zone resource limits handling Date: Thu, 30 May 2024 14:40:35 +0900 Message-ID: <20240530054035.491497-5-dlemoal@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240530054035.491497-1-dlemoal@kernel.org> References: <20240530054035.491497-1-dlemoal@kernel.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The generic stacking of limits implemented in the block layer cannot correctly handle stacking of zone resource limits (max open zones and max active zones) because these limits are for an entire device but the stacking may be for a portion of that device (e.g. a dm-linear target that does not cover an entire block device). As a result, when DM devices are created on top of zoned block devices, the DM device never has any zone resource limits advertized, which is only correct if all underlying target devices also have no zone resource limits. If at least one target device has resource limits, the user may see either performance issues (if the max open zone limit of the device is exceeded) or write I/O errors if the max active zone limit of one of the underlying target devices is exceeded. While it is very difficult to correctly and reliably stack zone resource limits in general, cases where targets are not sharing zone resources of the same device can be dealt with relatively easily. Such situation happens when a target maps all sequential zones of a zoned block device: for such mapping, other targets mapping other parts of the same zoned block device can only contain conventional zones and thus will not require any zone resource to correctly handle write operations. For a mapped device constructed with such targets, which includes mapped devices constructed with targets mapping entire zoned block devices, the zone resource limits can be reliably determined using the non-zero minimum of the zone resource limits of all targets. For mapped devices that include targets partially mapping the set of sequential write required zones of zoned block devices, instead of advertizing no zone resource limits, it is also better to set the mapped device limits to the non-zero minimum of the limits of all targets, capped with the number of sequential zones being mapped. While still not completely reliable, this allows indicating to the user that the underlying devices used have limits. This commit improves zone resource limits handling as described above using the function dm_set_zone_resource_limits(). This function is executed from dm_set_zones_restrictions() and iterates the targets of a mapped device to evaluate the max open and max active zone limits. This relies on an internal "stacking" of the limits of the target devices combined with a direct counting of the number of sequential zones mapped by the target. 1) For a target mapping an entire zoned block device, the limits are evaluated as the non-zero minimum of the limits of the target device and of the mapped device. 2) For a target partially mapping a zoned block device, the number of mapped sequential zones is compared to the total number of sequential zones of the target device to determine the limits: if the target maps all sequential write required zones of the device, then the target can reliably inherit the device limits. Otherwise, the target limits are set to the device limits capped by the number of mapped sequential zones. With this evaluation done for each target, the mapped device zone resource limits are evaluated as the non-zero minimum of the limits of all the targets. For configurations resulting in unreliable limits, a warning message is issued. The counting of mapped sequential zones for the target is done using the new function dm_device_count_zones() which performs a report zones on the entire block device with the callback dm_device_count_zones_cb(). This count of mapped sequential zones is used to determine if the mapped device contains only conventional zones. This allows simplifying dm_set_zones_restrictions() to not do a report zones. For mapped devices mapping only conventional zones, dm_set_zone_resource_limits() changes the mapped device to a regular device. To further cleanup dm_set_zones_restrictions(), the message about the type of zone append (native or emulated) is moved inside dm_revalidate_zones(). Signed-off-by: Damien Le Moal Reviewed-by: Niklas Cassel Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke --- drivers/md/dm-zone.c | 214 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 177 insertions(+), 37 deletions(-) diff --git a/drivers/md/dm-zone.c b/drivers/md/dm-zone.c index 5d66d916730e..5f8b499529cf 100644 --- a/drivers/md/dm-zone.c +++ b/drivers/md/dm-zone.c @@ -145,17 +145,174 @@ bool dm_is_zone_write(struct mapped_device *md, struct bio *bio) } } +struct dm_device_zone_count { + sector_t start; + sector_t len; + unsigned int total_nr_seq_zones; + unsigned int target_nr_seq_zones; +}; + /* - * Count conventional zones of a mapped zoned device. If the device - * only has conventional zones, do not expose it as zoned. + * Count the total number of and the number of mapped sequential zones of a + * target zoned device. */ -static int dm_check_zoned_cb(struct blk_zone *zone, unsigned int idx, - void *data) +static int dm_device_count_zones_cb(struct blk_zone *zone, + unsigned int idx, void *data) { - unsigned int *nr_conv_zones = data; + struct dm_device_zone_count *zc = data; + + if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { + zc->total_nr_seq_zones++; + if (zone->start >= zc->start && + zone->start < zc->start + zc->len) + zc->target_nr_seq_zones++; + } - if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) - (*nr_conv_zones)++; + return 0; +} + +static int dm_device_count_zones(struct dm_dev *dev, + struct dm_device_zone_count *zc) +{ + int ret; + + ret = blkdev_report_zones(dev->bdev, 0, UINT_MAX, + dm_device_count_zones_cb, zc); + if (ret < 0) + return ret; + if (!ret) + return -EIO; + return 0; +} + +struct dm_zone_resource_limits { + unsigned int mapped_nr_seq_zones; + unsigned int max_open_zones; + unsigned int max_active_zones; + bool reliable_limits; +}; + +static int device_get_zone_resource_limits(struct dm_target *ti, + struct dm_dev *dev, sector_t start, + sector_t len, void *data) +{ + struct dm_zone_resource_limits *zlim = data; + struct gendisk *disk = dev->bdev->bd_disk; + int ret; + struct dm_device_zone_count zc = { + .start = start, + .len = len, + }; + + /* + * If the target is not the whole device, the device zone resources may + * be shared between different targets. Check this by counting the + * number of mapped sequential zones: if this number is smaller than the + * total number of sequential zones of the target device, then resource + * sharing may happen and the zone limits will not be reliable. + */ + ret = dm_device_count_zones(dev, &zc); + if (ret) { + DMERR("Count device %s zones failed", + disk->disk_name); + return ret; + } + + zlim->mapped_nr_seq_zones += zc.target_nr_seq_zones; + + /* + * If the target does not map any sequential zones, then we do not need + * any zone resource limits. + */ + if (!zc.target_nr_seq_zones) + return 0; + + /* + * If the target does not map all sequential zones, the limits + * will not be reliable. + */ + if (zc.target_nr_seq_zones < zc.total_nr_seq_zones) + zlim->reliable_limits = false; + + zlim->max_active_zones = + min_not_zero(disk->queue->limits.max_active_zones, + zlim->max_active_zones); + if (zlim->max_active_zones != UINT_MAX) + zlim->max_active_zones = + min(zlim->max_active_zones, zc.target_nr_seq_zones); + + zlim->max_open_zones = + min_not_zero(disk->queue->limits.max_open_zones, + zlim->max_open_zones); + if (zlim->max_open_zones != UINT_MAX) + zlim->max_open_zones = + min3(zlim->max_open_zones, zc.target_nr_seq_zones, + zlim->max_active_zones); + + return 0; +} + +static int dm_set_zone_resource_limits(struct mapped_device *md, + struct dm_table *t, struct queue_limits *lim) +{ + struct gendisk *disk = md->disk; + struct dm_zone_resource_limits zlim = { + .max_open_zones = UINT_MAX, + .max_active_zones = UINT_MAX, + .reliable_limits = true, + }; + + /* Get the zone resource limits from the targets. */ + for (unsigned int i = 0; i < t->num_targets; i++) { + struct dm_target *ti = dm_table_get_target(t, i); + + if (!ti->type->iterate_devices || + ti->type->iterate_devices(ti, + device_get_zone_resource_limits, &zlim)) { + DMERR("Could not determine %s zone resource limits", + md->disk->disk_name); + return -ENODEV; + } + } + + /* + * If we only have conventional zones mapped, expose the mapped device + + as a regular device. + */ + if (!zlim.mapped_nr_seq_zones) { + lim->max_open_zones = 0; + lim->max_active_zones = 0; + lim->max_zone_append_sectors = 0; + lim->zone_write_granularity = 0; + lim->zoned = false; + clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); + md->nr_zones = 0; + disk->nr_zones = 0; + return 0; + } + + /* + * Warn if the mapped device is partially using zone resources of the + * target devices as that leads to unreliable limits, i.e. if another + * mapped device uses the same underlying devices, we cannot enforce + * zone limits to guarantee that writing will not lead to errors. + * Note that we really should return an error for such case but there is + * no easy way to find out if another mapped device uses the same + * underlying zoned devices. + */ + if (!zlim.reliable_limits) + DMWARN("%s zone resource limits may be unreliable", + disk->disk_name); + + if (zlim.max_open_zones >= zlim.mapped_nr_seq_zones) + lim->max_open_zones = 0; + else + lim->max_open_zones = zlim.max_open_zones; + + if (zlim.max_active_zones >= zlim.mapped_nr_seq_zones) + lim->max_active_zones = 0; + else + lim->max_active_zones = zlim.max_active_zones; return 0; } @@ -172,8 +329,13 @@ static int dm_revalidate_zones(struct mapped_device *md, struct dm_table *t) int ret; /* Revalidate only if something changed. */ - if (!disk->nr_zones || disk->nr_zones != md->nr_zones) + if (!disk->nr_zones || disk->nr_zones != md->nr_zones) { + DMINFO("%s using %s zone append", + md->disk->disk_name, + queue_emulates_zone_append(disk->queue) ? + "emulated" : "native"); md->nr_zones = 0; + } if (md->nr_zones) return 0; @@ -224,8 +386,6 @@ int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q, struct queue_limits *lim) { struct mapped_device *md = t->md; - struct gendisk *disk = md->disk; - unsigned int nr_conv_zones = 0; int ret; /* @@ -244,36 +404,16 @@ int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q, return 0; /* - * Count conventional zones to check that the mapped device will indeed - * have sequential write required zones. + * Determine the max open and max active zone limits for the mapped + * device. For a mapped device containing only conventional zones, the + * mapped device is changed to be a regular block device, so exit early + * for such case. */ - 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); + ret = dm_set_zone_resource_limits(md, t, lim); + if (ret) return ret; - } - - /* - * If we only have conventional zones, expose the mapped device as - * a regular device. - */ - if (nr_conv_zones >= ret) { - lim->max_open_zones = 0; - lim->max_active_zones = 0; - lim->zoned = false; - clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); - disk->nr_zones = 0; + if (!lim->zoned) return 0; - } - - if (!md->disk->nr_zones) { - DMINFO("%s using %s zone append", - md->disk->disk_name, - queue_emulates_zone_append(q) ? "emulated" : "native"); - } ret = dm_revalidate_zones(md, t); if (ret < 0)