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/sched/task.h>
  13. #include <linux/export.h>
  14. #include <linux/percpu.h>
  15. #include <linux/kthread.h>
  16. #include <linux/smpboot.h>
  17. #include "smpboot.h"
  18. #ifdef CONFIG_SMP
  19. #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
  20. /*
  21. * For the hotplug case we keep the task structs around and reuse
  22. * them.
  23. */
  24. static DEFINE_PER_CPU(struct task_struct *, idle_threads);
  25. struct task_struct *idle_thread_get(unsigned int cpu)
  26. {
  27. struct task_struct *tsk = per_cpu(idle_threads, cpu);
  28. if (!tsk)
  29. return ERR_PTR(-ENOMEM);
  30. init_idle(tsk, cpu);
  31. return tsk;
  32. }
  33. void __init idle_thread_set_boot_cpu(void)
  34. {
  35. per_cpu(idle_threads, smp_processor_id()) = current;
  36. }
  37. /**
  38. * idle_init - Initialize the idle thread for a cpu
  39. * @cpu: The cpu for which the idle thread should be initialized
  40. *
  41. * Creates the thread if it does not exist.
  42. */
  43. static inline void idle_init(unsigned int cpu)
  44. {
  45. struct task_struct *tsk = per_cpu(idle_threads, cpu);
  46. if (!tsk) {
  47. tsk = fork_idle(cpu);
  48. if (IS_ERR(tsk))
  49. pr_err("SMP: fork_idle() failed for CPU %u\n", cpu);
  50. else
  51. per_cpu(idle_threads, cpu) = tsk;
  52. }
  53. }
  54. /**
  55. * idle_threads_init - Initialize idle threads for all cpus
  56. */
  57. void __init idle_threads_init(void)
  58. {
  59. unsigned int cpu, boot_cpu;
  60. boot_cpu = smp_processor_id();
  61. for_each_possible_cpu(cpu) {
  62. if (cpu != boot_cpu)
  63. idle_init(cpu);
  64. }
  65. }
  66. #endif
  67. #endif /* #ifdef CONFIG_SMP */
  68. static LIST_HEAD(hotplug_threads);
  69. static DEFINE_MUTEX(smpboot_threads_lock);
  70. struct smpboot_thread_data {
  71. unsigned int cpu;
  72. unsigned int status;
  73. struct smp_hotplug_thread *ht;
  74. };
  75. enum {
  76. HP_THREAD_NONE = 0,
  77. HP_THREAD_ACTIVE,
  78. HP_THREAD_PARKED,
  79. };
  80. /**
  81. * smpboot_thread_fn - percpu hotplug thread loop function
  82. * @data: thread data pointer
  83. *
  84. * Checks for thread stop and park conditions. Calls the necessary
  85. * setup, cleanup, park and unpark functions for the registered
  86. * thread.
  87. *
  88. * Returns 1 when the thread should exit, 0 otherwise.
  89. */
  90. static int smpboot_thread_fn(void *data)
  91. {
  92. struct smpboot_thread_data *td = data;
  93. struct smp_hotplug_thread *ht = td->ht;
  94. while (1) {
  95. set_current_state(TASK_INTERRUPTIBLE);
  96. preempt_disable();
  97. if (kthread_should_stop()) {
  98. __set_current_state(TASK_RUNNING);
  99. preempt_enable();
  100. /* cleanup must mirror setup */
  101. if (ht->cleanup && td->status != HP_THREAD_NONE)
  102. ht->cleanup(td->cpu, cpu_online(td->cpu));
  103. kfree(td);
  104. return 0;
  105. }
  106. if (kthread_should_park()) {
  107. __set_current_state(TASK_RUNNING);
  108. preempt_enable();
  109. if (ht->park && td->status == HP_THREAD_ACTIVE) {
  110. BUG_ON(td->cpu != smp_processor_id());
  111. ht->park(td->cpu);
  112. td->status = HP_THREAD_PARKED;
  113. }
  114. kthread_parkme();
  115. /* We might have been woken for stop */
  116. continue;
  117. }
  118. BUG_ON(td->cpu != smp_processor_id());
  119. /* Check for state change setup */
  120. switch (td->status) {
  121. case HP_THREAD_NONE:
  122. __set_current_state(TASK_RUNNING);
  123. preempt_enable();
  124. if (ht->setup)
  125. ht->setup(td->cpu);
  126. td->status = HP_THREAD_ACTIVE;
  127. continue;
  128. case HP_THREAD_PARKED:
  129. __set_current_state(TASK_RUNNING);
  130. preempt_enable();
  131. if (ht->unpark)
  132. ht->unpark(td->cpu);
  133. td->status = HP_THREAD_ACTIVE;
  134. continue;
  135. }
  136. if (!ht->thread_should_run(td->cpu)) {
  137. preempt_enable_no_resched();
  138. schedule();
  139. } else {
  140. __set_current_state(TASK_RUNNING);
  141. preempt_enable();
  142. ht->thread_fn(td->cpu);
  143. }
  144. }
  145. }
  146. static int
  147. __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  148. {
  149. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  150. struct smpboot_thread_data *td;
  151. if (tsk)
  152. return 0;
  153. td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu));
  154. if (!td)
  155. return -ENOMEM;
  156. td->cpu = cpu;
  157. td->ht = ht;
  158. tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,
  159. ht->thread_comm);
  160. if (IS_ERR(tsk)) {
  161. kfree(td);
  162. return PTR_ERR(tsk);
  163. }
  164. kthread_set_per_cpu(tsk, cpu);
  165. /*
  166. * Park the thread so that it could start right on the CPU
  167. * when it is available.
  168. */
  169. kthread_park(tsk);
  170. get_task_struct(tsk);
  171. *per_cpu_ptr(ht->store, cpu) = tsk;
  172. if (ht->create) {
  173. /*
  174. * Make sure that the task has actually scheduled out
  175. * into park position, before calling the create
  176. * callback. At least the migration thread callback
  177. * requires that the task is off the runqueue.
  178. */
  179. if (!wait_task_inactive(tsk, TASK_PARKED))
  180. WARN_ON(1);
  181. else
  182. ht->create(cpu);
  183. }
  184. return 0;
  185. }
  186. int smpboot_create_threads(unsigned int cpu)
  187. {
  188. struct smp_hotplug_thread *cur;
  189. int ret = 0;
  190. mutex_lock(&smpboot_threads_lock);
  191. list_for_each_entry(cur, &hotplug_threads, list) {
  192. ret = __smpboot_create_thread(cur, cpu);
  193. if (ret)
  194. break;
  195. }
  196. mutex_unlock(&smpboot_threads_lock);
  197. return ret;
  198. }
  199. static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  200. {
  201. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  202. if (!ht->selfparking)
  203. kthread_unpark(tsk);
  204. }
  205. int smpboot_unpark_threads(unsigned int cpu)
  206. {
  207. struct smp_hotplug_thread *cur;
  208. mutex_lock(&smpboot_threads_lock);
  209. list_for_each_entry(cur, &hotplug_threads, list)
  210. if (cpumask_test_cpu(cpu, cur->cpumask))
  211. smpboot_unpark_thread(cur, cpu);
  212. mutex_unlock(&smpboot_threads_lock);
  213. return 0;
  214. }
  215. static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  216. {
  217. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  218. if (tsk && !ht->selfparking)
  219. kthread_park(tsk);
  220. }
  221. int smpboot_park_threads(unsigned int cpu)
  222. {
  223. struct smp_hotplug_thread *cur;
  224. mutex_lock(&smpboot_threads_lock);
  225. list_for_each_entry_reverse(cur, &hotplug_threads, list)
  226. smpboot_park_thread(cur, cpu);
  227. mutex_unlock(&smpboot_threads_lock);
  228. return 0;
  229. }
  230. static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
  231. {
  232. unsigned int cpu;
  233. /* We need to destroy also the parked threads of offline cpus */
  234. for_each_possible_cpu(cpu) {
  235. struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  236. if (tsk) {
  237. kthread_stop(tsk);
  238. put_task_struct(tsk);
  239. *per_cpu_ptr(ht->store, cpu) = NULL;
  240. }
  241. }
  242. }
  243. /**
  244. * smpboot_register_percpu_thread_cpumask - Register a per_cpu thread related
  245. * to hotplug
  246. * @plug_thread: Hotplug thread descriptor
  247. * @cpumask: The cpumask where threads run
  248. *
  249. * Creates and starts the threads on all online cpus.
  250. */
  251. int smpboot_register_percpu_thread_cpumask(struct smp_hotplug_thread *plug_thread,
  252. const struct cpumask *cpumask)
  253. {
  254. unsigned int cpu;
  255. int ret = 0;
  256. if (!alloc_cpumask_var(&plug_thread->cpumask, GFP_KERNEL))
  257. return -ENOMEM;
  258. cpumask_copy(plug_thread->cpumask, cpumask);
  259. get_online_cpus();
  260. mutex_lock(&smpboot_threads_lock);
  261. for_each_online_cpu(cpu) {
  262. ret = __smpboot_create_thread(plug_thread, cpu);
  263. if (ret) {
  264. smpboot_destroy_threads(plug_thread);
  265. free_cpumask_var(plug_thread->cpumask);
  266. goto out;
  267. }
  268. if (cpumask_test_cpu(cpu, cpumask))
  269. smpboot_unpark_thread(plug_thread, cpu);
  270. }
  271. list_add(&plug_thread->list, &hotplug_threads);
  272. out:
  273. mutex_unlock(&smpboot_threads_lock);
  274. put_online_cpus();
  275. return ret;
  276. }
  277. EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread_cpumask);
  278. /**
  279. * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
  280. * @plug_thread: Hotplug thread descriptor
  281. *
  282. * Stops all threads on all possible cpus.
  283. */
  284. void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  285. {
  286. get_online_cpus();
  287. mutex_lock(&smpboot_threads_lock);
  288. list_del(&plug_thread->list);
  289. smpboot_destroy_threads(plug_thread);
  290. mutex_unlock(&smpboot_threads_lock);
  291. put_online_cpus();
  292. free_cpumask_var(plug_thread->cpumask);
  293. }
  294. EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
  295. /**
  296. * smpboot_update_cpumask_percpu_thread - Adjust which per_cpu hotplug threads stay parked
  297. * @plug_thread: Hotplug thread descriptor
  298. * @new: Revised mask to use
  299. *
  300. * The cpumask field in the smp_hotplug_thread must not be updated directly
  301. * by the client, but only by calling this function.
  302. * This function can only be called on a registered smp_hotplug_thread.
  303. */
  304. void smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
  305. const struct cpumask *new)
  306. {
  307. struct cpumask *old = plug_thread->cpumask;
  308. static struct cpumask tmp;
  309. unsigned int cpu;
  310. lockdep_assert_cpus_held();
  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. }
  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 */