stop_machine.c 18 KB

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