Commit f9b0530f authored by Ming Lei's avatar Ming Lei Committed by Martin K. Petersen

scsi: core: Fix race on creating sense cache

When scsi_init_sense_cache(host) is called concurrently from different
hosts, each code path may find that no cache has been created and
allocate a new one. The lack of locking can lead to potentially
overriding a cache allocated by a different host.

Fix the issue by moving 'mutex_lock(&scsi_sense_cache_mutex)' before
scsi_select_sense_cache().

Fixes: 0a6ac4ee ("scsi: respect unchecked_isa_dma for blk-mq")
Cc: Stable <stable@vger.kernel.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Ewan D. Milne <emilne@redhat.com>
Signed-off-by: default avatarMing Lei <ming.lei@redhat.com>
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
parent 0cdc5858
...@@ -84,11 +84,11 @@ int scsi_init_sense_cache(struct Scsi_Host *shost) ...@@ -84,11 +84,11 @@ int scsi_init_sense_cache(struct Scsi_Host *shost)
struct kmem_cache *cache; struct kmem_cache *cache;
int ret = 0; int ret = 0;
mutex_lock(&scsi_sense_cache_mutex);
cache = scsi_select_sense_cache(shost->unchecked_isa_dma); cache = scsi_select_sense_cache(shost->unchecked_isa_dma);
if (cache) if (cache)
return 0; goto exit;
mutex_lock(&scsi_sense_cache_mutex);
if (shost->unchecked_isa_dma) { if (shost->unchecked_isa_dma) {
scsi_sense_isadma_cache = scsi_sense_isadma_cache =
kmem_cache_create("scsi_sense_cache(DMA)", kmem_cache_create("scsi_sense_cache(DMA)",
...@@ -104,7 +104,7 @@ int scsi_init_sense_cache(struct Scsi_Host *shost) ...@@ -104,7 +104,7 @@ int scsi_init_sense_cache(struct Scsi_Host *shost)
if (!scsi_sense_cache) if (!scsi_sense_cache)
ret = -ENOMEM; ret = -ENOMEM;
} }
exit:
mutex_unlock(&scsi_sense_cache_mutex); mutex_unlock(&scsi_sense_cache_mutex);
return ret; return ret;
} }
......
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