cpufreq_governor.c 16 KB

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