Skip to content

Commit 1eb6ea4

Browse files
GustavoARSilvaanholt
authored andcommitted
drm/vc4: Use struct_size() in kzalloc()
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) + count * sizeof(struct boo), 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> Signed-off-by: Eric Anholt <eric@anholt.net> Reviewed-by: Eric Anholt <eric@anholt.net> Link: https://patchwork.freedesktop.org/patch/msgid/20190131010015.GA32272@embeddedor
1 parent 8c77b22 commit 1eb6ea4

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

drivers/gpu/drm/vc4/vc4_perfmon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
117117
return -EINVAL;
118118
}
119119

120-
perfmon = kzalloc(sizeof(*perfmon) + (req->ncounters * sizeof(u64)),
120+
perfmon = kzalloc(struct_size(perfmon, counters, req->ncounters),
121121
GFP_KERNEL);
122122
if (!perfmon)
123123
return -ENOMEM;

0 commit comments

Comments
 (0)