Skip to contentSkip to conversation

scsi: lpfc: avoid usage of list iterator variable after loop

PATCH Applied Jakob Koschel 3 participants 3 messages Mar 1, 2023 – Mar 7, 2023

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

PATCH 1/1 scsi: lpfc: avoid usage of list iterator variable after loop

If the &epd_pool->list is empty when executing lpfc_get_io_buf_from_expedite_pool() the function would return an invalid pointer. Even in the case if the list is guaranteed to be populated, the iterator variable should not be used after the loop to be more robust for future changes.

Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after the loop [1].

---
 drivers/scsi/lpfc/lpfc_sli.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_sli.c +4 -4
index edbd81c3b643..5d06bf6d4f39 100644
@@ -21899,20 +21899,20 @@ lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
 static struct lpfc_io_buf *
 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
 {
-	struct lpfc_io_buf *lpfc_ncmd;
+	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
 	struct lpfc_io_buf *lpfc_ncmd_next;
 	unsigned long iflag;
 	struct lpfc_epd_pool *epd_pool;
 
 	epd_pool = &phba->epd_pool;
-	lpfc_ncmd = NULL;
 
 	spin_lock_irqsave(&epd_pool->lock, iflag);
 	if (epd_pool->count > 0) {
-		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
+		list_for_each_entry_safe(iter, lpfc_ncmd_next,
 					 &epd_pool->list, list) {
-			list_del(&lpfc_ncmd->list);
+			list_del(&iter->list);
 			epd_pool->count--;
+			lpfc_ncmd = iter;
 			break;
 		}
 	}

---
base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-scsi-lpfc-avoid-list-iterator-after-loop-7b7d5c3a8efc

Best regards,

Signature · 1 line
Jakob Koschel <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 51 lines · Jakob Koschel

If the &epd_pool->list is empty when executing lpfc_get_io_buf_from_expedite_pool() the function would return an invalid pointer. Even in the case if the list is guaranteed to be populated, the iterator variable should not be used after the loop to be more robust for future changes.

Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after the loop [1].

---
 drivers/scsi/lpfc/lpfc_sli.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/lpfc/lpfc_sli.c +4 -5
index edbd81c3b643..5d06bf6d4f39 100644
@@ -21899,20 +21899,20 @@ lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
 static struct lpfc_io_buf *
 lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
 {
-       struct lpfc_io_buf *lpfc_ncmd;
+       struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
        struct lpfc_io_buf *lpfc_ncmd_next;
        unsigned long iflag;
        struct lpfc_epd_pool *epd_pool;
 
        epd_pool = &phba->epd_pool;
-       lpfc_ncmd = NULL;
 
        spin_lock_irqsave(&epd_pool->lock, iflag);
        if (epd_pool->count > 0) {
-               list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
+               list_for_each_entry_safe(iter, lpfc_ncmd_next,
                                         &epd_pool->list, list) {
-                       list_del(&lpfc_ncmd->list);
+                       list_del(&iter->list);
                        epd_pool->count--;
+                       lpfc_ncmd = iter;
                        break;
                }
        }
 
---

base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-scsi-lpfc-avoid-list-iterator-after-loop-7b7d5c3a8efc

Quoted 4 lines

Best regards,
--
Jakob Koschel <[email protected]>

Thanks looks fine.

Regards,
Justin

#2 lore raw
In reply to Jakob Koschel
Quoted 11 lines · Jakob Koschel

If the &epd_pool->list is empty when executing lpfc_get_io_buf_from_expedite_pool() the function would return an invalid pointer. Even in the case if the list is guaranteed to be populated, the iterator variable should not be used after the loop to be more robust for future changes.

Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the marcro to avoid any potential misuse after the loop [1].

[...]

Applied to 6.3/scsi-fixes, thanks!

[1/1] scsi: lpfc: avoid usage of list iterator variable after loop

      https://git.kernel.org/mkp/scsi/c/2850b23e9f9a
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering