mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns()
@ 2026-01-31  9:36 Zilin Guan
  2026-01-31 19:18 ` Markus Elfring
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Zilin Guan @ 2026-01-31  9:36 UTC (permalink / raw)
  To: don.brace
  Cc: James.Bottomley, martin.petersen, scott.teel, john.p.donnelly,
	scott.benesh, Mike.McGowen, storagedev, linux-scsi, linux-kernel,
	jianhao.xu, Zilin Guan

pqi_report_phys_luns() fails to release the rpl_list buffer when
encountering an unsupported data format or when the allocation for
rpl_16byte_wwid_list fails. These early returns bypass the cleanup
logic, leading to memory leaks.

Consolidate the error handling by adding an out_free_rpl_list label
and use goto statements to ensure rpl_list is consistently freed
on failure.

Compile tested only. Issue found using a prototype static analysis tool
and code review.

Fixes: 28ca6d876c5a ("scsi: smartpqi: Add extended report physical LUNs")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 drivers/scsi/smartpqi/smartpqi_init.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/smartpqi/smartpqi_init.c b/drivers/scsi/smartpqi/smartpqi_init.c
index fe549e2b7c94..c829d9590558 100644
--- a/drivers/scsi/smartpqi/smartpqi_init.c
+++ b/drivers/scsi/smartpqi/smartpqi_init.c
@@ -1241,7 +1241,8 @@ static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **b
 			dev_err(&ctrl_info->pci_dev->dev,
 				"RPL returned unsupported data format %u\n",
 				rpl_response_format);
-			return -EINVAL;
+			rc = -EINVAL;
+			goto out_free_rpl_list;
 		} else {
 			dev_warn(&ctrl_info->pci_dev->dev,
 				"RPL returned extended format 2 instead of 4\n");
@@ -1253,8 +1254,10 @@ static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **b
 
 	rpl_16byte_wwid_list = kmalloc(struct_size(rpl_16byte_wwid_list, lun_entries,
 						   num_physicals), GFP_KERNEL);
-	if (!rpl_16byte_wwid_list)
-		return -ENOMEM;
+	if (!rpl_16byte_wwid_list) {
+		rc = -ENOMEM;
+		goto out_free_rpl_list;
+	}
 
 	put_unaligned_be32(num_physicals * sizeof(struct report_phys_lun_16byte_wwid),
 		&rpl_16byte_wwid_list->header.list_length);
@@ -1275,6 +1278,10 @@ static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **b
 	*buffer = rpl_16byte_wwid_list;
 
 	return 0;
+
+out_free_rpl_list:
+	kfree(rpl_list);
+	return rc;
 }
 
 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns()
  2026-01-31  9:36 [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns() Zilin Guan
@ 2026-01-31 19:18 ` Markus Elfring
  2026-02-03 19:54 ` Don.Brace
  2026-02-04  3:24 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2026-01-31 19:18 UTC (permalink / raw)
  To: Zilin Guan, linux-scsi, Don Brace
  Cc: storagedev, LKML, James Bottomley, Jianhao Xu, John Donnelly,
	Mike McGowen, Martin K. Petersen, Scott Benesh, Scott Teel

…
> Fixes: 28ca6d876c5a ("scsi: smartpqi: Add extended report physical LUNs")
…

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.19-rc7#n34> +++ b/drivers/scsi/smartpqi/smartpqi_init.c
> @@ -1275,6 +1278,10 @@ static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **b
>  	*buffer = rpl_16byte_wwid_list;
>  
>  	return 0;
> +
> +out_free_rpl_list:
> +	kfree(rpl_list);
> +	return rc;
>  }
…

* How do you think about to refer to the local variable “rpl_8byte_wwid_list” instead?

* Would you like to refine the control flow a bit more for the end
  of this function implementation?
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc7#n526


Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns()
  2026-01-31  9:36 [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns() Zilin Guan
  2026-01-31 19:18 ` Markus Elfring
@ 2026-02-03 19:54 ` Don.Brace
  2026-02-04  3:24 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Don.Brace @ 2026-02-03 19:54 UTC (permalink / raw)
  To: zilin
  Cc: James.Bottomley, martin.petersen, Scott.Teel, john.p.donnelly,
	Scott.Benesh, Mike.McGowen, storagedev, linux-scsi, linux-kernel,
	jianhao.xu

________________________________________
From: Zilin Guan <zilin@seu.edu.cn>
Sent: Saturday, January 31, 2026 3:36 AM
To: Don Brace - C33706 <Don.Brace@microchip.com>
Cc: James.Bottomley@HansenPartnership.com <James.Bottomley@HansenPartnership.com>; martin.petersen@oracle.com <martin.petersen@oracle.com>; Scott Teel - C33730 <Scott.Teel@microchip.com>; john.p.donnelly@oracle.com <john.p.donnelly@oracle.com>; Scott Benesh - C33703 <Scott.Benesh@microchip.com>; Mike McGowen - C62625 <Mike.McGowen@microchip.com>; storagedev <storagedev@microchip.com>; linux-scsi@vger.kernel.org <linux-scsi@vger.kernel.org>; linux-kernel@vger.kernel.org <linux-kernel@vger.kernel.org>; jianhao.xu@seu.edu.cn <jianhao.xu@seu.edu.cn>; Zilin Guan <zilin@seu.edu.cn>
Subject: [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns()
 
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

pqi_report_phys_luns() fails to release the rpl_list buffer when
encountering an unsupported data format or when the allocation for
rpl_16byte_wwid_list fails. These early returns bypass the cleanup
logic, leading to memory leaks.

Consolidate the error handling by adding an out_free_rpl_list label
and use goto statements to ensure rpl_list is consistently freed
on failure.

Compile tested only. Issue found using a prototype static analysis tool
and code review.

Fixes: 28ca6d876c5a ("scsi: smartpqi: Add extended report physical LUNs")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>

Thanks for your patch. Like the minimal change.
Tested-by: Don Brace <don.brace@microchip.com>
Acked-by: Don Brace <don.brace@microchip.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns()
  2026-01-31  9:36 [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns() Zilin Guan
  2026-01-31 19:18 ` Markus Elfring
  2026-02-03 19:54 ` Don.Brace
@ 2026-02-04  3:24 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2026-02-04  3:24 UTC (permalink / raw)
  To: Zilin Guan
  Cc: don.brace, James.Bottomley, martin.petersen, scott.teel,
	john.p.donnelly, scott.benesh, Mike.McGowen, storagedev,
	linux-scsi, linux-kernel, jianhao.xu


Zilin,

> pqi_report_phys_luns() fails to release the rpl_list buffer when
> encountering an unsupported data format or when the allocation for
> rpl_16byte_wwid_list fails. These early returns bypass the cleanup
> logic, leading to memory leaks.

Applied to 6.20/scsi-staging, thanks!

-- 
Martin K. Petersen

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-02-04  3:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-31  9:36 [PATCH] scsi: smartpqi: fix memory leak in pqi_report_phys_luns() Zilin Guan
2026-01-31 19:18 ` Markus Elfring
2026-02-03 19:54 ` Don.Brace
2026-02-04  3:24 ` Martin K. Petersen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®