idle_inject.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <[email protected]>
  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. * @cpumask: mask of CPUs affected by idle injection
  63. */
  64. struct idle_inject_device {
  65. struct hrtimer timer;
  66. unsigned int idle_duration_us;
  67. unsigned int run_duration_us;
  68. unsigned int latency_us;
  69. unsigned long cpumask[];
  70. };
  71. static DEFINE_PER_CPU(struct idle_inject_thread, idle_inject_thread);
  72. static DEFINE_PER_CPU(struct idle_inject_device *, idle_inject_device);
  73. /**
  74. * idle_inject_wakeup - Wake up idle injection threads
  75. * @ii_dev: target idle injection device
  76. *
  77. * Every idle injection task associated with the given idle injection device
  78. * and running on an online CPU will be woken up.
  79. */
  80. static void idle_inject_wakeup(struct idle_inject_device *ii_dev)
  81. {
  82. struct idle_inject_thread *iit;
  83. unsigned int cpu;
  84. for_each_cpu_and(cpu, to_cpumask(ii_dev->cpumask), cpu_online_mask) {
  85. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  86. iit->should_run = 1;
  87. wake_up_process(iit->tsk);
  88. }
  89. }
  90. /**
  91. * idle_inject_timer_fn - idle injection timer function
  92. * @timer: idle injection hrtimer
  93. *
  94. * This function is called when the idle injection timer expires. It wakes up
  95. * idle injection tasks associated with the timer and they, in turn, invoke
  96. * play_idle_precise() to inject a specified amount of CPU idle time.
  97. *
  98. * Return: HRTIMER_RESTART.
  99. */
  100. static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
  101. {
  102. unsigned int duration_us;
  103. struct idle_inject_device *ii_dev =
  104. container_of(timer, struct idle_inject_device, timer);
  105. duration_us = READ_ONCE(ii_dev->run_duration_us);
  106. duration_us += READ_ONCE(ii_dev->idle_duration_us);
  107. idle_inject_wakeup(ii_dev);
  108. hrtimer_forward_now(timer, ns_to_ktime(duration_us * NSEC_PER_USEC));
  109. return HRTIMER_RESTART;
  110. }
  111. /**
  112. * idle_inject_fn - idle injection work function
  113. * @cpu: the CPU owning the task
  114. *
  115. * This function calls play_idle_precise() to inject a specified amount of CPU
  116. * idle time.
  117. */
  118. static void idle_inject_fn(unsigned int cpu)
  119. {
  120. struct idle_inject_device *ii_dev;
  121. struct idle_inject_thread *iit;
  122. ii_dev = per_cpu(idle_inject_device, cpu);
  123. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  124. /*
  125. * Let the smpboot main loop know that the task should not run again.
  126. */
  127. iit->should_run = 0;
  128. play_idle_precise(READ_ONCE(ii_dev->idle_duration_us) * NSEC_PER_USEC,
  129. READ_ONCE(ii_dev->latency_us) * NSEC_PER_USEC);
  130. }
  131. /**
  132. * idle_inject_set_duration - idle and run duration update helper
  133. * @run_duration_us: CPU run time to allow in microseconds
  134. * @idle_duration_us: CPU idle time to inject in microseconds
  135. */
  136. void idle_inject_set_duration(struct idle_inject_device *ii_dev,
  137. unsigned int run_duration_us,
  138. unsigned int idle_duration_us)
  139. {
  140. if (run_duration_us && idle_duration_us) {
  141. WRITE_ONCE(ii_dev->run_duration_us, run_duration_us);
  142. WRITE_ONCE(ii_dev->idle_duration_us, idle_duration_us);
  143. }
  144. }
  145. EXPORT_SYMBOL_GPL(idle_inject_set_duration);
  146. /**
  147. * idle_inject_get_duration - idle and run duration retrieval helper
  148. * @run_duration_us: memory location to store the current CPU run time
  149. * @idle_duration_us: memory location to store the current CPU idle time
  150. */
  151. void idle_inject_get_duration(struct idle_inject_device *ii_dev,
  152. unsigned int *run_duration_us,
  153. unsigned int *idle_duration_us)
  154. {
  155. *run_duration_us = READ_ONCE(ii_dev->run_duration_us);
  156. *idle_duration_us = READ_ONCE(ii_dev->idle_duration_us);
  157. }
  158. EXPORT_SYMBOL_GPL(idle_inject_get_duration);
  159. /**
  160. * idle_inject_set_latency - set the maximum latency allowed
  161. * @latency_us: set the latency requirement for the idle state
  162. */
  163. void idle_inject_set_latency(struct idle_inject_device *ii_dev,
  164. unsigned int latency_us)
  165. {
  166. WRITE_ONCE(ii_dev->latency_us, latency_us);
  167. }
  168. EXPORT_SYMBOL_GPL(idle_inject_set_latency);
  169. /**
  170. * idle_inject_start - start idle injections
  171. * @ii_dev: idle injection control device structure
  172. *
  173. * The function starts idle injection by first waking up all of the idle
  174. * injection kthreads associated with @ii_dev to let them inject CPU idle time
  175. * sets up a timer to start the next idle injection period.
  176. *
  177. * Return: -EINVAL if the CPU idle or CPU run time is not set or 0 on success.
  178. */
  179. int idle_inject_start(struct idle_inject_device *ii_dev)
  180. {
  181. unsigned int idle_duration_us = READ_ONCE(ii_dev->idle_duration_us);
  182. unsigned int run_duration_us = READ_ONCE(ii_dev->run_duration_us);
  183. if (!idle_duration_us || !run_duration_us)
  184. return -EINVAL;
  185. pr_debug("Starting injecting idle cycles on CPUs '%*pbl'\n",
  186. cpumask_pr_args(to_cpumask(ii_dev->cpumask)));
  187. idle_inject_wakeup(ii_dev);
  188. hrtimer_start(&ii_dev->timer,
  189. ns_to_ktime((idle_duration_us + run_duration_us) *
  190. NSEC_PER_USEC),
  191. HRTIMER_MODE_REL);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(idle_inject_start);
  195. /**
  196. * idle_inject_stop - stops idle injections
  197. * @ii_dev: idle injection control device structure
  198. *
  199. * The function stops idle injection and waits for the threads to finish work.
  200. * If CPU idle time is being injected when this function runs, then it will
  201. * wait until the end of the cycle.
  202. *
  203. * When it returns, there is no more idle injection kthread activity. The
  204. * kthreads are scheduled out and the periodic timer is off.
  205. */
  206. void idle_inject_stop(struct idle_inject_device *ii_dev)
  207. {
  208. struct idle_inject_thread *iit;
  209. unsigned int cpu;
  210. pr_debug("Stopping idle injection on CPUs '%*pbl'\n",
  211. cpumask_pr_args(to_cpumask(ii_dev->cpumask)));
  212. hrtimer_cancel(&ii_dev->timer);
  213. /*
  214. * Stopping idle injection requires all of the idle injection kthreads
  215. * associated with the given cpumask to be parked and stay that way, so
  216. * prevent CPUs from going online at this point. Any CPUs going online
  217. * after the loop below will be covered by clearing the should_run flag
  218. * that will cause the smpboot main loop to schedule them out.
  219. */
  220. cpu_hotplug_disable();
  221. /*
  222. * Iterate over all (online + offline) CPUs here in case one of them
  223. * goes offline with the should_run flag set so as to prevent its idle
  224. * injection kthread from running when the CPU goes online again after
  225. * the ii_dev has been freed.
  226. */
  227. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask)) {
  228. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  229. iit->should_run = 0;
  230. wait_task_inactive(iit->tsk, TASK_ANY);
  231. }
  232. cpu_hotplug_enable();
  233. }
  234. EXPORT_SYMBOL_GPL(idle_inject_stop);
  235. /**
  236. * idle_inject_setup - prepare the current task for idle injection
  237. * @cpu: not used
  238. *
  239. * Called once, this function is in charge of setting the current task's
  240. * scheduler parameters to make it an RT task.
  241. */
  242. static void idle_inject_setup(unsigned int cpu)
  243. {
  244. sched_set_fifo(current);
  245. }
  246. /**
  247. * idle_inject_should_run - function helper for the smpboot API
  248. * @cpu: CPU the kthread is running on
  249. *
  250. * Return: whether or not the thread can run.
  251. */
  252. static int idle_inject_should_run(unsigned int cpu)
  253. {
  254. struct idle_inject_thread *iit =
  255. per_cpu_ptr(&idle_inject_thread, cpu);
  256. return iit->should_run;
  257. }
  258. /**
  259. * idle_inject_register - initialize idle injection on a set of CPUs
  260. * @cpumask: CPUs to be affected by idle injection
  261. *
  262. * This function creates an idle injection control device structure for the
  263. * given set of CPUs and initializes the timer associated with it. It does not
  264. * start any injection cycles.
  265. *
  266. * Return: NULL if memory allocation fails, idle injection control device
  267. * pointer on success.
  268. */
  269. struct idle_inject_device *idle_inject_register(struct cpumask *cpumask)
  270. {
  271. struct idle_inject_device *ii_dev;
  272. int cpu, cpu_rb;
  273. ii_dev = kzalloc(sizeof(*ii_dev) + cpumask_size(), GFP_KERNEL);
  274. if (!ii_dev)
  275. return NULL;
  276. cpumask_copy(to_cpumask(ii_dev->cpumask), cpumask);
  277. hrtimer_init(&ii_dev->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  278. ii_dev->timer.function = idle_inject_timer_fn;
  279. ii_dev->latency_us = UINT_MAX;
  280. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask)) {
  281. if (per_cpu(idle_inject_device, cpu)) {
  282. pr_err("cpu%d is already registered\n", cpu);
  283. goto out_rollback;
  284. }
  285. per_cpu(idle_inject_device, cpu) = ii_dev;
  286. }
  287. return ii_dev;
  288. out_rollback:
  289. for_each_cpu(cpu_rb, to_cpumask(ii_dev->cpumask)) {
  290. if (cpu == cpu_rb)
  291. break;
  292. per_cpu(idle_inject_device, cpu_rb) = NULL;
  293. }
  294. kfree(ii_dev);
  295. return NULL;
  296. }
  297. EXPORT_SYMBOL_GPL(idle_inject_register);
  298. /**
  299. * idle_inject_unregister - unregister idle injection control device
  300. * @ii_dev: idle injection control device to unregister
  301. *
  302. * The function stops idle injection for the given control device,
  303. * unregisters its kthreads and frees memory allocated when that device was
  304. * created.
  305. */
  306. void idle_inject_unregister(struct idle_inject_device *ii_dev)
  307. {
  308. unsigned int cpu;
  309. idle_inject_stop(ii_dev);
  310. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask))
  311. per_cpu(idle_inject_device, cpu) = NULL;
  312. kfree(ii_dev);
  313. }
  314. EXPORT_SYMBOL_GPL(idle_inject_unregister);
  315. static struct smp_hotplug_thread idle_inject_threads = {
  316. .store = &idle_inject_thread.tsk,
  317. .setup = idle_inject_setup,
  318. .thread_fn = idle_inject_fn,
  319. .thread_comm = "idle_inject/%u",
  320. .thread_should_run = idle_inject_should_run,
  321. };
  322. static int __init idle_inject_init(void)
  323. {
  324. return smpboot_register_percpu_thread(&idle_inject_threads);
  325. }
  326. early_initcall(idle_inject_init);