From: Viresh Kumar <viresh.kumar@linaro.org>
To: niklas.cassel@linaro.org, Viresh Kumar <vireshk@kernel.org>,
Nishanth Menon <nm@ti.com>, Stephen Boyd <sboyd@kernel.org>
Cc: Viresh Kumar <viresh.kumar@linaro.org>,
Rafael Wysocki <rjw@rjwysocki.net>,
linux-pm@vger.kernel.org,
Vincent Guittot <vincent.guittot@linaro.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH 02/11] OPP: Protect dev_list with opp_table lock
Date: Wed, 12 Sep 2018 13:58:41 +0530 [thread overview]
Message-ID: <547f728dc5f4d9f27dae0a85517d5b2502b3b03d.1536736872.git.viresh.kumar@linaro.org> (raw)
In-Reply-To: <cover.1536736872.git.viresh.kumar@linaro.org>
The dev_list needs to be protected with a lock, else we may have
simultaneous access (addition/removal) to it and that would be racy.
Extend scope of the opp_table lock to protect dev_list as well.
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
drivers/opp/core.c | 21 +++++++++++++++++++--
drivers/opp/cpu.c | 2 ++
drivers/opp/opp.h | 2 +-
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/drivers/opp/core.c b/drivers/opp/core.c
index 31ff03dbeb83..9f8aa31265fe 100644
--- a/drivers/opp/core.c
+++ b/drivers/opp/core.c
@@ -48,9 +48,14 @@ static struct opp_device *_find_opp_dev(const struct device *dev,
static struct opp_table *_find_opp_table_unlocked(struct device *dev)
{
struct opp_table *opp_table;
+ bool found;
list_for_each_entry(opp_table, &opp_tables, node) {
- if (_find_opp_dev(dev, opp_table)) {
+ mutex_lock(&opp_table->lock);
+ found = !!_find_opp_dev(dev, opp_table);
+ mutex_unlock(&opp_table->lock);
+
+ if (found) {
_get_opp_table_kref(opp_table);
return opp_table;
@@ -766,6 +771,8 @@ struct opp_device *_add_opp_dev(const struct device *dev,
/* Initialize opp-dev */
opp_dev->dev = dev;
+
+ mutex_lock(&opp_table->lock);
list_add(&opp_dev->node, &opp_table->dev_list);
/* Create debugfs entries for the opp_table */
@@ -773,6 +780,7 @@ struct opp_device *_add_opp_dev(const struct device *dev,
if (ret)
dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
__func__, ret);
+ mutex_unlock(&opp_table->lock);
return opp_dev;
}
@@ -791,6 +799,7 @@ static struct opp_table *_allocate_opp_table(struct device *dev)
if (!opp_table)
return NULL;
+ mutex_init(&opp_table->lock);
INIT_LIST_HEAD(&opp_table->dev_list);
opp_dev = _add_opp_dev(dev, opp_table);
@@ -812,7 +821,6 @@ static struct opp_table *_allocate_opp_table(struct device *dev)
BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
INIT_LIST_HEAD(&opp_table->opp_list);
- mutex_init(&opp_table->lock);
kref_init(&opp_table->kref);
/* Secure the device table modification */
@@ -854,6 +862,10 @@ static void _opp_table_kref_release(struct kref *kref)
if (!IS_ERR(opp_table->clk))
clk_put(opp_table->clk);
+ /*
+ * No need to take opp_table->lock here as we are guaranteed that no
+ * references to the OPP table are taken at this point.
+ */
opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
node);
@@ -1716,6 +1728,9 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
{
struct dev_pm_opp *opp, *tmp;
+ /* Protect dev_list */
+ mutex_lock(&opp_table->lock);
+
/* Find if opp_table manages a single device */
if (list_is_singular(&opp_table->dev_list)) {
/* Free static OPPs */
@@ -1733,6 +1748,8 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
} else {
_remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
}
+
+ mutex_unlock(&opp_table->lock);
}
void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
diff --git a/drivers/opp/cpu.c b/drivers/opp/cpu.c
index 0c0910709435..2868a022a040 100644
--- a/drivers/opp/cpu.c
+++ b/drivers/opp/cpu.c
@@ -222,8 +222,10 @@ int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
cpumask_clear(cpumask);
if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {
+ mutex_lock(&opp_table->lock);
list_for_each_entry(opp_dev, &opp_table->dev_list, node)
cpumask_set_cpu(opp_dev->dev->id, cpumask);
+ mutex_unlock(&opp_table->lock);
} else {
cpumask_set_cpu(cpu_dev->id, cpumask);
}
diff --git a/drivers/opp/opp.h b/drivers/opp/opp.h
index 7c540fd063b2..e0866b1c1f1b 100644
--- a/drivers/opp/opp.h
+++ b/drivers/opp/opp.h
@@ -126,7 +126,7 @@ enum opp_table_access {
* @dev_list: list of devices that share these OPPs
* @opp_list: table of opps
* @kref: for reference count of the table.
- * @lock: mutex protecting the opp_list.
+ * @lock: mutex protecting the opp_list and dev_list.
* @np: struct device_node pointer for opp's DT node.
* @clock_latency_ns_max: Max clock latency in nanoseconds.
* @shared_opp: OPP is shared between multiple devices.
--
2.18.0.rc1.242.g61856ae69a2c
next prev parent reply other threads:[~2018-09-12 8:29 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-12 8:28 [PATCH 00/11] OPP: Don't create multiple OPP tables for devices sharing OPP table Viresh Kumar
2018-09-12 8:28 ` [PATCH 01/11] OPP: Free OPP table properly on performance state irregularities Viresh Kumar
2018-09-12 8:28 ` Viresh Kumar [this message]
2018-09-12 8:28 ` [PATCH 03/11] OPP: Pass index to _of_init_opp_table() Viresh Kumar
2018-09-12 8:28 ` [PATCH 04/11] OPP: Parse OPP table's DT properties from _of_init_opp_table() Viresh Kumar
2018-09-12 8:28 ` [PATCH 05/11] OPP: Don't take OPP table's kref for static OPPs Viresh Kumar
2018-09-12 8:28 ` [PATCH 06/11] OPP: Create separate kref for static OPPs list Viresh Kumar
2018-09-12 8:28 ` [PATCH 07/11] cpufreq: mvebu: Remove OPPs using dev_pm_opp_remove() Viresh Kumar
2018-09-19 15:20 ` Gregory CLEMENT
2018-09-19 21:40 ` Viresh Kumar
2018-09-12 8:28 ` [PATCH 08/11] OPP: Don't remove dynamic OPPs from _dev_pm_opp_remove_table() Viresh Kumar
2018-09-12 8:28 ` [PATCH 09/11] OPP: Use a single mechanism to free the OPP table Viresh Kumar
2018-09-12 8:28 ` [PATCH 10/11] OPP: Prevent creating multiple OPP tables for devices sharing OPP nodes Viresh Kumar
2018-09-12 8:28 ` [PATCH 11/11] OPP: Pass OPP table to _of_add_opp_table_v{1|2}() Viresh Kumar
2018-09-12 13:55 ` [PATCH 00/11] OPP: Don't create multiple OPP tables for devices sharing OPP table Niklas Cassel
2018-09-13 7:48 ` Viresh Kumar
2018-09-13 10:21 ` Niklas Cassel
2018-09-19 21:38 ` Viresh Kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=547f728dc5f4d9f27dae0a85517d5b2502b3b03d.1536736872.git.viresh.kumar@linaro.org \
--to=viresh.kumar@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=niklas.cassel@linaro.org \
--cc=nm@ti.com \
--cc=rjw@rjwysocki.net \
--cc=sboyd@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vireshk@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).