| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/09 11:30 | flow | patch-triage |
7mArgs:null Results: {
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blkdev_zone_storage_elements_ioctl",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_remove_element_work",
"zloop_restore_elements_work",
"zloop_ctl_degrade_element",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements"
],
"KMSANReasoning": "The patch introduces storage element emulation and management for zoned block devices across blk-zoned, drivers/block/zloop, drivers/scsi/sd_zbc, and adds corresponding UAPI definitions and ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS).\n\nRegarding KMSAN applicability:\n1. Kernel-to-userspace data transfer: In `blkdev_report_storage_elements_ioctl`, the kernel copies `struct blk_se_report` containing flexible array members `struct blk_se elements[]` back to userspace via `copy_to_user`. The buffer is allocated using `kzalloc_flex`, which zero-initializes the entire allocated memory block. Furthermore, `struct blk_se` is 32 bytes in size (id: 4B, paired_id: 4B, nr_zones: 8B, type: 1B, status: 1B, restore_allowed: 1B, reserved: 13B) with an 8-byte alignment requirement and contains no padding holes between fields or at the end. In SCSI ZBC (`sd_zbc_parse_storage_element`), each element is explicitly `memset` to 0 before field assignment. In `zloop_report_elements`, each element descriptor originates from a `kzalloc_objs` allocation. The single integer written by `blkdev_get_nr_storage_elements_ioctl` via `put_user` transfers an initialized `unsigned int`.\n2. Heap and stack initializations: All newly allocated data structures (`zlo-\u003eelements`, `rep`, `buf`, `elements`) are allocated with zeroing allocators (`kzalloc`, `kzalloc_objs`, `kzalloc_flex`), and stack variables (e.g. `struct blk_revalidate_zone_args args = {}`, `struct zloop_options opts = {}`) are zero-initialized.\n3. Control flow: Branching logic does not depend on uninitialized variables.\n\nAny potential bugs in this patch series relate to concurrency/locking (handled by LOCKDEP), array bounds checking (e.g., `element_id - 1` indexing, handled by KASAN), or request queue handling. There are no uninitialized memory risks or kernel info-leaks that require a dedicated KMSAN session.",
"NeedsKMSAN": false,
"Reasoning": "The patch series introduces storage element management for zoned block devices, adding new UAPI ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS), core block layer handlers in block/blk-zoned.c, storage element emulation and control operations in the virtual zoned loop driver (zloop), and SCSI ZBC support in sd_zbc.c. The changes modify reachable block layer logic, introduce delayed work handlers, queue operations, and state machine transitions reachable in standard virtualized environments.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/09 11:30 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit ab617d08dddf09f15f0a18d1adcd42c4ab1499e3\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Fri Oct 9 11:30:22 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/blockdev/zoned_loop.rst b/Documentation/admin-guide/blockdev/zoned_loop.rst\nindex 64277494fb36a..ec39f141af8d6 100644\n--- a/Documentation/admin-guide/blockdev/zoned_loop.rst\n+++ b/Documentation/admin-guide/blockdev/zoned_loop.rst\n@@ -61,7 +61,7 @@ The options available for the add command can be listed by reading the\n /dev/zloop-control device::\n \n \t$ cat /dev/zloop-control\n- 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\n+ 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\n remove id=%d\n \n In more details, the options that can be used with the \"add\" command are as\n@@ -113,6 +113,12 @@ discard_write_cache Discard all data that was not explicitly persisted using a\n each zone file to the size recorded during the last flush\n operation. This simulates power fail events where\n uncommitted data is lost.\n+stor_elements Control storage element emulation. The default value is 0,\n+ indicating no emulation. A value of 1 indicates that all\n+ access storage elements (equivalent to read+write head of\n+ a disk) are emulated. A value of 2 enables fractional\n+ access storage element (equivalent to pairs of read and\n+ write heads of a disk) emulation .\n =================== =========================================================\n \n 3) Deleting a Zoned Device\ndiff --git a/block/blk-zoned.c b/block/blk-zoned.c\nindex 19268afb8752e..10fb3ca5adcd6 100644\n--- a/block/blk-zoned.c\n+++ b/block/blk-zoned.c\n@@ -18,6 +18,8 @@\n #include \u003clinux/mempool.h\u003e\n #include \u003clinux/kthread.h\u003e\n #include \u003clinux/freezer.h\u003e\n+#include \u003clinux/delay.h\u003e\n+#include \u003clinux/uaccess.h\u003e\n \n #include \u003ctrace/events/block.h\u003e\n \n@@ -292,6 +294,22 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)\n \treturn !blk_zstate_is_conv(zs);\n }\n \n+/**\n+ * disk_zone_is_offline - check if a sector belongs to an offline zone\n+ * @disk:\tgendisk to check\n+ * @sector:\tsector number\n+ *\n+ * Check if @sector on @disk is contained in an offline zone.\n+ */\n+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)\n+{\n+\tif (!blk_queue_is_zoned(disk-\u003equeue))\n+\t\treturn false;\n+\n+\treturn disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;\n+}\n+EXPORT_SYMBOL_GPL(disk_zone_is_offline);\n+\n /**\n * bdev_zone_is_seq - check if a sector belongs to a sequential write zone\n * @bdev: block device to check\n@@ -2526,20 +2544,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,\n \treturn ret;\n }\n \n-/**\n- * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs\n- * @disk:\tTarget disk\n- *\n- * Helper function for low-level device drivers to check, (re) allocate and\n- * initialize resources used for managing zoned disks. This function should\n- * normally be called by blk-mq based drivers when a zoned gendisk is probed\n- * and when the zone configuration of the gendisk changes (e.g. after a format).\n- * Before calling this function, the device driver must already have set the\n- * device zone size (chunk_sector limit) and the max zone append limit.\n- * BIO based drivers can also use this function as long as the device queue\n- * can be safely frozen.\n- */\n-int blk_revalidate_disk_zones(struct gendisk *disk)\n+static int disk_revalidate_zones(struct gendisk *disk)\n {\n \tstruct blk_revalidate_zone_args args = { };\n \tstruct blk_report_zones_args rep_args = {\n@@ -2550,19 +2555,10 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \tint retries = 2;\n \tint ret;\n \n-\tif (WARN_ON_ONCE(!blk_queue_is_zoned(disk-\u003equeue)))\n-\t\treturn -EIO;\n-\n-\t/*\n-\t * Serialize calls to this function so that we can safely look at and\n-\t * eventually change the disk zone information.\n-\t */\n-\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n-\n again:\n \tret = disk_revalidate_capacity(disk, \u0026args);\n \tif (ret)\n-\t\tgoto unlock;\n+\t\treturn ret;\n \n \t/*\n \t * Allocate zone resources if they are needed and we have not done\n@@ -2575,14 +2571,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \t\tret = disk_alloc_zone_resources(disk, args.capacity);\n \t\tif (ret) {\n \t\t\tmemalloc_noio_restore(noio_flag);\n-\t\t\tgoto unlock;\n+\t\t\treturn ret;\n \t\t}\n \t}\n \n \tret = disk_init_revalidate_args(disk, \u0026args);\n \tif (ret) {\n \t\tmemalloc_noio_restore(noio_flag);\n-\t\tgoto unlock;\n+\t\treturn ret;\n \t}\n \n \tret = disk-\u003efops-\u003ereport_zones(disk, 0, UINT_MAX, \u0026rep_args);\n@@ -2599,8 +2595,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \tif (ret)\n \t\tgoto free_args;\n \n-\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n-\n \treturn 0;\n \n free_args:\n@@ -2616,7 +2610,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \t}\n \n \tpr_warn(\"%s: failed to revalidate zones\\n\", disk-\u003edisk_name);\n-unlock:\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs\n+ * @disk:\tTarget disk\n+ *\n+ * Helper function for low-level device drivers to check, (re) allocate and\n+ * initialize resources used for managing zoned disks. This function should\n+ * normally be called by blk-mq based drivers when a zoned gendisk is probed\n+ * and when the zone configuration of the gendisk changes (e.g. after a format).\n+ * Before calling this function, the device driver must already have set the\n+ * device zone size (chunk_sector limit) and the max zone append limit.\n+ * BIO based drivers can also use this function as long as the device queue\n+ * can be safely frozen.\n+ */\n+int blk_revalidate_disk_zones(struct gendisk *disk)\n+{\n+\tint ret;\n+\n+\tif (WARN_ON_ONCE(!blk_queue_is_zoned(disk-\u003equeue)))\n+\t\treturn -EIO;\n+\n+\t/*\n+\t * Serialize calls to this function so that we can safely look at and\n+\t * eventually change the disk zone information.\n+\t */\n+\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\tret = disk_revalidate_zones(disk);\n \tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n \n \treturn ret;\n@@ -2700,5 +2723,408 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)\n \n \treturn 0;\n }\n-\n #endif\n+\n+static int disk_se_mgmt_in_progress(struct gendisk *disk,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t bool *in_progress)\n+{\n+\tunsigned int i, nr_se = disk-\u003enr_storage_elements;\n+\tint ret;\n+\n+\t/*\n+\t * Check if we have storage elements being removed or restored.\n+\t */\n+\tret = disk-\u003efops-\u003ese_ops-\u003ereport_elements(disk, elements, \u0026nr_se);\n+\tif (ret) {\n+\t\tpr_err(\"Failed to get storage elements\\n\");\n+\t\treturn ret;\n+\t}\n+\n+\tfor (i = 0; i \u003c nr_se; i++) {\n+\t\tswitch (elements[i].status) {\n+\t\tcase BLK_SE_STS_REMOVE_IN_PROGRESS:\n+\t\tcase BLK_SE_STS_RESTORE_IN_PROGRESS:\n+\t\t\t*in_progress = true;\n+\t\t\treturn 0;\n+\t\tdefault:\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\t*in_progress = false;\n+\n+\treturn 0;\n+}\n+\n+static int disk_wait_for_se_mgmt_completion(struct gendisk *disk)\n+{\n+\tstruct blk_se *elements;\n+\tunsigned int noio_flag;\n+\tbool in_progress;\n+\tint ret;\n+\n+\tnoio_flag = memalloc_noio_save();\n+\telements = kzalloc_objs(struct blk_se, disk-\u003enr_storage_elements);\n+\tmemalloc_noio_restore(noio_flag);\n+\tif (!elements)\n+\t\treturn -ENOMEM;\n+\n+\t/*\n+\t * Poll the storage elements heath state to detect the end of a\n+\t * removal or restoration operation. Since we do not know how long the\n+\t * operation may take, we cannot have a timeout on this loop. So check\n+\t * for fatal signals so that the user can terminate this retry loop if\n+\t * the device misbehaves.\n+\t */\n+\twhile (1) {\n+\t\tret = disk_se_mgmt_in_progress(disk, elements, \u0026in_progress);\n+\t\tif (ret || !in_progress)\n+\t\t\tbreak;\n+\n+\t\tif (fatal_signal_pending(current)) {\n+\t\t\tret = -EINTR;\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\t/* Not done yet: wait and retry. */\n+\t\tmsleep(1000);\n+\t}\n+\n+\tkfree(elements);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * bdev_report_storage_elements - report the storage elements of a zoned\n+ *\t\t\t\t block device\n+ * @bdev:\tthe zoned block device to check\n+ * @elements:\tarray of storage element descriptors to fill\n+ * @nr_elements: maximum size of @elements\n+ *\n+ * Fill at most @nr_elements storage element descriptors in the array\n+ * @elements. The number of storage elements filled in the array is returned\n+ * using @nr_elements. If @elements is NULL, only @nr_elements is returned to\n+ * indicate the maximum number of storage elements of @bdev.\n+ *\n+ * Returns 0 on success and a negative error code on failure.\n+ */\n+int bdev_report_storage_elements(struct block_device *bdev,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t unsigned int *nr_elements)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (!nr_elements)\n+\t\treturn -EINVAL;\n+\n+\tif (!elements) {\n+\t\t*nr_elements = disk-\u003enr_storage_elements;\n+\t\treturn 0;\n+\t}\n+\n+\t*nr_elements = min(*nr_elements, disk-\u003enr_storage_elements);\n+\tif (!*nr_elements)\n+\t\treturn -EINVAL;\n+\n+\treturn disk-\u003efops-\u003ese_ops-\u003ereport_elements(disk, elements,\n+\t\t\t\t\t\t nr_elements);\n+}\n+EXPORT_SYMBOL_GPL(bdev_report_storage_elements);\n+\n+static int blkdev_get_nr_storage_elements_ioctl(struct block_device *bdev,\n+\t\t\t\t\t\tvoid __user *argp)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\n+\tif (put_user(disk-\u003enr_storage_elements, (unsigned int __user *)argp))\n+\t\treturn -EFAULT;\n+\n+\treturn 0;\n+}\n+\n+static int blkdev_report_storage_elements_ioctl(struct block_device *bdev,\n+\t\t\t\t\t\tvoid __user *argp)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\tstruct blk_se_report *rep;\n+\tint ret;\n+\n+\tif (!argp)\n+\t\treturn -EINVAL;\n+\n+\trep = kzalloc_flex(*rep, elements, disk-\u003enr_storage_elements);\n+\tif (!rep)\n+\t\treturn -ENOMEM;\n+\n+\tif (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {\n+\t\tret = -EFAULT;\n+\t\tgoto free_rep;\n+\t}\n+\n+\trep-\u003enr_elements = min(rep-\u003enr_elements, disk-\u003enr_storage_elements);\n+\tif (!rep-\u003enr_elements) {\n+\t\tret = -EINVAL;\n+\t\tgoto free_rep;\n+\t}\n+\n+\tret = bdev_report_storage_elements(bdev, rep-\u003eelements,\n+\t\t\t\t\t \u0026rep-\u003enr_elements);\n+\tif (ret)\n+\t\tgoto free_rep;\n+\n+\tif (copy_to_user(argp, rep, struct_size_t(struct blk_se_report,\n+\t\t\t\t\t\t elements, rep-\u003enr_elements)))\n+\t\tret = -EFAULT;\n+\n+free_rep:\n+\tkfree(rep);\n+\treturn ret;\n+}\n+\n+/**\n+ * bdev_remove_storage_element - Remove (depopulate) a storage element of a\n+ *\t\t\t\t zoned block device\n+ * @bdev:\tblock device to operate on\n+ * @element_id:\tID of the storage element to remove\n+ *\n+ * Remove (depopulate) the storage element identified by @element_id from the\n+ * block device @bdev. The caller is responsible for taking care of any\n+ * necessary device write cache flush and invalidation of cached data for the\n+ * zones that will be offlined.\n+ *\n+ * Returns 0 on success and a negative error code on failure.\n+ */\n+int bdev_remove_storage_element(struct block_device *bdev,\n+\t\t\t\tunsigned int element_id)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\tunsigned int memflags;\n+\tint ret;\n+\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/* Zero is not a valid storage element ID. */\n+\tif (!element_id)\n+\t\treturn -EINVAL;\n+\n+\t/*\n+\t * Serialize against concurrent revalidation and concurrent storage\n+\t * element management operations so that we can see consistent zone\n+\t * condition changes.\n+\t */\n+\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\t/*\n+\t * Freeze and unfreeze the queue to flush any outstanding command.\n+\t * The caller is responsible for not queuing up more I/Os by higher\n+\t * level means.\n+\t */\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n+\n+\tret = disk-\u003efops-\u003ese_ops-\u003eremove_element(disk, element_id);\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\t/* Revalidate the device zones once the opration completes. */\n+\tret = disk_wait_for_se_mgmt_completion(disk);\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = disk_revalidate_zones(disk);\n+\n+unlock:\n+\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\treturn ret;\n+}\n+EXPORT_SYMBOL_GPL(bdev_remove_storage_element);\n+\n+static int blkdev_flush_and_truncate(struct block_device *bdev,\n+\t\t\t\t blk_mode_t mode)\n+{\n+\tint ret;\n+\n+\tfilemap_invalidate_lock(bdev-\u003ebd_mapping);\n+\tret = blkdev_issue_flush(bdev-\u003ebd_disk-\u003epart0);\n+\tif (!ret)\n+\t\tret = truncate_bdev_range(bdev, mode, 0,\n+\t\t\t\t\t bdev_nr_bytes(bdev) - 1);\n+\tfilemap_invalidate_unlock(bdev-\u003ebd_mapping);\n+\n+\treturn ret;\n+}\n+\n+static int blkdev_remove_storage_element_ioctl(struct block_device *bdev,\n+\t\t\t\t\tblk_mode_t mode, void __user *argp)\n+{\n+\tunsigned int element_id;\n+\tint ret;\n+\n+\tif (!(mode \u0026 BLK_OPEN_WRITE))\n+\t\treturn -EBADF;\n+\tif (bdev_read_only(bdev))\n+\t\treturn -EPERM;\n+\n+\tif (get_user(element_id, (unsigned int __user *)argp))\n+\t\treturn -EFAULT;\n+\n+\tinode_lock(bdev-\u003ebd_mapping-\u003ehost);\n+\n+\t/*\n+\t * Flush the device volatile write cache and invalidate all cached data\n+\t * so that reads do not return old data for zones that went offline.\n+\t * Since a user can only write to zoned devices using direct IOs, we\n+\t * should never have any dirty page invalidated and so no data loss as\n+\t * long as the device cache flush succeeds, that is, as long as the\n+\t * flush does not use degraded storage elements.\n+\t */\n+\tret = blkdev_flush_and_truncate(bdev, mode);\n+\tif (!ret)\n+\t\tret = bdev_remove_storage_element(bdev, element_id);\n+\n+\tinode_unlock(bdev-\u003ebd_mapping-\u003ehost);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * bdev_restore_storage_elements - Restore all depopulated storage elements of\n+ *\t\t\t\t a zoned block device\n+ * @bdev:\tblock device to operate on\n+ *\n+ * Restore all storage elements of @bdev that have been depopulated. Not all\n+ * elements may be restored by this operation.\n+ *\n+ * Returns 0 on success and a negative error code on failure.\n+ */\n+int bdev_restore_storage_elements(struct block_device *bdev)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\tunsigned int memflags;\n+\tint ret;\n+\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003enr_storage_elements)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/*\n+\t * Serialize against concurrent revalidation and concurrent storage\n+\t * element management operations so that we can see consistent zone\n+\t * condition changes.\n+\t */\n+\tmutex_lock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\t/*\n+\t * Freeze and unfreeze the queue to flush any outstanding commands.\n+\t * The caller is responsible for not queuing up more I/O by higher\n+\t * level means.\n+\t */\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n+\n+\tret = disk-\u003efops-\u003ese_ops-\u003erestore_elements(disk);\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = disk_wait_for_se_mgmt_completion(disk);\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = disk_revalidate_zones(disk);\n+\n+unlock:\n+\tmutex_unlock(\u0026disk-\u003ezone_revalidate_mutex);\n+\n+\treturn ret;\n+}\n+EXPORT_SYMBOL_GPL(bdev_restore_storage_elements);\n+\n+static int blkdev_restore_storage_elements_ioctl(struct block_device *bdev,\n+\t\t\t\t\t\t blk_mode_t mode)\n+{\n+\tint ret;\n+\n+\tif (!(mode \u0026 BLK_OPEN_WRITE))\n+\t\treturn -EBADF;\n+\tif (bdev_read_only(bdev))\n+\t\treturn -EPERM;\n+\n+\tinode_lock(bdev-\u003ebd_mapping-\u003ehost);\n+\n+\t/*\n+\t * Storage element restoration is a destructive operation that will\n+\t * reset all zones. So fflush the device volatile write cache and\n+\t * invalidate all cached data that we may have.\n+\t */\n+\tret = blkdev_flush_and_truncate(bdev, mode);\n+\tif (!ret)\n+\t\tret = bdev_restore_storage_elements(bdev);\n+\n+\tinode_unlock(bdev-\u003ebd_mapping-\u003ehost);\n+\n+\treturn ret;\n+}\n+\n+int disk_init_storage_element_mgmt(struct gendisk *disk)\n+{\n+\tint ret;\n+\n+\t/*\n+\t * Storage element operations are for zoned disks only and\n+\t * optional...\n+\t */\n+\tif (!blk_queue_is_zoned(disk-\u003equeue) || !disk-\u003efops-\u003ese_ops)\n+\t\treturn 0;\n+\n+\t/* ...but if supported, all operations must be defined. */\n+\tif (!disk-\u003efops-\u003ese_ops-\u003ereport_elements ||\n+\t !disk-\u003efops-\u003ese_ops-\u003eremove_element ||\n+\t !disk-\u003efops-\u003ese_ops-\u003erestore_elements) {\n+\t\tpr_err(\"%s: invalid storage element operations\\n\",\n+\t\t disk-\u003edisk_name);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\t/*\n+\t * Get the number of storage elements of the disk. If this fails,\n+\t * do not report an error as the failure may be because the zoned disk\n+\t * does not support storage element management.\n+\t */\n+\tret = disk-\u003efops-\u003ese_ops-\u003ereport_elements(disk, NULL,\n+\t\t\t\t\t\t \u0026disk-\u003enr_storage_elements);\n+\tif (ret)\n+\t\tdisk-\u003enr_storage_elements = 0;\n+\n+\treturn 0;\n+}\n+\n+int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,\n+\t\t\t\t blk_mode_t mode, unsigned int cmd,\n+\t\t\t\t void __user *argp)\n+{\n+\tstruct gendisk *disk = bdev-\u003ebd_disk;\n+\n+\tif (!disk-\u003enr_storage_elements)\n+\t\treturn -ENOTTY;\n+\n+\tswitch (cmd) {\n+\tcase BLKGETNRSTORELEMS:\n+\t\treturn blkdev_get_nr_storage_elements_ioctl(bdev, argp);\n+\tcase BLKREPORTSTORELEMS:\n+\t\treturn blkdev_report_storage_elements_ioctl(bdev, argp);\n+\tcase BLKREMOVESTORELEM:\n+\t\treturn blkdev_remove_storage_element_ioctl(bdev, mode, argp);\n+\tcase BLKRESTORESTORELEMS:\n+\t\treturn blkdev_restore_storage_elements_ioctl(bdev, mode);\n+\tdefault:\n+\t\tbreak;\n+\t}\n+\n+\treturn -ENOTTY;\n+}\ndiff --git a/block/blk.h b/block/blk.h\nindex 2cc03aa54c532..bbf523c9579cf 100644\n--- a/block/blk.h\n+++ b/block/blk.h\n@@ -527,6 +527,7 @@ static inline void ioc_clear_queue(struct request_queue *q)\n #endif /* CONFIG_BLK_ICQ */\n \n #ifdef CONFIG_BLK_DEV_ZONED\n+int disk_init_storage_element_mgmt(struct gendisk *disk);\n void disk_init_zone_resources(struct gendisk *disk);\n void disk_release_zone_resources(struct gendisk *disk);\n static inline bool bio_zone_write_plugging(struct bio *bio)\n@@ -578,7 +579,14 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,\n int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,\n \t\tunsigned int cmd, unsigned long arg);\n bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector);\n+int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,\n+\t\t\t\t blk_mode_t mode, unsigned int cmd,\n+\t\t\t\t void __user *argp);\n #else /* CONFIG_BLK_DEV_ZONED */\n+static inline int disk_init_storage_element_mgmt(struct gendisk *disk)\n+{\n+\treturn 0;\n+}\n static inline void disk_init_zone_resources(struct gendisk *disk)\n {\n }\n@@ -625,6 +633,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev,\n {\n \treturn false;\n }\n+static inline int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,\n+\t\t\t\t\tblk_mode_t mode, unsigned int cmd,\n+\t\t\t\t\tvoid __user *argp)\n+{\n+\treturn -ENOTTY;\n+}\n #endif /* CONFIG_BLK_DEV_ZONED */\n \n struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);\ndiff --git a/block/genhd.c b/block/genhd.c\nindex 10ca8b4d6eea7..e2cef791ac91a 100644\n--- a/block/genhd.c\n+++ b/block/genhd.c\n@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,\n \tif (WARN_ON_ONCE(bdev_nr_sectors(disk-\u003epart0) \u003e BLK_DEV_MAX_SECTORS))\n \t\treturn -EINVAL;\n \n+\tret = disk_init_storage_element_mgmt(disk);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tif (queue_is_mq(disk-\u003equeue)) {\n \t\t/*\n \t\t * -\u003esubmit_bio and -\u003epoll_bio are bypassed for blk-mq drivers.\ndiff --git a/block/ioctl.c b/block/ioctl.c\nindex 64b4e6c0f6965..ccf806c2e37d1 100644\n--- a/block/ioctl.c\n+++ b/block/ioctl.c\n@@ -679,6 +679,11 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,\n \t\treturn put_uint(argp, bdev_zone_sectors(bdev));\n \tcase BLKGETNRZONES:\n \t\treturn put_uint(argp, bdev_nr_zones(bdev));\n+\tcase BLKGETNRSTORELEMS:\n+\tcase BLKREPORTSTORELEMS:\n+\tcase BLKREMOVESTORELEM:\n+\tcase BLKRESTORESTORELEMS:\n+\t\treturn blkdev_zone_storage_elements_ioctl(bdev, mode, cmd, argp);\n \tcase BLKROGET:\n \t\treturn put_int(argp, bdev_read_only(bdev) != 0);\n \tcase BLKSSZGET: /* get block device logical block size */\ndiff --git a/drivers/block/zloop.c b/drivers/block/zloop.c\nindex 394dc2408ed73..a1c9f7a960cfd 100644\n--- a/drivers/block/zloop.c\n+++ b/drivers/block/zloop.c\n@@ -37,6 +37,8 @@ enum {\n \tZLOOP_OPT_ORDERED_ZONE_APPEND\t= (1 \u003c\u003c 10),\n \tZLOOP_OPT_DISCARD_WRITE_CACHE\t= (1 \u003c\u003c 11),\n \tZLOOP_OPT_MAX_OPEN_ZONES\t= (1 \u003c\u003c 12),\n+\tZLOOP_OPT_STOR_ELEMENTS\t\t= (1 \u003c\u003c 13),\n+\tZLOOP_OPT_ELEMENT_ID\t\t= (1 \u003c\u003c 14),\n };\n \n static const match_table_t zloop_opt_tokens = {\n@@ -51,11 +53,23 @@ static const match_table_t zloop_opt_tokens = {\n \t{ ZLOOP_OPT_BUFFERED_IO,\t\"buffered_io\"\t\t},\n \t{ ZLOOP_OPT_ZONE_APPEND,\t\"zone_append=%u\"\t},\n \t{ ZLOOP_OPT_ORDERED_ZONE_APPEND, \"ordered_zone_append\"\t},\n-\t{ ZLOOP_OPT_DISCARD_WRITE_CACHE, \"discard_write_cache\" },\n+\t{ ZLOOP_OPT_DISCARD_WRITE_CACHE, \"discard_write_cache\"\t},\n \t{ ZLOOP_OPT_MAX_OPEN_ZONES,\t\"max_open_zones=%u\"\t},\n+\t{ ZLOOP_OPT_STOR_ELEMENTS,\t\"stor_elements=%u\"\t},\n+\t{ ZLOOP_OPT_ELEMENT_ID,\t\t\"element_id=%u\"\t\t},\n \t{ ZLOOP_OPT_ERR,\t\tNULL\t\t\t}\n };\n \n+/* Storage elements emulation types. */\n+enum zloop_stor_elements {\n+\t/* No emulation. */\n+\tZLOOP_STOR_ELEMENTS_NONE,\n+\t/* Emulate read+write storage elements. */\n+\tZLOOP_STOR_ELEMENTS_RDWR,\n+\t/* Emulate pairs of associated read and write storage elements. */\n+\tZLOOP_STOR_ELEMENTS_PAIRS,\n+};\n+\n /* Default values for the \"add\" operation. */\n #define ZLOOP_DEF_ID\t\t\t-1\n #define ZLOOP_DEF_ZONE_SIZE\t\t((256ULL * SZ_1M) \u003e\u003e SECTOR_SHIFT)\n@@ -68,6 +82,8 @@ static const match_table_t zloop_opt_tokens = {\n #define ZLOOP_DEF_BUFFERED_IO\t\tfalse\n #define ZLOOP_DEF_ZONE_APPEND\t\ttrue\n #define ZLOOP_DEF_ORDERED_ZONE_APPEND\tfalse\n+#define ZLOOP_DEF_STOR_ELEMENTS\t\tZLOOP_STOR_ELEMENTS_NONE\n+#define ZLOOP_DEF_ELEMENT_ID\t\t0\n \n /* Arbitrary limit on the zone size (16GB). */\n #define ZLOOP_MAX_ZONE_SIZE_MB\t\t16384\n@@ -87,6 +103,8 @@ struct zloop_options {\n \tbool\t\t\tzone_append;\n \tbool\t\t\tordered_zone_append;\n \tbool\t\t\tdiscard_write_cache;\n+\tenum zloop_stor_elements stor_elements;\n+\tunsigned int\t\telement_id;\n };\n \n /*\n@@ -117,6 +135,8 @@ struct zloop_zone {\n \tenum blk_zone_cond\tcond;\n \tsector_t\t\tstart;\n \tsector_t\t\twp;\n+\tunsigned int\t\twr_se_id;\n+\tunsigned int\t\trd_se_id;\n \n \tgfp_t\t\t\told_gfp_mask;\n };\n@@ -133,6 +153,7 @@ struct zloop_device {\n \tbool\t\t\tzone_append;\n \tbool\t\t\tordered_zone_append;\n \tbool\t\t\tdiscard_write_cache;\n+\tenum zloop_stor_elements stor_elements;\n \n \tconst char\t\t*base_dir;\n \tstruct file\t\t*data_dir;\n@@ -150,6 +171,17 @@ struct zloop_device {\n \tstruct list_head\topen_zones_lru_list;\n \tunsigned int\t\tnr_open_zones;\n \n+\t/* For storage elements emulation. */\n+\tstruct mutex\t\tstor_elements_lock;\n+\tunsigned int\t\tnr_elements;\n+\tunsigned int\t\tmax_nr_removed_elements;\n+\tunsigned int\t\tnr_removed_elements;\n+\tstruct delayed_work\tremove_element_work;\n+\tunsigned int\t\tremove_element_id;\n+\tstruct delayed_work\trestore_elements_work;\n+\tbool\t\t\trestore_in_progress;\n+\tstruct blk_se\t\t*elements;\n+\n \tstruct zloop_zone\tzones[] __counted_by(nr_zones);\n };\n \n@@ -289,22 +321,43 @@ static bool zloop_do_open_zone(struct zloop_device *zlo,\n \t}\n }\n \n-static void zloop_mark_full(struct zloop_device *zlo, struct zloop_zone *zone)\n+static void zloop_set_zone_cond(struct zloop_device *zlo,\n+\t\t\t\tstruct zloop_zone *zone,\n+\t\t\t\tenum blk_zone_cond cond)\n {\n \tlockdep_assert_held(\u0026zone-\u003ewp_lock);\n \n \tzloop_lru_remove_open_zone(zlo, zone);\n-\tzone-\u003econd = BLK_ZONE_COND_FULL;\n-\tzone-\u003ewp = ULLONG_MAX;\n+\tzone-\u003econd = cond;\n+\tif (cond == BLK_ZONE_COND_EMPTY)\n+\t\tzone-\u003ewp = zone-\u003estart;\n+\telse\n+\t\tzone-\u003ewp = ULLONG_MAX;\n }\n \n-static void zloop_mark_empty(struct zloop_device *zlo, struct zloop_zone *zone)\n+static inline void zloop_set_zone_full(struct zloop_device *zlo,\n+\t\t\t\t struct zloop_zone *zone)\n {\n-\tlockdep_assert_held(\u0026zone-\u003ewp_lock);\n+\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);\n+}\n \n-\tzloop_lru_remove_open_zone(zlo, zone);\n-\tzone-\u003econd = BLK_ZONE_COND_EMPTY;\n-\tzone-\u003ewp = zone-\u003estart;\n+static inline void zloop_set_zone_empty(struct zloop_device *zlo,\n+\t\t\t\t\tstruct zloop_zone *zone)\n+{\n+\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);\n+}\n+\n+static bool zloop_zone_is_offline_or_readonly(struct zloop_device *zlo,\n+\t\t\t\t\t struct zloop_zone *zone)\n+{\n+\tbool ret;\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\tret = zone-\u003econd == BLK_ZONE_COND_OFFLINE ||\n+\t\tzone-\u003econd == BLK_ZONE_COND_READONLY;\n+\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\n+\treturn ret;\n }\n \n static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n@@ -316,6 +369,13 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tlockdep_assert_held(\u0026zone-\u003elock);\n \n+\t/*\n+\t * If the zone was already changed to offline or read-only, we have\n+\t * nothing to do.\n+\t */\n+\tif (zloop_zone_is_offline_or_readonly(zlo, zone))\n+\t\treturn 0;\n+\n \tret = vfs_getattr(\u0026zone-\u003efile-\u003ef_path, \u0026stat, STATX_SIZE, 0);\n \tif (ret \u003c 0) {\n \t\tpr_err(\"Failed to get zone %u file stat (err=%d)\\n\",\n@@ -339,9 +399,9 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tspin_lock(\u0026zone-\u003ewp_lock);\n \tif (!file_sectors) {\n-\t\tzloop_mark_empty(zlo, zone);\n+\t\tzloop_set_zone_empty(zlo, zone);\n \t} else if (file_sectors == zlo-\u003ezone_capacity) {\n-\t\tzloop_mark_full(zlo, zone);\n+\t\tzloop_set_zone_full(zlo, zone);\n \t} else {\n \t\tif (zone-\u003econd != BLK_ZONE_COND_IMP_OPEN \u0026\u0026\n \t\t zone-\u003econd != BLK_ZONE_COND_EXP_OPEN)\n@@ -363,6 +423,11 @@ static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (zloop_zone_is_offline_or_readonly(zlo, zone)) {\n+\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\n \t\tret = zloop_update_seq_zone(zlo, zone_no);\n \t\tif (ret)\n@@ -388,6 +453,11 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (zloop_zone_is_offline_or_readonly(zlo, zone)) {\n+\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\n \t\tret = zloop_update_seq_zone(zlo, zone_no);\n \t\tif (ret)\n@@ -420,7 +490,22 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\n \treturn ret;\n }\n \n-static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)\n+static int zloop_do_reset_zone(struct zloop_device *zlo,\n+\t\t\t struct zloop_zone *zone)\n+{\n+\tif (vfs_truncate(\u0026zone-\u003efile-\u003ef_path, 0))\n+\t\treturn -EIO;\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\tzloop_set_zone_empty(zlo, zone);\n+\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\n+\treturn 0;\n+}\n+\n+static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,\n+\t\t\t bool all_zones)\n {\n \tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\n \tint ret = 0;\n@@ -430,20 +515,19 @@ static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (zloop_zone_is_offline_or_readonly(zlo, zone)) {\n+\t\tif (!all_zones)\n+\t\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\n \t zone-\u003econd == BLK_ZONE_COND_EMPTY)\n \t\tgoto unlock;\n \n-\tif (vfs_truncate(\u0026zone-\u003efile-\u003ef_path, 0)) {\n+\tret = zloop_do_reset_zone(zlo, zone);\n+\tif (ret)\n \t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n-\t\tret = -EIO;\n-\t\tgoto unlock;\n-\t}\n-\n-\tspin_lock(\u0026zone-\u003ewp_lock);\n-\tzloop_mark_empty(zlo, zone);\n-\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n-\tspin_unlock(\u0026zone-\u003ewp_lock);\n \n unlock:\n \tmutex_unlock(\u0026zone-\u003elock);\n@@ -457,7 +541,7 @@ static int zloop_reset_all_zones(struct zloop_device *zlo)\n \tint ret;\n \n \tfor (i = zlo-\u003enr_conv_zones; i \u003c zlo-\u003enr_zones; i++) {\n-\t\tret = zloop_reset_zone(zlo, i);\n+\t\tret = zloop_reset_zone(zlo, i, true);\n \t\tif (ret)\n \t\t\treturn ret;\n \t}\n@@ -475,6 +559,11 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)\n \n \tmutex_lock(\u0026zone-\u003elock);\n \n+\tif (zloop_zone_is_offline_or_readonly(zlo, zone)) {\n+\t\tret = -EIO;\n+\t\tgoto unlock;\n+\t}\n+\n \tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\n \t zone-\u003econd == BLK_ZONE_COND_FULL)\n \t\tgoto unlock;\n@@ -487,7 +576,7 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)\n \t}\n \n \tspin_lock(\u0026zone-\u003ewp_lock);\n-\tzloop_mark_full(zlo, zone);\n+\tzloop_set_zone_full(zlo, zone);\n \tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n \tspin_unlock(\u0026zone-\u003ewp_lock);\n \n@@ -624,7 +713,7 @@ static int zloop_seq_write_prep(struct zloop_cmd *cmd)\n \tif (!is_append || !zlo-\u003eordered_zone_append) {\n \t\tzone-\u003ewp += nr_sectors;\n \t\tif (zone-\u003ewp == zone_end)\n-\t\t\tzloop_mark_full(zlo, zone);\n+\t\t\tzloop_set_zone_full(zlo, zone);\n \t}\n out_unlock:\n \tspin_unlock(\u0026zone-\u003ewp_lock);\n@@ -766,7 +855,7 @@ static void zloop_handle_cmd(struct zloop_cmd *cmd)\n \t\tcmd-\u003eret = zloop_flush(zlo);\n \t\tbreak;\n \tcase REQ_OP_ZONE_RESET:\n-\t\tcmd-\u003eret = zloop_reset_zone(zlo, rq_zone_no(rq));\n+\t\tcmd-\u003eret = zloop_reset_zone(zlo, rq_zone_no(rq), false);\n \t\tbreak;\n \tcase REQ_OP_ZONE_RESET_ALL:\n \t\tcmd-\u003eret = zloop_reset_all_zones(zlo);\n@@ -875,30 +964,72 @@ static void zloop_complete_rq(struct request *rq)\n \tblk_mq_end_request(rq, sts);\n }\n \n-static bool zloop_set_zone_append_sector(struct request *rq)\n+static bool zloop_set_zone_append_sector(struct zloop_device *zlo,\n+\t\t\t\t\t struct zloop_zone *zone,\n+\t\t\t\t\t struct request *rq)\n {\n-\tstruct zloop_device *zlo = rq-\u003eq-\u003equeuedata;\n-\tunsigned int zone_no = rq_zone_no(rq);\n-\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\n \tsector_t zone_end = zone-\u003estart + zlo-\u003ezone_capacity;\n \tsector_t nr_sectors = blk_rq_sectors(rq);\n \n-\tspin_lock(\u0026zone-\u003ewp_lock);\n-\n \tif (zone-\u003econd == BLK_ZONE_COND_FULL ||\n-\t zone-\u003ewp + nr_sectors \u003e zone_end) {\n-\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\t zone-\u003ewp + nr_sectors \u003e zone_end)\n \t\treturn false;\n-\t}\n \n \trq-\u003e__sector = zone-\u003ewp;\n \tzone-\u003ewp += blk_rq_sectors(rq);\n \tif (zone-\u003ewp \u003e= zone_end)\n-\t\tzloop_mark_full(zlo, zone);\n+\t\tzloop_set_zone_full(zlo, zone);\n+\n+\treturn true;\n+}\n+\n+\n+static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)\n+{\n+\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[rq_zone_no(rq)];\n+\tbool is_write = op_is_write(req_op(rq));\n+\tbool ret = true;\n+\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\n+\tif (zlo-\u003enr_elements) {\n+\t\tstruct blk_se *se;\n+\n+\t\t/*\n+\t\t * Check the zone condition and the health state of the storage\n+\t\t * element serving the zone: do not allow any operation to\n+\t\t * offline zones or if the storage element serving the zone is\n+\t\t * being removed, and allow only reads to read-only zones.\n+\t\t */\n+\t\tif (is_write)\n+\t\t\tse = \u0026zlo-\u003eelements[zone-\u003ewr_se_id - 1];\n+\t\telse\n+\t\t\tse = \u0026zlo-\u003eelements[zone-\u003erd_se_id - 1];\n+\t\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_DEGRADED ||\n+\t\t READ_ONCE(se-\u003estatus) == BLK_SE_STS_REMOVE_IN_PROGRESS) {\n+\t\t\tret = false;\n+\t\t\tgoto unlock;\n+\t\t}\n+\n+\t\tif (zone-\u003econd == BLK_ZONE_COND_OFFLINE ||\n+\t\t (zone-\u003econd == BLK_ZONE_COND_READONLY \u0026\u0026 is_write)) {\n+\t\t\tret = false;\n+\t\t\tgoto unlock;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * If we need to strongly order zone append operations, set the request\n+\t * sector to the zone write pointer location now instead of when the\n+\t * command work runs.\n+\t */\n+\tif (zlo-\u003eordered_zone_append \u0026\u0026 req_op(rq) == REQ_OP_ZONE_APPEND)\n+\t\tret = zloop_set_zone_append_sector(zlo, zone, rq);\n \n+unlock:\n \tspin_unlock(\u0026zone-\u003ewp_lock);\n \n-\treturn true;\n+\treturn ret;\n }\n \n static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,\n@@ -914,13 +1045,21 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,\n \t}\n \n \t/*\n-\t * If we need to strongly order zone append operations, set the request\n-\t * sector to the zone write pointer location now instead of when the\n-\t * command work runs.\n+\t * Do not allow commands while the restoration of storage elements is\n+\t * in progress.\n \t */\n-\tif (zlo-\u003eordered_zone_append \u0026\u0026 req_op(rq) == REQ_OP_ZONE_APPEND) {\n-\t\tif (!zloop_set_zone_append_sector(rq))\n+\tif (READ_ONCE(zlo-\u003erestore_in_progress))\n+\t\treturn BLK_STS_IOERR;\n+\n+\tswitch (req_op(rq)) {\n+\tcase REQ_OP_READ:\n+\tcase REQ_OP_WRITE:\n+\tcase REQ_OP_ZONE_APPEND:\n+\t\tif (!zloop_prep_rq(zlo, rq))\n \t\t\treturn BLK_STS_IOERR;\n+\t\tbreak;\n+\tdefault:\n+\t\tbreak;\n \t}\n \n \tblk_mq_start_request(rq);\n@@ -1002,11 +1141,299 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,\n \treturn nr_zones;\n }\n \n+static int zloop_report_elements(struct gendisk *disk,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t unsigned int *nr_elements)\n+{\n+\tstruct zloop_device *zlo = disk-\u003eprivate_data;\n+\tstruct blk_se *se;\n+\tunsigned int i;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\tif (!elements) {\n+\t\t*nr_elements = zlo-\u003enr_elements;\n+\t\tgoto unlock;\n+\t}\n+\n+\t*nr_elements = min(*nr_elements, zlo-\u003enr_elements);\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c *nr_elements; i++, se++) {\n+\t\tswitch (READ_ONCE(se-\u003estatus)) {\n+\t\tcase BLK_SE_STS_REMOVED:\n+\t\tcase BLK_SE_STS_RESTORE_ERROR:\n+\t\t\tse-\u003erestore_allowed = 1;\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\tse-\u003erestore_allowed = 0;\n+\t\t}\n+\t\tmemcpy(\u0026elements[i], se, sizeof(struct blk_se));\n+\t}\n+\n+unlock:\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn 0;\n+}\n+\n+static int zloop_degrade_element(struct zloop_device *zlo,\n+\t\t\t\t unsigned int element_id)\n+{\n+\tstruct blk_se *se;\n+\tint ret = 0;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (!element_id || element_id \u003e zlo-\u003enr_elements)\n+\t\treturn -EINVAL;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\tse = \u0026zlo-\u003eelements[element_id - 1];\n+\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_OK)\n+\t\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_DEGRADED);\n+\telse\n+\t\tret = -EINVAL;\n+\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn ret;\n+}\n+\n+static void zloop_remove_element_work(struct work_struct *work)\n+{\n+\tstruct zloop_device *zlo = container_of(work, struct zloop_device,\n+\t\t\t\t\t\tremove_element_work.work);\n+\tstruct blk_se *se, *paired_se = NULL;\n+\tenum blk_zone_cond cond;\n+\tstruct zloop_zone *zone;\n+\tunsigned int i;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/*\n+\t * If the element to remove is a read element, zones must go offline and\n+\t * the associated write element is also removed.\n+\t */\n+\tse = \u0026zlo-\u003eelements[zlo-\u003eremove_element_id - 1];\n+\tswitch (se-\u003etype) {\n+\tcase BLK_SE_TYPE_RDWR:\n+\t\tcond = BLK_ZONE_COND_OFFLINE;\n+\t\tbreak;\n+\tcase BLK_SE_TYPE_READ:\n+\t\tcond = BLK_ZONE_COND_OFFLINE;\n+\t\tpaired_se = \u0026zlo-\u003eelements[se-\u003epaired_id - 1];\n+\t\tbreak;\n+\tcase BLK_SE_TYPE_WRITE:\n+\t\tcond = BLK_ZONE_COND_READONLY;\n+\t\tbreak;\n+\tdefault:\n+\t\tWARN_ON_ONCE(1);\n+\t}\n+\n+\t/*\n+\t * Change the condition of the zones owned by the (pair of) elements\n+\t * being removed and mark the elements removed.\n+\t */\n+\tfor (i = 0, zone = zlo-\u003ezones; i \u003c zlo-\u003enr_zones; i++, zone++) {\n+\t\tif (zone-\u003ewr_se_id != se-\u003eid \u0026\u0026 zone-\u003erd_se_id != se-\u003eid)\n+\t\t\tcontinue;\n+\t\tmutex_lock(\u0026zone-\u003elock);\n+\t\tspin_lock(\u0026zone-\u003ewp_lock);\n+\t\tclear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\t\tzloop_set_zone_cond(zlo, zone, cond);\n+\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\t\tmutex_unlock(\u0026zone-\u003elock);\n+\t}\n+\n+\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_REMOVED);\n+\tzlo-\u003enr_removed_elements++;\n+\tif (paired_se \u0026\u0026 READ_ONCE(paired_se-\u003estatus) != BLK_SE_STS_REMOVED) {\n+\t\tWRITE_ONCE(paired_se-\u003estatus, BLK_SE_STS_REMOVED);\n+\t\tzlo-\u003enr_removed_elements++;\n+\t}\n+\n+\tzlo-\u003eremove_element_id = 0;\n+\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+}\n+\n+static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)\n+{\n+\tstruct zloop_device *zlo = disk-\u003eprivate_data;\n+\tstruct blk_se *se, *paired_se = NULL;\n+\tunsigned int nr_remove = 1;\n+\tint ret = 0;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tif (element_id \u003e zlo-\u003enr_elements)\n+\t\treturn -EINVAL;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\tif (zlo-\u003eremove_element_id || READ_ONCE(zlo-\u003erestore_in_progress)) {\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+\n+\t/*\n+\t * Get the element to remove. If it is already removed, we have nothing\n+\t * to do.\n+\t */\n+\tse = \u0026zlo-\u003eelements[element_id - 1];\n+\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_REMOVED)\n+\t\tgoto unlock;\n+\n+\t/*\n+\t * If the element to remove is a read element, the associated write\n+\t * element must also be removed.\n+\t */\n+\tif (se-\u003etype == BLK_SE_TYPE_READ) {\n+\t\tpaired_se = \u0026zlo-\u003eelements[se-\u003epaired_id - 1];\n+\t\tif (READ_ONCE(paired_se-\u003estatus) != BLK_SE_STS_REMOVED)\n+\t\t\tnr_remove = 2;\n+\t}\n+\n+\tif (zlo-\u003enr_removed_elements + nr_remove \u003e\n+\t zlo-\u003emax_nr_removed_elements) {\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+\n+\t/*\n+\t * Schedule the element removal with a delay, to emulate the (generally\n+\t * short) time it takes for a real device to depopulate a head and\n+\t * modify the zones.\n+\t */\n+\tzlo-\u003eremove_element_id = element_id;\n+\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_REMOVE_IN_PROGRESS);\n+\tif (paired_se \u0026\u0026 READ_ONCE(paired_se-\u003estatus) != BLK_SE_STS_REMOVED)\n+\t\tWRITE_ONCE(paired_se-\u003estatus, BLK_SE_STS_REMOVE_IN_PROGRESS);\n+\n+\tschedule_delayed_work(\u0026zlo-\u003eremove_element_work,\n+\t\t\t msecs_to_jiffies(2000));\n+\n+unlock:\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn ret;\n+}\n+\n+static void zloop_restore_conv_zone(struct zloop_device *zlo,\n+\t\t\t\t struct zloop_zone *zone)\n+{\n+\tmutex_lock(\u0026zone-\u003elock);\n+\tspin_lock(\u0026zone-\u003ewp_lock);\n+\tif (zone-\u003econd == BLK_ZONE_COND_OFFLINE ||\n+\t zone-\u003econd == BLK_ZONE_COND_READONLY)\n+\t\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_NOT_WP);\n+\tspin_unlock(\u0026zone-\u003ewp_lock);\n+\tmutex_unlock(\u0026zone-\u003elock);\n+}\n+\n+static int zloop_restore_seq_zone(struct zloop_device *zlo,\n+\t\t\t\t struct zloop_zone *zone)\n+{\n+\tint ret;\n+\n+\tmutex_lock(\u0026zone-\u003elock);\n+\tret = zloop_do_reset_zone(zlo, zone);\n+\tif (ret)\n+\t\tset_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags);\n+\tmutex_unlock(\u0026zone-\u003elock);\n+\n+\treturn ret;\n+}\n+\n+static void zloop_restore_elements_work(struct work_struct *work)\n+{\n+\tstruct zloop_device *zlo = container_of(work, struct zloop_device,\n+\t\t\t\t\t\trestore_elements_work.work);\n+\tstruct zloop_zone *zone;\n+\tstruct blk_se *se;\n+\tunsigned int i;\n+\tint ret;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\t/* Reset all zones. */\n+\tfor (i = 0, zone = zlo-\u003ezones; i \u003c zlo-\u003enr_zones; i++, zone++) {\n+\t\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\n+\t\t\tzloop_restore_conv_zone(zlo, zone);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tret = zloop_restore_seq_zone(zlo, zone);\n+\t\tif (ret)\n+\t\t\tgoto out_unlock;\n+\t}\n+\n+\t/* Restore all removed elements. */\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c zlo-\u003enr_elements; i++, se++) {\n+\t\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_RESTORE_IN_PROGRESS)\n+\t\t\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_OK);\n+\t}\n+\n+\tzlo-\u003enr_removed_elements = 0;\n+\n+out_unlock:\n+\tWRITE_ONCE(zlo-\u003erestore_in_progress, false);\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+}\n+\n+static int zloop_restore_elements(struct gendisk *disk)\n+{\n+\tstruct zloop_device *zlo = disk-\u003eprivate_data;\n+\tstruct blk_se *se;\n+\tunsigned int i;\n+\tint ret = 0;\n+\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_NONE)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tmutex_lock(\u0026zlo-\u003estor_elements_lock);\n+\n+\tif (zlo-\u003eremove_element_id || READ_ONCE(zlo-\u003erestore_in_progress)) {\n+\t\tret = -EBUSY;\n+\t\tgoto unlock;\n+\t}\n+\n+\t/* If we have no removed elements, we have nothing to do. */\n+\tif (!zlo-\u003enr_removed_elements)\n+\t\tgoto unlock;\n+\n+\t/*\n+\t * Schedule the elements restoration with a delay, to emulate the time\n+\t * it takes for a real device to restore all removed heads and reset\n+\t * all zones.\n+\t */\n+\tWRITE_ONCE(zlo-\u003erestore_in_progress, true);\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c zlo-\u003enr_elements; i++, se++) {\n+\t\tif (READ_ONCE(se-\u003estatus) == BLK_SE_STS_REMOVED)\n+\t\t\tWRITE_ONCE(se-\u003estatus, BLK_SE_STS_RESTORE_IN_PROGRESS);\n+\t}\n+\n+\tschedule_delayed_work(\u0026zlo-\u003erestore_elements_work,\n+\t\t\t msecs_to_jiffies(5000));\n+\n+unlock:\n+\tmutex_unlock(\u0026zlo-\u003estor_elements_lock);\n+\n+\treturn ret;\n+}\n+\n static void zloop_free_disk(struct gendisk *disk)\n {\n \tstruct zloop_device *zlo = disk-\u003eprivate_data;\n \tunsigned int i;\n \n+\tcancel_delayed_work_sync(\u0026zlo-\u003eremove_element_work);\n+\tcancel_delayed_work_sync(\u0026zlo-\u003erestore_elements_work);\n+\n \tblk_mq_free_tag_set(\u0026zlo-\u003etag_set);\n \n \tfor (i = 0; i \u003c zlo-\u003enr_zones; i++) {\n@@ -1019,15 +1446,24 @@ static void zloop_free_disk(struct gendisk *disk)\n \n \tfput(zlo-\u003edata_dir);\n \tdestroy_workqueue(zlo-\u003eworkqueue);\n+\tkfree(zlo-\u003eelements);\n \tkfree(zlo-\u003ebase_dir);\n \tkvfree(zlo);\n }\n \n+\n+static const struct blk_se_ops zloop_se_ops = {\n+\t.report_elements\t= zloop_report_elements,\n+\t.remove_element\t\t= zloop_remove_element,\n+\t.restore_elements\t= zloop_restore_elements,\n+};\n+\n static const struct block_device_operations zloop_fops = {\n \t.owner\t\t\t= THIS_MODULE,\n \t.open\t\t\t= zloop_open,\n \t.report_zones\t\t= zloop_report_zones,\n \t.free_disk\t\t= zloop_free_disk,\n+\t.se_ops\t\t\t= \u0026zloop_se_ops,\n };\n \n __printf(3, 4)\n@@ -1112,6 +1548,24 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,\n \tif (!opts-\u003ebuffered_io)\n \t\toflags |= O_DIRECT;\n \n+\tif (zlo-\u003estor_elements != ZLOOP_STOR_ELEMENTS_NONE) {\n+\t\tunsigned int nr_elems = zlo-\u003enr_elements;\n+\t\tunsigned int se_idx;\n+\n+\t\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\t\tnr_elems /= 2;\n+\t\tse_idx = zone_no % nr_elems;\n+\t\tzlo-\u003eelements[se_idx].nr_zones++;\n+\n+\t\tzone-\u003ewr_se_id = se_idx + 1;\n+\t\tzone-\u003erd_se_id = zone-\u003ewr_se_id;\n+\t\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {\n+\t\t\tzone-\u003erd_se_id += nr_elems;\n+\t\t\tzlo-\u003eelements[zone-\u003erd_se_id - 1].nr_zones =\n+\t\t\t\tzlo-\u003eelements[se_idx].nr_zones;\n+\t\t}\n+\t}\n+\n \tif (zone_no \u003c zlo-\u003enr_conv_zones) {\n \t\t/* Conventional zone file. */\n \t\tset_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags);\n@@ -1182,6 +1636,79 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,\n \treturn ret;\n }\n \n+#define ZLOOP_MIN_STOR_ELEMENTS\t\t\t2\n+#define ZLOOP_MAX_STOR_ELEMENTS\t\t\t32\n+#define ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS\t32\n+\n+static int zloop_create_storage_elements(struct zloop_device *zlo)\n+{\n+\tstruct blk_se *se, *paired_se;\n+\tunsigned int i, nr_elems, nr_elements;\n+\n+\t/*\n+\t * Calculate the number of storage elements we are going to emulate.\n+\t * To achieve a somewhat realistic emulation, we want at least 2 storage\n+\t * elements, and no more than 32, targeting at least 32 zones per\n+\t * element.\n+\t */\n+\tif (zlo-\u003enr_zones \u003c= 64)\n+\t\tnr_elements = 2;\n+\telse\n+\t\tnr_elements =\n+\t\t\tmin(ZLOOP_MAX_STOR_ELEMENTS,\n+\t\t\t zlo-\u003enr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);\n+\n+\t/*\n+\t * If we are emulating pairs of read and write storage elements, we need\n+\t * double the number of storage element descriptors.\n+\t */\n+\tnr_elems = nr_elements;\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\tnr_elems *= 2;\n+\tzlo-\u003eelements = kzalloc_objs(struct blk_se, nr_elems);\n+\tif (!zlo-\u003eelements)\n+\t\treturn -ENOMEM;\n+\n+\tfor (i = 0, se = zlo-\u003eelements; i \u003c nr_elements; i++, se++) {\n+\t\tse-\u003eid = i + 1;\n+\t\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\t\tse-\u003etype = BLK_SE_TYPE_WRITE;\n+\t\telse\n+\t\t\tse-\u003etype = BLK_SE_TYPE_RDWR;\n+\t\tse-\u003estatus = BLK_SE_STS_OK;\n+\t}\n+\n+\t/*\n+\t * If we are emulating pairs of read and write storage elements,\n+\t * initialize the read elements paired with the write elements we just\n+\t * initialized.\n+\t */\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {\n+\t\tfor (i = 0; i \u003c nr_elements; i++, se++) {\n+\t\t\tse-\u003eid = nr_elements + i + 1;\n+\t\t\tpaired_se = \u0026zlo-\u003eelements[i];\n+\t\t\tse-\u003epaired_id = paired_se-\u003eid;\n+\t\t\tpaired_se-\u003epaired_id = se-\u003eid;\n+\t\t\tse-\u003etype = BLK_SE_TYPE_READ;\n+\t\t\tse-\u003estatus = BLK_SE_STS_OK;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Make sure we do not allow removing all storage elements as that does\n+\t * not make any sense. This is consistent with the device advertized\n+\t * limit of SCSI and ATA devices supporting the storage element\n+\t * depopulation feature.\n+\t */\n+\tzlo-\u003enr_elements = nr_elems;\n+\tif (zlo-\u003estor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)\n+\t\tzlo-\u003emax_nr_removed_elements = zlo-\u003enr_elements - 2;\n+\telse\n+\t\tzlo-\u003emax_nr_removed_elements = zlo-\u003enr_elements - 1;\n+\n+\treturn 0;\n+}\n+\n static bool zloop_dev_exists(struct zloop_device *zlo)\n {\n \tstruct file *cnv, *seq;\n@@ -1237,6 +1764,11 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tWRITE_ONCE(zlo-\u003estate, Zlo_creating);\n \tspin_lock_init(\u0026zlo-\u003eopen_zones_lock);\n \tINIT_LIST_HEAD(\u0026zlo-\u003eopen_zones_lru_list);\n+\tmutex_init(\u0026zlo-\u003estor_elements_lock);\n+\tINIT_DELAYED_WORK(\u0026zlo-\u003eremove_element_work,\n+\t\t\t zloop_remove_element_work);\n+\tINIT_DELAYED_WORK(\u0026zlo-\u003erestore_elements_work,\n+\t\t\t zloop_restore_elements_work);\n \n \tret = mutex_lock_killable(\u0026zloop_ctl_mutex);\n \tif (ret)\n@@ -1270,12 +1802,19 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tif (zlo-\u003ezone_append)\n \t\tzlo-\u003eordered_zone_append = opts-\u003eordered_zone_append;\n \tzlo-\u003ediscard_write_cache = opts-\u003ediscard_write_cache;\n+\tzlo-\u003estor_elements = opts-\u003estor_elements;\n+\n+\tif (zlo-\u003estor_elements != ZLOOP_STOR_ELEMENTS_NONE) {\n+\t\tret = zloop_create_storage_elements(zlo);\n+\t\tif (ret)\n+\t\t\tgoto out_free_idr;\n+\t}\n \n \tzlo-\u003eworkqueue = alloc_workqueue(\"zloop%d\", WQ_UNBOUND | WQ_FREEZABLE,\n \t\t\t\topts-\u003enr_queues * opts-\u003equeue_depth, zlo-\u003eid);\n \tif (!zlo-\u003eworkqueue) {\n \t\tret = -ENOMEM;\n-\t\tgoto out_free_idr;\n+\t\tgoto out_destroy_storage_elements;\n \t}\n \n \tif (opts-\u003ebase_dir)\n@@ -1361,6 +1900,10 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \t\tzlo-\u003eid, zlo-\u003enr_zones,\n \t\t((sector_t)zlo-\u003ezone_size \u003c\u003c SECTOR_SHIFT) \u003e\u003e 20,\n \t\tzlo-\u003eblock_size);\n+\tif (zlo-\u003enr_elements)\n+\t\tpr_info(\"zloop%d: %d storage elements\\n\",\n+\t\t\tzlo-\u003eid, zlo-\u003enr_elements);\n+\n \tpr_info(\"zloop%d: using %s%s zone append\\n\",\n \t\tzlo-\u003eid,\n \t\tzlo-\u003eordered_zone_append ? \"ordered \" : \"\",\n@@ -1370,6 +1913,8 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \n out_cleanup_disk:\n \tput_disk(zlo-\u003edisk);\n+\treturn ret;\n+\n out_cleanup_tags:\n \tblk_mq_free_tag_set(\u0026zlo-\u003etag_set);\n out_close_files:\n@@ -1384,6 +1929,8 @@ static int zloop_ctl_add(struct zloop_options *opts)\n \tkfree(zlo-\u003ebase_dir);\n out_destroy_workqueue:\n \tdestroy_workqueue(zlo-\u003eworkqueue);\n+out_destroy_storage_elements:\n+\tkfree(zlo-\u003eelements);\n out_free_idr:\n \tmutex_lock(\u0026zloop_ctl_mutex);\n \tidr_remove(\u0026zloop_index_idr, zlo-\u003eid);\n@@ -1483,6 +2030,42 @@ static int zloop_ctl_remove(struct zloop_options *opts)\n \treturn 0;\n }\n \n+static int zloop_ctl_degrade_element(struct zloop_options *opts)\n+{\n+\tstruct zloop_device *zlo;\n+\tint ret = 0;\n+\n+\tif (!(opts-\u003emask \u0026 ZLOOP_OPT_ID)) {\n+\t\tpr_err(\"No ID specified for degrade_element\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (opts-\u003emask \u0026 ~(ZLOOP_OPT_ID | ZLOOP_OPT_ELEMENT_ID)) {\n+\t\tpr_err(\"Invalid option specified for degrade_element\\n\");\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tmutex_lock(\u0026zloop_ctl_mutex);\n+\n+\tzlo = idr_find(\u0026zloop_index_idr, opts-\u003eid);\n+\tif (!zlo || zlo-\u003estate == Zlo_creating)\n+\t\tret = -ENODEV;\n+\telse if (zlo-\u003estate == Zlo_deleting)\n+\t\tret = -EINVAL;\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\tret = zloop_degrade_element(zlo, opts-\u003eelement_id);\n+\tif (!ret)\n+\t\tpr_info(\"Degraded element %u of device %u\\n\",\n+\t\t\topts-\u003eelement_id, opts-\u003eid);\n+\n+unlock:\n+\tmutex_unlock(\u0026zloop_ctl_mutex);\n+\n+\treturn ret;\n+}\n+\n static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n {\n \tsubstring_t args[MAX_OPT_ARGS];\n@@ -1502,6 +2085,8 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n \topts-\u003ebuffered_io = ZLOOP_DEF_BUFFERED_IO;\n \topts-\u003ezone_append = ZLOOP_DEF_ZONE_APPEND;\n \topts-\u003eordered_zone_append = ZLOOP_DEF_ORDERED_ZONE_APPEND;\n+\topts-\u003estor_elements = ZLOOP_DEF_STOR_ELEMENTS;\n+\topts-\u003eelement_id = ZLOOP_DEF_ELEMENT_ID;\n \n \tif (!buf)\n \t\treturn 0;\n@@ -1636,6 +2221,30 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n \t\tcase ZLOOP_OPT_DISCARD_WRITE_CACHE:\n \t\t\topts-\u003ediscard_write_cache = true;\n \t\t\tbreak;\n+\t\tcase ZLOOP_OPT_STOR_ELEMENTS:\n+\t\t\tif (match_uint(args, \u0026token)) {\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\tswitch (token) {\n+\t\t\tcase ZLOOP_STOR_ELEMENTS_NONE:\n+\t\t\tcase ZLOOP_STOR_ELEMENTS_RDWR:\n+\t\t\tcase ZLOOP_STOR_ELEMENTS_PAIRS:\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tpr_err(\"Invalid stor_elements value\\n\");\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\topts-\u003estor_elements = token;\n+\t\t\tbreak;\n+\t\tcase ZLOOP_OPT_ELEMENT_ID:\n+\t\t\tif (match_uint(args, \u0026token)) {\n+\t\t\t\tret = -EINVAL;\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\topts-\u003eelement_id = token;\n+\t\t\tbreak;\n \t\tcase ZLOOP_OPT_ERR:\n \t\tdefault:\n \t\t\tpr_warn(\"unknown parameter or missing value '%s'\\n\", p);\n@@ -1664,14 +2273,16 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)\n enum {\n \tZLOOP_CTL_ADD,\n \tZLOOP_CTL_REMOVE,\n+\tZLOOP_CTL_DEGRADE_ELEMENT,\n };\n \n static struct zloop_ctl_op {\n \tint\t\tcode;\n \tconst char\t*name;\n } zloop_ctl_ops[] = {\n-\t{ ZLOOP_CTL_ADD,\t\"add\" },\n-\t{ ZLOOP_CTL_REMOVE,\t\"remove\" },\n+\t{ ZLOOP_CTL_ADD,\t\t\"add\" },\n+\t{ ZLOOP_CTL_REMOVE,\t\t\"remove\" },\n+\t{ ZLOOP_CTL_DEGRADE_ELEMENT,\t\"degrade_element\" },\n \t{ -1,\tNULL },\n };\n \n@@ -1719,6 +2330,9 @@ static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,\n \tcase ZLOOP_CTL_REMOVE:\n \t\tret = zloop_ctl_remove(\u0026opts);\n \t\tbreak;\n+\tcase ZLOOP_CTL_DEGRADE_ELEMENT:\n+\t\tret = zloop_ctl_degrade_element(\u0026opts);\n+\t\tbreak;\n \tdefault:\n \t\tpr_err(\"Invalid operation\\n\");\n \t\tret = -EINVAL;\n@@ -1742,6 +2356,8 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)\n \t\ttok = \u0026zloop_opt_tokens[i];\n \t\tif (!tok-\u003epattern)\n \t\t\tbreak;\n+\t\tif (tok-\u003etoken == ZLOOP_OPT_ELEMENT_ID)\n+\t\t\tcontinue;\n \t\tif (i)\n \t\t\tseq_putc(seq_file, ',');\n \t\tseq_puts(seq_file, tok-\u003epattern);\n@@ -1752,6 +2368,10 @@ static int zloop_ctl_show(struct seq_file *seq_file, void *private)\n \tseq_puts(seq_file, zloop_ctl_ops[1].name);\n \tseq_puts(seq_file, \" id=%d\\n\");\n \n+\t/* Degrade element operation */\n+\tseq_puts(seq_file, zloop_ctl_ops[2].name);\n+\tseq_puts(seq_file, \" id=%d,element_id=%d\\n\");\n+\n \treturn 0;\n }\n \ndiff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c\nindex a1b21ea14e549..48c46f4a1ace8 100644\n--- a/drivers/scsi/sd.c\n+++ b/drivers/scsi/sd.c\n@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)\n {\n \tstruct request *rq = scsi_cmd_to_rq(cmd);\n \tstruct scsi_device *sdp = cmd-\u003edevice;\n-\tstruct scsi_disk *sdkp = scsi_disk(rq-\u003eq-\u003edisk);\n+\tstruct gendisk *disk = rq-\u003eq-\u003edisk;\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n \tsector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));\n \tsector_t threshold;\n \tunsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));\n@@ -1456,6 +1457,11 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)\n \t\tgoto fail;\n \t}\n \n+\t/* Prevent read/write to offline zones of ZBC disks. */\n+\tif (sdkp-\u003edevice-\u003etype == TYPE_ZBC \u0026\u0026\n+\t disk_zone_is_offline(disk, blk_rq_pos(rq)))\n+\t\tgoto fail;\n+\n \t/*\n \t * Some SD card readers can't handle accesses which touch the\n \t * last one or two logical blocks. Split accesses as needed.\n@@ -3937,6 +3943,9 @@ static const struct block_device_operations sd_fops = {\n \t.get_unique_id\t\t= sd_get_unique_id,\n \t.free_disk\t\t= scsi_disk_free_disk,\n \t.pr_ops\t\t\t= \u0026sd_pr_ops,\n+#ifdef CONFIG_BLK_DEV_ZONED\n+\t.se_ops\t\t\t= \u0026sd_zbc_se_ops,\n+#endif\n };\n \n /**\ndiff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h\nindex 574af82430169..fc8c49903811e 100644\n--- a/drivers/scsi/sd.h\n+++ b/drivers/scsi/sd.h\n@@ -156,6 +156,7 @@ struct scsi_disk {\n \tunsigned\tignore_medium_access_errors : 1;\n \tunsigned\trscs : 1; /* reduced stream control support */\n \tunsigned\tuse_atomic_write_boundary : 1;\n+\tunsigned\tmodify_zones_supported : 1;\n };\n #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)\n \n@@ -242,6 +243,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,\n int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,\n \t\tunsigned int nr_zones, struct blk_report_zones_args *args);\n \n+extern const struct blk_se_ops sd_zbc_se_ops;\n+\n #else /* CONFIG_BLK_DEV_ZONED */\n \n static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,\ndiff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c\nindex 456beaf2e7690..39cd4f9fcce5c 100644\n--- a/drivers/scsi/sd_zbc.c\n+++ b/drivers/scsi/sd_zbc.c\n@@ -516,6 +516,21 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,\n \treturn 0;\n }\n \n+/*\n+ * sd_zbc_check_modify_zones - Check if the device supports depopulation\n+ * @sdkp: Target disk\n+ * @buf: command buffer\n+ *\n+ * Check if the device supports the REMOVE ELEMENT AND MODIFY ZONES command.\n+ */\n+static inline bool sd_zbc_check_modify_zones(struct scsi_disk *sdkp,\n+\t\t\t\t\t unsigned char *buf)\n+{\n+\treturn scsi_report_opcode(sdkp-\u003edevice, buf, SD_BUF_SIZE,\n+\t\t\t\t SERVICE_ACTION_IN_16,\n+\t\t\t\t SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) == 1;\n+}\n+\n static void sd_zbc_print_zones(struct scsi_disk *sdkp)\n {\n \tif (sdkp-\u003edevice-\u003etype != TYPE_ZBC || !sdkp-\u003ecapacity)\n@@ -533,6 +548,239 @@ static void sd_zbc_print_zones(struct scsi_disk *sdkp)\n \t\t\t sdkp-\u003ezone_info.zone_blocks);\n }\n \n+static void sd_zbc_parse_storage_element(struct scsi_disk *sdkp, u8 *desc,\n+\t\t\t\t\t struct blk_se *element)\n+{\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tsector_t zone_sectors = sd_zbc_zone_sectors(sdkp);\n+\tu64 capacity;\n+\n+\tmemset(element, 0, sizeof(*element));\n+\n+\telement-\u003eid = get_unaligned_be32(\u0026desc[4]);\n+\n+\tswitch (desc[14]) {\n+\tcase SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE:\n+\t\telement-\u003etype = BLK_SE_TYPE_RDWR;\n+\t\tcapacity = get_unaligned_be64(\u0026desc[16]);\n+\t\tif (!zone_sectors || capacity == ULLONG_MAX)\n+\t\t\telement-\u003enr_zones = 0;\n+\t\telse\n+\t\t\telement-\u003enr_zones =\n+\t\t\t\tlogical_to_sectors(sdp, capacity) \u003e\u003e\n+\t\t\t\t\tilog2(zone_sectors);\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:\n+\t\telement-\u003epaired_id = get_unaligned_be32(\u0026desc[16]);\n+\t\tif (desc[20] \u0026 0x01)\n+\t\t\telement-\u003etype = BLK_SE_TYPE_READ;\n+\t\telse\n+\t\t\telement-\u003etype = BLK_SE_TYPE_WRITE;\n+\t\telement-\u003enr_zones = get_unaligned_be64(\u0026desc[24]);\n+\t\tbreak;\n+\tdefault:\n+\t\telement-\u003etype = BLK_SE_TYPE_UNKNOWN;\n+\t}\n+\n+\tswitch (desc[15]) {\n+\tcase SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS:\n+\tcase SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:\n+\t\telement-\u003estatus = BLK_SE_STS_OK;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS:\n+\t\telement-\u003estatus = BLK_SE_STS_DEGRADED;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:\n+\t\telement-\u003estatus = BLK_SE_STS_RESTORE_ERROR;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:\n+\t\telement-\u003estatus = BLK_SE_STS_RESTORE_IN_PROGRESS;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:\n+\t\telement-\u003estatus = BLK_SE_STS_REMOVE_ERROR;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:\n+\t\telement-\u003estatus = BLK_SE_STS_REMOVE_IN_PROGRESS;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:\n+\t\telement-\u003estatus = BLK_SE_STS_REMOVED;\n+\t\telement-\u003erestore_allowed = desc[13] \u0026 0x01;\n+\t\tbreak;\n+\tcase SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:\n+\tdefault:\n+\t\telement-\u003estatus = BLK_SE_STS_UNKNOWN;\n+\t\tbreak;\n+\t}\n+}\n+\n+/*\n+ * Large hard limit on the number of storage elements. This accommodates all\n+ * known devices today and likely forever :)\n+ */\n+#define SD_ZBC_MAX_STORAGE_ELEMENTS\t255\n+\n+static int sd_zbc_report_storage_elements(struct gendisk *disk,\n+\t\t\t\t\t struct blk_se *elements,\n+\t\t\t\t\t unsigned int *nr_elements)\n+{\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n+\tstruct scsi_sense_hdr sshdr;\n+\tconst struct scsi_exec_args exec_args = {\n+\t\t.sshdr = \u0026sshdr,\n+\t};\n+\tunsigned char cmd[16];\n+\tunsigned int nr_descs, nr_se;\n+\tunsigned int buf_size;\n+\tint i, ret = 0, result;\n+\tu8 *desc, *buf;\n+\n+\tif (!sdkp-\u003emodify_zones_supported)\n+\t\treturn -EOPNOTSUPP;\n+\n+\t/*\n+\t * We need at least 32B for the report header and 32B for each\n+\t * descriptor.\n+\t */\n+\tnr_se = min(SD_ZBC_MAX_STORAGE_ELEMENTS, *nr_elements);\n+\tbuf_size = ALIGN((nr_se + 1) * 32, SECTOR_SIZE);\n+\n+\tbuf = kzalloc(buf_size, GFP_NOIO);\n+\tif (!buf)\n+\t\treturn -ENOMEM;\n+\n+\tmemset(cmd, 0, 16);\n+\tcmd[0] = SERVICE_ACTION_IN_16;\n+\tcmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;\n+\tput_unaligned_be32(buf_size, \u0026cmd[10]);\n+\n+\tresult = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buf_size,\n+\t\t\t\t timeout, 1, \u0026exec_args);\n+\tif (result) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"GET PHYSICAL ELEMENT STATUS failed\\n\");\n+\t\tsd_print_result(sdkp, \"GET PHYSICAL ELEMENT STATUS\", result);\n+\t\tif (result \u003e 0 \u0026\u0026 scsi_sense_valid(\u0026sshdr))\n+\t\t\tsd_print_sense_hdr(sdkp, \u0026sshdr);\n+\t\tret = -EIO;\n+\t\tgoto free_buf;\n+\t}\n+\n+\tnr_descs = get_unaligned_be32(\u0026buf[0]);\n+\tif (!nr_descs) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"Invalid number of phys element descriptors\\n\");\n+\t\tret = -EIO;\n+\t\tgoto free_buf;\n+\t}\n+\tif (nr_descs \u003e SD_ZBC_MAX_STORAGE_ELEMENTS) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"Too many phys element descriptors\\n\");\n+\t\tret = -EIO;\n+\t\tgoto free_buf;\n+\t}\n+\n+\tif (!elements) {\n+\t\t*nr_elements = nr_descs;\n+\t\tgoto free_buf;\n+\t}\n+\n+\tnr_descs = get_unaligned_be32(\u0026buf[4]);\n+\tif (!nr_descs) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t\"Invalid number of reported phys element descriptors\\n\");\n+\t\tret = -EIO;\n+\t\tgoto free_buf;\n+\t}\n+\n+\tdesc = \u0026buf[32];\n+\tfor (i = 0; i \u003c min(nr_se, nr_descs); i++, desc += 32)\n+\t\tsd_zbc_parse_storage_element(sdkp, desc, \u0026elements[i]);\n+\t*nr_elements = i;\n+\n+free_buf:\n+\tkfree(buf);\n+\n+\treturn ret;\n+}\n+\n+static int sd_zbc_remove_storage_element(struct gendisk *disk,\n+\t\t\t\t\t unsigned int element_id)\n+{\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n+\tstruct scsi_sense_hdr sshdr;\n+\tconst struct scsi_exec_args exec_args = {\n+\t\t.sshdr = \u0026sshdr,\n+\t};\n+\tunsigned char cmd[16];\n+\tint result;\n+\n+\tif (!sdkp-\u003emodify_zones_supported)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tmemset(cmd, 0, 16);\n+\tcmd[0] = SERVICE_ACTION_IN_16;\n+\tcmd[1] = SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES;\n+\tput_unaligned_be32(element_id, \u0026cmd[10]);\n+\n+\tresult = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,\n+\t\t\t\t timeout, 1, \u0026exec_args);\n+\tif (result) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"REMOVE ELEMENT AND MODIFY ZONES failed\\n\");\n+\t\tsd_print_result(sdkp,\n+\t\t\t\t\"REMOVE ELEMENT AND MODIFY ZONES\", result);\n+\t\tif (result \u003e 0 \u0026\u0026 scsi_sense_valid(\u0026sshdr))\n+\t\t\tsd_print_sense_hdr(sdkp, \u0026sshdr);\n+\t\treturn -EIO;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int sd_zbc_restore_storage_elements(struct gendisk *disk)\n+{\n+\tstruct scsi_disk *sdkp = scsi_disk(disk);\n+\tstruct scsi_device *sdp = sdkp-\u003edevice;\n+\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n+\tstruct scsi_sense_hdr sshdr;\n+\tconst struct scsi_exec_args exec_args = {\n+\t\t.sshdr = \u0026sshdr,\n+\t};\n+\tunsigned char cmd[16];\n+\tint result;\n+\n+\tif (!sdkp-\u003emodify_zones_supported)\n+\t\treturn -EOPNOTSUPP;\n+\n+\tmemset(cmd, 0, 16);\n+\tcmd[0] = SERVICE_ACTION_IN_16;\n+\tcmd[1] = SAI_RESTORE_ELEMENTS_AND_REBUILD;\n+\n+\tresult = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,\n+\t\t\t\t timeout, 1, \u0026exec_args);\n+\tif (result) {\n+\t\tsd_printk(KERN_ERR, sdkp,\n+\t\t\t \"RESTORE ELEMENTS AND REBUILD failed\\n\");\n+\t\tsd_print_result(sdkp,\n+\t\t\t\t\"RESTORE ELEMENTS AND REBUILD\", result);\n+\t\tif (result \u003e 0 \u0026\u0026 scsi_sense_valid(\u0026sshdr))\n+\t\t\tsd_print_sense_hdr(sdkp, \u0026sshdr);\n+\t\treturn -EIO;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+const struct blk_se_ops sd_zbc_se_ops = {\n+\t.report_elements\t= sd_zbc_report_storage_elements,\n+\t.remove_element\t\t= sd_zbc_remove_storage_element,\n+\t.restore_elements\t= sd_zbc_restore_storage_elements,\n+};\n+\n /*\n * Call blk_revalidate_disk_zones() if any of the zoned disk properties have\n * changed that make it necessary to call that function. Called by\n@@ -554,9 +802,15 @@ int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\n \tif (!blk_queue_is_zoned(q))\n \t\treturn 0;\n \n+\t/*\n+\t * If the zone size and number of zones has not changed, and the disk\n+\t * does not support depopulating heads, skip the rather slow call to\n+\t * blk_revalidate_disk_zones().\n+\t */\n \tif (sdkp-\u003ezone_info.zone_blocks == zone_blocks \u0026\u0026\n \t sdkp-\u003ezone_info.nr_zones == nr_zones \u0026\u0026\n-\t disk-\u003enr_zones == nr_zones)\n+\t disk-\u003enr_zones == nr_zones \u0026\u0026\n+\t !sdkp-\u003emodify_zones_supported)\n \t\treturn 0;\n \n \tsdkp-\u003ezone_info.zone_blocks = zone_blocks;\n@@ -620,6 +874,9 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,\n \tif (ret != 0)\n \t\tgoto err;\n \n+\t/* Check if REMOVE ELEMENT AND MODIFY ZONES is supported. */\n+\tsdkp-\u003emodify_zones_supported = sd_zbc_check_modify_zones(sdkp, buf);\n+\n \tnr_zones = round_up(sdkp-\u003ecapacity, zone_blocks) \u003e\u003e ilog2(zone_blocks);\n \tif (nr_zones \u003e INT_MAX) {\n \t\tsd_printk(KERN_ERR, sdkp, \"Too many zones (%llu)\\n\",\ndiff --git a/include/linux/blkdev.h b/include/linux/blkdev.h\nindex d003a9d2d1f6c..20b77c6b451fc 100644\n--- a/include/linux/blkdev.h\n+++ b/include/linux/blkdev.h\n@@ -189,6 +189,11 @@ struct gendisk {\n \tstruct disk_events *ev;\n \n #ifdef CONFIG_BLK_DEV_ZONED\n+\t/*\n+\t * Total number of storage elements of the disk. This is always 0 for\n+\t * disks that do not support storage element management.\n+\t */\n+\tunsigned int\t\tnr_storage_elements;\n \t/*\n \t * Zoned block device information. Reads of this information must be\n \t * protected with blk_queue_enter() / blk_queue_exit() or by holding a\n@@ -942,6 +947,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,\n \t\treturn disk-\u003elast_zone_capacity;\n \treturn disk-\u003ezone_capacity;\n }\n+\n+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector);\n+\n static inline unsigned int bdev_zone_capacity(struct block_device *bdev,\n \t\t\t\t\t sector_t pos)\n {\n@@ -956,6 +964,12 @@ static inline unsigned int disk_nr_zones(struct gendisk *disk)\n \treturn 0;\n }\n \n+static inline bool disk_zone_is_offline(struct gendisk *disk,\n+\t\t\t\t\tsector_t sector)\n+{\n+\treturn false;\n+}\n+\n static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)\n {\n \treturn false;\n@@ -1574,6 +1588,22 @@ enum blk_unique_id {\n \tBLK_UID_NAA\t= 3,\n };\n \n+/* Storage element management operations. */\n+struct blk_se_ops {\n+\tint (*report_elements)(struct gendisk *disk,\n+\t\t\t struct blk_se *elements,\n+\t\t\t unsigned int *nr_elements);\n+\tint (*remove_element)(struct gendisk *disk, unsigned int element_id);\n+\tint (*restore_elements)(struct gendisk *disk);\n+};\n+\n+int bdev_report_storage_elements(struct block_device *bdev,\n+\t\t\t\t struct blk_se *elements,\n+\t\t\t\t unsigned int *nr_elements);\n+int bdev_remove_storage_element(struct block_device *bdev,\n+\t\t\t\tunsigned int element_id);\n+int bdev_restore_storage_elements(struct block_device *bdev);\n+\n struct block_device_operations {\n \tvoid (*submit_bio)(struct bio *bio);\n \tint (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,\n@@ -1601,6 +1631,7 @@ struct block_device_operations {\n \t\t\tenum blk_unique_id id_type);\n \tstruct module *owner;\n \tconst struct pr_ops *pr_ops;\n+\tconst struct blk_se_ops\t*se_ops;\n \n \t/*\n \t * Special callback for probing GPT entry at a given sector.\ndiff --git a/include/uapi/linux/blkzoned.h b/include/uapi/linux/blkzoned.h\nindex 6638361209667..e6c60fba6ac9c 100644\n--- a/include/uapi/linux/blkzoned.h\n+++ b/include/uapi/linux/blkzoned.h\n@@ -208,4 +208,86 @@ struct blk_zone_range {\n #define BLKFINISHZONE\t_IOW(0x12, 136, struct blk_zone_range)\n #define BLKREPORTZONEV2\t_IOWR(0x12, 142, struct blk_zone_report)\n \n+/**\n+ * enum blk_se_type - Type of a zoned device storage element.\n+ *\n+ * @BLK_SE_TYPE_RDWR: The storage element handles both reads and writes.\n+ * @BLK_SE_TYPE_READ: The storage element handles reads only.\n+ * @BLK_SE_TYPE_WRITE: The storage element handles writes only.\n+ * @BLK_SE_TYPE_UNKNOWN: The storage element type is not known.\n+ */\n+enum blk_se_type {\n+\tBLK_SE_TYPE_RDWR\t\t= 0x01,\n+\tBLK_SE_TYPE_READ\t\t= 0x02,\n+\tBLK_SE_TYPE_WRITE\t\t= 0x03,\n+\tBLK_SE_TYPE_UNKNOWN\t\t= 0xFF,\n+};\n+\n+/**\n+ * enum blk_se_status - Status of a zoned device storage element.\n+ *\n+ * @BLK_SE_STS_OK: The storage element is operating normally.\n+ * @BLK_SE_STS_DEGRADED: The storage element has degraded and is not operating\n+ *\t\t\t normally.\n+ * @BLK_SE_STS_REMOVE_IN_PROGRESS: The storage element is being removed.\n+ * @BLK_SE_STS_REMOVE_ERROR: The storage element removal failed.\n+ * @BLK_SE_STS_RESTORE_IN_PROGRESS: The storage element is being restored.\n+ * @BLK_SE_STS_RESTORE_ERROR: The storage element restoration failed.\n+ * @BLK_SE_STS_REMOVED: The storage element was removed.\n+ * @BLK_SE_STS_UNKNOWN: The storage element status is unknown.\n+ */\n+enum blk_se_status {\n+\tBLK_SE_STS_OK\t\t\t= 0x01,\n+\tBLK_SE_STS_DEGRADED\t\t= 0x02,\n+\tBLK_SE_STS_REMOVE_IN_PROGRESS\t= 0x03,\n+\tBLK_SE_STS_REMOVE_ERROR\t\t= 0x04,\n+\tBLK_SE_STS_RESTORE_IN_PROGRESS\t= 0x05,\n+\tBLK_SE_STS_RESTORE_ERROR\t= 0x06,\n+\tBLK_SE_STS_REMOVED\t\t= 0x07,\n+\tBLK_SE_STS_UNKNOWN\t\t= 0xFF,\n+};\n+\n+/**\n+ * struct blk_se - Zoned device storage element descriptor.\n+ *\n+ * @id: The ID of the element (cannot be 0).\n+ * @paired_id: The ID of the paired element for an element that is not\n+ *\t of type BLK_SE_TYPE_RDWR.\n+ * @nr_zones: The number of zones that the storage element handles.\n+ * @type: The type of the storage element (enum blk_se_type).\n+ * @status: The health status of the storage element\n+ *\t (enum blk_se_status).\n+ * @restore_allowed: Indicate if the storage element can be restored.\n+ * @reserved: unused field to align this structure size to 32B\n+ */\n+struct blk_se {\n+\t__u32\tid;\n+\t__u32\tpaired_id;\n+\t__u64\tnr_zones;\n+\t__u8\ttype;\n+\t__u8\tstatus;\n+\t__u8\trestore_allowed;\n+\t__u8\treserved[13];\n+};\n+\n+struct blk_se_report {\n+\t__u32\t\tnr_elements;\n+\t__u32\t\treserved;\n+\tstruct blk_se\telements[];\n+};\n+\n+/**\n+ * Zoned block device storage element management ioctl's:\n+ *\n+ * @BLKGETNRSTORELEMS: Get the number of storage elements of the device.\n+ * @BLKREPORTSTORELEMS: Get the device storage elements.\n+ * @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device.\n+ * @BLKRESTORESTORELEM: Restore (repopulate if possible) all storage elements\n+ *\t\t\tthat have been removed.\n+ */\n+#define BLKGETNRSTORELEMS _IOR(0x12, 143, __u32)\n+#define BLKREPORTSTORELEMS _IOWR(0x12, 144, struct blk_se_report)\n+#define BLKREMOVESTORELEM _IOW(0x12, 145, __u32)\n+#define BLKRESTORESTORELEMS _IO(0x12, 146)\n+\n #endif /* _UAPI_BLKZONED_H */\ndiff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h\nindex 34c6f219462a5..8a979326aa7f0 100644\n--- a/include/uapi/linux/fs.h\n+++ b/include/uapi/linux/fs.h\n@@ -309,6 +309,7 @@ struct file_attr {\n /* 130-136 and 142 are used by zoned block device ioctls (uapi/linux/blkzoned.h) */\n /* 137-141 are used by blk-crypto ioctls (uapi/linux/blk-crypto.h) */\n #define BLKTRACESETUP2 _IOWR(0x12, 142, struct blk_user_trace_setup2)\n+/* 143-146 are used by storage element management for zoned block devices. */\n \n #define BMAP_IOCTL 1\t\t/* obsolete - kept for compatibility */\n #define FIBMAP\t _IO(0x00,1)\t/* bmap access */\n"
} |
| 2/1 | 2026/10/09 11:30 | agent | patch-evaluator |
3mModel:core Args: null Results: {
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blkdev_zone_storage_elements_ioctl",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_remove_element_work",
"zloop_restore_elements_work",
"zloop_ctl_degrade_element",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements"
],
"Reasoning": "The patch series introduces storage element management for zoned block devices, adding new UAPI ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS), core block layer handlers in block/blk-zoned.c, storage element emulation and control operations in the virtual zoned loop driver (zloop), and SCSI ZBC support in sd_zbc.c. The changes modify reachable block layer logic, introduce delayed work handlers, queue operations, and state machine transitions reachable in standard virtualized environments.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.
IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.
================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.
- REACHABILITY IS THE PRIMARY GATE:
Fuzzing can only discover bugs in code that can actually execute in standard virtualized
environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
regardless of whether it adds assertions or complex logic.
- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
Patch authors routinely label changes as "cleanups", "refactorings", or state
"No functional change intended". Do NOT take these claims at face value.
Code refactorings that rearrange logic, introduce helper functions, or alter state management
in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
If reachable executable code is modified or refactored, it MUST be fuzzed.
- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
Even if the author believes the invariant always holds, fuzzing is essential to verify whether
an unusual sequence of operations can violate it.
================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:
- Non-kernel and non-executable changes:
* Modifications to Documentation/, comments, or spelling fixes.
* User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
that do not affect the compiled kernel image (vmlinux) or kernel modules.
* Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
that do not alter control flow or data structures.
* Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
* Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
* Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
physical PCI hot-unplug or manual sysfs driver unbinding.
* Code paths exclusive to architectures other than the target architecture.
================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
* Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
* Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
* Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
* Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).
================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:
- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
Do NOT list generic, ubiquitous functions called by almost every program in the corpus
(including, but not limited to: general memory allocators and deallocators, page fault
and trap handlers, or core synchronization primitives; this is not an exhaustive list).
Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
which severely dilutes fuzzing effort away from the actual changes.
- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
List functions that specifically implement the logic being added or altered, or direct API entrypoints
for the subsystem feature under review.
- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).
================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit ab617d08dddf09f15f0a18d1adcd42c4ab1499e3
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Oct 9 11:30:22 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/blockdev/zoned_loop.rst b/Documentation/admin-guide/blockdev/zoned_loop.rst
index 64277494fb36a..ec39f141af8d6 100644
--- a/Documentation/admin-guide/blockdev/zoned_loop.rst
+++ b/Documentation/admin-guide/blockdev/zoned_loop.rst
@@ -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
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 19268afb8752e..10fb3ca5adcd6 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -18,6 +18,8 @@
#include <linux/mempool.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
+#include <linux/delay.h>
+#include <linux/uaccess.h>
#include <trace/events/block.h>
@@ -292,6 +294,22 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
return !blk_zstate_is_conv(zs);
}
+/**
+ * disk_zone_is_offline - check if a sector belongs to an offline zone
+ * @disk: gendisk to check
+ * @sector: sector number
+ *
+ * Check if @sector on @disk is contained in an offline zone.
+ */
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)
+{
+ if (!blk_queue_is_zoned(disk->queue))
+ return false;
+
+ return disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;
+}
+EXPORT_SYMBOL_GPL(disk_zone_is_offline);
+
/**
* bdev_zone_is_seq - check if a sector belongs to a sequential write zone
* @bdev: block device to check
@@ -2526,20 +2544,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
return ret;
}
-/**
- * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
- * @disk: Target disk
- *
- * Helper function for low-level device drivers to check, (re) allocate and
- * initialize resources used for managing zoned disks. This function should
- * normally be called by blk-mq based drivers when a zoned gendisk is probed
- * and when the zone configuration of the gendisk changes (e.g. after a format).
- * Before calling this function, the device driver must already have set the
- * device zone size (chunk_sector limit) and the max zone append limit.
- * BIO based drivers can also use this function as long as the device queue
- * can be safely frozen.
- */
-int blk_revalidate_disk_zones(struct gendisk *disk)
+static int disk_revalidate_zones(struct gendisk *disk)
{
struct blk_revalidate_zone_args args = { };
struct blk_report_zones_args rep_args = {
@@ -2550,19 +2555,10 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
int retries = 2;
int ret;
- if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
- return -EIO;
-
- /*
- * Serialize calls to this function so that we can safely look at and
- * eventually change the disk zone information.
- */
- mutex_lock(&disk->zone_revalidate_mutex);
-
again:
ret = disk_revalidate_capacity(disk, &args);
if (ret)
- goto unlock;
+ return ret;
/*
* Allocate zone resources if they are needed and we have not done
@@ -2575,14 +2571,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
ret = disk_alloc_zone_resources(disk, args.capacity);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
}
ret = disk_init_revalidate_args(disk, &args);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
ret = disk->fops->report_zones(disk, 0, UINT_MAX, &rep_args);
@@ -2599,8 +2595,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
if (ret)
goto free_args;
- mutex_unlock(&disk->zone_revalidate_mutex);
-
return 0;
free_args:
@@ -2616,7 +2610,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
}
pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
-unlock:
+
+ return ret;
+}
+
+/**
+ * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
+ * @disk: Target disk
+ *
+ * Helper function for low-level device drivers to check, (re) allocate and
+ * initialize resources used for managing zoned disks. This function should
+ * normally be called by blk-mq based drivers when a zoned gendisk is probed
+ * and when the zone configuration of the gendisk changes (e.g. after a format).
+ * Before calling this function, the device driver must already have set the
+ * device zone size (chunk_sector limit) and the max zone append limit.
+ * BIO based drivers can also use this function as long as the device queue
+ * can be safely frozen.
+ */
+int blk_revalidate_disk_zones(struct gendisk *disk)
+{
+ int ret;
+
+ if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
+ return -EIO;
+
+ /*
+ * Serialize calls to this function so that we can safely look at and
+ * eventually change the disk zone information.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+ ret = disk_revalidate_zones(disk);
mutex_unlock(&disk->zone_revalidate_mutex);
return ret;
@@ -2700,5 +2723,408 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)
return 0;
}
-
#endif
+
+static int disk_se_mgmt_in_progress(struct gendisk *disk,
+ struct blk_se *elements,
+ bool *in_progress)
+{
+ unsigned int i, nr_se = disk->nr_storage_elements;
+ int ret;
+
+ /*
+ * Check if we have storage elements being removed or restored.
+ */
+ ret = disk->fops->se_ops->report_elements(disk, elements, &nr_se);
+ if (ret) {
+ pr_err("Failed to get storage elements\n");
+ return ret;
+ }
+
+ for (i = 0; i < nr_se; i++) {
+ switch (elements[i].status) {
+ case BLK_SE_STS_REMOVE_IN_PROGRESS:
+ case BLK_SE_STS_RESTORE_IN_PROGRESS:
+ *in_progress = true;
+ return 0;
+ default:
+ break;
+ }
+ }
+
+ *in_progress = false;
+
+ return 0;
+}
+
+static int disk_wait_for_se_mgmt_completion(struct gendisk *disk)
+{
+ struct blk_se *elements;
+ unsigned int noio_flag;
+ bool in_progress;
+ int ret;
+
+ noio_flag = memalloc_noio_save();
+ elements = kzalloc_objs(struct blk_se, disk->nr_storage_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 check
+ * for fatal signals so that the user can terminate this retry loop if
+ * the device misbehaves.
+ */
+ while (1) {
+ ret = disk_se_mgmt_in_progress(disk, elements, &in_progress);
+ if (ret || !in_progress)
+ break;
+
+ if (fatal_signal_pending(current)) {
+ ret = -EINTR;
+ break;
+ }
+
+ /* Not done yet: wait and retry. */
+ msleep(1000);
+ }
+
+ kfree(elements);
+
+ return ret;
+}
+
+/**
+ * bdev_report_storage_elements - report the storage elements of a zoned
+ * block device
+ * @bdev: the zoned block device to check
+ * @elements: array of storage element descriptors to fill
+ * @nr_elements: maximum size of @elements
+ *
+ * Fill at most @nr_elements storage element descriptors in the array
+ * @elements. The number of storage elements filled in the array is returned
+ * using @nr_elements. If @elements is NULL, only @nr_elements is returned to
+ * indicate the maximum number of storage elements of @bdev.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ if (!nr_elements)
+ return -EINVAL;
+
+ if (!elements) {
+ *nr_elements = disk->nr_storage_elements;
+ return 0;
+ }
+
+ *nr_elements = min(*nr_elements, disk->nr_storage_elements);
+ if (!*nr_elements)
+ return -EINVAL;
+
+ return disk->fops->se_ops->report_elements(disk, elements,
+ nr_elements);
+}
+EXPORT_SYMBOL_GPL(bdev_report_storage_elements);
+
+static int blkdev_get_nr_storage_elements_ioctl(struct block_device *bdev,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (put_user(disk->nr_storage_elements, (unsigned int __user *)argp))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int blkdev_report_storage_elements_ioctl(struct block_device *bdev,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ struct blk_se_report *rep;
+ int ret;
+
+ if (!argp)
+ return -EINVAL;
+
+ rep = kzalloc_flex(*rep, elements, disk->nr_storage_elements);
+ if (!rep)
+ return -ENOMEM;
+
+ if (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {
+ ret = -EFAULT;
+ goto free_rep;
+ }
+
+ rep->nr_elements = min(rep->nr_elements, disk->nr_storage_elements);
+ if (!rep->nr_elements) {
+ ret = -EINVAL;
+ goto free_rep;
+ }
+
+ ret = bdev_report_storage_elements(bdev, rep->elements,
+ &rep->nr_elements);
+ if (ret)
+ goto free_rep;
+
+ if (copy_to_user(argp, rep, struct_size_t(struct blk_se_report,
+ elements, rep->nr_elements)))
+ ret = -EFAULT;
+
+free_rep:
+ kfree(rep);
+ return ret;
+}
+
+/**
+ * bdev_remove_storage_element - Remove (depopulate) a storage element of a
+ * zoned block device
+ * @bdev: block device to operate on
+ * @element_id: ID of the storage element to remove
+ *
+ * Remove (depopulate) the storage element identified by @element_id from the
+ * block device @bdev. The caller is responsible for taking care of any
+ * necessary device write cache flush and invalidation of cached data for the
+ * zones that will be offlined.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /* Zero is not a valid storage element ID. */
+ if (!element_id)
+ return -EINVAL;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding command.
+ * The caller is responsible for not queuing up more I/Os by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ ret = disk->fops->se_ops->remove_element(disk, element_id);
+ if (ret)
+ goto unlock;
+
+ /* Revalidate the device zones once the opration completes. */
+ ret = disk_wait_for_se_mgmt_completion(disk);
+ if (ret)
+ goto unlock;
+
+ ret = disk_revalidate_zones(disk);
+
+unlock:
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_remove_storage_element);
+
+static int blkdev_flush_and_truncate(struct block_device *bdev,
+ blk_mode_t mode)
+{
+ int ret;
+
+ filemap_invalidate_lock(bdev->bd_mapping);
+ ret = blkdev_issue_flush(bdev->bd_disk->part0);
+ if (!ret)
+ ret = truncate_bdev_range(bdev, mode, 0,
+ bdev_nr_bytes(bdev) - 1);
+ filemap_invalidate_unlock(bdev->bd_mapping);
+
+ return ret;
+}
+
+static int blkdev_remove_storage_element_ioctl(struct block_device *bdev,
+ blk_mode_t mode, void __user *argp)
+{
+ unsigned int element_id;
+ int ret;
+
+ if (!(mode & BLK_OPEN_WRITE))
+ return -EBADF;
+ if (bdev_read_only(bdev))
+ return -EPERM;
+
+ if (get_user(element_id, (unsigned int __user *)argp))
+ return -EFAULT;
+
+ 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.
+ */
+ ret = blkdev_flush_and_truncate(bdev, mode);
+ 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 zoned block device
+ * @bdev: block device to operate on
+ *
+ * Restore all storage elements of @bdev that have been depopulated. Not all
+ * elements may be restored by this operation.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_restore_storage_elements(struct block_device *bdev)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding commands.
+ * The caller is responsible for not queuing up more I/O by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ ret = disk->fops->se_ops->restore_elements(disk);
+ if (ret)
+ goto unlock;
+
+ ret = disk_wait_for_se_mgmt_completion(disk);
+ if (ret)
+ goto unlock;
+
+ ret = disk_revalidate_zones(disk);
+
+unlock:
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+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.
+ */
+ ret = blkdev_flush_and_truncate(bdev, mode);
+ if (!ret)
+ ret = bdev_restore_storage_elements(bdev);
+
+ inode_unlock(bdev->bd_mapping->host);
+
+ return ret;
+}
+
+int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ int ret;
+
+ /*
+ * Storage element operations are for zoned disks only and
+ * optional...
+ */
+ if (!blk_queue_is_zoned(disk->queue) || !disk->fops->se_ops)
+ return 0;
+
+ /* ...but if supported, all operations must be defined. */
+ if (!disk->fops->se_ops->report_elements ||
+ !disk->fops->se_ops->remove_element ||
+ !disk->fops->se_ops->restore_elements) {
+ pr_err("%s: invalid storage element operations\n",
+ disk->disk_name);
+ return -EINVAL;
+ }
+
+ /*
+ * Get the number of storage elements of the disk. If this fails,
+ * do not report an error as the failure may be because the zoned disk
+ * does not support storage element management.
+ */
+ ret = disk->fops->se_ops->report_elements(disk, NULL,
+ &disk->nr_storage_elements);
+ if (ret)
+ disk->nr_storage_elements = 0;
+
+ return 0;
+}
+
+int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (!disk->nr_storage_elements)
+ return -ENOTTY;
+
+ switch (cmd) {
+ case BLKGETNRSTORELEMS:
+ return blkdev_get_nr_storage_elements_ioctl(bdev, argp);
+ case BLKREPORTSTORELEMS:
+ return blkdev_report_storage_elements_ioctl(bdev, argp);
+ case BLKREMOVESTORELEM:
+ return blkdev_remove_storage_element_ioctl(bdev, mode, argp);
+ case BLKRESTORESTORELEMS:
+ return blkdev_restore_storage_elements_ioctl(bdev, mode);
+ default:
+ break;
+ }
+
+ return -ENOTTY;
+}
diff --git a/block/blk.h b/block/blk.h
index 2cc03aa54c532..bbf523c9579cf 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -527,6 +527,7 @@ static inline void ioc_clear_queue(struct request_queue *q)
#endif /* CONFIG_BLK_ICQ */
#ifdef CONFIG_BLK_DEV_ZONED
+int disk_init_storage_element_mgmt(struct gendisk *disk);
void disk_init_zone_resources(struct gendisk *disk);
void disk_release_zone_resources(struct gendisk *disk);
static inline bool bio_zone_write_plugging(struct bio *bio)
@@ -578,7 +579,14 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
unsigned int cmd, unsigned long arg);
bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector);
+int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp);
#else /* CONFIG_BLK_DEV_ZONED */
+static inline int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ return 0;
+}
static inline void disk_init_zone_resources(struct gendisk *disk)
{
}
@@ -625,6 +633,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev,
{
return false;
}
+static inline int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ return -ENOTTY;
+}
#endif /* CONFIG_BLK_DEV_ZONED */
struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
diff --git a/block/genhd.c b/block/genhd.c
index 10ca8b4d6eea7..e2cef791ac91a 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS))
return -EINVAL;
+ ret = disk_init_storage_element_mgmt(disk);
+ if (ret)
+ return ret;
+
if (queue_is_mq(disk->queue)) {
/*
* ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.
diff --git a/block/ioctl.c b/block/ioctl.c
index 64b4e6c0f6965..ccf806c2e37d1 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -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 */
diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 394dc2408ed73..a1c9f7a960cfd 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -37,6 +37,8 @@ enum {
ZLOOP_OPT_ORDERED_ZONE_APPEND = (1 << 10),
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 = {
@@ -51,11 +53,23 @@ static const match_table_t zloop_opt_tokens = {
{ ZLOOP_OPT_BUFFERED_IO, "buffered_io" },
{ ZLOOP_OPT_ZONE_APPEND, "zone_append=%u" },
{ ZLOOP_OPT_ORDERED_ZONE_APPEND, "ordered_zone_append" },
- { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" },
+ { 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 }
};
+/* Storage elements emulation types. */
+enum zloop_stor_elements {
+ /* No emulation. */
+ ZLOOP_STOR_ELEMENTS_NONE,
+ /* Emulate read+write storage elements. */
+ ZLOOP_STOR_ELEMENTS_RDWR,
+ /* Emulate pairs of associated read and write storage elements. */
+ ZLOOP_STOR_ELEMENTS_PAIRS,
+};
+
/* Default values for the "add" operation. */
#define ZLOOP_DEF_ID -1
#define ZLOOP_DEF_ZONE_SIZE ((256ULL * SZ_1M) >> SECTOR_SHIFT)
@@ -68,6 +82,8 @@ static const match_table_t zloop_opt_tokens = {
#define ZLOOP_DEF_BUFFERED_IO false
#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
@@ -87,6 +103,8 @@ struct zloop_options {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
+ unsigned int element_id;
};
/*
@@ -117,6 +135,8 @@ struct zloop_zone {
enum blk_zone_cond cond;
sector_t start;
sector_t wp;
+ unsigned int wr_se_id;
+ unsigned int rd_se_id;
gfp_t old_gfp_mask;
};
@@ -133,6 +153,7 @@ struct zloop_device {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
const char *base_dir;
struct file *data_dir;
@@ -150,6 +171,17 @@ struct zloop_device {
struct list_head open_zones_lru_list;
unsigned int nr_open_zones;
+ /* For storage elements emulation. */
+ struct mutex stor_elements_lock;
+ unsigned int nr_elements;
+ unsigned int max_nr_removed_elements;
+ unsigned int nr_removed_elements;
+ struct delayed_work remove_element_work;
+ unsigned int remove_element_id;
+ struct delayed_work restore_elements_work;
+ bool restore_in_progress;
+ struct blk_se *elements;
+
struct zloop_zone zones[] __counted_by(nr_zones);
};
@@ -289,22 +321,43 @@ static bool zloop_do_open_zone(struct zloop_device *zlo,
}
}
-static void zloop_mark_full(struct zloop_device *zlo, struct zloop_zone *zone)
+static void zloop_set_zone_cond(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ enum blk_zone_cond cond)
{
lockdep_assert_held(&zone->wp_lock);
zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_FULL;
- zone->wp = ULLONG_MAX;
+ zone->cond = cond;
+ if (cond == BLK_ZONE_COND_EMPTY)
+ zone->wp = zone->start;
+ else
+ zone->wp = ULLONG_MAX;
}
-static void zloop_mark_empty(struct zloop_device *zlo, struct zloop_zone *zone)
+static inline void zloop_set_zone_full(struct zloop_device *zlo,
+ struct zloop_zone *zone)
{
- lockdep_assert_held(&zone->wp_lock);
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);
+}
- zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_EMPTY;
- zone->wp = zone->start;
+static inline void zloop_set_zone_empty(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);
+}
+
+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_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
@@ -316,6 +369,13 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
lockdep_assert_held(&zone->lock);
+ /*
+ * If the zone was already changed to offline or read-only, we have
+ * nothing to do.
+ */
+ if (zloop_zone_is_offline_or_readonly(zlo, zone))
+ return 0;
+
ret = vfs_getattr(&zone->file->f_path, &stat, STATX_SIZE, 0);
if (ret < 0) {
pr_err("Failed to get zone %u file stat (err=%d)\n",
@@ -339,9 +399,9 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
spin_lock(&zone->wp_lock);
if (!file_sectors) {
- zloop_mark_empty(zlo, zone);
+ zloop_set_zone_empty(zlo, zone);
} else if (file_sectors == zlo->zone_capacity) {
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
} else {
if (zone->cond != BLK_ZONE_COND_IMP_OPEN &&
zone->cond != BLK_ZONE_COND_EXP_OPEN)
@@ -363,6 +423,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);
if (ret)
@@ -388,6 +453,11 @@ static int zloop_close_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);
if (ret)
@@ -420,7 +490,22 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)
return ret;
}
-static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)
+static int zloop_do_reset_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ if (vfs_truncate(&zone->file->f_path, 0))
+ return -EIO;
+
+ spin_lock(&zone->wp_lock);
+ zloop_set_zone_empty(zlo, zone);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ spin_unlock(&zone->wp_lock);
+
+ return 0;
+}
+
+static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,
+ bool all_zones)
{
struct zloop_zone *zone = &zlo->zones[zone_no];
int ret = 0;
@@ -430,20 +515,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;
- if (vfs_truncate(&zone->file->f_path, 0)) {
+ ret = zloop_do_reset_zone(zlo, zone);
+ if (ret)
set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- ret = -EIO;
- goto unlock;
- }
-
- spin_lock(&zone->wp_lock);
- zloop_mark_empty(zlo, zone);
- clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- spin_unlock(&zone->wp_lock);
unlock:
mutex_unlock(&zone->lock);
@@ -457,7 +541,7 @@ static int zloop_reset_all_zones(struct zloop_device *zlo)
int ret;
for (i = zlo->nr_conv_zones; i < zlo->nr_zones; i++) {
- ret = zloop_reset_zone(zlo, i);
+ ret = zloop_reset_zone(zlo, i, true);
if (ret)
return ret;
}
@@ -475,6 +559,11 @@ static int zloop_finish_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_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags) &&
zone->cond == BLK_ZONE_COND_FULL)
goto unlock;
@@ -487,7 +576,7 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)
}
spin_lock(&zone->wp_lock);
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
spin_unlock(&zone->wp_lock);
@@ -624,7 +713,7 @@ static int zloop_seq_write_prep(struct zloop_cmd *cmd)
if (!is_append || !zlo->ordered_zone_append) {
zone->wp += nr_sectors;
if (zone->wp == zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
}
out_unlock:
spin_unlock(&zone->wp_lock);
@@ -766,7 +855,7 @@ static void zloop_handle_cmd(struct zloop_cmd *cmd)
cmd->ret = zloop_flush(zlo);
break;
case REQ_OP_ZONE_RESET:
- cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq));
+ cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq), false);
break;
case REQ_OP_ZONE_RESET_ALL:
cmd->ret = zloop_reset_all_zones(zlo);
@@ -875,30 +964,72 @@ static void zloop_complete_rq(struct request *rq)
blk_mq_end_request(rq, sts);
}
-static bool zloop_set_zone_append_sector(struct request *rq)
+static bool zloop_set_zone_append_sector(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ struct request *rq)
{
- struct zloop_device *zlo = rq->q->queuedata;
- unsigned int zone_no = rq_zone_no(rq);
- struct zloop_zone *zone = &zlo->zones[zone_no];
sector_t zone_end = zone->start + zlo->zone_capacity;
sector_t nr_sectors = blk_rq_sectors(rq);
- spin_lock(&zone->wp_lock);
-
if (zone->cond == BLK_ZONE_COND_FULL ||
- zone->wp + nr_sectors > zone_end) {
- spin_unlock(&zone->wp_lock);
+ zone->wp + nr_sectors > zone_end)
return false;
- }
rq->__sector = zone->wp;
zone->wp += blk_rq_sectors(rq);
if (zone->wp >= zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
+
+ return true;
+}
+
+
+static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)
+{
+ struct zloop_zone *zone = &zlo->zones[rq_zone_no(rq)];
+ bool is_write = op_is_write(req_op(rq));
+ bool ret = true;
+
+ spin_lock(&zone->wp_lock);
+
+ if (zlo->nr_elements) {
+ struct blk_se *se;
+
+ /*
+ * Check the zone condition and the health state of the storage
+ * element serving the zone: do not allow any operation to
+ * offline zones or if the storage element serving the zone is
+ * being removed, and allow only reads to read-only zones.
+ */
+ if (is_write)
+ se = &zlo->elements[zone->wr_se_id - 1];
+ else
+ se = &zlo->elements[zone->rd_se_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_DEGRADED ||
+ READ_ONCE(se->status) == BLK_SE_STS_REMOVE_IN_PROGRESS) {
+ ret = false;
+ goto unlock;
+ }
+
+ if (zone->cond == BLK_ZONE_COND_OFFLINE ||
+ (zone->cond == BLK_ZONE_COND_READONLY && is_write)) {
+ ret = false;
+ goto unlock;
+ }
+ }
+
+ /*
+ * If we need to strongly order zone append operations, set the request
+ * sector to the zone write pointer location now instead of when the
+ * command work runs.
+ */
+ if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND)
+ ret = zloop_set_zone_append_sector(zlo, zone, rq);
+unlock:
spin_unlock(&zone->wp_lock);
- return true;
+ return ret;
}
static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
@@ -914,13 +1045,21 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
}
/*
- * If we need to strongly order zone append operations, set the request
- * sector to the zone write pointer location now instead of when the
- * command work runs.
+ * Do not allow commands while the restoration of storage elements is
+ * in progress.
*/
- if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND) {
- if (!zloop_set_zone_append_sector(rq))
+ if (READ_ONCE(zlo->restore_in_progress))
+ return BLK_STS_IOERR;
+
+ switch (req_op(rq)) {
+ case REQ_OP_READ:
+ case REQ_OP_WRITE:
+ case REQ_OP_ZONE_APPEND:
+ if (!zloop_prep_rq(zlo, rq))
return BLK_STS_IOERR;
+ break;
+ default:
+ break;
}
blk_mq_start_request(rq);
@@ -1002,11 +1141,299 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
return nr_zones;
}
+static int zloop_report_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (!elements) {
+ *nr_elements = zlo->nr_elements;
+ goto unlock;
+ }
+
+ *nr_elements = min(*nr_elements, zlo->nr_elements);
+ for (i = 0, se = zlo->elements; i < *nr_elements; i++, se++) {
+ switch (READ_ONCE(se->status)) {
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ se->restore_allowed = 1;
+ break;
+ default:
+ se->restore_allowed = 0;
+ }
+ memcpy(&elements[i], se, sizeof(struct blk_se));
+ }
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return 0;
+}
+
+static int zloop_degrade_element(struct zloop_device *zlo,
+ unsigned int element_id)
+{
+ struct blk_se *se;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (!element_id || element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_OK)
+ WRITE_ONCE(se->status, BLK_SE_STS_DEGRADED);
+ else
+ ret = -EINVAL;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static void zloop_remove_element_work(struct work_struct *work)
+{
+ struct zloop_device *zlo = container_of(work, struct zloop_device,
+ remove_element_work.work);
+ struct blk_se *se, *paired_se = NULL;
+ enum blk_zone_cond cond;
+ struct zloop_zone *zone;
+ unsigned int i;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /*
+ * If the element to remove is a read element, zones must go offline and
+ * the associated write element is also removed.
+ */
+ se = &zlo->elements[zlo->remove_element_id - 1];
+ switch (se->type) {
+ case BLK_SE_TYPE_RDWR:
+ cond = BLK_ZONE_COND_OFFLINE;
+ break;
+ case BLK_SE_TYPE_READ:
+ cond = BLK_ZONE_COND_OFFLINE;
+ paired_se = &zlo->elements[se->paired_id - 1];
+ break;
+ case BLK_SE_TYPE_WRITE:
+ cond = BLK_ZONE_COND_READONLY;
+ break;
+ default:
+ WARN_ON_ONCE(1);
+ }
+
+ /*
+ * Change the condition of the zones owned by the (pair of) elements
+ * being removed and mark the elements removed.
+ */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ if (zone->wr_se_id != se->id && zone->rd_se_id != se->id)
+ continue;
+ mutex_lock(&zone->lock);
+ spin_lock(&zone->wp_lock);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ zloop_set_zone_cond(zlo, zone, cond);
+ spin_unlock(&zone->wp_lock);
+ mutex_unlock(&zone->lock);
+ }
+
+ WRITE_ONCE(se->status, BLK_SE_STS_REMOVED);
+ zlo->nr_removed_elements++;
+ 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++;
+ }
+
+ zlo->remove_element_id = 0;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+}
+
+static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se, *paired_se = NULL;
+ unsigned int nr_remove = 1;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Get the element to remove. If it is already removed, we have nothing
+ * to do.
+ */
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_REMOVED)
+ goto unlock;
+
+ /*
+ * If the element to remove is a read element, the associated write
+ * element must also be removed.
+ */
+ if (se->type == BLK_SE_TYPE_READ) {
+ paired_se = &zlo->elements[se->paired_id - 1];
+ if (READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED)
+ nr_remove = 2;
+ }
+
+ if (zlo->nr_removed_elements + nr_remove >
+ zlo->max_nr_removed_elements) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Schedule the element removal with a delay, to emulate the (generally
+ * short) time it takes for a real device to depopulate a head and
+ * modify the zones.
+ */
+ zlo->remove_element_id = element_id;
+ 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));
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static void zloop_restore_conv_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ mutex_lock(&zone->lock);
+ spin_lock(&zone->wp_lock);
+ if (zone->cond == BLK_ZONE_COND_OFFLINE ||
+ zone->cond == BLK_ZONE_COND_READONLY)
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_NOT_WP);
+ spin_unlock(&zone->wp_lock);
+ mutex_unlock(&zone->lock);
+}
+
+static int zloop_restore_seq_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ int ret;
+
+ mutex_lock(&zone->lock);
+ ret = zloop_do_reset_zone(zlo, zone);
+ if (ret)
+ set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ mutex_unlock(&zone->lock);
+
+ return ret;
+}
+
+static void zloop_restore_elements_work(struct work_struct *work)
+{
+ struct zloop_device *zlo = container_of(work, struct zloop_device,
+ restore_elements_work.work);
+ struct zloop_zone *zone;
+ struct blk_se *se;
+ unsigned int i;
+ int ret;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* Reset all zones. */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ if (test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
+ zloop_restore_conv_zone(zlo, zone);
+ continue;
+ }
+ ret = zloop_restore_seq_zone(zlo, zone);
+ if (ret)
+ goto out_unlock;
+ }
+
+ /* Restore all removed elements. */
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ if (READ_ONCE(se->status) == BLK_SE_STS_RESTORE_IN_PROGRESS)
+ WRITE_ONCE(se->status, BLK_SE_STS_OK);
+ }
+
+ zlo->nr_removed_elements = 0;
+
+out_unlock:
+ WRITE_ONCE(zlo->restore_in_progress, false);
+ mutex_unlock(&zlo->stor_elements_lock);
+}
+
+static int zloop_restore_elements(struct gendisk *disk)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /* If we have no removed elements, we have nothing to do. */
+ if (!zlo->nr_removed_elements)
+ goto unlock;
+
+ /*
+ * Schedule the elements restoration with a delay, to emulate the time
+ * it takes for a real device to restore all removed heads and reset
+ * all zones.
+ */
+ WRITE_ONCE(zlo->restore_in_progress, true);
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ 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,
+ msecs_to_jiffies(5000));
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
static void zloop_free_disk(struct gendisk *disk)
{
struct zloop_device *zlo = disk->private_data;
unsigned int i;
+ cancel_delayed_work_sync(&zlo->remove_element_work);
+ cancel_delayed_work_sync(&zlo->restore_elements_work);
+
blk_mq_free_tag_set(&zlo->tag_set);
for (i = 0; i < zlo->nr_zones; i++) {
@@ -1019,15 +1446,24 @@ static void zloop_free_disk(struct gendisk *disk)
fput(zlo->data_dir);
destroy_workqueue(zlo->workqueue);
+ kfree(zlo->elements);
kfree(zlo->base_dir);
kvfree(zlo);
}
+
+static const struct blk_se_ops zloop_se_ops = {
+ .report_elements = zloop_report_elements,
+ .remove_element = zloop_remove_element,
+ .restore_elements = zloop_restore_elements,
+};
+
static const struct block_device_operations zloop_fops = {
.owner = THIS_MODULE,
.open = zloop_open,
.report_zones = zloop_report_zones,
.free_disk = zloop_free_disk,
+ .se_ops = &zloop_se_ops,
};
__printf(3, 4)
@@ -1112,6 +1548,24 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
if (!opts->buffered_io)
oflags |= O_DIRECT;
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ unsigned int nr_elems = zlo->nr_elements;
+ unsigned int se_idx;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems /= 2;
+ se_idx = zone_no % nr_elems;
+ zlo->elements[se_idx].nr_zones++;
+
+ zone->wr_se_id = se_idx + 1;
+ zone->rd_se_id = zone->wr_se_id;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ zone->rd_se_id += nr_elems;
+ zlo->elements[zone->rd_se_id - 1].nr_zones =
+ zlo->elements[se_idx].nr_zones;
+ }
+ }
+
if (zone_no < zlo->nr_conv_zones) {
/* Conventional zone file. */
set_bit(ZLOOP_ZONE_CONV, &zone->flags);
@@ -1182,6 +1636,79 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
return ret;
}
+#define ZLOOP_MIN_STOR_ELEMENTS 2
+#define ZLOOP_MAX_STOR_ELEMENTS 32
+#define ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS 32
+
+static int zloop_create_storage_elements(struct zloop_device *zlo)
+{
+ struct blk_se *se, *paired_se;
+ unsigned int i, nr_elems, nr_elements;
+
+ /*
+ * Calculate the number of storage elements we are going to emulate.
+ * To achieve a somewhat realistic emulation, we want at least 2 storage
+ * elements, and no more than 32, targeting at least 32 zones per
+ * element.
+ */
+ if (zlo->nr_zones <= 64)
+ nr_elements = 2;
+ else
+ nr_elements =
+ min(ZLOOP_MAX_STOR_ELEMENTS,
+ zlo->nr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);
+
+ /*
+ * If we are emulating pairs of read and write storage elements, we need
+ * double the number of storage element descriptors.
+ */
+ nr_elems = nr_elements;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems *= 2;
+ zlo->elements = kzalloc_objs(struct blk_se, nr_elems);
+ if (!zlo->elements)
+ return -ENOMEM;
+
+ for (i = 0, se = zlo->elements; i < nr_elements; i++, se++) {
+ se->id = i + 1;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ se->type = BLK_SE_TYPE_WRITE;
+ else
+ se->type = BLK_SE_TYPE_RDWR;
+ se->status = BLK_SE_STS_OK;
+ }
+
+ /*
+ * If we are emulating pairs of read and write storage elements,
+ * initialize the read elements paired with the write elements we just
+ * initialized.
+ */
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ for (i = 0; i < nr_elements; i++, se++) {
+ se->id = nr_elements + i + 1;
+ paired_se = &zlo->elements[i];
+ se->paired_id = paired_se->id;
+ paired_se->paired_id = se->id;
+ se->type = BLK_SE_TYPE_READ;
+ se->status = BLK_SE_STS_OK;
+ }
+ }
+
+ /*
+ * Make sure we do not allow removing all storage elements as that does
+ * not make any sense. This is consistent with the device advertized
+ * limit of SCSI and ATA devices supporting the storage element
+ * depopulation feature.
+ */
+ zlo->nr_elements = nr_elems;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ zlo->max_nr_removed_elements = zlo->nr_elements - 2;
+ else
+ zlo->max_nr_removed_elements = zlo->nr_elements - 1;
+
+ return 0;
+}
+
static bool zloop_dev_exists(struct zloop_device *zlo)
{
struct file *cnv, *seq;
@@ -1237,6 +1764,11 @@ static int zloop_ctl_add(struct zloop_options *opts)
WRITE_ONCE(zlo->state, Zlo_creating);
spin_lock_init(&zlo->open_zones_lock);
INIT_LIST_HEAD(&zlo->open_zones_lru_list);
+ mutex_init(&zlo->stor_elements_lock);
+ INIT_DELAYED_WORK(&zlo->remove_element_work,
+ zloop_remove_element_work);
+ INIT_DELAYED_WORK(&zlo->restore_elements_work,
+ zloop_restore_elements_work);
ret = mutex_lock_killable(&zloop_ctl_mutex);
if (ret)
@@ -1270,12 +1802,19 @@ static int zloop_ctl_add(struct zloop_options *opts)
if (zlo->zone_append)
zlo->ordered_zone_append = opts->ordered_zone_append;
zlo->discard_write_cache = opts->discard_write_cache;
+ zlo->stor_elements = opts->stor_elements;
+
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ ret = zloop_create_storage_elements(zlo);
+ if (ret)
+ goto out_free_idr;
+ }
zlo->workqueue = alloc_workqueue("zloop%d", WQ_UNBOUND | WQ_FREEZABLE,
opts->nr_queues * opts->queue_depth, zlo->id);
if (!zlo->workqueue) {
ret = -ENOMEM;
- goto out_free_idr;
+ goto out_destroy_storage_elements;
}
if (opts->base_dir)
@@ -1361,6 +1900,10 @@ static int zloop_ctl_add(struct zloop_options *opts)
zlo->id, zlo->nr_zones,
((sector_t)zlo->zone_size << SECTOR_SHIFT) >> 20,
zlo->block_size);
+ if (zlo->nr_elements)
+ pr_info("zloop%d: %d storage elements\n",
+ zlo->id, zlo->nr_elements);
+
pr_info("zloop%d: using %s%s zone append\n",
zlo->id,
zlo->ordered_zone_append ? "ordered " : "",
@@ -1370,6 +1913,8 @@ static int zloop_ctl_add(struct zloop_options *opts)
out_cleanup_disk:
put_disk(zlo->disk);
+ return ret;
+
out_cleanup_tags:
blk_mq_free_tag_set(&zlo->tag_set);
out_close_files:
@@ -1384,6 +1929,8 @@ static int zloop_ctl_add(struct zloop_options *opts)
kfree(zlo->base_dir);
out_destroy_workqueue:
destroy_workqueue(zlo->workqueue);
+out_destroy_storage_elements:
+ kfree(zlo->elements);
out_free_idr:
mutex_lock(&zloop_ctl_mutex);
idr_remove(&zloop_index_idr, zlo->id);
@@ -1483,6 +2030,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];
@@ -1502,6 +2085,8 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
opts->buffered_io = ZLOOP_DEF_BUFFERED_IO;
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;
@@ -1636,6 +2221,30 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
case ZLOOP_OPT_DISCARD_WRITE_CACHE:
opts->discard_write_cache = true;
break;
+ case ZLOOP_OPT_STOR_ELEMENTS:
+ if (match_uint(args, &token)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ switch (token) {
+ case ZLOOP_STOR_ELEMENTS_NONE:
+ case ZLOOP_STOR_ELEMENTS_RDWR:
+ case ZLOOP_STOR_ELEMENTS_PAIRS:
+ break;
+ default:
+ pr_err("Invalid stor_elements value\n");
+ ret = -EINVAL;
+ goto out;
+ }
+ 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);
@@ -1664,14 +2273,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 },
};
@@ -1719,6 +2330,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;
@@ -1742,6 +2356,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);
@@ -1752,6 +2368,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;
}
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a1b21ea14e549..48c46f4a1ace8 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
{
struct request *rq = scsi_cmd_to_rq(cmd);
struct scsi_device *sdp = cmd->device;
- struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
+ struct gendisk *disk = rq->q->disk;
+ struct scsi_disk *sdkp = scsi_disk(disk);
sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
sector_t threshold;
unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
@@ -1456,6 +1457,11 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
goto fail;
}
+ /* Prevent read/write to offline zones of ZBC disks. */
+ if (sdkp->device->type == TYPE_ZBC &&
+ disk_zone_is_offline(disk, blk_rq_pos(rq)))
+ goto fail;
+
/*
* Some SD card readers can't handle accesses which touch the
* last one or two logical blocks. Split accesses as needed.
@@ -3937,6 +3943,9 @@ static const struct block_device_operations sd_fops = {
.get_unique_id = sd_get_unique_id,
.free_disk = scsi_disk_free_disk,
.pr_ops = &sd_pr_ops,
+#ifdef CONFIG_BLK_DEV_ZONED
+ .se_ops = &sd_zbc_se_ops,
+#endif
};
/**
diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 574af82430169..fc8c49903811e 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -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)
@@ -242,6 +243,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
unsigned int nr_zones, struct blk_report_zones_args *args);
+extern const struct blk_se_ops sd_zbc_se_ops;
+
#else /* CONFIG_BLK_DEV_ZONED */
static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c
index 456beaf2e7690..39cd4f9fcce5c 100644
--- a/drivers/scsi/sd_zbc.c
+++ b/drivers/scsi/sd_zbc.c
@@ -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)
@@ -533,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_se *element)
+{
+ struct scsi_device *sdp = sdkp->device;
+ sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
+ u64 capacity;
+
+ memset(element, 0, sizeof(*element));
+
+ element->id = get_unaligned_be32(&desc[4]);
+
+ 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);
+ break;
+ case SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:
+ element->paired_id = get_unaligned_be32(&desc[16]);
+ if (desc[20] & 0x01)
+ element->type = BLK_SE_TYPE_READ;
+ else
+ element->type = BLK_SE_TYPE_WRITE;
+ element->nr_zones = get_unaligned_be64(&desc[24]);
+ break;
+ default:
+ element->type = BLK_SE_TYPE_UNKNOWN;
+ }
+
+ switch (desc[15]) {
+ case SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS:
+ case SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:
+ element->status = BLK_SE_STS_OK;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS:
+ element->status = BLK_SE_STS_DEGRADED;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:
+ element->status = BLK_SE_STS_RESTORE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:
+ element->status = BLK_SE_STS_RESTORE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:
+ element->status = BLK_SE_STS_REMOVE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:
+ element->status = BLK_SE_STS_REMOVE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:
+ element->status = BLK_SE_STS_REMOVED;
+ element->restore_allowed = desc[13] & 0x01;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:
+ default:
+ element->status = BLK_SE_STS_UNKNOWN;
+ break;
+ }
+}
+
+/*
+ * Large hard limit on the number of storage elements. This accommodates all
+ * known devices today and likely forever :)
+ */
+#define SD_ZBC_MAX_STORAGE_ELEMENTS 255
+
+static int sd_zbc_report_storage_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ 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;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ unsigned int nr_descs, nr_se;
+ unsigned int buf_size;
+ int i, ret = 0, result;
+ u8 *desc, *buf;
+
+ if (!sdkp->modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ /*
+ * We need at least 32B for the report header and 32B for each
+ * descriptor.
+ */
+ nr_se = min(SD_ZBC_MAX_STORAGE_ELEMENTS, *nr_elements);
+ buf_size = ALIGN((nr_se + 1) * 32, SECTOR_SIZE);
+
+ buf = kzalloc(buf_size, GFP_NOIO);
+ if (!buf)
+ return -ENOMEM;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;
+ put_unaligned_be32(buf_size, &cmd[10]);
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buf_size,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp,
+ "GET PHYSICAL ELEMENT STATUS failed\n");
+ sd_print_result(sdkp, "GET PHYSICAL ELEMENT STATUS", result);
+ if (result > 0 && scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ ret = -EIO;
+ goto free_buf;
+ }
+
+ nr_descs = get_unaligned_be32(&buf[0]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of phys element descriptors\n");
+ ret = -EIO;
+ goto free_buf;
+ }
+ if (nr_descs > SD_ZBC_MAX_STORAGE_ELEMENTS) {
+ sd_printk(KERN_ERR, sdkp,
+ "Too many phys element descriptors\n");
+ ret = -EIO;
+ goto free_buf;
+ }
+
+ if (!elements) {
+ *nr_elements = nr_descs;
+ goto free_buf;
+ }
+
+ nr_descs = get_unaligned_be32(&buf[4]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of reported phys element descriptors\n");
+ ret = -EIO;
+ goto free_buf;
+ }
+
+ desc = &buf[32];
+ for (i = 0; i < min(nr_se, nr_descs); i++, desc += 32)
+ sd_zbc_parse_storage_element(sdkp, desc, &elements[i]);
+ *nr_elements = i;
+
+free_buf:
+ kfree(buf);
+
+ return ret;
+}
+
+static int sd_zbc_remove_storage_element(struct gendisk *disk,
+ unsigned int element_id)
+{
+ 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;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ int result;
+
+ if (!sdkp->modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES;
+ put_unaligned_be32(element_id, &cmd[10]);
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp,
+ "REMOVE ELEMENT AND MODIFY ZONES failed\n");
+ sd_print_result(sdkp,
+ "REMOVE ELEMENT AND MODIFY ZONES", result);
+ if (result > 0 && scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static int sd_zbc_restore_storage_elements(struct gendisk *disk)
+{
+ 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;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ int result;
+
+ if (!sdkp->modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_RESTORE_ELEMENTS_AND_REBUILD;
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp,
+ "RESTORE ELEMENTS AND REBUILD failed\n");
+ sd_print_result(sdkp,
+ "RESTORE ELEMENTS AND REBUILD", result);
+ if (result > 0 && scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+const struct blk_se_ops sd_zbc_se_ops = {
+ .report_elements = sd_zbc_report_storage_elements,
+ .remove_element = sd_zbc_remove_storage_element,
+ .restore_elements = sd_zbc_restore_storage_elements,
+};
+
/*
* Call blk_revalidate_disk_zones() if any of the zoned disk properties have
* changed that make it necessary to call that function. Called by
@@ -554,9 +802,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 +874,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",
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index d003a9d2d1f6c..20b77c6b451fc 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -189,6 +189,11 @@ struct gendisk {
struct disk_events *ev;
#ifdef CONFIG_BLK_DEV_ZONED
+ /*
+ * Total number of storage elements of the disk. This is always 0 for
+ * disks that do not support storage element management.
+ */
+ unsigned int nr_storage_elements;
/*
* Zoned block device information. Reads of this information must be
* protected with blk_queue_enter() / blk_queue_exit() or by holding a
@@ -942,6 +947,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,
return disk->last_zone_capacity;
return disk->zone_capacity;
}
+
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector);
+
static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
sector_t pos)
{
@@ -956,6 +964,12 @@ static inline unsigned int disk_nr_zones(struct gendisk *disk)
return 0;
}
+static inline bool disk_zone_is_offline(struct gendisk *disk,
+ sector_t sector)
+{
+ return false;
+}
+
static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)
{
return false;
@@ -1574,6 +1588,22 @@ enum blk_unique_id {
BLK_UID_NAA = 3,
};
+/* Storage element management operations. */
+struct blk_se_ops {
+ int (*report_elements)(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+ int (*remove_element)(struct gendisk *disk, unsigned int element_id);
+ int (*restore_elements)(struct gendisk *disk);
+};
+
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id);
+int bdev_restore_storage_elements(struct block_device *bdev);
+
struct block_device_operations {
void (*submit_bio)(struct bio *bio);
int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
@@ -1601,6 +1631,7 @@ struct block_device_operations {
enum blk_unique_id id_type);
struct module *owner;
const struct pr_ops *pr_ops;
+ const struct blk_se_ops *se_ops;
/*
* Special callback for probing GPT entry at a given sector.
diff --git a/include/uapi/linux/blkzoned.h b/include/uapi/linux/blkzoned.h
index 6638361209667..e6c60fba6ac9c 100644
--- a/include/uapi/linux/blkzoned.h
+++ b/include/uapi/linux/blkzoned.h
@@ -208,4 +208,86 @@ struct blk_zone_range {
#define BLKFINISHZONE _IOW(0x12, 136, struct blk_zone_range)
#define BLKREPORTZONEV2 _IOWR(0x12, 142, struct blk_zone_report)
+/**
+ * enum blk_se_type - Type of a zoned device storage element.
+ *
+ * @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_se_type {
+ BLK_SE_TYPE_RDWR = 0x01,
+ BLK_SE_TYPE_READ = 0x02,
+ BLK_SE_TYPE_WRITE = 0x03,
+ BLK_SE_TYPE_UNKNOWN = 0xFF,
+};
+
+/**
+ * enum blk_se_status - Status of a zoned device storage element.
+ *
+ * @BLK_SE_STS_OK: The storage element is operating normally.
+ * @BLK_SE_STS_DEGRADED: The storage element has degraded and is not operating
+ * normally.
+ * @BLK_SE_STS_REMOVE_IN_PROGRESS: The storage element is being removed.
+ * @BLK_SE_STS_REMOVE_ERROR: The storage element removal failed.
+ * @BLK_SE_STS_RESTORE_IN_PROGRESS: The storage element is being restored.
+ * @BLK_SE_STS_RESTORE_ERROR: The storage element restoration failed.
+ * @BLK_SE_STS_REMOVED: The storage element was removed.
+ * @BLK_SE_STS_UNKNOWN: The storage element status is unknown.
+ */
+enum blk_se_status {
+ BLK_SE_STS_OK = 0x01,
+ BLK_SE_STS_DEGRADED = 0x02,
+ BLK_SE_STS_REMOVE_IN_PROGRESS = 0x03,
+ BLK_SE_STS_REMOVE_ERROR = 0x04,
+ BLK_SE_STS_RESTORE_IN_PROGRESS = 0x05,
+ BLK_SE_STS_RESTORE_ERROR = 0x06,
+ BLK_SE_STS_REMOVED = 0x07,
+ BLK_SE_STS_UNKNOWN = 0xFF,
+};
+
+/**
+ * struct blk_se - Zoned device storage element descriptor.
+ *
+ * @id: The ID of the element (cannot be 0).
+ * @paired_id: The ID of the paired element for an element that is not
+ * of type BLK_SE_TYPE_RDWR.
+ * @nr_zones: The number of zones that the storage element handles.
+ * @type: The type of the storage element (enum blk_se_type).
+ * @status: The health status of the storage element
+ * (enum blk_se_status).
+ * @restore_allowed: Indicate if the storage element can be restored.
+ * @reserved: unused field to align this structure size to 32B
+ */
+struct blk_se {
+ __u32 id;
+ __u32 paired_id;
+ __u64 nr_zones;
+ __u8 type;
+ __u8 status;
+ __u8 restore_allowed;
+ __u8 reserved[13];
+};
+
+struct blk_se_report {
+ __u32 nr_elements;
+ __u32 reserved;
+ struct blk_se elements[];
+};
+
+/**
+ * Zoned block device storage element management ioctl's:
+ *
+ * @BLKGETNRSTORELEMS: Get the number of storage elements of the device.
+ * @BLKREPORTSTORELEMS: Get the device storage elements.
+ * @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device.
+ * @BLKRESTORESTORELEM: Restore (repopulate if possible) all storage elements
+ * that have been removed.
+ */
+#define BLKGETNRSTORELEMS _IOR(0x12, 143, __u32)
+#define BLKREPORTSTORELEMS _IOWR(0x12, 144, struct blk_se_report)
+#define BLKREMOVESTORELEM _IOW(0x12, 145, __u32)
+#define BLKRESTORESTORELEMS _IO(0x12, 146)
+
#endif /* _UAPI_BLKZONED_H */
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index 34c6f219462a5..8a979326aa7f0 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -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 */
|
| 3/2 | 2026/10/09 11:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29324 output: 48 thoughts: 906 Thoughts: **Analyzing the Zoned Block Device Patch Series** Okay, so I need to evaluate this patch series for the Linux kernel on amd64. First, I need to get a handle on what's being modified. I'll start by diving into the diff and identify the added/modified code, which subsystems are impacted, and then assess things like reachability and configuration options. I'll need to pinpoint the critical symbols involved. I see the changes span several areas: 1. **Documentation:** `Documentation/admin-guide/blockdev/zoned_loop.rst` has a new `stor_elements` option for the zoned loopback device (`zloop`). 2. **Block Layer Core:** `block/blk-zoned.c` sees the introduction of `disk_zone_is_offline()` and a significant refactoring of zone validation. It also includes functions like `disk_se_mgmt_in_progress`, `disk_wait_for_se_mgmt_completion`, and ioctl handlers (`bdev_report_storage_elements`, etc.) for managing storage elements. 3. **Header Files:** Declarations for the new functions are added in `block/blk.h` and `include/linux/blkdev.h`, specifically for `gendisk` and `block_device_operations`, which indicates structural changes to the block device representation. 4. **Generic Block Device:** The `disk_init_storage_element_mgmt(disk)` function call is added to `__add_disk` in `block/genhd.c`, meaning storage element management is initialized during disk addition. 5. **IOCTL Handling:** `block/ioctl.c`'s `blkdev_common_ioctl` now handles new ioctls: `BLKGETNRSTORELEMS`, `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, and `BLKRESTORESTORELEMS`, calling `blkdev_zone_storage_elements_ioctl()`. This points to user-space interaction with the new storage element features. 6. **Zoned Loopback Driver:** `drivers/block/zloop.c` indicates the `zloop` driver is a zoned loop driver. This driver is getting emulation support for storage elements. The new code includes functions like `zloop_ctl_degrade_element`, `zloop_report_elements`, and `zloop_remove_element`. I'll need to figure out if `zloop` is upstream and its configuration requirements (Kconfig). 7. **SCSI ZBC:** `drivers/scsi/sd.c` and `drivers/scsi/sd_zbc.c` gain storage element support for SCSI ZBC (Zoned Block Commands) disks through functions like `sd_zbc_se_ops`, `sd_zbc_report_storage_elements`, and so on. 8. **User-Space API:** `include/uapi/linux/blkzoned.h` defines the new ioctls and data structures (`BLKGETNRSTORELEMS`, `struct blk_se`, etc.) for user-space interaction. Now, I need to investigate the `zloop` driver in more detail. I should run `grepper` or check the Kconfig files in `drivers/block` to determine its configuration options. This will help determine if the changes are part of the main kernel tree and how they are enabled. It looks like zoned block devices are the focal point of this series! |
| 4/2 | 2026/10/09 11:30 | tool | grepper |
0mArgs:{
"Expression": "config BLK_DEV_ZLOOP|config BLK_DEV_ZONED",
"PathPrefixes": [
"drivers/block/",
"block/"
]
}Results: {
"Output": "block/Kconfig=77=config BLK_DEV_WRITE_MOUNTED\n--\nblock/Kconfig-96-\nblock/Kconfig:97:config BLK_DEV_ZONED\nblock/Kconfig-98-\tbool \"Zoned block device support\"\n--\ndrivers/block/Kconfig=354=source \"drivers/block/rnbd/Kconfig\"\ndrivers/block/Kconfig-355-\ndrivers/block/Kconfig:356:config BLK_DEV_ZONED_LOOP\ndrivers/block/Kconfig-357-\ttristate \"Zoned loopback device support\"\n"
} |
| 5/2 | 2026/10/09 11:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31189 output: 34 thoughts: 35 Thoughts: Here's my analysis of the provided text, rendered in the first person, assuming I'm an expert in the field: **Diving into Block Device Configuration** Alright, let's take a look at the kernel configuration file, specifically `drivers/block/Kconfig`. I need to figure out the dependencies of `BLK_DEV_ZONED_LOOP`, so I'm going to jump straight to around line 356. I expect it'll be a fairly straightforward dependency chain, but it's always good to verify and refresh my understanding, especially when dealing with zoned block devices. I'm curious if it's relying on any specific hardware features or other block device drivers. Let's see what the Kconfig file has to tell us. |
| 6/2 | 2026/10/09 11:30 | tool | read-file |
0mArgs:{
"File": "drivers/block/Kconfig",
"FirstLine": 355,
"LineCount": 25
}Results: {
"Contents": " 355:\t\n 356:\tconfig BLK_DEV_ZONED_LOOP\n 357:\t\ttristate \"Zoned loopback device support\"\n 358:\t\tdepends on BLK_DEV_ZONED\n 359:\t\thelp\n 360:\t\t Saying Y here will allow you to use create a zoned block device using\n 361:\t\t regular files for zones (one file per zones). This is useful to test\n 362:\t\t file systems, device mapper and applications that support zoned block\n 363:\t\t devices. To create a zoned loop device, no user utility is needed, a\n 364:\t\t zoned loop device can be created (or re-started) using a command\n 365:\t\t like:\n 366:\t\n 367:\t\t echo \"add id=0,zone_size_mb=256,capacity_mb=16384,conv_zones=11\" \u003e \\\n 368:\t\t\t/dev/zloop-control\n 369:\t\n 370:\t\t See Documentation/admin-guide/blockdev/zoned_loop.rst for usage\n 371:\t\t details.\n 372:\t\n 373:\t\t If unsure, say N.\n 374:\t\n 375:\tendif # BLK_DEV\n"
} |
| 7/2 | 2026/10/09 11:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31771 output: 31 thoughts: 18 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Diving into the Configuration Space** Alright, so we're seeing something pop up. Given my knowledge of the system's architecture, my first instinct is to dive into the configuration files. Let's start with `block/Kconfig`, specifically around line 97. That area is likely where some key block device settings, dependencies, or default values are defined. I'm betting something relevant to the issue we're facing is configured, or more likely *unconfigured* there. I need to understand what's in that scope to get the lay of the land for further debugging. Let's see if that line or the lines around it provide a good starting point. |
| 8/2 | 2026/10/09 11:30 | tool | read-file |
0mArgs:{
"File": "block/Kconfig",
"FirstLine": 95,
"LineCount": 25
}Results: {
"Contents": " 95:\t\twith the bdev_allow_write_mounted boot option.\n 96:\t\n 97:\tconfig BLK_DEV_ZONED\n 98:\t\tbool \"Zoned block device support\"\n 99:\t\thelp\n 100:\t\tBlock layer zoned block device support. This option enables\n 101:\t\tsupport for ZAC/ZBC/ZNS host-managed and host-aware zoned block\n 102:\t\tdevices.\n 103:\t\n 104:\t\tSay yes here if you have a ZAC, ZBC, or ZNS storage device.\n 105:\t\n 106:\tconfig BLK_DEV_THROTTLING\n 107:\t\tbool \"Block layer bio throttling support\"\n 108:\t\tdepends on BLK_CGROUP\n 109:\t\tselect BLK_CGROUP_RWSTAT\n 110:\t\thelp\n 111:\t\tBlock layer bio throttling support. It can be used to limit\n 112:\t\tthe IO rate to a device. IO rate policies are per cgroup and\n 113:\t\tone needs to mount and use blkio cgroup controller for creating\n 114:\t\tcgroups and specifying per device IO rate policies.\n 115:\t\n 116:\t\tSee Documentation/admin-guide/cgroup-v1/blkio-controller.rst for more information.\n 117:\t\n 118:\tconfig BLK_WBT\n 119:\t\tbool \"Enable support for block device writeback throttling\"\n"
} |
| 9/2 | 2026/10/09 11:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 32375 output: 30 thoughts: 27 Thoughts: Here's my summary, as though I were thinking through the process: **SCSI ZBC Configuration: Where Does This Live?** Okay, so I need to get a handle on the ZBC (Zoned Block Command) configuration for SCSI. My first instinct, given my experience, is to dive straight into the heart of the SCSI subsystem within the kernel source. Where would the low-level configurations and build options for something like this likely reside? I'm almost certain I'll find the relevant settings and any associated dependencies in the `drivers/scsi` directory. Now, the question is, *where* in that directory? Generally, build configurations are defined in either the Kconfig files, which control the options presented to the user during the kernel configuration process (like with `make menuconfig`), or the Makefiles, which govern the build process itself and handle dependencies. Given that this deals with ZBC, a specific, potentially enabling feature, the likely spots are either `drivers/scsi/Kconfig` (for the actual *option* itself) or `drivers/scsi/Makefile`, which might contain conditional compilation logic based on that Kconfig option. I'll need to check both to be thorough, but I suspect `Kconfig` is the more likely starting point. Let's start there. |
| 10/2 | 2026/10/09 11:30 | tool | grepper |
0mArgs:{
"Expression": "sd_zbc",
"PathPrefixes": [
"drivers/scsi/"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 271 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (4 files in total):\ndrivers/scsi/Makefile:2\ndrivers/scsi/sd.c:10\ndrivers/scsi/sd.h:11\ndrivers/scsi/sd_zbc.c:49\n\ndrivers/scsi/Makefile=36=CONTEXT_ANALYSIS_sd_dif.o := y\ndrivers/scsi/Makefile:37:CONTEXT_ANALYSIS_sd_zbc.o := y\ndrivers/scsi/Makefile-38-CONTEXT_ANALYSIS_sr.o := y\n--\ndrivers/scsi/Makefile=199=sd_mod-$(CONFIG_BLK_DEV_INTEGRITY) += sd_dif.o\ndrivers/scsi/Makefile:200:sd_mod-$(CONFIG_BLK_DEV_ZONED) += sd_zbc.o\ndrivers/scsi/Makefile-201-\n--\ndrivers/scsi/sd.c=1543=static blk_status_t sd_init_command(struct scsi_cmnd *cmd)\n--\ndrivers/scsi/sd.c-1568-\tcase REQ_OP_ZONE_RESET:\ndrivers/scsi/sd.c:1569:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,\ndrivers/scsi/sd.c-1570-\t\t\t\t\t\t false);\ndrivers/scsi/sd.c-1571-\tcase REQ_OP_ZONE_RESET_ALL:\ndrivers/scsi/sd.c:1572:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_RESET_WRITE_POINTER,\ndrivers/scsi/sd.c-1573-\t\t\t\t\t\t true);\ndrivers/scsi/sd.c-1574-\tcase REQ_OP_ZONE_OPEN:\ndrivers/scsi/sd.c:1575:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_OPEN_ZONE, false);\ndrivers/scsi/sd.c-1576-\tcase REQ_OP_ZONE_CLOSE:\ndrivers/scsi/sd.c:1577:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_CLOSE_ZONE, false);\ndrivers/scsi/sd.c-1578-\tcase REQ_OP_ZONE_FINISH:\ndrivers/scsi/sd.c:1579:\t\treturn sd_zbc_setup_zone_mgmt_cmnd(cmd, ZO_FINISH_ZONE, false);\ndrivers/scsi/sd.c-1580-\tdefault:\n--\ndrivers/scsi/sd.c=2375=static int sd_done(struct scsi_cmnd *SCpnt)\n--\ndrivers/scsi/sd.c-2480-\tif (sdkp-\u003edevice-\u003etype == TYPE_ZBC)\ndrivers/scsi/sd.c:2481:\t\tgood_bytes = sd_zbc_complete(SCpnt, good_bytes, \u0026sshdr);\ndrivers/scsi/sd.c-2482-\n--\ndrivers/scsi/sd.c=3779=static void sd_revalidate_disk(struct gendisk *disk)\n--\ndrivers/scsi/sd.c-3836-\t\t\tsd_read_block_characteristics(sdkp, lim);\ndrivers/scsi/sd.c:3837:\t\t\tsd_zbc_read_zones(sdkp, lim, buffer);\ndrivers/scsi/sd.c-3838-\t\t}\n--\ndrivers/scsi/sd.c-3903-\t */\ndrivers/scsi/sd.c:3904:\tif (sd_zbc_revalidate_zones(sdkp))\ndrivers/scsi/sd.c-3905-\t\tset_capacity_and_notify(disk, 0);\n--\ndrivers/scsi/sd.c=3933=static const struct block_device_operations sd_fops = {\n--\ndrivers/scsi/sd.c-3941-\t.unlock_native_capacity\t= sd_unlock_native_capacity,\ndrivers/scsi/sd.c:3942:\t.report_zones\t\t= sd_zbc_report_zones,\ndrivers/scsi/sd.c-3943-\t.get_unique_id\t\t= sd_get_unique_id,\n--\ndrivers/scsi/sd.c-3946-#ifdef CONFIG_BLK_DEV_ZONED\ndrivers/scsi/sd.c:3947:\t.se_ops\t\t\t= \u0026sd_zbc_se_ops,\ndrivers/scsi/sd.c-3948-#endif\n--\ndrivers/scsi/sd.h=232=void sd_dif_config_host(struct scsi_disk *sdkp, struct queue_limits *lim);\n--\ndrivers/scsi/sd.h-235-\ndrivers/scsi/sd.h:236:int sd_zbc_read_zones(struct scsi_disk *sdkp, struct queue_limits *lim,\ndrivers/scsi/sd.h-237-\t\tu8 buf[SD_BUF_SIZE]);\ndrivers/scsi/sd.h:238:int sd_zbc_revalidate_zones(struct scsi_disk *sdkp);\ndrivers/scsi/sd.h:239:blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,\ndrivers/scsi/sd.h-240-\t\t\t\t\t unsigned char op, bool all);\ndrivers/scsi/sd.h:241:unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,\ndrivers/scsi/sd.h-242-\t\t\t struct scsi_sense_hdr *sshdr);\ndrivers/scsi/sd.h:243:int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,\ndrivers/scsi/sd.h-244-\t\tunsigned int nr_zones, struct blk_report_zones_args *args);\ndrivers/scsi/sd.h-245-\ndrivers/scsi/sd.h:246:extern const struct blk_se_ops sd_zbc_se_ops;\ndrivers/scsi/sd.h-247-\n--\ndrivers/scsi/sd.h-249-\ndrivers/scsi/sd.h:250:static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,\ndrivers/scsi/sd.h-251-\t\tstruct queue_limits *lim, u8 buf[SD_BUF_SIZE])\n--\ndrivers/scsi/sd.h-255-\ndrivers/scsi/sd.h:256:static inline int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)\ndrivers/scsi/sd.h-257-{\n--\ndrivers/scsi/sd.h-260-\ndrivers/scsi/sd.h:261:static inline blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,\ndrivers/scsi/sd.h-262-\t\t\t\t\t\t unsigned char op,\n--\ndrivers/scsi/sd.h-267-\ndrivers/scsi/sd.h:268:static inline unsigned int sd_zbc_complete(struct scsi_cmnd *cmd,\ndrivers/scsi/sd.h-269-\t\t\tunsigned int good_bytes, struct scsi_sense_hdr *sshdr)\n--\ndrivers/scsi/sd.h-273-\ndrivers/scsi/sd.h:274:#define sd_zbc_report_zones NULL\ndrivers/scsi/sd.h-275-\n--\ndrivers/scsi/sd_zbc.c-26-/* Whether or not a SCSI zone descriptor describes a gap zone. */\ndrivers/scsi/sd_zbc.c:27:static bool sd_zbc_is_gap_zone(const u8 buf[64])\ndrivers/scsi/sd_zbc.c-28-{\n--\ndrivers/scsi/sd_zbc.c-32-/**\ndrivers/scsi/sd_zbc.c:33: * sd_zbc_parse_report - Parse a SCSI zone descriptor\ndrivers/scsi/sd_zbc.c-34- * @sdkp: SCSI disk pointer.\n--\ndrivers/scsi/sd_zbc.c-36- * @idx: Index of the zone relative to the first zone reported by the current\ndrivers/scsi/sd_zbc.c:37: *\tsd_zbc_report_zones() call.\ndrivers/scsi/sd_zbc.c-38- * @args: report zones arguments (callback, etc)\n--\ndrivers/scsi/sd_zbc.c-44- */\ndrivers/scsi/sd_zbc.c:45:static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64],\ndrivers/scsi/sd_zbc.c-46-\t\t\tunsigned int idx, struct blk_report_zones_args *args)\n--\ndrivers/scsi/sd_zbc.c-51-\ndrivers/scsi/sd_zbc.c:52:\tif (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf)))\ndrivers/scsi/sd_zbc.c-53-\t\treturn -EINVAL;\n--\ndrivers/scsi/sd_zbc.c-91-/**\ndrivers/scsi/sd_zbc.c:92: * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.\ndrivers/scsi/sd_zbc.c-93- * @sdkp: The target disk\n--\ndrivers/scsi/sd_zbc.c-104- */\ndrivers/scsi/sd_zbc.c:105:static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,\ndrivers/scsi/sd_zbc.c-106-\t\t\t\t unsigned int buflen, sector_t lba,\n--\ndrivers/scsi/sd_zbc.c-149-/**\ndrivers/scsi/sd_zbc.c:150: * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply.\ndrivers/scsi/sd_zbc.c-151- * @sdkp: The target disk\n--\ndrivers/scsi/sd_zbc.c-161- */\ndrivers/scsi/sd_zbc.c:162:static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,\ndrivers/scsi/sd_zbc.c-163-\t\t\t\t\tunsigned int nr_zones, size_t *buflen)\n--\ndrivers/scsi/sd_zbc.c-200-/**\ndrivers/scsi/sd_zbc.c:201: * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.\ndrivers/scsi/sd_zbc.c-202- * @sdkp: The target disk\ndrivers/scsi/sd_zbc.c-203- */\ndrivers/scsi/sd_zbc.c:204:static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)\ndrivers/scsi/sd_zbc.c-205-{\n--\ndrivers/scsi/sd_zbc.c-209-/**\ndrivers/scsi/sd_zbc.c:210: * sd_zbc_report_zones - SCSI .report_zones() callback.\ndrivers/scsi/sd_zbc.c-211- * @disk: Disk to report zones for.\n--\ndrivers/scsi/sd_zbc.c-218- */\ndrivers/scsi/sd_zbc.c:219:int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,\ndrivers/scsi/sd_zbc.c-220-\t\t\tunsigned int nr_zones,\n--\ndrivers/scsi/sd_zbc.c-239-\ndrivers/scsi/sd_zbc.c:240:\tbuf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, \u0026buflen);\ndrivers/scsi/sd_zbc.c-241-\tif (!buf)\n--\ndrivers/scsi/sd_zbc.c-244-\twhile (zone_idx \u003c nr_zones \u0026\u0026 lba \u003c sdkp-\u003ecapacity) {\ndrivers/scsi/sd_zbc.c:245:\t\tret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true);\ndrivers/scsi/sd_zbc.c-246-\t\tif (ret)\n--\ndrivers/scsi/sd_zbc.c-269-\t\t\tlba = start_lba + zone_length;\ndrivers/scsi/sd_zbc.c:270:\t\t\tif (sd_zbc_is_gap_zone(\u0026buf[offset])) {\ndrivers/scsi/sd_zbc.c-271-\t\t\t\tif (sdkp-\u003ezone_starting_lba_gran)\n--\ndrivers/scsi/sd_zbc.c-278-\ndrivers/scsi/sd_zbc.c:279:\t\t\tret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,\ndrivers/scsi/sd_zbc.c-280-\t\t\t\t\t\t args);\n--\ndrivers/scsi/sd_zbc.c-293-\ndrivers/scsi/sd_zbc.c:294:static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)\ndrivers/scsi/sd_zbc.c-295-{\n--\ndrivers/scsi/sd_zbc.c-306-\ndrivers/scsi/sd_zbc.c:307:\tif (sector \u0026 (sd_zbc_zone_sectors(sdkp) - 1))\ndrivers/scsi/sd_zbc.c-308-\t\t/* Unaligned request */\n--\ndrivers/scsi/sd_zbc.c-314-/**\ndrivers/scsi/sd_zbc.c:315: * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations\ndrivers/scsi/sd_zbc.c-316- *\t\t\tcan be RESET WRITE POINTER, OPEN, CLOSE or FINISH.\n--\ndrivers/scsi/sd_zbc.c-323- */\ndrivers/scsi/sd_zbc.c:324:blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,\ndrivers/scsi/sd_zbc.c-325-\t\t\t\t\t unsigned char op, bool all)\n--\ndrivers/scsi/sd_zbc.c-332-\ndrivers/scsi/sd_zbc.c:333:\tret = sd_zbc_cmnd_checks(cmd);\ndrivers/scsi/sd_zbc.c-334-\tif (ret != BLK_STS_OK)\n--\ndrivers/scsi/sd_zbc.c-354-/**\ndrivers/scsi/sd_zbc.c:355: * sd_zbc_complete - ZBC command post processing.\ndrivers/scsi/sd_zbc.c-356- * @cmd: Completed command\n--\ndrivers/scsi/sd_zbc.c-362- */\ndrivers/scsi/sd_zbc.c:363:unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,\ndrivers/scsi/sd_zbc.c-364-\t\t struct scsi_sense_hdr *sshdr)\n--\ndrivers/scsi/sd_zbc.c-384-/**\ndrivers/scsi/sd_zbc.c:385: * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics\ndrivers/scsi/sd_zbc.c-386- * @sdkp: Target disk\n--\ndrivers/scsi/sd_zbc.c-390- */\ndrivers/scsi/sd_zbc.c:391:static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,\ndrivers/scsi/sd_zbc.c-392-\t\t\t\t\t unsigned char *buf)\n--\ndrivers/scsi/sd_zbc.c-455-/**\ndrivers/scsi/sd_zbc.c:456: * sd_zbc_check_capacity - Check the device capacity\ndrivers/scsi/sd_zbc.c-457- * @sdkp: Target disk\n--\ndrivers/scsi/sd_zbc.c-466- */\ndrivers/scsi/sd_zbc.c:467:static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,\ndrivers/scsi/sd_zbc.c-468-\t\t\t\t u32 *zblocks)\n--\ndrivers/scsi/sd_zbc.c-475-\t/* Do a report zone to get max_lba and the size of the first zone */\ndrivers/scsi/sd_zbc.c:476:\tret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);\ndrivers/scsi/sd_zbc.c-477-\tif (ret)\n\n"
} |
| 11/2 | 2026/10/09 11:30 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37188 output: 26 thoughts: 886 Thoughts: **Analyzing the Fuzzing Potential of This Zoned Block Device Patch Series** Okay, let's break this down. My initial assessment is whether this is fuzzable and worth the effort. First, I need to understand what this patch series actually *does*. Right, it's introducing storage element management for zoned block devices. A quick rundown: * **Block Layer:** It adds a suite of UAPI ioctls in `blkzoned.h`: `BLKGETNRSTORELEMS`, `BLKREPORTSTORELEMS`, `BLKREMOVESTORELEM`, and `BLKRESTORESTORELEMS`. These are the key entry points I'll want to focus on. It implements these ioctls and several helper functions (`bdev_report_storage_elements`, `blkdev_zone_storage_elements_ioctl`, etc.) in `block/ioctl.c` and `block/blk-zoned.c`. * **The interesting bit:** `zloop` is key because it emulates storage elements. It provides a software-based zoned block device driver (`CONFIG_BLK_DEV_ZONED_LOOP`). It's controlled through `/dev/zloop-control` with options like `stor_elements=1|2`, `element_id=...`, and the `degrade_element` command. It handles asynchronous removal/restoration via delayed work. This is *perfect* for fuzzing – a software emulated zoned device! * **SCSI Integration:** The patch also touches `sd.c` and `sd_zbc.c`, adding SCSI implementations, but this is less interesting for initial fuzzing. `zloop` is more direct. So, the next question is: *can* I actually reach this code in a standard, virtualized environment like QEMU? And the answer is YES! `zloop` is *designed* for software emulation. I can easily create a `zloop` device, set `stor_elements=1` or `2` via `/dev/zloop-control`, and then exercise the new ioctls on the created block device. Plus, since the ioctls are handled in `block/ioctl.c`, I can theoretically call them on *any* block device, though they'll likely return `-ENOTTY` if the device doesn't support the feature. That flexibility is good for broader testing. Hold on, I need to confirm the required configuration options. Let's make sure the dependencies are in place. Okay, a quick check of `drivers/block/zloop.c` and `block/blk-zoned.c`. I need to ensure that `CONFIG_BLK_DEV_ZONED` is enabled to build the necessary code, and I see it's guarded by `#ifdef CONFIG_BLK_DEV_ZONED` in `block/blk.h` and the relevant sections. Okay. I know now `blk-zoned.o` is compiled when the config is set. Seems I have everything I need to get started. I can build a minimal kernel configuration with `CONFIG_BLK_DEV_ZONED_LOOP` and go from there. |
| 12/2 | 2026/10/09 11:31 | tool | grepper |
0mArgs:{
"Expression": "blk-zoned",
"PathPrefixes": [
"block/"
]
}Results: {
"Output": "block/Makefile=31=obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o \\\nblock/Makefile-32-\t\t\t\t bio-integrity-auto.o bio-integrity-fs.o\nblock/Makefile:33:obj-$(CONFIG_BLK_DEV_ZONED)\t+= blk-zoned.o\nblock/Makefile-34-obj-$(CONFIG_BLK_WBT)\t\t+= blk-wbt.o\n"
} |
| 13/2 | 2026/10/09 11:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38903 output: 27 thoughts: 61 Thoughts: Here's my thinking: **Examining Zoned Block Device Compilation** Okay, so I'm looking at how the kernel builds these zoned block device components. Specifically, it looks like `blk-zoned.o` gets compiled only when `CONFIG_BLK_DEV_ZONED` is set to 'y'. That's the core of the zoned block device support, I'd assume. Now, the `zloop.o` module... that appears to be contingent on `CONFIG_BLK_DEV_ZONED_LOOP` being enabled. So, likely a loop-based implementation or a testing module for zoned devices. To confirm all of this, I need to open up `drivers/block/Makefile`. The makefile holds all the build rules and dependencies for these block device drivers, so that's the place to double-check these compilation conditions and see how they are implemented. |
| 14/2 | 2026/10/09 11:31 | tool | grepper |
0mArgs:{
"Expression": "zloop",
"PathPrefixes": [
"drivers/block/"
]
}Results: {
"Output": "\nFull output is too long, showing 200 out of 871 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (3 files in total):\ndrivers/block/Kconfig:1\ndrivers/block/Makefile:1\ndrivers/block/zloop.c:269\n\ndrivers/block/Kconfig=356=config BLK_DEV_ZONED_LOOP\n--\ndrivers/block/Kconfig-367-\t echo \"add id=0,zone_size_mb=256,capacity_mb=16384,conv_zones=11\" \u003e \\\ndrivers/block/Kconfig:368:\t\t/dev/zloop-control\ndrivers/block/Kconfig-369-\n--\ndrivers/block/Makefile=40=obj-$(CONFIG_BLK_DEV_UBLK)\t\t\t+= ublk_drv.o\ndrivers/block/Makefile:41:obj-$(CONFIG_BLK_DEV_ZONED_LOOP) += zloop.o\ndrivers/block/Makefile-42-\n--\ndrivers/block/zloop.c=25=enum {\n--\ndrivers/block/zloop.c-43-\ndrivers/block/zloop.c:44:static const match_table_t zloop_opt_tokens = {\ndrivers/block/zloop.c-45-\t{ ZLOOP_OPT_ID,\t\t\t\"id=%d\"\t},\n--\ndrivers/block/zloop.c-63-/* Storage elements emulation types. */\ndrivers/block/zloop.c:64:enum zloop_stor_elements {\ndrivers/block/zloop.c-65-\t/* No emulation. */\n--\ndrivers/block/zloop.c-78-#define ZLOOP_DEF_MAX_OPEN_ZONES\t0\ndrivers/block/zloop.c:79:#define ZLOOP_DEF_BASE_DIR\t\t\"/var/local/zloop\"\ndrivers/block/zloop.c-80-#define ZLOOP_DEF_NR_QUEUES\t\t1\n--\ndrivers/block/zloop.c-90-\ndrivers/block/zloop.c:91:struct zloop_options {\ndrivers/block/zloop.c-92-\tunsigned int\t\tmask;\n--\ndrivers/block/zloop.c-105-\tbool\t\t\tdiscard_write_cache;\ndrivers/block/zloop.c:106:\tenum zloop_stor_elements stor_elements;\ndrivers/block/zloop.c-107-\tunsigned int\t\telement_id;\n--\ndrivers/block/zloop.c=113=enum {\n--\ndrivers/block/zloop.c-118-\ndrivers/block/zloop.c:119:enum zloop_zone_flags {\ndrivers/block/zloop.c-120-\tZLOOP_ZONE_CONV = 0,\n--\ndrivers/block/zloop.c-127- */\ndrivers/block/zloop.c:128:struct zloop_zone {\ndrivers/block/zloop.c-129-\tstruct list_head\topen_zone_entry;\n--\ndrivers/block/zloop.c-143-\ndrivers/block/zloop.c:144:struct zloop_device {\ndrivers/block/zloop.c-145-\tunsigned int\t\tid;\n--\ndrivers/block/zloop.c-155-\tbool\t\t\tdiscard_write_cache;\ndrivers/block/zloop.c:156:\tenum zloop_stor_elements stor_elements;\ndrivers/block/zloop.c-157-\n--\ndrivers/block/zloop.c-184-\ndrivers/block/zloop.c:185:\tstruct zloop_zone\tzones[] __counted_by(nr_zones);\ndrivers/block/zloop.c-186-};\ndrivers/block/zloop.c-187-\ndrivers/block/zloop.c:188:struct zloop_cmd {\ndrivers/block/zloop.c-189-\tstruct work_struct\twork;\n--\ndrivers/block/zloop.c-197-\ndrivers/block/zloop.c:198:static DEFINE_IDR(zloop_index_idr);\ndrivers/block/zloop.c:199:static DEFINE_MUTEX(zloop_ctl_mutex);\ndrivers/block/zloop.c-200-\ndrivers/block/zloop.c=201=static unsigned int rq_zone_no(struct request *rq)\ndrivers/block/zloop.c-202-{\ndrivers/block/zloop.c:203:\tstruct zloop_device *zlo = rq-\u003eq-\u003equeuedata;\ndrivers/block/zloop.c-204-\n--\ndrivers/block/zloop.c-213- */\ndrivers/block/zloop.c:214:static inline void zloop_lru_rotate_open_zone(struct zloop_device *zlo,\ndrivers/block/zloop.c:215:\t\t\t\t\t struct zloop_zone *zone)\ndrivers/block/zloop.c-216-{\n--\ndrivers/block/zloop.c-224-\ndrivers/block/zloop.c:225:static inline void zloop_lru_remove_open_zone(struct zloop_device *zlo,\ndrivers/block/zloop.c:226:\t\t\t\t\t struct zloop_zone *zone)\ndrivers/block/zloop.c-227-{\n--\ndrivers/block/zloop.c-236-\ndrivers/block/zloop.c:237:static inline bool zloop_can_open_zone(struct zloop_device *zlo)\ndrivers/block/zloop.c-238-{\n--\ndrivers/block/zloop.c-246- */\ndrivers/block/zloop.c:247:static bool zloop_close_imp_open_zone(struct zloop_device *zlo)\ndrivers/block/zloop.c-248-{\ndrivers/block/zloop.c:249:\tstruct zloop_zone *zone;\ndrivers/block/zloop.c-250-\n--\ndrivers/block/zloop.c-252-\ndrivers/block/zloop.c:253:\tif (zloop_can_open_zone(zlo))\ndrivers/block/zloop.c-254-\t\treturn true;\n--\ndrivers/block/zloop.c-267-\ndrivers/block/zloop.c:268:static bool zloop_open_closed_or_empty_zone(struct zloop_device *zlo,\ndrivers/block/zloop.c:269:\t\t\t\t\t struct zloop_zone *zone,\ndrivers/block/zloop.c-270-\t\t\t\t\t bool explicit)\n--\ndrivers/block/zloop.c-278-\t\t */\ndrivers/block/zloop.c:279:\t\tif (!zloop_can_open_zone(zlo))\ndrivers/block/zloop.c-280-\t\t\tgoto fail;\n--\ndrivers/block/zloop.c-286-\t\t */\ndrivers/block/zloop.c:287:\t\tif (!zloop_close_imp_open_zone(zlo))\ndrivers/block/zloop.c-288-\t\t\tgoto fail;\n--\ndrivers/block/zloop.c-305-\ndrivers/block/zloop.c:306:static bool zloop_do_open_zone(struct zloop_device *zlo,\ndrivers/block/zloop.c:307:\t\t\t struct zloop_zone *zone, bool explicit)\ndrivers/block/zloop.c-308-{\n--\ndrivers/block/zloop.c-313-\t\t\tzone-\u003econd = BLK_ZONE_COND_EXP_OPEN;\ndrivers/block/zloop.c:314:\t\tzloop_lru_rotate_open_zone(zlo, zone);\ndrivers/block/zloop.c-315-\t\treturn true;\n--\ndrivers/block/zloop.c-317-\tcase BLK_ZONE_COND_CLOSED:\ndrivers/block/zloop.c:318:\t\treturn zloop_open_closed_or_empty_zone(zlo, zone, explicit);\ndrivers/block/zloop.c-319-\tdefault:\n--\ndrivers/block/zloop.c-323-\ndrivers/block/zloop.c:324:static void zloop_set_zone_cond(struct zloop_device *zlo,\ndrivers/block/zloop.c:325:\t\t\t\tstruct zloop_zone *zone,\ndrivers/block/zloop.c-326-\t\t\t\tenum blk_zone_cond cond)\n--\ndrivers/block/zloop.c-329-\ndrivers/block/zloop.c:330:\tzloop_lru_remove_open_zone(zlo, zone);\ndrivers/block/zloop.c-331-\tzone-\u003econd = cond;\n--\ndrivers/block/zloop.c-337-\ndrivers/block/zloop.c:338:static inline void zloop_set_zone_full(struct zloop_device *zlo,\ndrivers/block/zloop.c:339:\t\t\t\t struct zloop_zone *zone)\ndrivers/block/zloop.c-340-{\ndrivers/block/zloop.c:341:\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);\ndrivers/block/zloop.c-342-}\ndrivers/block/zloop.c-343-\ndrivers/block/zloop.c:344:static inline void zloop_set_zone_empty(struct zloop_device *zlo,\ndrivers/block/zloop.c:345:\t\t\t\t\tstruct zloop_zone *zone)\ndrivers/block/zloop.c-346-{\ndrivers/block/zloop.c:347:\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);\ndrivers/block/zloop.c-348-}\ndrivers/block/zloop.c-349-\ndrivers/block/zloop.c:350:static bool zloop_zone_is_offline_or_readonly(struct zloop_device *zlo,\ndrivers/block/zloop.c:351:\t\t\t\t\t struct zloop_zone *zone)\ndrivers/block/zloop.c-352-{\n--\ndrivers/block/zloop.c-362-\ndrivers/block/zloop.c:363:static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)\ndrivers/block/zloop.c-364-{\ndrivers/block/zloop.c:365:\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\ndrivers/block/zloop.c-366-\tstruct kstat stat;\n--\ndrivers/block/zloop.c-375-\t */\ndrivers/block/zloop.c:376:\tif (zloop_zone_is_offline_or_readonly(zlo, zone))\ndrivers/block/zloop.c-377-\t\treturn 0;\n--\ndrivers/block/zloop.c-401-\tif (!file_sectors) {\ndrivers/block/zloop.c:402:\t\tzloop_set_zone_empty(zlo, zone);\ndrivers/block/zloop.c-403-\t} else if (file_sectors == zlo-\u003ezone_capacity) {\ndrivers/block/zloop.c:404:\t\tzloop_set_zone_full(zlo, zone);\ndrivers/block/zloop.c-405-\t} else {\n--\ndrivers/block/zloop.c-415-\ndrivers/block/zloop.c:416:static int zloop_open_zone(struct zloop_device *zlo, unsigned int zone_no)\ndrivers/block/zloop.c-417-{\ndrivers/block/zloop.c:418:\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\ndrivers/block/zloop.c-419-\tint ret = 0;\n--\ndrivers/block/zloop.c-425-\ndrivers/block/zloop.c:426:\tif (zloop_zone_is_offline_or_readonly(zlo, zone)) {\ndrivers/block/zloop.c-427-\t\tret = -EIO;\n--\ndrivers/block/zloop.c-431-\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c:432:\t\tret = zloop_update_seq_zone(zlo, zone_no);\ndrivers/block/zloop.c-433-\t\tif (ret)\n--\ndrivers/block/zloop.c-436-\ndrivers/block/zloop.c:437:\tif (!zloop_do_open_zone(zlo, zone, true))\ndrivers/block/zloop.c-438-\t\tret = -EIO;\n--\ndrivers/block/zloop.c-445-\ndrivers/block/zloop.c:446:static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\ndrivers/block/zloop.c-447-{\ndrivers/block/zloop.c:448:\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\ndrivers/block/zloop.c-449-\tint ret = 0;\n--\ndrivers/block/zloop.c-455-\ndrivers/block/zloop.c:456:\tif (zloop_zone_is_offline_or_readonly(zlo, zone)) {\ndrivers/block/zloop.c-457-\t\tret = -EIO;\n--\ndrivers/block/zloop.c-461-\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\ndrivers/block/zloop.c:462:\t\tret = zloop_update_seq_zone(zlo, zone_no);\ndrivers/block/zloop.c-463-\t\tif (ret)\n--\ndrivers/block/zloop.c-472-\t\tspin_lock(\u0026zone-\u003ewp_lock);\ndrivers/block/zloop.c:473:\t\tzloop_lru_remove_open_zone(zlo, zone);\ndrivers/block/zloop.c-474-\t\tif (zone-\u003ewp == zone-\u003estart)\n--\ndrivers/block/zloop.c-492-\ndrivers/block/zloop.c:493:static int zloop_do_reset_zone(struct zloop_device *zlo,\ndrivers/block/zloop.c:494:\t\t\t struct zloop_zone *zone)\ndrivers/block/zloop.c-495-{\n--\ndrivers/block/zloop.c-499-\tspin_lock(\u0026zone-\u003ewp_lock);\n\n"
} |
| 15/2 | 2026/10/09 11:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 42975 output: 35 thoughts: 3126 Thoughts: **Deep Dive into Zoned Block Device Storage Element Management** Alright, let's dissect this new storage element management feature for zoned block devices. It seems comprehensive, introducing ioctls, methods, and SCSI-layer hooks, along with virtual emulation via the `zloop` driver. I need to be thorough and look for subtle issues, starting with potential race conditions, memory management, and those tricky locking scenarios. First, `bdev_remove_storage_element`. The use of `mutex_lock(&disk->zone_revalidate_mutex)` is standard, but the `blk_mq_freeze_queue` followed by the `msleep(1000)` *while holding* the mutex raises an immediate red flag. This can lead to a significant latency issue, potentially blocking operations that depend on zone revalidation. The `msleep` within a locked section also worries me; it can be a source of priority inversion if another task has a higher priority. In `zloop.c`, I see a two-second delay on the removal work and a five-second delay on the restoration. This could be problematic combined with the mutex. I need to ensure there are no unintended consequences. The `zloop_queue_rq` function adds a bit of safety with the `READ_ONCE(zlo->restore_in_progress)` check to prevent further I/O errors. Moving to the user-space interaction through `blkdev_remove_storage_element_ioctl` and `blkdev_report_storage_elements_ioctl`, I'm looking carefully at potential vulnerabilities. The `blkdev_remove_storage_element_ioctl` function calls the `bdev_remove_storage_element` function, which is promising, but I am particularly concerned about the `copy_from_user` and `copy_to_user` calls in `blkdev_report_storage_elements_ioctl`. Buffer overflows are a classic issue here. A user-space program could send a smaller buffer than expected, or provide a partial buffer and cause the system to read out of bounds. The `struct_size_t` calculation needs to be scrutinized for potential integer overflows, especially on 32-bit systems or when dealing with a large number of elements. Now, let's dive into the `zloop` driver. The `zloop_create_storage_elements` function combined with `zloop_prep_rq` has interesting interactions with storage elements. I see the potential for integer underflow when dealing with indexes to the `zlo->elements` array, using `zone->wr_se_id - 1` and `zone->rd_se_id - 1` without sufficient checks. What happens if those IDs are zero? The code is also not accounting for conventional zones when initializing the zone metadata. I wonder if there is a use-after-free scenario. This warrants careful review. The implementation of `zloop_remove_element` is the most concerning. While it checks for invalid `element_id` values, it doesn't check for underflow *before* indexing the `zlo->elements` array. Specifically, the line `se = &zlo->elements[element_id - 1]` can lead to an out-of-bounds access. The code relies on the caller checking `element_id != 0`, but the code could be triggered with element ID of zero internally. The code later, in `zloop_remove_element_work` also uses `zlo->remove_element_id - 1` to access an array index. This is a very serious bug that needs immediate attention. Also, `zloop_free_disk` cancels delayed work. This raises the question of what happens if that deletion takes place while the worker is running. |
| 16/2 | 2026/10/09 11:31 | tool | grepper |
0mArgs:{
"Expression": "zloop_ctl_remove",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=1947=static void zloop_forget_cache(struct zloop_device *zlo)\n--\ndrivers/block/zloop.c-1985-\ndrivers/block/zloop.c:1986:static int zloop_ctl_remove(struct zloop_options *opts)\ndrivers/block/zloop.c-1987-{\n--\ndrivers/block/zloop.c=2289=static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,\n--\ndrivers/block/zloop.c-2330-\tcase ZLOOP_CTL_REMOVE:\ndrivers/block/zloop.c:2331:\t\tret = zloop_ctl_remove(\u0026opts);\ndrivers/block/zloop.c-2332-\t\tbreak;\n"
} |
| 17/2 | 2026/10/09 11:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 47155 output: 37 thoughts: 12 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing `zloop_ctl_remove`** Alright, let's dive into this `zloop_ctl_remove` function. I need to understand its role in the system. The naming convention suggests it's involved in removing something, likely related to the `zloop` component, so I'm expecting some kind of control structure or metadata being targeted here. My initial questions are: what exactly is being removed, how is it identified, and what are the implications of a successful or failed removal? Are there any potential race conditions I should be on the lookout for? More broadly, what's the broader architectural context of this removal operation? Is it a key path? An infrequently used cleanup routine? Understanding the calling context and the expected state after the call is critical. Let's see how this function handles locks, memory management and how it interacts with other components, especially concurrent access or resource release. I'll be looking to see if proper error handling and resource cleanup are implemented to prevent resource leaks and potential corruption. |
| 18/2 | 2026/10/09 11:31 | tool | read-file |
0mArgs:{
"File": "drivers/block/zloop.c",
"FirstLine": 1985,
"LineCount": 50
}Results: {
"Contents": "1985:\t\n1986:\tstatic int zloop_ctl_remove(struct zloop_options *opts)\n1987:\t{\n1988:\t\tstruct zloop_device *zlo;\n1989:\t\tint ret;\n1990:\t\n1991:\t\tif (!(opts-\u003emask \u0026 ZLOOP_OPT_ID)) {\n1992:\t\t\tpr_err(\"No ID specified for remove\\n\");\n1993:\t\t\treturn -EINVAL;\n1994:\t\t}\n1995:\t\n1996:\t\tif (opts-\u003emask \u0026 ~ZLOOP_OPT_ID) {\n1997:\t\t\tpr_err(\"Invalid option specified for remove\\n\");\n1998:\t\t\treturn -EINVAL;\n1999:\t\t}\n2000:\t\n2001:\t\tret = mutex_lock_killable(\u0026zloop_ctl_mutex);\n2002:\t\tif (ret)\n2003:\t\t\treturn ret;\n2004:\t\n2005:\t\tzlo = idr_find(\u0026zloop_index_idr, opts-\u003eid);\n2006:\t\tif (!zlo || zlo-\u003estate == Zlo_creating) {\n2007:\t\t\tret = -ENODEV;\n2008:\t\t} else if (zlo-\u003estate == Zlo_deleting) {\n2009:\t\t\tret = -EINVAL;\n2010:\t\t} else {\n2011:\t\t\tidr_remove(\u0026zloop_index_idr, zlo-\u003eid);\n2012:\t\t\tWRITE_ONCE(zlo-\u003estate, Zlo_deleting);\n2013:\t\t}\n2014:\t\n2015:\t\tmutex_unlock(\u0026zloop_ctl_mutex);\n2016:\t\tif (ret)\n2017:\t\t\treturn ret;\n2018:\t\n2019:\t\tdel_gendisk(zlo-\u003edisk);\n2020:\t\n2021:\t\tif (zlo-\u003ediscard_write_cache)\n2022:\t\t\tzloop_forget_cache(zlo);\n2023:\t\n2024:\t\tput_disk(zlo-\u003edisk);\n2025:\t\n2026:\t\tpr_info(\"Removed device %d\\n\", opts-\u003eid);\n2027:\t\n2028:\t\tmodule_put(THIS_MODULE);\n2029:\t\n2030:\t\treturn 0;\n2031:\t}\n2032:\t\n2033:\tstatic int zloop_ctl_degrade_element(struct zloop_options *opts)\n2034:\t{\n"
} |
| 19/2 | 2026/10/09 11:31 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 48124 output: 30 thoughts: 2953 Thoughts: **Deep Dive into Zoned Block Device Concurrency and Potential Vulnerabilities** Okay, so I'm diving into this zoned block device code, and my initial concern revolves around the potential race conditions that can arise when a disk is being managed, specifically during the `del_gendisk` process and concurrent operations like removing or restoring storage elements. The core issue is that `del_gendisk` calls `zloop_free_disk` which relies on a refcount dropping to zero, but `bdev_remove_storage_element` or `bdev_restore_storage_elements` might be running concurrently, and these functions call `disk_wait_for_se_mgmt_completion`, creating a potential for a delayed cleanup if the device is unmounted or deleted. I'm focusing on the `disk_wait_for_se_mgmt_completion` function because it involves a loop with `msleep(1000)` and relies on polling a flag (`in_progress`) determined by `disk_se_mgmt_in_progress`. The next point to analyze is `disk_se_mgmt_in_progress`. This function calls `disk->fops->se_ops->report_elements` which reports the status of the storage elements. It then checks these elements' status and sets `in_progress` if elements are in removal or restore. But, the key question here is whether the device could be deleted while `disk_se_mgmt_in_progress` is running. `bdev_remove_storage_element` and `bdev_restore_storage_elements` involve freezing and unfreezing the queue using `blk_mq_freeze_queue` and `blk_mq_unfreeze_queue`, which could be problematic with concurrent I/Os and ioctls. I'm particularly interested in `blkdev_remove_storage_element_ioctl` which flushes and truncates and then calls `bdev_remove_storage_element`. There's also a potential issue if `BLKREPORTSTORELEMS` is called concurrently with `BLKREMOVESTORELEM` or `BLKRESTORESTORELEMS`. The `blkdev_report_storage_elements_ioctl` function uses `copy_to_user` to transfer information about storage elements. Specifically, there's a problem if `rep->nr_elements` is modified in `bdev_report_storage_elements` after `copy_from_user` and before `copy_to_user`. A malicious user can pass a small buffer and the kernel will copy out of bounds of the buffer. The `bdev_report_storage_elements` function itself needs scrutiny. It checks `nr_elements` after a pointer is passed to it, and also sets it, which introduces a potential use-after-free scenario. This could lead to a buffer overflow on the user side if the user allocates too small a buffer, for example by passing `sizeof(struct blk_se_report)` while the device has `disk->nr_storage_elements > 1`. Now, I'm shifting my focus to `sd_zbc.c`, looking at how storage element reports are handled specifically in the SCSI subsystem, `sd_zbc_report_storage_elements`. The code uses a buffer (`buf`) and the device returns information. The code calculates `nr_se` and `nr_descs` and then does the assignment. If the device reports a `nr_descs > nr_se` it will loop less times than expected. Conversely, the device could send less data than `buf_size`. Also `nr_descs` comes from the device, and the data may be incomplete or invalid, so it's essential to validate the returned data. Also, it is crucial to analyze whether the device returns fewer bytes than the size of the buffer that was allocated via `kzalloc`. Finally, the `disk_zone_is_offline` function, used to check offline zones, is critical. This is relevant to prevent read/write to offline zones of ZBC disks. |
| 20/2 | 2026/10/09 11:32 | tool | grepper |
0mArgs:{
"Expression": "disk_zone_get_cond",
"PathPrefixes": [
"block/"
]
}Results: {
"Output": "block/blk-zoned.c=251=static inline u8 disk_zone_get_state(struct gendisk *disk, sector_t sector)\n--\nblock/blk-zoned.c-266-\nblock/blk-zoned.c:267:static enum blk_zone_cond disk_zone_get_cond(struct gendisk *disk,\nblock/blk-zoned.c-268-\t\t\t\t\t sector_t sector)\n--\nblock/blk-zoned.c=282=static inline bool disk_zone_is_offline_or_readonly(struct gendisk *disk,\n--\nblock/blk-zoned.c-284-{\nblock/blk-zoned.c:285:\tenum blk_zone_cond cond = disk_zone_get_cond(disk, sector);\nblock/blk-zoned.c-286-\n--\nblock/blk-zoned.c=304=bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)\n--\nblock/blk-zoned.c-308-\nblock/blk-zoned.c:309:\treturn disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;\nblock/blk-zoned.c-310-}\n"
} |
| 21/2 | 2026/10/09 11:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52286 output: 35 thoughts: 30 Thoughts: Okay, let's dive into this. Here's my take on understanding this code: **Analyzing `disk_zone_get_cond` in `block/blk-zoned.c`** Alright, I'm going to take a look at the `disk_zone_get_cond` function within the `block/blk-zoned.c` file, specifically around line 251. I need to get a clear picture of how this function determines the condition of a zoned block device. I know this function is crucial for zone management, so understanding its logic is key to comprehending the overall zoned block device operation. I need to figure out what criteria this function is using to assess the current status of a specific zone and how it's mapping that status. It's probably going to involve looking at things like the zone's write pointer, capacity, and maybe some internal flags related to the zoned block device's metadata. My goal is to extract the logic to understand how the system reports the zone's health and whether it's ready for various operations. |
| 22/2 | 2026/10/09 11:32 | tool | read-file |
0mArgs:{
"File": "block/blk-zoned.c",
"FirstLine": 250,
"LineCount": 40
}Results: {
"Contents": " 250:\t\n 251:\tstatic inline u8 disk_zone_get_state(struct gendisk *disk, sector_t sector)\n 252:\t{\n 253:\t\tunsigned int zno = disk_zone_no(disk, sector);\n 254:\t\tu8 *zones_state, zs;\n 255:\t\n 256:\t\trcu_read_lock();\n 257:\t\tzones_state = rcu_dereference(disk-\u003ezones_state);\n 258:\t\tif (likely(zones_state \u0026\u0026 zno \u003c disk-\u003enr_zones))\n 259:\t\t\tzs = zones_state[zno];\n 260:\t\telse\n 261:\t\t\tzs = BLK_ZFLAG_CONV;\n 262:\t\trcu_read_unlock();\n 263:\t\n 264:\t\treturn zs;\n 265:\t}\n 266:\t\n 267:\tstatic enum blk_zone_cond disk_zone_get_cond(struct gendisk *disk,\n 268:\t\t\t\t\t\t sector_t sector)\n 269:\t{\n 270:\t\tu8 zs = disk_zone_get_state(disk, sector);\n 271:\t\n 272:\t\treturn blk_zstate_to_zone_cond(zs);\n 273:\t}\n 274:\t\n 275:\tstatic inline bool\n 276:\tdisk_zone_cond_is_offline_or_readonly(enum blk_zone_cond cond)\n 277:\t{\n 278:\t\treturn cond == BLK_ZONE_COND_READONLY ||\n 279:\t\t\tcond == BLK_ZONE_COND_OFFLINE;\n 280:\t}\n 281:\t\n 282:\tstatic inline bool disk_zone_is_offline_or_readonly(struct gendisk *disk,\n 283:\t\t\t\t\t\t\t sector_t sector)\n 284:\t{\n 285:\t\tenum blk_zone_cond cond = disk_zone_get_cond(disk, sector);\n 286:\t\n 287:\t\treturn disk_zone_cond_is_offline_or_readonly(cond);\n 288:\t}\n 289:\t\n"
} |
| 23/2 | 2026/10/09 11:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53170 output: 32 thoughts: 926 Thoughts: **Analyzing Storage Element Creation in `zloop.c`** Okay, let's dive into this. I'm thinking about the implications of how storage elements are created within `zloop.c`, specifically when `zloop_ctl_add` is invoked with `stor_elements` set. I need to understand how the number of storage elements is determined. The function `zloop_create_storage_elements` is the key here. First, I see the code calculates `nr_elements` based on `zlo->nr_zones`, ensuring a minimum of 2 and a maximum of 32 storage elements, with at least 32 zones per element. That makes sense, keeping things relatively realistic during emulation. Then, it considers whether we are emulating pairs of read and write storage elements. If `zlo->stor_elements` is set to pairs, it doubles the number of storage elements to create. The code then allocates and initializes the `blk_se` structures accordingly, setting IDs, types (read/write), and status. It handles pairing the read and write elements if that's enabled. It also sets `max_nr_removed_elements`, which has a special value if it's pairs. However, I've got a critical thought. What if `zlo->nr_zones` is either 0 or 1? The calculation of `nr_elements` would result in 2. But, is `nr_zones` validated anywhere else? I need to track down the validation for `nr_zones` to ensure this edge case is properly handled. I should check `zloop_ctl_add` and then `zloop_parse_options`, since they appear to be involved in the zone configuration. |
| 24/2 | 2026/10/09 11:32 | tool | grepper |
0mArgs:{
"Expression": "nr_zones",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=144=struct zloop_device {\n--\ndrivers/block/zloop.c-163-\tsector_t\t\tzone_capacity;\ndrivers/block/zloop.c:164:\tunsigned int\t\tnr_zones;\ndrivers/block/zloop.c-165-\tunsigned int\t\tnr_conv_zones;\n--\ndrivers/block/zloop.c-184-\ndrivers/block/zloop.c:185:\tstruct zloop_zone\tzones[] __counted_by(nr_zones);\ndrivers/block/zloop.c-186-};\n--\ndrivers/block/zloop.c=538=static int zloop_reset_all_zones(struct zloop_device *zlo)\n--\ndrivers/block/zloop.c-542-\ndrivers/block/zloop.c:543:\tfor (i = zlo-\u003enr_conv_zones; i \u003c zlo-\u003enr_zones; i++) {\ndrivers/block/zloop.c-544-\t\tret = zloop_reset_zone(zlo, i, true);\n--\ndrivers/block/zloop.c=723=static void zloop_rw(struct zloop_cmd *cmd)\n--\ndrivers/block/zloop.c-742-\t/* We should never get an I/O beyond the device capacity. */\ndrivers/block/zloop.c:743:\tif (WARN_ON_ONCE(zone_no \u003e= zlo-\u003enr_zones))\ndrivers/block/zloop.c-744-\t\tgoto out;\n--\ndrivers/block/zloop.c=791=static int zloop_record_safe_wps(struct zloop_device *zlo)\n--\ndrivers/block/zloop.c-795-\ndrivers/block/zloop.c:796:\tfor (i = 0; i \u003c zlo-\u003enr_zones; i++) {\ndrivers/block/zloop.c-797-\t\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[i];\n--\ndrivers/block/zloop.c=1093=static int zloop_report_zones(struct gendisk *disk, sector_t sector,\ndrivers/block/zloop.c:1094:\t\tunsigned int nr_zones, struct blk_report_zones_args *args)\ndrivers/block/zloop.c-1095-{\n--\ndrivers/block/zloop.c-1101-\tfirst = disk_zone_no(disk, sector);\ndrivers/block/zloop.c:1102:\tif (first \u003e= zlo-\u003enr_zones)\ndrivers/block/zloop.c-1103-\t\treturn 0;\ndrivers/block/zloop.c:1104:\tnr_zones = min(nr_zones, zlo-\u003enr_zones - first);\ndrivers/block/zloop.c-1105-\ndrivers/block/zloop.c:1106:\tfor (i = 0; i \u003c nr_zones; i++) {\ndrivers/block/zloop.c-1107-\t\tunsigned int zone_no = first + i;\n--\ndrivers/block/zloop.c-1140-\ndrivers/block/zloop.c:1141:\treturn nr_zones;\ndrivers/block/zloop.c-1142-}\n--\ndrivers/block/zloop.c=1206=static void zloop_remove_element_work(struct work_struct *work)\n--\ndrivers/block/zloop.c-1240-\t */\ndrivers/block/zloop.c:1241:\tfor (i = 0, zone = zlo-\u003ezones; i \u003c zlo-\u003enr_zones; i++, zone++) {\ndrivers/block/zloop.c-1242-\t\tif (zone-\u003ewr_se_id != se-\u003eid \u0026\u0026 zone-\u003erd_se_id != se-\u003eid)\n--\ndrivers/block/zloop.c=1353=static void zloop_restore_elements_work(struct work_struct *work)\n--\ndrivers/block/zloop.c-1364-\t/* Reset all zones. */\ndrivers/block/zloop.c:1365:\tfor (i = 0, zone = zlo-\u003ezones; i \u003c zlo-\u003enr_zones; i++, zone++) {\ndrivers/block/zloop.c-1366-\t\tif (test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags)) {\n--\ndrivers/block/zloop.c=1429=static void zloop_free_disk(struct gendisk *disk)\n--\ndrivers/block/zloop.c-1438-\ndrivers/block/zloop.c:1439:\tfor (i = 0; i \u003c zlo-\u003enr_zones; i++) {\ndrivers/block/zloop.c-1440-\t\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[i];\n--\ndrivers/block/zloop.c=1531=static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,\n--\ndrivers/block/zloop.c-1557-\t\tse_idx = zone_no % nr_elems;\ndrivers/block/zloop.c:1558:\t\tzlo-\u003eelements[se_idx].nr_zones++;\ndrivers/block/zloop.c-1559-\n--\ndrivers/block/zloop.c-1563-\t\t\tzone-\u003erd_se_id += nr_elems;\ndrivers/block/zloop.c:1564:\t\t\tzlo-\u003eelements[zone-\u003erd_se_id - 1].nr_zones =\ndrivers/block/zloop.c:1565:\t\t\t\tzlo-\u003eelements[se_idx].nr_zones;\ndrivers/block/zloop.c-1566-\t\t}\n--\ndrivers/block/zloop.c=1643=static int zloop_create_storage_elements(struct zloop_device *zlo)\n--\ndrivers/block/zloop.c-1653-\t */\ndrivers/block/zloop.c:1654:\tif (zlo-\u003enr_zones \u003c= 64)\ndrivers/block/zloop.c-1655-\t\tnr_elements = 2;\n--\ndrivers/block/zloop.c-1658-\t\t\tmin(ZLOOP_MAX_STOR_ELEMENTS,\ndrivers/block/zloop.c:1659:\t\t\t zlo-\u003enr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);\ndrivers/block/zloop.c-1660-\n--\ndrivers/block/zloop.c=1731=static int zloop_ctl_add(struct zloop_options *opts)\n--\ndrivers/block/zloop.c-1738-\t};\ndrivers/block/zloop.c:1739:\tunsigned int nr_zones, i, j;\ndrivers/block/zloop.c-1740-\tstruct zloop_device *zlo;\n--\ndrivers/block/zloop.c-1745-\ndrivers/block/zloop.c:1746:\tnr_zones = opts-\u003ecapacity \u003e\u003e ilog2(opts-\u003ezone_size);\ndrivers/block/zloop.c:1747:\tif (opts-\u003enr_conv_zones \u003e= nr_zones) {\ndrivers/block/zloop.c-1748-\t\tpr_err(\"Invalid number of conventional zones %u\\n\",\n--\ndrivers/block/zloop.c-1752-\ndrivers/block/zloop.c:1753:\tif (opts-\u003emax_open_zones \u003e nr_zones - opts-\u003enr_conv_zones) {\ndrivers/block/zloop.c-1754-\t\tpr_err(\"Invalid maximum number of open zones %u\\n\",\n--\ndrivers/block/zloop.c-1758-\ndrivers/block/zloop.c:1759:\tzlo = kvzalloc_flex(*zlo, zones, nr_zones);\ndrivers/block/zloop.c-1760-\tif (!zlo) {\n--\ndrivers/block/zloop.c-1796-\t\tzlo-\u003ezone_capacity = zlo-\u003ezone_size;\ndrivers/block/zloop.c:1797:\tzlo-\u003enr_zones = nr_zones;\ndrivers/block/zloop.c-1798-\tzlo-\u003enr_conv_zones = opts-\u003enr_conv_zones;\n--\ndrivers/block/zloop.c-1843-\trestore = zloop_dev_exists(zlo);\ndrivers/block/zloop.c:1844:\tfor (i = 0; i \u003c nr_zones; i++) {\ndrivers/block/zloop.c-1845-\t\tret = zloop_init_zone(zlo, opts, i, restore);\n--\ndrivers/block/zloop.c-1882-\tsprintf(zlo-\u003edisk-\u003edisk_name, \"zloop%d\", zlo-\u003eid);\ndrivers/block/zloop.c:1883:\tset_capacity(zlo-\u003edisk, (u64)lim.chunk_sectors * zlo-\u003enr_zones);\ndrivers/block/zloop.c-1884-\n--\ndrivers/block/zloop.c-1899-\tpr_info(\"zloop: device %d, %u zones of %llu MiB, %u B block size\\n\",\ndrivers/block/zloop.c:1900:\t\tzlo-\u003eid, zlo-\u003enr_zones,\ndrivers/block/zloop.c-1901-\t\t((sector_t)zlo-\u003ezone_size \u003c\u003c SECTOR_SHIFT) \u003e\u003e 20,\n--\ndrivers/block/zloop.c=1947=static void zloop_forget_cache(struct zloop_device *zlo)\n--\ndrivers/block/zloop.c-1953-\ndrivers/block/zloop.c:1954:\tfor (i = 0; i \u003c zlo-\u003enr_zones; i++) {\ndrivers/block/zloop.c-1955-\t\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[i];\n"
} |
| 25/2 | 2026/10/09 11:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 57037 output: 33 thoughts: 5990 Thoughts: **Deep Dive into Zloop's Storage Element Management** My analysis has revealed several potential race conditions and inconsistencies within the Zloop device driver, particularly around storage element removal and zone health management. Initially, I identified a potential integer overflow issue in `zloop_ctl_add` where an invalid number of zones could be created, leading to an error. I traced the calculations for `nr_zones`, `nr_elements`, and `max_nr_removed_elements`, noting their dependence on `stor_elements` configuration, especially within `zloop_init_zone`. The investigation then moved to `zloop_remove_element`, where I questioned the possibility of `element_id` being zero. While a check exists in the `bdev_remove_storage_element` calling function, a broader concern remains: could `zloop_remove_element` be called from an unexpected path? This led to an analysis of `zloop_degrade_element` and `zloop_ctl_degrade_element` and the possible implications of default values. A critical point emerged when examining `zloop_prep_rq`. I became concerned about the lack of boundary checks on `rq_zone_no(rq)`, especially if `blk_rq_pos(rq)` might be beyond the valid range. While `zloop_rw` contains a WARN_ON_ONCE to handle the out-of-bounds access, I had concerns that this still wouldn't protect other accesses from concurrent accesses. I then focused on how the queue is handled during a restore, analyzing `zloop_queue_rq`. In `zloop_queue_rq`, an `IOERR` can be returned without starting the request. Further review of `zloop_reset_zone` and `zloop_restore_elements_work` found that `zloop_reset_all_zones` will skip offline zones, matching the specification. The more important discovery was in the failure path of `zloop_restore_elements_work`, where the `RESTORE_IN_PROGRESS` state could persist indefinitely if a zone restore fails, potentially leading to a device hang due to the infinite loop in `disk_wait_for_se_mgmt_completion`. My critical area of investigation now focuses on `bdev_remove_storage_element` and the interactions of `disk_wait_for_se_mgmt_completion`, `disk_revalidate_zones`, and locking mechanisms. I noticed that the process might be interrupted by a signal, leading to the early unlocking of `disk->zone_revalidate_mutex` before `disk_revalidate_zones` is called, leaving the disk and the driver's zone states out of sync. This creates a potential vulnerability where an asynchronous work item, `zloop_remove_element_work`, later modifies zones, but the updated state is not reflected in the block layer, opening the door for inconsistent I/O behavior. I found that I/Os could be running concurrently during the change of state. Freezing and unfreezing the queue before an operation can also lead to race conditions. |
| 26/2 | 2026/10/09 11:32 | tool | grepper |
0mArgs:{
"Expression": "zloop_rw",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=589=static void zloop_put_cmd(struct zloop_cmd *cmd)\n--\ndrivers/block/zloop.c-600-\ndrivers/block/zloop.c:601:static void zloop_rw_complete(struct kiocb *iocb, long ret)\ndrivers/block/zloop.c-602-{\n--\ndrivers/block/zloop.c=609=static int zloop_do_rw(struct zloop_cmd *cmd)\n--\ndrivers/block/zloop.c-651-\tcmd-\u003eiocb.ki_filp = zone-\u003efile;\ndrivers/block/zloop.c:652:\tcmd-\u003eiocb.ki_complete = zloop_rw_complete;\ndrivers/block/zloop.c-653-\tif (!zlo-\u003ebuffered_io)\n--\ndrivers/block/zloop.c=662=static int zloop_seq_write_prep(struct zloop_cmd *cmd)\n--\ndrivers/block/zloop.c-722-\ndrivers/block/zloop.c:723:static void zloop_rw(struct zloop_cmd *cmd)\ndrivers/block/zloop.c-724-{\n--\ndrivers/block/zloop.c-774-\tif (ret != -EIOCBQUEUED)\ndrivers/block/zloop.c:775:\t\tzloop_rw_complete(\u0026cmd-\u003eiocb, ret);\ndrivers/block/zloop.c-776-\tzloop_put_cmd(cmd);\n--\ndrivers/block/zloop.c=835=static void zloop_handle_cmd(struct zloop_cmd *cmd)\n--\ndrivers/block/zloop.c-848-\t\t/*\ndrivers/block/zloop.c:849:\t\t * zloop_rw() always executes asynchronously or completes\ndrivers/block/zloop.c-850-\t\t * directly.\ndrivers/block/zloop.c-851-\t\t */\ndrivers/block/zloop.c:852:\t\tzloop_rw(cmd);\ndrivers/block/zloop.c-853-\t\treturn;\n"
} |
| 27/2 | 2026/10/09 11:32 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 64267 output: 36 thoughts: 21 Thoughts: Okay, let's dive into this. Here's my take on understanding this code: **Analyzing the zloop_rw Function** Right, so I'm focusing on `zloop_rw` within `drivers/block/zloop.c`. Let me get a clear picture of what this function is doing. I need to understand its role within the zloop device driver, particularly concerning read/write operations. I'm already thinking about its likely arguments: file pointers, buffer pointers, sizes, and a flag perhaps indicating a read or a write. Given it's a block device driver, I anticipate this function is the heart of data transfer to and from the virtual ZLoop device. I'll be looking for how it interacts with the underlying storage – likely using a `request_queue` or similar mechanism if it’s a modern driver. I expect to see the code handling error conditions, memory allocation/deallocation (if any), and perhaps some buffering or caching strategies specific to ZLoop's design. I'll need to pay close attention to potential race conditions or locking mechanisms to ensure data integrity during concurrent accesses. I'll look for any optimizations or unusual techniques used, as that's often where the interesting parts are! |
| 28/2 | 2026/10/09 11:33 | tool | read-file |
0mArgs:{
"File": "drivers/block/zloop.c",
"FirstLine": 720,
"LineCount": 60
}Results: {
"Contents": " 720:\t\treturn ret;\n 721:\t}\n 722:\t\n 723:\tstatic void zloop_rw(struct zloop_cmd *cmd)\n 724:\t{\n 725:\t\tstruct request *rq = blk_mq_rq_from_pdu(cmd);\n 726:\t\tstruct zloop_device *zlo = rq-\u003eq-\u003equeuedata;\n 727:\t\tunsigned int zone_no = rq_zone_no(rq);\n 728:\t\tsector_t nr_sectors = blk_rq_sectors(rq);\n 729:\t\tbool is_append = req_op(rq) == REQ_OP_ZONE_APPEND;\n 730:\t\tbool is_write = req_op(rq) == REQ_OP_WRITE || is_append;\n 731:\t\tstruct zloop_zone *zone;\n 732:\t\tint ret = -EIO;\n 733:\t\n 734:\t\tatomic_set(\u0026cmd-\u003eref, 2);\n 735:\t\tcmd-\u003esector = blk_rq_pos(rq);\n 736:\t\tcmd-\u003enr_sectors = nr_sectors;\n 737:\t\tcmd-\u003eret = 0;\n 738:\t\n 739:\t\tif (WARN_ON_ONCE(is_append \u0026\u0026 !zlo-\u003ezone_append))\n 740:\t\t\tgoto out;\n 741:\t\n 742:\t\t/* We should never get an I/O beyond the device capacity. */\n 743:\t\tif (WARN_ON_ONCE(zone_no \u003e= zlo-\u003enr_zones))\n 744:\t\t\tgoto out;\n 745:\t\n 746:\t\tzone = \u0026zlo-\u003ezones[zone_no];\n 747:\t\n 748:\t\t/*\n 749:\t\t * The block layer should never send requests that are not fully\n 750:\t\t * contained within the zone.\n 751:\t\t */\n 752:\t\tif (WARN_ON_ONCE(cmd-\u003esector + nr_sectors \u003e\n 753:\t\t\t\t zone-\u003estart + zlo-\u003ezone_size))\n 754:\t\t\tgoto out;\n 755:\t\n 756:\t\tif (test_and_clear_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags)) {\n 757:\t\t\tmutex_lock(\u0026zone-\u003elock);\n 758:\t\t\tret = zloop_update_seq_zone(zlo, zone_no);\n 759:\t\t\tmutex_unlock(\u0026zone-\u003elock);\n 760:\t\t\tif (ret)\n 761:\t\t\t\tgoto out;\n 762:\t\t}\n 763:\t\n 764:\t\tif (!test_bit(ZLOOP_ZONE_CONV, \u0026zone-\u003eflags) \u0026\u0026 is_write) {\n 765:\t\t\tmutex_lock(\u0026zone-\u003elock);\n 766:\t\t\tret = zloop_seq_write_prep(cmd);\n 767:\t\t\tif (!ret)\n 768:\t\t\t\tret = zloop_do_rw(cmd);\n 769:\t\t\tmutex_unlock(\u0026zone-\u003elock);\n 770:\t\t} else {\n 771:\t\t\tret = zloop_do_rw(cmd);\n 772:\t\t}\n 773:\tout:\n 774:\t\tif (ret != -EIOCBQUEUED)\n 775:\t\t\tzloop_rw_complete(\u0026cmd-\u003eiocb, ret);\n 776:\t\tzloop_put_cmd(cmd);\n 777:\t}\n 778:\t\n 779:\tstatic inline bool zloop_zone_is_active(struct zloop_zone *zone)\n"
} |
| 29/2 | 2026/10/09 11:33 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 65532 output: 36 thoughts: 34 Thoughts: Here's a summary of my thought process: **Diving into `zloop_seq_write_prep`** Alright, let's get down to brass tacks. I need to understand what `zloop_seq_write_prep` actually *does*. Time to get my hands dirty and examine the code. My immediate focus is on lines 662 through 720. That's where the core functionality should reside, so let's parse that code and see what insights we can uncover. Hopefully, the code comments are descriptive. I can almost feel the information percolating now. |
| 30/2 | 2026/10/09 11:33 | tool | read-file |
0mArgs:{
"File": "drivers/block/zloop.c",
"FirstLine": 662,
"LineCount": 60
}Results: {
"Contents": " 662:\tstatic int zloop_seq_write_prep(struct zloop_cmd *cmd)\n 663:\t{\n 664:\t\tstruct request *rq = blk_mq_rq_from_pdu(cmd);\n 665:\t\tstruct zloop_device *zlo = rq-\u003eq-\u003equeuedata;\n 666:\t\tunsigned int zone_no = rq_zone_no(rq);\n 667:\t\tsector_t nr_sectors = blk_rq_sectors(rq);\n 668:\t\tbool is_append = req_op(rq) == REQ_OP_ZONE_APPEND;\n 669:\t\tstruct zloop_zone *zone = \u0026zlo-\u003ezones[zone_no];\n 670:\t\tsector_t zone_end = zone-\u003estart + zlo-\u003ezone_capacity;\n 671:\t\tint ret = 0;\n 672:\t\n 673:\t\tspin_lock(\u0026zone-\u003ewp_lock);\n 674:\t\n 675:\t\t/*\n 676:\t\t * Zone append operations always go at the current write pointer, but\n 677:\t\t * regular write operations must already be aligned to the write pointer\n 678:\t\t * when submitted.\n 679:\t\t */\n 680:\t\tif (is_append) {\n 681:\t\t\t/*\n 682:\t\t\t * If ordered zone append is in use, we already checked and set\n 683:\t\t\t * the target sector in zloop_queue_rq().\n 684:\t\t\t */\n 685:\t\t\tif (!zlo-\u003eordered_zone_append) {\n 686:\t\t\t\tif (zone-\u003econd == BLK_ZONE_COND_FULL ||\n 687:\t\t\t\t zone-\u003ewp + nr_sectors \u003e zone_end) {\n 688:\t\t\t\t\tret = -EIO;\n 689:\t\t\t\t\tgoto out_unlock;\n 690:\t\t\t\t}\n 691:\t\t\t\tcmd-\u003esector = zone-\u003ewp;\n 692:\t\t\t}\n 693:\t\t} else {\n 694:\t\t\tif (cmd-\u003esector != zone-\u003ewp) {\n 695:\t\t\t\tpr_err(\"Zone %u: unaligned write: sect %llu, wp %llu\\n\",\n 696:\t\t\t\t zone_no, cmd-\u003esector, zone-\u003ewp);\n 697:\t\t\t\tret = -EIO;\n 698:\t\t\t\tgoto out_unlock;\n 699:\t\t\t}\n 700:\t\t}\n 701:\t\n 702:\t\t/* Implicitly open the target zone. */\n 703:\t\tif (!zloop_do_open_zone(zlo, zone, false)) {\n 704:\t\t\tret = -EIO;\n 705:\t\t\tgoto out_unlock;\n 706:\t\t}\n 707:\t\n 708:\t\t/*\n 709:\t\t * Advance the write pointer, unless ordered zone append is in use. If\n 710:\t\t * the write fails, the write pointer position will be corrected when\n 711:\t\t * the next I/O starts execution.\n 712:\t\t */\n 713:\t\tif (!is_append || !zlo-\u003eordered_zone_append) {\n 714:\t\t\tzone-\u003ewp += nr_sectors;\n 715:\t\t\tif (zone-\u003ewp == zone_end)\n 716:\t\t\t\tzloop_set_zone_full(zlo, zone);\n 717:\t\t}\n 718:\tout_unlock:\n 719:\t\tspin_unlock(\u0026zone-\u003ewp_lock);\n 720:\t\treturn ret;\n 721:\t}\n"
} |
| 31/2 | 2026/10/09 11:33 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 66660 output: 36 thoughts: 267 Thoughts: **Analyzing the `zloop_do_open_zone` Function** Okay, let's dive into this `zloop_do_open_zone` function. I need to understand its behavior in the context of zone management. I see the function takes a `zloop_device`, a `zloop_zone`, and a boolean `explicit` flag as input. The core logic is a `switch` statement based on `zone->cond`. The cases for `BLK_ZONE_COND_EXP_OPEN` and `BLK_ZONE_COND_IMP_OPEN` seem straightforward: one returns `true` directly, the other tries to explicitly open the zone, and rotate it. `BLK_ZONE_COND_CLOSED` opens a closed or empty zone. But here's a snag! The `default` case is `return false;`. Hold on a second... what if `zone->cond` is `BLK_ZONE_COND_EMPTY`? This code *explicitly* rejects opening an empty zone! That seems... counterintuitive. I mean, wouldn't we want to open an empty zone under some circumstances? I was expecting it to open the zone, but then I'm presented with this `return false;`. I need to investigate this further. I will search `zloop.c` immediately for all uses of `BLK_ZONE_COND_EMPTY` to understand how `zone->cond` is actually set and, most importantly, *why* this scenario is handled this way. This could be a critical bug or intentional design decision, so I need to understand this branch fully. |
| 32/2 | 2026/10/09 11:33 | tool | grepper |
0mArgs:{
"Expression": "BLK_ZONE_COND_EMPTY",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=306=static bool zloop_do_open_zone(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-315-\t\treturn true;\ndrivers/block/zloop.c:316:\tcase BLK_ZONE_COND_EMPTY:\ndrivers/block/zloop.c-317-\tcase BLK_ZONE_COND_CLOSED:\n--\ndrivers/block/zloop.c=324=static void zloop_set_zone_cond(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-331-\tzone-\u003econd = cond;\ndrivers/block/zloop.c:332:\tif (cond == BLK_ZONE_COND_EMPTY)\ndrivers/block/zloop.c-333-\t\tzone-\u003ewp = zone-\u003estart;\n--\ndrivers/block/zloop.c=344=static inline void zloop_set_zone_empty(struct zloop_device *zlo,\n--\ndrivers/block/zloop.c-346-{\ndrivers/block/zloop.c:347:\tzloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);\ndrivers/block/zloop.c-348-}\n--\ndrivers/block/zloop.c=446=static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)\n--\ndrivers/block/zloop.c-474-\t\tif (zone-\u003ewp == zone-\u003estart)\ndrivers/block/zloop.c:475:\t\t\tzone-\u003econd = BLK_ZONE_COND_EMPTY;\ndrivers/block/zloop.c-476-\t\telse\n--\ndrivers/block/zloop.c-479-\t\tbreak;\ndrivers/block/zloop.c:480:\tcase BLK_ZONE_COND_EMPTY:\ndrivers/block/zloop.c-481-\tcase BLK_ZONE_COND_FULL:\n--\ndrivers/block/zloop.c=507=static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,\n--\ndrivers/block/zloop.c-524-\tif (!test_bit(ZLOOP_ZONE_SEQ_ERROR, \u0026zone-\u003eflags) \u0026\u0026\ndrivers/block/zloop.c:525:\t zone-\u003econd == BLK_ZONE_COND_EMPTY)\ndrivers/block/zloop.c-526-\t\tgoto unlock;\n"
} |
| 33/2 | 2026/10/09 11:33 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 67936 output: 33 thoughts: 1131 Thoughts: **Fuzzing Verdict: Absolutely Worthwhile**
Alright, let's break this down. Line 316-317 handles the open/closed/empty zone states, which is a good starting point but the main issue is larger. I've got a patch series that introduces significant changes, and I need to determine if fuzzing is necessary. Looking at the guidelines, I can't ignore it.
First, the code touches the core block layer (`block/blk-zoned.c`, `block/genhd.c`, `block/ioctl.c`), and introduces new ioctls. I see the modification of `zloop` (`drivers/block/zloop.c`), which is a loopback driver, accessible via `/dev/zloop-control`, and I know that means it's a target for virtual buses, which is important for fuzzing. The patch implements complex state machines, locking, and queue management, which scream for careful analysis. Plus, there's the addition of SCSI ZBC support in `drivers/scsi/sd.c` and `drivers/scsi/sd_zbc.c`, which can be driven by `scsi_debug` or emulated SCSI disks in QEMU.
Given the criteria laid out, I conclude that `WorthFuzzing` is *unequivocally* TRUE. This is reachable, executable code with potential for critical bugs.
Now, I need the details for the fuzzing setup. I'll need to figure out which kernel configurations are required, any specific functions that should be targeted, and why.
For `EnableConfigs`, I need to look at the Makefiles to figure out the config flags for the modules. For `block/blk-zoned.c`, it's `BLK_DEV_ZONED` ("Zoned block device support"). For `drivers/block/zloop.c`, it's `BLK_DEV_ZONED_LOOP`, which depends on `BLK_DEV_ZONED`. The `scsi` modules require `BLK_DEV_ZONED` because it's only built if `CONFIG_BLK_DEV_ZONED=y`, and `BLK_DEV_SD`, because it's built under `CONFIG_BLK_DEV_SD=y`.
Now, I'm confident. This code touches core subsystems, introduces new functionality and state machines, and is accessible via virtual buses. It's a prime target for fuzzing.
|
| 34/2 | 2026/10/09 11:33 | tool | grepper |
0mArgs:{
"Expression": "config BLK_DEV_SD",
"PathPrefixes": [
"drivers/scsi/"
]
}Results: {
"Output": "drivers/scsi/Kconfig=79=comment \"SCSI support type (disk, tape, CD-ROM)\"\n--\ndrivers/scsi/Kconfig-81-\ndrivers/scsi/Kconfig:82:config BLK_DEV_SD\ndrivers/scsi/Kconfig-83-\ttristate \"SCSI disk support\"\n"
} |
| 35/2 | 2026/10/09 11:33 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 69721 output: 34 thoughts: 19 Thoughts: Here's my take on those thoughts: **Scouting the SCSI Landscape** Okay, let's dive into this. My initial thought is to zero in on `drivers/scsi/Kconfig`. Specifically, around line 82. I'm guessing that area holds a key configuration variable or some relevant conditional logic related to the SCSI subsystem. That line range feels like a likely spot to find something I'm interested in, given my understanding of the typical structure of these `Kconfig` files. I need to get a handle on the build configuration options, which is a key step towards understanding how a particular feature behaves or why it's enabled/disabled. Let's see what we can find! |
| 36/2 | 2026/10/09 11:33 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/Kconfig",
"FirstLine": 75,
"LineCount": 20
}Results: {
"Contents": " 75:\t\t Run SCSI Mid Layer's KUnit tests for scsi_lib.\n 76:\t\n 77:\t\t If unsure say N.\n 78:\t\n 79:\tcomment \"SCSI support type (disk, tape, CD-ROM)\"\n 80:\t\tdepends on SCSI\n 81:\t\n 82:\tconfig BLK_DEV_SD\n 83:\t\ttristate \"SCSI disk support\"\n 84:\t\tdepends on SCSI\n 85:\t\thelp\n 86:\t\t If you want to use SCSI hard disks, Fibre Channel disks,\n 87:\t\t Serial ATA (SATA) or Parallel ATA (PATA) hard disks,\n 88:\t\t USB storage or the SCSI or parallel port version of\n 89:\t\t the IOMEGA ZIP drive, say Y and read the SCSI-HOWTO,\n 90:\t\t the Disk-HOWTO and the Multi-Disk-HOWTO, available from\n 91:\t\t \u003chttp://www.tldp.org/docs.html#howto\u003e. This is NOT for SCSI\n 92:\t\t CD-ROMs.\n 93:\t\n 94:\t\t To compile this driver as a module, choose M here and read\n"
} |
| 37/2 | 2026/10/09 11:33 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 70236 output: 351 thoughts: 1845 Thoughts: **Focusing on Zoned Block Device Storage Element Management** Alright, let's break this down. My goal is to pinpoint the critical elements of this zoned block device storage element management implementation. I'm focusing on key functions that define the new functionality and relevant entry points, while avoiding ubiquitous system calls and hot-path functions. First, I identified the relevant configuration options: `BLK_DEV_ZONED`, `BLK_DEV_ZONED_LOOP`, `SCSI`, and `BLK_DEV_SD`. Then, I dove into the code, making a list of functions that were either newly added or significantly modified. I need to get a list for `FocusSymbols`. In `block/blk-zoned.c`, I've got the ioctl dispatcher (`blkdev_zone_storage_elements_ioctl`), the reporting functions (`bdev_report_storage_elements`), element removal/restoration (`bdev_remove_storage_element`, `bdev_restore_storage_elements`), zone status checks (`disk_zone_is_offline`), and zone revalidation (`disk_revalidate_zones`). I've also noted the initialization function (`disk_init_storage_element_mgmt`) and some helper ioctls. Moving to `drivers/block/zloop.c`, I see functions specifically for the loopback device: `zloop_report_elements`, `zloop_remove_element`, `zloop_restore_elements`, along with degradation and workqueue-based removal/restoration: `zloop_ctl_degrade_element`, `zloop_remove_element_work`, `zloop_restore_elements_work`. I have `zloop_prep_rq` and `zloop_create_storage_elements` for request management. Finally, in `drivers/scsi/sd_zbc.c` and `drivers/scsi/sd.c`, I'm looking at `sd_zbc_report_storage_elements`, `sd_zbc_remove_storage_element`, and `sd_zbc_restore_storage_elements`. `sd_setup_read_write_cmnd` is on a more general SCSI path, I'll need to double check this. Now, I'll examine the functions, checking the static/non-static status, and whether they might be inlined by the compiler. This helps me understand which functions are directly accessible, and how they connect. Based on this, I have a pretty clear picture: `blkdev_zone_storage_elements_ioctl`, `bdev_report_storage_elements`, `bdev_remove_storage_element`, `bdev_restore_storage_elements`, `disk_zone_is_offline`, and `disk_init_storage_element_mgmt` are all good candidates. The zloop functions are static via function pointers and control writes and the SCSI ZBC functions are static via function pointers. The workqueue functions are, well, workqueue callbacks. With that information, I'm going to revisit the instructions for determining the focus symbols. I am specifically targeting feature-specific functions and entrypoints to get the most specific results. Based on that, I've selected the following FocusSymbols: - `blkdev_zone_storage_elements_ioctl` - `bdev_report_storage_elements` - `bdev_remove_storage_element` - `bdev_restore_storage_elements` - `disk_zone_is_offline` - `disk_init_storage_element_mgmt` - `zloop_report_elements` - `zloop_remove_element` - `zloop_restore_elements` - `zloop_remove_element_work` - `zloop_restore_elements_work` - `zloop_ctl_degrade_element` - `sd_zbc_report_storage_elements` - `sd_zbc_remove_storage_element` - `sd_zbc_restore_storage_elements` To make sure I'm not missing anything, I will double check the configuration dependencies. `BLK_DEV_ZONED_LOOP` depends on `BLK_DEV_ZONED`, and `BLK_DEV_SD` depends on `SCSI`. I'll also double check the `Kconfig` and `Makefile` dependencies to ensure everything builds correctly. Everything looks correct. Finally, the reasoning: This patch is about introducing storage element management for zoned block devices. It does this with new UAPI ioctl commands, core block layer infrastructure in block/blk-zoned.c, emulation support in the zoned loopback driver (zloop), and SCSI ZBC support in drivers/scsi/sd_zbc.c |
| 38/2 | 2026/10/09 11:34 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blkdev_zone_storage_elements_ioctl",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_remove_element_work",
"zloop_restore_elements_work",
"zloop_ctl_degrade_element",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements"
],
"Reasoning": "The patch series introduces storage element management for zoned block devices, adding new UAPI ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS), core block layer handlers in block/blk-zoned.c, storage element emulation and control operations in the virtual zoned loop driver (zloop), and SCSI ZBC support in sd_zbc.c. The changes modify reachable block layer logic, introduce delayed work handlers, queue operations, and state machine transitions reachable in standard virtualized environments.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"BLK_DEV_ZONED",
"BLK_DEV_ZONED_LOOP",
"SCSI",
"BLK_DEV_SD"
],
"FocusSymbols": [
"blkdev_zone_storage_elements_ioctl",
"bdev_report_storage_elements",
"bdev_remove_storage_element",
"bdev_restore_storage_elements",
"disk_zone_is_offline",
"disk_init_storage_element_mgmt",
"zloop_report_elements",
"zloop_remove_element",
"zloop_restore_elements",
"zloop_remove_element_work",
"zloop_restore_elements_work",
"zloop_ctl_degrade_element",
"sd_zbc_report_storage_elements",
"sd_zbc_remove_storage_element",
"sd_zbc_restore_storage_elements"
],
"Reasoning": "The patch series introduces storage element management for zoned block devices, adding new UAPI ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS), core block layer handlers in block/blk-zoned.c, storage element emulation and control operations in the virtual zoned loop driver (zloop), and SCSI ZBC support in sd_zbc.c. The changes modify reachable block layer logic, introduce delayed work handlers, queue operations, and state machine transitions reachable in standard virtualized environments.",
"WorthFuzzing": true
} |
| 39/1 | 2026/10/09 11:34 | action | If |
3mArgs:{
"WorthFuzzing": true
}Results: null |
| 40/2 | 2026/10/09 11:34 | agent | kmsan-evaluator |
3mModel:core Args: null Results: {
"KMSANReasoning": "The patch introduces storage element emulation and management for zoned block devices across blk-zoned, drivers/block/zloop, drivers/scsi/sd_zbc, and adds corresponding UAPI definitions and ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS).\n\nRegarding KMSAN applicability:\n1. Kernel-to-userspace data transfer: In `blkdev_report_storage_elements_ioctl`, the kernel copies `struct blk_se_report` containing flexible array members `struct blk_se elements[]` back to userspace via `copy_to_user`. The buffer is allocated using `kzalloc_flex`, which zero-initializes the entire allocated memory block. Furthermore, `struct blk_se` is 32 bytes in size (id: 4B, paired_id: 4B, nr_zones: 8B, type: 1B, status: 1B, restore_allowed: 1B, reserved: 13B) with an 8-byte alignment requirement and contains no padding holes between fields or at the end. In SCSI ZBC (`sd_zbc_parse_storage_element`), each element is explicitly `memset` to 0 before field assignment. In `zloop_report_elements`, each element descriptor originates from a `kzalloc_objs` allocation. The single integer written by `blkdev_get_nr_storage_elements_ioctl` via `put_user` transfers an initialized `unsigned int`.\n2. Heap and stack initializations: All newly allocated data structures (`zlo-\u003eelements`, `rep`, `buf`, `elements`) are allocated with zeroing allocators (`kzalloc`, `kzalloc_objs`, `kzalloc_flex`), and stack variables (e.g. `struct blk_revalidate_zone_args args = {}`, `struct zloop_options opts = {}`) are zero-initialized.\n3. Control flow: Branching logic does not depend on uninitialized variables.\n\nAny potential bugs in this patch series relate to concurrency/locking (handled by LOCKDEP), array bounds checking (e.g., `element_id - 1` indexing, handled by KASAN), or request queue handling. There are no uninitialized memory risks or kernel info-leaks that require a dedicated KMSAN session.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: Target architecture: amd64
For your convenience, here is the diff of the changes:
commit ab617d08dddf09f15f0a18d1adcd42c4ab1499e3
Author: syz-cluster <triage@syzkaller.com>
Date: Fri Oct 9 11:30:22 2026 +0000
syz-cluster: applied patch under review
diff --git a/Documentation/admin-guide/blockdev/zoned_loop.rst b/Documentation/admin-guide/blockdev/zoned_loop.rst
index 64277494fb36a..ec39f141af8d6 100644
--- a/Documentation/admin-guide/blockdev/zoned_loop.rst
+++ b/Documentation/admin-guide/blockdev/zoned_loop.rst
@@ -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
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 19268afb8752e..10fb3ca5adcd6 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -18,6 +18,8 @@
#include <linux/mempool.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
+#include <linux/delay.h>
+#include <linux/uaccess.h>
#include <trace/events/block.h>
@@ -292,6 +294,22 @@ static bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)
return !blk_zstate_is_conv(zs);
}
+/**
+ * disk_zone_is_offline - check if a sector belongs to an offline zone
+ * @disk: gendisk to check
+ * @sector: sector number
+ *
+ * Check if @sector on @disk is contained in an offline zone.
+ */
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)
+{
+ if (!blk_queue_is_zoned(disk->queue))
+ return false;
+
+ return disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;
+}
+EXPORT_SYMBOL_GPL(disk_zone_is_offline);
+
/**
* bdev_zone_is_seq - check if a sector belongs to a sequential write zone
* @bdev: block device to check
@@ -2526,20 +2544,7 @@ static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
return ret;
}
-/**
- * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
- * @disk: Target disk
- *
- * Helper function for low-level device drivers to check, (re) allocate and
- * initialize resources used for managing zoned disks. This function should
- * normally be called by blk-mq based drivers when a zoned gendisk is probed
- * and when the zone configuration of the gendisk changes (e.g. after a format).
- * Before calling this function, the device driver must already have set the
- * device zone size (chunk_sector limit) and the max zone append limit.
- * BIO based drivers can also use this function as long as the device queue
- * can be safely frozen.
- */
-int blk_revalidate_disk_zones(struct gendisk *disk)
+static int disk_revalidate_zones(struct gendisk *disk)
{
struct blk_revalidate_zone_args args = { };
struct blk_report_zones_args rep_args = {
@@ -2550,19 +2555,10 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
int retries = 2;
int ret;
- if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
- return -EIO;
-
- /*
- * Serialize calls to this function so that we can safely look at and
- * eventually change the disk zone information.
- */
- mutex_lock(&disk->zone_revalidate_mutex);
-
again:
ret = disk_revalidate_capacity(disk, &args);
if (ret)
- goto unlock;
+ return ret;
/*
* Allocate zone resources if they are needed and we have not done
@@ -2575,14 +2571,14 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
ret = disk_alloc_zone_resources(disk, args.capacity);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
}
ret = disk_init_revalidate_args(disk, &args);
if (ret) {
memalloc_noio_restore(noio_flag);
- goto unlock;
+ return ret;
}
ret = disk->fops->report_zones(disk, 0, UINT_MAX, &rep_args);
@@ -2599,8 +2595,6 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
if (ret)
goto free_args;
- mutex_unlock(&disk->zone_revalidate_mutex);
-
return 0;
free_args:
@@ -2616,7 +2610,36 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
}
pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
-unlock:
+
+ return ret;
+}
+
+/**
+ * blk_revalidate_disk_zones - (re)allocate and initialize zone write plugs
+ * @disk: Target disk
+ *
+ * Helper function for low-level device drivers to check, (re) allocate and
+ * initialize resources used for managing zoned disks. This function should
+ * normally be called by blk-mq based drivers when a zoned gendisk is probed
+ * and when the zone configuration of the gendisk changes (e.g. after a format).
+ * Before calling this function, the device driver must already have set the
+ * device zone size (chunk_sector limit) and the max zone append limit.
+ * BIO based drivers can also use this function as long as the device queue
+ * can be safely frozen.
+ */
+int blk_revalidate_disk_zones(struct gendisk *disk)
+{
+ int ret;
+
+ if (WARN_ON_ONCE(!blk_queue_is_zoned(disk->queue)))
+ return -EIO;
+
+ /*
+ * Serialize calls to this function so that we can safely look at and
+ * eventually change the disk zone information.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+ ret = disk_revalidate_zones(disk);
mutex_unlock(&disk->zone_revalidate_mutex);
return ret;
@@ -2700,5 +2723,408 @@ int queue_zone_wplugs_show(void *data, struct seq_file *m)
return 0;
}
-
#endif
+
+static int disk_se_mgmt_in_progress(struct gendisk *disk,
+ struct blk_se *elements,
+ bool *in_progress)
+{
+ unsigned int i, nr_se = disk->nr_storage_elements;
+ int ret;
+
+ /*
+ * Check if we have storage elements being removed or restored.
+ */
+ ret = disk->fops->se_ops->report_elements(disk, elements, &nr_se);
+ if (ret) {
+ pr_err("Failed to get storage elements\n");
+ return ret;
+ }
+
+ for (i = 0; i < nr_se; i++) {
+ switch (elements[i].status) {
+ case BLK_SE_STS_REMOVE_IN_PROGRESS:
+ case BLK_SE_STS_RESTORE_IN_PROGRESS:
+ *in_progress = true;
+ return 0;
+ default:
+ break;
+ }
+ }
+
+ *in_progress = false;
+
+ return 0;
+}
+
+static int disk_wait_for_se_mgmt_completion(struct gendisk *disk)
+{
+ struct blk_se *elements;
+ unsigned int noio_flag;
+ bool in_progress;
+ int ret;
+
+ noio_flag = memalloc_noio_save();
+ elements = kzalloc_objs(struct blk_se, disk->nr_storage_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 check
+ * for fatal signals so that the user can terminate this retry loop if
+ * the device misbehaves.
+ */
+ while (1) {
+ ret = disk_se_mgmt_in_progress(disk, elements, &in_progress);
+ if (ret || !in_progress)
+ break;
+
+ if (fatal_signal_pending(current)) {
+ ret = -EINTR;
+ break;
+ }
+
+ /* Not done yet: wait and retry. */
+ msleep(1000);
+ }
+
+ kfree(elements);
+
+ return ret;
+}
+
+/**
+ * bdev_report_storage_elements - report the storage elements of a zoned
+ * block device
+ * @bdev: the zoned block device to check
+ * @elements: array of storage element descriptors to fill
+ * @nr_elements: maximum size of @elements
+ *
+ * Fill at most @nr_elements storage element descriptors in the array
+ * @elements. The number of storage elements filled in the array is returned
+ * using @nr_elements. If @elements is NULL, only @nr_elements is returned to
+ * indicate the maximum number of storage elements of @bdev.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ if (!nr_elements)
+ return -EINVAL;
+
+ if (!elements) {
+ *nr_elements = disk->nr_storage_elements;
+ return 0;
+ }
+
+ *nr_elements = min(*nr_elements, disk->nr_storage_elements);
+ if (!*nr_elements)
+ return -EINVAL;
+
+ return disk->fops->se_ops->report_elements(disk, elements,
+ nr_elements);
+}
+EXPORT_SYMBOL_GPL(bdev_report_storage_elements);
+
+static int blkdev_get_nr_storage_elements_ioctl(struct block_device *bdev,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (put_user(disk->nr_storage_elements, (unsigned int __user *)argp))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int blkdev_report_storage_elements_ioctl(struct block_device *bdev,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ struct blk_se_report *rep;
+ int ret;
+
+ if (!argp)
+ return -EINVAL;
+
+ rep = kzalloc_flex(*rep, elements, disk->nr_storage_elements);
+ if (!rep)
+ return -ENOMEM;
+
+ if (copy_from_user(rep, argp, sizeof(struct blk_se_report))) {
+ ret = -EFAULT;
+ goto free_rep;
+ }
+
+ rep->nr_elements = min(rep->nr_elements, disk->nr_storage_elements);
+ if (!rep->nr_elements) {
+ ret = -EINVAL;
+ goto free_rep;
+ }
+
+ ret = bdev_report_storage_elements(bdev, rep->elements,
+ &rep->nr_elements);
+ if (ret)
+ goto free_rep;
+
+ if (copy_to_user(argp, rep, struct_size_t(struct blk_se_report,
+ elements, rep->nr_elements)))
+ ret = -EFAULT;
+
+free_rep:
+ kfree(rep);
+ return ret;
+}
+
+/**
+ * bdev_remove_storage_element - Remove (depopulate) a storage element of a
+ * zoned block device
+ * @bdev: block device to operate on
+ * @element_id: ID of the storage element to remove
+ *
+ * Remove (depopulate) the storage element identified by @element_id from the
+ * block device @bdev. The caller is responsible for taking care of any
+ * necessary device write cache flush and invalidation of cached data for the
+ * zones that will be offlined.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /* Zero is not a valid storage element ID. */
+ if (!element_id)
+ return -EINVAL;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding command.
+ * The caller is responsible for not queuing up more I/Os by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ ret = disk->fops->se_ops->remove_element(disk, element_id);
+ if (ret)
+ goto unlock;
+
+ /* Revalidate the device zones once the opration completes. */
+ ret = disk_wait_for_se_mgmt_completion(disk);
+ if (ret)
+ goto unlock;
+
+ ret = disk_revalidate_zones(disk);
+
+unlock:
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(bdev_remove_storage_element);
+
+static int blkdev_flush_and_truncate(struct block_device *bdev,
+ blk_mode_t mode)
+{
+ int ret;
+
+ filemap_invalidate_lock(bdev->bd_mapping);
+ ret = blkdev_issue_flush(bdev->bd_disk->part0);
+ if (!ret)
+ ret = truncate_bdev_range(bdev, mode, 0,
+ bdev_nr_bytes(bdev) - 1);
+ filemap_invalidate_unlock(bdev->bd_mapping);
+
+ return ret;
+}
+
+static int blkdev_remove_storage_element_ioctl(struct block_device *bdev,
+ blk_mode_t mode, void __user *argp)
+{
+ unsigned int element_id;
+ int ret;
+
+ if (!(mode & BLK_OPEN_WRITE))
+ return -EBADF;
+ if (bdev_read_only(bdev))
+ return -EPERM;
+
+ if (get_user(element_id, (unsigned int __user *)argp))
+ return -EFAULT;
+
+ 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.
+ */
+ ret = blkdev_flush_and_truncate(bdev, mode);
+ 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 zoned block device
+ * @bdev: block device to operate on
+ *
+ * Restore all storage elements of @bdev that have been depopulated. Not all
+ * elements may be restored by this operation.
+ *
+ * Returns 0 on success and a negative error code on failure.
+ */
+int bdev_restore_storage_elements(struct block_device *bdev)
+{
+ struct gendisk *disk = bdev->bd_disk;
+ unsigned int memflags;
+ int ret;
+
+ if (!blk_queue_is_zoned(disk->queue) || !disk->nr_storage_elements)
+ return -EOPNOTSUPP;
+
+ /*
+ * Serialize against concurrent revalidation and concurrent storage
+ * element management operations so that we can see consistent zone
+ * condition changes.
+ */
+ mutex_lock(&disk->zone_revalidate_mutex);
+
+ /*
+ * Freeze and unfreeze the queue to flush any outstanding commands.
+ * The caller is responsible for not queuing up more I/O by higher
+ * level means.
+ */
+ memflags = blk_mq_freeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
+
+ ret = disk->fops->se_ops->restore_elements(disk);
+ if (ret)
+ goto unlock;
+
+ ret = disk_wait_for_se_mgmt_completion(disk);
+ if (ret)
+ goto unlock;
+
+ ret = disk_revalidate_zones(disk);
+
+unlock:
+ mutex_unlock(&disk->zone_revalidate_mutex);
+
+ return ret;
+}
+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.
+ */
+ ret = blkdev_flush_and_truncate(bdev, mode);
+ if (!ret)
+ ret = bdev_restore_storage_elements(bdev);
+
+ inode_unlock(bdev->bd_mapping->host);
+
+ return ret;
+}
+
+int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ int ret;
+
+ /*
+ * Storage element operations are for zoned disks only and
+ * optional...
+ */
+ if (!blk_queue_is_zoned(disk->queue) || !disk->fops->se_ops)
+ return 0;
+
+ /* ...but if supported, all operations must be defined. */
+ if (!disk->fops->se_ops->report_elements ||
+ !disk->fops->se_ops->remove_element ||
+ !disk->fops->se_ops->restore_elements) {
+ pr_err("%s: invalid storage element operations\n",
+ disk->disk_name);
+ return -EINVAL;
+ }
+
+ /*
+ * Get the number of storage elements of the disk. If this fails,
+ * do not report an error as the failure may be because the zoned disk
+ * does not support storage element management.
+ */
+ ret = disk->fops->se_ops->report_elements(disk, NULL,
+ &disk->nr_storage_elements);
+ if (ret)
+ disk->nr_storage_elements = 0;
+
+ return 0;
+}
+
+int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ struct gendisk *disk = bdev->bd_disk;
+
+ if (!disk->nr_storage_elements)
+ return -ENOTTY;
+
+ switch (cmd) {
+ case BLKGETNRSTORELEMS:
+ return blkdev_get_nr_storage_elements_ioctl(bdev, argp);
+ case BLKREPORTSTORELEMS:
+ return blkdev_report_storage_elements_ioctl(bdev, argp);
+ case BLKREMOVESTORELEM:
+ return blkdev_remove_storage_element_ioctl(bdev, mode, argp);
+ case BLKRESTORESTORELEMS:
+ return blkdev_restore_storage_elements_ioctl(bdev, mode);
+ default:
+ break;
+ }
+
+ return -ENOTTY;
+}
diff --git a/block/blk.h b/block/blk.h
index 2cc03aa54c532..bbf523c9579cf 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -527,6 +527,7 @@ static inline void ioc_clear_queue(struct request_queue *q)
#endif /* CONFIG_BLK_ICQ */
#ifdef CONFIG_BLK_DEV_ZONED
+int disk_init_storage_element_mgmt(struct gendisk *disk);
void disk_init_zone_resources(struct gendisk *disk);
void disk_release_zone_resources(struct gendisk *disk);
static inline bool bio_zone_write_plugging(struct bio *bio)
@@ -578,7 +579,14 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
unsigned int cmd, unsigned long arg);
bool bdev_zone_mgmt_allowed(struct block_device *bdev, sector_t sector);
+int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp);
#else /* CONFIG_BLK_DEV_ZONED */
+static inline int disk_init_storage_element_mgmt(struct gendisk *disk)
+{
+ return 0;
+}
static inline void disk_init_zone_resources(struct gendisk *disk)
{
}
@@ -625,6 +633,12 @@ static inline bool bdev_zone_mgmt_allowed(struct block_device *bdev,
{
return false;
}
+static inline int blkdev_zone_storage_elements_ioctl(struct block_device *bdev,
+ blk_mode_t mode, unsigned int cmd,
+ void __user *argp)
+{
+ return -ENOTTY;
+}
#endif /* CONFIG_BLK_DEV_ZONED */
struct block_device *bdev_alloc(struct gendisk *disk, u8 partno);
diff --git a/block/genhd.c b/block/genhd.c
index 10ca8b4d6eea7..e2cef791ac91a 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -435,6 +435,10 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS))
return -EINVAL;
+ ret = disk_init_storage_element_mgmt(disk);
+ if (ret)
+ return ret;
+
if (queue_is_mq(disk->queue)) {
/*
* ->submit_bio and ->poll_bio are bypassed for blk-mq drivers.
diff --git a/block/ioctl.c b/block/ioctl.c
index 64b4e6c0f6965..ccf806c2e37d1 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -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 */
diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 394dc2408ed73..a1c9f7a960cfd 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -37,6 +37,8 @@ enum {
ZLOOP_OPT_ORDERED_ZONE_APPEND = (1 << 10),
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 = {
@@ -51,11 +53,23 @@ static const match_table_t zloop_opt_tokens = {
{ ZLOOP_OPT_BUFFERED_IO, "buffered_io" },
{ ZLOOP_OPT_ZONE_APPEND, "zone_append=%u" },
{ ZLOOP_OPT_ORDERED_ZONE_APPEND, "ordered_zone_append" },
- { ZLOOP_OPT_DISCARD_WRITE_CACHE, "discard_write_cache" },
+ { 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 }
};
+/* Storage elements emulation types. */
+enum zloop_stor_elements {
+ /* No emulation. */
+ ZLOOP_STOR_ELEMENTS_NONE,
+ /* Emulate read+write storage elements. */
+ ZLOOP_STOR_ELEMENTS_RDWR,
+ /* Emulate pairs of associated read and write storage elements. */
+ ZLOOP_STOR_ELEMENTS_PAIRS,
+};
+
/* Default values for the "add" operation. */
#define ZLOOP_DEF_ID -1
#define ZLOOP_DEF_ZONE_SIZE ((256ULL * SZ_1M) >> SECTOR_SHIFT)
@@ -68,6 +82,8 @@ static const match_table_t zloop_opt_tokens = {
#define ZLOOP_DEF_BUFFERED_IO false
#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
@@ -87,6 +103,8 @@ struct zloop_options {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
+ unsigned int element_id;
};
/*
@@ -117,6 +135,8 @@ struct zloop_zone {
enum blk_zone_cond cond;
sector_t start;
sector_t wp;
+ unsigned int wr_se_id;
+ unsigned int rd_se_id;
gfp_t old_gfp_mask;
};
@@ -133,6 +153,7 @@ struct zloop_device {
bool zone_append;
bool ordered_zone_append;
bool discard_write_cache;
+ enum zloop_stor_elements stor_elements;
const char *base_dir;
struct file *data_dir;
@@ -150,6 +171,17 @@ struct zloop_device {
struct list_head open_zones_lru_list;
unsigned int nr_open_zones;
+ /* For storage elements emulation. */
+ struct mutex stor_elements_lock;
+ unsigned int nr_elements;
+ unsigned int max_nr_removed_elements;
+ unsigned int nr_removed_elements;
+ struct delayed_work remove_element_work;
+ unsigned int remove_element_id;
+ struct delayed_work restore_elements_work;
+ bool restore_in_progress;
+ struct blk_se *elements;
+
struct zloop_zone zones[] __counted_by(nr_zones);
};
@@ -289,22 +321,43 @@ static bool zloop_do_open_zone(struct zloop_device *zlo,
}
}
-static void zloop_mark_full(struct zloop_device *zlo, struct zloop_zone *zone)
+static void zloop_set_zone_cond(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ enum blk_zone_cond cond)
{
lockdep_assert_held(&zone->wp_lock);
zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_FULL;
- zone->wp = ULLONG_MAX;
+ zone->cond = cond;
+ if (cond == BLK_ZONE_COND_EMPTY)
+ zone->wp = zone->start;
+ else
+ zone->wp = ULLONG_MAX;
}
-static void zloop_mark_empty(struct zloop_device *zlo, struct zloop_zone *zone)
+static inline void zloop_set_zone_full(struct zloop_device *zlo,
+ struct zloop_zone *zone)
{
- lockdep_assert_held(&zone->wp_lock);
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_FULL);
+}
- zloop_lru_remove_open_zone(zlo, zone);
- zone->cond = BLK_ZONE_COND_EMPTY;
- zone->wp = zone->start;
+static inline void zloop_set_zone_empty(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_EMPTY);
+}
+
+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_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
@@ -316,6 +369,13 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
lockdep_assert_held(&zone->lock);
+ /*
+ * If the zone was already changed to offline or read-only, we have
+ * nothing to do.
+ */
+ if (zloop_zone_is_offline_or_readonly(zlo, zone))
+ return 0;
+
ret = vfs_getattr(&zone->file->f_path, &stat, STATX_SIZE, 0);
if (ret < 0) {
pr_err("Failed to get zone %u file stat (err=%d)\n",
@@ -339,9 +399,9 @@ static int zloop_update_seq_zone(struct zloop_device *zlo, unsigned int zone_no)
spin_lock(&zone->wp_lock);
if (!file_sectors) {
- zloop_mark_empty(zlo, zone);
+ zloop_set_zone_empty(zlo, zone);
} else if (file_sectors == zlo->zone_capacity) {
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
} else {
if (zone->cond != BLK_ZONE_COND_IMP_OPEN &&
zone->cond != BLK_ZONE_COND_EXP_OPEN)
@@ -363,6 +423,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);
if (ret)
@@ -388,6 +453,11 @@ static int zloop_close_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);
if (ret)
@@ -420,7 +490,22 @@ static int zloop_close_zone(struct zloop_device *zlo, unsigned int zone_no)
return ret;
}
-static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no)
+static int zloop_do_reset_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ if (vfs_truncate(&zone->file->f_path, 0))
+ return -EIO;
+
+ spin_lock(&zone->wp_lock);
+ zloop_set_zone_empty(zlo, zone);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ spin_unlock(&zone->wp_lock);
+
+ return 0;
+}
+
+static int zloop_reset_zone(struct zloop_device *zlo, unsigned int zone_no,
+ bool all_zones)
{
struct zloop_zone *zone = &zlo->zones[zone_no];
int ret = 0;
@@ -430,20 +515,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;
- if (vfs_truncate(&zone->file->f_path, 0)) {
+ ret = zloop_do_reset_zone(zlo, zone);
+ if (ret)
set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- ret = -EIO;
- goto unlock;
- }
-
- spin_lock(&zone->wp_lock);
- zloop_mark_empty(zlo, zone);
- clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
- spin_unlock(&zone->wp_lock);
unlock:
mutex_unlock(&zone->lock);
@@ -457,7 +541,7 @@ static int zloop_reset_all_zones(struct zloop_device *zlo)
int ret;
for (i = zlo->nr_conv_zones; i < zlo->nr_zones; i++) {
- ret = zloop_reset_zone(zlo, i);
+ ret = zloop_reset_zone(zlo, i, true);
if (ret)
return ret;
}
@@ -475,6 +559,11 @@ static int zloop_finish_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_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags) &&
zone->cond == BLK_ZONE_COND_FULL)
goto unlock;
@@ -487,7 +576,7 @@ static int zloop_finish_zone(struct zloop_device *zlo, unsigned int zone_no)
}
spin_lock(&zone->wp_lock);
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
spin_unlock(&zone->wp_lock);
@@ -624,7 +713,7 @@ static int zloop_seq_write_prep(struct zloop_cmd *cmd)
if (!is_append || !zlo->ordered_zone_append) {
zone->wp += nr_sectors;
if (zone->wp == zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
}
out_unlock:
spin_unlock(&zone->wp_lock);
@@ -766,7 +855,7 @@ static void zloop_handle_cmd(struct zloop_cmd *cmd)
cmd->ret = zloop_flush(zlo);
break;
case REQ_OP_ZONE_RESET:
- cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq));
+ cmd->ret = zloop_reset_zone(zlo, rq_zone_no(rq), false);
break;
case REQ_OP_ZONE_RESET_ALL:
cmd->ret = zloop_reset_all_zones(zlo);
@@ -875,30 +964,72 @@ static void zloop_complete_rq(struct request *rq)
blk_mq_end_request(rq, sts);
}
-static bool zloop_set_zone_append_sector(struct request *rq)
+static bool zloop_set_zone_append_sector(struct zloop_device *zlo,
+ struct zloop_zone *zone,
+ struct request *rq)
{
- struct zloop_device *zlo = rq->q->queuedata;
- unsigned int zone_no = rq_zone_no(rq);
- struct zloop_zone *zone = &zlo->zones[zone_no];
sector_t zone_end = zone->start + zlo->zone_capacity;
sector_t nr_sectors = blk_rq_sectors(rq);
- spin_lock(&zone->wp_lock);
-
if (zone->cond == BLK_ZONE_COND_FULL ||
- zone->wp + nr_sectors > zone_end) {
- spin_unlock(&zone->wp_lock);
+ zone->wp + nr_sectors > zone_end)
return false;
- }
rq->__sector = zone->wp;
zone->wp += blk_rq_sectors(rq);
if (zone->wp >= zone_end)
- zloop_mark_full(zlo, zone);
+ zloop_set_zone_full(zlo, zone);
+
+ return true;
+}
+
+
+static bool zloop_prep_rq(struct zloop_device *zlo, struct request *rq)
+{
+ struct zloop_zone *zone = &zlo->zones[rq_zone_no(rq)];
+ bool is_write = op_is_write(req_op(rq));
+ bool ret = true;
+
+ spin_lock(&zone->wp_lock);
+
+ if (zlo->nr_elements) {
+ struct blk_se *se;
+
+ /*
+ * Check the zone condition and the health state of the storage
+ * element serving the zone: do not allow any operation to
+ * offline zones or if the storage element serving the zone is
+ * being removed, and allow only reads to read-only zones.
+ */
+ if (is_write)
+ se = &zlo->elements[zone->wr_se_id - 1];
+ else
+ se = &zlo->elements[zone->rd_se_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_DEGRADED ||
+ READ_ONCE(se->status) == BLK_SE_STS_REMOVE_IN_PROGRESS) {
+ ret = false;
+ goto unlock;
+ }
+
+ if (zone->cond == BLK_ZONE_COND_OFFLINE ||
+ (zone->cond == BLK_ZONE_COND_READONLY && is_write)) {
+ ret = false;
+ goto unlock;
+ }
+ }
+
+ /*
+ * If we need to strongly order zone append operations, set the request
+ * sector to the zone write pointer location now instead of when the
+ * command work runs.
+ */
+ if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND)
+ ret = zloop_set_zone_append_sector(zlo, zone, rq);
+unlock:
spin_unlock(&zone->wp_lock);
- return true;
+ return ret;
}
static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
@@ -914,13 +1045,21 @@ static blk_status_t zloop_queue_rq(struct blk_mq_hw_ctx *hctx,
}
/*
- * If we need to strongly order zone append operations, set the request
- * sector to the zone write pointer location now instead of when the
- * command work runs.
+ * Do not allow commands while the restoration of storage elements is
+ * in progress.
*/
- if (zlo->ordered_zone_append && req_op(rq) == REQ_OP_ZONE_APPEND) {
- if (!zloop_set_zone_append_sector(rq))
+ if (READ_ONCE(zlo->restore_in_progress))
+ return BLK_STS_IOERR;
+
+ switch (req_op(rq)) {
+ case REQ_OP_READ:
+ case REQ_OP_WRITE:
+ case REQ_OP_ZONE_APPEND:
+ if (!zloop_prep_rq(zlo, rq))
return BLK_STS_IOERR;
+ break;
+ default:
+ break;
}
blk_mq_start_request(rq);
@@ -1002,11 +1141,299 @@ static int zloop_report_zones(struct gendisk *disk, sector_t sector,
return nr_zones;
}
+static int zloop_report_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (!elements) {
+ *nr_elements = zlo->nr_elements;
+ goto unlock;
+ }
+
+ *nr_elements = min(*nr_elements, zlo->nr_elements);
+ for (i = 0, se = zlo->elements; i < *nr_elements; i++, se++) {
+ switch (READ_ONCE(se->status)) {
+ case BLK_SE_STS_REMOVED:
+ case BLK_SE_STS_RESTORE_ERROR:
+ se->restore_allowed = 1;
+ break;
+ default:
+ se->restore_allowed = 0;
+ }
+ memcpy(&elements[i], se, sizeof(struct blk_se));
+ }
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return 0;
+}
+
+static int zloop_degrade_element(struct zloop_device *zlo,
+ unsigned int element_id)
+{
+ struct blk_se *se;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (!element_id || element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_OK)
+ WRITE_ONCE(se->status, BLK_SE_STS_DEGRADED);
+ else
+ ret = -EINVAL;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static void zloop_remove_element_work(struct work_struct *work)
+{
+ struct zloop_device *zlo = container_of(work, struct zloop_device,
+ remove_element_work.work);
+ struct blk_se *se, *paired_se = NULL;
+ enum blk_zone_cond cond;
+ struct zloop_zone *zone;
+ unsigned int i;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /*
+ * If the element to remove is a read element, zones must go offline and
+ * the associated write element is also removed.
+ */
+ se = &zlo->elements[zlo->remove_element_id - 1];
+ switch (se->type) {
+ case BLK_SE_TYPE_RDWR:
+ cond = BLK_ZONE_COND_OFFLINE;
+ break;
+ case BLK_SE_TYPE_READ:
+ cond = BLK_ZONE_COND_OFFLINE;
+ paired_se = &zlo->elements[se->paired_id - 1];
+ break;
+ case BLK_SE_TYPE_WRITE:
+ cond = BLK_ZONE_COND_READONLY;
+ break;
+ default:
+ WARN_ON_ONCE(1);
+ }
+
+ /*
+ * Change the condition of the zones owned by the (pair of) elements
+ * being removed and mark the elements removed.
+ */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ if (zone->wr_se_id != se->id && zone->rd_se_id != se->id)
+ continue;
+ mutex_lock(&zone->lock);
+ spin_lock(&zone->wp_lock);
+ clear_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ zloop_set_zone_cond(zlo, zone, cond);
+ spin_unlock(&zone->wp_lock);
+ mutex_unlock(&zone->lock);
+ }
+
+ WRITE_ONCE(se->status, BLK_SE_STS_REMOVED);
+ zlo->nr_removed_elements++;
+ 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++;
+ }
+
+ zlo->remove_element_id = 0;
+
+ mutex_unlock(&zlo->stor_elements_lock);
+}
+
+static int zloop_remove_element(struct gendisk *disk, unsigned int element_id)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se, *paired_se = NULL;
+ unsigned int nr_remove = 1;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ if (element_id > zlo->nr_elements)
+ return -EINVAL;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Get the element to remove. If it is already removed, we have nothing
+ * to do.
+ */
+ se = &zlo->elements[element_id - 1];
+ if (READ_ONCE(se->status) == BLK_SE_STS_REMOVED)
+ goto unlock;
+
+ /*
+ * If the element to remove is a read element, the associated write
+ * element must also be removed.
+ */
+ if (se->type == BLK_SE_TYPE_READ) {
+ paired_se = &zlo->elements[se->paired_id - 1];
+ if (READ_ONCE(paired_se->status) != BLK_SE_STS_REMOVED)
+ nr_remove = 2;
+ }
+
+ if (zlo->nr_removed_elements + nr_remove >
+ zlo->max_nr_removed_elements) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /*
+ * Schedule the element removal with a delay, to emulate the (generally
+ * short) time it takes for a real device to depopulate a head and
+ * modify the zones.
+ */
+ zlo->remove_element_id = element_id;
+ 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));
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
+static void zloop_restore_conv_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ mutex_lock(&zone->lock);
+ spin_lock(&zone->wp_lock);
+ if (zone->cond == BLK_ZONE_COND_OFFLINE ||
+ zone->cond == BLK_ZONE_COND_READONLY)
+ zloop_set_zone_cond(zlo, zone, BLK_ZONE_COND_NOT_WP);
+ spin_unlock(&zone->wp_lock);
+ mutex_unlock(&zone->lock);
+}
+
+static int zloop_restore_seq_zone(struct zloop_device *zlo,
+ struct zloop_zone *zone)
+{
+ int ret;
+
+ mutex_lock(&zone->lock);
+ ret = zloop_do_reset_zone(zlo, zone);
+ if (ret)
+ set_bit(ZLOOP_ZONE_SEQ_ERROR, &zone->flags);
+ mutex_unlock(&zone->lock);
+
+ return ret;
+}
+
+static void zloop_restore_elements_work(struct work_struct *work)
+{
+ struct zloop_device *zlo = container_of(work, struct zloop_device,
+ restore_elements_work.work);
+ struct zloop_zone *zone;
+ struct blk_se *se;
+ unsigned int i;
+ int ret;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ /* Reset all zones. */
+ for (i = 0, zone = zlo->zones; i < zlo->nr_zones; i++, zone++) {
+ if (test_bit(ZLOOP_ZONE_CONV, &zone->flags)) {
+ zloop_restore_conv_zone(zlo, zone);
+ continue;
+ }
+ ret = zloop_restore_seq_zone(zlo, zone);
+ if (ret)
+ goto out_unlock;
+ }
+
+ /* Restore all removed elements. */
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ if (READ_ONCE(se->status) == BLK_SE_STS_RESTORE_IN_PROGRESS)
+ WRITE_ONCE(se->status, BLK_SE_STS_OK);
+ }
+
+ zlo->nr_removed_elements = 0;
+
+out_unlock:
+ WRITE_ONCE(zlo->restore_in_progress, false);
+ mutex_unlock(&zlo->stor_elements_lock);
+}
+
+static int zloop_restore_elements(struct gendisk *disk)
+{
+ struct zloop_device *zlo = disk->private_data;
+ struct blk_se *se;
+ unsigned int i;
+ int ret = 0;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE)
+ return -EOPNOTSUPP;
+
+ mutex_lock(&zlo->stor_elements_lock);
+
+ if (zlo->remove_element_id || READ_ONCE(zlo->restore_in_progress)) {
+ ret = -EBUSY;
+ goto unlock;
+ }
+
+ /* If we have no removed elements, we have nothing to do. */
+ if (!zlo->nr_removed_elements)
+ goto unlock;
+
+ /*
+ * Schedule the elements restoration with a delay, to emulate the time
+ * it takes for a real device to restore all removed heads and reset
+ * all zones.
+ */
+ WRITE_ONCE(zlo->restore_in_progress, true);
+ for (i = 0, se = zlo->elements; i < zlo->nr_elements; i++, se++) {
+ 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,
+ msecs_to_jiffies(5000));
+
+unlock:
+ mutex_unlock(&zlo->stor_elements_lock);
+
+ return ret;
+}
+
static void zloop_free_disk(struct gendisk *disk)
{
struct zloop_device *zlo = disk->private_data;
unsigned int i;
+ cancel_delayed_work_sync(&zlo->remove_element_work);
+ cancel_delayed_work_sync(&zlo->restore_elements_work);
+
blk_mq_free_tag_set(&zlo->tag_set);
for (i = 0; i < zlo->nr_zones; i++) {
@@ -1019,15 +1446,24 @@ static void zloop_free_disk(struct gendisk *disk)
fput(zlo->data_dir);
destroy_workqueue(zlo->workqueue);
+ kfree(zlo->elements);
kfree(zlo->base_dir);
kvfree(zlo);
}
+
+static const struct blk_se_ops zloop_se_ops = {
+ .report_elements = zloop_report_elements,
+ .remove_element = zloop_remove_element,
+ .restore_elements = zloop_restore_elements,
+};
+
static const struct block_device_operations zloop_fops = {
.owner = THIS_MODULE,
.open = zloop_open,
.report_zones = zloop_report_zones,
.free_disk = zloop_free_disk,
+ .se_ops = &zloop_se_ops,
};
__printf(3, 4)
@@ -1112,6 +1548,24 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
if (!opts->buffered_io)
oflags |= O_DIRECT;
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ unsigned int nr_elems = zlo->nr_elements;
+ unsigned int se_idx;
+
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems /= 2;
+ se_idx = zone_no % nr_elems;
+ zlo->elements[se_idx].nr_zones++;
+
+ zone->wr_se_id = se_idx + 1;
+ zone->rd_se_id = zone->wr_se_id;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ zone->rd_se_id += nr_elems;
+ zlo->elements[zone->rd_se_id - 1].nr_zones =
+ zlo->elements[se_idx].nr_zones;
+ }
+ }
+
if (zone_no < zlo->nr_conv_zones) {
/* Conventional zone file. */
set_bit(ZLOOP_ZONE_CONV, &zone->flags);
@@ -1182,6 +1636,79 @@ static int zloop_init_zone(struct zloop_device *zlo, struct zloop_options *opts,
return ret;
}
+#define ZLOOP_MIN_STOR_ELEMENTS 2
+#define ZLOOP_MAX_STOR_ELEMENTS 32
+#define ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS 32
+
+static int zloop_create_storage_elements(struct zloop_device *zlo)
+{
+ struct blk_se *se, *paired_se;
+ unsigned int i, nr_elems, nr_elements;
+
+ /*
+ * Calculate the number of storage elements we are going to emulate.
+ * To achieve a somewhat realistic emulation, we want at least 2 storage
+ * elements, and no more than 32, targeting at least 32 zones per
+ * element.
+ */
+ if (zlo->nr_zones <= 64)
+ nr_elements = 2;
+ else
+ nr_elements =
+ min(ZLOOP_MAX_STOR_ELEMENTS,
+ zlo->nr_zones / ZLOOP_MIN_ZONES_PER_STOR_ELEMENTS);
+
+ /*
+ * If we are emulating pairs of read and write storage elements, we need
+ * double the number of storage element descriptors.
+ */
+ nr_elems = nr_elements;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ nr_elems *= 2;
+ zlo->elements = kzalloc_objs(struct blk_se, nr_elems);
+ if (!zlo->elements)
+ return -ENOMEM;
+
+ for (i = 0, se = zlo->elements; i < nr_elements; i++, se++) {
+ se->id = i + 1;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ se->type = BLK_SE_TYPE_WRITE;
+ else
+ se->type = BLK_SE_TYPE_RDWR;
+ se->status = BLK_SE_STS_OK;
+ }
+
+ /*
+ * If we are emulating pairs of read and write storage elements,
+ * initialize the read elements paired with the write elements we just
+ * initialized.
+ */
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS) {
+ for (i = 0; i < nr_elements; i++, se++) {
+ se->id = nr_elements + i + 1;
+ paired_se = &zlo->elements[i];
+ se->paired_id = paired_se->id;
+ paired_se->paired_id = se->id;
+ se->type = BLK_SE_TYPE_READ;
+ se->status = BLK_SE_STS_OK;
+ }
+ }
+
+ /*
+ * Make sure we do not allow removing all storage elements as that does
+ * not make any sense. This is consistent with the device advertized
+ * limit of SCSI and ATA devices supporting the storage element
+ * depopulation feature.
+ */
+ zlo->nr_elements = nr_elems;
+ if (zlo->stor_elements == ZLOOP_STOR_ELEMENTS_PAIRS)
+ zlo->max_nr_removed_elements = zlo->nr_elements - 2;
+ else
+ zlo->max_nr_removed_elements = zlo->nr_elements - 1;
+
+ return 0;
+}
+
static bool zloop_dev_exists(struct zloop_device *zlo)
{
struct file *cnv, *seq;
@@ -1237,6 +1764,11 @@ static int zloop_ctl_add(struct zloop_options *opts)
WRITE_ONCE(zlo->state, Zlo_creating);
spin_lock_init(&zlo->open_zones_lock);
INIT_LIST_HEAD(&zlo->open_zones_lru_list);
+ mutex_init(&zlo->stor_elements_lock);
+ INIT_DELAYED_WORK(&zlo->remove_element_work,
+ zloop_remove_element_work);
+ INIT_DELAYED_WORK(&zlo->restore_elements_work,
+ zloop_restore_elements_work);
ret = mutex_lock_killable(&zloop_ctl_mutex);
if (ret)
@@ -1270,12 +1802,19 @@ static int zloop_ctl_add(struct zloop_options *opts)
if (zlo->zone_append)
zlo->ordered_zone_append = opts->ordered_zone_append;
zlo->discard_write_cache = opts->discard_write_cache;
+ zlo->stor_elements = opts->stor_elements;
+
+ if (zlo->stor_elements != ZLOOP_STOR_ELEMENTS_NONE) {
+ ret = zloop_create_storage_elements(zlo);
+ if (ret)
+ goto out_free_idr;
+ }
zlo->workqueue = alloc_workqueue("zloop%d", WQ_UNBOUND | WQ_FREEZABLE,
opts->nr_queues * opts->queue_depth, zlo->id);
if (!zlo->workqueue) {
ret = -ENOMEM;
- goto out_free_idr;
+ goto out_destroy_storage_elements;
}
if (opts->base_dir)
@@ -1361,6 +1900,10 @@ static int zloop_ctl_add(struct zloop_options *opts)
zlo->id, zlo->nr_zones,
((sector_t)zlo->zone_size << SECTOR_SHIFT) >> 20,
zlo->block_size);
+ if (zlo->nr_elements)
+ pr_info("zloop%d: %d storage elements\n",
+ zlo->id, zlo->nr_elements);
+
pr_info("zloop%d: using %s%s zone append\n",
zlo->id,
zlo->ordered_zone_append ? "ordered " : "",
@@ -1370,6 +1913,8 @@ static int zloop_ctl_add(struct zloop_options *opts)
out_cleanup_disk:
put_disk(zlo->disk);
+ return ret;
+
out_cleanup_tags:
blk_mq_free_tag_set(&zlo->tag_set);
out_close_files:
@@ -1384,6 +1929,8 @@ static int zloop_ctl_add(struct zloop_options *opts)
kfree(zlo->base_dir);
out_destroy_workqueue:
destroy_workqueue(zlo->workqueue);
+out_destroy_storage_elements:
+ kfree(zlo->elements);
out_free_idr:
mutex_lock(&zloop_ctl_mutex);
idr_remove(&zloop_index_idr, zlo->id);
@@ -1483,6 +2030,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];
@@ -1502,6 +2085,8 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
opts->buffered_io = ZLOOP_DEF_BUFFERED_IO;
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;
@@ -1636,6 +2221,30 @@ static int zloop_parse_options(struct zloop_options *opts, const char *buf)
case ZLOOP_OPT_DISCARD_WRITE_CACHE:
opts->discard_write_cache = true;
break;
+ case ZLOOP_OPT_STOR_ELEMENTS:
+ if (match_uint(args, &token)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ switch (token) {
+ case ZLOOP_STOR_ELEMENTS_NONE:
+ case ZLOOP_STOR_ELEMENTS_RDWR:
+ case ZLOOP_STOR_ELEMENTS_PAIRS:
+ break;
+ default:
+ pr_err("Invalid stor_elements value\n");
+ ret = -EINVAL;
+ goto out;
+ }
+ 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);
@@ -1664,14 +2273,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 },
};
@@ -1719,6 +2330,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;
@@ -1742,6 +2356,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);
@@ -1752,6 +2368,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;
}
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a1b21ea14e549..48c46f4a1ace8 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1424,7 +1424,8 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
{
struct request *rq = scsi_cmd_to_rq(cmd);
struct scsi_device *sdp = cmd->device;
- struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
+ struct gendisk *disk = rq->q->disk;
+ struct scsi_disk *sdkp = scsi_disk(disk);
sector_t lba = sectors_to_logical(sdp, blk_rq_pos(rq));
sector_t threshold;
unsigned int nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
@@ -1456,6 +1457,11 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
goto fail;
}
+ /* Prevent read/write to offline zones of ZBC disks. */
+ if (sdkp->device->type == TYPE_ZBC &&
+ disk_zone_is_offline(disk, blk_rq_pos(rq)))
+ goto fail;
+
/*
* Some SD card readers can't handle accesses which touch the
* last one or two logical blocks. Split accesses as needed.
@@ -3937,6 +3943,9 @@ static const struct block_device_operations sd_fops = {
.get_unique_id = sd_get_unique_id,
.free_disk = scsi_disk_free_disk,
.pr_ops = &sd_pr_ops,
+#ifdef CONFIG_BLK_DEV_ZONED
+ .se_ops = &sd_zbc_se_ops,
+#endif
};
/**
diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 574af82430169..fc8c49903811e 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -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)
@@ -242,6 +243,8 @@ unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
unsigned int nr_zones, struct blk_report_zones_args *args);
+extern const struct blk_se_ops sd_zbc_se_ops;
+
#else /* CONFIG_BLK_DEV_ZONED */
static inline int sd_zbc_read_zones(struct scsi_disk *sdkp,
diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c
index 456beaf2e7690..39cd4f9fcce5c 100644
--- a/drivers/scsi/sd_zbc.c
+++ b/drivers/scsi/sd_zbc.c
@@ -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)
@@ -533,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_se *element)
+{
+ struct scsi_device *sdp = sdkp->device;
+ sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
+ u64 capacity;
+
+ memset(element, 0, sizeof(*element));
+
+ element->id = get_unaligned_be32(&desc[4]);
+
+ 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);
+ break;
+ case SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:
+ element->paired_id = get_unaligned_be32(&desc[16]);
+ if (desc[20] & 0x01)
+ element->type = BLK_SE_TYPE_READ;
+ else
+ element->type = BLK_SE_TYPE_WRITE;
+ element->nr_zones = get_unaligned_be64(&desc[24]);
+ break;
+ default:
+ element->type = BLK_SE_TYPE_UNKNOWN;
+ }
+
+ switch (desc[15]) {
+ case SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS:
+ case SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:
+ element->status = BLK_SE_STS_OK;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS:
+ element->status = BLK_SE_STS_DEGRADED;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:
+ element->status = BLK_SE_STS_RESTORE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:
+ element->status = BLK_SE_STS_RESTORE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:
+ element->status = BLK_SE_STS_REMOVE_ERROR;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:
+ element->status = BLK_SE_STS_REMOVE_IN_PROGRESS;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:
+ element->status = BLK_SE_STS_REMOVED;
+ element->restore_allowed = desc[13] & 0x01;
+ break;
+ case SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:
+ default:
+ element->status = BLK_SE_STS_UNKNOWN;
+ break;
+ }
+}
+
+/*
+ * Large hard limit on the number of storage elements. This accommodates all
+ * known devices today and likely forever :)
+ */
+#define SD_ZBC_MAX_STORAGE_ELEMENTS 255
+
+static int sd_zbc_report_storage_elements(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements)
+{
+ 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;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ unsigned int nr_descs, nr_se;
+ unsigned int buf_size;
+ int i, ret = 0, result;
+ u8 *desc, *buf;
+
+ if (!sdkp->modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ /*
+ * We need at least 32B for the report header and 32B for each
+ * descriptor.
+ */
+ nr_se = min(SD_ZBC_MAX_STORAGE_ELEMENTS, *nr_elements);
+ buf_size = ALIGN((nr_se + 1) * 32, SECTOR_SIZE);
+
+ buf = kzalloc(buf_size, GFP_NOIO);
+ if (!buf)
+ return -ENOMEM;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;
+ put_unaligned_be32(buf_size, &cmd[10]);
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buf_size,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp,
+ "GET PHYSICAL ELEMENT STATUS failed\n");
+ sd_print_result(sdkp, "GET PHYSICAL ELEMENT STATUS", result);
+ if (result > 0 && scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ ret = -EIO;
+ goto free_buf;
+ }
+
+ nr_descs = get_unaligned_be32(&buf[0]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of phys element descriptors\n");
+ ret = -EIO;
+ goto free_buf;
+ }
+ if (nr_descs > SD_ZBC_MAX_STORAGE_ELEMENTS) {
+ sd_printk(KERN_ERR, sdkp,
+ "Too many phys element descriptors\n");
+ ret = -EIO;
+ goto free_buf;
+ }
+
+ if (!elements) {
+ *nr_elements = nr_descs;
+ goto free_buf;
+ }
+
+ nr_descs = get_unaligned_be32(&buf[4]);
+ if (!nr_descs) {
+ sd_printk(KERN_ERR, sdkp,
+ "Invalid number of reported phys element descriptors\n");
+ ret = -EIO;
+ goto free_buf;
+ }
+
+ desc = &buf[32];
+ for (i = 0; i < min(nr_se, nr_descs); i++, desc += 32)
+ sd_zbc_parse_storage_element(sdkp, desc, &elements[i]);
+ *nr_elements = i;
+
+free_buf:
+ kfree(buf);
+
+ return ret;
+}
+
+static int sd_zbc_remove_storage_element(struct gendisk *disk,
+ unsigned int element_id)
+{
+ 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;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ int result;
+
+ if (!sdkp->modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES;
+ put_unaligned_be32(element_id, &cmd[10]);
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp,
+ "REMOVE ELEMENT AND MODIFY ZONES failed\n");
+ sd_print_result(sdkp,
+ "REMOVE ELEMENT AND MODIFY ZONES", result);
+ if (result > 0 && scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static int sd_zbc_restore_storage_elements(struct gendisk *disk)
+{
+ 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;
+ const struct scsi_exec_args exec_args = {
+ .sshdr = &sshdr,
+ };
+ unsigned char cmd[16];
+ int result;
+
+ if (!sdkp->modify_zones_supported)
+ return -EOPNOTSUPP;
+
+ memset(cmd, 0, 16);
+ cmd[0] = SERVICE_ACTION_IN_16;
+ cmd[1] = SAI_RESTORE_ELEMENTS_AND_REBUILD;
+
+ result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, NULL, 0,
+ timeout, 1, &exec_args);
+ if (result) {
+ sd_printk(KERN_ERR, sdkp,
+ "RESTORE ELEMENTS AND REBUILD failed\n");
+ sd_print_result(sdkp,
+ "RESTORE ELEMENTS AND REBUILD", result);
+ if (result > 0 && scsi_sense_valid(&sshdr))
+ sd_print_sense_hdr(sdkp, &sshdr);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+const struct blk_se_ops sd_zbc_se_ops = {
+ .report_elements = sd_zbc_report_storage_elements,
+ .remove_element = sd_zbc_remove_storage_element,
+ .restore_elements = sd_zbc_restore_storage_elements,
+};
+
/*
* Call blk_revalidate_disk_zones() if any of the zoned disk properties have
* changed that make it necessary to call that function. Called by
@@ -554,9 +802,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 +874,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",
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index d003a9d2d1f6c..20b77c6b451fc 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -189,6 +189,11 @@ struct gendisk {
struct disk_events *ev;
#ifdef CONFIG_BLK_DEV_ZONED
+ /*
+ * Total number of storage elements of the disk. This is always 0 for
+ * disks that do not support storage element management.
+ */
+ unsigned int nr_storage_elements;
/*
* Zoned block device information. Reads of this information must be
* protected with blk_queue_enter() / blk_queue_exit() or by holding a
@@ -942,6 +947,9 @@ static inline unsigned int disk_zone_capacity(struct gendisk *disk,
return disk->last_zone_capacity;
return disk->zone_capacity;
}
+
+bool disk_zone_is_offline(struct gendisk *disk, sector_t sector);
+
static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
sector_t pos)
{
@@ -956,6 +964,12 @@ static inline unsigned int disk_nr_zones(struct gendisk *disk)
return 0;
}
+static inline bool disk_zone_is_offline(struct gendisk *disk,
+ sector_t sector)
+{
+ return false;
+}
+
static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)
{
return false;
@@ -1574,6 +1588,22 @@ enum blk_unique_id {
BLK_UID_NAA = 3,
};
+/* Storage element management operations. */
+struct blk_se_ops {
+ int (*report_elements)(struct gendisk *disk,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+ int (*remove_element)(struct gendisk *disk, unsigned int element_id);
+ int (*restore_elements)(struct gendisk *disk);
+};
+
+int bdev_report_storage_elements(struct block_device *bdev,
+ struct blk_se *elements,
+ unsigned int *nr_elements);
+int bdev_remove_storage_element(struct block_device *bdev,
+ unsigned int element_id);
+int bdev_restore_storage_elements(struct block_device *bdev);
+
struct block_device_operations {
void (*submit_bio)(struct bio *bio);
int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
@@ -1601,6 +1631,7 @@ struct block_device_operations {
enum blk_unique_id id_type);
struct module *owner;
const struct pr_ops *pr_ops;
+ const struct blk_se_ops *se_ops;
/*
* Special callback for probing GPT entry at a given sector.
diff --git a/include/uapi/linux/blkzoned.h b/include/uapi/linux/blkzoned.h
index 6638361209667..e6c60fba6ac9c 100644
--- a/include/uapi/linux/blkzoned.h
+++ b/include/uapi/linux/blkzoned.h
@@ -208,4 +208,86 @@ struct blk_zone_range {
#define BLKFINISHZONE _IOW(0x12, 136, struct blk_zone_range)
#define BLKREPORTZONEV2 _IOWR(0x12, 142, struct blk_zone_report)
+/**
+ * enum blk_se_type - Type of a zoned device storage element.
+ *
+ * @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_se_type {
+ BLK_SE_TYPE_RDWR = 0x01,
+ BLK_SE_TYPE_READ = 0x02,
+ BLK_SE_TYPE_WRITE = 0x03,
+ BLK_SE_TYPE_UNKNOWN = 0xFF,
+};
+
+/**
+ * enum blk_se_status - Status of a zoned device storage element.
+ *
+ * @BLK_SE_STS_OK: The storage element is operating normally.
+ * @BLK_SE_STS_DEGRADED: The storage element has degraded and is not operating
+ * normally.
+ * @BLK_SE_STS_REMOVE_IN_PROGRESS: The storage element is being removed.
+ * @BLK_SE_STS_REMOVE_ERROR: The storage element removal failed.
+ * @BLK_SE_STS_RESTORE_IN_PROGRESS: The storage element is being restored.
+ * @BLK_SE_STS_RESTORE_ERROR: The storage element restoration failed.
+ * @BLK_SE_STS_REMOVED: The storage element was removed.
+ * @BLK_SE_STS_UNKNOWN: The storage element status is unknown.
+ */
+enum blk_se_status {
+ BLK_SE_STS_OK = 0x01,
+ BLK_SE_STS_DEGRADED = 0x02,
+ BLK_SE_STS_REMOVE_IN_PROGRESS = 0x03,
+ BLK_SE_STS_REMOVE_ERROR = 0x04,
+ BLK_SE_STS_RESTORE_IN_PROGRESS = 0x05,
+ BLK_SE_STS_RESTORE_ERROR = 0x06,
+ BLK_SE_STS_REMOVED = 0x07,
+ BLK_SE_STS_UNKNOWN = 0xFF,
+};
+
+/**
+ * struct blk_se - Zoned device storage element descriptor.
+ *
+ * @id: The ID of the element (cannot be 0).
+ * @paired_id: The ID of the paired element for an element that is not
+ * of type BLK_SE_TYPE_RDWR.
+ * @nr_zones: The number of zones that the storage element handles.
+ * @type: The type of the storage element (enum blk_se_type).
+ * @status: The health status of the storage element
+ * (enum blk_se_status).
+ * @restore_allowed: Indicate if the storage element can be restored.
+ * @reserved: unused field to align this structure size to 32B
+ */
+struct blk_se {
+ __u32 id;
+ __u32 paired_id;
+ __u64 nr_zones;
+ __u8 type;
+ __u8 status;
+ __u8 restore_allowed;
+ __u8 reserved[13];
+};
+
+struct blk_se_report {
+ __u32 nr_elements;
+ __u32 reserved;
+ struct blk_se elements[];
+};
+
+/**
+ * Zoned block device storage element management ioctl's:
+ *
+ * @BLKGETNRSTORELEMS: Get the number of storage elements of the device.
+ * @BLKREPORTSTORELEMS: Get the device storage elements.
+ * @BLKREMOVESTORELEM: Remove (depopulate) one storage element of a device.
+ * @BLKRESTORESTORELEM: Restore (repopulate if possible) all storage elements
+ * that have been removed.
+ */
+#define BLKGETNRSTORELEMS _IOR(0x12, 143, __u32)
+#define BLKREPORTSTORELEMS _IOWR(0x12, 144, struct blk_se_report)
+#define BLKREMOVESTORELEM _IOW(0x12, 145, __u32)
+#define BLKRESTORESTORELEMS _IO(0x12, 146)
+
#endif /* _UAPI_BLKZONED_H */
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index 34c6f219462a5..8a979326aa7f0 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -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 */
|
| 41/3 | 2026/10/09 11:34 | llm | kmsan-evaluator |
1mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28536 output: 40 thoughts: 5071 Thoughts: **KMSAN Evaluation for Zoned Block Device Emulation** Okay, let's break this down. The task is to determine if this zoned block device emulation code necessitates a dedicated KMSAN fuzzing session. The guiding principle is clear: avoid KMSAN if existing kernel sanitizers (KASAN, LOCKDEP, UBSAN, etc.) can catch the same issues. KMSAN is resource-intensive, so we want to be judicious with its use. My initial scan focuses on the potential KMSAN triggers: uninitialized memory reads, especially in kernel-to-user space interactions, conditional logic based on potentially uninitialized variables, and data structure initialization. The patch introduces emulation and management for zoned block devices. I need to scrutinize any data passed to user space, memory allocations, and critical logic paths. First, I examined `blkdev_report_storage_elements_ioctl`. My attention zeroed in on the `kzalloc_flex` allocation and subsequent `copy_from_user` call. The initial `kzalloc_flex` call with `struct blk_se_report` looked interesting. Given the structure is `kzalloc`'d, I expected zeroed memory. The `sizeof(struct blk_se_report)` is 8 bytes, which appears to overwrite the initial structure. However, the subsequent use of `bdev_report_storage_elements` and `copy_to_user` raised an eyebrow. The key here hinges on the number of elements. `bdev_report_storage_elements` can reduce the number of elements to report. However, all the memory is still zeroed. The `copy_to_user` copies the updated size, so no uninitialized values are copied to userspace. Next, I investigated how these storage elements are allocated and initialized. I checked how `zloop_report_elements` works. The zloop driver is responsible for its own storage element data, and `zloop_create_storage_elements` initializes all elements with a `kzalloc_objs` call - so they are all zeroed. The `sd_zbc_report_storage_elements` also uses `kzalloc` and `memset` in `sd_zbc_parse_storage_element` which also means zeroed. I also looked at `disk_se_mgmt_in_progress` and `disk_wait_for_se_mgmt_completion`. `disk_wait_for_se_mgmt_completion` allocates memory using `kzalloc_objs`, which means zero initialization. I analyzed `zloop_prep_rq`. I checked how `zone->wr_se_id` and `zone->rd_se_id` are used and initialized in `zloop_init_zone`. If `zlo->stor_elements == ZLOOP_STOR_ELEMENTS_NONE`, it means there are no storage elements and `zlo->nr_elements` is initialized to zero. I checked `struct blk_se`. I found no padding holes in `struct blk_se` or `struct blk_se_report`. This removed a source of potential uninitialized reads. **Conclusion:** Based on this review, I believe **KMSAN is not necessary here**. While the code interacts with user space, the crucial data structures are consistently initialized using `kzalloc` or `kzalloc_objs`. The ioctl handler either zeroes or `memset` memory. The code has also been carefully written to avoid reading uninitialized parts of allocated structures. The existing sanitizers should be sufficient to catch any remaining memory safety issues. |
| 42/3 | 2026/10/09 11:35 | tool | grepper |
0mArgs:{
"Expression": "kvzalloc|kzalloc.*zloop_device",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=1731=static int zloop_ctl_add(struct zloop_options *opts)\n--\ndrivers/block/zloop.c-1758-\ndrivers/block/zloop.c:1759:\tzlo = kvzalloc_flex(*zlo, zones, nr_zones);\ndrivers/block/zloop.c-1760-\tif (!zlo) {\n"
} |
| 43/3 | 2026/10/09 11:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 34520 output: 39 thoughts: 904 Thoughts: **Analyzing Potential Out-of-Bounds Access in Zoned Block Device Removal** Okay, so I'm diving into this `zloop` code, specifically focusing on how storage elements are removed and the potential for off-by-one errors. It looks like when a `zloop_device` (`zlo`) is initialized with `kvzalloc_flex`, the `zlo` structure and its `zones[]` array are zeroed out. That's good, predictable behavior. Now, the critical path seems to be if `zlo->stor_elements` is *not* `ZLOOP_STOR_ELEMENTS_NONE`. Then, `zloop_create_storage_elements` is called, allocating memory for `zlo->elements` and setting `zlo->nr_elements`. I see that `zloop_init_zone` then initializes `zone->wr_se_id` and `zone->rd_se_id` to values *between* 1 and `zlo->nr_elements`. So far, so good – the indices are guaranteed to be initialized and within the allocated range. Now, the red flag. I'm looking at `zloop_remove_element` and the condition `if (element_id > zlo->nr_elements) return -EINVAL;`. But what if `element_id` is *zero*? That condition doesn't catch it! The subsequent line, `se = &zlo->elements[element_id - 1];` becomes `zlo->elements[-1]`, a classic out-of-bounds access. KASAN should catch it, which is the good news! Let's see if this could be an issue. But before panicking, where does `zloop_remove_element` get called? It looks like it's only called through `zloop_se_ops.remove_element`. Ah, right, this is what the low-level callback that the SCSI layer calls, `bdev_remove_storage_element`. Ok, `bdev_remove_storage_element` has a check: `if (!element_id) return -EINVAL;`. Excellent! So, that protects us. `zloop_remove_element` *shouldn't* receive a zero `element_id`. But then, I need to know. Where is this function called? It is only called from SCSI, which then calls `bdev_remove_storage_element` which *does* check for zero. But, hang on... what about SCSI itself? What is the `sd_zbc_remove_storage_element` doing? Does *it* do any validation? It looks like it's just sending an `SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES` command to the device and placing `element_id` in the `cmd[10]` without checking it. That means the caller can pass any number as `element_id`, without validation! And if that `element_id` is zero, we *will* get an out-of-bounds access within `zloop_remove_element`! Right, I need to understand exactly how the SCSI layer interacts with the `zloop` layer. I will look at the `sd_zbc_report_storage_elements` code around the indicated lines in `sd_zbc.c` to see how the SCSI layer works. |
| 44/3 | 2026/10/09 11:35 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd_zbc.c",
"FirstLine": 600,
"LineCount": 85
}Results: {
"Contents": " 600:\t\t\telement-\u003estatus = BLK_SE_STS_REMOVE_ERROR;\n 601:\t\t\tbreak;\n 602:\t\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:\n 603:\t\t\telement-\u003estatus = BLK_SE_STS_REMOVE_IN_PROGRESS;\n 604:\t\t\tbreak;\n 605:\t\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_OK:\n 606:\t\t\telement-\u003estatus = BLK_SE_STS_REMOVED;\n 607:\t\t\telement-\u003erestore_allowed = desc[13] \u0026 0x01;\n 608:\t\t\tbreak;\n 609:\t\tcase SCSI_PHYS_ELEM_HEALTH_NOT_REPORTED:\n 610:\t\tdefault:\n 611:\t\t\telement-\u003estatus = BLK_SE_STS_UNKNOWN;\n 612:\t\t\tbreak;\n 613:\t\t}\n 614:\t}\n 615:\t\n 616:\t/*\n 617:\t * Large hard limit on the number of storage elements. This accommodates all\n 618:\t * known devices today and likely forever :)\n 619:\t */\n 620:\t#define SD_ZBC_MAX_STORAGE_ELEMENTS\t255\n 621:\t\n 622:\tstatic int sd_zbc_report_storage_elements(struct gendisk *disk,\n 623:\t\t\t\t\t\t struct blk_se *elements,\n 624:\t\t\t\t\t\t unsigned int *nr_elements)\n 625:\t{\n 626:\t\tstruct scsi_disk *sdkp = scsi_disk(disk);\n 627:\t\tstruct scsi_device *sdp = sdkp-\u003edevice;\n 628:\t\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n 629:\t\tstruct scsi_sense_hdr sshdr;\n 630:\t\tconst struct scsi_exec_args exec_args = {\n 631:\t\t\t.sshdr = \u0026sshdr,\n 632:\t\t};\n 633:\t\tunsigned char cmd[16];\n 634:\t\tunsigned int nr_descs, nr_se;\n 635:\t\tunsigned int buf_size;\n 636:\t\tint i, ret = 0, result;\n 637:\t\tu8 *desc, *buf;\n 638:\t\n 639:\t\tif (!sdkp-\u003emodify_zones_supported)\n 640:\t\t\treturn -EOPNOTSUPP;\n 641:\t\n 642:\t\t/*\n 643:\t\t * We need at least 32B for the report header and 32B for each\n 644:\t\t * descriptor.\n 645:\t\t */\n 646:\t\tnr_se = min(SD_ZBC_MAX_STORAGE_ELEMENTS, *nr_elements);\n 647:\t\tbuf_size = ALIGN((nr_se + 1) * 32, SECTOR_SIZE);\n 648:\t\n 649:\t\tbuf = kzalloc(buf_size, GFP_NOIO);\n 650:\t\tif (!buf)\n 651:\t\t\treturn -ENOMEM;\n 652:\t\n 653:\t\tmemset(cmd, 0, 16);\n 654:\t\tcmd[0] = SERVICE_ACTION_IN_16;\n 655:\t\tcmd[1] = SAI_GET_PHYSICAL_ELEMENT_STATUS;\n 656:\t\tput_unaligned_be32(buf_size, \u0026cmd[10]);\n 657:\t\n 658:\t\tresult = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buf_size,\n 659:\t\t\t\t\t timeout, 1, \u0026exec_args);\n 660:\t\tif (result) {\n 661:\t\t\tsd_printk(KERN_ERR, sdkp,\n 662:\t\t\t\t \"GET PHYSICAL ELEMENT STATUS failed\\n\");\n 663:\t\t\tsd_print_result(sdkp, \"GET PHYSICAL ELEMENT STATUS\", result);\n 664:\t\t\tif (result \u003e 0 \u0026\u0026 scsi_sense_valid(\u0026sshdr))\n 665:\t\t\t\tsd_print_sense_hdr(sdkp, \u0026sshdr);\n 666:\t\t\tret = -EIO;\n 667:\t\t\tgoto free_buf;\n 668:\t\t}\n 669:\t\n 670:\t\tnr_descs = get_unaligned_be32(\u0026buf[0]);\n 671:\t\tif (!nr_descs) {\n 672:\t\t\tsd_printk(KERN_ERR, sdkp,\n 673:\t\t\t\t \"Invalid number of phys element descriptors\\n\");\n 674:\t\t\tret = -EIO;\n 675:\t\t\tgoto free_buf;\n 676:\t\t}\n 677:\t\tif (nr_descs \u003e SD_ZBC_MAX_STORAGE_ELEMENTS) {\n 678:\t\t\tsd_printk(KERN_ERR, sdkp,\n 679:\t\t\t\t \"Too many phys element descriptors\\n\");\n 680:\t\t\tret = -EIO;\n 681:\t\t\tgoto free_buf;\n 682:\t\t}\n 683:\t\n 684:\t\tif (!elements) {\n"
} |
| 45/3 | 2026/10/09 11:35 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 37563 output: 39 thoughts: 15 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Diving Deeper into `sd_zbc.c`** Alright, so the task at hand is to understand the implications of something described earlier. I need to move past the initial overview and see what the code *actually* does. Let's dig deeper into the specifics, right? My next step is to get my hands dirty. My instincts tell me the details I need are in `sd_zbc.c`. Time to see what's what. Let's go look. |
| 46/3 | 2026/10/09 11:36 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd_zbc.c",
"FirstLine": 680,
"LineCount": 40
}Results: {
"Contents": " 680:\t\t\tret = -EIO;\n 681:\t\t\tgoto free_buf;\n 682:\t\t}\n 683:\t\n 684:\t\tif (!elements) {\n 685:\t\t\t*nr_elements = nr_descs;\n 686:\t\t\tgoto free_buf;\n 687:\t\t}\n 688:\t\n 689:\t\tnr_descs = get_unaligned_be32(\u0026buf[4]);\n 690:\t\tif (!nr_descs) {\n 691:\t\t\tsd_printk(KERN_ERR, sdkp,\n 692:\t\t\t\t\"Invalid number of reported phys element descriptors\\n\");\n 693:\t\t\tret = -EIO;\n 694:\t\t\tgoto free_buf;\n 695:\t\t}\n 696:\t\n 697:\t\tdesc = \u0026buf[32];\n 698:\t\tfor (i = 0; i \u003c min(nr_se, nr_descs); i++, desc += 32)\n 699:\t\t\tsd_zbc_parse_storage_element(sdkp, desc, \u0026elements[i]);\n 700:\t\t*nr_elements = i;\n 701:\t\n 702:\tfree_buf:\n 703:\t\tkfree(buf);\n 704:\t\n 705:\t\treturn ret;\n 706:\t}\n 707:\t\n 708:\tstatic int sd_zbc_remove_storage_element(struct gendisk *disk,\n 709:\t\t\t\t\t\t unsigned int element_id)\n 710:\t{\n 711:\t\tstruct scsi_disk *sdkp = scsi_disk(disk);\n 712:\t\tstruct scsi_device *sdp = sdkp-\u003edevice;\n 713:\t\tconst int timeout = sdp-\u003erequest_queue-\u003erq_timeout;\n 714:\t\tstruct scsi_sense_hdr sshdr;\n 715:\t\tconst struct scsi_exec_args exec_args = {\n 716:\t\t\t.sshdr = \u0026sshdr,\n 717:\t\t};\n 718:\t\tunsigned char cmd[16];\n 719:\t\tint result;\n"
} |
| 47/3 | 2026/10/09 11:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 38324 output: 39 thoughts: 43 Thoughts: Okay, here's my interpretation of that section of code, from a seasoned perspective: **Diving into `sd_zbc_parse_storage_element`** Alright, I'm taking a look at the `sd_zbc_parse_storage_element` function, specifically focusing on lines 545 to 615 in `drivers/scsi/sd_zbc.c`. Let's see what's happening within this routine. My initial expectation is that this section is critical for handling the parsing of storage element data related to Zoned Block Devices (ZBC). Given the function name, I suspect it's responsible for dissecting the information returned by a ZBC-aware storage device and extracting meaningful data for the SCSI subsystem to work with. I’m thinking this likely involves decoding vendor-specific data, verifying the consistency of the response, and potentially handling errors and exceptional conditions related to the storage element structure. My guess is it’s likely extracting metadata related to zones, such as their capacity, write pointer locations, and perhaps their overall state, to facilitate zoned storage operation. I'm keen to see the specifics of the data structures involved, and how they relate to the SCSI commands. Ultimately, it’s about providing a layer of abstraction between the low-level hardware details and the upper layers of the SCSI stack, ensuring that ZBC functionalities are correctly exposed to the operating system and applications. |
| 48/3 | 2026/10/09 11:36 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd_zbc.c",
"FirstLine": 545,
"LineCount": 60
}Results: {
"Contents": " 545:\t\t\tsd_printk(KERN_NOTICE, sdkp,\n 546:\t\t\t\t \"%u zones of %u logical blocks\\n\",\n 547:\t\t\t\t sdkp-\u003ezone_info.nr_zones,\n 548:\t\t\t\t sdkp-\u003ezone_info.zone_blocks);\n 549:\t}\n 550:\t\n 551:\tstatic void sd_zbc_parse_storage_element(struct scsi_disk *sdkp, u8 *desc,\n 552:\t\t\t\t\t\t struct blk_se *element)\n 553:\t{\n 554:\t\tstruct scsi_device *sdp = sdkp-\u003edevice;\n 555:\t\tsector_t zone_sectors = sd_zbc_zone_sectors(sdkp);\n 556:\t\tu64 capacity;\n 557:\t\n 558:\t\tmemset(element, 0, sizeof(*element));\n 559:\t\n 560:\t\telement-\u003eid = get_unaligned_be32(\u0026desc[4]);\n 561:\t\n 562:\t\tswitch (desc[14]) {\n 563:\t\tcase SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE:\n 564:\t\t\telement-\u003etype = BLK_SE_TYPE_RDWR;\n 565:\t\t\tcapacity = get_unaligned_be64(\u0026desc[16]);\n 566:\t\t\tif (!zone_sectors || capacity == ULLONG_MAX)\n 567:\t\t\t\telement-\u003enr_zones = 0;\n 568:\t\t\telse\n 569:\t\t\t\telement-\u003enr_zones =\n 570:\t\t\t\t\tlogical_to_sectors(sdp, capacity) \u003e\u003e\n 571:\t\t\t\t\t\tilog2(zone_sectors);\n 572:\t\t\tbreak;\n 573:\t\tcase SCSI_PHYS_ELEM_TYPE_FRAC_ACCESS_STORAGE:\n 574:\t\t\telement-\u003epaired_id = get_unaligned_be32(\u0026desc[16]);\n 575:\t\t\tif (desc[20] \u0026 0x01)\n 576:\t\t\t\telement-\u003etype = BLK_SE_TYPE_READ;\n 577:\t\t\telse\n 578:\t\t\t\telement-\u003etype = BLK_SE_TYPE_WRITE;\n 579:\t\t\telement-\u003enr_zones = get_unaligned_be64(\u0026desc[24]);\n 580:\t\t\tbreak;\n 581:\t\tdefault:\n 582:\t\t\telement-\u003etype = BLK_SE_TYPE_UNKNOWN;\n 583:\t\t}\n 584:\t\n 585:\t\tswitch (desc[15]) {\n 586:\t\tcase SCSI_PHYS_ELEM_HEALTH_WITHIN_SPEC_LIMITS:\n 587:\t\tcase SCSI_PHYS_ELEM_HEALTH_AT_SPEC_LIMITS:\n 588:\t\t\telement-\u003estatus = BLK_SE_STS_OK;\n 589:\t\t\tbreak;\n 590:\t\tcase SCSI_PHYS_ELEM_HEALTH_OUTSIDE_SPEC_LIMITS:\n 591:\t\t\telement-\u003estatus = BLK_SE_STS_DEGRADED;\n 592:\t\t\tbreak;\n 593:\t\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_ERR:\n 594:\t\t\telement-\u003estatus = BLK_SE_STS_RESTORE_ERROR;\n 595:\t\t\tbreak;\n 596:\t\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_REVOKE_IN_PROGRESS:\n 597:\t\t\telement-\u003estatus = BLK_SE_STS_RESTORE_IN_PROGRESS;\n 598:\t\t\tbreak;\n 599:\t\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_ERR:\n 600:\t\t\telement-\u003estatus = BLK_SE_STS_REMOVE_ERROR;\n 601:\t\t\tbreak;\n 602:\t\tcase SCSI_PHYS_ELEM_HEALTH_DEPOP_IN_PROGRESS:\n 603:\t\t\telement-\u003estatus = BLK_SE_STS_REMOVE_IN_PROGRESS;\n 604:\t\t\tbreak;\n"
} |
| 49/3 | 2026/10/09 11:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 39745 output: 30 thoughts: 414 Thoughts: **Analyzing Zoned Block Device Logic** Okay, first things first, in `sd_zbc_parse_storage_element`, I see that `memset` is used to zero out the entire `element` structure. That's good, all fields and reserved bytes start clean, initialized to zero. Nice, predictable starting state. Now, I'm shifting my focus to `disk_revalidate_zones`. Ah, they've refactored `blk_revalidate_disk_zones`. It looks like they pulled out the actual zone revalidation into an internal function called `disk_revalidate_zones`. The purpose of `blk_revalidate_disk_zones` is now simply to acquire the `disk->zone_revalidate_mutex` before calling the internal function and release the mutex after, ensuring safe access and modifications of the disk zone information through serialization. Smart move. Next, I'm looking at `disk_zone_is_offline`. This one's interesting, it checks if a zone is offline. It first checks if the queue is zoned, and then returns a boolean. If not zoned, it returns false. If zoned, it calls `disk_zone_get_cond`, and compares the result to `BLK_ZONE_COND_OFFLINE`. My question is, does `disk_zone_get_cond` somehow read uninitialized memory? I'd better dig into how `disk_zone_get_cond` is actually defined to make sure there aren't any potential issues here. Let's see... |
| 50/3 | 2026/10/09 11:36 | tool | grepper |
0mArgs:{
"Expression": "disk_zone_get_cond",
"PathPrefixes": [
"block/"
]
}Results: {
"Output": "block/blk-zoned.c=251=static inline u8 disk_zone_get_state(struct gendisk *disk, sector_t sector)\n--\nblock/blk-zoned.c-266-\nblock/blk-zoned.c:267:static enum blk_zone_cond disk_zone_get_cond(struct gendisk *disk,\nblock/blk-zoned.c-268-\t\t\t\t\t sector_t sector)\n--\nblock/blk-zoned.c=282=static inline bool disk_zone_is_offline_or_readonly(struct gendisk *disk,\n--\nblock/blk-zoned.c-284-{\nblock/blk-zoned.c:285:\tenum blk_zone_cond cond = disk_zone_get_cond(disk, sector);\nblock/blk-zoned.c-286-\n--\nblock/blk-zoned.c=304=bool disk_zone_is_offline(struct gendisk *disk, sector_t sector)\n--\nblock/blk-zoned.c-308-\nblock/blk-zoned.c:309:\treturn disk_zone_get_cond(disk, sector) == BLK_ZONE_COND_OFFLINE;\nblock/blk-zoned.c-310-}\n"
} |
| 51/3 | 2026/10/09 11:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 40832 output: 32 thoughts: 1880 Thoughts: **Analyzing Storage Element IOCTLs** Okay, let's break this down. I'm looking at these storage element IOCTLs, and I need to ensure they're safe. `disk_zone_get_cond` is an existing helper, so that’s fine. Let's start with `blkdev_get_nr_storage_elements_ioctl`. It just puts `disk->nr_storage_elements` to userspace. Where does that value come from? Ah, `disk_init_storage_element_mgmt` in `genhd.c`. `gendisk` is allocated with `kzalloc_node`, so it starts at 0. Then `disk_init_storage_element_mgmt` calls `report_elements`, which *might* set it to a non-zero value based on the underlying device. If the report fails, the value stays 0. Now, `blkdev_remove_storage_element_ioctl`. It reads an `element_id` from userspace using `get_user`. Good, that's what I expect. Then `blkdev_restore_storage_elements_ioctl` has no user data copy, and it doesn't look like a problem. Let's carefully re-examine `blkdev_report_storage_elements_ioctl`. It's a bit more involved. It allocates `rep` with `kzalloc_flex` based on `disk->nr_storage_elements`. Importantly, `kzalloc_flex` zero-initializes the allocated memory. The code then copies *only* the first 8 bytes of `argp` from userspace, which consists of `nr_elements` and `reserved`, into the allocated `rep` using `copy_from_user`. The `nr_elements` is then clamped to `disk->nr_storage_elements`. Then the core logic calls `bdev_report_storage_elements` which writes into the `rep->elements` array. It’s important to note the `zloop_report_elements` and `sd_zbc_report_storage_elements` functions, used by different kinds of storage, which use zeroed `zlo->elements` and `memset` the elements before use. This means that the contents are *initialized* either by `kzalloc_flex` or the device code. Finally, the function copies *back* to userspace. `copy_to_user(argp, rep, struct_size_t(struct blk_se_report, elements, rep->nr_elements))`. It copies *back* the contents of the zeroed `rep`, including what has been written into `rep->elements`, to userspace. Wait, the `reserved` field *is* copied from userspace, but the `kzalloc_flex` zeroed the *rest* of the elements and they are then filled with valid data from the driver. This implies no uninitialized memory leaks. So that looks safe. Now, let me verify those `kzalloc_flex` usages. I need to make sure I am not missing some subtle issue. Let me grep for `kzalloc_flex` in the kernel source. |
| 52/3 | 2026/10/09 11:36 | tool | grepper |
0mArgs:{
"Expression": "kzalloc_flex",
"PathPrefixes": [
"include/linux/slab.h"
]
}Results: {
"Output": "include/linux/slab.h=1073=void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node);\n--\ninclude/linux/slab.h-1158-\t__alloc_objs(kzalloc, default_gfp(__VA_ARGS__), typeof(P), COUNT)\ninclude/linux/slab.h:1159:#define kzalloc_flex(P, FAM, COUNT, ...)\t\t\\\ninclude/linux/slab.h-1160-\t__alloc_flex(kzalloc, default_gfp(__VA_ARGS__), typeof(P), FAM, COUNT)\n"
} |
| 53/3 | 2026/10/09 11:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 43586 output: 35 thoughts: 146 Thoughts: Okay, let me think this through. **Analyzing Memory Initialization in `kzalloc_flex` and `zloop`** Alright, so `kzalloc_flex` is using `kzalloc`. That means the allocated memory is guaranteed to be zero-initialized. Good, good, no unexpected garbage there. But wait, what about `zloop`? I need to double-check that. Let's look into how `zloop` handles options, specifically focusing on `zloop.c`. Right, the options parsing... `zloop_parse_options`: I see, `opts->stor_elements` gets set to `ZLOOP_DEF_STOR_ELEMENTS`, and `opts->element_id` gets set to `ZLOOP_DEF_ELEMENT_ID`. Okay, those are sensible defaults. Now, let's trace this further. In `zloop_ctl_write`, it calls `zloop_parse_options` again. And it appears that it initializes all fields of `opts`. Yes, all fields get properly initialized. Okay, let me just confirm the initialization of `opts` in `zloop_ctl_write`. I need to ensure there are no unintended memory leaks. |
| 54/3 | 2026/10/09 11:36 | tool | grepper |
0mArgs:{
"Expression": "struct zloop_options opts",
"PathPrefixes": [
"drivers/block/zloop.c"
]
}Results: {
"Output": "drivers/block/zloop.c=2289=static ssize_t zloop_ctl_write(struct file *file, const char __user *ubuf,\n--\ndrivers/block/zloop.c-2291-{\ndrivers/block/zloop.c:2292:\tstruct zloop_options opts = { };\ndrivers/block/zloop.c-2293-\tstruct zloop_ctl_op *op;\n"
} |
| 55/3 | 2026/10/09 11:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 44142 output: 35 thoughts: 662 Thoughts: **Analyzing Potential Issues in Zoned Block Device Operations**
Okay, so I'm digging into this code, specifically looking for potential vulnerabilities or uninitialized memory reads, which is always a good place to start. First, I see that `opts = { };` and `zloop_parse_options` are setting up default values. That's standard practice. Then I start thinking, "What about `zloop_ctl_show`? Let's check that function." I peek at the code: `seq_puts` is used to print some information. No uninitialized memory there, which is what I wanted to confirm. Good.
But wait, now my mind wanders to `zloop_degrade_element` and `zloop_ctl_degrade_element`. Let me look at that. Ah, here we are, and I'm reminded: if `opts->element_id` isn't specified, it defaults to `ZLOOP_DEF_ELEMENT_ID`, which is 0. Then, inside `zloop_degrade_element`, there's this check: `if (!element_id || element_id > zlo->nr_elements) return -EINVAL;`. So, even if the user doesn't provide an element ID, the code is smart enough to handle that by erroring out if it's invalid. This seems safe, at least from that perspective.
Okay, let's keep moving. I need to move from the `zloop_degrade_element` path, to the `sd_setup_read_write_cmnd`. I have to confirm that `sd_setup_read_write_cmnd` is secure. This function prevents read/write to offline zones of ZBC disks. I see that it calls `disk_zone_is_offline`. This looks interesting, because if an offline zone is requested, it will goto fail. But, is `disk_zone_is_offline` safe? It calls `disk_zone_get_cond`. I need to inspect how `disk_zone_get_cond` is implemented, in `block/blk-zoned.c`, to be certain. I'll need to examine that function's behavior to make sure it's handling everything correctly, especially around sector access and zone conditions. Let me check that next.
|
| 56/3 | 2026/10/09 11:36 | tool | read-file |
0mArgs:{
"File": "block/blk-zoned.c",
"FirstLine": 250,
"LineCount": 45
}Results: {
"Contents": " 250:\t\n 251:\tstatic inline u8 disk_zone_get_state(struct gendisk *disk, sector_t sector)\n 252:\t{\n 253:\t\tunsigned int zno = disk_zone_no(disk, sector);\n 254:\t\tu8 *zones_state, zs;\n 255:\t\n 256:\t\trcu_read_lock();\n 257:\t\tzones_state = rcu_dereference(disk-\u003ezones_state);\n 258:\t\tif (likely(zones_state \u0026\u0026 zno \u003c disk-\u003enr_zones))\n 259:\t\t\tzs = zones_state[zno];\n 260:\t\telse\n 261:\t\t\tzs = BLK_ZFLAG_CONV;\n 262:\t\trcu_read_unlock();\n 263:\t\n 264:\t\treturn zs;\n 265:\t}\n 266:\t\n 267:\tstatic enum blk_zone_cond disk_zone_get_cond(struct gendisk *disk,\n 268:\t\t\t\t\t\t sector_t sector)\n 269:\t{\n 270:\t\tu8 zs = disk_zone_get_state(disk, sector);\n 271:\t\n 272:\t\treturn blk_zstate_to_zone_cond(zs);\n 273:\t}\n 274:\t\n 275:\tstatic inline bool\n 276:\tdisk_zone_cond_is_offline_or_readonly(enum blk_zone_cond cond)\n 277:\t{\n 278:\t\treturn cond == BLK_ZONE_COND_READONLY ||\n 279:\t\t\tcond == BLK_ZONE_COND_OFFLINE;\n 280:\t}\n 281:\t\n 282:\tstatic inline bool disk_zone_is_offline_or_readonly(struct gendisk *disk,\n 283:\t\t\t\t\t\t\t sector_t sector)\n 284:\t{\n 285:\t\tenum blk_zone_cond cond = disk_zone_get_cond(disk, sector);\n 286:\t\n 287:\t\treturn disk_zone_cond_is_offline_or_readonly(cond);\n 288:\t}\n 289:\t\n 290:\tstatic bool disk_zone_is_seq(struct gendisk *disk, sector_t sector)\n 291:\t{\n 292:\t\tu8 zs = disk_zone_get_state(disk, sector);\n 293:\t\n 294:\t\treturn !blk_zstate_is_conv(zs);\n"
} |
| 57/3 | 2026/10/09 11:36 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 46011 output: 37 thoughts: 5353 Thoughts: **Comprehensive Analysis of Storage Element Management in the Linux Kernel** Okay, after this deep dive, I'm confident in saying that the code I've examined surrounding storage element management in the Linux kernel is quite robust in avoiding uninitialized memory reads. My initial concern was about how the `disk_zone_get_state` function handles potential issues, but the check within that function, ensuring the validity of `zones_state` and `zno`, seems sufficient. The path through `disk_init_storage_element_mgmt` was the most critical. I carefully traced its interactions with `blk_queue_is_zoned` and `disk->fops->se_ops`, and the subsequent call to `sd_zbc_report_storage_elements`. The way `sd_zbc_report_storage_elements` handles the `elements == NULL` case, specifically how the `buf` is populated through `scsi_execute_cmd` and how `nr_descs` is validated, is solid. I was initially worried about potential race conditions or uninitialized `buf` data if `scsi_execute_cmd` failed, but the `result != 0` check followed by a `-EIO` return, and the subsequent zero-initialization of the gendisks through `kzalloc_node`, effectively mitigated those concerns. My investigation of the `blkdev_zone_storage_elements_ioctl` was important. The early return of `-ENOTTY` if `disk->nr_storage_elements` is zero is appropriate. I worried about how user-space interacts with the kernel's data in ioctls. While examining `blkdev_report_storage_elements_ioctl` and the `copy_to_user` calls, I ensured that `nr_elements` is always safely bounded by `disk->nr_storage_elements` (with `min`), preventing out-of-bounds reads. I also verified that allocated memory from the kernel is always zeroed. I also thoroughly reviewed `zloop_report_elements`, and its usage of `kzalloc_objs` to initialize `zlo->elements`. I followed the flow from the allocation through its various modifications by `zloop_create_storage_elements`, `zloop_init_zone`, `zloop_degrade_element`, etc., all the way to its final usage in `memcpy` within `zloop_report_elements`. All the writes to `zlo->elements` always provide initialized data. The struct blk_se and blk_se_report were examined, including alignment, and struct layout, to ensure no padding or holes were present that could lead to vulnerabilities. The inspection of `disk_wait_for_se_mgmt_completion` also helped clarify the control flow. The `kzalloc_objs` allocation of `elements` and the subsequent checking in `disk_se_mgmt_in_progress` of the status of each element, with short-circuit evaluation, ensures there are no reads of uninitialized variables. Finally, examining functions like `disk_revalidate_zones`, `bdev_remove_storage_element`, `sd_zbc_report_storage_elements`, `sd_zbc_remove_storage_element`, and `sd_zbc_restore_storage_elements`, further solidified my confidence. They all employ similar initialization and error-checking patterns that prevent uninitialized memory issues. The analysis of `zloop_init_zone` and potential divide-by-zero errors showed that the code handles the edge cases properly, including the case where `zlo->stor_elements` is `ZLOOP_STOR_ELEMENTS_NONE`, where no element management happens, so no elements are accessed. |
| 58/3 | 2026/10/09 11:37 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd.c",
"FirstLine": 1460,
"LineCount": 40
}Results: {
"Contents": "1460:\t\t/* Prevent read/write to offline zones of ZBC disks. */\n1461:\t\tif (sdkp-\u003edevice-\u003etype == TYPE_ZBC \u0026\u0026\n1462:\t\t disk_zone_is_offline(disk, blk_rq_pos(rq)))\n1463:\t\t\tgoto fail;\n1464:\t\n1465:\t\t/*\n1466:\t\t * Some SD card readers can't handle accesses which touch the\n1467:\t\t * last one or two logical blocks. Split accesses as needed.\n1468:\t\t */\n1469:\t\tthreshold = sdkp-\u003ecapacity - SD_LAST_BUGGY_SECTORS;\n1470:\t\n1471:\t\tif (unlikely(sdp-\u003elast_sector_bug \u0026\u0026 lba + nr_blocks \u003e threshold)) {\n1472:\t\t\tif (lba \u003c threshold) {\n1473:\t\t\t\t/* Access up to the threshold but not beyond */\n1474:\t\t\t\tnr_blocks = threshold - lba;\n1475:\t\t\t} else {\n1476:\t\t\t\t/* Access only a single logical block */\n1477:\t\t\t\tnr_blocks = 1;\n1478:\t\t\t}\n1479:\t\t}\n1480:\t\n1481:\t\tfua = rq-\u003ecmd_flags \u0026 REQ_FUA ? 0x8 : 0;\n1482:\t\tdix = scsi_prot_sg_count(cmd);\n1483:\t\tdif = scsi_host_dif_capable(cmd-\u003edevice-\u003ehost, sdkp-\u003eprotection_type);\n1484:\t\tdld = sd_cdl_dld(sdkp, cmd);\n1485:\t\n1486:\t\tif (dif || dix)\n1487:\t\t\tprotect = sd_setup_protect_cmnd(cmd, dix, dif);\n1488:\t\telse\n1489:\t\t\tprotect = 0;\n1490:\t\n1491:\t\tif (protect \u0026\u0026 sdkp-\u003eprotection_type == T10_PI_TYPE2_PROTECTION) {\n1492:\t\t\tret = sd_setup_rw32_cmnd(cmd, write, lba, nr_blocks,\n1493:\t\t\t\t\t\t protect | fua, dld);\n1494:\t\t} else if (rq-\u003ecmd_flags \u0026 REQ_ATOMIC) {\n1495:\t\t\tret = sd_setup_atomic_cmnd(cmd, lba, nr_blocks,\n1496:\t\t\t\t\tsdkp-\u003euse_atomic_write_boundary,\n1497:\t\t\t\t\tprotect | fua);\n1498:\t\t} else if (sdp-\u003euse_16_for_rw || (nr_blocks \u003e 0xffff)) {\n1499:\t\t\tret = sd_setup_rw16_cmnd(cmd, write, lba, nr_blocks,\n"
} |
| 59/3 | 2026/10/09 11:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 52914 output: 37 thoughts: 46 Thoughts: Okay, let's dive into this. **Investigating the `fail:` Label in `sd_setup_read_write_cmnd`** Alright, so I'm seeing this `fail:` label used in `sd_setup_read_write_cmnd`, and my immediate thought is, "What's the context here? What's it doing?" I'm assuming it's part of some error handling, a jump target for a failure condition, but I need to see it in action to understand the logic. Let's go straight to the source. Line numbers are handy! I'm going to check out lines 1520 through 1535 of `drivers/scsi/sd.c`. That should give me a solid picture of what's happening around that `fail:` label and, hopefully, illuminate its purpose within the larger function. This should be a quick way to understand the flow control and error handling within this key function. I'm expecting to see what conditions trigger the jump to `fail:` and what cleanup or logging might be happening there. |
| 60/3 | 2026/10/09 11:37 | tool | read-file |
0mArgs:{
"File": "drivers/scsi/sd.c",
"FirstLine": 1515,
"LineCount": 25
}Results: {
"Contents": "1515:\t\t * host adapter, it's safe to assume that we can at least transfer\n1516:\t\t * this many bytes between each connect / disconnect.\n1517:\t\t */\n1518:\t\tcmd-\u003etransfersize = sdp-\u003esector_size;\n1519:\t\tcmd-\u003eunderflow = nr_blocks \u003c\u003c 9;\n1520:\t\tcmd-\u003eallowed = sdkp-\u003emax_retries;\n1521:\t\tcmd-\u003esdb.length = nr_blocks * sdp-\u003esector_size;\n1522:\t\n1523:\t\tSCSI_LOG_HLQUEUE(1,\n1524:\t\t\t\t scmd_printk(KERN_INFO, cmd,\n1525:\t\t\t\t\t \"%s: block=%llu, count=%d\\n\", __func__,\n1526:\t\t\t\t\t (unsigned long long)blk_rq_pos(rq),\n1527:\t\t\t\t\t blk_rq_sectors(rq)));\n1528:\t\tSCSI_LOG_HLQUEUE(2,\n1529:\t\t\t\t scmd_printk(KERN_INFO, cmd,\n1530:\t\t\t\t\t \"%s %d/%u 512 byte blocks.\\n\",\n1531:\t\t\t\t\t write ? \"writing\" : \"reading\", nr_blocks,\n1532:\t\t\t\t\t blk_rq_sectors(rq)));\n1533:\t\n1534:\t\t/*\n1535:\t\t * This indicates that the command is ready from our end to be queued.\n1536:\t\t */\n1537:\t\treturn BLK_STS_OK;\n1538:\tfail:\n1539:\t\tscsi_free_sgtables(cmd);\n"
} |
| 61/3 | 2026/10/09 11:37 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 53658 output: 539 thoughts: 2013 Thoughts: **KMSAN Assessment: No Need** Okay, let's break this down. My initial concern was, as always, uninitialized memory leaks or reads that could lead to information disclosure or unpredictable behavior. I systematically went through the code changes, focusing on potential areas where this could be an issue, essentially following the KMSAN guidelines in reverse. First, I looked at the error path and how sgtables were being freed – that was straightforward and didn't raise any flags. Then, I scrutinized the `zloop_set_zone_append_sector` function and the associated data structures (`zloop_device`, `zloop_zone`). The key was ensuring `zone->wp` was properly managed and initialized in all code paths, especially with the spinlock usage. The changes were scoped, and the structure looked okay. The `zloop_reset_zone` change and `zloop_set_zone_cond` were reviewed similarly, paying close attention to initialization and state transitions. Everything seemed sane there, no new uninitialized memory issues. The big one was always ioctls, because that's where you expose kernel data to user space. `blkdev_common_ioctl` was the key entry point, and then I dug into `blkdev_zone_storage_elements_ioctl`. I zeroed in on `BLKREPORTSTORELEMS` and the interaction with `copy_to_user`. I made sure `kzalloc_flex` was being used and that there were no padding holes or uninitialized fields. I verified that the driver was doing the right thing. I dug through the code, confirmed the structure layout, and traced the initialization flow. The `BLKGETNRSTORELEMS` ioctl was simple and didn't use `copy_to_user` to pass data, it was just the primitive unsigned integer and `put_user` and looked like a no-op. The `BLKREMOVESTORELEM` and `BLKRESTORESTORELEMS` ones were even easier as they do not invoke `copy_to_user`. So, I ran through the KMSAN trigger list in my head: 1. **Kernel data to user space:** Checked, `kzalloc_flex` is used everywhere, structs like `struct blk_se_report` were analyzed for padding, and the driver code was verified to initialize all fields. No leaks. 2. **Conditional logic based on uninitialized data:** All allocations are zeroed. All variables are initialized before use. 3. **Complex data structure initialization:** No complex structures are left partially initialized. 4. **Bounds checks exposing uninitialized data:** No issues there. This led me to the rule-based decision. The patch does *not* introduce or expose uninitialized memory risks detectable *only* by KMSAN. The potential problems are mainly OOB, NULL dereferences, and locking – all well-covered by KASAN, LOCKDEP, and standard bug detectors. Therefore, `NeedsKMSAN = false`. We don't need the overhead of a dedicated KMSAN session here. The risks are well-managed. |
| 62/3 | 2026/10/09 11:37 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch introduces storage element emulation and management for zoned block devices across blk-zoned, drivers/block/zloop, drivers/scsi/sd_zbc, and adds corresponding UAPI definitions and ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS).\n\nRegarding KMSAN applicability:\n1. Kernel-to-userspace data transfer: In `blkdev_report_storage_elements_ioctl`, the kernel copies `struct blk_se_report` containing flexible array members `struct blk_se elements[]` back to userspace via `copy_to_user`. The buffer is allocated using `kzalloc_flex`, which zero-initializes the entire allocated memory block. Furthermore, `struct blk_se` is 32 bytes in size (id: 4B, paired_id: 4B, nr_zones: 8B, type: 1B, status: 1B, restore_allowed: 1B, reserved: 13B) with an 8-byte alignment requirement and contains no padding holes between fields or at the end. In SCSI ZBC (`sd_zbc_parse_storage_element`), each element is explicitly `memset` to 0 before field assignment. In `zloop_report_elements`, each element descriptor originates from a `kzalloc_objs` allocation. The single integer written by `blkdev_get_nr_storage_elements_ioctl` via `put_user` transfers an initialized `unsigned int`.\n2. Heap and stack initializations: All newly allocated data structures (`zlo-\u003eelements`, `rep`, `buf`, `elements`) are allocated with zeroing allocators (`kzalloc`, `kzalloc_objs`, `kzalloc_flex`), and stack variables (e.g. `struct blk_revalidate_zone_args args = {}`, `struct zloop_options opts = {}`) are zero-initialized.\n3. Control flow: Branching logic does not depend on uninitialized variables.\n\nAny potential bugs in this patch series relate to concurrency/locking (handled by LOCKDEP), array bounds checking (e.g., `element_id - 1` indexing, handled by KASAN), or request queue handling. There are no uninitialized memory risks or kernel info-leaks that require a dedicated KMSAN session.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch introduces storage element emulation and management for zoned block devices across blk-zoned, drivers/block/zloop, drivers/scsi/sd_zbc, and adds corresponding UAPI definitions and ioctls (BLKGETNRSTORELEMS, BLKREPORTSTORELEMS, BLKREMOVESTORELEM, BLKRESTORESTORELEMS).\n\nRegarding KMSAN applicability:\n1. Kernel-to-userspace data transfer: In `blkdev_report_storage_elements_ioctl`, the kernel copies `struct blk_se_report` containing flexible array members `struct blk_se elements[]` back to userspace via `copy_to_user`. The buffer is allocated using `kzalloc_flex`, which zero-initializes the entire allocated memory block. Furthermore, `struct blk_se` is 32 bytes in size (id: 4B, paired_id: 4B, nr_zones: 8B, type: 1B, status: 1B, restore_allowed: 1B, reserved: 13B) with an 8-byte alignment requirement and contains no padding holes between fields or at the end. In SCSI ZBC (`sd_zbc_parse_storage_element`), each element is explicitly `memset` to 0 before field assignment. In `zloop_report_elements`, each element descriptor originates from a `kzalloc_objs` allocation. The single integer written by `blkdev_get_nr_storage_elements_ioctl` via `put_user` transfers an initialized `unsigned int`.\n2. Heap and stack initializations: All newly allocated data structures (`zlo-\u003eelements`, `rep`, `buf`, `elements`) are allocated with zeroing allocators (`kzalloc`, `kzalloc_objs`, `kzalloc_flex`), and stack variables (e.g. `struct blk_revalidate_zone_args args = {}`, `struct zloop_options opts = {}`) are zero-initialized.\n3. Control flow: Branching logic does not depend on uninitialized variables.\n\nAny potential bugs in this patch series relate to concurrency/locking (handled by LOCKDEP), array bounds checking (e.g., `element_id - 1` indexing, handled by KASAN), or request queue handling. There are no uninitialized memory risks or kernel info-leaks that require a dedicated KMSAN session.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|