cpufreq_schedutil.c 24 KB

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