cpufreq: remove race while accessing cur_policy
[linux-3.10.git] / drivers / cpufreq / cpufreq_governor.c
1 /*
2  * drivers/cpufreq/cpufreq_governor.c
3  *
4  * CPUFREQ governors common code
5  *
6  * Copyright    (C) 2001 Russell King
7  *              (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
8  *              (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
9  *              (C) 2009 Alexander Clouter <alex@digriz.org.uk>
10  *              (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
11  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <asm/cputime.h>
21 #include <linux/cpufreq.h>
22 #include <linux/cpumask.h>
23 #include <linux/export.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/workqueue.h>
29
30 #include "cpufreq_governor.h"
31
32 static struct attribute_group *get_sysfs_attr(struct dbs_data *dbs_data)
33 {
34         if (have_governor_per_policy())
35                 return dbs_data->cdata->attr_group_gov_pol;
36         else
37                 return dbs_data->cdata->attr_group_gov_sys;
38 }
39
40 void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
41 {
42         struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
43         struct od_dbs_tuners *od_tuners = dbs_data->tuners;
44         struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
45         struct cpufreq_policy *policy;
46         unsigned int max_load = 0;
47         unsigned int ignore_nice;
48         unsigned int j;
49
50         if (dbs_data->cdata->governor == GOV_ONDEMAND)
51                 ignore_nice = od_tuners->ignore_nice_load;
52         else
53                 ignore_nice = cs_tuners->ignore_nice_load;
54
55         policy = cdbs->cur_policy;
56
57         /* Get Absolute Load (in terms of freq for ondemand gov) */
58         for_each_cpu(j, policy->cpus) {
59                 struct cpu_dbs_common_info *j_cdbs;
60                 u64 cur_wall_time, cur_idle_time;
61                 unsigned int idle_time, wall_time;
62                 unsigned int load;
63                 int io_busy = 0;
64
65                 j_cdbs = dbs_data->cdata->get_cpu_cdbs(j);
66
67                 /*
68                  * For the purpose of ondemand, waiting for disk IO is
69                  * an indication that you're performance critical, and
70                  * not that the system is actually idle. So do not add
71                  * the iowait time to the cpu idle time.
72                  */
73                 if (dbs_data->cdata->governor == GOV_ONDEMAND)
74                         io_busy = od_tuners->io_is_busy;
75                 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
76
77                 wall_time = (unsigned int)
78                         (cur_wall_time - j_cdbs->prev_cpu_wall);
79                 j_cdbs->prev_cpu_wall = cur_wall_time;
80
81                 idle_time = (unsigned int)
82                         (cur_idle_time - j_cdbs->prev_cpu_idle);
83                 j_cdbs->prev_cpu_idle = cur_idle_time;
84
85                 if (ignore_nice) {
86                         u64 cur_nice;
87                         unsigned long cur_nice_jiffies;
88
89                         cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
90                                          cdbs->prev_cpu_nice;
91                         /*
92                          * Assumption: nice time between sampling periods will
93                          * be less than 2^32 jiffies for 32 bit sys
94                          */
95                         cur_nice_jiffies = (unsigned long)
96                                         cputime64_to_jiffies64(cur_nice);
97
98                         cdbs->prev_cpu_nice =
99                                 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
100                         idle_time += jiffies_to_usecs(cur_nice_jiffies);
101                 }
102
103                 if (unlikely(!wall_time || wall_time < idle_time))
104                         continue;
105
106                 load = 100 * (wall_time - idle_time) / wall_time;
107
108                 if (dbs_data->cdata->governor == GOV_ONDEMAND) {
109                         int freq_avg = __cpufreq_driver_getavg(policy, j);
110                         if (freq_avg <= 0)
111                                 freq_avg = policy->cur;
112
113                         load *= freq_avg;
114                 }
115
116                 if (load > max_load)
117                         max_load = load;
118         }
119
120         dbs_data->cdata->gov_check_cpu(cpu, max_load);
121 }
122 EXPORT_SYMBOL_GPL(dbs_check_cpu);
123
124 static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data,
125                 unsigned int delay)
126 {
127         struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
128
129         mod_delayed_work_on(cpu, system_wq, &cdbs->work, delay);
130 }
131
132 void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
133                 unsigned int delay, bool all_cpus)
134 {
135         int i;
136
137         if (!all_cpus) {
138                 __gov_queue_work(smp_processor_id(), dbs_data, delay);
139         } else {
140                 for_each_cpu(i, policy->cpus)
141                         __gov_queue_work(i, dbs_data, delay);
142         }
143 }
144 EXPORT_SYMBOL_GPL(gov_queue_work);
145
146 static inline void gov_cancel_work(struct dbs_data *dbs_data,
147                 struct cpufreq_policy *policy)
148 {
149         struct cpu_dbs_common_info *cdbs;
150         int i;
151
152         for_each_cpu(i, policy->cpus) {
153                 cdbs = dbs_data->cdata->get_cpu_cdbs(i);
154                 cancel_delayed_work_sync(&cdbs->work);
155         }
156 }
157
158 /* Will return if we need to evaluate cpu load again or not */
159 bool need_load_eval(struct cpu_dbs_common_info *cdbs,
160                 unsigned int sampling_rate)
161 {
162         if (policy_is_shared(cdbs->cur_policy)) {
163                 ktime_t time_now = ktime_get();
164                 s64 delta_us = ktime_us_delta(time_now, cdbs->time_stamp);
165
166                 /* Do nothing if we recently have sampled */
167                 if (delta_us < (s64)(sampling_rate / 2))
168                         return false;
169                 else
170                         cdbs->time_stamp = time_now;
171         }
172
173         return true;
174 }
175 EXPORT_SYMBOL_GPL(need_load_eval);
176
177 static void set_sampling_rate(struct dbs_data *dbs_data,
178                 unsigned int sampling_rate)
179 {
180         if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
181                 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
182                 cs_tuners->sampling_rate = sampling_rate;
183         } else {
184                 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
185                 od_tuners->sampling_rate = sampling_rate;
186         }
187 }
188
189 int cpufreq_governor_dbs(struct cpufreq_policy *policy,
190                 struct common_dbs_data *cdata, unsigned int event)
191 {
192         struct dbs_data *dbs_data;
193         struct od_cpu_dbs_info_s *od_dbs_info = NULL;
194         struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
195         struct od_ops *od_ops = NULL;
196         struct od_dbs_tuners *od_tuners = NULL;
197         struct cs_dbs_tuners *cs_tuners = NULL;
198         struct cpu_dbs_common_info *cpu_cdbs;
199         unsigned int sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
200         int io_busy = 0;
201         int rc;
202
203         if (have_governor_per_policy())
204                 dbs_data = policy->governor_data;
205         else
206                 dbs_data = cdata->gdbs_data;
207
208         WARN_ON(!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT));
209
210         switch (event) {
211         case CPUFREQ_GOV_POLICY_INIT:
212                 if (have_governor_per_policy()) {
213                         WARN_ON(dbs_data);
214                 } else if (dbs_data) {
215                         dbs_data->usage_count++;
216                         policy->governor_data = dbs_data;
217                         return 0;
218                 }
219
220                 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
221                 if (!dbs_data) {
222                         pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
223                         return -ENOMEM;
224                 }
225
226                 dbs_data->cdata = cdata;
227                 dbs_data->usage_count = 1;
228                 rc = cdata->init(dbs_data);
229                 if (rc) {
230                         pr_err("%s: POLICY_INIT: init() failed\n", __func__);
231                         kfree(dbs_data);
232                         return rc;
233                 }
234
235                 rc = sysfs_create_group(get_governor_parent_kobj(policy),
236                                 get_sysfs_attr(dbs_data));
237                 if (rc) {
238                         cdata->exit(dbs_data);
239                         kfree(dbs_data);
240                         return rc;
241                 }
242
243                 policy->governor_data = dbs_data;
244
245                 /* policy latency is in nS. Convert it to uS first */
246                 latency = policy->cpuinfo.transition_latency / 1000;
247                 if (latency == 0)
248                         latency = 1;
249
250                 /* Bring kernel and HW constraints together */
251                 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
252                                 MIN_LATENCY_MULTIPLIER * latency);
253                 set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
254                                         latency * LATENCY_MULTIPLIER));
255
256                 if ((cdata->governor == GOV_CONSERVATIVE) &&
257                                 (!policy->governor->initialized)) {
258                         struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
259
260                         cpufreq_register_notifier(cs_ops->notifier_block,
261                                         CPUFREQ_TRANSITION_NOTIFIER);
262                 }
263
264                 if (!have_governor_per_policy())
265                         cdata->gdbs_data = dbs_data;
266
267                 return 0;
268         case CPUFREQ_GOV_POLICY_EXIT:
269                 if (!--dbs_data->usage_count) {
270                         sysfs_remove_group(get_governor_parent_kobj(policy),
271                                         get_sysfs_attr(dbs_data));
272
273                         if ((dbs_data->cdata->governor == GOV_CONSERVATIVE) &&
274                                 (policy->governor->initialized == 1)) {
275                                 struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
276
277                                 cpufreq_unregister_notifier(cs_ops->notifier_block,
278                                                 CPUFREQ_TRANSITION_NOTIFIER);
279                         }
280
281                         cdata->exit(dbs_data);
282                         kfree(dbs_data);
283                         cdata->gdbs_data = NULL;
284                 }
285
286                 policy->governor_data = NULL;
287                 return 0;
288         }
289
290         cpu_cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
291
292         if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
293                 cs_tuners = dbs_data->tuners;
294                 cs_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
295                 sampling_rate = cs_tuners->sampling_rate;
296                 ignore_nice = cs_tuners->ignore_nice_load;
297         } else {
298                 od_tuners = dbs_data->tuners;
299                 od_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
300                 sampling_rate = od_tuners->sampling_rate;
301                 ignore_nice = od_tuners->ignore_nice_load;
302                 od_ops = dbs_data->cdata->gov_ops;
303                 io_busy = od_tuners->io_is_busy;
304         }
305
306         switch (event) {
307         case CPUFREQ_GOV_START:
308                 if (!policy->cur)
309                         return -EINVAL;
310
311                 mutex_lock(&dbs_data->mutex);
312
313                 for_each_cpu(j, policy->cpus) {
314                         struct cpu_dbs_common_info *j_cdbs =
315                                 dbs_data->cdata->get_cpu_cdbs(j);
316
317                         j_cdbs->cpu = j;
318                         j_cdbs->cur_policy = policy;
319                         j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
320                                                &j_cdbs->prev_cpu_wall, io_busy);
321                         if (ignore_nice)
322                                 j_cdbs->prev_cpu_nice =
323                                         kcpustat_cpu(j).cpustat[CPUTIME_NICE];
324
325                         mutex_init(&j_cdbs->timer_mutex);
326                         INIT_DEFERRABLE_WORK(&j_cdbs->work,
327                                              dbs_data->cdata->gov_dbs_timer);
328                 }
329
330                 /*
331                  * conservative does not implement micro like ondemand
332                  * governor, thus we are bound to jiffes/HZ
333                  */
334                 if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
335                         cs_dbs_info->down_skip = 0;
336                         cs_dbs_info->enable = 1;
337                         cs_dbs_info->requested_freq = policy->cur;
338                 } else {
339                         od_dbs_info->rate_mult = 1;
340                         od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
341                         od_ops->powersave_bias_init_cpu(cpu);
342                 }
343
344                 mutex_unlock(&dbs_data->mutex);
345
346                 /* Initiate timer time stamp */
347                 cpu_cdbs->time_stamp = ktime_get();
348
349                 gov_queue_work(dbs_data, policy,
350                                 delay_for_sampling_rate(sampling_rate), true);
351                 break;
352
353         case CPUFREQ_GOV_STOP:
354                 if (dbs_data->cdata->governor == GOV_CONSERVATIVE)
355                         cs_dbs_info->enable = 0;
356
357                 gov_cancel_work(dbs_data, policy);
358
359                 mutex_lock(&dbs_data->mutex);
360                 mutex_destroy(&cpu_cdbs->timer_mutex);
361                 cpu_cdbs->cur_policy = NULL;
362
363                 mutex_unlock(&dbs_data->mutex);
364
365                 break;
366
367         case CPUFREQ_GOV_LIMITS:
368                 mutex_lock(&dbs_data->mutex);
369                 if (!cpu_cdbs->cur_policy) {
370                         mutex_unlock(&dbs_data->mutex);
371                         break;
372                 }
373                 mutex_lock(&cpu_cdbs->timer_mutex);
374                 if (policy->max < cpu_cdbs->cur_policy->cur)
375                         __cpufreq_driver_target(cpu_cdbs->cur_policy,
376                                         policy->max, CPUFREQ_RELATION_H);
377                 else if (policy->min > cpu_cdbs->cur_policy->cur)
378                         __cpufreq_driver_target(cpu_cdbs->cur_policy,
379                                         policy->min, CPUFREQ_RELATION_L);
380                 dbs_check_cpu(dbs_data, cpu);
381                 mutex_unlock(&cpu_cdbs->timer_mutex);
382                 mutex_unlock(&dbs_data->mutex);
383                 break;
384         }
385         return 0;
386 }
387 EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);