cpufreq_schedutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * CPUFreq governor based on scheduler-provided CPU utilization data.
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/cpufreq.h>
  13. #include <linux/kthread.h>
  14. #include <uapi/linux/sched/types.h>
  15. #include <linux/slab.h>
  16. #include <trace/events/power.h>
  17. #include "sched.h"
  18. struct sugov_tunables {
  19. struct gov_attr_set attr_set;
  20. unsigned int rate_limit_us;
  21. };
  22. struct sugov_policy {
  23. struct cpufreq_policy *policy;
  24. struct sugov_tunables *tunables;
  25. struct list_head tunables_hook;
  26. raw_spinlock_t update_lock; /* For shared policies */
  27. u64 last_freq_update_time;
  28. s64 freq_update_delay_ns;
  29. unsigned int next_freq;
  30. unsigned int cached_raw_freq;
  31. /* The next fields are only needed if fast switch cannot be used. */
  32. struct irq_work irq_work;
  33. struct kthread_work work;
  34. struct mutex work_lock;
  35. struct kthread_worker worker;
  36. struct task_struct *thread;
  37. bool work_in_progress;
  38. bool need_freq_update;
  39. };
  40. struct sugov_cpu {
  41. struct update_util_data update_util;
  42. struct sugov_policy *sg_policy;
  43. unsigned int cpu;
  44. bool iowait_boost_pending;
  45. unsigned int iowait_boost;
  46. unsigned int iowait_boost_max;
  47. u64 last_update;
  48. /* The fields below are only needed when sharing a policy. */
  49. unsigned long util_cfs;
  50. unsigned long util_dl;
  51. unsigned long max;
  52. unsigned int flags;
  53. /* The field below is for single-CPU policies only. */
  54. #ifdef CONFIG_NO_HZ_COMMON
  55. unsigned long saved_idle_calls;
  56. #endif
  57. };
  58. static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
  59. /************************ Governor internals ***********************/
  60. static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
  61. {
  62. s64 delta_ns;
  63. /*
  64. * Since cpufreq_update_util() is called with rq->lock held for
  65. * the @target_cpu, our per-cpu data is fully serialized.
  66. *
  67. * However, drivers cannot in general deal with cross-cpu
  68. * requests, so while get_next_freq() will work, our
  69. * sugov_update_commit() call may not for the fast switching platforms.
  70. *
  71. * Hence stop here for remote requests if they aren't supported
  72. * by the hardware, as calculating the frequency is pointless if
  73. * we cannot in fact act on it.
  74. *
  75. * For the slow switching platforms, the kthread is always scheduled on
  76. * the right set of CPUs and any CPU can find the next frequency and
  77. * schedule the kthread.
  78. */
  79. if (sg_policy->policy->fast_switch_enabled &&
  80. !cpufreq_can_do_remote_dvfs(sg_policy->policy))
  81. return false;
  82. if (sg_policy->work_in_progress)
  83. return false;
  84. if (unlikely(sg_policy->need_freq_update)) {
  85. sg_policy->need_freq_update = false;
  86. /*
  87. * This happens when limits change, so forget the previous
  88. * next_freq value and force an update.
  89. */
  90. sg_policy->next_freq = UINT_MAX;
  91. return true;
  92. }
  93. delta_ns = time - sg_policy->last_freq_update_time;
  94. return delta_ns >= sg_policy->freq_update_delay_ns;
  95. }
  96. static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
  97. unsigned int next_freq)
  98. {
  99. struct cpufreq_policy *policy = sg_policy->policy;
  100. if (sg_policy->next_freq == next_freq)
  101. return;
  102. sg_policy->next_freq = next_freq;
  103. sg_policy->last_freq_update_time = time;
  104. if (policy->fast_switch_enabled) {
  105. next_freq = cpufreq_driver_fast_switch(policy, next_freq);
  106. if (!next_freq)
  107. return;
  108. policy->cur = next_freq;
  109. trace_cpu_frequency(next_freq, smp_processor_id());
  110. } else {
  111. sg_policy->work_in_progress = true;
  112. irq_work_queue(&sg_policy->irq_work);
  113. }
  114. }
  115. /**
  116. * get_next_freq - Compute a new frequency for a given cpufreq policy.
  117. * @sg_policy: schedutil policy object to compute the new frequency for.
  118. * @util: Current CPU utilization.
  119. * @max: CPU capacity.
  120. *
  121. * If the utilization is frequency-invariant, choose the new frequency to be
  122. * proportional to it, that is
  123. *
  124. * next_freq = C * max_freq * util / max
  125. *
  126. * Otherwise, approximate the would-be frequency-invariant utilization by
  127. * util_raw * (curr_freq / max_freq) which leads to
  128. *
  129. * next_freq = C * curr_freq * util_raw / max
  130. *
  131. * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
  132. *
  133. * The lowest driver-supported frequency which is equal or greater than the raw
  134. * next_freq (as calculated above) is returned, subject to policy min/max and
  135. * cpufreq driver limitations.
  136. */
  137. static unsigned int get_next_freq(struct sugov_policy *sg_policy,
  138. unsigned long util, unsigned long max)
  139. {
  140. struct cpufreq_policy *policy = sg_policy->policy;
  141. unsigned int freq = arch_scale_freq_invariant() ?
  142. policy->cpuinfo.max_freq : policy->cur;
  143. freq = (freq + (freq >> 2)) * util / max;
  144. if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
  145. return sg_policy->next_freq;
  146. sg_policy->cached_raw_freq = freq;
  147. return cpufreq_driver_resolve_freq(policy, freq);
  148. }
  149. static void sugov_get_util(struct sugov_cpu *sg_cpu)
  150. {
  151. struct rq *rq = cpu_rq(sg_cpu->cpu);
  152. sg_cpu->max = arch_scale_cpu_capacity(NULL, sg_cpu->cpu);
  153. sg_cpu->util_cfs = cpu_util_cfs(rq);
  154. sg_cpu->util_dl = cpu_util_dl(rq);
  155. }
  156. static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
  157. {
  158. /*
  159. * Ideally we would like to set util_dl as min/guaranteed freq and
  160. * util_cfs + util_dl as requested freq. However, cpufreq is not yet
  161. * ready for such an interface. So, we only do the latter for now.
  162. */
  163. return min(sg_cpu->util_cfs + sg_cpu->util_dl, sg_cpu->max);
  164. }
  165. static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time)
  166. {
  167. if (sg_cpu->flags & SCHED_CPUFREQ_IOWAIT) {
  168. if (sg_cpu->iowait_boost_pending)
  169. return;
  170. sg_cpu->iowait_boost_pending = true;
  171. if (sg_cpu->iowait_boost) {
  172. sg_cpu->iowait_boost <<= 1;
  173. if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
  174. sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
  175. } else {
  176. sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
  177. }
  178. } else if (sg_cpu->iowait_boost) {
  179. s64 delta_ns = time - sg_cpu->last_update;
  180. /* Clear iowait_boost if the CPU apprears to have been idle. */
  181. if (delta_ns > TICK_NSEC) {
  182. sg_cpu->iowait_boost = 0;
  183. sg_cpu->iowait_boost_pending = false;
  184. }
  185. }
  186. }
  187. static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
  188. unsigned long *max)
  189. {
  190. unsigned int boost_util, boost_max;
  191. if (!sg_cpu->iowait_boost)
  192. return;
  193. if (sg_cpu->iowait_boost_pending) {
  194. sg_cpu->iowait_boost_pending = false;
  195. } else {
  196. sg_cpu->iowait_boost >>= 1;
  197. if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) {
  198. sg_cpu->iowait_boost = 0;
  199. return;
  200. }
  201. }
  202. boost_util = sg_cpu->iowait_boost;
  203. boost_max = sg_cpu->iowait_boost_max;
  204. if (*util * boost_max < *max * boost_util) {
  205. *util = boost_util;
  206. *max = boost_max;
  207. }
  208. }
  209. #ifdef CONFIG_NO_HZ_COMMON
  210. static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
  211. {
  212. unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu);
  213. bool ret = idle_calls == sg_cpu->saved_idle_calls;
  214. sg_cpu->saved_idle_calls = idle_calls;
  215. return ret;
  216. }
  217. #else
  218. static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
  219. #endif /* CONFIG_NO_HZ_COMMON */
  220. static void sugov_update_single(struct update_util_data *hook, u64 time,
  221. unsigned int flags)
  222. {
  223. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  224. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  225. struct cpufreq_policy *policy = sg_policy->policy;
  226. unsigned long util, max;
  227. unsigned int next_f;
  228. bool busy;
  229. sugov_set_iowait_boost(sg_cpu, time);
  230. sg_cpu->last_update = time;
  231. if (!sugov_should_update_freq(sg_policy, time))
  232. return;
  233. busy = sugov_cpu_is_busy(sg_cpu);
  234. if (flags & SCHED_CPUFREQ_RT) {
  235. next_f = policy->cpuinfo.max_freq;
  236. } else {
  237. sugov_get_util(sg_cpu);
  238. max = sg_cpu->max;
  239. util = sugov_aggregate_util(sg_cpu);
  240. sugov_iowait_boost(sg_cpu, &util, &max);
  241. next_f = get_next_freq(sg_policy, util, max);
  242. /*
  243. * Do not reduce the frequency if the CPU has not been idle
  244. * recently, as the reduction is likely to be premature then.
  245. */
  246. if (busy && next_f < sg_policy->next_freq) {
  247. next_f = sg_policy->next_freq;
  248. /* Reset cached freq as next_freq has changed */
  249. sg_policy->cached_raw_freq = 0;
  250. }
  251. }
  252. sugov_update_commit(sg_policy, time, next_f);
  253. }
  254. static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
  255. {
  256. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  257. struct cpufreq_policy *policy = sg_policy->policy;
  258. unsigned long util = 0, max = 1;
  259. unsigned int j;
  260. for_each_cpu(j, policy->cpus) {
  261. struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
  262. unsigned long j_util, j_max;
  263. s64 delta_ns;
  264. /*
  265. * If the CFS CPU utilization was last updated before the
  266. * previous frequency update and the time elapsed between the
  267. * last update of the CPU utilization and the last frequency
  268. * update is long enough, reset iowait_boost and util_cfs, as
  269. * they are now probably stale. However, still consider the
  270. * CPU contribution if it has some DEADLINE utilization
  271. * (util_dl).
  272. */
  273. delta_ns = time - j_sg_cpu->last_update;
  274. if (delta_ns > TICK_NSEC) {
  275. j_sg_cpu->iowait_boost = 0;
  276. j_sg_cpu->iowait_boost_pending = false;
  277. j_sg_cpu->util_cfs = 0;
  278. if (j_sg_cpu->util_dl == 0)
  279. continue;
  280. }
  281. if (j_sg_cpu->flags & SCHED_CPUFREQ_RT)
  282. return policy->cpuinfo.max_freq;
  283. j_max = j_sg_cpu->max;
  284. j_util = sugov_aggregate_util(j_sg_cpu);
  285. if (j_util * max > j_max * util) {
  286. util = j_util;
  287. max = j_max;
  288. }
  289. sugov_iowait_boost(j_sg_cpu, &util, &max);
  290. }
  291. return get_next_freq(sg_policy, util, max);
  292. }
  293. static void sugov_update_shared(struct update_util_data *hook, u64 time,
  294. unsigned int flags)
  295. {
  296. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  297. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  298. unsigned int next_f;
  299. raw_spin_lock(&sg_policy->update_lock);
  300. sugov_get_util(sg_cpu);
  301. sg_cpu->flags = flags;
  302. sugov_set_iowait_boost(sg_cpu, time);
  303. sg_cpu->last_update = time;
  304. if (sugov_should_update_freq(sg_policy, time)) {
  305. if (flags & SCHED_CPUFREQ_RT)
  306. next_f = sg_policy->policy->cpuinfo.max_freq;
  307. else
  308. next_f = sugov_next_freq_shared(sg_cpu, time);
  309. sugov_update_commit(sg_policy, time, next_f);
  310. }
  311. raw_spin_unlock(&sg_policy->update_lock);
  312. }
  313. static void sugov_work(struct kthread_work *work)
  314. {
  315. struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
  316. mutex_lock(&sg_policy->work_lock);
  317. __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
  318. CPUFREQ_RELATION_L);
  319. mutex_unlock(&sg_policy->work_lock);
  320. sg_policy->work_in_progress = false;
  321. }
  322. static void sugov_irq_work(struct irq_work *irq_work)
  323. {
  324. struct sugov_policy *sg_policy;
  325. sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
  326. /*
  327. * For RT tasks, the schedutil governor shoots the frequency to maximum.
  328. * Special care must be taken to ensure that this kthread doesn't result
  329. * in the same behavior.
  330. *
  331. * This is (mostly) guaranteed by the work_in_progress flag. The flag is
  332. * updated only at the end of the sugov_work() function and before that
  333. * the schedutil governor rejects all other frequency scaling requests.
  334. *
  335. * There is a very rare case though, where the RT thread yields right
  336. * after the work_in_progress flag is cleared. The effects of that are
  337. * neglected for now.
  338. */
  339. kthread_queue_work(&sg_policy->worker, &sg_policy->work);
  340. }
  341. /************************** sysfs interface ************************/
  342. static struct sugov_tunables *global_tunables;
  343. static DEFINE_MUTEX(global_tunables_lock);
  344. static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
  345. {
  346. return container_of(attr_set, struct sugov_tunables, attr_set);
  347. }
  348. static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
  349. {
  350. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  351. return sprintf(buf, "%u\n", tunables->rate_limit_us);
  352. }
  353. static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
  354. size_t count)
  355. {
  356. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  357. struct sugov_policy *sg_policy;
  358. unsigned int rate_limit_us;
  359. if (kstrtouint(buf, 10, &rate_limit_us))
  360. return -EINVAL;
  361. tunables->rate_limit_us = rate_limit_us;
  362. list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
  363. sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
  364. return count;
  365. }
  366. static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
  367. static struct attribute *sugov_attributes[] = {
  368. &rate_limit_us.attr,
  369. NULL
  370. };
  371. static struct kobj_type sugov_tunables_ktype = {
  372. .default_attrs = sugov_attributes,
  373. .sysfs_ops = &governor_sysfs_ops,
  374. };
  375. /********************** cpufreq governor interface *********************/
  376. static struct cpufreq_governor schedutil_gov;
  377. static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
  378. {
  379. struct sugov_policy *sg_policy;
  380. sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
  381. if (!sg_policy)
  382. return NULL;
  383. sg_policy->policy = policy;
  384. raw_spin_lock_init(&sg_policy->update_lock);
  385. return sg_policy;
  386. }
  387. static void sugov_policy_free(struct sugov_policy *sg_policy)
  388. {
  389. kfree(sg_policy);
  390. }
  391. static int sugov_kthread_create(struct sugov_policy *sg_policy)
  392. {
  393. struct task_struct *thread;
  394. struct sched_attr attr = {
  395. .size = sizeof(struct sched_attr),
  396. .sched_policy = SCHED_DEADLINE,
  397. .sched_flags = SCHED_FLAG_SUGOV,
  398. .sched_nice = 0,
  399. .sched_priority = 0,
  400. /*
  401. * Fake (unused) bandwidth; workaround to "fix"
  402. * priority inheritance.
  403. */
  404. .sched_runtime = 1000000,
  405. .sched_deadline = 10000000,
  406. .sched_period = 10000000,
  407. };
  408. struct cpufreq_policy *policy = sg_policy->policy;
  409. int ret;
  410. /* kthread only required for slow path */
  411. if (policy->fast_switch_enabled)
  412. return 0;
  413. kthread_init_work(&sg_policy->work, sugov_work);
  414. kthread_init_worker(&sg_policy->worker);
  415. thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
  416. "sugov:%d",
  417. cpumask_first(policy->related_cpus));
  418. if (IS_ERR(thread)) {
  419. pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
  420. return PTR_ERR(thread);
  421. }
  422. ret = sched_setattr_nocheck(thread, &attr);
  423. if (ret) {
  424. kthread_stop(thread);
  425. pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__);
  426. return ret;
  427. }
  428. sg_policy->thread = thread;
  429. /* Kthread is bound to all CPUs by default */
  430. if (!policy->dvfs_possible_from_any_cpu)
  431. kthread_bind_mask(thread, policy->related_cpus);
  432. init_irq_work(&sg_policy->irq_work, sugov_irq_work);
  433. mutex_init(&sg_policy->work_lock);
  434. wake_up_process(thread);
  435. return 0;
  436. }
  437. static void sugov_kthread_stop(struct sugov_policy *sg_policy)
  438. {
  439. /* kthread only required for slow path */
  440. if (sg_policy->policy->fast_switch_enabled)
  441. return;
  442. kthread_flush_worker(&sg_policy->worker);
  443. kthread_stop(sg_policy->thread);
  444. mutex_destroy(&sg_policy->work_lock);
  445. }
  446. static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
  447. {
  448. struct sugov_tunables *tunables;
  449. tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
  450. if (tunables) {
  451. gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
  452. if (!have_governor_per_policy())
  453. global_tunables = tunables;
  454. }
  455. return tunables;
  456. }
  457. static void sugov_tunables_free(struct sugov_tunables *tunables)
  458. {
  459. if (!have_governor_per_policy())
  460. global_tunables = NULL;
  461. kfree(tunables);
  462. }
  463. static int sugov_init(struct cpufreq_policy *policy)
  464. {
  465. struct sugov_policy *sg_policy;
  466. struct sugov_tunables *tunables;
  467. int ret = 0;
  468. /* State should be equivalent to EXIT */
  469. if (policy->governor_data)
  470. return -EBUSY;
  471. cpufreq_enable_fast_switch(policy);
  472. sg_policy = sugov_policy_alloc(policy);
  473. if (!sg_policy) {
  474. ret = -ENOMEM;
  475. goto disable_fast_switch;
  476. }
  477. ret = sugov_kthread_create(sg_policy);
  478. if (ret)
  479. goto free_sg_policy;
  480. mutex_lock(&global_tunables_lock);
  481. if (global_tunables) {
  482. if (WARN_ON(have_governor_per_policy())) {
  483. ret = -EINVAL;
  484. goto stop_kthread;
  485. }
  486. policy->governor_data = sg_policy;
  487. sg_policy->tunables = global_tunables;
  488. gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
  489. goto out;
  490. }
  491. tunables = sugov_tunables_alloc(sg_policy);
  492. if (!tunables) {
  493. ret = -ENOMEM;
  494. goto stop_kthread;
  495. }
  496. tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy);
  497. policy->governor_data = sg_policy;
  498. sg_policy->tunables = tunables;
  499. ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
  500. get_governor_parent_kobj(policy), "%s",
  501. schedutil_gov.name);
  502. if (ret)
  503. goto fail;
  504. out:
  505. mutex_unlock(&global_tunables_lock);
  506. return 0;
  507. fail:
  508. policy->governor_data = NULL;
  509. sugov_tunables_free(tunables);
  510. stop_kthread:
  511. sugov_kthread_stop(sg_policy);
  512. free_sg_policy:
  513. mutex_unlock(&global_tunables_lock);
  514. sugov_policy_free(sg_policy);
  515. disable_fast_switch:
  516. cpufreq_disable_fast_switch(policy);
  517. pr_err("initialization failed (error %d)\n", ret);
  518. return ret;
  519. }
  520. static void sugov_exit(struct cpufreq_policy *policy)
  521. {
  522. struct sugov_policy *sg_policy = policy->governor_data;
  523. struct sugov_tunables *tunables = sg_policy->tunables;
  524. unsigned int count;
  525. mutex_lock(&global_tunables_lock);
  526. count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
  527. policy->governor_data = NULL;
  528. if (!count)
  529. sugov_tunables_free(tunables);
  530. mutex_unlock(&global_tunables_lock);
  531. sugov_kthread_stop(sg_policy);
  532. sugov_policy_free(sg_policy);
  533. cpufreq_disable_fast_switch(policy);
  534. }
  535. static int sugov_start(struct cpufreq_policy *policy)
  536. {
  537. struct sugov_policy *sg_policy = policy->governor_data;
  538. unsigned int cpu;
  539. sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
  540. sg_policy->last_freq_update_time = 0;
  541. sg_policy->next_freq = UINT_MAX;
  542. sg_policy->work_in_progress = false;
  543. sg_policy->need_freq_update = false;
  544. sg_policy->cached_raw_freq = 0;
  545. for_each_cpu(cpu, policy->cpus) {
  546. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  547. memset(sg_cpu, 0, sizeof(*sg_cpu));
  548. sg_cpu->cpu = cpu;
  549. sg_cpu->sg_policy = sg_policy;
  550. sg_cpu->flags = 0;
  551. sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
  552. }
  553. for_each_cpu(cpu, policy->cpus) {
  554. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  555. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  556. policy_is_shared(policy) ?
  557. sugov_update_shared :
  558. sugov_update_single);
  559. }
  560. return 0;
  561. }
  562. static void sugov_stop(struct cpufreq_policy *policy)
  563. {
  564. struct sugov_policy *sg_policy = policy->governor_data;
  565. unsigned int cpu;
  566. for_each_cpu(cpu, policy->cpus)
  567. cpufreq_remove_update_util_hook(cpu);
  568. synchronize_sched();
  569. if (!policy->fast_switch_enabled) {
  570. irq_work_sync(&sg_policy->irq_work);
  571. kthread_cancel_work_sync(&sg_policy->work);
  572. }
  573. }
  574. static void sugov_limits(struct cpufreq_policy *policy)
  575. {
  576. struct sugov_policy *sg_policy = policy->governor_data;
  577. if (!policy->fast_switch_enabled) {
  578. mutex_lock(&sg_policy->work_lock);
  579. cpufreq_policy_apply_limits(policy);
  580. mutex_unlock(&sg_policy->work_lock);
  581. }
  582. sg_policy->need_freq_update = true;
  583. }
  584. static struct cpufreq_governor schedutil_gov = {
  585. .name = "schedutil",
  586. .owner = THIS_MODULE,
  587. .dynamic_switching = true,
  588. .init = sugov_init,
  589. .exit = sugov_exit,
  590. .start = sugov_start,
  591. .stop = sugov_stop,
  592. .limits = sugov_limits,
  593. };
  594. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
  595. struct cpufreq_governor *cpufreq_default_governor(void)
  596. {
  597. return &schedutil_gov;
  598. }
  599. #endif
  600. static int __init sugov_register(void)
  601. {
  602. return cpufreq_register_governor(&schedutil_gov);
  603. }
  604. fs_initcall(sugov_register);