sd_zbc: Change the type of the ZBC fields into u32
Subsystems (4)
PATCH 1/5 sd_zbc: Change the type of the ZBC fields into u32
In reply to a message not in the archiveThis patch does not change any functionality but makes it clear that it is on purpose that these fields are 32 bits wide.
--- drivers/scsi/sd.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/scsi/sd.h +6 -6
@@ -74,12 +74,12 @@ struct scsi_disk {struct gendisk *disk;struct opal_dev *opal_dev;#ifdef CONFIG_BLK_DEV_ZONED -unsigned int nr_zones; -unsigned int zone_blocks; -unsigned int zone_shift; -unsigned int zones_optimal_open; -unsigned int zones_optimal_nonseq; -unsigned int zones_max_open; +u32 nr_zones; +u32 zone_blocks; +u32 zone_shift; +u32 zones_optimal_open; +u32 zones_optimal_nonseq; +u32 zones_max_open;#endif atomic_t openers;sector_t capacity;/* size in logical blocks */
Signature · 1 line
2.16.3
PATCH 2/5 sd_zbc: Let the SCSI core handle ILLEGAL REQUEST / ASC 0x21
In reply to a message not in the archivescsi_io_completion() translates the sense key ILLEGAL REQUEST / ASC 0x21 into ACTION_FAIL. That means that setting cmd->allowed to zero in sd_zbc_complete() for this sense code / ASC combination is not necessary. Hence remove the code that resets cmd->allowed from sd_zbc_complete().
--- drivers/scsi/sd_zbc.c | 10 ---------- 1 file changed, 10 deletions(-)
drivers/scsi/sd_zbc.c +0 -10
@@ -299,16 +299,6 @@ void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,case REQ_OP_WRITE :case REQ_OP_WRITE_ZEROES :case REQ_OP_WRITE_SAME : - -if (result && - sshdr->sense_key ==ILLEGAL_REQUEST && - sshdr->asc ==0x21 ) -/* -* INVALID ADDRESS FOR WRITE error: It is unlikely that -* retrying write requests failed with any kind of -* alignement error will result in success. So don't. -*/ - cmd->allowed =0 ;break ;case REQ_OP_ZONE_REPORT :
Signature · 1 line
2.16.3
PATCH 3/5 sd_zbc: Log zone reset failures
In reply to a message not in the archiveA zone reset only fails for conventional zones. Resetting a conventional zone indicates a bug in the software that submitted the zone reset so the information about a zone reset failure is useful information. Hence, do not suppress information about zone reset failures.
--- drivers/scsi/sd_zbc.c | 12 ------------ 1 file changed, 12 deletions(-)
drivers/scsi/sd_zbc.c +0 -12
@@ -284,18 +284,6 @@ void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,switch (req_op (rq)) {case REQ_OP_ZONE_RESET : - -if (result && - sshdr->sense_key ==ILLEGAL_REQUEST && - sshdr->asc ==0x24 ) -/* -* INVALID FIELD IN CDB error: reset of a conventional -* zone was attempted. Nothing to worry about, so be -* quiet about the error. -*/ - rq->rq_flags |=RQF_QUIET ; -break ; -case REQ_OP_WRITE :case REQ_OP_WRITE_ZEROES :case REQ_OP_WRITE_SAME :
Signature · 1 line
2.16.3
PATCH 4/5 block: Avoid executing a report or reset zones while a queue is frozen
In reply to a message not in the archiveThis change will be used by the next patch to prevent that zoned block device ioctls are executed while the queue is frozen.
--- block/blk-zoned.c | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-)
block/blk-zoned.c +29 -13
@@ -127,15 +127,19 @@ int blkdev_report_zones(struct block_device *bdev,if (!q)return -ENXIO ; +blk_queue_enter (q,0 ); + + ret = -EOPNOTSUPP ;if (!blk_queue_is_zoned (q)) -return -EOPNOTSUPP ; + goto exit_queue; + ret =0 ;if (!nrz) -return 0 ; + goto exit_queue;if (sector > bdev->bd_part->nr_sects) { *nr_zones =0 ; -return 0 ; + goto exit_queue; }/*
@@ -154,9 +158,10 @@ int blkdev_report_zones(struct block_device *bdev, nr_pages = min_t(unsigned int, nr_pages,queue_max_segments (q)); + ret = -ENOMEM ; bio =bio_alloc (gfp_mask, nr_pages);if (!bio) -return -ENOMEM ; + goto exit_queue;bio_set_dev (bio, bdev); bio->bi_iter.bi_sector =blk_zone_start (q, sector);
@@ -166,7 +171,7 @@ int blkdev_report_zones(struct block_device *bdev, page =alloc_page (gfp_mask);if (!page) { ret = -ENOMEM ; - goto out; + goto put_bio; }if (!bio_add_page (bio, page,PAGE_SIZE ,0 )) {__free_page (page);
@@ -179,7 +184,7 @@ int blkdev_report_zones(struct block_device *bdev,else ret =submit_bio_wait (bio);if (ret) - goto out; + goto put_bio;/* * Process the report result: skip the header and go through the
@@ -222,11 +227,14 @@ int blkdev_report_zones(struct block_device *bdev, } *nr_zones = nz; -out: +put_bio:bio_for_each_segment_all (bv, bio, i)__free_page (bv->bv_page);bio_put (bio); +exit_queue: +blk_queue_exit (q); +return ret; }EXPORT_SYMBOL_GPL (blkdev_report_zones);
@@ -256,21 +264,25 @@ int blkdev_reset_zones(struct block_device *bdev,if (!q)return -ENXIO ; +blk_queue_enter (q,0 ); + + ret = -EOPNOTSUPP ;if (!blk_queue_is_zoned (q)) -return -EOPNOTSUPP ; + goto out; + ret = -EINVAL ;if (end_sector > bdev->bd_part->nr_sects)/* Out of range */ -return -EINVAL ; + goto out;/* Check alignment (handle eventual smaller last zone) */ zone_sectors =blk_queue_zone_sectors (q);if (sector & (zone_sectors -1 )) -return -EINVAL ; + goto out;if ((nr_sectors & (zone_sectors -1 )) && end_sector != bdev->bd_part->nr_sects) -return -EINVAL ; + goto out;while (sector < end_sector) {
@@ -283,7 +295,7 @@ int blkdev_reset_zones(struct block_device *bdev,bio_put (bio);if (ret) -return ret; + goto out; sector += zone_sectors;
@@ -292,7 +304,11 @@ int blkdev_reset_zones(struct block_device *bdev, } -return 0 ; + ret =0 ; + +out: +blk_queue_exit (q); +return ret; }EXPORT_SYMBOL_GPL (blkdev_reset_zones);
Signature · 1 line
2.16.3
PATCH 5/5 sd_zbc: Avoid that resetting a zone fails sporadically
In reply to a message not in the archiveSince SCSI scanning occurs asynchronously, since sd_revalidate_disk() is called from sd_probe_async() and since sd_revalidate_disk() calls sd_zbc_read_zones() it can happen that sd_zbc_read_zones() is called concurrently with blkdev_report_zones() and/or blkdev_reset_zones(). That can cause these functions to fail with -EIO because sd_zbc_read_zones() e.g. sets q->nr_zones to zero before restoring it to the actual value, even if no drive characteristics have changed. Avoid that this can happen by making the following changes:
- Protect the code that updates zone information with blk_queue_enter() and blk_queue_exit().
- Modify sd_zbc_setup_seq_zones_bitmap() and sd_zbc_setup() such that these functions do not modify struct scsi_disk before all zone information has been obtained.
Note: since commit 055f6e18e08f ("block: Make q_usage_counter also track legacy requests"; kernel v4.15) the request queue freezing mechanism also affects legacy request queues.
--- drivers/scsi/sd_zbc.c | 140 +++++++++++++++++++++++++++++-------------------- include/linux/blkdev.h | 5 ++ 2 files changed, 87 insertions(+), 58 deletions(-)
drivers/scsi/sd_zbc.c +82 -58
@@ -378,8 +378,10 @@ static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf)* * Check that all zones of the device are equal. The last zone can however * be smaller. The zone size must also be a power of two number of LBAs. +* +* Returns the zone size in bytes upon success or an error code upon failure. */ -static int sd_zbc_check_zone_size (struct scsi_disk *sdkp) +static s64 sd_zbc_check_zone_size (struct scsi_disk *sdkp) {u64 zone_blocks =0 ;sector_t block =0 ;
@@ -390,8 +392,6 @@ static int sd_zbc_check_zone_size(struct scsi_disk *sdkp)int ret;u8 same; - sdkp->zone_blocks =0 ; -/* Get a buffer */ buf =kmalloc (SD_ZBC_BUF_SIZE ,GFP_KERNEL );if (!buf)
@@ -423,16 +423,17 @@ static int sd_zbc_check_zone_size(struct scsi_disk *sdkp)/* Parse zone descriptors */ while (rec < buf + buf_len) { - zone_blocks =get_unaligned_be64 (&rec[8 ]); -if (sdkp->zone_blocks ==0 ) { - sdkp->zone_blocks = zone_blocks; - }else if (zone_blocks != sdkp->zone_blocks && - (block + zone_blocks < sdkp->capacity - || zone_blocks > sdkp->zone_blocks)) { - zone_blocks =0 ; +u64 this_zone_blocks =get_unaligned_be64 (&rec[8 ]); + +if (zone_blocks ==0 ) { + zone_blocks = this_zone_blocks; + }else if (this_zone_blocks != zone_blocks && + (block + this_zone_blocks < sdkp->capacity + || this_zone_blocks > zone_blocks)) { + this_zone_blocks =0 ; goto out; } - block += zone_blocks; + block += this_zone_blocks; rec +=64 ; }
@@ -445,8 +446,6 @@ static int sd_zbc_check_zone_size(struct scsi_disk *sdkp) }while (block < sdkp->capacity); - zone_blocks = sdkp->zone_blocks; - out:if (!zone_blocks) {if (sdkp->first_scan)
@@ -466,8 +465,7 @@ static int sd_zbc_check_zone_size(struct scsi_disk *sdkp) "Zone size too large\n"); ret = -ENODEV ; }else { - sdkp->zone_blocks = zone_blocks; - sdkp->zone_shift =ilog2 (zone_blocks); + ret = zone_blocks; } out_free:
@@ -478,15 +476,14 @@ static int sd_zbc_check_zone_size(struct scsi_disk *sdkp)/** * sd_zbc_alloc_zone_bitmap - Allocate a zone bitmap (one bit per zone). -* @sdkp: The disk of the bitmap +* @nr_zones: Number of zones to allocate space for. +* @numa_node: NUMA node to allocate the memory from. */ -static inline unsigned long *sd_zbc_alloc_zone_bitmap (struct scsi_disk *sdkp) +static inline unsigned long * +sd_zbc_alloc_zone_bitmap (u32 nr_zones,int numa_node) { -struct request_queue *q = sdkp->disk->queue; - -return kzalloc_node (BITS_TO_LONGS (sdkp->nr_zones) - *sizeof (unsigned long ), -GFP_KERNEL , q->node); +return kzalloc_node (BITS_TO_LONGS (nr_zones) *sizeof (unsigned long ), +GFP_KERNEL , numa_node); }/**
@@ -494,6 +491,7 @@ static inline unsigned long *sd_zbc_alloc_zone_bitmap(struct scsi_disk *sdkp)* @sdkp: disk used * @buf: report reply buffer * @buflen: length of @buf +* @zone_shift: logarithm base 2 of the number of blocks in a zone * @seq_zones_bitmap: bitmap of sequential zones to set * * Parse reported zone descriptors in @buf to identify sequential zones and
@@ -503,7 +501,7 @@ static inline unsigned long *sd_zbc_alloc_zone_bitmap(struct scsi_disk *sdkp)* Return the LBA after the last zone reported. */ static sector_t sd_zbc_get_seq_zones (struct scsi_disk *sdkp,unsigned char *buf, -unsigned int buflen, +unsigned int buflen,u32 zone_shift,unsigned long *seq_zones_bitmap) {sector_t lba, next_lba = sdkp->capacity;
@@ -522,7 +520,7 @@ static sector_t sd_zbc_get_seq_zones(struct scsi_disk *sdkp, unsigned char *buf,if (type !=ZBC_ZONE_TYPE_CONV && cond !=ZBC_ZONE_COND_READONLY && cond !=ZBC_ZONE_COND_OFFLINE ) -set_bit (lba >> sdkp->zone_shift, seq_zones_bitmap); +set_bit (lba >> zone_shift, seq_zones_bitmap); next_lba = lba +get_unaligned_be64 (&rec[8 ]); rec +=64 ; }
@@ -531,12 +529,16 @@ static sector_t sd_zbc_get_seq_zones(struct scsi_disk *sdkp, unsigned char *buf, }/** -* sd_zbc_setup_seq_zones_bitmap - Initialize the disk seq zone bitmap. +* sd_zbc_setup_seq_zones_bitmap - Initialize a seq zone bitmap. * @sdkp: target disk +* @zone_shift: logarithm base 2 of the number of blocks in a zone +* @nr_zones: number of zones to set up a seq zone bitmap for * * Allocate a zone bitmap and initialize it by identifying sequential zones. */ -static int sd_zbc_setup_seq_zones_bitmap (struct scsi_disk *sdkp) +static unsigned long * +sd_zbc_setup_seq_zones_bitmap (struct scsi_disk *sdkp,u32 zone_shift, +u32 nr_zones) {struct request_queue *q = sdkp->disk->queue;unsigned long *seq_zones_bitmap;
@@ -544,9 +546,9 @@ static int sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp)unsigned char *buf;int ret = -ENOMEM ; - seq_zones_bitmap =sd_zbc_alloc_zone_bitmap (sdkp); + seq_zones_bitmap =sd_zbc_alloc_zone_bitmap (nr_zones, q->node);if (!seq_zones_bitmap) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); buf =kmalloc (SD_ZBC_BUF_SIZE ,GFP_KERNEL );if (!buf)
@@ -557,7 +559,7 @@ static int sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp)if (ret) goto out; lba =sd_zbc_get_seq_zones (sdkp, buf,SD_ZBC_BUF_SIZE , - seq_zones_bitmap); + zone_shift, seq_zones_bitmap); }if (lba != sdkp->capacity) {
@@ -569,12 +571,9 @@ static int sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp)kfree (buf);if (ret) {kfree (seq_zones_bitmap); -return ret; +return ERR_PTR (ret); } - - q->seq_zones_bitmap = seq_zones_bitmap; - -return 0 ; +return seq_zones_bitmap; }static void sd_zbc_cleanup (struct scsi_disk *sdkp)
@@ -590,44 +589,64 @@ static void sd_zbc_cleanup(struct scsi_disk *sdkp) q->nr_zones =0 ; } -static int sd_zbc_setup (struct scsi_disk *sdkp) +static int sd_zbc_setup (struct scsi_disk *sdkp,u32 zone_blocks) {struct request_queue *q = sdkp->disk->queue; +u32 zone_shift =ilog2 (zone_blocks); +u32 nr_zones;int ret; -/* READ16/WRITE16 is mandatory for ZBC disks */ - sdkp->device->use_16_for_rw =1 ; - sdkp->device->use_10_for_rw =0 ; -/* chunk_sectors indicates the zone size */ -blk_queue_chunk_sectors (sdkp->disk->queue, -logical_to_sectors (sdkp->device, sdkp->zone_blocks)); - sdkp->nr_zones = -round_up (sdkp->capacity, sdkp->zone_blocks) >> sdkp->zone_shift; +blk_queue_chunk_sectors (q, +logical_to_sectors (sdkp->device, zone_blocks)); + nr_zones =round_up (sdkp->capacity, zone_blocks) >> zone_shift;/* * Initialize the device request queue information if the number * of zones changed. */ -if (sdkp->nr_zones != q->nr_zones) { - -sd_zbc_cleanup (sdkp); - - q->nr_zones = sdkp->nr_zones; -if (sdkp->nr_zones) { - q->seq_zones_wlock =sd_zbc_alloc_zone_bitmap (sdkp); -if (!q->seq_zones_wlock) { +if (nr_zones != sdkp->nr_zones || nr_zones != q->nr_zones) { +unsigned long *seq_zones_wlock =NULL , *seq_zones_bitmap =NULL ; +size_t zone_bitmap_size; + +if (nr_zones) { + seq_zones_wlock =sd_zbc_alloc_zone_bitmap (nr_zones, + q->node); +if (!seq_zones_wlock) { ret = -ENOMEM ; goto err; } - ret =sd_zbc_setup_seq_zones_bitmap (sdkp); -if (ret) { -sd_zbc_cleanup (sdkp); + seq_zones_bitmap =sd_zbc_setup_seq_zones_bitmap (sdkp, + zone_shift, nr_zones); +if (IS_ERR (seq_zones_bitmap)) { + ret =PTR_ERR (seq_zones_bitmap); +kfree (seq_zones_wlock); goto err; } } - + zone_bitmap_size =BITS_TO_LONGS (nr_zones) * +sizeof (unsigned long ); +blk_mq_freeze_queue (q); +if (q->nr_zones != nr_zones) { +/* READ16/WRITE16 is mandatory for ZBC disks */ + sdkp->device->use_16_for_rw =1 ; + sdkp->device->use_10_for_rw =0 ; + + sdkp->zone_blocks = zone_blocks; + sdkp->zone_shift = zone_shift; + sdkp->nr_zones = nr_zones; + q->nr_zones = nr_zones; +swap (q->seq_zones_wlock, seq_zones_wlock); +swap (q->seq_zones_bitmap, seq_zones_bitmap); + }else if (memcmp (q->seq_zones_bitmap, seq_zones_bitmap, + zone_bitmap_size) !=0 ) { +memcpy (q->seq_zones_bitmap, seq_zones_bitmap, + zone_bitmap_size); + } +blk_mq_unfreeze_queue (q); +kfree (seq_zones_wlock); +kfree (seq_zones_bitmap); }return 0 ;
@@ -639,6 +658,7 @@ static int sd_zbc_setup(struct scsi_disk *sdkp)int sd_zbc_read_zones (struct scsi_disk *sdkp,unsigned char *buf) { +int64_t zone_blocks;int ret;if (!sd_is_zoned (sdkp))
@@ -675,12 +695,16 @@ int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)* Check zone size: only devices with a constant zone size (except * an eventual last runt zone) that is a power of 2 are supported. */ - ret =sd_zbc_check_zone_size (sdkp); -if (ret) + zone_blocks =sd_zbc_check_zone_size (sdkp); + ret = -EFBIG ; +if (zone_blocks != (u32 )zone_blocks) + goto err; + ret = zone_blocks; +if (ret <0 ) goto err;/* The drive satisfies the kernel restrictions: set it up */ - ret =sd_zbc_setup (sdkp); + ret =sd_zbc_setup (sdkp, zone_blocks);if (ret) goto err;
include/linux/blkdev.h +5 -0
@@ -605,6 +605,11 @@ struct request_queue {* initialized by the low level device driver (e.g. scsi/sd.c). * Stacking drivers (device mappers) may or may not initialize * these fields. +* +* Reads of this information must be protected with blk_queue_enter() / +* blk_queue_exit(). Modifying this information is only allowed while +* no requests are being processed. See also blk_mq_freeze_queue() and +* blk_mq_unfreeze_queue(). */ unsigned int nr_zones;unsigned long *seq_zones_bitmap;
Signature · 1 line
2.16.3