@@ -84,7 +84,11 @@ struct dev_pm_opp {
8484 *
8585 * This is an internal data structure maintaining the link to opps attached to
8686 * a device. This structure is not meant to be shared to users as it is
87- * meant for book keeping and private to OPP library
87+ * meant for book keeping and private to OPP library.
88+ *
89+ * Because the opp structures can be used from both rcu and srcu readers, we
90+ * need to wait for the grace period of both of them before freeing any
91+ * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
8892 */
8993struct device_opp {
9094 struct list_head node ;
@@ -382,12 +386,34 @@ struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
382386}
383387EXPORT_SYMBOL_GPL (dev_pm_opp_find_freq_floor );
384388
389+ static struct device_opp * add_device_opp (struct device * dev )
390+ {
391+ struct device_opp * dev_opp ;
392+
393+ /*
394+ * Allocate a new device OPP table. In the infrequent case where a new
395+ * device is needed to be added, we pay this penalty.
396+ */
397+ dev_opp = kzalloc (sizeof (* dev_opp ), GFP_KERNEL );
398+ if (!dev_opp )
399+ return NULL ;
400+
401+ dev_opp -> dev = dev ;
402+ srcu_init_notifier_head (& dev_opp -> srcu_head );
403+ INIT_LIST_HEAD (& dev_opp -> opp_list );
404+
405+ /* Secure the device list modification */
406+ list_add_rcu (& dev_opp -> node , & dev_opp_list );
407+ return dev_opp ;
408+ }
409+
385410static int dev_pm_opp_add_dynamic (struct device * dev , unsigned long freq ,
386411 unsigned long u_volt , bool dynamic )
387412{
388413 struct device_opp * dev_opp = NULL ;
389414 struct dev_pm_opp * opp , * new_opp ;
390415 struct list_head * head ;
416+ int ret ;
391417
392418 /* allocate new OPP node */
393419 new_opp = kzalloc (sizeof (* new_opp ), GFP_KERNEL );
@@ -408,27 +434,12 @@ static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
408434 /* Check for existing list for 'dev' */
409435 dev_opp = find_device_opp (dev );
410436 if (IS_ERR (dev_opp )) {
411- /*
412- * Allocate a new device OPP table. In the infrequent case
413- * where a new device is needed to be added, we pay this
414- * penalty.
415- */
416- dev_opp = kzalloc (sizeof (struct device_opp ), GFP_KERNEL );
437+ dev_opp = add_device_opp (dev );
417438 if (!dev_opp ) {
418- mutex_unlock (& dev_opp_list_lock );
419- kfree (new_opp );
420- dev_warn (dev ,
421- "%s: Unable to create device OPP structure\n" ,
422- __func__ );
423- return - ENOMEM ;
439+ ret = - ENOMEM ;
440+ goto free_opp ;
424441 }
425442
426- dev_opp -> dev = dev ;
427- srcu_init_notifier_head (& dev_opp -> srcu_head );
428- INIT_LIST_HEAD (& dev_opp -> opp_list );
429-
430- /* Secure the device list modification */
431- list_add_rcu (& dev_opp -> node , & dev_opp_list );
432443 head = & dev_opp -> opp_list ;
433444 goto list_add ;
434445 }
@@ -447,15 +458,13 @@ static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
447458
448459 /* Duplicate OPPs ? */
449460 if (new_opp -> rate == opp -> rate ) {
450- int ret = opp -> available && new_opp -> u_volt == opp -> u_volt ?
461+ ret = opp -> available && new_opp -> u_volt == opp -> u_volt ?
451462 0 : - EEXIST ;
452463
453464 dev_warn (dev , "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n" ,
454465 __func__ , opp -> rate , opp -> u_volt , opp -> available ,
455466 new_opp -> rate , new_opp -> u_volt , new_opp -> available );
456- mutex_unlock (& dev_opp_list_lock );
457- kfree (new_opp );
458- return ret ;
467+ goto free_opp ;
459468 }
460469
461470list_add :
@@ -469,6 +478,11 @@ static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
469478 */
470479 srcu_notifier_call_chain (& dev_opp -> srcu_head , OPP_EVENT_ADD , new_opp );
471480 return 0 ;
481+
482+ free_opp :
483+ mutex_unlock (& dev_opp_list_lock );
484+ kfree (new_opp );
485+ return ret ;
472486}
473487
474488/**
@@ -511,10 +525,11 @@ static void kfree_device_rcu(struct rcu_head *head)
511525{
512526 struct device_opp * device_opp = container_of (head , struct device_opp , rcu_head );
513527
514- kfree (device_opp );
528+ kfree_rcu (device_opp , rcu_head );
515529}
516530
517- void __dev_pm_opp_remove (struct device_opp * dev_opp , struct dev_pm_opp * opp )
531+ static void __dev_pm_opp_remove (struct device_opp * dev_opp ,
532+ struct dev_pm_opp * opp )
518533{
519534 /*
520535 * Notify the changes in the availability of the operable
@@ -592,7 +607,7 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
592607static int opp_set_availability (struct device * dev , unsigned long freq ,
593608 bool availability_req )
594609{
595- struct device_opp * tmp_dev_opp , * dev_opp = ERR_PTR ( - ENODEV ) ;
610+ struct device_opp * dev_opp ;
596611 struct dev_pm_opp * new_opp , * tmp_opp , * opp = ERR_PTR (- ENODEV );
597612 int r = 0 ;
598613
@@ -606,12 +621,7 @@ static int opp_set_availability(struct device *dev, unsigned long freq,
606621 mutex_lock (& dev_opp_list_lock );
607622
608623 /* Find the device_opp */
609- list_for_each_entry (tmp_dev_opp , & dev_opp_list , node ) {
610- if (dev == tmp_dev_opp -> dev ) {
611- dev_opp = tmp_dev_opp ;
612- break ;
613- }
614- }
624+ dev_opp = find_device_opp (dev );
615625 if (IS_ERR (dev_opp )) {
616626 r = PTR_ERR (dev_opp );
617627 dev_warn (dev , "%s: Device OPP not found (%d)\n" , __func__ , r );
0 commit comments