cpufreq_schedutil.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. /* The field below is for single-CPU policies only: */
  49. #ifdef CONFIG_NO_HZ_COMMON
  50. unsigned long saved_idle_calls;
  51. #endif
  52. };
  53. static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
  54. /************************ Governor internals ***********************/
  55. static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
  56. {
  57. s64 delta_ns;
  58. /*
  59. * Since cpufreq_update_util() is called with rq->lock held for
  60. * the @target_cpu, our per-CPU data is fully serialized.
  61. *
  62. * However, drivers cannot in general deal with cross-CPU
  63. * requests, so while get_next_freq() will work, our
  64. * sugov_update_commit() call may not for the fast switching platforms.
  65. *
  66. * Hence stop here for remote requests if they aren't supported
  67. * by the hardware, as calculating the frequency is pointless if
  68. * we cannot in fact act on it.
  69. *
  70. * For the slow switching platforms, the kthread is always scheduled on
  71. * the right set of CPUs and any CPU can find the next frequency and
  72. * schedule the kthread.
  73. */
  74. if (sg_policy->policy->fast_switch_enabled &&
  75. !cpufreq_this_cpu_can_update(sg_policy->policy))
  76. return false;
  77. if (unlikely(sg_policy->need_freq_update))
  78. return true;
  79. delta_ns = time - sg_policy->last_freq_update_time;
  80. return delta_ns >= sg_policy->freq_update_delay_ns;
  81. }
  82. static bool sugov_update_next_freq(struct sugov_policy *sg_policy, u64 time,
  83. unsigned int next_freq)
  84. {
  85. if (sg_policy->next_freq == next_freq)
  86. return false;
  87. sg_policy->next_freq = next_freq;
  88. sg_policy->last_freq_update_time = time;
  89. return true;
  90. }
  91. static void sugov_fast_switch(struct sugov_policy *sg_policy, u64 time,
  92. unsigned int next_freq)
  93. {
  94. struct cpufreq_policy *policy = sg_policy->policy;
  95. if (!sugov_update_next_freq(sg_policy, time, next_freq))
  96. return;
  97. next_freq = cpufreq_driver_fast_switch(policy, next_freq);
  98. if (!next_freq)
  99. return;
  100. policy->cur = next_freq;
  101. trace_cpu_frequency(next_freq, smp_processor_id());
  102. }
  103. static void sugov_deferred_update(struct sugov_policy *sg_policy, u64 time,
  104. unsigned int next_freq)
  105. {
  106. if (!sugov_update_next_freq(sg_policy, time, next_freq))
  107. return;
  108. if (!sg_policy->work_in_progress) {
  109. sg_policy->work_in_progress = true;
  110. irq_work_queue(&sg_policy->irq_work);
  111. }
  112. }
  113. /**
  114. * get_next_freq - Compute a new frequency for a given cpufreq policy.
  115. * @sg_policy: schedutil policy object to compute the new frequency for.
  116. * @util: Current CPU utilization.
  117. * @max: CPU capacity.
  118. *
  119. * If the utilization is frequency-invariant, choose the new frequency to be
  120. * proportional to it, that is
  121. *
  122. * next_freq = C * max_freq * util / max
  123. *
  124. * Otherwise, approximate the would-be frequency-invariant utilization by
  125. * util_raw * (curr_freq / max_freq) which leads to
  126. *
  127. * next_freq = C * curr_freq * util_raw / max
  128. *
  129. * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
  130. *
  131. * The lowest driver-supported frequency which is equal or greater than the raw
  132. * next_freq (as calculated above) is returned, subject to policy min/max and
  133. * cpufreq driver limitations.
  134. */
  135. static unsigned int get_next_freq(struct sugov_policy *sg_policy,
  136. unsigned long util, unsigned long max)
  137. {
  138. struct cpufreq_policy *policy = sg_policy->policy;
  139. unsigned int freq = arch_scale_freq_invariant() ?
  140. policy->cpuinfo.max_freq : policy->cur;
  141. freq = (freq + (freq >> 2)) * util / max;
  142. if (freq == sg_policy->cached_raw_freq && !sg_policy->need_freq_update)
  143. return sg_policy->next_freq;
  144. sg_policy->need_freq_update = false;
  145. sg_policy->cached_raw_freq = freq;
  146. return cpufreq_driver_resolve_freq(policy, freq);
  147. }
  148. static void sugov_get_util(struct sugov_cpu *sg_cpu)
  149. {
  150. struct rq *rq = cpu_rq(sg_cpu->cpu);
  151. sg_cpu->max = arch_scale_cpu_capacity(NULL, sg_cpu->cpu);
  152. sg_cpu->util_cfs = cpu_util_cfs(rq);
  153. sg_cpu->util_dl = cpu_util_dl(rq);
  154. }
  155. static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
  156. {
  157. struct rq *rq = cpu_rq(sg_cpu->cpu);
  158. if (rq->rt.rt_nr_running)
  159. return sg_cpu->max;
  160. /*
  161. * Utilization required by DEADLINE must always be granted while, for
  162. * FAIR, we use blocked utilization of IDLE CPUs as a mechanism to
  163. * gracefully reduce the frequency when no tasks show up for longer
  164. * periods of time.
  165. *
  166. * Ideally we would like to set util_dl as min/guaranteed freq and
  167. * util_cfs + util_dl as requested freq. However, cpufreq is not yet
  168. * ready for such an interface. So, we only do the latter for now.
  169. */
  170. return min(sg_cpu->max, (sg_cpu->util_dl + sg_cpu->util_cfs));
  171. }
  172. /**
  173. * sugov_iowait_reset() - Reset the IO boost status of a CPU.
  174. * @sg_cpu: the sugov data for the CPU to boost
  175. * @time: the update time from the caller
  176. * @set_iowait_boost: true if an IO boost has been requested
  177. *
  178. * The IO wait boost of a task is disabled after a tick since the last update
  179. * of a CPU. If a new IO wait boost is requested after more then a tick, then
  180. * we enable the boost starting from the minimum frequency, which improves
  181. * energy efficiency by ignoring sporadic wakeups from IO.
  182. */
  183. static bool sugov_iowait_reset(struct sugov_cpu *sg_cpu, u64 time,
  184. bool set_iowait_boost)
  185. {
  186. s64 delta_ns = time - sg_cpu->last_update;
  187. /* Reset boost only if a tick has elapsed since last request */
  188. if (delta_ns <= TICK_NSEC)
  189. return false;
  190. sg_cpu->iowait_boost = set_iowait_boost
  191. ? sg_cpu->sg_policy->policy->min : 0;
  192. sg_cpu->iowait_boost_pending = set_iowait_boost;
  193. return true;
  194. }
  195. /**
  196. * sugov_iowait_boost() - Updates the IO boost status of a CPU.
  197. * @sg_cpu: the sugov data for the CPU to boost
  198. * @time: the update time from the caller
  199. * @flags: SCHED_CPUFREQ_IOWAIT if the task is waking up after an IO wait
  200. *
  201. * Each time a task wakes up after an IO operation, the CPU utilization can be
  202. * boosted to a certain utilization which doubles at each "frequent and
  203. * successive" wakeup from IO, ranging from the utilization of the minimum
  204. * OPP to the utilization of the maximum OPP.
  205. * To keep doubling, an IO boost has to be requested at least once per tick,
  206. * otherwise we restart from the utilization of the minimum OPP.
  207. */
  208. static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
  209. unsigned int flags)
  210. {
  211. bool set_iowait_boost = flags & SCHED_CPUFREQ_IOWAIT;
  212. /* Reset boost if the CPU appears to have been idle enough */
  213. if (sg_cpu->iowait_boost &&
  214. sugov_iowait_reset(sg_cpu, time, set_iowait_boost))
  215. return;
  216. /* Boost only tasks waking up after IO */
  217. if (!set_iowait_boost)
  218. return;
  219. /* Ensure boost doubles only one time at each request */
  220. if (sg_cpu->iowait_boost_pending)
  221. return;
  222. sg_cpu->iowait_boost_pending = true;
  223. /* Double the boost at each request */
  224. if (sg_cpu->iowait_boost) {
  225. sg_cpu->iowait_boost <<= 1;
  226. if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
  227. sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
  228. return;
  229. }
  230. /* First wakeup after IO: start with minimum boost */
  231. sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
  232. }
  233. /**
  234. * sugov_iowait_apply() - Apply the IO boost to a CPU.
  235. * @sg_cpu: the sugov data for the cpu to boost
  236. * @time: the update time from the caller
  237. * @util: the utilization to (eventually) boost
  238. * @max: the maximum value the utilization can be boosted to
  239. *
  240. * A CPU running a task which woken up after an IO operation can have its
  241. * utilization boosted to speed up the completion of those IO operations.
  242. * The IO boost value is increased each time a task wakes up from IO, in
  243. * sugov_iowait_apply(), and it's instead decreased by this function,
  244. * each time an increase has not been requested (!iowait_boost_pending).
  245. *
  246. * A CPU which also appears to have been idle for at least one tick has also
  247. * its IO boost utilization reset.
  248. *
  249. * This mechanism is designed to boost high frequently IO waiting tasks, while
  250. * being more conservative on tasks which does sporadic IO operations.
  251. */
  252. static void sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time,
  253. unsigned long *util, unsigned long *max)
  254. {
  255. unsigned int boost_util, boost_max;
  256. /* No boost currently required */
  257. if (!sg_cpu->iowait_boost)
  258. return;
  259. /* Reset boost if the CPU appears to have been idle enough */
  260. if (sugov_iowait_reset(sg_cpu, time, false))
  261. return;
  262. /*
  263. * An IO waiting task has just woken up:
  264. * allow to further double the boost value
  265. */
  266. if (sg_cpu->iowait_boost_pending) {
  267. sg_cpu->iowait_boost_pending = false;
  268. } else {
  269. /*
  270. * Otherwise: reduce the boost value and disable it when we
  271. * reach the minimum.
  272. */
  273. sg_cpu->iowait_boost >>= 1;
  274. if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) {
  275. sg_cpu->iowait_boost = 0;
  276. return;
  277. }
  278. }
  279. /*
  280. * Apply the current boost value: a CPU is boosted only if its current
  281. * utilization is smaller then the current IO boost level.
  282. */
  283. boost_util = sg_cpu->iowait_boost;
  284. boost_max = sg_cpu->iowait_boost_max;
  285. if (*util * boost_max < *max * boost_util) {
  286. *util = boost_util;
  287. *max = boost_max;
  288. }
  289. }
  290. #ifdef CONFIG_NO_HZ_COMMON
  291. static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
  292. {
  293. unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu);
  294. bool ret = idle_calls == sg_cpu->saved_idle_calls;
  295. sg_cpu->saved_idle_calls = idle_calls;
  296. return ret;
  297. }
  298. #else
  299. static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
  300. #endif /* CONFIG_NO_HZ_COMMON */
  301. /*
  302. * Make sugov_should_update_freq() ignore the rate limit when DL
  303. * has increased the utilization.
  304. */
  305. static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu, struct sugov_policy *sg_policy)
  306. {
  307. if (cpu_util_dl(cpu_rq(sg_cpu->cpu)) > sg_cpu->util_dl)
  308. sg_policy->need_freq_update = true;
  309. }
  310. static void sugov_update_single(struct update_util_data *hook, u64 time,
  311. unsigned int flags)
  312. {
  313. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  314. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  315. unsigned long util, max;
  316. unsigned int next_f;
  317. bool busy;
  318. sugov_iowait_boost(sg_cpu, time, flags);
  319. sg_cpu->last_update = time;
  320. ignore_dl_rate_limit(sg_cpu, sg_policy);
  321. if (!sugov_should_update_freq(sg_policy, time))
  322. return;
  323. busy = sugov_cpu_is_busy(sg_cpu);
  324. sugov_get_util(sg_cpu);
  325. max = sg_cpu->max;
  326. util = sugov_aggregate_util(sg_cpu);
  327. sugov_iowait_apply(sg_cpu, time, &util, &max);
  328. next_f = get_next_freq(sg_policy, util, max);
  329. /*
  330. * Do not reduce the frequency if the CPU has not been idle
  331. * recently, as the reduction is likely to be premature then.
  332. */
  333. if (busy && next_f < sg_policy->next_freq) {
  334. next_f = sg_policy->next_freq;
  335. /* Reset cached freq as next_freq has changed */
  336. sg_policy->cached_raw_freq = 0;
  337. }
  338. /*
  339. * This code runs under rq->lock for the target CPU, so it won't run
  340. * concurrently on two different CPUs for the same target and it is not
  341. * necessary to acquire the lock in the fast switch case.
  342. */
  343. if (sg_policy->policy->fast_switch_enabled) {
  344. sugov_fast_switch(sg_policy, time, next_f);
  345. } else {
  346. raw_spin_lock(&sg_policy->update_lock);
  347. sugov_deferred_update(sg_policy, time, next_f);
  348. raw_spin_unlock(&sg_policy->update_lock);
  349. }
  350. }
  351. static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
  352. {
  353. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  354. struct cpufreq_policy *policy = sg_policy->policy;
  355. unsigned long util = 0, max = 1;
  356. unsigned int j;
  357. for_each_cpu(j, policy->cpus) {
  358. struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
  359. unsigned long j_util, j_max;
  360. sugov_get_util(j_sg_cpu);
  361. j_max = j_sg_cpu->max;
  362. j_util = sugov_aggregate_util(j_sg_cpu);
  363. sugov_iowait_apply(j_sg_cpu, time, &j_util, &j_max);
  364. if (j_util * max > j_max * util) {
  365. util = j_util;
  366. max = j_max;
  367. }
  368. }
  369. return get_next_freq(sg_policy, util, max);
  370. }
  371. static void
  372. sugov_update_shared(struct update_util_data *hook, u64 time, unsigned int flags)
  373. {
  374. struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
  375. struct sugov_policy *sg_policy = sg_cpu->sg_policy;
  376. unsigned int next_f;
  377. raw_spin_lock(&sg_policy->update_lock);
  378. sugov_iowait_boost(sg_cpu, time, flags);
  379. sg_cpu->last_update = time;
  380. ignore_dl_rate_limit(sg_cpu, sg_policy);
  381. if (sugov_should_update_freq(sg_policy, time)) {
  382. next_f = sugov_next_freq_shared(sg_cpu, time);
  383. if (sg_policy->policy->fast_switch_enabled)
  384. sugov_fast_switch(sg_policy, time, next_f);
  385. else
  386. sugov_deferred_update(sg_policy, time, next_f);
  387. }
  388. raw_spin_unlock(&sg_policy->update_lock);
  389. }
  390. static void sugov_work(struct kthread_work *work)
  391. {
  392. struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
  393. unsigned int freq;
  394. unsigned long flags;
  395. /*
  396. * Hold sg_policy->update_lock shortly to handle the case where:
  397. * incase sg_policy->next_freq is read here, and then updated by
  398. * sugov_deferred_update() just before work_in_progress is set to false
  399. * here, we may miss queueing the new update.
  400. *
  401. * Note: If a work was queued after the update_lock is released,
  402. * sugov_work() will just be called again by kthread_work code; and the
  403. * request will be proceed before the sugov thread sleeps.
  404. */
  405. raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
  406. freq = sg_policy->next_freq;
  407. sg_policy->work_in_progress = false;
  408. raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
  409. mutex_lock(&sg_policy->work_lock);
  410. __cpufreq_driver_target(sg_policy->policy, freq, CPUFREQ_RELATION_L);
  411. mutex_unlock(&sg_policy->work_lock);
  412. }
  413. static void sugov_irq_work(struct irq_work *irq_work)
  414. {
  415. struct sugov_policy *sg_policy;
  416. sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
  417. kthread_queue_work(&sg_policy->worker, &sg_policy->work);
  418. }
  419. /************************** sysfs interface ************************/
  420. static struct sugov_tunables *global_tunables;
  421. static DEFINE_MUTEX(global_tunables_lock);
  422. static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
  423. {
  424. return container_of(attr_set, struct sugov_tunables, attr_set);
  425. }
  426. static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
  427. {
  428. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  429. return sprintf(buf, "%u\n", tunables->rate_limit_us);
  430. }
  431. static ssize_t
  432. rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf, size_t count)
  433. {
  434. struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
  435. struct sugov_policy *sg_policy;
  436. unsigned int rate_limit_us;
  437. if (kstrtouint(buf, 10, &rate_limit_us))
  438. return -EINVAL;
  439. tunables->rate_limit_us = rate_limit_us;
  440. list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
  441. sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
  442. return count;
  443. }
  444. static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
  445. static struct attribute *sugov_attributes[] = {
  446. &rate_limit_us.attr,
  447. NULL
  448. };
  449. static struct kobj_type sugov_tunables_ktype = {
  450. .default_attrs = sugov_attributes,
  451. .sysfs_ops = &governor_sysfs_ops,
  452. };
  453. /********************** cpufreq governor interface *********************/
  454. static struct cpufreq_governor schedutil_gov;
  455. static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
  456. {
  457. struct sugov_policy *sg_policy;
  458. sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
  459. if (!sg_policy)
  460. return NULL;
  461. sg_policy->policy = policy;
  462. raw_spin_lock_init(&sg_policy->update_lock);
  463. return sg_policy;
  464. }
  465. static void sugov_policy_free(struct sugov_policy *sg_policy)
  466. {
  467. kfree(sg_policy);
  468. }
  469. static int sugov_kthread_create(struct sugov_policy *sg_policy)
  470. {
  471. struct task_struct *thread;
  472. struct sched_attr attr = {
  473. .size = sizeof(struct sched_attr),
  474. .sched_policy = SCHED_DEADLINE,
  475. .sched_flags = SCHED_FLAG_SUGOV,
  476. .sched_nice = 0,
  477. .sched_priority = 0,
  478. /*
  479. * Fake (unused) bandwidth; workaround to "fix"
  480. * priority inheritance.
  481. */
  482. .sched_runtime = 1000000,
  483. .sched_deadline = 10000000,
  484. .sched_period = 10000000,
  485. };
  486. struct cpufreq_policy *policy = sg_policy->policy;
  487. int ret;
  488. /* kthread only required for slow path */
  489. if (policy->fast_switch_enabled)
  490. return 0;
  491. kthread_init_work(&sg_policy->work, sugov_work);
  492. kthread_init_worker(&sg_policy->worker);
  493. thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
  494. "sugov:%d",
  495. cpumask_first(policy->related_cpus));
  496. if (IS_ERR(thread)) {
  497. pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
  498. return PTR_ERR(thread);
  499. }
  500. ret = sched_setattr_nocheck(thread, &attr);
  501. if (ret) {
  502. kthread_stop(thread);
  503. pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__);
  504. return ret;
  505. }
  506. sg_policy->thread = thread;
  507. kthread_bind_mask(thread, policy->related_cpus);
  508. init_irq_work(&sg_policy->irq_work, sugov_irq_work);
  509. mutex_init(&sg_policy->work_lock);
  510. wake_up_process(thread);
  511. return 0;
  512. }
  513. static void sugov_kthread_stop(struct sugov_policy *sg_policy)
  514. {
  515. /* kthread only required for slow path */
  516. if (sg_policy->policy->fast_switch_enabled)
  517. return;
  518. kthread_flush_worker(&sg_policy->worker);
  519. kthread_stop(sg_policy->thread);
  520. mutex_destroy(&sg_policy->work_lock);
  521. }
  522. static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
  523. {
  524. struct sugov_tunables *tunables;
  525. tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
  526. if (tunables) {
  527. gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
  528. if (!have_governor_per_policy())
  529. global_tunables = tunables;
  530. }
  531. return tunables;
  532. }
  533. static void sugov_tunables_free(struct sugov_tunables *tunables)
  534. {
  535. if (!have_governor_per_policy())
  536. global_tunables = NULL;
  537. kfree(tunables);
  538. }
  539. static int sugov_init(struct cpufreq_policy *policy)
  540. {
  541. struct sugov_policy *sg_policy;
  542. struct sugov_tunables *tunables;
  543. int ret = 0;
  544. /* State should be equivalent to EXIT */
  545. if (policy->governor_data)
  546. return -EBUSY;
  547. cpufreq_enable_fast_switch(policy);
  548. sg_policy = sugov_policy_alloc(policy);
  549. if (!sg_policy) {
  550. ret = -ENOMEM;
  551. goto disable_fast_switch;
  552. }
  553. ret = sugov_kthread_create(sg_policy);
  554. if (ret)
  555. goto free_sg_policy;
  556. mutex_lock(&global_tunables_lock);
  557. if (global_tunables) {
  558. if (WARN_ON(have_governor_per_policy())) {
  559. ret = -EINVAL;
  560. goto stop_kthread;
  561. }
  562. policy->governor_data = sg_policy;
  563. sg_policy->tunables = global_tunables;
  564. gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
  565. goto out;
  566. }
  567. tunables = sugov_tunables_alloc(sg_policy);
  568. if (!tunables) {
  569. ret = -ENOMEM;
  570. goto stop_kthread;
  571. }
  572. tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy);
  573. policy->governor_data = sg_policy;
  574. sg_policy->tunables = tunables;
  575. ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
  576. get_governor_parent_kobj(policy), "%s",
  577. schedutil_gov.name);
  578. if (ret)
  579. goto fail;
  580. out:
  581. mutex_unlock(&global_tunables_lock);
  582. return 0;
  583. fail:
  584. policy->governor_data = NULL;
  585. sugov_tunables_free(tunables);
  586. stop_kthread:
  587. sugov_kthread_stop(sg_policy);
  588. mutex_unlock(&global_tunables_lock);
  589. free_sg_policy:
  590. sugov_policy_free(sg_policy);
  591. disable_fast_switch:
  592. cpufreq_disable_fast_switch(policy);
  593. pr_err("initialization failed (error %d)\n", ret);
  594. return ret;
  595. }
  596. static void sugov_exit(struct cpufreq_policy *policy)
  597. {
  598. struct sugov_policy *sg_policy = policy->governor_data;
  599. struct sugov_tunables *tunables = sg_policy->tunables;
  600. unsigned int count;
  601. mutex_lock(&global_tunables_lock);
  602. count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
  603. policy->governor_data = NULL;
  604. if (!count)
  605. sugov_tunables_free(tunables);
  606. mutex_unlock(&global_tunables_lock);
  607. sugov_kthread_stop(sg_policy);
  608. sugov_policy_free(sg_policy);
  609. cpufreq_disable_fast_switch(policy);
  610. }
  611. static int sugov_start(struct cpufreq_policy *policy)
  612. {
  613. struct sugov_policy *sg_policy = policy->governor_data;
  614. unsigned int cpu;
  615. sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
  616. sg_policy->last_freq_update_time = 0;
  617. sg_policy->next_freq = 0;
  618. sg_policy->work_in_progress = false;
  619. sg_policy->need_freq_update = false;
  620. sg_policy->cached_raw_freq = 0;
  621. for_each_cpu(cpu, policy->cpus) {
  622. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  623. memset(sg_cpu, 0, sizeof(*sg_cpu));
  624. sg_cpu->cpu = cpu;
  625. sg_cpu->sg_policy = sg_policy;
  626. sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
  627. }
  628. for_each_cpu(cpu, policy->cpus) {
  629. struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
  630. cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
  631. policy_is_shared(policy) ?
  632. sugov_update_shared :
  633. sugov_update_single);
  634. }
  635. return 0;
  636. }
  637. static void sugov_stop(struct cpufreq_policy *policy)
  638. {
  639. struct sugov_policy *sg_policy = policy->governor_data;
  640. unsigned int cpu;
  641. for_each_cpu(cpu, policy->cpus)
  642. cpufreq_remove_update_util_hook(cpu);
  643. synchronize_sched();
  644. if (!policy->fast_switch_enabled) {
  645. irq_work_sync(&sg_policy->irq_work);
  646. kthread_cancel_work_sync(&sg_policy->work);
  647. }
  648. }
  649. static void sugov_limits(struct cpufreq_policy *policy)
  650. {
  651. struct sugov_policy *sg_policy = policy->governor_data;
  652. if (!policy->fast_switch_enabled) {
  653. mutex_lock(&sg_policy->work_lock);
  654. cpufreq_policy_apply_limits(policy);
  655. mutex_unlock(&sg_policy->work_lock);
  656. }
  657. sg_policy->need_freq_update = true;
  658. }
  659. static struct cpufreq_governor schedutil_gov = {
  660. .name = "schedutil",
  661. .owner = THIS_MODULE,
  662. .dynamic_switching = true,
  663. .init = sugov_init,
  664. .exit = sugov_exit,
  665. .start = sugov_start,
  666. .stop = sugov_stop,
  667. .limits = sugov_limits,
  668. };
  669. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
  670. struct cpufreq_governor *cpufreq_default_governor(void)
  671. {
  672. return &schedutil_gov;
  673. }
  674. #endif
  675. static int __init sugov_register(void)
  676. {
  677. return cpufreq_register_governor(&schedutil_gov);
  678. }
  679. fs_initcall(sugov_register);