cpufreq: Locking fixes and cleanups
Viresh Kumar
maintainerv5 cover cpufreq: Locking fixes and cleanups
Hi Rafael,
These are rest of the patches that fix some more locking issues with policy->rwsem and do some minor optimization/cleanups.
V4->V5:
- Changelog updated for 1-2 as suggested by Rafael
- 4th patch is dropped, which moved common tunable callbacks to cpufreq_governor.c
- 5-7 are resend on the V5 sent earlier, as reply to V4 of the series.
Viresh Kumar (6):
cpufreq: Merge cpufreq_offline_prepare/finish routines
cpufreq: Call __cpufreq_governor() with policy->rwsem held
cpufreq: Remove cpufreq_governor_lock
cpufreq: governor: No need to manage state machine now
cpufreq: conservative: Update sample_delay_ns immediately
cpufreq: ondemand: Rearrange od_dbs_timer() to avoid updating delay
drivers/cpufreq/cpufreq.c | 93 ++++++++++++++++------------------ drivers/cpufreq/cpufreq_conservative.c | 14 ----- drivers/cpufreq/cpufreq_governor.c | 90 ++++++++++++++++++++++++-------- drivers/cpufreq/cpufreq_governor.h | 3 +- drivers/cpufreq/cpufreq_ondemand.c | 78 ++-------------------------- 5 files changed, 117 insertions(+), 161 deletions(-)
Signature · 1 line
2.7.1.370.gb2aa7f8
Viresh Kumar
maintainerPATCH v5 1/6 cpufreq: Merge cpufreq_offline_prepare/finish routines
Governor sysfs attributes are still removed in __cpufreq_governor(_EXIT), though, so had store() been used for them, the deadlock described in the changelog of commit 1aee40ac9c86 would have been possible.
Fortunately, we don't use store() (which still does get_online_cpus()) for those attributes now. We use governor_store() for them and that doesn't call get_online_cpus().
And so we don't need to split cpufreq-offline functionality into DOWN_PREPARE and POST_DEAD hotplug notifiers anymore.
--- drivers/cpufreq/cpufreq.c | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-)
drivers/cpufreq/cpufreq.c +10 -26
@@ -1361,9 +1361,10 @@ static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)return ret; } -static void cpufreq_offline_prepare (unsigned int cpu) +static void cpufreq_offline (unsigned int cpu) {struct cpufreq_policy *policy; +int ret;pr_debug ("%s: unregistering CPU %u\n" , __func__, cpu);
@@ -1374,7 +1375,7 @@ static void cpufreq_offline_prepare(unsigned int cpu) }if (has_target ()) { -int ret =__cpufreq_governor (policy,CPUFREQ_GOV_STOP ); + ret =__cpufreq_governor (policy,CPUFREQ_GOV_STOP );if (ret)pr_err ("%s: Failed to stop governor\n" , __func__); }
@@ -1397,34 +1398,23 @@ static void cpufreq_offline_prepare(unsigned int cpu)/* Start governor again for active policy */ if (!policy_is_inactive (policy)) {if (has_target ()) { -int ret =__cpufreq_governor (policy,CPUFREQ_GOV_START ); + ret =__cpufreq_governor (policy,CPUFREQ_GOV_START );if (!ret) ret =__cpufreq_governor (policy,CPUFREQ_GOV_LIMITS );if (ret)pr_err ("%s: Failed to start governor\n" , __func__); } - }else if (cpufreq_driver->stop_cpu) { - cpufreq_driver->stop_cpu (policy); - } -} -static void cpufreq_offline_finish (unsigned int cpu) -{ -struct cpufreq_policy *policy =per_cpu (cpufreq_cpu_data, cpu); - -if (!policy) { -pr_debug ("%s: No cpu_data found\n" , __func__);return ; } -/* Only proceed for inactive policies */ -if (!policy_is_inactive (policy)) -return ; +if (cpufreq_driver->stop_cpu) + cpufreq_driver->stop_cpu (policy);/* If cpu is last user of policy, free policy */ if (has_target ()) { -int ret =__cpufreq_governor (policy,CPUFREQ_GOV_POLICY_EXIT ); + ret =__cpufreq_governor (policy,CPUFREQ_GOV_POLICY_EXIT );if (ret)pr_err ("%s: Failed to exit governor\n" , __func__); }
@@ -1453,10 +1443,8 @@ static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)if (!policy)return ; -if (cpu_online (cpu)) { -cpufreq_offline_prepare (cpu); -cpufreq_offline_finish (cpu); - } +if (cpu_online (cpu)) +cpufreq_offline (cpu);cpumask_clear_cpu (cpu, policy->real_cpus);remove_cpu_dev_symlink (policy, cpu);
@@ -2304,11 +2292,7 @@ static int cpufreq_cpu_callback(struct notifier_block *nfb,break ;case CPU_DOWN_PREPARE : -cpufreq_offline_prepare (cpu); -break ; - -case CPU_POST_DEAD : -cpufreq_offline_finish (cpu); +cpufreq_offline (cpu);break ;case CPU_DOWN_FAILED :
Signature · 1 line
2.7.1.370.gb2aa7f8
Viresh Kumar
maintainerPATCH v5 2/6 cpufreq: Call __cpufreq_governor() with policy->rwsem held
In reply to Viresh KumarThe cpufreq core code is not consistent with respect to invoking __cpufreq_governor() under policy->rwsem.
Changing all code to always hold policy->rwsem around __cpufreq_governor() invocations will allow us to remove cpufreq_governor_lock, that is used today because we can't guarantee that __cpufreq_governor() isn't executed in parallel.
We should also ensure that the lock is held across state changes to the governors.
For example, while adding a CPU to the policy on cpu-online path, we need to stop the governor, change policy->cpus, start the governor and then refresh its limits. The complete sequence must be guaranteed to execute without any concurrent races. And that can be achieved using policy->rwsem around these use cases.
Also note that, after this patch cpufreq_driver->stop_cpu() and ->exit() will get called while policy->rwsem is held, which wasn't the case earlier. That shouldn't have any side effects though.
--- drivers/cpufreq/cpufreq.c | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-)
drivers/cpufreq/cpufreq.c +33 -16
@@ -1048,30 +1048,29 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpif (cpumask_test_cpu (cpu, policy->cpus))return 0 ; +down_write (&policy->rwsem);if (has_target ()) { ret =__cpufreq_governor (policy,CPUFREQ_GOV_STOP );if (ret) {pr_err ("%s: Failed to stop governor\n" , __func__); -return ret; + goto unlock; } } -down_write (&policy->rwsem);cpumask_set_cpu (cpu, policy->cpus); -up_write (&policy->rwsem);if (has_target ()) { ret =__cpufreq_governor (policy,CPUFREQ_GOV_START );if (!ret) ret =__cpufreq_governor (policy,CPUFREQ_GOV_LIMITS ); -if (ret) { +if (ret)pr_err ("%s: Failed to start governor\n" , __func__); -return ret; - } } -return 0 ; +unlock: +up_write (&policy->rwsem); +return ret; }static struct cpufreq_policy *cpufreq_policy_alloc (unsigned int cpu)
@@ -1374,13 +1373,13 @@ static void cpufreq_offline(unsigned int cpu)return ; } +down_write (&policy->rwsem);if (has_target ()) { ret =__cpufreq_governor (policy,CPUFREQ_GOV_STOP );if (ret)pr_err ("%s: Failed to stop governor\n" , __func__); } -down_write (&policy->rwsem);cpumask_clear_cpu (cpu, policy->cpus);if (policy_is_inactive (policy)) {
@@ -1393,7 +1392,6 @@ static void cpufreq_offline(unsigned int cpu)/* Nominate new CPU */ policy->cpu =cpumask_any (policy->cpus); } -up_write (&policy->rwsem);/* Start governor again for active policy */ if (!policy_is_inactive (policy)) {
@@ -1406,7 +1404,7 @@ static void cpufreq_offline(unsigned int cpu)pr_err ("%s: Failed to start governor\n" , __func__); } -return ; + goto unlock; }if (cpufreq_driver->stop_cpu)
@@ -1428,6 +1426,9 @@ static void cpufreq_offline(unsigned int cpu) cpufreq_driver->exit (policy); policy->freq_table =NULL ; } + +unlock: +up_write (&policy->rwsem); }/**
@@ -1624,6 +1625,7 @@ EXPORT_SYMBOL(cpufreq_generic_suspend);void cpufreq_suspend (void ) {struct cpufreq_policy *policy; +int ret;if (!cpufreq_driver)return ;
@@ -1634,7 +1636,11 @@ void cpufreq_suspend(void)pr_debug ("%s: Suspending Governors\n" , __func__);for_each_active_policy (policy) { -if (__cpufreq_governor (policy,CPUFREQ_GOV_STOP )) +down_write (&policy->rwsem); + ret =__cpufreq_governor (policy,CPUFREQ_GOV_STOP ); +up_write (&policy->rwsem); + +if (ret)pr_err ("%s: Failed to stop governor for policy: %p\n" , __func__, policy); else if (cpufreq_driver->suspend
@@ -1656,6 +1662,7 @@ void cpufreq_suspend(void)void cpufreq_resume (void ) {struct cpufreq_policy *policy; +int ret;if (!cpufreq_driver)return ;
@@ -1668,13 +1675,20 @@ void cpufreq_resume(void)pr_debug ("%s: Resuming Governors\n" , __func__);for_each_active_policy (policy) { -if (cpufreq_driver->resume && cpufreq_driver->resume (policy)) +if (cpufreq_driver->resume && cpufreq_driver->resume (policy)) {pr_err ("%s: Failed to resume driver: %p\n" , __func__, policy); -else if (__cpufreq_governor (policy,CPUFREQ_GOV_START ) - ||__cpufreq_governor (policy,CPUFREQ_GOV_LIMITS )) -pr_err ("%s: Failed to start governor for policy: %p\n" , - __func__, policy); + }else { +down_write (&policy->rwsem); + ret =__cpufreq_governor (policy,CPUFREQ_GOV_START ); +if (!ret) +__cpufreq_governor (policy,CPUFREQ_GOV_LIMITS ); +up_write (&policy->rwsem); + +if (ret) +pr_err ("%s: Failed to start governor for policy: %p\n" , + __func__, policy); + } }/*
@@ -2325,8 +2339,11 @@ static int cpufreq_boost_set_sw(int state) __func__);break ; } + +down_write (&policy->rwsem); policy->user_policy.max = policy->max;__cpufreq_governor (policy,CPUFREQ_GOV_LIMITS ); +up_write (&policy->rwsem); } }
Signature · 1 line
2.7.1.370.gb2aa7f8
Viresh Kumar
maintainerPATCH v5 3/6 cpufreq: Remove cpufreq_governor_lock
In reply to Viresh KumarWe used to drop policy->rwsem just before calling __cpufreq_governor() in some cases earlier and so it was possible that __cpufreq_governor() runs concurrently via separate threads.
In order to guarantee valid state transitions for governors, 'governor_enabled' was required to be protected using some locking and we created cpufreq_governor_lock for that.
But now, __cpufreq_governor() is always called from within policy->rwsem held and so 'governor_enabled' is protected against races even without cpufreq_governor_lock.
Get rid of the extra lock now.
--- drivers/cpufreq/cpufreq.c | 8 -------- drivers/cpufreq/cpufreq_governor.h | 1 - 2 files changed, 9 deletions(-)
drivers/cpufreq/cpufreq.c +0 -8
@@ -146,8 +146,6 @@ void cpufreq_update_util(u64 time, unsigned long util, unsigned long max)rcu_read_unlock (); } -DEFINE_MUTEX (cpufreq_governor_lock); -/* Flag to suspend/resume CPUFreq governors */ static bool cpufreq_suspended;
@@ -2014,11 +2012,9 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,pr_debug ("%s: for CPU %u, event %u\n" , __func__, policy->cpu, event); -mutex_lock (&cpufreq_governor_lock);if ((policy->governor_enabled && event ==CPUFREQ_GOV_START ) || (!policy->governor_enabled && (event ==CPUFREQ_GOV_LIMITS || event ==CPUFREQ_GOV_STOP ))) { -mutex_unlock (&cpufreq_governor_lock);return -EBUSY ; }
@@ -2027,8 +2023,6 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,else if (event ==CPUFREQ_GOV_START ) policy->governor_enabled = true; -mutex_unlock (&cpufreq_governor_lock); - ret = policy->governor->governor (policy, event);if (!ret) {
@@ -2038,12 +2032,10 @@ static int __cpufreq_governor(struct cpufreq_policy *policy, policy->governor->initialized--; }else {/* Restore original values */ -mutex_lock (&cpufreq_governor_lock);if (event ==CPUFREQ_GOV_STOP ) policy->governor_enabled = true;else if (event ==CPUFREQ_GOV_START ) policy->governor_enabled = false; -mutex_unlock (&cpufreq_governor_lock); } if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
drivers/cpufreq/cpufreq_governor.h +0 -1
@@ -232,7 +232,6 @@ static inline int delay_for_sampling_rate(unsigned int sampling_rate) }extern struct mutex dbs_data_mutex; -extern struct mutex cpufreq_governor_lock;void dbs_check_cpu (struct cpufreq_policy *policy);int cpufreq_governor_dbs (struct cpufreq_policy *policy,unsigned int event); void od_register_powersave_bias_handler(unsigned int (*f)
Signature · 1 line
2.7.1.370.gb2aa7f8
Viresh Kumar
maintainerPATCH v5 4/6 cpufreq: governor: No need to manage state machine now
In reply to Viresh KumarThe cpufreq core now guarantees that policy->rwsem won't be dropped while running the ->governor callback for the CPUFREQ_GOV_POLICY_EXIT event and will be held acquired until the complete sequence of governor state changes has finished.
This allows governor state machine checks to be dropped from multiple functions in cpufreq_governor.c.
This also means that policy_dbs->policy can be initialized upfront, so the entire initialization of struct policy_dbs is carried out in one place.
--- drivers/cpufreq/cpufreq_governor.c | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-)
drivers/cpufreq/cpufreq_governor.c +5 -22
@@ -233,8 +233,10 @@ static inline void gov_clear_update_util(struct cpufreq_policy *policy)synchronize_rcu (); } -static void gov_cancel_work (struct policy_dbs_info *policy_dbs) +static void gov_cancel_work (struct cpufreq_policy *policy) { +struct policy_dbs_info *policy_dbs = policy->governor_data; +/* Tell dbs_update_util_handler() to skip queuing up work items. */ atomic_inc (&policy_dbs->skip_work);/*
@@ -330,6 +332,7 @@ static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *poliif (!policy_dbs)return NULL ; + policy_dbs->policy = policy;mutex_init (&policy_dbs->timer_mutex);atomic_set (&policy_dbs->skip_work,0 );init_irq_work (&policy_dbs->irq_work, dbs_irq_work);
@@ -460,10 +463,6 @@ static int cpufreq_governor_exit(struct cpufreq_policy *policy)struct dbs_data *dbs_data = policy_dbs->dbs_data;int count; -/* State should be equivalent to INIT */ -if (policy_dbs->policy) -return -EBUSY ; -mutex_lock (&dbs_data->mutex);list_del (&policy_dbs->list); count = --dbs_data->usage_count;
@@ -499,10 +498,6 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy)if (!policy->cur)return -EINVAL ; -/* State should be equivalent to INIT */ -if (policy_dbs->policy) -return -EBUSY ; - sampling_rate = dbs_data->sampling_rate; ignore_nice = dbs_data->ignore_nice_load;
@@ -527,7 +522,6 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy)if (ignore_nice) j_cdbs->prev_cpu_nice =kcpustat_cpu (j).cpustat[CPUTIME_NICE ]; } - policy_dbs->policy = policy;if (gov->governor ==GOV_CONSERVATIVE ) { struct cs_cpu_dbs_info_s *cs_dbs_info =
@@ -550,14 +544,7 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy)static int cpufreq_governor_stop (struct cpufreq_policy *policy) { -struct policy_dbs_info *policy_dbs = policy->governor_data; - -/* State should be equivalent to START */ -if (!policy_dbs->policy) -return -EBUSY ; - -gov_cancel_work (policy_dbs); - policy_dbs->policy =NULL ; +gov_cancel_work (policy);return 0 ; }
@@ -566,10 +553,6 @@ static int cpufreq_governor_limits(struct cpufreq_policy *policy) {struct policy_dbs_info *policy_dbs = policy->governor_data; -/* State should be equivalent to START */ -if (!policy_dbs->policy) -return -EBUSY ; -mutex_lock (&policy_dbs->timer_mutex);if (policy->max < policy->cur)__cpufreq_driver_target (policy, policy->max,CPUFREQ_RELATION_H );
Signature · 1 line
2.7.1.370.gb2aa7f8
Viresh Kumar
maintainerPATCH v5 5/6 cpufreq: conservative: Update sample_delay_ns immediately
In reply to Viresh KumarOndemand governor already updates sample_delay_ns immediately on updates to sampling rate, but conservative isn't doing that.
It was left out earlier as the code has been really complex to get that done easily. But now things are sorted out very well, and we can follow the same for conservative governor as well.
--- drivers/cpufreq/cpufreq_conservative.c | 14 ------- drivers/cpufreq/cpufreq_governor.c | 63 +++++++++++++++++++++++++++++++ drivers/cpufreq/cpufreq_governor.h | 2 + drivers/cpufreq/cpufreq_ondemand.c | 69 ---------------------------------- 4 files changed, 65 insertions(+), 83 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +0 -14
@@ -136,20 +136,6 @@ static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,return count; } -static ssize_t store_sampling_rate (struct dbs_data *dbs_data,const char *buf, -size_t count) -{ -unsigned int input; -int ret; - ret =sscanf (buf,"%u" , &input); - -if (ret !=1 ) -return -EINVAL ; - - dbs_data->sampling_rate =max (input, dbs_data->min_sampling_rate); -return count; -} -static ssize_t store_up_threshold (struct dbs_data *dbs_data,const char *buf,size_t count) {
drivers/cpufreq/cpufreq_governor.c +63 -0
69 lines not on this page · raw message
drivers/cpufreq/cpufreq_governor.h +2 -0
@@ -238,4 +238,6 @@ void od_register_powersave_bias_handler(unsigned int (*f) (struct cpufreq_policy *, unsigned int, unsigned int), unsigned int powersave_bias);void od_unregister_powersave_bias_handler (void ); +ssize_t store_sampling_rate (struct dbs_data *dbs_data,const char *buf, +size_t count);#endif /* _CPUFREQ_GOVERNOR_H */
drivers/cpufreq/cpufreq_ondemand.c +0 -69
75 lines not on this page · raw message
Signature · 1 line
2.7.1.370.gb2aa7f8
Viresh Kumar
maintainerPATCH v5 6/6 cpufreq: ondemand: Rearrange od_dbs_timer() to avoid updating delay
In reply to Viresh Kumar'delay' is updated properly in all paths of the routine od_dbs_timer(), leaving just one. And can be 0 only in that case.
Move the update to 'delay' as an else part of the if block.
--- drivers/cpufreq/cpufreq_ondemand.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/cpufreq_ondemand.c +4 -5
@@ -194,7 +194,7 @@ static unsigned int od_dbs_timer(struct cpufreq_policy *policy)struct policy_dbs_info *policy_dbs = policy->governor_data;struct dbs_data *dbs_data = policy_dbs->dbs_data;struct od_cpu_dbs_info_s *dbs_info = &per_cpu (od_cpu_dbs_info, policy->cpu); -int delay =0 , sample_type = dbs_info->sample_type; +int delay, sample_type = dbs_info->sample_type;/* Common NORMAL_SAMPLE setup */ dbs_info->sample_type =OD_NORMAL_SAMPLE ;
@@ -208,13 +208,12 @@ static unsigned int od_dbs_timer(struct cpufreq_policy *policy)/* Setup timer for SUB_SAMPLE */ dbs_info->sample_type =OD_SUB_SAMPLE ; delay = dbs_info->freq_hi_jiffies; + }else { + delay =delay_for_sampling_rate (dbs_data->sampling_rate + * dbs_info->rate_mult); } } -if (!delay) - delay =delay_for_sampling_rate (dbs_data->sampling_rate - * dbs_info->rate_mult); -return delay; }
Signature · 1 line
2.7.1.370.gb2aa7f8
Quoted 19 lines · Viresh Kumar
Hi Rafael,
These are rest of the patches that fix some more locking issues with
policy->rwsemand do some minor optimization/cleanups.
V4->V5:
- Changelog updated for 1-2 as suggested by Rafael
- 4th patch is dropped, which moved common tunable callbacks tocpufreq_governor.c
- 5-7 are resend on the V5 sent earlier, as reply to V4 of the series.Viresh Kumar (6):
cpufreq: Merge cpufreq_offline_prepare/finish routines
cpufreq: Call__cpufreq_governor()withpolicy->rwsemheld
cpufreq: Removecpufreq_governor_lock
cpufreq: governor: No need to manage state machine now
cpufreq: conservative: Updatesample_delay_nsimmediately
cpufreq: ondemand: Rearrangeod_dbs_timer()to avoid updating delay
Quoted 6 lines
drivers/cpufreq/cpufreq.c | 93 ++++++++++++++++------------------ drivers/cpufreq/cpufreq_conservative.c | 14 ----- drivers/cpufreq/cpufreq_governor.c | 90 ++++++++++++++++++++++++-------- drivers/cpufreq/cpufreq_governor.h | 3 +- drivers/cpufreq/cpufreq_ondemand.c | 78 ++-------------------------- 5 files changed, 117 insertions(+), 161 deletions(-)
OK, queued up for 0-day testing with some rebase and changelog (sigh) fixups.
Thanks,
Rafael