diff mbox

PM / OPP: Protect updates to list_dev with mutex

Message ID 5f8fac4ad84716ef68fc50ab0b78e11ad2837524.1446205160.git.viresh.kumar@linaro.org
State Accepted
Commit 87b4115db0239865bc812f61704bb1f43e2439b6
Headers show

Commit Message

Viresh Kumar Oct. 30, 2015, 11:56 a.m. UTC
dev_opp_list_lock is used everywhere to protect device and OPP lists,
but dev_pm_opp_set_sharing_cpus() is missed somehow. And instead we used
rcu-lock, which wouldn't help here as we are adding a new list_dev.

This also fixes a problem where we have called kzalloc(..., GFP_KERNEL)
from within rcu-lock, which isn't allowed as kzalloc can sleep when
called with GFP_KERNEL.

With CONFIG_DEBUG_ATOMIC_SLEEP set, we will see the caller vomiting.

Fixes: 8d4d4e98acd6 ("PM / OPP: Add helpers for initializing CPU OPPs")
Reported-by: Michael Turquette <mturquette@baylibre.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

---
Mike: Can you please verify if this fixes it for you..

 drivers/base/power/opp/core.c | 2 +-
 drivers/base/power/opp/cpu.c  | 8 ++++----
 drivers/base/power/opp/opp.h  | 3 +++
 3 files changed, 8 insertions(+), 5 deletions(-)

-- 
2.6.2.198.g614a2ac

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Viresh Kumar Oct. 31, 2015, 2:14 a.m. UTC | #1
On 30-10-15, 10:06, Stephen Boyd wrote:
> On 10/30, Viresh Kumar wrote:

> > dev_opp_list_lock is used everywhere to protect device and OPP lists,

> > but dev_pm_opp_set_sharing_cpus() is missed somehow. And instead we used

> > rcu-lock, which wouldn't help here as we are adding a new list_dev.

> > 

> > This also fixes a problem where we have called kzalloc(..., GFP_KERNEL)

> > from within rcu-lock, which isn't allowed as kzalloc can sleep when

> > called with GFP_KERNEL.

> 

> Care to share the splat here?


I don't know what is wrong (or right) with my exynos 5250 board, but I
didn't got any splat here even with the right config options (yes I
should have mentioned that earlier). I have seen this at other times
as well, while we were running after some cpufreq traces..

But, the case in hand is pretty straight forward and Mike T. did get a
splat as that's what he told me. We are calling a sleep-able function
from rcu_lock and that's obviously wrong.

> > diff --git a/drivers/base/power/opp/cpu.c b/drivers/base/power/opp/cpu.c

> > index 7654c5606307..91f15b2e25ee 100644

> > --- a/drivers/base/power/opp/cpu.c

> > +++ b/drivers/base/power/opp/cpu.c

> > @@ -124,12 +124,12 @@ int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)

> >  	struct device *dev;

> >  	int cpu, ret = 0;

> >  

> > -	rcu_read_lock();

> > +	mutex_lock(&dev_opp_list_lock);

> >  

> >  	dev_opp = _find_device_opp(cpu_dev);

> 

> So does _find_device_opp() need to be called with rcu_read_lock()

> held or not? The comment above the function makes it sound like

> we need RCU, but we don't do that here anymore.


That is more for the readers, as this function is going to return a
pointer to the device OPP, and to make sure it isn't freed behind
their back, they need to take the RCU lock.

There are other writer code paths as well, like add-opp, where we just
take the mutex as there can't be anything stronger than that :)

-- 
viresh
--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/base/power/opp/core.c b/drivers/base/power/opp/core.c
index d5c1149ff123..69f83cbe37b2 100644
--- a/drivers/base/power/opp/core.c
+++ b/drivers/base/power/opp/core.c
@@ -27,7 +27,7 @@ 
  */
 static LIST_HEAD(dev_opp_list);
 /* Lock to allow exclusive modification to the device and opp lists */
-static DEFINE_MUTEX(dev_opp_list_lock);
+DEFINE_MUTEX(dev_opp_list_lock);
 
 #define opp_rcu_lockdep_assert()					\
 do {									\
diff --git a/drivers/base/power/opp/cpu.c b/drivers/base/power/opp/cpu.c
index 7654c5606307..91f15b2e25ee 100644
--- a/drivers/base/power/opp/cpu.c
+++ b/drivers/base/power/opp/cpu.c
@@ -124,12 +124,12 @@  int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
 	struct device *dev;
 	int cpu, ret = 0;
 
-	rcu_read_lock();
+	mutex_lock(&dev_opp_list_lock);
 
 	dev_opp = _find_device_opp(cpu_dev);
 	if (IS_ERR(dev_opp)) {
 		ret = -EINVAL;
-		goto out_rcu_read_unlock;
+		goto unlock;
 	}
 
 	for_each_cpu(cpu, cpumask) {
@@ -150,8 +150,8 @@  int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask)
 			continue;
 		}
 	}
-out_rcu_read_unlock:
-	rcu_read_unlock();
+unlock:
+	mutex_unlock(&dev_opp_list_lock);
 
 	return 0;
 }
diff --git a/drivers/base/power/opp/opp.h b/drivers/base/power/opp/opp.h
index dcb38f78dae4..7366b2aa8997 100644
--- a/drivers/base/power/opp/opp.h
+++ b/drivers/base/power/opp/opp.h
@@ -21,6 +21,9 @@ 
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
 
+/* Lock to allow exclusive modification to the device and opp lists */
+extern struct mutex dev_opp_list_lock;
+
 /*
  * Internal data structure organization with the OPP layer library is as
  * follows: