smpboot.c 7.0 KB

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