···7979specific framework which uses the OPP library. Similar care needs to be taken8080care to refresh the cpufreq table in cases of these operations.81818282-WARNING on OPP List locking mechanism:8383--------------------------------------------------8484-OPP library uses RCU for exclusivity. RCU allows the query functions to operate8585-in multiple contexts and this synchronization mechanism is optimal for a read8686-intensive operations on data structure as the OPP library caters to.8787-8888-To ensure that the data retrieved are sane, the users such as SoC framework8989-should ensure that the section of code operating on OPP queries are locked9090-using RCU read locks. The opp_find_freq_{exact,ceil,floor},9191-opp_get_{voltage, freq, opp_count} fall into this category.9292-9393-opp_{add,enable,disable} are updaters which use mutex and implement it's own9494-RCU locking mechanisms. These functions should *NOT* be called under RCU locks9595-and other contexts that prevent blocking functions in RCU or mutex operations9696-from working.9797-98822. Initial OPP List Registration9983================================10084The SoC implementation calls dev_pm_opp_add function iteratively to add OPPs per···121137found, else returns error. These errors are expected to be handled by standard122138error checks such as IS_ERR() and appropriate actions taken by the caller.123139140140+Callers of these functions shall call dev_pm_opp_put() after they have used the141141+OPP. Otherwise the memory for the OPP will never get freed and result in142142+memleak.143143+124144dev_pm_opp_find_freq_exact - Search for an OPP based on an *exact* frequency and125145 availability. This function is especially useful to enable an OPP which126146 is not available by default.127147 Example: In a case when SoC framework detects a situation where a128148 higher frequency could be made available, it can use this function to129149 find the OPP prior to call the dev_pm_opp_enable to actually make it available.130130- rcu_read_lock();131150 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);132132- rcu_read_unlock();151151+ dev_pm_opp_put(opp);133152 /* dont operate on the pointer.. just do a sanity check.. */134153 if (IS_ERR(opp)) {135154 pr_err("frequency not disabled!\n");···150163 frequency.151164 Example: To find the highest opp for a device:152165 freq = ULONG_MAX;153153- rcu_read_lock();154154- dev_pm_opp_find_freq_floor(dev, &freq);155155- rcu_read_unlock();166166+ opp = dev_pm_opp_find_freq_floor(dev, &freq);167167+ dev_pm_opp_put(opp);156168157169dev_pm_opp_find_freq_ceil - Search for an available OPP which is *at least* the158170 provided frequency. This function is useful while searching for a···159173 frequency.160174 Example 1: To find the lowest opp for a device:161175 freq = 0;162162- rcu_read_lock();163163- dev_pm_opp_find_freq_ceil(dev, &freq);164164- rcu_read_unlock();176176+ opp = dev_pm_opp_find_freq_ceil(dev, &freq);177177+ dev_pm_opp_put(opp);165178 Example 2: A simplified implementation of a SoC cpufreq_driver->target:166179 soc_cpufreq_target(..)167180 {168181 /* Do stuff like policy checks etc. */169182 /* Find the best frequency match for the req */170170- rcu_read_lock();171183 opp = dev_pm_opp_find_freq_ceil(dev, &freq);172172- rcu_read_unlock();184184+ dev_pm_opp_put(opp);173185 if (!IS_ERR(opp))174186 soc_switch_to_freq_voltage(freq);175187 else···192208 implementation might choose to do something as follows:193209 if (cur_temp < temp_low_thresh) {194210 /* Enable 1GHz if it was disabled */195195- rcu_read_lock();196211 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false);197197- rcu_read_unlock();212212+ dev_pm_opp_put(opp);198213 /* just error check */199214 if (!IS_ERR(opp))200215 ret = dev_pm_opp_enable(dev, 1000000000);···207224 choose to do something as follows:208225 if (cur_temp > temp_high_thresh) {209226 /* Disable 1GHz if it was enabled */210210- rcu_read_lock();211227 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, true);212212- rcu_read_unlock();228228+ dev_pm_opp_put(opp);213229 /* just error check */214230 if (!IS_ERR(opp))215231 ret = dev_pm_opp_disable(dev, 1000000000);···231249 soc_switch_to_freq_voltage(freq)232250 {233251 /* do things */234234- rcu_read_lock();235252 opp = dev_pm_opp_find_freq_ceil(dev, &freq);236253 v = dev_pm_opp_get_voltage(opp);237237- rcu_read_unlock();254254+ dev_pm_opp_put(opp);238255 if (v)239256 regulator_set_voltage(.., v);240257 /* do other things */···247266 {248267 /* do things.. */249268 max_freq = ULONG_MAX;250250- rcu_read_lock();251269 max_opp = dev_pm_opp_find_freq_floor(dev,&max_freq);252270 requested_opp = dev_pm_opp_find_freq_ceil(dev,&freq);253271 if (!IS_ERR(max_opp) && !IS_ERR(requested_opp))254272 r = soc_test_validity(max_opp, requested_opp);255255- rcu_read_unlock();273273+ dev_pm_opp_put(max_opp);274274+ dev_pm_opp_put(requested_opp);256275 /* do other things */257276 }258277 soc_test_validity(..)···270289 soc_notify_coproc_available_frequencies()271290 {272291 /* Do things */273273- rcu_read_lock();274292 num_available = dev_pm_opp_get_opp_count(dev);275293 speeds = kzalloc(sizeof(u32) * num_available, GFP_KERNEL);276294 /* populate the table in increasing order */···278298 speeds[i] = freq;279299 freq++;280300 i++;301301+ dev_pm_opp_put(opp);281302 }282282- rcu_read_unlock();283303284304 soc_notify_coproc(AVAILABLE_FREQs, speeds, num_available);285305 /* Do other things */
+2-3
arch/arm/mach-omap2/pm.c
···130130 freq = clk_get_rate(clk);131131 clk_put(clk);132132133133- rcu_read_lock();134133 opp = dev_pm_opp_find_freq_ceil(dev, &freq);135134 if (IS_ERR(opp)) {136136- rcu_read_unlock();137135 pr_err("%s: unable to find boot up OPP for vdd_%s\n",138136 __func__, vdd_name);139137 goto exit;140138 }141139142140 bootup_volt = dev_pm_opp_get_voltage(opp);143143- rcu_read_unlock();141141+ dev_pm_opp_put(opp);142142+144143 if (!bootup_volt) {145144 pr_err("%s: unable to find voltage corresponding to the bootup OPP for vdd_%s\n",146145 __func__, vdd_name);
+389-632
drivers/base/power/opp/core.c
···3232/* Lock to allow exclusive modification to the device and opp lists */3333DEFINE_MUTEX(opp_table_lock);34343535-#define opp_rcu_lockdep_assert() \3636-do { \3737- RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \3838- !lockdep_is_held(&opp_table_lock), \3939- "Missing rcu_read_lock() or " \4040- "opp_table_lock protection"); \4141-} while (0)3535+static void dev_pm_opp_get(struct dev_pm_opp *opp);42364337static struct opp_device *_find_opp_dev(const struct device *dev,4438 struct opp_table *opp_table)···4652 return NULL;4753}48545555+static struct opp_table *_find_opp_table_unlocked(struct device *dev)5656+{5757+ struct opp_table *opp_table;5858+5959+ list_for_each_entry(opp_table, &opp_tables, node) {6060+ if (_find_opp_dev(dev, opp_table)) {6161+ _get_opp_table_kref(opp_table);6262+6363+ return opp_table;6464+ }6565+ }6666+6767+ return ERR_PTR(-ENODEV);6868+}6969+4970/**5071 * _find_opp_table() - find opp_table struct using device pointer5172 * @dev: device pointer used to lookup OPP table5273 *5353- * Search OPP table for one containing matching device. Does a RCU reader5454- * operation to grab the pointer needed.7474+ * Search OPP table for one containing matching device.5575 *5676 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or5777 * -EINVAL based on type of error.5878 *5959- * Locking: For readers, this function must be called under rcu_read_lock().6060- * opp_table is a RCU protected pointer, which means that opp_table is valid6161- * as long as we are under RCU lock.6262- *6363- * For Writers, this function must be called with opp_table_lock held.7979+ * The callers must call dev_pm_opp_put_opp_table() after the table is used.6480 */6581struct opp_table *_find_opp_table(struct device *dev)6682{6783 struct opp_table *opp_table;6868-6969- opp_rcu_lockdep_assert();70847185 if (IS_ERR_OR_NULL(dev)) {7286 pr_err("%s: Invalid parameters\n", __func__);7387 return ERR_PTR(-EINVAL);7488 }75897676- list_for_each_entry_rcu(opp_table, &opp_tables, node)7777- if (_find_opp_dev(dev, opp_table))7878- return opp_table;9090+ mutex_lock(&opp_table_lock);9191+ opp_table = _find_opp_table_unlocked(dev);9292+ mutex_unlock(&opp_table_lock);79938080- return ERR_PTR(-ENODEV);9494+ return opp_table;8195}82968397/**···9694 * return 09795 *9896 * This is useful only for devices with single power supply.9999- *100100- * Locking: This function must be called under rcu_read_lock(). opp is a rcu101101- * protected pointer. This means that opp which could have been fetched by102102- * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are103103- * under RCU lock. The pointer returned by the opp_find_freq family must be104104- * used in the same section as the usage of this function with the pointer105105- * prior to unlocking with rcu_read_unlock() to maintain the integrity of the106106- * pointer.10797 */10898unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)10999{110110- struct dev_pm_opp *tmp_opp;111111- unsigned long v = 0;112112-113113- opp_rcu_lockdep_assert();114114-115115- tmp_opp = rcu_dereference(opp);116116- if (IS_ERR_OR_NULL(tmp_opp))100100+ if (IS_ERR_OR_NULL(opp)) {117101 pr_err("%s: Invalid parameters\n", __func__);118118- else119119- v = tmp_opp->supplies[0].u_volt;102102+ return 0;103103+ }120104121121- return v;105105+ return opp->supplies[0].u_volt;122106}123107EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);124108···114126 *115127 * Return: frequency in hertz corresponding to the opp, else116128 * return 0117117- *118118- * Locking: This function must be called under rcu_read_lock(). opp is a rcu119119- * protected pointer. This means that opp which could have been fetched by120120- * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are121121- * under RCU lock. The pointer returned by the opp_find_freq family must be122122- * used in the same section as the usage of this function with the pointer123123- * prior to unlocking with rcu_read_unlock() to maintain the integrity of the124124- * pointer.125129 */126130unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)127131{128128- struct dev_pm_opp *tmp_opp;129129- unsigned long f = 0;130130-131131- opp_rcu_lockdep_assert();132132-133133- tmp_opp = rcu_dereference(opp);134134- if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)132132+ if (IS_ERR_OR_NULL(opp) || !opp->available) {135133 pr_err("%s: Invalid parameters\n", __func__);136136- else137137- f = tmp_opp->rate;134134+ return 0;135135+ }138136139139- return f;137137+ return opp->rate;140138}141139EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);142140···135161 * quickly. Running on them for longer times may overheat the chip.136162 *137163 * Return: true if opp is turbo opp, else false.138138- *139139- * Locking: This function must be called under rcu_read_lock(). opp is a rcu140140- * protected pointer. This means that opp which could have been fetched by141141- * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are142142- * under RCU lock. The pointer returned by the opp_find_freq family must be143143- * used in the same section as the usage of this function with the pointer144144- * prior to unlocking with rcu_read_unlock() to maintain the integrity of the145145- * pointer.146164 */147165bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)148166{149149- struct dev_pm_opp *tmp_opp;150150-151151- opp_rcu_lockdep_assert();152152-153153- tmp_opp = rcu_dereference(opp);154154- if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {167167+ if (IS_ERR_OR_NULL(opp) || !opp->available) {155168 pr_err("%s: Invalid parameters\n", __func__);156169 return false;157170 }158171159159- return tmp_opp->turbo;172172+ return opp->turbo;160173}161174EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);162175···152191 * @dev: device for which we do this operation153192 *154193 * Return: This function returns the max clock latency in nanoseconds.155155- *156156- * Locking: This function takes rcu_read_lock().157194 */158195unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)159196{160197 struct opp_table *opp_table;161198 unsigned long clock_latency_ns;162199163163- rcu_read_lock();164164-165200 opp_table = _find_opp_table(dev);166201 if (IS_ERR(opp_table))167167- clock_latency_ns = 0;168168- else169169- clock_latency_ns = opp_table->clock_latency_ns_max;202202+ return 0;170203171171- rcu_read_unlock();204204+ clock_latency_ns = opp_table->clock_latency_ns_max;205205+206206+ dev_pm_opp_put_opp_table(opp_table);207207+172208 return clock_latency_ns;173209}174210EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);175175-176176-static int _get_regulator_count(struct device *dev)177177-{178178- struct opp_table *opp_table;179179- int count;180180-181181- rcu_read_lock();182182-183183- opp_table = _find_opp_table(dev);184184- if (!IS_ERR(opp_table))185185- count = opp_table->regulator_count;186186- else187187- count = 0;188188-189189- rcu_read_unlock();190190-191191- return count;192192-}193211194212/**195213 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds196214 * @dev: device for which we do this operation197215 *198216 * Return: This function returns the max voltage latency in nanoseconds.199199- *200200- * Locking: This function takes rcu_read_lock().201217 */202218unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)203219{···188250 unsigned long max;189251 } *uV;190252191191- count = _get_regulator_count(dev);253253+ opp_table = _find_opp_table(dev);254254+ if (IS_ERR(opp_table))255255+ return 0;256256+257257+ count = opp_table->regulator_count;192258193259 /* Regulator may not be required for the device */194260 if (!count)195195- return 0;261261+ goto put_opp_table;196262197263 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);198264 if (!regulators)199199- return 0;265265+ goto put_opp_table;200266201267 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);202268 if (!uV)203269 goto free_regulators;204270205205- rcu_read_lock();206206-207207- opp_table = _find_opp_table(dev);208208- if (IS_ERR(opp_table)) {209209- rcu_read_unlock();210210- goto free_uV;211211- }212212-213271 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));272272+273273+ mutex_lock(&opp_table->lock);214274215275 for (i = 0; i < count; i++) {216276 uV[i].min = ~0;217277 uV[i].max = 0;218278219219- list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {279279+ list_for_each_entry(opp, &opp_table->opp_list, node) {220280 if (!opp->available)221281 continue;222282···225289 }226290 }227291228228- rcu_read_unlock();292292+ mutex_unlock(&opp_table->lock);229293230294 /*231295 * The caller needs to ensure that opp_table (and hence the regulator)···237301 latency_ns += ret * 1000;238302 }239303240240-free_uV:241304 kfree(uV);242305free_regulators:243306 kfree(regulators);307307+put_opp_table:308308+ dev_pm_opp_put_opp_table(opp_table);244309245310 return latency_ns;246311}···254317 *255318 * Return: This function returns the max transition latency, in nanoseconds, to256319 * switch from one OPP to other.257257- *258258- * Locking: This function takes rcu_read_lock().259320 */260321unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)261322{···263328EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);264329265330/**266266- * dev_pm_opp_get_suspend_opp() - Get suspend opp331331+ * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz267332 * @dev: device for which we do this operation268333 *269269- * Return: This function returns pointer to the suspend opp if it is270270- * defined and available, otherwise it returns NULL.271271- *272272- * Locking: This function must be called under rcu_read_lock(). opp is a rcu273273- * protected pointer. The reason for the same is that the opp pointer which is274274- * returned will remain valid for use with opp_get_{voltage, freq} only while275275- * under the locked area. The pointer returned must be used prior to unlocking276276- * with rcu_read_unlock() to maintain the integrity of the pointer.334334+ * Return: This function returns the frequency of the OPP marked as suspend_opp335335+ * if one is available, else returns 0;277336 */278278-struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)337337+unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)279338{280339 struct opp_table *opp_table;281281-282282- opp_rcu_lockdep_assert();340340+ unsigned long freq = 0;283341284342 opp_table = _find_opp_table(dev);285285- if (IS_ERR(opp_table) || !opp_table->suspend_opp ||286286- !opp_table->suspend_opp->available)287287- return NULL;343343+ if (IS_ERR(opp_table))344344+ return 0;288345289289- return opp_table->suspend_opp;346346+ if (opp_table->suspend_opp && opp_table->suspend_opp->available)347347+ freq = dev_pm_opp_get_freq(opp_table->suspend_opp);348348+349349+ dev_pm_opp_put_opp_table(opp_table);350350+351351+ return freq;290352}291291-EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);353353+EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);292354293355/**294356 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table···293361 *294362 * Return: This function returns the number of available opps if there are any,295363 * else returns 0 if none or the corresponding error value.296296- *297297- * Locking: This function takes rcu_read_lock().298364 */299365int dev_pm_opp_get_opp_count(struct device *dev)300366{···300370 struct dev_pm_opp *temp_opp;301371 int count = 0;302372303303- rcu_read_lock();304304-305373 opp_table = _find_opp_table(dev);306374 if (IS_ERR(opp_table)) {307375 count = PTR_ERR(opp_table);308376 dev_err(dev, "%s: OPP table not found (%d)\n",309377 __func__, count);310310- goto out_unlock;378378+ return count;311379 }312380313313- list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {381381+ mutex_lock(&opp_table->lock);382382+383383+ list_for_each_entry(temp_opp, &opp_table->opp_list, node) {314384 if (temp_opp->available)315385 count++;316386 }317387318318-out_unlock:319319- rcu_read_unlock();388388+ mutex_unlock(&opp_table->lock);389389+ dev_pm_opp_put_opp_table(opp_table);390390+320391 return count;321392}322393EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);···342411 * This provides a mechanism to enable an opp which is not available currently343412 * or the opposite as well.344413 *345345- * Locking: This function must be called under rcu_read_lock(). opp is a rcu346346- * protected pointer. The reason for the same is that the opp pointer which is347347- * returned will remain valid for use with opp_get_{voltage, freq} only while348348- * under the locked area. The pointer returned must be used prior to unlocking349349- * with rcu_read_unlock() to maintain the integrity of the pointer.414414+ * The callers are required to call dev_pm_opp_put() for the returned OPP after415415+ * use.350416 */351417struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,352418 unsigned long freq,···351423{352424 struct opp_table *opp_table;353425 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);354354-355355- opp_rcu_lockdep_assert();356426357427 opp_table = _find_opp_table(dev);358428 if (IS_ERR(opp_table)) {···360434 return ERR_PTR(r);361435 }362436363363- list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {437437+ mutex_lock(&opp_table->lock);438438+439439+ list_for_each_entry(temp_opp, &opp_table->opp_list, node) {364440 if (temp_opp->available == available &&365441 temp_opp->rate == freq) {366442 opp = temp_opp;443443+444444+ /* Increment the reference count of OPP */445445+ dev_pm_opp_get(opp);367446 break;368447 }369448 }449449+450450+ mutex_unlock(&opp_table->lock);451451+ dev_pm_opp_put_opp_table(opp_table);370452371453 return opp;372454}···385451{386452 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);387453388388- list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {454454+ mutex_lock(&opp_table->lock);455455+456456+ list_for_each_entry(temp_opp, &opp_table->opp_list, node) {389457 if (temp_opp->available && temp_opp->rate >= *freq) {390458 opp = temp_opp;391459 *freq = opp->rate;460460+461461+ /* Increment the reference count of OPP */462462+ dev_pm_opp_get(opp);392463 break;393464 }394465 }466466+467467+ mutex_unlock(&opp_table->lock);395468396469 return opp;397470}···418477 * ERANGE: no match found for search419478 * ENODEV: if device not found in list of registered devices420479 *421421- * Locking: This function must be called under rcu_read_lock(). opp is a rcu422422- * protected pointer. The reason for the same is that the opp pointer which is423423- * returned will remain valid for use with opp_get_{voltage, freq} only while424424- * under the locked area. The pointer returned must be used prior to unlocking425425- * with rcu_read_unlock() to maintain the integrity of the pointer.480480+ * The callers are required to call dev_pm_opp_put() for the returned OPP after481481+ * use.426482 */427483struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,428484 unsigned long *freq)429485{430486 struct opp_table *opp_table;431431-432432- opp_rcu_lockdep_assert();487487+ struct dev_pm_opp *opp;433488434489 if (!dev || !freq) {435490 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);···436499 if (IS_ERR(opp_table))437500 return ERR_CAST(opp_table);438501439439- return _find_freq_ceil(opp_table, freq);502502+ opp = _find_freq_ceil(opp_table, freq);503503+504504+ dev_pm_opp_put_opp_table(opp_table);505505+506506+ return opp;440507}441508EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);442509···459518 * ERANGE: no match found for search460519 * ENODEV: if device not found in list of registered devices461520 *462462- * Locking: This function must be called under rcu_read_lock(). opp is a rcu463463- * protected pointer. The reason for the same is that the opp pointer which is464464- * returned will remain valid for use with opp_get_{voltage, freq} only while465465- * under the locked area. The pointer returned must be used prior to unlocking466466- * with rcu_read_unlock() to maintain the integrity of the pointer.521521+ * The callers are required to call dev_pm_opp_put() for the returned OPP after522522+ * use.467523 */468524struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,469525 unsigned long *freq)470526{471527 struct opp_table *opp_table;472528 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);473473-474474- opp_rcu_lockdep_assert();475529476530 if (!dev || !freq) {477531 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);···477541 if (IS_ERR(opp_table))478542 return ERR_CAST(opp_table);479543480480- list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {544544+ mutex_lock(&opp_table->lock);545545+546546+ list_for_each_entry(temp_opp, &opp_table->opp_list, node) {481547 if (temp_opp->available) {482548 /* go to the next node, before choosing prev */483549 if (temp_opp->rate > *freq)···488550 opp = temp_opp;489551 }490552 }553553+554554+ /* Increment the reference count of OPP */555555+ if (!IS_ERR(opp))556556+ dev_pm_opp_get(opp);557557+ mutex_unlock(&opp_table->lock);558558+ dev_pm_opp_put_opp_table(opp_table);559559+491560 if (!IS_ERR(opp))492561 *freq = opp->rate;493562494563 return opp;495564}496565EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);497497-498498-/*499499- * The caller needs to ensure that opp_table (and hence the clk) isn't freed,500500- * while clk returned here is used.501501- */502502-static struct clk *_get_opp_clk(struct device *dev)503503-{504504- struct opp_table *opp_table;505505- struct clk *clk;506506-507507- rcu_read_lock();508508-509509- opp_table = _find_opp_table(dev);510510- if (IS_ERR(opp_table)) {511511- dev_err(dev, "%s: device opp doesn't exist\n", __func__);512512- clk = ERR_CAST(opp_table);513513- goto unlock;514514- }515515-516516- clk = opp_table->clk;517517- if (IS_ERR(clk))518518- dev_err(dev, "%s: No clock available for the device\n",519519- __func__);520520-521521-unlock:522522- rcu_read_unlock();523523- return clk;524524-}525566526567static int _set_opp_voltage(struct device *dev, struct regulator *reg,527568 struct dev_pm_opp_supply *supply)···597680 *598681 * This configures the power-supplies and clock source to the levels specified599682 * by the OPP corresponding to the target_freq.600600- *601601- * Locking: This function takes rcu_read_lock().602683 */603684int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)604685{···615700 return -EINVAL;616701 }617702618618- clk = _get_opp_clk(dev);619619- if (IS_ERR(clk))620620- return PTR_ERR(clk);703703+ opp_table = _find_opp_table(dev);704704+ if (IS_ERR(opp_table)) {705705+ dev_err(dev, "%s: device opp doesn't exist\n", __func__);706706+ return PTR_ERR(opp_table);707707+ }708708+709709+ clk = opp_table->clk;710710+ if (IS_ERR(clk)) {711711+ dev_err(dev, "%s: No clock available for the device\n",712712+ __func__);713713+ ret = PTR_ERR(clk);714714+ goto put_opp_table;715715+ }621716622717 freq = clk_round_rate(clk, target_freq);623718 if ((long)freq <= 0)···639714 if (old_freq == freq) {640715 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",641716 __func__, freq);642642- return 0;643643- }644644-645645- rcu_read_lock();646646-647647- opp_table = _find_opp_table(dev);648648- if (IS_ERR(opp_table)) {649649- dev_err(dev, "%s: device opp doesn't exist\n", __func__);650650- rcu_read_unlock();651651- return PTR_ERR(opp_table);717717+ ret = 0;718718+ goto put_opp_table;652719 }653720654721 old_opp = _find_freq_ceil(opp_table, &old_freq);···654737 ret = PTR_ERR(opp);655738 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",656739 __func__, freq, ret);657657- rcu_read_unlock();658658- return ret;740740+ goto put_old_opp;659741 }660742661743 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,···664748665749 /* Only frequency scaling */666750 if (!regulators) {667667- rcu_read_unlock();668668- return _generic_set_opp_clk_only(dev, clk, old_freq, freq);751751+ ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);752752+ goto put_opps;669753 }670754671755 if (opp_table->set_opp)···689773 data->new_opp.rate = freq;690774 memcpy(data->new_opp.supplies, opp->supplies, size);691775692692- rcu_read_unlock();776776+ ret = set_opp(data);693777694694- return set_opp(data);778778+put_opps:779779+ dev_pm_opp_put(opp);780780+put_old_opp:781781+ if (!IS_ERR(old_opp))782782+ dev_pm_opp_put(old_opp);783783+put_opp_table:784784+ dev_pm_opp_put_opp_table(opp_table);785785+ return ret;695786}696787EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);697788698789/* OPP-dev Helpers */699699-static void _kfree_opp_dev_rcu(struct rcu_head *head)700700-{701701- struct opp_device *opp_dev;702702-703703- opp_dev = container_of(head, struct opp_device, rcu_head);704704- kfree_rcu(opp_dev, rcu_head);705705-}706706-707790static void _remove_opp_dev(struct opp_device *opp_dev,708791 struct opp_table *opp_table)709792{710793 opp_debug_unregister(opp_dev, opp_table);711794 list_del(&opp_dev->node);712712- call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,713713- _kfree_opp_dev_rcu);795795+ kfree(opp_dev);714796}715797716798struct opp_device *_add_opp_dev(const struct device *dev,···723809724810 /* Initialize opp-dev */725811 opp_dev->dev = dev;726726- list_add_rcu(&opp_dev->node, &opp_table->dev_list);812812+ list_add(&opp_dev->node, &opp_table->dev_list);727813728814 /* Create debugfs entries for the opp_table */729815 ret = opp_debug_register(opp_dev, opp_table);···734820 return opp_dev;735821}736822737737-/**738738- * _add_opp_table() - Find OPP table or allocate a new one739739- * @dev: device for which we do this operation740740- *741741- * It tries to find an existing table first, if it couldn't find one, it742742- * allocates a new OPP table and returns that.743743- *744744- * Return: valid opp_table pointer if success, else NULL.745745- */746746-static struct opp_table *_add_opp_table(struct device *dev)823823+static struct opp_table *_allocate_opp_table(struct device *dev)747824{748825 struct opp_table *opp_table;749826 struct opp_device *opp_dev;750827 int ret;751751-752752- /* Check for existing table for 'dev' first */753753- opp_table = _find_opp_table(dev);754754- if (!IS_ERR(opp_table))755755- return opp_table;756828757829 /*758830 * Allocate a new OPP table. In the infrequent case where a new···767867 ret);768868 }769869770770- srcu_init_notifier_head(&opp_table->srcu_head);870870+ BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);771871 INIT_LIST_HEAD(&opp_table->opp_list);872872+ mutex_init(&opp_table->lock);873873+ kref_init(&opp_table->kref);772874773875 /* Secure the device table modification */774774- list_add_rcu(&opp_table->node, &opp_tables);876876+ list_add(&opp_table->node, &opp_tables);775877 return opp_table;776878}777879778778-/**779779- * _kfree_device_rcu() - Free opp_table RCU handler780780- * @head: RCU head781781- */782782-static void _kfree_device_rcu(struct rcu_head *head)880880+void _get_opp_table_kref(struct opp_table *opp_table)783881{784784- struct opp_table *opp_table = container_of(head, struct opp_table,785785- rcu_head);786786-787787- kfree_rcu(opp_table, rcu_head);882882+ kref_get(&opp_table->kref);788883}789884790790-/**791791- * _remove_opp_table() - Removes a OPP table792792- * @opp_table: OPP table to be removed.793793- *794794- * Removes/frees OPP table if it doesn't contain any OPPs.795795- */796796-static void _remove_opp_table(struct opp_table *opp_table)885885+struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)797886{887887+ struct opp_table *opp_table;888888+889889+ /* Hold our table modification lock here */890890+ mutex_lock(&opp_table_lock);891891+892892+ opp_table = _find_opp_table_unlocked(dev);893893+ if (!IS_ERR(opp_table))894894+ goto unlock;895895+896896+ opp_table = _allocate_opp_table(dev);897897+898898+unlock:899899+ mutex_unlock(&opp_table_lock);900900+901901+ return opp_table;902902+}903903+EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);904904+905905+static void _opp_table_kref_release(struct kref *kref)906906+{907907+ struct opp_table *opp_table = container_of(kref, struct opp_table, kref);798908 struct opp_device *opp_dev;799799-800800- if (!list_empty(&opp_table->opp_list))801801- return;802802-803803- if (opp_table->supported_hw)804804- return;805805-806806- if (opp_table->prop_name)807807- return;808808-809809- if (opp_table->regulators)810810- return;811811-812812- if (opp_table->set_opp)813813- return;814909815910 /* Release clk */816911 if (!IS_ERR(opp_table->clk))···819924 /* dev_list must be empty now */820925 WARN_ON(!list_empty(&opp_table->dev_list));821926822822- list_del_rcu(&opp_table->node);823823- call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,824824- _kfree_device_rcu);927927+ mutex_destroy(&opp_table->lock);928928+ list_del(&opp_table->node);929929+ kfree(opp_table);930930+931931+ mutex_unlock(&opp_table_lock);825932}826933827827-/**828828- * _kfree_opp_rcu() - Free OPP RCU handler829829- * @head: RCU head830830- */831831-static void _kfree_opp_rcu(struct rcu_head *head)934934+void dev_pm_opp_put_opp_table(struct opp_table *opp_table)832935{833833- struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);936936+ kref_put_mutex(&opp_table->kref, _opp_table_kref_release,937937+ &opp_table_lock);938938+}939939+EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);834940835835- kfree_rcu(opp, rcu_head);941941+void _opp_free(struct dev_pm_opp *opp)942942+{943943+ kfree(opp);836944}837945838838-/**839839- * _opp_remove() - Remove an OPP from a table definition840840- * @opp_table: points back to the opp_table struct this opp belongs to841841- * @opp: pointer to the OPP to remove842842- * @notify: OPP_EVENT_REMOVE notification should be sent or not843843- *844844- * This function removes an opp definition from the opp table.845845- *846846- * Locking: The internal opp_table and opp structures are RCU protected.847847- * It is assumed that the caller holds required mutex for an RCU updater848848- * strategy.849849- */850850-void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,851851- bool notify)946946+static void _opp_kref_release(struct kref *kref)852947{948948+ struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);949949+ struct opp_table *opp_table = opp->opp_table;950950+853951 /*854952 * Notify the changes in the availability of the operable855953 * frequency/voltage list.856954 */857857- if (notify)858858- srcu_notifier_call_chain(&opp_table->srcu_head,859859- OPP_EVENT_REMOVE, opp);955955+ blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);860956 opp_debug_remove_one(opp);861861- list_del_rcu(&opp->node);862862- call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);957957+ list_del(&opp->node);958958+ kfree(opp);863959864864- _remove_opp_table(opp_table);960960+ mutex_unlock(&opp_table->lock);961961+ dev_pm_opp_put_opp_table(opp_table);865962}963963+964964+static void dev_pm_opp_get(struct dev_pm_opp *opp)965965+{966966+ kref_get(&opp->kref);967967+}968968+969969+void dev_pm_opp_put(struct dev_pm_opp *opp)970970+{971971+ kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);972972+}973973+EXPORT_SYMBOL_GPL(dev_pm_opp_put);866974867975/**868976 * dev_pm_opp_remove() - Remove an OPP from OPP table···873975 * @freq: OPP to remove with matching 'freq'874976 *875977 * This function removes an opp from the opp table.876876- *877877- * Locking: The internal opp_table and opp structures are RCU protected.878878- * Hence this function internally uses RCU updater strategy with mutex locks879879- * to keep the integrity of the internal data structures. Callers should ensure880880- * that this function is *NOT* called under RCU protection or in contexts where881881- * mutex cannot be locked.882978 */883979void dev_pm_opp_remove(struct device *dev, unsigned long freq)884980{···880988 struct opp_table *opp_table;881989 bool found = false;882990883883- /* Hold our table modification lock here */884884- mutex_lock(&opp_table_lock);885885-886991 opp_table = _find_opp_table(dev);887992 if (IS_ERR(opp_table))888888- goto unlock;993993+ return;994994+995995+ mutex_lock(&opp_table->lock);889996890997 list_for_each_entry(opp, &opp_table->opp_list, node) {891998 if (opp->rate == freq) {···8931002 }8941003 }8951004896896- if (!found) {10051005+ mutex_unlock(&opp_table->lock);10061006+10071007+ if (found) {10081008+ dev_pm_opp_put(opp);10091009+ } else {8971010 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",8981011 __func__, freq);899899- goto unlock;9001012 }9011013902902- _opp_remove(opp_table, opp, true);903903-unlock:904904- mutex_unlock(&opp_table_lock);10141014+ dev_pm_opp_put_opp_table(opp_table);9051015}9061016EXPORT_SYMBOL_GPL(dev_pm_opp_remove);9071017908908-struct dev_pm_opp *_allocate_opp(struct device *dev,909909- struct opp_table **opp_table)10181018+struct dev_pm_opp *_opp_allocate(struct opp_table *table)9101019{9111020 struct dev_pm_opp *opp;9121021 int count, supply_size;913913- struct opp_table *table;914914-915915- table = _add_opp_table(dev);916916- if (!table)917917- return NULL;91810229191023 /* Allocate space for at least one supply */9201024 count = table->regulator_count ? table->regulator_count : 1;···91710319181032 /* allocate new OPP node and supplies structures */9191033 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);920920- if (!opp) {921921- kfree(table);10341034+ if (!opp)9221035 return NULL;923923- }92410369251037 /* Put the supplies at the end of the OPP structure as an empty array */9261038 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);9271039 INIT_LIST_HEAD(&opp->node);928928-929929- *opp_table = table;93010409311041 return opp;9321042}···9491067 return true;9501068}951106910701070+/*10711071+ * Returns:10721072+ * 0: On success. And appropriate error message for duplicate OPPs.10731073+ * -EBUSY: For OPP with same freq/volt and is available. The callers of10741074+ * _opp_add() must return 0 if they receive -EBUSY from it. This is to make10751075+ * sure we don't print error messages unnecessarily if different parts of10761076+ * kernel try to initialize the OPP table.10771077+ * -EEXIST: For OPP with same freq but different volt or is unavailable. This10781078+ * should be considered an error by the callers of _opp_add().10791079+ */9521080int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,9531081 struct opp_table *opp_table)9541082{9551083 struct dev_pm_opp *opp;956956- struct list_head *head = &opp_table->opp_list;10841084+ struct list_head *head;9571085 int ret;95810869591087 /*···9741082 * loop, don't replace it with head otherwise it will become an infinite9751083 * loop.9761084 */977977- list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {10851085+ mutex_lock(&opp_table->lock);10861086+ head = &opp_table->opp_list;10871087+10881088+ list_for_each_entry(opp, &opp_table->opp_list, node) {9781089 if (new_opp->rate > opp->rate) {9791090 head = &opp->node;9801091 continue;···9931098 new_opp->supplies[0].u_volt, new_opp->available);99410999951100 /* Should we compare voltages for all regulators here ? */996996- return opp->available &&997997- new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? 0 : -EEXIST;11011101+ ret = opp->available &&11021102+ new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;11031103+11041104+ mutex_unlock(&opp_table->lock);11051105+ return ret;9981106 }999110711081108+ list_add(&new_opp->node, head);11091109+ mutex_unlock(&opp_table->lock);11101110+10001111 new_opp->opp_table = opp_table;10011001- list_add_rcu(&new_opp->node, head);11121112+ kref_init(&new_opp->kref);11131113+11141114+ /* Get a reference to the OPP table */11151115+ _get_opp_table_kref(opp_table);1002111610031117 ret = opp_debug_create_one(new_opp, opp_table);10041118 if (ret)···1025112110261122/**10271123 * _opp_add_v1() - Allocate a OPP based on v1 bindings.11241124+ * @opp_table: OPP table10281125 * @dev: device for which we do this operation10291126 * @freq: Frequency in Hz for this OPP10301127 * @u_volt: Voltage in uVolts for this OPP···10381133 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table10391134 * and freed by dev_pm_opp_of_remove_table.10401135 *10411041- * Locking: The internal opp_table and opp structures are RCU protected.10421042- * Hence this function internally uses RCU updater strategy with mutex locks10431043- * to keep the integrity of the internal data structures. Callers should ensure10441044- * that this function is *NOT* called under RCU protection or in contexts where10451045- * mutex cannot be locked.10461046- *10471136 * Return:10481137 * 0 On success OR10491138 * Duplicate OPPs (both freq and volt are same) and opp->available···10451146 * Duplicate OPPs (both freq and volt are same) and !opp->available10461147 * -ENOMEM Memory allocation failure10471148 */10481048-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,10491049- bool dynamic)11491149+int _opp_add_v1(struct opp_table *opp_table, struct device *dev,11501150+ unsigned long freq, long u_volt, bool dynamic)10501151{10511051- struct opp_table *opp_table;10521152 struct dev_pm_opp *new_opp;10531153 unsigned long tol;10541154 int ret;1055115510561056- /* Hold our table modification lock here */10571057- mutex_lock(&opp_table_lock);10581058-10591059- new_opp = _allocate_opp(dev, &opp_table);10601060- if (!new_opp) {10611061- ret = -ENOMEM;10621062- goto unlock;10631063- }11561156+ new_opp = _opp_allocate(opp_table);11571157+ if (!new_opp)11581158+ return -ENOMEM;1064115910651160 /* populate the opp table */10661161 new_opp->rate = freq;···10661173 new_opp->dynamic = dynamic;1067117410681175 ret = _opp_add(dev, new_opp, opp_table);10691069- if (ret)11761176+ if (ret) {11771177+ /* Don't return error for duplicate OPPs */11781178+ if (ret == -EBUSY)11791179+ ret = 0;10701180 goto free_opp;10711071-10721072- mutex_unlock(&opp_table_lock);11811181+ }1073118210741183 /*10751184 * Notify the changes in the availability of the operable10761185 * frequency/voltage list.10771186 */10781078- srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);11871187+ blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);10791188 return 0;1080118910811190free_opp:10821082- _opp_remove(opp_table, new_opp, false);10831083-unlock:10841084- mutex_unlock(&opp_table_lock);11911191+ _opp_free(new_opp);11921192+10851193 return ret;10861194}10871195···10961202 * specify the hierarchy of versions it supports. OPP layer will then enable10971203 * OPPs, which are available for those versions, based on its 'opp-supported-hw'10981204 * property.10991099- *11001100- * Locking: The internal opp_table and opp structures are RCU protected.11011101- * Hence this function internally uses RCU updater strategy with mutex locks11021102- * to keep the integrity of the internal data structures. Callers should ensure11031103- * that this function is *NOT* called under RCU protection or in contexts where11041104- * mutex cannot be locked.11051205 */11061106-int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,11071107- unsigned int count)12061206+struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,12071207+ const u32 *versions, unsigned int count)11081208{11091209 struct opp_table *opp_table;11101110- int ret = 0;12101210+ int ret;1111121111121112- /* Hold our table modification lock here */11131113- mutex_lock(&opp_table_lock);11141114-11151115- opp_table = _add_opp_table(dev);11161116- if (!opp_table) {11171117- ret = -ENOMEM;11181118- goto unlock;11191119- }12121212+ opp_table = dev_pm_opp_get_opp_table(dev);12131213+ if (!opp_table)12141214+ return ERR_PTR(-ENOMEM);1120121511211216 /* Make sure there are no concurrent readers while updating opp_table */11221217 WARN_ON(!list_empty(&opp_table->opp_list));···11261243 }1127124411281245 opp_table->supported_hw_count = count;11291129- mutex_unlock(&opp_table_lock);11301130- return 0;12461246+12471247+ return opp_table;1131124811321249err:11331133- _remove_opp_table(opp_table);11341134-unlock:11351135- mutex_unlock(&opp_table_lock);12501250+ dev_pm_opp_put_opp_table(opp_table);1136125111371137- return ret;12521252+ return ERR_PTR(ret);11381253}11391254EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);1140125511411256/**11421257 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw11431143- * @dev: Device for which supported-hw has to be put.12581258+ * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().11441259 *11451260 * This is required only for the V2 bindings, and is called for a matching11461261 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure11471262 * will not be freed.11481148- *11491149- * Locking: The internal opp_table and opp structures are RCU protected.11501150- * Hence this function internally uses RCU updater strategy with mutex locks11511151- * to keep the integrity of the internal data structures. Callers should ensure11521152- * that this function is *NOT* called under RCU protection or in contexts where11531153- * mutex cannot be locked.11541263 */11551155-void dev_pm_opp_put_supported_hw(struct device *dev)12641264+void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)11561265{11571157- struct opp_table *opp_table;11581158-11591159- /* Hold our table modification lock here */11601160- mutex_lock(&opp_table_lock);11611161-11621162- /* Check for existing table for 'dev' first */11631163- opp_table = _find_opp_table(dev);11641164- if (IS_ERR(opp_table)) {11651165- dev_err(dev, "Failed to find opp_table: %ld\n",11661166- PTR_ERR(opp_table));11671167- goto unlock;11681168- }11691169-11701266 /* Make sure there are no concurrent readers while updating opp_table */11711267 WARN_ON(!list_empty(&opp_table->opp_list));1172126811731269 if (!opp_table->supported_hw) {11741174- dev_err(dev, "%s: Doesn't have supported hardware list\n",11751175- __func__);11761176- goto unlock;12701270+ pr_err("%s: Doesn't have supported hardware list\n",12711271+ __func__);12721272+ return;11771273 }1178127411791275 kfree(opp_table->supported_hw);11801276 opp_table->supported_hw = NULL;11811277 opp_table->supported_hw_count = 0;1182127811831183- /* Try freeing opp_table if this was the last blocking resource */11841184- _remove_opp_table(opp_table);11851185-11861186-unlock:11871187- mutex_unlock(&opp_table_lock);12791279+ dev_pm_opp_put_opp_table(opp_table);11881280}11891281EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);11901282···11721314 * specify the extn to be used for certain property names. The properties to11731315 * which the extension will apply are opp-microvolt and opp-microamp. OPP core11741316 * should postfix the property name with -<name> while looking for them.11751175- *11761176- * Locking: The internal opp_table and opp structures are RCU protected.11771177- * Hence this function internally uses RCU updater strategy with mutex locks11781178- * to keep the integrity of the internal data structures. Callers should ensure11791179- * that this function is *NOT* called under RCU protection or in contexts where11801180- * mutex cannot be locked.11811317 */11821182-int dev_pm_opp_set_prop_name(struct device *dev, const char *name)13181318+struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)11831319{11841320 struct opp_table *opp_table;11851185- int ret = 0;13211321+ int ret;1186132211871187- /* Hold our table modification lock here */11881188- mutex_lock(&opp_table_lock);11891189-11901190- opp_table = _add_opp_table(dev);11911191- if (!opp_table) {11921192- ret = -ENOMEM;11931193- goto unlock;11941194- }13231323+ opp_table = dev_pm_opp_get_opp_table(dev);13241324+ if (!opp_table)13251325+ return ERR_PTR(-ENOMEM);1195132611961327 /* Make sure there are no concurrent readers while updating opp_table */11971328 WARN_ON(!list_empty(&opp_table->opp_list));···11991352 goto err;12001353 }1201135412021202- mutex_unlock(&opp_table_lock);12031203- return 0;13551355+ return opp_table;1204135612051357err:12061206- _remove_opp_table(opp_table);12071207-unlock:12081208- mutex_unlock(&opp_table_lock);13581358+ dev_pm_opp_put_opp_table(opp_table);1209135912101210- return ret;13601360+ return ERR_PTR(ret);12111361}12121362EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);1213136312141364/**12151365 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name12161216- * @dev: Device for which the prop-name has to be put.13661366+ * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().12171367 *12181368 * This is required only for the V2 bindings, and is called for a matching12191369 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure12201370 * will not be freed.12211221- *12221222- * Locking: The internal opp_table and opp structures are RCU protected.12231223- * Hence this function internally uses RCU updater strategy with mutex locks12241224- * to keep the integrity of the internal data structures. Callers should ensure12251225- * that this function is *NOT* called under RCU protection or in contexts where12261226- * mutex cannot be locked.12271371 */12281228-void dev_pm_opp_put_prop_name(struct device *dev)13721372+void dev_pm_opp_put_prop_name(struct opp_table *opp_table)12291373{12301230- struct opp_table *opp_table;12311231-12321232- /* Hold our table modification lock here */12331233- mutex_lock(&opp_table_lock);12341234-12351235- /* Check for existing table for 'dev' first */12361236- opp_table = _find_opp_table(dev);12371237- if (IS_ERR(opp_table)) {12381238- dev_err(dev, "Failed to find opp_table: %ld\n",12391239- PTR_ERR(opp_table));12401240- goto unlock;12411241- }12421242-12431374 /* Make sure there are no concurrent readers while updating opp_table */12441375 WARN_ON(!list_empty(&opp_table->opp_list));1245137612461377 if (!opp_table->prop_name) {12471247- dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);12481248- goto unlock;13781378+ pr_err("%s: Doesn't have a prop-name\n", __func__);13791379+ return;12491380 }1250138112511382 kfree(opp_table->prop_name);12521383 opp_table->prop_name = NULL;1253138412541254- /* Try freeing opp_table if this was the last blocking resource */12551255- _remove_opp_table(opp_table);12561256-12571257-unlock:12581258- mutex_unlock(&opp_table_lock);13851385+ dev_pm_opp_put_opp_table(opp_table);12591386}12601387EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);12611388···12761455 * well.12771456 *12781457 * This must be called before any OPPs are initialized for the device.12791279- *12801280- * Locking: The internal opp_table and opp structures are RCU protected.12811281- * Hence this function internally uses RCU updater strategy with mutex locks12821282- * to keep the integrity of the internal data structures. Callers should ensure12831283- * that this function is *NOT* called under RCU protection or in contexts where12841284- * mutex cannot be locked.12851458 */12861459struct opp_table *dev_pm_opp_set_regulators(struct device *dev,12871460 const char * const names[],···12851470 struct regulator *reg;12861471 int ret, i;1287147212881288- mutex_lock(&opp_table_lock);12891289-12901290- opp_table = _add_opp_table(dev);12911291- if (!opp_table) {12921292- ret = -ENOMEM;12931293- goto unlock;12941294- }14731473+ opp_table = dev_pm_opp_get_opp_table(dev);14741474+ if (!opp_table)14751475+ return ERR_PTR(-ENOMEM);1295147612961477 /* This should be called before OPPs are initialized */12971478 if (WARN_ON(!list_empty(&opp_table->opp_list))) {···13291518 if (ret)13301519 goto free_regulators;1331152013321332- mutex_unlock(&opp_table_lock);13331521 return opp_table;1334152213351523free_regulators:···13391529 opp_table->regulators = NULL;13401530 opp_table->regulator_count = 0;13411531err:13421342- _remove_opp_table(opp_table);13431343-unlock:13441344- mutex_unlock(&opp_table_lock);15321532+ dev_pm_opp_put_opp_table(opp_table);1345153313461534 return ERR_PTR(ret);13471535}···13481540/**13491541 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator13501542 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().13511351- *13521352- * Locking: The internal opp_table and opp structures are RCU protected.13531353- * Hence this function internally uses RCU updater strategy with mutex locks13541354- * to keep the integrity of the internal data structures. Callers should ensure13551355- * that this function is *NOT* called under RCU protection or in contexts where13561356- * mutex cannot be locked.13571543 */13581544void dev_pm_opp_put_regulators(struct opp_table *opp_table)13591545{13601546 int i;1361154713621362- mutex_lock(&opp_table_lock);13631363-13641548 if (!opp_table->regulators) {13651549 pr_err("%s: Doesn't have regulators set\n", __func__);13661366- goto unlock;15501550+ return;13671551 }1368155213691553 /* Make sure there are no concurrent readers while updating opp_table */···13701570 opp_table->regulators = NULL;13711571 opp_table->regulator_count = 0;1372157213731373- /* Try freeing opp_table if this was the last blocking resource */13741374- _remove_opp_table(opp_table);13751375-13761376-unlock:13771377- mutex_unlock(&opp_table_lock);15731573+ dev_pm_opp_put_opp_table(opp_table);13781574}13791575EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);13801576···13831587 * regulators per device), instead of the generic OPP set rate helper.13841588 *13851589 * This must be called before any OPPs are initialized for the device.13861386- *13871387- * Locking: The internal opp_table and opp structures are RCU protected.13881388- * Hence this function internally uses RCU updater strategy with mutex locks13891389- * to keep the integrity of the internal data structures. Callers should ensure13901390- * that this function is *NOT* called under RCU protection or in contexts where13911391- * mutex cannot be locked.13921590 */13931393-int dev_pm_opp_register_set_opp_helper(struct device *dev,15911591+struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,13941592 int (*set_opp)(struct dev_pm_set_opp_data *data))13951593{13961594 struct opp_table *opp_table;13971595 int ret;1398159613991597 if (!set_opp)14001400- return -EINVAL;15981598+ return ERR_PTR(-EINVAL);1401159914021402- mutex_lock(&opp_table_lock);14031403-14041404- opp_table = _add_opp_table(dev);14051405- if (!opp_table) {14061406- ret = -ENOMEM;14071407- goto unlock;14081408- }16001600+ opp_table = dev_pm_opp_get_opp_table(dev);16011601+ if (!opp_table)16021602+ return ERR_PTR(-ENOMEM);1409160314101604 /* This should be called before OPPs are initialized */14111605 if (WARN_ON(!list_empty(&opp_table->opp_list))) {···1411162514121626 opp_table->set_opp = set_opp;1413162714141414- mutex_unlock(&opp_table_lock);14151415- return 0;16281628+ return opp_table;1416162914171630err:14181418- _remove_opp_table(opp_table);14191419-unlock:14201420- mutex_unlock(&opp_table_lock);16311631+ dev_pm_opp_put_opp_table(opp_table);1421163214221422- return ret;16331633+ return ERR_PTR(ret);14231634}14241635EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);1425163614261637/**14271638 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for14281639 * set_opp helper14291429- * @dev: Device for which custom set_opp helper has to be cleared.16401640+ * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().14301641 *14311431- * Locking: The internal opp_table and opp structures are RCU protected.14321432- * Hence this function internally uses RCU updater strategy with mutex locks14331433- * to keep the integrity of the internal data structures. Callers should ensure14341434- * that this function is *NOT* called under RCU protection or in contexts where14351435- * mutex cannot be locked.16421642+ * Release resources blocked for platform specific set_opp helper.14361643 */14371437-void dev_pm_opp_register_put_opp_helper(struct device *dev)16441644+void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table)14381645{14391439- struct opp_table *opp_table;14401440-14411441- mutex_lock(&opp_table_lock);14421442-14431443- /* Check for existing table for 'dev' first */14441444- opp_table = _find_opp_table(dev);14451445- if (IS_ERR(opp_table)) {14461446- dev_err(dev, "Failed to find opp_table: %ld\n",14471447- PTR_ERR(opp_table));14481448- goto unlock;14491449- }14501450-14511646 if (!opp_table->set_opp) {14521452- dev_err(dev, "%s: Doesn't have custom set_opp helper set\n",14531453- __func__);14541454- goto unlock;16471647+ pr_err("%s: Doesn't have custom set_opp helper set\n",16481648+ __func__);16491649+ return;14551650 }1456165114571652 /* Make sure there are no concurrent readers while updating opp_table */···1440167314411674 opp_table->set_opp = NULL;1442167514431443- /* Try freeing opp_table if this was the last blocking resource */14441444- _remove_opp_table(opp_table);14451445-14461446-unlock:14471447- mutex_unlock(&opp_table_lock);16761676+ dev_pm_opp_put_opp_table(opp_table);14481677}14491678EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);14501679···14541691 * The opp is made available by default and it can be controlled using14551692 * dev_pm_opp_enable/disable functions.14561693 *14571457- * Locking: The internal opp_table and opp structures are RCU protected.14581458- * Hence this function internally uses RCU updater strategy with mutex locks14591459- * to keep the integrity of the internal data structures. Callers should ensure14601460- * that this function is *NOT* called under RCU protection or in contexts where14611461- * mutex cannot be locked.14621462- *14631694 * Return:14641695 * 0 On success OR14651696 * Duplicate OPPs (both freq and volt are same) and opp->available···14631706 */14641707int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)14651708{14661466- return _opp_add_v1(dev, freq, u_volt, true);17091709+ struct opp_table *opp_table;17101710+ int ret;17111711+17121712+ opp_table = dev_pm_opp_get_opp_table(dev);17131713+ if (!opp_table)17141714+ return -ENOMEM;17151715+17161716+ ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);17171717+17181718+ dev_pm_opp_put_opp_table(opp_table);17191719+ return ret;14671720}14681721EXPORT_SYMBOL_GPL(dev_pm_opp_add);14691722···14831716 * @freq: OPP frequency to modify availability14841717 * @availability_req: availability status requested for this opp14851718 *14861486- * Set the availability of an OPP with an RCU operation, opp_{enable,disable}14871487- * share a common logic which is isolated here.17191719+ * Set the availability of an OPP, opp_{enable,disable} share a common logic17201720+ * which is isolated here.14881721 *14891722 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the14901723 * copy operation, returns 0 if no modification was done OR modification was14911724 * successful.14921492- *14931493- * Locking: The internal opp_table and opp structures are RCU protected.14941494- * Hence this function internally uses RCU updater strategy with mutex locks to14951495- * keep the integrity of the internal data structures. Callers should ensure14961496- * that this function is *NOT* called under RCU protection or in contexts where14971497- * mutex locking or synchronize_rcu() blocking calls cannot be used.14981725 */14991726static int _opp_set_availability(struct device *dev, unsigned long freq,15001727 bool availability_req)15011728{15021729 struct opp_table *opp_table;15031503- struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);17301730+ struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);15041731 int r = 0;15051505-15061506- /* keep the node allocated */15071507- new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);15081508- if (!new_opp)15091509- return -ENOMEM;15101510-15111511- mutex_lock(&opp_table_lock);1512173215131733 /* Find the opp_table */15141734 opp_table = _find_opp_table(dev);15151735 if (IS_ERR(opp_table)) {15161736 r = PTR_ERR(opp_table);15171737 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);15181518- goto unlock;17381738+ return r;15191739 }17401740+17411741+ mutex_lock(&opp_table->lock);1520174215211743 /* Do we have the frequency? */15221744 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {···15141758 break;15151759 }15161760 }17611761+15171762 if (IS_ERR(opp)) {15181763 r = PTR_ERR(opp);15191764 goto unlock;···15231766 /* Is update really needed? */15241767 if (opp->available == availability_req)15251768 goto unlock;15261526- /* copy the old data over */15271527- *new_opp = *opp;1528176915291529- /* plug in new node */15301530- new_opp->available = availability_req;15311531-15321532- list_replace_rcu(&opp->node, &new_opp->node);15331533- mutex_unlock(&opp_table_lock);15341534- call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);17701770+ opp->available = availability_req;1535177115361772 /* Notify the change of the OPP availability */15371773 if (availability_req)15381538- srcu_notifier_call_chain(&opp_table->srcu_head,15391539- OPP_EVENT_ENABLE, new_opp);17741774+ blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,17751775+ opp);15401776 else15411541- srcu_notifier_call_chain(&opp_table->srcu_head,15421542- OPP_EVENT_DISABLE, new_opp);15431543-15441544- return 0;17771777+ blocking_notifier_call_chain(&opp_table->head,17781778+ OPP_EVENT_DISABLE, opp);1545177915461780unlock:15471547- mutex_unlock(&opp_table_lock);15481548- kfree(new_opp);17811781+ mutex_unlock(&opp_table->lock);17821782+ dev_pm_opp_put_opp_table(opp_table);15491783 return r;15501784}15511785···15481800 * Enables a provided opp. If the operation is valid, this returns 0, else the15491801 * corresponding error value. It is meant to be used for users an OPP available15501802 * after being temporarily made unavailable with dev_pm_opp_disable.15511551- *15521552- * Locking: The internal opp_table and opp structures are RCU protected.15531553- * Hence this function indirectly uses RCU and mutex locks to keep the15541554- * integrity of the internal data structures. Callers should ensure that15551555- * this function is *NOT* called under RCU protection or in contexts where15561556- * mutex locking or synchronize_rcu() blocking calls cannot be used.15571803 *15581804 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the15591805 * copy operation, returns 0 if no modification was done OR modification was···15691827 * control by users to make this OPP not available until the circumstances are15701828 * right to make it available again (with a call to dev_pm_opp_enable).15711829 *15721572- * Locking: The internal opp_table and opp structures are RCU protected.15731573- * Hence this function indirectly uses RCU and mutex locks to keep the15741574- * integrity of the internal data structures. Callers should ensure that15751575- * this function is *NOT* called under RCU protection or in contexts where15761576- * mutex locking or synchronize_rcu() blocking calls cannot be used.15771577- *15781830 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the15791831 * copy operation, returns 0 if no modification was done OR modification was15801832 * successful.···15801844EXPORT_SYMBOL_GPL(dev_pm_opp_disable);1581184515821846/**15831583- * dev_pm_opp_get_notifier() - find notifier_head of the device with opp15841584- * @dev: device pointer used to lookup OPP table.18471847+ * dev_pm_opp_register_notifier() - Register OPP notifier for the device18481848+ * @dev: Device for which notifier needs to be registered18491849+ * @nb: Notifier block to be registered15851850 *15861586- * Return: pointer to notifier head if found, otherwise -ENODEV or15871587- * -EINVAL based on type of error casted as pointer. value must be checked15881588- * with IS_ERR to determine valid pointer or error result.15891589- *15901590- * Locking: This function must be called under rcu_read_lock(). opp_table is a15911591- * RCU protected pointer. The reason for the same is that the opp pointer which15921592- * is returned will remain valid for use with opp_get_{voltage, freq} only while15931593- * under the locked area. The pointer returned must be used prior to unlocking15941594- * with rcu_read_unlock() to maintain the integrity of the pointer.18511851+ * Return: 0 on success or a negative error value.15951852 */15961596-struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)18531853+int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)15971854{15981598- struct opp_table *opp_table = _find_opp_table(dev);18551855+ struct opp_table *opp_table;18561856+ int ret;1599185718581858+ opp_table = _find_opp_table(dev);16001859 if (IS_ERR(opp_table))16011601- return ERR_CAST(opp_table); /* matching type */18601860+ return PTR_ERR(opp_table);1602186116031603- return &opp_table->srcu_head;18621862+ ret = blocking_notifier_chain_register(&opp_table->head, nb);18631863+18641864+ dev_pm_opp_put_opp_table(opp_table);18651865+18661866+ return ret;16041867}16051605-EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);18681868+EXPORT_SYMBOL(dev_pm_opp_register_notifier);18691869+18701870+/**18711871+ * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device18721872+ * @dev: Device for which notifier needs to be unregistered18731873+ * @nb: Notifier block to be unregistered18741874+ *18751875+ * Return: 0 on success or a negative error value.18761876+ */18771877+int dev_pm_opp_unregister_notifier(struct device *dev,18781878+ struct notifier_block *nb)18791879+{18801880+ struct opp_table *opp_table;18811881+ int ret;18821882+18831883+ opp_table = _find_opp_table(dev);18841884+ if (IS_ERR(opp_table))18851885+ return PTR_ERR(opp_table);18861886+18871887+ ret = blocking_notifier_chain_unregister(&opp_table->head, nb);18881888+18891889+ dev_pm_opp_put_opp_table(opp_table);18901890+18911891+ return ret;18921892+}18931893+EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);1606189416071895/*16081896 * Free OPPs either created using static entries present in DT or even the16091897 * dynamically added entries based on remove_all param.16101898 */16111611-void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)18991899+void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,19001900+ bool remove_all)16121901{16131613- struct opp_table *opp_table;16141902 struct dev_pm_opp *opp, *tmp;1615190316161616- /* Hold our table modification lock here */16171617- mutex_lock(&opp_table_lock);19041904+ /* Find if opp_table manages a single device */19051905+ if (list_is_singular(&opp_table->dev_list)) {19061906+ /* Free static OPPs */19071907+ list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {19081908+ if (remove_all || !opp->dynamic)19091909+ dev_pm_opp_put(opp);19101910+ }19111911+ } else {19121912+ _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);19131913+ }19141914+}19151915+19161916+void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)19171917+{19181918+ struct opp_table *opp_table;1618191916191920 /* Check for existing table for 'dev' */16201921 opp_table = _find_opp_table(dev);···16631890 IS_ERR_OR_NULL(dev) ?16641891 "Invalid device" : dev_name(dev),16651892 error);16661666- goto unlock;18931893+ return;16671894 }1668189516691669- /* Find if opp_table manages a single device */16701670- if (list_is_singular(&opp_table->dev_list)) {16711671- /* Free static OPPs */16721672- list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {16731673- if (remove_all || !opp->dynamic)16741674- _opp_remove(opp_table, opp, true);16751675- }16761676- } else {16771677- _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);16781678- }18961896+ _dev_pm_opp_remove_table(opp_table, dev, remove_all);1679189716801680-unlock:16811681- mutex_unlock(&opp_table_lock);18981898+ dev_pm_opp_put_opp_table(opp_table);16821899}1683190016841901/**···16771914 *16781915 * Free both OPPs created using static entries present in DT and the16791916 * dynamically added entries.16801680- *16811681- * Locking: The internal opp_table and opp structures are RCU protected.16821682- * Hence this function indirectly uses RCU updater strategy with mutex locks16831683- * to keep the integrity of the internal data structures. Callers should ensure16841684- * that this function is *NOT* called under RCU protection or in contexts where16851685- * mutex cannot be locked.16861917 */16871918void dev_pm_opp_remove_table(struct device *dev)16881919{16891689- _dev_pm_opp_remove_table(dev, true);19201920+ _dev_pm_opp_find_and_remove_table(dev, true);16901921}16911922EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
+15-51
drivers/base/power/opp/cpu.c
···4242 *4343 * WARNING: It is important for the callers to ensure refreshing their copy of4444 * the table if any of the mentioned functions have been invoked in the interim.4545- *4646- * Locking: The internal opp_table and opp structures are RCU protected.4747- * Since we just use the regular accessor functions to access the internal data4848- * structures, we use RCU read lock inside this function. As a result, users of4949- * this function DONOT need to use explicit locks for invoking.5045 */5146int dev_pm_opp_init_cpufreq_table(struct device *dev,5247 struct cpufreq_frequency_table **table)···5156 int i, max_opps, ret = 0;5257 unsigned long rate;53585454- rcu_read_lock();5555-5659 max_opps = dev_pm_opp_get_opp_count(dev);5757- if (max_opps <= 0) {5858- ret = max_opps ? max_opps : -ENODATA;5959- goto out;6060- }6060+ if (max_opps <= 0)6161+ return max_opps ? max_opps : -ENODATA;61626263 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC);6363- if (!freq_table) {6464- ret = -ENOMEM;6565- goto out;6666- }6464+ if (!freq_table)6565+ return -ENOMEM;67666867 for (i = 0, rate = 0; i < max_opps; i++, rate++) {6968 /* find next rate */···7283 /* Is Boost/turbo opp ? */7384 if (dev_pm_opp_is_turbo(opp))7485 freq_table[i].flags = CPUFREQ_BOOST_FREQ;8686+8787+ dev_pm_opp_put(opp);7588 }76897790 freq_table[i].driver_data = i;···8291 *table = &freq_table[0];83928493out:8585- rcu_read_unlock();8694 if (ret)8795 kfree(freq_table);8896···137147 * This removes the OPP tables for CPUs present in the @cpumask.138148 * This should be used to remove all the OPPs entries associated with139149 * the cpus in @cpumask.140140- *141141- * Locking: The internal opp_table and opp structures are RCU protected.142142- * Hence this function internally uses RCU updater strategy with mutex locks143143- * to keep the integrity of the internal data structures. Callers should ensure144144- * that this function is *NOT* called under RCU protection or in contexts where145145- * mutex cannot be locked.146150 */147151void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask)148152{···153169 * @cpumask.154170 *155171 * Returns -ENODEV if OPP table isn't already present.156156- *157157- * Locking: The internal opp_table and opp structures are RCU protected.158158- * Hence this function internally uses RCU updater strategy with mutex locks159159- * to keep the integrity of the internal data structures. Callers should ensure160160- * that this function is *NOT* called under RCU protection or in contexts where161161- * mutex cannot be locked.162172 */163173int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev,164174 const struct cpumask *cpumask)···162184 struct device *dev;163185 int cpu, ret = 0;164186165165- mutex_lock(&opp_table_lock);166166-167187 opp_table = _find_opp_table(cpu_dev);168168- if (IS_ERR(opp_table)) {169169- ret = PTR_ERR(opp_table);170170- goto unlock;171171- }188188+ if (IS_ERR(opp_table))189189+ return PTR_ERR(opp_table);172190173191 for_each_cpu(cpu, cpumask) {174192 if (cpu == cpu_dev->id)···187213 /* Mark opp-table as multiple CPUs are sharing it now */188214 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;189215 }190190-unlock:191191- mutex_unlock(&opp_table_lock);216216+217217+ dev_pm_opp_put_opp_table(opp_table);192218193219 return ret;194220}···203229 *204230 * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP205231 * table's status is access-unknown.206206- *207207- * Locking: The internal opp_table and opp structures are RCU protected.208208- * Hence this function internally uses RCU updater strategy with mutex locks209209- * to keep the integrity of the internal data structures. Callers should ensure210210- * that this function is *NOT* called under RCU protection or in contexts where211211- * mutex cannot be locked.212232 */213233int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)214234{···210242 struct opp_table *opp_table;211243 int ret = 0;212244213213- mutex_lock(&opp_table_lock);214214-215245 opp_table = _find_opp_table(cpu_dev);216216- if (IS_ERR(opp_table)) {217217- ret = PTR_ERR(opp_table);218218- goto unlock;219219- }246246+ if (IS_ERR(opp_table))247247+ return PTR_ERR(opp_table);220248221249 if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) {222250 ret = -EINVAL;223223- goto unlock;251251+ goto put_opp_table;224252 }225253226254 cpumask_clear(cpumask);···228264 cpumask_set_cpu(cpu_dev->id, cpumask);229265 }230266231231-unlock:232232- mutex_unlock(&opp_table_lock);267267+put_opp_table:268268+ dev_pm_opp_put_opp_table(opp_table);233269234270 return ret;235271}
+60-94
drivers/base/power/opp/of.c
···24242525static struct opp_table *_managed_opp(const struct device_node *np)2626{2727- struct opp_table *opp_table;2727+ struct opp_table *opp_table, *managed_table = NULL;28282929- list_for_each_entry_rcu(opp_table, &opp_tables, node) {2929+ mutex_lock(&opp_table_lock);3030+3131+ list_for_each_entry(opp_table, &opp_tables, node) {3032 if (opp_table->np == np) {3133 /*3234 * Multiple devices can point to the same OPP table and···3735 * But the OPPs will be considered as shared only if the3836 * OPP table contains a "opp-shared" property.3937 */4040- if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED)4141- return opp_table;3838+ if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {3939+ _get_opp_table_kref(opp_table);4040+ managed_table = opp_table;4141+ }42424343- return NULL;4343+ break;4444 }4545 }46464747- return NULL;4747+ mutex_unlock(&opp_table_lock);4848+4949+ return managed_table;4850}49515052void _of_init_opp_table(struct opp_table *opp_table, struct device *dev)···235229 * @dev: device pointer used to lookup OPP table.236230 *237231 * Free OPPs created using static entries present in DT.238238- *239239- * Locking: The internal opp_table and opp structures are RCU protected.240240- * Hence this function indirectly uses RCU updater strategy with mutex locks241241- * to keep the integrity of the internal data structures. Callers should ensure242242- * that this function is *NOT* called under RCU protection or in contexts where243243- * mutex cannot be locked.244232 */245233void dev_pm_opp_of_remove_table(struct device *dev)246234{247247- _dev_pm_opp_remove_table(dev, false);235235+ _dev_pm_opp_find_and_remove_table(dev, false);248236}249237EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);250238251239/* Returns opp descriptor node for a device, caller must do of_node_put() */252252-static struct device_node *_of_get_opp_desc_node(struct device *dev)240240+struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev)253241{254242 /*255255- * TODO: Support for multiple OPP tables.256256- *257243 * There should be only ONE phandle present in "operating-points-v2"258244 * property.259245 */260246261247 return of_parse_phandle(dev->of_node, "operating-points-v2", 0);262248}249249+EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node);263250264251/**265252 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)253253+ * @opp_table: OPP table266254 * @dev: device for which we do this operation267255 * @np: device node268256 *269257 * This function adds an opp definition to the opp table and returns status. The270258 * opp can be controlled using dev_pm_opp_enable/disable functions and may be271259 * removed by dev_pm_opp_remove.272272- *273273- * Locking: The internal opp_table and opp structures are RCU protected.274274- * Hence this function internally uses RCU updater strategy with mutex locks275275- * to keep the integrity of the internal data structures. Callers should ensure276276- * that this function is *NOT* called under RCU protection or in contexts where277277- * mutex cannot be locked.278260 *279261 * Return:280262 * 0 On success OR···272278 * -ENOMEM Memory allocation failure273279 * -EINVAL Failed parsing the OPP node274280 */275275-static int _opp_add_static_v2(struct device *dev, struct device_node *np)281281+static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev,282282+ struct device_node *np)276283{277277- struct opp_table *opp_table;278284 struct dev_pm_opp *new_opp;279285 u64 rate;280286 u32 val;281287 int ret;282288283283- /* Hold our table modification lock here */284284- mutex_lock(&opp_table_lock);285285-286286- new_opp = _allocate_opp(dev, &opp_table);287287- if (!new_opp) {288288- ret = -ENOMEM;289289- goto unlock;290290- }289289+ new_opp = _opp_allocate(opp_table);290290+ if (!new_opp)291291+ return -ENOMEM;291292292293 ret = of_property_read_u64(np, "opp-hz", &rate);293294 if (ret < 0) {···316327 goto free_opp;317328318329 ret = _opp_add(dev, new_opp, opp_table);319319- if (ret)330330+ if (ret) {331331+ /* Don't return error for duplicate OPPs */332332+ if (ret == -EBUSY)333333+ ret = 0;320334 goto free_opp;335335+ }321336322337 /* OPP to select on device suspend */323338 if (of_property_read_bool(np, "opp-suspend")) {···338345 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max)339346 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns;340347341341- mutex_unlock(&opp_table_lock);342342-343348 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",344349 __func__, new_opp->turbo, new_opp->rate,345350 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min,···347356 * Notify the changes in the availability of the operable348357 * frequency/voltage list.349358 */350350- srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);359359+ blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);351360 return 0;352361353362free_opp:354354- _opp_remove(opp_table, new_opp, false);355355-unlock:356356- mutex_unlock(&opp_table_lock);363363+ _opp_free(new_opp);364364+357365 return ret;358366}359367···363373 struct opp_table *opp_table;364374 int ret = 0, count = 0;365375366366- mutex_lock(&opp_table_lock);367367-368376 opp_table = _managed_opp(opp_np);369377 if (opp_table) {370378 /* OPPs are already managed */371379 if (!_add_opp_dev(dev, opp_table))372380 ret = -ENOMEM;373373- mutex_unlock(&opp_table_lock);374374- return ret;381381+ goto put_opp_table;375382 }376376- mutex_unlock(&opp_table_lock);383383+384384+ opp_table = dev_pm_opp_get_opp_table(dev);385385+ if (!opp_table)386386+ return -ENOMEM;377387378388 /* We have opp-table node now, iterate over it and add OPPs */379389 for_each_available_child_of_node(opp_np, np) {380390 count++;381391382382- ret = _opp_add_static_v2(dev, np);392392+ ret = _opp_add_static_v2(opp_table, dev, np);383393 if (ret) {384394 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,385395 ret);386386- goto free_table;396396+ _dev_pm_opp_remove_table(opp_table, dev, false);397397+ goto put_opp_table;387398 }388399 }389400390401 /* There should be one of more OPP defined */391391- if (WARN_ON(!count))392392- return -ENOENT;393393-394394- mutex_lock(&opp_table_lock);395395-396396- opp_table = _find_opp_table(dev);397397- if (WARN_ON(IS_ERR(opp_table))) {398398- ret = PTR_ERR(opp_table);399399- mutex_unlock(&opp_table_lock);400400- goto free_table;402402+ if (WARN_ON(!count)) {403403+ ret = -ENOENT;404404+ goto put_opp_table;401405 }402406403407 opp_table->np = opp_np;···400416 else401417 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE;402418403403- mutex_unlock(&opp_table_lock);404404-405405- return 0;406406-407407-free_table:408408- dev_pm_opp_of_remove_table(dev);419419+put_opp_table:420420+ dev_pm_opp_put_opp_table(opp_table);409421410422 return ret;411423}···409429/* Initializes OPP tables based on old-deprecated bindings */410430static int _of_add_opp_table_v1(struct device *dev)411431{432432+ struct opp_table *opp_table;412433 const struct property *prop;413434 const __be32 *val;414414- int nr;435435+ int nr, ret = 0;415436416437 prop = of_find_property(dev->of_node, "operating-points", NULL);417438 if (!prop)···430449 return -EINVAL;431450 }432451452452+ opp_table = dev_pm_opp_get_opp_table(dev);453453+ if (!opp_table)454454+ return -ENOMEM;455455+433456 val = prop->value;434457 while (nr) {435458 unsigned long freq = be32_to_cpup(val++) * 1000;436459 unsigned long volt = be32_to_cpup(val++);437460438438- if (_opp_add_v1(dev, freq, volt, false))439439- dev_warn(dev, "%s: Failed to add OPP %ld\n",440440- __func__, freq);461461+ ret = _opp_add_v1(opp_table, dev, freq, volt, false);462462+ if (ret) {463463+ dev_err(dev, "%s: Failed to add OPP %ld (%d)\n",464464+ __func__, freq, ret);465465+ _dev_pm_opp_remove_table(opp_table, dev, false);466466+ break;467467+ }441468 nr -= 2;442469 }443470444444- return 0;471471+ dev_pm_opp_put_opp_table(opp_table);472472+ return ret;445473}446474447475/**···458468 * @dev: device pointer used to lookup OPP table.459469 *460470 * Register the initial OPP table with the OPP library for given device.461461- *462462- * Locking: The internal opp_table and opp structures are RCU protected.463463- * Hence this function indirectly uses RCU updater strategy with mutex locks464464- * to keep the integrity of the internal data structures. Callers should ensure465465- * that this function is *NOT* called under RCU protection or in contexts where466466- * mutex cannot be locked.467471 *468472 * Return:469473 * 0 On success OR···479495 * OPPs have two version of bindings now. The older one is deprecated,480496 * try for the new binding first.481497 */482482- opp_np = _of_get_opp_desc_node(dev);498498+ opp_np = dev_pm_opp_of_get_opp_desc_node(dev);483499 if (!opp_np) {484500 /*485501 * Try old-deprecated bindings for backward compatibility with···503519 *504520 * This removes the OPP tables for CPUs present in the @cpumask.505521 * This should be used only to remove static entries created from DT.506506- *507507- * Locking: The internal opp_table and opp structures are RCU protected.508508- * Hence this function internally uses RCU updater strategy with mutex locks509509- * to keep the integrity of the internal data structures. Callers should ensure510510- * that this function is *NOT* called under RCU protection or in contexts where511511- * mutex cannot be locked.512522 */513523void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask)514524{···515537 * @cpumask: cpumask for which OPP table needs to be added.516538 *517539 * This adds the OPP tables for CPUs present in the @cpumask.518518- *519519- * Locking: The internal opp_table and opp structures are RCU protected.520520- * Hence this function internally uses RCU updater strategy with mutex locks521521- * to keep the integrity of the internal data structures. Callers should ensure522522- * that this function is *NOT* called under RCU protection or in contexts where523523- * mutex cannot be locked.524540 */525541int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask)526542{···562590 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev.563591 *564592 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev.565565- *566566- * Locking: The internal opp_table and opp structures are RCU protected.567567- * Hence this function internally uses RCU updater strategy with mutex locks568568- * to keep the integrity of the internal data structures. Callers should ensure569569- * that this function is *NOT* called under RCU protection or in contexts where570570- * mutex cannot be locked.571593 */572594int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,573595 struct cpumask *cpumask)···571605 int cpu, ret = 0;572606573607 /* Get OPP descriptor node */574574- np = _of_get_opp_desc_node(cpu_dev);608608+ np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);575609 if (!np) {576610 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__);577611 return -ENOENT;···596630 }597631598632 /* Get OPP descriptor node */599599- tmp_np = _of_get_opp_desc_node(tcpu_dev);633633+ tmp_np = dev_pm_opp_of_get_opp_desc_node(tcpu_dev);600634 if (!tmp_np) {601635 dev_err(tcpu_dev, "%s: Couldn't find opp node.\n",602636 __func__);
+17-23
drivers/base/power/opp/opp.h
···16161717#include <linux/device.h>1818#include <linux/kernel.h>1919+#include <linux/kref.h>1920#include <linux/list.h>2021#include <linux/limits.h>2122#include <linux/pm_opp.h>2222-#include <linux/rculist.h>2323-#include <linux/rcupdate.h>2323+#include <linux/notifier.h>24242525struct clk;2626struct regulator;···5151 * @node: opp table node. The nodes are maintained throughout the lifetime5252 * of boot. It is expected only an optimal set of OPPs are5353 * added to the library by the SoC framework.5454- * RCU usage: opp table is traversed with RCU locks. node5555- * modification is possible realtime, hence the modifications5656- * are protected by the opp_table_lock for integrity.5754 * IMPORTANT: the opp nodes should be maintained in increasing5855 * order.5656+ * @kref: for reference count of the OPP.5957 * @available: true/false - marks if this OPP as available or not6058 * @dynamic: not-created from static DT entries.6159 * @turbo: true if turbo (boost) OPP···6365 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's6466 * frequency from any other OPP's frequency.6567 * @opp_table: points back to the opp_table struct this opp belongs to6666- * @rcu_head: RCU callback head used for deferred freeing6768 * @np: OPP's device node.6869 * @dentry: debugfs dentry pointer (per opp)6970 *···7073 */7174struct dev_pm_opp {7275 struct list_head node;7676+ struct kref kref;73777478 bool available;7579 bool dynamic;···8385 unsigned long clock_latency_ns;84868587 struct opp_table *opp_table;8686- struct rcu_head rcu_head;87888889 struct device_node *np;8990···9598 * struct opp_device - devices managed by 'struct opp_table'9699 * @node: list node97100 * @dev: device to which the struct object belongs9898- * @rcu_head: RCU callback head used for deferred freeing99101 * @dentry: debugfs dentry pointer (per device)100102 *101103 * This is an internal data structure maintaining the devices that are managed···103107struct opp_device {104108 struct list_head node;105109 const struct device *dev;106106- struct rcu_head rcu_head;107110108111#ifdef CONFIG_DEBUG_FS109112 struct dentry *dentry;···120125 * @node: table node - contains the devices with OPPs that121126 * have been registered. Nodes once added are not modified in this122127 * table.123123- * RCU usage: nodes are not modified in the table of opp_table,124124- * however addition is possible and is secured by opp_table_lock125125- * @srcu_head: notifier head to notify the OPP availability changes.126126- * @rcu_head: RCU callback head used for deferred freeing128128+ * @head: notifier head to notify the OPP availability changes.127129 * @dev_list: list of devices that share these OPPs128130 * @opp_list: table of opps131131+ * @kref: for reference count of the table.132132+ * @lock: mutex protecting the opp_list.129133 * @np: struct device_node pointer for opp's DT node.130134 * @clock_latency_ns_max: Max clock latency in nanoseconds.131135 * @shared_opp: OPP is shared between multiple devices.···145151 * This is an internal data structure maintaining the link to opps attached to146152 * a device. This structure is not meant to be shared to users as it is147153 * meant for book keeping and private to OPP library.148148- *149149- * Because the opp structures can be used from both rcu and srcu readers, we150150- * need to wait for the grace period of both of them before freeing any151151- * resources. And so we have used kfree_rcu() from within call_srcu() handlers.152154 */153155struct opp_table {154156 struct list_head node;155157156156- struct srcu_notifier_head srcu_head;157157- struct rcu_head rcu_head;158158+ struct blocking_notifier_head head;158159 struct list_head dev_list;159160 struct list_head opp_list;161161+ struct kref kref;162162+ struct mutex lock;160163161164 struct device_node *np;162165 unsigned long clock_latency_ns_max;···181190};182191183192/* Routines internal to opp core */193193+void _get_opp_table_kref(struct opp_table *opp_table);184194struct opp_table *_find_opp_table(struct device *dev);185195struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);186186-void _dev_pm_opp_remove_table(struct device *dev, bool remove_all);187187-struct dev_pm_opp *_allocate_opp(struct device *dev, struct opp_table **opp_table);196196+void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, bool remove_all);197197+void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all);198198+struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table);199199+void _opp_free(struct dev_pm_opp *opp);188200int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);189189-void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp, bool notify);190190-int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic);201201+int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic);191202void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of);203203+struct opp_table *_add_opp_table(struct device *dev);192204193205#ifdef CONFIG_OF194206void _of_init_opp_table(struct opp_table *opp_table, struct device *dev);
+6-11
drivers/clk/tegra/clk-dfll.c
···633633 struct dev_pm_opp *opp;634634 int i, uv;635635636636- rcu_read_lock();637637-638636 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);639639- if (IS_ERR(opp)) {640640- rcu_read_unlock();637637+ if (IS_ERR(opp))641638 return PTR_ERR(opp);642642- }643643- uv = dev_pm_opp_get_voltage(opp);644639645645- rcu_read_unlock();640640+ uv = dev_pm_opp_get_voltage(opp);641641+ dev_pm_opp_put(opp);646642647643 for (i = 0; i < td->i2c_lut_size; i++) {648644 if (regulator_list_voltage(td->vdd_reg, td->i2c_lut[i]) == uv)···14361440 struct dev_pm_opp *opp;14371441 int lut;1438144214391439- rcu_read_lock();14401440-14411443 rate = ULONG_MAX;14421444 opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate);14431445 if (IS_ERR(opp)) {···14431449 goto out;14441450 }14451451 v_max = dev_pm_opp_get_voltage(opp);14521452+ dev_pm_opp_put(opp);1446145314471454 v = td->soc->cvb->min_millivolts * 1000;14481455 lut = find_vdd_map_entry_exact(td, v);···1459146414601465 if (v_opp <= td->soc->cvb->min_millivolts * 1000)14611466 td->dvco_rate_min = dev_pm_opp_get_freq(opp);14671467+14681468+ dev_pm_opp_put(opp);1462146914631470 for (;;) {14641471 v += max(1, (v_max - v) / (MAX_DFLL_VOLTAGES - j));···14931496 ret = 0;1494149714951498out:14961496- rcu_read_unlock();14971497-14981499 return ret;14991500}15001501
···118118 unsigned int tmp, clk_div, ema_div, freq, volt_id;119119 struct dev_pm_opp *opp;120120121121- rcu_read_lock();122121 cpufreq_for_each_entry(pos, freq_tbl) {123122 opp = dev_pm_opp_find_freq_exact(dvfs_info->dev,124123 pos->frequency * 1000, true);125124 if (IS_ERR(opp)) {126126- rcu_read_unlock();127125 dev_err(dvfs_info->dev,128126 "failed to find valid OPP for %u KHZ\n",129127 pos->frequency);···138140139141 /* Calculate EMA */140142 volt_id = dev_pm_opp_get_voltage(opp);143143+141144 volt_id = (MAX_VOLTAGE - volt_id) / VOLTAGE_STEP;142145 if (volt_id < PMIC_HIGH_VOLT) {143146 ema_div = (CPUEMA_HIGH << P0_7_CPUEMA_SHIFT) |···156157157158 __raw_writel(tmp, dvfs_info->base + XMU_PMU_P0_7 + 4 *158159 (pos - freq_tbl));160160+ dev_pm_opp_put(opp);159161 }160162161161- rcu_read_unlock();162163 return 0;163164}164165
+5-5
drivers/cpufreq/imx6q-cpufreq.c
···5353 freq_hz = new_freq * 1000;5454 old_freq = clk_get_rate(arm_clk) / 1000;55555656- rcu_read_lock();5756 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);5857 if (IS_ERR(opp)) {5959- rcu_read_unlock();6058 dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);6159 return PTR_ERR(opp);6260 }63616462 volt = dev_pm_opp_get_voltage(opp);6565- rcu_read_unlock();6363+ dev_pm_opp_put(opp);6464+6665 volt_old = regulator_get_voltage(arm_reg);67666867 dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",···320321 * freq_table initialised from OPP is therefore sorted in the321322 * same order.322323 */323323- rcu_read_lock();324324 opp = dev_pm_opp_find_freq_exact(cpu_dev,325325 freq_table[0].frequency * 1000, true);326326 min_volt = dev_pm_opp_get_voltage(opp);327327+ dev_pm_opp_put(opp);327328 opp = dev_pm_opp_find_freq_exact(cpu_dev,328329 freq_table[--num].frequency * 1000, true);329330 max_volt = dev_pm_opp_get_voltage(opp);330330- rcu_read_unlock();331331+ dev_pm_opp_put(opp);332332+331333 ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);332334 if (ret > 0)333335 transition_latency += ret * 1000;
+2-6
drivers/cpufreq/mt8173-cpufreq.c
···232232233233 freq_hz = freq_table[index].frequency * 1000;234234235235- rcu_read_lock();236235 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);237236 if (IS_ERR(opp)) {238238- rcu_read_unlock();239237 pr_err("cpu%d: failed to find OPP for %ld\n",240238 policy->cpu, freq_hz);241239 return PTR_ERR(opp);242240 }243241 vproc = dev_pm_opp_get_voltage(opp);244244- rcu_read_unlock();242242+ dev_pm_opp_put(opp);245243246244 /*247245 * If the new voltage or the intermediate voltage is higher than the···409411410412 /* Search a safe voltage for intermediate frequency. */411413 rate = clk_get_rate(inter_clk);412412- rcu_read_lock();413414 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);414415 if (IS_ERR(opp)) {415415- rcu_read_unlock();416416 pr_err("failed to get intermediate opp for cpu%d\n", cpu);417417 ret = PTR_ERR(opp);418418 goto out_free_opp_table;419419 }420420 info->intermediate_voltage = dev_pm_opp_get_voltage(opp);421421- rcu_read_unlock();421421+ dev_pm_opp_put(opp);422422423423 info->cpu_dev = cpu_dev;424424 info->proc_reg = proc_reg;
+1-3
drivers/cpufreq/omap-cpufreq.c
···6363 freq = ret;64646565 if (mpu_reg) {6666- rcu_read_lock();6766 opp = dev_pm_opp_find_freq_ceil(mpu_dev, &freq);6867 if (IS_ERR(opp)) {6969- rcu_read_unlock();7068 dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",7169 __func__, new_freq);7270 return -EINVAL;7371 }7472 volt = dev_pm_opp_get_voltage(opp);7575- rcu_read_unlock();7373+ dev_pm_opp_put(opp);7674 tol = volt * OPP_TOLERANCE / 100;7775 volt_old = regulator_get_voltage(mpu_reg);7876 }
+7-6
drivers/cpufreq/sti-cpufreq.c
···160160 int pcode, substrate, major, minor;161161 int ret;162162 char name[MAX_PCODE_NAME_LEN];163163+ struct opp_table *opp_table;163164164165 reg_fields = sti_cpufreq_match();165166 if (!reg_fields) {···212211213212 snprintf(name, MAX_PCODE_NAME_LEN, "pcode%d", pcode);214213215215- ret = dev_pm_opp_set_prop_name(dev, name);216216- if (ret) {214214+ opp_table = dev_pm_opp_set_prop_name(dev, name);215215+ if (IS_ERR(opp_table)) {217216 dev_err(dev, "Failed to set prop name\n");218218- return ret;217217+ return PTR_ERR(opp_table);219218 }220219221220 version[0] = BIT(major);222221 version[1] = BIT(minor);223222 version[2] = BIT(substrate);224223225225- ret = dev_pm_opp_set_supported_hw(dev, version, VERSION_ELEMENTS);226226- if (ret) {224224+ opp_table = dev_pm_opp_set_supported_hw(dev, version, VERSION_ELEMENTS);225225+ if (IS_ERR(opp_table)) {227226 dev_err(dev, "Failed to set supported hardware\n");228228- return ret;227227+ return PTR_ERR(opp_table);229228 }230229231230 dev_dbg(dev, "pcode: %d major: %d minor: %d substrate: %d\n",
+6-34
drivers/devfreq/devfreq.c
···111111 return;112112 }113113114114- rcu_read_lock();115114 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {116115 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);117116 if (IS_ERR(opp)) {118117 devm_kfree(devfreq->dev.parent, profile->freq_table);119118 profile->max_state = 0;120120- rcu_read_unlock();121119 return;122120 }121121+ dev_pm_opp_put(opp);123122 profile->freq_table[i] = freq;124123 }125125- rcu_read_unlock();126124}127125128126/**···11101112 ssize_t count = 0;11111113 unsigned long freq = 0;1112111411131113- rcu_read_lock();11141115 do {11151116 opp = dev_pm_opp_find_freq_ceil(dev, &freq);11161117 if (IS_ERR(opp))11171118 break;1118111911201120+ dev_pm_opp_put(opp);11191121 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),11201122 "%lu ", freq);11211123 freq++;11221124 } while (1);11231123- rcu_read_unlock();1124112511251126 /* Truncate the trailing space */11261127 if (count)···12211224 * @freq: The frequency given to target function12221225 * @flags: Flags handed from devfreq framework.12231226 *12241224- * Locking: This function must be called under rcu_read_lock(). opp is a rcu12251225- * protected pointer. The reason for the same is that the opp pointer which is12261226- * returned will remain valid for use with opp_get_{voltage, freq} only while12271227- * under the locked area. The pointer returned must be used prior to unlocking12281228- * with rcu_read_unlock() to maintain the integrity of the pointer.12271227+ * The callers are required to call dev_pm_opp_put() for the returned OPP after12281228+ * use.12291229 */12301230struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,12311231 unsigned long *freq,···12591265 */12601266int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)12611267{12621262- struct srcu_notifier_head *nh;12631263- int ret = 0;12641264-12651265- rcu_read_lock();12661266- nh = dev_pm_opp_get_notifier(dev);12671267- if (IS_ERR(nh))12681268- ret = PTR_ERR(nh);12691269- rcu_read_unlock();12701270- if (!ret)12711271- ret = srcu_notifier_chain_register(nh, &devfreq->nb);12721272-12731273- return ret;12681268+ return dev_pm_opp_register_notifier(dev, &devfreq->nb);12741269}12751270EXPORT_SYMBOL(devfreq_register_opp_notifier);12761271···12751292 */12761293int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)12771294{12781278- struct srcu_notifier_head *nh;12791279- int ret = 0;12801280-12811281- rcu_read_lock();12821282- nh = dev_pm_opp_get_notifier(dev);12831283- if (IS_ERR(nh))12841284- ret = PTR_ERR(nh);12851285- rcu_read_unlock();12861286- if (!ret)12871287- ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);12881288-12891289- return ret;12951295+ return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);12901296}12911297EXPORT_SYMBOL(devfreq_unregister_opp_notifier);12921298
+5-9
drivers/devfreq/exynos-bus.c
···103103 int ret = 0;104104105105 /* Get new opp-bus instance according to new bus clock */106106- rcu_read_lock();107106 new_opp = devfreq_recommended_opp(dev, freq, flags);108107 if (IS_ERR(new_opp)) {109108 dev_err(dev, "failed to get recommended opp instance\n");110110- rcu_read_unlock();111109 return PTR_ERR(new_opp);112110 }113111114112 new_freq = dev_pm_opp_get_freq(new_opp);115113 new_volt = dev_pm_opp_get_voltage(new_opp);114114+ dev_pm_opp_put(new_opp);115115+116116 old_freq = bus->curr_freq;117117- rcu_read_unlock();118117119118 if (old_freq == new_freq)120119 return 0;···213214 int ret = 0;214215215216 /* Get new opp-bus instance according to new bus clock */216216- rcu_read_lock();217217 new_opp = devfreq_recommended_opp(dev, freq, flags);218218 if (IS_ERR(new_opp)) {219219 dev_err(dev, "failed to get recommended opp instance\n");220220- rcu_read_unlock();221220 return PTR_ERR(new_opp);222221 }223222224223 new_freq = dev_pm_opp_get_freq(new_opp);224224+ dev_pm_opp_put(new_opp);225225+225226 old_freq = bus->curr_freq;226226- rcu_read_unlock();227227228228 if (old_freq == new_freq)229229 return 0;···356358357359 rate = clk_get_rate(bus->clk);358360359359- rcu_read_lock();360361 opp = devfreq_recommended_opp(dev, &rate, 0);361362 if (IS_ERR(opp)) {362363 dev_err(dev, "failed to find dev_pm_opp\n");363363- rcu_read_unlock();364364 ret = PTR_ERR(opp);365365 goto err_opp;366366 }367367 bus->curr_freq = dev_pm_opp_get_freq(opp);368368- rcu_read_unlock();368368+ dev_pm_opp_put(opp);369369370370 return 0;371371
+2-2
drivers/devfreq/governor_passive.c
···5959 * list of parent device. Because in this case, *freq is temporary6060 * value which is decided by ondemand governor.6161 */6262- rcu_read_lock();6362 opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0);6464- rcu_read_unlock();6563 if (IS_ERR(opp)) {6664 ret = PTR_ERR(opp);6765 goto out;6866 }6767+6868+ dev_pm_opp_put(opp);69697070 /*7171 * Get the OPP table's index of decided freqeuncy by governor
+5-11
drivers/devfreq/rk3399_dmc.c
···9191 unsigned long target_volt, target_rate;9292 int err;93939494- rcu_read_lock();9594 opp = devfreq_recommended_opp(dev, freq, flags);9696- if (IS_ERR(opp)) {9797- rcu_read_unlock();9595+ if (IS_ERR(opp))9896 return PTR_ERR(opp);9999- }1009710198 target_rate = dev_pm_opp_get_freq(opp);10299 target_volt = dev_pm_opp_get_voltage(opp);103103-104104- rcu_read_unlock();100100+ dev_pm_opp_put(opp);105101106102 if (dmcfreq->rate == target_rate)107103 return 0;···418422419423 data->rate = clk_get_rate(data->dmc_clk);420424421421- rcu_read_lock();422425 opp = devfreq_recommended_opp(dev, &data->rate, 0);423423- if (IS_ERR(opp)) {424424- rcu_read_unlock();426426+ if (IS_ERR(opp))425427 return PTR_ERR(opp);426426- }428428+427429 data->rate = dev_pm_opp_get_freq(opp);428430 data->volt = dev_pm_opp_get_voltage(opp);429429- rcu_read_unlock();431431+ dev_pm_opp_put(opp);430432431433 rk3399_devfreq_dmc_profile.initial_freq = data->rate;432434
+1-3
drivers/devfreq/tegra-devfreq.c
···487487 struct dev_pm_opp *opp;488488 unsigned long rate = *freq * KHZ;489489490490- rcu_read_lock();491490 opp = devfreq_recommended_opp(dev, &rate, flags);492491 if (IS_ERR(opp)) {493493- rcu_read_unlock();494492 dev_err(dev, "Failed to find opp for %lu KHz\n", *freq);495493 return PTR_ERR(opp);496494 }497495 rate = dev_pm_opp_get_freq(opp);498498- rcu_read_unlock();496496+ dev_pm_opp_put(opp);499497500498 clk_set_min_rate(tegra->emc_clock, rate);501499 clk_set_rate(tegra->emc_clock, 0);
+2-9
drivers/thermal/cpu_cooling.c
···297297 if (!power_table)298298 return -ENOMEM;299299300300- rcu_read_lock();301301-302300 for (freq = 0, i = 0;303301 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);304302 freq++, i++) {···304306 u64 power;305307306308 if (i >= num_opps) {307307- rcu_read_unlock();308309 ret = -EAGAIN;309310 goto free_power_table;310311 }311312312313 freq_mhz = freq / 1000000;313314 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;315315+ dev_pm_opp_put(opp);314316315317 /*316318 * Do the multiplication with MHz and millivolt so as···325327 /* power is stored in mW */326328 power_table[i].power = power;327329 }328328-329329- rcu_read_unlock();330330331331 if (i != num_opps) {332332 ret = PTR_ERR(opp);···429433 return 0;430434 }431435432432- rcu_read_lock();433433-434436 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,435437 true);436438 voltage = dev_pm_opp_get_voltage(opp);437437-438438- rcu_read_unlock();439439+ dev_pm_opp_put(opp);439440440441 if (voltage == 0) {441442 dev_warn_ratelimited(cpufreq_device->cpu_dev,
+4-11
drivers/thermal/devfreq_cooling.c
···113113 unsigned int freq = dfc->freq_table[i];114114 bool want_enable = i >= cdev_state ? true : false;115115116116- rcu_read_lock();117116 opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);118118- rcu_read_unlock();119117120118 if (PTR_ERR(opp) == -ERANGE)121119 continue;122120 else if (IS_ERR(opp))123121 return PTR_ERR(opp);122122+123123+ dev_pm_opp_put(opp);124124125125 if (want_enable)126126 ret = dev_pm_opp_enable(dev, freq);···221221 if (!dfc->power_ops->get_static_power)222222 return 0;223223224224- rcu_read_lock();225225-226224 opp = dev_pm_opp_find_freq_exact(dev, freq, true);227225 if (IS_ERR(opp) && (PTR_ERR(opp) == -ERANGE))228226 opp = dev_pm_opp_find_freq_exact(dev, freq, false);229227230228 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */231231-232232- rcu_read_unlock();229229+ dev_pm_opp_put(opp);233230234231 if (voltage == 0) {235232 dev_warn_ratelimited(dev,···409412 unsigned long power_dyn, voltage;410413 struct dev_pm_opp *opp;411414412412- rcu_read_lock();413413-414415 opp = dev_pm_opp_find_freq_floor(dev, &freq);415416 if (IS_ERR(opp)) {416416- rcu_read_unlock();417417 ret = PTR_ERR(opp);418418 goto free_tables;419419 }420420421421 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */422422-423423- rcu_read_unlock();422422+ dev_pm_opp_put(opp);424423425424 if (dfc->power_ops) {426425 power_dyn = get_dynamic_power(dfc, freq, voltage);