stop_machine.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. };
  38. static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper);
  39. static DEFINE_PER_CPU(struct task_struct *, cpu_stopper_task);
  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(unsigned int cpu, struct cpu_stop_work *work)
  59. {
  60. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  61. struct task_struct *p = per_cpu(cpu_stopper_task, cpu);
  62. unsigned long flags;
  63. spin_lock_irqsave(&stopper->lock, flags);
  64. if (stopper->enabled) {
  65. list_add_tail(&work->list, &stopper->works);
  66. wake_up_process(p);
  67. } else
  68. cpu_stop_signal_done(work->done, false);
  69. spin_unlock_irqrestore(&stopper->lock, flags);
  70. }
  71. /**
  72. * stop_one_cpu - stop a cpu
  73. * @cpu: cpu to stop
  74. * @fn: function to execute
  75. * @arg: argument to @fn
  76. *
  77. * Execute @fn(@arg) on @cpu. @fn is run in a process context with
  78. * the highest priority preempting any task on the cpu and
  79. * monopolizing it. This function returns after the execution is
  80. * complete.
  81. *
  82. * This function doesn't guarantee @cpu stays online till @fn
  83. * completes. If @cpu goes down in the middle, execution may happen
  84. * partially or fully on different cpus. @fn should either be ready
  85. * for that or the caller should ensure that @cpu stays online until
  86. * this function completes.
  87. *
  88. * CONTEXT:
  89. * Might sleep.
  90. *
  91. * RETURNS:
  92. * -ENOENT if @fn(@arg) was not executed because @cpu was offline;
  93. * otherwise, the return value of @fn.
  94. */
  95. int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
  96. {
  97. struct cpu_stop_done done;
  98. struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
  99. cpu_stop_init_done(&done, 1);
  100. cpu_stop_queue_work(cpu, &work);
  101. wait_for_completion(&done.completion);
  102. return done.executed ? done.ret : -ENOENT;
  103. }
  104. /**
  105. * stop_one_cpu_nowait - stop a cpu but don't wait for completion
  106. * @cpu: cpu to stop
  107. * @fn: function to execute
  108. * @arg: argument to @fn
  109. *
  110. * Similar to stop_one_cpu() but doesn't wait for completion. The
  111. * caller is responsible for ensuring @work_buf is currently unused
  112. * and will remain untouched until stopper starts executing @fn.
  113. *
  114. * CONTEXT:
  115. * Don't care.
  116. */
  117. void stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
  118. struct cpu_stop_work *work_buf)
  119. {
  120. *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, };
  121. cpu_stop_queue_work(cpu, work_buf);
  122. }
  123. /* static data for stop_cpus */
  124. static DEFINE_MUTEX(stop_cpus_mutex);
  125. static DEFINE_PER_CPU(struct cpu_stop_work, stop_cpus_work);
  126. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  127. cpu_stop_fn_t fn, void *arg,
  128. struct cpu_stop_done *done)
  129. {
  130. struct cpu_stop_work *work;
  131. unsigned int cpu;
  132. /* initialize works and done */
  133. for_each_cpu(cpu, cpumask) {
  134. work = &per_cpu(stop_cpus_work, cpu);
  135. work->fn = fn;
  136. work->arg = arg;
  137. work->done = done;
  138. }
  139. /*
  140. * Disable preemption while queueing to avoid getting
  141. * preempted by a stopper which might wait for other stoppers
  142. * to enter @fn which can lead to deadlock.
  143. */
  144. preempt_disable();
  145. for_each_cpu(cpu, cpumask)
  146. cpu_stop_queue_work(cpu, &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 = per_cpu(cpu_stopper_task, cpu);
  272. switch (action & ~CPU_TASKS_FROZEN) {
  273. case CPU_UP_PREPARE:
  274. BUG_ON(p || stopper->enabled || !list_empty(&stopper->works));
  275. p = kthread_create_on_node(cpu_stopper_thread,
  276. stopper,
  277. cpu_to_node(cpu),
  278. "migration/%d", cpu);
  279. if (IS_ERR(p))
  280. return notifier_from_errno(PTR_ERR(p));
  281. get_task_struct(p);
  282. kthread_bind(p, cpu);
  283. sched_set_stop_task(cpu, p);
  284. per_cpu(cpu_stopper_task, cpu) = p;
  285. break;
  286. case CPU_ONLINE:
  287. /* strictly unnecessary, as first user will wake it */
  288. wake_up_process(p);
  289. /* mark enabled */
  290. spin_lock_irq(&stopper->lock);
  291. stopper->enabled = true;
  292. spin_unlock_irq(&stopper->lock);
  293. break;
  294. #ifdef CONFIG_HOTPLUG_CPU
  295. case CPU_UP_CANCELED:
  296. case CPU_POST_DEAD:
  297. {
  298. struct cpu_stop_work *work;
  299. sched_set_stop_task(cpu, NULL);
  300. /* kill the stopper */
  301. kthread_stop(p);
  302. /* drain remaining works */
  303. spin_lock_irq(&stopper->lock);
  304. list_for_each_entry(work, &stopper->works, list)
  305. cpu_stop_signal_done(work->done, false);
  306. stopper->enabled = false;
  307. spin_unlock_irq(&stopper->lock);
  308. /* release the stopper */
  309. put_task_struct(p);
  310. per_cpu(cpu_stopper_task, cpu) = NULL;
  311. break;
  312. }
  313. #endif
  314. }
  315. return NOTIFY_OK;
  316. }
  317. /*
  318. * Give it a higher priority so that cpu stopper is available to other
  319. * cpu notifiers. It currently shares the same priority as sched
  320. * migration_notifier.
  321. */
  322. static struct notifier_block __cpuinitdata cpu_stop_cpu_notifier = {
  323. .notifier_call = cpu_stop_cpu_callback,
  324. .priority = 10,
  325. };
  326. static int __init cpu_stop_init(void)
  327. {
  328. void *bcpu = (void *)(long)smp_processor_id();
  329. unsigned int cpu;
  330. int err;
  331. for_each_possible_cpu(cpu) {
  332. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  333. spin_lock_init(&stopper->lock);
  334. INIT_LIST_HEAD(&stopper->works);
  335. }
  336. /* start one for the boot cpu */
  337. err = cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_UP_PREPARE,
  338. bcpu);
  339. BUG_ON(err != NOTIFY_OK);
  340. cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_ONLINE, bcpu);
  341. register_cpu_notifier(&cpu_stop_cpu_notifier);
  342. stop_machine_initialized = true;
  343. return 0;
  344. }
  345. early_initcall(cpu_stop_init);
  346. #ifdef CONFIG_STOP_MACHINE
  347. /* This controls the threads on each CPU. */
  348. enum stopmachine_state {
  349. /* Dummy starting state for thread. */
  350. STOPMACHINE_NONE,
  351. /* Awaiting everyone to be scheduled. */
  352. STOPMACHINE_PREPARE,
  353. /* Disable interrupts. */
  354. STOPMACHINE_DISABLE_IRQ,
  355. /* Run the function */
  356. STOPMACHINE_RUN,
  357. /* Exit */
  358. STOPMACHINE_EXIT,
  359. };
  360. struct stop_machine_data {
  361. int (*fn)(void *);
  362. void *data;
  363. /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
  364. unsigned int num_threads;
  365. const struct cpumask *active_cpus;
  366. enum stopmachine_state state;
  367. atomic_t thread_ack;
  368. };
  369. static void set_state(struct stop_machine_data *smdata,
  370. enum stopmachine_state newstate)
  371. {
  372. /* Reset ack counter. */
  373. atomic_set(&smdata->thread_ack, smdata->num_threads);
  374. smp_wmb();
  375. smdata->state = newstate;
  376. }
  377. /* Last one to ack a state moves to the next state. */
  378. static void ack_state(struct stop_machine_data *smdata)
  379. {
  380. if (atomic_dec_and_test(&smdata->thread_ack))
  381. set_state(smdata, smdata->state + 1);
  382. }
  383. /* This is the cpu_stop function which stops the CPU. */
  384. static int stop_machine_cpu_stop(void *data)
  385. {
  386. struct stop_machine_data *smdata = data;
  387. enum stopmachine_state curstate = STOPMACHINE_NONE;
  388. int cpu = smp_processor_id(), err = 0;
  389. unsigned long flags;
  390. bool is_active;
  391. /*
  392. * When called from stop_machine_from_inactive_cpu(), irq might
  393. * already be disabled. Save the state and restore it on exit.
  394. */
  395. local_save_flags(flags);
  396. if (!smdata->active_cpus)
  397. is_active = cpu == cpumask_first(cpu_online_mask);
  398. else
  399. is_active = cpumask_test_cpu(cpu, smdata->active_cpus);
  400. /* Simple state machine */
  401. do {
  402. /* Chill out and ensure we re-read stopmachine_state. */
  403. cpu_relax();
  404. if (smdata->state != curstate) {
  405. curstate = smdata->state;
  406. switch (curstate) {
  407. case STOPMACHINE_DISABLE_IRQ:
  408. local_irq_disable();
  409. hard_irq_disable();
  410. break;
  411. case STOPMACHINE_RUN:
  412. if (is_active)
  413. err = smdata->fn(smdata->data);
  414. break;
  415. default:
  416. break;
  417. }
  418. ack_state(smdata);
  419. }
  420. } while (curstate != STOPMACHINE_EXIT);
  421. local_irq_restore(flags);
  422. return err;
  423. }
  424. int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  425. {
  426. struct stop_machine_data smdata = { .fn = fn, .data = data,
  427. .num_threads = num_online_cpus(),
  428. .active_cpus = cpus };
  429. if (!stop_machine_initialized) {
  430. /*
  431. * Handle the case where stop_machine() is called
  432. * early in boot before stop_machine() has been
  433. * initialized.
  434. */
  435. unsigned long flags;
  436. int ret;
  437. WARN_ON_ONCE(smdata.num_threads != 1);
  438. local_irq_save(flags);
  439. hard_irq_disable();
  440. ret = (*fn)(data);
  441. local_irq_restore(flags);
  442. return ret;
  443. }
  444. /* Set the initial state and stop all online cpus. */
  445. set_state(&smdata, STOPMACHINE_PREPARE);
  446. return stop_cpus(cpu_online_mask, stop_machine_cpu_stop, &smdata);
  447. }
  448. int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  449. {
  450. int ret;
  451. /* No CPUs can come up or down during this. */
  452. get_online_cpus();
  453. ret = __stop_machine(fn, data, cpus);
  454. put_online_cpus();
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(stop_machine);
  458. /**
  459. * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
  460. * @fn: the function to run
  461. * @data: the data ptr for the @fn()
  462. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  463. *
  464. * This is identical to stop_machine() but can be called from a CPU which
  465. * is not active. The local CPU is in the process of hotplug (so no other
  466. * CPU hotplug can start) and not marked active and doesn't have enough
  467. * context to sleep.
  468. *
  469. * This function provides stop_machine() functionality for such state by
  470. * using busy-wait for synchronization and executing @fn directly for local
  471. * CPU.
  472. *
  473. * CONTEXT:
  474. * Local CPU is inactive. Temporarily stops all active CPUs.
  475. *
  476. * RETURNS:
  477. * 0 if all executions of @fn returned 0, any non zero return value if any
  478. * returned non zero.
  479. */
  480. int stop_machine_from_inactive_cpu(int (*fn)(void *), void *data,
  481. const struct cpumask *cpus)
  482. {
  483. struct stop_machine_data smdata = { .fn = fn, .data = data,
  484. .active_cpus = cpus };
  485. struct cpu_stop_done done;
  486. int ret;
  487. /* Local CPU must be inactive and CPU hotplug in progress. */
  488. BUG_ON(cpu_active(raw_smp_processor_id()));
  489. smdata.num_threads = num_active_cpus() + 1; /* +1 for local */
  490. /* No proper task established and can't sleep - busy wait for lock. */
  491. while (!mutex_trylock(&stop_cpus_mutex))
  492. cpu_relax();
  493. /* Schedule work on other CPUs and execute directly for local CPU */
  494. set_state(&smdata, STOPMACHINE_PREPARE);
  495. cpu_stop_init_done(&done, num_active_cpus());
  496. queue_stop_cpus_work(cpu_active_mask, stop_machine_cpu_stop, &smdata,
  497. &done);
  498. ret = stop_machine_cpu_stop(&smdata);
  499. /* Busy wait for completion. */
  500. while (!completion_done(&done.completion))
  501. cpu_relax();
  502. mutex_unlock(&stop_cpus_mutex);
  503. return ret ?: done.ret;
  504. }
  505. #endif /* CONFIG_STOP_MACHINE */