2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
4 * This code is licenced under the GPL.
6 #include <linux/proc_fs.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/oom.h>
14 #include <linux/rcupdate.h>
15 #include <linux/export.h>
16 #include <linux/bug.h>
17 #include <linux/kthread.h>
18 #include <linux/stop_machine.h>
19 #include <linux/mutex.h>
20 #include <linux/gfp.h>
21 #include <linux/suspend.h>
22 #include <linux/lockdep.h>
27 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
28 static DEFINE_MUTEX(cpu_add_remove_lock
);
31 * The following two API's must be used when attempting
32 * to serialize the updates to cpu_online_mask, cpu_present_mask.
34 void cpu_maps_update_begin(void)
36 mutex_lock(&cpu_add_remove_lock
);
39 void cpu_maps_update_done(void)
41 mutex_unlock(&cpu_add_remove_lock
);
44 static RAW_NOTIFIER_HEAD(cpu_chain
);
46 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
47 * Should always be manipulated under cpu_add_remove_lock
49 static int cpu_hotplug_disabled
;
51 #ifdef CONFIG_HOTPLUG_CPU
54 struct task_struct
*active_writer
;
55 struct mutex lock
; /* Synchronizes accesses to refcount, */
57 * Also blocks the new readers during
58 * an ongoing cpu hotplug operation.
62 #ifdef CONFIG_DEBUG_LOCK_ALLOC
63 struct lockdep_map dep_map
;
66 .active_writer
= NULL
,
67 .lock
= __MUTEX_INITIALIZER(cpu_hotplug
.lock
),
69 #ifdef CONFIG_DEBUG_LOCK_ALLOC
70 .dep_map
= {.name
= "cpu_hotplug.lock" },
74 /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
75 #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
76 #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
77 #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
79 void get_online_cpus(void)
82 if (cpu_hotplug
.active_writer
== current
)
84 cpuhp_lock_acquire_read();
85 mutex_lock(&cpu_hotplug
.lock
);
86 cpu_hotplug
.refcount
++;
87 mutex_unlock(&cpu_hotplug
.lock
);
90 EXPORT_SYMBOL_GPL(get_online_cpus
);
92 void put_online_cpus(void)
94 if (cpu_hotplug
.active_writer
== current
)
96 mutex_lock(&cpu_hotplug
.lock
);
98 if (WARN_ON(!cpu_hotplug
.refcount
))
99 cpu_hotplug
.refcount
++; /* try to fix things up */
101 if (!--cpu_hotplug
.refcount
&& unlikely(cpu_hotplug
.active_writer
))
102 wake_up_process(cpu_hotplug
.active_writer
);
103 mutex_unlock(&cpu_hotplug
.lock
);
104 cpuhp_lock_release();
107 EXPORT_SYMBOL_GPL(put_online_cpus
);
110 * This ensures that the hotplug operation can begin only when the
111 * refcount goes to zero.
113 * Note that during a cpu-hotplug operation, the new readers, if any,
114 * will be blocked by the cpu_hotplug.lock
116 * Since cpu_hotplug_begin() is always called after invoking
117 * cpu_maps_update_begin(), we can be sure that only one writer is active.
119 * Note that theoretically, there is a possibility of a livelock:
120 * - Refcount goes to zero, last reader wakes up the sleeping
122 * - Last reader unlocks the cpu_hotplug.lock.
123 * - A new reader arrives at this moment, bumps up the refcount.
124 * - The writer acquires the cpu_hotplug.lock finds the refcount
125 * non zero and goes to sleep again.
127 * However, this is very difficult to achieve in practice since
128 * get_online_cpus() not an api which is called all that often.
131 void cpu_hotplug_begin(void)
133 cpu_hotplug
.active_writer
= current
;
135 cpuhp_lock_acquire();
137 mutex_lock(&cpu_hotplug
.lock
);
138 if (likely(!cpu_hotplug
.refcount
))
140 __set_current_state(TASK_UNINTERRUPTIBLE
);
141 mutex_unlock(&cpu_hotplug
.lock
);
146 void cpu_hotplug_done(void)
148 cpu_hotplug
.active_writer
= NULL
;
149 mutex_unlock(&cpu_hotplug
.lock
);
150 cpuhp_lock_release();
154 * Wait for currently running CPU hotplug operations to complete (if any) and
155 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
156 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
157 * hotplug path before performing hotplug operations. So acquiring that lock
158 * guarantees mutual exclusion from any currently running hotplug operations.
160 void cpu_hotplug_disable(void)
162 cpu_maps_update_begin();
163 cpu_hotplug_disabled
= 1;
164 cpu_maps_update_done();
167 void cpu_hotplug_enable(void)
169 cpu_maps_update_begin();
170 cpu_hotplug_disabled
= 0;
171 cpu_maps_update_done();
174 #endif /* CONFIG_HOTPLUG_CPU */
176 /* Need to know about CPUs going up/down? */
177 int __ref
register_cpu_notifier(struct notifier_block
*nb
)
180 cpu_maps_update_begin();
181 ret
= raw_notifier_chain_register(&cpu_chain
, nb
);
182 cpu_maps_update_done();
186 static int __cpu_notify(unsigned long val
, void *v
, int nr_to_call
,
191 ret
= __raw_notifier_call_chain(&cpu_chain
, val
, v
, nr_to_call
,
194 return notifier_to_errno(ret
);
197 static int cpu_notify(unsigned long val
, void *v
)
199 return __cpu_notify(val
, v
, -1, NULL
);
202 #ifdef CONFIG_HOTPLUG_CPU
204 static void cpu_notify_nofail(unsigned long val
, void *v
)
206 BUG_ON(cpu_notify(val
, v
));
208 EXPORT_SYMBOL(register_cpu_notifier
);
210 void __ref
unregister_cpu_notifier(struct notifier_block
*nb
)
212 cpu_maps_update_begin();
213 raw_notifier_chain_unregister(&cpu_chain
, nb
);
214 cpu_maps_update_done();
216 EXPORT_SYMBOL(unregister_cpu_notifier
);
219 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
222 * This function walks all processes, finds a valid mm struct for each one and
223 * then clears a corresponding bit in mm's cpumask. While this all sounds
224 * trivial, there are various non-obvious corner cases, which this function
225 * tries to solve in a safe manner.
227 * Also note that the function uses a somewhat relaxed locking scheme, so it may
228 * be called only for an already offlined CPU.
230 void clear_tasks_mm_cpumask(int cpu
)
232 struct task_struct
*p
;
235 * This function is called after the cpu is taken down and marked
236 * offline, so its not like new tasks will ever get this cpu set in
237 * their mm mask. -- Peter Zijlstra
238 * Thus, we may use rcu_read_lock() here, instead of grabbing
239 * full-fledged tasklist_lock.
241 WARN_ON(cpu_online(cpu
));
243 for_each_process(p
) {
244 struct task_struct
*t
;
247 * Main thread might exit, but other threads may still have
248 * a valid mm. Find one.
250 t
= find_lock_task_mm(p
);
253 cpumask_clear_cpu(cpu
, mm_cpumask(t
->mm
));
259 static inline void check_for_tasks(int cpu
)
261 struct task_struct
*p
;
262 cputime_t utime
, stime
;
264 write_lock_irq(&tasklist_lock
);
265 for_each_process(p
) {
266 task_cputime(p
, &utime
, &stime
);
267 if (task_cpu(p
) == cpu
&& p
->state
== TASK_RUNNING
&&
269 printk(KERN_WARNING
"Task %s (pid = %d) is on cpu %d "
270 "(state = %ld, flags = %x)\n",
271 p
->comm
, task_pid_nr(p
), cpu
,
274 write_unlock_irq(&tasklist_lock
);
277 struct take_cpu_down_param
{
282 /* Take this CPU down. */
283 static int __ref
take_cpu_down(void *_param
)
285 struct take_cpu_down_param
*param
= _param
;
288 /* Ensure this CPU doesn't handle any more interrupts. */
289 err
= __cpu_disable();
293 cpu_notify(CPU_DYING
| param
->mod
, param
->hcpu
);
294 /* Park the stopper thread */
295 kthread_park(current
);
299 /* Requires cpu_add_remove_lock to be held */
300 static int __ref
_cpu_down(unsigned int cpu
, int tasks_frozen
)
302 int err
, nr_calls
= 0;
303 void *hcpu
= (void *)(long)cpu
;
304 unsigned long mod
= tasks_frozen
? CPU_TASKS_FROZEN
: 0;
305 struct take_cpu_down_param tcd_param
= {
310 if (num_online_cpus() == 1)
313 if (!cpu_online(cpu
))
318 err
= __cpu_notify(CPU_DOWN_PREPARE
| mod
, hcpu
, -1, &nr_calls
);
321 __cpu_notify(CPU_DOWN_FAILED
| mod
, hcpu
, nr_calls
, NULL
);
322 printk("%s: attempt to take down CPU %u failed\n",
328 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
329 * and RCU users of this state to go away such that all new such users
332 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
333 * not imply sync_sched(), so explicitly call both.
335 * Do sync before park smpboot threads to take care the rcu boost case.
337 #ifdef CONFIG_PREEMPT
342 smpboot_park_threads(cpu
);
345 * So now all preempt/rcu users must observe !cpu_active().
348 err
= __stop_machine(take_cpu_down
, &tcd_param
, cpumask_of(cpu
));
350 /* CPU didn't die: tell everyone. Can't complain. */
351 smpboot_unpark_threads(cpu
);
352 cpu_notify_nofail(CPU_DOWN_FAILED
| mod
, hcpu
);
355 BUG_ON(cpu_online(cpu
));
358 * The migration_call() CPU_DYING callback will have removed all
359 * runnable tasks from the cpu, there's only the idle task left now
360 * that the migration thread is done doing the stop_machine thing.
362 * Wait for the stop thread to go away.
364 while (!idle_cpu(cpu
))
367 /* This actually kills the CPU. */
370 /* CPU is completely dead: tell everyone. Too late to complain. */
371 cpu_notify_nofail(CPU_DEAD
| mod
, hcpu
);
373 check_for_tasks(cpu
);
378 cpu_notify_nofail(CPU_POST_DEAD
| mod
, hcpu
);
382 int __ref
cpu_down(unsigned int cpu
)
386 cpu_maps_update_begin();
388 if (cpu_hotplug_disabled
) {
393 err
= _cpu_down(cpu
, 0);
396 cpu_maps_update_done();
399 EXPORT_SYMBOL(cpu_down
);
400 #endif /*CONFIG_HOTPLUG_CPU*/
402 /* Requires cpu_add_remove_lock to be held */
403 static int _cpu_up(unsigned int cpu
, int tasks_frozen
)
405 int ret
, nr_calls
= 0;
406 void *hcpu
= (void *)(long)cpu
;
407 unsigned long mod
= tasks_frozen
? CPU_TASKS_FROZEN
: 0;
408 struct task_struct
*idle
;
412 if (cpu_online(cpu
) || !cpu_present(cpu
)) {
417 idle
= idle_thread_get(cpu
);
423 ret
= smpboot_create_threads(cpu
);
427 ret
= __cpu_notify(CPU_UP_PREPARE
| mod
, hcpu
, -1, &nr_calls
);
430 printk(KERN_WARNING
"%s: attempt to bring up CPU %u failed\n",
435 /* Arch-specific enabling code. */
436 ret
= __cpu_up(cpu
, idle
);
439 BUG_ON(!cpu_online(cpu
));
441 /* Wake the per cpu threads */
442 smpboot_unpark_threads(cpu
);
444 /* Now call notifier in preparation. */
445 cpu_notify(CPU_ONLINE
| mod
, hcpu
);
449 __cpu_notify(CPU_UP_CANCELED
| mod
, hcpu
, nr_calls
, NULL
);
456 int cpu_up(unsigned int cpu
)
460 if (!cpu_possible(cpu
)) {
461 printk(KERN_ERR
"can't online cpu %d because it is not "
462 "configured as may-hotadd at boot time\n", cpu
);
463 #if defined(CONFIG_IA64)
464 printk(KERN_ERR
"please check additional_cpus= boot "
470 err
= try_online_node(cpu_to_node(cpu
));
474 cpu_maps_update_begin();
476 if (cpu_hotplug_disabled
) {
481 err
= _cpu_up(cpu
, 0);
484 cpu_maps_update_done();
487 EXPORT_SYMBOL_GPL(cpu_up
);
489 #ifdef CONFIG_PM_SLEEP_SMP
490 static cpumask_var_t frozen_cpus
;
492 int disable_nonboot_cpus(void)
494 int cpu
, first_cpu
, error
= 0;
496 cpu_maps_update_begin();
497 first_cpu
= cpumask_first(cpu_online_mask
);
499 * We take down all of the non-boot CPUs in one shot to avoid races
500 * with the userspace trying to use the CPU hotplug at the same time
502 cpumask_clear(frozen_cpus
);
504 printk("Disabling non-boot CPUs ...\n");
505 for_each_online_cpu(cpu
) {
506 if (cpu
== first_cpu
)
508 error
= _cpu_down(cpu
, 1);
510 cpumask_set_cpu(cpu
, frozen_cpus
);
512 printk(KERN_ERR
"Error taking CPU%d down: %d\n",
519 BUG_ON(num_online_cpus() > 1);
520 /* Make sure the CPUs won't be enabled by someone else */
521 cpu_hotplug_disabled
= 1;
523 printk(KERN_ERR
"Non-boot CPUs are not disabled\n");
525 cpu_maps_update_done();
529 void __weak
arch_enable_nonboot_cpus_begin(void)
533 void __weak
arch_enable_nonboot_cpus_end(void)
537 void __ref
enable_nonboot_cpus(void)
541 /* Allow everyone to use the CPU hotplug again */
542 cpu_maps_update_begin();
543 cpu_hotplug_disabled
= 0;
544 if (cpumask_empty(frozen_cpus
))
547 printk(KERN_INFO
"Enabling non-boot CPUs ...\n");
549 arch_enable_nonboot_cpus_begin();
551 for_each_cpu(cpu
, frozen_cpus
) {
552 error
= _cpu_up(cpu
, 1);
554 printk(KERN_INFO
"CPU%d is up\n", cpu
);
557 printk(KERN_WARNING
"Error taking CPU%d up: %d\n", cpu
, error
);
560 arch_enable_nonboot_cpus_end();
562 cpumask_clear(frozen_cpus
);
564 cpu_maps_update_done();
567 static int __init
alloc_frozen_cpus(void)
569 if (!alloc_cpumask_var(&frozen_cpus
, GFP_KERNEL
|__GFP_ZERO
))
573 core_initcall(alloc_frozen_cpus
);
576 * When callbacks for CPU hotplug notifications are being executed, we must
577 * ensure that the state of the system with respect to the tasks being frozen
578 * or not, as reported by the notification, remains unchanged *throughout the
579 * duration* of the execution of the callbacks.
580 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
582 * This synchronization is implemented by mutually excluding regular CPU
583 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
584 * Hibernate notifications.
587 cpu_hotplug_pm_callback(struct notifier_block
*nb
,
588 unsigned long action
, void *ptr
)
592 case PM_SUSPEND_PREPARE
:
593 case PM_HIBERNATION_PREPARE
:
594 cpu_hotplug_disable();
597 case PM_POST_SUSPEND
:
598 case PM_POST_HIBERNATION
:
599 cpu_hotplug_enable();
610 static int __init
cpu_hotplug_pm_sync_init(void)
613 * cpu_hotplug_pm_callback has higher priority than x86
614 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
615 * to disable cpu hotplug to avoid cpu hotplug race.
617 pm_notifier(cpu_hotplug_pm_callback
, 0);
620 core_initcall(cpu_hotplug_pm_sync_init
);
622 #endif /* CONFIG_PM_SLEEP_SMP */
625 * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
626 * @cpu: cpu that just started
628 * This function calls the cpu_chain notifiers with CPU_STARTING.
629 * It must be called by the arch code on the new cpu, before the new cpu
630 * enables interrupts and before the "boot" cpu returns from __cpu_up().
632 void notify_cpu_starting(unsigned int cpu
)
634 unsigned long val
= CPU_STARTING
;
636 #ifdef CONFIG_PM_SLEEP_SMP
637 if (frozen_cpus
!= NULL
&& cpumask_test_cpu(cpu
, frozen_cpus
))
638 val
= CPU_STARTING_FROZEN
;
639 #endif /* CONFIG_PM_SLEEP_SMP */
640 cpu_notify(val
, (void *)(long)cpu
);
643 #endif /* CONFIG_SMP */
646 * cpu_bit_bitmap[] is a special, "compressed" data structure that
647 * represents all NR_CPUS bits binary values of 1<<nr.
649 * It is used by cpumask_of() to get a constant address to a CPU
650 * mask value that has a single bit set only.
653 /* cpu_bit_bitmap[0] is empty - so we can back into it */
654 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
655 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
656 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
657 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
659 const unsigned long cpu_bit_bitmap
[BITS_PER_LONG
+1][BITS_TO_LONGS(NR_CPUS
)] = {
661 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
662 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
663 #if BITS_PER_LONG > 32
664 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
665 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
668 EXPORT_SYMBOL_GPL(cpu_bit_bitmap
);
670 const DECLARE_BITMAP(cpu_all_bits
, NR_CPUS
) = CPU_BITS_ALL
;
671 EXPORT_SYMBOL(cpu_all_bits
);
673 #ifdef CONFIG_INIT_ALL_POSSIBLE
674 static DECLARE_BITMAP(cpu_possible_bits
, CONFIG_NR_CPUS
) __read_mostly
677 static DECLARE_BITMAP(cpu_possible_bits
, CONFIG_NR_CPUS
) __read_mostly
;
679 const struct cpumask
*const cpu_possible_mask
= to_cpumask(cpu_possible_bits
);
680 EXPORT_SYMBOL(cpu_possible_mask
);
682 static DECLARE_BITMAP(cpu_online_bits
, CONFIG_NR_CPUS
) __read_mostly
;
683 const struct cpumask
*const cpu_online_mask
= to_cpumask(cpu_online_bits
);
684 EXPORT_SYMBOL(cpu_online_mask
);
686 static DECLARE_BITMAP(cpu_present_bits
, CONFIG_NR_CPUS
) __read_mostly
;
687 const struct cpumask
*const cpu_present_mask
= to_cpumask(cpu_present_bits
);
688 EXPORT_SYMBOL(cpu_present_mask
);
690 static DECLARE_BITMAP(cpu_active_bits
, CONFIG_NR_CPUS
) __read_mostly
;
691 const struct cpumask
*const cpu_active_mask
= to_cpumask(cpu_active_bits
);
692 EXPORT_SYMBOL(cpu_active_mask
);
694 void set_cpu_possible(unsigned int cpu
, bool possible
)
697 cpumask_set_cpu(cpu
, to_cpumask(cpu_possible_bits
));
699 cpumask_clear_cpu(cpu
, to_cpumask(cpu_possible_bits
));
702 void set_cpu_present(unsigned int cpu
, bool present
)
705 cpumask_set_cpu(cpu
, to_cpumask(cpu_present_bits
));
707 cpumask_clear_cpu(cpu
, to_cpumask(cpu_present_bits
));
710 void set_cpu_online(unsigned int cpu
, bool online
)
713 cpumask_set_cpu(cpu
, to_cpumask(cpu_online_bits
));
715 cpumask_clear_cpu(cpu
, to_cpumask(cpu_online_bits
));
718 void set_cpu_active(unsigned int cpu
, bool active
)
721 cpumask_set_cpu(cpu
, to_cpumask(cpu_active_bits
));
723 cpumask_clear_cpu(cpu
, to_cpumask(cpu_active_bits
));
726 void init_cpu_present(const struct cpumask
*src
)
728 cpumask_copy(to_cpumask(cpu_present_bits
), src
);
731 void init_cpu_possible(const struct cpumask
*src
)
733 cpumask_copy(to_cpumask(cpu_possible_bits
), src
);
736 void init_cpu_online(const struct cpumask
*src
)
738 cpumask_copy(to_cpumask(cpu_online_bits
), src
);