Skip to contentSkip to conversation

scsi: core: don't start concurrent async scan on same host

PATCH Applied Ming Lei 5 participants 6 messages Oct 10, 2020 – Oct 26, 2020 SCSI

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

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.

Cc Christoph Hellwig <[email protected]>
Cc Ewan D. Milne <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Bart Van Assche <[email protected]>
---
 drivers/scsi/scsi_scan.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +4 -3
index f2437a7570ce..9af50e6f94c4 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
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 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.

Cc Christoph Hellwig <[email protected]>
Cc Ewan D. Milne <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Bart Van Assche <[email protected]>
---
 drivers/scsi/scsi_scan.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +3 -3
index f2437a7570ce..9af50e6f94c4 100644
@@ -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;
 }
#2 lore raw
In reply to Ming Lei
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 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.

Cc Christoph Hellwig <[email protected]>
Cc Ewan D. Milne <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Bart Van Assche <[email protected]>
---
 drivers/scsi/scsi_scan.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/scsi_scan.c +4 -4
index f2437a7570ce..9af50e6f94c4 100644
@@ -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

Reviewed-by on v1 1/1
#3 lore raw
In reply to 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 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.

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:

author
#4 lore raw
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 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.

Quoted 5 lines
Quoted 2 lines

Fixes this issue by checking & setting shost->async_scan atomically with shost->scan_mutex.

Did you perhaps mean "by serializing shost->async_scan accesses with shost->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_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?

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:

Thanks!

Signature · 1 line
Ming
#5 lore raw
In reply to Ming Lei

Ming,

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 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.

Applied to 5.10/scsi-fixes, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering