cpufreq_schedutil.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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/module.h>
  14. #include <linux/slab.h>
  15. #include <trace/events/power.h>
  16. #include "sched.h"
  17. struct sugov_tunables {
  18. struct gov_attr_set attr_set;
  19. unsigned int rate_limit_us;
  20. };
  21. struct sugov_policy {
  22. struct cpufreq_policy *policy;
  23. struct sugov_tunables *tunables;
  24. struct list_head tunables_hook;
  25. raw_spinlock_t update_lock; /* For shared policies */
  26. u64 last_freq_update_time;
  27. s64 freq_update_delay_ns;
  28. unsigned int next_freq;
  29. /* The next fields are only needed if fast switch cannot be used. */
  30. struct irq_work irq_work;
  31. struct work_struct work;
  32. struct mutex work_lock;
  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. /* The fields below are only needed when sharing a policy. */
  40. unsigned long util;
  41. unsigned long max;
  42. u64 last_update;
  43. };
  44. static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
  45. /************************ Governor internals ***********************/
  46. static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
  47. {
  48. s64 delta_ns;
  49. if (sg_policy->work_in_progress)
  50. return false;
  51. if (unlikely(sg_policy->need_freq_update)) {
  52. sg_policy->need_freq_update = false;
  53. /*
  54. * This happens when limits change, so forget the previous
  55. * next_freq value and force an update.
  56. */
  57. sg_policy->next_freq = UINT_MAX;
  58. return true;
  59. }
  60. delta_ns = time - sg_policy->last_freq_update_time;
  61. return delta_ns >= sg_policy->freq_update_delay_ns;
  62. }
  63. static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
  64. unsigned int next_freq)
  65. {
  66. struct cpufreq_policy *policy = sg_policy->policy;
  67. sg_policy->last_freq_update_time = time;
  68. if (policy->fast_switch_enabled) {
  69. if (sg_policy->next_freq == next_freq) {
  70. trace_cpu_frequency(policy->cur, smp_processor_id());
  71. return;
  72. }
  73. sg_policy->next_freq = next_freq;
  74. next_freq = cpufreq_driver_fast_switch(policy, next_freq);
  75. if (next_freq == CPUFREQ_ENTRY_INVALID)
  76. return;
  77. policy->cur = next_freq;
  78. trace_cpu_frequency(next_freq, smp_processor_id());
  79. } else if (sg_policy->next_freq != next_freq) {
  80. sg_policy->next_freq = next_freq;
  81. sg_policy->work_in_progress = true;
  82. irq_work_queue(&sg_policy->irq_work);
  83. }
  84. }
  85. /**
  86. * get_next_freq - Compute a new frequency for a given cpufreq policy.
  87. * @policy: cpufreq policy object to compute the new frequency for.
  88. * @util: Current CPU utilization.
  89. * @max: CPU capacity.
  90. *
  91. * If the utilization is frequency-invariant, choose the new frequency to be
  92. * proportional to it, that is
  93. *
  94. * next_freq = C * max_freq * util / max
  95. *
  96. * Otherwise, approximate the would-be frequency-invariant utilization by
  97. * util_raw * (curr_freq / max_freq) which leads to
  98. *
  99. * next_freq = C * curr_freq * util_raw / max
  100. *
  101. * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
  102. */
  103. static unsigned int get_next_freq(struct cpufreq_policy *policy,
  104. unsigned long util, unsigned long max)
  105. {
  106. unsigned int freq = arch_scale_freq_invariant() ?
  107. policy->cpuinfo.max_freq : policy->cur;
  108. return (freq + (freq >> 2)) * util / max;
  109. }
  110. static void sugov_update_single(struct update_util_data *hook, u64 time,
  111. unsigned long util, unsigned long max)
  112. {
  113. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  114. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  115. struct cpufreq_policy *policy = sg_policy->policy;
  116. unsigned int next_f;
  117. if (!sugov_should_update_freq(sg_policy, time))
  118. return;
  119. next_f = util == ULONG_MAX ? policy->cpuinfo.max_freq :
  120. get_next_freq(policy, util, max);
  121. sugov_update_commit(sg_policy, time, next_f);
  122. }
  123. static unsigned int sugov_next_freq_shared(struct sugov_policy *sg_policy,
  124. unsigned long util, unsigned long max)
  125. {
  126. struct cpufreq_policy *policy = sg_policy->policy;
  127. unsigned int max_f = policy->cpuinfo.max_freq;
  128. u64 last_freq_update_time = sg_policy->last_freq_update_time;
  129. unsigned int j;
  130. if (util == ULONG_MAX)
  131. return max_f;
  132. for_each_cpu(j, policy->cpus) {
  133. struct sugov_cpu *j_sg_cpu;
  134. unsigned long j_util, j_max;
  135. s64 delta_ns;
  136. if (j == smp_processor_id())
  137. continue;
  138. j_sg_cpu = &per_cpu(sugov_cpu, j);
  139. /*
  140. * If the CPU utilization was last updated before the previous
  141. * frequency update and the time elapsed between the last update
  142. * of the CPU utilization and the last frequency update is long
  143. * enough, don't take the CPU into account as it probably is
  144. * idle now.
  145. */
  146. delta_ns = last_freq_update_time - j_sg_cpu->last_update;
  147. if (delta_ns > TICK_NSEC)
  148. continue;
  149. j_util = j_sg_cpu->util;
  150. if (j_util == ULONG_MAX)
  151. return max_f;
  152. j_max = j_sg_cpu->max;
  153. if (j_util * max > j_max * util) {
  154. util = j_util;
  155. max = j_max;
  156. }
  157. }
  158. return get_next_freq(policy, util, max);
  159. }
  160. static void sugov_update_shared(struct update_util_data *hook, u64 time,
  161. unsigned long util, unsigned long max)
  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. unsigned int next_f;
  166. raw_spin_lock(&sg_policy->update_lock);
  167. sg_cpu->util = util;
  168. sg_cpu->max = max;
  169. sg_cpu->last_update = time;
  170. if (sugov_should_update_freq(sg_policy, time)) {
  171. next_f = sugov_next_freq_shared(sg_policy, util, max);
  172. sugov_update_commit(sg_policy, time, next_f);
  173. }
  174. raw_spin_unlock(&sg_policy->update_lock);
  175. }
  176. static void sugov_work(struct work_struct *work)
  177. {
  178. struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
  179. mutex_lock(&sg_policy->work_lock);
  180. __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
  181. CPUFREQ_RELATION_L);
  182. mutex_unlock(&sg_policy->work_lock);
  183. sg_policy->work_in_progress = false;
  184. }
  185. static void sugov_irq_work(struct irq_work *irq_work)
  186. {
  187. struct sugov_policy *sg_policy;
  188. sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
  189. schedule_work_on(smp_processor_id(), &sg_policy->work);
  190. }
  191. /************************** sysfs interface ************************/
  192. static struct sugov_tunables *global_tunables;
  193. static DEFINE_MUTEX(global_tunables_lock);
  194. static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
  195. {
  196. return container_of(attr_set, struct sugov_tunables, attr_set);
  197. }
  198. static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
  199. {
  200. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  201. return sprintf(buf, "%u\n", tunables->rate_limit_us);
  202. }
  203. static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
  204. size_t count)
  205. {
  206. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  207. struct sugov_policy *sg_policy;
  208. unsigned int rate_limit_us;
  209. if (kstrtouint(buf, 10, &rate_limit_us))
  210. return -EINVAL;
  211. tunables->rate_limit_us = rate_limit_us;
  212. list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
  213. sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
  214. return count;
  215. }
  216. static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
  217. static struct attribute *sugov_attributes[] = {
  218. &rate_limit_us.attr,
  219. NULL
  220. };
  221. static struct kobj_type sugov_tunables_ktype = {
  222. .default_attrs = sugov_attributes,
  223. .sysfs_ops = &governor_sysfs_ops,
  224. };
  225. /********************** cpufreq governor interface *********************/
  226. static struct cpufreq_governor schedutil_gov;
  227. static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
  228. {
  229. struct sugov_policy *sg_policy;
  230. sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
  231. if (!sg_policy)
  232. return NULL;
  233. sg_policy->policy = policy;
  234. init_irq_work(&sg_policy->irq_work, sugov_irq_work);
  235. INIT_WORK(&sg_policy->work, sugov_work);
  236. mutex_init(&sg_policy->work_lock);
  237. raw_spin_lock_init(&sg_policy->update_lock);
  238. return sg_policy;
  239. }
  240. static void sugov_policy_free(struct sugov_policy *sg_policy)
  241. {
  242. mutex_destroy(&sg_policy->work_lock);
  243. kfree(sg_policy);
  244. }
  245. static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
  246. {
  247. struct sugov_tunables *tunables;
  248. tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
  249. if (tunables) {
  250. gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
  251. if (!have_governor_per_policy())
  252. global_tunables = tunables;
  253. }
  254. return tunables;
  255. }
  256. static void sugov_tunables_free(struct sugov_tunables *tunables)
  257. {
  258. if (!have_governor_per_policy())
  259. global_tunables = NULL;
  260. kfree(tunables);
  261. }
  262. static int sugov_init(struct cpufreq_policy *policy)
  263. {
  264. struct sugov_policy *sg_policy;
  265. struct sugov_tunables *tunables;
  266. unsigned int lat;
  267. int ret = 0;
  268. /* State should be equivalent to EXIT */
  269. if (policy->governor_data)
  270. return -EBUSY;
  271. sg_policy = sugov_policy_alloc(policy);
  272. if (!sg_policy)
  273. return -ENOMEM;
  274. mutex_lock(&global_tunables_lock);
  275. if (global_tunables) {
  276. if (WARN_ON(have_governor_per_policy())) {
  277. ret = -EINVAL;
  278. goto free_sg_policy;
  279. }
  280. policy->governor_data = sg_policy;
  281. sg_policy->tunables = global_tunables;
  282. gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
  283. goto out;
  284. }
  285. tunables = sugov_tunables_alloc(sg_policy);
  286. if (!tunables) {
  287. ret = -ENOMEM;
  288. goto free_sg_policy;
  289. }
  290. tunables->rate_limit_us = LATENCY_MULTIPLIER;
  291. lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
  292. if (lat)
  293. tunables->rate_limit_us *= lat;
  294. policy->governor_data = sg_policy;
  295. sg_policy->tunables = tunables;
  296. ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
  297. get_governor_parent_kobj(policy), "%s",
  298. schedutil_gov.name);
  299. if (ret)
  300. goto fail;
  301. out:
  302. mutex_unlock(&global_tunables_lock);
  303. cpufreq_enable_fast_switch(policy);
  304. return 0;
  305. fail:
  306. policy->governor_data = NULL;
  307. sugov_tunables_free(tunables);
  308. free_sg_policy:
  309. mutex_unlock(&global_tunables_lock);
  310. sugov_policy_free(sg_policy);
  311. pr_err("initialization failed (error %d)\n", ret);
  312. return ret;
  313. }
  314. static void sugov_exit(struct cpufreq_policy *policy)
  315. {
  316. struct sugov_policy *sg_policy = policy->governor_data;
  317. struct sugov_tunables *tunables = sg_policy->tunables;
  318. unsigned int count;
  319. cpufreq_disable_fast_switch(policy);
  320. mutex_lock(&global_tunables_lock);
  321. count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
  322. policy->governor_data = NULL;
  323. if (!count)
  324. sugov_tunables_free(tunables);
  325. mutex_unlock(&global_tunables_lock);
  326. sugov_policy_free(sg_policy);
  327. }
  328. static int sugov_start(struct cpufreq_policy *policy)
  329. {
  330. struct sugov_policy *sg_policy = policy->governor_data;
  331. unsigned int cpu;
  332. sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
  333. sg_policy->last_freq_update_time = 0;
  334. sg_policy->next_freq = UINT_MAX;
  335. sg_policy->work_in_progress = false;
  336. sg_policy->need_freq_update = false;
  337. for_each_cpu(cpu, policy->cpus) {
  338. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  339. sg_cpu->sg_policy = sg_policy;
  340. if (policy_is_shared(policy)) {
  341. sg_cpu->util = ULONG_MAX;
  342. sg_cpu->max = 0;
  343. sg_cpu->last_update = 0;
  344. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  345. sugov_update_shared);
  346. } else {
  347. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  348. sugov_update_single);
  349. }
  350. }
  351. return 0;
  352. }
  353. static void sugov_stop(struct cpufreq_policy *policy)
  354. {
  355. struct sugov_policy *sg_policy = policy->governor_data;
  356. unsigned int cpu;
  357. for_each_cpu(cpu, policy->cpus)
  358. cpufreq_remove_update_util_hook(cpu);
  359. synchronize_sched();
  360. irq_work_sync(&sg_policy->irq_work);
  361. cancel_work_sync(&sg_policy->work);
  362. }
  363. static void sugov_limits(struct cpufreq_policy *policy)
  364. {
  365. struct sugov_policy *sg_policy = policy->governor_data;
  366. if (!policy->fast_switch_enabled) {
  367. mutex_lock(&sg_policy->work_lock);
  368. if (policy->max < policy->cur)
  369. __cpufreq_driver_target(policy, policy->max,
  370. CPUFREQ_RELATION_H);
  371. else if (policy->min > policy->cur)
  372. __cpufreq_driver_target(policy, policy->min,
  373. CPUFREQ_RELATION_L);
  374. mutex_unlock(&sg_policy->work_lock);
  375. }
  376. sg_policy->need_freq_update = true;
  377. }
  378. static struct cpufreq_governor schedutil_gov = {
  379. .name = "schedutil",
  380. .owner = THIS_MODULE,
  381. .init = sugov_init,
  382. .exit = sugov_exit,
  383. .start = sugov_start,
  384. .stop = sugov_stop,
  385. .limits = sugov_limits,
  386. };
  387. static int __init sugov_module_init(void)
  388. {
  389. return cpufreq_register_governor(&schedutil_gov);
  390. }
  391. static void __exit sugov_module_exit(void)
  392. {
  393. cpufreq_unregister_governor(&schedutil_gov);
  394. }
  395. MODULE_AUTHOR("Rafael J. Wysocki <rafael.j.wysocki@intel.com>");
  396. MODULE_DESCRIPTION("Utilization-based CPU frequency selection");
  397. MODULE_LICENSE("GPL");
  398. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
  399. struct cpufreq_governor *cpufreq_default_governor(void)
  400. {
  401. return &schedutil_gov;
  402. }
  403. fs_initcall(sugov_module_init);
  404. #else
  405. module_init(sugov_module_init);
  406. #endif
  407. module_exit(sugov_module_exit);