cpufreq / sched: Pass flags to cpufreq_update_util()
[cascardo/linux.git] / kernel / sched / cpufreq_schedutil.c
1 /*
2  * CPUFreq governor based on scheduler-provided CPU utilization data.
3  *
4  * Copyright (C) 2016, Intel Corporation
5  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/cpufreq.h>
15 #include <linux/slab.h>
16 #include <trace/events/power.h>
17
18 #include "sched.h"
19
20 struct sugov_tunables {
21         struct gov_attr_set attr_set;
22         unsigned int rate_limit_us;
23 };
24
25 struct sugov_policy {
26         struct cpufreq_policy *policy;
27
28         struct sugov_tunables *tunables;
29         struct list_head tunables_hook;
30
31         raw_spinlock_t update_lock;  /* For shared policies */
32         u64 last_freq_update_time;
33         s64 freq_update_delay_ns;
34         unsigned int next_freq;
35
36         /* The next fields are only needed if fast switch cannot be used. */
37         struct irq_work irq_work;
38         struct work_struct work;
39         struct mutex work_lock;
40         bool work_in_progress;
41
42         bool need_freq_update;
43 };
44
45 struct sugov_cpu {
46         struct update_util_data update_util;
47         struct sugov_policy *sg_policy;
48
49         unsigned int cached_raw_freq;
50
51         /* The fields below are only needed when sharing a policy. */
52         unsigned long util;
53         unsigned long max;
54         u64 last_update;
55         unsigned int flags;
56 };
57
58 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
59
60 /************************ Governor internals ***********************/
61
62 static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
63 {
64         s64 delta_ns;
65
66         if (sg_policy->work_in_progress)
67                 return false;
68
69         if (unlikely(sg_policy->need_freq_update)) {
70                 sg_policy->need_freq_update = false;
71                 /*
72                  * This happens when limits change, so forget the previous
73                  * next_freq value and force an update.
74                  */
75                 sg_policy->next_freq = UINT_MAX;
76                 return true;
77         }
78
79         delta_ns = time - sg_policy->last_freq_update_time;
80         return delta_ns >= sg_policy->freq_update_delay_ns;
81 }
82
83 static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
84                                 unsigned int next_freq)
85 {
86         struct cpufreq_policy *policy = sg_policy->policy;
87
88         sg_policy->last_freq_update_time = time;
89
90         if (policy->fast_switch_enabled) {
91                 if (sg_policy->next_freq == next_freq) {
92                         trace_cpu_frequency(policy->cur, smp_processor_id());
93                         return;
94                 }
95                 sg_policy->next_freq = next_freq;
96                 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
97                 if (next_freq == CPUFREQ_ENTRY_INVALID)
98                         return;
99
100                 policy->cur = next_freq;
101                 trace_cpu_frequency(next_freq, smp_processor_id());
102         } else if (sg_policy->next_freq != next_freq) {
103                 sg_policy->next_freq = next_freq;
104                 sg_policy->work_in_progress = true;
105                 irq_work_queue(&sg_policy->irq_work);
106         }
107 }
108
109 /**
110  * get_next_freq - Compute a new frequency for a given cpufreq policy.
111  * @sg_cpu: schedutil cpu object to compute the new frequency for.
112  * @util: Current CPU utilization.
113  * @max: CPU capacity.
114  *
115  * If the utilization is frequency-invariant, choose the new frequency to be
116  * proportional to it, that is
117  *
118  * next_freq = C * max_freq * util / max
119  *
120  * Otherwise, approximate the would-be frequency-invariant utilization by
121  * util_raw * (curr_freq / max_freq) which leads to
122  *
123  * next_freq = C * curr_freq * util_raw / max
124  *
125  * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
126  *
127  * The lowest driver-supported frequency which is equal or greater than the raw
128  * next_freq (as calculated above) is returned, subject to policy min/max and
129  * cpufreq driver limitations.
130  */
131 static unsigned int get_next_freq(struct sugov_cpu *sg_cpu, unsigned long util,
132                                   unsigned long max)
133 {
134         struct sugov_policy *sg_policy = sg_cpu->sg_policy;
135         struct cpufreq_policy *policy = sg_policy->policy;
136         unsigned int freq = arch_scale_freq_invariant() ?
137                                 policy->cpuinfo.max_freq : policy->cur;
138
139         freq = (freq + (freq >> 2)) * util / max;
140
141         if (freq == sg_cpu->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
142                 return sg_policy->next_freq;
143         sg_cpu->cached_raw_freq = freq;
144         return cpufreq_driver_resolve_freq(policy, freq);
145 }
146
147 static void sugov_get_util(unsigned long *util, unsigned long *max)
148 {
149         struct rq *rq = this_rq();
150         unsigned long cfs_max = rq->cpu_capacity_orig;
151
152         *util = min(rq->cfs.avg.util_avg, cfs_max);
153         *max = cfs_max;
154 }
155
156 static void sugov_update_single(struct update_util_data *hook, u64 time,
157                                 unsigned int flags)
158 {
159         struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
160         struct sugov_policy *sg_policy = sg_cpu->sg_policy;
161         struct cpufreq_policy *policy = sg_policy->policy;
162         unsigned long util, max;
163         unsigned int next_f;
164
165         if (!sugov_should_update_freq(sg_policy, time))
166                 return;
167
168         if (flags & SCHED_CPUFREQ_RT_DL) {
169                 next_f = policy->cpuinfo.max_freq;
170         } else {
171                 sugov_get_util(&util, &max);
172                 next_f = get_next_freq(sg_cpu, util, max);
173         }
174         sugov_update_commit(sg_policy, time, next_f);
175 }
176
177 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu,
178                                            unsigned long util, unsigned long max,
179                                            unsigned int flags)
180 {
181         struct sugov_policy *sg_policy = sg_cpu->sg_policy;
182         struct cpufreq_policy *policy = sg_policy->policy;
183         unsigned int max_f = policy->cpuinfo.max_freq;
184         u64 last_freq_update_time = sg_policy->last_freq_update_time;
185         unsigned int j;
186
187         if (flags & SCHED_CPUFREQ_RT_DL)
188                 return max_f;
189
190         for_each_cpu(j, policy->cpus) {
191                 struct sugov_cpu *j_sg_cpu;
192                 unsigned long j_util, j_max;
193                 s64 delta_ns;
194
195                 if (j == smp_processor_id())
196                         continue;
197
198                 j_sg_cpu = &per_cpu(sugov_cpu, j);
199                 /*
200                  * If the CPU utilization was last updated before the previous
201                  * frequency update and the time elapsed between the last update
202                  * of the CPU utilization and the last frequency update is long
203                  * enough, don't take the CPU into account as it probably is
204                  * idle now.
205                  */
206                 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
207                 if (delta_ns > TICK_NSEC)
208                         continue;
209
210                 if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
211                         return max_f;
212
213                 j_util = j_sg_cpu->util;
214                 j_max = j_sg_cpu->max;
215                 if (j_util * max > j_max * util) {
216                         util = j_util;
217                         max = j_max;
218                 }
219         }
220
221         return get_next_freq(sg_cpu, util, max);
222 }
223
224 static void sugov_update_shared(struct update_util_data *hook, u64 time,
225                                 unsigned int flags)
226 {
227         struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
228         struct sugov_policy *sg_policy = sg_cpu->sg_policy;
229         unsigned long util, max;
230         unsigned int next_f;
231
232         sugov_get_util(&util, &max);
233
234         raw_spin_lock(&sg_policy->update_lock);
235
236         sg_cpu->util = util;
237         sg_cpu->max = max;
238         sg_cpu->flags = flags;
239         sg_cpu->last_update = time;
240
241         if (sugov_should_update_freq(sg_policy, time)) {
242                 next_f = sugov_next_freq_shared(sg_cpu, util, max, flags);
243                 sugov_update_commit(sg_policy, time, next_f);
244         }
245
246         raw_spin_unlock(&sg_policy->update_lock);
247 }
248
249 static void sugov_work(struct work_struct *work)
250 {
251         struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
252
253         mutex_lock(&sg_policy->work_lock);
254         __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
255                                 CPUFREQ_RELATION_L);
256         mutex_unlock(&sg_policy->work_lock);
257
258         sg_policy->work_in_progress = false;
259 }
260
261 static void sugov_irq_work(struct irq_work *irq_work)
262 {
263         struct sugov_policy *sg_policy;
264
265         sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
266         schedule_work_on(smp_processor_id(), &sg_policy->work);
267 }
268
269 /************************** sysfs interface ************************/
270
271 static struct sugov_tunables *global_tunables;
272 static DEFINE_MUTEX(global_tunables_lock);
273
274 static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
275 {
276         return container_of(attr_set, struct sugov_tunables, attr_set);
277 }
278
279 static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
280 {
281         struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
282
283         return sprintf(buf, "%u\n", tunables->rate_limit_us);
284 }
285
286 static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
287                                    size_t count)
288 {
289         struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
290         struct sugov_policy *sg_policy;
291         unsigned int rate_limit_us;
292
293         if (kstrtouint(buf, 10, &rate_limit_us))
294                 return -EINVAL;
295
296         tunables->rate_limit_us = rate_limit_us;
297
298         list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
299                 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
300
301         return count;
302 }
303
304 static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
305
306 static struct attribute *sugov_attributes[] = {
307         &rate_limit_us.attr,
308         NULL
309 };
310
311 static struct kobj_type sugov_tunables_ktype = {
312         .default_attrs = sugov_attributes,
313         .sysfs_ops = &governor_sysfs_ops,
314 };
315
316 /********************** cpufreq governor interface *********************/
317
318 static struct cpufreq_governor schedutil_gov;
319
320 static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
321 {
322         struct sugov_policy *sg_policy;
323
324         sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
325         if (!sg_policy)
326                 return NULL;
327
328         sg_policy->policy = policy;
329         init_irq_work(&sg_policy->irq_work, sugov_irq_work);
330         INIT_WORK(&sg_policy->work, sugov_work);
331         mutex_init(&sg_policy->work_lock);
332         raw_spin_lock_init(&sg_policy->update_lock);
333         return sg_policy;
334 }
335
336 static void sugov_policy_free(struct sugov_policy *sg_policy)
337 {
338         mutex_destroy(&sg_policy->work_lock);
339         kfree(sg_policy);
340 }
341
342 static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
343 {
344         struct sugov_tunables *tunables;
345
346         tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
347         if (tunables) {
348                 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
349                 if (!have_governor_per_policy())
350                         global_tunables = tunables;
351         }
352         return tunables;
353 }
354
355 static void sugov_tunables_free(struct sugov_tunables *tunables)
356 {
357         if (!have_governor_per_policy())
358                 global_tunables = NULL;
359
360         kfree(tunables);
361 }
362
363 static int sugov_init(struct cpufreq_policy *policy)
364 {
365         struct sugov_policy *sg_policy;
366         struct sugov_tunables *tunables;
367         unsigned int lat;
368         int ret = 0;
369
370         /* State should be equivalent to EXIT */
371         if (policy->governor_data)
372                 return -EBUSY;
373
374         sg_policy = sugov_policy_alloc(policy);
375         if (!sg_policy)
376                 return -ENOMEM;
377
378         mutex_lock(&global_tunables_lock);
379
380         if (global_tunables) {
381                 if (WARN_ON(have_governor_per_policy())) {
382                         ret = -EINVAL;
383                         goto free_sg_policy;
384                 }
385                 policy->governor_data = sg_policy;
386                 sg_policy->tunables = global_tunables;
387
388                 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
389                 goto out;
390         }
391
392         tunables = sugov_tunables_alloc(sg_policy);
393         if (!tunables) {
394                 ret = -ENOMEM;
395                 goto free_sg_policy;
396         }
397
398         tunables->rate_limit_us = LATENCY_MULTIPLIER;
399         lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
400         if (lat)
401                 tunables->rate_limit_us *= lat;
402
403         policy->governor_data = sg_policy;
404         sg_policy->tunables = tunables;
405
406         ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
407                                    get_governor_parent_kobj(policy), "%s",
408                                    schedutil_gov.name);
409         if (ret)
410                 goto fail;
411
412  out:
413         mutex_unlock(&global_tunables_lock);
414
415         cpufreq_enable_fast_switch(policy);
416         return 0;
417
418  fail:
419         policy->governor_data = NULL;
420         sugov_tunables_free(tunables);
421
422  free_sg_policy:
423         mutex_unlock(&global_tunables_lock);
424
425         sugov_policy_free(sg_policy);
426         pr_err("initialization failed (error %d)\n", ret);
427         return ret;
428 }
429
430 static void sugov_exit(struct cpufreq_policy *policy)
431 {
432         struct sugov_policy *sg_policy = policy->governor_data;
433         struct sugov_tunables *tunables = sg_policy->tunables;
434         unsigned int count;
435
436         cpufreq_disable_fast_switch(policy);
437
438         mutex_lock(&global_tunables_lock);
439
440         count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
441         policy->governor_data = NULL;
442         if (!count)
443                 sugov_tunables_free(tunables);
444
445         mutex_unlock(&global_tunables_lock);
446
447         sugov_policy_free(sg_policy);
448 }
449
450 static int sugov_start(struct cpufreq_policy *policy)
451 {
452         struct sugov_policy *sg_policy = policy->governor_data;
453         unsigned int cpu;
454
455         sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
456         sg_policy->last_freq_update_time = 0;
457         sg_policy->next_freq = UINT_MAX;
458         sg_policy->work_in_progress = false;
459         sg_policy->need_freq_update = false;
460
461         for_each_cpu(cpu, policy->cpus) {
462                 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
463
464                 sg_cpu->sg_policy = sg_policy;
465                 if (policy_is_shared(policy)) {
466                         sg_cpu->util = 0;
467                         sg_cpu->max = 0;
468                         sg_cpu->flags = SCHED_CPUFREQ_RT;
469                         sg_cpu->last_update = 0;
470                         sg_cpu->cached_raw_freq = 0;
471                         cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
472                                                      sugov_update_shared);
473                 } else {
474                         cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
475                                                      sugov_update_single);
476                 }
477         }
478         return 0;
479 }
480
481 static void sugov_stop(struct cpufreq_policy *policy)
482 {
483         struct sugov_policy *sg_policy = policy->governor_data;
484         unsigned int cpu;
485
486         for_each_cpu(cpu, policy->cpus)
487                 cpufreq_remove_update_util_hook(cpu);
488
489         synchronize_sched();
490
491         irq_work_sync(&sg_policy->irq_work);
492         cancel_work_sync(&sg_policy->work);
493 }
494
495 static void sugov_limits(struct cpufreq_policy *policy)
496 {
497         struct sugov_policy *sg_policy = policy->governor_data;
498
499         if (!policy->fast_switch_enabled) {
500                 mutex_lock(&sg_policy->work_lock);
501                 cpufreq_policy_apply_limits(policy);
502                 mutex_unlock(&sg_policy->work_lock);
503         }
504
505         sg_policy->need_freq_update = true;
506 }
507
508 static struct cpufreq_governor schedutil_gov = {
509         .name = "schedutil",
510         .owner = THIS_MODULE,
511         .init = sugov_init,
512         .exit = sugov_exit,
513         .start = sugov_start,
514         .stop = sugov_stop,
515         .limits = sugov_limits,
516 };
517
518 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
519 struct cpufreq_governor *cpufreq_default_governor(void)
520 {
521         return &schedutil_gov;
522 }
523 #endif
524
525 static int __init sugov_register(void)
526 {
527         return cpufreq_register_governor(&schedutil_gov);
528 }
529 fs_initcall(sugov_register);