Re: [PATCH] drivers: qcom: rpmh: use struct_size() in kzalloc()

From: Gustavo A. R. Silva
Date: Tue Jan 29 2019 - 00:13:49 EST


Hi all,

Friendly ping:

Who can take this?

Thanks
--
Gustavo

On 1/9/19 11:38 AM, Gustavo A. R. Silva wrote:
> Hi,
>
> Friendly ping:
>
> Who can ack or review this patch, please?
>
> Thanks
> --
> Gustavo
>
>
> On 12/24/18 12:22 AM, Gustavo A. R. Silva wrote:
>> 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 issue was detected with the help of Coccinelle.
>>
>> Signed-off-by: Gustavo A. R. Silva <gustavo@xxxxxxxxxxxxxx>
>> ---
>> Â drivers/soc/qcom/rpmh.c | 3 +--
>> Â 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/drivers/soc/qcom/rpmh.c b/drivers/soc/qcom/rpmh.c
>> index c7beb6841289..12c057a0b325 100644
>> --- a/drivers/soc/qcom/rpmh.c
>> +++ b/drivers/soc/qcom/rpmh.c
>> @@ -362,8 +362,7 @@ int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
>> ÂÂÂÂÂ if (!count)
>> ÂÂÂÂÂÂÂÂÂ return -EINVAL;
>> Â -ÂÂÂ req = kzalloc(sizeof(*req) + count * sizeof(req->rpm_msgs[0]),
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂ GFP_ATOMIC);
>> +ÂÂÂ req = kzalloc(struct_size(req, rpm_msgs, count), GFP_ATOMIC);
>> ÂÂÂÂÂ if (!req)
>> ÂÂÂÂÂÂÂÂÂ return -ENOMEM;
>> ÂÂÂÂÂ req->count = count;
>>