* [PATCH] scsi: mvsas: use struct_size() in kzalloc()
@ 2019-01-04 21:47 Gustavo A. R. Silva
0 siblings, 0 replies; 2+ messages in thread
From: Gustavo A. R. Silva @ 2019-01-04 21:47 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen
Cc: linux-scsi, linux-kernel, Gustavo A. R. Silva
One of the more common cases of allocation size calculations is finding the
size of a structure that has a zero-sized array at the end, along with memory
for some number of elements for that array. For example:
struct foo {
int stuff;
void *entry[];
};
instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);
Instead of leaving these open-coded and prone to type mistakes, we can now
use the new struct_size() helper:
instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);
This code was detected with the help of Coccinelle.
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
drivers/scsi/mvsas/mv_init.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/scsi/mvsas/mv_init.c b/drivers/scsi/mvsas/mv_init.c
index 030d911ee374..cd6ba7ed0ab4 100644
--- a/drivers/scsi/mvsas/mv_init.c
+++ b/drivers/scsi/mvsas/mv_init.c
@@ -366,9 +366,9 @@ static struct mvs_info *mvs_pci_alloc(struct pci_dev *pdev,
struct mvs_info *mvi = NULL;
struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
- mvi = kzalloc(sizeof(*mvi) +
- (1L << mvs_chips[ent->driver_data].slot_width) *
- sizeof(struct mvs_slot_info), GFP_KERNEL);
+ mvi = kzalloc(struct_size(mvi, slot_info,
+ (1L << mvs_chips[ent->driver_data].slot_width)),
+ GFP_KERNEL);
if (!mvi)
return NULL;
--
2.20.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH] scsi: mvsas: Use struct_size() in kzalloc()
@ 2019-06-19 14:02 Gustavo A. R. Silva
0 siblings, 0 replies; 2+ messages in thread
From: Gustavo A. R. Silva @ 2019-06-19 14:02 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen
Cc: linux-scsi, linux-kernel, Gustavo A. R. Silva
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:
struct mvs_info {
...
struct mvs_slot_info slot_info[0];
};
instance = kzalloc(sizeof(sizeof(*mvi) + count * sizeof(struct mvs_slot_info),
GFP_KERNEL);
Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:
instance = kzalloc(struct_size(mvi, slot_info, count), GFP_KERNEL);
This code was detected with the help of Coccinelle.
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
drivers/scsi/mvsas/mv_init.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/scsi/mvsas/mv_init.c b/drivers/scsi/mvsas/mv_init.c
index da719b0694dc..3f2022fd2335 100644
--- a/drivers/scsi/mvsas/mv_init.c
+++ b/drivers/scsi/mvsas/mv_init.c
@@ -350,9 +350,9 @@ static struct mvs_info *mvs_pci_alloc(struct pci_dev *pdev,
struct mvs_info *mvi = NULL;
struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
- mvi = kzalloc(sizeof(*mvi) +
- (1L << mvs_chips[ent->driver_data].slot_width) *
- sizeof(struct mvs_slot_info), GFP_KERNEL);
+ mvi = kzalloc(struct_size(mvi, slot_info,
+ 1L << mvs_chips[ent->driver_data].slot_width),
+ GFP_KERNEL);
if (!mvi)
return NULL;
--
2.21.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-06-19 14:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-04 21:47 [PATCH] scsi: mvsas: use struct_size() in kzalloc() Gustavo A. R. Silva
-- strict thread matches above, loose matches on Subject: below --
2019-06-19 14:02 [PATCH] scsi: mvsas: Use " Gustavo A. R. Silva
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.