linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit
@ 2021-01-11 12:57 Geert Uytterhoeven
  2021-01-11 18:07 ` Fabio Estevam
  2021-01-12  4:37 ` Laurent Pinchart
  0 siblings, 2 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2021-01-11 12:57 UTC (permalink / raw)
  To: Andrzej Hajda, Neil Armstrong
  Cc: Laurent Pinchart, Jonas Karlman, Jernej Skrabec, David Airlie,
	Daniel Vetter, dri-devel, linux-kernel, Geert Uytterhoeven

As nwl_dsi.lanes is u32, and NSEC_PER_SEC is 1000000000L, the second
multiplication in

    dsi->lanes * 8 * NSEC_PER_SEC

will overflow on a 32-bit platform.  Fix this by making the constant
unsigned long long, forcing 64-bit arithmetic.

While iMX8 is arm64, this driver is currently used on 64-bit platforms
only, where long is 64-bit, so this cannot happen.  But the issue may
start to happen when the driver is reused for a 32-bit SoC, or when code
is copied for a new driver.

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
Compile-tested only.
---
 drivers/gpu/drm/bridge/nwl-dsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/bridge/nwl-dsi.c b/drivers/gpu/drm/bridge/nwl-dsi.c
index 66b67402f1acd57d..a8da3081efdcc84e 100644
--- a/drivers/gpu/drm/bridge/nwl-dsi.c
+++ b/drivers/gpu/drm/bridge/nwl-dsi.c
@@ -195,7 +195,7 @@ static u32 ps2bc(struct nwl_dsi *dsi, unsigned long long ps)
 	u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
 
 	return DIV64_U64_ROUND_UP(ps * dsi->mode.clock * bpp,
-				  dsi->lanes * 8 * NSEC_PER_SEC);
+				  dsi->lanes * 8ULL * NSEC_PER_SEC);
 }
 
 /*
-- 
2.25.1


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

* Re: [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit
  2021-01-11 12:57 [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit Geert Uytterhoeven
@ 2021-01-11 18:07 ` Fabio Estevam
  2021-01-12  4:37 ` Laurent Pinchart
  1 sibling, 0 replies; 4+ messages in thread
From: Fabio Estevam @ 2021-01-11 18:07 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Andrzej Hajda, Neil Armstrong, Jernej Skrabec, Jonas Karlman,
	David Airlie, linux-kernel, DRI mailing list, Laurent Pinchart

Hi Geert,

On Mon, Jan 11, 2021 at 10:02 AM Geert Uytterhoeven
<geert+renesas@glider.be> wrote:
>
> As nwl_dsi.lanes is u32, and NSEC_PER_SEC is 1000000000L, the second
> multiplication in
>
>     dsi->lanes * 8 * NSEC_PER_SEC
>
> will overflow on a 32-bit platform.  Fix this by making the constant
> unsigned long long, forcing 64-bit arithmetic.
>
> While iMX8 is arm64, this driver is currently used on 64-bit platforms
> only, where long is 64-bit, so this cannot happen.  But the issue may
> start to happen when the driver is reused for a 32-bit SoC, or when code
> is copied for a new driver.

This IP is also present on i.MX7ULP, which is 32-bit, but not supported yet.

Thanks for taking care of this.

Reviewed-by: Fabio Estevam <festevam@gmail.com>

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

* Re: [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit
  2021-01-11 12:57 [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit Geert Uytterhoeven
  2021-01-11 18:07 ` Fabio Estevam
@ 2021-01-12  4:37 ` Laurent Pinchart
  2021-01-12  8:46   ` Geert Uytterhoeven
  1 sibling, 1 reply; 4+ messages in thread
From: Laurent Pinchart @ 2021-01-12  4:37 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Andrzej Hajda, Neil Armstrong, Jonas Karlman, Jernej Skrabec,
	David Airlie, Daniel Vetter, dri-devel, linux-kernel

Hi Geert,

Thank you for the patch.

On Mon, Jan 11, 2021 at 01:57:02PM +0100, Geert Uytterhoeven wrote:
> As nwl_dsi.lanes is u32, and NSEC_PER_SEC is 1000000000L, the second
> multiplication in
> 
>     dsi->lanes * 8 * NSEC_PER_SEC
> 
> will overflow on a 32-bit platform.  Fix this by making the constant
> unsigned long long, forcing 64-bit arithmetic.
> 
> While iMX8 is arm64, this driver is currently used on 64-bit platforms
> only, where long is 64-bit, so this cannot happen.  But the issue may
> start to happen when the driver is reused for a 32-bit SoC, or when code
> is copied for a new driver.
> 
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> Compile-tested only.
> ---
>  drivers/gpu/drm/bridge/nwl-dsi.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/bridge/nwl-dsi.c b/drivers/gpu/drm/bridge/nwl-dsi.c
> index 66b67402f1acd57d..a8da3081efdcc84e 100644
> --- a/drivers/gpu/drm/bridge/nwl-dsi.c
> +++ b/drivers/gpu/drm/bridge/nwl-dsi.c
> @@ -195,7 +195,7 @@ static u32 ps2bc(struct nwl_dsi *dsi, unsigned long long ps)
>  	u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
>  
>  	return DIV64_U64_ROUND_UP(ps * dsi->mode.clock * bpp,
> -				  dsi->lanes * 8 * NSEC_PER_SEC);
> +				  dsi->lanes * 8ULL * NSEC_PER_SEC);

I wonder if we could get rid of a whole class of bugs by turning
NSEC_PER_SEC into a ULL, but I suppose there are valid cases where a
32-bit integer is enough.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

How did you come across this by the way ?

>  }
>  
>  /*

-- 
Regards,

Laurent Pinchart

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

* Re: [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit
  2021-01-12  4:37 ` Laurent Pinchart
@ 2021-01-12  8:46   ` Geert Uytterhoeven
  0 siblings, 0 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2021-01-12  8:46 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: Andrzej Hajda, Neil Armstrong, Jonas Karlman, Jernej Skrabec,
	David Airlie, Daniel Vetter, DRI Development,
	Linux Kernel Mailing List

Hi Laurent,

On Tue, Jan 12, 2021 at 5:38 AM Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
> On Mon, Jan 11, 2021 at 01:57:02PM +0100, Geert Uytterhoeven wrote:
> > As nwl_dsi.lanes is u32, and NSEC_PER_SEC is 1000000000L, the second
> > multiplication in
> >
> >     dsi->lanes * 8 * NSEC_PER_SEC
> >
> > will overflow on a 32-bit platform.  Fix this by making the constant
> > unsigned long long, forcing 64-bit arithmetic.
> >
> > While iMX8 is arm64, this driver is currently used on 64-bit platforms
> > only, where long is 64-bit, so this cannot happen.  But the issue may
> > start to happen when the driver is reused for a 32-bit SoC, or when code
> > is copied for a new driver.
> >
> > Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>

> > --- a/drivers/gpu/drm/bridge/nwl-dsi.c
> > +++ b/drivers/gpu/drm/bridge/nwl-dsi.c
> > @@ -195,7 +195,7 @@ static u32 ps2bc(struct nwl_dsi *dsi, unsigned long long ps)
> >       u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
> >
> >       return DIV64_U64_ROUND_UP(ps * dsi->mode.clock * bpp,
> > -                               dsi->lanes * 8 * NSEC_PER_SEC);
> > +                               dsi->lanes * 8ULL * NSEC_PER_SEC);
>
> I wonder if we could get rid of a whole class of bugs by turning
> NSEC_PER_SEC into a ULL, but I suppose there are valid cases where a
> 32-bit integer is enough.

Indeed, and 64-bit arithmetic is more expensive on 32-bit platforms.
I considered that change, but doing so would require updates all over
the place (e.g. printing a value derived from NSEC_PER_SEC, divisions
 that need to be changed to do_div or div_u64(), ...)

Note that the selftests already use such a definition.

> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Thanks!

> How did you come across this by the way ?

https://lore.kernel.org/linux-renesas-soc/CAMuHMdXQvPY_mYicjPKjDSCwdO_rP-9PJOvqD0J6=S3Opr1ycg@mail.gmail.com/
and of course I grepped for similar use patterns...

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

end of thread, other threads:[~2021-01-12  8:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-11 12:57 [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit Geert Uytterhoeven
2021-01-11 18:07 ` Fabio Estevam
2021-01-12  4:37 ` Laurent Pinchart
2021-01-12  8:46   ` Geert Uytterhoeven

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