diff mbox series

[v2] scsi: qedf: Use kcalloc() and add check for bdt_info

Message ID 20250131213516.7750-1-jiashengjiangcool@gmail.com (mailing list archive)
State Superseded
Headers show
Series [v2] scsi: qedf: Use kcalloc() and add check for bdt_info | expand

Commit Message

Jiasheng Jiang Jan. 31, 2025, 9:35 p.m. UTC
Replace kmalloc_array() with kcalloc() to avoid old (dirty) data being
used/freed.
Moreover, add a check for "bdt_info". Otherwise, if one of the allocations
for cmgr->io_bdt_pool[i] fails, "bdt_info->bd_tbl" will cause a NULL
pointer dereference.

Fixes: 61d8658b4a43 ("scsi: qedf: Add QLogic FastLinQ offload FCoE driver framework.")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
---
Changelog:

v1 -> v2:

1. Replace kzalloc() with kcalloc().
---
 drivers/scsi/qedf/qedf_io.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Markus Elfring Feb. 2, 2025, 3:22 p.m. UTC | #1
> Replace kmalloc_array() with kcalloc() to avoid old (dirty) data being
> used/freed.
> Moreover, add a check for "bdt_info". Otherwise, if one of the allocations
…

Please provide desired changes as separate update steps.

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.13#n81

Regards,
Markus
Markus Elfring Feb. 2, 2025, 4:54 p.m. UTC | #2
> +++ b/drivers/scsi/qedf/qedf_io.c
…
@@ -254,9 +254,7 @@ struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
 	}

 	/* Allocate pool of io_bdts - one for each qedf_ioreq */
…
+	cmgr->io_bdt_pool = kcalloc(num_ios, sizeof(struct io_bdt *), GFP_KERNEL);
…

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.13#n941

Regards,
Markus
Jiasheng Jiang Feb. 2, 2025, 9:33 p.m. UTC | #3
Hi Markus,

On Sun, Feb 2, 2025 at 11:54 AM Markus Elfring <Markus.Elfring@web.de> wrote:
>
> …
> > +++ b/drivers/scsi/qedf/qedf_io.c
> …
> @@ -254,9 +254,7 @@ struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
>         }
>
>         /* Allocate pool of io_bdts - one for each qedf_ioreq */
> …
> +       cmgr->io_bdt_pool = kcalloc(num_ios, sizeof(struct io_bdt *), GFP_KERNEL);
> …
>
> See also:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.13#n941
>
> Regards,
> Markus

Thanks, I have split it into two new patches and fixed the error.

-Jiasheng
diff mbox series

Patch

diff --git a/drivers/scsi/qedf/qedf_io.c b/drivers/scsi/qedf/qedf_io.c
index fcfc3bed02c6..abb459e87a86 100644
--- a/drivers/scsi/qedf/qedf_io.c
+++ b/drivers/scsi/qedf/qedf_io.c
@@ -125,7 +125,7 @@  void qedf_cmd_mgr_free(struct qedf_cmd_mgr *cmgr)
 	bd_tbl_sz = QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge);
 	for (i = 0; i < num_ios; i++) {
 		bdt_info = cmgr->io_bdt_pool[i];
-		if (bdt_info->bd_tbl) {
+		if (bdt_info && bdt_info->bd_tbl) {
 			dma_free_coherent(&qedf->pdev->dev, bd_tbl_sz,
 			    bdt_info->bd_tbl, bdt_info->bd_tbl_dma);
 			bdt_info->bd_tbl = NULL;
@@ -254,9 +254,7 @@  struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
 	}
 
 	/* Allocate pool of io_bdts - one for each qedf_ioreq */
-	cmgr->io_bdt_pool = kmalloc_array(num_ios, sizeof(struct io_bdt *),
-	    GFP_KERNEL);
-
+	cmgr->io_bdt_pool = kcalloc(num_ios, sizeof(struct io_bdt *), GFP_KERNEL);
 	if (!cmgr->io_bdt_pool) {
 		QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc io_bdt_pool.\n");
 		goto mem_err;