stop_machine.c 16 KB

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