xref: /linux-6.15/kernel/cpu.c (revision 62db99f4)
11da177e4SLinus Torvalds /* CPU control.
21da177e4SLinus Torvalds  * (C) 2001, 2002, 2003, 2004 Rusty Russell
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This code is licenced under the GPL.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #include <linux/proc_fs.h>
71da177e4SLinus Torvalds #include <linux/smp.h>
81da177e4SLinus Torvalds #include <linux/init.h>
91da177e4SLinus Torvalds #include <linux/notifier.h>
101da177e4SLinus Torvalds #include <linux/sched.h>
111da177e4SLinus Torvalds #include <linux/unistd.h>
121da177e4SLinus Torvalds #include <linux/cpu.h>
13cb79295eSAnton Vorontsov #include <linux/oom.h>
14cb79295eSAnton Vorontsov #include <linux/rcupdate.h>
159984de1aSPaul Gortmaker #include <linux/export.h>
16e4cc2f87SAnton Vorontsov #include <linux/bug.h>
171da177e4SLinus Torvalds #include <linux/kthread.h>
181da177e4SLinus Torvalds #include <linux/stop_machine.h>
1981615b62SIngo Molnar #include <linux/mutex.h>
205a0e3ad6STejun Heo #include <linux/gfp.h>
2179cfbdfaSSrivatsa S. Bhat #include <linux/suspend.h>
22a19423b9SGautham R. Shenoy #include <linux/lockdep.h>
23bb3632c6STodd E Brandt #include <trace/events/power.h>
241da177e4SLinus Torvalds 
2538498a67SThomas Gleixner #include "smpboot.h"
2638498a67SThomas Gleixner 
2798a79d6aSRusty Russell #ifdef CONFIG_SMP
28b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */
29aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock);
301da177e4SLinus Torvalds 
3179a6cdebSLai Jiangshan /*
3293ae4f97SSrivatsa S. Bhat  * The following two APIs (cpu_maps_update_begin/done) must be used when
3393ae4f97SSrivatsa S. Bhat  * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
3493ae4f97SSrivatsa S. Bhat  * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
3593ae4f97SSrivatsa S. Bhat  * hotplug callback (un)registration performed using __register_cpu_notifier()
3693ae4f97SSrivatsa S. Bhat  * or __unregister_cpu_notifier().
3779a6cdebSLai Jiangshan  */
3879a6cdebSLai Jiangshan void cpu_maps_update_begin(void)
3979a6cdebSLai Jiangshan {
4079a6cdebSLai Jiangshan 	mutex_lock(&cpu_add_remove_lock);
4179a6cdebSLai Jiangshan }
4293ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_begin);
4379a6cdebSLai Jiangshan 
4479a6cdebSLai Jiangshan void cpu_maps_update_done(void)
4579a6cdebSLai Jiangshan {
4679a6cdebSLai Jiangshan 	mutex_unlock(&cpu_add_remove_lock);
4779a6cdebSLai Jiangshan }
4893ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_done);
4979a6cdebSLai Jiangshan 
505c113fbeSDaniel J Blueman static RAW_NOTIFIER_HEAD(cpu_chain);
511da177e4SLinus Torvalds 
52e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
53e3920fb4SRafael J. Wysocki  * Should always be manipulated under cpu_add_remove_lock
54e3920fb4SRafael J. Wysocki  */
55e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled;
56e3920fb4SRafael J. Wysocki 
5779a6cdebSLai Jiangshan #ifdef CONFIG_HOTPLUG_CPU
5879a6cdebSLai Jiangshan 
59d221938cSGautham R Shenoy static struct {
60d221938cSGautham R Shenoy 	struct task_struct *active_writer;
61d221938cSGautham R Shenoy 	struct mutex lock; /* Synchronizes accesses to refcount, */
62d221938cSGautham R Shenoy 	/*
63d221938cSGautham R Shenoy 	 * Also blocks the new readers during
64d221938cSGautham R Shenoy 	 * an ongoing cpu hotplug operation.
65d221938cSGautham R Shenoy 	 */
66d221938cSGautham R Shenoy 	int refcount;
67b2c4623dSPaul E. McKenney 	/* And allows lockless put_online_cpus(). */
68b2c4623dSPaul E. McKenney 	atomic_t puts_pending;
69a19423b9SGautham R. Shenoy 
70a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
71a19423b9SGautham R. Shenoy 	struct lockdep_map dep_map;
72a19423b9SGautham R. Shenoy #endif
7331950eb6SLinus Torvalds } cpu_hotplug = {
7431950eb6SLinus Torvalds 	.active_writer = NULL,
7531950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
7631950eb6SLinus Torvalds 	.refcount = 0,
77a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
78a19423b9SGautham R. Shenoy 	.dep_map = {.name = "cpu_hotplug.lock" },
79a19423b9SGautham R. Shenoy #endif
8031950eb6SLinus Torvalds };
81d221938cSGautham R Shenoy 
82a19423b9SGautham R. Shenoy /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
83a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
84dd56af42SPaul E. McKenney #define cpuhp_lock_acquire_tryread() \
85dd56af42SPaul E. McKenney 				  lock_map_acquire_tryread(&cpu_hotplug.dep_map)
86a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
87a19423b9SGautham R. Shenoy #define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)
88a19423b9SGautham R. Shenoy 
89*62db99f4SPaul E. McKenney static void apply_puts_pending(int max)
90*62db99f4SPaul E. McKenney {
91*62db99f4SPaul E. McKenney 	int delta;
92*62db99f4SPaul E. McKenney 
93*62db99f4SPaul E. McKenney 	if (atomic_read(&cpu_hotplug.puts_pending) >= max) {
94*62db99f4SPaul E. McKenney 		delta = atomic_xchg(&cpu_hotplug.puts_pending, 0);
95*62db99f4SPaul E. McKenney 		cpu_hotplug.refcount -= delta;
96*62db99f4SPaul E. McKenney 	}
97*62db99f4SPaul E. McKenney }
98*62db99f4SPaul E. McKenney 
9986ef5c9aSGautham R Shenoy void get_online_cpus(void)
100a9d9baa1SAshok Raj {
101d221938cSGautham R Shenoy 	might_sleep();
102d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
103aa953877SLinus Torvalds 		return;
104a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire_read();
105d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
106*62db99f4SPaul E. McKenney 	apply_puts_pending(65536);
107d221938cSGautham R Shenoy 	cpu_hotplug.refcount++;
108d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
109a9d9baa1SAshok Raj }
11086ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
111a9d9baa1SAshok Raj 
112dd56af42SPaul E. McKenney bool try_get_online_cpus(void)
113dd56af42SPaul E. McKenney {
114dd56af42SPaul E. McKenney 	if (cpu_hotplug.active_writer == current)
115dd56af42SPaul E. McKenney 		return true;
116dd56af42SPaul E. McKenney 	if (!mutex_trylock(&cpu_hotplug.lock))
117dd56af42SPaul E. McKenney 		return false;
118dd56af42SPaul E. McKenney 	cpuhp_lock_acquire_tryread();
119*62db99f4SPaul E. McKenney 	apply_puts_pending(65536);
120dd56af42SPaul E. McKenney 	cpu_hotplug.refcount++;
121dd56af42SPaul E. McKenney 	mutex_unlock(&cpu_hotplug.lock);
122dd56af42SPaul E. McKenney 	return true;
123dd56af42SPaul E. McKenney }
124dd56af42SPaul E. McKenney EXPORT_SYMBOL_GPL(try_get_online_cpus);
125dd56af42SPaul E. McKenney 
12686ef5c9aSGautham R Shenoy void put_online_cpus(void)
127a9d9baa1SAshok Raj {
128d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
129aa953877SLinus Torvalds 		return;
130b2c4623dSPaul E. McKenney 	if (!mutex_trylock(&cpu_hotplug.lock)) {
131b2c4623dSPaul E. McKenney 		atomic_inc(&cpu_hotplug.puts_pending);
132b2c4623dSPaul E. McKenney 		cpuhp_lock_release();
133b2c4623dSPaul E. McKenney 		return;
134b2c4623dSPaul E. McKenney 	}
135075663d1SSrivatsa S. Bhat 
136075663d1SSrivatsa S. Bhat 	if (WARN_ON(!cpu_hotplug.refcount))
137075663d1SSrivatsa S. Bhat 		cpu_hotplug.refcount++; /* try to fix things up */
138075663d1SSrivatsa S. Bhat 
139d2ba7e2aSOleg Nesterov 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
140d2ba7e2aSOleg Nesterov 		wake_up_process(cpu_hotplug.active_writer);
141d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
142a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
143d221938cSGautham R Shenoy 
144a9d9baa1SAshok Raj }
14586ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
146a9d9baa1SAshok Raj 
147d221938cSGautham R Shenoy /*
148d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
149d221938cSGautham R Shenoy  * refcount goes to zero.
150d221938cSGautham R Shenoy  *
151d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
152d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
153d221938cSGautham R Shenoy  *
154d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
155d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
156d221938cSGautham R Shenoy  *
157d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
158d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
159d221938cSGautham R Shenoy  *   writer.
160d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
161d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
162d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
163d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
164d221938cSGautham R Shenoy  *
165d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
16686ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
167d221938cSGautham R Shenoy  *
168d221938cSGautham R Shenoy  */
169b9d10be7SToshi Kani void cpu_hotplug_begin(void)
170d221938cSGautham R Shenoy {
171d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = current;
172d2ba7e2aSOleg Nesterov 
173a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire();
174d2ba7e2aSOleg Nesterov 	for (;;) {
175d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
176*62db99f4SPaul E. McKenney 		apply_puts_pending(1);
177d2ba7e2aSOleg Nesterov 		if (likely(!cpu_hotplug.refcount))
178d2ba7e2aSOleg Nesterov 			break;
179d2ba7e2aSOleg Nesterov 		__set_current_state(TASK_UNINTERRUPTIBLE);
180d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
181d221938cSGautham R Shenoy 		schedule();
182d221938cSGautham R Shenoy 	}
183d221938cSGautham R Shenoy }
184d221938cSGautham R Shenoy 
185b9d10be7SToshi Kani void cpu_hotplug_done(void)
186d221938cSGautham R Shenoy {
187d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
188d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
189a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
190d221938cSGautham R Shenoy }
19179a6cdebSLai Jiangshan 
19216e53dbfSSrivatsa S. Bhat /*
19316e53dbfSSrivatsa S. Bhat  * Wait for currently running CPU hotplug operations to complete (if any) and
19416e53dbfSSrivatsa S. Bhat  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
19516e53dbfSSrivatsa S. Bhat  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
19616e53dbfSSrivatsa S. Bhat  * hotplug path before performing hotplug operations. So acquiring that lock
19716e53dbfSSrivatsa S. Bhat  * guarantees mutual exclusion from any currently running hotplug operations.
19816e53dbfSSrivatsa S. Bhat  */
19916e53dbfSSrivatsa S. Bhat void cpu_hotplug_disable(void)
20016e53dbfSSrivatsa S. Bhat {
20116e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
20216e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 1;
20316e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
20416e53dbfSSrivatsa S. Bhat }
20516e53dbfSSrivatsa S. Bhat 
20616e53dbfSSrivatsa S. Bhat void cpu_hotplug_enable(void)
20716e53dbfSSrivatsa S. Bhat {
20816e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
20916e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 0;
21016e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
21116e53dbfSSrivatsa S. Bhat }
21216e53dbfSSrivatsa S. Bhat 
213b9d10be7SToshi Kani #endif	/* CONFIG_HOTPLUG_CPU */
21479a6cdebSLai Jiangshan 
2151da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
216f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
2171da177e4SLinus Torvalds {
218bd5349cfSNeil Brown 	int ret;
219d221938cSGautham R Shenoy 	cpu_maps_update_begin();
220bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
221d221938cSGautham R Shenoy 	cpu_maps_update_done();
222bd5349cfSNeil Brown 	return ret;
2231da177e4SLinus Torvalds }
22465edc68cSChandra Seetharaman 
22593ae4f97SSrivatsa S. Bhat int __ref __register_cpu_notifier(struct notifier_block *nb)
22693ae4f97SSrivatsa S. Bhat {
22793ae4f97SSrivatsa S. Bhat 	return raw_notifier_chain_register(&cpu_chain, nb);
22893ae4f97SSrivatsa S. Bhat }
22993ae4f97SSrivatsa S. Bhat 
230e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
231e9fb7631SAkinobu Mita 			int *nr_calls)
232e9fb7631SAkinobu Mita {
233e6bde73bSAkinobu Mita 	int ret;
234e6bde73bSAkinobu Mita 
235e6bde73bSAkinobu Mita 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
236e9fb7631SAkinobu Mita 					nr_calls);
237e6bde73bSAkinobu Mita 
238e6bde73bSAkinobu Mita 	return notifier_to_errno(ret);
239e9fb7631SAkinobu Mita }
240e9fb7631SAkinobu Mita 
241e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v)
242e9fb7631SAkinobu Mita {
243e9fb7631SAkinobu Mita 	return __cpu_notify(val, v, -1, NULL);
244e9fb7631SAkinobu Mita }
245e9fb7631SAkinobu Mita 
24600b9b0afSLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
24700b9b0afSLinus Torvalds 
248e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v)
249e9fb7631SAkinobu Mita {
25000b9b0afSLinus Torvalds 	BUG_ON(cpu_notify(val, v));
251e9fb7631SAkinobu Mita }
2521da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
25393ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__register_cpu_notifier);
2541da177e4SLinus Torvalds 
2559647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
2561da177e4SLinus Torvalds {
257d221938cSGautham R Shenoy 	cpu_maps_update_begin();
258bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
259d221938cSGautham R Shenoy 	cpu_maps_update_done();
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
2621da177e4SLinus Torvalds 
26393ae4f97SSrivatsa S. Bhat void __ref __unregister_cpu_notifier(struct notifier_block *nb)
26493ae4f97SSrivatsa S. Bhat {
26593ae4f97SSrivatsa S. Bhat 	raw_notifier_chain_unregister(&cpu_chain, nb);
26693ae4f97SSrivatsa S. Bhat }
26793ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__unregister_cpu_notifier);
26893ae4f97SSrivatsa S. Bhat 
269e4cc2f87SAnton Vorontsov /**
270e4cc2f87SAnton Vorontsov  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
271e4cc2f87SAnton Vorontsov  * @cpu: a CPU id
272e4cc2f87SAnton Vorontsov  *
273e4cc2f87SAnton Vorontsov  * This function walks all processes, finds a valid mm struct for each one and
274e4cc2f87SAnton Vorontsov  * then clears a corresponding bit in mm's cpumask.  While this all sounds
275e4cc2f87SAnton Vorontsov  * trivial, there are various non-obvious corner cases, which this function
276e4cc2f87SAnton Vorontsov  * tries to solve in a safe manner.
277e4cc2f87SAnton Vorontsov  *
278e4cc2f87SAnton Vorontsov  * Also note that the function uses a somewhat relaxed locking scheme, so it may
279e4cc2f87SAnton Vorontsov  * be called only for an already offlined CPU.
280e4cc2f87SAnton Vorontsov  */
281cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu)
282cb79295eSAnton Vorontsov {
283cb79295eSAnton Vorontsov 	struct task_struct *p;
284cb79295eSAnton Vorontsov 
285cb79295eSAnton Vorontsov 	/*
286cb79295eSAnton Vorontsov 	 * This function is called after the cpu is taken down and marked
287cb79295eSAnton Vorontsov 	 * offline, so its not like new tasks will ever get this cpu set in
288cb79295eSAnton Vorontsov 	 * their mm mask. -- Peter Zijlstra
289cb79295eSAnton Vorontsov 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
290cb79295eSAnton Vorontsov 	 * full-fledged tasklist_lock.
291cb79295eSAnton Vorontsov 	 */
292e4cc2f87SAnton Vorontsov 	WARN_ON(cpu_online(cpu));
293cb79295eSAnton Vorontsov 	rcu_read_lock();
294cb79295eSAnton Vorontsov 	for_each_process(p) {
295cb79295eSAnton Vorontsov 		struct task_struct *t;
296cb79295eSAnton Vorontsov 
297e4cc2f87SAnton Vorontsov 		/*
298e4cc2f87SAnton Vorontsov 		 * Main thread might exit, but other threads may still have
299e4cc2f87SAnton Vorontsov 		 * a valid mm. Find one.
300e4cc2f87SAnton Vorontsov 		 */
301cb79295eSAnton Vorontsov 		t = find_lock_task_mm(p);
302cb79295eSAnton Vorontsov 		if (!t)
303cb79295eSAnton Vorontsov 			continue;
304cb79295eSAnton Vorontsov 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
305cb79295eSAnton Vorontsov 		task_unlock(t);
306cb79295eSAnton Vorontsov 	}
307cb79295eSAnton Vorontsov 	rcu_read_unlock();
308cb79295eSAnton Vorontsov }
309cb79295eSAnton Vorontsov 
310b728ca06SKirill Tkhai static inline void check_for_tasks(int dead_cpu)
3111da177e4SLinus Torvalds {
312b728ca06SKirill Tkhai 	struct task_struct *g, *p;
3131da177e4SLinus Torvalds 
314b728ca06SKirill Tkhai 	read_lock_irq(&tasklist_lock);
315b728ca06SKirill Tkhai 	do_each_thread(g, p) {
316b728ca06SKirill Tkhai 		if (!p->on_rq)
317b728ca06SKirill Tkhai 			continue;
318b728ca06SKirill Tkhai 		/*
319b728ca06SKirill Tkhai 		 * We do the check with unlocked task_rq(p)->lock.
320b728ca06SKirill Tkhai 		 * Order the reading to do not warn about a task,
321b728ca06SKirill Tkhai 		 * which was running on this cpu in the past, and
322b728ca06SKirill Tkhai 		 * it's just been woken on another cpu.
323b728ca06SKirill Tkhai 		 */
324b728ca06SKirill Tkhai 		rmb();
325b728ca06SKirill Tkhai 		if (task_cpu(p) != dead_cpu)
326b728ca06SKirill Tkhai 			continue;
327b728ca06SKirill Tkhai 
32884117da5SFabian Frederick 		pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
329b728ca06SKirill Tkhai 			p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
330b728ca06SKirill Tkhai 	} while_each_thread(g, p);
331b728ca06SKirill Tkhai 	read_unlock_irq(&tasklist_lock);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
334db912f96SAvi Kivity struct take_cpu_down_param {
335db912f96SAvi Kivity 	unsigned long mod;
336db912f96SAvi Kivity 	void *hcpu;
337db912f96SAvi Kivity };
338db912f96SAvi Kivity 
3391da177e4SLinus Torvalds /* Take this CPU down. */
340514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
3411da177e4SLinus Torvalds {
342db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
3431da177e4SLinus Torvalds 	int err;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
3461da177e4SLinus Torvalds 	err = __cpu_disable();
3471da177e4SLinus Torvalds 	if (err < 0)
348f3705136SZwane Mwaikambo 		return err;
349f3705136SZwane Mwaikambo 
350e9fb7631SAkinobu Mita 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
35114e568e7SThomas Gleixner 	/* Park the stopper thread */
35214e568e7SThomas Gleixner 	kthread_park(current);
353f3705136SZwane Mwaikambo 	return 0;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
356e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
357514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
3581da177e4SLinus Torvalds {
359e7407dccSHeiko Carstens 	int err, nr_calls = 0;
360e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
3618bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
362db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
363db912f96SAvi Kivity 		.mod = mod,
364db912f96SAvi Kivity 		.hcpu = hcpu,
365db912f96SAvi Kivity 	};
3661da177e4SLinus Torvalds 
367e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
368e3920fb4SRafael J. Wysocki 		return -EBUSY;
3691da177e4SLinus Torvalds 
370e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
371e3920fb4SRafael J. Wysocki 		return -EINVAL;
3721da177e4SLinus Torvalds 
373d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3744d51985eSMichael Rodriguez 
375e9fb7631SAkinobu Mita 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
376e6bde73bSAkinobu Mita 	if (err) {
377a0d8cdb6SAkinobu Mita 		nr_calls--;
378e9fb7631SAkinobu Mita 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
37984117da5SFabian Frederick 		pr_warn("%s: attempt to take down CPU %u failed\n",
380af1f16d0SHarvey Harrison 			__func__, cpu);
381baaca49fSGautham R Shenoy 		goto out_release;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
3846acce3efSPeter Zijlstra 	/*
3856acce3efSPeter Zijlstra 	 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
3866acce3efSPeter Zijlstra 	 * and RCU users of this state to go away such that all new such users
3876acce3efSPeter Zijlstra 	 * will observe it.
3886acce3efSPeter Zijlstra 	 *
3896acce3efSPeter Zijlstra 	 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
3906acce3efSPeter Zijlstra 	 * not imply sync_sched(), so explicitly call both.
391106dd5afSMichael wang 	 *
392106dd5afSMichael wang 	 * Do sync before park smpboot threads to take care the rcu boost case.
3936acce3efSPeter Zijlstra 	 */
3946acce3efSPeter Zijlstra #ifdef CONFIG_PREEMPT
3956acce3efSPeter Zijlstra 	synchronize_sched();
3966acce3efSPeter Zijlstra #endif
3976acce3efSPeter Zijlstra 	synchronize_rcu();
3986acce3efSPeter Zijlstra 
399106dd5afSMichael wang 	smpboot_park_threads(cpu);
400106dd5afSMichael wang 
4016acce3efSPeter Zijlstra 	/*
4026acce3efSPeter Zijlstra 	 * So now all preempt/rcu users must observe !cpu_active().
4036acce3efSPeter Zijlstra 	 */
4046acce3efSPeter Zijlstra 
405e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
40604321587SRusty Russell 	if (err) {
4071da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
408f97f8f06SThomas Gleixner 		smpboot_unpark_threads(cpu);
409e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
4106a1bdc1bSOleg Nesterov 		goto out_release;
4111da177e4SLinus Torvalds 	}
41204321587SRusty Russell 	BUG_ON(cpu_online(cpu));
4131da177e4SLinus Torvalds 
41448c5ccaeSPeter Zijlstra 	/*
41548c5ccaeSPeter Zijlstra 	 * The migration_call() CPU_DYING callback will have removed all
41648c5ccaeSPeter Zijlstra 	 * runnable tasks from the cpu, there's only the idle task left now
41748c5ccaeSPeter Zijlstra 	 * that the migration thread is done doing the stop_machine thing.
41851a96c77SPeter Zijlstra 	 *
41951a96c77SPeter Zijlstra 	 * Wait for the stop thread to go away.
42048c5ccaeSPeter Zijlstra 	 */
42151a96c77SPeter Zijlstra 	while (!idle_cpu(cpu))
42251a96c77SPeter Zijlstra 		cpu_relax();
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	/* This actually kills the CPU. */
4251da177e4SLinus Torvalds 	__cpu_die(cpu);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
428e9fb7631SAkinobu Mita 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	check_for_tasks(cpu);
4311da177e4SLinus Torvalds 
432baaca49fSGautham R Shenoy out_release:
433d221938cSGautham R Shenoy 	cpu_hotplug_done();
434e9fb7631SAkinobu Mita 	if (!err)
435e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
436e3920fb4SRafael J. Wysocki 	return err;
437e3920fb4SRafael J. Wysocki }
438e3920fb4SRafael J. Wysocki 
439514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
440e3920fb4SRafael J. Wysocki {
4419ea09af3SHeiko Carstens 	int err;
442e3920fb4SRafael J. Wysocki 
443d221938cSGautham R Shenoy 	cpu_maps_update_begin();
444e761b772SMax Krasnyansky 
445e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
446e3920fb4SRafael J. Wysocki 		err = -EBUSY;
447e761b772SMax Krasnyansky 		goto out;
448e761b772SMax Krasnyansky 	}
449e761b772SMax Krasnyansky 
4508bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
451e3920fb4SRafael J. Wysocki 
452e761b772SMax Krasnyansky out:
453d221938cSGautham R Shenoy 	cpu_maps_update_done();
4541da177e4SLinus Torvalds 	return err;
4551da177e4SLinus Torvalds }
456b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
4571da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
4581da177e4SLinus Torvalds 
459e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
4600db0628dSPaul Gortmaker static int _cpu_up(unsigned int cpu, int tasks_frozen)
4611da177e4SLinus Torvalds {
462baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
4631da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
4648bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
4653bb5d2eeSSuresh Siddha 	struct task_struct *idle;
4661da177e4SLinus Torvalds 
467d221938cSGautham R Shenoy 	cpu_hotplug_begin();
46838498a67SThomas Gleixner 
4695e5041f3SYasuaki Ishimatsu 	if (cpu_online(cpu) || !cpu_present(cpu)) {
4705e5041f3SYasuaki Ishimatsu 		ret = -EINVAL;
4715e5041f3SYasuaki Ishimatsu 		goto out;
4725e5041f3SYasuaki Ishimatsu 	}
4735e5041f3SYasuaki Ishimatsu 
4743bb5d2eeSSuresh Siddha 	idle = idle_thread_get(cpu);
4753bb5d2eeSSuresh Siddha 	if (IS_ERR(idle)) {
4763bb5d2eeSSuresh Siddha 		ret = PTR_ERR(idle);
47738498a67SThomas Gleixner 		goto out;
4783bb5d2eeSSuresh Siddha 	}
47938498a67SThomas Gleixner 
480f97f8f06SThomas Gleixner 	ret = smpboot_create_threads(cpu);
481f97f8f06SThomas Gleixner 	if (ret)
482f97f8f06SThomas Gleixner 		goto out;
483f97f8f06SThomas Gleixner 
484e9fb7631SAkinobu Mita 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
485e6bde73bSAkinobu Mita 	if (ret) {
486a0d8cdb6SAkinobu Mita 		nr_calls--;
48784117da5SFabian Frederick 		pr_warn("%s: attempt to bring up CPU %u failed\n",
488af1f16d0SHarvey Harrison 			__func__, cpu);
4891da177e4SLinus Torvalds 		goto out_notify;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
4933bb5d2eeSSuresh Siddha 	ret = __cpu_up(cpu, idle);
4941da177e4SLinus Torvalds 	if (ret != 0)
4951da177e4SLinus Torvalds 		goto out_notify;
4966978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
4971da177e4SLinus Torvalds 
498f97f8f06SThomas Gleixner 	/* Wake the per cpu threads */
499f97f8f06SThomas Gleixner 	smpboot_unpark_threads(cpu);
500f97f8f06SThomas Gleixner 
5011da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
502e9fb7631SAkinobu Mita 	cpu_notify(CPU_ONLINE | mod, hcpu);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds out_notify:
5051da177e4SLinus Torvalds 	if (ret != 0)
506e9fb7631SAkinobu Mita 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
50738498a67SThomas Gleixner out:
508d221938cSGautham R Shenoy 	cpu_hotplug_done();
509e3920fb4SRafael J. Wysocki 
5101da177e4SLinus Torvalds 	return ret;
5111da177e4SLinus Torvalds }
512e3920fb4SRafael J. Wysocki 
5130db0628dSPaul Gortmaker int cpu_up(unsigned int cpu)
514e3920fb4SRafael J. Wysocki {
515e3920fb4SRafael J. Wysocki 	int err = 0;
516cf23422bSminskey guo 
517e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
51884117da5SFabian Frederick 		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
51984117da5SFabian Frederick 		       cpu);
52087d5e023SChen Gong #if defined(CONFIG_IA64)
52184117da5SFabian Frederick 		pr_err("please check additional_cpus= boot parameter\n");
52273e753a5SKAMEZAWA Hiroyuki #endif
52373e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
52473e753a5SKAMEZAWA Hiroyuki 	}
525e3920fb4SRafael J. Wysocki 
52601b0f197SToshi Kani 	err = try_online_node(cpu_to_node(cpu));
527cf23422bSminskey guo 	if (err)
528cf23422bSminskey guo 		return err;
529cf23422bSminskey guo 
530d221938cSGautham R Shenoy 	cpu_maps_update_begin();
531e761b772SMax Krasnyansky 
532e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
533e3920fb4SRafael J. Wysocki 		err = -EBUSY;
534e761b772SMax Krasnyansky 		goto out;
535e761b772SMax Krasnyansky 	}
536e761b772SMax Krasnyansky 
5378bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
538e3920fb4SRafael J. Wysocki 
539e761b772SMax Krasnyansky out:
540d221938cSGautham R Shenoy 	cpu_maps_update_done();
541e3920fb4SRafael J. Wysocki 	return err;
542e3920fb4SRafael J. Wysocki }
543a513f6baSPaul E. McKenney EXPORT_SYMBOL_GPL(cpu_up);
544e3920fb4SRafael J. Wysocki 
545f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
546e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
547e3920fb4SRafael J. Wysocki 
548e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
549e3920fb4SRafael J. Wysocki {
550e9a5f426SRafael J. Wysocki 	int cpu, first_cpu, error = 0;
551e3920fb4SRafael J. Wysocki 
552d221938cSGautham R Shenoy 	cpu_maps_update_begin();
553e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
5549ee349adSXiaotian Feng 	/*
5559ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
556e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
557e3920fb4SRafael J. Wysocki 	 */
558e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5596ad4c188SPeter Zijlstra 
56084117da5SFabian Frederick 	pr_info("Disabling non-boot CPUs ...\n");
561e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
562e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
563e3920fb4SRafael J. Wysocki 			continue;
564bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
5658bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
566bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
567feae3203SMike Travis 		if (!error)
568e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
569feae3203SMike Travis 		else {
57084117da5SFabian Frederick 			pr_err("Error taking CPU%d down: %d\n", cpu, error);
571e3920fb4SRafael J. Wysocki 			break;
572e3920fb4SRafael J. Wysocki 		}
573e3920fb4SRafael J. Wysocki 	}
57486886e55SJoseph Cihula 
575e3920fb4SRafael J. Wysocki 	if (!error) {
576e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
577e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
578e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
579e3920fb4SRafael J. Wysocki 	} else {
58084117da5SFabian Frederick 		pr_err("Non-boot CPUs are not disabled\n");
581e3920fb4SRafael J. Wysocki 	}
582d221938cSGautham R Shenoy 	cpu_maps_update_done();
583e3920fb4SRafael J. Wysocki 	return error;
584e3920fb4SRafael J. Wysocki }
585e3920fb4SRafael J. Wysocki 
586d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
587d0af9eedSSuresh Siddha {
588d0af9eedSSuresh Siddha }
589d0af9eedSSuresh Siddha 
590d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
591d0af9eedSSuresh Siddha {
592d0af9eedSSuresh Siddha }
593d0af9eedSSuresh Siddha 
594fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
595e3920fb4SRafael J. Wysocki {
596e3920fb4SRafael J. Wysocki 	int cpu, error;
597e3920fb4SRafael J. Wysocki 
598e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
599d221938cSGautham R Shenoy 	cpu_maps_update_begin();
600e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
601e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
6021d64b9cbSRafael J. Wysocki 		goto out;
603e3920fb4SRafael J. Wysocki 
60484117da5SFabian Frederick 	pr_info("Enabling non-boot CPUs ...\n");
605d0af9eedSSuresh Siddha 
606d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
607d0af9eedSSuresh Siddha 
608e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
609bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
6108bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
611bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
612e3920fb4SRafael J. Wysocki 		if (!error) {
61384117da5SFabian Frederick 			pr_info("CPU%d is up\n", cpu);
614e3920fb4SRafael J. Wysocki 			continue;
615e3920fb4SRafael J. Wysocki 		}
61684117da5SFabian Frederick 		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
617e3920fb4SRafael J. Wysocki 	}
618d0af9eedSSuresh Siddha 
619d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
620d0af9eedSSuresh Siddha 
621e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
6221d64b9cbSRafael J. Wysocki out:
623d221938cSGautham R Shenoy 	cpu_maps_update_done();
624e3920fb4SRafael J. Wysocki }
625e0b582ecSRusty Russell 
626d7268a31SFenghua Yu static int __init alloc_frozen_cpus(void)
627e0b582ecSRusty Russell {
628e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
629e0b582ecSRusty Russell 		return -ENOMEM;
630e0b582ecSRusty Russell 	return 0;
631e0b582ecSRusty Russell }
632e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
63379cfbdfaSSrivatsa S. Bhat 
63479cfbdfaSSrivatsa S. Bhat /*
63579cfbdfaSSrivatsa S. Bhat  * When callbacks for CPU hotplug notifications are being executed, we must
63679cfbdfaSSrivatsa S. Bhat  * ensure that the state of the system with respect to the tasks being frozen
63779cfbdfaSSrivatsa S. Bhat  * or not, as reported by the notification, remains unchanged *throughout the
63879cfbdfaSSrivatsa S. Bhat  * duration* of the execution of the callbacks.
63979cfbdfaSSrivatsa S. Bhat  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
64079cfbdfaSSrivatsa S. Bhat  *
64179cfbdfaSSrivatsa S. Bhat  * This synchronization is implemented by mutually excluding regular CPU
64279cfbdfaSSrivatsa S. Bhat  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
64379cfbdfaSSrivatsa S. Bhat  * Hibernate notifications.
64479cfbdfaSSrivatsa S. Bhat  */
64579cfbdfaSSrivatsa S. Bhat static int
64679cfbdfaSSrivatsa S. Bhat cpu_hotplug_pm_callback(struct notifier_block *nb,
64779cfbdfaSSrivatsa S. Bhat 			unsigned long action, void *ptr)
64879cfbdfaSSrivatsa S. Bhat {
64979cfbdfaSSrivatsa S. Bhat 	switch (action) {
65079cfbdfaSSrivatsa S. Bhat 
65179cfbdfaSSrivatsa S. Bhat 	case PM_SUSPEND_PREPARE:
65279cfbdfaSSrivatsa S. Bhat 	case PM_HIBERNATION_PREPARE:
65316e53dbfSSrivatsa S. Bhat 		cpu_hotplug_disable();
65479cfbdfaSSrivatsa S. Bhat 		break;
65579cfbdfaSSrivatsa S. Bhat 
65679cfbdfaSSrivatsa S. Bhat 	case PM_POST_SUSPEND:
65779cfbdfaSSrivatsa S. Bhat 	case PM_POST_HIBERNATION:
65816e53dbfSSrivatsa S. Bhat 		cpu_hotplug_enable();
65979cfbdfaSSrivatsa S. Bhat 		break;
66079cfbdfaSSrivatsa S. Bhat 
66179cfbdfaSSrivatsa S. Bhat 	default:
66279cfbdfaSSrivatsa S. Bhat 		return NOTIFY_DONE;
66379cfbdfaSSrivatsa S. Bhat 	}
66479cfbdfaSSrivatsa S. Bhat 
66579cfbdfaSSrivatsa S. Bhat 	return NOTIFY_OK;
66679cfbdfaSSrivatsa S. Bhat }
66779cfbdfaSSrivatsa S. Bhat 
66879cfbdfaSSrivatsa S. Bhat 
669d7268a31SFenghua Yu static int __init cpu_hotplug_pm_sync_init(void)
67079cfbdfaSSrivatsa S. Bhat {
6716e32d479SFenghua Yu 	/*
6726e32d479SFenghua Yu 	 * cpu_hotplug_pm_callback has higher priority than x86
6736e32d479SFenghua Yu 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
6746e32d479SFenghua Yu 	 * to disable cpu hotplug to avoid cpu hotplug race.
6756e32d479SFenghua Yu 	 */
67679cfbdfaSSrivatsa S. Bhat 	pm_notifier(cpu_hotplug_pm_callback, 0);
67779cfbdfaSSrivatsa S. Bhat 	return 0;
67879cfbdfaSSrivatsa S. Bhat }
67979cfbdfaSSrivatsa S. Bhat core_initcall(cpu_hotplug_pm_sync_init);
68079cfbdfaSSrivatsa S. Bhat 
681f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
68268f4f1ecSMax Krasnyansky 
683e545a614SManfred Spraul /**
684e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
685e545a614SManfred Spraul  * @cpu: cpu that just started
686e545a614SManfred Spraul  *
687e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
688e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
689e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
690e545a614SManfred Spraul  */
6910db0628dSPaul Gortmaker void notify_cpu_starting(unsigned int cpu)
692e545a614SManfred Spraul {
693e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
694e545a614SManfred Spraul 
695e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
696e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
697e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
698e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
699e9fb7631SAkinobu Mita 	cpu_notify(val, (void *)(long)cpu);
700e545a614SManfred Spraul }
701e545a614SManfred Spraul 
70268f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
703b8d317d1SMike Travis 
704e56b3bc7SLinus Torvalds /*
705e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
706e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
707e56b3bc7SLinus Torvalds  *
708e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
709e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
710e56b3bc7SLinus Torvalds  */
711b8d317d1SMike Travis 
712e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
7134d51985eSMichael Rodriguez #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
714e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
715e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
716e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
717b8d317d1SMike Travis 
718e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
719b8d317d1SMike Travis 
720e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
721e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
722e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
723e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
724e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
725b8d317d1SMike Travis #endif
726b8d317d1SMike Travis };
727e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
7282d3854a3SRusty Russell 
7292d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
7302d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
731b3199c02SRusty Russell 
732b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
733b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
734b3199c02SRusty Russell 	= CPU_BITS_ALL;
735b3199c02SRusty Russell #else
736b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
737b3199c02SRusty Russell #endif
738b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
739b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
740b3199c02SRusty Russell 
741b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
742b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
743b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
744b3199c02SRusty Russell 
745b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
746b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
747b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
748b3199c02SRusty Russell 
749b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
750b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
751b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
7523fa41520SRusty Russell 
7533fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
7543fa41520SRusty Russell {
7553fa41520SRusty Russell 	if (possible)
7563fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
7573fa41520SRusty Russell 	else
7583fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
7593fa41520SRusty Russell }
7603fa41520SRusty Russell 
7613fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
7623fa41520SRusty Russell {
7633fa41520SRusty Russell 	if (present)
7643fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
7653fa41520SRusty Russell 	else
7663fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
7673fa41520SRusty Russell }
7683fa41520SRusty Russell 
7693fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
7703fa41520SRusty Russell {
7716acbfb96SLai Jiangshan 	if (online) {
7723fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
7736acbfb96SLai Jiangshan 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7746acbfb96SLai Jiangshan 	} else {
7753fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
7763fa41520SRusty Russell 	}
7776acbfb96SLai Jiangshan }
7783fa41520SRusty Russell 
7793fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
7803fa41520SRusty Russell {
7813fa41520SRusty Russell 	if (active)
7823fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7833fa41520SRusty Russell 	else
7843fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
7853fa41520SRusty Russell }
7863fa41520SRusty Russell 
7873fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
7883fa41520SRusty Russell {
7893fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
7903fa41520SRusty Russell }
7913fa41520SRusty Russell 
7923fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
7933fa41520SRusty Russell {
7943fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
7953fa41520SRusty Russell }
7963fa41520SRusty Russell 
7973fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
7983fa41520SRusty Russell {
7993fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
8003fa41520SRusty Russell }
801