scsi: core: don't start concurrent async scan on same host
PATCH 1/1 scsi: core: don't start concurrent async scan on same host
Current scsi host scan mechanism supposes to fallback into sync host scan if async scan is in-progress. However, this rule isn't strictly respected, because scsi_prep_async_scan() doesn't hold scan_mutex when checking shost->async_scan. When scsi_scan_host() is called concurrently, two async scan on same host may be started, and hang in do_scan_async() is observed.
Fixes this issue by checking & setting shost->async_scan atomically with shost->scan_mutex.
--- drivers/scsi/scsi_scan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +4 -3
@@ -1714,15 +1714,16 @@ static void scsi_sysfs_add_devices(struct Scsi_Host *shost)*/ static struct async_scan_data *scsi_prep_async_scan (struct Scsi_Host *shost) { -struct async_scan_data *data; +struct async_scan_data *data =NULL ;unsigned long flags;if (strncmp (scsi_scan_type,"sync" ,4 ) ==0 )return NULL ; +mutex_lock (&shost->scan_mutex);if (shost->async_scan) {shost_printk (KERN_DEBUG , shost,"%s called twice\n" , __func__); -return NULL ; + goto err; } data =kmalloc (sizeof (*data),GFP_KERNEL );
@@ -1733,7 +1734,6 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) goto err;init_completion (&data->prev_finished); -mutex_lock (&shost->scan_mutex);spin_lock_irqsave (shost->host_lock, flags); shost->async_scan =1 ;spin_unlock_irqrestore (shost->host_lock, flags);
@@ -1748,6 +1748,7 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)return data; err: +mutex_unlock (&shost->scan_mutex);kfree (data);return NULL ; }
Signature · 1 line
2.25.2
Quoted 52 lines · Ming Lei
Current scsi host scan mechanism supposes to fallback into sync host scan if async scan is in-progress. However, this rule isn't strictly respected, because
scsi_prep_async_scan()doesn't holdscan_mutexwhen checkingshost->async_scan. Whenscsi_scan_host()is called concurrently, two async scan on same host may be started, and hang indo_scan_async()is observed.Fixes this issue by checking & setting
shost->async_scanatomically withshost->scan_mutex.Cc Christoph Hellwig <[email protected]>Cc Ewan D. Milne <[email protected]>Cc Hannes Reinecke <[email protected]>Cc Bart Van Assche <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/scsi_scan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/scsi/scsi_scan.c +3
-3@@ -1714,15 +1714,16 @@ static void scsi_sysfs_add_devices(struct Scsi_Host *shost)*/ static struct async_scan_data *scsi_prep_async_scan (struct Scsi_Host *shost) { -struct async_scan_data *data; +struct async_scan_data *data =NULL ;unsigned long flags;if (strncmp (scsi_scan_type,"sync" ,4 ) ==0 )return NULL ; +mutex_lock (&shost->scan_mutex);if (shost->async_scan) {shost_printk (KERN_DEBUG , shost,"%s called twice\n" , __func__); -return NULL ; + goto err; } data =kmalloc (sizeof (*data),GFP_KERNEL );@@ -1733,7 +1734,6 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) goto err;init_completion (&data->prev_finished); -mutex_lock (&shost->scan_mutex);spin_lock_irqsave (shost->host_lock, flags); shost->async_scan =1 ;spin_unlock_irqrestore (shost->host_lock, flags);
drivers/scsi/scsi_scan.c static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) · line 1754
Quoted 7 lines
drivers/scsi/scsi_scan.c +1
-0@@ -1748,6 +1748,7 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)return data; err: +mutex_unlock (&shost->scan_mutex);kfree (data);return NULL ; }
Quoted 60 lines · Ming Lei
Current scsi host scan mechanism supposes to fallback into sync host scan if async scan is in-progress. However, this rule isn't strictly respected, because
scsi_prep_async_scan()doesn't holdscan_mutexwhen checkingshost->async_scan. Whenscsi_scan_host()is called concurrently, two async scan on same host may be started, and hang indo_scan_async()is observed.Fixes this issue by checking & setting
shost->async_scanatomically withshost->scan_mutex.Cc Christoph Hellwig <[email protected]>Cc Ewan D. Milne <[email protected]>Cc Hannes Reinecke <[email protected]>Cc Bart Van Assche <[email protected]>Signed-off-by Ming Lei <[email protected]>--- drivers/scsi/scsi_scan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/scsi/scsi_scan.c +4
-4@@ -1714,15 +1714,16 @@ static void scsi_sysfs_add_devices(struct Scsi_Host *shost)*/ static struct async_scan_data *scsi_prep_async_scan (struct Scsi_Host *shost) { -struct async_scan_data *data; +struct async_scan_data *data =NULL ;unsigned long flags;if (strncmp (scsi_scan_type,"sync" ,4 ) ==0 )return NULL ; +mutex_lock (&shost->scan_mutex);if (shost->async_scan) {shost_printk (KERN_DEBUG , shost,"%s called twice\n" , __func__); -return NULL ; + goto err; } data =kmalloc (sizeof (*data),GFP_KERNEL );@@ -1733,7 +1734,6 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) goto err;init_completion (&data->prev_finished); -mutex_lock (&shost->scan_mutex);spin_lock_irqsave (shost->host_lock, flags); shost->async_scan =1 ;spin_unlock_irqrestore (shost->host_lock, flags);@@ -1748,6 +1748,7 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)return data; err: +mutex_unlock (&shost->scan_mutex);kfree (data);return NULL ; } --
Quoted 2 lines
2.25.2
Hello Guys,
Ping...
Thanks,
Ming Lei
Quoted 9 lines · Ming Lei
Current scsi host scan mechanism supposes to fallback into sync host scan if async scan is in-progress. However, this rule isn't strictly respected, because
scsi_prep_async_scan()doesn't holdscan_mutexwhen checkingshost->async_scan. Whenscsi_scan_host()is called concurrently, two async scan on same host may be started, and hang indo_scan_async()is observed.Fixes this issue by checking & setting
shost->async_scanatomically withshost->scan_mutex.
Did you perhaps mean "by serializing shost->async_scan accesses with shost->scan_mutex"?
It is not clear to me why the shost->async_scan assignment is protected with the host lock. Can spin_lock_irqsave(shost->host_lock, flags) and spin_unlock_irqrestore(shost->host_lock, flags) be left out from this function?
Anyway:
Quoted 8 lines · Bart Van Assche
Quoted 7 lines · Ming Lei
Current scsi host scan mechanism supposes to fallback into sync host scan if async scan is in-progress. However, this rule isn't strictly respected, because
scsi_prep_async_scan()doesn't holdscan_mutexwhen checkingshost->async_scan. Whenscsi_scan_host()is called concurrently, two async scan on same host may be started, and hang indo_scan_async()is observed.
Quoted 5 lines
Quoted 2 lines
Fixes this issue by checking & setting
shost->async_scanatomically withshost->scan_mutex.Did you perhaps mean "by serializing
shost->async_scanaccesses withshost->scan_mutex"?
Specifically, the following checking & setting has to be done atomically, so shost->scan_mutex is required, just as what scsi_finish_async_scan() does for clearing shost->async_scan:
scsi_prep_async_scan():
if (!shost->async_scan) return NULL; ... shost->async_scan = 1
Quoted 5 lines
It is not clear to me why the
shost->async_scanassignment is protected with the host lock. Canspin_lock_irqsave(shost->host_lock, flags)andspin_unlock_irqrestore(shost->host_lock, flags)be left out from this function?
I think it is doable to remove the ->host_lock from both scsi_prep_async_scan() and scsi_finish_async_scan(), which can be done as one follow-up cleanup.
With this patch, all reading/writing shost->async_scan are protected by shost->scan_mutex.
Quoted 4 lines
Anyway:
Reviewed-by Bart Van Assche <[email protected]>
Thanks!
Signature · 1 line
Ming
Martin K. Petersen
AppliedMing,
Quoted 9 lines
Current scsi host scan mechanism supposes to fallback into sync host scan if async scan is in-progress. However, this rule isn't strictly respected, because
scsi_prep_async_scan()doesn't holdscan_mutexwhen checkingshost->async_scan. Whenscsi_scan_host()is called concurrently, two async scan on same host may be started, and hang indo_scan_async()is observed.Fixes this issue by checking & setting
shost->async_scanatomically withshost->scan_mutex.
Applied to 5.10/scsi-fixes, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering