stop_machine.c 18 KB

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