public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] scsi/sd: release scan_mutex during sync_cache and start_stop
@ 2017-02-08 20:42 Song Liu
  2017-02-08 21:02 ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Song Liu @ 2017-02-08 20:42 UTC (permalink / raw)
  To: linux-scsi; +Cc: Song Liu

When a device is deleted through sysfs handle "delete", the code
locks shost->scan_mutex. If multiple devices are deleted at the
same time, these deletes will be handled in series.

On the other hand, sd_shutdown() sometimes issues long latency
commands: sync cache and start_stop. It is not necessary for these
commands to run in series.

To reduce latency of parallel "delete" requests, this patch unlock
shost->scan_mutex before long latency commands and relock the mutex
after the command.

Fixed bug from previous version.

Reported-by: kernel test robot <fengguang.wu@intel.com>
Signed-off-by: Song Liu <songliubraving@fb.com>
---
 drivers/scsi/sd.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 9e0783b..22add77 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3304,6 +3304,9 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
 static void sd_shutdown(struct device *dev)
 {
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
+	struct scsi_device *sdev;
+	struct Scsi_Host *shost;
+	bool scan_mutex_locked;
 
 	if (!sdkp)
 		return;         /* this can happen */
@@ -3311,14 +3314,26 @@ static void sd_shutdown(struct device *dev)
 	if (pm_runtime_suspended(dev))
 		return;
 
+	sdev = sdkp->device;
+	shost = sdev->host;
+	scan_mutex_locked = mutex_is_locked(&shost->scan_mutex);
+
 	if (sdkp->WCE && sdkp->media_present) {
+		if (scan_mutex_locked)
+			mutex_unlock(&shost->scan_mutex);
 		sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
 		sd_sync_cache(sdkp);
+		if (scan_mutex_locked)
+			mutex_lock(&shost->scan_mutex);
 	}
 
 	if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
+		if (scan_mutex_locked)
+			mutex_unlock(&shost->scan_mutex);
 		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
 		sd_start_stop_device(sdkp, 0);
+		if (scan_mutex_locked)
+			mutex_lock(&shost->scan_mutex);
 	}
 }
 
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] scsi/sd: release scan_mutex during sync_cache and start_stop
  2017-02-08 20:42 [PATCH v2] scsi/sd: release scan_mutex during sync_cache and start_stop Song Liu
@ 2017-02-08 21:02 ` Christoph Hellwig
  2017-02-08 21:15   ` Song Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2017-02-08 21:02 UTC (permalink / raw)
  To: Song Liu; +Cc: linux-scsi

On Wed, Feb 08, 2017 at 12:42:40PM -0800, Song Liu wrote:
> When a device is deleted through sysfs handle "delete", the code
> locks shost->scan_mutex. If multiple devices are deleted at the
> same time, these deletes will be handled in series.
> 
> On the other hand, sd_shutdown() sometimes issues long latency
> commands: sync cache and start_stop. It is not necessary for these
> commands to run in series.
> 
> To reduce latency of parallel "delete" requests, this patch unlock
> shost->scan_mutex before long latency commands and relock the mutex
> after the command.
> 
> Fixed bug from previous version.
> 
> Reported-by: kernel test robot <fengguang.wu@intel.com>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>  drivers/scsi/sd.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index 9e0783b..22add77 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
> @@ -3304,6 +3304,9 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
>  static void sd_shutdown(struct device *dev)
>  {
>  	struct scsi_disk *sdkp = dev_get_drvdata(dev);
> +	struct scsi_device *sdev;
> +	struct Scsi_Host *shost;
> +	bool scan_mutex_locked;
>  
>  	if (!sdkp)
>  		return;         /* this can happen */
> @@ -3311,14 +3314,26 @@ static void sd_shutdown(struct device *dev)
>  	if (pm_runtime_suspended(dev))
>  		return;
>  
> +	sdev = sdkp->device;
> +	shost = sdev->host;
> +	scan_mutex_locked = mutex_is_locked(&shost->scan_mutex);

The use of mutex_is_locked outside of asserts is always bogus.

The fix you want is most like in the caller of the method.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] scsi/sd: release scan_mutex during sync_cache and start_stop
  2017-02-08 21:02 ` Christoph Hellwig
@ 2017-02-08 21:15   ` Song Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Song Liu @ 2017-02-08 21:15 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-scsi@vger.kernel.org


> On Feb 8, 2017, at 1:02 PM, Christoph Hellwig <hch@infradead.org> wrote:
> 
> On Wed, Feb 08, 2017 at 12:42:40PM -0800, Song Liu wrote:
>> When a device is deleted through sysfs handle "delete", the code
>> locks shost->scan_mutex. If multiple devices are deleted at the
>> same time, these deletes will be handled in series.
>> 
>> On the other hand, sd_shutdown() sometimes issues long latency
>> commands: sync cache and start_stop. It is not necessary for these
>> commands to run in series.
>> 
>> To reduce latency of parallel "delete" requests, this patch unlock
>> shost->scan_mutex before long latency commands and relock the mutex
>> after the command.
>> 
>> Fixed bug from previous version.
>> 
>> Reported-by: kernel test robot <fengguang.wu@intel.com>
>> Signed-off-by: Song Liu <songliubraving@fb.com>
>> ---
>> drivers/scsi/sd.c | 15 +++++++++++++++
>> 1 file changed, 15 insertions(+)
>> 
>> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
>> index 9e0783b..22add77 100644
>> --- a/drivers/scsi/sd.c
>> +++ b/drivers/scsi/sd.c
>> @@ -3304,6 +3304,9 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
>> static void sd_shutdown(struct device *dev)
>> {
>> 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
>> +	struct scsi_device *sdev;
>> +	struct Scsi_Host *shost;
>> +	bool scan_mutex_locked;
>> 
>> 	if (!sdkp)
>> 		return;         /* this can happen */
>> @@ -3311,14 +3314,26 @@ static void sd_shutdown(struct device *dev)
>> 	if (pm_runtime_suspended(dev))
>> 		return;
>> 
>> +	sdev = sdkp->device;
>> +	shost = sdev->host;
>> +	scan_mutex_locked = mutex_is_locked(&shost->scan_mutex);
> 
> The use of mutex_is_locked outside of asserts is always bogus.
> 
> The fix you want is most like in the caller of the method.

Hi Christoph, 

Thanks for the feedback. The other caller (that does not hold scan_mutex) 
of sd_shutdown() is device_shutdown() in drivers/base/core.c. I could not 
think of a good way to lock scan_mutex there. 

Would some variation of mutex_trylock() be a better option here? 

Thanks,
Song

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-02-08 21:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-08 20:42 [PATCH v2] scsi/sd: release scan_mutex during sync_cache and start_stop Song Liu
2017-02-08 21:02 ` Christoph Hellwig
2017-02-08 21:15   ` Song Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox