2022-02-05 14:52:41

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation

kmalloc_array()/kcalloc() should be used to avoid potential overflow when
a multiplication is needed to compute the size of the requested memory.

So turn a kzalloc()+explicit size computation into an equivalent kcalloc().

Signed-off-by: Christophe JAILLET <[email protected]>
---
drivers/video/backlight/pwm_bl.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
index 8d8959a70e44..c0523a0269ee 100644
--- a/drivers/video/backlight/pwm_bl.c
+++ b/drivers/video/backlight/pwm_bl.c
@@ -263,9 +263,8 @@ static int pwm_backlight_parse_dt(struct device *dev,

/* read brightness levels from DT property */
if (num_levels > 0) {
- size_t size = sizeof(*data->levels) * num_levels;
-
- data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
+ data->levels = devm_kcalloc(dev, num_levels,
+ sizeof(*data->levels), GFP_KERNEL);
if (!data->levels)
return -ENOMEM;

@@ -320,8 +319,8 @@ static int pwm_backlight_parse_dt(struct device *dev,
* Create a new table of brightness levels with all the
* interpolated steps.
*/
- size = sizeof(*table) * num_levels;
- table = devm_kzalloc(dev, size, GFP_KERNEL);
+ table = devm_kcalloc(dev, num_levels, sizeof(*table),
+ GFP_KERNEL);
if (!table)
return -ENOMEM;
/*
--
2.32.0



2022-02-07 22:10:46

by Lee Jones

[permalink] [raw]
Subject: Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation

On Mon, 07 Feb 2022, Uwe Kleine-König wrote:

> On Sat, Feb 05, 2022 at 08:40:48AM +0100, Christophe JAILLET wrote:
> > kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> > a multiplication is needed to compute the size of the requested memory.
> >
> > So turn a kzalloc()+explicit size computation into an equivalent kcalloc().
> >
> > Signed-off-by: Christophe JAILLET <[email protected]>
>
> LGTM
>
> Acked-by: Christophe JAILLET <[email protected]>
>
> Thanks
> Uwe

I am totally confused!

--
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

2022-02-08 01:49:36

by Daniel Thompson

[permalink] [raw]
Subject: Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation

On Sat, Feb 05, 2022 at 08:40:48AM +0100, Christophe JAILLET wrote:
> kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> a multiplication is needed to compute the size of the requested memory.
>
> So turn a kzalloc()+explicit size computation into an equivalent kcalloc().
>
> Signed-off-by: Christophe JAILLET <[email protected]>

Reviewed-by: Daniel Thompson <[email protected]>


Daniel.

2022-02-08 18:28:22

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation

On Mon, Feb 07, 2022 at 09:31:00AM +0000, Lee Jones wrote:
> On Mon, 07 Feb 2022, Uwe Kleine-K?nig wrote:
>
> > On Sat, Feb 05, 2022 at 08:40:48AM +0100, Christophe JAILLET wrote:
> > > kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> > > a multiplication is needed to compute the size of the requested memory.
> > >
> > > So turn a kzalloc()+explicit size computation into an equivalent kcalloc().
> > >
> > > Signed-off-by: Christophe JAILLET <[email protected]>
> >
> > LGTM
> >
> > Acked-by: Christophe JAILLET <[email protected]>
> >
> > Thanks
> > Uwe
>
> I am totally confused!

An rightfully so. Copy-paste-fail, this was supposed to be

Acked-by: Uwe Kleine-K?nig <[email protected]>

Sorry!

Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (963.00 B)
signature.asc (499.00 B)
Download all attachments

2022-02-09 08:45:34

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation

On Sat, Feb 05, 2022 at 08:40:48AM +0100, Christophe JAILLET wrote:
> kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> a multiplication is needed to compute the size of the requested memory.
>
> So turn a kzalloc()+explicit size computation into an equivalent kcalloc().
>
> Signed-off-by: Christophe JAILLET <[email protected]>

LGTM

Acked-by: Christophe JAILLET <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (623.00 B)
signature.asc (499.00 B)
Download all attachments

2022-02-09 09:56:24

by Lee Jones

[permalink] [raw]
Subject: Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation

On Sat, 05 Feb 2022, Christophe JAILLET wrote:

> kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> a multiplication is needed to compute the size of the requested memory.
>
> So turn a kzalloc()+explicit size computation into an equivalent kcalloc().
>
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> drivers/video/backlight/pwm_bl.c | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)

Applied, thanks.

--
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog