linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] eeprom: at24: tweak the loop_until_timeout() macro
@ 2016-07-17 18:40 Bartosz Golaszewski
  2016-07-18 18:11 ` Wolfram Sang
  0 siblings, 1 reply; 2+ messages in thread
From: Bartosz Golaszewski @ 2016-07-17 18:40 UTC (permalink / raw)
  To: Wolfram Sang, linux-i2c, LKML, Andrew Lunn, Srinivas Kandagatla,
	Maxime Ripard, GregKH
  Cc: Bartosz Golaszewski

loop_until_timeout() replaced a do {} while loop in the at24 driver
with a for loop which, under certain circumstances (such as heavy load
or low value of the write_timeout argument), can lead to the code in
the loop never being executed.

Make sure that at least one iteration of the code enclosed within
loop_until_timeout() is always executed.

Suggested-by: Wolfram Sang <wsa@the-dreams.de>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/misc/eeprom/at24.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index 0b8fafdb..1f62aaf 100644
--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -116,7 +116,8 @@ MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
 /*
  * Both reads and writes fail if the previous write didn't complete yet. This
  * macro loops a few times waiting at least long enough for one entire page
- * write to work.
+ * write to work while making sure that at least one iteration is run before
+ * checking the break condition.
  *
  * It takes two parameters: a variable in which the future timeout in jiffies
  * will be stored and a temporary variable holding the time of the last
@@ -124,9 +125,8 @@ MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
  * holding at least 32 bits.
  */
 #define loop_until_timeout(tout, op_time)				\
-	for (tout = jiffies + msecs_to_jiffies(write_timeout),		\
-		op_time = jiffies;					\
-	     time_before(op_time, tout);				\
+	for (tout = jiffies + msecs_to_jiffies(write_timeout), op_time = 0; \
+	     op_time ? time_before(op_time, tout) : true;		\
 	     usleep_range(1000, 1500), op_time = jiffies)
 
 static const struct i2c_device_id at24_ids[] = {
-- 
2.7.4

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

* Re: [PATCH] eeprom: at24: tweak the loop_until_timeout() macro
  2016-07-17 18:40 [PATCH] eeprom: at24: tweak the loop_until_timeout() macro Bartosz Golaszewski
@ 2016-07-18 18:11 ` Wolfram Sang
  0 siblings, 0 replies; 2+ messages in thread
From: Wolfram Sang @ 2016-07-18 18:11 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: linux-i2c, LKML, Andrew Lunn, Srinivas Kandagatla, Maxime Ripard,
	GregKH

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

On Sun, Jul 17, 2016 at 08:40:06PM +0200, Bartosz Golaszewski wrote:
> loop_until_timeout() replaced a do {} while loop in the at24 driver
> with a for loop which, under certain circumstances (such as heavy load
> or low value of the write_timeout argument), can lead to the code in
> the loop never being executed.
> 
> Make sure that at least one iteration of the code enclosed within
> loop_until_timeout() is always executed.
> 
> Suggested-by: Wolfram Sang <wsa@the-dreams.de>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Applied to for-next, thanks!


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

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

end of thread, other threads:[~2016-07-18 18:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-17 18:40 [PATCH] eeprom: at24: tweak the loop_until_timeout() macro Bartosz Golaszewski
2016-07-18 18:11 ` Wolfram Sang

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