OPP: Don't create multiple OPP tables for devices sharing OPP table
Subsystems (5)
Viresh Kumar
maintainercover OPP: Don't create multiple OPP tables for devices sharing OPP table
Hello,
Niklas Cassle recently reported some regressions with his Qcom cpufreq driver where he was getting some errors while creating the OPPs tables.
After looking into it I realized that the OPP core incorrectly creates multiple OPP tables for the devices even if they are sharing the OPP table in DT. This happens when the request comes using different CPU devices. For example, dev_pm_opp_set_supported_hw() getting called using CPU0 and dev_pm_opp_of_add_table() getting called using CPU1.
This series redesigns the internals of the OPP core to fix that. The redesign has simplified the core itself though.
@Niklas: Can you please confirm that this series fixes the issues you have reported ? I have already tested it on Hikey960.
--
viresh
Viresh Kumar (11):
OPP: Free OPP table properly on performance state irregularities
OPP: Protect dev_list with opp_table lock
OPP: Pass index to _of_init_opp_table()
OPP: Parse OPP table's DT properties from _of_init_opp_table()
OPP: Don't take OPP table's kref for static OPPs
OPP: Create separate kref for static OPPs list
cpufreq: mvebu: Remove OPPs using dev_pm_opp_remove()
OPP: Don't remove dynamic OPPs from _dev_pm_opp_remove_table()
OPP: Use a single mechanism to free the OPP table
OPP: Prevent creating multiple OPP tables for devices sharing OPP
nodes
OPP: Pass OPP table to _of_add_opp_table_v{1|2}()
drivers/cpufreq/mvebu-cpufreq.c | 9 +- drivers/opp/core.c | 147 ++++++++++++++++--------- drivers/opp/cpu.c | 11 +- drivers/opp/of.c | 186 +++++++++++++++++--------------- drivers/opp/opp.h | 19 ++-- include/linux/pm_opp.h | 6 ++ 6 files changed, 221 insertions(+), 157 deletions(-)
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 1/11 OPP: Free OPP table properly on performance state irregularities
The OPP table was freed, but not the individual OPPs which is done from _dev_pm_opp_remove_table(). Fix it by calling _dev_pm_opp_remove_table() as well.
--- drivers/opp/of.c | 1 + 1 file changed, 1 insertion(+)
drivers/opp/of.c +1 -0
@@ -425,6 +425,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)dev_err (dev,"Not all nodes have performance state set (%d: %d)\n" , count, pstate_count); ret = -ENOENT ; +_dev_pm_opp_remove_table (opp_table, dev, false); goto put_opp_table; }
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 2/11 OPP: Protect dev_list with opp_table lock
In reply to Viresh KumarThe 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.
--- drivers/opp/core.c | 21 +++++++++++++++++++-- drivers/opp/cpu.c | 2 ++ drivers/opp/opp.h | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-)
drivers/opp/core.c +19 -2
@@ -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)
drivers/opp/cpu.c +2 -0
@@ -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); }
drivers/opp/opp.h +1 -1
@@ -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.
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 3/11 OPP: Pass index to _of_init_opp_table()
In reply to Viresh KumarThis is a preparatory patch required for the next commit which will start using OPP table's node pointer in _of_init_opp_table(), which requires the index in order to read the OPP table's phandle.
This commit adds the index argument in the call chains in order to get it delivered to _of_init_opp_table().
--- drivers/opp/core.c | 19 +++++++++++++++---- drivers/opp/of.c | 12 +++++++----- drivers/opp/opp.h | 4 ++-- include/linux/pm_opp.h | 6 ++++++ 4 files changed, 30 insertions(+), 11 deletions(-)
drivers/opp/core.c +15 -4
@@ -785,7 +785,7 @@ struct opp_device *_add_opp_dev(const struct device *dev,return opp_dev; } -static struct opp_table *_allocate_opp_table (struct device *dev) +static struct opp_table *_allocate_opp_table (struct device *dev,int index) {struct opp_table *opp_table;struct opp_device *opp_dev;
@@ -808,7 +808,7 @@ static struct opp_table *_allocate_opp_table(struct device *dev)return NULL ; } -_of_init_opp_table (opp_table, dev); +_of_init_opp_table (opp_table, dev, index);/* Find clk for the device */ opp_table->clk =clk_get (dev,NULL );
@@ -833,7 +833,7 @@ void _get_opp_table_kref(struct opp_table *opp_table)kref_get (&opp_table->kref); } -struct opp_table *dev_pm_opp_get_opp_table (struct device *dev) +static struct opp_table *_opp_get_opp_table (struct device *dev,int index) {struct opp_table *opp_table;
@@ -844,15 +844,26 @@ struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)if (!IS_ERR (opp_table)) goto unlock; - opp_table =_allocate_opp_table (dev); + opp_table =_allocate_opp_table (dev, index); unlock:mutex_unlock (&opp_table_lock);return opp_table; } + +struct opp_table *dev_pm_opp_get_opp_table (struct device *dev) +{ +return _opp_get_opp_table (dev,0 ); +}EXPORT_SYMBOL_GPL (dev_pm_opp_get_opp_table); +struct opp_table *dev_pm_opp_get_opp_table_indexed (struct device *dev, +int index) +{ +return _opp_get_opp_table (dev, index); +} +static void _opp_table_kref_release (struct kref *kref) { struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
drivers/opp/of.c +7 -5
@@ -52,7 +52,8 @@ static struct opp_table *_managed_opp(const struct device_node *np)return managed_table; } -void _of_init_opp_table (struct opp_table *opp_table,struct device *dev) +void _of_init_opp_table (struct opp_table *opp_table,struct device *dev, +int index) {struct device_node *np;
@@ -378,7 +379,8 @@ static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev, }/* Initializes OPP tables based on new bindings */ -static int _of_add_opp_table_v2 (struct device *dev,struct device_node *opp_np) +static int _of_add_opp_table_v2 (struct device *dev,struct device_node *opp_np, +int index) {struct device_node *np;struct opp_table *opp_table;
@@ -393,7 +395,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np) goto put_opp_table; } - opp_table =dev_pm_opp_get_opp_table (dev); + opp_table =dev_pm_opp_get_opp_table_indexed (dev, index);if (!opp_table)return -ENOMEM ;
@@ -526,7 +528,7 @@ int dev_pm_opp_of_add_table(struct device *dev)return _of_add_opp_table_v1 (dev); } - ret =_of_add_opp_table_v2 (dev, opp_np); + ret =_of_add_opp_table_v2 (dev, opp_np,0 );of_node_put (opp_np);return ret;
@@ -574,7 +576,7 @@ int dev_pm_opp_of_add_table_indexed(struct device *dev, int index)return -ENODEV ; } - ret =_of_add_opp_table_v2 (dev, opp_np); + ret =_of_add_opp_table_v2 (dev, opp_np, index);of_node_put (opp_np);return ret;
drivers/opp/opp.h +2 -2
@@ -200,9 +200,9 @@ 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); +void _of_init_opp_table (struct opp_table *opp_table,struct device *dev,int index);#else -static inline void _of_init_opp_table (struct opp_table *opp_table,struct device *dev) {} +static inline void _of_init_opp_table (struct opp_table *opp_table,struct device *dev,int index) {}#endif #ifdef CONFIG_DEBUG_FS
include/linux/pm_opp.h +6 -0
@@ -79,6 +79,7 @@ struct dev_pm_set_opp_data {#if defined(CONFIG_PM_OPP )struct opp_table *dev_pm_opp_get_opp_table (struct device *dev); +struct opp_table *dev_pm_opp_get_opp_table_indexed (struct device *dev,int index);void dev_pm_opp_put_opp_table (struct opp_table *opp_table);unsigned long dev_pm_opp_get_voltage (struct dev_pm_opp *opp);
@@ -136,6 +137,11 @@ static inline struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)return ERR_PTR (-ENOTSUPP ); } +static inline struct opp_table *dev_pm_opp_get_opp_table_indexed (struct device *dev,int index) +{ +return ERR_PTR (-ENOTSUPP ); +} +static inline void dev_pm_opp_put_opp_table (struct opp_table *opp_table) {}static inline unsigned long dev_pm_opp_get_voltage (struct dev_pm_opp *opp)
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 4/11 OPP: Parse OPP table's DT properties from _of_init_opp_table()
In reply to Viresh KumarParse the DT properties present in the OPP table from _of_init_opp_table(), which is a dedicated routine for DT parsing.
Minor relocation of helpers is required for this.
It is possible now for _managed_opp() to return a partially initialized OPP table if the OPP table is created via the helpers like dev_pm_opp_set_supported_hw() and we need another flag to indicate if the static OPP are already parsed or not to make sure we don't incorrectly skip initializing the static OPPs.
--- drivers/opp/of.c | 79 ++++++++++++++++++++++++++++------------------- drivers/opp/opp.h | 2 ++ 2 files changed, 50 insertions(+), 31 deletions(-)
drivers/opp/of.c +48 -31
@@ -23,6 +23,24 @@#include "opp.h" +/* +* Returns opp descriptor node for a device node, caller must +* do of_node_put(). +*/ +static struct device_node *_opp_of_get_opp_desc_node (struct device_node *np, +int index) +{ +/* "operating-points-v2" can be an array for power domain providers */ +return of_parse_phandle (np,"operating-points-v2" , index); +} + +/* Returns opp descriptor node for a device, caller must do of_node_put() */ +struct device_node *dev_pm_opp_of_get_opp_desc_node (struct device *dev) +{ +return _opp_of_get_opp_desc_node (dev->of_node,0 ); +} +EXPORT_SYMBOL_GPL (dev_pm_opp_of_get_opp_desc_node); +static struct opp_table *_managed_opp (const struct device_node *np) {struct opp_table *opp_table, *managed_table =NULL ;
@@ -55,22 +73,37 @@ static struct opp_table *_managed_opp(const struct device_node *np)void _of_init_opp_table (struct opp_table *opp_table,struct device *dev,int index) { -struct device_node *np; +struct device_node *np, *opp_np; +u32 val;/* * Only required for backward compatibility with v1 bindings, but isn't * harmful for other cases. And so we do it unconditionally. */ np =of_node_get (dev->of_node); -if (np) { -u32 val; - -if (!of_property_read_u32 (np,"clock-latency" , &val)) - opp_table->clock_latency_ns_max = val; -of_property_read_u32 (np,"voltage-tolerance" , - &opp_table->voltage_tolerance_v1); -of_node_put (np); - } +if (!np) +return ; + +if (!of_property_read_u32 (np,"clock-latency" , &val)) + opp_table->clock_latency_ns_max = val; +of_property_read_u32 (np,"voltage-tolerance" , + &opp_table->voltage_tolerance_v1); + +/* Get OPP table node */ + opp_np =_opp_of_get_opp_desc_node (np, index); +of_node_put (np); + +if (!opp_np) +return ; + +if (of_property_read_bool (opp_np,"opp-shared" )) + opp_table->shared_opp =OPP_TABLE_ACCESS_SHARED ; +else + opp_table->shared_opp =OPP_TABLE_ACCESS_EXCLUSIVE ; + + opp_table->np = opp_np; + +of_node_put (opp_np); } static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,
@@ -250,22 +283,6 @@ void dev_pm_opp_of_remove_table(struct device *dev) }EXPORT_SYMBOL_GPL (dev_pm_opp_of_remove_table); -/* Returns opp descriptor node for a device node, caller must -* do of_node_put() */ -static struct device_node *_opp_of_get_opp_desc_node (struct device_node *np, -int index) -{ -/* "operating-points-v2" can be an array for power domain providers */ -return of_parse_phandle (np,"operating-points-v2" , index); -} - -/* Returns opp descriptor node for a device, caller must do of_node_put() */ -struct device_node *dev_pm_opp_of_get_opp_desc_node (struct device *dev) -{ -return _opp_of_get_opp_desc_node (dev->of_node,0 ); -} -EXPORT_SYMBOL_GPL (dev_pm_opp_of_get_opp_desc_node); -/** * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) * @opp_table: OPP table
@@ -392,6 +409,9 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,/* OPPs are already managed */ if (!_add_opp_dev (dev, opp_table)) ret = -ENOMEM ; +else if (!opp_table->parsed_static_opps) + goto initialize_static_opps; + goto put_opp_table; }
@@ -399,6 +419,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,if (!opp_table)return -ENOMEM ; +initialize_static_opps:/* We have opp-table node now, iterate over it and add OPPs */ for_each_available_child_of_node (opp_np, np) { count++;
@@ -434,11 +455,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,if (pstate_count) opp_table->genpd_performance_state = true; - opp_table->np = opp_np; -if (of_property_read_bool (opp_np,"opp-shared" )) - opp_table->shared_opp =OPP_TABLE_ACCESS_SHARED ; -else - opp_table->shared_opp =OPP_TABLE_ACCESS_EXCLUSIVE ; + opp_table->parsed_static_opps = true; put_opp_table:dev_pm_opp_put_opp_table (opp_table);
drivers/opp/opp.h +2 -0
@@ -129,6 +129,7 @@ enum opp_table_access {* @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. +* @parsed_static_opps: True if OPPs are initialized from DT. * @shared_opp: OPP is shared between multiple devices. * @suspend_opp: Pointer to OPP to be used during device suspend. * @supported_hw: Array of version number to support.
@@ -164,6 +165,7 @@ struct opp_table {/* For backward compatibility with v1 bindings */ unsigned int voltage_tolerance_v1; +bool parsed_static_opps;enum opp_table_access shared_opp;struct dev_pm_opp *suspend_opp;
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 5/11 OPP: Don't take OPP table's kref for static OPPs
In reply to Viresh KumarThe reference count is only required to be incremented for every call that may lead to adding the OPP table. For static OPPs the same should be done from the parent routine which adds all static OPPs together and so only one refcount for all static OPPs.
Update code to reflect that.
The refcount is incremented every time a dynamic OPP is created (as that can lead to creating the OPP table) and the same is dropped when the OPP is removed.
--- drivers/opp/core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/opp/core.c +6 -5
@@ -919,7 +919,6 @@ static void _opp_kref_release(struct kref *kref)kfree (opp);mutex_unlock (&opp_table->lock); -dev_pm_opp_put_opp_table (opp_table); }void dev_pm_opp_get (struct dev_pm_opp *opp)
@@ -963,11 +962,15 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq)if (found) {dev_pm_opp_put (opp); + +/* Drop the reference taken by dev_pm_opp_add() */ +dev_pm_opp_put_opp_table (opp_table); }else {dev_warn (dev,"%s: Couldn't find OPP with freq: %lu\n" , __func__, freq); } +/* Drop the reference taken by _find_opp_table() */ dev_pm_opp_put_opp_table (opp_table); }EXPORT_SYMBOL_GPL (dev_pm_opp_remove);
@@ -1085,9 +1088,6 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, new_opp->opp_table = opp_table;kref_init (&new_opp->kref); -/* Get a reference to the OPP table */ -_get_opp_table_kref (opp_table); - ret =opp_debug_create_one (new_opp, opp_table);if (ret) dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
@@ -1566,8 +1566,9 @@ int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)return -ENOMEM ; ret =_opp_add_v1 (opp_table, dev, freq, u_volt, true); +if (ret) +dev_pm_opp_put_opp_table (opp_table); -dev_pm_opp_put_opp_table (opp_table);return ret; }EXPORT_SYMBOL_GPL (dev_pm_opp_add);
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 6/11 OPP: Create separate kref for static OPPs list
In reply to Viresh KumarThe static OPPs don't always get freed with the OPP table, it can happen before that as well. For example, if the OPP table is first created using helpers like dev_pm_opp_set_supported_hw() and the OPPs are created at a later point. Now when the OPPs are removed, the OPP table stays until the time dev_pm_opp_put_supported_hw() is called.
Later patches will streamline the freeing of OPP table and that requires the static OPPs to get freed with help of a separate kernel reference. This patch prepares for that by creating a separate kref for static OPPs list.
--- drivers/opp/core.c | 33 ++++++++++++++++++++++++++++++++- drivers/opp/of.c | 7 +++++++ drivers/opp/opp.h | 3 +++ 3 files changed, 42 insertions(+), 1 deletion(-)
drivers/opp/core.c +32 -1
@@ -892,6 +892,33 @@ static void _opp_table_kref_release(struct kref *kref)mutex_unlock (&opp_table_lock); } +void _opp_remove_all_static (struct opp_table *opp_table) +{ +struct dev_pm_opp *opp, *tmp; + +list_for_each_entry_safe (opp, tmp, &opp_table->opp_list, node) { +if (!opp->dynamic) +dev_pm_opp_put (opp); + } + + opp_table->parsed_static_opps = false; +} + +static void _opp_table_list_kref_release (struct kref *kref) +{ + struct opp_table *opp_table = container_of(kref, struct opp_table, + list_kref); + +_opp_remove_all_static (opp_table); +mutex_unlock (&opp_table_lock); +} + +void _put_opp_list_kref (struct opp_table *opp_table) +{ +kref_put_mutex (&opp_table->list_kref, _opp_table_list_kref_release, + &opp_table_lock); +} +void dev_pm_opp_put_opp_table (struct opp_table *opp_table) { kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
@@ -1746,8 +1773,11 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,/* Find if opp_table manages a single device */ if (list_is_singular (&opp_table->dev_list)) {/* Free static OPPs */ +_put_opp_list_kref (opp_table); + +/* Free dynamic OPPs */ list_for_each_entry_safe (opp, tmp, &opp_table->opp_list, node) { -if (remove_all || !opp->dynamic) +if (remove_all)dev_pm_opp_put (opp); }
@@ -1758,6 +1788,7 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,if (opp_table->genpd_performance_state)dev_pm_genpd_set_performance_state (dev,0 ); }else { +_put_opp_list_kref (opp_table);_remove_opp_dev (_find_opp_dev (dev, opp_table), opp_table); }
drivers/opp/of.c +7 -0
@@ -411,6 +411,8 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, ret = -ENOMEM ; else if (!opp_table->parsed_static_opps) goto initialize_static_opps; + else + kref_get(&opp_table->list_kref); goto put_opp_table; }
@@ -420,6 +422,8 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,return -ENOMEM ; initialize_static_opps: +kref_init (&opp_table->list_kref); +/* We have opp-table node now, iterate over it and add OPPs */ for_each_available_child_of_node (opp_np, np) { count++;
@@ -437,6 +441,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,/* There should be one of more OPP defined */ if (WARN_ON (!count)) { ret = -ENOENT ; +_put_opp_list_kref (opp_table); goto put_opp_table; }
@@ -491,6 +496,8 @@ static int _of_add_opp_table_v1(struct device *dev)if (!opp_table)return -ENOMEM ; +kref_init (&opp_table->list_kref); + val = prop->value;while (nr) {unsigned long freq =be32_to_cpup (val++) *1000 ;
drivers/opp/opp.h +3 -0
@@ -126,6 +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. +* @list_kref: for reference count of 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.
@@ -157,6 +158,7 @@ struct opp_table {struct list_head dev_list;struct list_head opp_list;struct kref kref; +struct kref list_kref;struct mutex lock;struct device_node *np;
@@ -200,6 +202,7 @@ int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *oint _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); +void _put_opp_list_kref (struct opp_table *opp_table);#ifdef CONFIG_OF void _of_init_opp_table (struct opp_table *opp_table,struct device *dev,int index);
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 7/11 cpufreq: mvebu: Remove OPPs using dev_pm_opp_remove()
In reply to Viresh Kumardev_pm_opp_cpumask_remove_table() is going to change in the next commit and will not remove dynamic OPPs automatically. They must be removed with a call to dev_pm_opp_remove().
--- drivers/cpufreq/mvebu-cpufreq.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/cpufreq/mvebu-cpufreq.c +2 -7
@@ -84,9 +84,10 @@ static int __init armada_xp_pmsu_cpufreq_init(void) ret =dev_pm_opp_add (cpu_dev,clk_get_rate (clk) /2 ,0 );if (ret) { +dev_pm_opp_remove (cpu_dev,clk_get_rate (clk));clk_put (clk);dev_err (cpu_dev,"Failed to register OPPs\n" ); - goto opp_register_failed; +return ret; } ret = dev_pm_opp_set_sharing_cpus(cpu_dev,
@@ -99,11 +100,5 @@ static int __init armada_xp_pmsu_cpufreq_init(void)platform_device_register_simple ("cpufreq-dt" ,-1 ,NULL ,0 );return 0 ; - -opp_register_failed: -/* As registering has failed remove all the opp for all cpus */ -dev_pm_opp_cpumask_remove_table (cpu_possible_mask); - -return ret; }device_initcall (armada_xp_pmsu_cpufreq_init);
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Re: [PATCH 07/11] cpufreq: mvebu: Remove OPPs using dev_pm_opp_remove()
Hi Viresh,
Quoted 3 lines · Viresh Kumar
dev_pm_opp_cpumask_remove_table()is going to change in the next commit and will not remove dynamic OPPs automatically. They must be removed with a call todev_pm_opp_remove().
So now that mean when adding more than 2 OPPs, we should have the list of them available in the driver to be able to remove them, right?
Quoted 1 line
Signed-off-by Viresh Kumar <[email protected]>
Gregory
Quoted 35 lines
--- drivers/cpufreq/mvebu-cpufreq.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)drivers/cpufreq/mvebu-cpufreq.c +2
-7@@ -84,9 +84,10 @@ static int __init armada_xp_pmsu_cpufreq_init(void) ret =dev_pm_opp_add (cpu_dev,clk_get_rate (clk) /2 ,0 );if (ret) { +dev_pm_opp_remove (cpu_dev,clk_get_rate (clk));clk_put (clk);dev_err (cpu_dev,"Failed to register OPPs\n" ); - goto opp_register_failed; +return ret; } ret = dev_pm_opp_set_sharing_cpus(cpu_dev,@@ -99,11 +100,5 @@ static int __init armada_xp_pmsu_cpufreq_init(void)platform_device_register_simple ("cpufreq-dt" ,-1 ,NULL ,0 );return 0 ; - -opp_register_failed: -/* As registering has failed remove all the opp for all cpus */ -dev_pm_opp_cpumask_remove_table (cpu_possible_mask); - -return ret; }device_initcall (armada_xp_pmsu_cpufreq_init);Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Signature · 3 lines
Gregory Clement, Bootlin Embedded Linux and Kernel engineering http://bootlin.com
Viresh Kumar
maintainerRe: [PATCH 07/11] cpufreq: mvebu: Remove OPPs using dev_pm_opp_remove()
Quoted 10 lines · Gregory CLEMENT
Hi Viresh,
Quoted 3 lines · Viresh Kumar
dev_pm_opp_cpumask_remove_table()is going to change in the next commit and will not remove dynamic OPPs automatically. They must be removed with a call todev_pm_opp_remove().So now that mean when adding more than 2 OPPs, we should have the list of them available in the driver to be able to remove them, right?
Right. Maybe we will add a separate API to remove all dynamic ones in one go but I couldn't find any platform which needs to do it right now.
Quoted 3 lines
Quoted 1 line
Signed-off-by Viresh Kumar <[email protected]>Reviewed-by Gregory CLEMENT <[email protected]>
Thanks.
Signature · 1 line
viresh
Viresh Kumar
maintainerPATCH 8/11 OPP: Don't remove dynamic OPPs from _dev_pm_opp_remove_table()
In reply to Viresh KumarOnly one platform was depending on this feature and it is already updated now. Stop removing dynamic OPPs from _dev_pm_opp_remove_table(). This simplifies lot of paths and removes unnecessary parameters.
--- drivers/opp/core.c | 20 +++++--------------- drivers/opp/cpu.c | 9 +++------ drivers/opp/of.c | 10 +++++----- drivers/opp/opp.h | 6 +++--- 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/opp/core.c +5 -15
@@ -1759,14 +1759,10 @@ int dev_pm_opp_unregister_notifier(struct device *dev,EXPORT_SYMBOL (dev_pm_opp_unregister_notifier);/* -* Free OPPs either created using static entries present in DT or even the -* dynamically added entries based on remove_all param. +* Free OPPs either created using static entries present in DT. */ -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) { -struct dev_pm_opp *opp, *tmp; -/* Protect dev_list */ mutex_lock (&opp_table->lock);
@@ -1775,12 +1771,6 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,/* Free static OPPs */ _put_opp_list_kref (opp_table); -/* Free dynamic OPPs */ -list_for_each_entry_safe (opp, tmp, &opp_table->opp_list, node) { -if (remove_all) -dev_pm_opp_put (opp); - } -/* * The OPP table is getting removed, drop the performance state * constraints.
@@ -1795,7 +1785,7 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,mutex_unlock (&opp_table->lock); } -void _dev_pm_opp_find_and_remove_table (struct device *dev,bool remove_all) +void _dev_pm_opp_find_and_remove_table (struct device *dev) {struct opp_table *opp_table;
@@ -1812,7 +1802,7 @@ void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)return ; } -_dev_pm_opp_remove_table (opp_table, dev, remove_all); +_dev_pm_opp_remove_table (opp_table, dev);dev_pm_opp_put_opp_table (opp_table); }
@@ -1826,6 +1816,6 @@ void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)*/ void dev_pm_opp_remove_table (struct device *dev) { -_dev_pm_opp_find_and_remove_table (dev, true); +_dev_pm_opp_find_and_remove_table (dev); }EXPORT_SYMBOL_GPL (dev_pm_opp_remove_table);
drivers/opp/cpu.c +3 -6
@@ -108,7 +108,7 @@ void dev_pm_opp_free_cpufreq_table(struct device *dev,EXPORT_SYMBOL_GPL (dev_pm_opp_free_cpufreq_table);#endif /* CONFIG_CPU_FREQ */ -void _dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask,bool of) +void _dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask) {struct device *cpu_dev;int cpu;
@@ -123,10 +123,7 @@ void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)continue ; } -if (of) -dev_pm_opp_of_remove_table (cpu_dev); -else -dev_pm_opp_remove_table (cpu_dev); +_dev_pm_opp_find_and_remove_table (cpu_dev); } }
@@ -140,7 +137,7 @@ void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)*/ void dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask) { -_dev_pm_opp_cpumask_remove_table (cpumask, false); +_dev_pm_opp_cpumask_remove_table (cpumask); }EXPORT_SYMBOL_GPL (dev_pm_opp_cpumask_remove_table);
drivers/opp/of.c +5 -5
@@ -279,7 +279,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_find_and_remove_table (dev, false); +_dev_pm_opp_find_and_remove_table (dev); }EXPORT_SYMBOL_GPL (dev_pm_opp_of_remove_table);
@@ -432,7 +432,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,if (ret) {dev_err (dev,"%s: Failed to add OPP, %d\n" , __func__, ret); -_dev_pm_opp_remove_table (opp_table, dev, false); +_dev_pm_opp_remove_table (opp_table, dev);of_node_put (np); goto put_opp_table; }
@@ -453,7 +453,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,dev_err (dev,"Not all nodes have performance state set (%d: %d)\n" , count, pstate_count); ret = -ENOENT ; -_dev_pm_opp_remove_table (opp_table, dev, false); +_dev_pm_opp_remove_table (opp_table, dev); goto put_opp_table; }
@@ -507,7 +507,7 @@ static int _of_add_opp_table_v1(struct device *dev)if (ret) {dev_err (dev,"%s: Failed to add OPP %ld (%d)\n" , __func__, freq, ret); -_dev_pm_opp_remove_table (opp_table, dev, false); +_dev_pm_opp_remove_table (opp_table, dev);break ; } nr -=2 ;
@@ -618,7 +618,7 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);*/ void dev_pm_opp_of_cpumask_remove_table (const struct cpumask *cpumask) { -_dev_pm_opp_cpumask_remove_table (cpumask, true); +_dev_pm_opp_cpumask_remove_table (cpumask); }EXPORT_SYMBOL_GPL (dev_pm_opp_of_cpumask_remove_table);
drivers/opp/opp.h +3 -3
@@ -194,13 +194,13 @@ void _get_opp_table_kref(struct opp_table *opp_table);int _get_opp_count (struct opp_table *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 opp_table *opp_table,struct device *dev,bool remove_all); -void _dev_pm_opp_find_and_remove_table (struct device *dev,bool remove_all); +void _dev_pm_opp_remove_table (struct opp_table *opp_table,struct device *dev); +void _dev_pm_opp_find_and_remove_table (struct device *dev);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,bool rate_not_available);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); +void _dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask);struct opp_table *_add_opp_table (struct device *dev);void _put_opp_list_kref (struct opp_table *opp_table);
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 9/11 OPP: Use a single mechanism to free the OPP table
In reply to Viresh KumarCurrently there are two separate ways to free the OPP table based on how it is created in the first place.
We call _dev_pm_opp_remove_table() to free the static and/or dynamic OPP, OPP list devices, etc. This is done for the case where the OPP table is added while initializing the OPPs, like via the path dev_pm_opp_of_add_table().
We also call dev_pm_opp_put_opp_table() in some cases which eventually frees the OPP table structure once the reference count reaches 0. This is used by the first case as well as other cases like dev_pm_opp_set_regulators() where the OPPs aren't necessarily initialized at this point.
This whole thing is a bit unclear and messy and obstruct any further cleanup/fixup of OPP core.
This patch tries to streamline this by keeping a single path for OPP table destruction, i.e. dev_pm_opp_put_opp_table().
All the cleanup happens in _opp_table_kref_release() now after the reference count reaches 0. _dev_pm_opp_remove_table() is removed as it isn't required anymore.
We don't drop the reference to the OPP table after creating it from _of_add_opp_table_v{1|2}() anymore and the same is dropped only when we try to remove them.
--- drivers/opp/core.c | 54 ++++++++++++++-------------------------------- drivers/opp/of.c | 32 +++++++++++++++------------ drivers/opp/opp.h | 2 +- 3 files changed, 35 insertions(+), 53 deletions(-)
drivers/opp/core.c +16 -38
@@ -867,23 +867,24 @@ struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,static void _opp_table_kref_release (struct kref *kref) { struct opp_table *opp_table = container_of(kref, struct opp_table, kref); -struct opp_device *opp_dev; +struct opp_device *opp_dev, *temp;/* Release clk */ 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); +WARN_ON (!list_empty (&opp_table->opp_list)); -_remove_opp_dev (opp_dev, opp_table); +list_for_each_entry_safe (opp_dev, temp, &opp_table->dev_list, node) { +/* +* The OPP table is getting removed, drop the performance state +* constraints. +*/ +if (opp_table->genpd_performance_state) +dev_pm_genpd_set_performance_state ((struct device *)(opp_dev->dev),0 ); -/* dev_list must be empty now */ -WARN_ON (!list_empty (&opp_table->dev_list)); +_remove_opp_dev (opp_dev, opp_table); + }mutex_destroy (&opp_table->lock);list_del (&opp_table->node);
@@ -1758,33 +1759,6 @@ int dev_pm_opp_unregister_notifier(struct device *dev, }EXPORT_SYMBOL (dev_pm_opp_unregister_notifier); -/* -* Free OPPs either created using static entries present in DT. -*/ -void _dev_pm_opp_remove_table (struct opp_table *opp_table,struct device *dev) -{ -/* 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 */ -_put_opp_list_kref (opp_table); - -/* -* The OPP table is getting removed, drop the performance state -* constraints. -*/ -if (opp_table->genpd_performance_state) -dev_pm_genpd_set_performance_state (dev,0 ); - }else { -_put_opp_list_kref (opp_table); -_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) {struct opp_table *opp_table;
@@ -1802,8 +1776,12 @@ void _dev_pm_opp_find_and_remove_table(struct device *dev)return ; } -_dev_pm_opp_remove_table (opp_table, dev); +_put_opp_list_kref (opp_table); + +/* Drop reference taken by _find_opp_table() */ +dev_pm_opp_put_opp_table (opp_table); +/* Drop reference taken while the OPP table was added */ dev_pm_opp_put_opp_table (opp_table); }
drivers/opp/of.c +18 -14
@@ -407,14 +407,17 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, opp_table =_managed_opp (opp_np);if (opp_table) {/* OPPs are already managed */ -if (!_add_opp_dev (dev, opp_table)) +if (!_add_opp_dev (dev, opp_table)) { ret = -ENOMEM ; -else if (!opp_table->parsed_static_opps) - goto initialize_static_opps; -else + goto put_opp_table; + } + +if (opp_table->parsed_static_opps) {kref_get (&opp_table->list_kref); +return 0 ; + } - goto put_opp_table; + goto initialize_static_opps; } opp_table =dev_pm_opp_get_opp_table_indexed (dev, index);
@@ -432,17 +435,15 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,if (ret) {dev_err (dev,"%s: Failed to add OPP, %d\n" , __func__, ret); -_dev_pm_opp_remove_table (opp_table, dev);of_node_put (np); - goto put_opp_table; + goto put_list_kref; } }/* There should be one of more OPP defined */ if (WARN_ON (!count)) { ret = -ENOENT ; -_put_opp_list_kref (opp_table); - goto put_opp_table; + goto put_list_kref; }list_for_each_entry (opp, &opp_table->opp_list, node)
@@ -453,8 +454,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,dev_err (dev,"Not all nodes have performance state set (%d: %d)\n" , count, pstate_count); ret = -ENOENT ; -_dev_pm_opp_remove_table (opp_table, dev); - goto put_opp_table; + goto put_list_kref; }if (pstate_count)
@@ -462,6 +462,10 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, opp_table->parsed_static_opps = true; +return 0 ; + +put_list_kref: +_put_opp_list_kref (opp_table); put_opp_table:dev_pm_opp_put_opp_table (opp_table);
@@ -507,13 +511,13 @@ static int _of_add_opp_table_v1(struct device *dev)if (ret) {dev_err (dev,"%s: Failed to add OPP %ld (%d)\n" , __func__, freq, ret); -_dev_pm_opp_remove_table (opp_table, dev); -break ; +_put_opp_list_kref (opp_table); +dev_pm_opp_put_opp_table (opp_table); +return ret; } nr -=2 ; } -dev_pm_opp_put_opp_table (opp_table);return ret; }
drivers/opp/opp.h +1 -1
@@ -190,11 +190,11 @@ struct opp_table {/* Routines internal to opp core */ void dev_pm_opp_get (struct dev_pm_opp *opp); +void _opp_remove_all_static (struct opp_table *opp_table);void _get_opp_table_kref (struct opp_table *opp_table);int _get_opp_count (struct opp_table *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 opp_table *opp_table,struct device *dev);void _dev_pm_opp_find_and_remove_table (struct device *dev);struct dev_pm_opp *_opp_allocate (struct opp_table *opp_table);void _opp_free (struct dev_pm_opp *opp);
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 10/11 OPP: Prevent creating multiple OPP tables for devices sharing OPP nodes
In reply to Viresh KumarWhen two or more devices are sharing their clock and voltage rails, they share the same OPP table. But there are some corner cases where the OPP core incorrectly creates separate OPP tables for them.
For example, CPU 0 and 1 share clock/voltage rails. The platform specific code calls dev_pm_opp_set_regulators() for CPU0 and the OPP core creates an OPP table for it (the individual OPPs aren't initialized as of now). The same is repeated for CPU1 then. Because _opp_get_opp_table() doesn't compare DT node pointers currently, it fails to find the link between CPU0 and CPU1 and so creates a new OPP table.
Fix this by calling _managed_opp() from _opp_get_opp_table(). _managed_opp() gain an additional argument (index) to get the right node pointer. This resulted in simplifying code in _of_add_opp_table_v2() as well.
--- drivers/opp/core.c | 25 ++++++++++++++++++++++--- drivers/opp/of.c | 35 +++++++++++++---------------------- drivers/opp/opp.h | 2 ++ 3 files changed, 37 insertions(+), 25 deletions(-)
drivers/opp/core.c +22 -3
@@ -759,8 +759,8 @@ static void _remove_opp_dev(struct opp_device *opp_dev,kfree (opp_dev); } -struct opp_device *_add_opp_dev (const struct device *dev, -struct opp_table *opp_table) +struct opp_device *_add_opp_dev_unlocked (const struct device *dev, +struct opp_table *opp_table) {struct opp_device *opp_dev;int ret;
@@ -772,7 +772,6 @@ 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 */
@@ -780,6 +779,17 @@ 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); + +return opp_dev; +} + +struct opp_device *_add_opp_dev (const struct device *dev, +struct opp_table *opp_table) +{ +struct opp_device *opp_dev; + +mutex_lock (&opp_table->lock); + opp_dev =_add_opp_dev_unlocked (dev, opp_table);mutex_unlock (&opp_table->lock);return opp_dev;
@@ -844,6 +854,15 @@ static struct opp_table *_opp_get_opp_table(struct device *dev, int index)if (!IS_ERR (opp_table)) goto unlock; + opp_table =_managed_opp (dev, index); +if (opp_table) { +if (!_add_opp_dev_unlocked (dev, opp_table)) { +dev_pm_opp_put_opp_table (opp_table); + opp_table =NULL ; + } + goto unlock; + } + opp_table =_allocate_opp_table (dev, index); unlock:
drivers/opp/of.c +13 -22
@@ -41,11 +41,14 @@ struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) }EXPORT_SYMBOL_GPL (dev_pm_opp_of_get_opp_desc_node); -static struct opp_table *_managed_opp (const struct device_node *np) +struct opp_table *_managed_opp (struct device *dev,int index) {struct opp_table *opp_table, *managed_table =NULL ; +struct device_node *np; -mutex_lock (&opp_table_lock); + np =_opp_of_get_opp_desc_node (dev->of_node, index); +if (!np) +return NULL ;list_for_each_entry (opp_table, &opp_tables, node) {if (opp_table->np == np) {
@@ -65,7 +68,7 @@ static struct opp_table *_managed_opp(const struct device_node *np) } } -mutex_unlock (&opp_table_lock); +of_node_put (np);return managed_table; }
@@ -401,30 +404,19 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, {struct device_node *np;struct opp_table *opp_table; -int ret =0 , count =0 , pstate_count =0 ; +int ret, count =0 , pstate_count =0 ;struct dev_pm_opp *opp; - opp_table =_managed_opp (opp_np); -if (opp_table) { -/* OPPs are already managed */ -if (!_add_opp_dev (dev, opp_table)) { - ret = -ENOMEM ; - goto put_opp_table; - } - -if (opp_table->parsed_static_opps) { -kref_get (&opp_table->list_kref); -return 0 ; - } - - goto initialize_static_opps; - } - opp_table =dev_pm_opp_get_opp_table_indexed (dev, index);if (!opp_table)return -ENOMEM ; -initialize_static_opps: +/* OPP table is already initialized for the device */ +if (opp_table->parsed_static_opps) { +kref_get (&opp_table->list_kref); +return 0 ; + } +kref_init (&opp_table->list_kref);/* We have opp-table node now, iterate over it and add OPPs */
@@ -466,7 +458,6 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, put_list_kref:_put_opp_list_kref (opp_table); -put_opp_table:dev_pm_opp_put_opp_table (opp_table);return ret;
drivers/opp/opp.h +2 -0
@@ -206,8 +206,10 @@ void _put_opp_list_kref(struct opp_table *opp_table);#ifdef CONFIG_OF void _of_init_opp_table (struct opp_table *opp_table,struct device *dev,int index); +struct opp_table *_managed_opp (struct device *dev,int index);#else static inline void _of_init_opp_table (struct opp_table *opp_table,struct device *dev,int index) {} +static inline struct opp_table *_managed_opp(struct device *dev, int index) { return NULL };#endif #ifdef CONFIG_DEBUG_FS
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerPATCH 11/11 OPP: Pass OPP table to _of_add_opp_table_v{1|2}()
In reply to Viresh KumarBoth _of_add_opp_table_v1() and _of_add_opp_table_v2() contain similar code to get the OPP table and their parent routine also parses the DT to find the OPP table's node pointer. This can be simplified by getting the OPP table in advance and then passing it as argument to these routines.
--- drivers/opp/of.c | 68 ++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 40 deletions(-)
drivers/opp/of.c +28 -40
@@ -399,18 +399,12 @@ static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev, }/* Initializes OPP tables based on new bindings */ -static int _of_add_opp_table_v2 (struct device *dev,struct device_node *opp_np, -int index) +static int _of_add_opp_table_v2 (struct device *dev,struct opp_table *opp_table) {struct device_node *np; -struct opp_table *opp_table;int ret, count =0 , pstate_count =0 ;struct dev_pm_opp *opp; - opp_table =dev_pm_opp_get_opp_table_indexed (dev, index); -if (!opp_table) -return -ENOMEM ; -/* OPP table is already initialized for the device */ if (opp_table->parsed_static_opps) {kref_get (&opp_table->list_kref);
@@ -420,7 +414,7 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np,kref_init (&opp_table->list_kref);/* We have opp-table node now, iterate over it and add OPPs */ -for_each_available_child_of_node (opp_np, np) { +for_each_available_child_of_node (opp_table->np, np) { count++; ret =_opp_add_static_v2 (opp_table, dev, np);
@@ -458,15 +452,13 @@ static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np, put_list_kref:_put_opp_list_kref (opp_table); -dev_pm_opp_put_opp_table (opp_table);return ret; }/* Initializes OPP tables based on old-deprecated bindings */ -static int _of_add_opp_table_v1 (struct device *dev) +static int _of_add_opp_table_v1 (struct device *dev,struct opp_table *opp_table) { -struct opp_table *opp_table;const struct property *prop;const __be32 *val;int nr, ret =0 ;
@@ -487,10 +479,6 @@ static int _of_add_opp_table_v1(struct device *dev)return -EINVAL ; } - opp_table =dev_pm_opp_get_opp_table (dev); -if (!opp_table) -return -ENOMEM ; -kref_init (&opp_table->list_kref); val = prop->value;
@@ -503,7 +491,6 @@ static int _of_add_opp_table_v1(struct device *dev)dev_err (dev,"%s: Failed to add OPP %ld (%d)\n" , __func__, freq, ret);_put_opp_list_kref (opp_table); -dev_pm_opp_put_opp_table (opp_table);return ret; } nr -=2 ;
@@ -531,24 +518,24 @@ static int _of_add_opp_table_v1(struct device *dev)*/ int dev_pm_opp_of_add_table (struct device *dev) { -struct device_node *opp_np; +struct opp_table *opp_table;int ret; + opp_table =dev_pm_opp_get_opp_table_indexed (dev,0 ); +if (!opp_table) +return -ENOMEM ; +/* -* OPPs have two version of bindings now. The older one is deprecated, -* try for the new binding first. +* OPPs have two version of bindings now. Also try the old (v1) +* bindings for backward compatibility with older dtbs. */ - opp_np =dev_pm_opp_of_get_opp_desc_node (dev); -if (!opp_np) { -/* -* Try old-deprecated bindings for backward compatibility with -* older dtbs. -*/ -return _of_add_opp_table_v1 (dev); - } +if (opp_table->np) + ret =_of_add_opp_table_v2 (dev, opp_table); +else + ret =_of_add_opp_table_v1 (dev, opp_table); - ret =_of_add_opp_table_v2 (dev, opp_np,0 ); -of_node_put (opp_np); +if (ret) +dev_pm_opp_put_opp_table (opp_table);return ret; }
@@ -575,28 +562,29 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);*/ int dev_pm_opp_of_add_table_indexed (struct device *dev,int index) { -struct device_node *opp_np; +struct opp_table *opp_table;int ret, count; -again: - opp_np =_opp_of_get_opp_desc_node (dev->of_node, index); -if (!opp_np) { +if (index) {/* * If only one phandle is present, then the same OPP table * applies for all index requests. */ count =of_count_phandle_with_args (dev->of_node,"operating-points-v2" ,NULL ); -if (count ==1 && index) { - index =0 ; - goto again; - } +if (count !=1 ) +return -ENODEV ; -return -ENODEV ; + index =0 ; } - ret =_of_add_opp_table_v2 (dev, opp_np, index); -of_node_put (opp_np); + opp_table =dev_pm_opp_get_opp_table_indexed (dev, index); +if (!opp_table) +return -ENOMEM ; + + ret =_of_add_opp_table_v2 (dev, opp_table); +if (ret) +dev_pm_opp_put_opp_table (opp_table);return ret; }
Signature · 1 line
2.18.0.rc1.242.g61856ae69a2c
Quoted 11 lines · Viresh Kumar
Hello,
Niklas Cassle recently reported some regressions with his Qcom cpufreq driver where he was getting some errors while creating the OPPs tables.
After looking into it I realized that the OPP core incorrectly creates multiple OPP tables for the devices even if they are sharing the OPP table in DT. This happens when the request comes using different CPU devices. For example,
dev_pm_opp_set_supported_hw()getting called using CPU0 anddev_pm_opp_of_add_table()getting called using CPU1.
Quoted 5 lines
This series redesigns the internals of the OPP core to fix that. The redesign has simplified the core itself though.
@Niklas: Can you please confirm that this series fixes the issues you have reported ? I have already tested it on Hikey960.
Hello Viresh,
This fixes the OPP error messages that I previously reported.
However, I also tested to add a duplicate OPP to the opp-table.
Before this series, I got the first two error prints.
After this series, I get the first two error prints + the use after free splat.
[ 5.693273] cpu cpu0: _opp_is_duplicate: duplicate OPPs detected. Existing: freq: 307200000, volt: 905000, enabled: 1. New: freq: 307200000, volt: 904000, enabled: 1 [ 5.695602] cpu cpu0: _of_add_opp_table_v2: Failed to add OPP, -17 [ 5.713673] ------------[ cut here ]------------ [ 5.715124] refcount_t: underflow; use-after-free. [ 5.720047] WARNING: CPU: 3 PID: 35 at lib/refcount.c:280 refcount_dec_not_one+0x9c/0xc0 [ 5.723463] Modules linked in: [ 5.731461] CPU: 3 PID: 35 Comm: kworker/3:1 Tainted: G W 4.19.0-rc3-00219-g 3f2e8f8e1fc5-dirty #63 [ 5.734688] Hardware name: Qualcomm Technologies, Inc. DB820c (DT) [ 5.744940] Workqueue: events deferred_probe_work_func [ 5.750810] pstate: 40000005 (nZcv daif -PAN -UAO) [ 5.755973] pc : refcount_dec_not_one+0x9c/0xc0 [ 5.760710] lr : refcount_dec_not_one+0x9c/0xc0 [ 5.765018] sp : ffff000009f8b3a0 [ 5.769469] x29: ffff000009f8b3a0 x28: 0000000000000000 [ 5.773052] x27: 0000000000000000 x26: 0000000000000001 [ 5.778428] x25: ffff8000d5347200 x24: ffff0000092f00e0 [ 5.783722] x23: ffff0000092efcf8 x22: ffff000008f5d250 [ 5.789023] x21: ffff0000094f9000 x20: ffff8000d5347264 [ 5.794313] x19: ffff000009637960 x18: ffffffffffffffff [ 5.799605] x17: 0000000000000000 x16: 0000000000000000 [ 5.804900] x15: ffff0000094f96c8 x14: 0720072007200720 [ 5.810199] x13: 0720072007200720 x12: 0720072007200720 [ 5.815491] x11: 0720072007200720 x10: 0720072007200720 [ 5.820799] x9 : 0720072007200720 x8 : 072007200720072e [ 5.826081] x7 : 0765076507720766 x6 : ffff8000da028f00 [ 5.831377] x5 : 0000000000000000 x4 : 0000000000000000 [ 5.836666] x3 : ffffffffffffffff x2 : ffff000009512480 [ 5.841971] x1 : a4c264660aaf4100 x0 : 0000000000000000 [ 5.847274] Call trace: [ 5.852544] refcount_dec_not_one+0x9c/0xc0 [ 5.854792] refcount_dec_and_mutex_lock+0x18/0x70 [ 5.859055] _put_opp_list_kref+0x28/0x50 [ 5.863822] _dev_pm_opp_find_and_remove_table+0x24/0x88 [ 5.867996] _dev_pm_opp_cpumask_remove_table+0x4c/0x98 [ 5.873369] dev_pm_opp_of_cpumask_add_table+0x98/0x100 [ 5.878315] cpufreq_init+0xe4/0x3a8 [ 5.883376] cpufreq_online+0xc4/0x6d0 [ 5.887186] cpufreq_add_dev+0xa8/0xb8 [ 5.890835] subsys_interface_register+0x9c/0x100 [ 5.894540] cpufreq_register_driver+0x190/0x278 [ 5.899344] dt_cpufreq_probe+0xa0/0x1e0 [ 5.903969] platform_drv_probe+0x50/0xa0 [ 5.907840] really_probe+0x260/0x3b8 [ 5.911720] driver_probe_device+0x5c/0x148 [ 5.915398] __device_attach_driver+0xa8/0x160 [ 5.919456] bus_for_each_drv+0x64/0xc8 [ 5.923875] __device_attach+0xd8/0x158 [ 5.927625] device_initial_probe+0x10/0x18 [ 5.931450] bus_probe_device+0x90/0x98 [ 5.935650] device_add+0x440/0x668 [ 5.939416] platform_device_add+0x124/0x2d0 [ 5.942977] platform_device_register_full+0xf8/0x118 [ 5.947571] qcom_cpufreq_kryo_probe+0x27c/0x320 [ 5.952445] platform_drv_probe+0x50/0xa0 [ 5.957066] really_probe+0x260/0x3b8 [ 5.960939] driver_probe_device+0x5c/0x148 [ 5.964612] __device_attach_driver+0xa8/0x160 [ 5.968687] bus_for_each_drv+0x64/0xc8 [ 5.973086] __device_attach+0xd8/0x158 [ 5.976831] device_initial_probe+0x10/0x18 [ 5.980657] bus_probe_device+0x90/0x98 [ 5.984824] deferred_probe_work_func+0x88/0xe0 [ 5.988801] process_one_work+0x1e0/0x318 [ 5.993243] worker_thread+0x228/0x450 [ 5.997345] kthread+0x128/0x130 [ 6.000973] ret_from_fork+0x10/0x18 [ 6.004313] ---[ end trace 5715d70f8f823685 ]---
Kind regards,
Niklas
Quoted 29 lines
--
vireshViresh Kumar (11):
OPP: Free OPP table properly on performance state irregularities
OPP: Protectdev_listwithopp_tablelock
OPP: Pass index to_of_init_opp_table()
OPP: Parse OPP table's DT properties from_of_init_opp_table()
OPP: Don't take OPP table's kref for static OPPs
OPP: Create separate kref for static OPPs list
cpufreq: mvebu: Remove OPPs usingdev_pm_opp_remove()
OPP: Don't remove dynamic OPPs from_dev_pm_opp_remove_table()
OPP: Use a single mechanism to free the OPP table
OPP: Prevent creating multiple OPP tables for devices sharing OPP
nodes
OPP: Pass OPP table to _of_add_opp_table_v{1|2}()drivers/cpufreq/mvebu-cpufreq.c | 9 +- drivers/opp/core.c | 147 ++++++++++++++++--------- drivers/opp/cpu.c | 11 +- drivers/opp/of.c | 186 +++++++++++++++++--------------- drivers/opp/opp.h | 19 ++-- include/linux/pm_opp.h | 6 ++ 6 files changed, 221 insertions(+), 157 deletions(-)--
2.18.0.rc1.242.g61856ae69a2c
Viresh Kumar
maintainerQuoted 18 lines · Niklas Cassel
Quoted 11 lines · Viresh Kumar
Hello,
Niklas Cassle recently reported some regressions with his Qcom cpufreq driver where he was getting some errors while creating the OPPs tables.
After looking into it I realized that the OPP core incorrectly creates multiple OPP tables for the devices even if they are sharing the OPP table in DT. This happens when the request comes using different CPU devices. For example,
dev_pm_opp_set_supported_hw()getting called using CPU0 anddev_pm_opp_of_add_table()getting called using CPU1.Quoted 5 lines
This series redesigns the internals of the OPP core to fix that. The redesign has simplified the core itself though.
@Niklas: Can you please confirm that this series fixes the issues you have reported ? I have already tested it on Hikey960.
Quoted 8 lines
Hello Viresh,
This fixes the OPP error messages that I previously reported.
However, I also tested to add a duplicate OPP to the opp-table.
Before this series, I got the first two error prints.
After this series, I get the first two error prints + the use after free splat.
This looks to be an old bug. Can you please try this branch:
git://git.kernel.org/pub/scm/linux/kernel/git/vireshk/pm.git opp/qcom-fix
?
Niklas Cassel
Tested-byQuoted 21 lines · Viresh Kumar
Quoted 20 lines · Niklas Cassel
Quoted 11 lines · Viresh Kumar
Hello,
Niklas Cassle recently reported some regressions with his Qcom cpufreq driver where he was getting some errors while creating the OPPs tables.
After looking into it I realized that the OPP core incorrectly creates multiple OPP tables for the devices even if they are sharing the OPP table in DT. This happens when the request comes using different CPU devices. For example,
dev_pm_opp_set_supported_hw()getting called using CPU0 anddev_pm_opp_of_add_table()getting called using CPU1.Quoted 5 lines
This series redesigns the internals of the OPP core to fix that. The redesign has simplified the core itself though.
@Niklas: Can you please confirm that this series fixes the issues you have reported ? I have already tested it on Hikey960.
Hello Viresh,
Quoted 8 lines
Quoted 6 lines
This fixes the OPP error messages that I previously reported.
However, I also tested to add a duplicate OPP to the opp-table.
Before this series, I got the first two error prints.
After this series, I get the first two error prints + the use after free splat.This looks to be an old bug. Can you please try this branch:
Hello Viresh,
You confused me here, since you did hide the fix for this old bug in the middle of your new patch series :)
I think that it would have been more obvious to simply paste the fix/diff in your reply directly, since that is the most common way to post a potential fix. Or, if you are really confident in your fix, post a V2 directly.
However, your branch works like a charm, so feel free to add:
when sending out your branch as a V2.
Kind regards,
Niklas
Quoted 4 lines
git://git.kernel.org/pub/scm/linux/kernel/git/vireshk/pm.gitopp/qcom-fix?
Viresh Kumar
maintainerQuoted 2 lines · Niklas Cassel
You confused me here, since you did hide the fix for this old bug in the middle of your new patch series :)
Actually I had to place the fix at the beginning of the series and that caused git rebase to have some conflicts. And so never posted the diff.
Quoted 4 lines
I think that it would have been more obvious to simply paste the fix/diff in your reply directly, since that is the most common way to post a potential fix. Or, if you are really confident in your fix, post a V2 directly.
I will post the v2 now.
Quoted 3 lines
However, your branch works like a charm, so feel free to add:
Tested-by Niklas Cassel <[email protected]>when sending out your branch as a V2.
Thanks.
Here is the new commit though which I added to this series:
-------------------------8<-------------------------
From: Viresh Kumar <[email protected]>
Date: Thu, 13 Sep 2018 13:09:27 +0530
Subject: [PATCH] OPP: Don't try to remove all OPP tables on failure
dev_pm_opp_of_cpumask_add_table() creates the OPP table for all CPUs present in the cpumask and on errors it should revert all changes it has done.
It actually is doing a bit more than that. On errors, it tries to free all the OPP tables, even the one it hasn't created yet. This may also end up freeing the OPP tables which were created from separate path, like dev_pm_opp_set_supported_hw().
--- drivers/opp/cpu.c | 8 ++++++-- drivers/opp/of.c | 4 ++-- drivers/opp/opp.h | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-)
drivers/opp/cpu.c +6 -2
@@ -108,7 +108,8 @@ void dev_pm_opp_free_cpufreq_table(struct device *dev,EXPORT_SYMBOL_GPL (dev_pm_opp_free_cpufreq_table);#endif /* CONFIG_CPU_FREQ */ -void _dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask,bool of) +void _dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask,bool of, +int last_cpu) {struct device *cpu_dev;int cpu;
@@ -116,6 +117,9 @@ void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)WARN_ON (cpumask_empty (cpumask));for_each_cpu (cpu, cpumask) { +if (cpu == last_cpu) +break ; + cpu_dev =get_cpu_device (cpu);if (!cpu_dev) { pr_err("%s: failed to get cpu%d device\n", __func__,
@@ -140,7 +144,7 @@ void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of)*/ void dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask) { -_dev_pm_opp_cpumask_remove_table (cpumask, false); +_dev_pm_opp_cpumask_remove_table (cpumask, false,-1 ); }EXPORT_SYMBOL_GPL (dev_pm_opp_cpumask_remove_table);
drivers/opp/of.c +2 -2
@@ -592,7 +592,7 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed);*/ void dev_pm_opp_of_cpumask_remove_table (const struct cpumask *cpumask) { -_dev_pm_opp_cpumask_remove_table (cpumask, true); +_dev_pm_opp_cpumask_remove_table (cpumask, true,-1 ); }EXPORT_SYMBOL_GPL (dev_pm_opp_of_cpumask_remove_table);
@@ -627,7 +627,7 @@ int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) __func__, cpu, ret);/* Free all other OPPs */ -dev_pm_opp_of_cpumask_remove_table (cpumask); +_dev_pm_opp_cpumask_remove_table (cpumask, true, cpu);break ; } }
drivers/opp/opp.h +1 -1
@@ -196,7 +196,7 @@ 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,bool rate_not_available);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); +void _dev_pm_opp_cpumask_remove_table (const struct cpumask *cpumask,bool of,int last_cpu);struct opp_table *_add_opp_table (struct device *dev);#ifdef CONFIG_OF
Signature · 1 line
viresh