Commit 6b07589d authored by Paul Osmialowski's avatar Paul Osmialowski Committed by Luis Henriques

i2c: s3c2410: fix ABBA deadlock by keeping clock prepared

commit 34e81ad5 upstream.

This patch solves deadlock between clock prepare mutex and regmap mutex reported
by Tomasz Figa in [1] by implementing solution from [2]: "always leave the clock
of the i2c controller in a prepared state".

[1] https://lkml.org/lkml/2014/7/2/171
[2] https://lkml.org/lkml/2014/7/2/207

On each i2c transfer handled by s3c24xx_i2c_xfer(), clk_prepare_enable() was
called, which calls clk_prepare() then clk_enable(). clk_prepare() takes
prepare_lock mutex before proceeding. Note that i2c transfer functions are
invoked from many places in kernel, typically with some other additional lock
held.

It may happen that function on CPU1 (e.g. regmap_update_bits()) has taken a
mutex (i.e. regmap lock mutex) then it attempts i2c communication in order to
proceed (so it needs to obtain clock related prepare_lock mutex during transfer
preparation stage due to clk_prepare() call). At the same time other task on
CPU0 wants to operate on clock (e.g. to (un)prepare clock for some other reason)
so it has taken prepare_lock mutex.

CPU0:                        CPU1:
clk_disable_unused()         regulator_disable()
  clk_prepare_lock()           map->lock(map->lock_arg)
  regmap_read()                s3c24xx_i2c_xfer()
    map->lock(map->lock_arg)     clk_prepare_lock()

Implemented solution from [2] leaves i2c clock prepared. Preparation is done in
s3c24xx_i2c_probe() function. Without this patch, it is immediately unprepared
by clk_disable_unprepare() call. I've replaced this call with clk_disable() and
I've added clk_unprepare() call in s3c24xx_i2c_remove().

The s3c24xx_i2c_xfer() function now uses clk_enable() instead of
clk_prepare_enable() (and clk_disable() instead of clk_unprepare_disable()).
Signed-off-by: default avatarPaul Osmialowski <p.osmialowsk@samsung.com>
Reviewed-by: default avatarKrzysztof Kozlowski <k.kozlowski@samsung.com>
Signed-off-by: default avatarWolfram Sang <wsa@the-dreams.de>
[ luis: backported to 3.16: adjusted context ]
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent eade3efe
...@@ -782,14 +782,16 @@ static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, ...@@ -782,14 +782,16 @@ static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
int ret; int ret;
pm_runtime_get_sync(&adap->dev); pm_runtime_get_sync(&adap->dev);
clk_prepare_enable(i2c->clk); ret = clk_enable(i2c->clk);
if (ret)
return ret;
for (retry = 0; retry < adap->retries; retry++) { for (retry = 0; retry < adap->retries; retry++) {
ret = s3c24xx_i2c_doxfer(i2c, msgs, num); ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
if (ret != -EAGAIN) { if (ret != -EAGAIN) {
clk_disable_unprepare(i2c->clk); clk_disable(i2c->clk);
pm_runtime_put(&adap->dev); pm_runtime_put(&adap->dev);
return ret; return ret;
} }
...@@ -799,7 +801,7 @@ static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, ...@@ -799,7 +801,7 @@ static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
udelay(100); udelay(100);
} }
clk_disable_unprepare(i2c->clk); clk_disable(i2c->clk);
pm_runtime_put(&adap->dev); pm_runtime_put(&adap->dev);
return -EREMOTEIO; return -EREMOTEIO;
} }
...@@ -1178,7 +1180,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) ...@@ -1178,7 +1180,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev)
clk_prepare_enable(i2c->clk); clk_prepare_enable(i2c->clk);
ret = s3c24xx_i2c_init(i2c); ret = s3c24xx_i2c_init(i2c);
clk_disable_unprepare(i2c->clk); clk_disable(i2c->clk);
if (ret != 0) { if (ret != 0) {
dev_err(&pdev->dev, "I2C controller init failed\n"); dev_err(&pdev->dev, "I2C controller init failed\n");
return ret; return ret;
...@@ -1191,6 +1193,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) ...@@ -1191,6 +1193,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev)
i2c->irq = ret = platform_get_irq(pdev, 0); i2c->irq = ret = platform_get_irq(pdev, 0);
if (ret <= 0) { if (ret <= 0) {
dev_err(&pdev->dev, "cannot find IRQ\n"); dev_err(&pdev->dev, "cannot find IRQ\n");
clk_unprepare(i2c->clk);
return ret; return ret;
} }
...@@ -1199,6 +1202,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) ...@@ -1199,6 +1202,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev)
if (ret != 0) { if (ret != 0) {
dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
clk_unprepare(i2c->clk);
return ret; return ret;
} }
} }
...@@ -1206,6 +1210,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) ...@@ -1206,6 +1210,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev)
ret = s3c24xx_i2c_register_cpufreq(i2c); ret = s3c24xx_i2c_register_cpufreq(i2c);
if (ret < 0) { if (ret < 0) {
dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
clk_unprepare(i2c->clk);
return ret; return ret;
} }
...@@ -1222,6 +1227,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) ...@@ -1222,6 +1227,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev)
if (ret < 0) { if (ret < 0) {
dev_err(&pdev->dev, "failed to add bus to i2c core\n"); dev_err(&pdev->dev, "failed to add bus to i2c core\n");
s3c24xx_i2c_deregister_cpufreq(i2c); s3c24xx_i2c_deregister_cpufreq(i2c);
clk_unprepare(i2c->clk);
return ret; return ret;
} }
...@@ -1243,6 +1249,8 @@ static int s3c24xx_i2c_remove(struct platform_device *pdev) ...@@ -1243,6 +1249,8 @@ static int s3c24xx_i2c_remove(struct platform_device *pdev)
{ {
struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
clk_unprepare(i2c->clk);
pm_runtime_disable(&i2c->adap.dev); pm_runtime_disable(&i2c->adap.dev);
pm_runtime_disable(&pdev->dev); pm_runtime_disable(&pdev->dev);
...@@ -1271,10 +1279,13 @@ static int s3c24xx_i2c_resume(struct device *dev) ...@@ -1271,10 +1279,13 @@ static int s3c24xx_i2c_resume(struct device *dev)
{ {
struct platform_device *pdev = to_platform_device(dev); struct platform_device *pdev = to_platform_device(dev);
struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
int ret;
clk_prepare_enable(i2c->clk); ret = clk_enable(i2c->clk);
if (ret)
return ret;
s3c24xx_i2c_init(i2c); s3c24xx_i2c_init(i2c);
clk_disable_unprepare(i2c->clk); clk_disable(i2c->clk);
i2c->suspended = 0; i2c->suspended = 0;
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment