stop_machine.c 17 KB

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