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