Blob Blame History Raw
From: Dick Kennedy <dick.kennedy@broadcom.com>
Date: Mon, 6 Jul 2020 13:42:46 -0700
Subject: scsi: lpfc: Fix less-than-zero comparison of unsigned value
Patch-mainline: Queued in subsystem maintainer repository
Git-repo: git://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git
Git-commit: 77dd7d7b344283a3bce334d0f43fc2c0629ffe48
References: bsc#1158983

The expression start_idx - dbg_cnt is evaluated using unsigned int
arthithmetic (since these variables are unsigned ints) and hence can never
be less than zero, so the less than comparison is never true.  Rewrite the
expression to check for start_idx being less than dbg_cnt.

After the logic was corrected, temp_idx wasn't working correctly. So fix it
as well.

Link: https://lore.kernel.org/r/20200706204246.130416-1-jsmart2021@gmail.com
Fixes: 372c187b8a70 ("scsi: lpfc: Add an internal trace log buffer")
CC: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Dick Kennedy <dick.kennedy@broadcom.com>
Signed-off-by: James Smart <jsmart2021@gmail.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Addresses-Coverity: ("Unsigned compared against 0")
Acked-by: Daniel Wagner <dwagner@suse.de>
---
 drivers/scsi/lpfc/lpfc_init.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -14173,12 +14173,10 @@ void lpfc_dmp_dbg(struct lpfc_hba *phba)
 		if ((start_idx + dbg_cnt) > (DBG_LOG_SZ - 1)) {
 			temp_idx = (start_idx + dbg_cnt) % DBG_LOG_SZ;
 		} else {
-			if ((start_idx - dbg_cnt) < 0) {
+			if (start_idx < dbg_cnt)
 				start_idx = DBG_LOG_SZ - (dbg_cnt - start_idx);
-				temp_idx = 0;
-			} else {
+			else
 				start_idx -= dbg_cnt;
-			}
 		}
 	}
 	dev_info(&phba->pcidev->dev, "start %d end %d cnt %d\n",
@@ -14186,7 +14184,7 @@ void lpfc_dmp_dbg(struct lpfc_hba *phba)
 
 	for (i = 0; i < dbg_cnt; i++) {
 		if ((start_idx + i) < DBG_LOG_SZ)
-			temp_idx = (start_idx + i) % (DBG_LOG_SZ - 1);
+			temp_idx = (start_idx + i) % DBG_LOG_SZ;
 		else
 			temp_idx = j++;
 		rem_nsec = do_div(phba->dbg_log[temp_idx].t_ns, NSEC_PER_SEC);