···8484 *8585 * This is an internal data structure maintaining the link to opps attached to8686 * a device. This structure is not meant to be shared to users as it is8787- * meant for book keeping and private to OPP library8787+ * meant for book keeping and private to OPP library.8888+ *8989+ * Because the opp structures can be used from both rcu and srcu readers, we9090+ * need to wait for the grace period of both of them before freeing any9191+ * resources. And so we have used kfree_rcu() from within call_srcu() handlers.8892 */8993struct device_opp {9094 struct list_head node;···107103static LIST_HEAD(dev_opp_list);108104/* Lock to allow exclusive modification to the device and opp lists */109105static DEFINE_MUTEX(dev_opp_list_lock);106106+107107+#define opp_rcu_lockdep_assert() \108108+do { \109109+ rcu_lockdep_assert(rcu_read_lock_held() || \110110+ lockdep_is_held(&dev_opp_list_lock), \111111+ "Missing rcu_read_lock() or " \112112+ "dev_opp_list_lock protection"); \113113+} while (0)110114111115/**112116 * find_device_opp() - find device_opp struct using device pointer···216204 * This function returns the number of available opps if there are any,217205 * else returns 0 if none or the corresponding error value.218206 *219219- * Locking: This function must be called under rcu_read_lock(). This function220220- * internally references two RCU protected structures: device_opp and opp which221221- * are safe as long as we are under a common RCU locked section.207207+ * Locking: This function takes rcu_read_lock().222208 */223209int dev_pm_opp_get_opp_count(struct device *dev)224210{···224214 struct dev_pm_opp *temp_opp;225215 int count = 0;226216217217+ rcu_read_lock();218218+227219 dev_opp = find_device_opp(dev);228220 if (IS_ERR(dev_opp)) {229229- int r = PTR_ERR(dev_opp);230230- dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);231231- return r;221221+ count = PTR_ERR(dev_opp);222222+ dev_err(dev, "%s: device OPP not found (%d)\n",223223+ __func__, count);224224+ goto out_unlock;232225 }233226234227 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {···239226 count++;240227 }241228229229+out_unlock:230230+ rcu_read_unlock();242231 return count;243232}244233EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);···277262{278263 struct device_opp *dev_opp;279264 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);265265+266266+ opp_rcu_lockdep_assert();280267281268 dev_opp = find_device_opp(dev);282269 if (IS_ERR(dev_opp)) {···325308{326309 struct device_opp *dev_opp;327310 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);311311+312312+ opp_rcu_lockdep_assert();328313329314 if (!dev || !freq) {330315 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);···376357 struct device_opp *dev_opp;377358 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);378359360360+ opp_rcu_lockdep_assert();361361+379362 if (!dev || !freq) {380363 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);381364 return ERR_PTR(-EINVAL);···403382}404383EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);405384385385+static struct device_opp *add_device_opp(struct device *dev)386386+{387387+ struct device_opp *dev_opp;388388+389389+ /*390390+ * Allocate a new device OPP table. In the infrequent case where a new391391+ * device is needed to be added, we pay this penalty.392392+ */393393+ dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);394394+ if (!dev_opp)395395+ return NULL;396396+397397+ dev_opp->dev = dev;398398+ srcu_init_notifier_head(&dev_opp->srcu_head);399399+ INIT_LIST_HEAD(&dev_opp->opp_list);400400+401401+ /* Secure the device list modification */402402+ list_add_rcu(&dev_opp->node, &dev_opp_list);403403+ return dev_opp;404404+}405405+406406static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,407407 unsigned long u_volt, bool dynamic)408408{409409 struct device_opp *dev_opp = NULL;410410 struct dev_pm_opp *opp, *new_opp;411411 struct list_head *head;412412+ int ret;412413413414 /* allocate new OPP node */414415 new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);···443400 mutex_lock(&dev_opp_list_lock);444401445402 /* populate the opp table */446446- new_opp->dev_opp = dev_opp;447403 new_opp->rate = freq;448404 new_opp->u_volt = u_volt;449405 new_opp->available = true;···451409 /* Check for existing list for 'dev' */452410 dev_opp = find_device_opp(dev);453411 if (IS_ERR(dev_opp)) {454454- /*455455- * Allocate a new device OPP table. In the infrequent case456456- * where a new device is needed to be added, we pay this457457- * penalty.458458- */459459- dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);412412+ dev_opp = add_device_opp(dev);460413 if (!dev_opp) {461461- mutex_unlock(&dev_opp_list_lock);462462- kfree(new_opp);463463- dev_warn(dev,464464- "%s: Unable to create device OPP structure\n",465465- __func__);466466- return -ENOMEM;414414+ ret = -ENOMEM;415415+ goto free_opp;467416 }468417469469- dev_opp->dev = dev;470470- srcu_init_notifier_head(&dev_opp->srcu_head);471471- INIT_LIST_HEAD(&dev_opp->opp_list);472472-473473- /* Secure the device list modification */474474- list_add_rcu(&dev_opp->node, &dev_opp_list);475418 head = &dev_opp->opp_list;476419 goto list_add;477420 }···475448476449 /* Duplicate OPPs ? */477450 if (new_opp->rate == opp->rate) {478478- int ret = opp->available && new_opp->u_volt == opp->u_volt ?451451+ ret = opp->available && new_opp->u_volt == opp->u_volt ?479452 0 : -EEXIST;480453481454 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",482455 __func__, opp->rate, opp->u_volt, opp->available,483456 new_opp->rate, new_opp->u_volt, new_opp->available);484484- mutex_unlock(&dev_opp_list_lock);485485- kfree(new_opp);486486- return ret;457457+ goto free_opp;487458 }488459489460list_add:461461+ new_opp->dev_opp = dev_opp;490462 list_add_rcu(&new_opp->node, head);491463 mutex_unlock(&dev_opp_list_lock);492464···495469 */496470 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);497471 return 0;472472+473473+free_opp:474474+ mutex_unlock(&dev_opp_list_lock);475475+ kfree(new_opp);476476+ return ret;498477}499478500479/**···542511{543512 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);544513545545- kfree(device_opp);514514+ kfree_rcu(device_opp, rcu_head);546515}547516548548-void __dev_pm_opp_remove(struct device_opp *dev_opp, struct dev_pm_opp *opp)517517+static void __dev_pm_opp_remove(struct device_opp *dev_opp,518518+ struct dev_pm_opp *opp)549519{550520 /*551521 * Notify the changes in the availability of the operable···624592static int opp_set_availability(struct device *dev, unsigned long freq,625593 bool availability_req)626594{627627- struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);595595+ struct device_opp *dev_opp;628596 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);629597 int r = 0;630598···638606 mutex_lock(&dev_opp_list_lock);639607640608 /* Find the device_opp */641641- list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {642642- if (dev == tmp_dev_opp->dev) {643643- dev_opp = tmp_dev_opp;644644- break;645645- }646646- }609609+ dev_opp = find_device_opp(dev);647610 if (IS_ERR(dev_opp)) {648611 r = PTR_ERR(dev_opp);649612 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);···795768 */796769void of_free_opp_table(struct device *dev)797770{798798- struct device_opp *dev_opp = find_device_opp(dev);771771+ struct device_opp *dev_opp;799772 struct dev_pm_opp *opp, *tmp;800773801774 /* Check for existing list for 'dev' */802775 dev_opp = find_device_opp(dev);803803- if (WARN(IS_ERR(dev_opp), "%s: dev_opp: %ld\n", dev_name(dev),804804- PTR_ERR(dev_opp)))776776+ if (IS_ERR(dev_opp)) {777777+ int error = PTR_ERR(dev_opp);778778+ if (error != -ENODEV)779779+ WARN(1, "%s: dev_opp: %d\n",780780+ IS_ERR_OR_NULL(dev) ?781781+ "Invalid device" : dev_name(dev),782782+ error);805783 return;784784+ }806785807786 /* Hold our list modification lock here */808787 mutex_lock(&dev_opp_list_lock);