smpboot.c 13 KB

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