* [PATCH v2 0/3] scsi: lpfc: Fix multiple memory leaks in error paths
@ 2025-12-30 14:58 Zilin Guan
2025-12-30 14:58 ` [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post() Zilin Guan
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Zilin Guan @ 2025-12-30 14:58 UTC (permalink / raw)
To: justin.tee
Cc: paul.ely, James.Bottomley, martin.petersen, Markus.Elfring,
jianhao.xu, linux-scsi, linux-kernel, Zilin Guan
This series fixes three independent memory leaks in the lpfc driver.
All of them occur in error handling paths where allocated memory
was not properly freed before returning.
These issues were identified during static analysis.
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
Changes in v2:
- Patch 1: Refactor error handling to use a goto label for cleanup.
- Patch 2: No changes.
- Patch 3: No changes.
Zilin Guan (3):
scsi: lpfc: Fix memory leak in lpfc_config_port_post()
scsi: lpfc: Fix memory leak in lpfc_sli4_driver_resource_setup()
scsi: lpfc: Fix memory leak in lpfc_cmpl_plogi_plogi_issue()
drivers/scsi/lpfc/lpfc_init.c | 21 +++++++++++----------
drivers/scsi/lpfc/lpfc_nportdisc.c | 4 +++-
2 files changed, 14 insertions(+), 11 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post()
2025-12-30 14:58 [PATCH v2 0/3] scsi: lpfc: Fix multiple memory leaks in error paths Zilin Guan
@ 2025-12-30 14:58 ` Zilin Guan
2025-12-30 16:01 ` Markus Elfring
2025-12-30 14:58 ` [PATCH v2 2/3] scsi: lpfc: Fix memory leak in lpfc_sli4_driver_resource_setup() Zilin Guan
2025-12-30 14:58 ` [PATCH v2 3/3] scsi: lpfc: Fix memory leak in lpfc_cmpl_plogi_plogi_issue() Zilin Guan
2 siblings, 1 reply; 6+ messages in thread
From: Zilin Guan @ 2025-12-30 14:58 UTC (permalink / raw)
To: justin.tee
Cc: paul.ely, James.Bottomley, martin.petersen, Markus.Elfring,
jianhao.xu, linux-scsi, linux-kernel, Zilin Guan
In lpfc_config_port_post(), pmb is allocated via mempool_alloc() but
is not freed when lpfc_readl() fails.
Instead of simply adding the missing free, refactor the error handling
to use a goto label. This unifies the cleanup logic and ensures pmb is
freed in all error paths.
Fixes: 9940b97bb30d ("[SCSI] lpfc 8.3.22: Add support for PCI Adapter Failure")
Suggested-by: Markus Elfring <Markus.Elfring@web.de>
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
v2:
- Refactor error handling to use a goto label for cleanup.
drivers/scsi/lpfc/lpfc_init.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index b1460b16dd91..1390d2b5095d 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -511,8 +511,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
"READ_CONFIG, mbxStatus x%x\n",
mb->mbxCommand, mb->mbxStatus);
phba->link_state = LPFC_HBA_ERROR;
- mempool_free( pmb, phba->mbox_mem_pool);
- return -EIO;
+ goto out_free;
}
/* Check if the port is disabled */
@@ -550,8 +549,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
if (phba->intr_type == MSIX) {
rc = lpfc_config_msi(phba, pmb);
if (rc) {
- mempool_free(pmb, phba->mbox_mem_pool);
- return -EIO;
+ goto out_free;
}
rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
if (rc != MBX_SUCCESS) {
@@ -560,8 +558,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
"failed, mbxCmd x%x, mbxStatus x%x\n",
pmb->u.mb.mbxCommand,
pmb->u.mb.mbxStatus);
- mempool_free(pmb, phba->mbox_mem_pool);
- return -EIO;
+ goto out_free;
}
}
@@ -572,7 +569,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
/* Enable appropriate host interrupts */
if (lpfc_readl(phba->HCregaddr, &status)) {
spin_unlock_irq(&phba->hbalock);
- return -EIO;
+ goto out_free;
}
status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
if (psli->num_rings > 0)
@@ -616,9 +613,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
"2599 Adapter failed to issue DOWN_LINK"
" mbox command rc 0x%x\n", rc);
-
- mempool_free(pmb, phba->mbox_mem_pool);
- return -EIO;
+ goto out_free;
}
} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
mempool_free(pmb, phba->mbox_mem_pool);
@@ -666,6 +661,10 @@ lpfc_config_port_post(struct lpfc_hba *phba)
}
return 0;
+
+out_free:
+ mempool_free(pmb, phba->mbox_mem_pool);
+ return -EIO;
}
/**
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/3] scsi: lpfc: Fix memory leak in lpfc_sli4_driver_resource_setup()
2025-12-30 14:58 [PATCH v2 0/3] scsi: lpfc: Fix multiple memory leaks in error paths Zilin Guan
2025-12-30 14:58 ` [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post() Zilin Guan
@ 2025-12-30 14:58 ` Zilin Guan
2025-12-30 14:58 ` [PATCH v2 3/3] scsi: lpfc: Fix memory leak in lpfc_cmpl_plogi_plogi_issue() Zilin Guan
2 siblings, 0 replies; 6+ messages in thread
From: Zilin Guan @ 2025-12-30 14:58 UTC (permalink / raw)
To: justin.tee
Cc: paul.ely, James.Bottomley, martin.petersen, Markus.Elfring,
jianhao.xu, linux-scsi, linux-kernel, Zilin Guan
In lpfc_sli4_driver_resource_setup(), mboxq is allocated via
mempool_alloc() but is not freed when the allocation of
phba->lpfc_sg_dma_buf_pool or phba->lpfc_cmd_rsp_buf_pool fails.
Fix this by adding mempool_free() in the error paths.
Fixes: d79c9e9d4b3d ("scsi: lpfc: Support dynamic unbounded SGL lists on G7 hardware.")
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
drivers/scsi/lpfc/lpfc_init.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index 1390d2b5095d..54a8d290c2e1 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -8295,6 +8295,7 @@ lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
i, 0);
if (!phba->lpfc_sg_dma_buf_pool) {
rc = -ENOMEM;
+ mempool_free(mboxq, phba->mbox_mem_pool);
goto out_free_bsmbx;
}
@@ -8306,6 +8307,7 @@ lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
i, 0);
if (!phba->lpfc_cmd_rsp_buf_pool) {
rc = -ENOMEM;
+ mempool_free(mboxq, phba->mbox_mem_pool);
goto out_free_sg_dma_buf;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/3] scsi: lpfc: Fix memory leak in lpfc_cmpl_plogi_plogi_issue()
2025-12-30 14:58 [PATCH v2 0/3] scsi: lpfc: Fix multiple memory leaks in error paths Zilin Guan
2025-12-30 14:58 ` [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post() Zilin Guan
2025-12-30 14:58 ` [PATCH v2 2/3] scsi: lpfc: Fix memory leak in lpfc_sli4_driver_resource_setup() Zilin Guan
@ 2025-12-30 14:58 ` Zilin Guan
2 siblings, 0 replies; 6+ messages in thread
From: Zilin Guan @ 2025-12-30 14:58 UTC (permalink / raw)
To: justin.tee
Cc: paul.ely, James.Bottomley, martin.petersen, Markus.Elfring,
jianhao.xu, linux-scsi, linux-kernel, Zilin Guan
In lpfc_cmpl_plogi_plogi_issue(), mbox is allocated via
mempool_alloc() and initialized. However, if lpfc_nlp_get() fails,
the mbox and its resources are not freed.
Fix this by using lpfc_mbox_rsrc_cleanup() to free the mailbox and
its associated resources.
Fixes: 4430f7fd09ec ("scsi: lpfc: Rework locations of ndlp reference taking")
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
drivers/scsi/lpfc/lpfc_nportdisc.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/lpfc/lpfc_nportdisc.c b/drivers/scsi/lpfc/lpfc_nportdisc.c
index 8240d59f4120..e81640223276 100644
--- a/drivers/scsi/lpfc/lpfc_nportdisc.c
+++ b/drivers/scsi/lpfc/lpfc_nportdisc.c
@@ -1505,8 +1505,10 @@ lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
}
mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
- if (!mbox->ctx_ndlp)
+ if (!mbox->ctx_ndlp) {
+ lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
goto out;
+ }
mbox->vport = vport;
if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post()
2025-12-30 14:58 ` [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post() Zilin Guan
@ 2025-12-30 16:01 ` Markus Elfring
2025-12-31 15:07 ` Zilin Guan
0 siblings, 1 reply; 6+ messages in thread
From: Markus Elfring @ 2025-12-30 16:01 UTC (permalink / raw)
To: Jianhao Xu, Zilin Guan, linux-scsi
Cc: LKML, James Bottomley, Justin Tee, Martin K. Petersen, Paul Ely
…
> +++ b/drivers/scsi/lpfc/lpfc_init.c
…
> @@ -550,8 +549,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
> if (phba->intr_type == MSIX) {
> rc = lpfc_config_msi(phba, pmb);
> if (rc) {
> - mempool_free(pmb, phba->mbox_mem_pool);
> - return -EIO;
> + goto out_free;
> }
You may omit curly brackets here.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc3#n197
> rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
> if (rc != MBX_SUCCESS) {
…
Regards,
Markus
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post()
2025-12-30 16:01 ` Markus Elfring
@ 2025-12-31 15:07 ` Zilin Guan
0 siblings, 0 replies; 6+ messages in thread
From: Zilin Guan @ 2025-12-31 15:07 UTC (permalink / raw)
To: markus.elfring
Cc: James.Bottomley, jianhao.xu, justin.tee, linux-kernel, linux-scsi,
martin.petersen, paul.ely, zilin
On Tue, Dec 30, 2025 at 05:01:32PM+0100, Markus Elfring wrote:
> …
> > +++ b/drivers/scsi/lpfc/lpfc_init.c
> …
> > @@ -550,8 +549,7 @@ lpfc_config_port_post(struct lpfc_hba *phba)
> > if (phba->intr_type == MSIX) {
> > rc = lpfc_config_msi(phba, pmb);
> > if (rc) {
> > - mempool_free(pmb, phba->mbox_mem_pool);
> > - return -EIO;
> > + goto out_free;
> > }
>
> You may omit curly brackets here.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc3#n197
Hi Markus,
Thanks for the review.
I missed this one during the refactoring. I will remove the unnecessary
braces in v3.
Best regards,
Zilin Guan
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-12-31 15:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-30 14:58 [PATCH v2 0/3] scsi: lpfc: Fix multiple memory leaks in error paths Zilin Guan
2025-12-30 14:58 ` [PATCH v2 1/3] scsi: lpfc: Fix memory leak in lpfc_config_port_post() Zilin Guan
2025-12-30 16:01 ` Markus Elfring
2025-12-31 15:07 ` Zilin Guan
2025-12-30 14:58 ` [PATCH v2 2/3] scsi: lpfc: Fix memory leak in lpfc_sli4_driver_resource_setup() Zilin Guan
2025-12-30 14:58 ` [PATCH v2 3/3] scsi: lpfc: Fix memory leak in lpfc_cmpl_plogi_plogi_issue() Zilin Guan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox