cpufreq_governor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. unsigned int rate;
  45. int ret;
  46. ret = sscanf(buf, "%u", &rate);
  47. if (ret != 1)
  48. return -EINVAL;
  49. dbs_data->sampling_rate = max(rate, dbs_data->min_sampling_rate);
  50. /*
  51. * We are operating under dbs_data->mutex and so the list and its
  52. * entries can't be freed concurrently.
  53. */
  54. list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
  55. mutex_lock(&policy_dbs->update_mutex);
  56. /*
  57. * On 32-bit architectures this may race with the
  58. * sample_delay_ns read in dbs_update_util_handler(), but that
  59. * really doesn't matter. If the read returns a value that's
  60. * too big, the sample will be skipped, but the next invocation
  61. * of dbs_update_util_handler() (when the update has been
  62. * completed) will take a sample.
  63. *
  64. * If this runs in parallel with dbs_work_handler(), we may end
  65. * up overwriting the sample_delay_ns value that it has just
  66. * written, but it will be corrected next time a sample is
  67. * taken, so it shouldn't be significant.
  68. */
  69. gov_update_sample_delay(policy_dbs, 0);
  70. mutex_unlock(&policy_dbs->update_mutex);
  71. }
  72. return count;
  73. }
  74. EXPORT_SYMBOL_GPL(store_sampling_rate);
  75. /**
  76. * gov_update_cpu_data - Update CPU load data.
  77. * @dbs_data: Top-level governor data pointer.
  78. *
  79. * Update CPU load data for all CPUs in the domain governed by @dbs_data
  80. * (that may be a single policy or a bunch of them if governor tunables are
  81. * system-wide).
  82. *
  83. * Call under the @dbs_data mutex.
  84. */
  85. void gov_update_cpu_data(struct dbs_data *dbs_data)
  86. {
  87. struct policy_dbs_info *policy_dbs;
  88. list_for_each_entry(policy_dbs, &dbs_data->attr_set.policy_list, list) {
  89. unsigned int j;
  90. for_each_cpu(j, policy_dbs->policy->cpus) {
  91. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  92. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time,
  93. dbs_data->io_is_busy);
  94. if (dbs_data->ignore_nice_load)
  95. j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  96. }
  97. }
  98. }
  99. EXPORT_SYMBOL_GPL(gov_update_cpu_data);
  100. unsigned int dbs_update(struct cpufreq_policy *policy)
  101. {
  102. struct policy_dbs_info *policy_dbs = policy->governor_data;
  103. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  104. unsigned int ignore_nice = dbs_data->ignore_nice_load;
  105. unsigned int max_load = 0, idle_periods = UINT_MAX;
  106. unsigned int sampling_rate, io_busy, j;
  107. /*
  108. * Sometimes governors may use an additional multiplier to increase
  109. * sample delays temporarily. Apply that multiplier to sampling_rate
  110. * so as to keep the wake-up-from-idle detection logic a bit
  111. * conservative.
  112. */
  113. sampling_rate = dbs_data->sampling_rate * policy_dbs->rate_mult;
  114. /*
  115. * For the purpose of ondemand, waiting for disk IO is an indication
  116. * that you're performance critical, and not that the system is actually
  117. * idle, so do not add the iowait time to the CPU idle time then.
  118. */
  119. io_busy = dbs_data->io_is_busy;
  120. /* Get Absolute Load */
  121. for_each_cpu(j, policy->cpus) {
  122. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  123. u64 update_time, cur_idle_time;
  124. unsigned int idle_time, time_elapsed;
  125. unsigned int load;
  126. cur_idle_time = get_cpu_idle_time(j, &update_time, io_busy);
  127. time_elapsed = update_time - j_cdbs->prev_update_time;
  128. j_cdbs->prev_update_time = update_time;
  129. idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;
  130. j_cdbs->prev_cpu_idle = cur_idle_time;
  131. if (ignore_nice) {
  132. u64 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  133. idle_time += div_u64(cur_nice - j_cdbs->prev_cpu_nice, NSEC_PER_USEC);
  134. j_cdbs->prev_cpu_nice = cur_nice;
  135. }
  136. if (unlikely(!time_elapsed)) {
  137. /*
  138. * That can only happen when this function is called
  139. * twice in a row with a very short interval between the
  140. * calls, so the previous load value can be used then.
  141. */
  142. load = j_cdbs->prev_load;
  143. } else if (unlikely(time_elapsed > 2 * sampling_rate &&
  144. j_cdbs->prev_load)) {
  145. /*
  146. * If the CPU had gone completely idle and a task has
  147. * just woken up on this CPU now, it would be unfair to
  148. * calculate 'load' the usual way for this elapsed
  149. * time-window, because it would show near-zero load,
  150. * irrespective of how CPU intensive that task actually
  151. * was. This is undesirable for latency-sensitive bursty
  152. * workloads.
  153. *
  154. * To avoid this, reuse the 'load' from the previous
  155. * time-window and give this task a chance to start with
  156. * a reasonably high CPU frequency. However, that
  157. * shouldn't be over-done, lest we get stuck at a high
  158. * load (high frequency) for too long, even when the
  159. * current system load has actually dropped down, so
  160. * clear prev_load to guarantee that the load will be
  161. * computed again next time.
  162. *
  163. * Detecting this situation is easy: the governor's
  164. * utilization update handler would not have run during
  165. * CPU-idle periods. Hence, an unusually large
  166. * 'time_elapsed' (as compared to the sampling rate)
  167. * indicates this scenario.
  168. */
  169. load = j_cdbs->prev_load;
  170. j_cdbs->prev_load = 0;
  171. } else {
  172. if (time_elapsed >= idle_time) {
  173. load = 100 * (time_elapsed - idle_time) / time_elapsed;
  174. } else {
  175. /*
  176. * That can happen if idle_time is returned by
  177. * get_cpu_idle_time_jiffy(). In that case
  178. * idle_time is roughly equal to the difference
  179. * between time_elapsed and "busy time" obtained
  180. * from CPU statistics. Then, the "busy time"
  181. * can end up being greater than time_elapsed
  182. * (for example, if jiffies_64 and the CPU
  183. * statistics are updated by different CPUs),
  184. * so idle_time may in fact be negative. That
  185. * means, though, that the CPU was busy all
  186. * the time (on the rough average) during the
  187. * last sampling interval and 100 can be
  188. * returned as the load.
  189. */
  190. load = (int)idle_time < 0 ? 100 : 0;
  191. }
  192. j_cdbs->prev_load = load;
  193. }
  194. if (time_elapsed > 2 * sampling_rate) {
  195. unsigned int periods = time_elapsed / sampling_rate;
  196. if (periods < idle_periods)
  197. idle_periods = periods;
  198. }
  199. if (load > max_load)
  200. max_load = load;
  201. }
  202. policy_dbs->idle_periods = idle_periods;
  203. return max_load;
  204. }
  205. EXPORT_SYMBOL_GPL(dbs_update);
  206. static void dbs_work_handler(struct work_struct *work)
  207. {
  208. struct policy_dbs_info *policy_dbs;
  209. struct cpufreq_policy *policy;
  210. struct dbs_governor *gov;
  211. policy_dbs = container_of(work, struct policy_dbs_info, work);
  212. policy = policy_dbs->policy;
  213. gov = dbs_governor_of(policy);
  214. /*
  215. * Make sure cpufreq_governor_limits() isn't evaluating load or the
  216. * ondemand governor isn't updating the sampling rate in parallel.
  217. */
  218. mutex_lock(&policy_dbs->update_mutex);
  219. gov_update_sample_delay(policy_dbs, gov->gov_dbs_update(policy));
  220. mutex_unlock(&policy_dbs->update_mutex);
  221. /* Allow the utilization update handler to queue up more work. */
  222. atomic_set(&policy_dbs->work_count, 0);
  223. /*
  224. * If the update below is reordered with respect to the sample delay
  225. * modification, the utilization update handler may end up using a stale
  226. * sample delay value.
  227. */
  228. smp_wmb();
  229. policy_dbs->work_in_progress = false;
  230. }
  231. static void dbs_irq_work(struct irq_work *irq_work)
  232. {
  233. struct policy_dbs_info *policy_dbs;
  234. policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
  235. schedule_work_on(smp_processor_id(), &policy_dbs->work);
  236. }
  237. static void dbs_update_util_handler(struct update_util_data *data, u64 time,
  238. unsigned int flags)
  239. {
  240. struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
  241. struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
  242. u64 delta_ns, lst;
  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. unsigned int latency;
  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. /* policy latency is in ns. Convert it to us first */
  370. latency = policy->cpuinfo.transition_latency / 1000;
  371. if (latency == 0)
  372. latency = 1;
  373. /* Bring kernel and HW constraints together */
  374. dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
  375. MIN_LATENCY_MULTIPLIER * latency);
  376. dbs_data->sampling_rate = max(dbs_data->min_sampling_rate,
  377. LATENCY_MULTIPLIER * latency);
  378. if (!have_governor_per_policy())
  379. gov->gdbs_data = dbs_data;
  380. policy_dbs->dbs_data = dbs_data;
  381. policy->governor_data = policy_dbs;
  382. gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
  383. ret = kobject_init_and_add(&dbs_data->attr_set.kobj, &gov->kobj_type,
  384. get_governor_parent_kobj(policy),
  385. "%s", gov->gov.name);
  386. if (!ret)
  387. goto out;
  388. /* Failure, so roll back. */
  389. pr_err("initialization failed (dbs_data kobject init error %d)\n", ret);
  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 = policy->governor_data;
  464. mutex_lock(&policy_dbs->update_mutex);
  465. cpufreq_policy_apply_limits(policy);
  466. gov_update_sample_delay(policy_dbs, 0);
  467. mutex_unlock(&policy_dbs->update_mutex);
  468. }
  469. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_limits);