cpufreq_schedutil.c 19 KB

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