stop_machine.c 15 KB

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