stop_machine.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 <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 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. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  125. cpu_stop_fn_t fn, void *arg,
  126. struct cpu_stop_done *done)
  127. {
  128. struct cpu_stop_work *work;
  129. unsigned int cpu;
  130. /* initialize works and done */
  131. for_each_cpu(cpu, cpumask) {
  132. work = &per_cpu(stop_cpus_work, cpu);
  133. work->fn = fn;
  134. work->arg = arg;
  135. work->done = done;
  136. }
  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. }
  148. static int __stop_cpus(const struct cpumask *cpumask,
  149. cpu_stop_fn_t fn, void *arg)
  150. {
  151. struct cpu_stop_done done;
  152. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  153. queue_stop_cpus_work(cpumask, fn, arg, &done);
  154. wait_for_completion(&done.completion);
  155. return done.executed ? done.ret : -ENOENT;
  156. }
  157. /**
  158. * stop_cpus - stop multiple cpus
  159. * @cpumask: cpus to stop
  160. * @fn: function to execute
  161. * @arg: argument to @fn
  162. *
  163. * Execute @fn(@arg) on online cpus in @cpumask. On each target cpu,
  164. * @fn is run in a process context with the highest priority
  165. * preempting any task on the cpu and monopolizing it. This function
  166. * returns after all executions are complete.
  167. *
  168. * This function doesn't guarantee the cpus in @cpumask stay online
  169. * till @fn completes. If some cpus go down in the middle, execution
  170. * on the cpu may happen partially or fully on different cpus. @fn
  171. * should either be ready for that or the caller should ensure that
  172. * the cpus stay online until this function completes.
  173. *
  174. * All stop_cpus() calls are serialized making it safe for @fn to wait
  175. * for all cpus to start executing it.
  176. *
  177. * CONTEXT:
  178. * Might sleep.
  179. *
  180. * RETURNS:
  181. * -ENOENT if @fn(@arg) was not executed at all because all cpus in
  182. * @cpumask were offline; otherwise, 0 if all executions of @fn
  183. * returned 0, any non zero return value if any returned non zero.
  184. */
  185. int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  186. {
  187. int ret;
  188. /* static works are used, process one request at a time */
  189. mutex_lock(&stop_cpus_mutex);
  190. ret = __stop_cpus(cpumask, fn, arg);
  191. mutex_unlock(&stop_cpus_mutex);
  192. return ret;
  193. }
  194. /**
  195. * try_stop_cpus - try to stop multiple cpus
  196. * @cpumask: cpus to stop
  197. * @fn: function to execute
  198. * @arg: argument to @fn
  199. *
  200. * Identical to stop_cpus() except that it fails with -EAGAIN if
  201. * someone else is already using the facility.
  202. *
  203. * CONTEXT:
  204. * Might sleep.
  205. *
  206. * RETURNS:
  207. * -EAGAIN if someone else is already stopping cpus, -ENOENT if
  208. * @fn(@arg) was not executed at all because all cpus in @cpumask were
  209. * offline; otherwise, 0 if all executions of @fn returned 0, any non
  210. * zero return value if any returned non zero.
  211. */
  212. int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
  213. {
  214. int ret;
  215. /* static works are used, process one request at a time */
  216. if (!mutex_trylock(&stop_cpus_mutex))
  217. return -EAGAIN;
  218. ret = __stop_cpus(cpumask, fn, arg);
  219. mutex_unlock(&stop_cpus_mutex);
  220. return ret;
  221. }
  222. static int cpu_stopper_thread(void *data)
  223. {
  224. struct cpu_stopper *stopper = data;
  225. struct cpu_stop_work *work;
  226. int ret;
  227. repeat:
  228. set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
  229. if (kthread_should_stop()) {
  230. __set_current_state(TASK_RUNNING);
  231. return 0;
  232. }
  233. work = NULL;
  234. spin_lock_irq(&stopper->lock);
  235. if (!list_empty(&stopper->works)) {
  236. work = list_first_entry(&stopper->works,
  237. struct cpu_stop_work, list);
  238. list_del_init(&work->list);
  239. }
  240. spin_unlock_irq(&stopper->lock);
  241. if (work) {
  242. cpu_stop_fn_t fn = work->fn;
  243. void *arg = work->arg;
  244. struct cpu_stop_done *done = work->done;
  245. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  246. __set_current_state(TASK_RUNNING);
  247. /* cpu stop callbacks are not allowed to sleep */
  248. preempt_disable();
  249. ret = fn(arg);
  250. if (ret)
  251. done->ret = ret;
  252. /* restore preemption and check it's still balanced */
  253. preempt_enable();
  254. WARN_ONCE(preempt_count(),
  255. "cpu_stop: %s(%p) leaked preempt count\n",
  256. kallsyms_lookup((unsigned long)fn, NULL, NULL, NULL,
  257. ksym_buf), arg);
  258. cpu_stop_signal_done(done, true);
  259. } else
  260. schedule();
  261. goto repeat;
  262. }
  263. extern void sched_set_stop_task(int cpu, struct task_struct *stop);
  264. /* manage stopper for a cpu, mostly lifted from sched migration thread mgmt */
  265. static int __cpuinit cpu_stop_cpu_callback(struct notifier_block *nfb,
  266. unsigned long action, void *hcpu)
  267. {
  268. unsigned int cpu = (unsigned long)hcpu;
  269. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  270. struct task_struct *p;
  271. switch (action & ~CPU_TASKS_FROZEN) {
  272. case CPU_UP_PREPARE:
  273. BUG_ON(stopper->thread || stopper->enabled ||
  274. !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. stopper->thread = p;
  285. break;
  286. case CPU_ONLINE:
  287. /* strictly unnecessary, as first user will wake it */
  288. wake_up_process(stopper->thread);
  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(stopper->thread);
  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(stopper->thread);
  310. stopper->thread = 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. return 0;
  343. }
  344. early_initcall(cpu_stop_init);
  345. #ifdef CONFIG_STOP_MACHINE
  346. /* This controls the threads on each CPU. */
  347. enum stopmachine_state {
  348. /* Dummy starting state for thread. */
  349. STOPMACHINE_NONE,
  350. /* Awaiting everyone to be scheduled. */
  351. STOPMACHINE_PREPARE,
  352. /* Disable interrupts. */
  353. STOPMACHINE_DISABLE_IRQ,
  354. /* Run the function */
  355. STOPMACHINE_RUN,
  356. /* Exit */
  357. STOPMACHINE_EXIT,
  358. };
  359. struct stop_machine_data {
  360. int (*fn)(void *);
  361. void *data;
  362. /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
  363. unsigned int num_threads;
  364. const struct cpumask *active_cpus;
  365. enum stopmachine_state state;
  366. atomic_t thread_ack;
  367. };
  368. static void set_state(struct stop_machine_data *smdata,
  369. enum stopmachine_state newstate)
  370. {
  371. /* Reset ack counter. */
  372. atomic_set(&smdata->thread_ack, smdata->num_threads);
  373. smp_wmb();
  374. smdata->state = newstate;
  375. }
  376. /* Last one to ack a state moves to the next state. */
  377. static void ack_state(struct stop_machine_data *smdata)
  378. {
  379. if (atomic_dec_and_test(&smdata->thread_ack))
  380. set_state(smdata, smdata->state + 1);
  381. }
  382. /* This is the cpu_stop function which stops the CPU. */
  383. static int stop_machine_cpu_stop(void *data)
  384. {
  385. struct stop_machine_data *smdata = data;
  386. enum stopmachine_state curstate = STOPMACHINE_NONE;
  387. int cpu = smp_processor_id(), err = 0;
  388. unsigned long flags;
  389. bool is_active;
  390. /*
  391. * When called from stop_machine_from_inactive_cpu(), irq might
  392. * already be disabled. Save the state and restore it on exit.
  393. */
  394. local_save_flags(flags);
  395. if (!smdata->active_cpus)
  396. is_active = cpu == cpumask_first(cpu_online_mask);
  397. else
  398. is_active = cpumask_test_cpu(cpu, smdata->active_cpus);
  399. /* Simple state machine */
  400. do {
  401. /* Chill out and ensure we re-read stopmachine_state. */
  402. cpu_relax();
  403. if (smdata->state != curstate) {
  404. curstate = smdata->state;
  405. switch (curstate) {
  406. case STOPMACHINE_DISABLE_IRQ:
  407. local_irq_disable();
  408. hard_irq_disable();
  409. break;
  410. case STOPMACHINE_RUN:
  411. if (is_active)
  412. err = smdata->fn(smdata->data);
  413. break;
  414. default:
  415. break;
  416. }
  417. ack_state(smdata);
  418. }
  419. } while (curstate != STOPMACHINE_EXIT);
  420. local_irq_restore(flags);
  421. return err;
  422. }
  423. int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  424. {
  425. struct stop_machine_data smdata = { .fn = fn, .data = data,
  426. .num_threads = num_online_cpus(),
  427. .active_cpus = cpus };
  428. /* Set the initial state and stop all online cpus. */
  429. set_state(&smdata, STOPMACHINE_PREPARE);
  430. return stop_cpus(cpu_online_mask, stop_machine_cpu_stop, &smdata);
  431. }
  432. int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
  433. {
  434. int ret;
  435. /* No CPUs can come up or down during this. */
  436. get_online_cpus();
  437. ret = __stop_machine(fn, data, cpus);
  438. put_online_cpus();
  439. return ret;
  440. }
  441. EXPORT_SYMBOL_GPL(stop_machine);
  442. /**
  443. * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
  444. * @fn: the function to run
  445. * @data: the data ptr for the @fn()
  446. * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
  447. *
  448. * This is identical to stop_machine() but can be called from a CPU which
  449. * is not active. The local CPU is in the process of hotplug (so no other
  450. * CPU hotplug can start) and not marked active and doesn't have enough
  451. * context to sleep.
  452. *
  453. * This function provides stop_machine() functionality for such state by
  454. * using busy-wait for synchronization and executing @fn directly for local
  455. * CPU.
  456. *
  457. * CONTEXT:
  458. * Local CPU is inactive. Temporarily stops all active CPUs.
  459. *
  460. * RETURNS:
  461. * 0 if all executions of @fn returned 0, any non zero return value if any
  462. * returned non zero.
  463. */
  464. int stop_machine_from_inactive_cpu(int (*fn)(void *), void *data,
  465. const struct cpumask *cpus)
  466. {
  467. struct stop_machine_data smdata = { .fn = fn, .data = data,
  468. .active_cpus = cpus };
  469. struct cpu_stop_done done;
  470. int ret;
  471. /* Local CPU must be inactive and CPU hotplug in progress. */
  472. BUG_ON(cpu_active(raw_smp_processor_id()));
  473. smdata.num_threads = num_active_cpus() + 1; /* +1 for local */
  474. /* No proper task established and can't sleep - busy wait for lock. */
  475. while (!mutex_trylock(&stop_cpus_mutex))
  476. cpu_relax();
  477. /* Schedule work on other CPUs and execute directly for local CPU */
  478. set_state(&smdata, STOPMACHINE_PREPARE);
  479. cpu_stop_init_done(&done, num_active_cpus());
  480. queue_stop_cpus_work(cpu_active_mask, stop_machine_cpu_stop, &smdata,
  481. &done);
  482. ret = stop_machine_cpu_stop(&smdata);
  483. /* Busy wait for completion. */
  484. while (!completion_done(&done.completion))
  485. cpu_relax();
  486. mutex_unlock(&stop_cpus_mutex);
  487. return ret ?: done.ret;
  488. }
  489. #endif /* CONFIG_STOP_MACHINE */