[PATCH v3] scsi: lpfc: drop orphaned continuation IOCB in lpfc_sli_process_unsol_iocb()

From: Ziheng Wei

Date: Sun Dec 06 2026 - 06:53:37 EST


In lpfc_sli_process_unsol_iocb(), if list_for_each_entry() does not find
a matching XRI on pring->iocb_continue_saveq, iocbq points to a type-
confused container_of() address derived from the list head. When
saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP, the code still executes
list_del_init(&iocbq->clist) and assigns saveq = iocbq, corrupting the
list head and dereferencing invalid memory.

Reaching the non-intermediate branch with !found only happens for an
orphaned CMD_IOCB_RCV_CONT64_CX continuation IOCB whose sequence head is
missing from pring->iocb_continue_saveq, so it should not be dispatched
downstream as a sequence-start IOCB.

When !found and saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP, release the
receive buffers retrieved for saveq (and any chained HBQ entries on
saveq->list) and return 1 so the caller frees the orphaned IOCB:
- Mark diagnostic loopback buffers posted by
lpfcdiag_sli3_loop_post_rxbufs() with QUE_BUFTAG_BIT in non-HBQ mode as
well (and zero-initialize normal postbufq entries in
lpfc_sli3_post_buffer()), and free QUE_BUFTAG_BIT buffers as struct
lpfc_dmabufext via lpfc_bsg_diag_buf_free() instead of passing them to
lpfc_in_buf_free().
- Free normal receive buffers via lpfc_in_buf_free() and replenish the
ring with lpfc_sli3_post_buffer() in non-HBQ mode.
When !found and ulpStatus == IOSTAT_INTERMED_RSP, queue saveq onto
iocb_continue_saveq and return 0 directly.

Found by LLM-assisted static analysis. Compile-tested on x86_64.

Fixes: 9c2face68782 ("[SCSI] lpfc 8.2.4 : Fix Unsolicited Data items")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
v2 -> v3:
- Distinguish diagnostic loopback lpfc_dmabufext buffers (QUE_BUFTAG_BIT
in both HBQ and non-HBQ modes, with normal non-HBQ buffers zero-
allocated in lpfc_sli3_post_buffer()) and release them via
lpfc_bsg_diag_buf_free() instead of lpfc_in_buf_free().
- Replenish non-HBQ ring receive buffers with lpfc_sli3_post_buffer()
after freeing normal buffers for dropped orphans.
- v2: https://lore.kernel.org/r/20261206090313.10315-1-zihengw03@xxxxxxx

v1 -> v2:
- Drop orphaned CMD_IOCB_RCV_CONT64_CX continuation IOCBs by freeing
their receive buffers and returning 1 instead of setting iocbq = saveq
and dispatching them downstream as sequence-start IOCBs.
- v1: https://lore.kernel.org/r/20261205074457.84293-1-zihengw03@xxxxxxx

drivers/scsi/lpfc/lpfc_bsg.c | 12 +++++++
drivers/scsi/lpfc/lpfc_crtn.h | 1 +
drivers/scsi/lpfc/lpfc_init.c | 4 +--
drivers/scsi/lpfc/lpfc_sli.c | 59 ++++++++++++++++++++++++++++++++++-
4 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/lpfc/lpfc_bsg.c b/drivers/scsi/lpfc/lpfc_bsg.c
index 7354ae9ba..d2df43313 100644
--- a/drivers/scsi/lpfc/lpfc_bsg.c
+++ b/drivers/scsi/lpfc/lpfc_bsg.c
@@ -892,6 +892,17 @@ diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
return 0;
}

+void lpfc_bsg_diag_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
+{
+ struct lpfc_dmabufext *ext = container_of(mp, struct lpfc_dmabufext,
+ dma);
+
+ if (ext->dma.virt)
+ dma_free_coherent(&phba->pcidev->dev, ext->size,
+ ext->dma.virt, ext->dma.phys);
+ kfree(ext);
+}
+
/*
* lpfc_bsg_ct_unsol_event - process an unsolicited CT command
*
@@ -2950,6 +2961,7 @@ static int lpfcdiag_sli3_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;

} else {
+ mp[i]->buffer_tag = QUE_BUFTAG_BIT;
cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
cmd->un.cont64[i].tus.f.bdeSize =
diff --git a/drivers/scsi/lpfc/lpfc_crtn.h b/drivers/scsi/lpfc/lpfc_crtn.h
index 8a5b76bde..52caf8b92 100644
--- a/drivers/scsi/lpfc/lpfc_crtn.h
+++ b/drivers/scsi/lpfc/lpfc_crtn.h
@@ -439,6 +439,7 @@ void *lpfc_nvmet_buf_alloc(struct lpfc_hba *phba, int flags,
void lpfc_nvmet_buf_free(struct lpfc_hba *phba, void *virtp, dma_addr_t dma);

void lpfc_in_buf_free(struct lpfc_hba *, struct lpfc_dmabuf *);
+void lpfc_bsg_diag_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp);
void lpfc_rq_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp);
void lpfc_setup_fdmi_mask(struct lpfc_vport *vport);
int lpfc_link_reset(struct lpfc_vport *vport);
diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index 6df5f4e7c..497e52e93 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -2828,7 +2828,7 @@ lpfc_sli3_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cn

/* 2 buffers can be posted per command */
/* Allocate buffer to post */
- mp1 = kmalloc_obj(struct lpfc_dmabuf);
+ mp1 = kzalloc_obj(struct lpfc_dmabuf);
if (mp1)
mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
if (!mp1 || !mp1->virt) {
@@ -2841,7 +2841,7 @@ lpfc_sli3_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cn
INIT_LIST_HEAD(&mp1->list);
/* Allocate buffer to post */
if (cnt > 1) {
- mp2 = kmalloc_obj(struct lpfc_dmabuf);
+ mp2 = kzalloc_obj(struct lpfc_dmabuf);
if (mp2)
mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
&mp2->phys);
diff --git a/drivers/scsi/lpfc/lpfc_sli.c b/drivers/scsi/lpfc/lpfc_sli.c
index 9173263c8..10eb72dc1 100644
--- a/drivers/scsi/lpfc/lpfc_sli.c
+++ b/drivers/scsi/lpfc/lpfc_sli.c
@@ -3417,6 +3417,56 @@ lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,
}
}

+static int
+lpfc_sli_free_unsol_buf(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
+{
+ if (!mp)
+ return 0;
+
+ if (mp->buffer_tag & QUE_BUFTAG_BIT) {
+ lpfc_bsg_diag_buf_free(phba, mp);
+ return 0;
+ }
+
+ lpfc_in_buf_free(phba, mp);
+ return !(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED);
+}
+
+static void
+lpfc_sli_free_unsol_iocb_bufs(struct lpfc_hba *phba,
+ struct lpfc_sli_ring *pring,
+ struct lpfc_iocbq *saveq)
+{
+ struct lpfc_iocbq *iocbq;
+ int repost_cnt = 0;
+
+ if (saveq->iocb.ulpBdeCount != 0 ||
+ !(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
+ repost_cnt += lpfc_sli_free_unsol_buf(phba, saveq->cmd_dmabuf);
+ saveq->cmd_dmabuf = NULL;
+ }
+ if (saveq->iocb.ulpBdeCount == 2) {
+ repost_cnt += lpfc_sli_free_unsol_buf(phba, saveq->bpl_dmabuf);
+ saveq->bpl_dmabuf = NULL;
+ }
+ if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
+ list_for_each_entry(iocbq, &saveq->list, list) {
+ if (iocbq->iocb.ulpBdeCount != 0) {
+ lpfc_sli_free_unsol_buf(phba,
+ iocbq->cmd_dmabuf);
+ iocbq->cmd_dmabuf = NULL;
+ }
+ if (iocbq->iocb.ulpBdeCount == 2) {
+ lpfc_sli_free_unsol_buf(phba,
+ iocbq->bpl_dmabuf);
+ iocbq->bpl_dmabuf = NULL;
+ }
+ }
+ } else if (repost_cnt) {
+ lpfc_sli3_post_buffer(phba, pring, repost_cnt);
+ }
+}
+
/**
* lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
* @phba: Pointer to HBA context object.
@@ -3566,9 +3616,16 @@ lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
break;
}
}
- if (!found)
+ if (!found) {
+ if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
+ lpfc_sli_free_unsol_iocb_bufs(phba, pring,
+ saveq);
+ return 1;
+ }
list_add_tail(&saveq->clist,
&pring->iocb_continue_saveq);
+ return 0;
+ }

if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
list_del_init(&iocbq->clist);

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
--
2.43.0