smpboot.c 6.9 KB

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