Skip to contentSkip to conversation

Improve const and reserved command handling

PATCH Applied Bart Van Assche 3 participants 16 messages Jun 24, 2025 – Jul 22, 2025 SCSI

lore
Share & tools
lore
#0 lore raw

cover Improve const and reserved command handling

Hi Martin,

This patch series includes three cleanup patches for the SCSI core that do not modify any functionality. Please consider these patches for the next merge window.

Thanks,

Bart.

Bart Van Assche (3):
scsi: core: Make scsi_cmd_to_rq() accept const arguments
scsi: core: Make scsi_cmd_priv() accept const arguments
scsi: core: Use scsi_cmd_priv() instead of open-coding it

 drivers/scsi/scsi_lib.c     |  2 +-
 drivers/scsi/scsi_logging.c | 10 +++++-----
 include/scsi/scsi_cmnd.h    | 17 +++++++++--------
 3 files changed, 15 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/3 scsi: core: Make scsi_cmd_to_rq() accept const arguments

Instead of requiring the caller to cast away constness, make scsi_cmd_to_rq() accept const arguments.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_logging.c | 10 +++++-----
 include/scsi/scsi_cmnd.h    |  9 +++++----
 2 files changed, 10 insertions(+), 9 deletions(-)
drivers/scsi/scsi_logging.c +5 -5
index b02af340c2d3..5aaff629b999 100644
@@ -28,7 +28,7 @@ static void scsi_log_release_buffer(char *bufptr)
 
 static inline const char *scmd_name(const struct scsi_cmnd *scmd)
 {
-	struct request *rq = scsi_cmd_to_rq((struct scsi_cmnd *)scmd);
+	const struct request *rq = scsi_cmd_to_rq(scmd);
 
 	if (!rq->q || !rq->q->disk)
 		return NULL;
@@ -94,7 +94,7 @@ void scmd_printk(const char *level, const struct scsi_cmnd *scmd,
 	if (!logbuf)
 		return;
 	off = sdev_format_header(logbuf, logbuf_len, scmd_name(scmd),
-				 scsi_cmd_to_rq((struct scsi_cmnd *)scmd)->tag);
+				 scsi_cmd_to_rq(scmd)->tag);
 	if (off < logbuf_len) {
 		va_start(args, fmt);
 		off += vscnprintf(logbuf + off, logbuf_len - off, fmt, args);
@@ -374,8 +374,8 @@ EXPORT_SYMBOL(__scsi_print_sense);
 void scsi_print_sense(const struct scsi_cmnd *cmd)
 {
 	scsi_log_print_sense(cmd->device, scmd_name(cmd),
-			     scsi_cmd_to_rq((struct scsi_cmnd *)cmd)->tag,
-			     cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
+			     scsi_cmd_to_rq(cmd)->tag, cmd->sense_buffer,
+			     SCSI_SENSE_BUFFERSIZE);
 }
 EXPORT_SYMBOL(scsi_print_sense);
 
@@ -393,7 +393,7 @@ void scsi_print_result(const struct scsi_cmnd *cmd, const char *msg,
 		return;
 
 	off = sdev_format_header(logbuf, logbuf_len, scmd_name(cmd),
-				 scsi_cmd_to_rq((struct scsi_cmnd *)cmd)->tag);
+				 scsi_cmd_to_rq(cmd)->tag);
 
 	if (off >= logbuf_len)
 		goto out_printk;
include/scsi/scsi_cmnd.h +5 -4
index 8ecfb94049db..154fbb39ca0c 100644
@@ -144,10 +144,11 @@ struct scsi_cmnd {
 };
 
 /* Variant of blk_mq_rq_from_pdu() that verifies the type of its argument. */
-static inline struct request *scsi_cmd_to_rq(struct scsi_cmnd *scmd)
-{
-	return blk_mq_rq_from_pdu(scmd);
-}
+#define scsi_cmd_to_rq(scmd)                                       \
+	_Generic(scmd,                                             \
+		const struct scsi_cmnd *: (const struct request *) \
+			blk_mq_rq_from_pdu((void *)scmd),          \
+		struct scsi_cmnd *: blk_mq_rq_from_pdu((void *)scmd))
 
 /*
  * Return the driver private allocation behind the command.
#2 lore raw

Re: [PATCH 1/3] scsi: core: Make scsi_cmd_to_rq() accept const arguments

drivers/scsi/scsi_logging.c

Quoted 12 lines · Bart Van Assche

Instead of requiring the caller to cast away constness, make scsi_cmd_to_rq() accept const arguments.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
  drivers/scsi/scsi_logging.c | 10 +++++-----
  include/scsi/scsi_cmnd.h    |  9 +++++----
  2 files changed, 10 insertions(+), 9 deletions(-)
drivers/scsi/scsi_logging.c +0 -0

Is there something special about the logging code that it even requires that const scsi_cmnd * be used?

Or will it be encouraged to use const scsi_cmnd * elsewhere in future (after this change)? Or, further than that, convert all scsi core code to use const scsi_cmnd * (when possible)?

Quoted 61 lines

index b02af340c2d3..5aaff629b999 100644

drivers/scsi/scsi_logging.c +5 -5
@@ -28,7 +28,7 @@ static void scsi_log_release_buffer(char *bufptr)
 
  static inline const char *scmd_name(const struct scsi_cmnd *scmd)
  {
-	struct request *rq = scsi_cmd_to_rq((struct scsi_cmnd *)scmd);
+	const struct request *rq = scsi_cmd_to_rq(scmd);
 
  	if (!rq->q || !rq->q->disk)
  		return NULL;
@@ -94,7 +94,7 @@ void scmd_printk(const char *level, const struct scsi_cmnd *scmd,
  	if (!logbuf)
  		return;
  	off = sdev_format_header(logbuf, logbuf_len, scmd_name(scmd),
-				 scsi_cmd_to_rq((struct scsi_cmnd *)scmd)->tag);
+				 scsi_cmd_to_rq(scmd)->tag);
  	if (off < logbuf_len) {
  		va_start(args, fmt);
  		off += vscnprintf(logbuf + off, logbuf_len - off, fmt, args);
@@ -374,8 +374,8 @@ EXPORT_SYMBOL(__scsi_print_sense);
  void scsi_print_sense(const struct scsi_cmnd *cmd)
  {
  	scsi_log_print_sense(cmd->device, scmd_name(cmd),
-			     scsi_cmd_to_rq((struct scsi_cmnd *)cmd)->tag,
-			     cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
+			     scsi_cmd_to_rq(cmd)->tag, cmd->sense_buffer,
+			     SCSI_SENSE_BUFFERSIZE);
  }
  EXPORT_SYMBOL(scsi_print_sense);
 
@@ -393,7 +393,7 @@ void scsi_print_result(const struct scsi_cmnd *cmd, const char *msg,
  		return;
 
  	off = sdev_format_header(logbuf, logbuf_len, scmd_name(cmd),
-				 scsi_cmd_to_rq((struct scsi_cmnd *)cmd)->tag);
+				 scsi_cmd_to_rq(cmd)->tag);
 
  	if (off >= logbuf_len)
  		goto out_printk;
include/scsi/scsi_cmnd.h +5 -4
index 8ecfb94049db..154fbb39ca0c 100644
@@ -144,10 +144,11 @@ struct scsi_cmnd {
  };
 
  /* Variant of blk_mq_rq_from_pdu() that verifies the type of its argument. */
-static inline struct request *scsi_cmd_to_rq(struct scsi_cmnd *scmd)
-{
-	return blk_mq_rq_from_pdu(scmd);
-}
+#define scsi_cmd_to_rq(scmd)                                       \
+	_Generic(scmd,                                             \
+		const struct scsi_cmnd *: (const struct request *) \
+			blk_mq_rq_from_pdu((void *)scmd),          \
+		struct scsi_cmnd *: blk_mq_rq_from_pdu((void *)scmd))
 
  /*
   * Return the driver private allocation behind the command.
#3 lore raw

Re: [PATCH 1/3] scsi: core: Make scsi_cmd_to_rq() accept const arguments

Quoted 1 line · John Garry

Is there something special about the logging code that it even requires that const scsi_cmnd * be used?

No. Declaring pointers 'const' helps to make the intention of code more clear to human readers.

Quoted 1 line

Or will it be encouraged to use const scsi_cmnd * elsewhere in future (after this change)? Or, further than that, convert all scsi core code to use const scsi_cmnd * (when possible)?

Many kernel developers don't care about declaring pointers 'const' even if these can be declared 'const'. Hence, a large scale change that changes struct scsi_cmnd pointer to const pointers would be considered controversial.

Thanks,

Bart.

#4 lore raw

Re: [PATCH 1/3] scsi: core: Make scsi_cmd_to_rq() accept const arguments

Quoted 11 lines · Bart Van Assche
Quoted 1 line · John Garry

Is there something special about the logging code that it even requires that const scsi_cmnd * be used?

No. Declaring pointers 'const' helps to make the intention of code more clear to human readers.

Quoted 1 line

Or will it be encouraged to use const scsi_cmnd * elsewhere in future (after this change)? Or, further than that, convert all scsi core code to use const scsi_cmnd * (when possible)?

Many kernel developers don't care about declaring pointers 'const' even if these can be declared 'const'. Hence, a large scale change that changes struct scsi_cmnd pointer to const pointers would be considered controversial.

I don't have a strong feeling on this change either way, but I'd be more inclined to get rid of the const usage in the logging code. But, again, I don't have a strong feeling either way.

#5 lore raw

Re: [PATCH 1/3] scsi: core: Make scsi_cmd_to_rq() accept const arguments

John,

Quoted 2 lines

I don't have a strong feeling on this change either way, but I'd be more inclined to get rid of the const usage in the logging code.

I agree. Don't really see the benefit.

Signature · 1 line
Martin K. Petersen
#6 lore raw

PATCH 2/3 scsi: core: Make scsi_cmd_priv() accept const arguments

In reply to Bart Van Assche

Instead of requiring the caller to cast away constness, make scsi_cmd_priv() accept const arguments.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 include/scsi/scsi_cmnd.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/scsi/scsi_cmnd.h +4 -4
index 154fbb39ca0c..09176b07e891 100644
@@ -154,10 +154,10 @@ struct scsi_cmnd {
  * Return the driver private allocation behind the command.
  * Only works if cmd_size is set in the host template.
  */
-static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
-{
-	return cmd + 1;
-}
+#define scsi_cmd_priv(cmd)                                         \
+	_Generic(cmd,                                              \
+		const struct scsi_cmnd *: (const void *)(cmd + 1), \
+		struct scsi_cmnd *: (void *)(cmd + 1))
 
 void scsi_done(struct scsi_cmnd *cmd);
 void scsi_done_direct(struct scsi_cmnd *cmd);
#7 lore raw

Re: [PATCH 2/3] scsi: core: Make scsi_cmd_priv() accept const arguments

Quoted 2 lines · Bart Van Assche

Instead of requiring the caller to cast away constness, make scsi_cmd_priv() accept const arguments.

Are there even instances where we are casting away const (for calling this function)? I assume not, since there are no changes to get rid of the casting away const.

Quoted 27 lines
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
  include/scsi/scsi_cmnd.h | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
include/scsi/scsi_cmnd.h +4 -4
index 154fbb39ca0c..09176b07e891 100644
@@ -154,10 +154,10 @@ struct scsi_cmnd {
   * Return the driver private allocation behind the command.
   * Only works if cmd_size is set in the host template.
   */
-static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
-{
-	return cmd + 1;
-}
+#define scsi_cmd_priv(cmd)                                         \
+	_Generic(cmd,                                              \
+		const struct scsi_cmnd *: (const void *)(cmd + 1), \
+		struct scsi_cmnd *: (void *)(cmd + 1))
 
  void scsi_done(struct scsi_cmnd *cmd);
  void scsi_done_direct(struct scsi_cmnd *cmd);
#8 lore raw

Re: [PATCH 2/3] scsi: core: Make scsi_cmd_priv() accept const arguments

Quoted 5 lines · John Garry
Quoted 2 lines · Bart Van Assche

Instead of requiring the caller to cast away constness, make scsi_cmd_priv() accept const arguments.

Are there even instances where we are casting away const (for calling this function)? I assume not, since there are no changes to get rid of the casting away const.

I have a patch pending that would need to cast away constness without this patch. See also
https://lore.kernel.org/linux-scsi/[email protected]/

Bart.

#9 lore raw

PATCH 3/3 scsi: core: Use scsi_cmd_priv() instead of open-coding it

In reply to Bart Van Assche

Improve code readability without modifying the behavior of the code.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_lib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi_lib.c +1 -1
index 144c72f0737a..0c65ecfedfbd 100644
@@ -1843,7 +1843,7 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
 	 * a function to initialize that data.
 	 */
 	if (shost->hostt->cmd_size && !shost->hostt->init_cmd_priv)
-		memset(cmd + 1, 0, shost->hostt->cmd_size);
+		memset(scsi_cmd_priv(cmd), 0, shost->hostt->cmd_size);
 
 	if (!(req->rq_flags & RQF_DONTPREP)) {
 		ret = scsi_prepare_cmd(req);
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] scsi: core: Use scsi_cmd_priv() instead of open-coding it

Quoted 5 lines · Bart Van Assche

Improve code readability without modifying the behavior of the code.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>

Do you even need to preceding patch for this change? cmd is not a pointer to const in this function AFAICS

Quoted 17 lines
---
  drivers/scsi/scsi_lib.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi_lib.c +1 -1
index 144c72f0737a..0c65ecfedfbd 100644
@@ -1843,7 +1843,7 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
  	 * a function to initialize that data.
  	 */
  	if (shost->hostt->cmd_size && !shost->hostt->init_cmd_priv)
-		memset(cmd + 1, 0, shost->hostt->cmd_size);
+		memset(scsi_cmd_priv(cmd), 0, shost->hostt->cmd_size);
 
  	if (!(req->rq_flags & RQF_DONTPREP)) {
  		ret = scsi_prepare_cmd(req);
#11 lore raw

Re: [PATCH 3/3] scsi: core: Use scsi_cmd_priv() instead of open-coding it

Quoted 1 line · John Garry

Do you even need to preceding patch for this change? cmd is not a pointer to const in this function AFAICS

Hi John,

This patch is unrelated to the previous patches. This patch series includes the cleanup patches from a larger series. I'm splitting up the larger series to make it easier to review the patches.

Thanks,

Bart.

#12 lore raw

Re: [PATCH 3/3] scsi: core: Use scsi_cmd_priv() instead of open-coding it

In reply to Bart Van Assche

Bart,

Quoted 1 line

Improve code readability without modifying the behavior of the code.

Applied to 6.17/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen
#13 lore raw
In reply to Bart Van Assche
Quoted 6 lines · Bart Van Assche

Hi Martin,

This patch series includes three cleanup patches for the SCSI core that do not modify any functionality. Please consider these patches for the next merge window.

Hi Bart,

I seem to remember seeing some of these patches before. Am I right?

If so, what's the history? Has anything changed (in this series)? Were RB tags dropped or not picked up?

Thanks,
John

Quoted 15 lines

Thanks,

Bart.

Bart Van Assche (3):
scsi: core: Make scsi_cmd_to_rq() accept const arguments
scsi: core: Make scsi_cmd_priv() accept const arguments
scsi: core: Use scsi_cmd_priv() instead of open-coding it

  drivers/scsi/scsi_lib.c     |  2 +-
  drivers/scsi/scsi_logging.c | 10 +++++-----
  include/scsi/scsi_cmnd.h    | 17 +++++++++--------
  3 files changed, 15 insertions(+), 14 deletions(-)
#14 lore raw
Quoted 3 lines · John Garry

I seem to remember seeing some of these patches before. Am I right?

If so, what's the history? Has anything changed (in this series)? Were RB tags dropped or not picked up?

Hi John,

Yes, these patches have been posted before. Last time nobody replied with "Reviewed-by:" on any of the patches in this series, hence no Reviewed-by tags in this patch series either. I'm splitting this patch
series because 24 patches is a bit much for a single patch series:
https://lore.kernel.org/linux-scsi/[email protected]/

Other than rebasing on top of Martin's latest for-next branch, no changes have been made in any of the three patches in this patch series.

Thanks,

Bart.

#15 lore raw

Re: (subset) [PATCH 0/3] Improve const and reserved command handling

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

This patch series includes three cleanup patches for the SCSI core that do not modify any functionality. Please consider these patches for the next merge window.

Thanks,

Bart.

[...]

Applied to 6.17/scsi-queue, thanks!

[3/3] scsi: core: Use scsi_cmd_priv() instead of open-coding it

      https://git.kernel.org/mkp/scsi/c/8314312c5286
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering