cpufreq_schedutil.c 15 KB

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