* [PATCH] drm/vc4: Use struct_size() in kzalloc()
@ 2019-01-31 1:00 Gustavo A. R. Silva
2019-02-07 22:59 ` Eric Anholt
0 siblings, 1 reply; 3+ messages in thread
From: Gustavo A. R. Silva @ 2019-01-31 1:00 UTC (permalink / raw)
To: Eric Anholt, David Airlie, Daniel Vetter
Cc: dri-devel, 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) + 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>
---
drivers/gpu/drm/vc4/vc4_perfmon.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vc4/vc4_perfmon.c b/drivers/gpu/drm/vc4/vc4_perfmon.c
index 437e7a27f21d..495150415020 100644
--- a/drivers/gpu/drm/vc4/vc4_perfmon.c
+++ b/drivers/gpu/drm/vc4/vc4_perfmon.c
@@ -117,7 +117,7 @@ int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
return -EINVAL;
}
- perfmon = kzalloc(sizeof(*perfmon) + (req->ncounters * sizeof(u64)),
+ perfmon = kzalloc(struct_size(perfmon, counters, req->ncounters),
GFP_KERNEL);
if (!perfmon)
return -ENOMEM;
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] drm/vc4: Use struct_size() in kzalloc()
2019-01-31 1:00 [PATCH] drm/vc4: Use struct_size() in kzalloc() Gustavo A. R. Silva
@ 2019-02-07 22:59 ` Eric Anholt
2019-02-07 23:02 ` Gustavo A. R. Silva
0 siblings, 1 reply; 3+ messages in thread
From: Eric Anholt @ 2019-02-07 22:59 UTC (permalink / raw)
To: David Airlie, Daniel Vetter; +Cc: dri-devel, linux-kernel, Gustavo A. R. Silva
[-- Attachment #1: Type: text/plain, Size: 774 bytes --]
"Gustavo A. R. Silva" <gustavo@embeddedor.com> writes:
> 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>
Reviewed and pushed to drm-misc-next. Thanks!
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] drm/vc4: Use struct_size() in kzalloc()
2019-02-07 22:59 ` Eric Anholt
@ 2019-02-07 23:02 ` Gustavo A. R. Silva
0 siblings, 0 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2019-02-07 23:02 UTC (permalink / raw)
To: Eric Anholt, David Airlie, Daniel Vetter; +Cc: dri-devel, linux-kernel
On 2/7/19 4:59 PM, Eric Anholt wrote:
> "Gustavo A. R. Silva" <gustavo@embeddedor.com> writes:
>
>> 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>
>
> Reviewed and pushed to drm-misc-next. Thanks!
>
Thank you, Eric.
--
Gustavo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-02-07 23:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-31 1:00 [PATCH] drm/vc4: Use struct_size() in kzalloc() Gustavo A. R. Silva
2019-02-07 22:59 ` Eric Anholt
2019-02-07 23:02 ` Gustavo A. R. Silva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).