cpufreq_governor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * drivers/cpufreq/cpufreq_governor.c
  3. *
  4. * CPUFREQ governors common code
  5. *
  6. * Copyright (C) 2001 Russell King
  7. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  8. * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
  9. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  10. * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/export.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include "cpufreq_governor.h"
  22. static DEFINE_PER_CPU(struct cpu_dbs_info, cpu_dbs);
  23. static DEFINE_MUTEX(gov_dbs_data_mutex);
  24. /* Common sysfs tunables */
  25. /**
  26. * store_sampling_rate - update sampling rate effective immediately if needed.
  27. *
  28. * If new rate is smaller than the old, simply updating
  29. * dbs.sampling_rate might not be appropriate. For example, if the
  30. * original sampling_rate was 1 second and the requested new sampling rate is 10
  31. * ms because the user needs immediate reaction from ondemand governor, but not
  32. * sure if higher frequency will be required or not, then, the governor may
  33. * change the sampling rate too late; up to 1 second later. Thus, if we are
  34. * reducing the sampling rate, we need to make the new value effective
  35. * immediately.
  36. *
  37. * This must be called with dbs_data->mutex held, otherwise traversing
  38. * policy_dbs_list isn't safe.
  39. */
  40. ssize_t store_sampling_rate(struct gov_attr_set *attr_set, const char *buf,
  41. size_t count)
  42. {
  43. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  44. struct policy_dbs_info *policy_dbs;
  45. unsigned int rate;
  46. int ret;
  47. ret = sscanf(buf, "%u", &rate);
  48. if (ret != 1)
  49. return -EINVAL;
  50. dbs_data->sampling_rate = max(rate, dbs_data->min_sampling_rate);
  51. /*
  52. * We are operating under dbs_data->mutex and so the list and its
  53. * entries can't be freed concurrently.
  54. */
  55. list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
  56. mutex_lock(&policy_dbs->timer_mutex);
  57. /*
  58. * On 32-bit architectures this may race with the
  59. * sample_delay_ns read in dbs_update_util_handler(), but that
  60. * really doesn't matter. If the read returns a value that's
  61. * too big, the sample will be skipped, but the next invocation
  62. * of dbs_update_util_handler() (when the update has been
  63. * completed) will take a sample.
  64. *
  65. * If this runs in parallel with dbs_work_handler(), we may end
  66. * up overwriting the sample_delay_ns value that it has just
  67. * written, but it will be corrected next time a sample is
  68. * taken, so it shouldn't be significant.
  69. */
  70. gov_update_sample_delay(policy_dbs, 0);
  71. mutex_unlock(&policy_dbs->timer_mutex);
  72. }
  73. return count;
  74. }
  75. EXPORT_SYMBOL_GPL(store_sampling_rate);
  76. /**
  77. * gov_update_cpu_data - Update CPU load data.
  78. * @dbs_data: Top-level governor data pointer.
  79. *
  80. * Update CPU load data for all CPUs in the domain governed by @dbs_data
  81. * (that may be a single policy or a bunch of them if governor tunables are
  82. * system-wide).
  83. *
  84. * Call under the @dbs_data mutex.
  85. */
  86. void gov_update_cpu_data(struct dbs_data *dbs_data)
  87. {
  88. struct policy_dbs_info *policy_dbs;
  89. list_for_each_entry(policy_dbs, &dbs_data->attr_set.policy_list, list) {
  90. unsigned int j;
  91. for_each_cpu(j, policy_dbs->policy->cpus) {
  92. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  93. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time,
  94. dbs_data->io_is_busy);
  95. if (dbs_data->ignore_nice_load)
  96. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  97. }
  98. }
  99. }
  100. EXPORT_SYMBOL_GPL(gov_update_cpu_data);
  101. unsigned int dbs_update(struct cpufreq_policy *policy)
  102. {
  103. struct policy_dbs_info *policy_dbs = policy->governor_data;
  104. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  105. unsigned int ignore_nice = dbs_data->ignore_nice_load;
  106. unsigned int max_load = 0;
  107. unsigned int sampling_rate, io_busy, j;
  108. /*
  109. * Sometimes governors may use an additional multiplier to increase
  110. * sample delays temporarily. Apply that multiplier to sampling_rate
  111. * so as to keep the wake-up-from-idle detection logic a bit
  112. * conservative.
  113. */
  114. sampling_rate = dbs_data->sampling_rate * policy_dbs->rate_mult;
  115. /*
  116. * For the purpose of ondemand, waiting for disk IO is an indication
  117. * that you're performance critical, and not that the system is actually
  118. * idle, so do not add the iowait time to the CPU idle time then.
  119. */
  120. io_busy = dbs_data->io_is_busy;
  121. /* Get Absolute Load */
  122. for_each_cpu(j, policy->cpus) {
  123. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  124. u64 update_time, cur_idle_time;
  125. unsigned int idle_time, time_elapsed;
  126. unsigned int load;
  127. cur_idle_time = get_cpu_idle_time(j, &update_time, io_busy);
  128. time_elapsed = update_time - j_cdbs->prev_update_time;
  129. j_cdbs->prev_update_time = update_time;
  130. idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;
  131. j_cdbs->prev_cpu_idle = cur_idle_time;
  132. if (ignore_nice) {
  133. u64 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  134. idle_time += cputime_to_usecs(cur_nice - j_cdbs->prev_cpu_nice);
  135. j_cdbs->prev_cpu_nice = cur_nice;
  136. }
  137. if (unlikely(!time_elapsed)) {
  138. /*
  139. * That can only happen when this function is called
  140. * twice in a row with a very short interval between the
  141. * calls, so the previous load value can be used then.
  142. */
  143. load = j_cdbs->prev_load;
  144. } else if (unlikely(time_elapsed > 2 * sampling_rate &&
  145. j_cdbs->prev_load)) {
  146. /*
  147. * If the CPU had gone completely idle and a task has
  148. * just woken up on this CPU now, it would be unfair to
  149. * calculate 'load' the usual way for this elapsed
  150. * time-window, because it would show near-zero load,
  151. * irrespective of how CPU intensive that task actually
  152. * was. This is undesirable for latency-sensitive bursty
  153. * workloads.
  154. *
  155. * To avoid this, reuse the 'load' from the previous
  156. * time-window and give this task a chance to start with
  157. * a reasonably high CPU frequency. However, that
  158. * shouldn't be over-done, lest we get stuck at a high
  159. * load (high frequency) for too long, even when the
  160. * current system load has actually dropped down, so
  161. * clear prev_load to guarantee that the load will be
  162. * computed again next time.
  163. *
  164. * Detecting this situation is easy: the governor's
  165. * utilization update handler would not have run during
  166. * CPU-idle periods. Hence, an unusually large
  167. * 'time_elapsed' (as compared to the sampling rate)
  168. * indicates this scenario.
  169. */
  170. load = j_cdbs->prev_load;
  171. j_cdbs->prev_load = 0;
  172. } else {
  173. if (time_elapsed >= idle_time) {
  174. load = 100 * (time_elapsed - idle_time) / time_elapsed;
  175. } else {
  176. /*
  177. * That can happen if idle_time is returned by
  178. * get_cpu_idle_time_jiffy(). In that case
  179. * idle_time is roughly equal to the difference
  180. * between time_elapsed and "busy time" obtained
  181. * from CPU statistics. Then, the "busy time"
  182. * can end up being greater than time_elapsed
  183. * (for example, if jiffies_64 and the CPU
  184. * statistics are updated by different CPUs),
  185. * so idle_time may in fact be negative. That
  186. * means, though, that the CPU was busy all
  187. * the time (on the rough average) during the
  188. * last sampling interval and 100 can be
  189. * returned as the load.
  190. */
  191. load = (int)idle_time < 0 ? 100 : 0;
  192. }
  193. j_cdbs->prev_load = load;
  194. }
  195. if (load > max_load)
  196. max_load = load;
  197. }
  198. return max_load;
  199. }
  200. EXPORT_SYMBOL_GPL(dbs_update);
  201. static void dbs_work_handler(struct work_struct *work)
  202. {
  203. struct policy_dbs_info *policy_dbs;
  204. struct cpufreq_policy *policy;
  205. struct dbs_governor *gov;
  206. policy_dbs = container_of(work, struct policy_dbs_info, work);
  207. policy = policy_dbs->policy;
  208. gov = dbs_governor_of(policy);
  209. /*
  210. * Make sure cpufreq_governor_limits() isn't evaluating load or the
  211. * ondemand governor isn't updating the sampling rate in parallel.
  212. */
  213. mutex_lock(&policy_dbs->timer_mutex);
  214. gov_update_sample_delay(policy_dbs, gov->gov_dbs_timer(policy));
  215. mutex_unlock(&policy_dbs->timer_mutex);
  216. /* Allow the utilization update handler to queue up more work. */
  217. atomic_set(&policy_dbs->work_count, 0);
  218. /*
  219. * If the update below is reordered with respect to the sample delay
  220. * modification, the utilization update handler may end up using a stale
  221. * sample delay value.
  222. */
  223. smp_wmb();
  224. policy_dbs->work_in_progress = false;
  225. }
  226. static void dbs_irq_work(struct irq_work *irq_work)
  227. {
  228. struct policy_dbs_info *policy_dbs;
  229. policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
  230. schedule_work_on(smp_processor_id(), &policy_dbs->work);
  231. }
  232. static void dbs_update_util_handler(struct update_util_data *data, u64 time,
  233. unsigned int flags)
  234. {
  235. struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
  236. struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
  237. u64 delta_ns, lst;
  238. /*
  239. * The work may not be allowed to be queued up right now.
  240. * Possible reasons:
  241. * - Work has already been queued up or is in progress.
  242. * - It is too early (too little time from the previous sample).
  243. */
  244. if (policy_dbs->work_in_progress)
  245. return;
  246. /*
  247. * If the reads below are reordered before the check above, the value
  248. * of sample_delay_ns used in the computation may be stale.
  249. */
  250. smp_rmb();
  251. lst = READ_ONCE(policy_dbs->last_sample_time);
  252. delta_ns = time - lst;
  253. if ((s64)delta_ns < policy_dbs->sample_delay_ns)
  254. return;
  255. /*
  256. * If the policy is not shared, the irq_work may be queued up right away
  257. * at this point. Otherwise, we need to ensure that only one of the
  258. * CPUs sharing the policy will do that.
  259. */
  260. if (policy_dbs->is_shared) {
  261. if (!atomic_add_unless(&policy_dbs->work_count, 1, 1))
  262. return;
  263. /*
  264. * If another CPU updated last_sample_time in the meantime, we
  265. * shouldn't be here, so clear the work counter and bail out.
  266. */
  267. if (unlikely(lst != READ_ONCE(policy_dbs->last_sample_time))) {
  268. atomic_set(&policy_dbs->work_count, 0);
  269. return;
  270. }
  271. }
  272. policy_dbs->last_sample_time = time;
  273. policy_dbs->work_in_progress = true;
  274. irq_work_queue(&policy_dbs->irq_work);
  275. }
  276. static void gov_set_update_util(struct policy_dbs_info *policy_dbs,
  277. unsigned int delay_us)
  278. {
  279. struct cpufreq_policy *policy = policy_dbs->policy;
  280. int cpu;
  281. gov_update_sample_delay(policy_dbs, delay_us);
  282. policy_dbs->last_sample_time = 0;
  283. for_each_cpu(cpu, policy->cpus) {
  284. struct cpu_dbs_info *cdbs = &per_cpu(cpu_dbs, cpu);
  285. cpufreq_add_update_util_hook(cpu, &cdbs->update_util,
  286. dbs_update_util_handler);
  287. }
  288. }
  289. static inline void gov_clear_update_util(struct cpufreq_policy *policy)
  290. {
  291. int i;
  292. for_each_cpu(i, policy->cpus)
  293. cpufreq_remove_update_util_hook(i);
  294. synchronize_sched();
  295. }
  296. static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
  297. struct dbs_governor *gov)
  298. {
  299. struct policy_dbs_info *policy_dbs;
  300. int j;
  301. /* Allocate memory for per-policy governor data. */
  302. policy_dbs = gov->alloc();
  303. if (!policy_dbs)
  304. return NULL;
  305. policy_dbs->policy = policy;
  306. mutex_init(&policy_dbs->timer_mutex);
  307. atomic_set(&policy_dbs->work_count, 0);
  308. init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
  309. INIT_WORK(&policy_dbs->work, dbs_work_handler);
  310. /* Set policy_dbs for all CPUs, online+offline */
  311. for_each_cpu(j, policy->related_cpus) {
  312. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  313. j_cdbs->policy_dbs = policy_dbs;
  314. }
  315. return policy_dbs;
  316. }
  317. static void free_policy_dbs_info(struct policy_dbs_info *policy_dbs,
  318. struct dbs_governor *gov)
  319. {
  320. int j;
  321. mutex_destroy(&policy_dbs->timer_mutex);
  322. for_each_cpu(j, policy_dbs->policy->related_cpus) {
  323. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  324. j_cdbs->policy_dbs = NULL;
  325. j_cdbs->update_util.func = NULL;
  326. }
  327. gov->free(policy_dbs);
  328. }
  329. int cpufreq_dbs_governor_init(struct cpufreq_policy *policy)
  330. {
  331. struct dbs_governor *gov = dbs_governor_of(policy);
  332. struct dbs_data *dbs_data;
  333. struct policy_dbs_info *policy_dbs;
  334. unsigned int latency;
  335. int ret = 0;
  336. /* State should be equivalent to EXIT */
  337. if (policy->governor_data)
  338. return -EBUSY;
  339. policy_dbs = alloc_policy_dbs_info(policy, gov);
  340. if (!policy_dbs)
  341. return -ENOMEM;
  342. /* Protect gov->gdbs_data against concurrent updates. */
  343. mutex_lock(&gov_dbs_data_mutex);
  344. dbs_data = gov->gdbs_data;
  345. if (dbs_data) {
  346. if (WARN_ON(have_governor_per_policy())) {
  347. ret = -EINVAL;
  348. goto free_policy_dbs_info;
  349. }
  350. policy_dbs->dbs_data = dbs_data;
  351. policy->governor_data = policy_dbs;
  352. gov_attr_set_get(&dbs_data->attr_set, &policy_dbs->list);
  353. goto out;
  354. }
  355. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  356. if (!dbs_data) {
  357. ret = -ENOMEM;
  358. goto free_policy_dbs_info;
  359. }
  360. gov_attr_set_init(&dbs_data->attr_set, &policy_dbs->list);
  361. ret = gov->init(dbs_data);
  362. if (ret)
  363. goto free_policy_dbs_info;
  364. /* policy latency is in ns. Convert it to us first */
  365. latency = policy->cpuinfo.transition_latency / 1000;
  366. if (latency == 0)
  367. latency = 1;
  368. /* Bring kernel and HW constraints together */
  369. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  370. MIN_LATENCY_MULTIPLIER * latency);
  371. dbs_data->sampling_rate = max(dbs_data->min_sampling_rate,
  372. LATENCY_MULTIPLIER * latency);
  373. if (!have_governor_per_policy())
  374. gov->gdbs_data = dbs_data;
  375. policy_dbs->dbs_data = dbs_data;
  376. policy->governor_data = policy_dbs;
  377. gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
  378. ret = kobject_init_and_add(&dbs_data->attr_set.kobj, &gov->kobj_type,
  379. get_governor_parent_kobj(policy),
  380. "%s", gov->gov.name);
  381. if (!ret)
  382. goto out;
  383. /* Failure, so roll back. */
  384. pr_err("initialization failed (dbs_data kobject init error %d)\n", ret);
  385. policy->governor_data = NULL;
  386. if (!have_governor_per_policy())
  387. gov->gdbs_data = NULL;
  388. gov->exit(dbs_data);
  389. kfree(dbs_data);
  390. free_policy_dbs_info:
  391. free_policy_dbs_info(policy_dbs, gov);
  392. out:
  393. mutex_unlock(&gov_dbs_data_mutex);
  394. return ret;
  395. }
  396. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_init);
  397. void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy)
  398. {
  399. struct dbs_governor *gov = dbs_governor_of(policy);
  400. struct policy_dbs_info *policy_dbs = policy->governor_data;
  401. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  402. unsigned int count;
  403. /* Protect gov->gdbs_data against concurrent updates. */
  404. mutex_lock(&gov_dbs_data_mutex);
  405. count = gov_attr_set_put(&dbs_data->attr_set, &policy_dbs->list);
  406. policy->governor_data = NULL;
  407. if (!count) {
  408. if (!have_governor_per_policy())
  409. gov->gdbs_data = NULL;
  410. gov->exit(dbs_data);
  411. kfree(dbs_data);
  412. }
  413. free_policy_dbs_info(policy_dbs, gov);
  414. mutex_unlock(&gov_dbs_data_mutex);
  415. }
  416. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_exit);
  417. int cpufreq_dbs_governor_start(struct cpufreq_policy *policy)
  418. {
  419. struct dbs_governor *gov = dbs_governor_of(policy);
  420. struct policy_dbs_info *policy_dbs = policy->governor_data;
  421. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  422. unsigned int sampling_rate, ignore_nice, j;
  423. unsigned int io_busy;
  424. if (!policy->cur)
  425. return -EINVAL;
  426. policy_dbs->is_shared = policy_is_shared(policy);
  427. policy_dbs->rate_mult = 1;
  428. sampling_rate = dbs_data->sampling_rate;
  429. ignore_nice = dbs_data->ignore_nice_load;
  430. io_busy = dbs_data->io_is_busy;
  431. for_each_cpu(j, policy->cpus) {
  432. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  433. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time, io_busy);
  434. /*
  435. * Make the first invocation of dbs_update() compute the load.
  436. */
  437. j_cdbs->prev_load = 0;
  438. if (ignore_nice)
  439. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  440. }
  441. gov->start(policy);
  442. gov_set_update_util(policy_dbs, sampling_rate);
  443. return 0;
  444. }
  445. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_start);
  446. void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy)
  447. {
  448. struct policy_dbs_info *policy_dbs = policy->governor_data;
  449. gov_clear_update_util(policy_dbs->policy);
  450. irq_work_sync(&policy_dbs->irq_work);
  451. cancel_work_sync(&policy_dbs->work);
  452. atomic_set(&policy_dbs->work_count, 0);
  453. policy_dbs->work_in_progress = false;
  454. }
  455. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_stop);
  456. void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy)
  457. {
  458. struct policy_dbs_info *policy_dbs = policy->governor_data;
  459. mutex_lock(&policy_dbs->timer_mutex);
  460. cpufreq_policy_apply_limits(policy);
  461. gov_update_sample_delay(policy_dbs, 0);
  462. mutex_unlock(&policy_dbs->timer_mutex);
  463. }
  464. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_limits);