SCSI patches for kernel v4.13
Subsystems (7)
v3 cover SCSI patches for kernel v4.13
Hello Martin,
This patch series consists of the bug fixes and improvements I came up with during the past two months. This patch series has been developed on top of your 4.13/scsi-queue branch. Please consider these patches for kernel v4.13.
Thanks,
Bart.
The changes compared to v2 of this patch series are:
- Addressed Christoph's review comments: added an explanation to patch "Protect SCSI device state changes with a mutex" of why that change is needed. Removed a printk() from patch "Make __scsi_remove_device go straight from BLOCKED to DEL". For scsi-mq, moved the initialization of .prot_sdb from scsi_mq_prep_fn() into scsi_init_request(). Fixed the driver name in the virtio_scsi patch.
Between v1 and v2:
- Left out the block layer patches from this series.
- Reworked this patch series such that it applies cleanly on the 4.13 SCSI patch queue and no longer depends on any block layer changes that are not yet upstream.
- In patch "Avoid that scsi_exit_rq() triggers a use-after-free", make the prep functions save and restore the SCMD_UNCHECKED_ISA_DMA flag.
- Added patch "Introduce scsi_start_queue()".
Bart Van Assche (12):
Avoid that scsi_exit_rq() triggers a use-after-free Split scsi_internal_device_block()
Create two versions of scsi_internal_device_unblock() Protect SCSI device state changes with a mutex
Introduce scsi_start_queue()
Make __scsi_remove_device go straight from BLOCKED to DEL Only add commands to the device command list if required by the LLD Introduce scsi_mq_sgl_size()
Make scsi_mq_prep_fn() call scsi_init_command()
snic: Remove code that zeroes driver-private command data
virtio_scsi: Remove code that zeroes driver-private command data xen/scsifront: Remove code that zeroes driver-private command data
drivers/scsi/mpt3sas/mpt3sas_scsih.c | 8 +- drivers/scsi/scsi.c | 9 +- drivers/scsi/scsi_error.c | 8 +- drivers/scsi/scsi_lib.c | 306 ++++++++++++++++++++++------------- drivers/scsi/scsi_priv.h | 3 + drivers/scsi/scsi_scan.c | 16 +- drivers/scsi/scsi_sysfs.c | 34 +++- drivers/scsi/scsi_transport_srp.c | 7 +- drivers/scsi/sd.c | 7 +- drivers/scsi/snic/snic_scsi.c | 2 - drivers/scsi/virtio_scsi.c | 1 - drivers/scsi/xen-scsifront.c | 1 - include/scsi/scsi_cmnd.h | 1 + include/scsi/scsi_device.h | 7 +- 14 files changed, 258 insertions(+), 152 deletions(-)
Signature · 1 line
2.12.2
PATCH v3 3/12 Create two versions of scsi_internal_device_unblock()
This will make it easier to serialize SCSI device state changes through a mutex.
--- drivers/scsi/mpt3sas/mpt3sas_scsih.c | 4 ++-- drivers/scsi/scsi_lib.c | 46 +++++++++++++++++++++++++----------- include/scsi/scsi_device.h | 4 ++-- 3 files changed, 36 insertions(+), 18 deletions(-)
drivers/scsi/mpt3sas/mpt3sas_scsih.c +2 -2
@@ -2883,7 +2883,7 @@ _scsih_internal_device_unblock(struct scsi_device *sdev,sdev_printk (KERN_WARNING , sdev,"device_unblock and setting to running, " "handle(0x%04x)\n" , sas_device_priv_data->sas_target->handle); sas_device_priv_data->block =0 ; - r =scsi_internal_device_unblock (sdev,SDEV_RUNNING ); + r =scsi_internal_device_unblock_nowait (sdev,SDEV_RUNNING );if (r == -EINVAL ) {/* The device has been set to SDEV_RUNNING by SD layer during * device addition but the request queue is still stopped by
@@ -2902,7 +2902,7 @@ _scsih_internal_device_unblock(struct scsi_device *sdev, r, sas_device_priv_data->sas_target->handle); sas_device_priv_data->block =0 ; - r =scsi_internal_device_unblock (sdev,SDEV_RUNNING ); + r =scsi_internal_device_unblock_nowait (sdev,SDEV_RUNNING );if (r) sdev_printk(KERN_WARNING, sdev, "retried device_unblock" " failed with return(%d) for handle(0x%04x)\n",
drivers/scsi/scsi_lib.c +32 -14
63 lines not on this page · raw message
include/scsi/scsi_device.h +2 -2
@@ -473,8 +473,8 @@ static inline int scsi_device_created(struct scsi_device *sdev) }int scsi_internal_device_block_nowait (struct scsi_device *sdev); -int scsi_internal_device_unblock (struct scsi_device *sdev, -enum scsi_device_state new_state); +int scsi_internal_device_unblock_nowait (struct scsi_device *sdev, +enum scsi_device_state new_state);/* accessor functions for the SCSI parameters */ static inline int scsi_device_sync (struct scsi_device *sdev)
Signature · 1 line
2.12.2
PATCH v3 2/12 Split scsi_internal_device_block()
In reply to Bart Van AsscheInstead of passing a "wait" argument to scsi_internal_device_block(), split this function into a function that waits and a function that doesn't wait. This will make it easier to serialize SCSI device state changes through a mutex.
--- drivers/scsi/mpt3sas/mpt3sas_scsih.c | 4 +- drivers/scsi/scsi_lib.c | 73 +++++++++++++++++++++++------------- include/scsi/scsi_device.h | 2 +- 3 files changed, 50 insertions(+), 29 deletions(-)
drivers/scsi/mpt3sas/mpt3sas_scsih.c +2 -2
@@ -2859,7 +2859,7 @@ _scsih_internal_device_block(struct scsi_device *sdev, sas_device_priv_data->sas_target->handle); sas_device_priv_data->block =1 ; - r =scsi_internal_device_block (sdev, false); + r =scsi_internal_device_block_nowait (sdev);if (r == -EINVAL ) sdev_printk(KERN_WARNING, sdev, "device_block failed with return(%d) for handle(0x%04x)\n",
@@ -2895,7 +2895,7 @@ _scsih_internal_device_unblock(struct scsi_device *sdev,"performing a block followed by an unblock\n" , r, sas_device_priv_data->sas_target->handle); sas_device_priv_data->block =1 ; - r =scsi_internal_device_block (sdev, false); + r =scsi_internal_device_block_nowait (sdev);if (r) sdev_printk(KERN_WARNING, sdev, "retried device_block " "failed with return(%d) for handle(0x%04x)\n",
drivers/scsi/scsi_lib.c +47 -26
103 lines not on this page · raw message
include/scsi/scsi_device.h +1 -1
@@ -472,7 +472,7 @@ static inline int scsi_device_created(struct scsi_device *sdev) sdev->sdev_state ==SDEV_CREATED_BLOCK ; } -int scsi_internal_device_block (struct scsi_device *sdev,bool wait); +int scsi_internal_device_block_nowait (struct scsi_device *sdev);int scsi_internal_device_unblock (struct scsi_device *sdev,enum scsi_device_state new_state);
Signature · 1 line
2.12.2
PATCH v3 11/12 virtio_scsi: Remove code that zeroes driver-private command data
In reply to Bart Van AsscheSince the SCSI core zeroes driver-private command data, remove that code from the virtio driver.
--- drivers/scsi/virtio_scsi.c | 1 - 1 file changed, 1 deletion(-)
drivers/scsi/virtio_scsi.c +0 -1
@@ -547,7 +547,6 @@ static int virtscsi_queuecommand(struct virtio_scsi *vscsi,dev_dbg (&sc->device->sdev_gendev,"cmd %p CDB: %#02x\n" , sc, sc->cmnd[0 ]); -memset (cmd,0 ,sizeof (*cmd)); cmd->sc = sc;BUG_ON (sc->cmd_len >VIRTIO_SCSI_CDB_SIZE );
Signature · 1 line
2.12.2
PATCH v3 5/12 Introduce scsi_start_queue()
In reply to Bart Van AsscheThis patch does not change any functionality.
--- drivers/scsi/scsi_lib.c | 25 +++++++++++++++---------- drivers/scsi/scsi_priv.h | 1 + 2 files changed, 16 insertions(+), 10 deletions(-)
drivers/scsi/scsi_lib.c +15 -10
@@ -3030,6 +3030,20 @@ static int scsi_internal_device_block(struct scsi_device *sdev)return err; } +void scsi_start_queue (struct scsi_device *sdev) +{ +struct request_queue *q = sdev->request_queue; +unsigned long flags; + +if (q->mq_ops) { +blk_mq_start_stopped_hw_queues (q, false); + }else { +spin_lock_irqsave (q->queue_lock, flags); +blk_start_queue (q); +spin_unlock_irqrestore (q->queue_lock, flags); + } +} +/** * scsi_internal_device_unblock_nowait - resume a device after a block request * @sdev: device to resume
@@ -3048,9 +3062,6 @@ static int scsi_internal_device_block(struct scsi_device *sdev)int scsi_internal_device_unblock_nowait (struct scsi_device *sdev,enum scsi_device_state new_state) { -struct request_queue *q = sdev->request_queue; -unsigned long flags; -/* * Try to transition the scsi device to SDEV_RUNNING or one of the * offlined states and goose the device queue if successful.
@@ -3068,13 +3079,7 @@ int scsi_internal_device_unblock_nowait(struct scsi_device *sdev, sdev->sdev_state != SDEV_OFFLINE)return -EINVAL ; -if (q->mq_ops) { -blk_mq_start_stopped_hw_queues (q, false); - }else { -spin_lock_irqsave (q->queue_lock, flags); -blk_start_queue (q); -spin_unlock_irqrestore (q->queue_lock, flags); - } +scsi_start_queue (sdev);return 0 ; }
drivers/scsi/scsi_priv.h +1 -0
@@ -88,6 +88,7 @@ extern void scsi_run_host_queues(struct Scsi_Host *shost);extern void scsi_requeue_run_queue (struct work_struct *work);extern struct request_queue *scsi_alloc_queue (struct scsi_device *sdev);extern struct request_queue *scsi_mq_alloc_queue (struct scsi_device *sdev); +extern void scsi_start_queue (struct scsi_device *sdev);extern int scsi_mq_setup_tags (struct Scsi_Host *shost);extern void scsi_mq_destroy_tags (struct Scsi_Host *shost);extern int scsi_init_queue (void );
Signature · 1 line
2.12.2
PATCH v3 6/12 Make __scsi_remove_device go straight from BLOCKED to DEL
In reply to Bart Van AsscheIf a device is blocked, make __scsi_remove_device() cause it to transition to the DEL state. This means that all the commands issued in .shutdown() will error in the mid-layer, thus making the removal proceed without being stopped.
This patch is a slightly modified version of a patch from James Bottomley. This patch avoids that the following lockup occurs:
Call Trace: schedule+0x35/0x80 schedule_timeout+0x237/0x2d0 io_schedule_timeout+0xa6/0x110 wait_for_completion_io+0xa3/0x110 blk_execute_rq+0xdf/0x120 scsi_execute+0xce/0x150 [scsi_mod] scsi_execute_req_flags+0x8f/0xf0 [scsi_mod] sd_sync_cache+0xa9/0x190 [sd_mod] sd_shutdown+0x6a/0x100 [sd_mod] sd_remove+0x64/0xc0 [sd_mod] __device_release_driver+0x8d/0x120 device_release_driver+0x1e/0x30 bus_remove_device+0xf9/0x170 device_del+0x127/0x240 __scsi_remove_device+0xc1/0xd0 [scsi_mod] scsi_forget_host+0x57/0x60 [scsi_mod] scsi_remove_host+0x72/0x110 [scsi_mod] srp_remove_work+0x8b/0x200 [ib_srp]
--- drivers/scsi/scsi_lib.c | 2 +- drivers/scsi/scsi_sysfs.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
drivers/scsi/scsi_lib.c +1 -1
@@ -2624,7 +2624,6 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)case SDEV_QUIESCE :case SDEV_OFFLINE :case SDEV_TRANSPORT_OFFLINE : -case SDEV_BLOCK :break ;default : goto illegal;
@@ -2638,6 +2637,7 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)case SDEV_OFFLINE :case SDEV_TRANSPORT_OFFLINE :case SDEV_CANCEL : +case SDEV_BLOCK :case SDEV_CREATED_BLOCK :break ;default :
drivers/scsi/scsi_sysfs.c +10 -0
@@ -1290,7 +1290,17 @@ void __scsi_remove_device(struct scsi_device *sdev)* wait until it has finished before changing the device state. */ mutex_lock (&sdev->state_mutex); +/* +* If blocked, we go straight to DEL and restart the queue so +* any commands issued during driver shutdown (like sync +* cache) are errored immediately. +*/ res =scsi_device_set_state (sdev,SDEV_CANCEL ); +if (res !=0 ) { + res =scsi_device_set_state (sdev,SDEV_DEL ); +if (res ==0 ) +scsi_start_queue (sdev); + }mutex_unlock (&sdev->state_mutex);if (res !=0 )
Signature · 1 line
2.12.2
PATCH v3 1/12 Avoid that scsi_exit_rq() triggers a use-after-free
In reply to Bart Van AsscheDereferencing shost from scsi_exit_rq() is not safe because the SCSI host may already have been freed when scsi_exit_rq() is called. Increasing the shost reference count in scsi_init_rq() and dropping that reference in scsi_exit_rq() is nontrivial since scsi_host_dev_release() may sleep and since scsi_exit_rq() may be called from interrupt context. Since scsi_exit_rq() only needs a single bit from shost, copy that bit into struct scsi_cmnd.
--- drivers/scsi/scsi_lib.c | 47 +++++++++++++++++++++++++++++------------------ include/scsi/scsi_cmnd.h | 1 + 2 files changed, 30 insertions(+), 18 deletions(-)
drivers/scsi/scsi_lib.c +29 -18
@@ -44,23 +44,23 @@ static struct kmem_cache *scsi_sense_isadma_cache;static DEFINE_MUTEX (scsi_sense_cache_mutex);static inline struct kmem_cache * -scsi_select_sense_cache (struct Scsi_Host *shost) +scsi_select_sense_cache (bool unchecked_isa_dma) { -return shost->unchecked_isa_dma ? - scsi_sense_isadma_cache : scsi_sense_cache; +return unchecked_isa_dma ? scsi_sense_isadma_cache : scsi_sense_cache; } -static void scsi_free_sense_buffer (struct Scsi_Host *shost, -unsigned char *sense_buffer) +static void scsi_free_sense_buffer (bool unchecked_isa_dma, +unsigned char *sense_buffer) { -kmem_cache_free (scsi_select_sense_cache (shost), sense_buffer); +kmem_cache_free (scsi_select_sense_cache (unchecked_isa_dma), + sense_buffer); } -static unsigned char *scsi_alloc_sense_buffer (struct Scsi_Host *shost, +static unsigned char *scsi_alloc_sense_buffer (bool unchecked_isa_dma,gfp_t gfp_mask,int numa_node) { -return kmem_cache_alloc_node (scsi_select_sense_cache (shost), gfp_mask, - numa_node); +return kmem_cache_alloc_node (scsi_select_sense_cache (unchecked_isa_dma), + gfp_mask, numa_node); }int scsi_init_sense_cache (struct Scsi_Host *shost)
@@ -68,7 +68,7 @@ int scsi_init_sense_cache(struct Scsi_Host *shost)struct kmem_cache *cache;int ret =0 ; - cache =scsi_select_sense_cache (shost); + cache =scsi_select_sense_cache (shost->unchecked_isa_dma);if (cache)return 0 ;
@@ -1137,6 +1137,7 @@ void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd) {void *buf = cmd->sense_buffer;void *prot = cmd->prot_sdb; +unsigned int unchecked_isa_dma = cmd->flags &SCMD_UNCHECKED_ISA_DMA ;unsigned long flags;/* zero out the cmd, except for the embedded scsi_request */
@@ -1146,6 +1147,7 @@ void scsi_init_command(struct scsi_device *dev, struct scsi_cmnd *cmd) cmd->device = dev; cmd->sense_buffer = buf; cmd->prot_sdb = prot; + cmd->flags = unchecked_isa_dma;INIT_DELAYED_WORK (&cmd->abort_work, scmd_eh_abort_handler); cmd->jiffies_at_alloc = jiffies;
@@ -1846,6 +1848,7 @@ static int scsi_mq_prep_fn(struct request *req)struct scsi_device *sdev = req->q->queuedata;struct Scsi_Host *shost = sdev->host;unsigned char *sense_buf = cmd->sense_buffer; +unsigned int unchecked_isa_dma = cmd->flags &SCMD_UNCHECKED_ISA_DMA ;struct scatterlist *sg;/* zero out the cmd, except for the embedded scsi_request */
@@ -1857,6 +1860,7 @@ static int scsi_mq_prep_fn(struct request *req) cmd->request = req; cmd->device = sdev; cmd->sense_buffer = sense_buf; + cmd->flags = unchecked_isa_dma; cmd->tag = req->tag;
@@ -2003,10 +2007,13 @@ static int scsi_init_request(struct blk_mq_tag_set *set, struct request *rq, unsigned int hctx_idx, unsigned int numa_node) { struct Scsi_Host *shost = set->driver_data; +const bool unchecked_isa_dma = shost->unchecked_isa_dma;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq); - cmd->sense_buffer = -scsi_alloc_sense_buffer (shost,GFP_KERNEL , numa_node); +if (unchecked_isa_dma) + cmd->flags |=SCMD_UNCHECKED_ISA_DMA ; + cmd->sense_buffer =scsi_alloc_sense_buffer (unchecked_isa_dma, +GFP_KERNEL , numa_node);if (!cmd->sense_buffer)return -ENOMEM ; cmd->req.sense = cmd->sense_buffer;
@@ -2016,10 +2023,10 @@ static int scsi_init_request(struct blk_mq_tag_set *set, struct request *rq,static void scsi_exit_request (struct blk_mq_tag_set *set,struct request *rq,unsigned int hctx_idx) { -struct Scsi_Host *shost = set->driver_data;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq); -scsi_free_sense_buffer (shost, cmd->sense_buffer); +scsi_free_sense_buffer (cmd->flags &SCMD_UNCHECKED_ISA_DMA , + cmd->sense_buffer); }static int scsi_map_queues (struct blk_mq_tag_set *set)
@@ -2092,11 +2099,15 @@ EXPORT_SYMBOL_GPL(__scsi_init_queue);static int scsi_init_rq (struct request_queue *q,struct request *rq,gfp_t gfp) {struct Scsi_Host *shost = q->rq_alloc_data; +const bool unchecked_isa_dma = shost->unchecked_isa_dma;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq);memset (cmd,0 ,sizeof (*cmd)); - cmd->sense_buffer =scsi_alloc_sense_buffer (shost, gfp,NUMA_NO_NODE ); +if (unchecked_isa_dma) + cmd->flags |=SCMD_UNCHECKED_ISA_DMA ; + cmd->sense_buffer =scsi_alloc_sense_buffer (unchecked_isa_dma, gfp, +NUMA_NO_NODE );if (!cmd->sense_buffer) goto fail; cmd->req.sense = cmd->sense_buffer;
@@ -2110,19 +2121,19 @@ static int scsi_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)return 0 ; fail_free_sense: -scsi_free_sense_buffer (shost, cmd->sense_buffer); +scsi_free_sense_buffer (unchecked_isa_dma, cmd->sense_buffer); fail:return -ENOMEM ; }static void scsi_exit_rq (struct request_queue *q,struct request *rq) { -struct Scsi_Host *shost = q->rq_alloc_data;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq);if (cmd->prot_sdb)kmem_cache_free (scsi_sdb_cache, cmd->prot_sdb); -scsi_free_sense_buffer (shost, cmd->sense_buffer); +scsi_free_sense_buffer (cmd->flags &SCMD_UNCHECKED_ISA_DMA , + cmd->sense_buffer); }struct request_queue *scsi_alloc_queue (struct scsi_device *sdev)
include/scsi/scsi_cmnd.h +1 -0
@@ -56,6 +56,7 @@ struct scsi_pointer {/* for scmd->flags */ #define SCMD_TAGGED (1 << 0) +#define SCMD_UNCHECKED_ISA_DMA (1 << 1)struct scsi_cmnd {struct scsi_request req;
Signature · 1 line
2.12.2
PATCH v3 8/12 Introduce scsi_mq_sgl_size()
In reply to Bart Van AsscheThis patch does not change any functionality but makes the next patch easier to read.
--- drivers/scsi/scsi_lib.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/scsi/scsi_lib.c +10 -9
@@ -1858,6 +1858,13 @@ static inline int prep_to_mq(int ret) } } +/* Size in bytes of the sg-list stored in the scsi-mq command-private data. */ +static unsigned int scsi_mq_sgl_size (struct Scsi_Host *shost) +{ + return min_t(unsigned int, shost->sg_tablesize, SG_CHUNK_SIZE) * +sizeof (struct scatterlist ); +} +static int scsi_mq_prep_fn (struct request *req) {struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (req);
@@ -1892,10 +1899,7 @@ static int scsi_mq_prep_fn(struct request *req) cmd->sdb.table.sgl = sg;if (scsi_host_get_prot (shost)) { - cmd->prot_sdb = (void *)sg + - min_t(unsigned int, - shost->sg_tablesize,SG_CHUNK_SIZE ) * -sizeof (struct scatterlist ); + cmd->prot_sdb = (void *)sg +scsi_mq_sgl_size (shost);memset (cmd->prot_sdb,0 ,sizeof (struct scsi_data_buffer )); cmd->prot_sdb->table.sgl =
@@ -2201,12 +2205,9 @@ struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)int scsi_mq_setup_tags (struct Scsi_Host *shost) { -unsigned int cmd_size, sgl_size, tbl_size; +unsigned int cmd_size, sgl_size; - tbl_size = shost->sg_tablesize; -if (tbl_size >SG_CHUNK_SIZE ) - tbl_size =SG_CHUNK_SIZE ; - sgl_size = tbl_size *sizeof (struct scatterlist ); + sgl_size =scsi_mq_sgl_size (shost); cmd_size =sizeof (struct scsi_cmnd ) + shost->hostt->cmd_size + sgl_size;if (scsi_host_get_prot (shost)) cmd_size +=sizeof (struct scsi_data_buffer ) + sgl_size;
Signature · 1 line
2.12.2
PATCH v3 9/12 Make scsi_mq_prep_fn() call scsi_init_command()
In reply to Bart Van AsscheThis patch reduces code duplication. There are two functional changes in this patch:
- It causes scsi_mq_prep_fn() to clear driver-private command data, just like the already upstream commit 1bad6c4a57ef ("scsi: zero per-cmd private driver data for each MQ I/O").
- The initialization of .prot_sdb is moved from scsi_mq_prep_fn() into scsi_init_request().
--- drivers/scsi/scsi_lib.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/scsi/scsi_lib.c +9 -16
@@ -1870,36 +1870,21 @@ static int scsi_mq_prep_fn(struct request *req)struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (req);struct scsi_device *sdev = req->q->queuedata;struct Scsi_Host *shost = sdev->host; -unsigned char *sense_buf = cmd->sense_buffer; -unsigned int unchecked_isa_dma = cmd->flags &SCMD_UNCHECKED_ISA_DMA ;struct scatterlist *sg; -/* zero out the cmd, except for the embedded scsi_request */ -memset ((char *)cmd +sizeof (cmd->req),0 , -sizeof (*cmd) -sizeof (cmd->req)); +scsi_init_command (sdev, cmd); req->special = cmd; cmd->request = req; - cmd->device = sdev; - cmd->sense_buffer = sense_buf; - cmd->flags = unchecked_isa_dma; cmd->tag = req->tag; - cmd->prot_op =SCSI_PROT_NORMAL ; -INIT_LIST_HEAD (&cmd->list); -INIT_DELAYED_WORK (&cmd->abort_work, scmd_eh_abort_handler); - cmd->jiffies_at_alloc = jiffies; - -scsi_add_cmd_to_list (cmd); - sg = (void *)cmd +sizeof (struct scsi_cmnd ) + shost->hostt->cmd_size; cmd->sdb.table.sgl = sg;if (scsi_host_get_prot (shost)) { - cmd->prot_sdb = (void *)sg +scsi_mq_sgl_size (shost);memset (cmd->prot_sdb,0 ,sizeof (struct scsi_data_buffer )); cmd->prot_sdb->table.sgl =
@@ -2025,6 +2010,7 @@ static int scsi_init_request(struct blk_mq_tag_set *set, struct request *rq,struct Scsi_Host *shost = set->driver_data;const bool unchecked_isa_dma = shost->unchecked_isa_dma;struct scsi_cmnd *cmd =blk_mq_rq_to_pdu (rq); +struct scatterlist *sg;if (unchecked_isa_dma) cmd->flags |=SCMD_UNCHECKED_ISA_DMA ;
@@ -2033,6 +2019,13 @@ static int scsi_init_request(struct blk_mq_tag_set *set, struct request *rq,if (!cmd->sense_buffer)return -ENOMEM ; cmd->req.sense = cmd->sense_buffer; + +if (scsi_host_get_prot (shost)) { + sg = (void *)cmd +sizeof (struct scsi_cmnd ) + + shost->hostt->cmd_size; + cmd->prot_sdb = (void *)sg +scsi_mq_sgl_size (shost); + } +return 0 ; }
Signature · 1 line
2.12.2
PATCH v3 10/12 snic: Remove code that zeroes driver-private command data
In reply to Bart Van AsscheSince the SCSI core zeroes driver-private command data, remove that code from the snic driver.
--- drivers/scsi/snic/snic_scsi.c | 2 -- 1 file changed, 2 deletions(-)
drivers/scsi/snic/snic_scsi.c +0 -2
@@ -359,8 +359,6 @@ snic_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc)SNIC_SCSI_DBG (shost,"sc %p Tag %d (sc %0x) lun %lld in snic_qcmd\n" , sc,snic_cmd_tag (sc), sc->cmnd[0 ], sc->device->lun); -memset (scsi_cmd_priv (sc),0 ,sizeof (struct snic_internal_io_state )); - ret =snic_issue_scsi_req (snic, tgt, sc);if (ret) {SNIC_HOST_ERR (shost,"Failed to Q, Scsi Req w/ err %d.\n" , ret);
Signature · 1 line
2.12.2
PATCH v3 7/12 Only add commands to the device command list if required by the LLD
In reply to Bart Van AsscheJust like for the scsi-mq code path, in the single queue SCSI code path only add commands to the per-device command list if required by the SCSI LLD. This patch will make it easier to merge the single-queue and multiqueue command initialization code.
--- drivers/scsi/scsi.c | 9 +-------- drivers/scsi/scsi_lib.c | 52 +++++++++++++++++++++++++++++------------------- drivers/scsi/scsi_priv.h | 2 ++ 3 files changed, 35 insertions(+), 28 deletions(-)
drivers/scsi/scsi.c +1 -8
@@ -108,14 +108,7 @@ EXPORT_SYMBOL(scsi_sd_pm_domain);*/ void scsi_put_command (struct scsi_cmnd *cmd) { -unsigned long flags; - -/* serious error if the command hasn't come from a device list */ -spin_lock_irqsave (&cmd->device->list_lock, flags); -BUG_ON (list_empty (&cmd->list)); -list_del_init (&cmd->list); -spin_unlock_irqrestore (&cmd->device->list_lock, flags); - +scsi_del_cmd_from_list (cmd);BUG_ON (delayed_work_pending (&cmd->abort_work)); }
drivers/scsi/scsi_lib.c +32 -20
83 lines not on this page · raw message
drivers/scsi/scsi_priv.h +2 -0
@@ -80,6 +80,8 @@ int scsi_eh_get_sense(struct list_head *work_q,int scsi_noretry_cmd (struct scsi_cmnd *scmd);/* scsi_lib.c */ +extern void scsi_add_cmd_to_list (struct scsi_cmnd *cmd); +extern void scsi_del_cmd_from_list (struct scsi_cmnd *cmd);extern int scsi_maybe_unblock_host (struct scsi_device *sdev);extern void scsi_device_unbusy (struct scsi_device *sdev);extern void scsi_queue_insert (struct scsi_cmnd *cmd,int reason);
Signature · 1 line
2.12.2
PATCH v3 12/12 xen/scsifront: Remove code that zeroes driver-private command data
In reply to Bart Van AsscheSince the SCSI core zeroes driver-private command data, remove that code from the xen-scsifront driver.
--- drivers/scsi/xen-scsifront.c | 1 - 1 file changed, 1 deletion(-)
drivers/scsi/xen-scsifront.c +0 -1
@@ -534,7 +534,6 @@ static int scsifront_queuecommand(struct Scsi_Host *shost,int err; sc->result =0 ; -memset (shadow,0 ,sizeof (*shadow)); shadow->sc = sc; shadow->act =VSCSIIF_ACT_SCSI_CDB ;
Signature · 1 line
2.12.2
PATCH v3 12/12 xen/scsifront: Remove code that zeroes driver-private command data
In reply to Bart Van AsscheSince the SCSI core zeroes driver-private command data, remove that code from the xen-scsifront driver.
--- drivers/scsi/xen-scsifront.c | 1 - 1 file changed, 1 deletion(-)
drivers/scsi/xen-scsifront.c +0 -1
@@ -534,7 +534,6 @@ static int scsifront_queuecommand(struct Scsi_Host *shost,int err; sc->result =0 ; -memset (shadow,0 ,sizeof (*shadow)); shadow->sc = sc; shadow->act =VSCSIIF_ACT_SCSI_CDB ;
Signature · 7 lines
2.12.2 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v3 4/12 Protect SCSI device state changes with a mutex
In reply to Bart Van AsscheSerializing SCSI device state changes avoids that two state changes can occur concurrently, e.g. the state changes in scsi_target_block() and __scsi_remove_device(). This serialization is essential to make patch "Make __scsi_remove_device go straight from BLOCKED to DEL" work reliably.
Enable this mechanism for all scsi_target_*block() callers but not for the scsi_internal_device_unblock() calls from the mpt3sas driver because that driver can call scsi_internal_device_unblock() from atomic context.
--- drivers/scsi/scsi_error.c | 8 +++++++- drivers/scsi/scsi_lib.c | 27 +++++++++++++++++++++------ drivers/scsi/scsi_scan.c | 16 +++++++++------- drivers/scsi/scsi_sysfs.c | 24 +++++++++++++++++++----- drivers/scsi/scsi_transport_srp.c | 7 ++++--- drivers/scsi/sd.c | 7 +++++-- include/scsi/scsi_device.h | 1 + 7 files changed, 66 insertions(+), 24 deletions(-)
drivers/scsi/scsi_error.c +7 -1
@@ -1628,11 +1628,17 @@ static void scsi_eh_offline_sdevs(struct list_head *work_q, struct list_head *done_q) {struct scsi_cmnd *scmd, *next; +struct scsi_device *sdev;list_for_each_entry_safe (scmd, next, work_q, eh_entry) {sdev_printk (KERN_INFO , scmd->device,"Device offlined - " "not ready after error recovery\n" ); -scsi_device_set_state (scmd->device,SDEV_OFFLINE ); + sdev = scmd->device; + +mutex_lock (&sdev->state_mutex); +scsi_device_set_state (sdev,SDEV_OFFLINE ); +mutex_unlock (&sdev->state_mutex); +scsi_eh_finish_cmd (scmd, done_q); }return ;
drivers/scsi/scsi_lib.c +21 -6
@@ -2881,7 +2881,12 @@ static void scsi_wait_for_queuecommand(struct scsi_device *sdev)int scsi_device_quiesce (struct scsi_device *sdev) { -int err =scsi_device_set_state (sdev,SDEV_QUIESCE ); +int err; + +mutex_lock (&sdev->state_mutex); + err =scsi_device_set_state (sdev,SDEV_QUIESCE ); +mutex_unlock (&sdev->state_mutex); +if (err)return err;
@@ -2909,10 +2914,11 @@ void scsi_device_resume(struct scsi_device *sdev)* so assume the state is being managed elsewhere (for example * device deleted during suspend) */ -if (sdev->sdev_state !=SDEV_QUIESCE || -scsi_device_set_state (sdev,SDEV_RUNNING )) -return ; -scsi_run_queue (sdev->request_queue); +mutex_lock (&sdev->state_mutex); +if (sdev->sdev_state ==SDEV_QUIESCE && +scsi_device_set_state (sdev,SDEV_RUNNING ) ==0 ) +scsi_run_queue (sdev->request_queue); +mutex_unlock (&sdev->state_mutex); }EXPORT_SYMBOL (scsi_device_resume);
@@ -3011,6 +3017,7 @@ static int scsi_internal_device_block(struct scsi_device *sdev)struct request_queue *q = sdev->request_queue;int err; +mutex_lock (&sdev->state_mutex); err =scsi_internal_device_block_nowait (sdev);if (err ==0 ) {if (q->mq_ops)
@@ -3018,6 +3025,8 @@ static int scsi_internal_device_block(struct scsi_device *sdev)else scsi_wait_for_queuecommand (sdev); } +mutex_unlock (&sdev->state_mutex); +return err; }
@@ -3088,7 +3097,13 @@ EXPORT_SYMBOL_GPL(scsi_internal_device_unblock_nowait);static int scsi_internal_device_unblock (struct scsi_device *sdev,enum scsi_device_state new_state) { -return scsi_internal_device_unblock_nowait (sdev, new_state); +int ret; + +mutex_lock (&sdev->state_mutex); + ret =scsi_internal_device_unblock_nowait (sdev, new_state); +mutex_unlock (&sdev->state_mutex); + +return ret; } static void
drivers/scsi/scsi_scan.c +9 -7
@@ -231,6 +231,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, sdev->id = starget->id; sdev->lun = lun; sdev->channel = starget->channel; +mutex_init (&sdev->state_mutex); sdev->sdev_state =SDEV_CREATED ;INIT_LIST_HEAD (&sdev->siblings);INIT_LIST_HEAD (&sdev->same_target_siblings);
@@ -943,16 +944,17 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,/* set the device running here so that slave configure * may do I/O */ +mutex_lock (&sdev->state_mutex); ret =scsi_device_set_state (sdev,SDEV_RUNNING ); -if (ret) { +if (ret) ret =scsi_device_set_state (sdev,SDEV_BLOCK ); +mutex_unlock (&sdev->state_mutex); -if (ret) { -sdev_printk (KERN_ERR , sdev, -"in wrong state %s to complete scan\n" , -scsi_device_state_name (sdev->sdev_state)); -return SCSI_SCAN_NO_RESPONSE ; - } +if (ret) { +sdev_printk (KERN_ERR , sdev, +"in wrong state %s to complete scan\n" , +scsi_device_state_name (sdev->sdev_state)); +return SCSI_SCAN_NO_RESPONSE ; }if (*bflags &BLIST_MS_192_BYTES_FOR_3F )
drivers/scsi/scsi_sysfs.c +19 -5
@@ -719,7 +719,7 @@ static ssize_t store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - int i; + int i, ret;struct scsi_device *sdev =to_scsi_device (dev);enum scsi_device_state state =0 ;
@@ -734,9 +734,11 @@ store_state_field(struct device *dev, struct device_attribute *attr,if (!state)return -EINVAL ; -if (scsi_device_set_state (sdev, state)) -return -EINVAL ; -return count; +mutex_lock (&sdev->state_mutex); + ret =scsi_device_set_state (sdev, state); +mutex_unlock (&sdev->state_mutex); + +return ret ==0 ? count : -EINVAL ; } static ssize_t
@@ -1272,6 +1274,7 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)void __scsi_remove_device (struct scsi_device *sdev) {struct device *dev = &sdev->sdev_gendev; +int res;/* * This cleanup path is not reentrant and while it is impossible
@@ -1282,7 +1285,15 @@ void __scsi_remove_device(struct scsi_device *sdev)return ;if (sdev->is_visible) { -if (scsi_device_set_state (sdev,SDEV_CANCEL ) !=0 ) +/* +* If scsi_internal_target_block() is running concurrently, +* wait until it has finished before changing the device state. +*/ +mutex_lock (&sdev->state_mutex); + res =scsi_device_set_state (sdev,SDEV_CANCEL ); +mutex_unlock (&sdev->state_mutex); + +if (res !=0 )return ;bsg_unregister_queue (sdev->request_queue);
@@ -1298,7 +1309,10 @@ void __scsi_remove_device(struct scsi_device *sdev)* scsi_run_queue() invocations have finished before tearing down the * device. */ +mutex_lock (&sdev->state_mutex);scsi_device_set_state (sdev,SDEV_DEL ); +mutex_unlock (&sdev->state_mutex); +blk_cleanup_queue (sdev->request_queue);cancel_work_sync (&sdev->requeue_work);
drivers/scsi/scsi_transport_srp.c +4 -3
@@ -554,11 +554,12 @@ int srp_reconnect_rport(struct srp_rport *rport)* invoking scsi_target_unblock() won't change the state of * these devices into running so do that explicitly. */ -spin_lock_irq (shost->host_lock); -__shost_for_each_device (sdev, shost) +shost_for_each_device (sdev, shost) { +mutex_lock (&sdev->state_mutex);if (sdev->sdev_state ==SDEV_OFFLINE ) sdev->sdev_state =SDEV_RUNNING ; -spin_unlock_irq (shost->host_lock); +mutex_unlock (&sdev->state_mutex); + } }else if (rport->state ==SRP_RPORT_RUNNING ) {/* * srp_reconnect_rport() has been invoked with fast_io_fail
drivers/scsi/sd.c +5 -2
@@ -1798,8 +1798,9 @@ static void sd_eh_reset(struct scsi_cmnd *scmd)static int sd_eh_action (struct scsi_cmnd *scmd,int eh_disp) {struct scsi_disk *sdkp =scsi_disk (scmd->request->rq_disk); +struct scsi_device *sdev = scmd->device; -if (!scsi_device_online (scmd->device) || +if (!scsi_device_online (sdev) || !scsi_medium_access_command (scmd) ||host_byte (scmd->result) !=DID_TIME_OUT || eh_disp !=SUCCESS )
@@ -1825,7 +1826,9 @@ static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {scmd_printk (KERN_ERR , scmd,"Medium access timeout failure. Offlining disk!\n" ); -scsi_device_set_state (scmd->device,SDEV_OFFLINE ); +mutex_lock (&sdev->state_mutex); +scsi_device_set_state (sdev,SDEV_OFFLINE ); +mutex_unlock (&sdev->state_mutex);return SUCCESS ; }
include/scsi/scsi_device.h +1 -0
@@ -207,6 +207,7 @@ struct scsi_device {void *handler_data;unsigned char access_state; +struct mutex state_mutex;enum scsi_device_state sdev_state;unsigned long sdev_data[0 ]; } __attribute__((aligned(sizeof(unsigned long))));
Signature · 1 line
2.12.2
Re: [PATCH v3 04/12] Protect SCSI device state changes with a mutex
Quoted 10 lines · Bart Van Assche
Serializing SCSI device state changes avoids that two state changes can occur concurrently, e.g. the state changes in
scsi_target_block()and__scsi_remove_device(). This serialization is essential to make patch "Make__scsi_remove_devicego straight from BLOCKED to DEL" work reliably.Enable this mechanism for all scsi_target_*block() callers but not for the
scsi_internal_device_unblock()calls from the mpt3sas driver because that driver can callscsi_internal_device_unblock()from atomic context.
And not taking the lock in that path is safe because of what conditions?
Re: [PATCH v3 09/12] Make scsi_mq_prep_fn() call scsi_init_command()
In reply to Bart Van AsscheLooks good,
Re: [PATCH v3 04/12] Protect SCSI device state changes with a mutex
In reply to Christoph HellwigQuoted 7 lines · Christoph Hellwig
Quoted 6 lines · Bart Van Assche
Serializing SCSI device state changes avoids that two state changes can occur concurrently, e.g. the state changes in
scsi_target_block()and__scsi_remove_device(). This serialization is essential to make patch "Make__scsi_remove_devicego straight from BLOCKED to DEL" work reliably.
Quoted 6 lines
Quoted 4 lines
Enable this mechanism for all scsi_target_*block() callers but not for the
scsi_internal_device_unblock()calls from the mpt3sas driver because that driver can callscsi_internal_device_unblock()from atomic context.And not taking the lock in that path is safe because of what conditions?
Hello Christoph,
The mpt3sas driver is the only driver that calls scsi_internal_device_block() and scsi_internal_device_unblock() from atomic context. Since it's not an option to protect the SCSI device state changes with a spinlock I prefer that the mpt3sas authors convert the scsi_internal_device_block() calls into scsi_target_block() calls.
Bart.
Re: [PATCH v3 04/12] Protect SCSI device state changes with a mutex
Quoted 13 lines · Bart Van Assche
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Bart Van Assche
Serializing SCSI device state changes avoids that two state changes can occur concurrently, e.g. the state changes in
scsi_target_block()and__scsi_remove_device(). This serialization is essential to make patch "Make__scsi_remove_devicego straight from BLOCKED to DEL" work reliably.Enable this mechanism for all scsi_target_*block() callers but not for the
scsi_internal_device_unblock()calls from the mpt3sas driver because that driver can callscsi_internal_device_unblock()from atomic context.
Quoted 7 lines
Quoted 1 line
And not taking the lock in that path is safe because of what conditions?
The mpt3sas driver is the only driver that calls
scsi_internal_device_block()andscsi_internal_device_unblock()from atomic context. Since it's not an option to protect the SCSI device state changes with a spinlock I prefer that the mpt3sas authors convert thescsi_internal_device_block()calls intoscsi_target_block()calls.
Please also note that although this patch series doesn't improve the mpt3sas driver, it doesn't change its behavior.
Bart.
Re: [PATCH v3 04/12] Protect SCSI device state changes with a mutex
Quoted 8 lines · Bart Van Assche
Quoted 5 lines
The mpt3sas driver is the only driver that calls
scsi_internal_device_block()andscsi_internal_device_unblock()from atomic context. Since it's not an option to protect the SCSI device state changes with a spinlock I prefer that the mpt3sas authors convert thescsi_internal_device_block()calls intoscsi_target_block()calls.Please also note that although this patch series doesn't improve the mpt3sas driver, it doesn't change its behavior.
Yes. And normally we try to get things right. I guess this series is useful enough without fixing everyone up, but adding a FIXME or getting the broadcom folks involved would be even better.
Martin K. Petersen
AppliedBart,
Quoted 4 lines
This patch series consists of the bug fixes and improvements I came up with during the past two months. This patch series has been developed on top of your 4.13/scsi-queue branch. Please consider these patches for kernel v4.13.
Applied to 4.13/scsi-queue, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering