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 "sched.h"
15
16 #include <trace/events/power.h>
17
18 struct sugov_tunables {
19 struct gov_attr_set attr_set;
20 unsigned int rate_limit_us;
21 };
22
23 struct sugov_policy {
24 struct cpufreq_policy *policy;
25
26 struct sugov_tunables *tunables;
27 struct list_head tunables_hook;
28
29 raw_spinlock_t update_lock; /* For shared policies */
30 u64 last_freq_update_time;
31 s64 freq_update_delay_ns;
32 unsigned int next_freq;
33 unsigned int cached_raw_freq;
34
35 /* The next fields are only needed if fast switch cannot be used: */
36 struct irq_work irq_work;
37 struct kthread_work work;
38 struct mutex work_lock;
39 struct kthread_worker worker;
40 struct task_struct *thread;
41 bool work_in_progress;
42
43 bool need_freq_update;
44 };
45
46 struct sugov_cpu {
47 struct update_util_data update_util;
48 struct sugov_policy *sg_policy;
49 unsigned int cpu;
50
51 bool iowait_boost_pending;
52 unsigned int iowait_boost;
53 unsigned int iowait_boost_max;
54 u64 last_update;
55
56 unsigned long bw_dl;
57 unsigned long max;
58
59 /* The field below is for single-CPU policies only: */
60 #ifdef CONFIG_NO_HZ_COMMON
61 unsigned long saved_idle_calls;
62 #endif
63 };
64
65 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
66
67 /************************ Governor internals ***********************/
68
sugov_should_update_freq(struct sugov_policy * sg_policy,u64 time)69 static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
70 {
71 s64 delta_ns;
72
73 /*
74 * Since cpufreq_update_util() is called with rq->lock held for
75 * the @target_cpu, our per-CPU data is fully serialized.
76 *
77 * However, drivers cannot in general deal with cross-CPU
78 * requests, so while get_next_freq() will work, our
79 * sugov_update_commit() call may not for the fast switching platforms.
80 *
81 * Hence stop here for remote requests if they aren't supported
82 * by the hardware, as calculating the frequency is pointless if
83 * we cannot in fact act on it.
84 *
85 * For the slow switching platforms, the kthread is always scheduled on
86 * the right set of CPUs and any CPU can find the next frequency and
87 * schedule the kthread.
88 */
89 if (sg_policy->policy->fast_switch_enabled &&
90 !cpufreq_this_cpu_can_update(sg_policy->policy))
91 return false;
92
93 if (unlikely(sg_policy->need_freq_update))
94 return true;
95
96 delta_ns = time - sg_policy->last_freq_update_time;
97
98 return delta_ns >= sg_policy->freq_update_delay_ns;
99 }
100
sugov_update_next_freq(struct sugov_policy * sg_policy,u64 time,unsigned int next_freq)101 static bool sugov_update_next_freq(struct sugov_policy *sg_policy, u64 time,
102 unsigned int next_freq)
103 {
104 if (sg_policy->next_freq == next_freq)
105 return false;
106
107 sg_policy->next_freq = next_freq;
108 sg_policy->last_freq_update_time = time;
109
110 return true;
111 }
112
sugov_fast_switch(struct sugov_policy * sg_policy,u64 time,unsigned int next_freq)113 static void sugov_fast_switch(struct sugov_policy *sg_policy, u64 time,
114 unsigned int next_freq)
115 {
116 struct cpufreq_policy *policy = sg_policy->policy;
117
118 if (!sugov_update_next_freq(sg_policy, time, next_freq))
119 return;
120
121 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
122 if (!next_freq)
123 return;
124
125 policy->cur = next_freq;
126 trace_cpu_frequency(next_freq, smp_processor_id());
127 }
128
sugov_deferred_update(struct sugov_policy * sg_policy,u64 time,unsigned int next_freq)129 static void sugov_deferred_update(struct sugov_policy *sg_policy, u64 time,
130 unsigned int next_freq)
131 {
132 if (!sugov_update_next_freq(sg_policy, time, next_freq))
133 return;
134
135 if (!sg_policy->work_in_progress) {
136 sg_policy->work_in_progress = true;
137 irq_work_queue(&sg_policy->irq_work);
138 }
139 }
140
141 /**
142 * get_next_freq - Compute a new frequency for a given cpufreq policy.
143 * @sg_policy: schedutil policy object to compute the new frequency for.
144 * @util: Current CPU utilization.
145 * @max: CPU capacity.
146 *
147 * If the utilization is frequency-invariant, choose the new frequency to be
148 * proportional to it, that is
149 *
150 * next_freq = C * max_freq * util / max
151 *
152 * Otherwise, approximate the would-be frequency-invariant utilization by
153 * util_raw * (curr_freq / max_freq) which leads to
154 *
155 * next_freq = C * curr_freq * util_raw / max
156 *
157 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
158 *
159 * The lowest driver-supported frequency which is equal or greater than the raw
160 * next_freq (as calculated above) is returned, subject to policy min/max and
161 * cpufreq driver limitations.
162 */
get_next_freq(struct sugov_policy * sg_policy,unsigned long util,unsigned long max)163 static unsigned int get_next_freq(struct sugov_policy *sg_policy,
164 unsigned long util, unsigned long max)
165 {
166 struct cpufreq_policy *policy = sg_policy->policy;
167 unsigned int freq = arch_scale_freq_invariant() ?
168 policy->cpuinfo.max_freq : policy->cur;
169
170 freq = (freq + (freq >> 2)) * util / max;
171
172 if (freq == sg_policy->cached_raw_freq && !sg_policy->need_freq_update)
173 return sg_policy->next_freq;
174
175 sg_policy->need_freq_update = false;
176 sg_policy->cached_raw_freq = freq;
177 return cpufreq_driver_resolve_freq(policy, freq);
178 }
179
180 /*
181 * This function computes an effective utilization for the given CPU, to be
182 * used for frequency selection given the linear relation: f = u * f_max.
183 *
184 * The scheduler tracks the following metrics:
185 *
186 * cpu_util_{cfs,rt,dl,irq}()
187 * cpu_bw_dl()
188 *
189 * Where the cfs,rt and dl util numbers are tracked with the same metric and
190 * synchronized windows and are thus directly comparable.
191 *
192 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
193 * which excludes things like IRQ and steal-time. These latter are then accrued
194 * in the irq utilization.
195 *
196 * The DL bandwidth number otoh is not a measured metric but a value computed
197 * based on the task model parameters and gives the minimal utilization
198 * required to meet deadlines.
199 */
sugov_get_util(struct sugov_cpu * sg_cpu)200 static unsigned long sugov_get_util(struct sugov_cpu *sg_cpu)
201 {
202 struct rq *rq = cpu_rq(sg_cpu->cpu);
203 unsigned long util, irq, max;
204
205 sg_cpu->max = max = arch_scale_cpu_capacity(NULL, sg_cpu->cpu);
206 sg_cpu->bw_dl = cpu_bw_dl(rq);
207
208 if (rt_rq_is_runnable(&rq->rt))
209 return max;
210
211 /*
212 * Early check to see if IRQ/steal time saturates the CPU, can be
213 * because of inaccuracies in how we track these -- see
214 * update_irq_load_avg().
215 */
216 irq = cpu_util_irq(rq);
217 if (unlikely(irq >= max))
218 return max;
219
220 /*
221 * Because the time spend on RT/DL tasks is visible as 'lost' time to
222 * CFS tasks and we use the same metric to track the effective
223 * utilization (PELT windows are synchronized) we can directly add them
224 * to obtain the CPU's actual utilization.
225 */
226 util = cpu_util_cfs(rq);
227 util += cpu_util_rt(rq);
228
229 /*
230 * We do not make cpu_util_dl() a permanent part of this sum because we
231 * want to use cpu_bw_dl() later on, but we need to check if the
232 * CFS+RT+DL sum is saturated (ie. no idle time) such that we select
233 * f_max when there is no idle time.
234 *
235 * NOTE: numerical errors or stop class might cause us to not quite hit
236 * saturation when we should -- something for later.
237 */
238 if ((util + cpu_util_dl(rq)) >= max)
239 return max;
240
241 /*
242 * There is still idle time; further improve the number by using the
243 * irq metric. Because IRQ/steal time is hidden from the task clock we
244 * need to scale the task numbers:
245 *
246 * 1 - irq
247 * U' = irq + ------- * U
248 * max
249 */
250 util = scale_irq_capacity(util, irq, max);
251 util += irq;
252
253 /*
254 * Bandwidth required by DEADLINE must always be granted while, for
255 * FAIR and RT, we use blocked utilization of IDLE CPUs as a mechanism
256 * to gracefully reduce the frequency when no tasks show up for longer
257 * periods of time.
258 *
259 * Ideally we would like to set bw_dl as min/guaranteed freq and util +
260 * bw_dl as requested freq. However, cpufreq is not yet ready for such
261 * an interface. So, we only do the latter for now.
262 */
263 return min(max, util + sg_cpu->bw_dl);
264 }
265
266 /**
267 * sugov_iowait_reset() - Reset the IO boost status of a CPU.
268 * @sg_cpu: the sugov data for the CPU to boost
269 * @time: the update time from the caller
270 * @set_iowait_boost: true if an IO boost has been requested
271 *
272 * The IO wait boost of a task is disabled after a tick since the last update
273 * of a CPU. If a new IO wait boost is requested after more then a tick, then
274 * we enable the boost starting from the minimum frequency, which improves
275 * energy efficiency by ignoring sporadic wakeups from IO.
276 */
sugov_iowait_reset(struct sugov_cpu * sg_cpu,u64 time,bool set_iowait_boost)277 static bool sugov_iowait_reset(struct sugov_cpu *sg_cpu, u64 time,
278 bool set_iowait_boost)
279 {
280 s64 delta_ns = time - sg_cpu->last_update;
281
282 /* Reset boost only if a tick has elapsed since last request */
283 if (delta_ns <= TICK_NSEC)
284 return false;
285
286 sg_cpu->iowait_boost = set_iowait_boost
287 ? sg_cpu->sg_policy->policy->min : 0;
288 sg_cpu->iowait_boost_pending = set_iowait_boost;
289
290 return true;
291 }
292
293 /**
294 * sugov_iowait_boost() - Updates the IO boost status of a CPU.
295 * @sg_cpu: the sugov data for the CPU to boost
296 * @time: the update time from the caller
297 * @flags: SCHED_CPUFREQ_IOWAIT if the task is waking up after an IO wait
298 *
299 * Each time a task wakes up after an IO operation, the CPU utilization can be
300 * boosted to a certain utilization which doubles at each "frequent and
301 * successive" wakeup from IO, ranging from the utilization of the minimum
302 * OPP to the utilization of the maximum OPP.
303 * To keep doubling, an IO boost has to be requested at least once per tick,
304 * otherwise we restart from the utilization of the minimum OPP.
305 */
sugov_iowait_boost(struct sugov_cpu * sg_cpu,u64 time,unsigned int flags)306 static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
307 unsigned int flags)
308 {
309 bool set_iowait_boost = flags & SCHED_CPUFREQ_IOWAIT;
310
311 /* Reset boost if the CPU appears to have been idle enough */
312 if (sg_cpu->iowait_boost &&
313 sugov_iowait_reset(sg_cpu, time, set_iowait_boost))
314 return;
315
316 /* Boost only tasks waking up after IO */
317 if (!set_iowait_boost)
318 return;
319
320 /* Ensure boost doubles only one time at each request */
321 if (sg_cpu->iowait_boost_pending)
322 return;
323 sg_cpu->iowait_boost_pending = true;
324
325 /* Double the boost at each request */
326 if (sg_cpu->iowait_boost) {
327 sg_cpu->iowait_boost <<= 1;
328 if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
329 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
330 return;
331 }
332
333 /* First wakeup after IO: start with minimum boost */
334 sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
335 }
336
337 /**
338 * sugov_iowait_apply() - Apply the IO boost to a CPU.
339 * @sg_cpu: the sugov data for the cpu to boost
340 * @time: the update time from the caller
341 * @util: the utilization to (eventually) boost
342 * @max: the maximum value the utilization can be boosted to
343 *
344 * A CPU running a task which woken up after an IO operation can have its
345 * utilization boosted to speed up the completion of those IO operations.
346 * The IO boost value is increased each time a task wakes up from IO, in
347 * sugov_iowait_apply(), and it's instead decreased by this function,
348 * each time an increase has not been requested (!iowait_boost_pending).
349 *
350 * A CPU which also appears to have been idle for at least one tick has also
351 * its IO boost utilization reset.
352 *
353 * This mechanism is designed to boost high frequently IO waiting tasks, while
354 * being more conservative on tasks which does sporadic IO operations.
355 */
sugov_iowait_apply(struct sugov_cpu * sg_cpu,u64 time,unsigned long * util,unsigned long * max)356 static void sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time,
357 unsigned long *util, unsigned long *max)
358 {
359 unsigned int boost_util, boost_max;
360
361 /* No boost currently required */
362 if (!sg_cpu->iowait_boost)
363 return;
364
365 /* Reset boost if the CPU appears to have been idle enough */
366 if (sugov_iowait_reset(sg_cpu, time, false))
367 return;
368
369 /*
370 * An IO waiting task has just woken up:
371 * allow to further double the boost value
372 */
373 if (sg_cpu->iowait_boost_pending) {
374 sg_cpu->iowait_boost_pending = false;
375 } else {
376 /*
377 * Otherwise: reduce the boost value and disable it when we
378 * reach the minimum.
379 */
380 sg_cpu->iowait_boost >>= 1;
381 if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) {
382 sg_cpu->iowait_boost = 0;
383 return;
384 }
385 }
386
387 /*
388 * Apply the current boost value: a CPU is boosted only if its current
389 * utilization is smaller then the current IO boost level.
390 */
391 boost_util = sg_cpu->iowait_boost;
392 boost_max = sg_cpu->iowait_boost_max;
393 if (*util * boost_max < *max * boost_util) {
394 *util = boost_util;
395 *max = boost_max;
396 }
397 }
398
399 #ifdef CONFIG_NO_HZ_COMMON
sugov_cpu_is_busy(struct sugov_cpu * sg_cpu)400 static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
401 {
402 unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu);
403 bool ret = idle_calls == sg_cpu->saved_idle_calls;
404
405 sg_cpu->saved_idle_calls = idle_calls;
406 return ret;
407 }
408 #else
sugov_cpu_is_busy(struct sugov_cpu * sg_cpu)409 static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
410 #endif /* CONFIG_NO_HZ_COMMON */
411
412 /*
413 * Make sugov_should_update_freq() ignore the rate limit when DL
414 * has increased the utilization.
415 */
ignore_dl_rate_limit(struct sugov_cpu * sg_cpu,struct sugov_policy * sg_policy)416 static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu, struct sugov_policy *sg_policy)
417 {
418 if (cpu_bw_dl(cpu_rq(sg_cpu->cpu)) > sg_cpu->bw_dl)
419 sg_policy->need_freq_update = true;
420 }
421
sugov_update_single(struct update_util_data * hook,u64 time,unsigned int flags)422 static void sugov_update_single(struct update_util_data *hook, u64 time,
423 unsigned int flags)
424 {
425 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
426 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
427 unsigned long util, max;
428 unsigned int next_f;
429 bool busy;
430
431 sugov_iowait_boost(sg_cpu, time, flags);
432 sg_cpu->last_update = time;
433
434 ignore_dl_rate_limit(sg_cpu, sg_policy);
435
436 if (!sugov_should_update_freq(sg_policy, time))
437 return;
438
439 busy = sugov_cpu_is_busy(sg_cpu);
440
441 util = sugov_get_util(sg_cpu);
442 max = sg_cpu->max;
443 sugov_iowait_apply(sg_cpu, time, &util, &max);
444 next_f = get_next_freq(sg_policy, util, max);
445 /*
446 * Do not reduce the frequency if the CPU has not been idle
447 * recently, as the reduction is likely to be premature then.
448 */
449 if (busy && next_f < sg_policy->next_freq) {
450 next_f = sg_policy->next_freq;
451
452 /* Reset cached freq as next_freq has changed */
453 sg_policy->cached_raw_freq = 0;
454 }
455
456 /*
457 * This code runs under rq->lock for the target CPU, so it won't run
458 * concurrently on two different CPUs for the same target and it is not
459 * necessary to acquire the lock in the fast switch case.
460 */
461 if (sg_policy->policy->fast_switch_enabled) {
462 sugov_fast_switch(sg_policy, time, next_f);
463 } else {
464 raw_spin_lock(&sg_policy->update_lock);
465 sugov_deferred_update(sg_policy, time, next_f);
466 raw_spin_unlock(&sg_policy->update_lock);
467 }
468 }
469
sugov_next_freq_shared(struct sugov_cpu * sg_cpu,u64 time)470 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
471 {
472 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
473 struct cpufreq_policy *policy = sg_policy->policy;
474 unsigned long util = 0, max = 1;
475 unsigned int j;
476
477 for_each_cpu(j, policy->cpus) {
478 struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
479 unsigned long j_util, j_max;
480
481 j_util = sugov_get_util(j_sg_cpu);
482 j_max = j_sg_cpu->max;
483 sugov_iowait_apply(j_sg_cpu, time, &j_util, &j_max);
484
485 if (j_util * max > j_max * util) {
486 util = j_util;
487 max = j_max;
488 }
489 }
490
491 return get_next_freq(sg_policy, util, max);
492 }
493
494 static void
sugov_update_shared(struct update_util_data * hook,u64 time,unsigned int flags)495 sugov_update_shared(struct update_util_data *hook, u64 time, unsigned int flags)
496 {
497 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
498 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
499 unsigned int next_f;
500
501 raw_spin_lock(&sg_policy->update_lock);
502
503 sugov_iowait_boost(sg_cpu, time, flags);
504 sg_cpu->last_update = time;
505
506 ignore_dl_rate_limit(sg_cpu, sg_policy);
507
508 if (sugov_should_update_freq(sg_policy, time)) {
509 next_f = sugov_next_freq_shared(sg_cpu, time);
510
511 if (sg_policy->policy->fast_switch_enabled)
512 sugov_fast_switch(sg_policy, time, next_f);
513 else
514 sugov_deferred_update(sg_policy, time, next_f);
515 }
516
517 raw_spin_unlock(&sg_policy->update_lock);
518 }
519
sugov_work(struct kthread_work * work)520 static void sugov_work(struct kthread_work *work)
521 {
522 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
523 unsigned int freq;
524 unsigned long flags;
525
526 /*
527 * Hold sg_policy->update_lock shortly to handle the case where:
528 * incase sg_policy->next_freq is read here, and then updated by
529 * sugov_deferred_update() just before work_in_progress is set to false
530 * here, we may miss queueing the new update.
531 *
532 * Note: If a work was queued after the update_lock is released,
533 * sugov_work() will just be called again by kthread_work code; and the
534 * request will be proceed before the sugov thread sleeps.
535 */
536 raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
537 freq = sg_policy->next_freq;
538 sg_policy->work_in_progress = false;
539 raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
540
541 mutex_lock(&sg_policy->work_lock);
542 __cpufreq_driver_target(sg_policy->policy, freq, CPUFREQ_RELATION_L);
543 mutex_unlock(&sg_policy->work_lock);
544 }
545
sugov_irq_work(struct irq_work * irq_work)546 static void sugov_irq_work(struct irq_work *irq_work)
547 {
548 struct sugov_policy *sg_policy;
549
550 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
551
552 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
553 }
554
555 /************************** sysfs interface ************************/
556
557 static struct sugov_tunables *global_tunables;
558 static DEFINE_MUTEX(global_tunables_lock);
559
to_sugov_tunables(struct gov_attr_set * attr_set)560 static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
561 {
562 return container_of(attr_set, struct sugov_tunables, attr_set);
563 }
564
rate_limit_us_show(struct gov_attr_set * attr_set,char * buf)565 static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
566 {
567 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
568
569 return sprintf(buf, "%u\n", tunables->rate_limit_us);
570 }
571
572 static ssize_t
rate_limit_us_store(struct gov_attr_set * attr_set,const char * buf,size_t count)573 rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf, size_t count)
574 {
575 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
576 struct sugov_policy *sg_policy;
577 unsigned int rate_limit_us;
578
579 if (kstrtouint(buf, 10, &rate_limit_us))
580 return -EINVAL;
581
582 tunables->rate_limit_us = rate_limit_us;
583
584 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
585 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
586
587 return count;
588 }
589
590 static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
591
592 static struct attribute *sugov_attributes[] = {
593 &rate_limit_us.attr,
594 NULL
595 };
596
597 static struct kobj_type sugov_tunables_ktype = {
598 .default_attrs = sugov_attributes,
599 .sysfs_ops = &governor_sysfs_ops,
600 };
601
602 /********************** cpufreq governor interface *********************/
603
604 static struct cpufreq_governor schedutil_gov;
605
sugov_policy_alloc(struct cpufreq_policy * policy)606 static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
607 {
608 struct sugov_policy *sg_policy;
609
610 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
611 if (!sg_policy)
612 return NULL;
613
614 sg_policy->policy = policy;
615 raw_spin_lock_init(&sg_policy->update_lock);
616 return sg_policy;
617 }
618
sugov_policy_free(struct sugov_policy * sg_policy)619 static void sugov_policy_free(struct sugov_policy *sg_policy)
620 {
621 kfree(sg_policy);
622 }
623
sugov_kthread_create(struct sugov_policy * sg_policy)624 static int sugov_kthread_create(struct sugov_policy *sg_policy)
625 {
626 struct task_struct *thread;
627 struct sched_attr attr = {
628 .size = sizeof(struct sched_attr),
629 .sched_policy = SCHED_DEADLINE,
630 .sched_flags = SCHED_FLAG_SUGOV,
631 .sched_nice = 0,
632 .sched_priority = 0,
633 /*
634 * Fake (unused) bandwidth; workaround to "fix"
635 * priority inheritance.
636 */
637 .sched_runtime = 1000000,
638 .sched_deadline = 10000000,
639 .sched_period = 10000000,
640 };
641 struct cpufreq_policy *policy = sg_policy->policy;
642 int ret;
643
644 /* kthread only required for slow path */
645 if (policy->fast_switch_enabled)
646 return 0;
647
648 kthread_init_work(&sg_policy->work, sugov_work);
649 kthread_init_worker(&sg_policy->worker);
650 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
651 "sugov:%d",
652 cpumask_first(policy->related_cpus));
653 if (IS_ERR(thread)) {
654 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
655 return PTR_ERR(thread);
656 }
657
658 ret = sched_setattr_nocheck(thread, &attr);
659 if (ret) {
660 kthread_stop(thread);
661 pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__);
662 return ret;
663 }
664
665 sg_policy->thread = thread;
666 kthread_bind_mask(thread, policy->related_cpus);
667 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
668 mutex_init(&sg_policy->work_lock);
669
670 wake_up_process(thread);
671
672 return 0;
673 }
674
sugov_kthread_stop(struct sugov_policy * sg_policy)675 static void sugov_kthread_stop(struct sugov_policy *sg_policy)
676 {
677 /* kthread only required for slow path */
678 if (sg_policy->policy->fast_switch_enabled)
679 return;
680
681 kthread_flush_worker(&sg_policy->worker);
682 kthread_stop(sg_policy->thread);
683 mutex_destroy(&sg_policy->work_lock);
684 }
685
sugov_tunables_alloc(struct sugov_policy * sg_policy)686 static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
687 {
688 struct sugov_tunables *tunables;
689
690 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
691 if (tunables) {
692 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
693 if (!have_governor_per_policy())
694 global_tunables = tunables;
695 }
696 return tunables;
697 }
698
sugov_tunables_free(struct sugov_tunables * tunables)699 static void sugov_tunables_free(struct sugov_tunables *tunables)
700 {
701 if (!have_governor_per_policy())
702 global_tunables = NULL;
703
704 kfree(tunables);
705 }
706
sugov_init(struct cpufreq_policy * policy)707 static int sugov_init(struct cpufreq_policy *policy)
708 {
709 struct sugov_policy *sg_policy;
710 struct sugov_tunables *tunables;
711 int ret = 0;
712
713 /* State should be equivalent to EXIT */
714 if (policy->governor_data)
715 return -EBUSY;
716
717 cpufreq_enable_fast_switch(policy);
718
719 sg_policy = sugov_policy_alloc(policy);
720 if (!sg_policy) {
721 ret = -ENOMEM;
722 goto disable_fast_switch;
723 }
724
725 ret = sugov_kthread_create(sg_policy);
726 if (ret)
727 goto free_sg_policy;
728
729 mutex_lock(&global_tunables_lock);
730
731 if (global_tunables) {
732 if (WARN_ON(have_governor_per_policy())) {
733 ret = -EINVAL;
734 goto stop_kthread;
735 }
736 policy->governor_data = sg_policy;
737 sg_policy->tunables = global_tunables;
738
739 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
740 goto out;
741 }
742
743 tunables = sugov_tunables_alloc(sg_policy);
744 if (!tunables) {
745 ret = -ENOMEM;
746 goto stop_kthread;
747 }
748
749 tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy);
750
751 policy->governor_data = sg_policy;
752 sg_policy->tunables = tunables;
753
754 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
755 get_governor_parent_kobj(policy), "%s",
756 schedutil_gov.name);
757 if (ret)
758 goto fail;
759
760 out:
761 mutex_unlock(&global_tunables_lock);
762 return 0;
763
764 fail:
765 policy->governor_data = NULL;
766 sugov_tunables_free(tunables);
767
768 stop_kthread:
769 sugov_kthread_stop(sg_policy);
770 mutex_unlock(&global_tunables_lock);
771
772 free_sg_policy:
773 sugov_policy_free(sg_policy);
774
775 disable_fast_switch:
776 cpufreq_disable_fast_switch(policy);
777
778 pr_err("initialization failed (error %d)\n", ret);
779 return ret;
780 }
781
sugov_exit(struct cpufreq_policy * policy)782 static void sugov_exit(struct cpufreq_policy *policy)
783 {
784 struct sugov_policy *sg_policy = policy->governor_data;
785 struct sugov_tunables *tunables = sg_policy->tunables;
786 unsigned int count;
787
788 mutex_lock(&global_tunables_lock);
789
790 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
791 policy->governor_data = NULL;
792 if (!count)
793 sugov_tunables_free(tunables);
794
795 mutex_unlock(&global_tunables_lock);
796
797 sugov_kthread_stop(sg_policy);
798 sugov_policy_free(sg_policy);
799 cpufreq_disable_fast_switch(policy);
800 }
801
sugov_start(struct cpufreq_policy * policy)802 static int sugov_start(struct cpufreq_policy *policy)
803 {
804 struct sugov_policy *sg_policy = policy->governor_data;
805 unsigned int cpu;
806
807 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
808 sg_policy->last_freq_update_time = 0;
809 sg_policy->next_freq = 0;
810 sg_policy->work_in_progress = false;
811 sg_policy->need_freq_update = false;
812 sg_policy->cached_raw_freq = 0;
813
814 for_each_cpu(cpu, policy->cpus) {
815 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
816
817 memset(sg_cpu, 0, sizeof(*sg_cpu));
818 sg_cpu->cpu = cpu;
819 sg_cpu->sg_policy = sg_policy;
820 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
821 }
822
823 for_each_cpu(cpu, policy->cpus) {
824 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
825
826 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
827 policy_is_shared(policy) ?
828 sugov_update_shared :
829 sugov_update_single);
830 }
831 return 0;
832 }
833
sugov_stop(struct cpufreq_policy * policy)834 static void sugov_stop(struct cpufreq_policy *policy)
835 {
836 struct sugov_policy *sg_policy = policy->governor_data;
837 unsigned int cpu;
838
839 for_each_cpu(cpu, policy->cpus)
840 cpufreq_remove_update_util_hook(cpu);
841
842 synchronize_sched();
843
844 if (!policy->fast_switch_enabled) {
845 irq_work_sync(&sg_policy->irq_work);
846 kthread_cancel_work_sync(&sg_policy->work);
847 }
848 }
849
sugov_limits(struct cpufreq_policy * policy)850 static void sugov_limits(struct cpufreq_policy *policy)
851 {
852 struct sugov_policy *sg_policy = policy->governor_data;
853
854 if (!policy->fast_switch_enabled) {
855 mutex_lock(&sg_policy->work_lock);
856 cpufreq_policy_apply_limits(policy);
857 mutex_unlock(&sg_policy->work_lock);
858 }
859
860 sg_policy->need_freq_update = true;
861 }
862
863 static struct cpufreq_governor schedutil_gov = {
864 .name = "schedutil",
865 .owner = THIS_MODULE,
866 .dynamic_switching = true,
867 .init = sugov_init,
868 .exit = sugov_exit,
869 .start = sugov_start,
870 .stop = sugov_stop,
871 .limits = sugov_limits,
872 };
873
874 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
cpufreq_default_governor(void)875 struct cpufreq_governor *cpufreq_default_governor(void)
876 {
877 return &schedutil_gov;
878 }
879 #endif
880
sugov_register(void)881 static int __init sugov_register(void)
882 {
883 return cpufreq_register_governor(&schedutil_gov);
884 }
885 fs_initcall(sugov_register);
886