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