Add support for storage element depopulation
Subsystems (7)
Damien Le Moal
maintainerv3 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 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 (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 | 361 +++++++++- block/blk.h | 15 + block/ioctl.c | 5 + drivers/block/zloop.c | 669 ++++++++++++++++-- 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, 1377 insertions(+), 47 deletions(-)
Signature · 1 line
2.55.0
Damien Le Moal
maintainerPATCH v3 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
[email protected]
AI reviewRe: [PATCH v3 1/7] block: fail reads to offline zones early
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] block: slab-out-of-bounds read in disk_zone_get_state() due to race with capacity expansion --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] block: slab-out-of-bounds read in disk_zone_get_state() due to race with capacity expansion --
commit 891363eb78b3eced11718bdb36c5d62832506870
Author: Damien Le Moal <[email protected]>
block: fail reads to offline zones early
Any BIO targeting an offline zone of a zoned block device will fail, including read commands. This commit fails these read operations early if we ever see one.
block/blk-core.c void submit_bio_noacct(struct bio *bio) · line 871
Quoted 10 lines
block/blk-core.c +2
-0@@ -867,6 +867,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))
[Severity: High]
Could this introduce a data race leading to a slab-out-of-bounds read?
Looking at the fast path in submit_bio_noacct(), this evaluates bdev_zone_is_offline() locklessly before blk_queue_enter() is called to freeze the queue.
Concurrently, disk_revalidate_zone_resources() can expand the zone capacity and update disk->nr_zones before replacing the RCU-protected zones_state array:
block/blk-zoned.c:disk_revalidate_zone_resources() {
...
disk->nr_zones = args->nr_zones;
...
disk_set_zones_state_array(disk, args->zones_state);
}Because disk->nr_zones is updated independently of the disk->zones_state array replacement, a reader in disk_zone_get_state() might fetch the new, larger nr_zones value but still dereference the old, smaller zones_state array pointer.
If a read bio targets a sector in the newly expanded region, the bounds check in disk_zone_get_state() would pass incorrectly:
block/blk-zoned.c:disk_zone_get_state() {
...
zones_state = rcu_dereference(disk->zones_state);
if (likely(zones_state && zno < disk->nr_zones))
zs = zones_state[zno];
...
}This would lead to an out-of-bounds read on the old array allocation. Should we ensure the queue is entered or otherwise protect against concurrent zone revalidation before checking the offline status?
Quoted 3 lines
block/blk-core.c +1
-0+ goto end_io;break ;case REQ_OP_WRITE :
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christoph Hellwig
reviewerRe: [PATCH v3 1/7] block: fail reads to offline zones early
In reply to Damien Le MoalQuoted 3 lines · Damien Le Moal
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.
Sashiko things this can race with zone updates. Which is real, at least sorta. Maybe we should just do this in the drivers instead where we are under freeze protection? A bit uglier due to duplication, but not really any more complicated. And drivers right now would basically just be sd and zloop anyway.
Damien Le Moal
maintainerRe: [PATCH v3 1/7] block: fail reads to offline zones early
Quoted 10 lines · Christoph Hellwig
Quoted 3 lines · Damien Le Moal
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.
Sashiko things this can race with zone updates. Which is real, at least sorta. Maybe we should just do this in the drivers instead where we are under freeze protection? A bit uglier due to duplication, but not really any more complicated. And drivers right now would basically just be sd and zloop anyway.
Yep, I will drop this patch and add a zloop and sd patch.
Signature · 2 lines
Damien Le Moal Western Digital Research
Damien Le Moal
maintainerPATCH v3 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 | 181 +++++++++++++++++++++++++++++++++- include/linux/blkdev.h | 16 +++ include/uapi/linux/blkzoned.h | 67 +++++++++++++ 3 files changed, 263 insertions(+), 1 deletion(-)
block/blk-zoned.c +180 -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,183 @@ 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 ; +unsigned int noio_flag; +int ret; + +/* The callers already checked that disk->fops->se_ops is set. */ + 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; + } + + noio_flag =memalloc_noio_save (); + elements = kzalloc_objs(struct blk_storage_element, nr_elements); +memalloc_noio_restore (noio_flag); +if (!elements) +return -ENOMEM ; + +/* +* 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 make it +* interruptible by a fatal signal so that the user can terminate this +* retry loop if the device misbehaves. +*/ +while (!fatal_signal_pending (current)) { +/* +* 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 (1000 ); + } + +if (!ret &&fatal_signal_pending (current)) + ret = -EINTR ; + +kfree (elements); + +return ret; +} + +/** +* bdev_report_storage_elements - report the storage elements of a block device +* @bdev: 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 nuber 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_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 +* @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; +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 +* @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; +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 v3 2/7] block: introduce storage element management
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] block: NULL pointer dereference if optional se_ops methods are not implemented - [Medium] block…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] block: NULL pointer dereference if optional se_ops methods are not implemented
- [Medium] block: disk_wait_for_se_mgmt_completion() ignores hardware errors and falsely reports success
- [Medium] block: disk_wait_for_se_mgmt_completion() overwrites successful completion with -EINTR --
commit d3d300e29a3d6ef944841ecbfddc83f5a8fabe94
Author: Damien Le Moal <[email protected]>
block: introduce storage element management
Recent 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.
Quoted 4 lines
block/blk-zoned.c +0
-0
[ ... ]
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2732
Quoted 9 lines
block/blk-zoned.c +9
-0+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 ; +unsigned int noio_flag; +int ret; + +/* The callers already checked that disk->fops->se_ops is set. */ + ret = disk->fops->se_ops->report_elements (disk,NULL , &nr_elements);
[Severity: High]
Does this cause a NULL pointer dereference if the device driver provides se_ops but leaves the optional report_elements method as NULL?
[ ... ]
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2770
Quoted 8 lines
block/blk-zoned.c +8
-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: Medium]
Will this loop falsely report success if a hardware operation fails?
If the hardware encounters an error and updates the element's status to BLK_SE_STS_REMOVE_ERROR or BLK_SE_STS_RESTORE_ERROR, the status is no longer IN_PROGRESS. As a result, the loop terminates successfully (i >= nr_se) and the function returns 0. Will the caller then assume the element was successfully depopulated or restored, masking the hardware failure?
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2777
Quoted 7 lines
block/blk-zoned.c +7
-0+ +/* Not done yet: wait and retry. */ +msleep (1000 ); + } + +if (!ret &&fatal_signal_pending (current)) + ret = -EINTR ;
[Severity: Medium]
Does this logic mistakenly overwrite a successful completion with an error if a signal arrives concurrently?
If the polling loop detects the operation finished successfully and breaks out, but a fatal signal is delivered at the exact same time, this will set ret = -EINTR.
Since the callers bdev_remove_storage_element() and bdev_restore_storage_elements() abort on an error return, won't this cause them to skip the mandatory blk_revalidate_disk_zones() step, leaving the kernel's zone state permanently out-of-sync with the physical hardware geometry?
[ ... ]
Quoted 6 lines
block/blk-zoned.c +6
-0+int bdev_report_storage_elements (struct block_device *bdev, +struct blk_storage_element *elements, +unsigned int *nr_elements) +{ +struct gendisk *disk = bdev->bd_disk; +
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2812
Quoted 10 lines
block/blk-zoned.c +10
-0+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);
[Severity: High]
Can this code cause a NULL pointer dereference if report_elements is not implemented by the driver?
Quoted 2 lines
block/blk-zoned.c +2
-0+} +EXPORT_SYMBOL_GPL (bdev_report_storage_elements);
[ ... ]
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 2851
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]
Can this trigger a NULL pointer dereference if the driver provides se_ops but omits the remove_element method?
[ ... ]
Quoted 8 lines
block/blk-zoned.c +8
-0+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 ;
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2891
Quoted 10 lines
block/blk-zoned.c +10
-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); + + ret = disk->fops->se_ops->restore_elements (disk);
[Severity: High]
Does this code trigger a NULL pointer dereference if the driver provides se_ops but leaves restore_elements as NULL?
The commit message notes that "Repopulating defective storage elements is generally not allowed by devices", suggesting drivers will commonly omit restore_elements.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Christoph Hellwig
reviewerRe: [PATCH v3 2/7] block: introduce storage element management
In reply to Damien Le MoalQuoted 2 lines · Damien Le Moal
Co-developed-by Christoph Hellwig <[email protected]>Signed-off-by Damien Le Moal <[email protected]>
That's a lot of credit for the two or three trivial fixes that got folded in, I'd drop that line.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2732
Quoted 9 lines
block/blk-zoned.c +9
-0+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 ; +unsigned int noio_flag; +int ret; + +/* The callers already checked that disk->fops->se_ops is set. */ + ret = disk->fops->se_ops->report_elements (disk,NULL , &nr_elements);
Sashikoa thing report_elements could be zero. Which is of course a bit stupid, but maybe we can protect against that by checking that all members are set at registration time for the bdops?
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2746
Quoted 3 lines
block/blk-zoned.c +3
-0+/* +* 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
Overly long line.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2755
Quoted 9 lines
block/blk-zoned.c +9
-0+* may take, we cannot have a timeout on this loop. So make it +* interruptible by a fatal signal so that the user can terminate this +* retry loop if the device misbehaves. +*/ +while (!fatal_signal_pending (current)) { +/* +* Check if we have storage elements being removed or restored. +*/ + nr_se = nr_elements;
Declare شr_se locally in the loop?
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2763
Quoted 8 lines
block/blk-zoned.c +8
-0+ ret = disk->fops->se_ops->report_elements (disk, elements, + &nr_se); +if (ret) { +pr_err ("Failed to get storage elements\n" ); +break ; + } + + e = elements;
Same for e? And maybe factor this entire loop into a helper to make sure nothing leaks out.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2767
Quoted 4 lines
block/blk-zoned.c +4
-0+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 ;
Switch? And yeah, the Sashiko comment on the error handling here looks correct to me.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2770
Quoted 2 lines
block/blk-zoned.c +2
-0+if (i >= nr_se) +break ;
And this looks a bit odd. Why not use a goto to get out instead of this nesting (splitting it into a separate helper would take care of that with a direct return as well).
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2773
Quoted 2 lines
block/blk-zoned.c +2
-0+/* Not done yet: wait and retry. */ +msleep (1000 );
It would be nice to have a UA for this in future spec versions instead of he busy wait.
Quoted 3 lines
+ * 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
Overly long lines.
Damien Le Moal
maintainerRe: [PATCH v3 2/7] block: introduce storage element management
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Damien Le Moal
Co-developed-by Christoph Hellwig <[email protected]>Signed-off-by Damien Le Moal <[email protected]>That's a lot of credit for the two or three trivial fixes that got folded in, I'd drop that line.
Quoted 13 lines
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2732
Quoted 9 lines
block/blk-zoned.c +9
-0+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 ; +unsigned int noio_flag; +int ret; + +/* The callers already checked that disk->fops->se_ops is set. */ + ret = disk->fops->se_ops->report_elements (disk,NULL , &nr_elements);Sashikoa thing
report_elementscould be zero. Which is of course a bit stupid, but maybe we can protect against that by checking that all members are set at registration time for the bdops?
Yes, I thought about the same.
Quoted 11 lines
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2763
Quoted 8 lines
block/blk-zoned.c +8
-0+ ret = disk->fops->se_ops->report_elements (disk, elements, + &nr_se); +if (ret) { +pr_err ("Failed to get storage elements\n" ); +break ; + } + + e = elements;Same for e? And maybe factor this entire loop into a helper to make sure nothing leaks out.
Yes, that will be cleaner.
Quoted 16 lines
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2767
Quoted 5 lines
block/blk-zoned.c +4
-0+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 ;Switch? And yeah, the Sashiko comment on the error handling here looks correct to me.
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2770
Quoted 2 lines
block/blk-zoned.c +2
-0+if (i >= nr_se) +break ;And this looks a bit odd. Why not use a goto to get out instead of this nesting (splitting it into a separate helper would take care of that with a direct return as well).
Quoted 5 lines
block/blk-zoned.c int queue_zone_wplugs_show(void *data, struct seq_file *m) · line 2773
Quoted 2 lines
block/blk-zoned.c +2
-0+/* Not done yet: wait and retry. */ +msleep (1000 );It would be nice to have a UA for this in future spec versions instead of he busy wait.
Yes it would be, but that would be supported by SAS drives only. SATA does not have UA, and that means that HBAs would need to emulate it... Not straightforward and probably will lead to lots of differences between adapters.
Signature · 2 lines
Damien Le Moal Western Digital Research
Damien Le Moal
maintainerPATCH v3 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 | 162 ++++++++++++++++++++++++++++++++++ block/blk.h | 9 ++ block/ioctl.c | 5 ++ include/uapi/linux/blkzoned.h | 14 +++ include/uapi/linux/fs.h | 1 + 5 files changed, 191 insertions(+)
block/blk-zoned.c +162 -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>
@@ -2813,6 +2814,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
@@ -2861,6 +2929,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 ; + +inode_lock (bdev->bd_mapping->host); + +/* +* 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. +*/ +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 ); +filemap_invalidate_unlock (bdev->bd_mapping); + +if (!ret) + ret =bdev_remove_storage_element (bdev, element_id); + +inode_unlock (bdev->bd_mapping->host); + +return ret; +} +/** * bdev_restore_storage_elements - Restore all depopulated storage elements of a * block device
@@ -2899,3 +3006,58 @@ 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 ; + +inode_lock (bdev->bd_mapping->host); + +/* +* 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. +*/ +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 ); +filemap_invalidate_unlock (bdev->bd_mapping); + +if (!ret) + ret =bdev_restore_storage_elements (bdev); + +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 v3 3/7] block: add storage element management ioctls
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] blk: missing partition check in device-wide storage element ioctls - [High] blk: NULL pointer…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] blk: missing partition check in device-wide storage element ioctls
- [High] blk: NULL pointer dereference if se_ops callbacks are not implemented
- [High] blk: TOCTOU race in blkdev_report_storage_elements_ioctl causes out-of-bounds read
- [High] blk: stale page cache due to premature filemap_invalidate_unlock
- [High] block: concurrent direct I/O bypasses inode_lock during storage element management
- [Medium] blk: kernel zone state desync on fatal signal interruption
--
commit a79f3f7a086e95bf1e025298308a290c19115ef1
Author: Damien Le Moal <[email protected]>
block: add storage element management ioctls
This commit introduces the BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, and BLKRESTORESTORELEMS ioctls. These allow user space to interact with the block layer's storage element management operations for zoned block devices.
Quoted 5 lines
block/blk-zoned.c +0
-0@@ -2813,6 +2814,73 @@ int bdev_report_storage_elements(struct block_device *bdev,
[ ... ]
Quoted 25 lines
block/blk-zoned.c +25
-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);
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2867
Quoted 10 lines
block/blk-zoned.c +10
-0+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);
[Severity: High]
Does this code have a TOCTOU race that could cause an out-of-bounds read?
In the second call to bdev_report_storage_elements(), the driver might update nr_elements to a larger value if the hardware state has changed and reports more elements.
If nr_elements is increased, the subsequent copy_to_user() will compute a copy size larger than the allocated elements buffer, potentially leaking kernel memory to userspace.
Quoted 4 lines
block/blk-zoned.c +4
-0+if (retc) { + ret = -EFAULT ; + goto free_elements; + }
[ ... ]
Quoted 5 lines
block/blk-zoned.c +5
-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;
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2946
Quoted 10 lines
block/blk-zoned.c +10
-0+ +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 ; + +inode_lock (bdev->bd_mapping->host);
[Severity: High]
Does this incorrectly rely on inode_lock to serialize device access?
The request queue is explicitly unfrozen in bdev_remove_storage_element() via blk_mq_unfreeze_queue() to allow polling. Concurrently, a thread could issue an O_DIRECT write to the block device. In blkdev_write_iter(), the IOCB_DIRECT path bypasses inode_lock and submits the BIO directly to the unfrozen queue.
This allows hardware reconfiguration commands to execute concurrently with regular I/O (reads, writes, zone resets), violating device requirements during depopulation.
Quoted 8 lines
block/blk-zoned.c +8
-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 +* 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.
block/blk-zoned.c int bdev_remove_storage_element(struct block_device *bdev, · line 2964
Quoted 10 lines
block/blk-zoned.c +10
-0+*/ +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 ); +filemap_invalidate_unlock (bdev->bd_mapping); + +if (!ret) + ret =bdev_remove_storage_element (bdev, element_id);
[Severity: High]
Can dropping the invalidation lock before the element removal cause the page cache to become stale?
The invalidation lock is dropped before the actual long-running device operation (bdev_remove_storage_element) executes.
Because the lock is dropped before the physical device operation completes, a concurrent buffered read or memory-mapped page fault can fetch the soon-to-be-removed data from the hardware and cache it. Once the hardware operation finishes, the page cache will be left containing stale data.
[Severity: Medium]
What happens if the element removal is interrupted by a fatal signal?
The bdev_remove_storage_element() function polls for completion via disk_wait_for_se_mgmt_completion(). If the polling loop is interrupted by a fatal signal, it returns -EINTR and bypasses the call to blk_revalidate_disk_zones(disk).
The hardware operation may still complete asynchronously, leaving the kernel's internal zone metadata permanently out of sync with the physical device state.
Quoted 5 lines
block/blk-zoned.c +5
-0+ +inode_unlock (bdev->bd_mapping->host); + +return ret; +}
[ ... ]
Quoted 16 lines
block/blk-zoned.c +16
-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 ; + +inode_lock (bdev->bd_mapping->host); + +/* +* 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.
block/blk-zoned.c int bdev_restore_storage_elements(struct block_device *bdev) · line 3035
Quoted 10 lines
block/blk-zoned.c +10
-0+*/ +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 ); +filemap_invalidate_unlock (bdev->bd_mapping); + +if (!ret) + ret =bdev_restore_storage_elements (bdev);
[Severity: High]
Similarly here, can dropping the invalidation lock before the element restoration cause the page cache to become stale?
The invalidation lock is dropped before the physical device operation (bdev_restore_storage_elements) executes, opening a race window for a concurrent read to cache the soon-to-be-reset data.
block/blk-zoned.c int bdev_restore_storage_elements(struct block_device *bdev) · line 3047
Quoted 12 lines
block/blk-zoned.c +12
-0+ +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 ;
[Severity: Critical]
Is a partition check missing for these device-wide operations?
BLKREMOVESTORELEM and BLKRESTORESTORELEMS execute whole-disk hardware operations, but there is no bdev_is_partition(bdev) check in the ioctl routing.
If executed on a partition:
- truncate_bdev_range() only truncates the page cache of that specific partition, leaving sibling partitions and the whole disk mapping with stale data.
- inode_lock() is taken only on the partition, meaning concurrent ioctls on different partitions can bypass serialization.
[Severity: High]
Can this result in a NULL pointer dereference if a driver omits some callbacks in se_ops?
The functions bdev_report_storage_elements(), bdev_remove_storage_element(), and bdev_restore_storage_elements() all check that disk->fops->se_ops is non-NULL. However, they do not verify that the specific function pointers (report_elements, remove_element, restore_elements) are actually implemented before dereferencing them.
If a driver provides the se_ops structure but omits an optional callback, invoking the corresponding ioctl will trigger a NULL pointer dereference.
Quoted 4 lines
block/blk-zoned.c +4
-0+ +switch (cmd) { +case BLKGETNRSTORELEMS : +return blkdev_get_nr_storage_elements_ioctl (bdev, argp);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Christoph Hellwig
reviewerRe: [PATCH v3 3/7] 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 2846
Quoted 2 lines
block/blk-zoned.c +2
-0+if (copy_from_user (&rep, argp, +sizeof (struct blk_storage_elements_report )))
Can we shorten the name of the struct a bit? blk_se_report? Similar for other identifiers?
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2857
Quoted 11 lines
block/blk-zoned.c +11
-0+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);
This doesn't work as it could race. We'll always need to allocate the space for all the elements the user asked for?
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2875
Quoted 11 lines
block/blk-zoned.c +11
-0+ 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 ));
Why not copy back the entire struct in one go?
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = truncate_bdev_range(bdev, mode, 0, + (get_capacity(bdev->bd_disk) << SECTOR_SHIFT) - 1);
Use bdev_nr_bytes() here?
block/blk-zoned.c int bdev_restore_storage_elements(struct block_device *bdev) · line 3031
Quoted 10 lines
block/blk-zoned.c +10
-0+/* +* 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. +*/ +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 );
Same. Also what about factoring the duplicate code block into a helper?
Damien Le Moal
maintainerRe: [PATCH v3 3/7] block: add storage element management ioctls
Quoted 5 lines · Christoph Hellwig
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2846
Quoted 2 lines
block/blk-zoned.c +2
-0+if (copy_from_user (&rep, argp, +sizeof (struct blk_storage_elements_report )))Can we shorten the name of the struct a bit?
blk_se_report? Similar for other identifiers?
Sure.
Quoted 15 lines
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2857
Quoted 12 lines
block/blk-zoned.c +11
-0+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);This doesn't work as it could race. We'll always need to allocate the space for all the elements the user asked for?
I am not following... The number of elements of a drive never changes, and that is what nr_elements indicates. So getting the min with the user indicated value still gives all elements, and that also creates a bound in case we get a crazy large value from the user.
Quoted 14 lines
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2875
Quoted 12 lines
block/blk-zoned.c +11
-0+ 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 ));Why not copy back the entire struct in one go?
Indeed.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = truncate_bdev_range(bdev, mode, 0, + (get_capacity(bdev->bd_disk) << SECTOR_SHIFT) - 1);Use
bdev_nr_bytes()here?
Quoted 12 lines
block/blk-zoned.c int bdev_restore_storage_elements(struct block_device *bdev) · line 3031
Quoted 10 lines
block/blk-zoned.c +10
-0+/* +* 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. +*/ +filemap_invalidate_lock (bdev->bd_mapping); + ret =blkdev_issue_flush (bdev->bd_disk->part0); +if (!ret) + ret =truncate_bdev_range (bdev, mode,0 , + (get_capacity (bdev->bd_disk) <<SECTOR_SHIFT ) -1 );Same. Also what about factoring the duplicate code block into a helper?
Yes.
Signature · 2 lines
Damien Le Moal Western Digital Research
Christoph Hellwig
reviewerRe: [PATCH v3 3/7] block: add storage element management ioctls
Quoted 9 lines · Damien Le Moal
Quoted 4 lines
block/blk-zoned.c int bdev_report_storage_elements(struct block_device *bdev, · line 2857
Quoted 1 line
block/blk-zoned.c +1
-0+ elements = kzalloc_objs(struct blk_storage_element, nr_elements);This doesn't work as it could race. We'll always need to allocate the space for all the elements the user asked for?
I am not following... The number of elements of a drive never changes, and that is what
nr_elementsindicates. So getting the min with the user indicated value still gives all elements, and that also creates a bound in case we get a crazy large value from the user.
The explicitly pass the bound in. Or have it struct gendisk as a fixed value.
Damien Le Moal
maintainerPATCH v3 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 | 565 ++++++++++++++++-- 2 files changed, 530 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 +523 -42
803 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 4/7] zloop: add storage element emulation
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] block: permanent device bricking due to uncleared restore_in_progress flag - [High] block…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] block: permanent device bricking due to uncleared restore_in_progress flag
- [High] block: restored storage elements' zones are permanently left offline or read-only
- [Medium] block: TOCTOU race allows I/O on OFFLINE/READONLY zones during removal --
commit 20ddc2153af7f05d1115bfa8b2509e5c9f65e4f8
Author: Damien Le Moal <[email protected]>
Subject: zloop: add storage element emulation
This commit adds storage element emulation to the zloop driver, allowing simulation of SMR disk fractional access storage elements.
Quoted 4 lines
drivers/block/zloop.c +0
-0
[ ... ]
drivers/block/zloop.c static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no) · line 522
Quoted 13 lines
drivers/block/zloop.c +6
-0@@ -430,20 +511,19 @@ static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)mutex_lock (&zone->lock); +if (zloop_zone_is_offline_or_readonly (zlo, zone)) { +if (!all_zones) + ret = -EIO ; + goto unlock; + } +if (!test_bit (ZLOOP_ZONE_SEQ_ERROR , &zone->flags) && zone->cond ==BLK_ZONE_COND_EMPTY ) goto unlock;
[Severity: High]
In zloop_restore_elements_work(), the code calls zloop_reset_all_zones() to reset the zones of restored elements, which in turn calls this function as zloop_reset_zone(..., all_zones=true).
Since zloop_zone_is_offline_or_readonly() evaluates to true for these removed elements' zones, the function jumps to unlock and skips zloop_do_reset_zone().
Does this permanently leave the restored elements' zones offline or read-only, preventing the restoration from completing successfully?
[ ... ]
Quoted 5 lines
drivers/block/zloop.c +5
-0+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;
drivers/block/zloop.c static void zloop_complete_rq(struct request *rq) · line 997
Quoted 10 lines
drivers/block/zloop.c +10
-0+ +spin_lock (&zone->wp_lock); + +if (zlo->nr_elements) { +if (zone->cond ==BLK_ZONE_COND_OFFLINE || + (zone->cond ==BLK_ZONE_COND_READONLY && is_write)) { + ret = false; + goto unlock; + } + }
[Severity: Medium]
Is there a time-of-check to time-of-use race condition here?
This validates the zone condition against offline and read-only states before adding the I/O request to the workqueue. If a concurrently executing zloop_remove_element_work() transitions the zone condition to offline while the request is pending in the queue, the request could still be processed.
Once popped from the queue, zloop_handle_cmd() calls zloop_rw(), which executes reads and conventional zone writes without re-acquiring the zone lock or re-validating the zone condition. Could this allow reads and writes to execute against offlined zones?
[ ... ]
Quoted 17 lines
drivers/block/zloop.c +17
-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 blk_storage_element *se; +unsigned int i; +int ret; + +mutex_lock (&zlo->stor_elements_lock); + +/* Reset all zones. */ + ret =zloop_reset_all_zones (zlo); +if (ret) + goto out_unlock; + +/* Restore all removed elements. */ +for (i =0 , se = zlo->elements; i < zlo->nr_elements; i++, se++) {
drivers/block/zloop.c static int zloop_report_zones(struct gendisk *disk, sector_t sector, · line 1305
Quoted 10 lines
drivers/block/zloop.c +10
-0+if (se->status ==BLK_SE_STS_RESTORE_IN_PROGRESS ) + se->status =BLK_SE_STS_OK ; + } + + zlo->nr_removed_elements =0 ; +WRITE_ONCE (zlo->restore_in_progress, false); + +out_unlock: +mutex_unlock (&zlo->stor_elements_lock); +}
[Severity: High]
If zloop_reset_all_zones() fails (for example, if vfs_truncate encounters an I/O error), this code branches to out_unlock and skips clearing the restore_in_progress flag.
Since zlo->restore_in_progress would remain true, zloop_queue_rq() appears to unconditionally reject all incoming block I/O with BLK_STS_IOERR afterward. Does this leave the device permanently bricked on a reset failure?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Christoph Hellwig
reviewerRe: [PATCH v3 4/7] zloop: add storage element emulation
In reply to Damien Le MoalLooks good modulo the corner cases pointed out by Sashiko.
Damien Le Moal
maintainerPATCH v3 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 | 130 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 115 insertions(+), 15 deletions(-)
drivers/block/zloop.c +115 -15
@@ -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; };/*
@@ -989,11 +993,27 @@ 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 || +READ_ONCE (se->status) ==BLK_SE_STS_REMOVE_IN_PROGRESS ) { + ret = false; + goto unlock; + } }/*
@@ -1137,7 +1157,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 ;
@@ -1155,6 +1175,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,
@@ -1201,10 +1246,10 @@ 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++; }
@@ -1238,7 +1283,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;/*
@@ -1247,7 +1292,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 ; }
@@ -1263,9 +1308,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 ));
@@ -1293,8 +1338,8 @@ 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 ) - se->status =BLK_SE_STS_OK ; +if (READ_ONCE (se->status) ==BLK_SE_STS_RESTORE_IN_PROGRESS ) +WRITE_ONCE (se->status,BLK_SE_STS_OK ); } zlo->nr_removed_elements =0 ;
@@ -1332,8 +1377,8 @@ static int zloop_restore_elements(struct gendisk *disk)*/ WRITE_ONCE (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,
@@ -1946,6 +1991,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 ];
@@ -1966,6 +2047,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 ;
@@ -2117,6 +2199,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);
@@ -2145,14 +2234,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 }, };
@@ -2200,6 +2291,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 ;
@@ -2223,6 +2317,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);
@@ -2233,6 +2329,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 v3 5/7] zloop: add degrade_element control command
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] block: double-free and UAF in zloop_ctl_add error path - [Low] block: missing presence check and…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] block: double-free and UAF in zloop_ctl_add error path
- [Low] block: missing presence check and error message for element_id in zloop_ctl_degrade_element --
commit 85f6af597dc5c733cb0ab607eae7cb9cac0357b4
Author: Damien Le Moal <[email protected]>
zloop: add degrade_element control command
Allow 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.
Quoted 4 lines
drivers/block/zloop.c +0
-0
[ ... ]
drivers/block/zloop.c static int zloop_ctl_add(struct zloop_options *opts) · line 1894
Quoted 6 lines
drivers/block/zloop.c +2
-0@@ -1384,6 +1890,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);
[Severity: High]
Does this code introduce a double-free and use-after-free on zlo->elements?
When a failure in zloop_ctl_add() causes execution to jump to the out_cleanup_disk label (which is located just above this block), put_disk(zlo->disk) is called. This synchronously triggers the zloop_free_disk() fops callback. This callback completely frees zlo and its members, including zlo->elements.
The error path then falls through and accesses the already freed zlo pointer to sequentially double-free the same resources, including the newly added zlo->elements shown here.
Can this be triggered from user-space by writing invalid parameters or encountering resource exhaustion during the device add command on /dev/zloop-control?
[ ... ]
Quoted 8 lines
drivers/block/zloop.c +4
-0@@ -1946,6 +1991,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 2007
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 fail to verify the presence of the ZLOOP_OPT_ELEMENT_ID option?
This bitwise check in zloop_ctl_degrade_element() only validates that no extra options are present, but it does not enforce that ZLOOP_OPT_ELEMENT_ID is actually provided. If element_id is omitted, it defaults to 0 and zloop_degrade_element(zlo, 0) will safely catch this and return -EINVAL. However, the user receives a silent failure rather than an informative error message about the missing required argument.
Can we add a check similar to ZLOOP_OPT_ID to ensure ZLOOP_OPT_ELEMENT_ID is explicitly provided?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Damien Le Moal
maintainerPATCH v3 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
[email protected]
AI reviewRe: [PATCH v3 6/7] scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] scsi: sd: Data race on struct scsi_disk bitfields including modify_zones_supported --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] scsi: sd: Data race on struct scsi_disk bitfields including modify_zones_supported --
commit 38d051c5f929d7e01fbe804c20673174398bee9b
Author: Damien Le Moal <[email protected]>
scsi: sd_zbc: always revalidate zones for disks supporting head depopulation
This commit ensures that zone revalidation is always performed for ZBC disks that support head depopulation, even if the zone size and number of zones have not changed. It adds a new bitfield to track the disk's support for the REMOVE ELEMENT AND MODIFY ZONES command.
drivers/scsi/sd.h struct scsi_disk { · line 159
Quoted 9 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;
[Severity: High]
Does adding this bitfield here introduce a data race with other concurrently accessed bitfields?
C compilers implement bitfield assignments as non-atomic read-modify-write (RMW) operations on the underlying memory word. This bitfield shares a memory word with WCE, cache_override, and ignore_medium_access_errors.
Quoted 3 lines
drivers/scsi/sd.h +0
-0};#define to_scsi_disk (obj) container_of(obj, struct scsi_disk, disk_dev)
[ ... ]
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 645
Quoted 6 lines
drivers/scsi/sd_zbc.c +2
-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);
[Severity: High]
Can this assignment silently overwrite concurrent updates to other bitfields?
This assignment is executed in sd_zbc_read_zones() during disk revalidation. Concurrently, a sysfs store via cache_type_store() can modify cache_override, WCE, and RCD before acquiring queue_limits_start_update(), meaning it does not synchronize with disk revalidation. Additionally, sd_eh_action() can modify ignore_medium_access_errors from the asynchronous SCSI error handler.
If sd_revalidate_disk() runs concurrently with the sysfs cache updates or SCSI EH, their non-atomic RMW operations on the shared bitfield word will race.
Losing the WCE bit update could result in skipped cache flushes and subsequent data corruption on power loss. Losing the modify_zones_supported update could cause the block layer to skip necessary zone revalidation on ZBC disks.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Damien Le Moal
maintainerPATCH v3 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
[email protected]
AI reviewRe: [PATCH v3 7/7] scsi: sd_zbc: define storage element management operations
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] block: race in bdev_remove_storage_element allows DIO during zone removal - [Medium] scsi: sd_zbc…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] block: race in bdev_remove_storage_element allows DIO during zone removal
- [Medium] scsi: sd_zbc: runt zone omitted from element->nr_zones calculation
- [Medium] scsi: sd_zbc: data race on sdkp->zone_info in sd_zbc_parse_storage_element() --
commit 944c56308f45deb202a2ccbb04113e14aa6d2842
Author: Damien Le Moal <[email protected]>
scsi: sd_zbc: define storage element management operations
Define 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_zbc.c static void sd_zbc_print_zones(struct scsi_disk *sdkp) · line 555
Quoted 13 lines
drivers/scsi/sd_zbc.c +5
-0@@ -548,6 +548,239 @@ static void sd_zbc_print_zones(struct scsi_disk *sdkp) sdkp->zone_info.zone_blocks); } +static void sd_zbc_parse_storage_element (struct scsi_disk *sdkp,u8 *desc, +struct blk_storage_element *element) +{ +struct scsi_device *sdp = sdkp->device; +sector_t zone_sectors =sd_zbc_zone_sectors (sdkp);
[Severity: Medium]
Could this read of sdkp->zone_info.zone_blocks (via sd_zbc_zone_sectors) race with concurrent disk revalidation?
It appears that sd_zbc_parse_storage_element() accesses zone_blocks without acquiring disk->zone_revalidate_mutex or freezing the request queue via blk_queue_enter(). Concurrently, disk revalidation (e.g., via the partition scan ioctl or uevent rescan) can call sd_zbc_revalidate_zones() which modifies sdkp->zone_info before acquiring zone_revalidate_mutex:
drivers/scsi/sd_zbc.c:sd_zbc_revalidate_zones() {
...
sdkp->zone_info.zone_blocks = zone_blocks;
sdkp->zone_info.nr_zones = nr_zones;
...
}Can this lead to torn reads, or an ilog2(0) evaluation if the variable is zeroed concurrently?
Quoted 6 lines
drivers/scsi/sd_zbc.c +6
-0+u64 capacity; + +memset (element,0 ,sizeof (*element)); + + element->id =get_unaligned_be32 (&desc[4 ]); +
drivers/scsi/sd_zbc.c static void sd_zbc_print_zones(struct scsi_disk *sdkp) · line 571
Quoted 10 lines
drivers/scsi/sd_zbc.c +10
-0+switch (desc[14 ]) { +case SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE : + element->type =BLK_SE_TYPE_RDWR ; + capacity =get_unaligned_be64 (&desc[16 ]); +if (!zone_sectors || capacity ==ULLONG_MAX ) + element->nr_zones =0 ; +else + element->nr_zones = +logical_to_sectors (sdp, capacity) >> +ilog2 (zone_sectors);
[Severity: Medium]
Does this floor division drop the remainder of the capacity, improperly omitting the runt zone from the element's zone count?
When the driver parses a BLK_SE_TYPE_RDWR storage element descriptor spanning a capacity that does not divide perfectly by the zone size (e.g., reaching the end of the disk where a runt zone resides), the bitwise right shift silently truncates the remainder.
For comparison, sd_zbc_read_zones() calculates the total disk zones using round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks) to correctly account for the runt zone.
[ ... ]
drivers/scsi/sd_zbc.c static void sd_zbc_print_zones(struct scsi_disk *sdkp) · line 710
Quoted 3 lines
drivers/scsi/sd_zbc.c +3
-0+static int sd_zbc_remove_storage_element (struct gendisk *disk, +unsigned int element_id) +{
[Severity: High]
Is it safe to rely on the caller for synchronization here when the block layer queue freeze has been dropped?
The caller in the block layer actively drops the queue freeze before executing the removal:
block/blk-zoned.c:bdev_remove_storage_element() {
...
memflags = blk_mq_freeze_queue(disk->queue);
blk_mq_unfreeze_queue(disk->queue, memflags);
ret = disk->fops->se_ops->remove_element(disk, element_id);
...
}And the ioctl only holds inode_lock:
block/blk-zoned.c:blkdev_remove_storage_element_ioctl() {
...
filemap_invalidate_unlock(bdev->bd_mapping);
if (!ret)
ret = bdev_remove_storage_element(bdev, element_id);
inode_unlock(bdev->bd_mapping->host);
...
}Since direct I/O (via blkdev_write_iter) explicitly bypasses inode_lock for block devices, this seems to allow direct I/O to be issued while the element is actively being removed. Could this cause torn state between the hardware and the block layer's in-memory zone write plug tracking?
Quoted 4 lines
drivers/scsi/sd_zbc.c +4
-0+struct scsi_disk *sdkp =scsi_disk (disk); +struct scsi_device *sdp = sdkp->device; +const int timeout = sdp->request_queue->rq_timeout; +struct scsi_sense_hdr sshdr;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7