diff mbox series

[v2] i2c: bcm-iproc: Fix bcm_iproc_i2c_isr deadlock issue

Message ID 20230707084941.28964-1-dg573847474@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2] i2c: bcm-iproc: Fix bcm_iproc_i2c_isr deadlock issue | expand

Commit Message

Chengfeng Ye July 7, 2023, 8:49 a.m. UTC
iproc_i2c_rd_reg() and iproc_i2c_wr_reg() are called from both
interrupt context (e.g. bcm_iproc_i2c_isr) and process context
(e.g. bcm_iproc_i2c_suspend). Therefore, interrupts should be
disabled to avoid potential deadlock. To prevent this scenario,
use spin_lock_irqsave().

Fixes: 9a1038728037 ("i2c: iproc: add NIC I2C support")
Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>
---
 drivers/i2c/busses/i2c-bcm-iproc.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Comments

Ray Jui July 7, 2023, 3:50 p.m. UTC | #1
On 7/7/2023 1:49 AM, Chengfeng Ye wrote:
> iproc_i2c_rd_reg() and iproc_i2c_wr_reg() are called from both
> interrupt context (e.g. bcm_iproc_i2c_isr) and process context
> (e.g. bcm_iproc_i2c_suspend). Therefore, interrupts should be
> disabled to avoid potential deadlock. To prevent this scenario,
> use spin_lock_irqsave().
> 
> Fixes: 9a1038728037 ("i2c: iproc: add NIC I2C support")
> Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>
> ---
>  drivers/i2c/busses/i2c-bcm-iproc.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-bcm-iproc.c b/drivers/i2c/busses/i2c-bcm-iproc.c
> index 85d8a6b04885..30a2a3200bed 100644
> --- a/drivers/i2c/busses/i2c-bcm-iproc.c
> +++ b/drivers/i2c/busses/i2c-bcm-iproc.c
> @@ -233,13 +233,14 @@ static inline u32 iproc_i2c_rd_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
>  				   u32 offset)
>  {
>  	u32 val;
> +	unsigned long flags;
>  
>  	if (iproc_i2c->idm_base) {
> -		spin_lock(&iproc_i2c->idm_lock);
> +		spin_lock_irqsave(&iproc_i2c->idm_lock, flags);
>  		writel(iproc_i2c->ape_addr_mask,
>  		       iproc_i2c->idm_base + IDM_CTRL_DIRECT_OFFSET);
>  		val = readl(iproc_i2c->base + offset);
> -		spin_unlock(&iproc_i2c->idm_lock);
> +		spin_unlock_irqrestore(&iproc_i2c->idm_lock, flags);
>  	} else {
>  		val = readl(iproc_i2c->base + offset);
>  	}
> @@ -250,12 +251,14 @@ static inline u32 iproc_i2c_rd_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
>  static inline void iproc_i2c_wr_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
>  				    u32 offset, u32 val)
>  {
> +	unsigned long flags;
> +
>  	if (iproc_i2c->idm_base) {
> -		spin_lock(&iproc_i2c->idm_lock);
> +		spin_lock_irqsave(&iproc_i2c->idm_lock, flags);
>  		writel(iproc_i2c->ape_addr_mask,
>  		       iproc_i2c->idm_base + IDM_CTRL_DIRECT_OFFSET);
>  		writel(val, iproc_i2c->base + offset);
> -		spin_unlock(&iproc_i2c->idm_lock);
> +		spin_unlock_irqrestore(&iproc_i2c->idm_lock, flags);
>  	} else {
>  		writel(val, iproc_i2c->base + offset);
>  	}

Acked-by: Ray Jui <ray.jui@broadcom.com>

Thanks, Chengfeng.
Andi Shyti July 27, 2023, 8:43 p.m. UTC | #2
Hi Chengfeng,

On Fri, Jul 07, 2023 at 08:49:41AM +0000, Chengfeng Ye wrote:
> iproc_i2c_rd_reg() and iproc_i2c_wr_reg() are called from both
> interrupt context (e.g. bcm_iproc_i2c_isr) and process context
> (e.g. bcm_iproc_i2c_suspend). Therefore, interrupts should be
> disabled to avoid potential deadlock. To prevent this scenario,
> use spin_lock_irqsave().
> 
> Fixes: 9a1038728037 ("i2c: iproc: add NIC I2C support")
> Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>

Reviewed-by: Andi Shyti <andi.shyti@kernel.org> 

Thanks,
Andi
Wolfram Sang Aug. 14, 2023, 4:17 p.m. UTC | #3
On Fri, Jul 07, 2023 at 08:49:41AM +0000, Chengfeng Ye wrote:
> iproc_i2c_rd_reg() and iproc_i2c_wr_reg() are called from both
> interrupt context (e.g. bcm_iproc_i2c_isr) and process context
> (e.g. bcm_iproc_i2c_suspend). Therefore, interrupts should be
> disabled to avoid potential deadlock. To prevent this scenario,
> use spin_lock_irqsave().
> 
> Fixes: 9a1038728037 ("i2c: iproc: add NIC I2C support")
> Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>

Applied to for-current, thanks!
diff mbox series

Patch

diff --git a/drivers/i2c/busses/i2c-bcm-iproc.c b/drivers/i2c/busses/i2c-bcm-iproc.c
index 85d8a6b04885..30a2a3200bed 100644
--- a/drivers/i2c/busses/i2c-bcm-iproc.c
+++ b/drivers/i2c/busses/i2c-bcm-iproc.c
@@ -233,13 +233,14 @@  static inline u32 iproc_i2c_rd_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
 				   u32 offset)
 {
 	u32 val;
+	unsigned long flags;
 
 	if (iproc_i2c->idm_base) {
-		spin_lock(&iproc_i2c->idm_lock);
+		spin_lock_irqsave(&iproc_i2c->idm_lock, flags);
 		writel(iproc_i2c->ape_addr_mask,
 		       iproc_i2c->idm_base + IDM_CTRL_DIRECT_OFFSET);
 		val = readl(iproc_i2c->base + offset);
-		spin_unlock(&iproc_i2c->idm_lock);
+		spin_unlock_irqrestore(&iproc_i2c->idm_lock, flags);
 	} else {
 		val = readl(iproc_i2c->base + offset);
 	}
@@ -250,12 +251,14 @@  static inline u32 iproc_i2c_rd_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
 static inline void iproc_i2c_wr_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
 				    u32 offset, u32 val)
 {
+	unsigned long flags;
+
 	if (iproc_i2c->idm_base) {
-		spin_lock(&iproc_i2c->idm_lock);
+		spin_lock_irqsave(&iproc_i2c->idm_lock, flags);
 		writel(iproc_i2c->ape_addr_mask,
 		       iproc_i2c->idm_base + IDM_CTRL_DIRECT_OFFSET);
 		writel(val, iproc_i2c->base + offset);
-		spin_unlock(&iproc_i2c->idm_lock);
+		spin_unlock_irqrestore(&iproc_i2c->idm_lock, flags);
 	} else {
 		writel(val, iproc_i2c->base + offset);
 	}