smpboot.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 * __cpuinit 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. return 0;
  164. }
  165. int smpboot_create_threads(unsigned int cpu)
  166. {
  167. struct smp_hotplug_thread *cur;
  168. int ret = 0;
  169. mutex_lock(&smpboot_threads_lock);
  170. list_for_each_entry(cur, &hotplug_threads, list) {
  171. ret = __smpboot_create_thread(cur, cpu);
  172. if (ret)
  173. break;
  174. }
  175. mutex_unlock(&smpboot_threads_lock);
  176. return ret;
  177. }
  178. static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  179. {
  180. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  181. kthread_unpark(tsk);
  182. }
  183. void smpboot_unpark_threads(unsigned int cpu)
  184. {
  185. struct smp_hotplug_thread *cur;
  186. mutex_lock(&smpboot_threads_lock);
  187. list_for_each_entry(cur, &hotplug_threads, list)
  188. smpboot_unpark_thread(cur, cpu);
  189. mutex_unlock(&smpboot_threads_lock);
  190. }
  191. static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  192. {
  193. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  194. if (tsk)
  195. kthread_park(tsk);
  196. }
  197. void smpboot_park_threads(unsigned int cpu)
  198. {
  199. struct smp_hotplug_thread *cur;
  200. mutex_lock(&smpboot_threads_lock);
  201. list_for_each_entry_reverse(cur, &hotplug_threads, list)
  202. smpboot_park_thread(cur, cpu);
  203. mutex_unlock(&smpboot_threads_lock);
  204. }
  205. static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
  206. {
  207. unsigned int cpu;
  208. /* We need to destroy also the parked threads of offline cpus */
  209. for_each_possible_cpu(cpu) {
  210. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  211. if (tsk) {
  212. kthread_stop(tsk);
  213. put_task_struct(tsk);
  214. *per_cpu_ptr(ht->store, cpu) = NULL;
  215. }
  216. }
  217. }
  218. /**
  219. * smpboot_register_percpu_thread - Register a per_cpu thread related to hotplug
  220. * @plug_thread: Hotplug thread descriptor
  221. *
  222. * Creates and starts the threads on all online cpus.
  223. */
  224. int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
  225. {
  226. unsigned int cpu;
  227. int ret = 0;
  228. mutex_lock(&smpboot_threads_lock);
  229. for_each_online_cpu(cpu) {
  230. ret = __smpboot_create_thread(plug_thread, cpu);
  231. if (ret) {
  232. smpboot_destroy_threads(plug_thread);
  233. goto out;
  234. }
  235. smpboot_unpark_thread(plug_thread, cpu);
  236. }
  237. list_add(&plug_thread->list, &hotplug_threads);
  238. out:
  239. mutex_unlock(&smpboot_threads_lock);
  240. return ret;
  241. }
  242. EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  243. /**
  244. * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
  245. * @plug_thread: Hotplug thread descriptor
  246. *
  247. * Stops all threads on all possible cpus.
  248. */
  249. void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  250. {
  251. get_online_cpus();
  252. mutex_lock(&smpboot_threads_lock);
  253. list_del(&plug_thread->list);
  254. smpboot_destroy_threads(plug_thread);
  255. mutex_unlock(&smpboot_threads_lock);
  256. put_online_cpus();
  257. }
  258. EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);