scsi: scsi_request_fn() reimplementation
Re: [PATCH scsi-misc-2.6 01/04] scsi: consolidate error handling out of scsi_init_io() into scsi_prep_fn()
In reply to Tejun Heo01_scsi_reqfn_consolidate_error_handling.patch
This patch fixes a queue stall bug which occurred when sgtable allocation failed and device_busy == 0. When scsi_init_io() returns BLKPREP_DEFER or BLKPREP_KILL, it's supposed to free resources itself. This patch consolidates defer and kill handling into scsi_prep_fn().
Note that this patch doesn't consolidate state defer/kill handlings in scsi_prep_fn(). They were omitted as all state checks will be moved into scsi_reques_fn() by the following reqfn_reimpl patch.
ret value checking was changed to switch() as in James's patch. Also, kill: comment is copied from James's patch.
scsi_lib.c | 46 +++++++++++++++++++++++++++++++--------------- 1 files changed, 31 insertions(+), 15 deletions(-)
drivers/scsi/scsi_lib.c +31 -15
@@ -945,10 +945,8 @@ static int scsi_init_io(struct scsi_cmnd* if sg table allocation fails, requeue request later. */ sgpnt =scsi_alloc_sgtable (cmd,GFP_ATOMIC ); -if (unlikely (!sgpnt)) { - req->flags |=REQ_SOFTBARRIER ; +if (unlikely (!sgpnt))return BLKPREP_DEFER ; - } cmd->request_buffer = (char *) sgpnt; cmd->request_bufflen = req->nr_sectors <<9 ;
@@ -975,9 +973,6 @@ static int scsi_init_io(struct scsi_cmndprintk (KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n" , req->nr_sectors, req->current_nr_sectors); -/* release the command and kill it */ -scsi_release_buffers (cmd); -scsi_put_command (cmd);return BLKPREP_KILL ; }
@@ -1145,18 +1140,24 @@ static int scsi_prep_fn(struct request_q* required). */ ret =scsi_init_io (cmd); -if (ret)/* BLKPREP_KILL return also releases the command */ -return ret; +switch (ret) { +case 0 : +/* Successful initialization. */ +break ; +case BLKPREP_DEFER : + goto defer; +default : +/* Unknown return value, fall through. */ +case BLKPREP_KILL : + goto kill; + }/* * Initialize the actual SCSI command for this request. */ drv = *(struct scsi_driver **)req->rq_disk->private_data; -if (unlikely (!drv->init_command (cmd))) { -scsi_release_buffers (cmd); -scsi_put_command (cmd); -return BLKPREP_KILL ; - } +if (unlikely (!drv->init_command (cmd))) + goto kill; }/*
@@ -1166,12 +1167,27 @@ static int scsi_prep_fn(struct request_qreturn BLKPREP_OK ; defer: -/* If we defer, the elv_next_request() returns NULL, but the +/* +* If we defer, the elv_next_request() returns NULL, but the * queue must be restarted, so we plug here if no returning -* command will automatically do that. */ +* command will automatically do that. Also, the request may +* have its cmd allocated, so we set REQ_SOFTBARRIER. +*/ if (sdev->device_busy ==0 )blk_plug_device (q); + req->flags |=REQ_SOFTBARRIER ;return BLKPREP_DEFER ; + + kill: +/* +* Here we have to release every resource associated with the +* request because this will complete at the request level +* (req->end_io), not the scsi command level, so no scsi +* routine will get to free the associated resources. +*/ +scsi_release_buffers (cmd); +scsi_put_command (cmd); +return BLKPREP_KILL ; }/*
cover scsi: scsi_request_fn() reimplementation
Hello, guys.
This patchset reimplements scsi_request_fn(). All prep's are moved
into prep_fn and all state checking/issueing are moved into scsi_reqfn. prep_fn() only terminates/defers unpreparable requests and all requests are terminated through scsi midlayer.
[ Start of patch descriptions ]
01_scsi_reqfn_consolidate_error_handling.patch
: consolidate error handling out of scsi_init_io() into scsi_prep_fn()
This patch fixes a queue stall bug which occurred when sgtable allocation failed and device_busy == 0. When scsi_init_io() returns BLKPREP_DEFER or BLKPREP_KILL, it's supposed to free resources itself. This patch consolidates defer and kill handling into scsi_prep_fn().
Note that this patch doesn't consolidate state defer/kill handlings in scsi_prep_fn(). They were omitted as all state checks will be moved into scsi_reques_fn() by the following reqfn_reimpl patch.
ret value checking was changed to switch() as in James's patch. Also, kill: comment is copied from James's patch.
02_scsi_reqfn_move_preps_to_prep_fn.patch
: move request preps in other places into prep_fn()
Move request preparations scattered in scsi_request_fn() and scsi_dispatch_cmd() into scsi_prep_fn()
* CDB_SIZE check in scsi_dispatch_cmd()
* SCSI-2 LUN preparation in scsi_dispatch_cmd()
No invalid request reaches scsi_request_fn() anymore.
Note that scsi_init_cmd_errh() is still left in scsi_request_fn(). As all requeued requests need its sense buffer and result value cleared, we can't move this to prep_fn() yet. This is moved into prep_fn in the following requeue path consoildation patchset.
03_scsi_reqfn_reimplementation.patch
: reimplement scsi_request_fn()
This patch rewrites scsi_request_fn(). scsi_dispatch_cmd() is
merged into scsi_request_fn(). Goals are
* Remove unnecessary operations (host_lock unlocking/locking, recursing into scsi_run_queue(), ...)
* Consolidate defer/kill paths.
* Be concise.
The following bugs are fixed.
* All killed requests now get fully prep'ed and pass through __scsi_done(). This is the only kill path.
- scsi_cmnd leak in offline-kill path removed
- unfinished request bug in scsi_dispatch_cmd():SDEV_DEL-kill path removed.
- commands are never terminated directly from blk layer unless they are invalid, so no need to supply req->end_io callback for special requests.
* Timer is added while holding host_lock, after all conditions are checked and serial number is assigned. This guarantees that until host_lock is released, the scsi_cmnd pointed to by cmd isn't released. That didn't hold in the original code and, theoretically, the original code could access already released cmd.
* For the same reason, if shost->hostt->queuecommand() fails, we try to delete the timer before releasing host_lock.
Other changes/notes
* host_lock is acquired and released only once.
enter (qlocked) -> enter loop -> dev-prep -> switch to hlock -\
^---- switch to qlock <- issue <- host-prep <-/
* unnecessary if () on get_device() removed.
* loop on elv_next_request() instead of blk_queue_plugged(). We now explicitly break out of loop when we plug and check if the queue has been plugged underneath us at the end of loop.
* All device/host state checks are done in this function and done only once while holding qlock/host_lock respectively.
* Requests which get deferred during dev-prep are never removed from request queue, so deferring is achieved by simply breaking out of the loop and returning.
* Failure of blk_queue_start_tag() on tagged queue is a BUG
now. This condition should have been catched by
scsi_dev_queue_ready().
* req->special == NULL test removed. This just can't happen, and even if it ever happens, scsi_request_fn() will deterministically oops.
* Requests which gets deferred during host-prep are requeued
using blk_requeue_request(). This is the only requeue path.
Note that scsi_kill_requests() still terminates requests using blk layer. The path is circular-ref workaround and soon to be replaced, so ignore it for now.
04_scsi_reqfn_remove_wait_req_end_io.patch
: remove unnecessary scsi_wait_req_end_io()
As all requests are now terminated via scsi midlayer, we don't need to set end_io for special reqs, remove it.
Note that scsi_kill_requests() still terminates requests using blk layer. The path is circular-ref workaround and soon to be replaced, so ignore it for now.
[ End of patch descriptions ]
Thanks a lot.
Re: [PATCH scsi-misc-2.6 02/04] scsi: move request preps in other places into prep_fn()
02_scsi_reqfn_move_preps_to_prep_fn.patch
Move request preparations scattered in scsi_request_fn() and scsi_dispatch_cmd() into scsi_prep_fn()
* CDB_SIZE check in scsi_dispatch_cmd()
* SCSI-2 LUN preparation in scsi_dispatch_cmd()
No invalid request reaches scsi_request_fn() anymore.
Note that scsi_init_cmd_errh() is still left in scsi_request_fn(). As all requeued requests need its sense buffer and result value cleared, we can't move this to prep_fn() yet. This is moved into prep_fn in the following requeue path consoildation patchset.
scsi.c | 30 ------------------------------ scsi_lib.c | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 30 deletions(-)
drivers/scsi/scsi.c +0 -30
@@ -79,15 +79,6 @@#define MIN_RESET_PERIOD (15*HZ)/* -* Macro to determine the size of SCSI command. This macro takes vendor -* unique commands into account. SCSI commands in groups 6 and 7 are -* vendor unique and we will depend upon the command length being -* supplied correctly in cmd_len. -*/ -#define CDB_SIZE (cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \ - COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len) - -/* * Note - the initial logging level can be set here to log events at boot time. * After the system is up, you may enable logging via the /proc interface. */
@@ -566,14 +557,6 @@ int scsi_dispatch_cmd(struct scsi_cmnd * goto out; } -/* -* If SCSI-2 or lower, store the LUN value in cmnd. -*/ -if (cmd->device->scsi_level <=SCSI_2 ) { - cmd->cmnd[1 ] = (cmd->cmnd[1 ] &0x1f ) | - (cmd->device->lun <<5 &0xe0 ); - } -/* * We will wait MIN_RESET_DELAY clock ticks after the last reset so * we can avoid the drive not being ready.
@@ -614,19 +597,6 @@ int scsi_dispatch_cmd(struct scsi_cmnd *atomic_inc (&cmd->device->iorequest_cnt); -/* -* Before we queue this command, check if the command -* length exceeds what the host adapter can handle. -*/ -if (CDB_SIZE (cmd) > cmd->device->host->max_cmd_len) { -SCSI_LOG_MLQUEUE (3 , -printk ("queuecommand : command too long.\n" )); - cmd->result = (DID_ABORT <<16 ); - -scsi_done (cmd); - goto out; - } -spin_lock_irqsave (host->host_lock, flags);scsi_cmd_get_serial (host, cmd);
drivers/scsi/scsi_lib.c +17 -0
@@ -28,6 +28,14 @@#include "scsi_priv.h" #include "scsi_logging.h" +/* +* Macro to determine the size of SCSI command. This macro takes vendor +* unique commands into account. SCSI commands in groups 6 and 7 are +* vendor unique and we will depend upon the command length being +* supplied correctly in cmd_len. +*/ +#define CDB_SIZE (cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \ + COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len)#define SG_MEMPOOL_NR (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))#define SG_MEMPOOL_SIZE 32
@@ -1160,6 +1168,15 @@ static int scsi_prep_fn(struct request_q goto kill; } +/* Check command length. */ +if (CDB_SIZE (cmd) > sdev->host->max_cmd_len) + goto kill; + +/* If SCSI-2 or lower, store the LUN value in cmnd. */ +if (cmd->device->scsi_level <=SCSI_2 ) + cmd->cmnd[1 ] = (cmd->cmnd[1 ] &0x1f ) | + (cmd->device->lun <<5 &0xe0 ); +/* * The request is now prepped, no need to come back here */
Re: [PATCH scsi-misc-2.6 04/04] scsi: remove unnecessary scsi_wait_req_end_io()
In reply to Tejun Heo04_scsi_reqfn_remove_wait_req_end_io.patch
As all requests are now terminated via scsi midlayer, we don't need to set end_io for special reqs, remove it.
Note that scsi_kill_requests() still terminates requests using blk layer. The path is circular-ref workaround and soon to be replaced, so ignore it for now.
scsi_lib.c | 11 ----------- 1 files changed, 11 deletions(-)
drivers/scsi/scsi_lib.c +0 -11
@@ -260,16 +260,6 @@ static void scsi_wait_done(struct scsi_ccomplete (req->waiting); } -/* This is the end routine we get to if a command was never attached -* to the request. Simply complete the request without changing -* rq_status; this will cause a DRIVER_ERROR. */ -static void scsi_wait_req_end_io (struct request *req) -{ -BUG_ON (!req->waiting); - -complete (req->waiting); -} -void scsi_wait_req (struct scsi_request *sreq,const void *cmnd,void *buffer,unsigned bufflen,int timeout,int retries) {
@@ -277,7 +267,6 @@ void scsi_wait_req(struct scsi_request * sreq->sr_request->waiting = &wait; sreq->sr_request->rq_status =RQ_SCSI_BUSY ; - sreq->sr_request->end_io = scsi_wait_req_end_io;scsi_do_req (sreq, cmnd, buffer, bufflen, scsi_wait_done, timeout, retries);wait_for_completion (&wait);
Re: [PATCH scsi-misc-2.6 03/04] scsi: reimplement scsi_request_fn()
In reply to Tejun Heo03_scsi_reqfn_reimplementation.patch
This patch rewrites scsi_request_fn(). scsi_dispatch_cmd() is
merged into scsi_request_fn(). Goals are
* Remove unnecessary operations (host_lock unlocking/locking, recursing into scsi_run_queue(), ...)
* Consolidate defer/kill paths.
* Be concise.
The following bugs are fixed.
* All killed requests now get fully prep'ed and pass through __scsi_done(). This is the only kill path.
- scsi_cmnd leak in offline-kill path removed
- unfinished request bug in scsi_dispatch_cmd():SDEV_DEL-kill path removed.
- commands are never terminated directly from blk layer unless they are invalid, so no need to supply req->end_io callback for special requests.
* Timer is added while holding host_lock, after all conditions are checked and serial number is assigned. This guarantees that until host_lock is released, the scsi_cmnd pointed to by cmd isn't released. That didn't hold in the original code and, theoretically, the original code could access already released cmd.
* For the same reason, if shost->hostt->queuecommand() fails, we try to delete the timer before releasing host_lock.
Other changes/notes
* host_lock is acquired and released only once.
enter (qlocked) -> enter loop -> dev-prep -> switch to hlock -\
^---- switch to qlock <- issue <- host-prep <-/
* unnecessary if () on get_device() removed.
* loop on elv_next_request() instead of blk_queue_plugged(). We now explicitly break out of loop when we plug and check if the queue has been plugged underneath us at the end of loop.
* All device/host state checks are done in this function and done only once while holding qlock/host_lock respectively.
* Requests which get deferred during dev-prep are never removed from request queue, so deferring is achieved by simply breaking out of the loop and returning.
* Failure of blk_queue_start_tag() on tagged queue is a BUG
now. This condition should have been catched by
scsi_dev_queue_ready().
* req->special == NULL test removed. This just can't happen, and even if it ever happens, scsi_request_fn() will deterministically oops.
* Requests which gets deferred during host-prep are requeued
using blk_requeue_request(). This is the only requeue path.
Note that scsi_kill_requests() still terminates requests using blk layer. The path is circular-ref workaround and soon to be replaced, so ignore it for now.
scsi.c | 137 ---------------------------- scsi_lib.c | 286 +++++++++++++++++++++++++++++++++--------------------------- scsi_priv.h | 1 3 files changed, 159 insertions(+), 265 deletions(-)
drivers/scsi/scsi.c +0 -137
@@ -70,15 +70,6 @@/* -* Definitions and constants. -*/ - -#define MIN_RESET_DELAY (2*HZ) - -/* Do not call reset on error if we just did a reset within 15 sec. */ -#define MIN_RESET_PERIOD (15*HZ) - -/* * Note - the initial logging level can be set here to log events at boot time. * After the system is up, you may enable logging via the /proc interface. */
@@ -495,134 +486,6 @@ void scsi_log_completion(struct scsi_cmn }#endif -/* -* Assign a serial number and pid to the request for error recovery -* and debugging purposes. Protected by the Host_Lock of host. -*/ -static inline void scsi_cmd_get_serial (struct Scsi_Host *host,struct scsi_cmnd *cmd) -{ - cmd->serial_number = host->cmd_serial_number++; -if (cmd->serial_number ==0 ) - cmd->serial_number = host->cmd_serial_number++; - - cmd->pid = host->cmd_pid++; -if (cmd->pid ==0 ) - cmd->pid = host->cmd_pid++; -} - -/* -* Function: scsi_dispatch_command -* -* Purpose: Dispatch a command to the low-level driver. -* -* Arguments: cmd - command block we are dispatching. -* -* Notes: -*/ -int scsi_dispatch_cmd (struct scsi_cmnd *cmd) -{ -struct Scsi_Host *host = cmd->device->host; -unsigned long flags =0 ; -unsigned long timeout; -int rtn =0 ; - -/* check if the device is still usable */ -if (unlikely (cmd->device->sdev_state ==SDEV_DEL )) { -/* in SDEV_DEL we error all commands. DID_NO_CONNECT -* returns an immediate error upwards, and signals -* that the device is no longer present */ - cmd->result =DID_NO_CONNECT <<16 ; -atomic_inc (&cmd->device->iorequest_cnt); -scsi_done (cmd); -/* return 0 (because the command has been processed) */ - goto out; - } - -/* Check to see if the scsi lld put this device into state SDEV_BLOCK. */ -if (unlikely (cmd->device->sdev_state ==SDEV_BLOCK )) { -/* -* in SDEV_BLOCK, the command is just put back on the device -* queue. The suspend state has already blocked the queue so -* future requests should not occur until the device -* transitions out of the suspend state. -*/ -scsi_queue_insert (cmd,SCSI_MLQUEUE_DEVICE_BUSY ); - -SCSI_LOG_MLQUEUE (3 ,printk ("queuecommand : device blocked \n" )); - -/* -* NOTE: rtn is still zero here because we don't need the -* queue to be plugged on return (it's already stopped) -*/ - goto out; - } - -/* -* We will wait MIN_RESET_DELAY clock ticks after the last reset so -* we can avoid the drive not being ready. -*/ - timeout = host->last_reset +MIN_RESET_DELAY ; - -if (host->resetting &&time_before (jiffies, timeout)) { -int ticks_remaining = timeout - jiffies; -/* -* NOTE: This may be executed from within an interrupt -* handler! This is bad, but for now, it'll do. The irq -* level of the interrupt handler has been masked out by the -* platform dependent interrupt handling code already, so the -* sti() here will not cause another call to the SCSI host's -* interrupt handler (assuming there is one irq-level per -* host). -*/ -while (--ticks_remaining >=0 ) -mdelay (1 +999 /HZ ); - host->resetting =0 ; - } - -/* -* AK: unlikely race here: for some reason the timer could -* expire before the serial number is set up below. -*/ -scsi_add_timer (cmd, cmd->timeout_per_command); - -scsi_log_send (cmd); - -/* -* We will use a queued command if possible, otherwise we will -* emulate the queuing and calling of completion function ourselves. -*/ - - cmd->state =SCSI_STATE_QUEUED ; - cmd->owner =SCSI_OWNER_LOWLEVEL ; - -atomic_inc (&cmd->device->iorequest_cnt); - -spin_lock_irqsave (host->host_lock, flags); -scsi_cmd_get_serial (host, cmd); - -if (unlikely (test_bit (SHOST_CANCEL , &host->shost_state))) { - cmd->result = (DID_NO_CONNECT <<16 ); -scsi_done (cmd); - }else { - rtn = host->hostt->queuecommand (cmd, scsi_done); - } -spin_unlock_irqrestore (host->host_lock, flags); -if (rtn) { -if (scsi_delete_timer (cmd)) { -atomic_inc (&cmd->device->iodone_cnt); -scsi_queue_insert (cmd, - (rtn ==SCSI_MLQUEUE_DEVICE_BUSY ) ? - rtn :SCSI_MLQUEUE_HOST_BUSY ); - } -SCSI_LOG_MLQUEUE (3 , -printk ("queuecommand : request rejected\n" )); - } - - out: -SCSI_LOG_MLQUEUE (3 ,printk ("leaving scsi_dispatch_cmnd()\n" )); -return rtn; -} -/* * Function: scsi_init_cmd_from_req *
drivers/scsi/scsi_lib.c +159 -127
358 lines not on this page · raw message
drivers/scsi/scsi_priv.h +0 -1
@@ -57,7 +57,6 @@ extern int scsi_init_hosts(void);extern void scsi_exit_hosts (void );/* scsi.c */ -extern int scsi_dispatch_cmd (struct scsi_cmnd *cmd);extern int scsi_setup_command_freelist (struct Scsi_Host *shost);extern void scsi_destroy_command_freelist (struct Scsi_Host *shost);extern void scsi_done (struct scsi_cmnd *cmd);
Re: [PATCH scsi-misc-2.6 03/04] scsi: reimplement scsi_request_fn()
Oops, I forgot to mention that reqfn is reformatted mostly as
suggested by Chritoph Hellwig. Sorry.
Quoted 55 lines · Tejun Heo
03_scsi_reqfn_reimplementation.patch
This patch rewrites
scsi_request_fn().scsi_dispatch_cmd()is
merged intoscsi_request_fn(). Goals are* Remove unnecessary operations (
host_lockunlocking/locking, recursing intoscsi_run_queue(), ...)
* Consolidate defer/kill paths.
* Be concise.The following bugs are fixed.
* All killed requests now get fully prep'ed and pass through
__scsi_done(). This is the only kill path.
-scsi_cmndleak in offline-kill path removed
- unfinished request bug in scsi_dispatch_cmd():SDEV_DEL-kill path removed.
- commands are never terminated directly from blk layer unless they are invalid, so no need to supplyreq->end_iocallback for special requests.
* Timer is added while holdinghost_lock, after all conditions are checked and serial number is assigned. This guarantees that untilhost_lockis released, thescsi_cmndpointed to by cmd isn't released. That didn't hold in the original code and, theoretically, the original code could access already released cmd.
* For the same reason, ifshost->hostt->queuecommand()fails, we try to delete the timer before releasinghost_lock.Other changes/notes
*
host_lockis acquired and released only once.
enter (qlocked) -> enter loop -> dev-prep -> switch to hlock -\
^---- switch to qlock <- issue <- host-prep <-/
* unnecessary if () onget_device()removed.
* loop onelv_next_request()instead ofblk_queue_plugged(). We now explicitly break out of loop when we plug and check if the queue has been plugged underneath us at the end of loop.
* All device/host state checks are done in this function and done only once while holding qlock/host_lock respectively.
* Requests which get deferred during dev-prep are never removed from request queue, so deferring is achieved by simply breaking out of the loop and returning.
* Failure ofblk_queue_start_tag()on tagged queue is a BUG
now. This condition should have been catched by
scsi_dev_queue_ready().
*req->special== NULL test removed. This just can't happen, and even if it ever happens,scsi_request_fn()will deterministically oops.
* Requests which gets deferred during host-prep are requeued
usingblk_requeue_request(). This is the only requeue path.Note that
scsi_kill_requests()still terminates requests using blk layer. The path is circular-ref workaround and soon to be replaced, so ignore it for now.
Signature · 1 line
tejun
Quoted 7 lines · Tejun Heo
Hello, guys.
This patchset reimplements
scsi_request_fn(). All prep's are moved
intoprep_fnand all state checking/issueing are moved intoscsi_reqfn.prep_fn()only terminates/defers unpreparable requests and all requests are terminated through scsi midlayer.
This patchset assumes that previous patchsets 'timer updates' and
'clear REQ_SPECIAL/REQ_SOFTBARRIER usages' are applied.
Thanks.
Signature · 1 line
tejun