cpufreq_schedutil.c 13 KB

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