Skip to contentSkip to conversation

mtd: cfi: use struct_size() helper for cfiq allocation

PATCH Applied Gopi Krishna Menon 3 participants 4 messages Sep 18, 2025 – Sep 29, 2025 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: cfi: use struct_size() helper for cfiq allocation

Documentation/process/deprecated.rst recommends against performing dynamic size calculations in the arguments of memory allocator function due to the risk of overflow. Such calculations can wrap around and result in a smaller allocation than what the caller was expecting.

Replace the size calculation in cfiq allocation with struct_size() helper to make the code clearer and handle the overflows correctly.

---
 drivers/mtd/chips/cfi_probe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/chips/cfi_probe.c +1 -1
index a04b6174181c..e254f9cd2796 100644
@@ -208,7 +208,7 @@ static int __xipram cfi_chip_setup(struct map_info *map,
 	if (!num_erase_regions)
 		return 0;
 
-	cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
+	cfi->cfiq = kmalloc(struct_size(cfi->cfiq, EraseRegionInfo, num_erase_regions), GFP_KERNEL);
 	if (!cfi->cfiq)
 		return 0;
 
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Gopi Krishna Menon

Documentation/process/deprecated.rst recommends against performing dynamic size calculations in the arguments of memory allocator function due to the risk of overflow. Such calculations can wrap around and result in a smaller allocation than what the caller was expecting.

Replace the size calculation in cfiq allocation with struct_size() helper to make the code clearer and handle the overflows correctly.

---
 drivers/mtd/chips/cfi_probe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/chips/cfi_probe.c +0 -0
index a04b6174181c..e254f9cd2796 100644

drivers/mtd/chips/cfi_probe.c static int __xipram cfi_chip_setup(struct map_info *map, · line 214

Quoted 8 lines
drivers/mtd/chips/cfi_probe.c +1 -1
@@ -208,7 +208,7 @@ static int __xipram cfi_chip_setup(struct map_info *map,
 	if (!num_erase_regions)
 		return 0;
 
-	cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
+	cfi->cfiq = kmalloc(struct_size(cfi->cfiq, EraseRegionInfo, num_erase_regions), GFP_KERNEL);
 	if (!cfi->cfiq)
 		return 0;
 

There seems to be one more occurrence of this pattern in jedec_probe.c. Would appreciate if you could fix that too. Thanks!

Regards
Vignesh

maintainer Applied
#3 lore raw
In reply to Gopi Krishna Menon
Quoted 10 lines · Gopi Krishna Menon

Documentation/process/deprecated.rst recommends against performing dynamic size calculations in the arguments of memory allocator function due to the risk of overflow. Such calculations can wrap around and result in a smaller allocation than what the caller was expecting.

Replace the size calculation in cfiq allocation with struct_size() helper to make the code clearer and handle the overflows correctly.

[...]

Applied to mtd/next, thanks!

[1/1] mtd: cfi: use struct_size() helper for cfiq allocation

      commit: d496b6f42eb0455caf5d8cb30cf1f01b7fc2a747

Patche(s) should be available on mtd/linux.git and will be part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl