Skip to contentSkip to conversation

scsi: smartpqi: fix memory leak in pqi_report_phys_luns()

PATCH Applied Zilin Guan 4 participants 4 messages Jan 31 – Feb 4

Subsystems (2)
lore
Share & tools
lore

Zilin Guan

author
#0 lore raw

PATCH 1/1 scsi: smartpqi: fix memory leak in pqi_report_phys_luns()

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")
---
 drivers/scsi/smartpqi/smartpqi_init.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/scsi/smartpqi/smartpqi_init.c +10 -3
index fe549e2b7c94..c829d9590558 100644
@@ -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)
Signature · 1 line
2.34.1
#1 lore raw

…

Quoted 1 line
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

…

Quoted 1 line

+++ b/drivers/scsi/smartpqi/smartpqi_init.c

…

drivers/scsi/smartpqi/smartpqi_init.c static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **b · line 1285

Quoted 9 lines
drivers/scsi/smartpqi/smartpqi_init.c +4 -0
@@ -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

maintainer Tested-by Acked-by on v1 1/1
#2 lore raw
In reply to Zilin Guan

________________________________________
From: Zilin Guan <[email protected]>
Sent: Saturday, January 31, 2026 3:36 AM
To: Don Brace - C33706 <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; Scott Teel - C33730 <[email protected]>; [email protected] <[email protected]>; Scott Benesh - C33703 <[email protected]>; Mike McGowen - C62625 <[email protected]>; storagedev <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Zilin Guan <[email protected]> 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")

Thanks for your patch. Like the minimal change.

#3 lore raw
In reply to Zilin Guan

Zilin,

Quoted 4 lines

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!

Signature · 1 line
Martin K. Petersen