idle_inject.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. *
  7. * The idle injection framework provides a way to force CPUs to enter idle
  8. * states for a specified fraction of time over a specified period.
  9. *
  10. * It relies on the smpboot kthreads feature providing common code for CPU
  11. * hotplug and thread [un]parking.
  12. *
  13. * All of the kthreads used for idle injection are created at init time.
  14. *
  15. * Next, the users of the idle injection framework provide a cpumask via
  16. * its register function. The kthreads will be synchronized with respect to
  17. * this cpumask.
  18. *
  19. * The idle + run duration is specified via separate helpers and that allows
  20. * idle injection to be started.
  21. *
  22. * The idle injection kthreads will call play_idle_precise() with the idle
  23. * duration and max allowed latency specified as per the above.
  24. *
  25. * After all of them have been woken up, a timer is set to start the next idle
  26. * injection cycle.
  27. *
  28. * The timer interrupt handler will wake up the idle injection kthreads for
  29. * all of the CPUs in the cpumask provided by the user.
  30. *
  31. * Idle injection is stopped synchronously and no leftover idle injection
  32. * kthread activity after its completion is guaranteed.
  33. *
  34. * It is up to the user of this framework to provide a lock for higher-level
  35. * synchronization to prevent race conditions like starting idle injection
  36. * while unregistering from the framework.
  37. */
  38. #define pr_fmt(fmt) "ii_dev: " fmt
  39. #include <linux/cpu.h>
  40. #include <linux/hrtimer.h>
  41. #include <linux/kthread.h>
  42. #include <linux/sched.h>
  43. #include <linux/slab.h>
  44. #include <linux/smpboot.h>
  45. #include <linux/idle_inject.h>
  46. #include <uapi/linux/sched/types.h>
  47. /**
  48. * struct idle_inject_thread - task on/off switch structure
  49. * @tsk: task injecting the idle cycles
  50. * @should_run: whether or not to run the task (for the smpboot kthread API)
  51. */
  52. struct idle_inject_thread {
  53. struct task_struct *tsk;
  54. int should_run;
  55. };
  56. /**
  57. * struct idle_inject_device - idle injection data
  58. * @timer: idle injection period timer
  59. * @idle_duration_us: duration of CPU idle time to inject
  60. * @run_duration_us: duration of CPU run time to allow
  61. * @latency_us: max allowed latency
  62. * @update: Optional callback deciding whether or not to skip idle
  63. * injection in the given cycle.
  64. * @cpumask: mask of CPUs affected by idle injection
  65. *
  66. * This structure is used to define per instance idle inject device data. Each
  67. * instance has an idle duration, a run duration and mask of CPUs to inject
  68. * idle.
  69. *
  70. * Actual CPU idle time is injected by calling kernel scheduler interface
  71. * play_idle_precise(). There is one optional callback that can be registered
  72. * by calling idle_inject_register_full():
  73. *
  74. * update() - This callback is invoked just before waking up CPUs to inject
  75. * idle. If it returns false, CPUs are not woken up to inject idle in the given
  76. * cycle. It also allows the caller to readjust the idle and run duration by
  77. * calling idle_inject_set_duration() for the next cycle.
  78. */
  79. struct idle_inject_device {
  80. struct hrtimer timer;
  81. unsigned int idle_duration_us;
  82. unsigned int run_duration_us;
  83. unsigned int latency_us;
  84. bool (*update)(void);
  85. unsigned long cpumask[];
  86. };
  87. static DEFINE_PER_CPU(struct idle_inject_thread, idle_inject_thread);
  88. static DEFINE_PER_CPU(struct idle_inject_device *, idle_inject_device);
  89. /**
  90. * idle_inject_wakeup - Wake up idle injection threads
  91. * @ii_dev: target idle injection device
  92. *
  93. * Every idle injection task associated with the given idle injection device
  94. * and running on an online CPU will be woken up.
  95. */
  96. static void idle_inject_wakeup(struct idle_inject_device *ii_dev)
  97. {
  98. struct idle_inject_thread *iit;
  99. unsigned int cpu;
  100. for_each_cpu_and(cpu, to_cpumask(ii_dev->cpumask), cpu_online_mask) {
  101. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  102. iit->should_run = 1;
  103. wake_up_process(iit->tsk);
  104. }
  105. }
  106. /**
  107. * idle_inject_timer_fn - idle injection timer function
  108. * @timer: idle injection hrtimer
  109. *
  110. * This function is called when the idle injection timer expires. It wakes up
  111. * idle injection tasks associated with the timer and they, in turn, invoke
  112. * play_idle_precise() to inject a specified amount of CPU idle time.
  113. *
  114. * Return: HRTIMER_RESTART.
  115. */
  116. static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
  117. {
  118. unsigned int duration_us;
  119. struct idle_inject_device *ii_dev =
  120. container_of(timer, struct idle_inject_device, timer);
  121. if (!ii_dev->update || ii_dev->update())
  122. idle_inject_wakeup(ii_dev);
  123. duration_us = READ_ONCE(ii_dev->run_duration_us);
  124. duration_us += READ_ONCE(ii_dev->idle_duration_us);
  125. hrtimer_forward_now(timer, us_to_ktime(duration_us));
  126. return HRTIMER_RESTART;
  127. }
  128. /**
  129. * idle_inject_fn - idle injection work function
  130. * @cpu: the CPU owning the task
  131. *
  132. * This function calls play_idle_precise() to inject a specified amount of CPU
  133. * idle time.
  134. */
  135. static void idle_inject_fn(unsigned int cpu)
  136. {
  137. struct idle_inject_device *ii_dev;
  138. struct idle_inject_thread *iit;
  139. ii_dev = per_cpu(idle_inject_device, cpu);
  140. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  141. /*
  142. * Let the smpboot main loop know that the task should not run again.
  143. */
  144. iit->should_run = 0;
  145. play_idle_precise(READ_ONCE(ii_dev->idle_duration_us) * NSEC_PER_USEC,
  146. READ_ONCE(ii_dev->latency_us) * NSEC_PER_USEC);
  147. }
  148. /**
  149. * idle_inject_set_duration - idle and run duration update helper
  150. * @ii_dev: idle injection control device structure
  151. * @run_duration_us: CPU run time to allow in microseconds
  152. * @idle_duration_us: CPU idle time to inject in microseconds
  153. */
  154. void idle_inject_set_duration(struct idle_inject_device *ii_dev,
  155. unsigned int run_duration_us,
  156. unsigned int idle_duration_us)
  157. {
  158. if (run_duration_us + idle_duration_us) {
  159. WRITE_ONCE(ii_dev->run_duration_us, run_duration_us);
  160. WRITE_ONCE(ii_dev->idle_duration_us, idle_duration_us);
  161. }
  162. if (!run_duration_us)
  163. pr_debug("CPU is forced to 100 percent idle\n");
  164. }
  165. EXPORT_SYMBOL_NS_GPL(idle_inject_set_duration, "IDLE_INJECT");
  166. /**
  167. * idle_inject_get_duration - idle and run duration retrieval helper
  168. * @ii_dev: idle injection control device structure
  169. * @run_duration_us: memory location to store the current CPU run time
  170. * @idle_duration_us: memory location to store the current CPU idle time
  171. */
  172. void idle_inject_get_duration(struct idle_inject_device *ii_dev,
  173. unsigned int *run_duration_us,
  174. unsigned int *idle_duration_us)
  175. {
  176. *run_duration_us = READ_ONCE(ii_dev->run_duration_us);
  177. *idle_duration_us = READ_ONCE(ii_dev->idle_duration_us);
  178. }
  179. EXPORT_SYMBOL_NS_GPL(idle_inject_get_duration, "IDLE_INJECT");
  180. /**
  181. * idle_inject_set_latency - set the maximum latency allowed
  182. * @ii_dev: idle injection control device structure
  183. * @latency_us: set the latency requirement for the idle state
  184. */
  185. void idle_inject_set_latency(struct idle_inject_device *ii_dev,
  186. unsigned int latency_us)
  187. {
  188. WRITE_ONCE(ii_dev->latency_us, latency_us);
  189. }
  190. EXPORT_SYMBOL_NS_GPL(idle_inject_set_latency, "IDLE_INJECT");
  191. /**
  192. * idle_inject_start - start idle injections
  193. * @ii_dev: idle injection control device structure
  194. *
  195. * The function starts idle injection by first waking up all of the idle
  196. * injection kthreads associated with @ii_dev to let them inject CPU idle time
  197. * sets up a timer to start the next idle injection period.
  198. *
  199. * Return: -EINVAL if the CPU idle or CPU run time is not set or 0 on success.
  200. */
  201. int idle_inject_start(struct idle_inject_device *ii_dev)
  202. {
  203. unsigned int idle_duration_us = READ_ONCE(ii_dev->idle_duration_us);
  204. unsigned int run_duration_us = READ_ONCE(ii_dev->run_duration_us);
  205. if (!(idle_duration_us + run_duration_us))
  206. return -EINVAL;
  207. pr_debug("Starting injecting idle cycles on CPUs '%*pbl'\n",
  208. cpumask_pr_args(to_cpumask(ii_dev->cpumask)));
  209. idle_inject_wakeup(ii_dev);
  210. hrtimer_start(&ii_dev->timer,
  211. us_to_ktime(idle_duration_us + run_duration_us),
  212. HRTIMER_MODE_REL);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_NS_GPL(idle_inject_start, "IDLE_INJECT");
  216. /**
  217. * idle_inject_stop - stops idle injections
  218. * @ii_dev: idle injection control device structure
  219. *
  220. * The function stops idle injection and waits for the threads to finish work.
  221. * If CPU idle time is being injected when this function runs, then it will
  222. * wait until the end of the cycle.
  223. *
  224. * When it returns, there is no more idle injection kthread activity. The
  225. * kthreads are scheduled out and the periodic timer is off.
  226. */
  227. void idle_inject_stop(struct idle_inject_device *ii_dev)
  228. {
  229. struct idle_inject_thread *iit;
  230. unsigned int cpu;
  231. pr_debug("Stopping idle injection on CPUs '%*pbl'\n",
  232. cpumask_pr_args(to_cpumask(ii_dev->cpumask)));
  233. hrtimer_cancel(&ii_dev->timer);
  234. /*
  235. * Stopping idle injection requires all of the idle injection kthreads
  236. * associated with the given cpumask to be parked and stay that way, so
  237. * prevent CPUs from going online at this point. Any CPUs going online
  238. * after the loop below will be covered by clearing the should_run flag
  239. * that will cause the smpboot main loop to schedule them out.
  240. */
  241. cpu_hotplug_disable();
  242. /*
  243. * Iterate over all (online + offline) CPUs here in case one of them
  244. * goes offline with the should_run flag set so as to prevent its idle
  245. * injection kthread from running when the CPU goes online again after
  246. * the ii_dev has been freed.
  247. */
  248. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask)) {
  249. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  250. iit->should_run = 0;
  251. wait_task_inactive(iit->tsk, TASK_ANY);
  252. }
  253. cpu_hotplug_enable();
  254. }
  255. EXPORT_SYMBOL_NS_GPL(idle_inject_stop, "IDLE_INJECT");
  256. /**
  257. * idle_inject_setup - prepare the current task for idle injection
  258. * @cpu: not used
  259. *
  260. * Called once, this function is in charge of setting the current task's
  261. * scheduler parameters to make it an RT task.
  262. */
  263. static void idle_inject_setup(unsigned int cpu)
  264. {
  265. sched_set_fifo(current);
  266. }
  267. /**
  268. * idle_inject_should_run - function helper for the smpboot API
  269. * @cpu: CPU the kthread is running on
  270. *
  271. * Return: whether or not the thread can run.
  272. */
  273. static int idle_inject_should_run(unsigned int cpu)
  274. {
  275. struct idle_inject_thread *iit =
  276. per_cpu_ptr(&idle_inject_thread, cpu);
  277. return iit->should_run;
  278. }
  279. /**
  280. * idle_inject_register_full - initialize idle injection on a set of CPUs
  281. * @cpumask: CPUs to be affected by idle injection
  282. * @update: This callback is called just before waking up CPUs to inject
  283. * idle
  284. *
  285. * This function creates an idle injection control device structure for the
  286. * given set of CPUs and initializes the timer associated with it. This
  287. * function also allows to register update()callback.
  288. * It does not start any injection cycles.
  289. *
  290. * Return: NULL if memory allocation fails, idle injection control device
  291. * pointer on success.
  292. */
  293. struct idle_inject_device *idle_inject_register_full(struct cpumask *cpumask,
  294. bool (*update)(void))
  295. {
  296. struct idle_inject_device *ii_dev;
  297. int cpu, cpu_rb;
  298. ii_dev = kzalloc(sizeof(*ii_dev) + cpumask_size(), GFP_KERNEL);
  299. if (!ii_dev)
  300. return NULL;
  301. cpumask_copy(to_cpumask(ii_dev->cpumask), cpumask);
  302. hrtimer_setup(&ii_dev->timer, idle_inject_timer_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  303. ii_dev->latency_us = UINT_MAX;
  304. ii_dev->update = update;
  305. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask)) {
  306. if (per_cpu(idle_inject_device, cpu)) {
  307. pr_err("cpu%d is already registered\n", cpu);
  308. goto out_rollback;
  309. }
  310. per_cpu(idle_inject_device, cpu) = ii_dev;
  311. }
  312. return ii_dev;
  313. out_rollback:
  314. for_each_cpu(cpu_rb, to_cpumask(ii_dev->cpumask)) {
  315. if (cpu == cpu_rb)
  316. break;
  317. per_cpu(idle_inject_device, cpu_rb) = NULL;
  318. }
  319. kfree(ii_dev);
  320. return NULL;
  321. }
  322. EXPORT_SYMBOL_NS_GPL(idle_inject_register_full, "IDLE_INJECT");
  323. /**
  324. * idle_inject_register - initialize idle injection on a set of CPUs
  325. * @cpumask: CPUs to be affected by idle injection
  326. *
  327. * This function creates an idle injection control device structure for the
  328. * given set of CPUs and initializes the timer associated with it. It does not
  329. * start any injection cycles.
  330. *
  331. * Return: NULL if memory allocation fails, idle injection control device
  332. * pointer on success.
  333. */
  334. struct idle_inject_device *idle_inject_register(struct cpumask *cpumask)
  335. {
  336. return idle_inject_register_full(cpumask, NULL);
  337. }
  338. EXPORT_SYMBOL_NS_GPL(idle_inject_register, "IDLE_INJECT");
  339. /**
  340. * idle_inject_unregister - unregister idle injection control device
  341. * @ii_dev: idle injection control device to unregister
  342. *
  343. * The function stops idle injection for the given control device,
  344. * unregisters its kthreads and frees memory allocated when that device was
  345. * created.
  346. */
  347. void idle_inject_unregister(struct idle_inject_device *ii_dev)
  348. {
  349. unsigned int cpu;
  350. idle_inject_stop(ii_dev);
  351. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask))
  352. per_cpu(idle_inject_device, cpu) = NULL;
  353. kfree(ii_dev);
  354. }
  355. EXPORT_SYMBOL_NS_GPL(idle_inject_unregister, "IDLE_INJECT");
  356. static struct smp_hotplug_thread idle_inject_threads = {
  357. .store = &idle_inject_thread.tsk,
  358. .setup = idle_inject_setup,
  359. .thread_fn = idle_inject_fn,
  360. .thread_comm = "idle_inject/%u",
  361. .thread_should_run = idle_inject_should_run,
  362. };
  363. static int __init idle_inject_init(void)
  364. {
  365. return smpboot_register_percpu_thread(&idle_inject_threads);
  366. }
  367. early_initcall(idle_inject_init);