cpufreq_governor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/cpufreq/cpufreq_governor.c
  4. *
  5. * CPUFREQ governors common code
  6. *
  7. * Copyright (C) 2001 Russell King
  8. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  9. * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
  10. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  11. * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/export.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/slab.h>
  17. #include "cpufreq_governor.h"
  18. #define CPUFREQ_DBS_MIN_SAMPLING_INTERVAL (2 * TICK_NSEC / NSEC_PER_USEC)
  19. static DEFINE_PER_CPU(struct cpu_dbs_info, cpu_dbs);
  20. static DEFINE_MUTEX(gov_dbs_data_mutex);
  21. /* Common sysfs tunables */
  22. /*
  23. * sampling_rate_store - update sampling rate effective immediately if needed.
  24. *
  25. * If new rate is smaller than the old, simply updating
  26. * dbs.sampling_rate might not be appropriate. For example, if the
  27. * original sampling_rate was 1 second and the requested new sampling rate is 10
  28. * ms because the user needs immediate reaction from ondemand governor, but not
  29. * sure if higher frequency will be required or not, then, the governor may
  30. * change the sampling rate too late; up to 1 second later. Thus, if we are
  31. * reducing the sampling rate, we need to make the new value effective
  32. * immediately.
  33. *
  34. * This must be called with dbs_data->mutex held, otherwise traversing
  35. * policy_dbs_list isn't safe.
  36. */
  37. ssize_t sampling_rate_store(struct gov_attr_set *attr_set, const char *buf,
  38. size_t count)
  39. {
  40. struct dbs_data *dbs_data = to_dbs_data(attr_set);
  41. struct policy_dbs_info *policy_dbs;
  42. unsigned int sampling_interval;
  43. int ret;
  44. ret = sscanf(buf, "%u", &sampling_interval);
  45. if (ret != 1 || sampling_interval < CPUFREQ_DBS_MIN_SAMPLING_INTERVAL)
  46. return -EINVAL;
  47. dbs_data->sampling_rate = sampling_interval;
  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(sampling_rate_store);
  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_field(&kcpustat_cpu(j), CPUTIME_NICE, j);
  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. /*
  128. * cur_idle_time could be smaller than j_cdbs->prev_cpu_idle if
  129. * it's obtained from get_cpu_idle_time_jiffy() when NOHZ is
  130. * off, where idle_time is calculated by the difference between
  131. * time elapsed in jiffies and "busy time" obtained from CPU
  132. * statistics. If a CPU is 100% busy, the time elapsed and busy
  133. * time should grow with the same amount in two consecutive
  134. * samples, but in practice there could be a tiny difference,
  135. * making the accumulated idle time decrease sometimes. Hence,
  136. * in this case, idle_time should be regarded as 0 in order to
  137. * make the further process correct.
  138. */
  139. if (cur_idle_time > j_cdbs->prev_cpu_idle)
  140. idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;
  141. else
  142. idle_time = 0;
  143. j_cdbs->prev_cpu_idle = cur_idle_time;
  144. if (ignore_nice) {
  145. u64 cur_nice = kcpustat_field(&kcpustat_cpu(j), CPUTIME_NICE, j);
  146. idle_time += div_u64(cur_nice - j_cdbs->prev_cpu_nice, NSEC_PER_USEC);
  147. j_cdbs->prev_cpu_nice = cur_nice;
  148. }
  149. if (unlikely(!time_elapsed)) {
  150. /*
  151. * That can only happen when this function is called
  152. * twice in a row with a very short interval between the
  153. * calls, so the previous load value can be used then.
  154. */
  155. load = j_cdbs->prev_load;
  156. } else if (unlikely(idle_time > 2 * sampling_rate &&
  157. j_cdbs->prev_load)) {
  158. /*
  159. * If the CPU had gone completely idle and a task has
  160. * just woken up on this CPU now, it would be unfair to
  161. * calculate 'load' the usual way for this elapsed
  162. * time-window, because it would show near-zero load,
  163. * irrespective of how CPU intensive that task actually
  164. * was. This is undesirable for latency-sensitive bursty
  165. * workloads.
  166. *
  167. * To avoid this, reuse the 'load' from the previous
  168. * time-window and give this task a chance to start with
  169. * a reasonably high CPU frequency. However, that
  170. * shouldn't be over-done, lest we get stuck at a high
  171. * load (high frequency) for too long, even when the
  172. * current system load has actually dropped down, so
  173. * clear prev_load to guarantee that the load will be
  174. * computed again next time.
  175. *
  176. * Detecting this situation is easy: an unusually large
  177. * 'idle_time' (as compared to the sampling rate)
  178. * indicates this scenario.
  179. */
  180. load = j_cdbs->prev_load;
  181. j_cdbs->prev_load = 0;
  182. } else {
  183. if (time_elapsed > idle_time)
  184. load = 100 * (time_elapsed - idle_time) / time_elapsed;
  185. else
  186. load = 0;
  187. j_cdbs->prev_load = load;
  188. }
  189. if (unlikely(idle_time > 2 * sampling_rate)) {
  190. unsigned int periods = idle_time / sampling_rate;
  191. if (periods < idle_periods)
  192. idle_periods = periods;
  193. }
  194. if (load > max_load)
  195. max_load = load;
  196. }
  197. policy_dbs->idle_periods = idle_periods;
  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->update_mutex);
  214. gov_update_sample_delay(policy_dbs, gov->gov_dbs_update(policy));
  215. mutex_unlock(&policy_dbs->update_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. if (!cpufreq_this_cpu_can_update(policy_dbs->policy))
  239. return;
  240. /*
  241. * The work may not be allowed to be queued up right now.
  242. * Possible reasons:
  243. * - Work has already been queued up or is in progress.
  244. * - It is too early (too little time from the previous sample).
  245. */
  246. if (policy_dbs->work_in_progress)
  247. return;
  248. /*
  249. * If the reads below are reordered before the check above, the value
  250. * of sample_delay_ns used in the computation may be stale.
  251. */
  252. smp_rmb();
  253. lst = READ_ONCE(policy_dbs->last_sample_time);
  254. delta_ns = time - lst;
  255. if ((s64)delta_ns < policy_dbs->sample_delay_ns)
  256. return;
  257. /*
  258. * If the policy is not shared, the irq_work may be queued up right away
  259. * at this point. Otherwise, we need to ensure that only one of the
  260. * CPUs sharing the policy will do that.
  261. */
  262. if (policy_dbs->is_shared) {
  263. if (!atomic_add_unless(&policy_dbs->work_count, 1, 1))
  264. return;
  265. /*
  266. * If another CPU updated last_sample_time in the meantime, we
  267. * shouldn't be here, so clear the work counter and bail out.
  268. */
  269. if (unlikely(lst != READ_ONCE(policy_dbs->last_sample_time))) {
  270. atomic_set(&policy_dbs->work_count, 0);
  271. return;
  272. }
  273. }
  274. policy_dbs->last_sample_time = time;
  275. policy_dbs->work_in_progress = true;
  276. irq_work_queue(&policy_dbs->irq_work);
  277. }
  278. static void gov_set_update_util(struct policy_dbs_info *policy_dbs,
  279. unsigned int delay_us)
  280. {
  281. struct cpufreq_policy *policy = policy_dbs->policy;
  282. int cpu;
  283. gov_update_sample_delay(policy_dbs, delay_us);
  284. policy_dbs->last_sample_time = 0;
  285. for_each_cpu(cpu, policy->cpus) {
  286. struct cpu_dbs_info *cdbs = &per_cpu(cpu_dbs, cpu);
  287. cpufreq_add_update_util_hook(cpu, &cdbs->update_util,
  288. dbs_update_util_handler);
  289. }
  290. }
  291. static inline void gov_clear_update_util(struct cpufreq_policy *policy)
  292. {
  293. int i;
  294. for_each_cpu(i, policy->cpus)
  295. cpufreq_remove_update_util_hook(i);
  296. synchronize_rcu();
  297. }
  298. static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
  299. struct dbs_governor *gov)
  300. {
  301. struct policy_dbs_info *policy_dbs;
  302. int j;
  303. /* Allocate memory for per-policy governor data. */
  304. policy_dbs = gov->alloc();
  305. if (!policy_dbs)
  306. return NULL;
  307. policy_dbs->policy = policy;
  308. mutex_init(&policy_dbs->update_mutex);
  309. atomic_set(&policy_dbs->work_count, 0);
  310. init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
  311. INIT_WORK(&policy_dbs->work, dbs_work_handler);
  312. /* Set policy_dbs for all CPUs, online+offline */
  313. for_each_cpu(j, policy->related_cpus) {
  314. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  315. j_cdbs->policy_dbs = policy_dbs;
  316. }
  317. return policy_dbs;
  318. }
  319. static void free_policy_dbs_info(struct policy_dbs_info *policy_dbs,
  320. struct dbs_governor *gov)
  321. {
  322. int j;
  323. mutex_destroy(&policy_dbs->update_mutex);
  324. for_each_cpu(j, policy_dbs->policy->related_cpus) {
  325. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  326. j_cdbs->policy_dbs = NULL;
  327. j_cdbs->update_util.func = NULL;
  328. }
  329. gov->free(policy_dbs);
  330. }
  331. static void cpufreq_dbs_data_release(struct kobject *kobj)
  332. {
  333. struct dbs_data *dbs_data = to_dbs_data(to_gov_attr_set(kobj));
  334. struct dbs_governor *gov = dbs_data->gov;
  335. gov->exit(dbs_data);
  336. kfree(dbs_data);
  337. }
  338. int cpufreq_dbs_governor_init(struct cpufreq_policy *policy)
  339. {
  340. struct dbs_governor *gov = dbs_governor_of(policy);
  341. struct dbs_data *dbs_data;
  342. struct policy_dbs_info *policy_dbs;
  343. int ret = 0;
  344. /* State should be equivalent to EXIT */
  345. if (policy->governor_data)
  346. return -EBUSY;
  347. policy_dbs = alloc_policy_dbs_info(policy, gov);
  348. if (!policy_dbs)
  349. return -ENOMEM;
  350. /* Protect gov->gdbs_data against concurrent updates. */
  351. mutex_lock(&gov_dbs_data_mutex);
  352. dbs_data = gov->gdbs_data;
  353. if (dbs_data) {
  354. if (WARN_ON(have_governor_per_policy())) {
  355. ret = -EINVAL;
  356. goto free_policy_dbs_info;
  357. }
  358. policy_dbs->dbs_data = dbs_data;
  359. policy->governor_data = policy_dbs;
  360. gov_attr_set_get(&dbs_data->attr_set, &policy_dbs->list);
  361. goto out;
  362. }
  363. dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
  364. if (!dbs_data) {
  365. ret = -ENOMEM;
  366. goto free_policy_dbs_info;
  367. }
  368. dbs_data->gov = gov;
  369. gov_attr_set_init(&dbs_data->attr_set, &policy_dbs->list);
  370. ret = gov->init(dbs_data);
  371. if (ret)
  372. goto free_dbs_data;
  373. /*
  374. * The sampling interval should not be less than the transition latency
  375. * of the CPU and it also cannot be too small for dbs_update() to work
  376. * correctly.
  377. */
  378. dbs_data->sampling_rate = max_t(unsigned int,
  379. CPUFREQ_DBS_MIN_SAMPLING_INTERVAL,
  380. cpufreq_policy_transition_delay_us(policy));
  381. if (!have_governor_per_policy())
  382. gov->gdbs_data = dbs_data;
  383. policy_dbs->dbs_data = dbs_data;
  384. policy->governor_data = policy_dbs;
  385. gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
  386. gov->kobj_type.release = cpufreq_dbs_data_release;
  387. ret = kobject_init_and_add(&dbs_data->attr_set.kobj, &gov->kobj_type,
  388. get_governor_parent_kobj(policy),
  389. "%s", gov->gov.name);
  390. if (!ret)
  391. goto out;
  392. /* Failure, so roll back. */
  393. pr_err("initialization failed (dbs_data kobject init error %d)\n", ret);
  394. kobject_put(&dbs_data->attr_set.kobj);
  395. policy->governor_data = NULL;
  396. if (!have_governor_per_policy())
  397. gov->gdbs_data = NULL;
  398. gov->exit(dbs_data);
  399. free_dbs_data:
  400. kfree(dbs_data);
  401. free_policy_dbs_info:
  402. free_policy_dbs_info(policy_dbs, gov);
  403. out:
  404. mutex_unlock(&gov_dbs_data_mutex);
  405. return ret;
  406. }
  407. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_init);
  408. void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy)
  409. {
  410. struct dbs_governor *gov = dbs_governor_of(policy);
  411. struct policy_dbs_info *policy_dbs = policy->governor_data;
  412. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  413. unsigned int count;
  414. /* Protect gov->gdbs_data against concurrent updates. */
  415. mutex_lock(&gov_dbs_data_mutex);
  416. count = gov_attr_set_put(&dbs_data->attr_set, &policy_dbs->list);
  417. policy->governor_data = NULL;
  418. if (!count && !have_governor_per_policy())
  419. gov->gdbs_data = NULL;
  420. free_policy_dbs_info(policy_dbs, gov);
  421. mutex_unlock(&gov_dbs_data_mutex);
  422. }
  423. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_exit);
  424. int cpufreq_dbs_governor_start(struct cpufreq_policy *policy)
  425. {
  426. struct dbs_governor *gov = dbs_governor_of(policy);
  427. struct policy_dbs_info *policy_dbs = policy->governor_data;
  428. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  429. unsigned int sampling_rate, ignore_nice, j;
  430. unsigned int io_busy;
  431. if (!policy->cur)
  432. return -EINVAL;
  433. policy_dbs->is_shared = policy_is_shared(policy);
  434. policy_dbs->rate_mult = 1;
  435. sampling_rate = dbs_data->sampling_rate;
  436. ignore_nice = dbs_data->ignore_nice_load;
  437. io_busy = dbs_data->io_is_busy;
  438. for_each_cpu(j, policy->cpus) {
  439. struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j);
  440. j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time, io_busy);
  441. /*
  442. * Make the first invocation of dbs_update() compute the load.
  443. */
  444. j_cdbs->prev_load = 0;
  445. if (ignore_nice)
  446. j_cdbs->prev_cpu_nice = kcpustat_field(&kcpustat_cpu(j), CPUTIME_NICE, j);
  447. }
  448. gov->start(policy);
  449. gov_set_update_util(policy_dbs, sampling_rate);
  450. return 0;
  451. }
  452. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_start);
  453. void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy)
  454. {
  455. struct policy_dbs_info *policy_dbs = policy->governor_data;
  456. gov_clear_update_util(policy_dbs->policy);
  457. irq_work_sync(&policy_dbs->irq_work);
  458. cancel_work_sync(&policy_dbs->work);
  459. atomic_set(&policy_dbs->work_count, 0);
  460. policy_dbs->work_in_progress = false;
  461. }
  462. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_stop);
  463. void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy)
  464. {
  465. struct policy_dbs_info *policy_dbs;
  466. /* Protect gov->gdbs_data against cpufreq_dbs_governor_exit() */
  467. mutex_lock(&gov_dbs_data_mutex);
  468. policy_dbs = policy->governor_data;
  469. if (!policy_dbs)
  470. goto out;
  471. mutex_lock(&policy_dbs->update_mutex);
  472. cpufreq_policy_apply_limits(policy);
  473. gov_update_sample_delay(policy_dbs, 0);
  474. mutex_unlock(&policy_dbs->update_mutex);
  475. out:
  476. mutex_unlock(&gov_dbs_data_mutex);
  477. }
  478. EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_limits);