linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/6] scsi: use min()/min_t()/max() to improve code
@ 2025-08-15 12:16 Qianfeng Rong
  2025-08-15 12:16 ` [PATCH 1/6] scsi: bfa: use min_t() " Qianfeng Rong
                   ` (6 more replies)
  0 siblings, 7 replies; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: Anil Gurumurthy, Sudarsana Kalluru, James E.J. Bottomley,
	Martin K. Petersen, Don Brace, James Smart, Dick Kennedy,
	Kashyap Desai, Sumit Saxena, Shivasharan S, Chandrakanth patil,
	Sathya Prakash Veerichetty, Sreekanth Reddy, Nilesh Javali,
	maintainer:QLOGIC QLA2XXX FC-SCSI DRIVER,
	open list:BROCADE BFA FC SCSI DRIVER, open list,
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list:MEGARAID SCSI/SAS DRIVERS,
	open list:BROADCOM MPI3 STORAGE CONTROLLER DRIVER
  Cc: 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

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

-- 
2.34.1


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

* [PATCH 1/6] scsi: bfa: use min_t() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
@ 2025-08-15 12:16 ` Qianfeng Rong
  2025-08-20 12:13   ` David Laight
  2025-08-15 12:16 ` [PATCH 2/6] scsi: hpsa: use min()/min_t() " Qianfeng Rong
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: Anil Gurumurthy, Sudarsana Kalluru, James E.J. Bottomley,
	Martin K. Petersen, open list:BROCADE BFA FC SCSI DRIVER,
	open list
  Cc: Qianfeng Rong

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/scsi/bfa/bfa_fcs_rport.c | 8 +++-----
 drivers/scsi/bfa/bfa_svc.c       | 5 +----
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/scsi/bfa/bfa_fcs_rport.c b/drivers/scsi/bfa/bfa_fcs_rport.c
index d4bde9bbe75b..77dc7aaf5985 100644
--- a/drivers/scsi/bfa/bfa_fcs_rport.c
+++ b/drivers/scsi/bfa/bfa_fcs_rport.c
@@ -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);
diff --git a/drivers/scsi/bfa/bfa_svc.c b/drivers/scsi/bfa/bfa_svc.c
index df33afaaa673..2570793aae7f 100644
--- a/drivers/scsi/bfa/bfa_svc.c
+++ b/drivers/scsi/bfa/bfa_svc.c
@@ -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) {
-- 
2.34.1


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

* [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
  2025-08-15 12:16 ` [PATCH 1/6] scsi: bfa: use min_t() " Qianfeng Rong
@ 2025-08-15 12:16 ` Qianfeng Rong
  2025-08-15 19:59   ` Don.Brace
                     ` (2 more replies)
  2025-08-15 12:16 ` [PATCH 3/6] scsi: lpfc: use min() " Qianfeng Rong
                   ` (4 subsequent siblings)
  6 siblings, 3 replies; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: Don Brace, James E.J. Bottomley, Martin K. Petersen,
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list
  Cc: Qianfeng Rong

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/scsi/hpsa.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
index c73a71ac3c29..95dfcbac997f 100644
--- a/drivers/scsi/hpsa.c
+++ b/drivers/scsi/hpsa.c
@@ -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,
-- 
2.34.1


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

* [PATCH 3/6] scsi: lpfc: use min() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
  2025-08-15 12:16 ` [PATCH 1/6] scsi: bfa: use min_t() " Qianfeng Rong
  2025-08-15 12:16 ` [PATCH 2/6] scsi: hpsa: use min()/min_t() " Qianfeng Rong
@ 2025-08-15 12:16 ` Qianfeng Rong
  2025-08-15 19:55   ` Justin Tee
  2025-08-20  2:25   ` Martin K. Petersen
  2025-08-15 12:16 ` [PATCH 4/6] scsi: megaraid_sas: use max() " Qianfeng Rong
                   ` (3 subsequent siblings)
  6 siblings, 2 replies; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: James Smart, Dick Kennedy, James E.J. Bottomley,
	Martin K. Petersen,
	open list:EMULEX/BROADCOM LPFC FC/FCOE SCSI DRIVER, open list
  Cc: Qianfeng Rong

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/scsi/lpfc/lpfc_init.c | 5 +----
 drivers/scsi/lpfc/lpfc_nvme.c | 8 ++------
 2 files changed, 3 insertions(+), 10 deletions(-)

diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index 4081d2a358ee..ad8a85c65bfd 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -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",
diff --git a/drivers/scsi/lpfc/lpfc_nvme.c b/drivers/scsi/lpfc/lpfc_nvme.c
index a6647dd360d1..e6f632521cff 100644
--- a/drivers/scsi/lpfc/lpfc_nvme.c
+++ b/drivers/scsi/lpfc/lpfc_nvme.c
@@ -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;
 			}
-- 
2.34.1


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

* [PATCH 4/6] scsi: megaraid_sas: use max() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
                   ` (2 preceding siblings ...)
  2025-08-15 12:16 ` [PATCH 3/6] scsi: lpfc: use min() " Qianfeng Rong
@ 2025-08-15 12:16 ` Qianfeng Rong
  2025-08-15 12:16 ` [PATCH 5/6] scsi: mpi3mr: use min() " Qianfeng Rong
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: Kashyap Desai, Sumit Saxena, Shivasharan S, Chandrakanth patil,
	James E.J. Bottomley, Martin K. Petersen,
	open list:MEGARAID SCSI/SAS DRIVERS,
	open list:MEGARAID SCSI/SAS DRIVERS, open list
  Cc: Qianfeng Rong

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/scsi/megaraid/megaraid_sas_base.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/scsi/megaraid/megaraid_sas_base.c b/drivers/scsi/megaraid/megaraid_sas_base.c
index 615e06fd4ee8..18ace4fff9a8 100644
--- a/drivers/scsi/megaraid/megaraid_sas_base.c
+++ b/drivers/scsi/megaraid/megaraid_sas_base.c
@@ -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);
-- 
2.34.1


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

* [PATCH 5/6] scsi: mpi3mr: use min() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
                   ` (3 preceding siblings ...)
  2025-08-15 12:16 ` [PATCH 4/6] scsi: megaraid_sas: use max() " Qianfeng Rong
@ 2025-08-15 12:16 ` Qianfeng Rong
  2025-08-15 12:16 ` [PATCH 6/6] scsi: qla2xxx: " Qianfeng Rong
  2025-08-26  2:33 ` (subset) [PATCH 0/6] scsi: use min()/min_t()/max() " Martin K. Petersen
  6 siblings, 0 replies; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: Sathya Prakash Veerichetty, Kashyap Desai, Sumit Saxena,
	Sreekanth Reddy, James E.J. Bottomley, Martin K. Petersen,
	open list:BROADCOM MPI3 STORAGE CONTROLLER DRIVER,
	open list:BROADCOM MPI3 STORAGE CONTROLLER DRIVER, open list
  Cc: Qianfeng Rong

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/scsi/mpi3mr/mpi3mr_app.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/scsi/mpi3mr/mpi3mr_app.c b/drivers/scsi/mpi3mr/mpi3mr_app.c
index 0e5478d62580..c48dac57b530 100644
--- a/drivers/scsi/mpi3mr/mpi3mr_app.c
+++ b/drivers/scsi/mpi3mr/mpi3mr_app.c
@@ -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);
-- 
2.34.1


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

* [PATCH 6/6] scsi: qla2xxx: use min() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
                   ` (4 preceding siblings ...)
  2025-08-15 12:16 ` [PATCH 5/6] scsi: mpi3mr: use min() " Qianfeng Rong
@ 2025-08-15 12:16 ` Qianfeng Rong
  2025-08-26  2:33 ` (subset) [PATCH 0/6] scsi: use min()/min_t()/max() " Martin K. Petersen
  6 siblings, 0 replies; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-15 12:16 UTC (permalink / raw)
  To: Nilesh Javali, maintainer:QLOGIC QLA2XXX FC-SCSI DRIVER,
	James E.J. Bottomley, Martin K. Petersen,
	open list:QLOGIC QLA2XXX FC-SCSI DRIVER, open list
  Cc: Qianfeng Rong

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/scsi/qla2xxx/qla_init.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/qla2xxx/qla_init.c b/drivers/scsi/qla2xxx/qla_init.c
index be211ff22acb..52fc5b8932e3 100644
--- a/drivers/scsi/qla2xxx/qla_init.c
+++ b/drivers/scsi/qla2xxx/qla_init.c
@@ -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 *),
-- 
2.34.1


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

* Re: [PATCH 3/6] scsi: lpfc: use min() to improve code
  2025-08-15 12:16 ` [PATCH 3/6] scsi: lpfc: use min() " Qianfeng Rong
@ 2025-08-15 19:55   ` Justin Tee
  2025-08-20  2:25   ` Martin K. Petersen
  1 sibling, 0 replies; 16+ messages in thread
From: Justin Tee @ 2025-08-15 19:55 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Justin Tee, James Smart, Dick Kennedy, James E.J. Bottomley,
	Martin K. Petersen,
	open list:EMULEX/BROADCOM LPFC FC/FCOE SCSI DRIVER, open list

Reviewed-by: Justin Tee <justin.tee@broadcom.com>

Regards,
Justin

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

* Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code
  2025-08-15 12:16 ` [PATCH 2/6] scsi: hpsa: use min()/min_t() " Qianfeng Rong
@ 2025-08-15 19:59   ` Don.Brace
  2025-08-20  2:25   ` Martin K. Petersen
  2025-08-20 12:02   ` David Laight
  2 siblings, 0 replies; 16+ messages in thread
From: Don.Brace @ 2025-08-15 19:59 UTC (permalink / raw)
  To: rongqianfeng, James.Bottomley, martin.petersen, storagedev,
	linux-scsi, linux-kernel


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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 
Acked-by: Don Brace <don.brace@microchip.com>

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

Thanks,
Don Brace

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

* Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code
  2025-08-15 12:16 ` [PATCH 2/6] scsi: hpsa: use min()/min_t() " Qianfeng Rong
  2025-08-15 19:59   ` Don.Brace
@ 2025-08-20  2:25   ` Martin K. Petersen
  2025-08-20 12:02   ` David Laight
  2 siblings, 0 replies; 16+ messages in thread
From: Martin K. Petersen @ 2025-08-20  2:25 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Don Brace, James E.J. Bottomley, Martin K. Petersen,
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list


Qianfeng,

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

-- 
Martin K. Petersen

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

* Re: [PATCH 3/6] scsi: lpfc: use min() to improve code
  2025-08-15 12:16 ` [PATCH 3/6] scsi: lpfc: use min() " Qianfeng Rong
  2025-08-15 19:55   ` Justin Tee
@ 2025-08-20  2:25   ` Martin K. Petersen
  1 sibling, 0 replies; 16+ messages in thread
From: Martin K. Petersen @ 2025-08-20  2:25 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: James Smart, Dick Kennedy, James E.J. Bottomley,
	Martin K. Petersen,
	open list:EMULEX/BROADCOM LPFC FC/FCOE SCSI DRIVER, open list


Qianfeng,

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

-- 
Martin K. Petersen

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

* Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code
  2025-08-15 12:16 ` [PATCH 2/6] scsi: hpsa: use min()/min_t() " Qianfeng Rong
  2025-08-15 19:59   ` Don.Brace
  2025-08-20  2:25   ` Martin K. Petersen
@ 2025-08-20 12:02   ` David Laight
  2025-08-20 12:50     ` Qianfeng Rong
  2 siblings, 1 reply; 16+ messages in thread
From: David Laight @ 2025-08-20 12:02 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Don Brace, James E.J. Bottomley, Martin K. Petersen,
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list

On Fri, 15 Aug 2025 20:16:04 +0800
Qianfeng Rong <rongqianfeng@vivo.com> wrote:

> Use min()/min_t() to reduce the code in complete_scsi_command() and
> hpsa_vpd_page_supported(), and improve readability.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>  drivers/scsi/hpsa.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
> index c73a71ac3c29..95dfcbac997f 100644
> --- a/drivers/scsi/hpsa.c
> +++ b/drivers/scsi/hpsa.c
> @@ -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

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


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

* Re: [PATCH 1/6] scsi: bfa: use min_t() to improve code
  2025-08-15 12:16 ` [PATCH 1/6] scsi: bfa: use min_t() " Qianfeng Rong
@ 2025-08-20 12:13   ` David Laight
  0 siblings, 0 replies; 16+ messages in thread
From: David Laight @ 2025-08-20 12:13 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Anil Gurumurthy, Sudarsana Kalluru, James E.J. Bottomley,
	Martin K. Petersen, open list:BROCADE BFA FC SCSI DRIVER,
	open list

On Fri, 15 Aug 2025 20:16:03 +0800
Qianfeng Rong <rongqianfeng@vivo.com> wrote:

> Use min_t() to reduce the code in bfa_fcs_rport_update() and
> bfa_sgpg_mfree(), and improve readability.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>  drivers/scsi/bfa/bfa_fcs_rport.c | 8 +++-----
>  drivers/scsi/bfa/bfa_svc.c       | 5 +----
>  2 files changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/scsi/bfa/bfa_fcs_rport.c b/drivers/scsi/bfa/bfa_fcs_rport.c
> index d4bde9bbe75b..77dc7aaf5985 100644
> --- a/drivers/scsi/bfa/bfa_fcs_rport.c
> +++ b/drivers/scsi/bfa/bfa_fcs_rport.c
> @@ -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));

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

>  
>  	bfa_trc(port->fcs, be16_to_cpu(plogi->csp.bbcred));
>  	bfa_trc(port->fcs, port->fabric->bb_credit);
> diff --git a/drivers/scsi/bfa/bfa_svc.c b/drivers/scsi/bfa/bfa_svc.c
> index df33afaaa673..2570793aae7f 100644
> --- a/drivers/scsi/bfa/bfa_svc.c
> +++ b/drivers/scsi/bfa/bfa_svc.c
> @@ -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) {


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

* Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code
  2025-08-20 12:02   ` David Laight
@ 2025-08-20 12:50     ` Qianfeng Rong
  2025-08-20 17:57       ` David Laight
  0 siblings, 1 reply; 16+ messages in thread
From: Qianfeng Rong @ 2025-08-20 12:50 UTC (permalink / raw)
  To: David Laight
  Cc: Don Brace, James E.J. Bottomley, Martin K. Petersen,
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list


在 2025/8/20 20:02, David Laight 写道:
> [You don't often get email from david.laight.linux@gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> On Fri, 15 Aug 2025 20:16:04 +0800
> Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>
>> Use min()/min_t() to reduce the code in complete_scsi_command() and
>> hpsa_vpd_page_supported(), and improve readability.
>>
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>> ---
>>   drivers/scsi/hpsa.c | 11 +++--------
>>   1 file changed, 3 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
>> index c73a71ac3c29..95dfcbac997f 100644
>> --- a/drivers/scsi/hpsa.c
>> +++ b/drivers/scsi/hpsa.c
>> @@ -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().
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

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

* Re: [PATCH 2/6] scsi: hpsa: use min()/min_t() to improve code
  2025-08-20 12:50     ` Qianfeng Rong
@ 2025-08-20 17:57       ` David Laight
  0 siblings, 0 replies; 16+ messages in thread
From: David Laight @ 2025-08-20 17:57 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Don Brace, James E.J. Bottomley, Martin K. Petersen,
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list:HEWLETT-PACKARD SMART ARRAY RAID DRIVER (hpsa),
	open list

On Wed, 20 Aug 2025 20:50:08 +0800
Qianfeng Rong <rongqianfeng@vivo.com> wrote:

> 在 2025/8/20 20:02, David Laight 写道:
> > [You don't often get email from david.laight.linux@gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
> >
> > On Fri, 15 Aug 2025 20:16:04 +0800
> > Qianfeng Rong <rongqianfeng@vivo.com> wrote:
> >  
> >> Use min()/min_t() to reduce the code in complete_scsi_command() and
> >> hpsa_vpd_page_supported(), and improve readability.
> >>
> >> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> >> ---
> >>   drivers/scsi/hpsa.c | 11 +++--------
> >>   1 file changed, 3 insertions(+), 8 deletions(-)
> >>
> >> diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
> >> index c73a71ac3c29..95dfcbac997f 100644
> >> --- a/drivers/scsi/hpsa.c
> >> +++ b/drivers/scsi/hpsa.c
> >> @@ -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().

> 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

> 
> Best regards,
> Qianfeng


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

* Re: (subset) [PATCH 0/6] scsi: use min()/min_t()/max() to improve code
  2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
                   ` (5 preceding siblings ...)
  2025-08-15 12:16 ` [PATCH 6/6] scsi: qla2xxx: " Qianfeng Rong
@ 2025-08-26  2:33 ` Martin K. Petersen
  6 siblings, 0 replies; 16+ messages in thread
From: Martin K. Petersen @ 2025-08-26  2:33 UTC (permalink / raw)
  To: Anil Gurumurthy, Sudarsana Kalluru, James E.J. Bottomley,
	Don Brace, James Smart, Dick Kennedy, Kashyap Desai, Sumit Saxena,
	Shivasharan S, Chandrakanth patil, Sathya Prakash Veerichetty,
	Sreekanth Reddy, Nilesh Javali, GR-QLogic-Storage-Upstream,
	linux-scsi, linux-kernel, storagedev, megaraidlinux.pdl,
	mpi3mr-linuxdrv.pdl, Qianfeng Rong
  Cc: Martin K . Petersen

On Fri, 15 Aug 2025 20:16:02 +0800, Qianfeng Rong wrote:

> 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
> 
> [...]

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

-- 
Martin K. Petersen

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

end of thread, other threads:[~2025-08-26  2:34 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-15 12:16 [PATCH 0/6] scsi: use min()/min_t()/max() to improve code Qianfeng Rong
2025-08-15 12:16 ` [PATCH 1/6] scsi: bfa: use min_t() " Qianfeng Rong
2025-08-20 12:13   ` David Laight
2025-08-15 12:16 ` [PATCH 2/6] scsi: hpsa: use min()/min_t() " Qianfeng Rong
2025-08-15 19:59   ` Don.Brace
2025-08-20  2:25   ` Martin K. Petersen
2025-08-20 12:02   ` David Laight
2025-08-20 12:50     ` Qianfeng Rong
2025-08-20 17:57       ` David Laight
2025-08-15 12:16 ` [PATCH 3/6] scsi: lpfc: use min() " Qianfeng Rong
2025-08-15 19:55   ` Justin Tee
2025-08-20  2:25   ` Martin K. Petersen
2025-08-15 12:16 ` [PATCH 4/6] scsi: megaraid_sas: use max() " Qianfeng Rong
2025-08-15 12:16 ` [PATCH 5/6] scsi: mpi3mr: use min() " Qianfeng Rong
2025-08-15 12:16 ` [PATCH 6/6] scsi: qla2xxx: " Qianfeng Rong
2025-08-26  2:33 ` (subset) [PATCH 0/6] scsi: use min()/min_t()/max() " 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;
as well as URLs for NNTP newsgroup(s).