Add support for storage element depopulation
Subsystems (7)
Damien Le Moal
maintainerv2 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 v1:
- Addressed the many valid comments that Sashiko add
(https://sashiko.dev/#/patchset/20261005094632.580753-1-dlemoal%40kernel.org)
- Rebased on latest block/for-next
Damien Le Moal (7):
block: fail reads to offline zones early
block: introduce storage element management
block: add storage element management ioctls
zloop: add storage element emulation
zloop: add degrade_element control command
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-core.c | 3 + block/blk-zoned.c | 343 ++++++++- block/blk.h | 15 + block/ioctl.c | 5 + drivers/block/zloop.c | 670 ++++++++++++++++-- drivers/scsi/sd.c | 3 + drivers/scsi/sd.h | 3 + drivers/scsi/sd_zbc.c | 259 ++++++- include/linux/blkdev.h | 16 + include/uapi/linux/blkzoned.h | 81 +++ include/uapi/linux/fs.h | 1 + 12 files changed, 1358 insertions(+), 49 deletions(-)
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v2 1/7 block: fail reads to offline zones early
Any BIO targeting an offline zone of a zoned block device will fail, including read commands. So there is no point in issuing read BIOs. Fail these read operations early if we ever see one.
--- block/blk-core.c | 3 +++ block/blk-zoned.c | 18 ++++++++++++++++++ block/blk.h | 6 ++++++ 3 files changed, 27 insertions(+)
block/blk-core.c +3 -0
@@ -866,6 +866,9 @@ void submit_bio_noacct(struct bio *bio)switch (bio_op (bio)) {case REQ_OP_READ : +if (bdev_is_zoned (bdev) && +bdev_zone_is_offline (bdev, bio->bi_iter.bi_sector)) + goto end_io;break ;case REQ_OP_WRITE :if (bio->bi_opf &REQ_ATOMIC ) {
block/blk-zoned.c +18 -0
@@ -308,6 +308,24 @@ bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector) }EXPORT_SYMBOL_GPL (bdev_zone_is_seq); +/** +* bdev_zone_is_offline - check if a sector belongs to an offline zone +* @bdev: block device to check +* @sector: sector number +* +* Check if @sector on @bdev is contained in an offline zone. +*/ +bool bdev_zone_is_offline (struct block_device *bdev,sector_t sector) +{ +enum blk_zone_cond cond; + +if (!bdev_is_zoned (bdev)) +return false; + + cond =disk_zone_get_cond (bdev->bd_disk, sector); +return cond ==BLK_ZONE_COND_OFFLINE ; +} +/** * bdev_zone_mgmt_allowed - check if management operations are allowed on a zone * @bdev: block device to check
block/blk.h +6 -0
@@ -578,6 +578,7 @@ 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); +bool bdev_zone_is_offline (struct block_device *bdev,sector_t sector);#else /* CONFIG_BLK_DEV_ZONED */ static inline void disk_init_zone_resources (struct gendisk *disk) {
@@ -625,6 +626,11 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev, {return false; } +static inline bool bdev_zone_is_offline (struct block_device *bdev, +sector_t sector) +{ +return false; +}#endif /* CONFIG_BLK_DEV_ZONED */ struct block_device *bdev_alloc (struct gendisk *disk,u8 partno);
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v2 2/7 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_storage_elements_ops and includes three operations:
- report_elements: get information on a device storage elements state
- 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 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 | 160 +++++++++++++++++++++++++++++++++- include/linux/blkdev.h | 16 ++++ include/uapi/linux/blkzoned.h | 67 ++++++++++++++ 3 files changed, 242 insertions(+), 1 deletion(-)
block/blk-zoned.c +159 -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>
@@ -2718,5 +2719,162 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)return 0 ; } -#endif + +static int disk_wait_for_se_mgmt_completion (struct gendisk *disk) +{ +struct blk_storage_element *elements, *e; +unsigned int i, nr_se, nr_elements =0 ; +int ret; + + ret = disk->fops->se_ops->report_elements (disk,NULL , &nr_elements); +if (ret) { +pr_err ("Failed to get number of storage elements\n" ); +return ret; + } + + elements = kzalloc_objs(struct blk_storage_element, nr_elements); +if (!elements) +return -ENOMEM ; + +while (1 ) { +/* +* Check if we have storage elements being removed or restored. +*/ + nr_se = nr_elements; + ret = disk->fops->se_ops->report_elements (disk, elements, + &nr_se); +if (ret) { +pr_err ("Failed to get storage elements\n" ); +break ; + } + + e = elements; +for (i =0 ; i < nr_se; i++, e++) { +if (e->status ==BLK_SE_STS_REMOVE_IN_PROGRESS || + e->status ==BLK_SE_STS_RESTORE_IN_PROGRESS ) +break ; + } +if (i >= nr_se) +break ; + +/* Not done yet: wait and retry. */ +msleep (500 ); + } + +kfree (elements); + +return ret; +} + +/** +* bdev_report_storage_elements - report the storage elements of a block device +* +* 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. +* +* Returns 0 on success and a negative error code on failure. +*/ +int bdev_report_storage_elements (struct block_device *bdev, +struct blk_storage_element *elements, +unsigned int *nr_elements) +{ +struct gendisk *disk = bdev->bd_disk; + +if (!bdev_is_zoned (bdev) || !disk->fops->se_ops) +return -EOPNOTSUPP ; + +if (!nr_elements) +return -EINVAL ; + +if (*nr_elements && !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 +* block device +* +* 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; +int ret; + +if (!bdev_is_zoned (bdev) || !disk->fops->se_ops) +return -EOPNOTSUPP ; + +/* Zero is not a valid storage element ID. */ +if (!element_id) +return -EINVAL ; + +/* +* 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); + + ret = disk->fops->se_ops->remove_element (disk, element_id); +if (ret) +return ret; + +/* Revalidate the device zones once the opration completes. */ + ret =disk_wait_for_se_mgmt_completion (disk); +if (ret) +return ret; + +return blk_revalidate_disk_zones (disk); +} +EXPORT_SYMBOL_GPL (bdev_remove_storage_element); + +/** +* bdev_restore_storage_elements - Restore all depopulated storage elements of a +* block device +* +* 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; +int ret; + +if (!bdev_is_zoned (bdev) || !disk->fops->se_ops) +return -EOPNOTSUPP ; + +/* +* 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); + + ret = disk->fops->se_ops->restore_elements (disk); +if (ret) +return ret; + + ret =disk_wait_for_se_mgmt_completion (disk); +if (ret) +return ret; + +return blk_revalidate_disk_zones (disk); +} +EXPORT_SYMBOL_GPL (bdev_restore_storage_elements);
include/linux/blkdev.h +16 -0
@@ -1574,6 +1574,21 @@ enum blk_unique_id { BLK_UID_NAA = 3, }; +struct blk_storage_elements_ops { +int (*report_elements)(struct gendisk *disk, +struct blk_storage_element *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_storage_element *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,
@@ -1601,6 +1616,7 @@ struct block_device_operations { enum blk_unique_id id_type);struct module *owner;const struct pr_ops *pr_ops; +const struct blk_storage_elements_ops *se_ops;/* * Special callback for probing GPT entry at a given sector.
include/uapi/linux/blkzoned.h +67 -0
71 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 2/7] block: introduce storage element management
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] block: memory allocation deadlock in disk_wait_for_se_mgmt_completion() - [High] block: NULL…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] block: memory allocation deadlock in disk_wait_for_se_mgmt_completion()
- [High] block: NULL pointer dereference on missing se_ops callbacks
- [High] block: ignored asynchronous hardware error statuses in disk_wait_for_se_mgmt_completion()
- [High] block: unkillable task hang due to unbounded hardware polling loop
- [High] block: TOCTOU data race from unfrozen queue during blk_revalidate_disk_zones()
- [Low] block: UAPI copy-paste error in blk_storage_element_type docstring
--
commit eb5c21d5182da32571bd17948ff3d25e0ac2e46c
Author: Damien Le Moal <[email protected]>
block: introduce storage element management
Recent SCSI (SBC) and ATA (ACS) standards define the storage element depopulation feature, which allows keeping disks with defective heads longer in production by removing them. This introduces generic operations and functions to block device drivers for managing storage elements.
Quoted 4 lines
block/blk-zoned.c +0
-0
[ ... ]
Quoted 4 lines
block/blk-zoned.c +0
-0@@ -2718,5 +2719,162 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)return 0 ; }
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2730
Quoted 10 lines
block/blk-zoned.c +8
-1-#endif + +static int disk_wait_for_se_mgmt_completion (struct gendisk *disk) +{ +struct blk_storage_element *elements, *e; +unsigned int i, nr_se, nr_elements =0 ; +int ret; + + ret = disk->fops->se_ops->report_elements (disk,NULL , &nr_elements);
[Severity: High]
Should there be a NULL check for the specific se_ops function pointers (report_elements, remove_element, and restore_elements) before invoking them to prevent a kernel panic?
The commit message notes that some operations like repopulating defective elements are generally not allowed by devices, which suggests drivers might legitimately leave restore_elements or other operations as NULL.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2736
Quoted 6 lines
block/blk-zoned.c +6
-0+if (ret) { +pr_err ("Failed to get number of storage elements\n" ); +return ret; + } + + elements = kzalloc_objs(struct blk_storage_element, nr_elements);
[Severity: High]
Should this allocation be protected by memalloc_noio_save()?
This function is called by storage element management APIs like bdev_remove_storage_element() where I/O might be paused or frozen. An allocation using the default GFP_KERNEL could enter direct reclaim.
If the reclaim attempts to write back dirty pages to the frozen filesystem, could this cause an indefinite block and deadlock?
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2740
Quoted 4 lines
block/blk-zoned.c +4
-0+if (!elements) +return -ENOMEM ; + +while (1 ) {
[Severity: High]
Does this unbounded loop risk an unkillable task hang?
If a device malfunctions, firmware stalls, or the completion event drops, the task will block indefinitely in the D state because msleep() below uses TASK_UNINTERRUPTIBLE.
There are no bounds, timeout limits, or checks for fatal_signal_pending().
Quoted 9 lines
block/blk-zoned.c +9
-0+/* +* Check if we have storage elements being removed or restored. +*/ + nr_se = nr_elements; + ret = disk->fops->se_ops->report_elements (disk, elements, + &nr_se); +if (ret) { +pr_err ("Failed to get storage elements\n" ); +break ;
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2759
Quoted 10 lines
block/blk-zoned.c +10
-0+ } + + e = elements; +for (i =0 ; i < nr_se; i++, e++) { +if (e->status ==BLK_SE_STS_REMOVE_IN_PROGRESS || + e->status ==BLK_SE_STS_RESTORE_IN_PROGRESS ) +break ; + } +if (i >= nr_se) +break ;
[Severity: High]
This polling loop treats ERROR states like BLK_SE_STS_REMOVE_ERROR and BLK_SE_STS_RESTORE_ERROR as not in progress, causing the loop to break normally and falsely return success (0).
Could this mislead a caller into assuming offlined zones are safe to format or ignore, potentially leading to data corruption?
Quoted 9 lines
block/blk-zoned.c +9
-0+ +/* Not done yet: wait and retry. */ +msleep (500 ); + } + +kfree (elements); + +return ret; +}
[ ... ]
Quoted 13 lines
block/blk-zoned.c +13
-0+int bdev_remove_storage_element (struct block_device *bdev, +unsigned int element_id) +{ +struct gendisk *disk = bdev->bd_disk; +unsigned int memflags; +int ret; + +if (!bdev_is_zoned (bdev) || !disk->fops->se_ops) +return -EOPNOTSUPP ; + +/* Zero is not a valid storage element ID. */ +if (!element_id) +return -EINVAL ;
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2831
Quoted 10 lines
block/blk-zoned.c +10
-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); + + ret = disk->fops->se_ops->remove_element (disk, element_id);
[Severity: High]
By deliberately unfreezing the queue before the hardware removal command and the subsequent call to blk_revalidate_disk_zones(), does this introduce a TOCTOU race?
The queue remains unfrozen during the first half of blk_revalidate_disk_zones(), which could allow concurrent write I/Os to bypass validation and allocate new blk_zone_wplug entries based on stale state.
When the queue is finally frozen to install the new zone array, these freshly created stale write plugs aren't purged, and bios might proceed to write to the offlined device zones.
Quoted 10 lines
block/blk-zoned.c +10
-0+if (ret) +return ret; + +/* Revalidate the device zones once the opration completes. */ + ret =disk_wait_for_se_mgmt_completion (disk); +if (ret) +return ret; + +return blk_revalidate_disk_zones (disk); +}
[ ... ]
Quoted 4 lines
include/uapi/linux/blkzoned.h +0
-0
[ ... ]
include/uapi/linux/blkzoned.h struct blk_zone_range { · line 219
Quoted 9 lines
include/uapi/linux/blkzoned.h +9
-0+/** +* enum blk_storage_element_status - Status of a zoned device storage elements. +* +* @BLK_SE_TYPE_RDWR: The storage element handles both reads and writes. +* @BLK_SE_TYPE_READ: The storage element handles reads only. +* @BLK_SE_TYPE_WRITE: The storage element handles writes only. +* @BLK_SE_TYPE_UNKNOWN: The storage element type is not known. +*/ +enum blk_storage_element_type {
[Severity: Low]
this isn't a bug, but the docstring incorrectly identifies the enum as enum blk_storage_element_status.
Could this copy-paste error be fixed to enum blk_storage_element_type to avoid confusion in userspace?
Quoted 2 lines
include/uapi/linux/blkzoned.h +2
-0+BLK_SE_TYPE_RDWR =0x01 , + BLK_SE_TYPE_READ = 0x02,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Damien Le Moal
maintainerPATCH v2 3/7 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 | 165 ++++++++++++++++++++++++++++++++++ block/blk.h | 9 ++ block/ioctl.c | 5 ++ include/uapi/linux/blkzoned.h | 14 +++ include/uapi/linux/fs.h | 1 + 5 files changed, 194 insertions(+)
block/blk-zoned.c +165 -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>
@@ -2795,6 +2796,73 @@ 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) +{ +unsigned int nr_elements =0 ; +int ret; + + ret =bdev_report_storage_elements (bdev,NULL , &nr_elements); +if (ret) +return ret; + +if (put_user (nr_elements, (unsigned int __user *)argp)) +return -EFAULT ; + +return 0 ; +} + +static int blkdev_report_storage_elements_ioctl (struct block_device *bdev, +void __user *argp) +{ +struct blk_storage_elements_report rep; +struct blk_storage_element *elements; +unsigned int nr_elements =0 ; +unsigned long retc; +int ret; + +if (!argp) +return -EINVAL ; + +if (copy_from_user (&rep, argp, +sizeof (struct blk_storage_elements_report ))) +return -EFAULT ; + + ret =bdev_report_storage_elements (bdev,NULL , &nr_elements); +if (ret) +return ret; + + nr_elements =min (rep.nr_elements, nr_elements); +if (!nr_elements) +return -EINVAL ; + + elements = kzalloc_objs(struct blk_storage_element, nr_elements); +if (!elements) +return -ENOMEM ; + + ret =bdev_report_storage_elements (bdev, elements, &nr_elements); +if (ret) + goto free_elements; + + retc =copy_to_user (argp +sizeof (struct blk_storage_elements_report ), + elements, +sizeof (struct blk_storage_element ) * nr_elements); +if (retc) { + ret = -EFAULT ; + goto free_elements; + } + + rep.nr_elements = nr_elements; + retc =copy_to_user (argp, &rep, +sizeof (struct blk_storage_elements_report )); +if (retc) + ret = -EFAULT ; + +free_elements: +kfree (elements); +return ret; +} +/** * bdev_remove_storage_element - Remove (depopulate) a storage element of a * block device
@@ -2841,6 +2909,45 @@ int bdev_remove_storage_element(struct block_device *bdev, }EXPORT_SYMBOL_GPL (bdev_remove_storage_element); +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. +*/ +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (ret) + goto out_unlock; + + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 ); +if (ret) + goto out_unlock; + + ret =bdev_remove_storage_element (bdev, element_id); + +out_unlock: +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +} +/** * bdev_restore_storage_elements - Restore all depopulated storage elements of a * block device
@@ -2878,3 +2985,61 @@ int bdev_restore_storage_elements(struct block_device *bdev)return blk_revalidate_disk_zones (disk); }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 ; + +/* +* Flush the device volatile write cache and invalidate all cached data +* so that reads do not return old data for zones that went offline. +*/ +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (ret) + goto out_unlock; + + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 ); +if (ret) + goto out_unlock; + + ret =bdev_restore_storage_elements (bdev); + +out_unlock: +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +} + +int blkdev_zone_storage_elements_ioctl (struct block_device *bdev, +blk_mode_t mode,unsigned int cmd, +void __user *argp) +{ +if (!bdev_is_zoned (bdev) || !bdev->bd_disk->fops->se_ops) +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_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);bool bdev_zone_is_offline (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 void disk_init_zone_resources (struct gendisk *disk) {
@@ -631,6 +634,12 @@ static inline bool bdev_zone_is_offline(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 +14 -0
@@ -275,4 +275,18 @@ struct blk_storage_elements_report { struct blk_storage_element 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_storage_elements_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
[email protected]
AI reviewRe: [PATCH v2 3/7] block: add storage element management ioctls
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] block: silent data loss via full truncation without writeback - [High] block: unkillable task…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] block: silent data loss via full truncation without writeback
- [High] block: unkillable task and VFS deadlock in disk_wait_for_se_mgmt_completion() --
commit 9851d611abe8fdc4d981018f157a08dd381fede2
Author: Damien Le Moal <[email protected]>
block: add storage element management ioctls
This commit introduces four new ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, and BLKRESTORESTORELEMS) for managing storage elements on zoned block devices. It allows querying, removing, and restoring degraded storage elements from userspace.
Quoted 4 lines
block/blk-zoned.c +0
-0
[ ... ]
Quoted 17 lines
block/blk-zoned.c +17
-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 ; + +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.
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2938
Quoted 10 lines
block/blk-zoned.c +10
-0+*/ +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (ret) + goto out_unlock; + + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 );
[Severity: Critical]
Does this truncate the full block device page cache without prior writeback?
The code issues a hardware cache flush via blkdev_issue_flush() but appears to completely skip writing back dirty pages in the OS page cache (e.g., via sync_blockdev()). The subsequent truncate_bdev_range() call unconditionally drops all page cache entries, which could discard dirty data even for zones that are completely unaffected by the storage element operation.
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2942
Quoted 4 lines
block/blk-zoned.c +4
-0+if (ret) + goto out_unlock; + + ret =bdev_remove_storage_element (bdev, element_id);
[Severity: High]
Could this lead to a VFS deadlock and an unkillable task if the device hangs?
VFS locks (inode_lock and filemap_invalidate_lock) are acquired earlier in this function before calling bdev_remove_storage_element(). Inside that function, it delegates to the driver and waits for completion. Looking at disk_wait_for_se_mgmt_completion(), it uses an infinite loop:
while (1) {
...
/* Not done yet: wait and retry. */
msleep(500);
}If the device takes an extended time to complete or stops responding, the polling task can become unkillable because it lacks a timeout, max retry bound, or fatal_signal_pending() check. Because the ioctl caller holds the block device's i_rwsem and invalidate_lock during this wait, a hung device could permanently lock out all other processes attempting to access the block device.
Quoted 7 lines
block/blk-zoned.c +7
-0+ +out_unlock: +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +}
[ ... ]
Quoted 13 lines
block/blk-zoned.c +13
-0+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 ; + +/* +* Flush the device volatile write cache and invalidate all cached data +* so that reads do not return old data for zones that went offline.
block/blk-zoned.c int bdev_restore_storage_elements(struct block_device *bdev) · line 3011
Quoted 10 lines
block/blk-zoned.c +10
-0+*/ +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping); + + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (ret) + goto out_unlock; + + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 );
[Severity: Critical]
Does this also drop dirty pages without prior writeback?
Similar to blkdev_remove_storage_element_ioctl(), this appears to truncate the entire device's page cache unconditionally after issuing a hardware flush, which skips writing back dirty pages in the OS page cache first.
block/blk-zoned.c int bdev_restore_storage_elements(struct block_device *bdev) · line 3015
Quoted 4 lines
block/blk-zoned.c +4
-0+if (ret) + goto out_unlock; + + ret =bdev_restore_storage_elements (bdev);
[Severity: High]
Does this code also suffer from the same VFS deadlock risk?
Similar to the issue above, bdev_restore_storage_elements() invokes disk_wait_for_se_mgmt_completion() while holding both inode_lock and filemap_invalidate_lock, which could permanently stall other processes accessing the block device if the device becomes unresponsive.
Quoted 7 lines
block/blk-zoned.c +7
-0+ +out_unlock: +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); + +return ret; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Damien Le Moal
maintainerPATCH v2 4/7 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 | 567 ++++++++++++++++-- 2 files changed, 530 insertions(+), 45 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 +523 -44
804 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 4/7] zloop: add storage element emulation
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] block/zloop: restore_elements strays elements in RESTORE_ERROR state - [Medium] block/zloop…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] block/zloop: restore_elements strays elements in RESTORE_ERROR state
- [Medium] block/zloop: zloop_report_zones overwrites OFFLINE/READONLY zone conditions --
commit 589ee9798dfa33f2ae7ed9bd47dc9d1e880a131f
Author: Damien Le Moal <[email protected]>
Subject: zloop: add storage element emulation
This commit introduces storage element emulation to the zloop driver, providing ways to emulate SMR disk depopulation features. It defines new options and states to handle fractional access storage elements and their associated zone conditions.
Quoted 4 lines
drivers/block/zloop.c +0
-0
[ ... ]
Quoted 21 lines
drivers/block/zloop.c +13
-0@@ -353,6 +389,19 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)return 0 ; } +static bool zloop_zone_is_offline_or_readonly (struct zloop_device *zlo, +struct zloop_zone *zone) +{ +bool ret; + +spin_lock (&zone->wp_lock); + ret = zone->cond ==BLK_ZONE_COND_OFFLINE || + zone->cond ==BLK_ZONE_COND_READONLY ; +spin_unlock (&zone->wp_lock); + +return ret; +} +static int zloop_open_zone (struct zloop_device *zlo,unsigned int zone_no) {struct zloop_zone *zone = &zlo->zones[zone_no];
drivers/block/zloop.c static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no) · line 421
Quoted 10 lines
drivers/block/zloop.c +5
-0@@ -363,6 +412,11 @@ static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)mutex_lock (&zone->lock); +if (zloop_zone_is_offline_or_readonly (zlo, zone)) { + ret = -EIO ; + goto unlock; + } +if (test_and_clear_bit (ZLOOP_ZONE_SEQ_ERROR , &zone->flags)) { ret =zloop_update_seq_zone (zlo, zone_no);
[Severity: Medium]
Should we also be checking zloop_zone_is_offline_or_readonly() in zloop_report_zones()?
Currently, if a user depopulates a storage element, the zone transitions to BLK_ZONE_COND_OFFLINE or BLK_ZONE_COND_READONLY. But if a report zones command is executed, zloop_report_zones() checks for the ZLOOP_ZONE_SEQ_ERROR bit and invokes zloop_update_seq_zone():
drivers/block/zloop.c:zloop_report_zones() {
...
if (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags)) {
ret = zloop_update_seq_zone(zlo, zone_no);...
}
Since zloop_report_zones() does not check if the zone is offline or readonly first, zloop_update_seq_zone() could unconditionally overwrite zone->cond to EMPTY, FULL, or CLOSED. Could this incorrectly revive the zone and erase the status imposed by the storage element depopulation?
[ ... ]
Quoted 1 line
drivers/block/zloop.c +0
-0@@ -1002,11 +1105,252 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
[ ... ]
Quoted 2 lines
drivers/block/zloop.c +2
-0+static void zloop_restore_elements_work (struct work_struct *work) +{
[ ... ]
Quoted 5 lines
drivers/block/zloop.c +5
-0+/* Restore all removed elements. */ +for (i =0 , se = zlo->elements; i < zlo->nr_elements; i++, se++) { +if (se->status !=BLK_SE_STS_RESTORE_IN_PROGRESS ) +continue ; +if (!ret)
drivers/block/zloop.c static int zloop_report_zones(struct gendisk *disk, sector_t sector, · line 1303
Quoted 10 lines
drivers/block/zloop.c +10
-0+ se->status =BLK_SE_STS_OK ; + else + se->status = BLK_SE_STS_RESTORE_ERROR; + } + + zlo->nr_removed_elements =0 ; + zlo->restore_in_progress = false; + +mutex_unlock (&zlo->stor_elements_lock); +}
[Severity: Medium]
Is it safe to unconditionally clear zlo->nr_removed_elements here when elements might remain in the BLK_SE_STS_RESTORE_ERROR state?
If a zone reset fails (e.g. zloop_do_reset_zone() returns an error), the affected elements transition to BLK_SE_STS_RESTORE_ERROR. However, the code still sets zlo->nr_removed_elements = 0 despite these elements remaining functionally disabled.
This breaks the accounting for the hardware limit check in zloop_remove_element():
drivers/block/zloop.c:zloop_remove_element() {
...
if (zlo->nr_removed_elements + nr_remove >
zlo->max_nr_removed_elements) {
ret = -EBUSY;
goto unlock;
}...
}
This could allow a user to evade the zlo->max_nr_removed_elements limit by deliberately failing restorations, potentially offlining all storage elements.
Additionally, because nr_removed_elements becomes 0, elements in the RESTORE_ERROR state might become stranded. In zloop_restore_elements():
Quoted 2 lines
drivers/block/zloop.c +2
-0+static int zloop_restore_elements (struct gendisk *disk) +{
[ ... ]
Quoted 8 lines
drivers/block/zloop.c +8
-0+ zlo->restore_in_progress = true; +for (i =0 , se = zlo->elements; i < zlo->nr_elements; i++, se++) { +if (se->status ==BLK_SE_STS_REMOVED ) + se->status =BLK_SE_STS_RESTORE_IN_PROGRESS ; + } + +schedule_delayed_work (&zlo->restore_elements_work, +msecs_to_jiffies (5000 ));
[ ... ]
Quoted 1 line
+}
Since zloop_restore_elements() strictly checks for BLK_SE_STS_REMOVED, it will ignore elements in the BLK_SE_STS_RESTORE_ERROR state. Does this mean those elements can never be restored again, stranding them in a permanently broken state?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Damien Le Moal
maintainerPATCH v2 5/7 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 | 131 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 115 insertions(+), 16 deletions(-)
drivers/block/zloop.c +115 -16
@@ -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; };/*
@@ -982,11 +986,26 @@ static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)spin_lock (&zone->wp_lock);if (zlo->nr_elements) { +struct blk_storage_element *se; +if (zone->cond ==BLK_ZONE_COND_OFFLINE || (zone->cond ==BLK_ZONE_COND_READONLY && is_write)) { ret = false; goto unlock; } + +/* +* Check the health state of the storage element serving the +* zone. +*/ +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_DEGRADED ) { + ret = false; + goto unlock; + } }/*
@@ -1123,7 +1142,7 @@ static int zloop_report_elements(struct gendisk *disk,unsigned int i;for (i =0 ; i <min (nr_report, zlo->nr_elements); i++, se++) { -switch (se->status) { +switch (READ_ONCE (se->status)) {case BLK_SE_STS_REMOVED :case BLK_SE_STS_RESTORE_ERROR : se->restore_allowed =1 ;
@@ -1141,6 +1160,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_storage_element *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,
@@ -1186,11 +1230,11 @@ static void zloop_remove_element_work(struct work_struct *work)mutex_unlock (&zone->lock); } - se->status =BLK_SE_STS_REMOVED ; +WRITE_ONCE (se->status,BLK_SE_STS_REMOVED ); zlo->nr_removed_elements++; -if (paired_se && paired_se->status !=BLK_SE_STS_REMOVED ) { - paired_se->status =BLK_SE_STS_REMOVED ; +if (paired_se &&READ_ONCE (paired_se->status) !=BLK_SE_STS_REMOVED ) { +WRITE_ONCE (paired_se->status,BLK_SE_STS_REMOVED ); zlo->nr_removed_elements++; }
@@ -1224,7 +1268,7 @@ static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)* to do. */ se = &zlo->elements[element_id -1 ]; -if (se->status ==BLK_SE_STS_REMOVED ) +if (READ_ONCE (se->status) ==BLK_SE_STS_REMOVED ) goto unlock;/*
@@ -1233,7 +1277,7 @@ static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)*/ if (se->type ==BLK_SE_TYPE_READ ) { paired_se = &zlo->elements[se->paired_id -1 ]; -if (paired_se->status !=BLK_SE_STS_REMOVED ) +if (READ_ONCE (paired_se->status) !=BLK_SE_STS_REMOVED ) nr_remove =2 ; }
@@ -1249,9 +1293,9 @@ static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)* modify the zones. */ zlo->remove_element_id = element_id; - se->status =BLK_SE_STS_REMOVE_IN_PROGRESS ; -if (paired_se && paired_se->status !=BLK_SE_STS_REMOVED ) - paired_se->status =BLK_SE_STS_REMOVE_IN_PROGRESS ; +WRITE_ONCE (se->status,BLK_SE_STS_REMOVE_IN_PROGRESS ); +if (paired_se &&READ_ONCE (paired_se->status) !=BLK_SE_STS_REMOVED ) +WRITE_ONCE (paired_se->status,BLK_SE_STS_REMOVE_IN_PROGRESS );schedule_delayed_work (&zlo->remove_element_work,msecs_to_jiffies (2000 ));
@@ -1288,12 +1332,12 @@ static void zloop_restore_elements_work(struct work_struct *work)/* Restore all removed elements. */ for (i =0 , se = zlo->elements; i < zlo->nr_elements; i++, se++) { -if (se->status !=BLK_SE_STS_RESTORE_IN_PROGRESS ) +if (READ_ONCE (se->status) !=BLK_SE_STS_RESTORE_IN_PROGRESS )continue ;if (!ret) - se->status =BLK_SE_STS_OK ; +WRITE_ONCE (se->status,BLK_SE_STS_OK );else - se->status =BLK_SE_STS_RESTORE_ERROR ; +WRITE_ONCE (se->status,BLK_SE_STS_RESTORE_ERROR ); } zlo->nr_removed_elements =0 ;
@@ -1330,8 +1374,8 @@ static int zloop_restore_elements(struct gendisk *disk)*/ zlo->restore_in_progress = true;for (i =0 , se = zlo->elements; i < zlo->nr_elements; i++, se++) { -if (se->status ==BLK_SE_STS_REMOVED ) - se->status =BLK_SE_STS_RESTORE_IN_PROGRESS ; +if (READ_ONCE (se->status) ==BLK_SE_STS_REMOVED ) +WRITE_ONCE (se->status,BLK_SE_STS_RESTORE_IN_PROGRESS ); } schedule_delayed_work(&zlo->restore_elements_work,
@@ -1944,6 +1988,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->id, opts->element_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 ];
@@ -1964,6 +2044,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 ;
@@ -2115,6 +2196,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);
@@ -2143,14 +2231,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 }, };
@@ -2198,6 +2288,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 ;
@@ -2221,6 +2314,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);
@@ -2231,6 +2326,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
[email protected]
AI reviewRe: [PATCH v2 5/7] zloop: add degrade_element control command
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] block/zloop: removing degraded element re-enables I/O during transition - [Low] block/zloop…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] block/zloop: removing degraded element re-enables I/O during transition
- [Low] block/zloop: format string arguments swapped in degrade_element
- [Low] block/zloop: missing validation for ZLOOP_OPT_ELEMENT_ID
--
commit a9552af0fb1bf978fc86a4c32f3c9541477e5165
Author: Damien Le Moal <[email protected]>
Subject: zloop: add degrade_element control command
This commit introduces a new degrade_element control command for zloop devices to simulate degraded storage elements. Read and write operations to zones served by a degraded element are failed based on element type. The patch also updates storage element status accesses to use READ_ONCE() and WRITE_ONCE() for safe lockless checks in the I/O path.
Quoted 4 lines
drivers/block/zloop.c +0
-0
[ ... ]
Quoted 25 lines
drivers/block/zloop.c +15
-0@@ -982,11 +986,26 @@ static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)spin_lock (&zone->wp_lock);if (zlo->nr_elements) { +struct blk_storage_element *se; +if (zone->cond ==BLK_ZONE_COND_OFFLINE || (zone->cond ==BLK_ZONE_COND_READONLY && is_write)) { ret = false; goto unlock; } + +/* +* Check the health state of the storage element serving the +* zone. +*/ +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_DEGRADED ) { + ret = false; + goto unlock; + } }
[ ... ]
drivers/block/zloop.c static int zloop_remove_element(struct gendisk *disk, unsigned int element_id) · line 1298
Quoted 10 lines
drivers/block/zloop.c +3
-3@@ -1249,9 +1293,9 @@ static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)* modify the zones. */ zlo->remove_element_id = element_id; - se->status =BLK_SE_STS_REMOVE_IN_PROGRESS ; -if (paired_se && paired_se->status !=BLK_SE_STS_REMOVED ) - paired_se->status =BLK_SE_STS_REMOVE_IN_PROGRESS ; +WRITE_ONCE (se->status,BLK_SE_STS_REMOVE_IN_PROGRESS ); +if (paired_se &&READ_ONCE (paired_se->status) !=BLK_SE_STS_REMOVED ) +WRITE_ONCE (paired_se->status,BLK_SE_STS_REMOVE_IN_PROGRESS );
[Severity: Medium]
When zloop_remove_element() is called on an element that was previously marked as degraded, won't this unconditionally overwrite the status to BLK_SE_STS_REMOVE_IN_PROGRESS?
If the status changes to BLK_SE_STS_REMOVE_IN_PROGRESS, the check in zloop_prep_rq() for BLK_SE_STS_DEGRADED will no longer match. Does this mean that I/O will unexpectedly succeed on the degraded element until the 2-second delayed removal work actually offlines the zones?
[ ... ]
Quoted 8 lines
drivers/block/zloop.c +4
-0@@ -1944,6 +1988,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 ;
drivers/block/zloop.c static int zloop_ctl_remove(struct zloop_options *opts) · line 2004
Quoted 10 lines
drivers/block/zloop.c +10
-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 ; + }
[Severity: Low]
Does this code silently fail if the user omits the element_id option?
The check above ensures no invalid options are provided, but it lacks a validation to ensure ZLOOP_OPT_ELEMENT_ID is actually present in opts->mask. If omitted, opts->element_id defaults to 0, which gets rejected silently inside zloop_degrade_element() without any error message to the user.
Quoted 5 lines
drivers/block/zloop.c +5
-0+ +mutex_lock (&zloop_ctl_mutex); + + zlo =idr_find (&zloop_index_idr, opts->id); +if (!zlo || zlo->state == Zlo_creating)
drivers/block/zloop.c static int zloop_ctl_remove(struct zloop_options *opts) · line 2019
Quoted 10 lines
drivers/block/zloop.c +10
-0+ 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->id, opts->element_id);
[Severity: Low]
Are the arguments to pr_info() swapped here?
The format string specifies "element %u of device %u", but the variables are passed as opts->id (the device ID) followed by opts->element_id (the element ID). This will display the values in the reverse order of the text.
Quoted 6 lines
drivers/block/zloop.c +6
-0+ +unlock: +mutex_unlock (&zloop_ctl_mutex); + +return ret; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Damien Le Moal
maintainerRe: [PATCH v2 5/7] zloop: add degrade_element control command
Quoted 67 lines · On 2026/10/06 14:56, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] block/zloop: removing degraded element re-enables I/O during transition
- [Low] block/zloop: format string arguments swapped indegrade_element
- [Low] block/zloop: missing validation forZLOOP_OPT_ELEMENT_ID
--commit
a9552af0fb1bf978fc86a4c32f3c9541477e5165
Author: Damien Le Moal <[email protected]>
Subject: zloop: adddegrade_elementcontrol commandThis commit introduces a new
degrade_elementcontrol command for zloop devices to simulate degraded storage elements. Read and write operations to zones served by a degraded element are failed based on element type. The patch also updates storage element status accesses to useREAD_ONCE()andWRITE_ONCE()for safe lockless checks in the I/O path.Quoted 4 lines
drivers/block/zloop.c +0
-0[ ... ]
Quoted 25 lines
drivers/block/zloop.c +15
-0@@ -982,11 +986,26 @@ static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)spin_lock (&zone->wp_lock);if (zlo->nr_elements) { +struct blk_storage_element *se; +if (zone->cond ==BLK_ZONE_COND_OFFLINE || (zone->cond ==BLK_ZONE_COND_READONLY && is_write)) { ret = false; goto unlock; } + +/* +* Check the health state of the storage element serving the +* zone. +*/ +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_DEGRADED ) { + ret = false; + goto unlock; + } }[ ... ]
drivers/block/zloop.c static int zloop_remove_element(struct gendisk *disk, unsigned int element_id) · line 1298
Quoted 10 lines
drivers/block/zloop.c +3
-3@@ -1249,9 +1293,9 @@ static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)* modify the zones. */ zlo->remove_element_id = element_id; - se->status =BLK_SE_STS_REMOVE_IN_PROGRESS ; -if (paired_se && paired_se->status !=BLK_SE_STS_REMOVED ) - paired_se->status =BLK_SE_STS_REMOVE_IN_PROGRESS ; +WRITE_ONCE (se->status,BLK_SE_STS_REMOVE_IN_PROGRESS ); +if (paired_se &&READ_ONCE (paired_se->status) !=BLK_SE_STS_REMOVED ) +WRITE_ONCE (paired_se->status,BLK_SE_STS_REMOVE_IN_PROGRESS );[Severity: Medium]
Whenzloop_remove_element()is called on an element that was previously marked as degraded, won't this unconditionally overwrite the status toBLK_SE_STS_REMOVE_IN_PROGRESS?
Quoted 4 lines
If the status changes to
BLK_SE_STS_REMOVE_IN_PROGRESS, the check inzloop_prep_rq()forBLK_SE_STS_DEGRADEDwill no longer match. Does this mean that I/O will unexpectedly succeed on the degraded element until the 2-second delayed removal work actually offlines the zones?
Yes. The remove in progress condition needs to be checked too.
Quoted 16 lines
[ ... ]
Quoted 13 lines
drivers/block/zloop.c +9
-0@@ -1944,6 +1988,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 ; + }
Quoted 8 lines
drivers/block/zloop.c static int zloop_ctl_remove(struct zloop_options *opts) · line 2004
Quoted 5 lines
drivers/block/zloop.c +5
-0+ +if (opts->mask & ~(ZLOOP_OPT_ID |ZLOOP_OPT_ELEMENT_ID )) { +pr_err ("Invalid option specified for degrade_element\n" ); +return -EINVAL ; + }[Severity: Low]
Does this code silently fail if the user omits theelement_idoption?
No. The missing element_id option is checked before this if.
Quoted 16 lines
The check above ensures no invalid options are provided, but it lacks a validation to ensure
ZLOOP_OPT_ELEMENT_IDis actually present inopts->mask. If omitted,opts->element_iddefaults to 0, which gets rejected silently insidezloop_degrade_element()without any error message to the user.Quoted 10 lines
drivers/block/zloop.c +10
-0+ +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;
Quoted 8 lines
drivers/block/zloop.c static int zloop_ctl_remove(struct zloop_options *opts) · line 2019
Quoted 5 lines
drivers/block/zloop.c +5
-0+ + ret =zloop_degrade_element (zlo, opts->element_id); +if (!ret) +pr_info ("Degraded element %u of device %u\n" , + opts->id, opts->element_id);[Severity: Low]
Are the arguments topr_info()swapped here?
Yes they are.
Quoted 12 lines
The format string specifies "element %u of device %u", but the variables are passed as
opts->id(the device ID) followed byopts->element_id(the element ID). This will display the values in the reverse order of the text.Quoted 6 lines
drivers/block/zloop.c +6
-0+ +unlock: +mutex_unlock (&zloop_ctl_mutex); + +return ret; +}
Signature · 2 lines
Damien Le Moal Western Digital Research
Damien Le Moal
maintainerPATCH v2 6/7 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 v2 7/7 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
@@ -3937,6 +3937,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_storage_elements_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