stop_machine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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/nmi.h>
  24. #include <linux/sched/wake_q.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. raw_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. /* static data for stop_cpus */
  45. static DEFINE_MUTEX(stop_cpus_mutex);
  46. static bool stop_cpus_in_progress;
  47. static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo)
  48. {
  49. memset(done, 0, sizeof(*done));
  50. atomic_set(&done->nr_todo, nr_todo);
  51. init_completion(&done->completion);
  52. }
  53. /* signal completion unless @done is NULL */
  54. static void cpu_stop_signal_done(struct cpu_stop_done *done)
  55. {
  56. if (atomic_dec_and_test(&done->nr_todo))
  57. complete(&done->completion);
  58. }
  59. static void __cpu_stop_queue_work(struct cpu_stopper *stopper,
  60. struct cpu_stop_work *work,
  61. struct wake_q_head *wakeq)
  62. {
  63. list_add_tail(&work->list, &stopper->works);
  64. wake_q_add(wakeq, stopper->thread);
  65. }
  66. /* queue @work to @stopper. if offline, @work is completed immediately */
  67. static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  68. {
  69. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  70. DEFINE_WAKE_Q(wakeq);
  71. unsigned long flags;
  72. bool enabled;
  73. raw_spin_lock_irqsave(&stopper->lock, flags);
  74. enabled = stopper->enabled;
  75. if (enabled)
  76. __cpu_stop_queue_work(stopper, work, &wakeq);
  77. else if (work->done)
  78. cpu_stop_signal_done(work->done);
  79. raw_spin_unlock_irqrestore(&stopper->lock, flags);
  80. wake_up_q(&wakeq);
  81. return enabled;
  82. }
  83. /**
  84. * stop_one_cpu - stop a cpu
  85. * @cpu: cpu to stop
  86. * @fn: function to execute
  87. * @arg: argument to @fn
  88. *
  89. * Execute @fn(@arg) on @cpu. @fn is run in a process context with
  90. * the highest priority preempting any task on the cpu and
  91. * monopolizing it. This function returns after the execution is
  92. * complete.
  93. *
  94. * This function doesn't guarantee @cpu stays online till @fn
  95. * completes. If @cpu goes down in the middle, execution may happen
  96. * partially or fully on different cpus. @fn should either be ready
  97. * for that or the caller should ensure that @cpu stays online until
  98. * this function completes.
  99. *
  100. * CONTEXT:
  101. * Might sleep.
  102. *
  103. * RETURNS:
  104. * -ENOENT if @fn(@arg) was not executed because @cpu was offline;
  105. * otherwise, the return value of @fn.
  106. */
  107. int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  108. {
  109. struct cpu_stop_done done;
  110. struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
  111. cpu_stop_init_done(&done, 1);
  112. if (!cpu_stop_queue_work(cpu, &work))
  113. return -ENOENT;
  114. /*
  115. * In case @cpu == smp_proccessor_id() we can avoid a sleep+wakeup
  116. * cycle by doing a preemption:
  117. */
  118. cond_resched();
  119. wait_for_completion(&done.completion);
  120. return done.ret;
  121. }
  122. /* This controls the threads on each CPU. */
  123. enum multi_stop_state {
  124. /* Dummy starting state for thread. */
  125. MULTI_STOP_NONE,
  126. /* Awaiting everyone to be scheduled. */
  127. MULTI_STOP_PREPARE,
  128. /* Disable interrupts. */
  129. MULTI_STOP_DISABLE_IRQ,
  130. /* Run the function */
  131. MULTI_STOP_RUN,
  132. /* Exit */
  133. MULTI_STOP_EXIT,
  134. };
  135. struct multi_stop_data {
  136. cpu_stop_fn_t fn;
  137. void *data;
  138. /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
  139. unsigned int num_threads;
  140. const struct cpumask *active_cpus;
  141. enum multi_stop_state state;
  142. atomic_t thread_ack;
  143. };
  144. static void set_state(struct multi_stop_data *msdata,
  145. enum multi_stop_state newstate)
  146. {
  147. /* Reset ack counter. */
  148. atomic_set(&msdata->thread_ack, msdata->num_threads);
  149. smp_wmb();
  150. msdata->state = newstate;
  151. }
  152. /* Last one to ack a state moves to the next state. */
  153. static void ack_state(struct multi_stop_data *msdata)
  154. {
  155. if (atomic_dec_and_test(&msdata->thread_ack))
  156. set_state(msdata, msdata->state + 1);
  157. }
  158. /* This is the cpu_stop function which stops the CPU. */
  159. static int multi_cpu_stop(void *data)
  160. {
  161. struct multi_stop_data *msdata = data;
  162. enum multi_stop_state curstate = MULTI_STOP_NONE;
  163. int cpu = smp_processor_id(), err = 0;
  164. unsigned long flags;
  165. bool is_active;
  166. /*
  167. * When called from stop_machine_from_inactive_cpu(), irq might
  168. * already be disabled. Save the state and restore it on exit.
  169. */
  170. local_save_flags(flags);
  171. if (!msdata->active_cpus)
  172. is_active = cpu == cpumask_first(cpu_online_mask);
  173. else
  174. is_active = cpumask_test_cpu(cpu, msdata->active_cpus);
  175. /* Simple state machine */
  176. do {
  177. /* Chill out and ensure we re-read multi_stop_state. */
  178. cpu_relax_yield();
  179. if (msdata->state != curstate) {
  180. curstate = msdata->state;
  181. switch (curstate) {
  182. case MULTI_STOP_DISABLE_IRQ:
  183. local_irq_disable();
  184. hard_irq_disable();
  185. break;
  186. case MULTI_STOP_RUN:
  187. if (is_active)
  188. err = msdata->fn(msdata->data);
  189. break;
  190. default:
  191. break;
  192. }
  193. ack_state(msdata);
  194. } else if (curstate > MULTI_STOP_PREPARE) {
  195. /*
  196. * At this stage all other CPUs we depend on must spin
  197. * in the same loop. Any reason for hard-lockup should
  198. * be detected and reported on their side.
  199. */
  200. touch_nmi_watchdog();
  201. }
  202. } while (curstate != MULTI_STOP_EXIT);
  203. local_irq_restore(flags);
  204. return err;
  205. }
  206. static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  207. int cpu2, struct cpu_stop_work *work2)
  208. {
  209. struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1);
  210. struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
  211. DEFINE_WAKE_Q(wakeq);
  212. int err;
  213. retry:
  214. raw_spin_lock_irq(&stopper1->lock);
  215. raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  216. err = -ENOENT;
  217. if (!stopper1->enabled || !stopper2->enabled)
  218. goto unlock;
  219. /*
  220. * Ensure that if we race with __stop_cpus() the stoppers won't get
  221. * queued up in reverse order leading to system deadlock.
  222. *
  223. * We can't miss stop_cpus_in_progress if queue_stop_cpus_work() has
  224. * queued a work on cpu1 but not on cpu2, we hold both locks.
  225. *
  226. * It can be falsely true but it is safe to spin until it is cleared,
  227. * queue_stop_cpus_work() does everything under preempt_disable().
  228. */
  229. err = -EDEADLK;
  230. if (unlikely(stop_cpus_in_progress))
  231. goto unlock;
  232. err = 0;
  233. __cpu_stop_queue_work(stopper1, work1, &wakeq);
  234. __cpu_stop_queue_work(stopper2, work2, &wakeq);
  235. unlock:
  236. raw_spin_unlock(&stopper2->lock);
  237. raw_spin_unlock_irq(&stopper1->lock);
  238. if (unlikely(err == -EDEADLK)) {
  239. while (stop_cpus_in_progress)
  240. cpu_relax();
  241. goto retry;
  242. }
  243. wake_up_q(&wakeq);
  244. return err;
  245. }
  246. /**
  247. * stop_two_cpus - stops two cpus
  248. * @cpu1: the cpu to stop
  249. * @cpu2: the other cpu to stop
  250. * @fn: function to execute
  251. * @arg: argument to @fn
  252. *
  253. * Stops both the current and specified CPU and runs @fn on one of them.
  254. *
  255. * returns when both are completed.
  256. */
  257. int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg)
  258. {
  259. struct cpu_stop_done done;
  260. struct cpu_stop_work work1, work2;
  261. struct multi_stop_data msdata;
  262. msdata = (struct multi_stop_data){
  263. .fn = fn,
  264. .data = arg,
  265. .num_threads = 2,
  266. .active_cpus = cpumask_of(cpu1),
  267. };
  268. work1 = work2 = (struct cpu_stop_work){
  269. .fn = multi_cpu_stop,
  270. .arg = &msdata,
  271. .done = &done
  272. };
  273. cpu_stop_init_done(&done, 2);
  274. set_state(&msdata, MULTI_STOP_PREPARE);
  275. if (cpu1 > cpu2)
  276. swap(cpu1, cpu2);
  277. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2))
  278. return -ENOENT;
  279. wait_for_completion(&done.completion);
  280. return done.ret;
  281. }
  282. /**
  283. * stop_one_cpu_nowait - stop a cpu but don't wait for completion
  284. * @cpu: cpu to stop
  285. * @fn: function to execute
  286. * @arg: argument to @fn
  287. * @work_buf: pointer to cpu_stop_work structure
  288. *
  289. * Similar to stop_one_cpu() but doesn't wait for completion. The
  290. * caller is responsible for ensuring @work_buf is currently unused
  291. * and will remain untouched until stopper starts executing @fn.
  292. *
  293. * CONTEXT:
  294. * Don't care.
  295. *
  296. * RETURNS:
  297. * true if cpu_stop_work was queued successfully and @fn will be called,
  298. * false otherwise.
  299. */
  300. bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  301. struct cpu_stop_work *work_buf)
  302. {
  303. *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, };
  304. return cpu_stop_queue_work(cpu, work_buf);
  305. }
  306. static bool queue_stop_cpus_work(const struct cpumask *cpumask,
  307. cpu_stop_fn_t fn, void *arg,
  308. struct cpu_stop_done *done)
  309. {
  310. struct cpu_stop_work *work;
  311. unsigned int cpu;
  312. bool queued = false;
  313. /*
  314. * Disable preemption while queueing to avoid getting
  315. * preempted by a stopper which might wait for other stoppers
  316. * to enter @fn which can lead to deadlock.
  317. */
  318. preempt_disable();
  319. stop_cpus_in_progress = true;
  320. for_each_cpu(cpu, cpumask) {
  321. work = &per_cpu(cpu_stopper.stop_work, cpu);
  322. work->fn = fn;
  323. work->arg = arg;
  324. work->done = done;
  325. if (cpu_stop_queue_work(cpu, work))
  326. queued = true;
  327. }
  328. stop_cpus_in_progress = false;
  329. preempt_enable();
  330. return queued;
  331. }
  332. static int __stop_cpus(const struct cpumask *cpumask,
  333. cpu_stop_fn_t fn, void *arg)
  334. {
  335. struct cpu_stop_done done;
  336. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  337. if (!queue_stop_cpus_work(cpumask, fn, arg, &done))
  338. return -ENOENT;
  339. wait_for_completion(&done.completion);
  340. return done.ret;
  341. }
  342. /**
  343. * stop_cpus - stop multiple cpus
  344. * @cpumask: cpus to stop
  345. * @fn: function to execute
  346. * @arg: argument to @fn
  347. *
  348. * Execute @fn(@arg) on online cpus in @cpumask. On each target cpu,
  349. * @fn is run in a process context with the highest priority
  350. * preempting any task on the cpu and monopolizing it. This function
  351. * returns after all executions are complete.
  352. *
  353. * This function doesn't guarantee the cpus in @cpumask stay online
  354. * till @fn completes. If some cpus go down in the middle, execution
  355. * on the cpu may happen partially or fully on different cpus. @fn
  356. * should either be ready for that or the caller should ensure that
  357. * the cpus stay online until this function completes.
  358. *
  359. * All stop_cpus() calls are serialized making it safe for @fn to wait
  360. * for all cpus to start executing it.
  361. *
  362. * CONTEXT:
  363. * Might sleep.
  364. *
  365. * RETURNS:
  366. * -ENOENT if @fn(@arg) was not executed at all because all cpus in
  367. * @cpumask were offline; otherwise, 0 if all executions of @fn
  368. * returned 0, any non zero return value if any returned non zero.
  369. */
  370. int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  371. {
  372. int ret;
  373. /* static works are used, process one request at a time */
  374. mutex_lock(&stop_cpus_mutex);
  375. ret = __stop_cpus(cpumask, fn, arg);
  376. mutex_unlock(&stop_cpus_mutex);
  377. return ret;
  378. }
  379. /**
  380. * try_stop_cpus - try to stop multiple cpus
  381. * @cpumask: cpus to stop
  382. * @fn: function to execute
  383. * @arg: argument to @fn
  384. *
  385. * Identical to stop_cpus() except that it fails with -EAGAIN if
  386. * someone else is already using the facility.
  387. *
  388. * CONTEXT:
  389. * Might sleep.
  390. *
  391. * RETURNS:
  392. * -EAGAIN if someone else is already stopping cpus, -ENOENT if
  393. * @fn(@arg) was not executed at all because all cpus in @cpumask were
  394. * offline; otherwise, 0 if all executions of @fn returned 0, any non
  395. * zero return value if any returned non zero.
  396. */
  397. int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  398. {
  399. int ret;
  400. /* static works are used, process one request at a time */
  401. if (!mutex_trylock(&stop_cpus_mutex))
  402. return -EAGAIN;
  403. ret = __stop_cpus(cpumask, fn, arg);
  404. mutex_unlock(&stop_cpus_mutex);
  405. return ret;
  406. }
  407. static int cpu_stop_should_run(unsigned int cpu)
  408. {
  409. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  410. unsigned long flags;
  411. int run;
  412. raw_spin_lock_irqsave(&stopper->lock, flags);
  413. run = !list_empty(&stopper->works);
  414. raw_spin_unlock_irqrestore(&stopper->lock, flags);
  415. return run;
  416. }
  417. static void cpu_stopper_thread(unsigned int cpu)
  418. {
  419. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  420. struct cpu_stop_work *work;
  421. repeat:
  422. work = NULL;
  423. raw_spin_lock_irq(&stopper->lock);
  424. if (!list_empty(&stopper->works)) {
  425. work = list_first_entry(&stopper->works,
  426. struct cpu_stop_work, list);
  427. list_del_init(&work->list);
  428. }
  429. raw_spin_unlock_irq(&stopper->lock);
  430. if (work) {
  431. cpu_stop_fn_t fn = work->fn;
  432. void *arg = work->arg;
  433. struct cpu_stop_done *done = work->done;
  434. int ret;
  435. /* cpu stop callbacks must not sleep, make in_atomic() == T */
  436. preempt_count_inc();
  437. ret = fn(arg);
  438. if (done) {
  439. if (ret)
  440. done->ret = ret;
  441. cpu_stop_signal_done(done);
  442. }
  443. preempt_count_dec();
  444. WARN_ONCE(preempt_count(),
  445. "cpu_stop: %pf(%p) leaked preempt count\n", fn, arg);
  446. goto repeat;
  447. }
  448. }
  449. void stop_machine_park(int cpu)
  450. {
  451. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  452. /*
  453. * Lockless. cpu_stopper_thread() will take stopper->lock and flush
  454. * the pending works before it parks, until then it is fine to queue
  455. * the new works.
  456. */
  457. stopper->enabled = false;
  458. kthread_park(stopper->thread);
  459. }
  460. extern void sched_set_stop_task(int cpu, struct task_struct *stop);
  461. static void cpu_stop_create(unsigned int cpu)
  462. {
  463. sched_set_stop_task(cpu, per_cpu(cpu_stopper.thread, cpu));
  464. }
  465. static void cpu_stop_park(unsigned int cpu)
  466. {
  467. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  468. WARN_ON(!list_empty(&stopper->works));
  469. }
  470. void stop_machine_unpark(int cpu)
  471. {
  472. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  473. stopper->enabled = true;
  474. kthread_unpark(stopper->thread);
  475. }
  476. static struct smp_hotplug_thread cpu_stop_threads = {
  477. .store = &cpu_stopper.thread,
  478. .thread_should_run = cpu_stop_should_run,
  479. .thread_fn = cpu_stopper_thread,
  480. .thread_comm = "migration/%u",
  481. .create = cpu_stop_create,
  482. .park = cpu_stop_park,
  483. .selfparking = true,
  484. };
  485. static int __init cpu_stop_init(void)
  486. {
  487. unsigned int cpu;
  488. for_each_possible_cpu(cpu) {
  489. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  490. raw_spin_lock_init(&stopper->lock);
  491. INIT_LIST_HEAD(&stopper->works);
  492. }
  493. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  494. stop_machine_unpark(raw_smp_processor_id());
  495. stop_machine_initialized = true;
  496. return 0;
  497. }
  498. early_initcall(cpu_stop_init);
  499. int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
  500. const struct cpumask *cpus)
  501. {
  502. struct multi_stop_data msdata = {
  503. .fn = fn,
  504. .data = data,
  505. .num_threads = num_online_cpus(),
  506. .active_cpus = cpus,
  507. };
  508. lockdep_assert_cpus_held();
  509. if (!stop_machine_initialized) {
  510. /*
  511. * Handle the case where stop_machine() is called
  512. * early in boot before stop_machine() has been
  513. * initialized.
  514. */
  515. unsigned long flags;
  516. int ret;
  517. WARN_ON_ONCE(msdata.num_threads != 1);
  518. local_irq_save(flags);
  519. hard_irq_disable();
  520. ret = (*fn)(data);
  521. local_irq_restore(flags);
  522. return ret;
  523. }
  524. /* Set the initial state and stop all online cpus. */
  525. set_state(&msdata, MULTI_STOP_PREPARE);
  526. return stop_cpus(cpu_online_mask, multi_cpu_stop, &msdata);
  527. }
  528. int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
  529. {
  530. int ret;
  531. /* No CPUs can come up or down during this. */
  532. cpus_read_lock();
  533. ret = stop_machine_cpuslocked(fn, data, cpus);
  534. cpus_read_unlock();
  535. return ret;
  536. }
  537. EXPORT_SYMBOL_GPL(stop_machine);
  538. /**
  539. * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
  540. * @fn: the function to run
  541. * @data: the data ptr for the @fn()
  542. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  543. *
  544. * This is identical to stop_machine() but can be called from a CPU which
  545. * is not active. The local CPU is in the process of hotplug (so no other
  546. * CPU hotplug can start) and not marked active and doesn't have enough
  547. * context to sleep.
  548. *
  549. * This function provides stop_machine() functionality for such state by
  550. * using busy-wait for synchronization and executing @fn directly for local
  551. * CPU.
  552. *
  553. * CONTEXT:
  554. * Local CPU is inactive. Temporarily stops all active CPUs.
  555. *
  556. * RETURNS:
  557. * 0 if all executions of @fn returned 0, any non zero return value if any
  558. * returned non zero.
  559. */
  560. int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  561. const struct cpumask *cpus)
  562. {
  563. struct multi_stop_data msdata = { .fn = fn, .data = data,
  564. .active_cpus = cpus };
  565. struct cpu_stop_done done;
  566. int ret;
  567. /* Local CPU must be inactive and CPU hotplug in progress. */
  568. BUG_ON(cpu_active(raw_smp_processor_id()));
  569. msdata.num_threads = num_active_cpus() + 1; /* +1 for local */
  570. /* No proper task established and can't sleep - busy wait for lock. */
  571. while (!mutex_trylock(&stop_cpus_mutex))
  572. cpu_relax();
  573. /* Schedule work on other CPUs and execute directly for local CPU */
  574. set_state(&msdata, MULTI_STOP_PREPARE);
  575. cpu_stop_init_done(&done, num_active_cpus());
  576. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  577. &done);
  578. ret = multi_cpu_stop(&msdata);
  579. /* Busy wait for completion. */
  580. while (!completion_done(&done.completion))
  581. cpu_relax();
  582. mutex_unlock(&stop_cpus_mutex);
  583. return ret ?: done.ret;
  584. }