blob: 10c5a3a8d638388a3cce63f3a987a2ee80133560 [file] [log] [blame]
Thomas Gleixner38498a62012-04-20 13:05:44 +00001/*
2 * Common SMP CPU bringup/teardown functions
3 */
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +00004#include <linux/cpu.h>
Thomas Gleixner29d5e042012-04-20 13:05:45 +00005#include <linux/err.h>
6#include <linux/smp.h>
Paul E. McKenney8038dad2015-02-25 10:34:39 -08007#include <linux/delay.h>
Thomas Gleixner38498a62012-04-20 13:05:44 +00008#include <linux/init.h>
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +00009#include <linux/list.h>
10#include <linux/slab.h>
Thomas Gleixner29d5e042012-04-20 13:05:45 +000011#include <linux/sched.h>
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +000012#include <linux/export.h>
Thomas Gleixner29d5e042012-04-20 13:05:45 +000013#include <linux/percpu.h>
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +000014#include <linux/kthread.h>
15#include <linux/smpboot.h>
Thomas Gleixner38498a62012-04-20 13:05:44 +000016
17#include "smpboot.h"
18
Paul E. McKenney3180d892012-07-12 01:55:54 -070019#ifdef CONFIG_SMP
20
Thomas Gleixner29d5e042012-04-20 13:05:45 +000021#ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
Thomas Gleixner29d5e042012-04-20 13:05:45 +000022/*
23 * For the hotplug case we keep the task structs around and reuse
24 * them.
25 */
26static DEFINE_PER_CPU(struct task_struct *, idle_threads);
27
Paul Gortmaker0db06282013-06-19 14:53:51 -040028struct task_struct *idle_thread_get(unsigned int cpu)
Thomas Gleixner29d5e042012-04-20 13:05:45 +000029{
30 struct task_struct *tsk = per_cpu(idle_threads, cpu);
31
32 if (!tsk)
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070033 return ERR_PTR(-ENOMEM);
Syed Rameez Mustafa60cef212016-05-09 16:28:07 -070034 init_idle(tsk, cpu, true);
Thomas Gleixner29d5e042012-04-20 13:05:45 +000035 return tsk;
36}
37
Thomas Gleixner29d5e042012-04-20 13:05:45 +000038void __init idle_thread_set_boot_cpu(void)
39{
40 per_cpu(idle_threads, smp_processor_id()) = current;
41}
42
Srivatsa S. Bhat4a70d2d2012-05-24 20:41:00 +053043/**
44 * idle_init - Initialize the idle thread for a cpu
45 * @cpu: The cpu for which the idle thread should be initialized
46 *
47 * Creates the thread if it does not exist.
48 */
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070049static inline void idle_init(unsigned int cpu)
50{
51 struct task_struct *tsk = per_cpu(idle_threads, cpu);
52
53 if (!tsk) {
54 tsk = fork_idle(cpu);
55 if (IS_ERR(tsk))
56 pr_err("SMP: fork_idle() failed for CPU %u\n", cpu);
57 else
58 per_cpu(idle_threads, cpu) = tsk;
59 }
60}
61
Thomas Gleixner29d5e042012-04-20 13:05:45 +000062/**
Srivatsa S. Bhat4a70d2d2012-05-24 20:41:00 +053063 * idle_threads_init - Initialize idle threads for all cpus
Thomas Gleixner29d5e042012-04-20 13:05:45 +000064 */
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070065void __init idle_threads_init(void)
Thomas Gleixner29d5e042012-04-20 13:05:45 +000066{
Srivatsa S. Bhatee74d132012-05-24 20:40:55 +053067 unsigned int cpu, boot_cpu;
68
69 boot_cpu = smp_processor_id();
Thomas Gleixner29d5e042012-04-20 13:05:45 +000070
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070071 for_each_possible_cpu(cpu) {
Srivatsa S. Bhatee74d132012-05-24 20:40:55 +053072 if (cpu != boot_cpu)
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070073 idle_init(cpu);
Thomas Gleixner29d5e042012-04-20 13:05:45 +000074 }
Thomas Gleixner29d5e042012-04-20 13:05:45 +000075}
Thomas Gleixner29d5e042012-04-20 13:05:45 +000076#endif
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +000077
Paul E. McKenney3180d892012-07-12 01:55:54 -070078#endif /* #ifdef CONFIG_SMP */
79
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +000080static LIST_HEAD(hotplug_threads);
81static DEFINE_MUTEX(smpboot_threads_lock);
82
83struct smpboot_thread_data {
84 unsigned int cpu;
85 unsigned int status;
86 struct smp_hotplug_thread *ht;
87};
88
89enum {
90 HP_THREAD_NONE = 0,
91 HP_THREAD_ACTIVE,
92 HP_THREAD_PARKED,
93};
94
95/**
96 * smpboot_thread_fn - percpu hotplug thread loop function
97 * @data: thread data pointer
98 *
99 * Checks for thread stop and park conditions. Calls the necessary
100 * setup, cleanup, park and unpark functions for the registered
101 * thread.
102 *
103 * Returns 1 when the thread should exit, 0 otherwise.
104 */
105static int smpboot_thread_fn(void *data)
106{
107 struct smpboot_thread_data *td = data;
108 struct smp_hotplug_thread *ht = td->ht;
109
110 while (1) {
111 set_current_state(TASK_INTERRUPTIBLE);
112 preempt_disable();
113 if (kthread_should_stop()) {
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200114 __set_current_state(TASK_RUNNING);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000115 preempt_enable();
Frederic Weisbecker3dd08c02015-09-04 15:45:03 -0700116 /* cleanup must mirror setup */
117 if (ht->cleanup && td->status != HP_THREAD_NONE)
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000118 ht->cleanup(td->cpu, cpu_online(td->cpu));
119 kfree(td);
120 return 0;
121 }
122
123 if (kthread_should_park()) {
Gaurav Kohlie3ec2822018-04-24 12:13:34 +0530124 /*
125 * Serialize against wakeup. If we take the lock first,
126 * wakeup is skipped. If we run later, we observe,
127 * TASK_RUNNING update from wakeup path, before moving
128 * forward. This helps avoid the race, where wakeup
129 * observes TASK_INTERRUPTIBLE, and also observes
130 * the TASK_PARKED in kthread_parkme() before updating
131 * task state to TASK_RUNNING. In this case, kthread
132 * gets parked in TASK_RUNNING state. This results
133 * in panic later on in kthread_unpark(), as it sees
134 * KTHREAD_IS_PARKED flag set but fails to rebind the
135 * kthread, due to it being not in TASK_PARKED state.
136 *
137 * Control thread Hotplug Thread
138 *
139 * kthread_park()
140 * set KTHREAD_SHOULD_PARK
141 * smpboot_thread_fn()
142 * set_current_state(
143 * TASK_INTERRUPTIBLE);
144 * kthread_parkme()
145 *
146 * wake_up_process()
147 *
148 * raw_spin_lock_irqsave(&p->pi_lock, flags);
149 * if (!(p->state & state))
150 * goto out;
151 *
152 * __set_current_state(
153 * TASK_PARKED);
154 *
155 * if (p->on_rq && ttwu_remote(p, wake_flags))
156 * ttwu_remote()
157 * p->state = TASK_RUNNING;
158 * schedule();
159 */
160 raw_spin_lock(&current->pi_lock);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000161 __set_current_state(TASK_RUNNING);
Gaurav Kohlie3ec2822018-04-24 12:13:34 +0530162 raw_spin_unlock(&current->pi_lock);
Ingo Molnarbe6a2e42016-10-04 09:55:57 +0200163 preempt_enable();
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000164 if (ht->park && td->status == HP_THREAD_ACTIVE) {
165 BUG_ON(td->cpu != smp_processor_id());
166 ht->park(td->cpu);
167 td->status = HP_THREAD_PARKED;
168 }
169 kthread_parkme();
170 /* We might have been woken for stop */
171 continue;
172 }
173
Arnd Bergmanndc893e12013-03-08 12:43:31 -0800174 BUG_ON(td->cpu != smp_processor_id());
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000175
176 /* Check for state change setup */
177 switch (td->status) {
178 case HP_THREAD_NONE:
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200179 __set_current_state(TASK_RUNNING);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000180 preempt_enable();
181 if (ht->setup)
182 ht->setup(td->cpu);
183 td->status = HP_THREAD_ACTIVE;
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200184 continue;
185
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000186 case HP_THREAD_PARKED:
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200187 __set_current_state(TASK_RUNNING);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000188 preempt_enable();
189 if (ht->unpark)
190 ht->unpark(td->cpu);
191 td->status = HP_THREAD_ACTIVE;
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200192 continue;
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000193 }
194
195 if (!ht->thread_should_run(td->cpu)) {
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200196 preempt_enable_no_resched();
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000197 schedule();
198 } else {
Peter Zijlstra7d4d2692014-09-24 10:18:52 +0200199 __set_current_state(TASK_RUNNING);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000200 preempt_enable();
201 ht->thread_fn(td->cpu);
202 }
203 }
204}
205
206static int
207__smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
208{
209 struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
210 struct smpboot_thread_data *td;
211
212 if (tsk)
213 return 0;
214
215 td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu));
216 if (!td)
217 return -ENOMEM;
218 td->cpu = cpu;
219 td->ht = ht;
220
221 tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,
222 ht->thread_comm);
223 if (IS_ERR(tsk)) {
224 kfree(td);
225 return PTR_ERR(tsk);
226 }
Petr Mladeka65d4092016-10-11 13:55:23 -0700227 /*
228 * Park the thread so that it could start right on the CPU
229 * when it is available.
230 */
231 kthread_park(tsk);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000232 get_task_struct(tsk);
233 *per_cpu_ptr(ht->store, cpu) = tsk;
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200234 if (ht->create) {
235 /*
236 * Make sure that the task has actually scheduled out
237 * into park position, before calling the create
238 * callback. At least the migration thread callback
239 * requires that the task is off the runqueue.
240 */
241 if (!wait_task_inactive(tsk, TASK_PARKED))
242 WARN_ON(1);
243 else
244 ht->create(cpu);
245 }
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000246 return 0;
247}
248
249int smpboot_create_threads(unsigned int cpu)
250{
251 struct smp_hotplug_thread *cur;
252 int ret = 0;
253
254 mutex_lock(&smpboot_threads_lock);
255 list_for_each_entry(cur, &hotplug_threads, list) {
256 ret = __smpboot_create_thread(cur, cpu);
257 if (ret)
258 break;
259 }
260 mutex_unlock(&smpboot_threads_lock);
261 return ret;
262}
263
264static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
265{
266 struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
267
Oleg Nesterovc00166d2015-10-09 18:00:49 +0200268 if (!ht->selfparking)
269 kthread_unpark(tsk);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000270}
271
Thomas Gleixner931ef162016-02-26 18:43:36 +0000272int smpboot_unpark_threads(unsigned int cpu)
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000273{
274 struct smp_hotplug_thread *cur;
275
276 mutex_lock(&smpboot_threads_lock);
277 list_for_each_entry(cur, &hotplug_threads, list)
Chris Metcalfb5242e92015-06-24 16:55:42 -0700278 if (cpumask_test_cpu(cpu, cur->cpumask))
279 smpboot_unpark_thread(cur, cpu);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000280 mutex_unlock(&smpboot_threads_lock);
Thomas Gleixner931ef162016-02-26 18:43:36 +0000281 return 0;
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000282}
283
284static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
285{
286 struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
287
Thomas Gleixner7d7e4992013-01-31 12:11:12 +0000288 if (tsk && !ht->selfparking)
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000289 kthread_park(tsk);
290}
291
Thomas Gleixner931ef162016-02-26 18:43:36 +0000292int smpboot_park_threads(unsigned int cpu)
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000293{
294 struct smp_hotplug_thread *cur;
295
296 mutex_lock(&smpboot_threads_lock);
297 list_for_each_entry_reverse(cur, &hotplug_threads, list)
298 smpboot_park_thread(cur, cpu);
299 mutex_unlock(&smpboot_threads_lock);
Thomas Gleixner931ef162016-02-26 18:43:36 +0000300 return 0;
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000301}
302
303static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
304{
305 unsigned int cpu;
306
307 /* We need to destroy also the parked threads of offline cpus */
308 for_each_possible_cpu(cpu) {
309 struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
310
311 if (tsk) {
312 kthread_stop(tsk);
313 put_task_struct(tsk);
314 *per_cpu_ptr(ht->store, cpu) = NULL;
315 }
316 }
317}
318
319/**
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700320 * smpboot_register_percpu_thread_cpumask - Register a per_cpu thread related
321 * to hotplug
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000322 * @plug_thread: Hotplug thread descriptor
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700323 * @cpumask: The cpumask where threads run
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000324 *
325 * Creates and starts the threads on all online cpus.
326 */
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700327int smpboot_register_percpu_thread_cpumask(struct smp_hotplug_thread *plug_thread,
328 const struct cpumask *cpumask)
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000329{
330 unsigned int cpu;
331 int ret = 0;
332
Chris Metcalfb5242e92015-06-24 16:55:42 -0700333 if (!alloc_cpumask_var(&plug_thread->cpumask, GFP_KERNEL))
334 return -ENOMEM;
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700335 cpumask_copy(plug_thread->cpumask, cpumask);
Chris Metcalfb5242e92015-06-24 16:55:42 -0700336
Lai Jiangshan4bee9682014-07-31 11:30:17 +0800337 get_online_cpus();
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000338 mutex_lock(&smpboot_threads_lock);
339 for_each_online_cpu(cpu) {
340 ret = __smpboot_create_thread(plug_thread, cpu);
341 if (ret) {
342 smpboot_destroy_threads(plug_thread);
Frederic Weisbecker5869b502015-09-04 15:45:00 -0700343 free_cpumask_var(plug_thread->cpumask);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000344 goto out;
345 }
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700346 if (cpumask_test_cpu(cpu, cpumask))
347 smpboot_unpark_thread(plug_thread, cpu);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000348 }
349 list_add(&plug_thread->list, &hotplug_threads);
350out:
351 mutex_unlock(&smpboot_threads_lock);
Lai Jiangshan4bee9682014-07-31 11:30:17 +0800352 put_online_cpus();
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000353 return ret;
354}
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700355EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread_cpumask);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000356
357/**
358 * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
359 * @plug_thread: Hotplug thread descriptor
360 *
361 * Stops all threads on all possible cpus.
362 */
363void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
364{
365 get_online_cpus();
366 mutex_lock(&smpboot_threads_lock);
367 list_del(&plug_thread->list);
368 smpboot_destroy_threads(plug_thread);
369 mutex_unlock(&smpboot_threads_lock);
370 put_online_cpus();
Chris Metcalfb5242e92015-06-24 16:55:42 -0700371 free_cpumask_var(plug_thread->cpumask);
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000372}
373EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
Paul E. McKenney8038dad2015-02-25 10:34:39 -0800374
Chris Metcalfb5242e92015-06-24 16:55:42 -0700375/**
376 * smpboot_update_cpumask_percpu_thread - Adjust which per_cpu hotplug threads stay parked
377 * @plug_thread: Hotplug thread descriptor
378 * @new: Revised mask to use
379 *
380 * The cpumask field in the smp_hotplug_thread must not be updated directly
381 * by the client, but only by calling this function.
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700382 * This function can only be called on a registered smp_hotplug_thread.
Chris Metcalfb5242e92015-06-24 16:55:42 -0700383 */
384int smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
385 const struct cpumask *new)
386{
387 struct cpumask *old = plug_thread->cpumask;
388 cpumask_var_t tmp;
389 unsigned int cpu;
390
391 if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
392 return -ENOMEM;
393
394 get_online_cpus();
395 mutex_lock(&smpboot_threads_lock);
396
397 /* Park threads that were exclusively enabled on the old mask. */
398 cpumask_andnot(tmp, old, new);
399 for_each_cpu_and(cpu, tmp, cpu_online_mask)
400 smpboot_park_thread(plug_thread, cpu);
401
402 /* Unpark threads that are exclusively enabled on the new mask. */
403 cpumask_andnot(tmp, new, old);
404 for_each_cpu_and(cpu, tmp, cpu_online_mask)
405 smpboot_unpark_thread(plug_thread, cpu);
406
407 cpumask_copy(old, new);
408
409 mutex_unlock(&smpboot_threads_lock);
410 put_online_cpus();
411
412 free_cpumask_var(tmp);
413
414 return 0;
415}
416EXPORT_SYMBOL_GPL(smpboot_update_cpumask_percpu_thread);
417
Paul E. McKenney8038dad2015-02-25 10:34:39 -0800418static DEFINE_PER_CPU(atomic_t, cpu_hotplug_state) = ATOMIC_INIT(CPU_POST_DEAD);
419
420/*
421 * Called to poll specified CPU's state, for example, when waiting for
422 * a CPU to come online.
423 */
424int cpu_report_state(int cpu)
425{
426 return atomic_read(&per_cpu(cpu_hotplug_state, cpu));
427}
428
429/*
430 * If CPU has died properly, set its state to CPU_UP_PREPARE and
431 * return success. Otherwise, return -EBUSY if the CPU died after
432 * cpu_wait_death() timed out. And yet otherwise again, return -EAGAIN
433 * if cpu_wait_death() timed out and the CPU still hasn't gotten around
434 * to dying. In the latter two cases, the CPU might not be set up
435 * properly, but it is up to the arch-specific code to decide.
436 * Finally, -EIO indicates an unanticipated problem.
437 *
438 * Note that it is permissible to omit this call entirely, as is
439 * done in architectures that do no CPU-hotplug error checking.
440 */
441int cpu_check_up_prepare(int cpu)
442{
443 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
444 atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
445 return 0;
446 }
447
448 switch (atomic_read(&per_cpu(cpu_hotplug_state, cpu))) {
449
450 case CPU_POST_DEAD:
451
452 /* The CPU died properly, so just start it up again. */
453 atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_UP_PREPARE);
454 return 0;
455
456 case CPU_DEAD_FROZEN:
457
458 /*
459 * Timeout during CPU death, so let caller know.
460 * The outgoing CPU completed its processing, but after
461 * cpu_wait_death() timed out and reported the error. The
462 * caller is free to proceed, in which case the state
463 * will be reset properly by cpu_set_state_online().
464 * Proceeding despite this -EBUSY return makes sense
465 * for systems where the outgoing CPUs take themselves
466 * offline, with no post-death manipulation required from
467 * a surviving CPU.
468 */
469 return -EBUSY;
470
471 case CPU_BROKEN:
472
473 /*
474 * The most likely reason we got here is that there was
475 * a timeout during CPU death, and the outgoing CPU never
476 * did complete its processing. This could happen on
477 * a virtualized system if the outgoing VCPU gets preempted
478 * for more than five seconds, and the user attempts to
479 * immediately online that same CPU. Trying again later
480 * might return -EBUSY above, hence -EAGAIN.
481 */
482 return -EAGAIN;
483
484 default:
485
486 /* Should not happen. Famous last words. */
487 return -EIO;
488 }
489}
490
491/*
492 * Mark the specified CPU online.
493 *
494 * Note that it is permissible to omit this call entirely, as is
495 * done in architectures that do no CPU-hotplug error checking.
496 */
497void cpu_set_state_online(int cpu)
498{
499 (void)atomic_xchg(&per_cpu(cpu_hotplug_state, cpu), CPU_ONLINE);
500}
501
502#ifdef CONFIG_HOTPLUG_CPU
503
504/*
505 * Wait for the specified CPU to exit the idle loop and die.
506 */
507bool cpu_wait_death(unsigned int cpu, int seconds)
508{
509 int jf_left = seconds * HZ;
510 int oldstate;
511 bool ret = true;
512 int sleep_jf = 1;
513
514 might_sleep();
515
516 /* The outgoing CPU will normally get done quite quickly. */
517 if (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) == CPU_DEAD)
518 goto update_state;
519 udelay(5);
520
521 /* But if the outgoing CPU dawdles, wait increasingly long times. */
522 while (atomic_read(&per_cpu(cpu_hotplug_state, cpu)) != CPU_DEAD) {
523 schedule_timeout_uninterruptible(sleep_jf);
524 jf_left -= sleep_jf;
525 if (jf_left <= 0)
526 break;
527 sleep_jf = DIV_ROUND_UP(sleep_jf * 11, 10);
528 }
529update_state:
530 oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
531 if (oldstate == CPU_DEAD) {
532 /* Outgoing CPU died normally, update state. */
533 smp_mb(); /* atomic_read() before update. */
534 atomic_set(&per_cpu(cpu_hotplug_state, cpu), CPU_POST_DEAD);
535 } else {
536 /* Outgoing CPU still hasn't died, set state accordingly. */
537 if (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
538 oldstate, CPU_BROKEN) != oldstate)
539 goto update_state;
540 ret = false;
541 }
542 return ret;
543}
544
545/*
546 * Called by the outgoing CPU to report its successful death. Return
547 * false if this report follows the surviving CPU's timing out.
548 *
549 * A separate "CPU_DEAD_FROZEN" is used when the surviving CPU
550 * timed out. This approach allows architectures to omit calls to
551 * cpu_check_up_prepare() and cpu_set_state_online() without defeating
552 * the next cpu_wait_death()'s polling loop.
553 */
554bool cpu_report_death(void)
555{
556 int oldstate;
557 int newstate;
558 int cpu = smp_processor_id();
559
560 do {
561 oldstate = atomic_read(&per_cpu(cpu_hotplug_state, cpu));
562 if (oldstate != CPU_BROKEN)
563 newstate = CPU_DEAD;
564 else
565 newstate = CPU_DEAD_FROZEN;
566 } while (atomic_cmpxchg(&per_cpu(cpu_hotplug_state, cpu),
567 oldstate, newstate) != oldstate);
568 return newstate == CPU_DEAD;
569}
570
571#endif /* #ifdef CONFIG_HOTPLUG_CPU */