mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] i2c:at91: Fix a race condition during signal handling in at91_do_twi_xfer.
@ 2014-08-26 19:13 Simon Lindgren
  2014-09-01  9:07 ` Ludovic Desroches
  2014-09-02 10:56 ` Wolfram Sang
  0 siblings, 2 replies; 3+ messages in thread
From: Simon Lindgren @ 2014-08-26 19:13 UTC (permalink / raw)
  To: wsa; +Cc: ludovic.desroches, linux-i2c, linux-kernel, Simon Lindgren

There is a race condition in at91_do_twi_xfer when signals arrive.
If a signal is recieved while waiting for a transfer to complete
wait_for_completion_interruptible_timeout() will return -ERESTARTSYS.
This is not handled correctly resulting in interrupts still being
enabled and a transfer being in flight when we return.

Symptoms include a range of oopses and bus lockups. Oopses can happen
when the transfer completes because the interrupt handler will corrupt
the stack. If a new transfer is started before the interrupt fires
the controller will start a new transfer in the middle of the old one,
resulting in confused slaves and a locked bus.

To avoid this, use wait_for_completion_io_timeout instead so that we
don't have to deal with gracefully shutting down the transfer and
disabling the interrupts.

Signed-off-by: Simon Lindgren <simon@aqwary.com>
---
 drivers/i2c/busses/i2c-at91.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c
index 79a6899..ec299ae 100644
--- a/drivers/i2c/busses/i2c-at91.c
+++ b/drivers/i2c/busses/i2c-at91.c
@@ -421,8 +421,8 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
 		}
 	}
 
-	ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
-							dev->adapter.timeout);
+	ret = wait_for_completion_io_timeout(&dev->cmd_complete,
+					     dev->adapter.timeout);
 	if (ret == 0) {
 		dev_err(dev->dev, "controller timed out\n");
 		at91_init_twi_bus(dev);
-- 
1.7.9.5


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

* Re: [PATCH] i2c:at91: Fix a race condition during signal handling in at91_do_twi_xfer.
  2014-08-26 19:13 [PATCH] i2c:at91: Fix a race condition during signal handling in at91_do_twi_xfer Simon Lindgren
@ 2014-09-01  9:07 ` Ludovic Desroches
  2014-09-02 10:56 ` Wolfram Sang
  1 sibling, 0 replies; 3+ messages in thread
From: Ludovic Desroches @ 2014-09-01  9:07 UTC (permalink / raw)
  To: Simon Lindgren; +Cc: wsa, ludovic.desroches, linux-i2c, linux-kernel

On Tue, Aug 26, 2014 at 09:13:24PM +0200, Simon Lindgren wrote:
> There is a race condition in at91_do_twi_xfer when signals arrive.
> If a signal is recieved while waiting for a transfer to complete
> wait_for_completion_interruptible_timeout() will return -ERESTARTSYS.
> This is not handled correctly resulting in interrupts still being
> enabled and a transfer being in flight when we return.
> 
> Symptoms include a range of oopses and bus lockups. Oopses can happen
> when the transfer completes because the interrupt handler will corrupt
> the stack. If a new transfer is started before the interrupt fires
> the controller will start a new transfer in the middle of the old one,
> resulting in confused slaves and a locked bus.
> 
> To avoid this, use wait_for_completion_io_timeout instead so that we
> don't have to deal with gracefully shutting down the transfer and
> disabling the interrupts.
> 
> Signed-off-by: Simon Lindgren <simon@aqwary.com>
Acked-by: Ludovic Desroches <ludovic.desroches@atmel.com>

Thanks for this fix.

> ---
>  drivers/i2c/busses/i2c-at91.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c
> index 79a6899..ec299ae 100644
> --- a/drivers/i2c/busses/i2c-at91.c
> +++ b/drivers/i2c/busses/i2c-at91.c
> @@ -421,8 +421,8 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
>  		}
>  	}
>  
> -	ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
> -							dev->adapter.timeout);
> +	ret = wait_for_completion_io_timeout(&dev->cmd_complete,
> +					     dev->adapter.timeout);
>  	if (ret == 0) {
>  		dev_err(dev->dev, "controller timed out\n");
>  		at91_init_twi_bus(dev);
> -- 
> 1.7.9.5
> 

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

* Re: [PATCH] i2c:at91: Fix a race condition during signal handling in at91_do_twi_xfer.
  2014-08-26 19:13 [PATCH] i2c:at91: Fix a race condition during signal handling in at91_do_twi_xfer Simon Lindgren
  2014-09-01  9:07 ` Ludovic Desroches
@ 2014-09-02 10:56 ` Wolfram Sang
  1 sibling, 0 replies; 3+ messages in thread
From: Wolfram Sang @ 2014-09-02 10:56 UTC (permalink / raw)
  To: Simon Lindgren; +Cc: ludovic.desroches, linux-i2c, linux-kernel

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

On Tue, Aug 26, 2014 at 09:13:24PM +0200, Simon Lindgren wrote:
> There is a race condition in at91_do_twi_xfer when signals arrive.
> If a signal is recieved while waiting for a transfer to complete
> wait_for_completion_interruptible_timeout() will return -ERESTARTSYS.
> This is not handled correctly resulting in interrupts still being
> enabled and a transfer being in flight when we return.
> 
> Symptoms include a range of oopses and bus lockups. Oopses can happen
> when the transfer completes because the interrupt handler will corrupt
> the stack. If a new transfer is started before the interrupt fires
> the controller will start a new transfer in the middle of the old one,
> resulting in confused slaves and a locked bus.
> 
> To avoid this, use wait_for_completion_io_timeout instead so that we
> don't have to deal with gracefully shutting down the transfer and
> disabling the interrupts.
> 
> Signed-off-by: Simon Lindgren <simon@aqwary.com>

Applied to for-current, thanks!


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

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

end of thread, other threads:[~2014-09-02 10:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-26 19:13 [PATCH] i2c:at91: Fix a race condition during signal handling in at91_do_twi_xfer Simon Lindgren
2014-09-01  9:07 ` Ludovic Desroches
2014-09-02 10:56 ` Wolfram Sang

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

all inboxes | Powered by JetHome®