All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-15 22:04 ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-15 22:04 UTC (permalink / raw)
  To: linux-kernel
  Cc: Anand Moon, Paul Osmialowski, Javier Martinez Canillas,
	Kukjin Kim, linux-samsung-soc, Wolfram Sang, Krzysztof Kozlowski,
	linux-i2c, linux-arm-kernel

The exynos5 I2C controller driver always prepares and enables a clock
before using it and then disables unprepares it when the clock is not
used anymore.

But this can cause a possible ABBA deadlock in some scenarios since a
driver that uses regmap to access its I2C registers, will first grab
the regmap lock and then the I2C xfer function will grab the prepare
lock when preparing the I2C clock. But since the clock driver also
uses regmap for I2C accesses, preparing a clock will first grab the
prepare lock and then the regmap lock when using the regmap API.

An example of this happens on the Exynos5422 Odroid XU board where a
s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
share the same I2C regmap.

The possible deadlock is reported by the kernel lockdep:

  Possible unsafe locking scenario:

        CPU0                    CPU1
        ----                    ----
   lock(sec_core:428:(regmap)->lock);
                                lock(prepare_lock);
                                lock(sec_core:428:(regmap)->lock);
   lock(prepare_lock);

  *** DEADLOCK ***

Fix this by only preparing the clock on probe and {en,dis}able in the
rest of the driver.

This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
deadlock by keeping clock prepared") that fixes the same bug in other
driver for an I2C controller found in Samsung SoCs.

Reported-by: Anand Moon <linux.amoon@gmail.com>
Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>

---

 drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
index b29c7500461a..602633747149 100644
--- a/drivers/i2c/busses/i2c-exynos5.c
+++ b/drivers/i2c/busses/i2c-exynos5.c
@@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
 		return -EIO;
 	}
 
-	clk_prepare_enable(i2c->clk);
+	ret = clk_enable(i2c->clk);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < num; i++, msgs++) {
 		stop = (i == num - 1);
@@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
 	}
 
  out:
-	clk_disable_unprepare(i2c->clk);
+	clk_disable(i2c->clk);
 	return ret;
 }
 
@@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, i2c);
 
+	clk_disable(i2c->clk);
+
+	return 0;
+
  err_clk:
 	clk_disable_unprepare(i2c->clk);
 	return ret;
@@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
 
 	i2c_del_adapter(&i2c->adap);
 
+	clk_unprepare(i2c->clk);
+
 	return 0;
 }
 
@@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
 	struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
 	int ret = 0;
 
-	clk_prepare_enable(i2c->clk);
+	ret = clk_enable(i2c->clk);
+	if (ret)
+		return ret;
 
 	ret = exynos5_hsi2c_clock_setup(i2c);
 	if (ret) {
-		clk_disable_unprepare(i2c->clk);
+		clk_disable(i2c->clk);
 		return ret;
 	}
 
 	exynos5_i2c_init(i2c);
-	clk_disable_unprepare(i2c->clk);
+	clk_disable(i2c->clk);
 	i2c->suspended = 0;
 
 	return 0;
-- 
2.5.5

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-15 22:04 ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-15 22:04 UTC (permalink / raw)
  To: linux-arm-kernel

The exynos5 I2C controller driver always prepares and enables a clock
before using it and then disables unprepares it when the clock is not
used anymore.

But this can cause a possible ABBA deadlock in some scenarios since a
driver that uses regmap to access its I2C registers, will first grab
the regmap lock and then the I2C xfer function will grab the prepare
lock when preparing the I2C clock. But since the clock driver also
uses regmap for I2C accesses, preparing a clock will first grab the
prepare lock and then the regmap lock when using the regmap API.

An example of this happens on the Exynos5422 Odroid XU board where a
s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
share the same I2C regmap.

The possible deadlock is reported by the kernel lockdep:

  Possible unsafe locking scenario:

        CPU0                    CPU1
        ----                    ----
   lock(sec_core:428:(regmap)->lock);
                                lock(prepare_lock);
                                lock(sec_core:428:(regmap)->lock);
   lock(prepare_lock);

  *** DEADLOCK ***

Fix this by only preparing the clock on probe and {en,dis}able in the
rest of the driver.

This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
deadlock by keeping clock prepared") that fixes the same bug in other
driver for an I2C controller found in Samsung SoCs.

Reported-by: Anand Moon <linux.amoon@gmail.com>
Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>

---

 drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
index b29c7500461a..602633747149 100644
--- a/drivers/i2c/busses/i2c-exynos5.c
+++ b/drivers/i2c/busses/i2c-exynos5.c
@@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
 		return -EIO;
 	}
 
-	clk_prepare_enable(i2c->clk);
+	ret = clk_enable(i2c->clk);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < num; i++, msgs++) {
 		stop = (i == num - 1);
@@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
 	}
 
  out:
-	clk_disable_unprepare(i2c->clk);
+	clk_disable(i2c->clk);
 	return ret;
 }
 
@@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, i2c);
 
+	clk_disable(i2c->clk);
+
+	return 0;
+
  err_clk:
 	clk_disable_unprepare(i2c->clk);
 	return ret;
@@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
 
 	i2c_del_adapter(&i2c->adap);
 
+	clk_unprepare(i2c->clk);
+
 	return 0;
 }
 
@@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
 	struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
 	int ret = 0;
 
-	clk_prepare_enable(i2c->clk);
+	ret = clk_enable(i2c->clk);
+	if (ret)
+		return ret;
 
 	ret = exynos5_hsi2c_clock_setup(i2c);
 	if (ret) {
-		clk_disable_unprepare(i2c->clk);
+		clk_disable(i2c->clk);
 		return ret;
 	}
 
 	exynos5_i2c_init(i2c);
-	clk_disable_unprepare(i2c->clk);
+	clk_disable(i2c->clk);
 	i2c->suspended = 0;
 
 	return 0;
-- 
2.5.5

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-15 22:04 ` Javier Martinez Canillas
  (?)
@ 2016-04-16 12:15   ` Anand Moon
  -1 siblings, 0 replies; 20+ messages in thread
From: Anand Moon @ 2016-04-16 12:15 UTC (permalink / raw)
  To: Javier Martinez Canillas
  Cc: Linux Kernel, Paul Osmialowski, Kukjin Kim, linux-samsung-soc,
	Wolfram Sang, Krzysztof Kozlowski, linux-i2c, linux-arm-kernel

Hi Javier,

On 16 April 2016 at 03:34, Javier Martinez Canillas
<javier@osg.samsung.com> wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
>
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
>
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
>
> The possible deadlock is reported by the kernel lockdep:
>
>   Possible unsafe locking scenario:
>
>         CPU0                    CPU1
>         ----                    ----
>    lock(sec_core:428:(regmap)->lock);
>                                 lock(prepare_lock);
>                                 lock(sec_core:428:(regmap)->lock);
>    lock(prepare_lock);
>
>   *** DEADLOCK ***
>
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
>
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.
>
> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>
> ---
>
>  drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>                 return -EIO;
>         }
>
> -       clk_prepare_enable(i2c->clk);
> +       ret = clk_enable(i2c->clk);
> +       if (ret)
> +               return ret;
>
>         for (i = 0; i < num; i++, msgs++) {
>                 stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>         }
>
>   out:
> -       clk_disable_unprepare(i2c->clk);
> +       clk_disable(i2c->clk);
>         return ret;
>  }
>
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>
>         platform_set_drvdata(pdev, i2c);
>
> +       clk_disable(i2c->clk);
> +
> +       return 0;
> +
>   err_clk:
>         clk_disable_unprepare(i2c->clk);
>         return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>
>         i2c_del_adapter(&i2c->adap);
>
> +       clk_unprepare(i2c->clk);
> +
>         return 0;
>  }
>
> @@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
>         struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
>         int ret = 0;
>
> -       clk_prepare_enable(i2c->clk);
> +       ret = clk_enable(i2c->clk);
> +       if (ret)
> +               return ret;
>
>         ret = exynos5_hsi2c_clock_setup(i2c);
>         if (ret) {
> -               clk_disable_unprepare(i2c->clk);
> +               clk_disable(i2c->clk);
>                 return ret;
>         }
>
>         exynos5_i2c_init(i2c);
> -       clk_disable_unprepare(i2c->clk);
> +       clk_disable(i2c->clk);
>         i2c->suspended = 0;
>
>         return 0;
> --
> 2.5.5
>

Thanks for fixing this bug. I also tried the same thing on clk, but
missed the return on failed.

Reviewed-by: Anand Moon <linux.amoon@gmail.com>
Tested-by: Anand Moon <linux.amoon@gmail.com>

Best Regards
-Anand Moon

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-16 12:15   ` Anand Moon
  0 siblings, 0 replies; 20+ messages in thread
From: Anand Moon @ 2016-04-16 12:15 UTC (permalink / raw)
  To: Javier Martinez Canillas
  Cc: Linux Kernel, Paul Osmialowski, Kukjin Kim, linux-samsung-soc,
	Wolfram Sang, Krzysztof Kozlowski, linux-i2c, linux-arm-kernel

Hi Javier,

On 16 April 2016 at 03:34, Javier Martinez Canillas
<javier@osg.samsung.com> wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
>
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
>
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
>
> The possible deadlock is reported by the kernel lockdep:
>
>   Possible unsafe locking scenario:
>
>         CPU0                    CPU1
>         ----                    ----
>    lock(sec_core:428:(regmap)->lock);
>                                 lock(prepare_lock);
>                                 lock(sec_core:428:(regmap)->lock);
>    lock(prepare_lock);
>
>   *** DEADLOCK ***
>
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
>
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.
>
> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>
> ---
>
>  drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>                 return -EIO;
>         }
>
> -       clk_prepare_enable(i2c->clk);
> +       ret = clk_enable(i2c->clk);
> +       if (ret)
> +               return ret;
>
>         for (i = 0; i < num; i++, msgs++) {
>                 stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>         }
>
>   out:
> -       clk_disable_unprepare(i2c->clk);
> +       clk_disable(i2c->clk);
>         return ret;
>  }
>
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>
>         platform_set_drvdata(pdev, i2c);
>
> +       clk_disable(i2c->clk);
> +
> +       return 0;
> +
>   err_clk:
>         clk_disable_unprepare(i2c->clk);
>         return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>
>         i2c_del_adapter(&i2c->adap);
>
> +       clk_unprepare(i2c->clk);
> +
>         return 0;
>  }
>
> @@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
>         struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
>         int ret = 0;
>
> -       clk_prepare_enable(i2c->clk);
> +       ret = clk_enable(i2c->clk);
> +       if (ret)
> +               return ret;
>
>         ret = exynos5_hsi2c_clock_setup(i2c);
>         if (ret) {
> -               clk_disable_unprepare(i2c->clk);
> +               clk_disable(i2c->clk);
>                 return ret;
>         }
>
>         exynos5_i2c_init(i2c);
> -       clk_disable_unprepare(i2c->clk);
> +       clk_disable(i2c->clk);
>         i2c->suspended = 0;
>
>         return 0;
> --
> 2.5.5
>

Thanks for fixing this bug. I also tried the same thing on clk, but
missed the return on failed.

Reviewed-by: Anand Moon <linux.amoon@gmail.com>
Tested-by: Anand Moon <linux.amoon@gmail.com>

Best Regards
-Anand Moon

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-16 12:15   ` Anand Moon
  0 siblings, 0 replies; 20+ messages in thread
From: Anand Moon @ 2016-04-16 12:15 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Javier,

On 16 April 2016 at 03:34, Javier Martinez Canillas
<javier@osg.samsung.com> wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
>
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
>
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
>
> The possible deadlock is reported by the kernel lockdep:
>
>   Possible unsafe locking scenario:
>
>         CPU0                    CPU1
>         ----                    ----
>    lock(sec_core:428:(regmap)->lock);
>                                 lock(prepare_lock);
>                                 lock(sec_core:428:(regmap)->lock);
>    lock(prepare_lock);
>
>   *** DEADLOCK ***
>
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
>
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.
>
> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>
> ---
>
>  drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>                 return -EIO;
>         }
>
> -       clk_prepare_enable(i2c->clk);
> +       ret = clk_enable(i2c->clk);
> +       if (ret)
> +               return ret;
>
>         for (i = 0; i < num; i++, msgs++) {
>                 stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>         }
>
>   out:
> -       clk_disable_unprepare(i2c->clk);
> +       clk_disable(i2c->clk);
>         return ret;
>  }
>
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>
>         platform_set_drvdata(pdev, i2c);
>
> +       clk_disable(i2c->clk);
> +
> +       return 0;
> +
>   err_clk:
>         clk_disable_unprepare(i2c->clk);
>         return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>
>         i2c_del_adapter(&i2c->adap);
>
> +       clk_unprepare(i2c->clk);
> +
>         return 0;
>  }
>
> @@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
>         struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
>         int ret = 0;
>
> -       clk_prepare_enable(i2c->clk);
> +       ret = clk_enable(i2c->clk);
> +       if (ret)
> +               return ret;
>
>         ret = exynos5_hsi2c_clock_setup(i2c);
>         if (ret) {
> -               clk_disable_unprepare(i2c->clk);
> +               clk_disable(i2c->clk);
>                 return ret;
>         }
>
>         exynos5_i2c_init(i2c);
> -       clk_disable_unprepare(i2c->clk);
> +       clk_disable(i2c->clk);
>         i2c->suspended = 0;
>
>         return 0;
> --
> 2.5.5
>

Thanks for fixing this bug. I also tried the same thing on clk, but
missed the return on failed.

Reviewed-by: Anand Moon <linux.amoon@gmail.com>
Tested-by: Anand Moon <linux.amoon@gmail.com>

Best Regards
-Anand Moon

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-15 22:04 ` Javier Martinez Canillas
@ 2016-04-16 16:11   ` Krzysztof Kozlowski
  -1 siblings, 0 replies; 20+ messages in thread
From: Krzysztof Kozlowski @ 2016-04-16 16:11 UTC (permalink / raw)
  To: Javier Martinez Canillas
  Cc: linux-kernel, Anand Moon, Paul Osmialowski, Kukjin Kim,
	linux-samsung-soc, Wolfram Sang, Krzysztof Kozlowski, linux-i2c,
	linux-arm-kernel

On Fri, Apr 15, 2016 at 06:04:47PM -0400, Javier Martinez Canillas wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
> 
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
> 
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
> 
> The possible deadlock is reported by the kernel lockdep:
> 
>   Possible unsafe locking scenario:
> 
>         CPU0                    CPU1
>         ----                    ----
>    lock(sec_core:428:(regmap)->lock);
>                                 lock(prepare_lock);
>                                 lock(sec_core:428:(regmap)->lock);
>    lock(prepare_lock);
> 
>   *** DEADLOCK ***
> 
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
> 
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.

I wish this would be fixed by introducing more granular clock locks
(e.g. per controller) instead of implementing another workaround.
I think this driver shouldn't care about this deadlock... although I see
that this is the simplest solution for now.

> 
> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
> 
> ---
> 
>  drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>  		return -EIO;
>  	}
>  
> -	clk_prepare_enable(i2c->clk);
> +	ret = clk_enable(i2c->clk);
> +	if (ret)
> +		return ret;
>  
>  	for (i = 0; i < num; i++, msgs++) {
>  		stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>  	}
>  
>   out:
> -	clk_disable_unprepare(i2c->clk);
> +	clk_disable(i2c->clk);
>  	return ret;
>  }
>  
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>  
>  	platform_set_drvdata(pdev, i2c);
>  
> +	clk_disable(i2c->clk);
> +
> +	return 0;
> +
>   err_clk:
>  	clk_disable_unprepare(i2c->clk);
>  	return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>  
>  	i2c_del_adapter(&i2c->adap);
>  
> +	clk_unprepare(i2c->clk);
> +
>  	return 0;
>  }

Please unprepare the clock when suspending. There is no point of having
it prepared in that level.

Best regards,
Krzysztof

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-16 16:11   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 20+ messages in thread
From: Krzysztof Kozlowski @ 2016-04-16 16:11 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, Apr 15, 2016 at 06:04:47PM -0400, Javier Martinez Canillas wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
> 
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
> 
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
> 
> The possible deadlock is reported by the kernel lockdep:
> 
>   Possible unsafe locking scenario:
> 
>         CPU0                    CPU1
>         ----                    ----
>    lock(sec_core:428:(regmap)->lock);
>                                 lock(prepare_lock);
>                                 lock(sec_core:428:(regmap)->lock);
>    lock(prepare_lock);
> 
>   *** DEADLOCK ***
> 
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
> 
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.

I wish this would be fixed by introducing more granular clock locks
(e.g. per controller) instead of implementing another workaround.
I think this driver shouldn't care about this deadlock... although I see
that this is the simplest solution for now.

> 
> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
> 
> ---
> 
>  drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>  1 file changed, 15 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>  		return -EIO;
>  	}
>  
> -	clk_prepare_enable(i2c->clk);
> +	ret = clk_enable(i2c->clk);
> +	if (ret)
> +		return ret;
>  
>  	for (i = 0; i < num; i++, msgs++) {
>  		stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>  	}
>  
>   out:
> -	clk_disable_unprepare(i2c->clk);
> +	clk_disable(i2c->clk);
>  	return ret;
>  }
>  
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>  
>  	platform_set_drvdata(pdev, i2c);
>  
> +	clk_disable(i2c->clk);
> +
> +	return 0;
> +
>   err_clk:
>  	clk_disable_unprepare(i2c->clk);
>  	return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>  
>  	i2c_del_adapter(&i2c->adap);
>  
> +	clk_unprepare(i2c->clk);
> +
>  	return 0;
>  }

Please unprepare the clock when suspending. There is no point of having
it prepared in that level.

Best regards,
Krzysztof

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-16 12:15   ` Anand Moon
  (?)
@ 2016-04-17  0:40     ` Javier Martinez Canillas
  -1 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-17  0:40 UTC (permalink / raw)
  To: Anand Moon
  Cc: Linux Kernel, Paul Osmialowski, Kukjin Kim, linux-samsung-soc,
	Wolfram Sang, Krzysztof Kozlowski, linux-i2c, linux-arm-kernel

Hello Anand,

On 04/16/2016 08:15 AM, Anand Moon wrote:

[snip]

>>
> 
> Thanks for fixing this bug. I also tried the same thing on clk, but
> missed the return on failed.
> 
> Reviewed-by: Anand Moon <linux.amoon@gmail.com>
> Tested-by: Anand Moon <linux.amoon@gmail.com>
>

Thanks a lot for your testing and review.
 
> Best Regards
> -Anand Moon
> 

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-17  0:40     ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-17  0:40 UTC (permalink / raw)
  To: Anand Moon
  Cc: Linux Kernel, Paul Osmialowski, Kukjin Kim, linux-samsung-soc,
	Wolfram Sang, Krzysztof Kozlowski, linux-i2c, linux-arm-kernel

Hello Anand,

On 04/16/2016 08:15 AM, Anand Moon wrote:

[snip]

>>
> 
> Thanks for fixing this bug. I also tried the same thing on clk, but
> missed the return on failed.
> 
> Reviewed-by: Anand Moon <linux.amoon@gmail.com>
> Tested-by: Anand Moon <linux.amoon@gmail.com>
>

Thanks a lot for your testing and review.
 
> Best Regards
> -Anand Moon
> 

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-17  0:40     ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-17  0:40 UTC (permalink / raw)
  To: linux-arm-kernel

Hello Anand,

On 04/16/2016 08:15 AM, Anand Moon wrote:

[snip]

>>
> 
> Thanks for fixing this bug. I also tried the same thing on clk, but
> missed the return on failed.
> 
> Reviewed-by: Anand Moon <linux.amoon@gmail.com>
> Tested-by: Anand Moon <linux.amoon@gmail.com>
>

Thanks a lot for your testing and review.
 
> Best Regards
> -Anand Moon
> 

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-16 16:11   ` Krzysztof Kozlowski
@ 2016-04-17  0:58     ` Javier Martinez Canillas
  -1 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-17  0:58 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: linux-kernel, Anand Moon, Kukjin Kim, linux-samsung-soc,
	Wolfram Sang, linux-i2c, linux-arm-kernel

Hello Krzysztof,

Thanks a lot for your feedback.

On 04/16/2016 12:11 PM, Krzysztof Kozlowski wrote:
> On Fri, Apr 15, 2016 at 06:04:47PM -0400, Javier Martinez Canillas wrote:

[snip]

>>
>> Fix this by only preparing the clock on probe and {en,dis}able in the
>> rest of the driver.
>>
>> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
>> deadlock by keeping clock prepared") that fixes the same bug in other
>> driver for an I2C controller found in Samsung SoCs.
> 
> I wish this would be fixed by introducing more granular clock locks
> (e.g. per controller) instead of implementing another workaround.
> I think this driver shouldn't care about this deadlock... although I see
> that this is the simplest solution for now.
>

Agreed, but that would be a much intrusive core change affecting every single
platform so I didn't feel brave enough to attempt it :)

But regardless of the ABBA deadlock, there are reasons why the clk API is
split into an {,un}prepare and {en,dis}able functions (e.g: non-atomic vs
atomic) and it is a common pattern for drivers to prepare the clock(s) on
setup (i.e: probe), unprepare on driver removal, and just {en,dis}able the
clock(s) during runtime.

So I believe this patch is good on its own and at least makes the driver more
consistent with most I2C controller drivers that do the same w.r.t clocks. The
fact that the deadlock is fixed by this change is just a nice side effect IMHO.

[snip]

>> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>>  
>>  	i2c_del_adapter(&i2c->adap);
>>  
>> +	clk_unprepare(i2c->clk);
>> +
>>  	return 0;
>>  }
> 
> Please unprepare the clock when suspending. There is no point of having
> it prepared in that level.
>

Yes, I in fact thought the same when writing the patch but was reluctant to
change the prepared state in suspend because I don't have a way to test the
S2R path in this board due broken firmware that prevents the cores to enter
into deep sleep states (I believe is the same issue we faced with CPUidle).

But I'll do the change that you suggested since I agree with you.

> Best regards,
> Krzysztof
> 

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-17  0:58     ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-17  0:58 UTC (permalink / raw)
  To: linux-arm-kernel

Hello Krzysztof,

Thanks a lot for your feedback.

On 04/16/2016 12:11 PM, Krzysztof Kozlowski wrote:
> On Fri, Apr 15, 2016 at 06:04:47PM -0400, Javier Martinez Canillas wrote:

[snip]

>>
>> Fix this by only preparing the clock on probe and {en,dis}able in the
>> rest of the driver.
>>
>> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
>> deadlock by keeping clock prepared") that fixes the same bug in other
>> driver for an I2C controller found in Samsung SoCs.
> 
> I wish this would be fixed by introducing more granular clock locks
> (e.g. per controller) instead of implementing another workaround.
> I think this driver shouldn't care about this deadlock... although I see
> that this is the simplest solution for now.
>

Agreed, but that would be a much intrusive core change affecting every single
platform so I didn't feel brave enough to attempt it :)

But regardless of the ABBA deadlock, there are reasons why the clk API is
split into an {,un}prepare and {en,dis}able functions (e.g: non-atomic vs
atomic) and it is a common pattern for drivers to prepare the clock(s) on
setup (i.e: probe), unprepare on driver removal, and just {en,dis}able the
clock(s) during runtime.

So I believe this patch is good on its own and at least makes the driver more
consistent with most I2C controller drivers that do the same w.r.t clocks. The
fact that the deadlock is fixed by this change is just a nice side effect IMHO.

[snip]

>> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>>  
>>  	i2c_del_adapter(&i2c->adap);
>>  
>> +	clk_unprepare(i2c->clk);
>> +
>>  	return 0;
>>  }
> 
> Please unprepare the clock when suspending. There is no point of having
> it prepared in that level.
>

Yes, I in fact thought the same when writing the patch but was reluctant to
change the prepared state in suspend because I don't have a way to test the
S2R path in this board due broken firmware that prevents the cores to enter
into deep sleep states (I believe is the same issue we faced with CPUidle).

But I'll do the change that you suggested since I agree with you.

> Best regards,
> Krzysztof
> 

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-17  0:58     ` Javier Martinez Canillas
@ 2016-04-17 13:29       ` Krzysztof Kozlowski
  -1 siblings, 0 replies; 20+ messages in thread
From: Krzysztof Kozlowski @ 2016-04-17 13:29 UTC (permalink / raw)
  To: Javier Martinez Canillas
  Cc: Krzysztof Kozlowski, linux-kernel, Anand Moon, Kukjin Kim,
	linux-samsung-soc, Wolfram Sang, linux-i2c, linux-arm-kernel

On Sat, Apr 16, 2016 at 08:58:49PM -0400, Javier Martinez Canillas wrote:
> But regardless of the ABBA deadlock, there are reasons why the clk API is
> split into an {,un}prepare and {en,dis}able functions (e.g: non-atomic vs
> atomic) and it is a common pattern for drivers to prepare the clock(s) on
> setup (i.e: probe), unprepare on driver removal, and just {en,dis}able the
> clock(s) during runtime.

The reason to split prepare+enable is to differentiate the sleeping and
non-sleeping paths. Not to encourage doing one type in probe() and rest
in other places. The clock consumer does not care whether prepare or
enable is a noop. It shouldn't care, because the clock provider might
change and consumer remains the same. On the other hand the consumer is
interested whether it can block or not.

The argument that common pattern is to prepare in probe does not
convince me. If a clock enable is an noop and everything is done in
prepare/unprepare, then following such "common pattern" would be equal
to keeping the clock on always, even if it could be unprepared (gated). 


> So I believe this patch is good on its own 

As I said above, no. The pattern might be misleading. The prepare path
is there for sleeping purposes. Not for probes. Choosing such
pattern effectively makes clock handling worthless in certain clock
providers (these which enable is a noop).

> and at least makes the driver more
> consistent with most I2C controller drivers that do the same w.r.t clocks.

I cannot respond to that argument because I do not know most I2C
controllers.

Best regards,
Krzysztof

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-17 13:29       ` Krzysztof Kozlowski
  0 siblings, 0 replies; 20+ messages in thread
From: Krzysztof Kozlowski @ 2016-04-17 13:29 UTC (permalink / raw)
  To: linux-arm-kernel

On Sat, Apr 16, 2016 at 08:58:49PM -0400, Javier Martinez Canillas wrote:
> But regardless of the ABBA deadlock, there are reasons why the clk API is
> split into an {,un}prepare and {en,dis}able functions (e.g: non-atomic vs
> atomic) and it is a common pattern for drivers to prepare the clock(s) on
> setup (i.e: probe), unprepare on driver removal, and just {en,dis}able the
> clock(s) during runtime.

The reason to split prepare+enable is to differentiate the sleeping and
non-sleeping paths. Not to encourage doing one type in probe() and rest
in other places. The clock consumer does not care whether prepare or
enable is a noop. It shouldn't care, because the clock provider might
change and consumer remains the same. On the other hand the consumer is
interested whether it can block or not.

The argument that common pattern is to prepare in probe does not
convince me. If a clock enable is an noop and everything is done in
prepare/unprepare, then following such "common pattern" would be equal
to keeping the clock on always, even if it could be unprepared (gated). 


> So I believe this patch is good on its own 

As I said above, no. The pattern might be misleading. The prepare path
is there for sleeping purposes. Not for probes. Choosing such
pattern effectively makes clock handling worthless in certain clock
providers (these which enable is a noop).

> and at least makes the driver more
> consistent with most I2C controller drivers that do the same w.r.t clocks.

I cannot respond to that argument because I do not know most I2C
controllers.

Best regards,
Krzysztof

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-15 22:04 ` Javier Martinez Canillas
@ 2016-04-18  7:50   ` Marek Szyprowski
  -1 siblings, 0 replies; 20+ messages in thread
From: Marek Szyprowski @ 2016-04-18  7:50 UTC (permalink / raw)
  To: Javier Martinez Canillas, linux-kernel
  Cc: Anand Moon, Kukjin Kim, linux-samsung-soc, Wolfram Sang,
	Krzysztof Kozlowski, linux-i2c, linux-arm-kernel

Hello,

On 2016-04-16 00:04, Javier Martinez Canillas wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
>
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
>
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
>
> The possible deadlock is reported by the kernel lockdep:
>
>    Possible unsafe locking scenario:
>
>          CPU0                    CPU1
>          ----                    ----
>     lock(sec_core:428:(regmap)->lock);
>                                  lock(prepare_lock);
>                                  lock(sec_core:428:(regmap)->lock);
>     lock(prepare_lock);
>
>    *** DEADLOCK ***
>
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
>
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.

I'm sorry, but this is not the right approach imho. It is just a workaround
applied to specific driver, it also duplicates incorrect clock usage
pattern (there is really no point keeping clock prepared all the time).

IMHO this ABBA deadlock should be really fixed in clocks core (probably
by removing global prepare mutex and replacing it with per clock
controller mutexes). Without a proper patch this issue will hit us again
with other i2c controllers or other drivers as well.

> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>
> ---
>
>   drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>   1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>   		return -EIO;
>   	}
>   
> -	clk_prepare_enable(i2c->clk);
> +	ret = clk_enable(i2c->clk);
> +	if (ret)
> +		return ret;
>   
>   	for (i = 0; i < num; i++, msgs++) {
>   		stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>   	}
>   
>    out:
> -	clk_disable_unprepare(i2c->clk);
> +	clk_disable(i2c->clk);
>   	return ret;
>   }
>   
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>   
>   	platform_set_drvdata(pdev, i2c);
>   
> +	clk_disable(i2c->clk);
> +
> +	return 0;
> +
>    err_clk:
>   	clk_disable_unprepare(i2c->clk);
>   	return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>   
>   	i2c_del_adapter(&i2c->adap);
>   
> +	clk_unprepare(i2c->clk);
> +
>   	return 0;
>   }
>   
> @@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
>   	struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
>   	int ret = 0;
>   
> -	clk_prepare_enable(i2c->clk);
> +	ret = clk_enable(i2c->clk);
> +	if (ret)
> +		return ret;
>   
>   	ret = exynos5_hsi2c_clock_setup(i2c);
>   	if (ret) {
> -		clk_disable_unprepare(i2c->clk);
> +		clk_disable(i2c->clk);
>   		return ret;
>   	}
>   
>   	exynos5_i2c_init(i2c);
> -	clk_disable_unprepare(i2c->clk);
> +	clk_disable(i2c->clk);
>   	i2c->suspended = 0;
>   
>   	return 0;

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-18  7:50   ` Marek Szyprowski
  0 siblings, 0 replies; 20+ messages in thread
From: Marek Szyprowski @ 2016-04-18  7:50 UTC (permalink / raw)
  To: linux-arm-kernel

Hello,

On 2016-04-16 00:04, Javier Martinez Canillas wrote:
> The exynos5 I2C controller driver always prepares and enables a clock
> before using it and then disables unprepares it when the clock is not
> used anymore.
>
> But this can cause a possible ABBA deadlock in some scenarios since a
> driver that uses regmap to access its I2C registers, will first grab
> the regmap lock and then the I2C xfer function will grab the prepare
> lock when preparing the I2C clock. But since the clock driver also
> uses regmap for I2C accesses, preparing a clock will first grab the
> prepare lock and then the regmap lock when using the regmap API.
>
> An example of this happens on the Exynos5422 Odroid XU board where a
> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
> share the same I2C regmap.
>
> The possible deadlock is reported by the kernel lockdep:
>
>    Possible unsafe locking scenario:
>
>          CPU0                    CPU1
>          ----                    ----
>     lock(sec_core:428:(regmap)->lock);
>                                  lock(prepare_lock);
>                                  lock(sec_core:428:(regmap)->lock);
>     lock(prepare_lock);
>
>    *** DEADLOCK ***
>
> Fix this by only preparing the clock on probe and {en,dis}able in the
> rest of the driver.
>
> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
> deadlock by keeping clock prepared") that fixes the same bug in other
> driver for an I2C controller found in Samsung SoCs.

I'm sorry, but this is not the right approach imho. It is just a workaround
applied to specific driver, it also duplicates incorrect clock usage
pattern (there is really no point keeping clock prepared all the time).

IMHO this ABBA deadlock should be really fixed in clocks core (probably
by removing global prepare mutex and replacing it with per clock
controller mutexes). Without a proper patch this issue will hit us again
with other i2c controllers or other drivers as well.

> Reported-by: Anand Moon <linux.amoon@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>
> ---
>
>   drivers/i2c/busses/i2c-exynos5.c | 20 +++++++++++++++-----
>   1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
> index b29c7500461a..602633747149 100644
> --- a/drivers/i2c/busses/i2c-exynos5.c
> +++ b/drivers/i2c/busses/i2c-exynos5.c
> @@ -671,7 +671,9 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>   		return -EIO;
>   	}
>   
> -	clk_prepare_enable(i2c->clk);
> +	ret = clk_enable(i2c->clk);
> +	if (ret)
> +		return ret;
>   
>   	for (i = 0; i < num; i++, msgs++) {
>   		stop = (i == num - 1);
> @@ -695,7 +697,7 @@ static int exynos5_i2c_xfer(struct i2c_adapter *adap,
>   	}
>   
>    out:
> -	clk_disable_unprepare(i2c->clk);
> +	clk_disable(i2c->clk);
>   	return ret;
>   }
>   
> @@ -799,6 +801,10 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
>   
>   	platform_set_drvdata(pdev, i2c);
>   
> +	clk_disable(i2c->clk);
> +
> +	return 0;
> +
>    err_clk:
>   	clk_disable_unprepare(i2c->clk);
>   	return ret;
> @@ -810,6 +816,8 @@ static int exynos5_i2c_remove(struct platform_device *pdev)
>   
>   	i2c_del_adapter(&i2c->adap);
>   
> +	clk_unprepare(i2c->clk);
> +
>   	return 0;
>   }
>   
> @@ -830,16 +838,18 @@ static int exynos5_i2c_resume_noirq(struct device *dev)
>   	struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
>   	int ret = 0;
>   
> -	clk_prepare_enable(i2c->clk);
> +	ret = clk_enable(i2c->clk);
> +	if (ret)
> +		return ret;
>   
>   	ret = exynos5_hsi2c_clock_setup(i2c);
>   	if (ret) {
> -		clk_disable_unprepare(i2c->clk);
> +		clk_disable(i2c->clk);
>   		return ret;
>   	}
>   
>   	exynos5_i2c_init(i2c);
> -	clk_disable_unprepare(i2c->clk);
> +	clk_disable(i2c->clk);
>   	i2c->suspended = 0;
>   
>   	return 0;

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-18  7:50   ` Marek Szyprowski
@ 2016-04-18 13:29     ` Javier Martinez Canillas
  -1 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-18 13:29 UTC (permalink / raw)
  To: Marek Szyprowski, linux-kernel
  Cc: Anand Moon, Kukjin Kim, linux-samsung-soc, Wolfram Sang,
	Krzysztof Kozlowski, linux-i2c, linux-arm-kernel

Hello Marek,

On 04/18/2016 03:50 AM, Marek Szyprowski wrote:
> Hello,
> 
> On 2016-04-16 00:04, Javier Martinez Canillas wrote:
>> The exynos5 I2C controller driver always prepares and enables a clock
>> before using it and then disables unprepares it when the clock is not
>> used anymore.
>>
>> But this can cause a possible ABBA deadlock in some scenarios since a
>> driver that uses regmap to access its I2C registers, will first grab
>> the regmap lock and then the I2C xfer function will grab the prepare
>> lock when preparing the I2C clock. But since the clock driver also
>> uses regmap for I2C accesses, preparing a clock will first grab the
>> prepare lock and then the regmap lock when using the regmap API.
>>
>> An example of this happens on the Exynos5422 Odroid XU board where a
>> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
>> share the same I2C regmap.
>>
>> The possible deadlock is reported by the kernel lockdep:
>>
>>    Possible unsafe locking scenario:
>>
>>          CPU0                    CPU1
>>          ----                    ----
>>     lock(sec_core:428:(regmap)->lock);
>>                                  lock(prepare_lock);
>>                                  lock(sec_core:428:(regmap)->lock);
>>     lock(prepare_lock);
>>
>>    *** DEADLOCK ***
>>
>> Fix this by only preparing the clock on probe and {en,dis}able in the
>> rest of the driver.
>>
>> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
>> deadlock by keeping clock prepared") that fixes the same bug in other
>> driver for an I2C controller found in Samsung SoCs.
> 
> I'm sorry, but this is not the right approach imho. It is just a workaround
> applied to specific driver, it also duplicates incorrect clock usage
> pattern (there is really no point keeping clock prepared all the time).
> 
> IMHO this ABBA deadlock should be really fixed in clocks core (probably
> by removing global prepare mutex and replacing it with per clock
> controller mutexes). Without a proper patch this issue will hit us again
> with other i2c controllers or other drivers as well.
>

Agreed, Krzysztof mentioned the same before. I'll take a look to the global
mutex to see how I can make it more fine grained to prevent these deadlocks.
 
>> Reported-by: Anand Moon <linux.amoon@gmail.com>
>> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>>

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-18 13:29     ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-18 13:29 UTC (permalink / raw)
  To: linux-arm-kernel

Hello Marek,

On 04/18/2016 03:50 AM, Marek Szyprowski wrote:
> Hello,
> 
> On 2016-04-16 00:04, Javier Martinez Canillas wrote:
>> The exynos5 I2C controller driver always prepares and enables a clock
>> before using it and then disables unprepares it when the clock is not
>> used anymore.
>>
>> But this can cause a possible ABBA deadlock in some scenarios since a
>> driver that uses regmap to access its I2C registers, will first grab
>> the regmap lock and then the I2C xfer function will grab the prepare
>> lock when preparing the I2C clock. But since the clock driver also
>> uses regmap for I2C accesses, preparing a clock will first grab the
>> prepare lock and then the regmap lock when using the regmap API.
>>
>> An example of this happens on the Exynos5422 Odroid XU board where a
>> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
>> share the same I2C regmap.
>>
>> The possible deadlock is reported by the kernel lockdep:
>>
>>    Possible unsafe locking scenario:
>>
>>          CPU0                    CPU1
>>          ----                    ----
>>     lock(sec_core:428:(regmap)->lock);
>>                                  lock(prepare_lock);
>>                                  lock(sec_core:428:(regmap)->lock);
>>     lock(prepare_lock);
>>
>>    *** DEADLOCK ***
>>
>> Fix this by only preparing the clock on probe and {en,dis}able in the
>> rest of the driver.
>>
>> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
>> deadlock by keeping clock prepared") that fixes the same bug in other
>> driver for an I2C controller found in Samsung SoCs.
> 
> I'm sorry, but this is not the right approach imho. It is just a workaround
> applied to specific driver, it also duplicates incorrect clock usage
> pattern (there is really no point keeping clock prepared all the time).
> 
> IMHO this ABBA deadlock should be really fixed in clocks core (probably
> by removing global prepare mutex and replacing it with per clock
> controller mutexes). Without a proper patch this issue will hit us again
> with other i2c controllers or other drivers as well.
>

Agreed, Krzysztof mentioned the same before. I'll take a look to the global
mutex to see how I can make it more fine grained to prevent these deadlocks.
 
>> Reported-by: Anand Moon <linux.amoon@gmail.com>
>> Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com>
>>

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* Re: [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
  2016-04-18 13:29     ` Javier Martinez Canillas
@ 2016-04-18 19:18       ` Javier Martinez Canillas
  -1 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-18 19:18 UTC (permalink / raw)
  To: Marek Szyprowski, linux-kernel
  Cc: Anand Moon, Kukjin Kim, linux-samsung-soc, Wolfram Sang,
	Krzysztof Kozlowski, linux-i2c, linux-arm-kernel,
	Michael Turquette, Stephen Boyd, linux-clk

[adding clk maintainers/list to cc]

On 04/18/2016 09:29 AM, Javier Martinez Canillas wrote:
> Hello Marek,
> 
> On 04/18/2016 03:50 AM, Marek Szyprowski wrote:
>> Hello,
>>
>> On 2016-04-16 00:04, Javier Martinez Canillas wrote:
>>> The exynos5 I2C controller driver always prepares and enables a clock
>>> before using it and then disables unprepares it when the clock is not
>>> used anymore.
>>>
>>> But this can cause a possible ABBA deadlock in some scenarios since a
>>> driver that uses regmap to access its I2C registers, will first grab
>>> the regmap lock and then the I2C xfer function will grab the prepare
>>> lock when preparing the I2C clock. But since the clock driver also
>>> uses regmap for I2C accesses, preparing a clock will first grab the
>>> prepare lock and then the regmap lock when using the regmap API.
>>>
>>> An example of this happens on the Exynos5422 Odroid XU board where a
>>> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
>>> share the same I2C regmap.
>>>
>>> The possible deadlock is reported by the kernel lockdep:
>>>
>>>    Possible unsafe locking scenario:
>>>
>>>          CPU0                    CPU1
>>>          ----                    ----
>>>     lock(sec_core:428:(regmap)->lock);
>>>                                  lock(prepare_lock);
>>>                                  lock(sec_core:428:(regmap)->lock);
>>>     lock(prepare_lock);
>>>
>>>    *** DEADLOCK ***
>>>
>>> Fix this by only preparing the clock on probe and {en,dis}able in the
>>> rest of the driver.
>>>
>>> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
>>> deadlock by keeping clock prepared") that fixes the same bug in other
>>> driver for an I2C controller found in Samsung SoCs.
>>
>> I'm sorry, but this is not the right approach imho. It is just a workaround
>> applied to specific driver, it also duplicates incorrect clock usage
>> pattern (there is really no point keeping clock prepared all the time).
>>
>> IMHO this ABBA deadlock should be really fixed in clocks core (probably
>> by removing global prepare mutex and replacing it with per clock
>> controller mutexes). Without a proper patch this issue will hit us again
>> with other i2c controllers or other drivers as well.
>>
> 
> Agreed, Krzysztof mentioned the same before. I'll take a look to the global
> mutex to see how I can make it more fine grained to prevent these deadlocks.
>  

I've been looking at the Common Clk Framework and it seems that removing the
global prepare_lock mutex and making more fine grained (i.e: per controller)
is not trivial since clocks prepare operations are propagated to the parents
so the lock is not just used to have mutual exclusion of the clocks in the
same controller but is to prevent concurrent access to the whole hierarchy.

Also, I wonder why the clock usage pattern of leaving the clock prepared and
just {en,dis}able the clock at runtime is that bad since many drivers do it?

In fact, most I2C controllers drivers manage their clocks that way and that
is why I thought that this patch had merits on its own and the fact that the
deadlock was fixed was just a nice side effect of $SUBJECT.

Finally, Krzysztof was able to hit the deadlock so this is not just about a
theoretical deadlock but an important bug that should be fixed and $SUBJECT
is a possible fix until the CCF gets rids of the global mutex IMHO.

Stephen, Mike:

Some context has already been removed in the thread (including the patch
diff) so you can find the original patch here if you need more context:

https://lkml.org/lkml/2016/4/15/900

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

* [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared
@ 2016-04-18 19:18       ` Javier Martinez Canillas
  0 siblings, 0 replies; 20+ messages in thread
From: Javier Martinez Canillas @ 2016-04-18 19:18 UTC (permalink / raw)
  To: linux-arm-kernel

[adding clk maintainers/list to cc]

On 04/18/2016 09:29 AM, Javier Martinez Canillas wrote:
> Hello Marek,
> 
> On 04/18/2016 03:50 AM, Marek Szyprowski wrote:
>> Hello,
>>
>> On 2016-04-16 00:04, Javier Martinez Canillas wrote:
>>> The exynos5 I2C controller driver always prepares and enables a clock
>>> before using it and then disables unprepares it when the clock is not
>>> used anymore.
>>>
>>> But this can cause a possible ABBA deadlock in some scenarios since a
>>> driver that uses regmap to access its I2C registers, will first grab
>>> the regmap lock and then the I2C xfer function will grab the prepare
>>> lock when preparing the I2C clock. But since the clock driver also
>>> uses regmap for I2C accesses, preparing a clock will first grab the
>>> prepare lock and then the regmap lock when using the regmap API.
>>>
>>> An example of this happens on the Exynos5422 Odroid XU board where a
>>> s2mps11 PMIC is used and both the s2mps11 regulators and clk drivers
>>> share the same I2C regmap.
>>>
>>> The possible deadlock is reported by the kernel lockdep:
>>>
>>>    Possible unsafe locking scenario:
>>>
>>>          CPU0                    CPU1
>>>          ----                    ----
>>>     lock(sec_core:428:(regmap)->lock);
>>>                                  lock(prepare_lock);
>>>                                  lock(sec_core:428:(regmap)->lock);
>>>     lock(prepare_lock);
>>>
>>>    *** DEADLOCK ***
>>>
>>> Fix this by only preparing the clock on probe and {en,dis}able in the
>>> rest of the driver.
>>>
>>> This patch is similar to commit 34e81ad5f0b6 ("i2c: s3c2410: fix ABBA
>>> deadlock by keeping clock prepared") that fixes the same bug in other
>>> driver for an I2C controller found in Samsung SoCs.
>>
>> I'm sorry, but this is not the right approach imho. It is just a workaround
>> applied to specific driver, it also duplicates incorrect clock usage
>> pattern (there is really no point keeping clock prepared all the time).
>>
>> IMHO this ABBA deadlock should be really fixed in clocks core (probably
>> by removing global prepare mutex and replacing it with per clock
>> controller mutexes). Without a proper patch this issue will hit us again
>> with other i2c controllers or other drivers as well.
>>
> 
> Agreed, Krzysztof mentioned the same before. I'll take a look to the global
> mutex to see how I can make it more fine grained to prevent these deadlocks.
>  

I've been looking at the Common Clk Framework and it seems that removing the
global prepare_lock mutex and making more fine grained (i.e: per controller)
is not trivial since clocks prepare operations are propagated to the parents
so the lock is not just used to have mutual exclusion of the clocks in the
same controller but is to prevent concurrent access to the whole hierarchy.

Also, I wonder why the clock usage pattern of leaving the clock prepared and
just {en,dis}able the clock at runtime is that bad since many drivers do it?

In fact, most I2C controllers drivers manage their clocks that way and that
is why I thought that this patch had merits on its own and the fact that the
deadlock was fixed was just a nice side effect of $SUBJECT.

Finally, Krzysztof was able to hit the deadlock so this is not just about a
theoretical deadlock but an important bug that should be fixed and $SUBJECT
is a possible fix until the CCF gets rids of the global mutex IMHO.

Stephen, Mike:

Some context has already been removed in the thread (including the patch
diff) so you can find the original patch here if you need more context:

https://lkml.org/lkml/2016/4/15/900

Best regards,
-- 
Javier Martinez Canillas
Open Source Group
Samsung Research America

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

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

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-15 22:04 [PATCH] i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared Javier Martinez Canillas
2016-04-15 22:04 ` Javier Martinez Canillas
2016-04-16 12:15 ` Anand Moon
2016-04-16 12:15   ` Anand Moon
2016-04-16 12:15   ` Anand Moon
2016-04-17  0:40   ` Javier Martinez Canillas
2016-04-17  0:40     ` Javier Martinez Canillas
2016-04-17  0:40     ` Javier Martinez Canillas
2016-04-16 16:11 ` Krzysztof Kozlowski
2016-04-16 16:11   ` Krzysztof Kozlowski
2016-04-17  0:58   ` Javier Martinez Canillas
2016-04-17  0:58     ` Javier Martinez Canillas
2016-04-17 13:29     ` Krzysztof Kozlowski
2016-04-17 13:29       ` Krzysztof Kozlowski
2016-04-18  7:50 ` Marek Szyprowski
2016-04-18  7:50   ` Marek Szyprowski
2016-04-18 13:29   ` Javier Martinez Canillas
2016-04-18 13:29     ` Javier Martinez Canillas
2016-04-18 19:18     ` Javier Martinez Canillas
2016-04-18 19:18       ` Javier Martinez Canillas

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.