Skip to contentSkip to conversation

Rework SCSI runtime power management support

PATCH Applied Bart Van Assche 4 participants 11 messages Oct 6, 2021 – Oct 21, 2021 SCSI

lore
Share & tools
lore
#0 lore raw

cover Rework SCSI runtime power management support

Hi Martin,

For the UFS driver it is undesired that the SCSI power management core activates runtime suspended devices during system resume. This patch series leaves SCSI devices runtime suspended during system resume if these were runtime suspended before the system was suspended. Please consider this patch series for Linux kernel v5.16.

Thanks,

Bart.

Bart Van Assche (3):
scsi: pm: Rely on the device driver core for async power management
scsi: sd: Rename sd_resume() into sd_resume_system()
scsi: pm: Only runtime resume if necessary

 drivers/scsi/hosts.c      |   1 +
 drivers/scsi/scsi.c       |   8 ---
 drivers/scsi/scsi_pm.c    | 105 +++++---------------------------------
 drivers/scsi/scsi_priv.h  |   4 +-
 drivers/scsi/scsi_scan.c  |  17 ++++++
 drivers/scsi/scsi_sysfs.c |   1 +
 drivers/scsi/sd.c         |  18 +++++--
 7 files changed, 46 insertions(+), 108 deletions(-)
#1 lore raw

PATCH 1/3 scsi: pm: Rely on the device driver core for async power management

Instead of implementing asynchronous resume support in the SCSI core, rely on the device driver core for resuming SCSI devices asynchronously. Instead of only supporting asynchronous resumes, also support asynchronous suspends.

Cc Alan Stern <[email protected]>
Cc Dan Williams <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Martin Kepplinger <[email protected]>
---
 drivers/scsi/hosts.c      |  1 +
 drivers/scsi/scsi.c       |  8 -------
 drivers/scsi/scsi_pm.c    | 44 ++-------------------------------------
 drivers/scsi/scsi_priv.h  |  4 +---
 drivers/scsi/scsi_scan.c  | 17 +++++++++++++++
 drivers/scsi/scsi_sysfs.c |  1 +
 drivers/scsi/sd.c         |  1 -
 7 files changed, 22 insertions(+), 54 deletions(-)
drivers/scsi/hosts.c +1 -0
index 3f6f14f0cafb..b8362700a9de 100644
@@ -475,6 +475,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
 	dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
 	shost->shost_gendev.bus = &scsi_bus_type;
 	shost->shost_gendev.type = &scsi_host_type;
+	scsi_enable_async_suspend(&shost->shost_gendev);
 
 	device_initialize(&shost->shost_dev);
 	shost->shost_dev.parent = &shost->shost_gendev;
drivers/scsi/scsi.c +0 -8
index b241f9e3885c..ed9753f54b8a 100644
@@ -86,14 +86,6 @@ unsigned int scsi_logging_level;
 EXPORT_SYMBOL(scsi_logging_level);
 #endif
 
-/*
- * Domain for asynchronous system resume operations.  It is marked 'exclusive'
- * to avoid being included in the async_synchronize_full() that is invoked by
- * dpm_resume().
- */
-ASYNC_DOMAIN_EXCLUSIVE(scsi_sd_pm_domain);
-EXPORT_SYMBOL(scsi_sd_pm_domain);
-
 #ifdef CONFIG_SCSI_LOGGING
 void scsi_log_send(struct scsi_cmnd *cmd)
 {
drivers/scsi/scsi_pm.c +2 -42

59 lines not on this page · raw message

drivers/scsi/scsi_priv.h +1 -3
index 6d9152031a40..2d9de5a165ae 100644
@@ -116,7 +116,7 @@ extern void scsi_exit_procfs(void);
 #endif /* CONFIG_PROC_FS */
 
 /* scsi_scan.c */
-extern char scsi_scan_type[];
+void scsi_enable_async_suspend(struct device *dev);
 extern int scsi_complete_async_scans(void);
 extern int scsi_scan_host_selected(struct Scsi_Host *, unsigned int,
 				   unsigned int, u64, enum scsi_scan_mode);
@@ -170,8 +170,6 @@ static inline int scsi_autopm_get_host(struct Scsi_Host *h) { return 0; }
 static inline void scsi_autopm_put_host(struct Scsi_Host *h) {}
 #endif /* CONFIG_PM */
 
-extern struct async_domain scsi_sd_pm_domain;
-
 /* scsi_dh.c */
 #ifdef CONFIG_SCSI_DH
 void scsi_dh_add_device(struct scsi_device *sdev);
drivers/scsi/scsi_scan.c +17 -0
index 0d0381df25f7..c86152f9c47a 100644
@@ -122,6 +122,22 @@ struct async_scan_data {
 	struct completion prev_finished;
 };
 
+/**
+ * scsi_enable_async_suspend - Enable async suspend and resume
+ */
+void scsi_enable_async_suspend(struct device *dev)
+{
+	/*
+	 * If a user has disabled async probing a likely reason is due to a
+	 * storage enclosure that does not inject staggered spin-ups. For
+	 * safety, make resume synchronous as well in that case.
+	 */
+	if (strncmp(scsi_scan_type, "async", 5) != 0)
+		return;
+	/* Enable asynchronous suspend and resume. */
+	device_enable_async_suspend(dev);
+}
+
 /**
  * scsi_complete_async_scans - Wait for asynchronous scans to complete
  *
@@ -454,6 +470,7 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
 	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
 	dev->bus = &scsi_bus_type;
 	dev->type = &scsi_target_type;
+	scsi_enable_async_suspend(dev);
 	starget->id = id;
 	starget->channel = channel;
 	starget->can_queue = 0;
drivers/scsi/scsi_sysfs.c +1 -0
index 86793259e541..b598dfcbb67d 100644
@@ -1616,6 +1616,7 @@ void scsi_sysfs_device_initialize(struct scsi_device *sdev)
 	device_initialize(&sdev->sdev_gendev);
 	sdev->sdev_gendev.bus = &scsi_bus_type;
 	sdev->sdev_gendev.type = &scsi_dev_type;
+	scsi_enable_async_suspend(&sdev->sdev_gendev);
 	dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
 
drivers/scsi/sd.c +0 -1
index 71fa70b42c2b..78ebccd9d106 100644
@@ -3500,7 +3500,6 @@ static int sd_remove(struct device *dev)
 	sdkp = dev_get_drvdata(dev);
 	scsi_autopm_get_device(sdkp->device);
 
-	async_synchronize_full_domain(&scsi_sd_pm_domain);
 	device_del(&sdkp->dev);
 	del_gendisk(sdkp->disk);
 	sd_shutdown(dev);
#2 lore raw

PATCH 2/3 scsi: sd: Rename sd_resume() into sd_resume_system()

In reply to Bart Van Assche

This patch does not change any functionality but makes the next patch in this series easier to read.

Cc Alan Stern <[email protected]>
Cc Dan Williams <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Martin Kepplinger <[email protected]>
---
 drivers/scsi/sd.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/scsi/sd.c +8 -3
index 78ebccd9d106..d61b096562d3 100644
@@ -109,7 +109,7 @@ static int  sd_remove(struct device *);
 static void sd_shutdown(struct device *);
 static int sd_suspend_system(struct device *);
 static int sd_suspend_runtime(struct device *);
-static int sd_resume(struct device *);
+static int sd_resume_system(struct device *);
 static int sd_resume_runtime(struct device *);
 static void sd_rescan(struct device *);
 static blk_status_t sd_init_command(struct scsi_cmnd *SCpnt);
@@ -602,9 +602,9 @@ static struct class sd_disk_class = {
 
 static const struct dev_pm_ops sd_pm_ops = {
 	.suspend		= sd_suspend_system,
-	.resume			= sd_resume,
+	.resume			= sd_resume_system,
 	.poweroff		= sd_suspend_system,
-	.restore		= sd_resume,
+	.restore		= sd_resume_system,
 	.runtime_suspend	= sd_suspend_runtime,
 	.runtime_resume		= sd_resume_runtime,
 };
@@ -3682,6 +3682,11 @@ static int sd_resume(struct device *dev)
 	return ret;
 }
 
+static int sd_resume_system(struct device *dev)
+{
+	return sd_resume(dev);
+}
+
 static int sd_resume_runtime(struct device *dev)
 {
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
#3 lore raw

PATCH 3/3 scsi: pm: Only runtime resume if necessary

In reply to Bart Van Assche

The following query shows which drivers define callbacks that are called by the power management support code in the SCSI core (scsi_pm.c):

$ git grep -nHEwA16 "$(echo $(git grep -h 'scsi_register_driver(&' |
      sed 's/.*&//;s/\..*//') | sed 's/ /|/g')" |
    grep '\.pm[[:blank:]]*=[[:blank:]]'
drivers/scsi/sd.c-620-		.pm		= &sd_pm_ops,
drivers/scsi/sr.c-100-		.pm		= &sr_pm_ops,
drivers/scsi/ufs/ufshcd.c-9765-		.pm = &ufshcd_wl_pm_ops,

Since unconditionally runtime resuming a device during system resume is not necessary, remove that code. Modify the SCSI disk (sd) driver such that it follows the same approach as the UFS driver, namely to skip system suspend and resume for devices that are runtime suspended. The CD-ROM code does not need to be updated since its PM callbacks do not affect the device power state.

This patch has been tested as follows:

[ shell 1 ]

cd /sys/kernel/debug/tracing
grep -E 'blk_(pre|post)_runtime|runtime_(suspend|resume)|autosuspend_delay|pm_runtime_(get|put)' available_filter_functions |
while read a b; do echo "$a"; done |
grep -v __pm_runtime_resume >set_ftrace_filter
echo function > current_tracer
echo 1 > tracing_on
cat trace_pipe

[ shell 2 ]

cd /sys/block/sr0
# Increase the event poll interval to make it easier to derive from the

 # tracing output whether runtime power actions are the result of sg_inq.
echo 30000 > events_poll_msecs
cd device/power
 # Enable runtime power management.
echo auto > control
echo 1000 > autosuspend_delay_ms
sleep 1
 # Verify in shell 1 that sr0 has been runtime suspended
sg_inq /dev/sr0
eject /dev/sr0
sg_inq /dev/sr0
 # Disable runtime power management.
echo on > control

cd /sys/block/sda/device/power
echo auto > control
echo 1000 > autosuspend_delay_ms
sleep 1
# Verify in shell 1 that sr0 has been runtime suspended
sg_inq /dev/sda

Cc Alan Stern <[email protected]>
Cc Dan Williams <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Martin Kepplinger <[email protected]>
---
 drivers/scsi/scsi_pm.c | 69 +++++++++---------------------------------
 drivers/scsi/sd.c      |  6 ++++
 2 files changed, 20 insertions(+), 55 deletions(-)
drivers/scsi/scsi_pm.c +14 -55
index 50b6bad4df79..b5a858c29488 100644
@@ -66,71 +66,30 @@ static int scsi_dev_type_suspend(struct device *dev,
 	return err;
 }
 
-static int scsi_dev_type_resume(struct device *dev,
-		int (*cb)(struct device *, const struct dev_pm_ops *))
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-	int err = 0;
-
-	err = cb(dev, pm);
-	scsi_device_resume(to_scsi_device(dev));
-	dev_dbg(dev, "scsi resume: %d\n", err);
-
-	if (err == 0) {
-		pm_runtime_disable(dev);
-		err = pm_runtime_set_active(dev);
-		pm_runtime_enable(dev);
-
-		/*
-		 * Forcibly set runtime PM status of request queue to "active"
-		 * to make sure we can again get requests from the queue
-		 * (see also blk_pm_peek_request()).
-		 *
-		 * The resume hook will correct runtime PM status of the disk.
-		 */
-		if (!err && scsi_is_sdev_device(dev)) {
-			struct scsi_device *sdev = to_scsi_device(dev);
-
-			blk_set_runtime_active(sdev->request_queue);
-		}
-	}
-
-	return err;
-}
-
 static int
 scsi_bus_suspend_common(struct device *dev,
 		int (*cb)(struct device *, const struct dev_pm_ops *))
 {
-	int err = 0;
+	if (!scsi_is_sdev_device(dev))
+		return 0;
 
-	if (scsi_is_sdev_device(dev)) {
-		/*
-		 * All the high-level SCSI drivers that implement runtime
-		 * PM treat runtime suspend, system suspend, and system
-		 * hibernate nearly identically. In all cases the requirements
-		 * for runtime suspension are stricter.
-		 */
-		if (pm_runtime_suspended(dev))
-			return 0;
-
-		err = scsi_dev_type_suspend(dev, cb);
-	}
-
-	return err;
+	return scsi_dev_type_suspend(dev, cb);
 }
 
 static int scsi_bus_resume_common(struct device *dev,
 		int (*cb)(struct device *, const struct dev_pm_ops *))
 {
-	if (scsi_is_sdev_device(dev)) {
-		scsi_dev_type_resume(dev, cb);
-	} else {
-		pm_runtime_disable(dev);
-		pm_runtime_set_active(dev);
-		pm_runtime_enable(dev);
-	}
-	return 0;
+	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+	int err;
+
+	if (!scsi_is_sdev_device(dev))
+		return 0;
+
+	err = cb(dev, pm);
+	scsi_device_resume(to_scsi_device(dev));
+	dev_dbg(dev, "scsi resume: %d\n", err);
+
+	return err;
 }
 
 static int scsi_bus_prepare(struct device *dev)
drivers/scsi/sd.c +6 -0
index d61b096562d3..a3c6e156e83a 100644
@@ -3656,6 +3656,9 @@ static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
 
 static int sd_suspend_system(struct device *dev)
 {
+	if (pm_runtime_suspended(dev))
+		return 0;
+
 	return sd_suspend_common(dev, true);
 }
 
@@ -3684,6 +3687,9 @@ static int sd_resume(struct device *dev)
 
 static int sd_resume_system(struct device *dev)
 {
+	if (pm_runtime_suspended(dev))
+		return 0;
+
 	return sd_resume(dev);
 }
 
Tested-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] scsi: pm: Only runtime resume if necessary

Am Mittwoch, dem 06.10.2021 um 14:54 -0700 schrieb Bart Van Assche:

Quoted 67 lines

The following query shows which drivers define callbacks that are called
by the power management support code in the SCSI core (scsi_pm.c):

$ git grep -nHEwA16 "$(echo $(git grep -h 'scsi_register_driver(&' |
      sed 's/.*&//;s/\..*//') | sed 's/ /|/g')" |
    grep '\.pm[[:blank:]]*=[[:blank:]]'
drivers/scsi/sd.c-620-          .pm             = &sd_pm_ops,
drivers/scsi/sr.c-100-          .pm             = &sr_pm_ops,
drivers/scsi/ufs/ufshcd.c-9765-         .pm = &ufshcd_wl_pm_ops,

Since unconditionally runtime resuming a device during system resume is
not necessary, remove that code. Modify the SCSI disk (sd) driver such
that it follows the same approach as the UFS driver, namely to skip system suspend and resume for devices that are runtime suspended. The CD-ROM code does not need to be updated since its PM callbacks do not affect the device power state.

This patch has been tested as follows:

[ shell 1 ]

cd /sys/kernel/debug/tracing
grep -E 'blk_(pre|post)_runtime|runtime_(suspend|resume)|autosuspend_delay|pm _runtime_(get|put)' available_filter_functions |
  while read a b; do echo "$a"; done |
  grep -v __pm_runtime_resume >set_ftrace_filter
echo function > current_tracer
echo 1 > tracing_on
cat trace_pipe

[ shell 2 ]

cd /sys/block/sr0
 # Increase the event poll interval to make it easier to derive from the

 # tracing output whether runtime power actions are the result of
sg_inq.
echo 30000 > events_poll_msecs
cd device/power
 # Enable runtime power management.
echo auto > control
echo 1000 > autosuspend_delay_ms
sleep 1
 # Verify in shell 1 that sr0 has been runtime suspended
sg_inq /dev/sr0
eject /dev/sr0
sg_inq /dev/sr0
 # Disable runtime power management.
echo on > control

cd /sys/block/sda/device/power
echo auto > control
echo 1000 > autosuspend_delay_ms
sleep 1
 # Verify in shell 1 that sr0 has been runtime suspended sg_inq /dev/sda

Cc Alan Stern <[email protected]>
Cc Dan Williams <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Martin Kepplinger <[email protected]>
Quoted 6 lines

---
 drivers/scsi/scsi_pm.c | 69 +++++++++------------------------------- --
 drivers/scsi/sd.c      |  6 ++++

 2 files changed, 20 insertions(+), 55 deletions(-)

on my system with a runtime-suspend enabled sd cardreader.

thanks,
martin

#5 lore raw

Re: [PATCH 3/3] scsi: pm: Only runtime resume if necessary

Am Donnerstag, dem 07.10.2021 um 08:59 +0200 schrieb Martin Kepplinger:

Quoted 83 lines

Am Mittwoch, dem 06.10.2021 um 14:54 -0700 schrieb Bart Van Assche:

Quoted 73 lines

The following query shows which drivers define callbacks that are called
by the power management support code in the SCSI core (scsi_pm.c):

$ git grep -nHEwA16 "$(echo $(git grep -h 'scsi_register_driver(&'
|
      sed 's/.*&//;s/\..*//') | sed 's/ /|/g')" |
    grep '\.pm[[:blank:]]*=[[:blank:]]'
drivers/scsi/sd.c-620-          .pm             = &sd_pm_ops,
drivers/scsi/sr.c-100-          .pm             = &sr_pm_ops,
drivers/scsi/ufs/ufshcd.c-9765-         .pm = &ufshcd_wl_pm_ops,

Since unconditionally runtime resuming a device during system resume
is
not necessary, remove that code. Modify the SCSI disk (sd) driver such
that it follows the same approach as the UFS driver, namely to skip system suspend and resume for devices that are runtime suspended. The
CD-ROM code does not need to be updated since its PM callbacks do not
affect the device power state.

This patch has been tested as follows:

[ shell 1 ]

cd /sys/kernel/debug/tracing
grep -E 'blk_(pre|post)_runtime|runtime_(suspend|resume)|autosuspend_delay| pm
_runtime_(get|put)' available_filter_functions |
  while read a b; do echo "$a"; done |
  grep -v __pm_runtime_resume >set_ftrace_filter
echo function > current_tracer
echo 1 > tracing_on
cat trace_pipe

[ shell 2 ]

cd /sys/block/sr0
 # Increase the event poll interval to make it easier to derive from
the

 # tracing output whether runtime power actions are the result of
sg_inq.
echo 30000 > events_poll_msecs
cd device/power
 # Enable runtime power management.
echo auto > control
echo 1000 > autosuspend_delay_ms
sleep 1
 # Verify in shell 1 that sr0 has been runtime suspended
sg_inq /dev/sr0
eject /dev/sr0
sg_inq /dev/sr0
 # Disable runtime power management.
echo on > control

cd /sys/block/sda/device/power
echo auto > control
echo 1000 > autosuspend_delay_ms
sleep 1
 # Verify in shell 1 that sr0 has been runtime suspended sg_inq /dev/sda

Cc Alan Stern <[email protected]>
Cc Dan Williams <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Martin Kepplinger <[email protected]>
Quoted 7 lines

---
 drivers/scsi/scsi_pm.c | 69 +++++++++----------------------------- --
--
 drivers/scsi/sd.c      |  6 ++++

 2 files changed, 20 insertions(+), 55 deletions(-)
Quoted 7 lines

on my system with a runtime-suspend enabled sd cardreader.

thanks,
                             martin

Hi Bart,

I didn't get CC'd on the cover letter and just saw it. Code-wise I think this makes sense, but what I tested was runtime-pm behaviour, not (yet) together with system suspend. Just so you know.

thank you for improving this!

martin

#6 lore raw

Re: [PATCH 3/3] scsi: pm: Only runtime resume if necessary

In reply to Bart Van Assche
Quoted 10 lines · Bart Van Assche

The following query shows which drivers define callbacks that are called by the power management support code in the SCSI core (scsi_pm.c):

$ git grep -nHEwA16 "$(echo $(git grep -h 'scsi_register_driver(&' |
      sed 's/.*&//;s/\..*//') | sed 's/ /|/g')" |
    grep '\.pm[[:blank:]]*=[[:blank:]]'
drivers/scsi/sd.c-620-		.pm		= &sd_pm_ops,
drivers/scsi/sr.c-100-		.pm		= &sr_pm_ops,
drivers/scsi/ufs/ufshcd.c-9765-		.pm = &ufshcd_wl_pm_ops,
Quoted 6 lines

Since unconditionally runtime resuming a device during system resume is not necessary, remove that code. Modify the SCSI disk (sd) driver such that it follows the same approach as the UFS driver, namely to skip system suspend and resume for devices that are runtime suspended. The CD-ROM code does not need to be updated since its PM callbacks do not affect the device power state.

You may already be aware of this, but in case you aren't...

The PM core already contains some provisions for handling these kinds of things. They are described in Documentation/driver-api/pm/devices.rst. See particularly the parts relating to the DPM_FLAG_NO_DIRECT_COMPLETE, DPM_FLAG_SMART_PREPARE, DPM_FLAG_SMART_SUSPEND, DPM_FLAG_MAY_SKIP_RESUME, and power.direct_complete flags.

A follow-up patch after this series might be able to take advantage of these facilities.

Alan Stern

#7 lore raw

Re: [PATCH 3/3] scsi: pm: Only runtime resume if necessary

Quoted 27 lines · Alan Stern
Quoted 10 lines · Bart Van Assche

The following query shows which drivers define callbacks that are called by the power management support code in the SCSI core (scsi_pm.c):

$ git grep -nHEwA16 "$(echo $(git grep -h 'scsi_register_driver(&' |
       sed 's/.*&//;s/\..*//') | sed 's/ /|/g')" |
     grep '\.pm[[:blank:]]*=[[:blank:]]'
drivers/scsi/sd.c-620-		.pm		= &sd_pm_ops,
drivers/scsi/sr.c-100-		.pm		= &sr_pm_ops,
drivers/scsi/ufs/ufshcd.c-9765-		.pm = &ufshcd_wl_pm_ops,
Quoted 6 lines

Since unconditionally runtime resuming a device during system resume is not necessary, remove that code. Modify the SCSI disk (sd) driver such that it follows the same approach as the UFS driver, namely to skip system suspend and resume for devices that are runtime suspended. The CD-ROM code does not need to be updated since its PM callbacks do not affect the device power state.

You may already be aware of this, but in case you aren't...

The PM core already contains some provisions for handling these kinds of things. They are described in Documentation/driver-api/pm/devices.rst. See particularly the parts relating to the DPM_FLAG_NO_DIRECT_COMPLETE, DPM_FLAG_SMART_PREPARE, DPM_FLAG_SMART_SUSPEND, DPM_FLAG_MAY_SKIP_RESUME, and power.direct_complete flags.

Quoted 2 lines

A follow-up patch after this series might be able to take advantage of these facilities.

Hi Alan,

Thanks for the feedback. I was not yet aware of these flags. Since using these flags would make patch 3/3 only a few lines shorter and would make it harder to review for anyone who is not familiar with the DPM flags, I'm considering to keep patch 3/3 as is (this means not using the DPM flags).

Thanks,

Bart.

#8 lore raw

Re: [PATCH 3/3] scsi: pm: Only runtime resume if necessary

Quoted 31 lines · Bart Van Assche
Quoted 27 lines · Alan Stern
Quoted 10 lines · Bart Van Assche

The following query shows which drivers define callbacks that are called by the power management support code in the SCSI core (scsi_pm.c):

$ git grep -nHEwA16 "$(echo $(git grep -h 'scsi_register_driver(&' |
       sed 's/.*&//;s/\..*//') | sed 's/ /|/g')" |
     grep '\.pm[[:blank:]]*=[[:blank:]]'
drivers/scsi/sd.c-620-		.pm		= &sd_pm_ops,
drivers/scsi/sr.c-100-		.pm		= &sr_pm_ops,
drivers/scsi/ufs/ufshcd.c-9765-		.pm = &ufshcd_wl_pm_ops,
Quoted 6 lines

Since unconditionally runtime resuming a device during system resume is not necessary, remove that code. Modify the SCSI disk (sd) driver such that it follows the same approach as the UFS driver, namely to skip system suspend and resume for devices that are runtime suspended. The CD-ROM code does not need to be updated since its PM callbacks do not affect the device power state.

You may already be aware of this, but in case you aren't...

The PM core already contains some provisions for handling these kinds of things. They are described in Documentation/driver-api/pm/devices.rst. See particularly the parts relating to the DPM_FLAG_NO_DIRECT_COMPLETE, DPM_FLAG_SMART_PREPARE, DPM_FLAG_SMART_SUSPEND, DPM_FLAG_MAY_SKIP_RESUME, and power.direct_complete flags.

Quoted 2 lines

A follow-up patch after this series might be able to take advantage of these facilities.

Quoted 6 lines

Hi Alan,

Thanks for the feedback. I was not yet aware of these flags. Since using these flags would make patch 3/3 only a few lines shorter and would make it harder to review for anyone who is not familiar with the DPM flags, I'm considering to keep patch 3/3 as is (this means not using the DPM flags).

Sure. That's why I suggested doing it as a follow-up patch.

But if you don't want to write one, that's understandable. Sometimes I think the multitude of PM flags is somewhat over-engineered. (Even though I'm guilty of adding to it...)

Note, however, that this works for SCSI only because SCSI devices don't have power-manageable children. If they did, you could run into a situation where the child device wanted to go back to full power during a system resume, but was unable to do so because its parent SCSI device was still in runtime suspend. That's the sort of potential problem the PM flags were meant to address. (In fact, you might want to add comments to this effect in the sd and UFS drivers' resume routines.)

Alan Stern

#9 lore raw
In reply to Bart Van Assche

Bart,

Quoted 5 lines

For the UFS driver it is undesired that the SCSI power management core activates runtime suspended devices during system resume. This patch series leaves SCSI devices runtime suspended during system resume if these were runtime suspended before the system was suspended. Please consider this patch series for Linux kernel v5.16.

Applied to 5.16/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#10 lore raw
In reply to Bart Van Assche
Quoted 9 lines · Bart Van Assche

For the UFS driver it is undesired that the SCSI power management core activates runtime suspended devices during system resume. This patch series leaves SCSI devices runtime suspended during system resume if these were runtime suspended before the system was suspended. Please consider this patch series for Linux kernel v5.16.

Thanks,

[...]

Applied to 5.16/scsi-queue, thanks!

[1/3] scsi: pm: Rely on the device driver core for async power management

      https://git.kernel.org/mkp/scsi/c/a19a93e4c6a9
[2/3] scsi: sd: Rename sd_resume() into sd_resume_system()
      https://git.kernel.org/mkp/scsi/c/1c9575326a4a
[3/3] scsi: pm: Only runtime resume if necessary
      https://git.kernel.org/mkp/scsi/c/9131bff6a9f1
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering