Skip to contentSkip to conversation

scsi: Prefer kmalloc_array over kmalloc involving dynamic size calculations

PATCH In review Bhanu Seshu Kumar Valluri 2 participants 3 messages Oct 7, 2025 – Nov 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 scsi: Prefer kmalloc_array over kmalloc involving dynamic size calculations

As a best practice use kmalloc_array to safely calculate dynamic object sizes without overflow.

---
Note: The patch is tested for compilation.
Change log:
v1->v2:
Updated commit message to refelect correct intention of the patch to address James Bottomley review in v1.
v1 Link : https://lore.kernel.org/all/[email protected]/

 drivers/scsi/smartpqi/smartpqi_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/smartpqi/smartpqi_init.c +1 -1
index 03c97e60d36f..19b0075eb256 100644
@@ -8936,7 +8936,7 @@ static int pqi_host_alloc_mem(struct pqi_ctrl_info *ctrl_info,
 	if (sg_count == 0 || sg_count > PQI_HOST_MAX_SG_DESCRIPTORS)
 		goto out;
 
-	host_memory_descriptor->host_chunk_virt_address = kmalloc(sg_count * sizeof(void *), GFP_KERNEL);
+	host_memory_descriptor->host_chunk_virt_address = kmalloc_array(sg_count, sizeof(void *), GFP_KERNEL);
 	if (!host_memory_descriptor->host_chunk_virt_address)
 		goto out;
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 20 lines · Bhanu Seshu Kumar Valluri

As a best practice use kmalloc_array to safely calculate dynamic object sizes without overflow.

---
Note: The patch is tested for compilation.
Change log:
v1->v2:
Updated commit message to refelect correct intention of the patch to address James Bottomley review in v1.
v1 Link : https://lore.kernel.org/all/[email protected]/

 drivers/scsi/smartpqi/smartpqi_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/smartpqi/smartpqi_init.c +0 -0
index 03c97e60d36f..19b0075eb256 100644

drivers/scsi/smartpqi/smartpqi_init.c static int pqi_host_alloc_mem(struct pqi_ctrl_info *ctrl_info, · line 8942

Quoted 8 lines
drivers/scsi/smartpqi/smartpqi_init.c +1 -1
@@ -8936,7 +8936,7 @@ static int pqi_host_alloc_mem(struct pqi_ctrl_info *ctrl_info,
 	if (sg_count == 0 || sg_count > PQI_HOST_MAX_SG_DESCRIPTORS)
 		goto out;
 
-	host_memory_descriptor->host_chunk_virt_address = kmalloc(sg_count * sizeof(void *), GFP_KERNEL);
+	host_memory_descriptor->host_chunk_virt_address = kmalloc_array(sg_count, sizeof(void *), GFP_KERNEL);
 	if (!host_memory_descriptor->host_chunk_virt_address)
 		goto out;
 

Hi,

I just wanted to check if you had a chance to review it or if any changes are needed from my side.

Regards,
Bhanu Seshu Kumar Valluri

#2 lore raw

Bhanu,

Quoted 2 lines

I just wanted to check if you had a chance to review it or if any changes are needed from my side.

This patch was merged last month.

Signature · 1 line
Martin K. Petersen