Commit 89d9a567 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by James Bottomley

[SCSI] add support for per-host cmd pools

This allows drivers to specify the size of their per-command private
data in the host template and then get extra memory allocated for
each command instead of needing another allocation in ->queuecommand.

With the current SCSI code that already does multiple allocations for
each command this probably doesn't make a big performance impact, but
it allows to clean up the drivers, and prepare them for using the
blk-mq infrastructure where the common allocation will make a difference.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: default avatarJames Bottomley <JBottomley@Parallels.com>
parent 7c283341
...@@ -331,46 +331,103 @@ void scsi_put_command(struct scsi_cmnd *cmd) ...@@ -331,46 +331,103 @@ void scsi_put_command(struct scsi_cmnd *cmd)
} }
EXPORT_SYMBOL(scsi_put_command); EXPORT_SYMBOL(scsi_put_command);
static struct scsi_host_cmd_pool *scsi_get_host_cmd_pool(gfp_t gfp_mask) static struct scsi_host_cmd_pool *
scsi_find_host_cmd_pool(struct Scsi_Host *shost)
{ {
if (shost->hostt->cmd_size)
return shost->hostt->cmd_pool;
if (shost->unchecked_isa_dma)
return &scsi_cmd_dma_pool;
return &scsi_cmd_pool;
}
static void
scsi_free_host_cmd_pool(struct scsi_host_cmd_pool *pool)
{
kfree(pool->sense_name);
kfree(pool->cmd_name);
kfree(pool);
}
static struct scsi_host_cmd_pool *
scsi_alloc_host_cmd_pool(struct Scsi_Host *shost)
{
struct scsi_host_template *hostt = shost->hostt;
struct scsi_host_cmd_pool *pool;
pool = kzalloc(sizeof(*pool), GFP_KERNEL);
if (!pool)
return NULL;
pool->cmd_name = kasprintf(GFP_KERNEL, "%s_cmd", hostt->name);
pool->sense_name = kasprintf(GFP_KERNEL, "%s_sense", hostt->name);
if (!pool->cmd_name || !pool->sense_name) {
scsi_free_host_cmd_pool(pool);
return NULL;
}
pool->slab_flags = SLAB_HWCACHE_ALIGN;
if (shost->unchecked_isa_dma) {
pool->slab_flags |= SLAB_CACHE_DMA;
pool->gfp_mask = __GFP_DMA;
}
return pool;
}
static struct scsi_host_cmd_pool *
scsi_get_host_cmd_pool(struct Scsi_Host *shost)
{
struct scsi_host_template *hostt = shost->hostt;
struct scsi_host_cmd_pool *retval = NULL, *pool; struct scsi_host_cmd_pool *retval = NULL, *pool;
size_t cmd_size = sizeof(struct scsi_cmnd) + hostt->cmd_size;
/* /*
* Select a command slab for this host and create it if not * Select a command slab for this host and create it if not
* yet existent. * yet existent.
*/ */
mutex_lock(&host_cmd_pool_mutex); mutex_lock(&host_cmd_pool_mutex);
pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool : pool = scsi_find_host_cmd_pool(shost);
&scsi_cmd_pool; if (!pool) {
pool = scsi_alloc_host_cmd_pool(shost);
if (!pool)
goto out;
}
if (!pool->users) { if (!pool->users) {
pool->cmd_slab = kmem_cache_create(pool->cmd_name, pool->cmd_slab = kmem_cache_create(pool->cmd_name, cmd_size, 0,
sizeof(struct scsi_cmnd), 0,
pool->slab_flags, NULL); pool->slab_flags, NULL);
if (!pool->cmd_slab) if (!pool->cmd_slab)
goto fail; goto out_free_pool;
pool->sense_slab = kmem_cache_create(pool->sense_name, pool->sense_slab = kmem_cache_create(pool->sense_name,
SCSI_SENSE_BUFFERSIZE, 0, SCSI_SENSE_BUFFERSIZE, 0,
pool->slab_flags, NULL); pool->slab_flags, NULL);
if (!pool->sense_slab) { if (!pool->sense_slab)
kmem_cache_destroy(pool->cmd_slab); goto out_free_slab;
goto fail;
}
} }
pool->users++; pool->users++;
retval = pool; retval = pool;
fail: out:
mutex_unlock(&host_cmd_pool_mutex); mutex_unlock(&host_cmd_pool_mutex);
return retval; return retval;
out_free_slab:
kmem_cache_destroy(pool->cmd_slab);
out_free_pool:
if (hostt->cmd_size)
scsi_free_host_cmd_pool(pool);
goto out;
} }
static void scsi_put_host_cmd_pool(gfp_t gfp_mask) static void scsi_put_host_cmd_pool(struct Scsi_Host *shost)
{ {
struct scsi_host_template *hostt = shost->hostt;
struct scsi_host_cmd_pool *pool; struct scsi_host_cmd_pool *pool;
mutex_lock(&host_cmd_pool_mutex); mutex_lock(&host_cmd_pool_mutex);
pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool : pool = scsi_find_host_cmd_pool(shost);
&scsi_cmd_pool;
/* /*
* This may happen if a driver has a mismatched get and put * This may happen if a driver has a mismatched get and put
* of the command pool; the driver should be implicated in * of the command pool; the driver should be implicated in
...@@ -381,6 +438,8 @@ static void scsi_put_host_cmd_pool(gfp_t gfp_mask) ...@@ -381,6 +438,8 @@ static void scsi_put_host_cmd_pool(gfp_t gfp_mask)
if (!--pool->users) { if (!--pool->users) {
kmem_cache_destroy(pool->cmd_slab); kmem_cache_destroy(pool->cmd_slab);
kmem_cache_destroy(pool->sense_slab); kmem_cache_destroy(pool->sense_slab);
if (hostt->cmd_size)
scsi_free_host_cmd_pool(pool);
} }
mutex_unlock(&host_cmd_pool_mutex); mutex_unlock(&host_cmd_pool_mutex);
} }
...@@ -397,14 +456,13 @@ static void scsi_put_host_cmd_pool(gfp_t gfp_mask) ...@@ -397,14 +456,13 @@ static void scsi_put_host_cmd_pool(gfp_t gfp_mask)
*/ */
int scsi_setup_command_freelist(struct Scsi_Host *shost) int scsi_setup_command_freelist(struct Scsi_Host *shost)
{ {
struct scsi_cmnd *cmd;
const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL; const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL;
struct scsi_cmnd *cmd;
spin_lock_init(&shost->free_list_lock); spin_lock_init(&shost->free_list_lock);
INIT_LIST_HEAD(&shost->free_list); INIT_LIST_HEAD(&shost->free_list);
shost->cmd_pool = scsi_get_host_cmd_pool(gfp_mask); shost->cmd_pool = scsi_get_host_cmd_pool(shost);
if (!shost->cmd_pool) if (!shost->cmd_pool)
return -ENOMEM; return -ENOMEM;
...@@ -413,7 +471,7 @@ int scsi_setup_command_freelist(struct Scsi_Host *shost) ...@@ -413,7 +471,7 @@ int scsi_setup_command_freelist(struct Scsi_Host *shost)
*/ */
cmd = scsi_host_alloc_command(shost, gfp_mask); cmd = scsi_host_alloc_command(shost, gfp_mask);
if (!cmd) { if (!cmd) {
scsi_put_host_cmd_pool(gfp_mask); scsi_put_host_cmd_pool(shost);
shost->cmd_pool = NULL; shost->cmd_pool = NULL;
return -ENOMEM; return -ENOMEM;
} }
...@@ -442,7 +500,7 @@ void scsi_destroy_command_freelist(struct Scsi_Host *shost) ...@@ -442,7 +500,7 @@ void scsi_destroy_command_freelist(struct Scsi_Host *shost)
scsi_host_free_command(shost, cmd); scsi_host_free_command(shost, cmd);
} }
shost->cmd_pool = NULL; shost->cmd_pool = NULL;
scsi_put_host_cmd_pool(shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL); scsi_put_host_cmd_pool(shost);
} }
#ifdef CONFIG_SCSI_LOGGING #ifdef CONFIG_SCSI_LOGGING
......
...@@ -15,6 +15,7 @@ struct completion; ...@@ -15,6 +15,7 @@ struct completion;
struct module; struct module;
struct scsi_cmnd; struct scsi_cmnd;
struct scsi_device; struct scsi_device;
struct scsi_host_cmd_pool;
struct scsi_target; struct scsi_target;
struct Scsi_Host; struct Scsi_Host;
struct scsi_host_cmd_pool; struct scsi_host_cmd_pool;
...@@ -524,6 +525,12 @@ struct scsi_host_template { ...@@ -524,6 +525,12 @@ struct scsi_host_template {
* scsi_netlink.h * scsi_netlink.h
*/ */
u64 vendor_id; u64 vendor_id;
/*
* Additional per-command data allocated for the driver.
*/
unsigned int cmd_size;
struct scsi_host_cmd_pool *cmd_pool;
}; };
/* /*
......
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