idle_inject.c 10 KB

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