linux-fbdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation
@ 2022-02-05  7:40 Christophe JAILLET
  2022-02-07  8:01 ` Uwe Kleine-König
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Christophe JAILLET @ 2022-02-05  7:40 UTC (permalink / raw)
  To: Thierry Reding, Uwe Kleine-König, Lee Jones, Daniel Thompson,
	Jingoo Han, Helge Deller
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linux-pwm,
	dri-devel, linux-fbdev

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 <christophe.jaillet@wanadoo.fr>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation
  2022-02-05  7:40 [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation Christophe JAILLET
@ 2022-02-07  8:01 ` Uwe Kleine-König
  2022-02-07  9:31   ` Lee Jones
  2022-02-07 16:21 ` Daniel Thompson
  2022-02-08  8:53 ` Lee Jones
  2 siblings, 1 reply; 6+ messages in thread
From: Uwe Kleine-König @ 2022-02-07  8:01 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Thierry Reding, Lee Jones, Daniel Thompson, Jingoo Han,
	Helge Deller, linux-kernel, kernel-janitors, linux-pwm, dri-devel,
	linux-fbdev

[-- Attachment #1: Type: text/plain, Size: 623 bytes --]

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 <christophe.jaillet@wanadoo.fr>

LGTM

Acked-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Thanks
Uwe

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation
  2022-02-07  8:01 ` Uwe Kleine-König
@ 2022-02-07  9:31   ` Lee Jones
  2022-02-08  8:43     ` Uwe Kleine-König
  0 siblings, 1 reply; 6+ messages in thread
From: Lee Jones @ 2022-02-07  9:31 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Christophe JAILLET, Thierry Reding, Daniel Thompson, Jingoo Han,
	Helge Deller, linux-kernel, kernel-janitors, linux-pwm, dri-devel,
	linux-fbdev

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 <christophe.jaillet@wanadoo.fr>
> 
> LGTM
> 
> Acked-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> 
> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation
  2022-02-05  7:40 [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation Christophe JAILLET
  2022-02-07  8:01 ` Uwe Kleine-König
@ 2022-02-07 16:21 ` Daniel Thompson
  2022-02-08  8:53 ` Lee Jones
  2 siblings, 0 replies; 6+ messages in thread
From: Daniel Thompson @ 2022-02-07 16:21 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Thierry Reding, Uwe Kleine-König, Lee Jones, Jingoo Han,
	Helge Deller, linux-kernel, kernel-janitors, linux-pwm, dri-devel,
	linux-fbdev

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 <christophe.jaillet@wanadoo.fr>

Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>


Daniel.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation
  2022-02-07  9:31   ` Lee Jones
@ 2022-02-08  8:43     ` Uwe Kleine-König
  0 siblings, 0 replies; 6+ messages in thread
From: Uwe Kleine-König @ 2022-02-08  8:43 UTC (permalink / raw)
  To: Lee Jones
  Cc: Christophe JAILLET, Thierry Reding, Daniel Thompson, Jingoo Han,
	Helge Deller, linux-kernel, kernel-janitors, linux-pwm, dri-devel,
	linux-fbdev

[-- Attachment #1: Type: text/plain, Size: 963 bytes --]

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 <christophe.jaillet@wanadoo.fr>
> > 
> > LGTM
> > 
> > Acked-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> > 
> > Thanks
> > Uwe
> 
> I am totally confused!

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

Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Sorry!

Uwe

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation
  2022-02-05  7:40 [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation Christophe JAILLET
  2022-02-07  8:01 ` Uwe Kleine-König
  2022-02-07 16:21 ` Daniel Thompson
@ 2022-02-08  8:53 ` Lee Jones
  2 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2022-02-08  8:53 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Thierry Reding, Uwe Kleine-König, Daniel Thompson,
	Jingoo Han, Helge Deller, linux-kernel, kernel-janitors,
	linux-pwm, dri-devel, linux-fbdev

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 <christophe.jaillet@wanadoo.fr>
> ---
>  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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-02-08  8:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-05  7:40 [PATCH] backlight: pwm_bl: Avoid open coded arithmetic in memory allocation Christophe JAILLET
2022-02-07  8:01 ` Uwe Kleine-König
2022-02-07  9:31   ` Lee Jones
2022-02-08  8:43     ` Uwe Kleine-König
2022-02-07 16:21 ` Daniel Thompson
2022-02-08  8:53 ` Lee Jones

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).