cpufreq_governor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. #define CPUFREQ_DBS_MIN_SAMPLING_INTERVAL (2 * TICK_NSEC / NSEC_PER_USEC)
  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 sampling_interval;
  46. int ret;
  47. ret = sscanf(buf, "%u", &sampling_interval);
  48. if (ret != 1 || sampling_interval < CPUFREQ_DBS_MIN_SAMPLING_INTERVAL)
  49. return -EINVAL;
  50. dbs_data->sampling_rate = sampling_interval;
  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->update_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->update_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, idle_periods = UINT_MAX;
  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 += div_u64(cur_nice - j_cdbs->prev_cpu_nice, NSEC_PER_USEC);
  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((int)idle_time > 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: an unusually large
  165. * 'idle_time' (as compared to the sampling rate)
  166. * indicates this scenario.
  167. */
  168. load = j_cdbs->prev_load;
  169. j_cdbs->prev_load = 0;
  170. } else {
  171. if (time_elapsed >= idle_time) {
  172. load = 100 * (time_elapsed - idle_time) / time_elapsed;
  173. } else {
  174. /*
  175. * That can happen if idle_time is returned by
  176. * get_cpu_idle_time_jiffy(). In that case
  177. * idle_time is roughly equal to the difference
  178. * between time_elapsed and "busy time" obtained
  179. * from CPU statistics. Then, the "busy time"
  180. * can end up being greater than time_elapsed
  181. * (for example, if jiffies_64 and the CPU
  182. * statistics are updated by different CPUs),
  183. * so idle_time may in fact be negative. That
  184. * means, though, that the CPU was busy all
  185. * the time (on the rough average) during the
  186. * last sampling interval and 100 can be
  187. * returned as the load.
  188. */
  189. load = (int)idle_time < 0 ? 100 : 0;
  190. }
  191. j_cdbs->prev_load = load;
  192. }
  193. if (unlikely((int)idle_time > 2 * sampling_rate)) {
  194. unsigned int periods = idle_time / sampling_rate;
  195. if (periods < idle_periods)
  196. idle_periods = periods;
  197. }
  198. if (load > max_load)
  199. max_load = load;
  200. }
  201. policy_dbs->idle_periods = idle_periods;
  202. return max_load;
  203. }
  204. EXPORT_SYMBOL_GPL(dbs_update);
  205. static void dbs_work_handler(struct work_struct *work)
  206. {
  207. struct policy_dbs_info *policy_dbs;
  208. struct cpufreq_policy *policy;
  209. struct dbs_governor *gov;
  210. policy_dbs = container_of(work, struct policy_dbs_info, work);
  211. policy = policy_dbs->policy;
  212. gov = dbs_governor_of(policy);
  213. /*
  214. * Make sure cpufreq_governor_limits() isn't evaluating load or the
  215. * ondemand governor isn't updating the sampling rate in parallel.
  216. */
  217. mutex_lock(&policy_dbs->update_mutex);
  218. gov_update_sample_delay(policy_dbs, gov->gov_dbs_update(policy));
  219. mutex_unlock(&policy_dbs->update_mutex);
  220. /* Allow the utilization update handler to queue up more work. */
  221. atomic_set(&policy_dbs->work_count, 0);
  222. /*
  223. * If the update below is reordered with respect to the sample delay
  224. * modification, the utilization update handler may end up using a stale
  225. * sample delay value.
  226. */
  227. smp_wmb();
  228. policy_dbs->work_in_progress = false;
  229. }
  230. static void dbs_irq_work(struct irq_work *irq_work)
  231. {
  232. struct policy_dbs_info *policy_dbs;
  233. policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
  234. schedule_work_on(smp_processor_id(), &policy_dbs->work);
  235. }
  236. static void dbs_update_util_handler(struct update_util_data *data, u64 time,
  237. unsigned int flags)
  238. {
  239. struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
  240. struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
  241. u64 delta_ns, lst;
  242. if (!cpufreq_can_do_remote_dvfs(policy_dbs->policy))
  243. return;
  244. /*
  245. * The work may not be allowed to be queued up right now.
  246. * Possible reasons:
  247. * - Work has already been queued up or is in progress.
  248. * - It is too early (too little time from the previous sample).
  249. */
  250. if (policy_dbs->work_in_progress)
  251. return;
  252. /*
  253. * If the reads below are reordered before the check above, the value
  254. * of sample_delay_ns used in the computation may be stale.
  255. */
  256. smp_rmb();
  257. lst = READ_ONCE(policy_dbs->last_sample_time);
  258. delta_ns = time - lst;
  259. if ((s64)delta_ns < policy_dbs->sample_delay_ns)
  260. return;
  261. /*
  262. * If the policy is not shared, the irq_work may be queued up right away
  263. * at this point. Otherwise, we need to ensure that only one of the
  264. * CPUs sharing the policy will do that.
  265. */
  266. if (policy_dbs->is_shared) {
  267. if (!atomic_add_unless(&policy_dbs->work_count, 1, 1))
  268. return;
  269. /*
  270. * If another CPU updated last_sample_time in the meantime, we
  271. * shouldn't be here, so clear the work counter and bail out.
  272. */
  273. if (unlikely(lst != READ_ONCE(policy_dbs->last_sample_time))) {
  274. atomic_set(&policy_dbs->work_count, 0);
  275. return;
  276. }
  277. }
  278. policy_dbs->last_sample_time = time;
  279. policy_dbs->work_in_progress = true;
  280. irq_work_queue(&policy_dbs->irq_work);
  281. }
  282. static void gov_set_update_util(struct policy_dbs_info *policy_dbs,
  283. unsigned int delay_us)
  284. {
  285. struct cpufreq_policy *policy = policy_dbs->policy;
  286. int cpu;
  287. gov_update_sample_delay(policy_dbs, delay_us);
  288. policy_dbs->last_sample_time = 0;
  289. for_each_cpu(cpu, policy->cpus) {
  290. struct cpu_dbs_info *cdbs = &per_cpu(cpu_dbs, cpu);
  291. cpufreq_add_update_util_hook(cpu, &cdbs->update_util,
  292. dbs_update_util_handler);
  293. }
  294. }
  295. static inline void gov_clear_update_util(struct cpufreq_policy *policy)
  296. {
  297. int i;
  298. for_each_cpu(i, policy->cpus)
  299. cpufreq_remove_update_util_hook(i);
  300. synchronize_sched();
  301. }
  302. static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
  303. struct dbs_governor *gov)
  304. {
  305. struct policy_dbs_info *policy_dbs;
  306. int j;
  307. /* Allocate memory for per-policy governor data. */
  308. policy_dbs = gov->alloc();
  309. if (!policy_dbs)
  310. return NULL;
  311. policy_dbs->policy = policy;
  312. mutex_init(&policy_dbs->update_mutex);
  313. atomic_set(&policy_dbs->work_count, 0);
  314. init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
  315. INIT_WORK(&policy_dbs->work, dbs_work_handler);
  316. /* Set policy_dbs for all CPUs, online+offline */
  317. for_each_cpu(j, policy->related_cpus) {
  318. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  319. j_cdbs->policy_dbs = policy_dbs;
  320. }
  321. return policy_dbs;
  322. }
  323. static void free_policy_dbs_info(struct policy_dbs_info *policy_dbs,
  324. struct dbs_governor *gov)
  325. {
  326. int j;
  327. mutex_destroy(&policy_dbs->update_mutex);
  328. for_each_cpu(j, policy_dbs->policy->related_cpus) {
  329. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  330. j_cdbs->policy_dbs = NULL;
  331. j_cdbs->update_util.func = NULL;
  332. }
  333. gov->free(policy_dbs);
  334. }
  335. int cpufreq_dbs_governor_init(struct cpufreq_policy *policy)
  336. {
  337. struct dbs_governor *gov = dbs_governor_of(policy);
  338. struct dbs_data *dbs_data;
  339. struct policy_dbs_info *policy_dbs;
  340. int ret = 0;
  341. /* State should be equivalent to EXIT */
  342. if (policy->governor_data)
  343. return -EBUSY;
  344. policy_dbs = alloc_policy_dbs_info(policy, gov);
  345. if (!policy_dbs)
  346. return -ENOMEM;
  347. /* Protect gov->gdbs_data against concurrent updates. */
  348. mutex_lock(&gov_dbs_data_mutex);
  349. dbs_data = gov->gdbs_data;
  350. if (dbs_data) {
  351. if (WARN_ON(have_governor_per_policy())) {
  352. ret = -EINVAL;
  353. goto free_policy_dbs_info;
  354. }
  355. policy_dbs->dbs_data = dbs_data;
  356. policy->governor_data = policy_dbs;
  357. gov_attr_set_get(&dbs_data->attr_set, &policy_dbs->list);
  358. goto out;
  359. }
  360. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  361. if (!dbs_data) {
  362. ret = -ENOMEM;
  363. goto free_policy_dbs_info;
  364. }
  365. gov_attr_set_init(&dbs_data->attr_set, &policy_dbs->list);
  366. ret = gov->init(dbs_data);
  367. if (ret)
  368. goto free_policy_dbs_info;
  369. /*
  370. * The sampling interval should not be less than the transition latency
  371. * of the CPU and it also cannot be too small for dbs_update() to work
  372. * correctly.
  373. */
  374. dbs_data->sampling_rate = max_t(unsigned int,
  375. CPUFREQ_DBS_MIN_SAMPLING_INTERVAL,
  376. cpufreq_policy_transition_delay_us(policy));
  377. if (!have_governor_per_policy())
  378. gov->gdbs_data = dbs_data;
  379. policy_dbs->dbs_data = dbs_data;
  380. policy->governor_data = policy_dbs;
  381. gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
  382. ret = kobject_init_and_add(&dbs_data->attr_set.kobj, &gov->kobj_type,
  383. get_governor_parent_kobj(policy),
  384. "%s", gov->gov.name);
  385. if (!ret)
  386. goto out;
  387. /* Failure, so roll back. */
  388. pr_err("initialization failed (dbs_data kobject init error %d)\n", ret);
  389. kobject_put(&dbs_data->attr_set.kobj);
  390. policy->governor_data = NULL;
  391. if (!have_governor_per_policy())
  392. gov->gdbs_data = NULL;
  393. gov->exit(dbs_data);
  394. kfree(dbs_data);
  395. free_policy_dbs_info:
  396. free_policy_dbs_info(policy_dbs, gov);
  397. out:
  398. mutex_unlock(&gov_dbs_data_mutex);
  399. return ret;
  400. }
  401. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_init);
  402. void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy)
  403. {
  404. struct dbs_governor *gov = dbs_governor_of(policy);
  405. struct policy_dbs_info *policy_dbs = policy->governor_data;
  406. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  407. unsigned int count;
  408. /* Protect gov->gdbs_data against concurrent updates. */
  409. mutex_lock(&gov_dbs_data_mutex);
  410. count = gov_attr_set_put(&dbs_data->attr_set, &policy_dbs->list);
  411. policy->governor_data = NULL;
  412. if (!count) {
  413. if (!have_governor_per_policy())
  414. gov->gdbs_data = NULL;
  415. gov->exit(dbs_data);
  416. kfree(dbs_data);
  417. }
  418. free_policy_dbs_info(policy_dbs, gov);
  419. mutex_unlock(&gov_dbs_data_mutex);
  420. }
  421. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_exit);
  422. int cpufreq_dbs_governor_start(struct cpufreq_policy *policy)
  423. {
  424. struct dbs_governor *gov = dbs_governor_of(policy);
  425. struct policy_dbs_info *policy_dbs = policy->governor_data;
  426. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  427. unsigned int sampling_rate, ignore_nice, j;
  428. unsigned int io_busy;
  429. if (!policy->cur)
  430. return -EINVAL;
  431. policy_dbs->is_shared = policy_is_shared(policy);
  432. policy_dbs->rate_mult = 1;
  433. sampling_rate = dbs_data->sampling_rate;
  434. ignore_nice = dbs_data->ignore_nice_load;
  435. io_busy = dbs_data->io_is_busy;
  436. for_each_cpu(j, policy->cpus) {
  437. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  438. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time, io_busy);
  439. /*
  440. * Make the first invocation of dbs_update() compute the load.
  441. */
  442. j_cdbs->prev_load = 0;
  443. if (ignore_nice)
  444. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  445. }
  446. gov->start(policy);
  447. gov_set_update_util(policy_dbs, sampling_rate);
  448. return 0;
  449. }
  450. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_start);
  451. void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy)
  452. {
  453. struct policy_dbs_info *policy_dbs = policy->governor_data;
  454. gov_clear_update_util(policy_dbs->policy);
  455. irq_work_sync(&policy_dbs->irq_work);
  456. cancel_work_sync(&policy_dbs->work);
  457. atomic_set(&policy_dbs->work_count, 0);
  458. policy_dbs->work_in_progress = false;
  459. }
  460. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_stop);
  461. void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy)
  462. {
  463. struct policy_dbs_info *policy_dbs;
  464. /* Protect gov->gdbs_data against cpufreq_dbs_governor_exit() */
  465. mutex_lock(&gov_dbs_data_mutex);
  466. policy_dbs = policy->governor_data;
  467. if (!policy_dbs)
  468. goto out;
  469. mutex_lock(&policy_dbs->update_mutex);
  470. cpufreq_policy_apply_limits(policy);
  471. gov_update_sample_delay(policy_dbs, 0);
  472. mutex_unlock(&policy_dbs->update_mutex);
  473. out:
  474. mutex_unlock(&gov_dbs_data_mutex);
  475. }
  476. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_limits);