public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] i2c: designware: use u64 for clock freq to avoid u32 multiplication overflow
@ 2022-12-14 10:34 Hanna Hawa
  2022-12-14 11:44 ` Andy Shevchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Hanna Hawa @ 2022-12-14 10:34 UTC (permalink / raw)
  To: jarkko.nikula, andriy.shevchenko, mika.westerberg, jsd, linux-i2c,
	linux-kernel
  Cc: dwmw, benh, ronenk, talel, jonnyc, hanochu, farbere, itamark,
	lareine, hhhawa

From: Lareine Khawaly <lareine@amazon.com>

In functions i2c_dw_scl_lcnt() and i2c_dw_scl_hcnt() may have overflow
by depending on the values of the given parameters including the ic_clk.
For example in our usecase where ic_clk >= 1000000, multiplication of
ic_clk * 4700 will result in 32 bit overflow.

This commit change the ic_clk to be u64 parameter to avoid the overflow.

Signed-off-by: Lareine Khawaly <lareine@amazon.com>
Signed-off-by: Hanna Hawa <hhhawa@amazon.com>
---
 drivers/i2c/busses/i2c-designware-common.c | 4 ++--
 drivers/i2c/busses/i2c-designware-core.h   | 4 ++--
 drivers/i2c/busses/i2c-designware-master.c | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/busses/i2c-designware-common.c b/drivers/i2c/busses/i2c-designware-common.c
index c023b691441e..61a6b7bb8935 100644
--- a/drivers/i2c/busses/i2c-designware-common.c
+++ b/drivers/i2c/busses/i2c-designware-common.c
@@ -332,7 +332,7 @@ void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
 }
 EXPORT_SYMBOL_GPL(i2c_dw_adjust_bus_speed);
 
-u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
+u32 i2c_dw_scl_hcnt(u64 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
 {
 	/*
 	 * DesignWare I2C core doesn't seem to have solid strategy to meet
@@ -370,7 +370,7 @@ u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
 		return DIV_ROUND_CLOSEST(ic_clk * (tSYMBOL + tf), MICRO) - 3 + offset;
 }
 
-u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
+u32 i2c_dw_scl_lcnt(u64 ic_clk, u32 tLOW, u32 tf, int offset)
 {
 	/*
 	 * Conditional expression:
diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h
index 4d3a3b464ecd..aaba6f9977b6 100644
--- a/drivers/i2c/busses/i2c-designware-core.h
+++ b/drivers/i2c/busses/i2c-designware-core.h
@@ -319,8 +319,8 @@ struct i2c_dw_semaphore_callbacks {
 };
 
 int i2c_dw_init_regmap(struct dw_i2c_dev *dev);
-u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset);
-u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset);
+u32 i2c_dw_scl_hcnt(u64 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset);
+u32 i2c_dw_scl_lcnt(u64 ic_clk, u32 tLOW, u32 tf, int offset);
 int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev);
 unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev);
 int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare);
diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c
index 478318b1d35f..d6960507ea46 100644
--- a/drivers/i2c/busses/i2c-designware-master.c
+++ b/drivers/i2c/busses/i2c-designware-master.c
@@ -44,7 +44,7 @@ static int i2c_dw_set_timings_master(struct dw_i2c_dev *dev)
 	u32 sda_falling_time, scl_falling_time;
 	struct i2c_timings *t = &dev->timings;
 	const char *fp_str = "";
-	u32 ic_clk;
+	u64 ic_clk;
 	int ret;
 
 	ret = i2c_dw_acquire_lock(dev);
-- 
2.38.1


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

* Re: [PATCH 1/1] i2c: designware: use u64 for clock freq to avoid u32 multiplication overflow
  2022-12-14 10:34 [PATCH 1/1] i2c: designware: use u64 for clock freq to avoid u32 multiplication overflow Hanna Hawa
@ 2022-12-14 11:44 ` Andy Shevchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2022-12-14 11:44 UTC (permalink / raw)
  To: Hanna Hawa
  Cc: jarkko.nikula, mika.westerberg, jsd, linux-i2c, linux-kernel,
	dwmw, benh, ronenk, talel, jonnyc, hanochu, farbere, itamark,
	lareine

On Wed, Dec 14, 2022 at 10:34:18AM +0000, Hanna Hawa wrote:
> From: Lareine Khawaly <lareine@amazon.com>
> 
> In functions i2c_dw_scl_lcnt() and i2c_dw_scl_hcnt() may have overflow
> by depending on the values of the given parameters including the ic_clk.
> For example in our usecase where ic_clk >= 1000000, multiplication of

It's hard to count 0:s, can you use plain words instead (like million, billion)?

> ic_clk * 4700 will result in 32 bit overflow.

> This commit change the ic_clk to be u64 parameter to avoid the overflow.

Read Submitting Patches about imperative mood in the commit messages.

...

Smells like you are missing the Fixes tag.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2022-12-14 11:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-14 10:34 [PATCH 1/1] i2c: designware: use u64 for clock freq to avoid u32 multiplication overflow Hanna Hawa
2022-12-14 11:44 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox