stop_machine.c 18 KB

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