Skip to contentSkip to conversation

SCSI: Replace ternary operations with min()/max() macros

PATCH In review Prabhakar Pujeri 4 participants 20 messages Jun 26, 2024 – Jun 27, 2024

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

cover SCSI: Replace ternary operations with min()/max() macros

Hi All,

This patch series, generated using Coccinelle, aims to improve code readability and maintainability by replacing ternary operations with the min() and max() macros where applicable in the SCSI subsystem.

I acknowledge that some of these drivers are old and may not require modifications unless addressing critical issues. However, I intend to include these patches for those older drivers alongside any future critical fixes.

The patches have been compiled and tested on an x86_64 system. Physical device testing was conducted for the megaraid_sas and mpi3mr drivers.

Prabhakar Pujeri (14):
scsi: advansys: Simplified memcpy length calculation in adv_build_req
scsi: bfa: Used min() in fc_rftid_build_sol for bitmap size
scsi: bfa:Simplified logic in bfa_fcs_rport_update and bfa_sgpg_mfree
scsi: cxlflash: Replaced ternary operation in write_same16 with min()
scsi: hpsa: Used min() for sense data size and buffer size in
complete_scsi_command and hpsa_vpd_page_supported
scsi: megaraid_sas: Simplified transfer length calculation using max()
in mega_m_to_n
scsi: megaraid_sas: Replaced ternary operation with max() in
megasas_alloc_irq_vectors
scsi: mpi3mr: Used min() in mpi3mr_map_data_buffer_dma for buffer size
scsi: ncr53c8xx: Simplified tag number calculation with max() in
ncr_sir_to_redo
scsi: qla2xxx: Used max() for queue count in qla25xx_copy_mq
scsi: qla2xxx: Simplified outstanding commands calculation in
qla2x00_alloc_outstanding_cmds and qla24xx_read_fcp_prio_cfg
scsi: scsi_debug: Replaced ternary operation with min() in
resp_get_lba_status
scsi: scsi_transport_spi: Simplified period calculation with max() in
spi_dv_retrain
scsi: st: Used max() for buffer size in setup_buffering and Simplified
transfer calculations in st_read, append_to_buffer, and from_buffer

 drivers/scsi/advansys.c                   |  2 +-
 drivers/scsi/bfa/bfa_fcbuild.c            |  2 +-
 drivers/scsi/bfa/bfa_fcs_rport.c          |  6 ++----
 drivers/scsi/bfa/bfa_svc.c                |  5 +----
 drivers/scsi/cxlflash/vlun.c              |  2 +-
 drivers/scsi/hpsa.c                       | 11 +++--------
 drivers/scsi/megaraid.c                   |  3 +--
 drivers/scsi/megaraid/megaraid_sas_base.c |  5 +----
 drivers/scsi/mpi3mr/mpi3mr_app.c          |  7 ++-----
 drivers/scsi/ncr53c8xx.c                  |  2 +-
 drivers/scsi/qla2xxx/qla_dbg.c            |  3 +--
 drivers/scsi/qla2xxx/qla_init.c           |  6 ++----
 drivers/scsi/qla2xxx/qla_sup.c            |  2 +-
 drivers/scsi/scsi_debug.c                 |  5 +----
 drivers/scsi/scsi_transport_spi.c         |  2 +-
 drivers/scsi/st.c                         | 10 ++++------
 16 files changed, 24 insertions(+), 49 deletions(-)

--
2.45.1

#1 lore raw

PATCH 1/14 scsi: advansys: Simplified memcpy length calculation in adv_build_req

---
 drivers/scsi/advansys.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/advansys.c +1 -1
index ab066bb27a57..e5ae43e00f24 100644
@@ -7691,7 +7691,7 @@ adv_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
 	/* Set CDB length and copy it to the request structure.  */
 	scsiqp->cdb_len = scp->cmd_len;
 	/* Copy first 12 CDB bytes to cdb[]. */
-	memcpy(scsiqp->cdb, scp->cmnd, scp->cmd_len < 12 ? scp->cmd_len : 12);
+	memcpy(scsiqp->cdb, scp->cmnd, min(scp->cmd_len, 12));
 	/* Copy last 4 CDB bytes, if present, to cdb16[]. */
 	if (scp->cmd_len > 12) {
 		int cdb16_len = scp->cmd_len - 12;
Signature · 1 line
2.45.1
#2 lore raw

PATCH 2/14 scsi: bfa: Used min() in fc_rftid_build_sol for bitmap size

In reply to Prabhakar Pujeri
---
 drivers/scsi/bfa/bfa_fcbuild.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/bfa/bfa_fcbuild.c +1 -1
index 52303e8c716d..784be9b435f1 100644
@@ -1093,7 +1093,7 @@ fc_rftid_build_sol(struct fchs_s *fchs, void *pyld, u32 s_id, u16 ox_id,
 
 	rftid->dap = s_id;
 	memcpy((void *)rftid->fc4_type, (void *)fc4_bitmap,
-		(bitmap_size < 32 ? bitmap_size : 32));
+		min(bitmap_size, 32));
 
 	return sizeof(struct fcgs_rftid_req_s) + sizeof(struct ct_hdr_s);
 }
Signature · 1 line
2.45.1
#3 lore raw

PATCH 3/14 scsi: bfa:Simplified logic in bfa_fcs_rport_update and bfa_sgpg_mfree

In reply to Prabhakar Pujeri
---
 drivers/scsi/bfa/bfa_fcs_rport.c | 6 ++----
 drivers/scsi/bfa/bfa_svc.c       | 5 +----
 2 files changed, 3 insertions(+), 8 deletions(-)
drivers/scsi/bfa/bfa_fcs_rport.c +2 -4
index ce52a9c88ae6..41e55030596a 100644
@@ -2555,10 +2555,8 @@ 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(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 9f33aa303b18..3cbe87fc343b 100644
@@ -5257,10 +5257,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(mod->free_sgpgs, wqe->nsgpg);
 		bfa_sgpg_malloc(bfa, &wqe->sgpg_q, nsgpg);
 		wqe->nsgpg -= nsgpg;
 		if (wqe->nsgpg == 0) {
Signature · 1 line
2.45.1
#4 lore raw

PATCH 4/14 scsi: cxlflash: Replaced ternary operation in write_same16 with min()

In reply to Prabhakar Pujeri
---
 drivers/scsi/cxlflash/vlun.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/cxlflash/vlun.c +1 -1
index 35326e311991..e39ef6967d5d 100644
@@ -445,7 +445,7 @@ static int write_same16(struct scsi_device *sdev,
 		scsi_cmd[0] = WRITE_SAME_16;
 		scsi_cmd[1] = cfg->ws_unmap ? 0x8 : 0;
 		put_unaligned_be64(offset, &scsi_cmd[2]);
-		put_unaligned_be32(ws_limit < left ? ws_limit : left,
+		put_unaligned_be32(min(ws_limit, left),
 				   &scsi_cmd[10]);
 
 		/* Drop the ioctl read semaphore across lengthy call */
Signature · 1 line
2.45.1
#5 lore raw

Re: [PATCH 04/14] scsi: cxlflash: Replaced ternary operation in write_same16 with min()

kernel test robot noticed the following build errors:

Hi Prabhakar,

kernel test robot noticed the following build errors:

[auto build test ERROR on jejb-scsi/for-next]
[also build test ERROR on mkp-scsi/for-next linus/master v6.10-rc5 next-20240626] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Prabhakar-Pujeri/scsi-advansys-Simplified-memcpy-length-calculation-in-adv_build_req/20240626-231800
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi.git for-next
patch link:    https://lore.kernel.org/r/20240626101342.1440049-5-prabhakar.pujeri%40gmail.com
patch subject: [PATCH 04/14] scsi: cxlflash: Replaced ternary operation in write_same16 with min()
config: powerpc-powernv_defconfig (https://download.01.org/0day-ci/archive/20240627/[email protected]/config)
compiler: powerpc64le-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240627/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/container_of.h:5,
from include/linux/kernel.h:22, from include/linux/interrupt.h:6, from drivers/scsi/cxlflash/vlun.c:11:
drivers/scsi/cxlflash/vlun.c: In function 'write_same16':

>> include/linux/build_bug.h:78:41: error: static assertion failed: "min(ws_limit, left) signedness error, fix types or consider umin() before min_t()"
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                         ^~~~~~~~~~~~~~
   include/linux/build_bug.h:77:34: note: in expansion of macro '__static_assert'
      77 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
         |                                  ^~~~~~~~~~~~~~~
   include/linux/minmax.h:51:9: note: in expansion of macro 'static_assert'
      51 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:58:17: note: in expansion of macro '__cmp_once'
      58 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:85:25: note: in expansion of macro '__careful_cmp'
      85 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~

drivers/scsi/cxlflash/vlun.c:448:36: note: in expansion of macro 'min'
448 | put_unaligned_be32(min(ws_limit, left),
| ^~~

vim +78 include/linux/build_bug.h

bc6245e5efd70c Ian Abbott       2017-07-10  60  
6bab69c65013be Rasmus Villemoes 2019-03-07  61  /**
6bab69c65013be Rasmus Villemoes 2019-03-07  62   * static_assert - check integer constant expression at build time
6bab69c65013be Rasmus Villemoes 2019-03-07  63   *
6bab69c65013be Rasmus Villemoes 2019-03-07  64   * static_assert() is a wrapper for the C11 _Static_assert, with a
6bab69c65013be Rasmus Villemoes 2019-03-07  65   * little macro magic to make the message optional (defaulting to the
6bab69c65013be Rasmus Villemoes 2019-03-07  66   * stringification of the tested expression).
6bab69c65013be Rasmus Villemoes 2019-03-07  67   *
6bab69c65013be Rasmus Villemoes 2019-03-07  68   * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
6bab69c65013be Rasmus Villemoes 2019-03-07  69   * scope, but requires the expression to be an integer constant
6bab69c65013be Rasmus Villemoes 2019-03-07  70   * expression (i.e., it is not enough that __builtin_constant_p() is
6bab69c65013be Rasmus Villemoes 2019-03-07  71   * true for expr).
6bab69c65013be Rasmus Villemoes 2019-03-07  72   *
6bab69c65013be Rasmus Villemoes 2019-03-07  73   * Also note that BUILD_BUG_ON() fails the build if the condition is
6bab69c65013be Rasmus Villemoes 2019-03-07  74   * true, while static_assert() fails the build if the expression is
6bab69c65013be Rasmus Villemoes 2019-03-07  75   * false.
6bab69c65013be Rasmus Villemoes 2019-03-07  76   */
6bab69c65013be Rasmus Villemoes 2019-03-07  77  #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)

6bab69c65013be Rasmus Villemoes 2019-03-07 @78 #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) 6bab69c65013be Rasmus Villemoes 2019-03-07 79
07a368b3f55a79 Maxim Levitsky 2022-10-25 80

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#6 lore raw

Re: [PATCH 04/14] scsi: cxlflash: Replaced ternary operation in write_same16 with min()

In reply to Prabhakar Pujeri
kernel test robot noticed the following build errors:

Hi Prabhakar,

kernel test robot noticed the following build errors:

[auto build test ERROR on jejb-scsi/for-next]
[also build test ERROR on mkp-scsi/for-next linus/master v6.10-rc5 next-20240626] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Prabhakar-Pujeri/scsi-advansys-Simplified-memcpy-length-calculation-in-adv_build_req/20240626-231800
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi.git for-next
patch link:    https://lore.kernel.org/r/20240626101342.1440049-5-prabhakar.pujeri%40gmail.com
patch subject: [PATCH 04/14] scsi: cxlflash: Replaced ternary operation in write_same16 with min()
config: powerpc-allyesconfig (https://download.01.org/0day-ci/archive/20240627/[email protected]/config)
compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project ad79a14c9e5ec4a369eed4adf567c22cc029863f)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240627/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/scsi/cxlflash/vlun.c:11:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from arch/powerpc/include/asm/hardirq.h:6:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:14:
In file included from arch/powerpc/include/asm/io.h:24:
In file included from include/linux/mm.h:2253:
include/linux/vmstat.h:500:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     500 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     501 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:507:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     507 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     508 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:514:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     514 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:519:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     519 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     520 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:528:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     528 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     529 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/scsi/cxlflash/vlun.c:448:22: error: static assertion failed due to requirement '__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((const unsigned int)(-1)) < (const unsigned int)1)) * 0L)) : (int *)8))), (((const unsigned int)(-1)) < (const unsigned int)1), 0) == __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0) || __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0) == __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0) || (__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)(ws_limit) * 0L)) : (int *)8))) && __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((const unsigned int)(-1)) < (const unsigned int)1)) * 0L)) : (int *)8))), (((const unsigned int)(-1)) < (const unsigned int)1), 0), ws_limit, -1) >= 0) || (__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)(left) * 0L)) : (int *)8))) && __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0), left, -1) >= 0)': min(ws_limit, left) signedness error, fix types or consider umin() before min_t()
     448 |                 put_unaligned_be32(min(ws_limit, left),
         |                                    ^~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:85:19: note: expanded from macro 'min'
      85 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:58:3: note: expanded from macro '__careful_cmp'
      58 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:51:16: note: expanded from macro '__cmp_once'
      51 |         static_assert(__types_ok(x, y),                 \
         |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      52 |                 #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
         |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
   include/linux/minmax.h:31:2: note: expanded from macro '__is_signed'
      31 |         __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))),        \
         |         ^
   include/linux/build_bug.h:77:50: note: expanded from macro 'static_assert'
      77 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
         |                                  ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:78:56: note: expanded from macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~

5 warnings and 1 error generated.

vim +448 drivers/scsi/cxlflash/vlun.c

392
393 /**
394 * write_same16() - sends a SCSI WRITE_SAME16 (0) command to specified LUN
395 * @sdev: SCSI device associated with LUN.
396 * @lba: Logical block address to start write same.
397 * @nblks: Number of logical blocks to write same.
398 *
399 * The SCSI WRITE_SAME16 can take quite a while to complete. Should an EEH occur
400 * while in scsi_execute_cmd(), the EEH handler will attempt to recover. As
401 * part of the recovery, the handler drains all currently running ioctls,
402 * waiting until they have completed before proceeding with a reset. As this
403 * routine is used on the ioctl path, this can create a condition where the
404 * EEH handler becomes stuck, infinitely waiting for this ioctl thread. To
405 * avoid this behavior, temporarily unmark this thread as an ioctl thread by
406 * releasing the ioctl read semaphore. This will allow the EEH handler to
407 * proceed with a recovery while this thread is still running. Once the
408 * scsi_execute_cmd() returns, reacquire the ioctl read semaphore and check the
409 * adapter state in case it changed while inside of scsi_execute_cmd(). The
410 * state check will wait if the adapter is still being recovered or return a
411 * failure if the recovery failed. In the event that the adapter reset failed,
412 * simply return the failure as the ioctl would be unable to continue.
413 *
414 * Note that the above puts a requirement on this routine to only be called on
415 * an ioctl thread.
416 *
417 * Return: 0 on success, -errno on failure
418 */
419 static int write_same16(struct scsi_device *sdev,
420 u64 lba,
421 u32 nblks)
422 {
423 u8 *cmd_buf = NULL;
424 u8 *scsi_cmd = NULL;
425 int rc = 0;
426 int result = 0;
427 u64 offset = lba;
428 int left = nblks;
429 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
430 struct device *dev = &cfg->dev->dev;
431 const u32 s = ilog2(sdev->sector_size) - 9;
432 const u32 to = sdev->request_queue->rq_timeout;
433 const u32 ws_limit =
434 sdev->request_queue->limits.max_write_zeroes_sectors >> s;
435
436 cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
437 scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
438 if (unlikely(!cmd_buf || !scsi_cmd)) {
439 rc = -ENOMEM;
440 goto out;
441 }
442
443 while (left > 0) {
444
445 scsi_cmd[0] = WRITE_SAME_16;
446 scsi_cmd[1] = cfg->ws_unmap ? 0x8 : 0;
447 put_unaligned_be64(offset, &scsi_cmd[2]);

 > 448			put_unaligned_be32(min(ws_limit, left),

449 &scsi_cmd[10]);
450
451 /* Drop the ioctl read semaphore across lengthy call */
452 up_read(&cfg->ioctl_rwsem);
453 result = scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_OUT,
454 cmd_buf, CMD_BUFSIZE, to,
455 CMD_RETRIES, NULL);
456 down_read(&cfg->ioctl_rwsem);
457 rc = check_state(cfg);
458 if (rc) {
459 dev_err(dev, "%s: Failed state result=%08x\n",
460 __func__, result);
461 rc = -ENODEV;
462 goto out;
463 }
464
465 if (result) {
466 dev_err_ratelimited(dev, "%s: command failed for "
467 "offset=%lld result=%08x\n",
468 __func__, offset, result);
469 rc = -EIO;
470 goto out;
471 }
472 left -= ws_limit;
473 offset += ws_limit;
474 }
475
476 out:
477 kfree(cmd_buf);
478 kfree(scsi_cmd);
479 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
480 return rc;
481 }
482

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

PATCH 5/14 scsi: hpsa: Used min() for sense data size and buffer size in complete_scsi_command and hpsa_vpd_page_supported

In reply to Prabhakar Pujeri
---
 drivers/scsi/hpsa.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/scsi/hpsa.c +3 -8
index e044ed09d7e0..05fba23d94e5 100644
@@ -2668,10 +2668,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(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);
@@ -3634,10 +3632,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.45.1
#8 lore raw

PATCH 6/14 scsi: megaraid_sas: Simplified transfer length calculation using max() in mega_m_to_n

In reply to Prabhakar Pujeri
---
 drivers/scsi/megaraid.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/scsi/megaraid.c +1 -2
index 38976f94453e..8e9caf124778 100644
@@ -3469,8 +3469,7 @@ mega_m_to_n(void __user *arg, nitioctl_t *uioc)
 		/*
 		 * Choose the xferlen bigger of input and output data
 		 */
-		uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
-			uioc_mimd.outlen : uioc_mimd.inlen;
+		uioc->xferlen = max(uioc_mimd.outlen, uioc_mimd.inlen);
 
 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
Signature · 1 line
2.45.1
#9 lore raw

PATCH 7/14 scsi: megaraid_sas: Replaced ternary operation with max() in megasas_alloc_irq_vectors

In reply to Prabhakar Pujeri
---
 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 88acefbf9aea..ad7369d78ab9 100644
@@ -5996,10 +5996,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.45.1
#10 lore raw

PATCH 8/14 scsi: mpi3mr: Used min() in mpi3mr_map_data_buffer_dma for buffer size

In reply to Prabhakar Pujeri
---
 drivers/scsi/mpi3mr/mpi3mr_app.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +2 -5
index cd261b48eb46..350f3df02baf 100644
@@ -1171,11 +1171,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.45.1
#11 lore raw

PATCH 9/14 scsi: ncr53c8xx: Simplified tag number calculation with max() in ncr_sir_to_redo

In reply to Prabhakar Pujeri
---
 drivers/scsi/ncr53c8xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ncr53c8xx.c +1 -1
index 35869b4f9329..863139c98cce 100644
@@ -6410,7 +6410,7 @@ static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
 					"CCBs\n", busy_cnt, disc_cnt);
 		}
 		if (disc_cnt < lp->numtags) {
-			lp->numtags	= disc_cnt > 2 ? disc_cnt : 2;
+			lp->numtags	= max(disc_cnt, 2);
 			lp->num_good	= 0;
 			ncr_setup_tags (np, cmd->device);
 		}
Signature · 1 line
2.45.1
#12 lore raw

PATCH 10/14 scsi: qla2xxx: Used max() for queue count in qla25xx_copy_mq

In reply to Prabhakar Pujeri
---
 drivers/scsi/qla2xxx/qla_dbg.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/scsi/qla2xxx/qla_dbg.c +1 -2
index 691ef827a5ab..5e3afd7ffa76 100644
@@ -685,8 +685,7 @@ qla25xx_copy_mq(struct qla_hw_data *ha, void *ptr, __be32 **last_chain)
 	mq->type = htonl(DUMP_CHAIN_MQ);
 	mq->chain_size = htonl(sizeof(struct qla2xxx_mq_chain));
 
-	que_cnt = ha->max_req_queues > ha->max_rsp_queues ?
-		ha->max_req_queues : ha->max_rsp_queues;
+	que_cnt = max(ha->max_req_queues, ha->max_rsp_queues);
 	mq->count = htonl(que_cnt);
 	for (cnt = 0; cnt < que_cnt; cnt++) {
 		reg = ISP_QUE_REG(ha, cnt);
Signature · 1 line
2.45.1
#13 lore raw

PATCH 11/14 scsi: qla2xxx: Simplified outstanding commands calculation in qla2x00_alloc_outstanding_cmds and qla24xx_read_fcp_prio_cfg

In reply to Prabhakar Pujeri
---
 drivers/scsi/qla2xxx/qla_init.c | 6 ++----
 drivers/scsi/qla2xxx/qla_sup.c  | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)
drivers/scsi/qla2xxx/qla_init.c +2 -4
index 8377624d76c9..9caaaf25de64 100644
@@ -3999,10 +3999,8 @@ 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;
+		req->num_outstanding_cmds = min(ha->cur_fw_xcb_count,
+						ha->cur_fw_iocb_count);
 	}
 
 	req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
drivers/scsi/qla2xxx/qla_sup.c +1 -1
index c092a6b1ced4..2703408c60e7 100644
@@ -3625,7 +3625,7 @@ qla24xx_read_fcp_prio_cfg(scsi_qla_host_t *vha)
 	max_len = FCP_PRIO_CFG_SIZE - FCP_PRIO_CFG_HDR_SIZE;
 
 	ha->isp_ops->read_optrom(vha, &ha->fcp_prio_cfg->entry[0],
-			fcp_prio_addr << 2, (len < max_len ? len : max_len));
+			fcp_prio_addr << 2, min(len, max_len));
 
 	/* revalidate the entire FCP priority config data, including entries */
 	if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 1))
Signature · 1 line
2.45.1
#14 lore raw

PATCH 12/14 scsi: scsi_debug: Replaced ternary operation with min() in resp_get_lba_status

In reply to Prabhakar Pujeri
---
 drivers/scsi/scsi_debug.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/scsi/scsi_debug.c +1 -4
index acf0592d63da..ae64c0df95f7 100644
@@ -4579,10 +4579,7 @@ static int resp_get_lba_status(struct scsi_cmnd *scp,
 		mapped = 1;
 		/* following just in case virtual_gb changed */
 		sdebug_capacity = get_sdebug_capacity();
-		if (sdebug_capacity - lba <= 0xffffffff)
-			num = sdebug_capacity - lba;
-		else
-			num = 0xffffffff;
+		num = min(sdebug_capacity - lba, 0xffffffff);
 	}
 
 	memset(arr, 0, SDEBUG_GET_LBA_STATUS_LEN);
Signature · 1 line
2.45.1
#15 lore raw

PATCH 13/14 scsi: scsi_transport_spi: Simplified period calculation with max() in spi_dv_retrain

In reply to Prabhakar Pujeri
---
 drivers/scsi/scsi_transport_spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi_transport_spi.c +1 -1
index 64852e6df3e3..c50e923bfa0f 100644
@@ -783,7 +783,7 @@ spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
 			DV_SET(qas, 0);
 		} else {
 			newperiod = spi_period(starget);
-			period = newperiod > period ? newperiod : period;
+			period = max(newperiod, period);
 			if (period < 0x0d)
 				period++;
 			else
Signature · 1 line
2.45.1
#16 lore raw

PATCH 14/14 scsi: st: Used max() for buffer size in setup_buffering and Simplified transfer calculations in st_read, append_to_buffer, and from_buffer

In reply to Prabhakar Pujeri
---
 drivers/scsi/st.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/scsi/st.c +4 -6
index 0d8ce1a92168..b473f79c5a73 100644
@@ -1575,8 +1575,7 @@ static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
 
 	if (!STbp->do_dio) {
 		if (STp->block_size)
-			bufsize = STp->block_size > st_fixed_buffer_size ?
-				STp->block_size : st_fixed_buffer_size;
+			bufsize = max(STp->block_size, st_fixed_buffer_size);
 		else {
 			bufsize = count;
 			/* Make sure that data from previous user is not leaked even if
@@ -2187,8 +2186,7 @@ st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
 					  STps->eof, STbp->buffer_bytes,
 					  (int)(count - total));
 			) /* end DEB */
-			transfer = STbp->buffer_bytes < count - total ?
-			    STbp->buffer_bytes : count - total;
+			transfer = min(STbp->buffer_bytes, count - total);
 			if (!do_dio) {
 				i = from_buffer(STbp, buf, transfer);
 				if (i) {
@@ -3997,7 +3995,7 @@ static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, in
 	}
 	for (; i < st_bp->frp_segs && do_count > 0; i++) {
 		struct page *page = st_bp->reserved_pages[i];
-		cnt = length - offset < do_count ? length - offset : do_count;
+		cnt = min(length - offset, do_count);
 		res = copy_from_user(page_address(page) + offset, ubp, cnt);
 		if (res)
 			return (-EFAULT);
@@ -4029,7 +4027,7 @@ static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count)
 	}
 	for (; i < st_bp->frp_segs && do_count > 0; i++) {
 		struct page *page = st_bp->reserved_pages[i];
-		cnt = length - offset < do_count ? length - offset : do_count;
+		cnt = min(length - offset, do_count);
 		res = copy_to_user(ubp, page_address(page) + offset, cnt);
 		if (res)
 			return (-EFAULT);
Signature · 1 line
2.45.1
#17 lore raw

Re: [PATCH 14/14] scsi: st: Used max() for buffer size in setup_buffering and Simplified transfer calculations in st_read, append_to_buffer, and from_buffer

kernel test robot noticed the following build errors:

Hi Prabhakar,

kernel test robot noticed the following build errors:

[auto build test ERROR on jejb-scsi/for-next]
[also build test ERROR on mkp-scsi/for-next linus/master v6.10-rc5 next-20240626] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Prabhakar-Pujeri/scsi-advansys-Simplified-memcpy-length-calculation-in-adv_build_req/20240626-231800
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi.git for-next
patch link:    https://lore.kernel.org/r/20240626101342.1440049-15-prabhakar.pujeri%40gmail.com
patch subject: [PATCH 14/14] scsi: st: Used max() for buffer size in setup_buffering and Simplified transfer calculations in st_read, append_to_buffer, and from_buffer
config: arm-randconfig-001-20240627 (https://download.01.org/0day-ci/archive/20240627/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240627/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/container_of.h:5,

                    from include/linux/list.h:5,
                    from include/linux/module.h:12,
                    from drivers/scsi/st.c:23:
   drivers/scsi/st.c: In function 'st_read':
>> include/linux/build_bug.h:78:41: error: static assertion failed: "min(STbp->buffer_bytes, count - total) signedness error, fix types or consider umin() before min_t()"
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                         ^~~~~~~~~~~~~~
   include/linux/build_bug.h:77:34: note: in expansion of macro '__static_assert'
      77 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
         |                                  ^~~~~~~~~~~~~~~
   include/linux/minmax.h:51:9: note: in expansion of macro 'static_assert'
      51 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:58:17: note: in expansion of macro '__cmp_once'
      58 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:85:25: note: in expansion of macro '__careful_cmp'
      85 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   drivers/scsi/st.c:2189:36: note: in expansion of macro 'min'
    2189 |                         transfer = min(STbp->buffer_bytes, count - total);
         |                                    ^~~

vim +78 include/linux/build_bug.h

bc6245e5efd70c Ian Abbott       2017-07-10  60  
6bab69c65013be Rasmus Villemoes 2019-03-07  61  /**
6bab69c65013be Rasmus Villemoes 2019-03-07  62   * static_assert - check integer constant expression at build time
6bab69c65013be Rasmus Villemoes 2019-03-07  63   *
6bab69c65013be Rasmus Villemoes 2019-03-07  64   * static_assert() is a wrapper for the C11 _Static_assert, with a
6bab69c65013be Rasmus Villemoes 2019-03-07  65   * little macro magic to make the message optional (defaulting to the
6bab69c65013be Rasmus Villemoes 2019-03-07  66   * stringification of the tested expression).
6bab69c65013be Rasmus Villemoes 2019-03-07  67   *
6bab69c65013be Rasmus Villemoes 2019-03-07  68   * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
6bab69c65013be Rasmus Villemoes 2019-03-07  69   * scope, but requires the expression to be an integer constant
6bab69c65013be Rasmus Villemoes 2019-03-07  70   * expression (i.e., it is not enough that __builtin_constant_p() is
6bab69c65013be Rasmus Villemoes 2019-03-07  71   * true for expr).
6bab69c65013be Rasmus Villemoes 2019-03-07  72   *
6bab69c65013be Rasmus Villemoes 2019-03-07  73   * Also note that BUILD_BUG_ON() fails the build if the condition is
6bab69c65013be Rasmus Villemoes 2019-03-07  74   * true, while static_assert() fails the build if the expression is
6bab69c65013be Rasmus Villemoes 2019-03-07  75   * false.
6bab69c65013be Rasmus Villemoes 2019-03-07  76   */
6bab69c65013be Rasmus Villemoes 2019-03-07  77  #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)

6bab69c65013be Rasmus Villemoes 2019-03-07 @78 #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) 6bab69c65013be Rasmus Villemoes 2019-03-07 79
07a368b3f55a79 Maxim Levitsky 2022-10-25 80

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#18 lore raw
In reply to Prabhakar Pujeri

Hi Prabhakar!

Quoted 3 lines

This patch series, generated using Coccinelle, aims to improve code readability and maintainability by replacing ternary operations with the min() and max() macros where applicable in the SCSI subsystem.

Your proposed changes do not improve maintainability. They are creating additional maintenance work and making backports of bug fixes more difficult. Plus they introduce the potential for regressions of tried and true code. So the series has the opposite effect of what is claimed.

Readability is in the eye of the beholder. Whether to use one approach or the other is the code author's choice...

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering