smpboot.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common SMP CPU bringup/teardown functions
  4. */
  5. #include <linux/cpu.h>
  6. #include <linux/err.h>
  7. #include <linux/smp.h>
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/sched/task.h>
  14. #include <linux/export.h>
  15. #include <linux/percpu.h>
  16. #include <linux/kthread.h>
  17. #include <linux/smpboot.h>
  18. #include "smpboot.h"
  19. #ifdef CONFIG_SMP
  20. #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
  21. /*
  22. * For the hotplug case we keep the task structs around and reuse
  23. * them.
  24. */
  25. static DEFINE_PER_CPU(struct task_struct *, idle_threads);
  26. struct task_struct *idle_thread_get(unsigned int cpu)
  27. {
  28. struct task_struct *tsk = per_cpu(idle_threads, cpu);
  29. if (!tsk)
  30. return ERR_PTR(-ENOMEM);
  31. return tsk;
  32. }
  33. void __init idle_thread_set_boot_cpu(void)
  34. {
  35. per_cpu(idle_threads, smp_processor_id()) = current;
  36. }
  37. /**
  38. * idle_init - Initialize the idle thread for a cpu
  39. * @cpu: The cpu for which the idle thread should be initialized
  40. *
  41. * Creates the thread if it does not exist.
  42. */
  43. static __always_inline void idle_init(unsigned int cpu)
  44. {
  45. struct task_struct *tsk = per_cpu(idle_threads, cpu);
  46. if (!tsk) {
  47. tsk = fork_idle(cpu);
  48. if (IS_ERR(tsk))
  49. pr_err("SMP: fork_idle() failed for CPU %u\n", cpu);
  50. else
  51. per_cpu(idle_threads, cpu) = tsk;
  52. }
  53. }
  54. /**
  55. * idle_threads_init - Initialize idle threads for all cpus
  56. */
  57. void __init idle_threads_init(void)
  58. {
  59. unsigned int cpu, boot_cpu;
  60. boot_cpu = smp_processor_id();
  61. for_each_possible_cpu(cpu) {
  62. if (cpu != boot_cpu)
  63. idle_init(cpu);
  64. }
  65. }
  66. #endif
  67. #endif /* #ifdef CONFIG_SMP */
  68. static LIST_HEAD(hotplug_threads);
  69. static DEFINE_MUTEX(smpboot_threads_lock);
  70. struct smpboot_thread_data {
  71. unsigned int cpu;
  72. unsigned int status;
  73. struct smp_hotplug_thread *ht;
  74. };
  75. enum {
  76. HP_THREAD_NONE = 0,
  77. HP_THREAD_ACTIVE,
  78. HP_THREAD_PARKED,
  79. };
  80. /**
  81. * smpboot_thread_fn - percpu hotplug thread loop function
  82. * @data: thread data pointer
  83. *
  84. * Checks for thread stop and park conditions. Calls the necessary
  85. * setup, cleanup, park and unpark functions for the registered
  86. * thread.
  87. *
  88. * Returns 1 when the thread should exit, 0 otherwise.
  89. */
  90. static int smpboot_thread_fn(void *data)
  91. {
  92. struct smpboot_thread_data *td = data;
  93. struct smp_hotplug_thread *ht = td->ht;
  94. while (1) {
  95. set_current_state(TASK_INTERRUPTIBLE);
  96. preempt_disable();
  97. if (kthread_should_stop()) {
  98. __set_current_state(TASK_RUNNING);
  99. preempt_enable();
  100. /* cleanup must mirror setup */
  101. if (ht->cleanup && td->status != HP_THREAD_NONE)
  102. ht->cleanup(td->cpu, cpu_online(td->cpu));
  103. kfree(td);
  104. return 0;
  105. }
  106. if (kthread_should_park()) {
  107. __set_current_state(TASK_RUNNING);
  108. preempt_enable();
  109. if (ht->park && td->status == HP_THREAD_ACTIVE) {
  110. BUG_ON(td->cpu != smp_processor_id());
  111. ht->park(td->cpu);
  112. td->status = HP_THREAD_PARKED;
  113. }
  114. kthread_parkme();
  115. /* We might have been woken for stop */
  116. continue;
  117. }
  118. BUG_ON(td->cpu != smp_processor_id());
  119. /* Check for state change setup */
  120. switch (td->status) {
  121. case HP_THREAD_NONE:
  122. __set_current_state(TASK_RUNNING);
  123. preempt_enable();
  124. if (ht->setup)
  125. ht->setup(td->cpu);
  126. td->status = HP_THREAD_ACTIVE;
  127. continue;
  128. case HP_THREAD_PARKED:
  129. __set_current_state(TASK_RUNNING);
  130. preempt_enable();
  131. if (ht->unpark)
  132. ht->unpark(td->cpu);
  133. td->status = HP_THREAD_ACTIVE;
  134. continue;
  135. }
  136. if (!ht->thread_should_run(td->cpu)) {
  137. preempt_enable_no_resched();
  138. schedule();
  139. } else {
  140. __set_current_state(TASK_RUNNING);
  141. preempt_enable();
  142. ht->thread_fn(td->cpu);
  143. }
  144. }
  145. }
  146. static int
  147. __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  148. {
  149. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  150. struct smpboot_thread_data *td;
  151. if (tsk)
  152. return 0;
  153. td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu));
  154. if (!td)
  155. return -ENOMEM;
  156. td->cpu = cpu;
  157. td->ht = ht;
  158. tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,
  159. ht->thread_comm);
  160. if (IS_ERR(tsk)) {
  161. kfree(td);
  162. return PTR_ERR(tsk);
  163. }
  164. kthread_set_per_cpu(tsk, cpu);
  165. /*
  166. * Park the thread so that it could start right on the CPU
  167. * when it is available.
  168. */
  169. kthread_park(tsk);
  170. get_task_struct(tsk);
  171. *per_cpu_ptr(ht->store, cpu) = tsk;
  172. if (ht->create) {
  173. /*
  174. * Make sure that the task has actually scheduled out
  175. * into park position, before calling the create
  176. * callback. At least the migration thread callback
  177. * requires that the task is off the runqueue.
  178. */
  179. if (!wait_task_inactive(tsk, TASK_PARKED))
  180. WARN_ON(1);
  181. else
  182. ht->create(cpu);
  183. }
  184. return 0;
  185. }
  186. int smpboot_create_threads(unsigned int cpu)
  187. {
  188. struct smp_hotplug_thread *cur;
  189. int ret = 0;
  190. mutex_lock(&smpboot_threads_lock);
  191. list_for_each_entry(cur, &hotplug_threads, list) {
  192. ret = __smpboot_create_thread(cur, cpu);
  193. if (ret)
  194. break;
  195. }
  196. mutex_unlock(&smpboot_threads_lock);
  197. return ret;
  198. }
  199. static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  200. {
  201. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  202. if (!ht->selfparking)
  203. kthread_unpark(tsk);
  204. }
  205. int smpboot_unpark_threads(unsigned int cpu)
  206. {
  207. struct smp_hotplug_thread *cur;
  208. mutex_lock(&smpboot_threads_lock);
  209. list_for_each_entry(cur, &hotplug_threads, list)
  210. smpboot_unpark_thread(cur, cpu);
  211. mutex_unlock(&smpboot_threads_lock);
  212. return 0;
  213. }
  214. static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  215. {
  216. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  217. if (tsk && !ht->selfparking)
  218. kthread_park(tsk);
  219. }
  220. int smpboot_park_threads(unsigned int cpu)
  221. {
  222. struct smp_hotplug_thread *cur;
  223. mutex_lock(&smpboot_threads_lock);
  224. list_for_each_entry_reverse(cur, &hotplug_threads, list)
  225. smpboot_park_thread(cur, cpu);
  226. mutex_unlock(&smpboot_threads_lock);
  227. return 0;
  228. }
  229. static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
  230. {
  231. unsigned int cpu;
  232. /* We need to destroy also the parked threads of offline cpus */
  233. for_each_possible_cpu(cpu) {
  234. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  235. if (tsk) {
  236. kthread_stop_put(tsk);
  237. *per_cpu_ptr(ht->store, cpu) = NULL;
  238. }
  239. }
  240. }
  241. /**
  242. * smpboot_register_percpu_thread - Register a per_cpu thread related
  243. * to hotplug
  244. * @plug_thread: Hotplug thread descriptor
  245. *
  246. * Creates and starts the threads on all online cpus.
  247. */
  248. int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
  249. {
  250. unsigned int cpu;
  251. int ret = 0;
  252. cpus_read_lock();
  253. mutex_lock(&smpboot_threads_lock);
  254. for_each_online_cpu(cpu) {
  255. ret = __smpboot_create_thread(plug_thread, cpu);
  256. if (ret) {
  257. smpboot_destroy_threads(plug_thread);
  258. goto out;
  259. }
  260. smpboot_unpark_thread(plug_thread, cpu);
  261. }
  262. list_add(&plug_thread->list, &hotplug_threads);
  263. out:
  264. mutex_unlock(&smpboot_threads_lock);
  265. cpus_read_unlock();
  266. return ret;
  267. }
  268. EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  269. /**
  270. * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
  271. * @plug_thread: Hotplug thread descriptor
  272. *
  273. * Stops all threads on all possible cpus.
  274. */
  275. void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  276. {
  277. cpus_read_lock();
  278. mutex_lock(&smpboot_threads_lock);
  279. list_del(&plug_thread->list);
  280. smpboot_destroy_threads(plug_thread);
  281. mutex_unlock(&smpboot_threads_lock);
  282. cpus_read_unlock();
  283. }
  284. EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);