Re: [PATCH 4.19 237/276] scsi: lpfc: avoid uninitialized variable warning

From: Nathan Chancellor
Date: Thu Jun 06 2019 - 12:04:56 EST


On Thu, Jun 06, 2019 at 02:53:23PM +0200, Pavel Machek wrote:
> Hi!
>
> > [ Upstream commit faf5a744f4f8d76e7c03912b5cd381ac8045f6ec ]
> >
> > clang -Wuninitialized incorrectly sees a variable being used without
> > initialization:
> >
> > drivers/scsi/lpfc/lpfc_nvme.c:2102:37: error: variable 'localport' is uninitialized when used here
> > [-Werror,-Wuninitialized]
> > lport = (struct lpfc_nvme_lport *)localport->private;
> > ^~~~~~~~~
> > drivers/scsi/lpfc/lpfc_nvme.c:2059:38: note: initialize the variable 'localport' to silence this warning
> > struct nvme_fc_local_port *localport;
> > ^
> > = NULL
> > 1 error generated.
> >
> > This is clearly in dead code, as the condition leading up to it is always
> > false when CONFIG_NVME_FC is disabled, and the variable is always
> > initialized when nvme_fc_register_localport() got called successfully.
> >
> > Change the preprocessor conditional to the equivalent C construct, which
> > makes the code more readable and gets rid of the warning.
>
> Unfortunately, this missed "else" branch where the code was freeing
> the memory with kfree(cstat)... so this introduces a memory leak.
>
> Best regards,
> Pavel

For the record, this is not a problem with the upstream commit (not
saying you thought that or not, I just want to be clear).

Looks like commit 4c47efc140fa ("scsi: lpfc: Move SCSI and NVME Stats to
hardware queue structures") "resolved" this by not making it an issue in
the first place. I think the simpler fix is this.

Thanks for pointing it out!

diff --git a/drivers/scsi/lpfc/lpfc_nvme.c b/drivers/scsi/lpfc/lpfc_nvme.c
index 099f70798fdd..645ffb5332b4 100644
--- a/drivers/scsi/lpfc/lpfc_nvme.c
+++ b/drivers/scsi/lpfc/lpfc_nvme.c
@@ -2477,14 +2477,14 @@ lpfc_nvme_create_localport(struct lpfc_vport *vport)
lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;

+ if (!IS_ENABLED(CONFIG_NVME_FC))
+ return ret;
+
cstat = kmalloc((sizeof(struct lpfc_nvme_ctrl_stat) *
phba->cfg_nvme_io_channel), GFP_KERNEL);
if (!cstat)
return -ENOMEM;

- if (!IS_ENABLED(CONFIG_NVME_FC))
- return ret;
-
/* localport is allocated from the stack, but the registration
* call allocates heap memory as well as the private area.
*/