stop_machine.c 18 KB

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