Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 24/06/2020 20.12, Gustavo A. R. Silva wrote:
> Hi Vinod,
> 
> On 6/24/20 00:55, Vinod Koul wrote:
>> On 19-06-20, 17:43, Gustavo A. R. Silva wrote:
>>> Make use of the struct_size() helper instead of an open-coded version
>>> in order to avoid any potential type mistakes.
>>>
>>> This code was detected with the help of Coccinelle and, audited and
>>> fixed manually.
>>>
>>> Addresses-KSPP-ID: https://github.com/KSPP/linux/issues/83
>>> Signed-off-by: Gustavo A. R. Silva <gustavoars@xxxxxxxxxx>
>>> ---
>>>  drivers/dma/ti/k3-udma.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/dma/ti/k3-udma.c b/drivers/dma/ti/k3-udma.c
>>> index 0d5fb154b8e2..411c54b86ba8 100644
>>> --- a/drivers/dma/ti/k3-udma.c
>>> +++ b/drivers/dma/ti/k3-udma.c
>>> @@ -2209,7 +2209,7 @@ udma_prep_slave_sg_pkt(struct udma_chan *uc, struct scatterlist *sgl,
>>>  	u32 ring_id;
>>>  	unsigned int i;
>>>  
>>> -	d = kzalloc(sizeof(*d) + sglen * sizeof(d->hwdesc[0]), GFP_NOWAIT);
>>> +	d = kzalloc(struct_size(d, hwdesc, sglen), GFP_NOWAIT);
>>
>> struct_size() is a * b + c but here we need, a + b * c.. the trailing
>> struct is N times here..
>>
> 
> struct_size() works exactly as expected in this case. :)
> Please, see:
> 
> include/linux/overflow.h:314:
> 314 #define struct_size(p, member, count)                                   \
> 315         __ab_c_size(count,                                              \
> 316                     sizeof(*(p)->member) + __must_be_array((p)->member),\
> 317                     sizeof(*(p)))

True, struct_size is for this sort of things.

Acked-by: Peter Ujfalusi <peter.ujfalusi@xxxxxx>

While looking it up in include/linux/overflow.h I have noticed your
commit in linux-next, which adds flex_array_size()

The example in the commit message contradicts with what the helper
does imho. To be correct it should have been:

struct something {
	size_t count;
	struct foo items[];
};

- struct something *instance;
+ struct something instance;

- instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
+ instance.items = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
instance->count = count;
memcpy(instance->items, src, flex_array_size(instance, items, instance->count));

If I'm not mistaken..


> 
> Thanks
> --
> Gustavo
> 
>>>  	if (!d)
>>>  		return NULL;
>>>  
>>> @@ -2525,7 +2525,7 @@ udma_prep_dma_cyclic_pkt(struct udma_chan *uc, dma_addr_t buf_addr,
>>>  	if (period_len >= SZ_4M)
>>>  		return NULL;
>>>  
>>> -	d = kzalloc(sizeof(*d) + periods * sizeof(d->hwdesc[0]), GFP_NOWAIT);
>>> +	d = kzalloc(struct_size(d, hwdesc, periods), GFP_NOWAIT);
>>>  	if (!d)
>>>  		return NULL;
>>>  
>>> -- 
>>> 2.27.0
>>

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki





[Index of Archives]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux PCI]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux