smpboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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/delay.h>
  8. #include <linux/init.h>
  9. #include <linux/list.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched.h>
  12. #include <linux/export.h>
  13. #include <linux/percpu.h>
  14. #include <linux/kthread.h>
  15. #include <linux/smpboot.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 *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. td->cpu = cpu;
  155. td->ht = ht;
  156. tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,
  157. ht->thread_comm);
  158. if (IS_ERR(tsk)) {
  159. kfree(td);
  160. return PTR_ERR(tsk);
  161. }
  162. get_task_struct(tsk);
  163. *per_cpu_ptr(ht->store, cpu) = tsk;
  164. if (ht->create) {
  165. /*
  166. * Make sure that the task has actually scheduled out
  167. * into park position, before calling the create
  168. * callback. At least the migration thread callback
  169. * requires that the task is off the runqueue.
  170. */
  171. if (!wait_task_inactive(tsk, TASK_PARKED))
  172. WARN_ON(1);
  173. else
  174. ht->create(cpu);
  175. }
  176. return 0;
  177. }
  178. int smpboot_create_threads(unsigned int cpu)
  179. {
  180. struct smp_hotplug_thread *cur;
  181. int ret = 0;
  182. mutex_lock(&smpboot_threads_lock);
  183. list_for_each_entry(cur, &hotplug_threads, list) {
  184. ret = __smpboot_create_thread(cur, cpu);
  185. if (ret)
  186. break;
  187. }
  188. mutex_unlock(&smpboot_threads_lock);
  189. return ret;
  190. }
  191. static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  192. {
  193. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  194. if (ht->pre_unpark)
  195. ht->pre_unpark(cpu);
  196. kthread_unpark(tsk);
  197. }
  198. void smpboot_unpark_threads(unsigned int cpu)
  199. {
  200. struct smp_hotplug_thread *cur;
  201. mutex_lock(&smpboot_threads_lock);
  202. list_for_each_entry(cur, &hotplug_threads, list)
  203. if (cpumask_test_cpu(cpu, cur->cpumask))
  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. /* Unpark any threads that were voluntarily parked. */
  225. for_each_cpu_not(cpu, ht->cpumask) {
  226. if (cpu_online(cpu)) {
  227. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  228. if (tsk)
  229. kthread_unpark(tsk);
  230. }
  231. }
  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(tsk);
  237. put_task_struct(tsk);
  238. *per_cpu_ptr(ht->store, cpu) = NULL;
  239. }
  240. }
  241. }
  242. /**
  243. * smpboot_register_percpu_thread - Register a per_cpu thread related 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. if (!alloc_cpumask_var(&plug_thread->cpumask, GFP_KERNEL))
  253. return -ENOMEM;
  254. cpumask_copy(plug_thread->cpumask, cpu_possible_mask);
  255. get_online_cpus();
  256. mutex_lock(&smpboot_threads_lock);
  257. for_each_online_cpu(cpu) {
  258. ret = __smpboot_create_thread(plug_thread, cpu);
  259. if (ret) {
  260. smpboot_destroy_threads(plug_thread);
  261. goto out;
  262. }
  263. smpboot_unpark_thread(plug_thread, cpu);
  264. }
  265. list_add(&plug_thread->list, &hotplug_threads);
  266. out:
  267. mutex_unlock(&smpboot_threads_lock);
  268. put_online_cpus();
  269. return ret;
  270. }
  271. EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  272. /**
  273. * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
  274. * @plug_thread: Hotplug thread descriptor
  275. *
  276. * Stops all threads on all possible cpus.
  277. */
  278. void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  279. {
  280. get_online_cpus();
  281. mutex_lock(&smpboot_threads_lock);
  282. list_del(&plug_thread->list);
  283. smpboot_destroy_threads(plug_thread);
  284. mutex_unlock(&smpboot_threads_lock);
  285. put_online_cpus();
  286. free_cpumask_var(plug_thread->cpumask);
  287. }
  288. EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
  289. /**
  290. * smpboot_update_cpumask_percpu_thread - Adjust which per_cpu hotplug threads stay parked
  291. * @plug_thread: Hotplug thread descriptor
  292. * @new: Revised mask to use
  293. *
  294. * The cpumask field in the smp_hotplug_thread must not be updated directly
  295. * by the client, but only by calling this function.
  296. * This function can only be called on a registered smp_hotplug_thread.
  297. */
  298. int smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
  299. const struct cpumask *new)
  300. {
  301. struct cpumask *old = plug_thread->cpumask;
  302. cpumask_var_t tmp;
  303. unsigned int cpu;
  304. if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
  305. return -ENOMEM;
  306. get_online_cpus();
  307. mutex_lock(&smpboot_threads_lock);
  308. /* Park threads that were exclusively enabled on the old mask. */
  309. cpumask_andnot(tmp, old, new);
  310. for_each_cpu_and(cpu, tmp, cpu_online_mask)
  311. smpboot_park_thread(plug_thread, cpu);
  312. /* Unpark threads that are exclusively enabled on the new mask. */
  313. cpumask_andnot(tmp, new, old);
  314. for_each_cpu_and(cpu, tmp, cpu_online_mask)
  315. smpboot_unpark_thread(plug_thread, cpu);
  316. cpumask_copy(old, new);
  317. mutex_unlock(&smpboot_threads_lock);
  318. put_online_cpus();
  319. free_cpumask_var(tmp);
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(smpboot_update_cpumask_percpu_thread);
  323. static DEFINE_PER_CPU(atomic_t, cpu_hotplug_state) = ATOMIC_INIT(CPU_POST_DEAD);
  324. /*
  325. * Called to poll specified CPU's state, for example, when waiting for
  326. * a CPU to come online.
  327. */
  328. int cpu_report_state(int cpu)
  329. {
  330. return atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  331. }
  332. /*
  333. * If CPU has died properly, set its state to CPU_UP_PREPARE and
  334. * return success. Otherwise, return -EBUSY if the CPU died after
  335. * cpu_wait_death() timed out. And yet otherwise again, return -EAGAIN
  336. * if cpu_wait_death() timed out and the CPU still hasn't gotten around
  337. * to dying. In the latter two cases, the CPU might not be set up
  338. * properly, but it is up to the arch-specific code to decide.
  339. * Finally, -EIO indicates an unanticipated problem.
  340. *
  341. * Note that it is permissible to omit this call entirely, as is
  342. * done in architectures that do no CPU-hotplug error checking.
  343. */
  344. int cpu_check_up_prepare(int cpu)
  345. {
  346. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
  347. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
  348. return 0;
  349. }
  350. switch (atomic_read(&per_cpu(cpu_hotplug_state, cpu))) {
  351. case CPU_POST_DEAD:
  352. /* The CPU died properly, so just start it up again. */
  353. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
  354. return 0;
  355. case CPU_DEAD_FROZEN:
  356. /*
  357. * Timeout during CPU death, so let caller know.
  358. * The outgoing CPU completed its processing, but after
  359. * cpu_wait_death() timed out and reported the error. The
  360. * caller is free to proceed, in which case the state
  361. * will be reset properly by cpu_set_state_online().
  362. * Proceeding despite this -EBUSY return makes sense
  363. * for systems where the outgoing CPUs take themselves
  364. * offline, with no post-death manipulation required from
  365. * a surviving CPU.
  366. */
  367. return -EBUSY;
  368. case CPU_BROKEN:
  369. /*
  370. * The most likely reason we got here is that there was
  371. * a timeout during CPU death, and the outgoing CPU never
  372. * did complete its processing. This could happen on
  373. * a virtualized system if the outgoing VCPU gets preempted
  374. * for more than five seconds, and the user attempts to
  375. * immediately online that same CPU. Trying again later
  376. * might return -EBUSY above, hence -EAGAIN.
  377. */
  378. return -EAGAIN;
  379. default:
  380. /* Should not happen. Famous last words. */
  381. return -EIO;
  382. }
  383. }
  384. /*
  385. * Mark the specified CPU online.
  386. *
  387. * Note that it is permissible to omit this call entirely, as is
  388. * done in architectures that do no CPU-hotplug error checking.
  389. */
  390. void cpu_set_state_online(int cpu)
  391. {
  392. (void)atomic_xchg(&per_cpu(cpu_hotplug_state, cpu), CPU_ONLINE);
  393. }
  394. #ifdef CONFIG_HOTPLUG_CPU
  395. /*
  396. * Wait for the specified CPU to exit the idle loop and die.
  397. */
  398. bool cpu_wait_death(unsigned int cpu, int seconds)
  399. {
  400. int jf_left = seconds * HZ;
  401. int oldstate;
  402. bool ret = true;
  403. int sleep_jf = 1;
  404. might_sleep();
  405. /* The outgoing CPU will normally get done quite quickly. */
  406. if (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) == CPU_DEAD)
  407. goto update_state;
  408. udelay(5);
  409. /* But if the outgoing CPU dawdles, wait increasingly long times. */
  410. while (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) != CPU_DEAD) {
  411. schedule_timeout_uninterruptible(sleep_jf);
  412. jf_left -= sleep_jf;
  413. if (jf_left <= 0)
  414. break;
  415. sleep_jf = DIV_ROUND_UP(sleep_jf * 11, 10);
  416. }
  417. update_state:
  418. oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  419. if (oldstate == CPU_DEAD) {
  420. /* Outgoing CPU died normally, update state. */
  421. smp_mb(); /* atomic_read() before update. */
  422. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_POST_DEAD);
  423. } else {
  424. /* Outgoing CPU still hasn't died, set state accordingly. */
  425. if (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
  426. oldstate, CPU_BROKEN) != oldstate)
  427. goto update_state;
  428. ret = false;
  429. }
  430. return ret;
  431. }
  432. /*
  433. * Called by the outgoing CPU to report its successful death. Return
  434. * false if this report follows the surviving CPU's timing out.
  435. *
  436. * A separate "CPU_DEAD_FROZEN" is used when the surviving CPU
  437. * timed out. This approach allows architectures to omit calls to
  438. * cpu_check_up_prepare() and cpu_set_state_online() without defeating
  439. * the next cpu_wait_death()'s polling loop.
  440. */
  441. bool cpu_report_death(void)
  442. {
  443. int oldstate;
  444. int newstate;
  445. int cpu = smp_processor_id();
  446. do {
  447. oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  448. if (oldstate != CPU_BROKEN)
  449. newstate = CPU_DEAD;
  450. else
  451. newstate = CPU_DEAD_FROZEN;
  452. } while (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
  453. oldstate, newstate) != oldstate);
  454. return newstate == CPU_DEAD;
  455. }
  456. #endif /* #ifdef CONFIG_HOTPLUG_CPU */