smpboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. /* cleanup must mirror setup */
  100. if (ht->cleanup && td->status != HP_THREAD_NONE)
  101. ht->cleanup(td->cpu, cpu_online(td->cpu));
  102. kfree(td);
  103. return 0;
  104. }
  105. if (kthread_should_park()) {
  106. __set_current_state(TASK_RUNNING);
  107. preempt_enable();
  108. if (ht->park && td->status == HP_THREAD_ACTIVE) {
  109. BUG_ON(td->cpu != smp_processor_id());
  110. ht->park(td->cpu);
  111. td->status = HP_THREAD_PARKED;
  112. }
  113. kthread_parkme();
  114. /* We might have been woken for stop */
  115. continue;
  116. }
  117. BUG_ON(td->cpu != smp_processor_id());
  118. /* Check for state change setup */
  119. switch (td->status) {
  120. case HP_THREAD_NONE:
  121. __set_current_state(TASK_RUNNING);
  122. preempt_enable();
  123. if (ht->setup)
  124. ht->setup(td->cpu);
  125. td->status = HP_THREAD_ACTIVE;
  126. continue;
  127. case HP_THREAD_PARKED:
  128. __set_current_state(TASK_RUNNING);
  129. preempt_enable();
  130. if (ht->unpark)
  131. ht->unpark(td->cpu);
  132. td->status = HP_THREAD_ACTIVE;
  133. continue;
  134. }
  135. if (!ht->thread_should_run(td->cpu)) {
  136. preempt_enable_no_resched();
  137. schedule();
  138. } else {
  139. __set_current_state(TASK_RUNNING);
  140. preempt_enable();
  141. ht->thread_fn(td->cpu);
  142. }
  143. }
  144. }
  145. static int
  146. __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  147. {
  148. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  149. struct smpboot_thread_data *td;
  150. if (tsk)
  151. return 0;
  152. td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu));
  153. if (!td)
  154. return -ENOMEM;
  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->selfparking)
  196. kthread_unpark(tsk);
  197. }
  198. int 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. return 0;
  207. }
  208. static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  209. {
  210. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  211. if (tsk && !ht->selfparking)
  212. kthread_park(tsk);
  213. }
  214. int smpboot_park_threads(unsigned int cpu)
  215. {
  216. struct smp_hotplug_thread *cur;
  217. mutex_lock(&smpboot_threads_lock);
  218. list_for_each_entry_reverse(cur, &hotplug_threads, list)
  219. smpboot_park_thread(cur, cpu);
  220. mutex_unlock(&smpboot_threads_lock);
  221. return 0;
  222. }
  223. static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
  224. {
  225. unsigned int cpu;
  226. /* We need to destroy also the parked threads of offline cpus */
  227. for_each_possible_cpu(cpu) {
  228. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  229. if (tsk) {
  230. kthread_stop(tsk);
  231. put_task_struct(tsk);
  232. *per_cpu_ptr(ht->store, cpu) = NULL;
  233. }
  234. }
  235. }
  236. /**
  237. * smpboot_register_percpu_thread_cpumask - Register a per_cpu thread related
  238. * to hotplug
  239. * @plug_thread: Hotplug thread descriptor
  240. * @cpumask: The cpumask where threads run
  241. *
  242. * Creates and starts the threads on all online cpus.
  243. */
  244. int smpboot_register_percpu_thread_cpumask(struct smp_hotplug_thread *plug_thread,
  245. const struct cpumask *cpumask)
  246. {
  247. unsigned int cpu;
  248. int ret = 0;
  249. if (!alloc_cpumask_var(&plug_thread->cpumask, GFP_KERNEL))
  250. return -ENOMEM;
  251. cpumask_copy(plug_thread->cpumask, cpumask);
  252. get_online_cpus();
  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. free_cpumask_var(plug_thread->cpumask);
  259. goto out;
  260. }
  261. if (cpumask_test_cpu(cpu, cpumask))
  262. smpboot_unpark_thread(plug_thread, cpu);
  263. }
  264. list_add(&plug_thread->list, &hotplug_threads);
  265. out:
  266. mutex_unlock(&smpboot_threads_lock);
  267. put_online_cpus();
  268. return ret;
  269. }
  270. EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread_cpumask);
  271. /**
  272. * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
  273. * @plug_thread: Hotplug thread descriptor
  274. *
  275. * Stops all threads on all possible cpus.
  276. */
  277. void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  278. {
  279. get_online_cpus();
  280. mutex_lock(&smpboot_threads_lock);
  281. list_del(&plug_thread->list);
  282. smpboot_destroy_threads(plug_thread);
  283. mutex_unlock(&smpboot_threads_lock);
  284. put_online_cpus();
  285. free_cpumask_var(plug_thread->cpumask);
  286. }
  287. EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
  288. /**
  289. * smpboot_update_cpumask_percpu_thread - Adjust which per_cpu hotplug threads stay parked
  290. * @plug_thread: Hotplug thread descriptor
  291. * @new: Revised mask to use
  292. *
  293. * The cpumask field in the smp_hotplug_thread must not be updated directly
  294. * by the client, but only by calling this function.
  295. * This function can only be called on a registered smp_hotplug_thread.
  296. */
  297. int smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
  298. const struct cpumask *new)
  299. {
  300. struct cpumask *old = plug_thread->cpumask;
  301. cpumask_var_t tmp;
  302. unsigned int cpu;
  303. if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
  304. return -ENOMEM;
  305. get_online_cpus();
  306. mutex_lock(&smpboot_threads_lock);
  307. /* Park threads that were exclusively enabled on the old mask. */
  308. cpumask_andnot(tmp, old, new);
  309. for_each_cpu_and(cpu, tmp, cpu_online_mask)
  310. smpboot_park_thread(plug_thread, cpu);
  311. /* Unpark threads that are exclusively enabled on the new mask. */
  312. cpumask_andnot(tmp, new, old);
  313. for_each_cpu_and(cpu, tmp, cpu_online_mask)
  314. smpboot_unpark_thread(plug_thread, cpu);
  315. cpumask_copy(old, new);
  316. mutex_unlock(&smpboot_threads_lock);
  317. put_online_cpus();
  318. free_cpumask_var(tmp);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(smpboot_update_cpumask_percpu_thread);
  322. static DEFINE_PER_CPU(atomic_t, cpu_hotplug_state) = ATOMIC_INIT(CPU_POST_DEAD);
  323. /*
  324. * Called to poll specified CPU's state, for example, when waiting for
  325. * a CPU to come online.
  326. */
  327. int cpu_report_state(int cpu)
  328. {
  329. return atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  330. }
  331. /*
  332. * If CPU has died properly, set its state to CPU_UP_PREPARE and
  333. * return success. Otherwise, return -EBUSY if the CPU died after
  334. * cpu_wait_death() timed out. And yet otherwise again, return -EAGAIN
  335. * if cpu_wait_death() timed out and the CPU still hasn't gotten around
  336. * to dying. In the latter two cases, the CPU might not be set up
  337. * properly, but it is up to the arch-specific code to decide.
  338. * Finally, -EIO indicates an unanticipated problem.
  339. *
  340. * Note that it is permissible to omit this call entirely, as is
  341. * done in architectures that do no CPU-hotplug error checking.
  342. */
  343. int cpu_check_up_prepare(int cpu)
  344. {
  345. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
  346. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
  347. return 0;
  348. }
  349. switch (atomic_read(&per_cpu(cpu_hotplug_state, cpu))) {
  350. case CPU_POST_DEAD:
  351. /* The CPU died properly, so just start it up again. */
  352. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
  353. return 0;
  354. case CPU_DEAD_FROZEN:
  355. /*
  356. * Timeout during CPU death, so let caller know.
  357. * The outgoing CPU completed its processing, but after
  358. * cpu_wait_death() timed out and reported the error. The
  359. * caller is free to proceed, in which case the state
  360. * will be reset properly by cpu_set_state_online().
  361. * Proceeding despite this -EBUSY return makes sense
  362. * for systems where the outgoing CPUs take themselves
  363. * offline, with no post-death manipulation required from
  364. * a surviving CPU.
  365. */
  366. return -EBUSY;
  367. case CPU_BROKEN:
  368. /*
  369. * The most likely reason we got here is that there was
  370. * a timeout during CPU death, and the outgoing CPU never
  371. * did complete its processing. This could happen on
  372. * a virtualized system if the outgoing VCPU gets preempted
  373. * for more than five seconds, and the user attempts to
  374. * immediately online that same CPU. Trying again later
  375. * might return -EBUSY above, hence -EAGAIN.
  376. */
  377. return -EAGAIN;
  378. default:
  379. /* Should not happen. Famous last words. */
  380. return -EIO;
  381. }
  382. }
  383. /*
  384. * Mark the specified CPU online.
  385. *
  386. * Note that it is permissible to omit this call entirely, as is
  387. * done in architectures that do no CPU-hotplug error checking.
  388. */
  389. void cpu_set_state_online(int cpu)
  390. {
  391. (void)atomic_xchg(&per_cpu(cpu_hotplug_state, cpu), CPU_ONLINE);
  392. }
  393. #ifdef CONFIG_HOTPLUG_CPU
  394. /*
  395. * Wait for the specified CPU to exit the idle loop and die.
  396. */
  397. bool cpu_wait_death(unsigned int cpu, int seconds)
  398. {
  399. int jf_left = seconds * HZ;
  400. int oldstate;
  401. bool ret = true;
  402. int sleep_jf = 1;
  403. might_sleep();
  404. /* The outgoing CPU will normally get done quite quickly. */
  405. if (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) == CPU_DEAD)
  406. goto update_state;
  407. udelay(5);
  408. /* But if the outgoing CPU dawdles, wait increasingly long times. */
  409. while (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) != CPU_DEAD) {
  410. schedule_timeout_uninterruptible(sleep_jf);
  411. jf_left -= sleep_jf;
  412. if (jf_left <= 0)
  413. break;
  414. sleep_jf = DIV_ROUND_UP(sleep_jf * 11, 10);
  415. }
  416. update_state:
  417. oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  418. if (oldstate == CPU_DEAD) {
  419. /* Outgoing CPU died normally, update state. */
  420. smp_mb(); /* atomic_read() before update. */
  421. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_POST_DEAD);
  422. } else {
  423. /* Outgoing CPU still hasn't died, set state accordingly. */
  424. if (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
  425. oldstate, CPU_BROKEN) != oldstate)
  426. goto update_state;
  427. ret = false;
  428. }
  429. return ret;
  430. }
  431. /*
  432. * Called by the outgoing CPU to report its successful death. Return
  433. * false if this report follows the surviving CPU's timing out.
  434. *
  435. * A separate "CPU_DEAD_FROZEN" is used when the surviving CPU
  436. * timed out. This approach allows architectures to omit calls to
  437. * cpu_check_up_prepare() and cpu_set_state_online() without defeating
  438. * the next cpu_wait_death()'s polling loop.
  439. */
  440. bool cpu_report_death(void)
  441. {
  442. int oldstate;
  443. int newstate;
  444. int cpu = smp_processor_id();
  445. do {
  446. oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  447. if (oldstate != CPU_BROKEN)
  448. newstate = CPU_DEAD;
  449. else
  450. newstate = CPU_DEAD_FROZEN;
  451. } while (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
  452. oldstate, newstate) != oldstate);
  453. return newstate == CPU_DEAD;
  454. }
  455. #endif /* #ifdef CONFIG_HOTPLUG_CPU */