cpufreq_governor.c 16 KB

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