Skip to contentSkip to conversation

scsi: use min()/min_t()/max() to improve code

PATCH Applied Qianfeng Rong 5 participants 16 messages Aug 15, 2025 – Aug 26, 2025

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover scsi: use min()/min_t()/max() to improve code

Use min()/min_t()/max() to reduce the code and improve readability.

No functional changes.

Qianfeng Rong (6):
scsi: bfa: use min_t() to improve code
scsi: hpsa: use min()/min_t() to improve code
scsi: lpfc: use min() to improve code
scsi: megaraid_sas: use max() to improve code
scsi: mpi3mr: use min() to improve codee
scsi: qla2xxx: use min() to improve code

 drivers/scsi/bfa/bfa_fcs_rport.c          |  8 +++-----
 drivers/scsi/bfa/bfa_svc.c                |  5 +----
 drivers/scsi/hpsa.c                       | 11 +++--------
 drivers/scsi/lpfc/lpfc_init.c             |  5 +----
 drivers/scsi/lpfc/lpfc_nvme.c             |  8 ++------
 drivers/scsi/megaraid/megaraid_sas_base.c |  5 +----
 drivers/scsi/mpi3mr/mpi3mr_app.c          |  7 ++-----
 drivers/scsi/qla2xxx/qla_init.c           |  9 +++------
 8 files changed, 16 insertions(+), 42 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/6 scsi: bfa: use min_t() to improve code

Use min_t() to reduce the code in bfa_fcs_rport_update() and bfa_sgpg_mfree(), and improve readability.

---
 drivers/scsi/bfa/bfa_fcs_rport.c | 8 +++-----
 drivers/scsi/bfa/bfa_svc.c       | 5 +----
 2 files changed, 4 insertions(+), 9 deletions(-)
drivers/scsi/bfa/bfa_fcs_rport.c +3 -5
index d4bde9bbe75b..77dc7aaf5985 100644
@@ -11,7 +11,6 @@
 /*
  *  rport.c Remote port implementation.
  */
-
 #include "bfad_drv.h"
 #include "bfad_im.h"
 #include "bfa_fcs.h"
@@ -2555,10 +2554,9 @@ bfa_fcs_rport_update(struct bfa_fcs_rport_s *rport, struct fc_logi_s *plogi)
 	 * - MAX receive frame size
 	 */
 	rport->cisc = plogi->csp.cisc;
-	if (be16_to_cpu(plogi->class3.rxsz) < be16_to_cpu(plogi->csp.rxsz))
-		rport->maxfrsize = be16_to_cpu(plogi->class3.rxsz);
-	else
-		rport->maxfrsize = be16_to_cpu(plogi->csp.rxsz);
+	rport->maxfrsize = min_t(typeof(rport->maxfrsize),
+				 be16_to_cpu(plogi->class3.rxsz),
+				 be16_to_cpu(plogi->csp.rxsz));
 
 	bfa_trc(port->fcs, be16_to_cpu(plogi->csp.bbcred));
 	bfa_trc(port->fcs, port->fabric->bb_credit);
drivers/scsi/bfa/bfa_svc.c +1 -4
index df33afaaa673..2570793aae7f 100644
@@ -5202,10 +5202,7 @@ bfa_sgpg_mfree(struct bfa_s *bfa, struct list_head *sgpg_q, int nsgpg)
 	 */
 	do {
 		wqe = bfa_q_first(&mod->sgpg_wait_q);
-		if (mod->free_sgpgs < wqe->nsgpg)
-			nsgpg = mod->free_sgpgs;
-		else
-			nsgpg = wqe->nsgpg;
+		nsgpg = min_t(int, mod->free_sgpgs, wqe->nsgpg);
 		bfa_sgpg_malloc(bfa, &wqe->sgpg_q, nsgpg);
 		wqe->nsgpg -= nsgpg;
 		if (wqe->nsgpg == 0) {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/6] scsi: bfa: use min_t() to improve code

Quoted 22 lines · On Fri, 15 Aug 2025 20:16:03 +0800 Qianfeng Rong <[email protected]> wrote:

Use min_t() to reduce the code in bfa_fcs_rport_update() and bfa_sgpg_mfree(), and improve readability.

---
 drivers/scsi/bfa/bfa_fcs_rport.c | 8 +++-----
 drivers/scsi/bfa/bfa_svc.c       | 5 +----
 2 files changed, 4 insertions(+), 9 deletions(-)
drivers/scsi/bfa/bfa_fcs_rport.c +0 -1
index d4bde9bbe75b..77dc7aaf5985 100644
@@ -11,7 +11,6 @@
 /*
  *  rport.c Remote port implementation.
  */
-
 #include "bfad_drv.h"
 #include "bfad_im.h"
 #include "bfa_fcs.h"

drivers/scsi/bfa/bfa_fcs_rport.c bfa_fcs_rport_update(struct bfa_fcs_rport_s *rport, struct fc_logi_s *plogi) · line 2559

Quoted 10 lines
drivers/scsi/bfa/bfa_fcs_rport.c +3 -4
@@ -2555,10 +2554,9 @@ bfa_fcs_rport_update(struct bfa_fcs_rport_s *rport, struct fc_logi_s *plogi)
 	 * - MAX receive frame size
 	 */
 	rport->cisc = plogi->csp.cisc;
-	if (be16_to_cpu(plogi->class3.rxsz) < be16_to_cpu(plogi->csp.rxsz))
-		rport->maxfrsize = be16_to_cpu(plogi->class3.rxsz);
-	else
-		rport->maxfrsize = be16_to_cpu(plogi->csp.rxsz);
+	rport->maxfrsize = min_t(typeof(rport->maxfrsize),
+				 be16_to_cpu(plogi->class3.rxsz),
+				 be16_to_cpu(plogi->csp.rxsz));

I think I want to nak that one.
If you are going to use min_t() the type has to be one than includes all possible values of both arguments.
Using the type of the result is just plain wrong.
There is also pretty much no point casting the values to char/short types unless you need the implicit masking.
The values are immediately promoted to 'signed int' before the comparison. I also think that min() will accept an 'unsigned char/short' variable for a comparison against a 'signed int'.

So, all in all, min() should be fine.
Avoiding the extra be16_to_cpu() is probably a gain. The compiler may not always know the value doesn't change.

David

Quoted 19 lines
drivers/scsi/bfa/bfa_fcs_rport.c +0 -0
 	bfa_trc(port->fcs, be16_to_cpu(plogi->csp.bbcred));
 	bfa_trc(port->fcs, port->fabric->bb_credit);
drivers/scsi/bfa/bfa_svc.c +1 -4
index df33afaaa673..2570793aae7f 100644
@@ -5202,10 +5202,7 @@ bfa_sgpg_mfree(struct bfa_s *bfa, struct list_head *sgpg_q, int nsgpg)
 	 */
 	do {
 		wqe = bfa_q_first(&mod->sgpg_wait_q);
-		if (mod->free_sgpgs < wqe->nsgpg)
-			nsgpg = mod->free_sgpgs;
-		else
-			nsgpg = wqe->nsgpg;
+		nsgpg = min_t(int, mod->free_sgpgs, wqe->nsgpg);
 		bfa_sgpg_malloc(bfa, &wqe->sgpg_q, nsgpg);
 		wqe->nsgpg -= nsgpg;
 		if (wqe->nsgpg == 0) {
#3 lore raw

PATCH 2/6 scsi: hpsa: use min()/min_t() to improve code

In reply to Qianfeng Rong

Use min()/min_t() to reduce the code in complete_scsi_command() and hpsa_vpd_page_supported(), and improve readability.

---
 drivers/scsi/hpsa.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/scsi/hpsa.c +3 -8
index c73a71ac3c29..95dfcbac997f 100644
@@ -2662,10 +2662,8 @@ static void complete_scsi_command(struct CommandList *cp)
 	case CMD_TARGET_STATUS:
 		cmd->result |= ei->ScsiStatus;
 		/* copy the sense data */
-		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
-			sense_data_size = SCSI_SENSE_BUFFERSIZE;
-		else
-			sense_data_size = sizeof(ei->SenseInfo);
+		sense_data_size = min_t(unsigned long, SCSI_SENSE_BUFFERSIZE,
+					sizeof(ei->SenseInfo));
 		if (ei->SenseLen < sense_data_size)
 			sense_data_size = ei->SenseLen;
 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
@@ -3628,10 +3626,7 @@ static bool hpsa_vpd_page_supported(struct ctlr_info *h,
 	if (rc != 0)
 		goto exit_unsupported;
 	pages = buf[3];
-	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
-		bufsize = pages + HPSA_VPD_HEADER_SZ;
-	else
-		bufsize = 255;
+	bufsize = min(pages + HPSA_VPD_HEADER_SZ, 255);
 
 	/* Get the whole VPD page list */
 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Signature · 1 line
2.34.1
maintainer Acked-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code

Use min()/min_t() to reduce the code in complete_scsi_command() and hpsa_vpd_page_supported(), and improve readability.

---

Thanks for your patch. We do not normally update hpsa anymore. But this change looks OK.

Thanks,
Don Brace

#5 lore raw

Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code

In reply to Qianfeng Rong

Qianfeng,

Quoted 2 lines

Use min()/min_t() to reduce the code in complete_scsi_command() and hpsa_vpd_page_supported(), and improve readability.

Applied to 6.18/scsi-staging, thanks!

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

Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code

In reply to Qianfeng Rong
Quoted 13 lines · On Fri, 15 Aug 2025 20:16:04 +0800 Qianfeng Rong <[email protected]> wrote:

Use min()/min_t() to reduce the code in complete_scsi_command() and hpsa_vpd_page_supported(), and improve readability.

---
 drivers/scsi/hpsa.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/scsi/hpsa.c +0 -0
index c73a71ac3c29..95dfcbac997f 100644

drivers/scsi/hpsa.c static void complete_scsi_command(struct CommandList *cp) · line 2666

Quoted 9 lines
drivers/scsi/hpsa.c +2 -4
@@ -2662,10 +2662,8 @@ static void complete_scsi_command(struct CommandList *cp)
 	case CMD_TARGET_STATUS:
 		cmd->result |= ei->ScsiStatus;
 		/* copy the sense data */
-		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
-			sense_data_size = SCSI_SENSE_BUFFERSIZE;
-		else
-			sense_data_size = sizeof(ei->SenseInfo);
+		sense_data_size = min_t(unsigned long, SCSI_SENSE_BUFFERSIZE,
+					sizeof(ei->SenseInfo));

Why min_t() ?
A plain min() should be fine.
If it isn't you should really need to justify why the type of one parameter can't be changes before using min_t().

David

Quoted 15 lines
drivers/scsi/hpsa.c +1 -4
 		if (ei->SenseLen < sense_data_size)
 			sense_data_size = ei->SenseLen;
 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
@@ -3628,10 +3626,7 @@ static bool hpsa_vpd_page_supported(struct ctlr_info *h,
 	if (rc != 0)
 		goto exit_unsupported;
 	pages = buf[3];
-	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
-		bufsize = pages + HPSA_VPD_HEADER_SZ;
-	else
-		bufsize = 255;
+	bufsize = min(pages + HPSA_VPD_HEADER_SZ, 255);
 
 	/* Get the whole VPD page list */
 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
#7 lore raw

Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code

Quoted 23 lines · David Laight

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 18 lines · On Fri, 15 Aug 2025 20:16:04 +0800 Qianfeng Rong <[email protected]> wrote:

Use min()/min_t() to reduce the code in complete_scsi_command() and hpsa_vpd_page_supported(), and improve readability.

---
  drivers/scsi/hpsa.c | 11 +++--------
  1 file changed, 3 insertions(+), 8 deletions(-)
drivers/scsi/hpsa.c +0 -2
index c73a71ac3c29..95dfcbac997f 100644
@@ -2662,10 +2662,8 @@ static void complete_scsi_command(struct CommandList *cp)
       case CMD_TARGET_STATUS:
               cmd->result |= ei->ScsiStatus;
               /* copy the sense data */
-             if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
-                     sense_data_size = SCSI_SENSE_BUFFERSIZE;
Quoted 8 lines

drivers/scsi/hpsa.c static void complete_scsi_command(struct CommandList *cp) · line 2666

Quoted 4 lines
drivers/scsi/hpsa.c +2 -2
-             else
-                     sense_data_size = sizeof(ei->SenseInfo);
+             sense_data_size = min_t(unsigned long, SCSI_SENSE_BUFFERSIZE,
+                                     sizeof(ei->SenseInfo));

Why min_t() ?
A plain min() should be fine.
If it isn't you should really need to justify why the type of one parameter can't be changes before using min_t().

SCSI_SENSE_BUFFERSIZE is a macro definition and is generally of type int. The return type of sizeof(ei->SenseInfo) is size_t, so I used min_t() here.  However, as you mentioned, min() can also be used.  Do I need to send v2?

Best regards,
Qianfeng

#8 lore raw

Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code

Quoted 33 lines · On Wed, 20 Aug 2025 20:50:08 +0800 Qianfeng Rong <[email protected]> wrote:
Quoted 23 lines · David Laight

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 18 lines · On Fri, 15 Aug 2025 20:16:04 +0800 Qianfeng Rong <[email protected]> wrote:

Use min()/min_t() to reduce the code in complete_scsi_command() and hpsa_vpd_page_supported(), and improve readability.

---
  drivers/scsi/hpsa.c | 11 +++--------
  1 file changed, 3 insertions(+), 8 deletions(-)
drivers/scsi/hpsa.c +0 -2
index c73a71ac3c29..95dfcbac997f 100644
@@ -2662,10 +2662,8 @@ static void complete_scsi_command(struct CommandList *cp)
       case CMD_TARGET_STATUS:
               cmd->result |= ei->ScsiStatus;
               /* copy the sense data */
-             if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
-                     sense_data_size = SCSI_SENSE_BUFFERSIZE;
Quoted 8 lines

drivers/scsi/hpsa.c static void complete_scsi_command(struct CommandList *cp) · line 2666

Quoted 4 lines
drivers/scsi/hpsa.c +2 -2
-             else
-                     sense_data_size = sizeof(ei->SenseInfo);
+             sense_data_size = min_t(unsigned long, SCSI_SENSE_BUFFERSIZE,
+                                     sizeof(ei->SenseInfo));  

Why min_t() ?
A plain min() should be fine.
If it isn't you should really need to justify why the type of one parameter can't be changes before using min_t().

Quoted 4 lines

SCSI_SENSE_BUFFERSIZE is a macro definition and is generally of type int. The return type of sizeof(ei->SenseInfo) is size_t, so I used min_t() here.  However, as you mentioned, min() can also be used.  Do I need to send v2?

The thing to remember is that min_t(type, a, b) is just min((type)a, (type)b)) and you really would never write the latter.

David

Quoted 3 lines

Best regards,
Qianfeng

#9 lore raw

PATCH 3/6 scsi: lpfc: use min() to improve code

In reply to Qianfeng Rong

Use min_t() to reduce the code in lpfc_sli4_driver_resource_setup() and lpfc_nvme_prep_io_cmd(), and improve readability.

---
 drivers/scsi/lpfc/lpfc_init.c | 5 +----
 drivers/scsi/lpfc/lpfc_nvme.c | 8 ++------
 2 files changed, 3 insertions(+), 10 deletions(-)
drivers/scsi/lpfc/lpfc_init.c +1 -4
index 4081d2a358ee..ad8a85c65bfd 100644
@@ -8300,10 +8300,7 @@ lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
 			phba->cfg_total_seg_cnt,  phba->cfg_scsi_seg_cnt,
 			phba->cfg_nvme_seg_cnt);
 
-	if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
-		i = phba->cfg_sg_dma_buf_size;
-	else
-		i = SLI4_PAGE_SIZE;
+	i = min(phba->cfg_sg_dma_buf_size, SLI4_PAGE_SIZE);
 
 	phba->lpfc_sg_dma_buf_pool =
 			dma_pool_create("lpfc_sg_dma_buf_pool",
drivers/scsi/lpfc/lpfc_nvme.c +2 -6
index a6647dd360d1..e6f632521cff 100644
@@ -1234,12 +1234,8 @@ lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
 			if ((phba->cfg_nvme_enable_fb) &&
 			    test_bit(NLP_FIRSTBURST, &pnode->nlp_flag)) {
 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
-				if (req_len < pnode->nvme_fb_size)
-					wqe->fcp_iwrite.initial_xfer_len =
-						req_len;
-				else
-					wqe->fcp_iwrite.initial_xfer_len =
-						pnode->nvme_fb_size;
+				wqe->fcp_iwrite.initial_xfer_len = min(req_len,
+								       pnode->nvme_fb_size);
 			} else {
 				wqe->fcp_iwrite.initial_xfer_len = 0;
 			}
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH 3/6] scsi: lpfc: use min() to improve code

In reply to Qianfeng Rong

Qianfeng,

Quoted 2 lines

Use min_t() to reduce the code in lpfc_sli4_driver_resource_setup() and lpfc_nvme_prep_io_cmd(), and improve readability.

Applied to 6.18/scsi-staging, thanks!

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

PATCH 4/6 scsi: megaraid_sas: use max() to improve code

In reply to Qianfeng Rong

Use max() to reduce the code in megasas_alloc_irq_vectors() and improve its readability.

---
 drivers/scsi/megaraid/megaraid_sas_base.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/scsi/megaraid/megaraid_sas_base.c +1 -4
index 615e06fd4ee8..18ace4fff9a8 100644
@@ -6004,10 +6004,7 @@ megasas_alloc_irq_vectors(struct megasas_instance *instance)
 			instance->msix_vectors - instance->iopoll_q_count,
 			i, instance->iopoll_q_count);
 
-	if (i > 0)
-		instance->msix_vectors = i;
-	else
-		instance->msix_vectors = 0;
+	instance->msix_vectors = max(i, 0);
 
 	if (instance->smp_affinity_enable)
 		megasas_set_high_iops_queue_affinity_and_hint(instance);
Signature · 1 line
2.34.1
#13 lore raw

PATCH 5/6 scsi: mpi3mr: use min() to improve code

In reply to Qianfeng Rong

Use min() to reduce the code in mpi3mr_map_data_buffer_dma() and improve its readability.

---
 drivers/scsi/mpi3mr/mpi3mr_app.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +2 -5
index 0e5478d62580..c48dac57b530 100644
@@ -2343,11 +2343,8 @@ static int mpi3mr_map_data_buffer_dma(struct mpi3mr_ioc *mrioc,
 		drv_buf->dma_desc[i].addr = mrioc->ioctl_sge[desc_count].addr;
 		drv_buf->dma_desc[i].dma_addr =
 		    mrioc->ioctl_sge[desc_count].dma_addr;
-		if (buf_len < mrioc->ioctl_sge[desc_count].size)
-			drv_buf->dma_desc[i].size = buf_len;
-		else
-			drv_buf->dma_desc[i].size =
-			    mrioc->ioctl_sge[desc_count].size;
+		drv_buf->dma_desc[i].size = min(buf_len,
+						mrioc->ioctl_sge[desc_count].size);
 		buf_len -= drv_buf->dma_desc[i].size;
 		memset(drv_buf->dma_desc[i].addr, 0,
 		       mrioc->ioctl_sge[desc_count].size);
Signature · 1 line
2.34.1
#14 lore raw

PATCH 6/6 scsi: qla2xxx: use min() to improve code

In reply to Qianfeng Rong

Use min_t() to reduce the code in qla2x00_alloc_outstanding_cmds() and improve its readability.

---
 drivers/scsi/qla2xxx/qla_init.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/scsi/qla2xxx/qla_init.c +3 -6
index be211ff22acb..52fc5b8932e3 100644
@@ -4020,12 +4020,9 @@ qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
 
 	if (!IS_FWI2_CAPABLE(ha))
 		req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
-	else {
-		if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
-			req->num_outstanding_cmds = ha->cur_fw_xcb_count;
-		else
-			req->num_outstanding_cmds = ha->cur_fw_iocb_count;
-	}
+	else
+		req->num_outstanding_cmds = min(ha->cur_fw_xcb_count,
+						ha->cur_fw_iocb_count);
 
 	req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
 					sizeof(srb_t *),
Signature · 1 line
2.34.1
#15 lore raw

Re: (subset) [PATCH 0/6] scsi: use min()/min_t()/max() to improve code

In reply to Qianfeng Rong
Quoted 12 lines · Qianfeng Rong

Use min()/min_t()/max() to reduce the code and improve readability.

No functional changes.

Qianfeng Rong (6):
scsi: bfa: use min_t() to improve code
scsi: hpsa: use min()/min_t() to improve code
scsi: lpfc: use min() to improve code
scsi: megaraid_sas: use max() to improve code
scsi: mpi3mr: use min() to improve codee
scsi: qla2xxx: use min() to improve code

Quoted 1 line

[...]

Applied to 6.18/scsi-queue, thanks!

[2/6] scsi: hpsa: use min()/min_t() to improve code

      https://git.kernel.org/mkp/scsi/c/0138c16872bd
[3/6] scsi: lpfc: use min() to improve code
      https://git.kernel.org/mkp/scsi/c/e79aa10e288c
Signature · 1 line
Martin K. Petersen