CPUFreq: governors: further cleanups
Viresh Kumar
maintainerv2 cover CPUFreq: governors: further cleanups
Hi Rafael,
This series fixes few more possible race conditions. Over that there is some non-trivial cleanup, in order to simplify code.
Preeti did review some of them before she left and shared concerns on others, all that is sorted out now.
V1->V2:
- Dropped 2/10 from V1 as it wasn't required
- 3/10 saw some changes due to above patch being dropped
- 7/10 changed a bit as we check for pending work items by looking at shared->policy, rather than calling delayed_work_pending. We wanted to check if governor is operational or not and the new check is enough for that.
Viresh Kumar (9):
cpufreq: Use __func__ to print function's name
cpufreq: conservative: remove 'enable' field
cpufreq: ondemand: only queue canceled works from
update_sampling_rate()
cpufreq: governor: Drop __gov_queue_work()
cpufreq: ondemand: Drop unnecessary locks from update_sampling_rate()
cpufreq: ondemand: queue work for policy->cpus together
cpufreq: ondemand: update sampling rate immidiately
cpufreq: governor: Quit work-handlers early if governor is stopped
cpufreq: Get rid of ->governor_enabled and its lock
drivers/cpufreq/cpufreq.c | 27 +---------- drivers/cpufreq/cpufreq_conservative.c | 38 +++++++++------ drivers/cpufreq/cpufreq_governor.c | 86 +++++++++++++++------------------- drivers/cpufreq/cpufreq_governor.h | 5 +- drivers/cpufreq/cpufreq_ondemand.c | 54 +++++++-------------- include/linux/cpufreq.h | 1 - 6 files changed, 81 insertions(+), 130 deletions(-)
Signature · 1 line
2.4.0
Viresh Kumar
maintainerPATCH v2 1/9 cpufreq: Use __func__ to print function's name
Its better to use __func__ to print functions name instead of writing the name in the print statement. This also has the advantage that a change in function's name doesn't force us to change the print message as well.
--- drivers/cpufreq/cpufreq.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/cpufreq/cpufreq.c +1 -2
@@ -2074,8 +2074,7 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,if (!try_module_get (policy->governor->owner))return -EINVAL ; -pr_debug ("__cpufreq_governor for CPU %u, event %u\n" , - policy->cpu, event); +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)
Signature · 1 line
2.4.0
Re: [PATCH V2 1/9] cpufreq: Use __func__ to print function's name
Quoted 7 lines · Viresh Kumar
Its better to use
__func__to print functions name instead of writing the name in the print statement. This also has the advantage that a change in function's name doesn't force us to change the print message as well.Reviewed-by Preeti U Murthy <[email protected]>Signed-off-by Viresh Kumar <[email protected]>
OK, this is simple enough. Applied, thanks!
Quoted 19 lines
--- drivers/cpufreq/cpufreq.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/cpufreq/cpufreq.c +1
-2@@ -2074,8 +2074,7 @@ static int __cpufreq_governor(struct cpufreq_policy *policy,if (!try_module_get (policy->governor->owner))return -EINVAL ; -pr_debug ("__cpufreq_governor for CPU %u, event %u\n" , - policy->cpu, event); +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)
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Viresh Kumar
maintainerPATCH v2 2/9 cpufreq: conservative: remove 'enable' field
In reply to Viresh KumarConservative governor has its own 'enable' field to check if conservative governor is used for a CPU or not
This can be checked by policy->governor with 'cpufreq_gov_conservative' and so this field can be dropped.
Because its not guaranteed that dbs_info->cdbs.shared will a valid pointer for all CPUs (will be NULL for CPUs that don't use ondemand/conservative governors), we can't use it anymore. Lets get policy with cpufreq_cpu_get() instead.
--- drivers/cpufreq/cpufreq_conservative.c | 34 +++++++++++++++++++++------------- drivers/cpufreq/cpufreq_governor.c | 12 +----------- drivers/cpufreq/cpufreq_governor.h | 1 - 3 files changed, 22 insertions(+), 25 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +21 -13
@@ -23,6 +23,19 @@ static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info); +static int cs_cpufreq_governor_dbs (struct cpufreq_policy *policy, +unsigned int event); + +#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE +static +#endif +struct cpufreq_governor cpufreq_gov_conservative = { + .name ="conservative" , + .governor = cs_cpufreq_governor_dbs, + .max_transition_latency =TRANSITION_LATENCY_LIMIT , + .owner =THIS_MODULE , +}; +static inline unsigned int get_freq_target (struct cs_dbs_tuners *cs_tuners,struct cpufreq_policy *policy) {
@@ -119,12 +132,14 @@ static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,struct cpufreq_freqs *freq = data;struct cs_cpu_dbs_info_s *dbs_info = &per_cpu (cs_cpu_dbs_info, freq->cpu); -struct cpufreq_policy *policy; +struct cpufreq_policy *policy =cpufreq_cpu_get (freq->cpu); -if (!dbs_info->enable) +if (!policy)return 0 ; - policy = dbs_info->cdbs.shared->policy; +/* policy isn't governed by conservative governor */ +if (policy->governor != &cpufreq_gov_conservative) + goto policy_put;/* * we only care if our internally tracked freq moves outside the 'valid'
@@ -134,6 +149,9 @@ static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, || dbs_info->requested_freq < policy->min) dbs_info->requested_freq = freq->new; +policy_put: +cpufreq_cpu_put (policy); +return 0 ; }
@@ -367,16 +385,6 @@ static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,return cpufreq_governor_dbs (policy, &cs_dbs_cdata, event); } -#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE -static -#endif -struct cpufreq_governor cpufreq_gov_conservative = { - .name ="conservative" , - .governor = cs_cpufreq_governor_dbs, - .max_transition_latency =TRANSITION_LATENCY_LIMIT , - .owner =THIS_MODULE , -}; -static int __init cpufreq_gov_dbs_init (void ) {return cpufreq_register_governor (&cpufreq_gov_conservative);
drivers/cpufreq/cpufreq_governor.c +1 -11
@@ -463,7 +463,6 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy, cdata->get_cpu_dbs_info_s (cpu); cs_dbs_info->down_skip =0 ; - cs_dbs_info->enable =1 ; cs_dbs_info->requested_freq = policy->cur; }else {struct od_ops *od_ops = cdata->gov_ops;
@@ -482,9 +481,7 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy,static int cpufreq_governor_stop (struct cpufreq_policy *policy,struct dbs_data *dbs_data) { -struct common_dbs_data *cdata = dbs_data->cdata; -unsigned int cpu = policy->cpu; -struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs (cpu); +struct cpu_dbs_info *cdbs = dbs_data->cdata->get_cpu_cdbs (policy->cpu);struct cpu_common_dbs_info *shared = cdbs->shared;/* State should be equivalent to START */
@@ -493,13 +490,6 @@ static int cpufreq_governor_stop(struct cpufreq_policy *policy,gov_cancel_work (dbs_data, policy); -if (cdata->governor ==GOV_CONSERVATIVE ) { -struct cs_cpu_dbs_info_s *cs_dbs_info = - cdata->get_cpu_dbs_info_s (cpu); - - cs_dbs_info->enable =0 ; - } - shared->policy =NULL ;mutex_destroy (&shared->timer_mutex);return 0 ;
drivers/cpufreq/cpufreq_governor.h +0 -1
@@ -170,7 +170,6 @@ struct cs_cpu_dbs_info_s {struct cpu_dbs_info cdbs;unsigned int down_skip;unsigned int requested_freq; - unsigned int enable:1; };/* Per policy Governors sysfs tunables */
Signature · 1 line
2.4.0
Re: [PATCH V2 2/9] cpufreq: conservative: remove 'enable' field
Quoted 10 lines · Viresh Kumar
Conservative governor has its own 'enable' field to check if conservative governor is used for a CPU or not
This can be checked by
policy->governorwith 'cpufreq_gov_conservative' and so this field can be dropped.Because its not guaranteed that
dbs_info->cdbs.sharedwill a valid pointer for all CPUs (will be NULL for CPUs that don't use ondemand/conservative governors), we can't use it anymore. Lets get policy withcpufreq_cpu_get()instead.
But previously, if the enable bit was set, we actually new that the pointer was valid, right?
Quoted 33 lines
Signed-off-by Viresh Kumar <[email protected]>--- drivers/cpufreq/cpufreq_conservative.c | 34 +++++++++++++++++++++------------- drivers/cpufreq/cpufreq_governor.c | 12 +----------- drivers/cpufreq/cpufreq_governor.h | 1 - 3 files changed, 22 insertions(+), 25 deletions(-)drivers/cpufreq/cpufreq_conservative.c +13
-0@@ -23,6 +23,19 @@ static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info); +static int cs_cpufreq_governor_dbs (struct cpufreq_policy *policy, +unsigned int event); + +#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE +static +#endif +struct cpufreq_governor cpufreq_gov_conservative = { + .name ="conservative" , + .governor = cs_cpufreq_governor_dbs, + .max_transition_latency =TRANSITION_LATENCY_LIMIT , + .owner =THIS_MODULE , +}; +static inline unsigned int get_freq_target (struct cs_dbs_tuners *cs_tuners,struct cpufreq_policy *policy) {@@ -119,12 +132,14 @@ static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,struct cpufreq_freqs *freq = data;
drivers/cpufreq/cpufreq_conservative.c static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, · line 127
Quoted 10 lines
drivers/cpufreq/cpufreq_conservative.c +2
-3struct cs_cpu_dbs_info_s *dbs_info = &per_cpu (cs_cpu_dbs_info, freq->cpu); -struct cpufreq_policy *policy; +struct cpufreq_policy *policy =cpufreq_cpu_get (freq->cpu); -if (!dbs_info->enable) +if (!policy)return 0 ; - policy = dbs_info->cdbs.shared->policy;
So here we could get to the policy directly. After the change we have to:
- acquire cpufreq_rwsem
- acquire cpufreq_driver_lock
- go the kobject_get on policy->kobj
and then finally drop the reference to the kobject when we're done.
So may I ask where exactly is the improvement?
drivers/cpufreq/cpufreq_conservative.c static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, · line 145
Quoted 6 lines
drivers/cpufreq/cpufreq_conservative.c +3
-0+/* policy isn't governed by conservative governor */ +if (policy->governor != &cpufreq_gov_conservative) + goto policy_put;/* * we only care if our internally tracked freq moves outside the 'valid'
Thanks,
Rafael
Viresh Kumar
maintainerRe: [PATCH V2 2/9] cpufreq: conservative: remove 'enable' field
Quoted 14 lines · Rafael J. Wysocki
Quoted 14 lines
static inline unsigned int get_freq_target(struct
cs_dbs_tuners*cs_tuners,drivers/cpufreq/cpufreq_conservative.c +2
-2struct cpufreq_policy *policy) {@@ -119,12 +132,14 @@ static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,struct cpufreq_freqs *freq = data;struct cs_cpu_dbs_info_s *dbs_info = &per_cpu (cs_cpu_dbs_info, freq->cpu); -struct cpufreq_policy *policy; +struct cpufreq_policy *policy =cpufreq_cpu_get (freq->cpu); -if (!dbs_info->enable) +if (!policy)return 0 ;
Quoted 7 lines
drivers/cpufreq/cpufreq_conservative.c static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, · line 127
Quoted 1 line
drivers/cpufreq/cpufreq_conservative.c +0
-1- policy = dbs_info->cdbs.shared->policy;So here we could get to the policy directly. After the change we have to:
- acquire
cpufreq_rwsem
- acquirecpufreq_driver_lock
- go thekobject_getonpolicy->kobj
Hmm, actually we can do cpufreq_cpu_get_raw() here as this is getting called from notifier and policy isn't going to get freed for sure.
And then it wouldn't be that bad.
Quoted 3 lines
and then finally drop the reference to the kobject when we're done.
So may I ask where exactly is the improvement?
Agree. Let me resend it quickly.
Quoted 9 lines
drivers/cpufreq/cpufreq_conservative.c static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, · line 145
Quoted 6 lines
drivers/cpufreq/cpufreq_conservative.c +3
-0+/* policy isn't governed by conservative governor */ +if (policy->governor != &cpufreq_gov_conservative) + goto policy_put;/* * we only care if our internally tracked freq moves outside the 'valid' Thanks,
Rafael
Signature · 1 line
viresh
Viresh Kumar
maintainerPATCH v3 2/9 cpufreq: conservative: remove 'enable' field
In reply to Rafael J. WysockiConservative governor has its own 'enable' field to check if conservative governor is used for a CPU or not
This can be checked by policy->governor with 'cpufreq_gov_conservative' and so this field can be dropped.
Because its not guaranteed that dbs_info->cdbs.shared will a valid pointer for all CPUs (will be NULL for CPUs that don't use ondemand/conservative governors), we can't use it anymore. Lets get policy with cpufreq_cpu_get_raw() instead.
---
Updates: use cpufreq_cpu_get_raw() instead of cpufreq_cpu_get().
drivers/cpufreq/cpufreq.c | 3 ++- drivers/cpufreq/cpufreq_conservative.c | 31 ++++++++++++++++++------------- drivers/cpufreq/cpufreq_governor.c | 12 +----------- drivers/cpufreq/cpufreq_governor.h | 2 +- 4 files changed, 22 insertions(+), 26 deletions(-)
drivers/cpufreq/cpufreq.c +2 -1
@@ -239,12 +239,13 @@ int cpufreq_generic_init(struct cpufreq_policy *policy,EXPORT_SYMBOL_GPL (cpufreq_generic_init);/* Only for cpufreq core internal use */ -static struct cpufreq_policy *cpufreq_cpu_get_raw (unsigned int cpu) +struct cpufreq_policy *cpufreq_cpu_get_raw (unsigned int cpu) {struct cpufreq_policy *policy =per_cpu (cpufreq_cpu_data, cpu);return policy &&cpumask_test_cpu (cpu, policy->cpus) ? policy :NULL ; } +EXPORT_SYMBOL_GPL (cpufreq_cpu_get_raw);unsigned int cpufreq_generic_get (unsigned int cpu) {
drivers/cpufreq/cpufreq_conservative.c +18 -13
@@ -23,6 +23,19 @@ static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info); +static int cs_cpufreq_governor_dbs (struct cpufreq_policy *policy, +unsigned int event); + +#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE +static +#endif +struct cpufreq_governor cpufreq_gov_conservative = { + .name ="conservative" , + .governor = cs_cpufreq_governor_dbs, + .max_transition_latency =TRANSITION_LATENCY_LIMIT , + .owner =THIS_MODULE , +}; +static inline unsigned int get_freq_target (struct cs_dbs_tuners *cs_tuners,struct cpufreq_policy *policy) {
@@ -119,12 +132,14 @@ static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,struct cpufreq_freqs *freq = data;struct cs_cpu_dbs_info_s *dbs_info = &per_cpu (cs_cpu_dbs_info, freq->cpu); -struct cpufreq_policy *policy; +struct cpufreq_policy *policy =cpufreq_cpu_get_raw (freq->cpu); -if (!dbs_info->enable) +if (!policy)return 0 ; - policy = dbs_info->cdbs.shared->policy; +/* policy isn't governed by conservative governor */ +if (policy->governor != &cpufreq_gov_conservative) +return 0 ;/* * we only care if our internally tracked freq moves outside the 'valid'
@@ -367,16 +382,6 @@ static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,return cpufreq_governor_dbs (policy, &cs_dbs_cdata, event); } -#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE -static -#endif -struct cpufreq_governor cpufreq_gov_conservative = { - .name ="conservative" , - .governor = cs_cpufreq_governor_dbs, - .max_transition_latency =TRANSITION_LATENCY_LIMIT , - .owner =THIS_MODULE , -}; -static int __init cpufreq_gov_dbs_init (void ) {return cpufreq_register_governor (&cpufreq_gov_conservative);
drivers/cpufreq/cpufreq_governor.c +1 -11
@@ -463,7 +463,6 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy, cdata->get_cpu_dbs_info_s (cpu); cs_dbs_info->down_skip =0 ; - cs_dbs_info->enable =1 ; cs_dbs_info->requested_freq = policy->cur; }else {struct od_ops *od_ops = cdata->gov_ops;
@@ -482,9 +481,7 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy,static int cpufreq_governor_stop (struct cpufreq_policy *policy,struct dbs_data *dbs_data) { -struct common_dbs_data *cdata = dbs_data->cdata; -unsigned int cpu = policy->cpu; -struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs (cpu); +struct cpu_dbs_info *cdbs = dbs_data->cdata->get_cpu_cdbs (policy->cpu);struct cpu_common_dbs_info *shared = cdbs->shared;/* State should be equivalent to START */
@@ -493,13 +490,6 @@ static int cpufreq_governor_stop(struct cpufreq_policy *policy,gov_cancel_work (dbs_data, policy); -if (cdata->governor ==GOV_CONSERVATIVE ) { -struct cs_cpu_dbs_info_s *cs_dbs_info = - cdata->get_cpu_dbs_info_s (cpu); - - cs_dbs_info->enable =0 ; - } - shared->policy =NULL ;mutex_destroy (&shared->timer_mutex);return 0 ;
drivers/cpufreq/cpufreq_governor.h +1 -1
@@ -170,7 +170,6 @@ struct cs_cpu_dbs_info_s {struct cpu_dbs_info cdbs;unsigned int down_skip;unsigned int requested_freq; - unsigned int enable:1; };/* Per policy Governors sysfs tunables */
@@ -270,6 +269,7 @@ static ssize_t show_sampling_rate_min_gov_pol \extern struct mutex cpufreq_governor_lock; +struct cpufreq_policy *cpufreq_cpu_get_raw (unsigned int cpu);void dbs_check_cpu (struct dbs_data *dbs_data,int cpu);int cpufreq_governor_dbs (struct cpufreq_policy *policy,struct common_dbs_data *cdata,unsigned int event);
Signature · 1 line
2.4.0
Viresh Kumar
maintainerPATCH v2 3/9 cpufreq: ondemand: only queue canceled works from update_sampling_rate()
In reply to Viresh KumarThe sampling rate is updated with a call to update_sampling_rate(), and we process CPUs one by one here. While the work is canceled on per-cpu basis, it is getting queued (by mistake) for all policy->cpus.
This would result in wasting cpu cycles for queuing works which are already queued and never canceled.
This patch changes this behavior to queue work only on the cpu, for which it was canceled earlier.
To do that, replace 'modify_all' parameter to gov_queue_work() with a mask of CPUs. Also the last parameter to ->gov_dbs_timer() was named 'modify_all' earlier, but its purpose was to decide if load has to be evaluated again or not. Lets rename that to load_eval.
--- drivers/cpufreq/cpufreq_conservative.c | 4 ++-- drivers/cpufreq/cpufreq_governor.c | 30 ++++++++++-------------------- drivers/cpufreq/cpufreq_governor.h | 4 ++-- drivers/cpufreq/cpufreq_ondemand.c | 7 ++++--- 4 files changed, 18 insertions(+), 27 deletions(-)
drivers/cpufreq/cpufreq_conservative.c +2 -2
@@ -116,11 +116,11 @@ static void cs_check_cpu(int cpu, unsigned int load) }static unsigned int cs_dbs_timer (struct cpu_dbs_info *cdbs, -struct dbs_data *dbs_data,bool modify_all) +struct dbs_data *dbs_data,bool load_eval) {struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; -if (modify_all) +if (load_eval)dbs_check_cpu (dbs_data, cdbs->shared->policy->cpu);return delay_for_sampling_rate (cs_tuners->sampling_rate);
drivers/cpufreq/cpufreq_governor.c +10 -20
@@ -167,7 +167,7 @@ static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data, }void gov_queue_work (struct dbs_data *dbs_data,struct cpufreq_policy *policy, -unsigned int delay,bool all_cpus) +unsigned int delay,const struct cpumask *cpus) {int i;
@@ -175,19 +175,8 @@ void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,if (!policy->governor_enabled) goto out_unlock; -if (!all_cpus) { -/* -* Use raw_smp_processor_id() to avoid preemptible warnings. -* We know that this is only called with all_cpus == false from -* works that have been queued with *_work_on() functions and -* those works are canceled during CPU_DOWN_PREPARE so they -* can't possibly run on any other CPU. -*/ -__gov_queue_work (raw_smp_processor_id (), dbs_data, delay); - }else { -for_each_cpu (i, policy->cpus) -__gov_queue_work (i, dbs_data, delay); - } +for_each_cpu (i, cpus) +__gov_queue_work (i, dbs_data, delay); out_unlock:mutex_unlock (&cpufreq_governor_lock);
@@ -232,7 +221,8 @@ static void dbs_timer(struct work_struct *work)struct cpufreq_policy *policy = shared->policy;struct dbs_data *dbs_data = policy->governor_data;unsigned int sampling_rate, delay; -bool modify_all = true; +const struct cpumask *cpus; +bool load_eval;mutex_lock (&shared->timer_mutex);
@@ -246,11 +236,11 @@ static void dbs_timer(struct work_struct *work) sampling_rate = od_tuners->sampling_rate; } -if (!need_load_eval (cdbs->shared, sampling_rate)) - modify_all = false; + load_eval =need_load_eval (cdbs->shared, sampling_rate); + cpus = load_eval ? policy->cpus :cpumask_of (raw_smp_processor_id ()); - delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, modify_all); -gov_queue_work (dbs_data, policy, delay, modify_all); + delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, load_eval); +gov_queue_work (dbs_data, policy, delay, cpus);mutex_unlock (&shared->timer_mutex); }
@@ -474,7 +464,7 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy, }gov_queue_work (dbs_data, policy,delay_for_sampling_rate (sampling_rate), - true); + policy->cpus);return 0 ; }
drivers/cpufreq/cpufreq_governor.h +2 -2
@@ -211,7 +211,7 @@ struct common_dbs_data {void *(*get_cpu_dbs_info_s)(int cpu);unsigned int (*gov_dbs_timer)(struct cpu_dbs_info *cdbs,struct dbs_data *dbs_data, -bool modify_all); +bool load_eval);void (*gov_check_cpu)(int cpu,unsigned int load);int (*init)(struct dbs_data *dbs_data,bool notify);void (*exit)(struct dbs_data *dbs_data,bool notify);
@@ -273,7 +273,7 @@ void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);int cpufreq_governor_dbs (struct cpufreq_policy *policy,struct common_dbs_data *cdata,unsigned int event);void gov_queue_work (struct dbs_data *dbs_data,struct cpufreq_policy *policy, -unsigned int delay,bool all_cpus); +unsigned int delay,const struct cpumask *cpus);void od_register_powersave_bias_handler (unsigned int (*f) (struct cpufreq_policy *,unsigned int ,unsigned int ),unsigned int powersave_bias);
drivers/cpufreq/cpufreq_ondemand.c +4 -3
@@ -192,7 +192,7 @@ static void od_check_cpu(int cpu, unsigned int load) }static unsigned int od_dbs_timer (struct cpu_dbs_info *cdbs, -struct dbs_data *dbs_data,bool modify_all) +struct dbs_data *dbs_data,bool load_eval) {struct cpufreq_policy *policy = cdbs->shared->policy;unsigned int cpu = policy->cpu;
@@ -201,7 +201,7 @@ static unsigned int od_dbs_timer(struct cpu_dbs_info *cdbs,struct od_dbs_tuners *od_tuners = dbs_data->tuners;int delay =0 , sample_type = dbs_info->sample_type; -if (!modify_all) +if (!load_eval) goto max_delay;/* Common NORMAL_SAMPLE setup */
@@ -284,7 +284,8 @@ static void update_sampling_rate(struct dbs_data *dbs_data,mutex_lock (&dbs_info->cdbs.shared->timer_mutex);gov_queue_work (dbs_data, policy, -usecs_to_jiffies (new_rate), true); +usecs_to_jiffies (new_rate), +cpumask_of (cpu)); }mutex_unlock (&dbs_info->cdbs.shared->timer_mutex);
Signature · 1 line
2.4.0
Re: [PATCH V2 3/9] cpufreq: ondemand: only queue canceled works from update_sampling_rate()
Quoted 4 lines · Viresh Kumar
The sampling rate is updated with a call to
update_sampling_rate(), and we process CPUs one by one here. While the work is canceled on per-cpu basis, it is getting queued (by mistake) for allpolicy->cpus.
Quoted 8 lines
This would result in wasting cpu cycles for queuing works which are already queued and never canceled.
This patch changes this behavior to queue work only on the cpu, for which it was canceled earlier.
To do that, replace '
modify_all' parameter togov_queue_work()with a mask of CPUs.
There really are two cases, either you pass a CPU or gov_queue_work() has to walk policy->cpus. Doing it the way you did hides that IMO.
I'd simply pass an int and use a special value to indicate that policy->cpus is to be walked.
Quoted 46 lines
Also the last parameter to ->gov_dbs_timer() was named '
modify_all' earlier, but its purpose was to decide if load has to be evaluated again or not. Lets rename that toload_eval.Fixes 031299b3be30 ("cpufreq: governors: Avoid unnecessary per cpu timer interrupts")Signed-off-by Viresh Kumar <[email protected]>--- drivers/cpufreq/cpufreq_conservative.c | 4 ++-- drivers/cpufreq/cpufreq_governor.c | 30 ++++++++++-------------------- drivers/cpufreq/cpufreq_governor.h | 4 ++-- drivers/cpufreq/cpufreq_ondemand.c | 7 ++++--- 4 files changed, 18 insertions(+), 27 deletions(-)drivers/cpufreq/cpufreq_conservative.c +2
-2@@ -116,11 +116,11 @@ static void cs_check_cpu(int cpu, unsigned int load) }static unsigned int cs_dbs_timer (struct cpu_dbs_info *cdbs, -struct dbs_data *dbs_data,bool modify_all) +struct dbs_data *dbs_data,bool load_eval) {struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; -if (modify_all) +if (load_eval)dbs_check_cpu (dbs_data, cdbs->shared->policy->cpu);return delay_for_sampling_rate (cs_tuners->sampling_rate);drivers/cpufreq/cpufreq_governor.c +1
-1@@ -167,7 +167,7 @@ static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data, }void gov_queue_work (struct dbs_data *dbs_data,struct cpufreq_policy *policy, -unsigned int delay,bool all_cpus) +unsigned int delay,const struct cpumask *cpus) {int i;@@ -175,19 +175,8 @@ void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,if (!policy->governor_enabled)
drivers/cpufreq/cpufreq_governor.c void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy, · line 185
Quoted 10 lines
drivers/cpufreq/cpufreq_governor.c +0
-8goto out_unlock; -if (!all_cpus) { -/* -* Use raw_smp_processor_id() to avoid preemptible warnings. -* We know that this is only called with all_cpus == false from -* works that have been queued with *_work_on() functions and -* those works are canceled during CPU_DOWN_PREPARE so they -* can't possibly run on any other CPU. -*/
This was a useful comment and it should be moved along the logic it was supposed to explain and not just dropped.
Quoted 11 lines
drivers/cpufreq/cpufreq_governor.c +2
-5-__gov_queue_work (raw_smp_processor_id (), dbs_data, delay); - }else { -for_each_cpu (i, policy->cpus) -__gov_queue_work (i, dbs_data, delay); - } +for_each_cpu (i, cpus) +__gov_queue_work (i, dbs_data, delay); out_unlock:mutex_unlock (&cpufreq_governor_lock);
drivers/cpufreq/cpufreq_governor.c static void dbs_timer(struct work_struct *work) · line 224
Quoted 5 lines
drivers/cpufreq/cpufreq_governor.c +1
-1@@ -232,7 +221,8 @@ static void dbs_timer(struct work_struct *work)struct cpufreq_policy *policy = shared->policy;struct dbs_data *dbs_data = policy->governor_data;unsigned int sampling_rate, delay; -bool modify_all = true; +const struct cpumask *cpus;
I don't think this local variable is necessary.
Quoted 75 lines
drivers/cpufreq/cpufreq_governor.c +6
-5+bool load_eval;mutex_lock (&shared->timer_mutex);@@ -246,11 +236,11 @@ static void dbs_timer(struct work_struct *work) sampling_rate = od_tuners->sampling_rate; } -if (!need_load_eval (cdbs->shared, sampling_rate)) - modify_all = false; + load_eval =need_load_eval (cdbs->shared, sampling_rate); + cpus = load_eval ? policy->cpus :cpumask_of (raw_smp_processor_id ()); - delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, modify_all); -gov_queue_work (dbs_data, policy, delay, modify_all); + delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, load_eval); +gov_queue_work (dbs_data, policy, delay, cpus);mutex_unlock (&shared->timer_mutex); }@@ -474,7 +464,7 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy, }gov_queue_work (dbs_data, policy,delay_for_sampling_rate (sampling_rate), - true); + policy->cpus);return 0 ; }drivers/cpufreq/cpufreq_governor.h +2
-2@@ -211,7 +211,7 @@ struct common_dbs_data {void *(*get_cpu_dbs_info_s)(int cpu);unsigned int (*gov_dbs_timer)(struct cpu_dbs_info *cdbs,struct dbs_data *dbs_data, -bool modify_all); +bool load_eval);void (*gov_check_cpu)(int cpu,unsigned int load);int (*init)(struct dbs_data *dbs_data,bool notify);void (*exit)(struct dbs_data *dbs_data,bool notify);@@ -273,7 +273,7 @@ void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);int cpufreq_governor_dbs (struct cpufreq_policy *policy,struct common_dbs_data *cdata,unsigned int event);void gov_queue_work (struct dbs_data *dbs_data,struct cpufreq_policy *policy, -unsigned int delay,bool all_cpus); +unsigned int delay,const struct cpumask *cpus);void od_register_powersave_bias_handler (unsigned int (*f) (struct cpufreq_policy *,unsigned int ,unsigned int ),unsigned int powersave_bias);drivers/cpufreq/cpufreq_ondemand.c +2
-2@@ -192,7 +192,7 @@ static void od_check_cpu(int cpu, unsigned int load) }static unsigned int od_dbs_timer (struct cpu_dbs_info *cdbs, -struct dbs_data *dbs_data,bool modify_all) +struct dbs_data *dbs_data,bool load_eval) {struct cpufreq_policy *policy = cdbs->shared->policy;unsigned int cpu = policy->cpu;@@ -201,7 +201,7 @@ static unsigned int od_dbs_timer(struct cpu_dbs_info *cdbs,struct od_dbs_tuners *od_tuners = dbs_data->tuners;int delay =0 , sample_type = dbs_info->sample_type; -if (!modify_all) +if (!load_eval) goto max_delay;/* Common NORMAL_SAMPLE setup */
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 291
Quoted 9 lines
drivers/cpufreq/cpufreq_ondemand.c +2
-1@@ -284,7 +284,8 @@ static void update_sampling_rate(struct dbs_data *dbs_data,mutex_lock (&dbs_info->cdbs.shared->timer_mutex);gov_queue_work (dbs_data, policy, -usecs_to_jiffies (new_rate), true); +usecs_to_jiffies (new_rate), +cpumask_of (cpu)); }mutex_unlock (&dbs_info->cdbs.shared->timer_mutex);
Thanks,
Rafael
Viresh Kumar
maintainerRe: [PATCH V2 3/9] cpufreq: ondemand: only queue canceled works from update_sampling_rate()
Quoted 2 lines · Rafael J. Wysocki
There really are two cases, either you pass a CPU or
gov_queue_work()has to walkpolicy->cpus.
Right (At least for now, we are doing just that.)
Quoted 1 line
Doing it the way you did hides that IMO.
Maybe. But I see it otherwise. Adding special meaning to a variable (like int cpu == -1 being the special case to specify policy->cpus) hides things morei, as we need to look at how it is decoded finally in the routine gov_queue_work().
But if we send a mask instead, it is very clear by reading the callers site, what we are trying to do.
Quoted 2 lines
I'd simply pass an int and use a special value to indicate that
policy->cpusis to be walked.
Like cpu == -1 thing? Or something else?
Quoted 11 lines
drivers/cpufreq/cpufreq_governor.c void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy, · line 185
Quoted 8 lines
drivers/cpufreq/cpufreq_governor.c +0
-8-if (!all_cpus) { -/* -* Use raw_smp_processor_id() to avoid preemptible warnings. -* We know that this is only called with all_cpus == false from -* works that have been queued with *_work_on() functions and -* those works are canceled during CPU_DOWN_PREPARE so they -* can't possibly run on any other CPU. -*/ This was a useful comment and it should be moved along the logic it was supposed to explain and not just dropped.
Sigh
Quoted 32 lines
Quoted 11 lines
drivers/cpufreq/cpufreq_governor.c +2
-5-__gov_queue_work (raw_smp_processor_id (), dbs_data, delay); - }else { -for_each_cpu (i, policy->cpus) -__gov_queue_work (i, dbs_data, delay); - } +for_each_cpu (i, cpus) +__gov_queue_work (i, dbs_data, delay); out_unlock:mutex_unlock (&cpufreq_governor_lock);drivers/cpufreq/cpufreq_governor.c static void dbs_timer(struct work_struct *work) · line 224
Quoted 5 lines
drivers/cpufreq/cpufreq_governor.c +1
-1@@ -232,7 +221,8 @@ static void dbs_timer(struct work_struct *work)struct cpufreq_policy *policy = shared->policy;struct dbs_data *dbs_data = policy->governor_data;unsigned int sampling_rate, delay; -bool modify_all = true; +const struct cpumask *cpus;I don't think this local variable is necessary.
Quoted 13 lines
drivers/cpufreq/cpufreq_governor.c +3
-2+bool load_eval;mutex_lock (&shared->timer_mutex);@@ -246,11 +236,11 @@ static void dbs_timer(struct work_struct *work) sampling_rate = od_tuners->sampling_rate; } -if (!need_load_eval (cdbs->shared, sampling_rate)) - modify_all = false; + load_eval =need_load_eval (cdbs->shared, sampling_rate); + cpus = load_eval ? policy->cpus :cpumask_of (raw_smp_processor_id ());
Quoted 4 lines
drivers/cpufreq/cpufreq_governor.c static void dbs_timer(struct work_struct *work) · line 243
Quoted 4 lines
drivers/cpufreq/cpufreq_governor.c +2
-2- delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, modify_all); -gov_queue_work (dbs_data, policy, delay, modify_all); + delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, load_eval); +gov_queue_work (dbs_data, policy, delay, cpus);
Avoiding that local variable would have made this a little longer, but I can surely drop it :)
gov_queue_work(dbs_data, policy, delay,
load_eval ? policy->cpus :
cpumask_of(raw_smp_processor_id());Signature · 1 line
viresh
Re: [PATCH V2 3/9] cpufreq: ondemand: only queue canceled works from update_sampling_rate()
Quoted 4 lines · Viresh Kumar
Quoted 2 lines · Rafael J. Wysocki
There really are two cases, either you pass a CPU or
gov_queue_work()has to walkpolicy->cpus.
Quoted 8 lines
Right (At least for now, we are doing just that.)
Quoted 1 line
Doing it the way you did hides that IMO.
Maybe. But I see it otherwise. Adding special meaning to a variable (like int cpu == -1 being the special case to specify
policy->cpus) hides things morei, as we need to look at how it is decoded finally in the routinegov_queue_work().
Oh well.
I've just realized that if you combined this patch with the [6/9], you wouldn't need to make any changes to gov_queue_work() at all, because that patch removes the case in point entirely.
Thanks,
Rafael
Viresh Kumar
maintainerRe: [PATCH V2 3/9] cpufreq: ondemand: only queue canceled works from update_sampling_rate()
Quoted 3 lines · Rafael J. Wysocki
I've just realized that if you combined this patch with the [6/9], you wouldn't need to make any changes to
gov_queue_work()at all, because that patch removes the case in point entirely.
Yeah, but then these are really two separate issues at hand and so I solved them separately. Lemme know how you want to see that and I can change :)
Signature · 1 line
viresh
Re: [PATCH V2 3/9] cpufreq: ondemand: only queue canceled works from update_sampling_rate()
Quoted 8 lines · Viresh Kumar
Quoted 3 lines · Rafael J. Wysocki
I've just realized that if you combined this patch with the [6/9], you wouldn't need to make any changes to
gov_queue_work()at all, because that patch removes the case in point entirely.Yeah, but then these are really two separate issues at hand and so I solved them separately. Lemme know how you want to see that and I can change :)
I don't want to make artificial changes.
If you can address two problems in one go with one relatively simple patch, why don't you do that?
Thanks,
Rafael
Viresh Kumar
maintainerPATCH v2 4/9 cpufreq: governor: Drop __gov_queue_work()
In reply to Viresh Kumar__gov_queue_work() isn't required anymore and can be merged with gov_queue_work(). Do it.
--- drivers/cpufreq/cpufreq_governor.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/cpufreq/cpufreq_governor.c +6 -11
@@ -158,25 +158,20 @@ void dbs_check_cpu(struct dbs_data *dbs_data, int cpu) }EXPORT_SYMBOL_GPL (dbs_check_cpu); -static inline void __gov_queue_work (int cpu,struct dbs_data *dbs_data, -unsigned int delay) -{ -struct cpu_dbs_info *cdbs = dbs_data->cdata->get_cpu_cdbs (cpu); - -mod_delayed_work_on (cpu, system_wq, &cdbs->dwork, delay); -} -void gov_queue_work (struct dbs_data *dbs_data,struct cpufreq_policy *policy,unsigned int delay,const struct cpumask *cpus) { -int i; +struct cpu_dbs_info *cdbs; +int cpu;mutex_lock (&cpufreq_governor_lock);if (!policy->governor_enabled) goto out_unlock; -for_each_cpu (i, cpus) -__gov_queue_work (i, dbs_data, delay); +for_each_cpu (cpu, cpus) { + cdbs = dbs_data->cdata->get_cpu_cdbs (cpu); +mod_delayed_work_on (cpu, system_wq, &cdbs->dwork, delay); + } out_unlock:mutex_unlock (&cpufreq_governor_lock);
Signature · 1 line
2.4.0
Re: [PATCH V2 4/9] cpufreq: governor: Drop __gov_queue_work()
Quoted 4 lines · Viresh Kumar
__gov_queue_work()isn't required anymore and can be merged withgov_queue_work(). Do it.Signed-off-by Viresh Kumar <[email protected]>
Quite frankly I don't see the point.
I'd even remove the inline from its definition and let the compiler decide what to do with it.
Quoted 31 lines
--- drivers/cpufreq/cpufreq_governor.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)drivers/cpufreq/cpufreq_governor.c +2
-9@@ -158,25 +158,20 @@ void dbs_check_cpu(struct dbs_data *dbs_data, int cpu) }EXPORT_SYMBOL_GPL (dbs_check_cpu); -static inline void __gov_queue_work (int cpu,struct dbs_data *dbs_data, -unsigned int delay) -{ -struct cpu_dbs_info *cdbs = dbs_data->cdata->get_cpu_cdbs (cpu); - -mod_delayed_work_on (cpu, system_wq, &cdbs->dwork, delay); -} -void gov_queue_work (struct dbs_data *dbs_data,struct cpufreq_policy *policy,unsigned int delay,const struct cpumask *cpus) { -int i; +struct cpu_dbs_info *cdbs; +int cpu;mutex_lock (&cpufreq_governor_lock);if (!policy->governor_enabled) goto out_unlock;
drivers/cpufreq/cpufreq_governor.c void dbs_check_cpu(struct dbs_data *dbs_data, int cpu) · line 177
Quoted 10 lines
drivers/cpufreq/cpufreq_governor.c +4
-2-for_each_cpu (i, cpus) -__gov_queue_work (i, dbs_data, delay); +for_each_cpu (cpu, cpus) { + cdbs = dbs_data->cdata->get_cpu_cdbs (cpu); +mod_delayed_work_on (cpu, system_wq, &cdbs->dwork, delay); + } out_unlock:mutex_unlock (&cpufreq_governor_lock);
Thanks,
Rafael
Viresh Kumar
maintainerRe: [PATCH V2 4/9] cpufreq: governor: Drop __gov_queue_work()
Quoted 7 lines · Rafael J. Wysocki
Quoted 4 lines · Viresh Kumar
__gov_queue_work()isn't required anymore and can be merged withgov_queue_work(). Do it.Signed-off-by Viresh Kumar <[email protected]>Quite frankly I don't see the point.
But isn't that just an unnecessary wrapper ?
Quoted 2 lines
I'd even remove the inline from its definition and let the compiler decide what to do with it.
What if the compiler decides to link it? Why add a function call for (almost) no use?
Signature · 1 line
viresh
Re: [PATCH V2 4/9] cpufreq: governor: Drop __gov_queue_work()
Quoted 10 lines · Viresh Kumar
Quoted 7 lines · Rafael J. Wysocki
Quoted 4 lines · Viresh Kumar
__gov_queue_work()isn't required anymore and can be merged withgov_queue_work(). Do it.Signed-off-by Viresh Kumar <[email protected]>Quite frankly I don't see the point.
But isn't that just an unnecessary wrapper ?
It isn't a wrapper, just a separation of code executed in each step of the loop. There's nothing wrong with having a separate function for that in principle.
I wouldn't make a fuss about that if that was new code even, so I don't see why we should change it.
Quoted 5 lines
Quoted 2 lines
I'd even remove the inline from its definition and let the compiler decide what to do with it.
What if the compiler decides to link it? Why add a function call for (almost) no use?
If the compiler does that, let it do it. :-)
If you think that you can outsmart the compiler people by doing such optimizations at this level manually, you're likely wrong. Serious man-hours go into making that stuff work as well as it can in compilers.
Thanks,
Rafael
Viresh Kumar
maintainerPATCH v2 5/9 cpufreq: ondemand: Drop unnecessary locks from update_sampling_rate()
In reply to Viresh Kumar'timer_mutex' is required to sync work-handlers of policy->cpus. update_sampling_rate() is just canceling the works and queuing them again. This isn't protecting anything at all in update_sampling_rate() and is not gonna be of any use.
Even if a work-handler is already running for a CPU, cancel_delayed_work_sync() will wait for it to finish.
Drop these unnecessary locks.
--- drivers/cpufreq/cpufreq_ondemand.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
drivers/cpufreq/cpufreq_ondemand.c +1 -9
@@ -267,28 +267,20 @@ static void update_sampling_rate(struct dbs_data *dbs_data, dbs_info = &per_cpu (od_cpu_dbs_info, cpu);cpufreq_cpu_put (policy); -mutex_lock (&dbs_info->cdbs.shared->timer_mutex); - -if (!delayed_work_pending (&dbs_info->cdbs.dwork)) { -mutex_unlock (&dbs_info->cdbs.shared->timer_mutex); +if (!delayed_work_pending (&dbs_info->cdbs.dwork))continue ; - } next_sampling = jiffies +usecs_to_jiffies (new_rate); appointed_at = dbs_info->cdbs.dwork.timer.expires;if (time_before (next_sampling, appointed_at)) { - -mutex_unlock (&dbs_info->cdbs.shared->timer_mutex);cancel_delayed_work_sync (&dbs_info->cdbs.dwork); -mutex_lock (&dbs_info->cdbs.shared->timer_mutex);gov_queue_work (dbs_data, policy,usecs_to_jiffies (new_rate),cpumask_of (cpu)); } -mutex_unlock (&dbs_info->cdbs.shared->timer_mutex); } }
Signature · 1 line
2.4.0
Viresh Kumar
maintainerPATCH v2 6/9 cpufreq: ondemand: queue work for policy->cpus together
In reply to Viresh KumarCurrently update_sampling_rate() runs over each online CPU and cancels/queues work on it. Its very inefficient for the case where a single policy manages multiple CPUs, as they can be processed together.
Also drop the unnecessary cancel_delayed_work_sync() as we are doing a mod_delayed_work_on() in gov_queue_work(), which will take care of pending works for us.
--- drivers/cpufreq/cpufreq_ondemand.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
drivers/cpufreq/cpufreq_ondemand.c +18 -13
@@ -247,40 +247,45 @@ static void update_sampling_rate(struct dbs_data *dbs_data, unsigned int new_rate) {struct od_dbs_tuners *od_tuners = dbs_data->tuners; +struct cpufreq_policy *policy; +struct od_cpu_dbs_info_s *dbs_info; +unsigned long next_sampling, appointed_at; +struct cpumask cpumask;int cpu; +cpumask_copy (&cpumask, cpu_online_mask); + od_tuners->sampling_rate = new_rate =max (new_rate, dbs_data->min_sampling_rate); -for_each_online_cpu (cpu) { -struct cpufreq_policy *policy; -struct od_cpu_dbs_info_s *dbs_info; -unsigned long next_sampling, appointed_at; - +for_each_cpu (cpu, &cpumask) { policy =cpufreq_cpu_get (cpu);if (!policy)continue ; + +/* clear all CPUs of this policy */ +cpumask_andnot (&cpumask, &cpumask, policy->cpus); +if (policy->governor != &cpufreq_gov_ondemand) {cpufreq_cpu_put (policy);continue ; } + dbs_info = &per_cpu (od_cpu_dbs_info, cpu);cpufreq_cpu_put (policy); -if (!delayed_work_pending (&dbs_info->cdbs.dwork)) +/* Make sure the work is not canceled on policy->cpus */ +if (!dbs_info->cdbs.shared->policy)continue ; next_sampling = jiffies +usecs_to_jiffies (new_rate); appointed_at = dbs_info->cdbs.dwork.timer.expires; -if (time_before (next_sampling, appointed_at)) { -cancel_delayed_work_sync (&dbs_info->cdbs.dwork); - -gov_queue_work (dbs_data, policy, -usecs_to_jiffies (new_rate), -cpumask_of (cpu)); +if (!time_before (next_sampling, appointed_at)) +continue ; - } +gov_queue_work (dbs_data, policy,usecs_to_jiffies (new_rate), + policy->cpus); } }
Signature · 1 line
2.4.0
Re: [PATCH V2 6/9] cpufreq: ondemand: queue work for policy->cpus together
Quoted 34 lines · Viresh Kumar
Currently
update_sampling_rate()runs over each online CPU and cancels/queues work on it. Its very inefficient for the case where a single policy manages multiple CPUs, as they can be processed together.Also drop the unnecessary
cancel_delayed_work_sync()as we are doing amod_delayed_work_on()ingov_queue_work(), which will take care of pending works for us.Signed-off-by Viresh Kumar <[email protected]>--- drivers/cpufreq/cpufreq_ondemand.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)drivers/cpufreq/cpufreq_ondemand.c +6
-2@@ -247,40 +247,45 @@ static void update_sampling_rate(struct dbs_data *dbs_data, unsigned int new_rate) {struct od_dbs_tuners *od_tuners = dbs_data->tuners; +struct cpufreq_policy *policy; +struct od_cpu_dbs_info_s *dbs_info; +unsigned long next_sampling, appointed_at; +struct cpumask cpumask;int cpu; +cpumask_copy (&cpumask, cpu_online_mask); + od_tuners->sampling_rate = new_rate =max (new_rate, dbs_data->min_sampling_rate); -for_each_online_cpu (cpu) { -struct cpufreq_policy *policy;
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 267
Quoted 10 lines
drivers/cpufreq/cpufreq_ondemand.c +4
-3-struct od_cpu_dbs_info_s *dbs_info; -unsigned long next_sampling, appointed_at; - +for_each_cpu (cpu, &cpumask) { policy =cpufreq_cpu_get (cpu);if (!policy)continue ; + +/* clear all CPUs of this policy */ +cpumask_andnot (&cpumask, &cpumask, policy->cpus);
Well, this is not exactly straightforward, but should work.
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 277
Quoted 11 lines
drivers/cpufreq/cpufreq_ondemand.c +3
-1+if (policy->governor != &cpufreq_gov_ondemand) {cpufreq_cpu_put (policy);continue ; } + dbs_info = &per_cpu (od_cpu_dbs_info, cpu);cpufreq_cpu_put (policy); -if (!delayed_work_pending (&dbs_info->cdbs.dwork)) +/* Make sure the work is not canceled on policy->cpus */
I'm not sure what scenario can lead to that. Care to explain?
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 282
Quoted 5 lines
drivers/cpufreq/cpufreq_ondemand.c +1
-0+if (!dbs_info->cdbs.shared->policy)continue ; next_sampling = jiffies +usecs_to_jiffies (new_rate); appointed_at = dbs_info->cdbs.dwork.timer.expires;
For that to work we always need to do stuff for policy->cpus in sync. Do we?
Quoted 6 lines
drivers/cpufreq/cpufreq_ondemand.c +0
-6-if (time_before (next_sampling, appointed_at)) { -cancel_delayed_work_sync (&dbs_info->cdbs.dwork); - -gov_queue_work (dbs_data, policy, -usecs_to_jiffies (new_rate), -cpumask_of (cpu));
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data,
Quoted 10 lines
drivers/cpufreq/cpufreq_ondemand.c +4
-1+if (!time_before (next_sampling, appointed_at)) +continue ; - } +gov_queue_work (dbs_data, policy,usecs_to_jiffies (new_rate), + policy->cpus); } }
Thanks,
Rafael
Viresh Kumar
maintainerRe: [PATCH V2 6/9] cpufreq: ondemand: queue work for policy->cpus together
Quoted 3 lines · Rafael J. Wysocki
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 277
Quoted 1 line
drivers/cpufreq/cpufreq_ondemand.c +1
-0+/* Make sure the work is not canceled on policy->cpus */ I'm not sure what scenario can lead to that. Care to explain?
CPUFREQ_GOV_STOP event called for the policy and so all its works are in canceled state.
Quoted 8 lines
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 282
Quoted 5 lines
drivers/cpufreq/cpufreq_ondemand.c +1
-0+if (!dbs_info->cdbs.shared->policy)continue ; next_sampling = jiffies +usecs_to_jiffies (new_rate); appointed_at = dbs_info->cdbs.dwork.timer.expires;For that to work we always need to do stuff for
policy->cpusin sync. Do we?
Hmm, we are not in 100% sync for sure. Will check that again.
Signature · 1 line
viresh
Viresh Kumar
maintainerRe: [PATCH V2 6/9] cpufreq: ondemand: queue work for policy->cpus together
Quoted 7 lines · Viresh Kumar
Quoted 5 lines
drivers/cpufreq/cpufreq_ondemand.c static void update_sampling_rate(struct dbs_data *dbs_data, · line 282
Quoted 2 lines
drivers/cpufreq/cpufreq_ondemand.c +0
-0next_sampling = jiffies +usecs_to_jiffies (new_rate); appointed_at = dbs_info->cdbs.dwork.timer.expires;For that to work we always need to do stuff for
policy->cpusin sync. Do we?Hmm, we are not in 100% sync for sure. Will check that again.
On the other hand, if we decide to apply 7/9 as well, then this is anyway going to get removed :)
Signature · 1 line
viresh
Viresh Kumar
maintainerPATCH v2 7/9 cpufreq: ondemand: update sampling rate immidiately
In reply to Viresh KumarWe are immediately updating sampling rate for already queued-works, only if the new expiry is lesser than the old one.
But what about the case, where the user doesn't want frequent events and want to increase sampling time? Shouldn't we cancel the works (and so their interrupts) on all policy->cpus (which might occur very shortly).
This patch removes this special case and simplifies code by immediately updating the expiry.
--- drivers/cpufreq/cpufreq_ondemand.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-)
drivers/cpufreq/cpufreq_ondemand.c +1 -17
@@ -231,17 +231,8 @@ static unsigned int od_dbs_timer(struct cpu_dbs_info *cdbs,static struct common_dbs_data od_dbs_cdata;/** -* update_sampling_rate - update sampling rate effective immediately if needed. +* update_sampling_rate - update sampling rate immediately. * @new_rate: new sampling rate -* -* If new rate is smaller than the old, simply updating -* dbs_tuners_int.sampling_rate might not be appropriate. For example, if the -* original sampling_rate was 1 second and the requested new sampling rate is 10 -* ms because the user needs immediate reaction from ondemand governor, but not -* sure if higher frequency will be required or not, then, the governor may -* change the sampling rate too late; up to 1 second later. Thus, if we are -* reducing the sampling rate, we need to make the new value effective -* immediately. */ static void update_sampling_rate (struct dbs_data *dbs_data,unsigned int new_rate)
@@ -249,7 +240,6 @@ static void update_sampling_rate(struct dbs_data *dbs_data,struct od_dbs_tuners *od_tuners = dbs_data->tuners;struct cpufreq_policy *policy;struct od_cpu_dbs_info_s *dbs_info; -unsigned long next_sampling, appointed_at;struct cpumask cpumask;int cpu;
@@ -278,12 +268,6 @@ static void update_sampling_rate(struct dbs_data *dbs_data,if (!dbs_info->cdbs.shared->policy)continue ; - next_sampling = jiffies +usecs_to_jiffies (new_rate); - appointed_at = dbs_info->cdbs.dwork.timer.expires; - -if (!time_before (next_sampling, appointed_at)) -continue ; -gov_queue_work (dbs_data, policy,usecs_to_jiffies (new_rate), policy->cpus); }
Signature · 1 line
2.4.0
Viresh Kumar
maintainerPATCH v2 8/9 cpufreq: governor: Quit work-handlers early if governor is stopped
In reply to Viresh Kumarcpufreq_governor_lock is abused by using it outside of cpufreq core, i.e. in cpufreq-governors. But we didn't had a solution at that point of time, and so doing that was the only acceptable solution:
6f1e4efd882e ("cpufreq: Fix timer/workqueue corruption by protecting reading governor_enabled")
The cpufreq governor core is fixed now against possible races and things are in much better shape.
cpufreq core is checking for invalid state-transitions of governors in __cpufreq_governor() with help of governor_enabled flag. The governor core is already taking care of that now and so we can get rid of those extra checks in __cpufreq_governor().
To do that, we first need to get rid of the dependency on governor_enabled flag in governor core, in gov_queue_work.
This patch is about getting rid of this dependency.
When a CPU is hot removed we'll cancel all the delayed work items via gov_cancel_work(). Normally this will just cancels a delayed timer on each CPU that the policy is managing and the work won't run. But if the work is already running, the workqueue code will wait for the work to finish before continuing to prevent the work items from re-queuing themselves like they normally do.
This scheme will work most of the time, except for the case where the work function determines that it should adjust the delay for all other CPUs that the policy is managing. If this scenario occurs, the canceling CPU will cancel its own work but queue up the other CPUs works to run.
And we will enter a situation where gov_cancel_work() has returned with work being queued on few CPUs.
To fix that in a different (non-hacky) way, set set shared->policy to false before trying to cancel the work. It should be updated within timer_mutex, which will prevent the work-handlers to start. Once the work-handlers finds that we are already trying to stop the governor, it will exit early. And that will prevent queuing of works again as well.
--- drivers/cpufreq/cpufreq_governor.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-)
drivers/cpufreq/cpufreq_governor.c +23 -10
@@ -164,17 +164,10 @@ void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,struct cpu_dbs_info *cdbs;int cpu; -mutex_lock (&cpufreq_governor_lock); -if (!policy->governor_enabled) - goto out_unlock; -for_each_cpu (cpu, cpus) { cdbs = dbs_data->cdata->get_cpu_cdbs (cpu);mod_delayed_work_on (cpu, system_wq, &cdbs->dwork, delay); } - -out_unlock: -mutex_unlock (&cpufreq_governor_lock); }EXPORT_SYMBOL_GPL (gov_queue_work);
@@ -213,14 +206,25 @@ static void dbs_timer(struct work_struct *work) struct cpu_dbs_info *cdbs = container_of(work, struct cpu_dbs_info, dwork.work);struct cpu_common_dbs_info *shared = cdbs->shared; -struct cpufreq_policy *policy = shared->policy; -struct dbs_data *dbs_data = policy->governor_data; +struct cpufreq_policy *policy; +struct dbs_data *dbs_data;unsigned int sampling_rate, delay;const struct cpumask *cpus;bool load_eval;mutex_lock (&shared->timer_mutex); + policy = shared->policy; + +/* +* Governor might already be disabled and there is no point continuing +* with the work-handler. +*/ +if (!policy) + goto unlock; + + dbs_data = policy->governor_data; +if (dbs_data->cdata->governor ==GOV_CONSERVATIVE ) {struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
@@ -237,6 +241,7 @@ static void dbs_timer(struct work_struct *work) delay = dbs_data->cdata->gov_dbs_timer (cdbs, dbs_data, load_eval);gov_queue_work (dbs_data, policy, delay, cpus); +unlock:mutex_unlock (&shared->timer_mutex); }
@@ -473,9 +478,17 @@ static int cpufreq_governor_stop(struct cpufreq_policy *policy,if (!shared || !shared->policy)return -EBUSY ; +/* +* Work-handler must see this updated, as it should not proceed any +* further after governor is disabled. And so timer_mutex is taken while +* updating this value. +*/ +mutex_lock (&shared->timer_mutex); + shared->policy =NULL ; +mutex_unlock (&shared->timer_mutex); +gov_cancel_work (dbs_data, policy); - shared->policy =NULL ;mutex_destroy (&shared->timer_mutex);return 0 ; }
Signature · 1 line
2.4.0
Viresh Kumar
maintainerPATCH v2 9/9 cpufreq: Get rid of ->governor_enabled and its lock
In reply to Viresh KumarInvalid state-transitions is verified by governor core now and there is no need to replicate that in cpufreq core.
Stop verifying the same in cpufreq core. That will get rid of policy->governor_enabled and cpufreq_governor_lock.
--- drivers/cpufreq/cpufreq.c | 24 ------------------------ include/linux/cpufreq.h | 1 - 2 files changed, 25 deletions(-)
drivers/cpufreq/cpufreq.c +0 -24
@@ -102,7 +102,6 @@ static LIST_HEAD(cpufreq_governor_list);static struct cpufreq_driver *cpufreq_driver; static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);static DEFINE_RWLOCK (cpufreq_driver_lock); -DEFINE_MUTEX (cpufreq_governor_lock);/* Flag to suspend/resume CPUFreq governors */ static bool cpufreq_suspended;
@@ -2076,21 +2075,6 @@ 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 ; - } - -if (event ==CPUFREQ_GOV_STOP ) - policy->governor_enabled = false; -else if (event ==CPUFREQ_GOV_START ) - policy->governor_enabled = true; - -mutex_unlock (&cpufreq_governor_lock); - ret = policy->governor->governor (policy, event);if (!ret) {
@@ -2098,14 +2082,6 @@ static int __cpufreq_governor(struct cpufreq_policy *policy, policy->governor->initialized++; else if (event == CPUFREQ_GOV_POLICY_EXIT) 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) ||
include/linux/cpufreq.h +0 -1
@@ -82,7 +82,6 @@ struct cpufreq_policy {unsigned int policy;/* see above */ struct cpufreq_governor *governor;/* see below */ void *governor_data; -bool governor_enabled;/* governor start/stop flag */ char last_governor[CPUFREQ_NAME_LEN ];/* last governor used */ struct work_struct update;/* if update_policy() needs to be
Signature · 1 line
2.4.0
Viresh Kumar
maintainerQuoted 16 lines · Viresh Kumar
Hi Rafael,
This series fixes few more possible race conditions. Over that there is some non-trivial cleanup, in order to simplify code.
Preeti did review some of them before she left and shared concerns on others, all that is sorted out now.
V1->V2:
- Dropped 2/10 from V1 as it wasn't required
- 3/10 saw some changes due to above patch being dropped
- 7/10 changed a bit as we check for pending work items by looking atshared->policy, rather than callingdelayed_work_pending. We wanted to check if governor is operational or not and the new check is enough for that.
Quoted 11 lines
Viresh Kumar (9):
cpufreq: Use__func__to print function's name
cpufreq: conservative: remove 'enable' field
cpufreq: ondemand: only queue canceled works from
update_sampling_rate()
cpufreq: governor: Drop__gov_queue_work()
cpufreq: ondemand: Drop unnecessary locks fromupdate_sampling_rate()
cpufreq: ondemand: queue work forpolicy->cpustogether
cpufreq: ondemand: update sampling rate immidiately
cpufreq: governor: Quit work-handlers early if governor is stopped
cpufreq: Get rid of ->governor_enabled and its lock
I thought you had some review comment for this series?
Signature · 1 line
viresh
Quoted 29 lines · Viresh Kumar
Quoted 16 lines · Viresh Kumar
Hi Rafael,
This series fixes few more possible race conditions. Over that there is some non-trivial cleanup, in order to simplify code.
Preeti did review some of them before she left and shared concerns on others, all that is sorted out now.
V1->V2:
- Dropped 2/10 from V1 as it wasn't required
- 3/10 saw some changes due to above patch being dropped
- 7/10 changed a bit as we check for pending work items by looking atshared->policy, rather than callingdelayed_work_pending. We wanted to check if governor is operational or not and the new check is enough for that.Quoted 11 lines
Viresh Kumar (9):
cpufreq: Use__func__to print function's name
cpufreq: conservative: remove 'enable' field
cpufreq: ondemand: only queue canceled works from
update_sampling_rate()
cpufreq: governor: Drop__gov_queue_work()
cpufreq: ondemand: Drop unnecessary locks fromupdate_sampling_rate()
cpufreq: ondemand: queue work forpolicy->cpustogether
cpufreq: ondemand: update sampling rate immidiately
cpufreq: governor: Quit work-handlers early if governor is stopped
cpufreq: Get rid of ->governor_enabled and its lock
Quoted 1 line
I thought you had some review comment for this series?
Yes, I did. I'll send them later today if all goes well.
Thanks,
Rafael