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