Skip to contentSkip to conversation

PM / OPP: Fixes and cleanups

PATCH Superseded Viresh Kumar 4 participants 37 messages Dec 6, 2016 – Dec 22, 2016 1 message not in the archive

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Viresh Kumar

author maintainer
#0 lore raw

cover PM / OPP: Fixes and cleanups

Hi,

This series fixes a memory leak (first patch) and does some cleanups to the OPP core.

It is based of pm/bleeding-edge branch. Though not compulsory, but it would be nice to get queued up for 4.10, of course once they are reviewed by other OPP guys.

This series is tested for few days by various build and boot bots:
- Kernel CI (Linaro)
- Fengguang Wu's bot (Intel)

--
viresh

Viresh Kumar (10):
PM / OPP: Fix memory leak while adding duplicate OPPs
PM / OPP: Remove useless TODO
PM / OPP: Rename _allocate_opp() to _opp_allocate()
PM / OPP: Error out on failing to add static OPPs for v1 bindings PM / OPP: Add light weight _opp_free() routine
PM / OPP: Rename and split _dev_pm_opp_remove_table()
PM / OPP: Don't allocate OPP table from _opp_allocate() PM / OPP: Rename dev_pm_opp_get_suspend_opp() and return OPP rate PM / OPP: Don't expose srcu_head to register notifiers
PM / OPP: Split out part of _add_opp_table() and _remove_opp_table()

 drivers/base/power/opp/core.c | 290 ++++++++++++++++++++++++++----------------
 drivers/base/power/opp/of.c   |  90 +++++++------
 drivers/base/power/opp/opp.h  |  11 +-
 drivers/cpufreq/cpufreq-dt.c  |   7 +-
 drivers/devfreq/devfreq.c     |  26 +---
 include/linux/pm_opp.h        |  20 ++-
 6 files changed, 249 insertions(+), 195 deletions(-)
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#1 lore raw

PATCH 1/10 PM / OPP: Fix memory leak while adding duplicate OPPs

There are two types of duplicate OPPs that get different behavior from the core:
A). An earlier OPP is marked 'available' and has same freq/voltages as
the new one.
B). An earlier OPP with same frequency, but is marked 'unavailable' OR
doesn't have same voltages as the new one.

The OPP core returns 0 for the first one, but -EEXIST for the second.

While the OPP core returns 0 for the first case, its callers don't free the newly allocated OPP structure which isn't used anymore. Fix that by returning -EBUSY instead of 0, but make the callers return 0 eventually.

As this isn't a critical fix, its not getting marked for stable kernel.

---
 drivers/base/power/opp/core.c | 14 ++++++++++++--
 drivers/base/power/opp/of.c   |  6 +++++-
 2 files changed, 17 insertions(+), 3 deletions(-)
drivers/base/power/opp/core.c +12 -2
index a0e6294baf1d..cc69f903fd34 100644
@@ -1080,6 +1080,12 @@ static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
 	return true;
 }
 
+/*
+ * Returns:
+ * 0: On success. And appropriate error message for Duplicate OPPs.
+ * -EBUSY: For OPP with same freq/volt and is available.
+ * -EEXIST: For OPP with same freq but different volt or is unavailable.
+ */
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 	     struct opp_table *opp_table)
 {
@@ -1112,7 +1118,7 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 
 		/* Should we compare voltages for all regulators here ? */
 		return opp->available &&
-		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? 0 : -EEXIST;
+		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
 	}
 
 	new_opp->opp_table = opp_table;
@@ -1186,8 +1192,12 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	new_opp->dynamic = dynamic;
 
 	ret = _opp_add(dev, new_opp, opp_table);
-	if (ret)
+	if (ret) {
+		/* Don't return error for duplicate OPPs */
+		if (ret == -EBUSY)
+			ret = 0;
 		goto free_opp;
+	}
 
 	mutex_unlock(&opp_table_lock);
 
drivers/base/power/opp/of.c +5 -1
index 3f7d2591b173..356c75edd656 100644
@@ -327,8 +327,12 @@ static int _opp_add_static_v2(struct device *dev, struct device_node *np)
 		goto free_opp;
 
 	ret = _opp_add(dev, new_opp, opp_table);
-	if (ret)
+	if (ret) {
+		/* Don't return error for duplicate OPPs */
+		if (ret == -EBUSY)
+			ret = 0;
 		goto free_opp;
+	}
 
 	/* OPP to select on device suspend */
 	if (of_property_read_bool(np, "opp-suspend")) {
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#4 lore raw

PATCH 2/10 PM / OPP: Remove useless TODO

In reply to Viresh Kumar

This TODO doesn't make sense anymore as we have all the information in a single OPP table. Remove it.

---
 drivers/base/power/opp/of.c | 2 --
 1 file changed, 2 deletions(-)
drivers/base/power/opp/of.c +0 -2
index 356c75edd656..996ca3b42f47 100644
@@ -246,8 +246,6 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
 static struct device_node *_of_get_opp_desc_node(struct device *dev)
 {
 	/*
-	 * TODO: Support for multiple OPP tables.
-	 *
 	 * There should be only ONE phandle present in "operating-points-v2"
 	 * property.
 	 */
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#7 lore raw

PATCH 3/10 PM / OPP: Rename _allocate_opp() to _opp_allocate()

In reply to Viresh Kumar

Make the naming consistent with how other routines are named.

---
 drivers/base/power/opp/core.c | 4 ++--
 drivers/base/power/opp/of.c   | 2 +-
 drivers/base/power/opp/opp.h  | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/base/power/opp/core.c +2 -2
index cc69f903fd34..4e070d58d537 100644
@@ -1027,7 +1027,7 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
 
-struct dev_pm_opp *_allocate_opp(struct device *dev,
+struct dev_pm_opp *_opp_allocate(struct device *dev,
 				 struct opp_table **opp_table)
 {
 	struct dev_pm_opp *opp;
@@ -1176,7 +1176,7 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	/* Hold our table modification lock here */
 	mutex_lock(&opp_table_lock);
 
-	new_opp = _allocate_opp(dev, &opp_table);
+	new_opp = _opp_allocate(dev, &opp_table);
 	if (!new_opp) {
 		ret = -ENOMEM;
 		goto unlock;
drivers/base/power/opp/of.c +1 -1
index 996ca3b42f47..f8512ca2bf41 100644
@@ -287,7 +287,7 @@ static int _opp_add_static_v2(struct device *dev, struct device_node *np)
 	/* Hold our table modification lock here */
 	mutex_lock(&opp_table_lock);
 
-	new_opp = _allocate_opp(dev, &opp_table);
+	new_opp = _opp_allocate(dev, &opp_table);
 	if (!new_opp) {
 		ret = -ENOMEM;
 		goto unlock;
drivers/base/power/opp/opp.h +1 -1
index af9f2b849a66..0f23a1059605 100644
@@ -193,7 +193,7 @@ struct opp_table {
 struct opp_table *_find_opp_table(struct device *dev);
 struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
 void _dev_pm_opp_remove_table(struct device *dev, bool remove_all);
-struct dev_pm_opp *_allocate_opp(struct device *dev, struct opp_table **opp_table);
+struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table);
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
 void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp, bool notify);
 int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic);
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#9 lore raw

PATCH 4/10 PM / OPP: Error out on failing to add static OPPs for v1 bindings

In reply to Viresh Kumar

The code adding static OPPs for V2 bindings already does so. Make the V1 bindings specific code behave the same.

---
 drivers/base/power/opp/of.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/base/power/opp/of.c +8 -4
index f8512ca2bf41..c8fe815774ff 100644
@@ -433,7 +433,7 @@ static int _of_add_opp_table_v1(struct device *dev)
 {
 	const struct property *prop;
 	const __be32 *val;
-	int nr;
+	int nr, ret;
 
 	prop = of_find_property(dev->of_node, "operating-points", NULL);
 	if (!prop)
@@ -456,9 +456,13 @@ static int _of_add_opp_table_v1(struct device *dev)
 		unsigned long freq = be32_to_cpup(val++) * 1000;
 		unsigned long volt = be32_to_cpup(val++);
 
-		if (_opp_add_v1(dev, freq, volt, false))
-			dev_warn(dev, "%s: Failed to add OPP %ld\n",
-				 __func__, freq);
+		ret = _opp_add_v1(dev, freq, volt, false);
+		if (ret) {
+			dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
+				__func__, freq, ret);
+			dev_pm_opp_of_remove_table(dev);
+			return ret;
+		}
 		nr -= 2;
 	}
 
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#18 lore raw

PATCH 5/10 PM / OPP: Add light weight _opp_free() routine

In reply to Viresh Kumar

The OPPs which are never successfully added using _opp_add() are not required to be freed with the _opp_remove() routine, as a simple kfree() is enough for them.

Introduce a new light weight routine _opp_free(), which will do that.

That also helps us removing the 'notify' parameter to _opp_remove(), which isn't required anymore.

Note that _opp_free() contains a call to _remove_opp_table() as the OPP table might have been added for this very OPP only. The _remove_opp_table() routine returns quickly if there are more OPPs in the table. This will be simplified in later patches though.

---
 drivers/base/power/opp/core.c | 20 +++++++++++---------
 drivers/base/power/opp/of.c   |  2 +-
 drivers/base/power/opp/opp.h  |  2 +-
 3 files changed, 13 insertions(+), 11 deletions(-)
drivers/base/power/opp/core.c +11 -9
index 4e070d58d537..ee5fbbd6da93 100644
@@ -942,6 +942,12 @@ static void _remove_opp_table(struct opp_table *opp_table)
 		  _kfree_device_rcu);
 }
 
+void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table)
+{
+	kfree(opp);
+	_remove_opp_table(opp_table);
+}
+
 /**
  * _kfree_opp_rcu() - Free OPP RCU handler
  * @head:	RCU head
@@ -957,7 +963,6 @@ static void _kfree_opp_rcu(struct rcu_head *head)
  * _opp_remove()  - Remove an OPP from a table definition
  * @opp_table:	points back to the opp_table struct this opp belongs to
  * @opp:	pointer to the OPP to remove
- * @notify:	OPP_EVENT_REMOVE notification should be sent or not
  *
  * This function removes an opp definition from the opp table.
  *
@@ -965,16 +970,13 @@ static void _kfree_opp_rcu(struct rcu_head *head)
  * It is assumed that the caller holds required mutex for an RCU updater
  * strategy.
  */
-void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,
-		 bool notify)
+static void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp)
 {
 	/*
 	 * Notify the changes in the availability of the operable
 	 * frequency/voltage list.
 	 */
-	if (notify)
-		srcu_notifier_call_chain(&opp_table->srcu_head,
-					 OPP_EVENT_REMOVE, opp);
+	srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp);
 	opp_debug_remove_one(opp);
 	list_del_rcu(&opp->node);
 	call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
@@ -1021,7 +1023,7 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 		goto unlock;
 	}
 
-	_opp_remove(opp_table, opp, true);
+	_opp_remove(opp_table, opp);
 unlock:
 	mutex_unlock(&opp_table_lock);
 }
@@ -1209,7 +1211,7 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	return 0;
 
 free_opp:
-	_opp_remove(opp_table, new_opp, false);
+	_opp_free(new_opp, opp_table);
 unlock:
 	mutex_unlock(&opp_table_lock);
 	return ret;
@@ -1921,7 +1923,7 @@ void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
 		/* Free static OPPs */
 		list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
 			if (remove_all || !opp->dynamic)
-				_opp_remove(opp_table, opp, true);
+				_opp_remove(opp_table, opp);
 		}
 	} else {
 		_remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
drivers/base/power/opp/of.c +1 -1
index c8fe815774ff..67c9eeded4e1 100644
@@ -362,7 +362,7 @@ static int _opp_add_static_v2(struct device *dev, struct device_node *np)
 	return 0;
 
 free_opp:
-	_opp_remove(opp_table, new_opp, false);
+	_opp_free(new_opp, opp_table);
 unlock:
 	mutex_unlock(&opp_table_lock);
 	return ret;
drivers/base/power/opp/opp.h +1 -1
index 0f23a1059605..334f7570df32 100644
@@ -194,8 +194,8 @@ struct opp_table *_find_opp_table(struct device *dev);
 struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
 void _dev_pm_opp_remove_table(struct device *dev, bool remove_all);
 struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table);
+void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table);
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
-void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp, bool notify);
 int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic);
 void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of);
 
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#20 lore raw

PATCH 6/10 PM / OPP: Rename and split _dev_pm_opp_remove_table()

In reply to Viresh Kumar

Later patches would want to remove OPP table (and its OPPs) using the opp_table pointer instead of 'dev'.

In order to prepare for that, rename _dev_pm_opp_remove_table() as _dev_pm_opp_find_and_remove_table() split out part of it.

---
 drivers/base/power/opp/core.c | 33 ++++++++++++++++++++-------------
 drivers/base/power/opp/of.c   |  2 +-
 drivers/base/power/opp/opp.h  |  2 +-
 3 files changed, 22 insertions(+), 15 deletions(-)
drivers/base/power/opp/core.c +20 -13
index ee5fbbd6da93..ef114cf9edcd 100644
@@ -1897,11 +1897,27 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
  * Free OPPs either created using static entries present in DT or even the
  * dynamically added entries based on remove_all param.
  */
-void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
+static void _dev_pm_opp_remove_table(struct opp_table *opp_table,
+				     struct device *dev, bool remove_all)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *opp, *tmp;
 
+	/* Find if opp_table manages a single device */
+	if (list_is_singular(&opp_table->dev_list)) {
+		/* Free static OPPs */
+		list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
+			if (remove_all || !opp->dynamic)
+				_opp_remove(opp_table, opp);
+		}
+	} else {
+		_remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
+	}
+}
+
+void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
+{
+	struct opp_table *opp_table;
+
 	/* Hold our table modification lock here */
 	mutex_lock(&opp_table_lock);
 
@@ -1918,16 +1934,7 @@ void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
 		goto unlock;
 	}
 
-	/* Find if opp_table manages a single device */
-	if (list_is_singular(&opp_table->dev_list)) {
-		/* Free static OPPs */
-		list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
-			if (remove_all || !opp->dynamic)
-				_opp_remove(opp_table, opp);
-		}
-	} else {
-		_remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
-	}
+	_dev_pm_opp_remove_table(opp_table, dev, remove_all);
 
 unlock:
 	mutex_unlock(&opp_table_lock);
@@ -1948,6 +1955,6 @@ void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
  */
 void dev_pm_opp_remove_table(struct device *dev)
 {
-	_dev_pm_opp_remove_table(dev, true);
+	_dev_pm_opp_find_and_remove_table(dev, true);
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
drivers/base/power/opp/of.c +1 -1
index 67c9eeded4e1..442fa46c4f5c 100644
@@ -238,7 +238,7 @@ static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,
  */
 void dev_pm_opp_of_remove_table(struct device *dev)
 {
-	_dev_pm_opp_remove_table(dev, false);
+	_dev_pm_opp_find_and_remove_table(dev, false);
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
 
drivers/base/power/opp/opp.h +1 -1
index 334f7570df32..c4b539a8533a 100644
@@ -192,7 +192,7 @@ struct opp_table {
 /* Routines internal to opp core */
 struct opp_table *_find_opp_table(struct device *dev);
 struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
-void _dev_pm_opp_remove_table(struct device *dev, bool remove_all);
+void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all);
 struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table);
 void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table);
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#22 lore raw

PATCH 7/10 PM / OPP: Don't allocate OPP table from _opp_allocate()

In reply to Viresh Kumar

There is no point in trying to find/allocate the table for every OPP that is added for a device. It would be far more efficient to allocate the table only once and pass its pointer to the routines that add the OPP entry.

Locking is removed from _opp_add_static_v2() and _opp_add_v1() now as the callers call them with that lock already held.

Call to _remove_opp_table() routine is also removed from _opp_free() now, as opp_table isn't allocated from within _opp_allocate(). This is handled by the routines which created the OPP table in the first place.

---
 drivers/base/power/opp/core.c | 68 +++++++++++++++++++-------------------
 drivers/base/power/opp/of.c   | 76 ++++++++++++++++++++++---------------------
 drivers/base/power/opp/opp.h  |  9 +++--
 3 files changed, 79 insertions(+), 74 deletions(-)
drivers/base/power/opp/core.c +34 -34
index ef114cf9edcd..6bcbb64a5582 100644
@@ -842,7 +842,7 @@ struct opp_device *_add_opp_dev(const struct device *dev,
  *
  * Return: valid opp_table pointer if success, else NULL.
  */
-static struct opp_table *_add_opp_table(struct device *dev)
+struct opp_table *_add_opp_table(struct device *dev)
 {
 	struct opp_table *opp_table;
 	struct opp_device *opp_dev;
@@ -942,10 +942,9 @@ static void _remove_opp_table(struct opp_table *opp_table)
 		  _kfree_device_rcu);
 }
 
-void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table)
+void _opp_free(struct dev_pm_opp *opp)
 {
 	kfree(opp);
-	_remove_opp_table(opp_table);
 }
 
 /**
@@ -1030,33 +1029,24 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
 
 struct dev_pm_opp *_opp_allocate(struct device *dev,
-				 struct opp_table **opp_table)
+				 struct opp_table *opp_table)
 {
 	struct dev_pm_opp *opp;
 	int count, supply_size;
-	struct opp_table *table;
-
-	table = _add_opp_table(dev);
-	if (!table)
-		return NULL;
 
 	/* Allocate space for at least one supply */
-	count = table->regulator_count ? table->regulator_count : 1;
+	count = opp_table->regulator_count ? opp_table->regulator_count : 1;
 	supply_size = sizeof(*opp->supplies) * count;
 
 	/* allocate new OPP node and supplies structures */
 	opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
-	if (!opp) {
-		kfree(table);
+	if (!opp)
 		return NULL;
-	}
 
 	/* Put the supplies at the end of the OPP structure as an empty array */
 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
 	INIT_LIST_HEAD(&opp->node);
 
-	*opp_table = table;
-
 	return opp;
 }
 
@@ -1142,6 +1132,7 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 
 /**
  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
+ * @opp_table:	OPP table
  * @dev:	device for which we do this operation
  * @freq:	Frequency in Hz for this OPP
  * @u_volt:	Voltage in uVolts for this OPP
@@ -1167,22 +1158,16 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  *		Duplicate OPPs (both freq and volt are same) and !opp->available
  * -ENOMEM	Memory allocation failure
  */
-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
-		bool dynamic)
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
+		unsigned long freq, long u_volt, bool dynamic)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *new_opp;
 	unsigned long tol;
 	int ret;
 
-	/* Hold our table modification lock here */
-	mutex_lock(&opp_table_lock);
-
-	new_opp = _opp_allocate(dev, &opp_table);
-	if (!new_opp) {
-		ret = -ENOMEM;
-		goto unlock;
-	}
+	new_opp = _opp_allocate(dev, opp_table);
+	if (!new_opp)
+		return -ENOMEM;
 
 	/* populate the opp table */
 	new_opp->rate = freq;
@@ -1201,8 +1186,6 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 		goto free_opp;
 	}
 
-	mutex_unlock(&opp_table_lock);
-
 	/*
 	 * Notify the changes in the availability of the operable
 	 * frequency/voltage list.
@@ -1211,9 +1194,8 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	return 0;
 
 free_opp:
-	_opp_free(new_opp, opp_table);
-unlock:
-	mutex_unlock(&opp_table_lock);
+	_opp_free(new_opp);
+
 	return ret;
 }
 
@@ -1731,7 +1713,25 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
  */
 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
 {
-	return _opp_add_v1(dev, freq, u_volt, true);
+	struct opp_table *opp_table;
+	int ret;
+
+	/* Hold our table modification lock here */
+	mutex_lock(&opp_table_lock);
+
+	opp_table = _add_opp_table(dev);
+	if (!opp_table) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+
+	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
+	if (ret)
+		_remove_opp_table(opp_table);
+
+unlock:
+	mutex_unlock(&opp_table_lock);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
 
@@ -1897,8 +1897,8 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
  * Free OPPs either created using static entries present in DT or even the
  * dynamically added entries based on remove_all param.
  */
-static void _dev_pm_opp_remove_table(struct opp_table *opp_table,
-				     struct device *dev, bool remove_all)
+void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
+			      bool remove_all)
 {
 	struct dev_pm_opp *opp, *tmp;
 
drivers/base/power/opp/of.c +39 -37

158 lines not on this page · raw message

drivers/base/power/opp/opp.h +6 -3
index c4b539a8533a..e32dc80ddc12 100644
@@ -191,13 +191,16 @@ struct opp_table {
 
 /* Routines internal to opp core */
 struct opp_table *_find_opp_table(struct device *dev);
+struct opp_table *_add_opp_table(struct device *dev);
 struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
+void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, bool remove_all);
 void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all);
-struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table);
-void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table);
+struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table *opp_table);
+void _opp_free(struct dev_pm_opp *opp);
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic);
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic);
 void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of);
+struct opp_table *_add_opp_table(struct device *dev);
 
 #ifdef CONFIG_OF
 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev);
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#28 lore raw

PATCH 8/10 PM / OPP: Rename dev_pm_opp_get_suspend_opp() and return OPP rate

In reply to Viresh Kumar

There is only one user of dev_pm_opp_get_suspend_opp() and that uses it to get the OPP rate for the suspend_opp.

Rename dev_pm_opp_get_suspend_opp() as dev_pm_opp_get_suspend_opp_freq() and return the rate directly from it.

---
 drivers/base/power/opp/core.c | 27 +++++++++++++--------------
 drivers/cpufreq/cpufreq-dt.c  |  7 +------
 include/linux/pm_opp.h        |  6 +++---
 3 files changed, 17 insertions(+), 23 deletions(-)
drivers/base/power/opp/core.c +13 -14
index 6bcbb64a5582..6fd098618683 100644
@@ -328,32 +328,31 @@ unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
 
 /**
- * dev_pm_opp_get_suspend_opp() - Get suspend opp
+ * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp
  * @dev:	device for which we do this operation
  *
- * Return: This function returns pointer to the suspend opp if it is
- * defined and available, otherwise it returns NULL.
- *
- * Locking: This function must be called under rcu_read_lock(). opp is a rcu
- * protected pointer. The reason for the same is that the opp pointer which is
- * returned will remain valid for use with opp_get_{voltage, freq} only while
- * under the locked area. The pointer returned must be used prior to unlocking
- * with rcu_read_unlock() to maintain the integrity of the pointer.
+ * Return: This function returns the frequency of the OPP marked as suspend_opp
+ * if one is available, else returns 0;
  */
-struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
+unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
 {
 	struct opp_table *opp_table;
+	unsigned long freq = 0;
 
-	opp_rcu_lockdep_assert();
+	rcu_read_lock();
 
 	opp_table = _find_opp_table(dev);
 	if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
 	    !opp_table->suspend_opp->available)
-		return NULL;
+		goto unlock;
 
-	return opp_table->suspend_opp;
+	freq = dev_pm_opp_get_freq(opp_table->suspend_opp) / 1000;
+
+unlock:
+	rcu_read_unlock();
+	return freq;
 }
-EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
+EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
 
 /**
  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
drivers/cpufreq/cpufreq-dt.c +1 -6
index 269013311e79..d0a53119d8a2 100644
@@ -148,7 +148,6 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 	struct private_data *priv;
 	struct device *cpu_dev;
 	struct clk *cpu_clk;
-	struct dev_pm_opp *suspend_opp;
 	unsigned int transition_latency;
 	bool fallback = false;
 	const char *name;
@@ -252,11 +251,7 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 	policy->driver_data = priv;
 	policy->clk = cpu_clk;
 
-	rcu_read_lock();
-	suspend_opp = dev_pm_opp_get_suspend_opp(cpu_dev);
-	if (suspend_opp)
-		policy->suspend_freq = dev_pm_opp_get_freq(suspend_opp) / 1000;
-	rcu_read_unlock();
+	policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev);
 
 	ret = cpufreq_table_validate_and_show(policy, freq_table);
 	if (ret) {
include/linux/pm_opp.h +3 -3
index 0edd88f93904..7483ff291a8e 100644
@@ -88,7 +88,7 @@ int dev_pm_opp_get_opp_count(struct device *dev);
 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev);
 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev);
 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev);
-struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev);
+unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev);
 
 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
 					      unsigned long freq,
@@ -159,9 +159,9 @@ static inline unsigned long dev_pm_opp_get_max_transition_latency(struct device
 	return 0;
 }
 
-static inline struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
+static inline unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
 {
-	return NULL;
+	return 0;
 }
 
 static inline struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#31 lore raw

PATCH 9/10 PM / OPP: Don't expose srcu_head to register notifiers

In reply to Viresh Kumar

Let the OPP core provide helpers to register notifiers for any device, instead of exposing srcu_head outside of the core.

---
 drivers/base/power/opp/core.c | 66 ++++++++++++++++++++++++++++++++-----------
 drivers/devfreq/devfreq.c     | 26 ++---------------
 include/linux/pm_opp.h        | 14 ++++++---
 3 files changed, 62 insertions(+), 44 deletions(-)
drivers/base/power/opp/core.c +50 -16
index 6fd098618683..f4b935892f5b 100644
@@ -1868,29 +1868,63 @@ int dev_pm_opp_disable(struct device *dev, unsigned long freq)
 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
 
 /**
- * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
- * @dev:	device pointer used to lookup OPP table.
+ * dev_pm_opp_register_notifier() - Register OPP notifier for the device
+ * @dev:	Device for which notifier needs to be registered
+ * @nb:		Notifier block to be registered
  *
- * Return: pointer to  notifier head if found, otherwise -ENODEV or
- * -EINVAL based on type of error casted as pointer. value must be checked
- *  with IS_ERR to determine valid pointer or error result.
+ * Return: 0 on success or a negative error value.
+ */
+int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
+{
+	struct opp_table *opp_table;
+	int ret;
+
+	rcu_read_lock();
+
+	opp_table = _find_opp_table(dev);
+	if (IS_ERR(opp_table)) {
+		ret = PTR_ERR(opp_table);
+		goto unlock;
+	}
+
+	ret = srcu_notifier_chain_register(&opp_table->srcu_head, nb);
+
+unlock:
+	rcu_read_unlock();
+
+	return ret;
+}
+EXPORT_SYMBOL(dev_pm_opp_register_notifier);
+
+/**
+ * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
+ * @dev:	Device for which notifier needs to be unregistered
+ * @nb:		Notifier block to be unregistered
  *
- * Locking: This function must be called under rcu_read_lock(). opp_table is a
- * RCU protected pointer. The reason for the same is that the opp pointer which
- * is returned will remain valid for use with opp_get_{voltage, freq} only while
- * under the locked area. The pointer returned must be used prior to unlocking
- * with rcu_read_unlock() to maintain the integrity of the pointer.
+ * Return: 0 on success or a negative error value.
  */
-struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
+int dev_pm_opp_unregister_notifier(struct device *dev,
+				   struct notifier_block *nb)
 {
-	struct opp_table *opp_table = _find_opp_table(dev);
+	struct opp_table *opp_table;
+	int ret;
 
-	if (IS_ERR(opp_table))
-		return ERR_CAST(opp_table); /* matching type */
+	rcu_read_lock();
+
+	opp_table = _find_opp_table(dev);
+	if (IS_ERR(opp_table)) {
+		ret = PTR_ERR(opp_table);
+		goto unlock;
+	}
+
+	ret = srcu_notifier_chain_unregister(&opp_table->srcu_head, nb);
 
-	return &opp_table->srcu_head;
+unlock:
+	rcu_read_unlock();
+
+	return ret;
 }
-EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
+EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
 
 /*
  * Free OPPs either created using static entries present in DT or even the
drivers/devfreq/devfreq.c +2 -24
index a324801d6a66..b0de42972b74 100644
@@ -1260,18 +1260,7 @@ EXPORT_SYMBOL(devfreq_recommended_opp);
  */
 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
 {
-	struct srcu_notifier_head *nh;
-	int ret = 0;
-
-	rcu_read_lock();
-	nh = dev_pm_opp_get_notifier(dev);
-	if (IS_ERR(nh))
-		ret = PTR_ERR(nh);
-	rcu_read_unlock();
-	if (!ret)
-		ret = srcu_notifier_chain_register(nh, &devfreq->nb);
-
-	return ret;
+	return dev_pm_opp_register_notifier(dev, &devfreq->nb);
 }
 EXPORT_SYMBOL(devfreq_register_opp_notifier);
 
@@ -1287,18 +1276,7 @@ EXPORT_SYMBOL(devfreq_register_opp_notifier);
  */
 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
 {
-	struct srcu_notifier_head *nh;
-	int ret = 0;
-
-	rcu_read_lock();
-	nh = dev_pm_opp_get_notifier(dev);
-	if (IS_ERR(nh))
-		ret = PTR_ERR(nh);
-	rcu_read_unlock();
-	if (!ret)
-		ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
-
-	return ret;
+	return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
 }
 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
 
include/linux/pm_opp.h +10 -4
index 7483ff291a8e..66a02deeb03f 100644
@@ -108,7 +108,9 @@ int dev_pm_opp_enable(struct device *dev, unsigned long freq);
 
 int dev_pm_opp_disable(struct device *dev, unsigned long freq);
 
-struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev);
+int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb);
+int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb);
+
 int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
 				unsigned int count);
 void dev_pm_opp_put_supported_hw(struct device *dev);
@@ -202,10 +204,14 @@ static inline int dev_pm_opp_disable(struct device *dev, unsigned long freq)
 	return 0;
 }
 
-static inline struct srcu_notifier_head *dev_pm_opp_get_notifier(
-							struct device *dev)
+static inline int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
 {
-	return ERR_PTR(-ENOTSUPP);
+	return -ENOTSUPP;
+}
+
+static inline int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb)
+{
+	return -ENOTSUPP;
 }
 
 static inline int dev_pm_opp_set_supported_hw(struct device *dev,
Signature · 1 line
2.7.1.410.g6faf27b
author maintainer
#33 lore raw

PATCH 10/10 PM / OPP: Split out part of _add_opp_table() and _remove_opp_table()

In reply to Viresh Kumar

Split out parts of _add_opp_table() and _remove_opp_table() into separate routines. This will be useful going forward (while doing further cleanups).

Should result in no functional changes.

---
 drivers/base/power/opp/core.c | 76 +++++++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 32 deletions(-)
drivers/base/power/opp/core.c +44 -32
index f4b935892f5b..9dd9d3287854 100644
@@ -832,26 +832,12 @@ struct opp_device *_add_opp_dev(const struct device *dev,
 	return opp_dev;
 }
 
-/**
- * _add_opp_table() - Find OPP table or allocate a new one
- * @dev:	device for which we do this operation
- *
- * It tries to find an existing table first, if it couldn't find one, it
- * allocates a new OPP table and returns that.
- *
- * Return: valid opp_table pointer if success, else NULL.
- */
-struct opp_table *_add_opp_table(struct device *dev)
+static struct opp_table *_allocate_opp_table(struct device *dev)
 {
 	struct opp_table *opp_table;
 	struct opp_device *opp_dev;
 	int ret;
 
-	/* Check for existing table for 'dev' first */
-	opp_table = _find_opp_table(dev);
-	if (!IS_ERR(opp_table))
-		return opp_table;
-
 	/*
 	 * Allocate a new OPP table. In the infrequent case where a new
 	 * device is needed to be added, we pay this penalty.
@@ -888,6 +874,27 @@ struct opp_table *_add_opp_table(struct device *dev)
 }
 
 /**
+ * _add_opp_table() - Find OPP table or allocate a new one
+ * @dev:	device for which we do this operation
+ *
+ * It tries to find an existing table first, if it couldn't find one, it
+ * allocates a new OPP table and returns that.
+ *
+ * Return: valid opp_table pointer if success, else NULL.
+ */
+struct opp_table *_add_opp_table(struct device *dev)
+{
+	struct opp_table *opp_table;
+
+	/* Check for existing table for 'dev' first */
+	opp_table = _find_opp_table(dev);
+	if (!IS_ERR(opp_table))
+		return opp_table;
+
+	return _allocate_opp_table(dev);
+}
+
+/**
  * _kfree_device_rcu() - Free opp_table RCU handler
  * @head:	RCU head
  */
@@ -899,6 +906,27 @@ static void _kfree_device_rcu(struct rcu_head *head)
 	kfree_rcu(opp_table, rcu_head);
 }
 
+static void _free_opp_table(struct opp_table *opp_table)
+{
+	struct opp_device *opp_dev;
+
+	/* Release clk */
+	if (!IS_ERR(opp_table->clk))
+		clk_put(opp_table->clk);
+
+	opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
+				   node);
+
+	_remove_opp_dev(opp_dev, opp_table);
+
+	/* dev_list must be empty now */
+	WARN_ON(!list_empty(&opp_table->dev_list));
+
+	list_del_rcu(&opp_table->node);
+	call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
+		  _kfree_device_rcu);
+}
+
 /**
  * _remove_opp_table() - Removes a OPP table
  * @opp_table: OPP table to be removed.
@@ -907,8 +935,6 @@ static void _kfree_device_rcu(struct rcu_head *head)
  */
 static void _remove_opp_table(struct opp_table *opp_table)
 {
-	struct opp_device *opp_dev;
-
 	if (!list_empty(&opp_table->opp_list))
 		return;
 
@@ -924,21 +950,7 @@ static void _remove_opp_table(struct opp_table *opp_table)
 	if (opp_table->set_opp)
 		return;
 
-	/* Release clk */
-	if (!IS_ERR(opp_table->clk))
-		clk_put(opp_table->clk);
-
-	opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
-				   node);
-
-	_remove_opp_dev(opp_dev, opp_table);
-
-	/* dev_list must be empty now */
-	WARN_ON(!list_empty(&opp_table->dev_list));
-
-	list_del_rcu(&opp_table->node);
-	call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
-		  _kfree_device_rcu);
+	_free_opp_table(opp_table);
 }
 
 void _opp_free(struct dev_pm_opp *opp)
Signature · 1 line
2.7.1.410.g6faf27b
maintainer Acked-by
#37 lore raw

RE: [PATCH 09/10] PM / OPP: Don't expose srcu_head to register notifiers

In reply to a message not in the archive
Quoted 10 lines

Let the OPP core provide helpers to register notifiers for any device, instead of exposing srcu_head outside of the core.

---

drivers/base/power/opp/core.c | 66 ++++++++++++++++++++++++++++++++-----------
drivers/devfreq/devfreq.c     | 26 ++---------------
include/linux/pm_opp.h        | 14 ++++++---
3 files changed, 62 insertions(+), 44 deletions(-)

Cheers,
MyungJoo

#23 lore raw

Re: [PATCH 07/10] PM / OPP: Don't allocate OPP table from _opp_allocate()

In reply to Viresh Kumar

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1032

Quoted 10 lines · Viresh Kumar
drivers/base/power/opp/core.c +1 -1
index ef114cf9edcd..6bcbb64a5582 100644
@@ -1030,33 +1029,24 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
 
 struct dev_pm_opp *_opp_allocate(struct device *dev,
-				 struct opp_table **opp_table)
+				 struct opp_table *opp_table)

Please call it table instead.

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1039

Quoted 6 lines
drivers/base/power/opp/core.c +0 -3
 {
 	struct dev_pm_opp *opp;
 	int count, supply_size;
-	struct opp_table *table;
-
-	table = _add_opp_table(dev);

Is this the only user of dev? Why do we keep passing dev to this function then?

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1038

Quoted 6 lines
drivers/base/power/opp/core.c +1 -3
-	if (!table)
-		return NULL;
 
 	/* Allocate space for at least one supply */
-	count = table->regulator_count ? table->regulator_count : 1;
+	count = opp_table->regulator_count ? opp_table->regulator_count : 1;

We keep it table so that this doesn't change.

Quoted 16 lines
drivers/base/power/opp/core.c +1 -2
 	supply_size = sizeof(*opp->supplies) * count;
 
@@ -1142,6 +1132,7 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 
 /**
  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
+ * @opp_table:	OPP table
  * @dev:	device for which we do this operation
  * @freq:	Frequency in Hz for this OPP
  * @u_volt:	Voltage in uVolts for this OPP
@@ -1167,22 +1158,16 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  *		Duplicate OPPs (both freq and volt are same) and !opp->available
  * -ENOMEM	Memory allocation failure
  */
-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
-		bool dynamic)

drivers/base/power/opp/core.c int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, · line 1179

Quoted 10 lines
drivers/base/power/opp/core.c +2 -3
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
+		unsigned long freq, long u_volt, bool dynamic)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *new_opp;
 	unsigned long tol;
 	int ret;
 
-	/* Hold our table modification lock here */
-	mutex_lock(&opp_table_lock);

Can we have a mutex locked assertion here? Or a note in the comments that we assume the opp table lock is held?

drivers/base/power/opp/core.c int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, · line 1173

Quoted 12 lines
drivers/base/power/opp/core.c +3 -6
-
-	new_opp = _opp_allocate(dev, &opp_table);
-	if (!new_opp) {
-		ret = -ENOMEM;
-		goto unlock;
-	}
+	new_opp = _opp_allocate(dev, opp_table);
+	if (!new_opp)
+		return -ENOMEM;
 
 	/* populate the opp table */
 	new_opp->rate = freq;

Also, now we call the srcu notifier chain with the opp_table_lock held? That seems not so good. Do we need to drop it and reaquire the lock across the table lock? Or perhaps we should rethink widening the lock this much across the notifier.

Quoted 13 lines
drivers/base/power/opp/core.c +8 -1
@@ -1731,7 +1713,25 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
  */
 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
 {
-	return _opp_add_v1(dev, freq, u_volt, true);
+	struct opp_table *opp_table;
+	int ret;
+
+	/* Hold our table modification lock here */
+	mutex_lock(&opp_table_lock);
+
+	opp_table = _add_opp_table(dev);
+	if (!opp_table) {

drivers/base/power/opp/core.c EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper); · line 1733

Quoted 10 lines
drivers/base/power/opp/core.c +10 -0
+		ret = -ENOMEM;
+		goto unlock;
+	}
+
+	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
+	if (ret)
+		_remove_opp_table(opp_table);
+
+unlock:
+	mutex_unlock(&opp_table_lock);

I'd call it table here too, given that we don't have other tables inside OPP anyway. But no problem either way.

Quoted 21 lines
drivers/base/power/opp/core.c +1 -0
+	return ret;
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
 
drivers/base/power/opp/of.c +2 -1
index 442fa46c4f5c..37dd79378f39 100644
@@ -431,9 +421,10 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
 /* Initializes OPP tables based on old-deprecated bindings */
 static int _of_add_opp_table_v1(struct device *dev)
 {
+	struct opp_table *opp_table;
 	const struct property *prop;
 	const __be32 *val;
-	int nr, ret;
+	int nr, ret = 0;
 
 	prop = of_find_property(dev->of_node, "operating-points", NULL);
 	if (!prop)

drivers/base/power/opp/of.c static int _of_add_opp_table_v1(struct device *dev) · line 445

Quoted 4 lines
drivers/base/power/opp/of.c +1 -0
@@ -451,22 +442,33 @@ static int _of_add_opp_table_v1(struct device *dev)
 		return -EINVAL;
 	}
 
+	/* Hold our table modification lock here */

This comment is fairly worthless.

Quoted 33 lines
drivers/base/power/opp/of.c +14 -4
+	mutex_lock(&opp_table_lock);
+
+	opp_table = _add_opp_table(dev);
+	if (!opp_table) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+
 	val = prop->value;
 	while (nr) {
 		unsigned long freq = be32_to_cpup(val++) * 1000;
 		unsigned long volt = be32_to_cpup(val++);
 
-		ret = _opp_add_v1(dev, freq, volt, false);
+		ret = _opp_add_v1(opp_table, dev, freq, volt, false);
 		if (ret) {
 			dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",
 				__func__, freq, ret);
-			dev_pm_opp_of_remove_table(dev);
-			return ret;
+			_dev_pm_opp_remove_table(opp_table, dev, false);
+			break;
 		}
 		nr -= 2;
 	}
 
-	return 0;
+unlock:
+	mutex_unlock(&opp_table_lock);
+	return ret;
 }
 
 /**
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Reviewed-by on v1 5/10
#19 lore raw

Re: [PATCH 05/10] PM / OPP: Add light weight _opp_free() routine

In reply to Viresh Kumar
Quoted 9 lines · Viresh Kumar

The OPPs which are never successfully added using _opp_add() are not required to be freed with the _opp_remove() routine, as a simple kfree() is enough for them.

Introduce a new light weight routine _opp_free(), which will do that.

That also helps us removing the 'notify' parameter to _opp_remove(), which isn't required anymore.

Quoted 7 lines

Note that _opp_free() contains a call to _remove_opp_table() as the OPP table might have been added for this very OPP only. The _remove_opp_table() routine returns quickly if there are more OPPs in the table. This will be simplified in later patches though.

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Reviewed-by on v1 4/10
#10 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

In reply to Viresh Kumar
Quoted 5 lines · Viresh Kumar

The code adding static OPPs for V2 bindings already does so. Make the V1 bindings specific code behave the same.

---

It looks like that goes back to the original DT OPP binding support written by Shawn Guo. Any idea why we kept trying to add more OPPs if it failed to add one? Best effort or something?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH 01/10] PM / OPP: Fix memory leak while adding duplicate OPPs

In reply to Viresh Kumar
Quoted 9 lines · Viresh Kumar

There are two types of duplicate OPPs that get different behavior from the core:
A). An earlier OPP is marked 'available' and has same freq/voltages as
the new one.
B). An earlier OPP with same frequency, but is marked 'unavailable' OR
doesn't have same voltages as the new one.

The OPP core returns 0 for the first one, but -EEXIST for the second.

Quoted 7 lines

While the OPP core returns 0 for the first case, its callers don't free the newly allocated OPP structure which isn't used anymore. Fix that by returning -EBUSY instead of 0, but make the callers return 0 eventually.

As this isn't a critical fix, its not getting marked for stable kernel.

Makes sense.

Quoted 10 lines
---
 drivers/base/power/opp/core.c | 14 ++++++++++++--
 drivers/base/power/opp/of.c   |  6 +++++-
 2 files changed, 17 insertions(+), 3 deletions(-)
drivers/base/power/opp/core.c +0 -0
index a0e6294baf1d..cc69f903fd34 100644

drivers/base/power/opp/core.c static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, · line 1085

Quoted 6 lines
drivers/base/power/opp/core.c +3 -0
@@ -1080,6 +1080,12 @@ static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
 	return true;
 }
 
+/*
+ * Returns:
+ * 0: On success. And appropriate error message for Duplicate OPPs.

lowercase duplicate please

Quoted 16 lines
drivers/base/power/opp/core.c +4 -1
+ * -EBUSY: For OPP with same freq/volt and is available.
+ * -EEXIST: For OPP with same freq but different volt or is unavailable.
+ */
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 	     struct opp_table *opp_table)
 {
@@ -1112,7 +1118,7 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 
 		/* Should we compare voltages for all regulators here ? */
 		return opp->available &&
-		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? 0 : -EEXIST;
+		       new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
 	}
 
 	new_opp->opp_table = opp_table;

drivers/base/power/opp/core.c int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, · line 1196

Quoted 6 lines
drivers/base/power/opp/core.c +2 -1
@@ -1186,8 +1192,12 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	new_opp->dynamic = dynamic;
 
 	ret = _opp_add(dev, new_opp, opp_table);
-	if (ret)
+	if (ret) {
+		/* Don't return error for duplicate OPPs */

Yes, but why?

Quoted 7 lines
drivers/base/power/opp/core.c +3 -0
+		if (ret == -EBUSY)
+			ret = 0;
 		goto free_opp;
+	}
 
 	mutex_unlock(&opp_table_lock);
 
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Reviewed-by on v1 2/10
#5 lore raw

Re: [PATCH 02/10] PM / OPP: Remove useless TODO

In reply to Viresh Kumar
Quoted 5 lines · Viresh Kumar

This TODO doesn't make sense anymore as we have all the information in a single OPP table. Remove it.

---

Was this for multiple phandles style of binding that we never merged?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#21 lore raw

Re: [PATCH 06/10] PM / OPP: Rename and split _dev_pm_opp_remove_table()

In reply to Viresh Kumar
Quoted 5 lines · Viresh Kumar
drivers/base/power/opp/core.c +0 -0
index ee5fbbd6da93..ef114cf9edcd 100644

drivers/base/power/opp/core.c EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier); · line 1903

Quoted 9 lines
drivers/base/power/opp/core.c +2 -2
@@ -1897,11 +1897,27 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
  * Free OPPs either created using static entries present in DT or even the
  * dynamically added entries based on remove_all param.
  */
-void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
+static void _dev_pm_opp_remove_table(struct opp_table *opp_table,
+				     struct device *dev, bool remove_all)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *opp, *tmp;

Please add a comment or lockdep check to make sure we're holding the table lock.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#29 lore raw

Re: [PATCH 08/10] PM / OPP: Rename dev_pm_opp_get_suspend_opp() and return OPP rate

In reply to Viresh Kumar
Quoted 18 lines · Viresh Kumar

There is only one user of dev_pm_opp_get_suspend_opp() and that uses it to get the OPP rate for the suspend_opp.

Rename dev_pm_opp_get_suspend_opp() as dev_pm_opp_get_suspend_opp_freq() and return the rate directly from it.

---
 drivers/base/power/opp/core.c | 27 +++++++++++++--------------
 drivers/cpufreq/cpufreq-dt.c  |  7 +------
 include/linux/pm_opp.h        |  6 +++---
 3 files changed, 17 insertions(+), 23 deletions(-)
drivers/base/power/opp/core.c +0 -0
index 6bcbb64a5582..6fd098618683 100644

drivers/base/power/opp/core.c unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) · line 331

Quoted 5 lines
drivers/base/power/opp/core.c +1 -1
@@ -328,32 +328,31 @@ unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
 
 /**
- * dev_pm_opp_get_suspend_opp() - Get suspend opp
+ * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp

Please indicate units of frequency.

Quoted 20 lines
drivers/base/power/opp/core.c +4 -10
  * @dev:	device for which we do this operation
  *
- * Return: This function returns pointer to the suspend opp if it is
- * defined and available, otherwise it returns NULL.
- *
- * Locking: This function must be called under rcu_read_lock(). opp is a rcu
- * protected pointer. The reason for the same is that the opp pointer which is
- * returned will remain valid for use with opp_get_{voltage, freq} only while
- * under the locked area. The pointer returned must be used prior to unlocking
- * with rcu_read_unlock() to maintain the integrity of the pointer.
+ * Return: This function returns the frequency of the OPP marked as suspend_opp
+ * if one is available, else returns 0;
  */
-struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
+unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
 {
 	struct opp_table *opp_table;
+	unsigned long freq = 0;
 
-	opp_rcu_lockdep_assert();

drivers/base/power/opp/core.c unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) · line 349

Quoted 10 lines
drivers/base/power/opp/core.c +3 -2
+	rcu_read_lock();
 
 	opp_table = _find_opp_table(dev);
 	if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
 	    !opp_table->suspend_opp->available)
-		return NULL;
+		goto unlock;
 
-	return opp_table->suspend_opp;
+	freq = dev_pm_opp_get_freq(opp_table->suspend_opp) / 1000;

Why do we include the / 1000? Perhaps we should just return freq and have the caller divide.

Quoted 11 lines
drivers/base/power/opp/core.c +5 -1
+
+unlock:
+	rcu_read_unlock();
+	return freq;
 }
-EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
+EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
 
 /**
  * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table

drivers/base/power/opp/core.c static inline unsigned long dev_pm_opp_get_max_transition_latency(struct device · line 164

Quoted 8 lines
drivers/base/power/opp/core.c +2 -2
@@ -159,9 +159,9 @@ static inline unsigned long dev_pm_opp_get_max_transition_latency(struct device
 	return 0;
 }
 
-static inline struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
+static inline unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
 {
-	return NULL;
+	return 0;

Dividing 0 by anything is just 0 anyway.

Quoted 1 line
drivers/base/power/opp/core.c +0 -0
 }
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Reviewed-by on v1 10/10
#34 lore raw

Re: [PATCH 10/10] PM / OPP: Split out part of _add_opp_table() and _remove_opp_table()

In reply to Viresh Kumar
Quoted 8 lines · Viresh Kumar

Split out parts of _add_opp_table() and _remove_opp_table() into separate routines. This will be useful going forward (while doing further cleanups).

Should result in no functional changes.

---

But perhaps this should wait until we have patches that actually use them.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Reviewed-by on v1 9/10
#32 lore raw

Re: [PATCH 09/10] PM / OPP: Don't expose srcu_head to register notifiers

In reply to Viresh Kumar
Quoted 5 lines · Viresh Kumar

Let the OPP core provide helpers to register notifiers for any device, instead of exposing srcu_head outside of the core.

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
author maintainer
#11 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

In reply to Stephen Boyd
Quoted 4 lines · Stephen Boyd
Quoted 3 lines · Viresh Kumar

The code adding static OPPs for V2 bindings already does so. Make the V1 bindings specific code behave the same.

Quoted 8 lines
Quoted 2 lines

---

It looks like that goes back to the original DT OPP binding support written by Shawn Guo. Any idea why we kept trying to add more OPPs if it failed to add one? Best effort or something?

Not sure, maybe for handling duplicate OPPs.

Signature · 1 line
viresh
author maintainer
#6 lore raw

Re: [PATCH 02/10] PM / OPP: Remove useless TODO

In reply to Stephen Boyd
Quoted 11 lines · Stephen Boyd
Quoted 5 lines · Viresh Kumar

This TODO doesn't make sense anymore as we have all the information in a single OPP table. Remove it.

---

Was this for multiple phandles style of binding that we never merged?

Yes, for something like slow and fast OPP tables. We solved that problem in a different way.

Signature · 1 line
viresh
author maintainer
#3 lore raw

Re: [PATCH 01/10] PM / OPP: Fix memory leak while adding duplicate OPPs

In reply to Stephen Boyd
Quoted 9 lines · Stephen Boyd

drivers/base/power/opp/core.c int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, · line 1196

Quoted 7 lines
drivers/base/power/opp/core.c +2 -1
@@ -1186,8 +1192,12 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	new_opp->dynamic = dynamic;
 
 	ret = _opp_add(dev, new_opp, opp_table);
-	if (ret)
+	if (ret) {
+		/* Don't return error for duplicate OPPs */

Yes, but why?

Will update the comment over _opp_add() to describe that.

Signature · 1 line
viresh
author maintainer
#24 lore raw

Re: [PATCH 07/10] PM / OPP: Don't allocate OPP table from _opp_allocate()

In reply to Stephen Boyd
Quoted 8 lines · Stephen Boyd
Quoted 7 lines · Viresh Kumar
drivers/base/power/opp/core.c +0 -0
index ef114cf9edcd..6bcbb64a5582 100644
@@ -1030,33 +1029,24 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
 
Quoted 5 lines

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1032

Quoted 3 lines
drivers/base/power/opp/core.c +1 -1
 struct dev_pm_opp *_opp_allocate(struct device *dev,
-				 struct opp_table **opp_table)
+				 struct opp_table *opp_table)

Please call it table instead.

Sure.

Quoted 9 lines

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1039

Quoted 6 lines
drivers/base/power/opp/core.c +0 -3
 {
 	struct dev_pm_opp *opp;
 	int count, supply_size;
-	struct opp_table *table;
-
-	table = _add_opp_table(dev);

Is this the only user of dev? Why do we keep passing dev to this function then?

Because we are still working with struct list_dev, which needs to save a pointer to dev. We may simplify that with later series though, not sure yet.

Quoted 8 lines
Quoted 8 lines
drivers/base/power/opp/core.c +2 -1
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
+		unsigned long freq, long u_volt, bool dynamic)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *new_opp;
 	unsigned long tol;
 	int ret;
 
Quoted 5 lines

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1047

Quoted 2 lines
drivers/base/power/opp/core.c +0 -2
-	/* Hold our table modification lock here */
-	mutex_lock(&opp_table_lock);

Can we have a mutex locked assertion here? Or a note in the comments that we assume the opp table lock is held?

Done.

Quoted 10 lines
Quoted 10 lines
drivers/base/power/opp/core.c +3 -6
-
-	new_opp = _opp_allocate(dev, &opp_table);
-	if (!new_opp) {
-		ret = -ENOMEM;
-		goto unlock;
-	}
+	new_opp = _opp_allocate(dev, opp_table);
+	if (!new_opp)
+		return -ENOMEM;
 
Quoted 7 lines

drivers/base/power/opp/core.c int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, · line 1173

Quoted 2 lines
drivers/base/power/opp/core.c +0 -0
 	/* populate the opp table */
 	new_opp->rate = freq;

Also, now we call the srcu notifier chain with the opp_table_lock held? That seems not so good. Do we need to drop it and reaquire the lock across the table lock? Or perhaps we should rethink widening the lock this much across the notifier.

Hmm, fair point but:
- The OPP notifiers are used only by devfreq and no one else. So the most common case of cpufreq will be just fine.
- The lock is taken across only OPP_EVENT_ADD event and that doesn't get called all the time. Normally it will happen only at boot (once for each OPP) and that's it. I am not sure if we should actually remove the notifier completely going forward.
- Looking at devfreq implementation it seems that they are mostly interested in the updates to the OPP nodes.
- The later series (which I may post today as this one is reviewed mostly), will simplify it a lot. The lock wouldn't be taken across any big parts as we will use kref instead.
- So, I would like to keep this patch as is as this is going to be sorted out anyway.

Quoted 18 lines
Quoted 18 lines
drivers/base/power/opp/core.c +13 -1
@@ -1731,7 +1713,25 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
  */
 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
 {
-	return _opp_add_v1(dev, freq, u_volt, true);
+	struct opp_table *opp_table;
+	int ret;
+
+	/* Hold our table modification lock here */
+	mutex_lock(&opp_table_lock);
+
+	opp_table = _add_opp_table(dev);
+	if (!opp_table) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+
+	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Quoted 8 lines

drivers/base/power/opp/core.c EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper); · line 1733

Quoted 5 lines
drivers/base/power/opp/core.c +5 -0
+	if (ret)
+		_remove_opp_table(opp_table);
+
+unlock:
+	mutex_unlock(&opp_table_lock);

I'd call it table here too, given that we don't have other tables inside OPP anyway. But no problem either way.

Its called as opp_table almost everywhere else in the core.

Signature · 1 line
viresh
author maintainer
#30 lore raw

Re: [PATCH 08/10] PM / OPP: Rename dev_pm_opp_get_suspend_opp() and return OPP rate

In reply to Stephen Boyd
Quoted 21 lines · Stephen Boyd
Quoted 20 lines · Viresh Kumar

There is only one user of dev_pm_opp_get_suspend_opp() and that uses it to get the OPP rate for the suspend_opp.

Rename dev_pm_opp_get_suspend_opp() as dev_pm_opp_get_suspend_opp_freq() and return the rate directly from it.

---
 drivers/base/power/opp/core.c | 27 +++++++++++++--------------
 drivers/cpufreq/cpufreq-dt.c  |  7 +------
 include/linux/pm_opp.h        |  6 +++---
 3 files changed, 17 insertions(+), 23 deletions(-)
drivers/base/power/opp/core.c +0 -0
index 6bcbb64a5582..6fd098618683 100644
@@ -328,32 +328,31 @@ unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
 
Quoted 5 lines

drivers/base/power/opp/core.c unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) · line 331

Quoted 3 lines
drivers/base/power/opp/core.c +1 -1
 /**
- * dev_pm_opp_get_suspend_opp() - Get suspend opp
+ * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp

Please indicate units of frequency.

Sure.

Quoted 28 lines
Quoted 28 lines
drivers/base/power/opp/core.c +6 -11
  * @dev:	device for which we do this operation
  *
- * Return: This function returns pointer to the suspend opp if it is
- * defined and available, otherwise it returns NULL.
- *
- * Locking: This function must be called under rcu_read_lock(). opp is a rcu
- * protected pointer. The reason for the same is that the opp pointer which is
- * returned will remain valid for use with opp_get_{voltage, freq} only while
- * under the locked area. The pointer returned must be used prior to unlocking
- * with rcu_read_unlock() to maintain the integrity of the pointer.
+ * Return: This function returns the frequency of the OPP marked as suspend_opp
+ * if one is available, else returns 0;
  */
-struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
+unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
 {
 	struct opp_table *opp_table;
+	unsigned long freq = 0;
 
-	opp_rcu_lockdep_assert();
+	rcu_read_lock();
 
 	opp_table = _find_opp_table(dev);
 	if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
 	    !opp_table->suspend_opp->available)
-		return NULL;
+		goto unlock;
 
Quoted 5 lines

drivers/base/power/opp/core.c unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) · line 349

Quoted 2 lines
drivers/base/power/opp/core.c +1 -1
-	return opp_table->suspend_opp;
+	freq = dev_pm_opp_get_freq(opp_table->suspend_opp) / 1000;

Why do we include the / 1000? Perhaps we should just return freq and have the caller divide.

Stupid enough. Thanks for pointing out.

Signature · 1 line
viresh
author maintainer
#35 lore raw

Re: [PATCH 10/10] PM / OPP: Split out part of _add_opp_table() and _remove_opp_table()

In reply to Stephen Boyd
Quoted 7 lines · Stephen Boyd
Quoted 6 lines · Viresh Kumar

Split out parts of _add_opp_table() and _remove_opp_table() into separate routines. This will be useful going forward (while doing further cleanups).

Should result in no functional changes.

Quoted 7 lines
Quoted 2 lines

---

But perhaps this should wait until we have patches that actually use them.

Even if there are no more patches, it would be good to separate our allocation/removal to separate routines.

Will update the commit log a bit and will include your Reviewed-by tag. Thanks.

Signature · 1 line
viresh
#12 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

In reply to Viresh Kumar
Quoted 8 lines · Viresh Kumar
Quoted 7 lines · Stephen Boyd
Quoted 5 lines · Viresh Kumar

The code adding static OPPs for V2 bindings already does so. Make the V1 bindings specific code behave the same.

---

Quoted 8 lines
Quoted 5 lines

It looks like that goes back to the original DT OPP binding support written by Shawn Guo. Any idea why we kept trying to add more OPPs if it failed to add one? Best effort or something?

Not sure, maybe for handling duplicate OPPs.

So perhaps we should Cc Shawn Guo who wrote the original code then?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#25 lore raw

Re: [PATCH 07/10] PM / OPP: Don't allocate OPP table from _opp_allocate()

In reply to Viresh Kumar
Quoted 27 lines · Viresh Kumar
Quoted 8 lines · Stephen Boyd
Quoted 7 lines · Viresh Kumar
drivers/base/power/opp/core.c +0 -0
index ef114cf9edcd..6bcbb64a5582 100644
@@ -1030,33 +1029,24 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
 
Quoted 5 lines

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1032

Quoted 3 lines
drivers/base/power/opp/core.c +1 -1
 struct dev_pm_opp *_opp_allocate(struct device *dev,
-				 struct opp_table **opp_table)
+				 struct opp_table *opp_table)

Please call it table instead.

Sure.

Quoted 9 lines

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1039

Quoted 6 lines
drivers/base/power/opp/core.c +0 -3
 {
 	struct dev_pm_opp *opp;
 	int count, supply_size;
-	struct opp_table *table;
-
-	table = _add_opp_table(dev);

Is this the only user of dev? Why do we keep passing dev to this function then?

Quoted 3 lines

Because we are still working with struct list_dev, which needs to save a pointer to dev. We may simplify that with later series though, not sure yet.

Sorry I don't understand. After this patch it looks like dev is unused in the function because we delete the only user _add_opp_table().

Quoted 35 lines
Quoted 9 lines
Quoted 9 lines
drivers/base/power/opp/core.c +2 -1
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
+		unsigned long freq, long u_volt, bool dynamic)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *new_opp;
 	unsigned long tol;
 	int ret;
 
Quoted 5 lines

drivers/base/power/opp/core.c void dev_pm_opp_remove(struct device *dev, unsigned long freq) · line 1047

Quoted 2 lines
drivers/base/power/opp/core.c +0 -2
-	/* Hold our table modification lock here */
-	mutex_lock(&opp_table_lock);

Can we have a mutex locked assertion here? Or a note in the comments that we assume the opp table lock is held?

Done.

Quoted 10 lines
Quoted 10 lines
drivers/base/power/opp/core.c +3 -6
-
-	new_opp = _opp_allocate(dev, &opp_table);
-	if (!new_opp) {
-		ret = -ENOMEM;
-		goto unlock;
-	}
+	new_opp = _opp_allocate(dev, opp_table);
+	if (!new_opp)
+		return -ENOMEM;
 
Quoted 7 lines

drivers/base/power/opp/core.c int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, · line 1173

Quoted 2 lines
drivers/base/power/opp/core.c +0 -0
 	/* populate the opp table */
 	new_opp->rate = freq;

Also, now we call the srcu notifier chain with the opp_table_lock held? That seems not so good. Do we need to drop it and reaquire the lock across the table lock? Or perhaps we should rethink widening the lock this much across the notifier.

Quoted 14 lines

Hmm, fair point but:
- The OPP notifiers are used only by devfreq and no one else. So the most common case of cpufreq will be just fine.
- The lock is taken across only OPP_EVENT_ADD event and that doesn't get called all the time. Normally it will happen only at boot (once for each OPP) and that's it. I am not sure if we should actually remove the notifier completely going forward.
- Looking at devfreq implementation it seems that they are mostly interested in the updates to the OPP nodes.
- The later series (which I may post today as this one is reviewed mostly), will simplify it a lot. The lock wouldn't be taken across any big parts as we will use kref instead.
- So, I would like to keep this patch as is as this is going to be sorted out anyway.

I haven't looked at the next round of patches. If the lock is still held across the notifier for OPP_EVENT_ADD then it would seem that we should get some sort of lockdep splat if the notified functions want to call back into the OPP code and that takes the same lock we held across the notifier. Even if it would work for the other notifiers that aren't OPP_EVENT_ADD, lockdep wouldn't know that because of locking classes, hence the splat. That's my only concern. Obviously if the locking is going away then it's just a bisection hole problem that I'm willing to ignore.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
author maintainer
#13 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

In reply to Stephen Boyd

Hi Shawn,

Quoted 6 lines · Stephen Boyd
Quoted 5 lines · Viresh Kumar
Quoted 4 lines · Stephen Boyd
Quoted 3 lines · Viresh Kumar

The code adding static OPPs for V2 bindings already does so. Make the V1 bindings specific code behave the same.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

---

It looks like that goes back to the original DT OPP binding support written by Shawn Guo. Any idea why we kept trying to add more OPPs if it failed to add one? Best effort or something?

Can you please answer this question from Stephen?

Quoted 5 lines
Quoted 2 lines

Not sure, maybe for handling duplicate OPPs.

So perhaps we should Cc Shawn Guo who wrote the original code then?

I wouldn't mind doing that, but I don't think it needs to be a blocker for now.

Signature · 1 line
viresh
author maintainer
#26 lore raw

Re: [PATCH 07/10] PM / OPP: Don't allocate OPP table from _opp_allocate()

In reply to Stephen Boyd
Quoted 3 lines · Stephen Boyd

Sorry I don't understand. After this patch it looks like dev is unused in the function because we delete the only user _add_opp_table().

Sorry, I misread your earlier comment :(

Quoted 5 lines

I haven't looked at the next round of patches. If the lock is still held across the notifier for OPP_EVENT_ADD then it would seem that we should get some sort of lockdep splat if the notified functions want to call back into the OPP code and that takes the same lock we held across the notifier.

Right.

Quoted 3 lines

Even if it would
work for the other notifiers that aren't OPP_EVENT_ADD, lockdep wouldn't know that because of locking classes, hence the splat.

yes.

Quoted 3 lines

That's my only concern. Obviously if the locking is going away then it's just a bisection hole problem that I'm willing to ignore.

Yes it is surely going away and we wouldn't get the splat for now as well. I already confirmed that only devfreq uses OPP notifiers. I can also confirm that only two devfreq drivers (rk3399 and exynos) register the notifiers and they do that after adding the OPP table.

IOW, no one is listening to OPP_ADD event for now.

Here is the new version of the patch. I will send the complete series only after all the patches are reviewed now..

-------------------------8<-------------------------

Subject: [PATCH] PM / OPP: Don't allocate OPP table from _opp_allocate()

There is no point in trying to find/allocate the table for every OPP that is added for a device. It would be far more efficient to allocate the table only once and pass its pointer to the routines that add the OPP entry.

Locking is removed from _opp_add_static_v2() and _opp_add_v1() now as the callers call them with that lock already held.

Call to _remove_opp_table() routine is also removed from _opp_free() now, as opp_table isn't allocated from within _opp_allocate(). This is handled by the routines which created the OPP table in the first place.

---
 drivers/base/power/opp/core.c | 67 +++++++++++++++++++-------------------
 drivers/base/power/opp/of.c   | 75 ++++++++++++++++++++++---------------------
 drivers/base/power/opp/opp.h  |  9 ++++--
 3 files changed, 78 insertions(+), 73 deletions(-)
drivers/base/power/opp/core.c +34 -33
index b3a624a4af81..dde7dd099aa0 100644
@@ -829,7 +829,7 @@ struct opp_device *_add_opp_dev(const struct device *dev,
  *
  * Return: valid opp_table pointer if success, else NULL.
  */
-static struct opp_table *_add_opp_table(struct device *dev)
+struct opp_table *_add_opp_table(struct device *dev)
 {
 	struct opp_table *opp_table;
 	struct opp_device *opp_dev;
@@ -929,10 +929,9 @@ static void _remove_opp_table(struct opp_table *opp_table)
 		  _kfree_device_rcu);
 }
 
-void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table)
+void _opp_free(struct dev_pm_opp *opp)
 {
 	kfree(opp);
-	_remove_opp_table(opp_table);
 }
 
 /**
@@ -1016,16 +1015,10 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
 
-struct dev_pm_opp *_opp_allocate(struct device *dev,
-				 struct opp_table **opp_table)
+struct dev_pm_opp *_opp_allocate(struct opp_table *table)
 {
 	struct dev_pm_opp *opp;
 	int count, supply_size;
-	struct opp_table *table;
-
-	table = _add_opp_table(dev);
-	if (!table)
-		return NULL;
 
 	/* Allocate space for at least one supply */
 	count = table->regulator_count ? table->regulator_count : 1;
@@ -1033,17 +1026,13 @@ struct dev_pm_opp *_opp_allocate(struct device *dev,
 
 	/* allocate new OPP node and supplies structures */
 	opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
-	if (!opp) {
-		kfree(table);
+	if (!opp)
 		return NULL;
-	}
 
 	/* Put the supplies at the end of the OPP structure as an empty array */
 	opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
 	INIT_LIST_HEAD(&opp->node);
 
-	*opp_table = table;
-
 	return opp;
 }
 
@@ -1133,6 +1122,7 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
 
 /**
  * _opp_add_v1() - Allocate a OPP based on v1 bindings.
+ * @opp_table:	OPP table
  * @dev:	device for which we do this operation
  * @freq:	Frequency in Hz for this OPP
  * @u_volt:	Voltage in uVolts for this OPP
@@ -1158,22 +1148,18 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  *		Duplicate OPPs (both freq and volt are same) and !opp->available
  * -ENOMEM	Memory allocation failure
  */
-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
-		bool dynamic)
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
+		unsigned long freq, long u_volt, bool dynamic)
 {
-	struct opp_table *opp_table;
 	struct dev_pm_opp *new_opp;
 	unsigned long tol;
 	int ret;
 
-	/* Hold our table modification lock here */
-	mutex_lock(&opp_table_lock);
+	opp_rcu_lockdep_assert();
 
-	new_opp = _opp_allocate(dev, &opp_table);
-	if (!new_opp) {
-		ret = -ENOMEM;
-		goto unlock;
-	}
+	new_opp = _opp_allocate(opp_table);
+	if (!new_opp)
+		return -ENOMEM;
 
 	/* populate the opp table */
 	new_opp->rate = freq;
@@ -1192,8 +1178,6 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 		goto free_opp;
 	}
 
-	mutex_unlock(&opp_table_lock);
-
 	/*
 	 * Notify the changes in the availability of the operable
 	 * frequency/voltage list.
@@ -1202,9 +1186,8 @@ int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
 	return 0;
 
 free_opp:
-	_opp_free(new_opp, opp_table);
-unlock:
-	mutex_unlock(&opp_table_lock);
+	_opp_free(new_opp);
+
 	return ret;
 }
 
@@ -1722,7 +1705,25 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
  */
 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
 {
-	return _opp_add_v1(dev, freq, u_volt, true);
+	struct opp_table *opp_table;
+	int ret;
+
+	/* Hold our table modification lock here */
+	mutex_lock(&opp_table_lock);
+
+	opp_table = _add_opp_table(dev);
+	if (!opp_table) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+
+	ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
+	if (ret)
+		_remove_opp_table(opp_table);
+
+unlock:
+	mutex_unlock(&opp_table_lock);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
 
@@ -1888,8 +1889,8 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
  * Free OPPs either created using static entries present in DT or even the
  * dynamically added entries based on remove_all param.
  */
-static void _dev_pm_opp_remove_table(struct opp_table *opp_table,
-				     struct device *dev, bool remove_all)
+void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
+			      bool remove_all)
 {
 	struct dev_pm_opp *opp, *tmp;
 
drivers/base/power/opp/of.c +38 -37

157 lines not on this page · raw message

drivers/base/power/opp/opp.h +6 -3
index c4b539a8533a..0a5eb4d1e8f7 100644
@@ -191,13 +191,16 @@ struct opp_table {
 
 /* Routines internal to opp core */
 struct opp_table *_find_opp_table(struct device *dev);
+struct opp_table *_add_opp_table(struct device *dev);
 struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
+void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, bool remove_all);
 void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all);
-struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table);
-void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table);
+struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table);
+void _opp_free(struct dev_pm_opp *opp);
 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic);
+int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic);
 void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of);
+struct opp_table *_add_opp_table(struct device *dev);
 
 #ifdef CONFIG_OF
 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev);
#14 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

In reply to Viresh Kumar
Quoted 12 lines · Viresh Kumar

Hi Shawn,

Quoted 9 lines · Stephen Boyd
Quoted 8 lines · Viresh Kumar
Quoted 7 lines · Stephen Boyd
Quoted 5 lines · Viresh Kumar

The code adding static OPPs for V2 bindings already does so. Make the V1 bindings specific code behave the same.

---

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

It looks like that goes back to the original DT OPP binding support written by Shawn Guo. Any idea why we kept trying to add more OPPs if it failed to add one? Best effort or something?

Can you please answer this question from Stephen?

Yes, it's the best effort. For example, a duplicated OPP shouldn't be a stopper for the driver.

Shawn

author maintainer
#15 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

Quoted 1 line · Shawn Guo

Yes, it's the best effort.

That may get a risky IMO. What if we fail to add all OPPs except the last one, which selects the highest frequency? We will surely overheat most of the SoCs and burn them out.

I think its better to fail and return in such cases.

Quoted 2 lines

For example, a duplicated OPP shouldn't be a
stopper for the driver.

That's a different case and is already supported properly, as we wouldn't fail in that case.

Thanks for your inputs Shawn.

Signature · 1 line
viresh
#16 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

Quoted 5 lines · Viresh Kumar
Quoted 1 line · Shawn Guo

Yes, it's the best effort.

That may get a risky IMO. What if we fail to add all OPPs except the last one, which selects the highest frequency?

That's the same case when Performance governor is selected, and should be guaranteed safe, shouldn't it?

Quoted 2 lines

We will surely overheat most of the SoCs
and burn them out.

IMO, the problem is on OPP table. Every entry in the table should be safe for running for any time.

Shawn

author maintainer
#17 lore raw

Re: [PATCH 04/10] PM / OPP: Error out on failing to add static OPPs for v1 bindings

Quoted 2 lines · Shawn Guo

That's the same case when Performance governor is selected, and should be guaranteed safe, shouldn't it?

Depends on platform I would say. For example for a big LITTLE platform, running the big core on max not only increases the amount of power it uses but also the amount of heat it generates, which is really scary.

Performance governor is fine for testing but it is not used for battery operated devices in production code.

Quoted 5 lines
Quoted 2 lines

We will surely overheat most of the SoCs
and burn them out.

IMO, the problem is on OPP table. Every entry in the table should be safe for running for any time.

Not necessarily. The highest OPPs might be suggested to run only for small period of time only. And there is nothing wrong in it.

Over that if some of the OPPs fail to get registered from the OPP table in DT, it surely hints at some sort of bugs in the code or DT.

Why should we try to go ahead if only a part of the DT table gets registered? I don't find a compelling reason for that. It will never happen normally anyway, as we wouldn't have any errors.

--
viresh

Reviewed-by
#27 lore raw

Re: [PATCH 07/10] PM / OPP: Don't allocate OPP table from _opp_allocate()

In reply to Viresh Kumar
Quoted 10 lines · Viresh Kumar

Subject: [PATCH] PM / OPP: Don't allocate OPP table from _opp_allocate()

There is no point in trying to find/allocate the table for every OPP that is added for a device. It would be far more efficient to allocate the table only once and pass its pointer to the routines that add the OPP entry.

Locking is removed from _opp_add_static_v2() and _opp_add_v1() now as the callers call them with that lock already held.

Quoted 6 lines

Call to _remove_opp_table() routine is also removed from _opp_free() now, as opp_table isn't allocated from within _opp_allocate(). This is handled by the routines which created the OPP table in the first place.

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project