Improve const and reserved command handling
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(-)
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.
--- 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
@@ -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
@@ -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.
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]>Signed-off-by Bart Van Assche <[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..5aaff629b999100644drivers/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@@ -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.
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 constscsi_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.
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 constscsi_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_cmndpointer 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.
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
PATCH 2/3 scsi: core: Make scsi_cmd_priv() accept const arguments
In reply to Bart Van AsscheInstead of requiring the caller to cast away constness, make scsi_cmd_priv() accept const arguments.
--- include/scsi/scsi_cmnd.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/scsi/scsi_cmnd.h +4 -4
@@ -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);
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]>Signed-off-by Bart Van Assche <[email protected]>--- include/scsi/scsi_cmnd.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)include/scsi/scsi_cmnd.h +4
-4@@ -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);
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.
PATCH 3/3 scsi: core: Use scsi_cmd_priv() instead of open-coding it
In reply to Bart Van AsscheImprove code readability without modifying the behavior of the code.
--- drivers/scsi/scsi_lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi_lib.c +1 -1
@@ -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);
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]>Signed-off-by Bart Van Assche <[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@@ -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);
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.
Martin K. Petersen
AppliedRe: [PATCH 3/3] scsi: core: Use scsi_cmd_priv() instead of open-coding it
In reply to Bart Van AsscheBart,
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
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: Makescsi_cmd_to_rq()accept const arguments
scsi: core: Makescsi_cmd_priv()accept const arguments
scsi: core: Usescsi_cmd_priv()instead of open-coding itdrivers/scsi/scsi_lib.c | 2 +- drivers/scsi/scsi_logging.c | 10 +++++----- include/scsi/scsi_cmnd.h | 17 +++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-)
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.
Martin K. Petersen
AppliedRe: (subset) [PATCH 0/3] Improve const and reserved command handling
In reply to Bart Van AsscheQuoted 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