cpufreq_schedutil.c 26 KB

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