cpufreq_schedutil.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 <linux/cpufreq.h>
  13. #include <linux/kthread.h>
  14. #include <linux/slab.h>
  15. #include <trace/events/power.h>
  16. #include "sched.h"
  17. #define SUGOV_KTHREAD_PRIORITY 50
  18. struct sugov_tunables {
  19. struct gov_attr_set attr_set;
  20. unsigned int rate_limit_us;
  21. };
  22. struct sugov_policy {
  23. struct cpufreq_policy *policy;
  24. struct sugov_tunables *tunables;
  25. struct list_head tunables_hook;
  26. raw_spinlock_t update_lock; /* For shared policies */
  27. u64 last_freq_update_time;
  28. s64 freq_update_delay_ns;
  29. unsigned int next_freq;
  30. /* The next fields are only needed if fast switch cannot be used. */
  31. struct irq_work irq_work;
  32. struct kthread_work work;
  33. struct mutex work_lock;
  34. struct kthread_worker worker;
  35. struct task_struct *thread;
  36. bool work_in_progress;
  37. bool need_freq_update;
  38. };
  39. struct sugov_cpu {
  40. struct update_util_data update_util;
  41. struct sugov_policy *sg_policy;
  42. unsigned int cached_raw_freq;
  43. unsigned long iowait_boost;
  44. unsigned long iowait_boost_max;
  45. u64 last_update;
  46. /* The fields below are only needed when sharing a policy. */
  47. unsigned long util;
  48. unsigned long max;
  49. unsigned int flags;
  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. if (sg_policy->work_in_progress)
  57. return false;
  58. if (unlikely(sg_policy->need_freq_update)) {
  59. sg_policy->need_freq_update = false;
  60. /*
  61. * This happens when limits change, so forget the previous
  62. * next_freq value and force an update.
  63. */
  64. sg_policy->next_freq = UINT_MAX;
  65. return true;
  66. }
  67. delta_ns = time - sg_policy->last_freq_update_time;
  68. return delta_ns >= sg_policy->freq_update_delay_ns;
  69. }
  70. static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
  71. unsigned int next_freq)
  72. {
  73. struct cpufreq_policy *policy = sg_policy->policy;
  74. sg_policy->last_freq_update_time = time;
  75. if (policy->fast_switch_enabled) {
  76. if (sg_policy->next_freq == next_freq) {
  77. trace_cpu_frequency(policy->cur, smp_processor_id());
  78. return;
  79. }
  80. sg_policy->next_freq = next_freq;
  81. next_freq = cpufreq_driver_fast_switch(policy, next_freq);
  82. if (next_freq == CPUFREQ_ENTRY_INVALID)
  83. return;
  84. policy->cur = next_freq;
  85. trace_cpu_frequency(next_freq, smp_processor_id());
  86. } else if (sg_policy->next_freq != next_freq) {
  87. sg_policy->next_freq = next_freq;
  88. sg_policy->work_in_progress = true;
  89. irq_work_queue(&sg_policy->irq_work);
  90. }
  91. }
  92. /**
  93. * get_next_freq - Compute a new frequency for a given cpufreq policy.
  94. * @sg_cpu: schedutil cpu object to compute the new frequency for.
  95. * @util: Current CPU utilization.
  96. * @max: CPU capacity.
  97. *
  98. * If the utilization is frequency-invariant, choose the new frequency to be
  99. * proportional to it, that is
  100. *
  101. * next_freq = C * max_freq * util / max
  102. *
  103. * Otherwise, approximate the would-be frequency-invariant utilization by
  104. * util_raw * (curr_freq / max_freq) which leads to
  105. *
  106. * next_freq = C * curr_freq * util_raw / max
  107. *
  108. * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
  109. *
  110. * The lowest driver-supported frequency which is equal or greater than the raw
  111. * next_freq (as calculated above) is returned, subject to policy min/max and
  112. * cpufreq driver limitations.
  113. */
  114. static unsigned int get_next_freq(struct sugov_cpu *sg_cpu, unsigned long util,
  115. unsigned long max)
  116. {
  117. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  118. struct cpufreq_policy *policy = sg_policy->policy;
  119. unsigned int freq = arch_scale_freq_invariant() ?
  120. policy->cpuinfo.max_freq : policy->cur;
  121. freq = (freq + (freq >> 2)) * util / max;
  122. if (freq == sg_cpu->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
  123. return sg_policy->next_freq;
  124. sg_cpu->cached_raw_freq = freq;
  125. return cpufreq_driver_resolve_freq(policy, freq);
  126. }
  127. static void sugov_get_util(unsigned long *util, unsigned long *max)
  128. {
  129. struct rq *rq = this_rq();
  130. unsigned long cfs_max;
  131. cfs_max = arch_scale_cpu_capacity(NULL, smp_processor_id());
  132. *util = min(rq->cfs.avg.util_avg, cfs_max);
  133. *max = cfs_max;
  134. }
  135. static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
  136. unsigned int flags)
  137. {
  138. if (flags & SCHED_CPUFREQ_IOWAIT) {
  139. sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
  140. } else if (sg_cpu->iowait_boost) {
  141. s64 delta_ns = time - sg_cpu->last_update;
  142. /* Clear iowait_boost if the CPU apprears to have been idle. */
  143. if (delta_ns > TICK_NSEC)
  144. sg_cpu->iowait_boost = 0;
  145. }
  146. }
  147. static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
  148. unsigned long *max)
  149. {
  150. unsigned long boost_util = sg_cpu->iowait_boost;
  151. unsigned long boost_max = sg_cpu->iowait_boost_max;
  152. if (!boost_util)
  153. return;
  154. if (*util * boost_max < *max * boost_util) {
  155. *util = boost_util;
  156. *max = boost_max;
  157. }
  158. sg_cpu->iowait_boost >>= 1;
  159. }
  160. static void sugov_update_single(struct update_util_data *hook, u64 time,
  161. unsigned int flags)
  162. {
  163. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  164. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  165. struct cpufreq_policy *policy = sg_policy->policy;
  166. unsigned long util, max;
  167. unsigned int next_f;
  168. sugov_set_iowait_boost(sg_cpu, time, flags);
  169. sg_cpu->last_update = time;
  170. if (!sugov_should_update_freq(sg_policy, time))
  171. return;
  172. if (flags & SCHED_CPUFREQ_RT_DL) {
  173. next_f = policy->cpuinfo.max_freq;
  174. } else {
  175. sugov_get_util(&util, &max);
  176. sugov_iowait_boost(sg_cpu, &util, &max);
  177. next_f = get_next_freq(sg_cpu, util, max);
  178. }
  179. sugov_update_commit(sg_policy, time, next_f);
  180. }
  181. static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu,
  182. unsigned long util, unsigned long max,
  183. unsigned int flags)
  184. {
  185. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  186. struct cpufreq_policy *policy = sg_policy->policy;
  187. unsigned int max_f = policy->cpuinfo.max_freq;
  188. u64 last_freq_update_time = sg_policy->last_freq_update_time;
  189. unsigned int j;
  190. if (flags & SCHED_CPUFREQ_RT_DL)
  191. return max_f;
  192. sugov_iowait_boost(sg_cpu, &util, &max);
  193. for_each_cpu(j, policy->cpus) {
  194. struct sugov_cpu *j_sg_cpu;
  195. unsigned long j_util, j_max;
  196. s64 delta_ns;
  197. if (j == smp_processor_id())
  198. continue;
  199. j_sg_cpu = &per_cpu(sugov_cpu, j);
  200. /*
  201. * If the CPU utilization was last updated before the previous
  202. * frequency update and the time elapsed between the last update
  203. * of the CPU utilization and the last frequency update is long
  204. * enough, don't take the CPU into account as it probably is
  205. * idle now (and clear iowait_boost for it).
  206. */
  207. delta_ns = last_freq_update_time - j_sg_cpu->last_update;
  208. if (delta_ns > TICK_NSEC) {
  209. j_sg_cpu->iowait_boost = 0;
  210. continue;
  211. }
  212. if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
  213. return max_f;
  214. j_util = j_sg_cpu->util;
  215. j_max = j_sg_cpu->max;
  216. if (j_util * max > j_max * util) {
  217. util = j_util;
  218. max = j_max;
  219. }
  220. sugov_iowait_boost(j_sg_cpu, &util, &max);
  221. }
  222. return get_next_freq(sg_cpu, util, max);
  223. }
  224. static void sugov_update_shared(struct update_util_data *hook, u64 time,
  225. unsigned int flags)
  226. {
  227. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  228. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  229. unsigned long util, max;
  230. unsigned int next_f;
  231. sugov_get_util(&util, &max);
  232. raw_spin_lock(&sg_policy->update_lock);
  233. sg_cpu->util = util;
  234. sg_cpu->max = max;
  235. sg_cpu->flags = flags;
  236. sugov_set_iowait_boost(sg_cpu, time, flags);
  237. sg_cpu->last_update = time;
  238. if (sugov_should_update_freq(sg_policy, time)) {
  239. next_f = sugov_next_freq_shared(sg_cpu, util, max, flags);
  240. sugov_update_commit(sg_policy, time, next_f);
  241. }
  242. raw_spin_unlock(&sg_policy->update_lock);
  243. }
  244. static void sugov_work(struct kthread_work *work)
  245. {
  246. struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
  247. mutex_lock(&sg_policy->work_lock);
  248. __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
  249. CPUFREQ_RELATION_L);
  250. mutex_unlock(&sg_policy->work_lock);
  251. sg_policy->work_in_progress = false;
  252. }
  253. static void sugov_irq_work(struct irq_work *irq_work)
  254. {
  255. struct sugov_policy *sg_policy;
  256. sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
  257. /*
  258. * For RT and deadline tasks, the schedutil governor shoots the
  259. * frequency to maximum. Special care must be taken to ensure that this
  260. * kthread doesn't result in the same behavior.
  261. *
  262. * This is (mostly) guaranteed by the work_in_progress flag. The flag is
  263. * updated only at the end of the sugov_work() function and before that
  264. * the schedutil governor rejects all other frequency scaling requests.
  265. *
  266. * There is a very rare case though, where the RT thread yields right
  267. * after the work_in_progress flag is cleared. The effects of that are
  268. * neglected for now.
  269. */
  270. kthread_queue_work(&sg_policy->worker, &sg_policy->work);
  271. }
  272. /************************** sysfs interface ************************/
  273. static struct sugov_tunables *global_tunables;
  274. static DEFINE_MUTEX(global_tunables_lock);
  275. static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
  276. {
  277. return container_of(attr_set, struct sugov_tunables, attr_set);
  278. }
  279. static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
  280. {
  281. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  282. return sprintf(buf, "%u\n", tunables->rate_limit_us);
  283. }
  284. static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
  285. size_t count)
  286. {
  287. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  288. struct sugov_policy *sg_policy;
  289. unsigned int rate_limit_us;
  290. if (kstrtouint(buf, 10, &rate_limit_us))
  291. return -EINVAL;
  292. tunables->rate_limit_us = rate_limit_us;
  293. list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
  294. sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
  295. return count;
  296. }
  297. static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
  298. static struct attribute *sugov_attributes[] = {
  299. &rate_limit_us.attr,
  300. NULL
  301. };
  302. static struct kobj_type sugov_tunables_ktype = {
  303. .default_attrs = sugov_attributes,
  304. .sysfs_ops = &governor_sysfs_ops,
  305. };
  306. /********************** cpufreq governor interface *********************/
  307. static struct cpufreq_governor schedutil_gov;
  308. static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
  309. {
  310. struct sugov_policy *sg_policy;
  311. sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
  312. if (!sg_policy)
  313. return NULL;
  314. sg_policy->policy = policy;
  315. raw_spin_lock_init(&sg_policy->update_lock);
  316. return sg_policy;
  317. }
  318. static void sugov_policy_free(struct sugov_policy *sg_policy)
  319. {
  320. kfree(sg_policy);
  321. }
  322. static int sugov_kthread_create(struct sugov_policy *sg_policy)
  323. {
  324. struct task_struct *thread;
  325. struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
  326. struct cpufreq_policy *policy = sg_policy->policy;
  327. int ret;
  328. /* kthread only required for slow path */
  329. if (policy->fast_switch_enabled)
  330. return 0;
  331. kthread_init_work(&sg_policy->work, sugov_work);
  332. kthread_init_worker(&sg_policy->worker);
  333. thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
  334. "sugov:%d",
  335. cpumask_first(policy->related_cpus));
  336. if (IS_ERR(thread)) {
  337. pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
  338. return PTR_ERR(thread);
  339. }
  340. ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
  341. if (ret) {
  342. kthread_stop(thread);
  343. pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
  344. return ret;
  345. }
  346. sg_policy->thread = thread;
  347. kthread_bind_mask(thread, policy->related_cpus);
  348. init_irq_work(&sg_policy->irq_work, sugov_irq_work);
  349. mutex_init(&sg_policy->work_lock);
  350. wake_up_process(thread);
  351. return 0;
  352. }
  353. static void sugov_kthread_stop(struct sugov_policy *sg_policy)
  354. {
  355. /* kthread only required for slow path */
  356. if (sg_policy->policy->fast_switch_enabled)
  357. return;
  358. kthread_flush_worker(&sg_policy->worker);
  359. kthread_stop(sg_policy->thread);
  360. mutex_destroy(&sg_policy->work_lock);
  361. }
  362. static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
  363. {
  364. struct sugov_tunables *tunables;
  365. tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
  366. if (tunables) {
  367. gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
  368. if (!have_governor_per_policy())
  369. global_tunables = tunables;
  370. }
  371. return tunables;
  372. }
  373. static void sugov_tunables_free(struct sugov_tunables *tunables)
  374. {
  375. if (!have_governor_per_policy())
  376. global_tunables = NULL;
  377. kfree(tunables);
  378. }
  379. static int sugov_init(struct cpufreq_policy *policy)
  380. {
  381. struct sugov_policy *sg_policy;
  382. struct sugov_tunables *tunables;
  383. unsigned int lat;
  384. int ret = 0;
  385. /* State should be equivalent to EXIT */
  386. if (policy->governor_data)
  387. return -EBUSY;
  388. cpufreq_enable_fast_switch(policy);
  389. sg_policy = sugov_policy_alloc(policy);
  390. if (!sg_policy) {
  391. ret = -ENOMEM;
  392. goto disable_fast_switch;
  393. }
  394. ret = sugov_kthread_create(sg_policy);
  395. if (ret)
  396. goto free_sg_policy;
  397. mutex_lock(&global_tunables_lock);
  398. if (global_tunables) {
  399. if (WARN_ON(have_governor_per_policy())) {
  400. ret = -EINVAL;
  401. goto stop_kthread;
  402. }
  403. policy->governor_data = sg_policy;
  404. sg_policy->tunables = global_tunables;
  405. gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
  406. goto out;
  407. }
  408. tunables = sugov_tunables_alloc(sg_policy);
  409. if (!tunables) {
  410. ret = -ENOMEM;
  411. goto stop_kthread;
  412. }
  413. tunables->rate_limit_us = LATENCY_MULTIPLIER;
  414. lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
  415. if (lat)
  416. tunables->rate_limit_us *= lat;
  417. policy->governor_data = sg_policy;
  418. sg_policy->tunables = tunables;
  419. ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
  420. get_governor_parent_kobj(policy), "%s",
  421. schedutil_gov.name);
  422. if (ret)
  423. goto fail;
  424. out:
  425. mutex_unlock(&global_tunables_lock);
  426. return 0;
  427. fail:
  428. policy->governor_data = NULL;
  429. sugov_tunables_free(tunables);
  430. stop_kthread:
  431. sugov_kthread_stop(sg_policy);
  432. free_sg_policy:
  433. mutex_unlock(&global_tunables_lock);
  434. sugov_policy_free(sg_policy);
  435. disable_fast_switch:
  436. cpufreq_disable_fast_switch(policy);
  437. pr_err("initialization failed (error %d)\n", ret);
  438. return ret;
  439. }
  440. static void sugov_exit(struct cpufreq_policy *policy)
  441. {
  442. struct sugov_policy *sg_policy = policy->governor_data;
  443. struct sugov_tunables *tunables = sg_policy->tunables;
  444. unsigned int count;
  445. mutex_lock(&global_tunables_lock);
  446. count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
  447. policy->governor_data = NULL;
  448. if (!count)
  449. sugov_tunables_free(tunables);
  450. mutex_unlock(&global_tunables_lock);
  451. sugov_kthread_stop(sg_policy);
  452. sugov_policy_free(sg_policy);
  453. cpufreq_disable_fast_switch(policy);
  454. }
  455. static int sugov_start(struct cpufreq_policy *policy)
  456. {
  457. struct sugov_policy *sg_policy = policy->governor_data;
  458. unsigned int cpu;
  459. sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
  460. sg_policy->last_freq_update_time = 0;
  461. sg_policy->next_freq = UINT_MAX;
  462. sg_policy->work_in_progress = false;
  463. sg_policy->need_freq_update = false;
  464. for_each_cpu(cpu, policy->cpus) {
  465. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  466. sg_cpu->sg_policy = sg_policy;
  467. if (policy_is_shared(policy)) {
  468. sg_cpu->util = 0;
  469. sg_cpu->max = 0;
  470. sg_cpu->flags = SCHED_CPUFREQ_RT;
  471. sg_cpu->last_update = 0;
  472. sg_cpu->cached_raw_freq = 0;
  473. sg_cpu->iowait_boost = 0;
  474. sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
  475. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  476. sugov_update_shared);
  477. } else {
  478. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  479. sugov_update_single);
  480. }
  481. }
  482. return 0;
  483. }
  484. static void sugov_stop(struct cpufreq_policy *policy)
  485. {
  486. struct sugov_policy *sg_policy = policy->governor_data;
  487. unsigned int cpu;
  488. for_each_cpu(cpu, policy->cpus)
  489. cpufreq_remove_update_util_hook(cpu);
  490. synchronize_sched();
  491. if (!policy->fast_switch_enabled) {
  492. irq_work_sync(&sg_policy->irq_work);
  493. kthread_cancel_work_sync(&sg_policy->work);
  494. }
  495. }
  496. static void sugov_limits(struct cpufreq_policy *policy)
  497. {
  498. struct sugov_policy *sg_policy = policy->governor_data;
  499. if (!policy->fast_switch_enabled) {
  500. mutex_lock(&sg_policy->work_lock);
  501. cpufreq_policy_apply_limits(policy);
  502. mutex_unlock(&sg_policy->work_lock);
  503. }
  504. sg_policy->need_freq_update = true;
  505. }
  506. static struct cpufreq_governor schedutil_gov = {
  507. .name = "schedutil",
  508. .owner = THIS_MODULE,
  509. .init = sugov_init,
  510. .exit = sugov_exit,
  511. .start = sugov_start,
  512. .stop = sugov_stop,
  513. .limits = sugov_limits,
  514. };
  515. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
  516. struct cpufreq_governor *cpufreq_default_governor(void)
  517. {
  518. return &schedutil_gov;
  519. }
  520. #endif
  521. static int __init sugov_register(void)
  522. {
  523. return cpufreq_register_governor(&schedutil_gov);
  524. }
  525. fs_initcall(sugov_register);