stop_machine.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. /* static data for stop_cpus */
  122. static DEFINE_MUTEX(stop_cpus_mutex);
  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];
  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. /* manage stopper for a cpu, mostly lifted from sched migration thread mgmt */
  257. static int __cpuinit cpu_stop_cpu_callback(struct notifier_block *nfb,
  258. unsigned long action, void *hcpu)
  259. {
  260. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  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(cpu_stopper_thread, stopper, "migration/%d",
  269. cpu);
  270. if (IS_ERR(p))
  271. return NOTIFY_BAD;
  272. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  273. get_task_struct(p);
  274. stopper->thread = p;
  275. break;
  276. case CPU_ONLINE:
  277. kthread_bind(stopper->thread, cpu);
  278. /* strictly unnecessary, as first user will wake it */
  279. wake_up_process(stopper->thread);
  280. /* mark enabled */
  281. spin_lock_irq(&stopper->lock);
  282. stopper->enabled = true;
  283. spin_unlock_irq(&stopper->lock);
  284. break;
  285. #ifdef CONFIG_HOTPLUG_CPU
  286. case CPU_UP_CANCELED:
  287. case CPU_POST_DEAD:
  288. {
  289. struct cpu_stop_work *work;
  290. /* kill the stopper */
  291. kthread_stop(stopper->thread);
  292. /* drain remaining works */
  293. spin_lock_irq(&stopper->lock);
  294. list_for_each_entry(work, &stopper->works, list)
  295. cpu_stop_signal_done(work->done, false);
  296. stopper->enabled = false;
  297. spin_unlock_irq(&stopper->lock);
  298. /* release the stopper */
  299. put_task_struct(stopper->thread);
  300. stopper->thread = NULL;
  301. break;
  302. }
  303. #endif
  304. }
  305. return NOTIFY_OK;
  306. }
  307. /*
  308. * Give it a higher priority so that cpu stopper is available to other
  309. * cpu notifiers. It currently shares the same priority as sched
  310. * migration_notifier.
  311. */
  312. static struct notifier_block __cpuinitdata cpu_stop_cpu_notifier = {
  313. .notifier_call = cpu_stop_cpu_callback,
  314. .priority = 10,
  315. };
  316. static int __init cpu_stop_init(void)
  317. {
  318. void *bcpu = (void *)(long)smp_processor_id();
  319. unsigned int cpu;
  320. int err;
  321. for_each_possible_cpu(cpu) {
  322. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  323. spin_lock_init(&stopper->lock);
  324. INIT_LIST_HEAD(&stopper->works);
  325. }
  326. /* start one for the boot cpu */
  327. err = cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_UP_PREPARE,
  328. bcpu);
  329. BUG_ON(err == NOTIFY_BAD);
  330. cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_ONLINE, bcpu);
  331. register_cpu_notifier(&cpu_stop_cpu_notifier);
  332. return 0;
  333. }
  334. early_initcall(cpu_stop_init);
  335. #ifdef CONFIG_STOP_MACHINE
  336. /* This controls the threads on each CPU. */
  337. enum stopmachine_state {
  338. /* Dummy starting state for thread. */
  339. STOPMACHINE_NONE,
  340. /* Awaiting everyone to be scheduled. */
  341. STOPMACHINE_PREPARE,
  342. /* Disable interrupts. */
  343. STOPMACHINE_DISABLE_IRQ,
  344. /* Run the function */
  345. STOPMACHINE_RUN,
  346. /* Exit */
  347. STOPMACHINE_EXIT,
  348. };
  349. struct stop_machine_data {
  350. int (*fn)(void *);
  351. void *data;
  352. /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
  353. unsigned int num_threads;
  354. const struct cpumask *active_cpus;
  355. enum stopmachine_state state;
  356. atomic_t thread_ack;
  357. };
  358. static void set_state(struct stop_machine_data *smdata,
  359. enum stopmachine_state newstate)
  360. {
  361. /* Reset ack counter. */
  362. atomic_set(&smdata->thread_ack, smdata->num_threads);
  363. smp_wmb();
  364. smdata->state = newstate;
  365. }
  366. /* Last one to ack a state moves to the next state. */
  367. static void ack_state(struct stop_machine_data *smdata)
  368. {
  369. if (atomic_dec_and_test(&smdata->thread_ack))
  370. set_state(smdata, smdata->state + 1);
  371. }
  372. /* This is the cpu_stop function which stops the CPU. */
  373. static int stop_machine_cpu_stop(void *data)
  374. {
  375. struct stop_machine_data *smdata = data;
  376. enum stopmachine_state curstate = STOPMACHINE_NONE;
  377. int cpu = smp_processor_id(), err = 0;
  378. bool is_active;
  379. if (!smdata->active_cpus)
  380. is_active = cpu == cpumask_first(cpu_online_mask);
  381. else
  382. is_active = cpumask_test_cpu(cpu, smdata->active_cpus);
  383. /* Simple state machine */
  384. do {
  385. /* Chill out and ensure we re-read stopmachine_state. */
  386. cpu_relax();
  387. if (smdata->state != curstate) {
  388. curstate = smdata->state;
  389. switch (curstate) {
  390. case STOPMACHINE_DISABLE_IRQ:
  391. local_irq_disable();
  392. hard_irq_disable();
  393. break;
  394. case STOPMACHINE_RUN:
  395. if (is_active)
  396. err = smdata->fn(smdata->data);
  397. break;
  398. default:
  399. break;
  400. }
  401. ack_state(smdata);
  402. }
  403. } while (curstate != STOPMACHINE_EXIT);
  404. local_irq_enable();
  405. return err;
  406. }
  407. int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  408. {
  409. struct stop_machine_data smdata = { .fn = fn, .data = data,
  410. .num_threads = num_online_cpus(),
  411. .active_cpus = cpus };
  412. /* Set the initial state and stop all online cpus. */
  413. set_state(&smdata, STOPMACHINE_PREPARE);
  414. return stop_cpus(cpu_online_mask, stop_machine_cpu_stop, &smdata);
  415. }
  416. int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  417. {
  418. int ret;
  419. /* No CPUs can come up or down during this. */
  420. get_online_cpus();
  421. ret = __stop_machine(fn, data, cpus);
  422. put_online_cpus();
  423. return ret;
  424. }
  425. EXPORT_SYMBOL_GPL(stop_machine);
  426. #endif /* CONFIG_STOP_MACHINE */