linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Wang Xin <xin.wang7@cn.bosch.com>,
	Mark Jonas <mark.jonas@de.bosch.com>,
	Bartosz Golaszewski <brgl@bgdev.pl>
Subject: [PATCH 4.14 26/43] eeprom: at24: fix unexpected timeout under high load
Date: Tue,  2 Jul 2019 10:02:06 +0200	[thread overview]
Message-ID: <20190702080125.196152902@linuxfoundation.org> (raw)
In-Reply-To: <20190702080123.904399496@linuxfoundation.org>

From: Wang Xin <xin.wang7@cn.bosch.com>

commit 9a9e295e7c5c0409c020088b0ae017e6c2b7df6e upstream.

Within at24_loop_until_timeout the timestamp used for timeout checking
is recorded after the I2C transfer and sleep_range(). Under high CPU
load either the execution time for I2C transfer or sleep_range() could
actually be larger than the timeout value. Worst case the I2C transfer
is only tried once because the loop will exit due to the timeout
although the EEPROM is now ready.

To fix this issue the timestamp is recorded at the beginning of each
iteration. That is, before I2C transfer and sleep. Then the timeout
is actually checked against the timestamp of the previous iteration.
This makes sure that even if the timeout is reached, there is still one
more chance to try the I2C transfer in case the EEPROM is ready.

Example:

If you have a system which combines high CPU load with repeated EEPROM
writes you will run into the following scenario.

 - System makes a successful regmap_bulk_write() to EEPROM.
 - System wants to perform another write to EEPROM but EEPROM is still
   busy with the last write.
 - Because of high CPU load the usleep_range() will sleep more than
   25 ms (at24_write_timeout).
 - Within the over-long sleeping the EEPROM finished the previous write
   operation and is ready again.
 - at24_loop_until_timeout() will detect timeout and won't try to write.

Signed-off-by: Wang Xin <xin.wang7@cn.bosch.com>
Signed-off-by: Mark Jonas <mark.jonas@de.bosch.com>
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>


---
 drivers/misc/eeprom/at24.c |  107 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 77 insertions(+), 30 deletions(-)

--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -113,22 +113,6 @@ MODULE_PARM_DESC(write_timeout, "Time (i
 	((1 << AT24_SIZE_FLAGS | (_flags)) 		\
 	    << AT24_SIZE_BYTELEN | ilog2(_len))
 
-/*
- * 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 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
- * iteration of processing the request. Both should be unsigned integers
- * holding at least 32 bits.
- */
-#define loop_until_timeout(tout, op_time)				\
-	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[] = {
 	/* needs 8 addresses as A0-A2 are ignored */
 	{ "24c00",	AT24_DEVICE_MAGIC(128 / 8,	AT24_FLAG_TAKE8ADDR) },
@@ -234,7 +218,14 @@ static ssize_t at24_eeprom_read_smbus(st
 	if (count > I2C_SMBUS_BLOCK_MAX)
 		count = I2C_SMBUS_BLOCK_MAX;
 
-	loop_until_timeout(timeout, read_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		read_time = jiffies;
+
 		status = i2c_smbus_read_i2c_block_data_or_emulated(client,
 								   offset,
 								   count, buf);
@@ -244,7 +235,9 @@ static ssize_t at24_eeprom_read_smbus(st
 
 		if (status == count)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(read_time, timeout));
 
 	return -ETIMEDOUT;
 }
@@ -284,7 +277,14 @@ static ssize_t at24_eeprom_read_i2c(stru
 	msg[1].buf = buf;
 	msg[1].len = count;
 
-	loop_until_timeout(timeout, read_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		read_time = jiffies;
+
 		status = i2c_transfer(client->adapter, msg, 2);
 		if (status == 2)
 			status = count;
@@ -294,7 +294,9 @@ static ssize_t at24_eeprom_read_i2c(stru
 
 		if (status == count)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(read_time, timeout));
 
 	return -ETIMEDOUT;
 }
@@ -343,11 +345,20 @@ static ssize_t at24_eeprom_read_serial(s
 	msg[1].buf = buf;
 	msg[1].len = count;
 
-	loop_until_timeout(timeout, read_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		read_time = jiffies;
+
 		status = i2c_transfer(client->adapter, msg, 2);
 		if (status == 2)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(read_time, timeout));
 
 	return -ETIMEDOUT;
 }
@@ -374,11 +385,20 @@ static ssize_t at24_eeprom_read_mac(stru
 	msg[1].buf = buf;
 	msg[1].len = count;
 
-	loop_until_timeout(timeout, read_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		read_time = jiffies;
+
 		status = i2c_transfer(client->adapter, msg, 2);
 		if (status == 2)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(read_time, timeout));
 
 	return -ETIMEDOUT;
 }
@@ -420,7 +440,14 @@ static ssize_t at24_eeprom_write_smbus_b
 	client = at24_translate_offset(at24, &offset);
 	count = at24_adjust_write_count(at24, offset, count);
 
-	loop_until_timeout(timeout, write_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		write_time = jiffies;
+
 		status = i2c_smbus_write_i2c_block_data(client,
 							offset, count, buf);
 		if (status == 0)
@@ -431,7 +458,9 @@ static ssize_t at24_eeprom_write_smbus_b
 
 		if (status == count)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(write_time, timeout));
 
 	return -ETIMEDOUT;
 }
@@ -446,7 +475,14 @@ static ssize_t at24_eeprom_write_smbus_b
 
 	client = at24_translate_offset(at24, &offset);
 
-	loop_until_timeout(timeout, write_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		write_time = jiffies;
+
 		status = i2c_smbus_write_byte_data(client, offset, buf[0]);
 		if (status == 0)
 			status = count;
@@ -456,7 +492,9 @@ static ssize_t at24_eeprom_write_smbus_b
 
 		if (status == count)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(write_time, timeout));
 
 	return -ETIMEDOUT;
 }
@@ -485,7 +523,14 @@ static ssize_t at24_eeprom_write_i2c(str
 	memcpy(&msg.buf[i], buf, count);
 	msg.len = i + count;
 
-	loop_until_timeout(timeout, write_time) {
+	timeout = jiffies + msecs_to_jiffies(write_timeout);
+	do {
+		/*
+		 * The timestamp shall be taken before the actual operation
+		 * to avoid a premature timeout in case of high CPU load.
+		 */
+		write_time = jiffies;
+
 		status = i2c_transfer(client->adapter, &msg, 1);
 		if (status == 1)
 			status = count;
@@ -495,7 +540,9 @@ static ssize_t at24_eeprom_write_i2c(str
 
 		if (status == count)
 			return count;
-	}
+
+		usleep_range(1000, 1500);
+	} while (time_before(write_time, timeout));
 
 	return -ETIMEDOUT;
 }



  parent reply	other threads:[~2019-07-02  8:10 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-02  8:01 [PATCH 4.14 00/43] 4.14.132-stable review Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 01/43] perf ui helpline: Use strlcpy() as a shorter form of strncpy() + explicit set nul Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 02/43] perf help: Remove needless use of strncpy() Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 03/43] perf header: Fix unchecked usage " Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 04/43] Revert "x86/uaccess, ftrace: Fix ftrace_likely_update() vs. SMAP" Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 05/43] IB/hfi1: Close PSM sdma_progress sleep window Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 06/43] block: add a lower-level bio_add_page interface Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 07/43] block: bio_iov_iter_get_pages: pin more pages for multi-segment IOs Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 08/43] 9p/xen: fix check for xenbus_read error in front_probe Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 09/43] 9p/rdma: do not disconnect on down_interruptible EAGAIN Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 10/43] 9p: acl: fix uninitialized iattr access Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 11/43] 9p/rdma: remove useless check in cm_event_handler Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 12/43] 9p: p9dirent_read: check network-provided name length Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 13/43] net/9p: include trans_common.h to fix missing prototype warning Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 14/43] qmi_wwan: Fix out-of-bounds read Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 15/43] Revert "compiler.h: update definition of unreachable()" Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 16/43] fs/proc/array.c: allow reporting eip/esp for all coredumping threads Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 17/43] mm/mempolicy.c: fix an incorrect rebind node in mpol_rebind_nodemask Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 18/43] fs/binfmt_flat.c: make load_flat_shared_library() work Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.14 19/43] mm/page_idle.c: fix oops because end_pfn is larger than max_pfn Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 20/43] dm log writes: make sure super sector log updates are written in order Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 21/43] scsi: vmw_pscsi: Fix use-after-free in pvscsi_queue_lck() Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 22/43] x86/speculation: Allow guests to use SSBD even if host does not Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 23/43] x86/microcode: Fix the microcode load on CPU hotplug for real Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 24/43] NFS/flexfiles: Use the correct TCP timeout for flexfiles I/O Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 25/43] cpu/speculation: Warn on unsupported mitigations= parameter Greg Kroah-Hartman
2019-07-02  8:02 ` Greg Kroah-Hartman [this message]
2019-07-02  8:02 ` [PATCH 4.14 27/43] af_packet: Block execution of tasks waiting for transmit to complete in AF_PACKET Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 28/43] ipv4: Use return value of inet_iif() for __raw_v4_lookup in the while loop Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 29/43] net/packet: fix memory leak in packet_set_ring() Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 30/43] net: remove duplicate fetch in sock_getsockopt Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 31/43] net: stmmac: fixed new system time seconds value calculation Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 32/43] sctp: change to hold sk after auth shkey is created successfully Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 33/43] tipc: change to use register_pernet_device Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 34/43] tipc: check msg->req data len in tipc_nl_compat_bearer_disable Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 35/43] tun: wake up waitqueues after IFF_UP is set Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 36/43] team: Always enable vlan tx offload Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 37/43] bonding: " Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 38/43] bpf: udp: Avoid calling reuseports bpf_prog from udp_gro Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 39/43] bpf: udp: ipv6: Avoid running reuseports bpf_prog from __udp6_lib_err Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 40/43] arm64: futex: Avoid copying out uninitialised stack in failed cmpxchg() Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 41/43] bpf, arm64: use more scalable stadd over ldxr / stxr loop in xadd Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 42/43] futex: Update comments and docs about return values of arch futex code Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.14 43/43] tipc: pass tunnel dev as NULL to udp_tunnel(6)_xmit_skb Greg Kroah-Hartman
2019-08-01 10:17   ` Rantala, Tommi T. (Nokia - FI/Espoo)
2019-08-02  7:28     ` gregkh
2019-08-02 11:03       ` Rantala, Tommi T. (Nokia - FI/Espoo)
2019-08-02 11:34         ` gregkh
2019-08-03  0:45         ` Xin Long
2019-08-03  7:11           ` gregkh
2019-07-02 12:12 ` [PATCH 4.14 00/43] 4.14.132-stable review kernelci.org bot
2019-07-02 15:43 ` Naresh Kamboju
2019-07-02 20:22 ` Guenter Roeck
2019-07-02 21:10 ` Kelsey Skunberg
2019-07-02 22:51 ` shuah
2019-07-03 10:20 ` Jon Hunter
2019-07-04  5:26 ` Bharath Vedartham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190702080125.196152902@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=brgl@bgdev.pl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.jonas@de.bosch.com \
    --cc=stable@vger.kernel.org \
    --cc=xin.wang7@cn.bosch.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).