stop_machine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. #include <linux/nmi.h>
  25. /*
  26. * Structure to determine completion condition and record errors. May
  27. * be shared by works on different cpus.
  28. */
  29. struct cpu_stop_done {
  30. atomic_t nr_todo; /* nr left to execute */
  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)
  59. {
  60. if (atomic_dec_and_test(&done->nr_todo))
  61. complete(&done->completion);
  62. }
  63. static void __cpu_stop_queue_work(struct cpu_stopper *stopper,
  64. struct cpu_stop_work *work)
  65. {
  66. list_add_tail(&work->list, &stopper->works);
  67. wake_up_process(stopper->thread);
  68. }
  69. /* queue @work to @stopper. if offline, @work is completed immediately */
  70. static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  71. {
  72. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  73. unsigned long flags;
  74. bool enabled;
  75. spin_lock_irqsave(&stopper->lock, flags);
  76. enabled = stopper->enabled;
  77. if (enabled)
  78. __cpu_stop_queue_work(stopper, work);
  79. else if (work->done)
  80. cpu_stop_signal_done(work->done);
  81. spin_unlock_irqrestore(&stopper->lock, flags);
  82. return enabled;
  83. }
  84. /**
  85. * stop_one_cpu - stop a cpu
  86. * @cpu: cpu to stop
  87. * @fn: function to execute
  88. * @arg: argument to @fn
  89. *
  90. * Execute @fn(@arg) on @cpu. @fn is run in a process context with
  91. * the highest priority preempting any task on the cpu and
  92. * monopolizing it. This function returns after the execution is
  93. * complete.
  94. *
  95. * This function doesn't guarantee @cpu stays online till @fn
  96. * completes. If @cpu goes down in the middle, execution may happen
  97. * partially or fully on different cpus. @fn should either be ready
  98. * for that or the caller should ensure that @cpu stays online until
  99. * this function completes.
  100. *
  101. * CONTEXT:
  102. * Might sleep.
  103. *
  104. * RETURNS:
  105. * -ENOENT if @fn(@arg) was not executed because @cpu was offline;
  106. * otherwise, the return value of @fn.
  107. */
  108. int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  109. {
  110. struct cpu_stop_done done;
  111. struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
  112. cpu_stop_init_done(&done, 1);
  113. if (!cpu_stop_queue_work(cpu, &work))
  114. return -ENOENT;
  115. wait_for_completion(&done.completion);
  116. return done.ret;
  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. } else if (curstate > MULTI_STOP_PREPARE) {
  191. /*
  192. * At this stage all other CPUs we depend on must spin
  193. * in the same loop. Any reason for hard-lockup should
  194. * be detected and reported on their side.
  195. */
  196. touch_nmi_watchdog();
  197. }
  198. } while (curstate != MULTI_STOP_EXIT);
  199. local_irq_restore(flags);
  200. return err;
  201. }
  202. static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  203. int cpu2, struct cpu_stop_work *work2)
  204. {
  205. struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1);
  206. struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
  207. int err;
  208. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  209. spin_lock_irq(&stopper1->lock);
  210. spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  211. err = -ENOENT;
  212. if (!stopper1->enabled || !stopper2->enabled)
  213. goto unlock;
  214. err = 0;
  215. __cpu_stop_queue_work(stopper1, work1);
  216. __cpu_stop_queue_work(stopper2, work2);
  217. unlock:
  218. spin_unlock(&stopper2->lock);
  219. spin_unlock_irq(&stopper1->lock);
  220. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  221. return err;
  222. }
  223. /**
  224. * stop_two_cpus - stops two cpus
  225. * @cpu1: the cpu to stop
  226. * @cpu2: the other cpu to stop
  227. * @fn: function to execute
  228. * @arg: argument to @fn
  229. *
  230. * Stops both the current and specified CPU and runs @fn on one of them.
  231. *
  232. * returns when both are completed.
  233. */
  234. int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg)
  235. {
  236. struct cpu_stop_done done;
  237. struct cpu_stop_work work1, work2;
  238. struct multi_stop_data msdata;
  239. msdata = (struct multi_stop_data){
  240. .fn = fn,
  241. .data = arg,
  242. .num_threads = 2,
  243. .active_cpus = cpumask_of(cpu1),
  244. };
  245. work1 = work2 = (struct cpu_stop_work){
  246. .fn = multi_cpu_stop,
  247. .arg = &msdata,
  248. .done = &done
  249. };
  250. cpu_stop_init_done(&done, 2);
  251. set_state(&msdata, MULTI_STOP_PREPARE);
  252. if (cpu1 > cpu2)
  253. swap(cpu1, cpu2);
  254. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2))
  255. return -ENOENT;
  256. wait_for_completion(&done.completion);
  257. return done.ret;
  258. }
  259. /**
  260. * stop_one_cpu_nowait - stop a cpu but don't wait for completion
  261. * @cpu: cpu to stop
  262. * @fn: function to execute
  263. * @arg: argument to @fn
  264. * @work_buf: pointer to cpu_stop_work structure
  265. *
  266. * Similar to stop_one_cpu() but doesn't wait for completion. The
  267. * caller is responsible for ensuring @work_buf is currently unused
  268. * and will remain untouched until stopper starts executing @fn.
  269. *
  270. * CONTEXT:
  271. * Don't care.
  272. *
  273. * RETURNS:
  274. * true if cpu_stop_work was queued successfully and @fn will be called,
  275. * false otherwise.
  276. */
  277. bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  278. struct cpu_stop_work *work_buf)
  279. {
  280. *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, };
  281. return cpu_stop_queue_work(cpu, work_buf);
  282. }
  283. /* static data for stop_cpus */
  284. static DEFINE_MUTEX(stop_cpus_mutex);
  285. static bool queue_stop_cpus_work(const struct cpumask *cpumask,
  286. cpu_stop_fn_t fn, void *arg,
  287. struct cpu_stop_done *done)
  288. {
  289. struct cpu_stop_work *work;
  290. unsigned int cpu;
  291. bool queued = false;
  292. /*
  293. * Disable preemption while queueing to avoid getting
  294. * preempted by a stopper which might wait for other stoppers
  295. * to enter @fn which can lead to deadlock.
  296. */
  297. lg_global_lock(&stop_cpus_lock);
  298. for_each_cpu(cpu, cpumask) {
  299. work = &per_cpu(cpu_stopper.stop_work, cpu);
  300. work->fn = fn;
  301. work->arg = arg;
  302. work->done = done;
  303. if (cpu_stop_queue_work(cpu, work))
  304. queued = true;
  305. }
  306. lg_global_unlock(&stop_cpus_lock);
  307. return queued;
  308. }
  309. static int __stop_cpus(const struct cpumask *cpumask,
  310. cpu_stop_fn_t fn, void *arg)
  311. {
  312. struct cpu_stop_done done;
  313. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  314. if (!queue_stop_cpus_work(cpumask, fn, arg, &done))
  315. return -ENOENT;
  316. wait_for_completion(&done.completion);
  317. return done.ret;
  318. }
  319. /**
  320. * stop_cpus - stop multiple cpus
  321. * @cpumask: cpus to stop
  322. * @fn: function to execute
  323. * @arg: argument to @fn
  324. *
  325. * Execute @fn(@arg) on online cpus in @cpumask. On each target cpu,
  326. * @fn is run in a process context with the highest priority
  327. * preempting any task on the cpu and monopolizing it. This function
  328. * returns after all executions are complete.
  329. *
  330. * This function doesn't guarantee the cpus in @cpumask stay online
  331. * till @fn completes. If some cpus go down in the middle, execution
  332. * on the cpu may happen partially or fully on different cpus. @fn
  333. * should either be ready for that or the caller should ensure that
  334. * the cpus stay online until this function completes.
  335. *
  336. * All stop_cpus() calls are serialized making it safe for @fn to wait
  337. * for all cpus to start executing it.
  338. *
  339. * CONTEXT:
  340. * Might sleep.
  341. *
  342. * RETURNS:
  343. * -ENOENT if @fn(@arg) was not executed at all because all cpus in
  344. * @cpumask were offline; otherwise, 0 if all executions of @fn
  345. * returned 0, any non zero return value if any returned non zero.
  346. */
  347. int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  348. {
  349. int ret;
  350. /* static works are used, process one request at a time */
  351. mutex_lock(&stop_cpus_mutex);
  352. ret = __stop_cpus(cpumask, fn, arg);
  353. mutex_unlock(&stop_cpus_mutex);
  354. return ret;
  355. }
  356. /**
  357. * try_stop_cpus - try to stop multiple cpus
  358. * @cpumask: cpus to stop
  359. * @fn: function to execute
  360. * @arg: argument to @fn
  361. *
  362. * Identical to stop_cpus() except that it fails with -EAGAIN if
  363. * someone else is already using the facility.
  364. *
  365. * CONTEXT:
  366. * Might sleep.
  367. *
  368. * RETURNS:
  369. * -EAGAIN if someone else is already stopping cpus, -ENOENT if
  370. * @fn(@arg) was not executed at all because all cpus in @cpumask were
  371. * offline; otherwise, 0 if all executions of @fn returned 0, any non
  372. * zero return value if any returned non zero.
  373. */
  374. int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  375. {
  376. int ret;
  377. /* static works are used, process one request at a time */
  378. if (!mutex_trylock(&stop_cpus_mutex))
  379. return -EAGAIN;
  380. ret = __stop_cpus(cpumask, fn, arg);
  381. mutex_unlock(&stop_cpus_mutex);
  382. return ret;
  383. }
  384. static int cpu_stop_should_run(unsigned int cpu)
  385. {
  386. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  387. unsigned long flags;
  388. int run;
  389. spin_lock_irqsave(&stopper->lock, flags);
  390. run = !list_empty(&stopper->works);
  391. spin_unlock_irqrestore(&stopper->lock, flags);
  392. return run;
  393. }
  394. static void cpu_stopper_thread(unsigned int cpu)
  395. {
  396. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  397. struct cpu_stop_work *work;
  398. repeat:
  399. work = NULL;
  400. spin_lock_irq(&stopper->lock);
  401. if (!list_empty(&stopper->works)) {
  402. work = list_first_entry(&stopper->works,
  403. struct cpu_stop_work, list);
  404. list_del_init(&work->list);
  405. }
  406. spin_unlock_irq(&stopper->lock);
  407. if (work) {
  408. cpu_stop_fn_t fn = work->fn;
  409. void *arg = work->arg;
  410. struct cpu_stop_done *done = work->done;
  411. int ret;
  412. /* cpu stop callbacks must not sleep, make in_atomic() == T */
  413. preempt_count_inc();
  414. ret = fn(arg);
  415. if (done) {
  416. if (ret)
  417. done->ret = ret;
  418. cpu_stop_signal_done(done);
  419. }
  420. preempt_count_dec();
  421. WARN_ONCE(preempt_count(),
  422. "cpu_stop: %pf(%p) leaked preempt count\n", fn, arg);
  423. goto repeat;
  424. }
  425. }
  426. void stop_machine_park(int cpu)
  427. {
  428. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  429. /*
  430. * Lockless. cpu_stopper_thread() will take stopper->lock and flush
  431. * the pending works before it parks, until then it is fine to queue
  432. * the new works.
  433. */
  434. stopper->enabled = false;
  435. kthread_park(stopper->thread);
  436. }
  437. extern void sched_set_stop_task(int cpu, struct task_struct *stop);
  438. static void cpu_stop_create(unsigned int cpu)
  439. {
  440. sched_set_stop_task(cpu, per_cpu(cpu_stopper.thread, cpu));
  441. }
  442. static void cpu_stop_park(unsigned int cpu)
  443. {
  444. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  445. WARN_ON(!list_empty(&stopper->works));
  446. }
  447. void stop_machine_unpark(int cpu)
  448. {
  449. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  450. stopper->enabled = true;
  451. kthread_unpark(stopper->thread);
  452. }
  453. static struct smp_hotplug_thread cpu_stop_threads = {
  454. .store = &cpu_stopper.thread,
  455. .thread_should_run = cpu_stop_should_run,
  456. .thread_fn = cpu_stopper_thread,
  457. .thread_comm = "migration/%u",
  458. .create = cpu_stop_create,
  459. .park = cpu_stop_park,
  460. .selfparking = true,
  461. };
  462. static int __init cpu_stop_init(void)
  463. {
  464. unsigned int cpu;
  465. for_each_possible_cpu(cpu) {
  466. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  467. spin_lock_init(&stopper->lock);
  468. INIT_LIST_HEAD(&stopper->works);
  469. }
  470. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  471. stop_machine_unpark(raw_smp_processor_id());
  472. stop_machine_initialized = true;
  473. return 0;
  474. }
  475. early_initcall(cpu_stop_init);
  476. static int __stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
  477. {
  478. struct multi_stop_data msdata = {
  479. .fn = fn,
  480. .data = data,
  481. .num_threads = num_online_cpus(),
  482. .active_cpus = cpus,
  483. };
  484. if (!stop_machine_initialized) {
  485. /*
  486. * Handle the case where stop_machine() is called
  487. * early in boot before stop_machine() has been
  488. * initialized.
  489. */
  490. unsigned long flags;
  491. int ret;
  492. WARN_ON_ONCE(msdata.num_threads != 1);
  493. local_irq_save(flags);
  494. hard_irq_disable();
  495. ret = (*fn)(data);
  496. local_irq_restore(flags);
  497. return ret;
  498. }
  499. /* Set the initial state and stop all online cpus. */
  500. set_state(&msdata, MULTI_STOP_PREPARE);
  501. return stop_cpus(cpu_online_mask, multi_cpu_stop, &msdata);
  502. }
  503. int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
  504. {
  505. int ret;
  506. /* No CPUs can come up or down during this. */
  507. get_online_cpus();
  508. ret = __stop_machine(fn, data, cpus);
  509. put_online_cpus();
  510. return ret;
  511. }
  512. EXPORT_SYMBOL_GPL(stop_machine);
  513. /**
  514. * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
  515. * @fn: the function to run
  516. * @data: the data ptr for the @fn()
  517. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  518. *
  519. * This is identical to stop_machine() but can be called from a CPU which
  520. * is not active. The local CPU is in the process of hotplug (so no other
  521. * CPU hotplug can start) and not marked active and doesn't have enough
  522. * context to sleep.
  523. *
  524. * This function provides stop_machine() functionality for such state by
  525. * using busy-wait for synchronization and executing @fn directly for local
  526. * CPU.
  527. *
  528. * CONTEXT:
  529. * Local CPU is inactive. Temporarily stops all active CPUs.
  530. *
  531. * RETURNS:
  532. * 0 if all executions of @fn returned 0, any non zero return value if any
  533. * returned non zero.
  534. */
  535. int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  536. const struct cpumask *cpus)
  537. {
  538. struct multi_stop_data msdata = { .fn = fn, .data = data,
  539. .active_cpus = cpus };
  540. struct cpu_stop_done done;
  541. int ret;
  542. /* Local CPU must be inactive and CPU hotplug in progress. */
  543. BUG_ON(cpu_active(raw_smp_processor_id()));
  544. msdata.num_threads = num_active_cpus() + 1; /* +1 for local */
  545. /* No proper task established and can't sleep - busy wait for lock. */
  546. while (!mutex_trylock(&stop_cpus_mutex))
  547. cpu_relax();
  548. /* Schedule work on other CPUs and execute directly for local CPU */
  549. set_state(&msdata, MULTI_STOP_PREPARE);
  550. cpu_stop_init_done(&done, num_active_cpus());
  551. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  552. &done);
  553. ret = multi_cpu_stop(&msdata);
  554. /* Busy wait for completion. */
  555. while (!completion_done(&done.completion))
  556. cpu_relax();
  557. mutex_unlock(&stop_cpus_mutex);
  558. return ret ?: done.ret;
  559. }