cpufreq_schedutil.c 24 KB

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