diff mbox series

i2c: imx-lpi2c: fix type char overflow issue when calculating the clock cycle

Message ID 20230529080251.3614380-1-carlos.song@nxp.com
State Accepted
Delegated to: Andi Shyti
Headers show
Series i2c: imx-lpi2c: fix type char overflow issue when calculating the clock cycle | expand

Commit Message

Carlos Song May 29, 2023, 8:02 a.m. UTC
From: Clark Wang <xiaoning.wang@nxp.com>

Claim clkhi and clklo as integer type to avoid possible calculation
errors caused by data overflow.

Fixes: a55fa9d0e42e ("i2c: imx-lpi2c: add low power i2c bus driver")
Signed-off-by: Clark Wang <xiaoning.wang@nxp.com>
Signed-off-by: Carlos Song <carlos.song@nxp.com>
---
 drivers/i2c/busses/i2c-imx-lpi2c.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Andi Shyti May 30, 2023, 6:50 p.m. UTC | #1
On Mon, May 29, 2023 at 04:02:51PM +0800, carlos.song@nxp.com wrote:
> From: Clark Wang <xiaoning.wang@nxp.com>
> 
> Claim clkhi and clklo as integer type to avoid possible calculation
> errors caused by data overflow.
> 
> Fixes: a55fa9d0e42e ("i2c: imx-lpi2c: add low power i2c bus driver")
> Signed-off-by: Clark Wang <xiaoning.wang@nxp.com>
> Signed-off-by: Carlos Song <carlos.song@nxp.com>
> ---
>  drivers/i2c/busses/i2c-imx-lpi2c.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
> index 40a4420d4c12..7dd33564cbe1 100644
> --- a/drivers/i2c/busses/i2c-imx-lpi2c.c
> +++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
> @@ -211,8 +211,8 @@ static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx)
>  /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
>  static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
>  {
> -	u8 prescale, filt, sethold, clkhi, clklo, datavd;
> -	unsigned int clk_rate, clk_cycle;
> +	u8 prescale, filt, sethold, datavd;
> +	unsigned int clk_rate, clk_cycle, clkhi, clklo;

looks OK.

Reviewed-by: Andi Shyti <andi.shyti@kernel.org> 

there is, though, another part I would take care of, this bit:

	clklo = clk_cycle - clkhi;
	if (clklo < 64)
		break;

It might be unlikely, but if "clk_cycle > clkhi" then all the
calculation. Do you mind adding a check:

	if (unlikely(clk_cycle > clkhi))
		break;

Andi

>  	enum lpi2c_imx_pincfg pincfg;
>  	unsigned int temp;
>  
> -- 
> 2.34.1
>
Uwe Kleine-König June 1, 2023, 1:32 p.m. UTC | #2
On Tue, May 30, 2023 at 08:50:36PM +0200, Andi Shyti wrote:
> On Mon, May 29, 2023 at 04:02:51PM +0800, carlos.song@nxp.com wrote:
> > From: Clark Wang <xiaoning.wang@nxp.com>
> > 
> > Claim clkhi and clklo as integer type to avoid possible calculation
> > errors caused by data overflow.
> > 
> > Fixes: a55fa9d0e42e ("i2c: imx-lpi2c: add low power i2c bus driver")
> > Signed-off-by: Clark Wang <xiaoning.wang@nxp.com>
> > Signed-off-by: Carlos Song <carlos.song@nxp.com>
> > ---
> >  drivers/i2c/busses/i2c-imx-lpi2c.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
> > index 40a4420d4c12..7dd33564cbe1 100644
> > --- a/drivers/i2c/busses/i2c-imx-lpi2c.c
> > +++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
> > @@ -211,8 +211,8 @@ static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx)
> >  /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
> >  static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
> >  {
> > -	u8 prescale, filt, sethold, clkhi, clklo, datavd;
> > -	unsigned int clk_rate, clk_cycle;
> > +	u8 prescale, filt, sethold, datavd;
> > +	unsigned int clk_rate, clk_cycle, clkhi, clklo;
> 
> looks OK.
> 
> Reviewed-by: Andi Shyti <andi.shyti@kernel.org> 
> 
> there is, though, another part I would take care of, this bit:
> 
> 	clklo = clk_cycle - clkhi;
> 	if (clklo < 64)
> 		break;
> 
> It might be unlikely, but if "clk_cycle > clkhi" then all the
> calculation. Do you mind adding a check:
> 
> 	if (unlikely(clk_cycle > clkhi))
> 		break;

With clk_cycle being unsigned and clkhi = DIV_ROUND_UP(clk_cycle, 3)
this will be true unless clk_cycle 0 or 1, so I'd say this is quite
likely to trigger?!

I guess you meant something different than you wrote. (And your sentence
no verb.)

Best regards
Uwe
Wolfram Sang June 23, 2023, 8:14 a.m. UTC | #3
On Mon, May 29, 2023 at 04:02:51PM +0800, carlos.song@nxp.com wrote:
> From: Clark Wang <xiaoning.wang@nxp.com>
> 
> Claim clkhi and clklo as integer type to avoid possible calculation
> errors caused by data overflow.
> 
> Fixes: a55fa9d0e42e ("i2c: imx-lpi2c: add low power i2c bus driver")
> Signed-off-by: Clark Wang <xiaoning.wang@nxp.com>
> Signed-off-by: Carlos Song <carlos.song@nxp.com>

Applied to for-current, thanks!
diff mbox series

Patch

diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c
index 40a4420d4c12..7dd33564cbe1 100644
--- a/drivers/i2c/busses/i2c-imx-lpi2c.c
+++ b/drivers/i2c/busses/i2c-imx-lpi2c.c
@@ -211,8 +211,8 @@  static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx)
 /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
 static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
 {
-	u8 prescale, filt, sethold, clkhi, clklo, datavd;
-	unsigned int clk_rate, clk_cycle;
+	u8 prescale, filt, sethold, datavd;
+	unsigned int clk_rate, clk_cycle, clkhi, clklo;
 	enum lpi2c_imx_pincfg pincfg;
 	unsigned int temp;