cpufreq_schedutil.c 13 KB

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