Skip to contentSkip to conversation

scsi: sd: Introduce manage_shutdown device flag

PATCH Superseded Damien Le Moal 4 participants 9 messages Oct 25, 2023 – Oct 27, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Damien Le Moal

author maintainer
#0 lore raw

PATCH 1/1 scsi: sd: Introduce manage_shutdown device flag

Commit aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop") change setting the manage_system_start_stop flag to false for libata managed disks to enable libata internal management of disk suspend/resume. However, a side effect of this change is that on system shutdown, disks are no longer being stopped (set to standby mode with the heads unloaded). While this is not a critical issue, this unclean shutdown is not recommended and shows up with increased smart counters (e.g. the unexpected power loss counter "Unexpect_Power_Loss_Ct").

Instead of defining a shutdown driver method for all ATA adapter drivers (not all of them define that operation), this patch resolves this issue by further refining the sd driver start/stop control of disks using the new flag manage_shutdown. If set to true, the function sd_shutdown() will issue a START STOP UNIT command with the start argument set to 0 when a disk is shutdown on system power off (system_state == SYSTEM_POWER_OFF).

Fixes aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop")
---
 drivers/ata/libata-scsi.c  | 5 +++--
 drivers/firewire/sbp2.c    | 1 +
 drivers/scsi/sd.c          | 6 ++++--
 include/scsi/scsi_device.h | 1 +
 4 files changed, 9 insertions(+), 4 deletions(-)
drivers/ata/libata-scsi.c +3 -2
index a371b497035e..3a957c4da409 100644
@@ -1053,10 +1053,11 @@ int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)
 
 		/*
 		 * Ask the sd driver to issue START STOP UNIT on runtime suspend
-		 * and resume only. For system level suspend/resume, devices
-		 * power state is handled directly by libata EH.
+		 * and resume and shutdown only. For system level suspend/resume,
+		 * devices power state is handled directly by libata EH.
 		 */
 		sdev->manage_runtime_start_stop = true;
+		sdev->manage_shutdown = true;
 	}
 
 	/*
drivers/firewire/sbp2.c +1 -0
index 749868b9e80d..7edf2c95282f 100644
@@ -1521,6 +1521,7 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
 	if (sbp2_param_exclusive_login) {
 		sdev->manage_system_start_stop = true;
 		sdev->manage_runtime_start_stop = true;
+		sdev->manage_shutdown = true;
 	}
 
 	if (sdev->type == TYPE_ROM)
drivers/scsi/sd.c +4 -2
index 83b6a3f3863b..52fa266d976f 100644
@@ -3819,8 +3819,10 @@ static void sd_shutdown(struct device *dev)
 		sd_sync_cache(sdkp, NULL);
 	}
 
-	if (system_state != SYSTEM_RESTART &&
-	    sdkp->device->manage_system_start_stop) {
+	if ((system_state != SYSTEM_RESTART &&
+	     sdkp->device->manage_system_start_stop) ||
+	    (system_state == SYSTEM_POWER_OFF &&
+	     sdkp->device->manage_shutdown)) {
 		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
 		sd_start_stop_device(sdkp, 0);
 	}
include/scsi/scsi_device.h +1 -0
index fd41fdac0a8e..7edefb73bf69 100644
@@ -164,6 +164,7 @@ struct scsi_device {
 
 	bool manage_system_start_stop; /* Let HLD (sd) manage system start/stop */
 	bool manage_runtime_start_stop; /* Let HLD (sd) manage runtime start/stop */
+	bool manage_shutdown;	/* Let HLD (sd) manage shutdown */
 
 	unsigned removable:1;
 	unsigned changed:1;	/* Data invalid due to media change */
Signature · 1 line
2.41.0
author maintainer
#1 lore raw
Quoted 18 lines · Damien Le Moal

Commit aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop") change setting the manage_system_start_stop flag to false for libata managed disks to enable libata internal management of disk suspend/resume. However, a side effect of this change is that on system shutdown, disks are no longer being stopped (set to standby mode with the heads unloaded). While this is not a critical issue, this unclean shutdown is not recommended and shows up with increased smart counters (e.g. the unexpected power loss counter "Unexpect_Power_Loss_Ct").

Instead of defining a shutdown driver method for all ATA adapter drivers (not all of them define that operation), this patch resolves this issue by further refining the sd driver start/stop control of disks using the new flag manage_shutdown. If set to true, the function sd_shutdown() will issue a START STOP UNIT command with the start argument set to 0 when a disk is shutdown on system power off (system_state == SYSTEM_POWER_OFF).

Quoted 5 lines
Fixes aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop")

Martin,

I can take this patch through libata fixes branch if you prefer (as I suspect it may not apply to your tree without rebasing first).

Quoted 66 lines
---
 drivers/ata/libata-scsi.c  | 5 +++--
 drivers/firewire/sbp2.c    | 1 +
 drivers/scsi/sd.c          | 6 ++++--
 include/scsi/scsi_device.h | 1 +
 4 files changed, 9 insertions(+), 4 deletions(-)
drivers/ata/libata-scsi.c +3 -2
index a371b497035e..3a957c4da409 100644
@@ -1053,10 +1053,11 @@ int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)
 
 		/*
 		 * Ask the sd driver to issue START STOP UNIT on runtime suspend
-		 * and resume only. For system level suspend/resume, devices
-		 * power state is handled directly by libata EH.
+		 * and resume and shutdown only. For system level suspend/resume,
+		 * devices power state is handled directly by libata EH.
 		 */
 		sdev->manage_runtime_start_stop = true;
+		sdev->manage_shutdown = true;
 	}
 
 	/*
drivers/firewire/sbp2.c +1 -0
index 749868b9e80d..7edf2c95282f 100644
@@ -1521,6 +1521,7 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
 	if (sbp2_param_exclusive_login) {
 		sdev->manage_system_start_stop = true;
 		sdev->manage_runtime_start_stop = true;
+		sdev->manage_shutdown = true;
 	}
 
 	if (sdev->type == TYPE_ROM)
drivers/scsi/sd.c +4 -2
index 83b6a3f3863b..52fa266d976f 100644
@@ -3819,8 +3819,10 @@ static void sd_shutdown(struct device *dev)
 		sd_sync_cache(sdkp, NULL);
 	}
 
-	if (system_state != SYSTEM_RESTART &&
-	    sdkp->device->manage_system_start_stop) {
+	if ((system_state != SYSTEM_RESTART &&
+	     sdkp->device->manage_system_start_stop) ||
+	    (system_state == SYSTEM_POWER_OFF &&
+	     sdkp->device->manage_shutdown)) {
 		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
 		sd_start_stop_device(sdkp, 0);
 	}
include/scsi/scsi_device.h +1 -0
index fd41fdac0a8e..7edefb73bf69 100644
@@ -164,6 +164,7 @@ struct scsi_device {
 
 	bool manage_system_start_stop; /* Let HLD (sd) manage system start/stop */
 	bool manage_runtime_start_stop; /* Let HLD (sd) manage runtime start/stop */
+	bool manage_shutdown;	/* Let HLD (sd) manage shutdown */
 
 	unsigned removable:1;
 	unsigned changed:1;	/* Data invalid due to media change */
Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#2 lore raw
In reply to Damien Le Moal
Quoted 9 lines · Damien Le Moal

+++ b/include/scsi/scsi_device.h

(quoted diff) +0 -0
@@ -164,6 +164,7 @@ struct scsi_device {


        bool manage_system_start_stop; /* Let HLD (sd) manage system start/stop */
        bool manage_runtime_start_stop; /* Let HLD (sd) manage runtime start/stop */
+       bool manage_shutdown;   /* Let HLD (sd) manage shutdown */

I think at least 85% of the world gets confused about the difference between runtime/system start/stop and shutdown. Could we at least point to a doc explaining it in a comment here?

James

author maintainer
#3 lore raw
Quoted 14 lines · James Bottomley
Quoted 9 lines · Damien Le Moal

+++ b/include/scsi/scsi_device.h

(quoted diff) +0 -0
@@ -164,6 +164,7 @@ struct scsi_device {


        bool manage_system_start_stop; /* Let HLD (sd) manage system start/stop */
        bool manage_runtime_start_stop; /* Let HLD (sd) manage runtime start/stop */
+       bool manage_shutdown;   /* Let HLD (sd) manage shutdown */

I think at least 85% of the world gets confused about the difference between runtime/system start/stop and shutdown. Could we at least point to a doc explaining it in a comment here?

Would improving the comments here be enough ? E.g. something like:

	/* Let the HLD (sd) manage system suspend (start) and resume (stop).
	 * This applies to both suspend to RAM and suspend to disk
	 * (hybernation).
	 */
	bool manage_system_start_stop;

	/*
	 * Let the HLD (sd) manage device runtime suspend (stop) and
	 * resume (start).
	 */
	bool manage_runtime_start_stop;

	/* Let the HLD (sd) manage system power-off (shutdown) */
	bool manage_shutdown;
Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#4 lore raw
Quoted 28 lines · Damien Le Moal
Quoted 17 lines · James Bottomley
Quoted 11 lines · Damien Le Moal

+++ b/include/scsi/scsi_device.h

(quoted diff) +0 -0
@@ -164,6 +164,7 @@ struct scsi_device {


        bool manage_system_start_stop; /* Let HLD (sd) manage system
start/stop */
        bool manage_runtime_start_stop; /* Let HLD (sd) manage runtime start/stop */
+       bool manage_shutdown;   /* Let HLD (sd) manage shutdown */

I think at least 85% of the world gets confused about the difference
between runtime/system start/stop and shutdown.  Could we at least point to a doc explaining it in a comment here?

Would improving the comments here be enough ? E.g. something like:

        /* Let the HLD (sd) manage system suspend (start) and resume (stop).
         * This applies to both suspend to RAM and suspend to disk
         * (hybernation).
         */
        bool manage_system_start_stop;

Quoted 8 lines

        /*
         * Let the HLD (sd) manage device runtime suspend (stop) and
         * resume (start).
         */
        bool manage_runtime_start_stop;

        /* Let the HLD (sd) manage system power-off (shutdown) */ bool manage_shutdown;

Heh, well, I was going to say we should still point to the doc, but I simply can't find it, so the above is perhaps the best we can do, thanks!

James

#5 lore raw
Quoted 3 lines · James Bottomley

Heh, well, I was going to say we should still point to the doc, but I simply can't find it, so the above is perhaps the best we can do, thanks!

I think this should be documented in the Documentation/power directory. After having taken another look at that directory, I see that there is only detailed documentation and no overview documentation. Maybe I overlooked something but I couldn't find an explanation of the system suspend/resume nor of the runtime power management concepts in that directory. My understanding is that system suspend/resume is about system-wide power state changes (hibernation and suspend-to-RAM) and also that runtime power management is about changing the power state of a single device or bus if no activity has happened within a certain time.

Bart.

author maintainer
#6 lore raw
Quoted 5 lines · Bart Van Assche
Quoted 3 lines · James Bottomley

Heh, well, I was going to say we should still point to the doc, but I simply can't find it, so the above is perhaps the best we can do, thanks!

Quoted 10 lines

I think this should be documented in the Documentation/power directory. After having taken another look at that directory, I see that there is only detailed documentation and no overview documentation. Maybe I overlooked something but I couldn't find an explanation of the system suspend/resume nor of the runtime power management concepts in that directory. My understanding is that system suspend/resume is about system-wide power state changes (hibernation and suspend-to-RAM) and also that runtime power management is about changing the power state of a single device or bus if no activity has happened within a certain time.

I actually thought that James wanted a reference to scsi sysfs attributes documentation, which is also not in the best of shape, to say the least...

In any case, I would like to push this fix for 6.6-final as this is a tracked regression. Martin, James, are you OK with this patch ?

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#7 lore raw
Quoted 17 lines · Damien Le Moal
Quoted 5 lines · Bart Van Assche
Quoted 3 lines · James Bottomley

Heh, well, I was going to say we should still point to the doc, but I simply can't find it, so the above is perhaps the best we can do, thanks!

Quoted 10 lines

I think this should be documented in the Documentation/power directory. After having taken another look at that directory, I see that there is only detailed documentation and no overview documentation. Maybe I overlooked something but I couldn't find an explanation of the system suspend/resume nor of the runtime power management concepts in that directory. My understanding is that system suspend/resume is about system-wide power state changes (hibernation and suspend-to-RAM) and also that runtime power management is about changing the power state of a single device or bus if no activity has happened within a certain time.

Quoted 6 lines

I actually thought that James wanted a reference to scsi sysfs attributes documentation, which is also not in the best of shape, to say the least...

In any case, I would like to push this fix for 6.6-final as this is a tracked regression. Martin, James, are you OK with this patch ?

Yes, works for me, you can add my Reviewed-by.

James