Commit 04796336 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by James Bottomley

[SCSI] do not manipulate device reference counts in scsi_get/put_command

Many callers won't need this and we can optimize them away.  In addition
the handling in the __-prefixed variants was inconsistant to start with.

Based on an earlier patch from Bart Van Assche.

[jejb: fix kerneldoc probelm picked up by Fengguang Wu]
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
Signed-off-by: default avatarJames Bottomley <JBottomley@Parallels.com>
parent 21a05df5
...@@ -284,27 +284,19 @@ EXPORT_SYMBOL_GPL(__scsi_get_command); ...@@ -284,27 +284,19 @@ EXPORT_SYMBOL_GPL(__scsi_get_command);
*/ */
struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask) struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
{ {
struct scsi_cmnd *cmd; struct scsi_cmnd *cmd = __scsi_get_command(dev->host, gfp_mask);
unsigned long flags;
/* Bail if we can't get a reference to the device */ if (unlikely(cmd == NULL))
if (!get_device(&dev->sdev_gendev))
return NULL; return NULL;
cmd = __scsi_get_command(dev->host, gfp_mask); cmd->device = dev;
INIT_LIST_HEAD(&cmd->list);
if (likely(cmd != NULL)) { INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
unsigned long flags; spin_lock_irqsave(&dev->list_lock, flags);
list_add_tail(&cmd->list, &dev->cmd_list);
cmd->device = dev; spin_unlock_irqrestore(&dev->list_lock, flags);
INIT_LIST_HEAD(&cmd->list); cmd->jiffies_at_alloc = jiffies;
INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
spin_lock_irqsave(&dev->list_lock, flags);
list_add_tail(&cmd->list, &dev->cmd_list);
spin_unlock_irqrestore(&dev->list_lock, flags);
cmd->jiffies_at_alloc = jiffies;
} else
put_device(&dev->sdev_gendev);
return cmd; return cmd;
} }
EXPORT_SYMBOL(scsi_get_command); EXPORT_SYMBOL(scsi_get_command);
...@@ -313,10 +305,8 @@ EXPORT_SYMBOL(scsi_get_command); ...@@ -313,10 +305,8 @@ EXPORT_SYMBOL(scsi_get_command);
* __scsi_put_command - Free a struct scsi_cmnd * __scsi_put_command - Free a struct scsi_cmnd
* @shost: dev->host * @shost: dev->host
* @cmd: Command to free * @cmd: Command to free
* @dev: parent scsi device
*/ */
void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd, void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
struct device *dev)
{ {
unsigned long flags; unsigned long flags;
...@@ -331,8 +321,6 @@ void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd, ...@@ -331,8 +321,6 @@ void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd,
if (likely(cmd != NULL)) if (likely(cmd != NULL))
scsi_pool_free_command(shost->cmd_pool, cmd); scsi_pool_free_command(shost->cmd_pool, cmd);
put_device(dev);
} }
EXPORT_SYMBOL(__scsi_put_command); EXPORT_SYMBOL(__scsi_put_command);
...@@ -346,7 +334,6 @@ EXPORT_SYMBOL(__scsi_put_command); ...@@ -346,7 +334,6 @@ EXPORT_SYMBOL(__scsi_put_command);
*/ */
void scsi_put_command(struct scsi_cmnd *cmd) void scsi_put_command(struct scsi_cmnd *cmd)
{ {
struct scsi_device *sdev = cmd->device;
unsigned long flags; unsigned long flags;
/* serious error if the command hasn't come from a device list */ /* serious error if the command hasn't come from a device list */
...@@ -357,7 +344,7 @@ void scsi_put_command(struct scsi_cmnd *cmd) ...@@ -357,7 +344,7 @@ void scsi_put_command(struct scsi_cmnd *cmd)
cancel_delayed_work(&cmd->abort_work); cancel_delayed_work(&cmd->abort_work);
__scsi_put_command(cmd->device->host, cmd, &sdev->sdev_gendev); __scsi_put_command(cmd->device->host, cmd);
} }
EXPORT_SYMBOL(scsi_put_command); EXPORT_SYMBOL(scsi_put_command);
......
...@@ -2288,6 +2288,11 @@ scsi_reset_provider(struct scsi_device *dev, int flag) ...@@ -2288,6 +2288,11 @@ scsi_reset_provider(struct scsi_device *dev, int flag)
if (scsi_autopm_get_host(shost) < 0) if (scsi_autopm_get_host(shost) < 0)
return FAILED; return FAILED;
if (!get_device(&dev->sdev_gendev)) {
rtn = FAILED;
goto out_put_autopm_host;
}
scmd = scsi_get_command(dev, GFP_KERNEL); scmd = scsi_get_command(dev, GFP_KERNEL);
blk_rq_init(NULL, &req); blk_rq_init(NULL, &req);
scmd->request = &req; scmd->request = &req;
...@@ -2345,6 +2350,7 @@ scsi_reset_provider(struct scsi_device *dev, int flag) ...@@ -2345,6 +2350,7 @@ scsi_reset_provider(struct scsi_device *dev, int flag)
scsi_run_host_queues(shost); scsi_run_host_queues(shost);
scsi_next_command(scmd); scsi_next_command(scmd);
out_put_autopm_host:
scsi_autopm_put_host(shost); scsi_autopm_put_host(shost);
return rtn; return rtn;
} }
......
...@@ -95,6 +95,7 @@ static void scsi_unprep_request(struct request *req) ...@@ -95,6 +95,7 @@ static void scsi_unprep_request(struct request *req)
req->special = NULL; req->special = NULL;
scsi_put_command(cmd); scsi_put_command(cmd);
put_device(&cmd->device->sdev_gendev);
} }
/** /**
...@@ -529,6 +530,7 @@ void scsi_next_command(struct scsi_cmnd *cmd) ...@@ -529,6 +530,7 @@ void scsi_next_command(struct scsi_cmnd *cmd)
get_device(&sdev->sdev_gendev); get_device(&sdev->sdev_gendev);
scsi_put_command(cmd); scsi_put_command(cmd);
put_device(&sdev->sdev_gendev);
scsi_run_queue(q); scsi_run_queue(q);
/* ok to remove device now */ /* ok to remove device now */
...@@ -1123,6 +1125,7 @@ int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask) ...@@ -1123,6 +1125,7 @@ int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
scsi_release_buffers(cmd); scsi_release_buffers(cmd);
cmd->request->special = NULL; cmd->request->special = NULL;
scsi_put_command(cmd); scsi_put_command(cmd);
put_device(&cmd->device->sdev_gendev);
return error; return error;
} }
EXPORT_SYMBOL(scsi_init_io); EXPORT_SYMBOL(scsi_init_io);
...@@ -1133,9 +1136,15 @@ static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev, ...@@ -1133,9 +1136,15 @@ static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
struct scsi_cmnd *cmd; struct scsi_cmnd *cmd;
if (!req->special) { if (!req->special) {
/* Bail if we can't get a reference to the device */
if (!get_device(&sdev->sdev_gendev))
return NULL;
cmd = scsi_get_command(sdev, GFP_ATOMIC); cmd = scsi_get_command(sdev, GFP_ATOMIC);
if (unlikely(!cmd)) if (unlikely(!cmd)) {
put_device(&sdev->sdev_gendev);
return NULL; return NULL;
}
req->special = cmd; req->special = cmd;
} else { } else {
cmd = req->special; cmd = req->special;
...@@ -1298,6 +1307,7 @@ int scsi_prep_return(struct request_queue *q, struct request *req, int ret) ...@@ -1298,6 +1307,7 @@ int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
struct scsi_cmnd *cmd = req->special; struct scsi_cmnd *cmd = req->special;
scsi_release_buffers(cmd); scsi_release_buffers(cmd);
scsi_put_command(cmd); scsi_put_command(cmd);
put_device(&cmd->device->sdev_gendev);
req->special = NULL; req->special = NULL;
} }
break; break;
......
...@@ -155,7 +155,8 @@ void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd) ...@@ -155,7 +155,8 @@ void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
__blk_put_request(q, rq); __blk_put_request(q, rq);
spin_unlock_irqrestore(q->queue_lock, flags); spin_unlock_irqrestore(q->queue_lock, flags);
__scsi_put_command(shost, cmd, &shost->shost_gendev); __scsi_put_command(shost, cmd);
put_device(&shost->shost_gendev);
} }
EXPORT_SYMBOL_GPL(scsi_host_put_command); EXPORT_SYMBOL_GPL(scsi_host_put_command);
......
...@@ -142,8 +142,7 @@ static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd) ...@@ -142,8 +142,7 @@ static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
extern struct scsi_cmnd *scsi_get_command(struct scsi_device *, gfp_t); extern struct scsi_cmnd *scsi_get_command(struct scsi_device *, gfp_t);
extern struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *, gfp_t); extern struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *, gfp_t);
extern void scsi_put_command(struct scsi_cmnd *); extern void scsi_put_command(struct scsi_cmnd *);
extern void __scsi_put_command(struct Scsi_Host *, struct scsi_cmnd *, extern void __scsi_put_command(struct Scsi_Host *, struct scsi_cmnd *);
struct device *);
extern void scsi_finish_command(struct scsi_cmnd *cmd); extern void scsi_finish_command(struct scsi_cmnd *cmd);
extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count, extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment