stop_machine.c 13 KB

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