stop_machine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * kernel/stop_machine.c
  3. *
  4. * Copyright (C) 2008, 2005 IBM Corporation.
  5. * Copyright (C) 2008, 2005 Rusty Russell rusty@rustcorp.com.au
  6. * Copyright (C) 2010 SUSE Linux Products GmbH
  7. * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
  8. *
  9. * This file is released under the GPLv2 and any later version.
  10. */
  11. #include <linux/completion.h>
  12. #include <linux/cpu.h>
  13. #include <linux/init.h>
  14. #include <linux/kthread.h>
  15. #include <linux/export.h>
  16. #include <linux/percpu.h>
  17. #include <linux/sched.h>
  18. #include <linux/stop_machine.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/smpboot.h>
  22. #include <linux/atomic.h>
  23. #include <linux/lglock.h>
  24. /*
  25. * Structure to determine completion condition and record errors. May
  26. * be shared by works on different cpus.
  27. */
  28. struct cpu_stop_done {
  29. atomic_t nr_todo; /* nr left to execute */
  30. bool executed; /* actually executed? */
  31. int ret; /* collected return value */
  32. struct completion completion; /* fired if nr_todo reaches 0 */
  33. };
  34. /* the actual stopper, one per every possible cpu, enabled on online cpus */
  35. struct cpu_stopper {
  36. struct task_struct *thread;
  37. spinlock_t lock;
  38. bool enabled; /* is this stopper enabled? */
  39. struct list_head works; /* list of pending works */
  40. struct cpu_stop_work stop_work; /* for stop_cpus */
  41. };
  42. static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper);
  43. static bool stop_machine_initialized = false;
  44. /*
  45. * Avoids a race between stop_two_cpus and global stop_cpus, where
  46. * the stoppers could get queued up in reverse order, leading to
  47. * system deadlock. Using an lglock means stop_two_cpus remains
  48. * relatively cheap.
  49. */
  50. DEFINE_STATIC_LGLOCK(stop_cpus_lock);
  51. static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo)
  52. {
  53. memset(done, 0, sizeof(*done));
  54. atomic_set(&done->nr_todo, nr_todo);
  55. init_completion(&done->completion);
  56. }
  57. /* signal completion unless @done is NULL */
  58. static void cpu_stop_signal_done(struct cpu_stop_done *done, bool executed)
  59. {
  60. if (done) {
  61. if (executed)
  62. done->executed = true;
  63. if (atomic_dec_and_test(&done->nr_todo))
  64. complete(&done->completion);
  65. }
  66. }
  67. static void __cpu_stop_queue_work(struct cpu_stopper *stopper,
  68. struct cpu_stop_work *work)
  69. {
  70. list_add_tail(&work->list, &stopper->works);
  71. wake_up_process(stopper->thread);
  72. }
  73. /* queue @work to @stopper. if offline, @work is completed immediately */
  74. static void cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  75. {
  76. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  77. unsigned long flags;
  78. spin_lock_irqsave(&stopper->lock, flags);
  79. if (stopper->enabled)
  80. __cpu_stop_queue_work(stopper, work);
  81. else
  82. cpu_stop_signal_done(work->done, false);
  83. spin_unlock_irqrestore(&stopper->lock, flags);
  84. }
  85. /**
  86. * stop_one_cpu - stop a cpu
  87. * @cpu: cpu to stop
  88. * @fn: function to execute
  89. * @arg: argument to @fn
  90. *
  91. * Execute @fn(@arg) on @cpu. @fn is run in a process context with
  92. * the highest priority preempting any task on the cpu and
  93. * monopolizing it. This function returns after the execution is
  94. * complete.
  95. *
  96. * This function doesn't guarantee @cpu stays online till @fn
  97. * completes. If @cpu goes down in the middle, execution may happen
  98. * partially or fully on different cpus. @fn should either be ready
  99. * for that or the caller should ensure that @cpu stays online until
  100. * this function completes.
  101. *
  102. * CONTEXT:
  103. * Might sleep.
  104. *
  105. * RETURNS:
  106. * -ENOENT if @fn(@arg) was not executed because @cpu was offline;
  107. * otherwise, the return value of @fn.
  108. */
  109. int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  110. {
  111. struct cpu_stop_done done;
  112. struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
  113. cpu_stop_init_done(&done, 1);
  114. cpu_stop_queue_work(cpu, &work);
  115. wait_for_completion(&done.completion);
  116. return done.executed ? done.ret : -ENOENT;
  117. }
  118. /* This controls the threads on each CPU. */
  119. enum multi_stop_state {
  120. /* Dummy starting state for thread. */
  121. MULTI_STOP_NONE,
  122. /* Awaiting everyone to be scheduled. */
  123. MULTI_STOP_PREPARE,
  124. /* Disable interrupts. */
  125. MULTI_STOP_DISABLE_IRQ,
  126. /* Run the function */
  127. MULTI_STOP_RUN,
  128. /* Exit */
  129. MULTI_STOP_EXIT,
  130. };
  131. struct multi_stop_data {
  132. cpu_stop_fn_t fn;
  133. void *data;
  134. /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
  135. unsigned int num_threads;
  136. const struct cpumask *active_cpus;
  137. enum multi_stop_state state;
  138. atomic_t thread_ack;
  139. };
  140. static void set_state(struct multi_stop_data *msdata,
  141. enum multi_stop_state newstate)
  142. {
  143. /* Reset ack counter. */
  144. atomic_set(&msdata->thread_ack, msdata->num_threads);
  145. smp_wmb();
  146. msdata->state = newstate;
  147. }
  148. /* Last one to ack a state moves to the next state. */
  149. static void ack_state(struct multi_stop_data *msdata)
  150. {
  151. if (atomic_dec_and_test(&msdata->thread_ack))
  152. set_state(msdata, msdata->state + 1);
  153. }
  154. /* This is the cpu_stop function which stops the CPU. */
  155. static int multi_cpu_stop(void *data)
  156. {
  157. struct multi_stop_data *msdata = data;
  158. enum multi_stop_state curstate = MULTI_STOP_NONE;
  159. int cpu = smp_processor_id(), err = 0;
  160. unsigned long flags;
  161. bool is_active;
  162. /*
  163. * When called from stop_machine_from_inactive_cpu(), irq might
  164. * already be disabled. Save the state and restore it on exit.
  165. */
  166. local_save_flags(flags);
  167. if (!msdata->active_cpus)
  168. is_active = cpu == cpumask_first(cpu_online_mask);
  169. else
  170. is_active = cpumask_test_cpu(cpu, msdata->active_cpus);
  171. /* Simple state machine */
  172. do {
  173. /* Chill out and ensure we re-read multi_stop_state. */
  174. cpu_relax();
  175. if (msdata->state != curstate) {
  176. curstate = msdata->state;
  177. switch (curstate) {
  178. case MULTI_STOP_DISABLE_IRQ:
  179. local_irq_disable();
  180. hard_irq_disable();
  181. break;
  182. case MULTI_STOP_RUN:
  183. if (is_active)
  184. err = msdata->fn(msdata->data);
  185. break;
  186. default:
  187. break;
  188. }
  189. ack_state(msdata);
  190. }
  191. } while (curstate != MULTI_STOP_EXIT);
  192. local_irq_restore(flags);
  193. return err;
  194. }
  195. static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  196. int cpu2, struct cpu_stop_work *work2)
  197. {
  198. struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1);
  199. struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
  200. int err;
  201. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  202. spin_lock_irq(&stopper1->lock);
  203. spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  204. err = -ENOENT;
  205. if (!stopper1->enabled || !stopper2->enabled)
  206. goto unlock;
  207. err = 0;
  208. __cpu_stop_queue_work(stopper1, work1);
  209. __cpu_stop_queue_work(stopper2, work2);
  210. unlock:
  211. spin_unlock(&stopper2->lock);
  212. spin_unlock_irq(&stopper1->lock);
  213. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  214. return err;
  215. }
  216. /**
  217. * stop_two_cpus - stops two cpus
  218. * @cpu1: the cpu to stop
  219. * @cpu2: the other cpu to stop
  220. * @fn: function to execute
  221. * @arg: argument to @fn
  222. *
  223. * Stops both the current and specified CPU and runs @fn on one of them.
  224. *
  225. * returns when both are completed.
  226. */
  227. int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg)
  228. {
  229. struct cpu_stop_done done;
  230. struct cpu_stop_work work1, work2;
  231. struct multi_stop_data msdata;
  232. preempt_disable();
  233. msdata = (struct multi_stop_data){
  234. .fn = fn,
  235. .data = arg,
  236. .num_threads = 2,
  237. .active_cpus = cpumask_of(cpu1),
  238. };
  239. work1 = work2 = (struct cpu_stop_work){
  240. .fn = multi_cpu_stop,
  241. .arg = &msdata,
  242. .done = &done
  243. };
  244. cpu_stop_init_done(&done, 2);
  245. set_state(&msdata, MULTI_STOP_PREPARE);
  246. if (cpu1 > cpu2)
  247. swap(cpu1, cpu2);
  248. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) {
  249. preempt_enable();
  250. return -ENOENT;
  251. }
  252. preempt_enable();
  253. wait_for_completion(&done.completion);
  254. return done.executed ? done.ret : -ENOENT;
  255. }
  256. /**
  257. * stop_one_cpu_nowait - stop a cpu but don't wait for completion
  258. * @cpu: cpu to stop
  259. * @fn: function to execute
  260. * @arg: argument to @fn
  261. * @work_buf: pointer to cpu_stop_work structure
  262. *
  263. * Similar to stop_one_cpu() but doesn't wait for completion. The
  264. * caller is responsible for ensuring @work_buf is currently unused
  265. * and will remain untouched until stopper starts executing @fn.
  266. *
  267. * CONTEXT:
  268. * Don't care.
  269. */
  270. void stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  271. struct cpu_stop_work *work_buf)
  272. {
  273. *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, };
  274. cpu_stop_queue_work(cpu, work_buf);
  275. }
  276. /* static data for stop_cpus */
  277. static DEFINE_MUTEX(stop_cpus_mutex);
  278. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  279. cpu_stop_fn_t fn, void *arg,
  280. struct cpu_stop_done *done)
  281. {
  282. struct cpu_stop_work *work;
  283. unsigned int cpu;
  284. /*
  285. * Disable preemption while queueing to avoid getting
  286. * preempted by a stopper which might wait for other stoppers
  287. * to enter @fn which can lead to deadlock.
  288. */
  289. lg_global_lock(&stop_cpus_lock);
  290. for_each_cpu(cpu, cpumask) {
  291. work = &per_cpu(cpu_stopper.stop_work, cpu);
  292. work->fn = fn;
  293. work->arg = arg;
  294. work->done = done;
  295. cpu_stop_queue_work(cpu, work);
  296. }
  297. lg_global_unlock(&stop_cpus_lock);
  298. }
  299. static int __stop_cpus(const struct cpumask *cpumask,
  300. cpu_stop_fn_t fn, void *arg)
  301. {
  302. struct cpu_stop_done done;
  303. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  304. queue_stop_cpus_work(cpumask, fn, arg, &done);
  305. wait_for_completion(&done.completion);
  306. return done.executed ? done.ret : -ENOENT;
  307. }
  308. /**
  309. * stop_cpus - stop multiple cpus
  310. * @cpumask: cpus to stop
  311. * @fn: function to execute
  312. * @arg: argument to @fn
  313. *
  314. * Execute @fn(@arg) on online cpus in @cpumask. On each target cpu,
  315. * @fn is run in a process context with the highest priority
  316. * preempting any task on the cpu and monopolizing it. This function
  317. * returns after all executions are complete.
  318. *
  319. * This function doesn't guarantee the cpus in @cpumask stay online
  320. * till @fn completes. If some cpus go down in the middle, execution
  321. * on the cpu may happen partially or fully on different cpus. @fn
  322. * should either be ready for that or the caller should ensure that
  323. * the cpus stay online until this function completes.
  324. *
  325. * All stop_cpus() calls are serialized making it safe for @fn to wait
  326. * for all cpus to start executing it.
  327. *
  328. * CONTEXT:
  329. * Might sleep.
  330. *
  331. * RETURNS:
  332. * -ENOENT if @fn(@arg) was not executed at all because all cpus in
  333. * @cpumask were offline; otherwise, 0 if all executions of @fn
  334. * returned 0, any non zero return value if any returned non zero.
  335. */
  336. int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  337. {
  338. int ret;
  339. /* static works are used, process one request at a time */
  340. mutex_lock(&stop_cpus_mutex);
  341. ret = __stop_cpus(cpumask, fn, arg);
  342. mutex_unlock(&stop_cpus_mutex);
  343. return ret;
  344. }
  345. /**
  346. * try_stop_cpus - try to stop multiple cpus
  347. * @cpumask: cpus to stop
  348. * @fn: function to execute
  349. * @arg: argument to @fn
  350. *
  351. * Identical to stop_cpus() except that it fails with -EAGAIN if
  352. * someone else is already using the facility.
  353. *
  354. * CONTEXT:
  355. * Might sleep.
  356. *
  357. * RETURNS:
  358. * -EAGAIN if someone else is already stopping cpus, -ENOENT if
  359. * @fn(@arg) was not executed at all because all cpus in @cpumask were
  360. * offline; otherwise, 0 if all executions of @fn returned 0, any non
  361. * zero return value if any returned non zero.
  362. */
  363. int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  364. {
  365. int ret;
  366. /* static works are used, process one request at a time */
  367. if (!mutex_trylock(&stop_cpus_mutex))
  368. return -EAGAIN;
  369. ret = __stop_cpus(cpumask, fn, arg);
  370. mutex_unlock(&stop_cpus_mutex);
  371. return ret;
  372. }
  373. static int cpu_stop_should_run(unsigned int cpu)
  374. {
  375. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  376. unsigned long flags;
  377. int run;
  378. spin_lock_irqsave(&stopper->lock, flags);
  379. run = !list_empty(&stopper->works);
  380. spin_unlock_irqrestore(&stopper->lock, flags);
  381. return run;
  382. }
  383. static void cpu_stopper_thread(unsigned int cpu)
  384. {
  385. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  386. struct cpu_stop_work *work;
  387. int ret;
  388. repeat:
  389. work = NULL;
  390. spin_lock_irq(&stopper->lock);
  391. if (!list_empty(&stopper->works)) {
  392. work = list_first_entry(&stopper->works,
  393. struct cpu_stop_work, list);
  394. list_del_init(&work->list);
  395. }
  396. spin_unlock_irq(&stopper->lock);
  397. if (work) {
  398. cpu_stop_fn_t fn = work->fn;
  399. void *arg = work->arg;
  400. struct cpu_stop_done *done = work->done;
  401. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  402. /* cpu stop callbacks are not allowed to sleep */
  403. preempt_disable();
  404. ret = fn(arg);
  405. if (ret)
  406. done->ret = ret;
  407. /* restore preemption and check it's still balanced */
  408. preempt_enable();
  409. WARN_ONCE(preempt_count(),
  410. "cpu_stop: %s(%p) leaked preempt count\n",
  411. kallsyms_lookup((unsigned long)fn, NULL, NULL, NULL,
  412. ksym_buf), arg);
  413. cpu_stop_signal_done(done, true);
  414. goto repeat;
  415. }
  416. }
  417. void stop_machine_park(int cpu)
  418. {
  419. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  420. /*
  421. * Lockless. cpu_stopper_thread() will take stopper->lock and flush
  422. * the pending works before it parks, until then it is fine to queue
  423. * the new works.
  424. */
  425. stopper->enabled = false;
  426. kthread_park(stopper->thread);
  427. }
  428. extern void sched_set_stop_task(int cpu, struct task_struct *stop);
  429. static void cpu_stop_create(unsigned int cpu)
  430. {
  431. sched_set_stop_task(cpu, per_cpu(cpu_stopper.thread, cpu));
  432. }
  433. static void cpu_stop_park(unsigned int cpu)
  434. {
  435. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  436. WARN_ON(!list_empty(&stopper->works));
  437. }
  438. void stop_machine_unpark(int cpu)
  439. {
  440. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  441. stopper->enabled = true;
  442. kthread_unpark(stopper->thread);
  443. }
  444. static struct smp_hotplug_thread cpu_stop_threads = {
  445. .store = &cpu_stopper.thread,
  446. .thread_should_run = cpu_stop_should_run,
  447. .thread_fn = cpu_stopper_thread,
  448. .thread_comm = "migration/%u",
  449. .create = cpu_stop_create,
  450. .park = cpu_stop_park,
  451. .selfparking = true,
  452. };
  453. static int __init cpu_stop_init(void)
  454. {
  455. unsigned int cpu;
  456. for_each_possible_cpu(cpu) {
  457. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  458. spin_lock_init(&stopper->lock);
  459. INIT_LIST_HEAD(&stopper->works);
  460. }
  461. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  462. stop_machine_unpark(raw_smp_processor_id());
  463. stop_machine_initialized = true;
  464. return 0;
  465. }
  466. early_initcall(cpu_stop_init);
  467. #if defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
  468. static int __stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
  469. {
  470. struct multi_stop_data msdata = {
  471. .fn = fn,
  472. .data = data,
  473. .num_threads = num_online_cpus(),
  474. .active_cpus = cpus,
  475. };
  476. if (!stop_machine_initialized) {
  477. /*
  478. * Handle the case where stop_machine() is called
  479. * early in boot before stop_machine() has been
  480. * initialized.
  481. */
  482. unsigned long flags;
  483. int ret;
  484. WARN_ON_ONCE(msdata.num_threads != 1);
  485. local_irq_save(flags);
  486. hard_irq_disable();
  487. ret = (*fn)(data);
  488. local_irq_restore(flags);
  489. return ret;
  490. }
  491. /* Set the initial state and stop all online cpus. */
  492. set_state(&msdata, MULTI_STOP_PREPARE);
  493. return stop_cpus(cpu_online_mask, multi_cpu_stop, &msdata);
  494. }
  495. int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
  496. {
  497. int ret;
  498. /* No CPUs can come up or down during this. */
  499. get_online_cpus();
  500. ret = __stop_machine(fn, data, cpus);
  501. put_online_cpus();
  502. return ret;
  503. }
  504. EXPORT_SYMBOL_GPL(stop_machine);
  505. /**
  506. * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
  507. * @fn: the function to run
  508. * @data: the data ptr for the @fn()
  509. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  510. *
  511. * This is identical to stop_machine() but can be called from a CPU which
  512. * is not active. The local CPU is in the process of hotplug (so no other
  513. * CPU hotplug can start) and not marked active and doesn't have enough
  514. * context to sleep.
  515. *
  516. * This function provides stop_machine() functionality for such state by
  517. * using busy-wait for synchronization and executing @fn directly for local
  518. * CPU.
  519. *
  520. * CONTEXT:
  521. * Local CPU is inactive. Temporarily stops all active CPUs.
  522. *
  523. * RETURNS:
  524. * 0 if all executions of @fn returned 0, any non zero return value if any
  525. * returned non zero.
  526. */
  527. int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  528. const struct cpumask *cpus)
  529. {
  530. struct multi_stop_data msdata = { .fn = fn, .data = data,
  531. .active_cpus = cpus };
  532. struct cpu_stop_done done;
  533. int ret;
  534. /* Local CPU must be inactive and CPU hotplug in progress. */
  535. BUG_ON(cpu_active(raw_smp_processor_id()));
  536. msdata.num_threads = num_active_cpus() + 1; /* +1 for local */
  537. /* No proper task established and can't sleep - busy wait for lock. */
  538. while (!mutex_trylock(&stop_cpus_mutex))
  539. cpu_relax();
  540. /* Schedule work on other CPUs and execute directly for local CPU */
  541. set_state(&msdata, MULTI_STOP_PREPARE);
  542. cpu_stop_init_done(&done, num_active_cpus());
  543. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  544. &done);
  545. ret = multi_cpu_stop(&msdata);
  546. /* Busy wait for completion. */
  547. while (!completion_done(&done.completion))
  548. cpu_relax();
  549. mutex_unlock(&stop_cpus_mutex);
  550. return ret ?: done.ret;
  551. }
  552. #endif /* CONFIG_SMP || CONFIG_HOTPLUG_CPU */