* [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
@ 2020-06-19 22:43 Gustavo A. R. Silva
2020-06-24 5:55 ` Vinod Koul
0 siblings, 1 reply; 12+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-19 22:43 UTC (permalink / raw)
To: Dan Williams, Vinod Koul; +Cc: dmaengine, linux-kernel, Gustavo A. R. Silva
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@kernel.org>
---
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);
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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-19 22:43 [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc() Gustavo A. R. Silva
@ 2020-06-24 5:55 ` Vinod Koul
2020-06-24 5:56 ` Joe Perches
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Vinod Koul @ 2020-06-24 5:55 UTC (permalink / raw)
To: Gustavo A. R. Silva, Peter Ujfalusi
Cc: Dan Williams, dmaengine, linux-kernel, Gustavo A. R. Silva
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@kernel.org>
> ---
> 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..
> 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
--
~Vinod
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-24 5:55 ` Vinod Koul
@ 2020-06-24 5:56 ` Joe Perches
2020-06-24 7:27 ` Vinod Koul
2020-06-24 9:14 ` Peter Ujfalusi
2020-06-24 17:12 ` Gustavo A. R. Silva
2 siblings, 1 reply; 12+ messages in thread
From: Joe Perches @ 2020-06-24 5:56 UTC (permalink / raw)
To: Vinod Koul, Gustavo A. R. Silva, Peter Ujfalusi
Cc: Dan Williams, dmaengine, linux-kernel, Gustavo A. R. Silva
On Wed, 2020-06-24 at 11:25 +0530, 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
Is this odd tag really useful?
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> > 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..
>
>
> > 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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-24 5:56 ` Joe Perches
@ 2020-06-24 7:27 ` Vinod Koul
0 siblings, 0 replies; 12+ messages in thread
From: Vinod Koul @ 2020-06-24 7:27 UTC (permalink / raw)
To: Joe Perches
Cc: Gustavo A. R. Silva, Peter Ujfalusi, Dan Williams, dmaengine,
linux-kernel, Gustavo A. R. Silva
On 23-06-20, 22:56, Joe Perches wrote:
> On Wed, 2020-06-24 at 11:25 +0530, 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
>
> Is this odd tag really useful?
Not at all :)
--
~Vinod
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-24 5:55 ` Vinod Koul
2020-06-24 5:56 ` Joe Perches
@ 2020-06-24 9:14 ` Peter Ujfalusi
2020-06-24 17:12 ` Gustavo A. R. Silva
2 siblings, 0 replies; 12+ messages in thread
From: Peter Ujfalusi @ 2020-06-24 9:14 UTC (permalink / raw)
To: Vinod Koul, Gustavo A. R. Silva
Cc: Dan Williams, dmaengine, linux-kernel, Gustavo A. R. Silva
On 24/06/2020 8.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@kernel.org>
>> ---
>> 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..
Yes, that's correct.
>
>
>> 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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-24 5:55 ` Vinod Koul
2020-06-24 5:56 ` Joe Perches
2020-06-24 9:14 ` Peter Ujfalusi
@ 2020-06-24 17:12 ` Gustavo A. R. Silva
2020-06-26 7:30 ` Peter Ujfalusi
2 siblings, 1 reply; 12+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-24 17:12 UTC (permalink / raw)
To: Vinod Koul, Gustavo A. R. Silva, Peter Ujfalusi
Cc: Dan Williams, dmaengine, linux-kernel
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@kernel.org>
>> ---
>> 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)))
318
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
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-24 17:12 ` Gustavo A. R. Silva
@ 2020-06-26 7:30 ` Peter Ujfalusi
2020-06-26 13:29 ` Gustavo A. R. Silva
0 siblings, 1 reply; 12+ messages in thread
From: Peter Ujfalusi @ 2020-06-26 7:30 UTC (permalink / raw)
To: Gustavo A. R. Silva, Vinod Koul, Gustavo A. R. Silva
Cc: Dan Williams, dmaengine, linux-kernel
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@kernel.org>
>>> ---
>>> 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@ti.com>
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-26 7:30 ` Peter Ujfalusi
@ 2020-06-26 13:29 ` Gustavo A. R. Silva
2020-06-29 6:43 ` Peter Ujfalusi
0 siblings, 1 reply; 12+ messages in thread
From: Gustavo A. R. Silva @ 2020-06-26 13:29 UTC (permalink / raw)
To: Peter Ujfalusi
Cc: Gustavo A. R. Silva, Vinod Koul, Dan Williams, dmaengine, linux-kernel
Hi Peter,
Please, see my comments below...
On Fri, Jun 26, 2020 at 10:30:37AM +0300, Peter Ujfalusi wrote:
>
>
> 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@kernel.org>
> >>> ---
> >>> 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@ti.com>
>
> 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
There is no contradiction here.
> 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));
>
This is all wrong. Please, double check how struct_size() works.
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
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-26 13:29 ` Gustavo A. R. Silva
@ 2020-06-29 6:43 ` Peter Ujfalusi
2020-07-10 18:25 ` Gustavo A. R. Silva
0 siblings, 1 reply; 12+ messages in thread
From: Peter Ujfalusi @ 2020-06-29 6:43 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: Gustavo A. R. Silva, Vinod Koul, Dan Williams, dmaengine, linux-kernel
On 26/06/2020 16.29, Gustavo A. R. Silva wrote:
> Hi Peter,
>
> Please, see my comments below...
>
> On Fri, Jun 26, 2020 at 10:30:37AM +0300, Peter Ujfalusi wrote:
>>
>>
>> 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@kernel.org>
>>>>> ---
>>>>> 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@ti.com>
>>
>> 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
>
> There is no contradiction here.
>
>> 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));
>>
>
> This is all wrong. Please, double check how struct_size() works.
Yes, I got it mixed up, I know how struct_size works... For some reason
I just overlooked it's use in the kmalloc().
>
> 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
>>
- Péter
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-06-29 6:43 ` Peter Ujfalusi
@ 2020-07-10 18:25 ` Gustavo A. R. Silva
2020-07-15 6:21 ` Vinod Koul
0 siblings, 1 reply; 12+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-10 18:25 UTC (permalink / raw)
To: Peter Ujfalusi
Cc: Gustavo A. R. Silva, Vinod Koul, Dan Williams, dmaengine, linux-kernel
Hi all,
Friendly ping: who can take this, please?
Thanks
--
Gustavo
On Mon, Jun 29, 2020 at 09:43:26AM +0300, Peter Ujfalusi wrote:
>
>
> On 26/06/2020 16.29, Gustavo A. R. Silva wrote:
> > Hi Peter,
> >
> > Please, see my comments below...
> >
> > On Fri, Jun 26, 2020 at 10:30:37AM +0300, Peter Ujfalusi wrote:
> >>
> >>
> >> 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@kernel.org>
> >>>>> ---
> >>>>> 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@ti.com>
> >>
> >> 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
> >
> > There is no contradiction here.
> >
> >> 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));
> >>
> >
> > This is all wrong. Please, double check how struct_size() works.
>
> Yes, I got it mixed up, I know how struct_size works... For some reason
> I just overlooked it's use in the kmalloc().
>
> >
> > 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
> >>
>
> - Péter
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
> Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-07-10 18:25 ` Gustavo A. R. Silva
@ 2020-07-15 6:21 ` Vinod Koul
2020-07-15 14:06 ` Gustavo A. R. Silva
0 siblings, 1 reply; 12+ messages in thread
From: Vinod Koul @ 2020-07-15 6:21 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: Peter Ujfalusi, Gustavo A. R. Silva, Dan Williams, dmaengine,
linux-kernel
On 10-07-20, 13:25, Gustavo A. R. Silva wrote:
> Hi all,
>
> Friendly ping: who can take this, please?
Applied now
--
~Vinod
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc()
2020-07-15 6:21 ` Vinod Koul
@ 2020-07-15 14:06 ` Gustavo A. R. Silva
0 siblings, 0 replies; 12+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-15 14:06 UTC (permalink / raw)
To: Vinod Koul, Gustavo A. R. Silva
Cc: Peter Ujfalusi, Dan Williams, dmaengine, linux-kernel
On 7/15/20 01:21, Vinod Koul wrote:
> On 10-07-20, 13:25, Gustavo A. R. Silva wrote:
>> Hi all,
>>
>> Friendly ping: who can take this, please?
>
> Applied now
>
Thanks, Vinod.
--
Gustavo
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2020-07-15 14:24 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-19 22:43 [PATCH][next] dmaengine: ti: k3-udma: Use struct_size() in kzalloc() Gustavo A. R. Silva
2020-06-24 5:55 ` Vinod Koul
2020-06-24 5:56 ` Joe Perches
2020-06-24 7:27 ` Vinod Koul
2020-06-24 9:14 ` Peter Ujfalusi
2020-06-24 17:12 ` Gustavo A. R. Silva
2020-06-26 7:30 ` Peter Ujfalusi
2020-06-26 13:29 ` Gustavo A. R. Silva
2020-06-29 6:43 ` Peter Ujfalusi
2020-07-10 18:25 ` Gustavo A. R. Silva
2020-07-15 6:21 ` Vinod Koul
2020-07-15 14:06 ` 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).