Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: core: Fix the unit attention counter implementation
@ 2025-10-14 22:02 Bart Van Assche
  2025-10-15 16:59 ` Ewan Milne
  2025-10-22  2:28 ` Martin K. Petersen
  0 siblings, 2 replies; 3+ messages in thread
From: Bart Van Assche @ 2025-10-14 22:02 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Kai Mäkisara,
	James E.J. Bottomley, John Meneghini

scsi_decide_disposition() may call scsi_check_sense().
scsi_decide_disposition() calls are not serialized. Hence, counter
updates by scsi_check_sense() must be serialized. Hence this patch that
makes the counters updated by scsi_check_sense() atomic.

Cc: Kai Mäkisara <Kai.Makisara@kolumbus.fi>
Fixes: a5d518cd4e3e ("scsi: core: Add counters for New Media and Power On/Reset UNIT ATTENTIONs")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/scsi_error.c  |  4 ++--
 include/scsi/scsi_device.h | 10 ++++------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/drivers/scsi/scsi_error.c b/drivers/scsi/scsi_error.c
index 746ff6a1f309..1c13812a3f03 100644
--- a/drivers/scsi/scsi_error.c
+++ b/drivers/scsi/scsi_error.c
@@ -554,9 +554,9 @@ enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
 		 * happened, even if someone else gets the sense data.
 		 */
 		if (sshdr.asc == 0x28)
-			scmd->device->ua_new_media_ctr++;
+			atomic_inc(&sdev->ua_new_media_ctr);
 		else if (sshdr.asc == 0x29)
-			scmd->device->ua_por_ctr++;
+			atomic_inc(&sdev->ua_por_ctr);
 	}
 
 	if (scsi_sense_is_deferred(&sshdr))
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 4c106342c4ae..3c6c8e3995c3 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -252,8 +252,8 @@ struct scsi_device {
 	unsigned int queue_stopped;	/* request queue is quiesced */
 	bool offline_already;		/* Device offline message logged */
 
-	unsigned int ua_new_media_ctr;	/* Counter for New Media UNIT ATTENTIONs */
-	unsigned int ua_por_ctr;	/* Counter for Power On / Reset UAs */
+	atomic_t ua_new_media_ctr;	/* Counter for New Media UNIT ATTENTIONs */
+	atomic_t ua_por_ctr;		/* Counter for Power On / Reset UAs */
 
 	atomic_t disk_events_disable_depth; /* disable depth for disk events */
 
@@ -693,10 +693,8 @@ static inline int scsi_device_busy(struct scsi_device *sdev)
 }
 
 /* Macros to access the UNIT ATTENTION counters */
-#define scsi_get_ua_new_media_ctr(sdev) \
-	((const unsigned int)(sdev->ua_new_media_ctr))
-#define scsi_get_ua_por_ctr(sdev) \
-	((const unsigned int)(sdev->ua_por_ctr))
+#define scsi_get_ua_new_media_ctr(sdev)	atomic_read(&sdev->ua_new_media_ctr)
+#define scsi_get_ua_por_ctr(sdev)	atomic_read(&sdev->ua_por_ctr)
 
 #define MODULE_ALIAS_SCSI_DEVICE(type) \
 	MODULE_ALIAS("scsi:t-" __stringify(type) "*")

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

* Re: [PATCH] scsi: core: Fix the unit attention counter implementation
  2025-10-14 22:02 [PATCH] scsi: core: Fix the unit attention counter implementation Bart Van Assche
@ 2025-10-15 16:59 ` Ewan Milne
  2025-10-22  2:28 ` Martin K. Petersen
  1 sibling, 0 replies; 3+ messages in thread
From: Ewan Milne @ 2025-10-15 16:59 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: Martin K . Petersen, linux-scsi, Kai Mäkisara,
	James E.J. Bottomley, John Meneghini

On Tue, Oct 14, 2025 at 6:03 PM Bart Van Assche <bvanassche@acm.org> wrote:
>
> scsi_decide_disposition() may call scsi_check_sense().
> scsi_decide_disposition() calls are not serialized. Hence, counter
> updates by scsi_check_sense() must be serialized. Hence this patch that
> makes the counters updated by scsi_check_sense() atomic.
>
> Cc: Kai Mäkisara <Kai.Makisara@kolumbus.fi>
> Fixes: a5d518cd4e3e ("scsi: core: Add counters for New Media and Power On/Reset UNIT ATTENTIONs")
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>  drivers/scsi/scsi_error.c  |  4 ++--
>  include/scsi/scsi_device.h | 10 ++++------
>  2 files changed, 6 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/scsi/scsi_error.c b/drivers/scsi/scsi_error.c
> index 746ff6a1f309..1c13812a3f03 100644
> --- a/drivers/scsi/scsi_error.c
> +++ b/drivers/scsi/scsi_error.c
> @@ -554,9 +554,9 @@ enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
>                  * happened, even if someone else gets the sense data.
>                  */
>                 if (sshdr.asc == 0x28)
> -                       scmd->device->ua_new_media_ctr++;
> +                       atomic_inc(&sdev->ua_new_media_ctr);
>                 else if (sshdr.asc == 0x29)
> -                       scmd->device->ua_por_ctr++;
> +                       atomic_inc(&sdev->ua_por_ctr);
>         }
>
>         if (scsi_sense_is_deferred(&sshdr))
> diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
> index 4c106342c4ae..3c6c8e3995c3 100644
> --- a/include/scsi/scsi_device.h
> +++ b/include/scsi/scsi_device.h
> @@ -252,8 +252,8 @@ struct scsi_device {
>         unsigned int queue_stopped;     /* request queue is quiesced */
>         bool offline_already;           /* Device offline message logged */
>
> -       unsigned int ua_new_media_ctr;  /* Counter for New Media UNIT ATTENTIONs */
> -       unsigned int ua_por_ctr;        /* Counter for Power On / Reset UAs */
> +       atomic_t ua_new_media_ctr;      /* Counter for New Media UNIT ATTENTIONs */
> +       atomic_t ua_por_ctr;            /* Counter for Power On / Reset UAs */
>
>         atomic_t disk_events_disable_depth; /* disable depth for disk events */
>
> @@ -693,10 +693,8 @@ static inline int scsi_device_busy(struct scsi_device *sdev)
>  }
>
>  /* Macros to access the UNIT ATTENTION counters */
> -#define scsi_get_ua_new_media_ctr(sdev) \
> -       ((const unsigned int)(sdev->ua_new_media_ctr))
> -#define scsi_get_ua_por_ctr(sdev) \
> -       ((const unsigned int)(sdev->ua_por_ctr))
> +#define scsi_get_ua_new_media_ctr(sdev)        atomic_read(&sdev->ua_new_media_ctr)
> +#define scsi_get_ua_por_ctr(sdev)      atomic_read(&sdev->ua_por_ctr)
>
>  #define MODULE_ALIAS_SCSI_DEVICE(type) \
>         MODULE_ALIAS("scsi:t-" __stringify(type) "*")
>

Reviewed-by: Ewan D. Milne <emilne@redhat.com>


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

* Re: [PATCH] scsi: core: Fix the unit attention counter implementation
  2025-10-14 22:02 [PATCH] scsi: core: Fix the unit attention counter implementation Bart Van Assche
  2025-10-15 16:59 ` Ewan Milne
@ 2025-10-22  2:28 ` Martin K. Petersen
  1 sibling, 0 replies; 3+ messages in thread
From: Martin K. Petersen @ 2025-10-22  2:28 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: Martin K . Petersen, linux-scsi, Kai Mäkisara,
	James E.J. Bottomley, John Meneghini


Bart,

> scsi_decide_disposition() may call scsi_check_sense().
> scsi_decide_disposition() calls are not serialized. Hence, counter
> updates by scsi_check_sense() must be serialized. Hence this patch
> that makes the counters updated by scsi_check_sense() atomic.

Applied to 6.18/scsi-fixes, thanks!

-- 
Martin K. Petersen

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

end of thread, other threads:[~2025-10-22  2:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-14 22:02 [PATCH] scsi: core: Fix the unit attention counter implementation Bart Van Assche
2025-10-15 16:59 ` Ewan Milne
2025-10-22  2:28 ` Martin K. Petersen

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