Add support for storage element depopulation
Subsystems (7)
Damien Le Moal
maintainerv5 cover Add support for storage element depopulation
Jens,
These patches define a new set of block device operations for generically using from the block layer the storage element depopulation feature of zoned block devices. Support for this feature is added to the SCSI disk driver and an emulation of this feature added to the zloop driver.
The management operations can be accessed using block layer API, which is intended for file systems (e.g. zonefs and XFS), as well as using ioctls for users using zoned disks directly from user space.
Changes from v4:
- Rebased on latest block/for-next
- Added review tags to patch 1 and 2
- Reworked patch 3, 4 and 5 to address Christoph's and sashiko comments
Changes from v3:
- Reworked patch 1 to only provide the disk_zone_is_offline() helper function. Failing of read operations to offline zones is handled directly by the zloop driver and the new patch 7 is added to handle failing early read operations to offline zones of ZBC SCSI disks.
- Added patch 2
- Addressed further valid comments from Sashiko
(https://sashiko.dev/#/patchset/20261006124510.882017-1-dlemoal%40kernel.org)
The biggest changes are in patch 2 to save the number of storage elements of a disk in struct gendisk and to serialize the execution of the storage element management operations using the zone revalidation mutex.
- Rebased on latest block/for-next
Changes from v2:
- Addressed further valid comments from Sashiko
(https://sashiko.dev/#/patchset/20261006124510.882017-1-dlemoal%40kernel.org)
- Rebased on latest block/for-next
Changes from v1:
- Addressed the many valid comments that Sashiko had
(https://sashiko.dev/#/patchset/20261005094632.580753-1-dlemoal%40kernel.org)
- Rebased on latest block/for-next
Damien Le Moal (9):
block: introduce disk_zone_is_offline()
block: refactor blk_revalidate_disk_zones()
block: introduce storage element management
block: add storage element management ioctls
zloop: add storage element emulation
zloop: add degrade_element control command
scsi: sd: fail read commands to offline zones early
scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
scsi: sd_zbc: define storage element management operations
.../admin-guide/blockdev/zoned_loop.rst | 8 +- block/blk-zoned.c | 516 ++++++++++++- block/blk.h | 14 + block/genhd.c | 4 + block/ioctl.c | 5 + drivers/block/zloop.c | 724 ++++++++++++++++-- drivers/scsi/sd.c | 11 +- drivers/scsi/sd.h | 3 + drivers/scsi/sd_zbc.c | 259 ++++++- include/linux/blkdev.h | 31 + include/uapi/linux/blkzoned.h | 83 ++ include/uapi/linux/fs.h | 1 + 12 files changed, 1582 insertions(+), 77 deletions(-)
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 1/9 block: introduce disk_zone_is_offline()
Introduce the helper function disk_zone_is_offline() for testing if a zone of a gendisk has the offline condition. The zone to test is identified using a sector number that must belong to the target zone to check.
--- block/blk-zoned.c | 16 ++++++++++++++++ include/linux/blkdev.h | 9 +++++++++ 2 files changed, 25 insertions(+)
block/blk-zoned.c +16 -0
@@ -292,6 +292,22 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)return !blk_zstate_is_conv (zs); } +/** +* disk_zone_is_offline - check if a sector belongs to an offline zone +* @disk: gendisk to check +* @sector: sector number +* +* Check if @sector on @disk is contained in an offline zone. +*/ +bool disk_zone_is_offline (struct gendisk *disk,sector_t sector) +{ +if (!blk_queue_is_zoned (disk->queue)) +return false; + +return disk_zone_get_cond (disk, sector) ==BLK_ZONE_COND_OFFLINE ; +} +EXPORT_SYMBOL_GPL (disk_zone_is_offline); +/** * bdev_zone_is_seq - check if a sector belongs to a sequential write zone * @bdev: block device to check
include/linux/blkdev.h +9 -0
@@ -942,6 +942,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,return disk->last_zone_capacity;return disk->zone_capacity; } + +bool disk_zone_is_offline (struct gendisk *disk,sector_t sector); +static inline unsigned int bdev_zone_capacity (struct block_device *bdev,sector_t pos) {
@@ -956,6 +959,12 @@ static inline unsigned int disk_nr_zones(struct gendisk *disk)return 0 ; } +static inline bool disk_zone_is_offline (struct gendisk *disk, +sector_t sector) +{ +return false; +} +static inline bool bdev_zone_is_seq (struct block_device *bdev,sector_t sector) {return false;
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 2/9 block: refactor blk_revalidate_disk_zones()
In reply to Damien Le MoalSplit out most of the code of blk_revalidate_disk_zones() into the internal function disk_revalidate_zones(). This will allow calling this new helper together with other code under the zone revalidation mutex. No functional change.
--- block/blk-zoned.c | 63 +++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 29 deletions(-)
block/blk-zoned.c +34 -29
@@ -2542,20 +2542,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,return ret; } -/** -* blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs -* @disk: Target disk -* -* Helper function for low-level device drivers to check, (re) allocate and -* initialize resources used for managing zoned disks. This function should -* normally be called by blk-mq based drivers when a zoned gendisk is probed -* and when the zone configuration of the gendisk changes (e.g. after a format). -* Before calling this function, the device driver must already have set the -* device zone size (chunk_sector limit) and the max zone append limit. -* BIO based drivers can also use this function as long as the device queue -* can be safely frozen. -*/ -int blk_revalidate_disk_zones (struct gendisk *disk) +static int disk_revalidate_zones (struct gendisk *disk) {struct blk_revalidate_zone_args args = { };struct blk_report_zones_args rep_args = {
@@ -2566,19 +2553,10 @@ int blk_revalidate_disk_zones(struct gendisk *disk)int retries =2 ;int ret; -if (WARN_ON_ONCE (!blk_queue_is_zoned (disk->queue))) -return -EIO ; - -/* -* 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); - again: ret =disk_revalidate_capacity (disk, &args);if (ret) - goto unlock; +return ret;/* * Allocate zone resources if they are needed and we have not done
@@ -2591,14 +2569,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk) ret =disk_alloc_zone_resources (disk, args.capacity);if (ret) {memalloc_noio_restore (noio_flag); - goto unlock; +return ret; } } ret =disk_init_revalidate_args (disk, &args);if (ret) {memalloc_noio_restore (noio_flag); - goto unlock; +return ret; } ret = disk->fops->report_zones (disk,0 ,UINT_MAX , &rep_args);
@@ -2615,8 +2593,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)if (ret) goto free_args; -mutex_unlock (&disk->zone_revalidate_mutex); -return 0 ; free_args:
@@ -2632,7 +2608,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk) }pr_warn ("%s: failed to revalidate zones\n" , disk->disk_name); -unlock: + +return ret; +} + +/** +* blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs +* @disk: Target disk +* +* Helper function for low-level device drivers to check, (re) allocate and +* initialize resources used for managing zoned disks. This function should +* normally be called by blk-mq based drivers when a zoned gendisk is probed +* and when the zone configuration of the gendisk changes (e.g. after a format). +* Before calling this function, the device driver must already have set the +* device zone size (chunk_sector limit) and the max zone append limit. +* BIO based drivers can also use this function as long as the device queue +* can be safely frozen. +*/ +int blk_revalidate_disk_zones (struct gendisk *disk) +{ +int ret; + +if (WARN_ON_ONCE (!blk_queue_is_zoned (disk->queue))) +return -EIO ; + +/* +* 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); + ret =disk_revalidate_zones (disk);mutex_unlock (&disk->zone_revalidate_mutex);return ret;
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 3/9 block: introduce storage element management
In reply to Damien Le MoalRecent SCSI (SBC) and ATA (ACS) standards define the storage element depopulation feature. This feature is intended for managing hard-disks heads, either combined read-write heads or pairs of read and write heads, allowing to keep disks with defective heads longer in production by allowing "depopulating" (removing) defective heads.
This feature comes in two different flavors:
- A destructive version which removes a head and reformats the disk at a lower capacity point, restoaring a fully functional contiguous LBA address space.
- A data preserving version restricted to host-managed zoned disks, which marks the zones served by a removed head as offline or read-only.
In preparation for using the data-preserving flavor of the depopulation feature in file systems natively supporting zoned block devices, introduce a set of storage element management operations and functions to define generic calls into block device drivers for managing storage elements.
The set of operations is defined with struct blk_se_ops and includes three operations:
- report_elements: get information on a device storage elements state reported with the new struct blk_se.
- remove_element: depopulate a defective storage element
- restore_elements: restore depopulated storage elements
Each operation is called from the functions bdev_report_storage_elements(), bdev_remove_storage_element() and bdev_restore_storage_elements().
Removing a healthy storage element from a device is possible and useful for testing. The storage element restoration operation restore_elements allows repopulating such healthy element. Repopulating defective storage elements is generally not allowed by devices. The remove_element and restore_elements operations are executed under the zone revalidation lock so that they are serialized iand also serialized against user initiated device revalidation, thus allowing to always have a consistent view of the device zone conditions.
The device drivers of zoned block devices can indicate support for the storage element depopulation feature by specifying the storage element management operations with the se_ops field of struct block_device_operations.
--- block/blk-zoned.c | 293 +++++++++++++++++++++++++++++++++- block/blk.h | 5 + block/genhd.c | 4 + include/linux/blkdev.h | 22 +++ include/uapi/linux/blkzoned.h | 62 +++++++ 5 files changed, 385 insertions(+), 1 deletion(-)
block/blk-zoned.c +292 -1
@@ -18,6 +18,7 @@#include <linux/mempool.h> #include <linux/kthread.h> #include <linux/freezer.h> +#include <linux/delay.h> #include <trace/events/block.h>
@@ -2721,5 +2722,295 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)return 0 ; } -#endif + +static int disk_se_mgmt_in_progress (struct gendisk *disk, +struct blk_se *elements, +bool *in_progress) +{ +unsigned int i, nr_se = disk->nr_storage_elements; +int ret; + +/* +* Check if we have storage elements being removed or restored. +*/ + ret = disk->fops->se_ops->report_elements (disk, elements, &nr_se); +if (ret) { +pr_err ("Failed to get storage elements\n" ); +return ret; + } + +for (i =0 ; i < nr_se; i++) { +switch (elements[i].status) { +case BLK_SE_STS_REMOVE_IN_PROGRESS : +case BLK_SE_STS_RESTORE_IN_PROGRESS : + *in_progress = true; +return 0 ; +default : +break ; + } + } + + *in_progress = false; + +return 0 ; +} + +static int disk_check_remove_error (struct gendisk *disk, +struct blk_se *elements, +unsigned int element_id) +{ +unsigned int i; + +for (i =0 ; i < disk->nr_storage_elements; i++) { +if (elements[i].id == element_id + && elements[i].status ==BLK_SE_STS_REMOVE_ERROR ) +return -EIO ; + } + +return 0 ; +} + +static int disk_wait_for_se_mgmt_completion (struct gendisk *disk, +struct blk_se *elements, +unsigned int element_id) +{ +bool in_progress; +int ret; + +/* +* Poll the storage elements heath state to detect the end of a +* removal or restoration operation. Since we do not know how long the +* operation may take, we cannot have a timeout on this loop. So check +* for fatal signals so that the user can terminate this retry loop if +* the device misbehaves. +*/ +while (1 ) { + ret =disk_se_mgmt_in_progress (disk, elements, &in_progress); +if (ret || !in_progress) +break ; + +if (fatal_signal_pending (current)) { + ret = -EINTR ; +break ; + } + +/* Not done yet: wait and retry. */ +msleep (1000 ); + } + +/* Check for remove errors. */ +if (!ret && !in_progress && element_id) + ret =disk_check_remove_error (disk, elements, element_id); + +return ret; +} + +/** +* bdev_report_storage_elements - report the storage elements of a zoned +* block device +* @bdev: the zoned block device to check +* @elements: array of storage element descriptors to fill +* @nr_elements: maximum size of @elements +* +* Fill at most @nr_elements storage element descriptors in the array +* @elements. The number of storage elements filled in the array is returned +* using @nr_elements. If @elements is NULL, only @nr_elements is returned to +* indicate the maximum number of storage elements of @bdev. +* +* Returns 0 on success and a negative error code on failure. +*/ +int bdev_report_storage_elements (struct block_device *bdev, +struct blk_se *elements, +unsigned int *nr_elements) +{ +struct gendisk *disk = bdev->bd_disk; + +if (!blk_queue_is_zoned (disk->queue) || !disk->nr_storage_elements) +return -EOPNOTSUPP ; + +if (!nr_elements) +return -EINVAL ; + +if (!elements) { + *nr_elements = disk->nr_storage_elements; +return 0 ; + } + + *nr_elements =min (*nr_elements, disk->nr_storage_elements); +if (!*nr_elements) +return -EINVAL ; + +return disk->fops->se_ops->report_elements (disk, elements, + nr_elements); +} +EXPORT_SYMBOL_GPL (bdev_report_storage_elements); + +/** +* bdev_remove_storage_element - Remove (depopulate) a storage element of a +* zoned block device +* @bdev: block device to operate on +* @element_id: ID of the storage element to remove +* +* Remove (depopulate) the storage element identified by @element_id from the +* block device @bdev. The caller is responsible for taking care of any +* necessary device write cache flush and invalidation of cached data for the +* zones that will be offlined. +* +* Returns 0 on success and a negative error code on failure. +*/ +int bdev_remove_storage_element (struct block_device *bdev, +unsigned int element_id) +{ +struct gendisk *disk = bdev->bd_disk; +unsigned int memflags, noio_flag; +struct blk_se *elements; +int ret, err; + +if (!blk_queue_is_zoned (disk->queue) || !disk->nr_storage_elements) +return -EOPNOTSUPP ; + +/* Zero is not a valid storage element ID. */ +if (!element_id) +return -EINVAL ; + + noio_flag =memalloc_noio_save (); + elements = kzalloc_objs(struct blk_se, disk->nr_storage_elements); +memalloc_noio_restore (noio_flag); +if (!elements) +return -ENOMEM ; + +/* +* Serialize against concurrent revalidation and concurrent storage +* element management operations so that we can see consistent zone +* condition changes. +*/ +mutex_lock (&disk->zone_revalidate_mutex); + +/* +* Freeze and unfreeze the queue to flush any outstanding command. +* The caller is responsible for not queuing up more I/Os by higher +* level means. +*/ + memflags =blk_mq_freeze_queue (disk->queue); +blk_mq_unfreeze_queue (disk->queue, memflags); + +/* Initiate the storage element removal and wait for its completion. */ + ret = disk->fops->se_ops->remove_element (disk, element_id); +if (!ret) + ret =disk_wait_for_se_mgmt_completion (disk, elements, + element_id); + +/* +* Always revalidate zones, even in the case of an error as we do not +* know what condition the drive zones are in. +*/ + err =disk_revalidate_zones (disk); +if (err && !ret) + ret = err; + +mutex_unlock (&disk->zone_revalidate_mutex); + +kfree (elements); + +return ret; +} +EXPORT_SYMBOL_GPL (bdev_remove_storage_element); + +/** +* bdev_restore_storage_elements - Restore all depopulated storage elements of +* a zoned block device +* @bdev: block device to operate on +* +* Restore all storage elements of @bdev that have been depopulated. Not all +* elements may be restored by this operation. +* +* Returns 0 on success and a negative error code on failure. +*/ +int bdev_restore_storage_elements (struct block_device *bdev) +{ +struct gendisk *disk = bdev->bd_disk; +unsigned int memflags, noio_flag; +struct blk_se *elements; +int ret, err; + +if (!blk_queue_is_zoned (disk->queue) || !disk->nr_storage_elements) +return -EOPNOTSUPP ; + + noio_flag =memalloc_noio_save (); + elements = kzalloc_objs(struct blk_se, disk->nr_storage_elements); +memalloc_noio_restore (noio_flag); +if (!elements) +return -ENOMEM ; + +/* +* Serialize against concurrent revalidation and concurrent storage +* element management operations so that we can see consistent zone +* condition changes. +*/ +mutex_lock (&disk->zone_revalidate_mutex); + +/* +* Freeze and unfreeze the queue to flush any outstanding commands. +* The caller is responsible for not queuing up more I/O by higher +* level means. +*/ + memflags =blk_mq_freeze_queue (disk->queue); +blk_mq_unfreeze_queue (disk->queue, memflags); + +/* +* Initiate the storage elements restoration and wait for its +* completion. +*/ + ret = disk->fops->se_ops->restore_elements (disk); +if (!ret) + ret =disk_wait_for_se_mgmt_completion (disk, elements,0 ); + +/* +* Always revalidate zones, even in the case of an error as we do not +* know what condition the drive zones are in. +*/ + err =disk_revalidate_zones (disk); +if (err && !ret) + ret = err; + +mutex_unlock (&disk->zone_revalidate_mutex); + +kfree (elements); + +return ret; +} +EXPORT_SYMBOL_GPL (bdev_restore_storage_elements); + +int disk_init_storage_element_mgmt (struct gendisk *disk) +{ +int ret; + +/* +* Storage element operations are for zoned disks only and +* optional... +*/ +if (!blk_queue_is_zoned (disk->queue) || !disk->fops->se_ops) +return 0 ; + +/* ...but if supported, all operations must be defined. */ +if (!disk->fops->se_ops->report_elements || + !disk->fops->se_ops->remove_element || + !disk->fops->se_ops->restore_elements) { +pr_err ("%s: invalid storage element operations\n" , + disk->disk_name); +return -EINVAL ; + } + +/* +* Get the number of storage elements of the disk. If this fails, +* do not report an error as the failure may be because the zoned disk +* does not support storage element management. +*/ + ret = disk->fops->se_ops->report_elements (disk,NULL , + &disk->nr_storage_elements); +if (ret) + disk->nr_storage_elements =0 ; + +return 0 ; +}
block/blk.h +5 -0
@@ -527,6 +527,7 @@ static inline void ioc_clear_queue(struct request_queue *q)#endif /* CONFIG_BLK_ICQ */ #ifdef CONFIG_BLK_DEV_ZONED +int disk_init_storage_element_mgmt (struct gendisk *disk);void disk_init_zone_resources (struct gendisk *disk);void disk_release_zone_resources (struct gendisk *disk);static inline bool bio_zone_write_plugging (struct bio *bio)
@@ -579,6 +580,10 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode, unsigned int cmd, unsigned long arg);bool bdev_zone_mgmt_allowed (struct block_device *bdev,sector_t sector);#else /* CONFIG_BLK_DEV_ZONED */ +static inline int disk_init_storage_element_mgmt (struct gendisk *disk) +{ +return 0 ; +}static inline void disk_init_zone_resources (struct gendisk *disk) { }
block/genhd.c +4 -0
@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,if (WARN_ON_ONCE (bdev_nr_sectors (disk->part0) >BLK_DEV_MAX_SECTORS ))return -EINVAL ; + ret =disk_init_storage_element_mgmt (disk); +if (ret) +return ret; +if (queue_is_mq (disk->queue)) {/* * ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.
include/linux/blkdev.h +22 -0
@@ -189,6 +189,11 @@ struct gendisk {struct disk_events *ev;#ifdef CONFIG_BLK_DEV_ZONED +/* +* Total number of storage elements of the disk. This is always 0 for +* disks that do not support storage element management. +*/ +unsigned int nr_storage_elements;/* * Zoned block device information. Reads of this information must be * protected with blk_queue_enter() / blk_queue_exit() or by holding a
@@ -1583,6 +1588,22 @@ enum blk_unique_id { BLK_UID_NAA = 3, }; +/* Storage element management operations. */ +struct blk_se_ops { +int (*report_elements)(struct gendisk *disk, +struct blk_se *elements, +unsigned int *nr_elements); +int (*remove_element)(struct gendisk *disk,unsigned int element_id); +int (*restore_elements)(struct gendisk *disk); +}; + +int bdev_report_storage_elements (struct block_device *bdev, +struct blk_se *elements, +unsigned int *nr_elements); +int bdev_remove_storage_element (struct block_device *bdev, +unsigned int element_id); +int bdev_restore_storage_elements (struct block_device *bdev); +struct block_device_operations {void (*submit_bio)(struct bio *bio); int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
@@ -1610,6 +1631,7 @@ struct block_device_operations { enum blk_unique_id id_type);struct module *owner;const struct pr_ops *pr_ops; +const struct blk_se_ops *se_ops;/* * Special callback for probing GPT entry at a given sector.
include/uapi/linux/blkzoned.h +62 -0
66 lines not on this page · raw message
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 4/9 block: add storage element management ioctls
In reply to Damien Le MoalDefine the new ioctl commands BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, and BLKRESTORESTORELEMS to provide users accessing zoned block devices directly with an interface to the storage elements management operations of the block layer.
The ioctls BLKGETNRSTORELEMS and BLKREPORTSTORELEMS are defined to respectively get the number of storage elements of a zoned device and to get an array of struct blk_storage_element describing the current state of the device storage elements.
The ioctl BLKREMOVESTORELEM can be used to remove (depopulate) a storage element that has a degraded status (i.e. BLK_SE_STS_DEGRADED).
Finally, the BLKRESTORESTORELEMS ioctl interfaces with bdev_restore_storage_elements() to restore the depopulated storage elements of a zoned device.
--- block/blk-zoned.c | 144 ++++++++++++++++++++++++++++++++++ block/blk.h | 9 +++ block/ioctl.c | 5 ++ include/uapi/linux/blkzoned.h | 21 +++++ include/uapi/linux/fs.h | 1 + 5 files changed, 180 insertions(+)
block/blk-zoned.c +144 -0
@@ -19,6 +19,7 @@#include <linux/kthread.h> #include <linux/freezer.h> #include <linux/delay.h> +#include <linux/uaccess.h> #include <trace/events/block.h>
@@ -2846,6 +2847,56 @@ int bdev_report_storage_elements(struct block_device *bdev, }EXPORT_SYMBOL_GPL (bdev_report_storage_elements); +static int blkdev_get_nr_storage_elements_ioctl (struct block_device *bdev, +void __user *argp) +{ +struct gendisk *disk = bdev->bd_disk; + +if (put_user (disk->nr_storage_elements, (unsigned int __user *)argp)) +return -EFAULT ; + +return 0 ; +} + +static int blkdev_report_storage_elements_ioctl (struct block_device *bdev, +void __user *argp) +{ +struct gendisk *disk = bdev->bd_disk; +struct blk_se_report *rep; +int ret; + +if (!argp) +return -EINVAL ; + + rep =kzalloc_flex (*rep, elements, disk->nr_storage_elements); +if (!rep) +return -ENOMEM ; + +if (copy_from_user (rep, argp,sizeof (struct blk_se_report ))) { + ret = -EFAULT ; + goto free_rep; + } + +if (rep->nr_elements < disk->nr_storage_elements) { + ret = -EINVAL ; + goto free_rep; + } + + rep->nr_reported_elements = disk->nr_storage_elements; + ret =bdev_report_storage_elements (bdev, rep->elements, + &rep->nr_reported_elements); +if (ret) + goto free_rep; + + if (copy_to_user(argp, rep, struct_size_t(struct blk_se_report, + elements, rep->nr_reported_elements))) + ret = -EFAULT ; + +free_rep: +kfree (rep); +return ret; +} +/** * bdev_remove_storage_element - Remove (depopulate) a storage element of a * zoned block device
@@ -2917,6 +2968,52 @@ int bdev_remove_storage_element(struct block_device *bdev, }EXPORT_SYMBOL_GPL (bdev_remove_storage_element); +static int blkdev_flush_and_truncate (struct block_device *bdev, +blk_mode_t mode) +{ +int ret; + +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , +bdev_nr_bytes (bdev) -1 ); +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +} + +static int blkdev_remove_storage_element_ioctl (struct block_device *bdev, +blk_mode_t mode,void __user *argp) +{ +unsigned int element_id; +int ret; + +if (!(mode &BLK_OPEN_WRITE )) +return -EBADF ; +if (bdev_read_only (bdev)) +return -EPERM ; + +if (get_user (element_id, (unsigned int __user *)argp)) +return -EFAULT ; + +/* +* Flush the device volatile write cache and invalidate all cached data +* so that reads do not return old data for zones that went offline. +* Since a user can only write to zoned devices using direct IOs, we +* should never have any dirty page invalidated and so no data loss as +* long as the device cache flush succeeds, that is, as long as the +* flush does not use degraded storage elements. +*/ + ret =blkdev_flush_and_truncate (bdev, mode); +if (ret) +return ret; + +return bdev_remove_storage_element (bdev, element_id); +} +/** * bdev_restore_storage_elements - Restore all depopulated storage elements of * a zoned block device
@@ -2982,6 +3079,28 @@ int bdev_restore_storage_elements(struct block_device *bdev) }EXPORT_SYMBOL_GPL (bdev_restore_storage_elements); +static int blkdev_restore_storage_elements_ioctl (struct block_device *bdev, +blk_mode_t mode) +{ +int ret; + +if (!(mode &BLK_OPEN_WRITE )) +return -EBADF ; +if (bdev_read_only (bdev)) +return -EPERM ; + +/* +* Storage element restoration is a destructive operation that will +* reset all zones. So fflush the device volatile write cache and +* invalidate all cached data that we may have. +*/ + ret =blkdev_flush_and_truncate (bdev, mode); +if (ret) +return ret; + +return bdev_restore_storage_elements (bdev); +} +int disk_init_storage_element_mgmt (struct gendisk *disk) {int ret;
@@ -3014,3 +3133,28 @@ int disk_init_storage_element_mgmt(struct gendisk *disk)return 0 ; } + +int blkdev_zone_storage_elements_ioctl (struct block_device *bdev, +blk_mode_t mode,unsigned int cmd, +void __user *argp) +{ +struct gendisk *disk = bdev->bd_disk; + +if (!disk->nr_storage_elements) +return -ENOTTY ; + +switch (cmd) { +case BLKGETNRSTORELEMS : +return blkdev_get_nr_storage_elements_ioctl (bdev, argp); +case BLKREPORTSTORELEMS : +return blkdev_report_storage_elements_ioctl (bdev, argp); +case BLKREMOVESTORELEM : +return blkdev_remove_storage_element_ioctl (bdev, mode, argp); +case BLKRESTORESTORELEMS : +return blkdev_restore_storage_elements_ioctl (bdev, mode); +default : +break ; + } + +return -ENOTTY ; +}
block/blk.h +9 -0
@@ -579,6 +579,9 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,int blkdev_zone_mgmt_ioctl (struct block_device *bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg);bool bdev_zone_mgmt_allowed (struct block_device *bdev,sector_t sector); +int blkdev_zone_storage_elements_ioctl (struct block_device *bdev, +blk_mode_t mode,unsigned int cmd, +void __user *argp);#else /* CONFIG_BLK_DEV_ZONED */ static inline int disk_init_storage_element_mgmt (struct gendisk *disk) {
@@ -630,6 +633,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev, {return false; } +static inline int blkdev_zone_storage_elements_ioctl (struct block_device *bdev, +blk_mode_t mode,unsigned int cmd, +void __user *argp) +{ +return -ENOTTY ; +}#endif /* CONFIG_BLK_DEV_ZONED */ struct block_device *bdev_alloc (struct gendisk *disk,u8 partno);
block/ioctl.c +5 -0
@@ -679,6 +679,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,return put_uint (argp,bdev_zone_sectors (bdev));case BLKGETNRZONES :return put_uint (argp,bdev_nr_zones (bdev)); +case BLKGETNRSTORELEMS : +case BLKREPORTSTORELEMS : +case BLKREMOVESTORELEM : +case BLKRESTORESTORELEMS : +return blkdev_zone_storage_elements_ioctl (bdev, mode, cmd, argp);case BLKROGET :return put_int (argp,bdev_read_only (bdev) !=0 );case BLKSSZGET :/* get block device logical block size */
include/uapi/linux/blkzoned.h +21 -0
@@ -270,4 +270,25 @@ struct blk_se { __u8 reserved[13]; }; +struct blk_se_report { +__u32 nr_elements; +__u32 nr_reported_elements; +__u64 reserved; +struct blk_se elements[]; +}; + +/** +* Zoned block device storage element management ioctl's: +* +* @BLKGETNRSTORELEMS: Get the number of storage elements of the device. +* @BLKREPORTSTORELEMS: Get the device storage elements. +* @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device. +* @BLKRESTORESTORELEM: Restore (repopulate if possible) all storage elements +* that have been removed. +*/ +#define BLKGETNRSTORELEMS _IOR(0x12, 143, __u32) +#define BLKREPORTSTORELEMS _IOWR(0x12, 144, struct blk_se_report) +#define BLKREMOVESTORELEM _IOW(0x12, 145, __u32) +#define BLKRESTORESTORELEMS _IO(0x12, 146) +#endif /* _UAPI_BLKZONED_H */
include/uapi/linux/fs.h +1 -0
@@ -309,6 +309,7 @@ struct file_attr {/* 130-136 and 142 are used by zoned block device ioctls (uapi/linux/blkzoned.h) */ /* 137-141 are used by blk-crypto ioctls (uapi/linux/blk-crypto.h) */ #define BLKTRACESETUP2 _IOWR(0x12, 142, struct blk_user_trace_setup2) +/* 143-146 are used by storage element management for zoned block devices. */ #define BMAP_IOCTL 1/* obsolete - kept for compatibility */ #define FIBMAP _IO(0x00,1)/* bmap access */
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 5/9 zloop: add storage element emulation
In reply to Damien Le MoalEmulate the storage element depopulation feature of SMR disks in the zloop driver. This emulation is controlled using the new stor_elements option.
This option can take several values:
- ZLOOP_STOR_ELEMENTS_NONE (0): no emulation (default)
- ZLOOP_STOR_ELEMENTS_RDWR (1): emulate all access storage elements (e.g. read+write heads)
- ZLOOP_STOR_ELEMENTS_PAIRS (2): emulate fractional access storage elements (e.g. pairs of read and write heads)
If enabled with the value 1 or 2, the number of storage elements, or of pairs of fractional access storage elements, is automatically calculated based on the number of zones of the device so that we have at least 2 and at most 32 storage elements (or 2 pairs of fractional access storage elements).
The mapping of zones to storage elements is defined simply as the modulo of a zone number and a storage element ID. E.g, removing a storage element from a set of 4 storage elements will offline 1 zone every 4 zones.
The storage element management operations are specified with the zloop_se_ops (struct blk_storage_elements_ops).
--- .../admin-guide/blockdev/zoned_loop.rst | 8 +- drivers/block/zloop.c | 635 ++++++++++++++++-- 2 files changed, 600 insertions(+), 43 deletions(-)
Documentation/admin-guide/blockdev/zoned_loop.rst +7 -1
@@ -61,7 +61,7 @@ The options available for the add command can be listed by reading the /dev/zloop-control device:: $ cat /dev/zloop-control - add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache + add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,max_open_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io,zone_append=%u,ordered_zone_append,discard_write_cache,stor_elements=%u remove id=%d In more details, the options that can be used with the "add" command are as
@@ -113,6 +113,12 @@ discard_write_cache Discard all data that was not explicitly persisted using a each zone file to the size recorded during the last flush operation. This simulates power fail events where uncommitted data is lost. +stor_elements Control storage element emulation. The default value is 0, + indicating no emulation. A value of 1 indicates that all + access storage elements (equivalent to read+write head of + a disk) are emulated. A value of 2 enables fractional + access storage element (equivalent to pairs of read and + write heads of a disk) emulation . =================== ========================================================= 3) Deleting a Zoned Device
drivers/block/zloop.c +593 -42
873 lines not on this page · raw message
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 6/9 zloop: add degrade_element control command
In reply to Damien Le MoalAllow users to mark storage elements of a zloop device as degraded using the new "degrade_element" control command. The element to degrade is indicated using the element_id option. Example:
echo "degrade_element id=0,element_id=2" > /dev/zloop-control
If the element ID identifies an all access storage element, read and write operations targeting a zone served by the degraded lement are failed. For a partial access storage element, read or write operations are failed depending on the storage element type. This check for an element status is done without taking the storage elements mutex lock, so in order to avoid memory access ordering issues, storage element status access is changed to use READ_ONCE() and WRITE_ONCE()
--- drivers/block/zloop.c | 91 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-)
drivers/block/zloop.c +88 -3
@@ -38,6 +38,7 @@ enum {ZLOOP_OPT_DISCARD_WRITE_CACHE = (1 <<11 ),ZLOOP_OPT_MAX_OPEN_ZONES = (1 <<12 ),ZLOOP_OPT_STOR_ELEMENTS = (1 <<13 ), + ZLOOP_OPT_ELEMENT_ID = (1 << 14), }; static const match_table_t zloop_opt_tokens = {
@@ -55,6 +56,7 @@ static const match_table_t zloop_opt_tokens = { { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" }, { ZLOOP_OPT_MAX_OPEN_ZONES, "max_open_zones=%u" }, { ZLOOP_OPT_STOR_ELEMENTS, "stor_elements=%u" }, + { ZLOOP_OPT_ELEMENT_ID, "element_id=%u" }, { ZLOOP_OPT_ERR, NULL } };
@@ -81,6 +83,7 @@ enum zloop_stor_elements {#define ZLOOP_DEF_ZONE_APPEND true#define ZLOOP_DEF_ORDERED_ZONE_APPEND false#define ZLOOP_DEF_STOR_ELEMENTS ZLOOP_STOR_ELEMENTS_NONE +#define ZLOOP_DEF_ELEMENT_ID 0/* Arbitrary limit on the zone size (16GB). */ #define ZLOOP_MAX_ZONE_SIZE_MB 16384
@@ -101,6 +104,7 @@ struct zloop_options {bool ordered_zone_append;bool discard_write_cache;enum zloop_stor_elements stor_elements; +unsigned int element_id; };/*
@@ -1001,7 +1005,8 @@ static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq) se = &zlo->elements[zone->wr_se_id -1 ]; else se = &zlo->elements[zone->rd_se_id -1 ]; -if (READ_ONCE (se->status) ==BLK_SE_STS_REMOVE_IN_PROGRESS ) { +if (READ_ONCE (se->status) ==BLK_SE_STS_DEGRADED || +READ_ONCE (se->status) ==BLK_SE_STS_REMOVE_IN_PROGRESS ) { ret = false; goto unlock; }
@@ -1173,6 +1178,31 @@ static int zloop_report_elements(struct gendisk *disk,return 0 ; } +static int zloop_degrade_element (struct zloop_device *zlo, +unsigned int element_id) +{ +struct blk_se *se; +int ret =0 ; + +if (zlo->stor_elements ==ZLOOP_STOR_ELEMENTS_NONE ) +return -EOPNOTSUPP ; + +if (!element_id || element_id > zlo->nr_elements) +return -EINVAL ; + +mutex_lock (&zlo->stor_elements_lock); + + se = &zlo->elements[element_id -1 ]; +if (READ_ONCE (se->status) ==BLK_SE_STS_OK ) +WRITE_ONCE (se->status,BLK_SE_STS_DEGRADED ); +else + ret = -EINVAL ; + +mutex_unlock (&zlo->stor_elements_lock); + +return ret; +} +static void zloop_remove_element_work (struct work_struct *work) { struct zloop_device *zlo = container_of(work, struct zloop_device,
@@ -2016,6 +2046,42 @@ static int zloop_ctl_remove(struct zloop_options *opts)return 0 ; } +static int zloop_ctl_degrade_element (struct zloop_options *opts) +{ +struct zloop_device *zlo; +int ret =0 ; + +if (!(opts->mask &ZLOOP_OPT_ID )) { +pr_err ("No ID specified for degrade_element\n" ); +return -EINVAL ; + } + +if (opts->mask & ~(ZLOOP_OPT_ID |ZLOOP_OPT_ELEMENT_ID )) { +pr_err ("Invalid option specified for degrade_element\n" ); +return -EINVAL ; + } + +mutex_lock (&zloop_ctl_mutex); + + zlo =idr_find (&zloop_index_idr, opts->id); +if (!zlo || zlo->state == Zlo_creating) + ret = -ENODEV ; +else if (zlo->state == Zlo_deleting) + ret = -EINVAL ; +if (ret) + goto unlock; + + ret =zloop_degrade_element (zlo, opts->element_id); +if (!ret) +pr_info ("Degraded element %u of device %u\n" , + opts->element_id, opts->id); + +unlock: +mutex_unlock (&zloop_ctl_mutex); + +return ret; +} +static int zloop_parse_options (struct zloop_options *opts,const char *buf) {substring_t args[MAX_OPT_ARGS ];
@@ -2036,6 +2102,7 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf) opts->zone_append =ZLOOP_DEF_ZONE_APPEND ; opts->ordered_zone_append =ZLOOP_DEF_ORDERED_ZONE_APPEND ; opts->stor_elements =ZLOOP_DEF_STOR_ELEMENTS ; + opts->element_id =ZLOOP_DEF_ELEMENT_ID ;if (!buf)return 0 ;
@@ -2187,6 +2254,13 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf) } opts->stor_elements = token;break ; +case ZLOOP_OPT_ELEMENT_ID : +if (match_uint (args, &token)) { + ret = -EINVAL ; + goto out; + } + opts->element_id = token; +break ;case ZLOOP_OPT_ERR :default :pr_warn ("unknown parameter or missing value '%s'\n" , p);
@@ -2215,14 +2289,16 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)enum {ZLOOP_CTL_ADD ,ZLOOP_CTL_REMOVE , +ZLOOP_CTL_DEGRADE_ELEMENT , };static struct zloop_ctl_op {int code;const char *name; } zloop_ctl_ops[] = { - {ZLOOP_CTL_ADD ,"add" }, - {ZLOOP_CTL_REMOVE ,"remove" }, + {ZLOOP_CTL_ADD ,"add" }, + {ZLOOP_CTL_REMOVE ,"remove" }, + {ZLOOP_CTL_DEGRADE_ELEMENT ,"degrade_element" }, {-1 ,NULL }, };
@@ -2270,6 +2346,9 @@ static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,case ZLOOP_CTL_REMOVE : ret =zloop_ctl_remove (&opts);break ; +case ZLOOP_CTL_DEGRADE_ELEMENT : + ret =zloop_ctl_degrade_element (&opts); +break ;default :pr_err ("Invalid operation\n" ); ret = -EINVAL ;
@@ -2293,6 +2372,8 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private) tok = &zloop_opt_tokens[i];if (!tok->pattern)break ; +if (tok->token ==ZLOOP_OPT_ELEMENT_ID ) +continue ;if (i)seq_putc (seq_file,',' );seq_puts (seq_file, tok->pattern);
@@ -2303,6 +2384,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)seq_puts (seq_file, zloop_ctl_ops[1 ].name);seq_puts (seq_file," id=%d\n" ); +/* Degrade element operation */ +seq_puts (seq_file, zloop_ctl_ops[2 ].name); +seq_puts (seq_file," id=%d,element_id=%d\n" ); +return 0 ; }
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 7/9 scsi: sd: fail read commands to offline zones early
In reply to Damien Le MoalIssuing read commands to offline zones of a ZBC disk will result in failures. So do not issue such command and fail then early in sd_setup_read_write_cmnd(). This change also checks write commands, which is fine but should actually never happen as the block layer zone write plugging code already fails write BIOs to offline zones early in the submission path.
--- drivers/scsi/sd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/sd.c +7 -1
@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd) {struct request *rq =scsi_cmd_to_rq (cmd);struct scsi_device *sdp = cmd->device; -struct scsi_disk *sdkp =scsi_disk (rq->q->disk); +struct gendisk *disk = rq->q->disk; +struct scsi_disk *sdkp =scsi_disk (disk);sector_t lba =sectors_to_logical (sdp,blk_rq_pos (rq));sector_t threshold;unsigned int nr_blocks =sectors_to_logical (sdp,blk_rq_sectors (rq));
@@ -1456,6 +1457,11 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd) goto fail; } +/* Prevent read/write to offline zones of ZBC disks. */ +if (sdkp->device->type ==TYPE_ZBC && +disk_zone_is_offline (disk,blk_rq_pos (rq))) + goto fail; +/* * Some SD card readers can't handle accesses which touch the * last one or two logical blocks. Split accesses as needed.
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 8/9 scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
In reply to Damien Le Moalsd_zbc_revalidate_zones() skips revalidating the zones of a ZBC device if the zone size and total number of zones of the disk has not changed. This is to avoid a call to the rather slow blk_revalidate_disk_zones().
However, for ZBC devices that support data preserving head depopulation (REMOVE ELEMENT AND MODIFY ZONES command), a disk capacity and number of zones does not change after a head is depopulated but the condition of zones changes as the zones served by the head that was depopulated become either read-only or offline. In this case, not calling blk_revalidate_disk_zones() prevents the block layer from taking appropriate actions on the zone write plugs of the disk for the zones that became read-only or offline.
Avoid any issue with the block layer view of the zone conditions by not skipping the call to blk_revalidate_disk_zones() for disks that support the REMOVE ELEMENT AND MODIFY ZONES command. This check is done from sd_zbc_read_zones() using the helper function sd_zbc_check_modify_zones(). The new scsi disk flag modify_zones_supported is defined to remember the result of this check.
--- drivers/scsi/sd.h | 1 + drivers/scsi/sd_zbc.c | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-)
drivers/scsi/sd.h +1 -0
@@ -156,6 +156,7 @@ struct scsi_disk { unsigned ignore_medium_access_errors : 1; unsigned rscs : 1; /* reduced stream control support */ unsigned use_atomic_write_boundary : 1; + unsigned modify_zones_supported : 1; };#define to_scsi_disk (obj) container_of(obj, struct scsi_disk, disk_dev)
drivers/scsi/sd_zbc.c +25 -1
@@ -516,6 +516,21 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,return 0 ; } +/* +* sd_zbc_check_modify_zones - Check if the device supports depopulation +* @sdkp: Target disk +* @buf: command buffer +* +* Check if the device supports the REMOVE ELEMENT AND MODIFY ZONES command. +*/ +static inline bool sd_zbc_check_modify_zones (struct scsi_disk *sdkp, +unsigned char *buf) +{ +return scsi_report_opcode (sdkp->device, buf,SD_BUF_SIZE , +SERVICE_ACTION_IN_16 , +SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES ) ==1 ; +} +static void sd_zbc_print_zones (struct scsi_disk *sdkp) {if (sdkp->device->type !=TYPE_ZBC || !sdkp->capacity)
@@ -554,9 +569,15 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)if (!blk_queue_is_zoned (q))return 0 ; +/* +* If the zone size and number of zones has not changed, and the disk +* does not support depopulating heads, skip the rather slow call to +* blk_revalidate_disk_zones(). +*/ if (sdkp->zone_info.zone_blocks == zone_blocks && sdkp->zone_info.nr_zones == nr_zones && - disk->nr_zones == nr_zones) + disk->nr_zones == nr_zones && + !sdkp->modify_zones_supported)return 0 ; sdkp->zone_info.zone_blocks = zone_blocks;
@@ -620,6 +641,9 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,if (ret !=0 ) goto err; +/* Check if REMOVE ELEMENT AND MODIFY ZONES is supported. */ + sdkp->modify_zones_supported =sd_zbc_check_modify_zones (sdkp, buf); + nr_zones =round_up (sdkp->capacity, zone_blocks) >>ilog2 (zone_blocks);if (nr_zones >INT_MAX ) { sd_printk(KERN_ERR, sdkp, "Too many zones (%llu)\n",
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v5 9/9 scsi: sd_zbc: define storage element management operations
In reply to Damien Le MoalDefine the storage element management operations using struct blk_storage_elements_ops. These operations are valid only on SMR disks supporting the storage element depopulation feature.
--- drivers/scsi/sd.c | 3 + drivers/scsi/sd.h | 2 + drivers/scsi/sd_zbc.c | 233 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 238 insertions(+)
drivers/scsi/sd.c +3 -0
@@ -3943,6 +3943,9 @@ static const struct block_device_operations sd_fops = { .get_unique_id = sd_get_unique_id, .free_disk = scsi_disk_free_disk, .pr_ops = &sd_pr_ops, +#ifdef CONFIG_BLK_DEV_ZONED + .se_ops = &sd_zbc_se_ops, +#endif };/**
drivers/scsi/sd.h +2 -0
@@ -243,6 +243,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,int sd_zbc_report_zones (struct gendisk *disk,sector_t sector,unsigned int nr_zones,struct blk_report_zones_args *args); +extern const struct blk_se_ops sd_zbc_se_ops; +#else /* CONFIG_BLK_DEV_ZONED */ static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
drivers/scsi/sd_zbc.c +233 -0
239 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 3/9] block: introduce storage element management
In reply to Damien Le MoalThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] block: missing queue freeze during zoned device revalidation after element removal --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] block: missing queue freeze during zoned device revalidation after element removal --
commit b956da4759d697b5157b820cef7c5479dd817a3b
Author: Damien Le Moal <[email protected]>
block: introduce storage element management
This commit introduces storage element management operations for zoned block devices. It adds functions to report, remove, and restore storage elements to support features like element depopulation for defective heads.
Quoted 4 lines
block/blk-zoned.c +0
-0
[ ... ]
Quoted 3 lines
block/blk-zoned.c +3
-0+int bdev_remove_storage_element (struct block_device *bdev, +unsigned int element_id) +{
[ ... ]
Quoted 9 lines
block/blk-zoned.c +9
-0+/* +* Freeze and unfreeze the queue to flush any outstanding command. +* The caller is responsible for not queuing up more I/Os by higher +* level means. +*/ + memflags =blk_mq_freeze_queue (disk->queue); +blk_mq_unfreeze_queue (disk->queue, memflags); + +/* Initiate the storage element removal and wait for its completion. */
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2908
Quoted 10 lines
block/blk-zoned.c +10
-0+ ret = disk->fops->se_ops->remove_element (disk, element_id); +if (!ret) + ret =disk_wait_for_se_mgmt_completion (disk, elements, + element_id); + +/* +* Always revalidate zones, even in the case of an error as we do not +* know what condition the drive zones are in. +*/ + err =disk_revalidate_zones (disk);
[Severity: High]
Could this unfreeze the queue too early?
The queue is unfrozen before disk_revalidate_zones() is called, which means the internal zone arrays and capacities can be modified while I/O is potentially in flight. This might allow concurrent readers in the block layer, who rely on blk_queue_enter() for protection, to experience torn reads or use invalid zone states.
Would it be safer to hold the queue freeze until after the zone revalidation is complete to prevent these concurrent access issues?
[ ... ]
Quoted 2 lines
block/blk-zoned.c +2
-0+int bdev_restore_storage_elements (struct block_device *bdev) +{
[ ... ]
Quoted 11 lines
block/blk-zoned.c +11
-0+/* +* Freeze and unfreeze the queue to flush any outstanding commands. +* The caller is responsible for not queuing up more I/O by higher +* level means. +*/ + memflags =blk_mq_freeze_queue (disk->queue); +blk_mq_unfreeze_queue (disk->queue, memflags); + +/* +* Initiate the storage elements restoration and wait for its +* completion.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2973
Quoted 10 lines
block/blk-zoned.c +10
-0+*/ + ret = disk->fops->se_ops->restore_elements (disk); +if (!ret) + ret =disk_wait_for_se_mgmt_completion (disk, elements,0 ); + +/* +* Always revalidate zones, even in the case of an error as we do not +* know what condition the drive zones are in. +*/ + err =disk_revalidate_zones (disk);
[Severity: High]
Does this share the same queue freeze vulnerability as the removal path?
Just like in bdev_remove_storage_element(), the queue is unfrozen prior to calling disk_revalidate_zones(), leaving the internal state vulnerable to concurrent I/O races during restoration.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Damien Le Moal
maintainerRe: [PATCH v5 3/9] block: introduce storage element management
Quoted 49 lines · On 2026/10/12 15:23, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] block: missing queue freeze during zoned device revalidation after element removal --commit
b956da4759d697b5157b820cef7c5479dd817a3b
Author: Damien Le Moal <[email protected]>block: introduce storage element management
This commit introduces storage element management operations for zoned block devices. It adds functions to report, remove, and restore storage elements to support features like element depopulation for defective heads.
Quoted 4 lines
block/blk-zoned.c +0
-0[ ... ]
Quoted 3 lines
block/blk-zoned.c +3
-0+int bdev_remove_storage_element (struct block_device *bdev, +unsigned int element_id) +{[ ... ]
Quoted 9 lines
block/blk-zoned.c +9
-0+/* +* Freeze and unfreeze the queue to flush any outstanding command. +* The caller is responsible for not queuing up more I/Os by higher +* level means. +*/ + memflags =blk_mq_freeze_queue (disk->queue); +blk_mq_unfreeze_queue (disk->queue, memflags); + +/* Initiate the storage element removal and wait for its completion. */ block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2908
Quoted 10 lines
block/blk-zoned.c +10
-0+ ret = disk->fops->se_ops->remove_element (disk, element_id); +if (!ret) + ret =disk_wait_for_se_mgmt_completion (disk, elements, + element_id); + +/* +* Always revalidate zones, even in the case of an error as we do not +* know what condition the drive zones are in. +*/ + err =disk_revalidate_zones (disk);[Severity: High]
Could this unfreeze the queue too early?
Quoted 8 lines
The queue is unfrozen before
disk_revalidate_zones()is called, which means the internal zone arrays and capacities can be modified while I/O is potentially in flight. This might allow concurrent readers in the block layer, who rely onblk_queue_enter()for protection, to experience torn reads or use invalid zone states.Would it be safer to hold the queue freeze until after the zone revalidation is complete to prevent these concurrent access issues?
Ideally, yes, we should freeze the queue to prevent user IOs from being issued while an element is being depoped. However, the current queue freeze will block *all* commands, even internal ones that we need to use to initiate the depop and poll for completion.
So we cannot hold the queue freeze here. Further patches will improve on this by introducing a "user freeze" to block only user IO and let internal commands proceed. But that is for later.
Quoted 37 lines
[ ... ]
Quoted 2 lines
block/blk-zoned.c +2
-0+int bdev_restore_storage_elements (struct block_device *bdev) +{[ ... ]
Quoted 11 lines
block/blk-zoned.c +11
-0+/* +* Freeze and unfreeze the queue to flush any outstanding commands. +* The caller is responsible for not queuing up more I/O by higher +* level means. +*/ + memflags =blk_mq_freeze_queue (disk->queue); +blk_mq_unfreeze_queue (disk->queue, memflags); + +/* +* Initiate the storage elements restoration and wait for its +* completion. block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2973
Quoted 10 lines
block/blk-zoned.c +10
-0+*/ + ret = disk->fops->se_ops->restore_elements (disk); +if (!ret) + ret =disk_wait_for_se_mgmt_completion (disk, elements,0 ); + +/* +* Always revalidate zones, even in the case of an error as we do not +* know what condition the drive zones are in. +*/ + err =disk_revalidate_zones (disk);[Severity: High]
Does this share the same queue freeze vulnerability as the removal path?Just like in
bdev_remove_storage_element(), the queue is unfrozen prior to callingdisk_revalidate_zones(), leaving the internal state vulnerable to concurrent I/O races during restoration.
Signature · 2 lines
Damien Le Moal Western Digital Research
[email protected]
AI reviewRe: [PATCH v5 4/9] block: add storage element management ioctls
In reply to Damien Le MoalThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] block: concurrent I/O race in storage element removal/restoration - [High] block: silent data loss…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] block: concurrent I/O race in storage element removal/restoration
- [High] block: silent data loss in conventional zones on storage element ioctls
- [High] block: cross-partition data destruction via storage element ioctls
- [Medium] block: zone state permanently desynchronized if SE ioctl is interrupted --
commit 8f33e7baabedb5d2ad1cccf418448851e57fed71
Author: Damien Le Moal <[email protected]>
block: add storage element management ioctls
This commit introduces new ioctl commands for storage element management on zoned block devices. The new commands allow userspace to query, report, remove, and restore storage elements.
Quoted 4 lines
block/blk-zoned.c +0
-0
[ ... ]
Quoted 5 lines
block/blk-zoned.c +1
-0@@ -2917,6 +2968,52 @@ int bdev_remove_storage_element(struct block_device *bdev, }EXPORT_SYMBOL_GPL (bdev_remove_storage_element); +static int blkdev_flush_and_truncate(struct block_device *bdev,
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2981
Quoted 10 lines
block/blk-zoned.c +10
-0+ blk_mode_t mode) +{ +int ret; + +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , +bdev_nr_bytes (bdev) -1 );
[Severity: High]
Will this discard unwritten buffered data in conventional zones?
While users might typically use direct I/O for zoned devices, zoned devices can contain conventional zones which fully support and commonly use buffered I/O. truncate_bdev_range() delegates to truncate_inode_pages_range() which forcefully drops all pages in the mapping. Could dirty pages be silently destroyed here? Should this code call filemap_write_and_wait() before truncating the cache?
[Severity: High]
Does this only invalidate the page cache for the opened partition?
When a user invokes these ioctls on a partition (e.g., /dev/sda1), bdev->bd_mapping is the page cache for that partition. Since the underlying hardware operation modifies the entire physical device, the page caches for the whole disk and other partitions are not invalidated, which could lead to stale reads and silent data corruption across the system.
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2986
Quoted 5 lines
block/blk-zoned.c +5
-0+filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +}
[Severity: High]
Are we safely dropping the VFS locks before the storage element removal starts?
The locks preventing concurrent I/O and page cache repopulation are released here. When the ioctl handler later delegates to bdev_remove_storage_element(), there is no barrier preventing concurrent processes from opening the device and issuing buffered or direct I/Os during a destructive hardware operation.
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2997
Quoted 11 lines
block/blk-zoned.c +11
-0+ +static int blkdev_remove_storage_element_ioctl (struct block_device *bdev, +blk_mode_t mode,void __user *argp) +{ +unsigned int element_id; +int ret; + +if (!(mode &BLK_OPEN_WRITE )) +return -EBADF ; +if (bdev_read_only (bdev)) +return -EPERM ;
[Severity: High]
Is a user with write access to a single partition able to destroy data on other partitions?
The storage element ioctls issue a disk-wide destructive hardware command that can alter the physical geometry of the drive. Without checking if the block device is a partition (e.g., via bdev_is_partition()) or requiring capable(CAP_SYS_ADMIN), these operations might destroy zones belonging to other partitions.
[ ... ]
Quoted 4 lines
block/blk-zoned.c +4
-0+/* +* Flush the device volatile write cache and invalidate all cached data +* so that reads do not return old data for zones that went offline. +* Since a user can only write to zoned devices using direct IOs, we
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 3015
Quoted 10 lines
block/blk-zoned.c +10
-0+* should never have any dirty page invalidated and so no data loss as +* long as the device cache flush succeeds, that is, as long as the +* flush does not use degraded storage elements. +*/ + ret =blkdev_flush_and_truncate (bdev, mode); +if (ret) +return ret; + +return bdev_remove_storage_element (bdev, element_id); +}
[Severity: High]
Is it safe to rely on the caller to prevent new I/Os here?
bdev_remove_storage_element() freezes the queue but immediately unfreezes it before executing the long-running remove_element callback. Since the VFS locks were already dropped in blkdev_flush_and_truncate(), the device is left unprotected from concurrent I/Os during a destructive hardware operation.
[Severity: Medium]
Could the zone state become permanently desynchronized if this ioctl is interrupted?
When this ioctl is interrupted by a fatal signal, the underlying loop in disk_wait_for_se_mgmt_completion() returns -EINTR and bdev_remove_storage_element() immediately revalidates the zones while the hardware operation is still in progress.
Because the polling loop was the only tracking mechanism, the kernel's zones_state array is never updated to reflect the final state of the depopulated zones once the hardware finishes. This leaves the kernel permanently desynchronized, potentially leading to unexpected I/O errors.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Damien Le Moal
maintainerRe: [PATCH v5 4/9] block: add storage element management ioctls
Quoted 42 lines · On 2026/10/12 15:40, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] block: concurrent I/O race in storage element removal/restoration
- [High] block: silent data loss in conventional zones on storage element ioctls
- [High] block: cross-partition data destruction via storage element ioctls
- [Medium] block: zone state permanently desynchronized if SE ioctl is interrupted --commit
8f33e7baabedb5d2ad1cccf418448851e57fed71
Author: Damien Le Moal <[email protected]>block: add storage element management ioctls
This commit introduces new ioctl commands for storage element management on zoned block devices. The new commands allow userspace to query, report, remove, and restore storage elements.
Quoted 4 lines
block/blk-zoned.c +0
-0[ ... ]
Quoted 5 lines
block/blk-zoned.c +1
-0@@ -2917,6 +2968,52 @@ int bdev_remove_storage_element(struct block_device *bdev, }EXPORT_SYMBOL_GPL (bdev_remove_storage_element); +static int blkdev_flush_and_truncate(struct block_device *bdev,block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2981
Quoted 10 lines
block/blk-zoned.c +10
-0+ blk_mode_t mode) +{ +int ret; + +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , +bdev_nr_bytes (bdev) -1 );[Severity: High]
Will this discard unwritten buffered data in conventional zones?
Quoted 6 lines
While users might typically use direct I/O for zoned devices, zoned devices can contain conventional zones which fully support and commonly use buffered I/O.
truncate_bdev_range()delegates totruncate_inode_pages_range()which forcefully drops all pages in the mapping. Could dirty pages be silently destroyed here? Should this code callfilemap_write_and_wait()before truncating the cache?
Yes. It is the responsibility of the user to sync its dirty data before initiating a depop.
Quoted 20 lines
[Severity: High]
Does this only invalidate the page cache for the opened partition?When a user invokes these ioctls on a partition (e.g.,
/dev/sda1),bdev->bd_mappingis the page cache for that partition. Since the underlying hardware operation modifies the entire physical device, the page caches for the whole disk and other partitions are not invalidated, which could lead to stale reads and silent data corruption across the system.block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2986
Quoted 5 lines
block/blk-zoned.c +5
-0+filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +}[Severity: High]
Are we safely dropping the VFS locks before the storage element removal starts?
Quoted 5 lines
The locks preventing concurrent I/O and page cache repopulation are released here. When the ioctl handler later delegates to
bdev_remove_storage_element(), there is no barrier preventing concurrent processes from opening the device and issuing buffered or direct I/Os during a destructive hardware operation.
Yes. It is the reponsibility of the user to not issue IOs while depop in on-going.
Quoted 16 lines
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2997
Quoted 12 lines
block/blk-zoned.c +11
-0+ +static int blkdev_remove_storage_element_ioctl (struct block_device *bdev, +blk_mode_t mode,void __user *argp) +{ +unsigned int element_id; +int ret; + +if (!(mode &BLK_OPEN_WRITE )) +return -EBADF ; +if (bdev_read_only (bdev)) +return -EPERM ;[Severity: High]
Is a user with write access to a single partition able to destroy data on other partitions?
Partitions on zoned block devices are *not* supported. There are no partitions, only the entire bdev.
Quoted 26 lines
The storage element ioctls issue a disk-wide destructive hardware command that can alter the physical geometry of the drive. Without checking if the block device is a partition (e.g., via
bdev_is_partition()) or requiringcapable(CAP_SYS_ADMIN), these operations might destroy zones belonging to other partitions.[ ... ]
Quoted 4 lines
block/blk-zoned.c +4
-0+/* +* Flush the device volatile write cache and invalidate all cached data +* so that reads do not return old data for zones that went offline. +* Since a user can only write to zoned devices using direct IOs, we block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 3015
Quoted 10 lines
block/blk-zoned.c +10
-0+* should never have any dirty page invalidated and so no data loss as +* long as the device cache flush succeeds, that is, as long as the +* flush does not use degraded storage elements. +*/ + ret =blkdev_flush_and_truncate (bdev, mode); +if (ret) +return ret; + +return bdev_remove_storage_element (bdev, element_id); +}[Severity: High]
Is it safe to rely on the caller to prevent new I/Os here?
Quoted 5 lines
bdev_remove_storage_element()freezes the queue but immediately unfreezes it before executing the long-runningremove_elementcallback. Since the VFS locks were already dropped inblkdev_flush_and_truncate(), the device is left unprotected from concurrent I/Os during a destructive hardware operation.
Yes it is safe. If the user still issues IOs while depop or restore is in progress, IOs may/will fail and the zone write plugging code will handle that, so will the final zone revalidation.
Quoted 10 lines
[Severity: Medium]
Could the zone state become permanently desynchronized if this ioctl is interrupted?When this ioctl is interrupted by a fatal signal, the underlying loop in
disk_wait_for_se_mgmt_completion()returns-EINTRandbdev_remove_storage_element()immediately revalidates the zones while the hardware operation is still in progress.
Quoted 4 lines
Because the polling loop was the only tracking mechanism, the kernel's
zones_statearray is never updated to reflect the final state of the depopulated zones once the hardware finishes. This leaves the kernel permanently desynchronized, potentially leading to unexpected I/O errors.
Yes it will potentially desynchronize the state, but the user can initiate a revalidation to re-synchronize everything. If the poll loop was interrupted, a problem happened anyway.
Signature · 2 lines
Damien Le Moal Western Digital Research
[email protected]
AI reviewRe: [PATCH v5 5/9] zloop: add storage element emulation
In reply to Damien Le MoalThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] block: array underflow in zloop_remove_element() via element_id == 0 - [High] block: UAF of…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] block: array underflow in zloop_remove_element() via element_id == 0
- [High] block: UAF of disk->queue in delayed works during zloop_free_disk()
- [High] block: double-free of elements and other structures in zloop_ctl_add() error path
- [High] block/zloop: softirq deadlock on wp_lock in process context
- [Medium] block: emulation state inconsistency on partial restore failure
- [Medium] block: zone management commands bypass element removal lock-out --
commit 1027b8e5f9403129fc0dca5c5db75877129cfc64
Author: Damien Le Moal <[email protected]>
zloop: add storage element emulation
This commit adds storage element emulation to the zloop driver, simulating the depopulation feature of SMR disks. It allows mapping zones to specific storage elements to test offline behaviors and transitions.
drivers/block/zloop.c static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq) · line 1016
Quoted 11 lines
drivers/block/zloop.c +0
-0@@ -985,6 +1011,41 @@ static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq) {struct zloop_zone *zone = &zlo->zones[rq_zone_no (rq)];bool is_write =op_is_write (req_op (rq));bool ret = true;spin_lock (&zone->wp_lock);
[Severity: High]
Can this cause a softirq deadlock? The zloop_prep_rq() function is called from zloop_queue_rq(), which runs in softirq context. It acquires zone->wp_lock using a plain spin_lock(). Later in this file, several new process-context functions (such as zloop_remove_element_work and zloop_restore_conv_zone) also acquire zone->wp_lock without disabling softirqs. If a process holding the lock is preempted by a softirq attempting to queue I/O to the same zone, it could result in a deadlock.
Quoted 26 lines
drivers/block/zloop.c +25
-0+if (zlo->nr_elements) { +struct blk_se *se; + +/* +* Check the zone condition and the health state of the storage +* element serving the zone: do not allow any operation to +* offline zones or if the storage element serving the zone is +* being removed, and allow only reads to read-only zones. +*/ +if (is_write) + se = &zlo->elements[zone->wr_se_id -1 ]; +else + se = &zlo->elements[zone->rd_se_id -1 ]; +if (READ_ONCE (se->status) ==BLK_SE_STS_REMOVE_IN_PROGRESS ) { + ret = false; + goto unlock; + } + +if (zone->cond ==BLK_ZONE_COND_OFFLINE || + (zone->cond ==BLK_ZONE_COND_READONLY && is_write)) { + ret = false; + goto unlock; + } + } +
[ ... ]
Quoted 15 lines
drivers/block/zloop.c +5
-5@@ -1040,11 +1188,19 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx, }/* -* If we need to strongly order zone append operations, set the request -* sector to the zone write pointer location now instead of when the -* command work runs. +* Do not allow commands while the restoration of storage elements is +* in progress. */ -if (zlo->ordered_zone_append &&req_op (rq) ==REQ_OP_ZONE_APPEND ) { -if (!zloop_set_zone_append_sector (rq)) +if (READ_ONCE (zlo->restore_in_progress)) +return BLK_STS_IOERR ; +
drivers/block/zloop.c static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx, · line 1206
Quoted 10 lines
drivers/block/zloop.c +8
-0+switch (req_op (rq)) { +case REQ_OP_READ : +case REQ_OP_WRITE : +case REQ_OP_ZONE_APPEND : +if (!zloop_prep_rq (zlo, rq))return BLK_STS_IOERR ; +break ; +default : +break ; }
[Severity: Medium]
Do zone management commands bypass the element removal lock-out? Operations like REQ_OP_ZONE_RESET are not handled by zloop_prep_rq() here, meaning they do not check if the storage element is in the BLK_SE_STS_REMOVE_IN_PROGRESS state. Could this allow them to proceed on zones belonging to a storage element that is actively undergoing depopulation?
[ ... ]
Quoted 4 lines
drivers/block/zloop.c +4
-0+static void zloop_remove_element_work (struct work_struct *work) +{ + struct zloop_device *zlo = container_of(work, struct zloop_device, + remove_element_work.work);
drivers/block/zloop.c static int zloop_report_zones(struct gendisk *disk, sector_t sector, · line 1189
Quoted 10 lines
drivers/block/zloop.c +10
-0+struct blk_se *se, *paired_se =NULL ; +enum blk_zone_cond cond; +struct zloop_zone *zone; +unsigned int memflags, i; + +mutex_lock (&zlo->stor_elements_lock); + +/* Wait for all IOs to complete. */ + memflags =blk_mq_freeze_queue (zlo->disk->queue); +blk_mq_unfreeze_queue (zlo->disk->queue, memflags);
[Severity: High]
Does this worker access the queue after it may have been freed? When the disk is removed, disk_release() drops the queue reference and schedules it for RCU free, followed by a call to zloop_free_disk() which calls cancel_delayed_work_sync(). If this worker executes during that time, could it result in a use-after-free on zlo->disk->queue here?
[ ... ]
Quoted 15 lines
drivers/block/zloop.c +15
-0+static int zloop_remove_element (struct gendisk *disk,unsigned int element_id) +{ +struct zloop_device *zlo = disk->private_data; +struct blk_se *se, *paired_se =NULL ; +unsigned int nr_remove =1 ; +int ret =0 ; + +if (zlo->stor_elements ==ZLOOP_STOR_ELEMENTS_NONE ) +return -EOPNOTSUPP ; + +if (element_id > zlo->nr_elements) +return -EINVAL ; + +mutex_lock (&zlo->stor_elements_lock); +
drivers/block/zloop.c static int zloop_report_zones(struct gendisk *disk, sector_t sector, · line 1262
Quoted 10 lines
drivers/block/zloop.c +10
-0+if (zlo->remove_element_id ||READ_ONCE (zlo->restore_in_progress)) { + ret = -EBUSY ; + goto unlock; + } + +/* +* Get the element to remove. If it is already removed, we have nothing +* to do. +*/ + se = &zlo->elements[element_id -1 ];
[Severity: High]
Is it possible for element_id to be 0 here? The bounds check above prevents element_id > zlo->nr_elements, but does not check for element_id == 0. If element_id is 0, element_id - 1 could underflow and result in an out-of-bounds array access on zlo->elements.
[ ... ]
Quoted 31 lines
drivers/block/zloop.c +31
-0+static void zloop_restore_elements_work (struct work_struct *work) +{ + struct zloop_device *zlo = container_of(work, struct zloop_device, + restore_elements_work.work); +struct zloop_zone *zone; +struct blk_se *se; +unsigned int memflags, i; +enum blk_se_status sts; +int ret =0 ; + +mutex_lock (&zlo->stor_elements_lock); + +/* Wait for all IOs to complete. */ + memflags =blk_mq_freeze_queue (zlo->disk->queue); +blk_mq_unfreeze_queue (zlo->disk->queue, memflags); + +/* Reset all zones. */ +for (i =0 , zone = zlo->zones; i < zlo->nr_zones; i++, zone++) { +if (test_bit (ZLOOP_ZONE_CONV , &zone->flags)) { +zloop_restore_conv_zone (zlo, zone); +continue ; + } + ret =zloop_restore_seq_zone (zlo, zone); +if (ret) +break ; + } + +/* +* If we had an error, maintain all elements as removed instead of +* using the restore error status. +*/
drivers/block/zloop.c static int zloop_report_zones(struct gendisk *disk, sector_t sector, · line 1367
Quoted 10 lines
drivers/block/zloop.c +10
-0+if (ret) + sts =BLK_SE_STS_REMOVED ; +else + sts =BLK_SE_STS_OK ; + +/* Restore all removed elements. */ +for (i =0 , se = zlo->elements; i < zlo->nr_elements; i++, se++) { +if (READ_ONCE (se->status) ==BLK_SE_STS_RESTORE_IN_PROGRESS ) +WRITE_ONCE (se->status, sts); + }
[Severity: Medium]
If zloop_restore_seq_zone() fails and breaks the loop, does this leave the already-processed zones in the BLK_ZONE_COND_EMPTY state? Since the element status reverts to BLK_SE_STS_REMOVED on failure, this mismatch might trick zloop_prep_rq() into allowing I/O to those EMPTY zones, even though their storage element is officially removed.
[ ... ]
Quoted 19 lines
static void
zloop_free_disk(struct gendisk *disk){drivers/block/zloop.c +4
-0struct zloop_device *zlo = disk->private_data;unsigned int i; +cancel_delayed_work_sync (&zlo->remove_element_work); +cancel_delayed_work_sync (&zlo->restore_elements_work); +blk_mq_free_tag_set (&zlo->tag_set);for (i =0 ; i < zlo->nr_zones; i++) {@@ -1019,15 +1434,24 @@ static void zloop_free_disk(struct gendisk *disk)fput (zlo->data_dir);destroy_workqueue (zlo->workqueue); +kfree (zlo->elements);kfree (zlo->base_dir);kvfree (zlo); }
[ ... ]
drivers/block/zloop.c
Quoted 12 lines
drivers/block/zloop.c +2
-0@@ -1384,6 +1915,8 @@ static int zloop_ctl_add(struct zloop_options *opts)kfree (zlo->base_dir); out_destroy_workqueue:destroy_workqueue (zlo->workqueue); +out_destroy_storage_elements: +kfree (zlo->elements); out_free_idr:mutex_lock (&zloop_ctl_mutex);idr_remove (&zloop_index_idr, zlo->id);drivers/block/zloop.c +0
-0mutex_unlock (&zloop_ctl_mutex); out_free_dev:kvfree (zlo);
[Severity: High]
Does this error path cause a double-free? If add_disk() fails, the code jumps to out_cleanup_disk, which calls put_disk(zlo->disk). This drops the final reference and invokes zloop_free_disk(), successfully freeing zlo->elements, the workqueue, and zlo itself. Continuing the fall-through then explicitly calls kfree(zlo->elements) and kvfree(zlo) again on the already-freed memory.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v5 6/9] zloop: add degrade_element control command
In reply to Damien Le MoalThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] block: documentation format string mismatch for element_id in zloop_ctl_show() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] block: documentation format string mismatch for element_id in zloop_ctl_show() --
commit 09c168795398e63847062539d79768bb5214f9cb
Author: Damien Le Moal <[email protected]>
zloop: add degrade_element control command
This commit introduces a new degrade_element control command to allow users to mark storage elements of a zloop device as degraded. The user specifies the element ID to be degraded via the element_id option. Operations to zones served by degraded elements will then be failed.
Quoted 4 lines
drivers/block/zloop.c +0
-0
[ ... ]
drivers/block/zloop.c static int zloop_ctl_show(struct seq_file *seq_file, void *private) · line 2389
Quoted 7 lines
drivers/block/zloop.c +3
-0@@ -2303,6 +2384,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)seq_puts (seq_file, zloop_ctl_ops[1 ].name);seq_puts (seq_file," id=%d\n" ); +/* Degrade element operation */ +seq_puts (seq_file, zloop_ctl_ops[2 ].name); +seq_puts (seq_file," id=%d,element_id=%d\n" );
[Severity: Low]
Does this documentation output accurately reflect the expected format?
The option parser for zloop uses match_uint() for ZLOOP_OPT_ELEMENT_ID:
drivers/block/zloop.c:zloop_parse_options() {
...
case ZLOOP_OPT_ELEMENT_ID:
if (match_uint(args, &token)) {
ret = -EINVAL;
goto out;
}
opts->element_id = token;
break;
...
}If a user follows the %d documentation from zloop_ctl_show() and provides a signed negative integer, the parser will reject it. Should the output string in zloop_ctl_show() use %u instead to match the parser's requirement and avoid misleading users?
Quoted 3 lines
drivers/block/zloop.c +1
-0+return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v5 8/9] scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
In reply to Damien Le MoalThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] scsi: data race on bitfield sdkp->modify_zones_supported vs sysfs updates --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] scsi: data race on bitfield sdkp->modify_zones_supported vs sysfs updates --
commit 008f16ea9959160e815da59ad94c230e5abd0803
Author: Damien Le Moal <[email protected]>
scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
This commit prevents the block layer from skipping blk_revalidate_disk_zones() for ZBC devices that support head depopulation. It introduces a new bitfield, modify_zones_supported, to remember if the device supports the REMOVE ELEMENT AND MODIFY ZONES command.
drivers/scsi/sd.h struct scsi_disk { · line 161
Quoted 11 lines
drivers/scsi/sd.h +1
-0@@ -156,6 +156,7 @@ struct scsi_disk { unsigned ignore_medium_access_errors : 1; unsigned rscs : 1; /* reduced stream control support */ unsigned use_atomic_write_boundary : 1; + unsigned modify_zones_supported : 1; };#define to_scsi_disk (obj) container_of(obj, struct scsi_disk, disk_dev)
[Severity: Medium]
Is it safe to place this bitfield here? It shares the same machine word as the sysfs-writable (WCE, cache_override) and EH-writable (ignore_medium_access_errors) bitfields.
Quoted 4 lines
drivers/scsi/sd_zbc.c +0
-0
[ ... ]
drivers/scsi/sd_zbc.c int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim, · line 649
Quoted 10 lines
drivers/scsi/sd_zbc.c +3
-0@@ -620,6 +641,9 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,if (ret !=0 ) goto err; +/* Check if REMOVE ELEMENT AND MODIFY ZONES is supported. */ + sdkp->modify_zones_supported =sd_zbc_check_modify_zones (sdkp, buf); + nr_zones =round_up (sdkp->capacity, zone_blocks) >>ilog2 (zone_blocks);if (nr_zones >INT_MAX ) { sd_printk(KERN_ERR, sdkp, "Too many zones (%llu)\n",
[Severity: Medium]
Can this lockless update race with other bitfield updates, leading to a lost write?
Because bitfield updates compile down to non-atomic read-modify-write operations, and no locks synchronize sysfs stores like cache_type_store() with disk revalidation, concurrent modifications to these bitfields could result in a data race and lost updates.
If the modify_zones_supported update is lost, sd_zbc_revalidate_zones() might skip blk_revalidate_disk_zones(), leading to a stale block-layer view of offline/read-only zones.
Alternatively, if a sysfs update to cache_type (WCE or cache_override) is lost, it could lead to an unsafe or unexpected write cache configuration.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Christoph Hellwig
reviewerRe: [PATCH v5 3/9] block: introduce storage element management
In reply to Damien Le Moalblock/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2767
Quoted 9 lines · Damien Le Moal
block/blk-zoned.c +9
-0+static int disk_check_remove_error (struct gendisk *disk, +struct blk_se *elements, +unsigned int element_id) +{ +unsigned int i; + +for (i =0 ; i < disk->nr_storage_elements; i++) { +if (elements[i].id == element_id + && elements[i].status ==BLK_SE_STS_REMOVE_ERROR )
kernel style places the && on the previous line.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2879
Quoted 3 lines
block/blk-zoned.c +3
-0+ noio_flag =memalloc_noio_save (); + elements = kzalloc_objs(struct blk_se, disk->nr_storage_elements); +memalloc_noio_restore (noio_flag);
This is a bit odd, we can still use GFP_NOIO for now, but if we want to use the scope API, I'd expect it to be used higher level and not over a single call. Of course not a bug, but somewhat strange looking code.
The rest looks good. I think the Sashiko comment is a bit confused, as the code explicitly just flushes with a freeze/unfreeze, so extending the scope is a strange suggestion.
Christoph Hellwig
reviewerRe: [PATCH v5 4/9] block: add storage element management ioctls
In reply to Damien Le Moalblock/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2852
Quoted 3 lines · Damien Le Moal
block/blk-zoned.c +3
-0+static int blkdev_get_nr_storage_elements_ioctl (struct block_device *bdev, +void __user *argp) +{
The normal nameing would be more blkdev_ioctl_foo instead of the ioctl at the end.
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2855
Quoted 3 lines
block/blk-zoned.c +3
-0+struct gendisk *disk = bdev->bd_disk; + +if (put_user (disk->nr_storage_elements, (unsigned int __user *)argp))
Just type the argument as unsigned int and avoid the cast?
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2862
Quoted 2 lines
block/blk-zoned.c +2
-0+static int blkdev_report_storage_elements_ioctl (struct block_device *bdev, +void __user *argp)
Same naming and typing nitpick here.
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2869
Quoted 7 lines
block/blk-zoned.c +7
-0+{ +struct gendisk *disk = bdev->bd_disk; +struct blk_se_report *rep; +int ret; + +if (!argp) +return -EINVAL ;
copy_from_user will fault on a NULL pointer, so there should be no need for this.
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2883
Quoted 13 lines
block/blk-zoned.c +13
-0+ rep =kzalloc_flex (*rep, elements, disk->nr_storage_elements); +if (!rep) +return -ENOMEM ; + +if (copy_from_user (rep, argp,sizeof (struct blk_se_report ))) { + ret = -EFAULT ; + goto free_rep; + } + +if (rep->nr_elements < disk->nr_storage_elements) { + ret = -EINVAL ; + goto free_rep; + }
Just report the number of elements the user asked for in this case? That would also allow you to incorporate the nr storage elements query ioctl into this one by querying for 0 descriptors and just looking at:
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2885
Quoted 1 line
block/blk-zoned.c +1
-0+ rep->nr_reported_elements = disk->nr_storage_elements;
this to size the array in userspace.
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2892
Quoted 7 lines
block/blk-zoned.c +7
-0+ ret =bdev_report_storage_elements (bdev, rep->elements, + &rep->nr_reported_elements); +if (ret) + goto free_rep; + + if (copy_to_user(argp, rep, struct_size_t(struct blk_se_report, + elements, rep->nr_reported_elements)))
The indent looks a bit odd here. Also can't you use struct_size on rep, e.g.:
if (copy_to_user(argp, rep, struct_size(rep, elements, rep->nr_reported_elements)))
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2983
Quoted 13 lines
block/blk-zoned.c +13
-0+static int blkdev_flush_and_truncate (struct block_device *bdev, +blk_mode_t mode) +{ +int ret; + +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , +bdev_nr_bytes (bdev) -1 ); +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host);
Sashiko is obviously confused here about partitions that aren't supported on zoned devices. The conventional zones argument isn't entirely stupid, but also not a great one. Maybe we should add a check to just reject the ioctls if conventional zones are present, as that would make a mess of the depop anyway?
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2989
Quoted 2 lines
block/blk-zoned.c +2
-0+static int blkdev_remove_storage_element_ioctl (struct block_device *bdev, +blk_mode_t mode,void __user *argp)
The usual naming and type nit here again. I'm going to stop that now.
Re: [PATCH v5 7/9] scsi: sd: fail read commands to offline zones early
In reply to Damien Le MoalLooks good:
Re: [PATCH v5 8/9] scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
In reply to Damien Le MoalLooks generally good:
I think the Sashiko warning about the non-atomic bitfields is valid, but probably not sensible to address here. It would be great if someone could do a pass and convert the gazillions of scsi 1-bit bitfields to proper atomic bitops, though..
Re: [PATCH v5 9/9] scsi: sd_zbc: define storage element management operations
In reply to Damien Le MoalLooks good:
Re: [PATCH v5 6/9] zloop: add degrade_element control command
In reply to Damien Le Moaldrivers/block/zloop.c static int zloop_ctl_show(struct seq_file *seq_file, void *private) · line 2389
Quoted 7 lines · Damien Le Moal
drivers/block/zloop.c +3
-0@@ -2303,6 +2384,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)seq_puts (seq_file, zloop_ctl_ops[1 ].name);seq_puts (seq_file," id=%d\n" ); +/* Degrade element operation */ +seq_puts (seq_file, zloop_ctl_ops[2 ].name); +seq_puts (seq_file," id=%d,element_id=%d\n" );
This should be a %u I think.
Otherwise looks good:
Damien Le Moal
maintainerRe: [PATCH v5 4/9] block: add storage element management ioctls
In reply to Christoph HellwigQuoted 11 lines · Christoph Hellwig
Quoted 11 lines
block/blk-zoned.c +11
-0+static int blkdev_flush_and_truncate (struct block_device *bdev, +blk_mode_t mode) +{ +int ret; + +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , +bdev_nr_bytes (bdev) -1 );
Quoted 8 lines
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2983
Quoted 2 lines
block/blk-zoned.c +2
-0+filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host);Sashiko is obviously confused here about partitions that aren't supported on zoned devices. The conventional zones argument isn't entirely stupid, but also not a great one. Maybe we should add a check to just reject the ioctls if conventional zones are present, as that would make a mess of the depop anyway?
Not sure sure it makes sense. 3 things can happen:
1) The conv zone goes offline on remove
2) The conv zone goes read-only on remove
3) The conv zone content is totally unknown on restore since that is a destructive op.
So the "data loss" comment makes sense only in the case (2). All other cases lose the data anyway. As we make it clear that the user is responsible for sync & no io before issuing a remove ioctl, I am not sure if we really need to do anything special here. And we cannot really do anything special since we do not know which zones are going to be affected. So either we do nothing and let the user deal with potentially cached data not physically accessible anymore (not great), or like here invalidate everything.
Quoted 4 lines
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2989
Quoted 2 lines
block/blk-zoned.c +2
-0+static int blkdev_remove_storage_element_ioctl (struct block_device *bdev, +blk_mode_t mode,void __user *argp)The usual naming and type nit here again. I'm going to stop that now.
Signature · 2 lines
Damien Le Moal Western Digital Research
Christoph Hellwig
reviewerRe: [PATCH v5 4/9] block: add storage element management ioctls
Quoted 13 lines · Damien Le Moal
Quoted 6 lines
Sashiko is obviously confused here about partitions that aren't supported on zoned devices. The conventional zones argument isn't entirely stupid, but also not a great one. Maybe we should add a check to just reject the ioctls if conventional zones are present, as that would make a mess of the depop anyway?
Not sure sure it makes sense. 3 things can happen:
1) The conv zone goes offline on remove
2) The conv zone goes read-only on remove
3) The conv zone content is totally unknown on restore since that is a destructive op.
Quoted 7 lines
So the "data loss" comment makes sense only in the case (2). All other cases lose the data anyway. As we make it clear that the user is responsible for sync & no io before issuing a remove ioctl, I am not sure if we really need to do anything special here. And we cannot really do anything special since we do not know which zones are going to be affected. So either we do nothing and let the user deal with potentially cached data not physically accessible anymore (not great), or like here invalidate everything.
Maybe add a comment?
Damien Le Moal
maintainerRe: [PATCH v5 8/9] scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
Looks generally good:
Reviewed-by Christoph Hellwig <[email protected]>I think the Sashiko warning about the non-atomic bitfields is valid, but probably not sensible to address here. It would be great if someone could do a pass and convert the gazillions of scsi 1-bit bitfields to proper atomic bitops, though..
Yes, this is a recurring one. I need to send a patch to change all these 1-bit flags into proper flags handled with test/set/clear_bit.
Signature · 2 lines
Damien Le Moal Western Digital Research
Damien Le Moal
maintainerRe: [PATCH v5 4/9] block: add storage element management ioctls
In reply to Christoph HellwigQuoted 22 lines · Christoph Hellwig
Quoted 13 lines · Damien Le Moal
Quoted 6 lines
Sashiko is obviously confused here about partitions that aren't supported on zoned devices. The conventional zones argument isn't entirely stupid, but also not a great one. Maybe we should add a check to just reject the ioctls if conventional zones are present, as that would make a mess of the depop anyway?
Not sure sure it makes sense. 3 things can happen:
1) The conv zone goes offline on remove
2) The conv zone goes read-only on remove
3) The conv zone content is totally unknown on restore since that is a destructive op.Quoted 7 lines
So the "data loss" comment makes sense only in the case (2). All other cases lose the data anyway. As we make it clear that the user is responsible for sync & no io before issuing a remove ioctl, I am not sure if we really need to do anything special here. And we cannot really do anything special since we do not know which zones are going to be affected. So either we do nothing and let the user deal with potentially cached data not physically accessible anymore (not great), or like here invalidate everything.
Quoted 1 line
Maybe add a comment?
Will do.
Signature · 2 lines
Damien Le Moal Western Digital Research