linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] i2c: sprd: use a specific timeout to avoid system hang up issue
@ 2020-12-14  4:58 Chunyan Zhang
  2021-01-04 18:23 ` Wolfram Sang
  0 siblings, 1 reply; 3+ messages in thread
From: Chunyan Zhang @ 2020-12-14  4:58 UTC (permalink / raw)
  To: Wolfram Sang, Baolin Wang, Orson Zhai
  Cc: linux-i2c, linux-kernel, Chunyan Zhang, Chunyan Zhang, Linhua Xu

From: Chunyan Zhang <chunyan.zhang@unisoc.com>

If the i2c device SCL bus being pulled up due to some exception before
message transfer done, the system cannot receive the completing interrupt
signal any more, it would not exit waiting loop until MAX_SCHEDULE_TIMEOUT
jiffies eclipse, that would make the system seemed hang up. To avoid that
happen, this patch adds a specific timeout for message transfer.

Fixes: 8b9ec0719834 ("i2c: Add Spreadtrum I2C controller driver")
Signed-off-by: Linhua Xu <linhua.xu@unisoc.com>
Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>
---
* Changes since v1:
- Addressed Wolfram's comments for code;
- Changed to use Signed-off-by instead of Original-by;
- Fixed a compile error.
---
 drivers/i2c/busses/i2c-sprd.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-sprd.c b/drivers/i2c/busses/i2c-sprd.c
index 19cda6742423..b9f58a4b2281 100644
--- a/drivers/i2c/busses/i2c-sprd.c
+++ b/drivers/i2c/busses/i2c-sprd.c
@@ -72,6 +72,8 @@
 
 /* timeout (ms) for pm runtime autosuspend */
 #define SPRD_I2C_PM_TIMEOUT	1000
+/* timeout (ms) for transfer message */
+#define I2C_XFER_TIMEOUT	1000
 
 /* SPRD i2c data structure */
 struct sprd_i2c {
@@ -244,6 +246,7 @@ static int sprd_i2c_handle_msg(struct i2c_adapter *i2c_adap,
 			       struct i2c_msg *msg, bool is_last_msg)
 {
 	struct sprd_i2c *i2c_dev = i2c_adap->algo_data;
+	unsigned long time_left;
 
 	i2c_dev->msg = msg;
 	i2c_dev->buf = msg->buf;
@@ -273,7 +276,10 @@ static int sprd_i2c_handle_msg(struct i2c_adapter *i2c_adap,
 
 	sprd_i2c_opt_start(i2c_dev);
 
-	wait_for_completion(&i2c_dev->complete);
+	time_left = wait_for_completion_timeout(&i2c_dev->complete,
+				msecs_to_jiffies(I2C_XFER_TIMEOUT));
+	if (!time_left)
+		return -EIO;
 
 	return i2c_dev->err;
 }
-- 
2.25.1


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

* Re: [PATCH v2] i2c: sprd: use a specific timeout to avoid system hang up issue
  2020-12-14  4:58 [PATCH v2] i2c: sprd: use a specific timeout to avoid system hang up issue Chunyan Zhang
@ 2021-01-04 18:23 ` Wolfram Sang
  2021-01-05  2:13   ` Chunyan Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Wolfram Sang @ 2021-01-04 18:23 UTC (permalink / raw)
  To: Chunyan Zhang
  Cc: Baolin Wang, Orson Zhai, linux-i2c, linux-kernel, Chunyan Zhang,
	Linhua Xu

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

On Mon, Dec 14, 2020 at 12:58:50PM +0800, Chunyan Zhang wrote:
> From: Chunyan Zhang <chunyan.zhang@unisoc.com>
> 
> If the i2c device SCL bus being pulled up due to some exception before
> message transfer done, the system cannot receive the completing interrupt
> signal any more, it would not exit waiting loop until MAX_SCHEDULE_TIMEOUT
> jiffies eclipse, that would make the system seemed hang up. To avoid that
> happen, this patch adds a specific timeout for message transfer.
> 
> Fixes: 8b9ec0719834 ("i2c: Add Spreadtrum I2C controller driver")
> Signed-off-by: Linhua Xu <linhua.xu@unisoc.com>
> Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>

Took the liberty to change the errno to ETIMEDOUT and applied to
for-current, thanks!


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

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

* Re: [PATCH v2] i2c: sprd: use a specific timeout to avoid system hang up issue
  2021-01-04 18:23 ` Wolfram Sang
@ 2021-01-05  2:13   ` Chunyan Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Chunyan Zhang @ 2021-01-05  2:13 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Baolin Wang, Orson Zhai, linux-i2c, Linux Kernel Mailing List,
	Chunyan Zhang, Linhua Xu

On Tue, 5 Jan 2021 at 02:24, Wolfram Sang <wsa@kernel.org> wrote:
>
> On Mon, Dec 14, 2020 at 12:58:50PM +0800, Chunyan Zhang wrote:
> > From: Chunyan Zhang <chunyan.zhang@unisoc.com>
> >
> > If the i2c device SCL bus being pulled up due to some exception before
> > message transfer done, the system cannot receive the completing interrupt
> > signal any more, it would not exit waiting loop until MAX_SCHEDULE_TIMEOUT
> > jiffies eclipse, that would make the system seemed hang up. To avoid that
> > happen, this patch adds a specific timeout for message transfer.
> >
> > Fixes: 8b9ec0719834 ("i2c: Add Spreadtrum I2C controller driver")
> > Signed-off-by: Linhua Xu <linhua.xu@unisoc.com>
> > Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>
>
> Took the liberty to change the errno to ETIMEDOUT and applied to
> for-current, thanks!

Thanks!

Chunyan

>

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

end of thread, other threads:[~2021-01-05  2:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-14  4:58 [PATCH v2] i2c: sprd: use a specific timeout to avoid system hang up issue Chunyan Zhang
2021-01-04 18:23 ` Wolfram Sang
2021-01-05  2:13   ` Chunyan Zhang

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