diff mbox series

scsi: sd: Introduce manage_shutdown device flag

Message ID 20231025070117.464903-1-dlemoal@kernel.org (mailing list archive)
State Superseded
Headers show
Series scsi: sd: Introduce manage_shutdown device flag | expand

Commit Message

Damien Le Moal Oct. 25, 2023, 7:01 a.m. UTC
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")
Cc: stable@vger.kernel.org
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218038
Link: https://lore.kernel.org/all/cd397c88-bf53-4768-9ab8-9d107df9e613@gmail.com/
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
---
 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(-)

Comments

Damien Le Moal Oct. 25, 2023, 7:14 a.m. UTC | #1
On 10/25/23 16:01, Damien Le Moal wrote:
> 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")
> Cc: stable@vger.kernel.org
> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218038
> Link: https://lore.kernel.org/all/cd397c88-bf53-4768-9ab8-9d107df9e613@gmail.com/
> Signed-off-by: Damien Le Moal <dlemoal@kernel.org>

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

> ---
>  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(-)
> 
> diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
> index a371b497035e..3a957c4da409 100644
> --- a/drivers/ata/libata-scsi.c
> +++ b/drivers/ata/libata-scsi.c
> @@ -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;
>  	}
>  
>  	/*
> diff --git a/drivers/firewire/sbp2.c b/drivers/firewire/sbp2.c
> index 749868b9e80d..7edf2c95282f 100644
> --- a/drivers/firewire/sbp2.c
> +++ b/drivers/firewire/sbp2.c
> @@ -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)
> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index 83b6a3f3863b..52fa266d976f 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
> @@ -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);
>  	}
> diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
> index fd41fdac0a8e..7edefb73bf69 100644
> --- a/include/scsi/scsi_device.h
> +++ b/include/scsi/scsi_device.h
> @@ -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 */
James Bottomley Oct. 25, 2023, 11:57 a.m. UTC | #2
On Wed, 2023-10-25 at 16:01 +0900, Damien Le Moal wrote:
> +++ b/include/scsi/scsi_device.h
> @@ -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
Damien Le Moal Oct. 25, 2023, 9:30 p.m. UTC | #3
On 10/25/23 20:57, James Bottomley wrote:
> On Wed, 2023-10-25 at 16:01 +0900, Damien Le Moal wrote:
>> +++ b/include/scsi/scsi_device.h
>> @@ -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;
James Bottomley Oct. 26, 2023, 12:01 p.m. UTC | #4
On Thu, 2023-10-26 at 06:30 +0900, Damien Le Moal wrote:
> On 10/25/23 20:57, James Bottomley wrote:
> > On Wed, 2023-10-25 at 16:01 +0900, Damien Le Moal wrote:
> > > +++ b/include/scsi/scsi_device.h
> > > @@ -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;

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
Bart Van Assche Oct. 26, 2023, 9:36 p.m. UTC | #5
On 10/26/23 05:01, James Bottomley wrote:
> 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.
Damien Le Moal Oct. 27, 2023, 12:26 a.m. UTC | #6
On 10/27/23 06:36, Bart Van Assche wrote:
> On 10/26/23 05:01, James Bottomley wrote:
>> 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.

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 ?
James Bottomley Oct. 27, 2023, 12:37 a.m. UTC | #7
On Fri, 2023-10-27 at 09:26 +0900, Damien Le Moal wrote:
> On 10/27/23 06:36, Bart Van Assche wrote:
> > On 10/26/23 05:01, James Bottomley wrote:
> > > 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.
> 
> 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
Martin K. Petersen Oct. 27, 2023, 12:57 a.m. UTC | #8
Damien,

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

Yep.

Acked-by: Martin K. Petersen <martin.petersen@oracle.com>
diff mbox series

Patch

diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index a371b497035e..3a957c4da409 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -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;
 	}
 
 	/*
diff --git a/drivers/firewire/sbp2.c b/drivers/firewire/sbp2.c
index 749868b9e80d..7edf2c95282f 100644
--- a/drivers/firewire/sbp2.c
+++ b/drivers/firewire/sbp2.c
@@ -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)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 83b6a3f3863b..52fa266d976f 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -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);
 	}
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index fd41fdac0a8e..7edefb73bf69 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -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 */