2024-02-08 09:37:39

by Fedor Pchelkin

[permalink] [raw]
Subject: [PATCH] scsi: elx: efct: adjust error handling inside efct_hw_setup_io

IO and WQE buffers are allocated once per HW and can be reused later. If
WQE buffers allocation fails then the whole allocation is marked as failed
but already created IO array internal objects are not freed. hw->io is
freed but not nullified in that specific case - it may become a problem
later as efct_hw_setup_io() is supposed to be reusable for the same HW.

While at it, use kcalloc instead of kmalloc_array/memset-zero combination
and get rid of some needless NULL assignments: nullifying hw->io[i]
elements just before freeing hw->io is not really useful.

Found by Linux Verification Center (linuxtesting.org).

Fixes: 4df84e846624 ("scsi: elx: efct: Driver initialization routines")
Cc: [email protected]
Signed-off-by: Fedor Pchelkin <[email protected]>
---
drivers/scsi/elx/efct/efct_hw.c | 14 ++++----------
1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/drivers/scsi/elx/efct/efct_hw.c b/drivers/scsi/elx/efct/efct_hw.c
index 5a5525054d71..e5486e6949f9 100644
--- a/drivers/scsi/elx/efct/efct_hw.c
+++ b/drivers/scsi/elx/efct/efct_hw.c
@@ -487,12 +487,10 @@ efct_hw_setup_io(struct efct_hw *hw)
struct efct *efct = hw->os;

if (!hw->io) {
- hw->io = kmalloc_array(hw->config.n_io, sizeof(io), GFP_KERNEL);
+ hw->io = kcalloc(hw->config.n_io, sizeof(io), GFP_KERNEL);
if (!hw->io)
return -ENOMEM;

- memset(hw->io, 0, hw->config.n_io * sizeof(io));
-
for (i = 0; i < hw->config.n_io; i++) {
hw->io[i] = kzalloc(sizeof(*io), GFP_KERNEL);
if (!hw->io[i])
@@ -502,10 +500,8 @@ efct_hw_setup_io(struct efct_hw *hw)
/* Create WQE buffs for IO */
hw->wqe_buffs = kzalloc((hw->config.n_io * hw->sli.wqe_size),
GFP_KERNEL);
- if (!hw->wqe_buffs) {
- kfree(hw->io);
- return -ENOMEM;
- }
+ if (!hw->wqe_buffs)
+ goto error;

} else {
/* re-use existing IOs, including SGLs */
@@ -586,10 +582,8 @@ efct_hw_setup_io(struct efct_hw *hw)

return 0;
error:
- for (i = 0; i < hw->config.n_io && hw->io[i]; i++) {
+ for (i = 0; i < hw->config.n_io && hw->io[i]; i++)
kfree(hw->io[i]);
- hw->io[i] = NULL;
- }

kfree(hw->io);
hw->io = NULL;
--
2.39.2



2024-02-09 06:24:34

by Daniel Wagner

[permalink] [raw]
Subject: Re: [PATCH] scsi: elx: efct: adjust error handling inside efct_hw_setup_io

On Thu, Feb 08, 2024 at 12:36:57PM +0300, Fedor Pchelkin wrote:
> IO and WQE buffers are allocated once per HW and can be reused later. If
> WQE buffers allocation fails then the whole allocation is marked as failed
> but already created IO array internal objects are not freed. hw->io is
> freed but not nullified in that specific case - it may become a problem
> later as efct_hw_setup_io() is supposed to be reusable for the same HW.
>
> While at it, use kcalloc instead of kmalloc_array/memset-zero combination
> and get rid of some needless NULL assignments: nullifying hw->io[i]
> elements just before freeing hw->io is not really useful.
>
> Found by Linux Verification Center (linuxtesting.org).
>
> Fixes: 4df84e846624 ("scsi: elx: efct: Driver initialization routines")
> Cc: [email protected]
> Signed-off-by: Fedor Pchelkin <[email protected]>

The patch looks okay. Though I think this funktion leaks all over the
place memory as soon we take the error path. Could you also prepare
a fix for these path while you are at it?

Thanks!
Daniel

2024-02-10 11:10:21

by Fedor Pchelkin

[permalink] [raw]
Subject: [PATCH v2] scsi: elx: efct: adjust error handling inside efct_hw_setup_io

IO and WQE buffers are allocated once per HW and can be reused later. If
WQE buffers allocation fails then the whole allocation is marked as failed
but already created IO array internal objects are not freed. hw->io is
freed but not nullified in that specific case - it may become a problem
later as efct_hw_setup_io() is supposed to be reusable for the same HW.

Also rollback if HW IO objects initialization loop fails due to memory
allocation error.

While at it, use kcalloc instead of kmalloc_array/memset-zero combination
and get rid of some needless NULL assignments: nullifying hw->io[i]
elements just before freeing hw->io is not really useful.

Found by Linux Verification Center (linuxtesting.org).

Fixes: 4df84e846624 ("scsi: elx: efct: Driver initialization routines")
Cc: [email protected]
Suggested-by: Daniel Wagner <[email protected]>
Signed-off-by: Fedor Pchelkin <[email protected]>
---
v2: per Daniel Wagner's notice, handle the other possible memory
allocation errors inside the function.

drivers/scsi/elx/efct/efct_hw.c | 66 +++++++++++++++++++++++----------
1 file changed, 47 insertions(+), 19 deletions(-)

diff --git a/drivers/scsi/elx/efct/efct_hw.c b/drivers/scsi/elx/efct/efct_hw.c
index 5a5525054d71..a0871a53f71d 100644
--- a/drivers/scsi/elx/efct/efct_hw.c
+++ b/drivers/scsi/elx/efct/efct_hw.c
@@ -485,27 +485,24 @@ efct_hw_setup_io(struct efct_hw *hw)
bool new_alloc = true;
struct efc_dma *dma;
struct efct *efct = hw->os;
+ int err = -ENOMEM;

if (!hw->io) {
- hw->io = kmalloc_array(hw->config.n_io, sizeof(io), GFP_KERNEL);
+ hw->io = kcalloc(hw->config.n_io, sizeof(io), GFP_KERNEL);
if (!hw->io)
- return -ENOMEM;
-
- memset(hw->io, 0, hw->config.n_io * sizeof(io));
+ return err;

for (i = 0; i < hw->config.n_io; i++) {
hw->io[i] = kzalloc(sizeof(*io), GFP_KERNEL);
if (!hw->io[i])
- goto error;
+ goto err_alloc_io;
}

/* Create WQE buffs for IO */
hw->wqe_buffs = kzalloc((hw->config.n_io * hw->sli.wqe_size),
GFP_KERNEL);
- if (!hw->wqe_buffs) {
- kfree(hw->io);
- return -ENOMEM;
- }
+ if (!hw->wqe_buffs)
+ goto err_alloc_io;

} else {
/* re-use existing IOs, including SGLs */
@@ -517,8 +514,10 @@ efct_hw_setup_io(struct efct_hw *hw)
dma->size = sizeof(struct fcp_txrdy) * hw->config.n_io;
dma->virt = dma_alloc_coherent(&efct->pci->dev,
dma->size, &dma->phys, GFP_KERNEL);
- if (!dma->virt)
- return -ENOMEM;
+ if (!dma->virt) {
+ memset(&hw->xfer_rdy, 0, sizeof(struct efc_dma));
+ goto err_alloc_wqe;
+ }
}
xfer_virt = (uintptr_t)hw->xfer_rdy.virt;
xfer_phys = hw->xfer_rdy.phys;
@@ -539,7 +538,8 @@ efct_hw_setup_io(struct efct_hw *hw)
wqcb = efct_hw_reqtag_alloc(hw, efct_hw_wq_process_io, io);
if (!wqcb) {
efc_log_err(hw->os, "can't allocate request tag\n");
- return -ENOSPC;
+ err = -ENOSPC;
+ goto err_init_io;
}
io->reqtag = wqcb->instance_index;

@@ -553,7 +553,8 @@ efct_hw_setup_io(struct efct_hw *hw)
&io->indicator, &index)) {
efc_log_err(hw->os,
"sli_resource_alloc failed @ %d\n", i);
- return -ENOMEM;
+ efct_hw_reqtag_free(hw, wqcb);
+ goto err_init_io;
}

if (new_alloc) {
@@ -567,7 +568,10 @@ efct_hw_setup_io(struct efct_hw *hw)
efc_log_err(hw->os, "dma_alloc fail %d\n", i);
memset(&io->def_sgl, 0,
sizeof(struct efc_dma));
- return -ENOMEM;
+ sli_resource_free(&hw->sli, SLI4_RSRC_XRI,
+ io->indicator);
+ efct_hw_reqtag_free(hw, wqcb);
+ goto err_init_io;
}
}
io->def_sgl_count = hw->config.n_sgl;
@@ -585,16 +589,40 @@ efct_hw_setup_io(struct efct_hw *hw)
}

return 0;
-error:
- for (i = 0; i < hw->config.n_io && hw->io[i]; i++) {
- kfree(hw->io[i]);
- hw->io[i] = NULL;
+
+err_init_io:
+ for (u32 j = 0; j < i; j++) {
+ struct hw_wq_callback *wqcb;
+
+ io = hw->io[j];
+ wqcb = efct_hw_reqtag_get_instance(hw, io->reqtag);
+
+ if (new_alloc) {
+ dma = &io->def_sgl;
+ dma_free_coherent(&efct->pci->dev, dma->size,
+ dma->virt, dma->phys);
+ memset(&io->def_sgl, 0, sizeof(struct efc_dma));
+ }
+ sli_resource_free(&hw->sli, SLI4_RSRC_XRI, io->indicator);
+ efct_hw_reqtag_free(hw, wqcb);
}
+ if (new_alloc) {
+ dma = &hw->xfer_rdy;
+ dma_free_coherent(&efct->pci->dev, dma->size, dma->virt,
+ dma->phys);
+ memset(&hw->xfer_rdy, 0, sizeof(struct efc_dma));
+ }
+err_alloc_wqe:
+ kfree(hw->wqe_buffs);
+ hw->wqe_buffs = NULL;
+err_alloc_io:
+ for (i = 0; i < hw->config.n_io && hw->io[i]; i++)
+ kfree(hw->io[i]);

kfree(hw->io);
hw->io = NULL;

- return -ENOMEM;
+ return err;
}

static int
--
2.39.2


2024-02-12 09:55:36

by Daniel Wagner

[permalink] [raw]
Subject: Re: [PATCH v2] scsi: elx: efct: adjust error handling inside efct_hw_setup_io

On Sat, Feb 10, 2024 at 02:08:33PM +0300, Fedor Pchelkin wrote:
> IO and WQE buffers are allocated once per HW and can be reused later. If
> WQE buffers allocation fails then the whole allocation is marked as failed
> but already created IO array internal objects are not freed. hw->io is
> freed but not nullified in that specific case - it may become a problem
> later as efct_hw_setup_io() is supposed to be reusable for the same HW.
>
> Also rollback if HW IO objects initialization loop fails due to memory
> allocation error.
>
> While at it, use kcalloc instead of kmalloc_array/memset-zero combination
> and get rid of some needless NULL assignments: nullifying hw->io[i]
> elements just before freeing hw->io is not really useful.
>
> Found by Linux Verification Center (linuxtesting.org).
>
> Fixes: 4df84e846624 ("scsi: elx: efct: Driver initialization routines")
> Cc: [email protected]
> Suggested-by: Daniel Wagner <[email protected]>
> Signed-off-by: Fedor Pchelkin <[email protected]>
> ---
> v2: per Daniel Wagner's notice, handle the other possible memory
> allocation errors inside the function.

Looks good to me! Thanks!

Reviewed-by: Daniel Wagner <[email protected]>