smpboot.c 13 KB

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