PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/powercap/idle_inject.c

https://github.com/kvaneesh/linux
C | 370 lines | 162 code | 51 blank | 157 comment | 8 complexity | 985df71940e1a0e8031f69f7cd7909e3 MD5 | raw file
  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_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. /**
  146. * idle_inject_get_duration - idle and run duration retrieval helper
  147. * @run_duration_us: memory location to store the current CPU run time
  148. * @idle_duration_us: memory location to store the current CPU idle time
  149. */
  150. void idle_inject_get_duration(struct idle_inject_device *ii_dev,
  151. unsigned int *run_duration_us,
  152. unsigned int *idle_duration_us)
  153. {
  154. *run_duration_us = READ_ONCE(ii_dev->run_duration_us);
  155. *idle_duration_us = READ_ONCE(ii_dev->idle_duration_us);
  156. }
  157. /**
  158. * idle_inject_set_latency - set the maximum latency allowed
  159. * @latency_us: set the latency requirement for the idle state
  160. */
  161. void idle_inject_set_latency(struct idle_inject_device *ii_dev,
  162. unsigned int latency_us)
  163. {
  164. WRITE_ONCE(ii_dev->latency_us, latency_us);
  165. }
  166. /**
  167. * idle_inject_start - start idle injections
  168. * @ii_dev: idle injection control device structure
  169. *
  170. * The function starts idle injection by first waking up all of the idle
  171. * injection kthreads associated with @ii_dev to let them inject CPU idle time
  172. * sets up a timer to start the next idle injection period.
  173. *
  174. * Return: -EINVAL if the CPU idle or CPU run time is not set or 0 on success.
  175. */
  176. int idle_inject_start(struct idle_inject_device *ii_dev)
  177. {
  178. unsigned int idle_duration_us = READ_ONCE(ii_dev->idle_duration_us);
  179. unsigned int run_duration_us = READ_ONCE(ii_dev->run_duration_us);
  180. if (!idle_duration_us || !run_duration_us)
  181. return -EINVAL;
  182. pr_debug("Starting injecting idle cycles on CPUs '%*pbl'\n",
  183. cpumask_pr_args(to_cpumask(ii_dev->cpumask)));
  184. idle_inject_wakeup(ii_dev);
  185. hrtimer_start(&ii_dev->timer,
  186. ns_to_ktime((idle_duration_us + run_duration_us) *
  187. NSEC_PER_USEC),
  188. HRTIMER_MODE_REL);
  189. return 0;
  190. }
  191. /**
  192. * idle_inject_stop - stops idle injections
  193. * @ii_dev: idle injection control device structure
  194. *
  195. * The function stops idle injection and waits for the threads to finish work.
  196. * If CPU idle time is being injected when this function runs, then it will
  197. * wait until the end of the cycle.
  198. *
  199. * When it returns, there is no more idle injection kthread activity. The
  200. * kthreads are scheduled out and the periodic timer is off.
  201. */
  202. void idle_inject_stop(struct idle_inject_device *ii_dev)
  203. {
  204. struct idle_inject_thread *iit;
  205. unsigned int cpu;
  206. pr_debug("Stopping idle injection on CPUs '%*pbl'\n",
  207. cpumask_pr_args(to_cpumask(ii_dev->cpumask)));
  208. hrtimer_cancel(&ii_dev->timer);
  209. /*
  210. * Stopping idle injection requires all of the idle injection kthreads
  211. * associated with the given cpumask to be parked and stay that way, so
  212. * prevent CPUs from going online at this point. Any CPUs going online
  213. * after the loop below will be covered by clearing the should_run flag
  214. * that will cause the smpboot main loop to schedule them out.
  215. */
  216. cpu_hotplug_disable();
  217. /*
  218. * Iterate over all (online + offline) CPUs here in case one of them
  219. * goes offline with the should_run flag set so as to prevent its idle
  220. * injection kthread from running when the CPU goes online again after
  221. * the ii_dev has been freed.
  222. */
  223. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask)) {
  224. iit = per_cpu_ptr(&idle_inject_thread, cpu);
  225. iit->should_run = 0;
  226. wait_task_inactive(iit->tsk, 0);
  227. }
  228. cpu_hotplug_enable();
  229. }
  230. /**
  231. * idle_inject_setup - prepare the current task for idle injection
  232. * @cpu: not used
  233. *
  234. * Called once, this function is in charge of setting the current task's
  235. * scheduler parameters to make it an RT task.
  236. */
  237. static void idle_inject_setup(unsigned int cpu)
  238. {
  239. sched_set_fifo(current);
  240. }
  241. /**
  242. * idle_inject_should_run - function helper for the smpboot API
  243. * @cpu: CPU the kthread is running on
  244. *
  245. * Return: whether or not the thread can run.
  246. */
  247. static int idle_inject_should_run(unsigned int cpu)
  248. {
  249. struct idle_inject_thread *iit =
  250. per_cpu_ptr(&idle_inject_thread, cpu);
  251. return iit->should_run;
  252. }
  253. /**
  254. * idle_inject_register - initialize idle injection on a set of CPUs
  255. * @cpumask: CPUs to be affected by idle injection
  256. *
  257. * This function creates an idle injection control device structure for the
  258. * given set of CPUs and initializes the timer associated with it. It does not
  259. * start any injection cycles.
  260. *
  261. * Return: NULL if memory allocation fails, idle injection control device
  262. * pointer on success.
  263. */
  264. struct idle_inject_device *idle_inject_register(struct cpumask *cpumask)
  265. {
  266. struct idle_inject_device *ii_dev;
  267. int cpu, cpu_rb;
  268. ii_dev = kzalloc(sizeof(*ii_dev) + cpumask_size(), GFP_KERNEL);
  269. if (!ii_dev)
  270. return NULL;
  271. cpumask_copy(to_cpumask(ii_dev->cpumask), cpumask);
  272. hrtimer_init(&ii_dev->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  273. ii_dev->timer.function = idle_inject_timer_fn;
  274. ii_dev->latency_us = UINT_MAX;
  275. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask)) {
  276. if (per_cpu(idle_inject_device, cpu)) {
  277. pr_err("cpu%d is already registered\n", cpu);
  278. goto out_rollback;
  279. }
  280. per_cpu(idle_inject_device, cpu) = ii_dev;
  281. }
  282. return ii_dev;
  283. out_rollback:
  284. for_each_cpu(cpu_rb, to_cpumask(ii_dev->cpumask)) {
  285. if (cpu == cpu_rb)
  286. break;
  287. per_cpu(idle_inject_device, cpu_rb) = NULL;
  288. }
  289. kfree(ii_dev);
  290. return NULL;
  291. }
  292. /**
  293. * idle_inject_unregister - unregister idle injection control device
  294. * @ii_dev: idle injection control device to unregister
  295. *
  296. * The function stops idle injection for the given control device,
  297. * unregisters its kthreads and frees memory allocated when that device was
  298. * created.
  299. */
  300. void idle_inject_unregister(struct idle_inject_device *ii_dev)
  301. {
  302. unsigned int cpu;
  303. idle_inject_stop(ii_dev);
  304. for_each_cpu(cpu, to_cpumask(ii_dev->cpumask))
  305. per_cpu(idle_inject_device, cpu) = NULL;
  306. kfree(ii_dev);
  307. }
  308. static struct smp_hotplug_thread idle_inject_threads = {
  309. .store = &idle_inject_thread.tsk,
  310. .setup = idle_inject_setup,
  311. .thread_fn = idle_inject_fn,
  312. .thread_comm = "idle_inject/%u",
  313. .thread_should_run = idle_inject_should_run,
  314. };
  315. static int __init idle_inject_init(void)
  316. {
  317. return smpboot_register_percpu_thread(&idle_inject_threads);
  318. }
  319. early_initcall(idle_inject_init);