smpboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. 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. /* 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_cpumask - Register a per_cpu thread related
  236. * to hotplug
  237. * @plug_thread: Hotplug thread descriptor
  238. * @cpumask: The cpumask where threads run
  239. *
  240. * Creates and starts the threads on all online cpus.
  241. */
  242. int smpboot_register_percpu_thread_cpumask(struct smp_hotplug_thread *plug_thread,
  243. const struct cpumask *cpumask)
  244. {
  245. unsigned int cpu;
  246. int ret = 0;
  247. if (!alloc_cpumask_var(&plug_thread->cpumask, GFP_KERNEL))
  248. return -ENOMEM;
  249. cpumask_copy(plug_thread->cpumask, cpumask);
  250. get_online_cpus();
  251. mutex_lock(&smpboot_threads_lock);
  252. for_each_online_cpu(cpu) {
  253. ret = __smpboot_create_thread(plug_thread, cpu);
  254. if (ret) {
  255. smpboot_destroy_threads(plug_thread);
  256. free_cpumask_var(plug_thread->cpumask);
  257. goto out;
  258. }
  259. if (cpumask_test_cpu(cpu, cpumask))
  260. smpboot_unpark_thread(plug_thread, cpu);
  261. }
  262. list_add(&plug_thread->list, &hotplug_threads);
  263. out:
  264. mutex_unlock(&smpboot_threads_lock);
  265. put_online_cpus();
  266. return ret;
  267. }
  268. EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread_cpumask);
  269. /**
  270. * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
  271. * @plug_thread: Hotplug thread descriptor
  272. *
  273. * Stops all threads on all possible cpus.
  274. */
  275. void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  276. {
  277. get_online_cpus();
  278. mutex_lock(&smpboot_threads_lock);
  279. list_del(&plug_thread->list);
  280. smpboot_destroy_threads(plug_thread);
  281. mutex_unlock(&smpboot_threads_lock);
  282. put_online_cpus();
  283. free_cpumask_var(plug_thread->cpumask);
  284. }
  285. EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
  286. /**
  287. * smpboot_update_cpumask_percpu_thread - Adjust which per_cpu hotplug threads stay parked
  288. * @plug_thread: Hotplug thread descriptor
  289. * @new: Revised mask to use
  290. *
  291. * The cpumask field in the smp_hotplug_thread must not be updated directly
  292. * by the client, but only by calling this function.
  293. * This function can only be called on a registered smp_hotplug_thread.
  294. */
  295. int smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
  296. const struct cpumask *new)
  297. {
  298. struct cpumask *old = plug_thread->cpumask;
  299. cpumask_var_t tmp;
  300. unsigned int cpu;
  301. if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
  302. return -ENOMEM;
  303. get_online_cpus();
  304. mutex_lock(&smpboot_threads_lock);
  305. /* Park threads that were exclusively enabled on the old mask. */
  306. cpumask_andnot(tmp, old, new);
  307. for_each_cpu_and(cpu, tmp, cpu_online_mask)
  308. smpboot_park_thread(plug_thread, cpu);
  309. /* Unpark threads that are exclusively enabled on the new mask. */
  310. cpumask_andnot(tmp, new, old);
  311. for_each_cpu_and(cpu, tmp, cpu_online_mask)
  312. smpboot_unpark_thread(plug_thread, cpu);
  313. cpumask_copy(old, new);
  314. mutex_unlock(&smpboot_threads_lock);
  315. put_online_cpus();
  316. free_cpumask_var(tmp);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(smpboot_update_cpumask_percpu_thread);
  320. static DEFINE_PER_CPU(atomic_t, cpu_hotplug_state) = ATOMIC_INIT(CPU_POST_DEAD);
  321. /*
  322. * Called to poll specified CPU's state, for example, when waiting for
  323. * a CPU to come online.
  324. */
  325. int cpu_report_state(int cpu)
  326. {
  327. return atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  328. }
  329. /*
  330. * If CPU has died properly, set its state to CPU_UP_PREPARE and
  331. * return success. Otherwise, return -EBUSY if the CPU died after
  332. * cpu_wait_death() timed out. And yet otherwise again, return -EAGAIN
  333. * if cpu_wait_death() timed out and the CPU still hasn't gotten around
  334. * to dying. In the latter two cases, the CPU might not be set up
  335. * properly, but it is up to the arch-specific code to decide.
  336. * Finally, -EIO indicates an unanticipated problem.
  337. *
  338. * Note that it is permissible to omit this call entirely, as is
  339. * done in architectures that do no CPU-hotplug error checking.
  340. */
  341. int cpu_check_up_prepare(int cpu)
  342. {
  343. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
  344. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
  345. return 0;
  346. }
  347. switch (atomic_read(&per_cpu(cpu_hotplug_state, cpu))) {
  348. case CPU_POST_DEAD:
  349. /* The CPU died properly, so just start it up again. */
  350. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
  351. return 0;
  352. case CPU_DEAD_FROZEN:
  353. /*
  354. * Timeout during CPU death, so let caller know.
  355. * The outgoing CPU completed its processing, but after
  356. * cpu_wait_death() timed out and reported the error. The
  357. * caller is free to proceed, in which case the state
  358. * will be reset properly by cpu_set_state_online().
  359. * Proceeding despite this -EBUSY return makes sense
  360. * for systems where the outgoing CPUs take themselves
  361. * offline, with no post-death manipulation required from
  362. * a surviving CPU.
  363. */
  364. return -EBUSY;
  365. case CPU_BROKEN:
  366. /*
  367. * The most likely reason we got here is that there was
  368. * a timeout during CPU death, and the outgoing CPU never
  369. * did complete its processing. This could happen on
  370. * a virtualized system if the outgoing VCPU gets preempted
  371. * for more than five seconds, and the user attempts to
  372. * immediately online that same CPU. Trying again later
  373. * might return -EBUSY above, hence -EAGAIN.
  374. */
  375. return -EAGAIN;
  376. default:
  377. /* Should not happen. Famous last words. */
  378. return -EIO;
  379. }
  380. }
  381. /*
  382. * Mark the specified CPU online.
  383. *
  384. * Note that it is permissible to omit this call entirely, as is
  385. * done in architectures that do no CPU-hotplug error checking.
  386. */
  387. void cpu_set_state_online(int cpu)
  388. {
  389. (void)atomic_xchg(&per_cpu(cpu_hotplug_state, cpu), CPU_ONLINE);
  390. }
  391. #ifdef CONFIG_HOTPLUG_CPU
  392. /*
  393. * Wait for the specified CPU to exit the idle loop and die.
  394. */
  395. bool cpu_wait_death(unsigned int cpu, int seconds)
  396. {
  397. int jf_left = seconds * HZ;
  398. int oldstate;
  399. bool ret = true;
  400. int sleep_jf = 1;
  401. might_sleep();
  402. /* The outgoing CPU will normally get done quite quickly. */
  403. if (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) == CPU_DEAD)
  404. goto update_state;
  405. udelay(5);
  406. /* But if the outgoing CPU dawdles, wait increasingly long times. */
  407. while (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) != CPU_DEAD) {
  408. schedule_timeout_uninterruptible(sleep_jf);
  409. jf_left -= sleep_jf;
  410. if (jf_left <= 0)
  411. break;
  412. sleep_jf = DIV_ROUND_UP(sleep_jf * 11, 10);
  413. }
  414. update_state:
  415. oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  416. if (oldstate == CPU_DEAD) {
  417. /* Outgoing CPU died normally, update state. */
  418. smp_mb(); /* atomic_read() before update. */
  419. atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_POST_DEAD);
  420. } else {
  421. /* Outgoing CPU still hasn't died, set state accordingly. */
  422. if (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
  423. oldstate, CPU_BROKEN) != oldstate)
  424. goto update_state;
  425. ret = false;
  426. }
  427. return ret;
  428. }
  429. /*
  430. * Called by the outgoing CPU to report its successful death. Return
  431. * false if this report follows the surviving CPU's timing out.
  432. *
  433. * A separate "CPU_DEAD_FROZEN" is used when the surviving CPU
  434. * timed out. This approach allows architectures to omit calls to
  435. * cpu_check_up_prepare() and cpu_set_state_online() without defeating
  436. * the next cpu_wait_death()'s polling loop.
  437. */
  438. bool cpu_report_death(void)
  439. {
  440. int oldstate;
  441. int newstate;
  442. int cpu = smp_processor_id();
  443. do {
  444. oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
  445. if (oldstate != CPU_BROKEN)
  446. newstate = CPU_DEAD;
  447. else
  448. newstate = CPU_DEAD_FROZEN;
  449. } while (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
  450. oldstate, newstate) != oldstate);
  451. return newstate == CPU_DEAD;
  452. }
  453. #endif /* #ifdef CONFIG_HOTPLUG_CPU */