xref: /linux-6.15/kernel/cpu.c (revision 927da9df)
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>
23345527b1SPreeti U Murthy #include <linux/tick.h>
24bb3632c6STodd E Brandt #include <trace/events/power.h>
251da177e4SLinus Torvalds 
2638498a67SThomas Gleixner #include "smpboot.h"
2738498a67SThomas Gleixner 
2898a79d6aSRusty Russell #ifdef CONFIG_SMP
29b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */
30aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock);
311da177e4SLinus Torvalds 
3279a6cdebSLai Jiangshan /*
3393ae4f97SSrivatsa S. Bhat  * The following two APIs (cpu_maps_update_begin/done) must be used when
3493ae4f97SSrivatsa S. Bhat  * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
3593ae4f97SSrivatsa S. Bhat  * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
3693ae4f97SSrivatsa S. Bhat  * hotplug callback (un)registration performed using __register_cpu_notifier()
3793ae4f97SSrivatsa S. Bhat  * or __unregister_cpu_notifier().
3879a6cdebSLai Jiangshan  */
3979a6cdebSLai Jiangshan void cpu_maps_update_begin(void)
4079a6cdebSLai Jiangshan {
4179a6cdebSLai Jiangshan 	mutex_lock(&cpu_add_remove_lock);
4279a6cdebSLai Jiangshan }
4393ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_begin);
4479a6cdebSLai Jiangshan 
4579a6cdebSLai Jiangshan void cpu_maps_update_done(void)
4679a6cdebSLai Jiangshan {
4779a6cdebSLai Jiangshan 	mutex_unlock(&cpu_add_remove_lock);
4879a6cdebSLai Jiangshan }
4993ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_done);
5079a6cdebSLai Jiangshan 
515c113fbeSDaniel J Blueman static RAW_NOTIFIER_HEAD(cpu_chain);
521da177e4SLinus Torvalds 
53e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
54e3920fb4SRafael J. Wysocki  * Should always be manipulated under cpu_add_remove_lock
55e3920fb4SRafael J. Wysocki  */
56e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled;
57e3920fb4SRafael J. Wysocki 
5879a6cdebSLai Jiangshan #ifdef CONFIG_HOTPLUG_CPU
5979a6cdebSLai Jiangshan 
60d221938cSGautham R Shenoy static struct {
61d221938cSGautham R Shenoy 	struct task_struct *active_writer;
6287af9e7fSDavid Hildenbrand 	/* wait queue to wake up the active_writer */
6387af9e7fSDavid Hildenbrand 	wait_queue_head_t wq;
6487af9e7fSDavid Hildenbrand 	/* verifies that no writer will get active while readers are active */
6587af9e7fSDavid Hildenbrand 	struct mutex lock;
66d221938cSGautham R Shenoy 	/*
67d221938cSGautham R Shenoy 	 * Also blocks the new readers during
68d221938cSGautham R Shenoy 	 * an ongoing cpu hotplug operation.
69d221938cSGautham R Shenoy 	 */
7087af9e7fSDavid Hildenbrand 	atomic_t refcount;
71a19423b9SGautham R. Shenoy 
72a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
73a19423b9SGautham R. Shenoy 	struct lockdep_map dep_map;
74a19423b9SGautham R. Shenoy #endif
7531950eb6SLinus Torvalds } cpu_hotplug = {
7631950eb6SLinus Torvalds 	.active_writer = NULL,
7787af9e7fSDavid Hildenbrand 	.wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
7831950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
79a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
80a19423b9SGautham R. Shenoy 	.dep_map = {.name = "cpu_hotplug.lock" },
81a19423b9SGautham R. Shenoy #endif
8231950eb6SLinus Torvalds };
83d221938cSGautham R Shenoy 
84a19423b9SGautham R. Shenoy /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
85a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
86dd56af42SPaul E. McKenney #define cpuhp_lock_acquire_tryread() \
87dd56af42SPaul E. McKenney 				  lock_map_acquire_tryread(&cpu_hotplug.dep_map)
88a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
89a19423b9SGautham R. Shenoy #define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)
90a19423b9SGautham R. Shenoy 
9162db99f4SPaul E. McKenney 
9286ef5c9aSGautham R Shenoy void get_online_cpus(void)
93a9d9baa1SAshok Raj {
94d221938cSGautham R Shenoy 	might_sleep();
95d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
96aa953877SLinus Torvalds 		return;
97a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire_read();
98d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
9987af9e7fSDavid Hildenbrand 	atomic_inc(&cpu_hotplug.refcount);
100d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
101a9d9baa1SAshok Raj }
10286ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
103a9d9baa1SAshok Raj 
104dd56af42SPaul E. McKenney bool try_get_online_cpus(void)
105dd56af42SPaul E. McKenney {
106dd56af42SPaul E. McKenney 	if (cpu_hotplug.active_writer == current)
107dd56af42SPaul E. McKenney 		return true;
108dd56af42SPaul E. McKenney 	if (!mutex_trylock(&cpu_hotplug.lock))
109dd56af42SPaul E. McKenney 		return false;
110dd56af42SPaul E. McKenney 	cpuhp_lock_acquire_tryread();
11187af9e7fSDavid Hildenbrand 	atomic_inc(&cpu_hotplug.refcount);
112dd56af42SPaul E. McKenney 	mutex_unlock(&cpu_hotplug.lock);
113dd56af42SPaul E. McKenney 	return true;
114dd56af42SPaul E. McKenney }
115dd56af42SPaul E. McKenney EXPORT_SYMBOL_GPL(try_get_online_cpus);
116dd56af42SPaul E. McKenney 
11786ef5c9aSGautham R Shenoy void put_online_cpus(void)
118a9d9baa1SAshok Raj {
11987af9e7fSDavid Hildenbrand 	int refcount;
12087af9e7fSDavid Hildenbrand 
121d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
122aa953877SLinus Torvalds 		return;
123075663d1SSrivatsa S. Bhat 
12487af9e7fSDavid Hildenbrand 	refcount = atomic_dec_return(&cpu_hotplug.refcount);
12587af9e7fSDavid Hildenbrand 	if (WARN_ON(refcount < 0)) /* try to fix things up */
12687af9e7fSDavid Hildenbrand 		atomic_inc(&cpu_hotplug.refcount);
127075663d1SSrivatsa S. Bhat 
12887af9e7fSDavid Hildenbrand 	if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
12987af9e7fSDavid Hildenbrand 		wake_up(&cpu_hotplug.wq);
13087af9e7fSDavid Hildenbrand 
131a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
132d221938cSGautham R Shenoy 
133a9d9baa1SAshok Raj }
13486ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
135a9d9baa1SAshok Raj 
136d221938cSGautham R Shenoy /*
137d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
138d221938cSGautham R Shenoy  * refcount goes to zero.
139d221938cSGautham R Shenoy  *
140d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
141d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
142d221938cSGautham R Shenoy  *
143d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
144d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
145d221938cSGautham R Shenoy  *
146d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
147d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
148d221938cSGautham R Shenoy  *   writer.
149d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
150d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
151d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
152d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
153d221938cSGautham R Shenoy  *
154d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
15586ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
156d221938cSGautham R Shenoy  *
157d221938cSGautham R Shenoy  */
158b9d10be7SToshi Kani void cpu_hotplug_begin(void)
159d221938cSGautham R Shenoy {
16087af9e7fSDavid Hildenbrand 	DEFINE_WAIT(wait);
161d2ba7e2aSOleg Nesterov 
16287af9e7fSDavid Hildenbrand 	cpu_hotplug.active_writer = current;
163a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire();
16487af9e7fSDavid Hildenbrand 
165d2ba7e2aSOleg Nesterov 	for (;;) {
166d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
16787af9e7fSDavid Hildenbrand 		prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
16887af9e7fSDavid Hildenbrand 		if (likely(!atomic_read(&cpu_hotplug.refcount)))
169d2ba7e2aSOleg Nesterov 				break;
170d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
171d221938cSGautham R Shenoy 		schedule();
172d221938cSGautham R Shenoy 	}
17387af9e7fSDavid Hildenbrand 	finish_wait(&cpu_hotplug.wq, &wait);
174d221938cSGautham R Shenoy }
175d221938cSGautham R Shenoy 
176b9d10be7SToshi Kani void cpu_hotplug_done(void)
177d221938cSGautham R Shenoy {
178d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
179d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
180a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
181d221938cSGautham R Shenoy }
18279a6cdebSLai Jiangshan 
18316e53dbfSSrivatsa S. Bhat /*
18416e53dbfSSrivatsa S. Bhat  * Wait for currently running CPU hotplug operations to complete (if any) and
18516e53dbfSSrivatsa S. Bhat  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
18616e53dbfSSrivatsa S. Bhat  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
18716e53dbfSSrivatsa S. Bhat  * hotplug path before performing hotplug operations. So acquiring that lock
18816e53dbfSSrivatsa S. Bhat  * guarantees mutual exclusion from any currently running hotplug operations.
18916e53dbfSSrivatsa S. Bhat  */
19016e53dbfSSrivatsa S. Bhat void cpu_hotplug_disable(void)
19116e53dbfSSrivatsa S. Bhat {
19216e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
19316e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 1;
19416e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
19516e53dbfSSrivatsa S. Bhat }
19616e53dbfSSrivatsa S. Bhat 
19716e53dbfSSrivatsa S. Bhat void cpu_hotplug_enable(void)
19816e53dbfSSrivatsa S. Bhat {
19916e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
20016e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 0;
20116e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
20216e53dbfSSrivatsa S. Bhat }
20316e53dbfSSrivatsa S. Bhat 
204b9d10be7SToshi Kani #endif	/* CONFIG_HOTPLUG_CPU */
20579a6cdebSLai Jiangshan 
2061da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
207f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
2081da177e4SLinus Torvalds {
209bd5349cfSNeil Brown 	int ret;
210d221938cSGautham R Shenoy 	cpu_maps_update_begin();
211bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
212d221938cSGautham R Shenoy 	cpu_maps_update_done();
213bd5349cfSNeil Brown 	return ret;
2141da177e4SLinus Torvalds }
21565edc68cSChandra Seetharaman 
21693ae4f97SSrivatsa S. Bhat int __ref __register_cpu_notifier(struct notifier_block *nb)
21793ae4f97SSrivatsa S. Bhat {
21893ae4f97SSrivatsa S. Bhat 	return raw_notifier_chain_register(&cpu_chain, nb);
21993ae4f97SSrivatsa S. Bhat }
22093ae4f97SSrivatsa S. Bhat 
221e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
222e9fb7631SAkinobu Mita 			int *nr_calls)
223e9fb7631SAkinobu Mita {
224e6bde73bSAkinobu Mita 	int ret;
225e6bde73bSAkinobu Mita 
226e6bde73bSAkinobu Mita 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
227e9fb7631SAkinobu Mita 					nr_calls);
228e6bde73bSAkinobu Mita 
229e6bde73bSAkinobu Mita 	return notifier_to_errno(ret);
230e9fb7631SAkinobu Mita }
231e9fb7631SAkinobu Mita 
232e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v)
233e9fb7631SAkinobu Mita {
234e9fb7631SAkinobu Mita 	return __cpu_notify(val, v, -1, NULL);
235e9fb7631SAkinobu Mita }
236e9fb7631SAkinobu Mita 
23700b9b0afSLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
23800b9b0afSLinus Torvalds 
239e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v)
240e9fb7631SAkinobu Mita {
24100b9b0afSLinus Torvalds 	BUG_ON(cpu_notify(val, v));
242e9fb7631SAkinobu Mita }
2431da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
24493ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__register_cpu_notifier);
2451da177e4SLinus Torvalds 
2469647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
2471da177e4SLinus Torvalds {
248d221938cSGautham R Shenoy 	cpu_maps_update_begin();
249bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
250d221938cSGautham R Shenoy 	cpu_maps_update_done();
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
2531da177e4SLinus Torvalds 
25493ae4f97SSrivatsa S. Bhat void __ref __unregister_cpu_notifier(struct notifier_block *nb)
25593ae4f97SSrivatsa S. Bhat {
25693ae4f97SSrivatsa S. Bhat 	raw_notifier_chain_unregister(&cpu_chain, nb);
25793ae4f97SSrivatsa S. Bhat }
25893ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__unregister_cpu_notifier);
25993ae4f97SSrivatsa S. Bhat 
260e4cc2f87SAnton Vorontsov /**
261e4cc2f87SAnton Vorontsov  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
262e4cc2f87SAnton Vorontsov  * @cpu: a CPU id
263e4cc2f87SAnton Vorontsov  *
264e4cc2f87SAnton Vorontsov  * This function walks all processes, finds a valid mm struct for each one and
265e4cc2f87SAnton Vorontsov  * then clears a corresponding bit in mm's cpumask.  While this all sounds
266e4cc2f87SAnton Vorontsov  * trivial, there are various non-obvious corner cases, which this function
267e4cc2f87SAnton Vorontsov  * tries to solve in a safe manner.
268e4cc2f87SAnton Vorontsov  *
269e4cc2f87SAnton Vorontsov  * Also note that the function uses a somewhat relaxed locking scheme, so it may
270e4cc2f87SAnton Vorontsov  * be called only for an already offlined CPU.
271e4cc2f87SAnton Vorontsov  */
272cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu)
273cb79295eSAnton Vorontsov {
274cb79295eSAnton Vorontsov 	struct task_struct *p;
275cb79295eSAnton Vorontsov 
276cb79295eSAnton Vorontsov 	/*
277cb79295eSAnton Vorontsov 	 * This function is called after the cpu is taken down and marked
278cb79295eSAnton Vorontsov 	 * offline, so its not like new tasks will ever get this cpu set in
279cb79295eSAnton Vorontsov 	 * their mm mask. -- Peter Zijlstra
280cb79295eSAnton Vorontsov 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
281cb79295eSAnton Vorontsov 	 * full-fledged tasklist_lock.
282cb79295eSAnton Vorontsov 	 */
283e4cc2f87SAnton Vorontsov 	WARN_ON(cpu_online(cpu));
284cb79295eSAnton Vorontsov 	rcu_read_lock();
285cb79295eSAnton Vorontsov 	for_each_process(p) {
286cb79295eSAnton Vorontsov 		struct task_struct *t;
287cb79295eSAnton Vorontsov 
288e4cc2f87SAnton Vorontsov 		/*
289e4cc2f87SAnton Vorontsov 		 * Main thread might exit, but other threads may still have
290e4cc2f87SAnton Vorontsov 		 * a valid mm. Find one.
291e4cc2f87SAnton Vorontsov 		 */
292cb79295eSAnton Vorontsov 		t = find_lock_task_mm(p);
293cb79295eSAnton Vorontsov 		if (!t)
294cb79295eSAnton Vorontsov 			continue;
295cb79295eSAnton Vorontsov 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
296cb79295eSAnton Vorontsov 		task_unlock(t);
297cb79295eSAnton Vorontsov 	}
298cb79295eSAnton Vorontsov 	rcu_read_unlock();
299cb79295eSAnton Vorontsov }
300cb79295eSAnton Vorontsov 
301b728ca06SKirill Tkhai static inline void check_for_tasks(int dead_cpu)
3021da177e4SLinus Torvalds {
303b728ca06SKirill Tkhai 	struct task_struct *g, *p;
3041da177e4SLinus Torvalds 
305b728ca06SKirill Tkhai 	read_lock_irq(&tasklist_lock);
306b728ca06SKirill Tkhai 	do_each_thread(g, p) {
307b728ca06SKirill Tkhai 		if (!p->on_rq)
308b728ca06SKirill Tkhai 			continue;
309b728ca06SKirill Tkhai 		/*
310b728ca06SKirill Tkhai 		 * We do the check with unlocked task_rq(p)->lock.
311b728ca06SKirill Tkhai 		 * Order the reading to do not warn about a task,
312b728ca06SKirill Tkhai 		 * which was running on this cpu in the past, and
313b728ca06SKirill Tkhai 		 * it's just been woken on another cpu.
314b728ca06SKirill Tkhai 		 */
315b728ca06SKirill Tkhai 		rmb();
316b728ca06SKirill Tkhai 		if (task_cpu(p) != dead_cpu)
317b728ca06SKirill Tkhai 			continue;
318b728ca06SKirill Tkhai 
31984117da5SFabian Frederick 		pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
320b728ca06SKirill Tkhai 			p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
321b728ca06SKirill Tkhai 	} while_each_thread(g, p);
322b728ca06SKirill Tkhai 	read_unlock_irq(&tasklist_lock);
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325db912f96SAvi Kivity struct take_cpu_down_param {
326db912f96SAvi Kivity 	unsigned long mod;
327db912f96SAvi Kivity 	void *hcpu;
328db912f96SAvi Kivity };
329db912f96SAvi Kivity 
3301da177e4SLinus Torvalds /* Take this CPU down. */
331514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
3321da177e4SLinus Torvalds {
333db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
3341da177e4SLinus Torvalds 	int err;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
3371da177e4SLinus Torvalds 	err = __cpu_disable();
3381da177e4SLinus Torvalds 	if (err < 0)
339f3705136SZwane Mwaikambo 		return err;
340f3705136SZwane Mwaikambo 
341e9fb7631SAkinobu Mita 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
34252c063d1SThomas Gleixner 	/* Give up timekeeping duties */
34352c063d1SThomas Gleixner 	tick_handover_do_timer();
34414e568e7SThomas Gleixner 	/* Park the stopper thread */
34514e568e7SThomas Gleixner 	kthread_park(current);
346f3705136SZwane Mwaikambo 	return 0;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
349e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
350514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
3511da177e4SLinus Torvalds {
352e7407dccSHeiko Carstens 	int err, nr_calls = 0;
353e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
3548bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
355db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
356db912f96SAvi Kivity 		.mod = mod,
357db912f96SAvi Kivity 		.hcpu = hcpu,
358db912f96SAvi Kivity 	};
3591da177e4SLinus Torvalds 
360e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
361e3920fb4SRafael J. Wysocki 		return -EBUSY;
3621da177e4SLinus Torvalds 
363e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
364e3920fb4SRafael J. Wysocki 		return -EINVAL;
3651da177e4SLinus Torvalds 
366d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3674d51985eSMichael Rodriguez 
368e9fb7631SAkinobu Mita 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
369e6bde73bSAkinobu Mita 	if (err) {
370a0d8cdb6SAkinobu Mita 		nr_calls--;
371e9fb7631SAkinobu Mita 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
37284117da5SFabian Frederick 		pr_warn("%s: attempt to take down CPU %u failed\n",
373af1f16d0SHarvey Harrison 			__func__, cpu);
374baaca49fSGautham R Shenoy 		goto out_release;
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 
3776acce3efSPeter Zijlstra 	/*
3786acce3efSPeter Zijlstra 	 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
3796acce3efSPeter Zijlstra 	 * and RCU users of this state to go away such that all new such users
3806acce3efSPeter Zijlstra 	 * will observe it.
3816acce3efSPeter Zijlstra 	 *
3826acce3efSPeter Zijlstra 	 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
3836acce3efSPeter Zijlstra 	 * not imply sync_sched(), so explicitly call both.
384106dd5afSMichael wang 	 *
385106dd5afSMichael wang 	 * Do sync before park smpboot threads to take care the rcu boost case.
3866acce3efSPeter Zijlstra 	 */
3876acce3efSPeter Zijlstra #ifdef CONFIG_PREEMPT
3886acce3efSPeter Zijlstra 	synchronize_sched();
3896acce3efSPeter Zijlstra #endif
3906acce3efSPeter Zijlstra 	synchronize_rcu();
3916acce3efSPeter Zijlstra 
392106dd5afSMichael wang 	smpboot_park_threads(cpu);
393106dd5afSMichael wang 
3946acce3efSPeter Zijlstra 	/*
3956acce3efSPeter Zijlstra 	 * So now all preempt/rcu users must observe !cpu_active().
3966acce3efSPeter Zijlstra 	 */
3976acce3efSPeter Zijlstra 
398e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
39904321587SRusty Russell 	if (err) {
4001da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
401e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
4026a1bdc1bSOleg Nesterov 		goto out_release;
4031da177e4SLinus Torvalds 	}
40404321587SRusty Russell 	BUG_ON(cpu_online(cpu));
4051da177e4SLinus Torvalds 
40648c5ccaeSPeter Zijlstra 	/*
40748c5ccaeSPeter Zijlstra 	 * The migration_call() CPU_DYING callback will have removed all
40848c5ccaeSPeter Zijlstra 	 * runnable tasks from the cpu, there's only the idle task left now
40948c5ccaeSPeter Zijlstra 	 * that the migration thread is done doing the stop_machine thing.
41051a96c77SPeter Zijlstra 	 *
41151a96c77SPeter Zijlstra 	 * Wait for the stop thread to go away.
41248c5ccaeSPeter Zijlstra 	 */
413528a25b0SPaul E. McKenney 	while (!per_cpu(cpu_dead_idle, cpu))
41451a96c77SPeter Zijlstra 		cpu_relax();
415528a25b0SPaul E. McKenney 	smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */
416528a25b0SPaul E. McKenney 	per_cpu(cpu_dead_idle, cpu) = false;
4171da177e4SLinus Torvalds 
418345527b1SPreeti U Murthy 	hotplug_cpu__broadcast_tick_pull(cpu);
4191da177e4SLinus Torvalds 	/* This actually kills the CPU. */
4201da177e4SLinus Torvalds 	__cpu_die(cpu);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
423a49b116dSThomas Gleixner 	tick_cleanup_dead_cpu(cpu);
424e9fb7631SAkinobu Mita 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	check_for_tasks(cpu);
4271da177e4SLinus Torvalds 
428baaca49fSGautham R Shenoy out_release:
429d221938cSGautham R Shenoy 	cpu_hotplug_done();
430e9fb7631SAkinobu Mita 	if (!err)
431e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
432e3920fb4SRafael J. Wysocki 	return err;
433e3920fb4SRafael J. Wysocki }
434e3920fb4SRafael J. Wysocki 
435514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
436e3920fb4SRafael J. Wysocki {
4379ea09af3SHeiko Carstens 	int err;
438e3920fb4SRafael J. Wysocki 
439d221938cSGautham R Shenoy 	cpu_maps_update_begin();
440e761b772SMax Krasnyansky 
441e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
442e3920fb4SRafael J. Wysocki 		err = -EBUSY;
443e761b772SMax Krasnyansky 		goto out;
444e761b772SMax Krasnyansky 	}
445e761b772SMax Krasnyansky 
4468bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
447e3920fb4SRafael J. Wysocki 
448e761b772SMax Krasnyansky out:
449d221938cSGautham R Shenoy 	cpu_maps_update_done();
4501da177e4SLinus Torvalds 	return err;
4511da177e4SLinus Torvalds }
452b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
4531da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
4541da177e4SLinus Torvalds 
45500df35f9SPaul E. McKenney /*
45600df35f9SPaul E. McKenney  * Unpark per-CPU smpboot kthreads at CPU-online time.
45700df35f9SPaul E. McKenney  */
45800df35f9SPaul E. McKenney static int smpboot_thread_call(struct notifier_block *nfb,
45900df35f9SPaul E. McKenney 			       unsigned long action, void *hcpu)
46000df35f9SPaul E. McKenney {
46100df35f9SPaul E. McKenney 	int cpu = (long)hcpu;
46200df35f9SPaul E. McKenney 
46300df35f9SPaul E. McKenney 	switch (action & ~CPU_TASKS_FROZEN) {
46400df35f9SPaul E. McKenney 
46564eaf974SPaul E. McKenney 	case CPU_DOWN_FAILED:
46600df35f9SPaul E. McKenney 	case CPU_ONLINE:
46700df35f9SPaul E. McKenney 		smpboot_unpark_threads(cpu);
46800df35f9SPaul E. McKenney 		break;
46900df35f9SPaul E. McKenney 
47000df35f9SPaul E. McKenney 	default:
47100df35f9SPaul E. McKenney 		break;
47200df35f9SPaul E. McKenney 	}
47300df35f9SPaul E. McKenney 
47400df35f9SPaul E. McKenney 	return NOTIFY_OK;
47500df35f9SPaul E. McKenney }
47600df35f9SPaul E. McKenney 
47700df35f9SPaul E. McKenney static struct notifier_block smpboot_thread_notifier = {
47800df35f9SPaul E. McKenney 	.notifier_call = smpboot_thread_call,
47900df35f9SPaul E. McKenney 	.priority = CPU_PRI_SMPBOOT,
48000df35f9SPaul E. McKenney };
48100df35f9SPaul E. McKenney 
482*927da9dfSPaul Gortmaker void smpboot_thread_init(void)
48300df35f9SPaul E. McKenney {
48400df35f9SPaul E. McKenney 	register_cpu_notifier(&smpboot_thread_notifier);
48500df35f9SPaul E. McKenney }
48600df35f9SPaul E. McKenney 
487e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
4880db0628dSPaul Gortmaker static int _cpu_up(unsigned int cpu, int tasks_frozen)
4891da177e4SLinus Torvalds {
490baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
4911da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
4928bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
4933bb5d2eeSSuresh Siddha 	struct task_struct *idle;
4941da177e4SLinus Torvalds 
495d221938cSGautham R Shenoy 	cpu_hotplug_begin();
49638498a67SThomas Gleixner 
4975e5041f3SYasuaki Ishimatsu 	if (cpu_online(cpu) || !cpu_present(cpu)) {
4985e5041f3SYasuaki Ishimatsu 		ret = -EINVAL;
4995e5041f3SYasuaki Ishimatsu 		goto out;
5005e5041f3SYasuaki Ishimatsu 	}
5015e5041f3SYasuaki Ishimatsu 
5023bb5d2eeSSuresh Siddha 	idle = idle_thread_get(cpu);
5033bb5d2eeSSuresh Siddha 	if (IS_ERR(idle)) {
5043bb5d2eeSSuresh Siddha 		ret = PTR_ERR(idle);
50538498a67SThomas Gleixner 		goto out;
5063bb5d2eeSSuresh Siddha 	}
50738498a67SThomas Gleixner 
508f97f8f06SThomas Gleixner 	ret = smpboot_create_threads(cpu);
509f97f8f06SThomas Gleixner 	if (ret)
510f97f8f06SThomas Gleixner 		goto out;
511f97f8f06SThomas Gleixner 
512e9fb7631SAkinobu Mita 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
513e6bde73bSAkinobu Mita 	if (ret) {
514a0d8cdb6SAkinobu Mita 		nr_calls--;
51584117da5SFabian Frederick 		pr_warn("%s: attempt to bring up CPU %u failed\n",
516af1f16d0SHarvey Harrison 			__func__, cpu);
5171da177e4SLinus Torvalds 		goto out_notify;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
5213bb5d2eeSSuresh Siddha 	ret = __cpu_up(cpu, idle);
5221da177e4SLinus Torvalds 	if (ret != 0)
5231da177e4SLinus Torvalds 		goto out_notify;
5246978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
527e9fb7631SAkinobu Mita 	cpu_notify(CPU_ONLINE | mod, hcpu);
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds out_notify:
5301da177e4SLinus Torvalds 	if (ret != 0)
531e9fb7631SAkinobu Mita 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
53238498a67SThomas Gleixner out:
533d221938cSGautham R Shenoy 	cpu_hotplug_done();
534e3920fb4SRafael J. Wysocki 
5351da177e4SLinus Torvalds 	return ret;
5361da177e4SLinus Torvalds }
537e3920fb4SRafael J. Wysocki 
5380db0628dSPaul Gortmaker int cpu_up(unsigned int cpu)
539e3920fb4SRafael J. Wysocki {
540e3920fb4SRafael J. Wysocki 	int err = 0;
541cf23422bSminskey guo 
542e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
54384117da5SFabian Frederick 		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
54484117da5SFabian Frederick 		       cpu);
54587d5e023SChen Gong #if defined(CONFIG_IA64)
54684117da5SFabian Frederick 		pr_err("please check additional_cpus= boot parameter\n");
54773e753a5SKAMEZAWA Hiroyuki #endif
54873e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
54973e753a5SKAMEZAWA Hiroyuki 	}
550e3920fb4SRafael J. Wysocki 
55101b0f197SToshi Kani 	err = try_online_node(cpu_to_node(cpu));
552cf23422bSminskey guo 	if (err)
553cf23422bSminskey guo 		return err;
554cf23422bSminskey guo 
555d221938cSGautham R Shenoy 	cpu_maps_update_begin();
556e761b772SMax Krasnyansky 
557e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
558e3920fb4SRafael J. Wysocki 		err = -EBUSY;
559e761b772SMax Krasnyansky 		goto out;
560e761b772SMax Krasnyansky 	}
561e761b772SMax Krasnyansky 
5628bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
563e3920fb4SRafael J. Wysocki 
564e761b772SMax Krasnyansky out:
565d221938cSGautham R Shenoy 	cpu_maps_update_done();
566e3920fb4SRafael J. Wysocki 	return err;
567e3920fb4SRafael J. Wysocki }
568a513f6baSPaul E. McKenney EXPORT_SYMBOL_GPL(cpu_up);
569e3920fb4SRafael J. Wysocki 
570f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
571e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
572e3920fb4SRafael J. Wysocki 
573e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
574e3920fb4SRafael J. Wysocki {
575e9a5f426SRafael J. Wysocki 	int cpu, first_cpu, error = 0;
576e3920fb4SRafael J. Wysocki 
577d221938cSGautham R Shenoy 	cpu_maps_update_begin();
578e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
5799ee349adSXiaotian Feng 	/*
5809ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
581e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
582e3920fb4SRafael J. Wysocki 	 */
583e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5846ad4c188SPeter Zijlstra 
58584117da5SFabian Frederick 	pr_info("Disabling non-boot CPUs ...\n");
586e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
587e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
588e3920fb4SRafael J. Wysocki 			continue;
589bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
5908bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
591bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
592feae3203SMike Travis 		if (!error)
593e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
594feae3203SMike Travis 		else {
59584117da5SFabian Frederick 			pr_err("Error taking CPU%d down: %d\n", cpu, error);
596e3920fb4SRafael J. Wysocki 			break;
597e3920fb4SRafael J. Wysocki 		}
598e3920fb4SRafael J. Wysocki 	}
59986886e55SJoseph Cihula 
600e3920fb4SRafael J. Wysocki 	if (!error) {
601e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
602e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
603e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
604e3920fb4SRafael J. Wysocki 	} else {
60584117da5SFabian Frederick 		pr_err("Non-boot CPUs are not disabled\n");
606e3920fb4SRafael J. Wysocki 	}
607d221938cSGautham R Shenoy 	cpu_maps_update_done();
608e3920fb4SRafael J. Wysocki 	return error;
609e3920fb4SRafael J. Wysocki }
610e3920fb4SRafael J. Wysocki 
611d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
612d0af9eedSSuresh Siddha {
613d0af9eedSSuresh Siddha }
614d0af9eedSSuresh Siddha 
615d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
616d0af9eedSSuresh Siddha {
617d0af9eedSSuresh Siddha }
618d0af9eedSSuresh Siddha 
619fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
620e3920fb4SRafael J. Wysocki {
621e3920fb4SRafael J. Wysocki 	int cpu, error;
622e3920fb4SRafael J. Wysocki 
623e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
624d221938cSGautham R Shenoy 	cpu_maps_update_begin();
625e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
626e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
6271d64b9cbSRafael J. Wysocki 		goto out;
628e3920fb4SRafael J. Wysocki 
62984117da5SFabian Frederick 	pr_info("Enabling non-boot CPUs ...\n");
630d0af9eedSSuresh Siddha 
631d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
632d0af9eedSSuresh Siddha 
633e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
634bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
6358bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
636bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
637e3920fb4SRafael J. Wysocki 		if (!error) {
63884117da5SFabian Frederick 			pr_info("CPU%d is up\n", cpu);
639e3920fb4SRafael J. Wysocki 			continue;
640e3920fb4SRafael J. Wysocki 		}
64184117da5SFabian Frederick 		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
642e3920fb4SRafael J. Wysocki 	}
643d0af9eedSSuresh Siddha 
644d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
645d0af9eedSSuresh Siddha 
646e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
6471d64b9cbSRafael J. Wysocki out:
648d221938cSGautham R Shenoy 	cpu_maps_update_done();
649e3920fb4SRafael J. Wysocki }
650e0b582ecSRusty Russell 
651d7268a31SFenghua Yu static int __init alloc_frozen_cpus(void)
652e0b582ecSRusty Russell {
653e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
654e0b582ecSRusty Russell 		return -ENOMEM;
655e0b582ecSRusty Russell 	return 0;
656e0b582ecSRusty Russell }
657e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
65879cfbdfaSSrivatsa S. Bhat 
65979cfbdfaSSrivatsa S. Bhat /*
66079cfbdfaSSrivatsa S. Bhat  * When callbacks for CPU hotplug notifications are being executed, we must
66179cfbdfaSSrivatsa S. Bhat  * ensure that the state of the system with respect to the tasks being frozen
66279cfbdfaSSrivatsa S. Bhat  * or not, as reported by the notification, remains unchanged *throughout the
66379cfbdfaSSrivatsa S. Bhat  * duration* of the execution of the callbacks.
66479cfbdfaSSrivatsa S. Bhat  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
66579cfbdfaSSrivatsa S. Bhat  *
66679cfbdfaSSrivatsa S. Bhat  * This synchronization is implemented by mutually excluding regular CPU
66779cfbdfaSSrivatsa S. Bhat  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
66879cfbdfaSSrivatsa S. Bhat  * Hibernate notifications.
66979cfbdfaSSrivatsa S. Bhat  */
67079cfbdfaSSrivatsa S. Bhat static int
67179cfbdfaSSrivatsa S. Bhat cpu_hotplug_pm_callback(struct notifier_block *nb,
67279cfbdfaSSrivatsa S. Bhat 			unsigned long action, void *ptr)
67379cfbdfaSSrivatsa S. Bhat {
67479cfbdfaSSrivatsa S. Bhat 	switch (action) {
67579cfbdfaSSrivatsa S. Bhat 
67679cfbdfaSSrivatsa S. Bhat 	case PM_SUSPEND_PREPARE:
67779cfbdfaSSrivatsa S. Bhat 	case PM_HIBERNATION_PREPARE:
67816e53dbfSSrivatsa S. Bhat 		cpu_hotplug_disable();
67979cfbdfaSSrivatsa S. Bhat 		break;
68079cfbdfaSSrivatsa S. Bhat 
68179cfbdfaSSrivatsa S. Bhat 	case PM_POST_SUSPEND:
68279cfbdfaSSrivatsa S. Bhat 	case PM_POST_HIBERNATION:
68316e53dbfSSrivatsa S. Bhat 		cpu_hotplug_enable();
68479cfbdfaSSrivatsa S. Bhat 		break;
68579cfbdfaSSrivatsa S. Bhat 
68679cfbdfaSSrivatsa S. Bhat 	default:
68779cfbdfaSSrivatsa S. Bhat 		return NOTIFY_DONE;
68879cfbdfaSSrivatsa S. Bhat 	}
68979cfbdfaSSrivatsa S. Bhat 
69079cfbdfaSSrivatsa S. Bhat 	return NOTIFY_OK;
69179cfbdfaSSrivatsa S. Bhat }
69279cfbdfaSSrivatsa S. Bhat 
69379cfbdfaSSrivatsa S. Bhat 
694d7268a31SFenghua Yu static int __init cpu_hotplug_pm_sync_init(void)
69579cfbdfaSSrivatsa S. Bhat {
6966e32d479SFenghua Yu 	/*
6976e32d479SFenghua Yu 	 * cpu_hotplug_pm_callback has higher priority than x86
6986e32d479SFenghua Yu 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
6996e32d479SFenghua Yu 	 * to disable cpu hotplug to avoid cpu hotplug race.
7006e32d479SFenghua Yu 	 */
70179cfbdfaSSrivatsa S. Bhat 	pm_notifier(cpu_hotplug_pm_callback, 0);
70279cfbdfaSSrivatsa S. Bhat 	return 0;
70379cfbdfaSSrivatsa S. Bhat }
70479cfbdfaSSrivatsa S. Bhat core_initcall(cpu_hotplug_pm_sync_init);
70579cfbdfaSSrivatsa S. Bhat 
706f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
70768f4f1ecSMax Krasnyansky 
708e545a614SManfred Spraul /**
709e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
710e545a614SManfred Spraul  * @cpu: cpu that just started
711e545a614SManfred Spraul  *
712e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
713e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
714e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
715e545a614SManfred Spraul  */
7160db0628dSPaul Gortmaker void notify_cpu_starting(unsigned int cpu)
717e545a614SManfred Spraul {
718e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
719e545a614SManfred Spraul 
720e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
721e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
722e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
723e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
724e9fb7631SAkinobu Mita 	cpu_notify(val, (void *)(long)cpu);
725e545a614SManfred Spraul }
726e545a614SManfred Spraul 
72768f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
728b8d317d1SMike Travis 
729e56b3bc7SLinus Torvalds /*
730e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
731e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
732e56b3bc7SLinus Torvalds  *
733e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
734e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
735e56b3bc7SLinus Torvalds  */
736b8d317d1SMike Travis 
737e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
7384d51985eSMichael Rodriguez #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
739e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
740e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
741e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
742b8d317d1SMike Travis 
743e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
744b8d317d1SMike Travis 
745e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
746e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
747e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
748e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
749e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
750b8d317d1SMike Travis #endif
751b8d317d1SMike Travis };
752e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
7532d3854a3SRusty Russell 
7542d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
7552d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
756b3199c02SRusty Russell 
757b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
758b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
759b3199c02SRusty Russell 	= CPU_BITS_ALL;
760b3199c02SRusty Russell #else
761b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
762b3199c02SRusty Russell #endif
763b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
764b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
765b3199c02SRusty Russell 
766b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
767b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
768b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
769b3199c02SRusty Russell 
770b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
771b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
772b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
773b3199c02SRusty Russell 
774b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
775b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
776b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
7773fa41520SRusty Russell 
7783fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
7793fa41520SRusty Russell {
7803fa41520SRusty Russell 	if (possible)
7813fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
7823fa41520SRusty Russell 	else
7833fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
7843fa41520SRusty Russell }
7853fa41520SRusty Russell 
7863fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
7873fa41520SRusty Russell {
7883fa41520SRusty Russell 	if (present)
7893fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
7903fa41520SRusty Russell 	else
7913fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
7923fa41520SRusty Russell }
7933fa41520SRusty Russell 
7943fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
7953fa41520SRusty Russell {
7966acbfb96SLai Jiangshan 	if (online) {
7973fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
7986acbfb96SLai Jiangshan 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7996acbfb96SLai Jiangshan 	} else {
8003fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
8013fa41520SRusty Russell 	}
8026acbfb96SLai Jiangshan }
8033fa41520SRusty Russell 
8043fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
8053fa41520SRusty Russell {
8063fa41520SRusty Russell 	if (active)
8073fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
8083fa41520SRusty Russell 	else
8093fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
8103fa41520SRusty Russell }
8113fa41520SRusty Russell 
8123fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
8133fa41520SRusty Russell {
8143fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
8153fa41520SRusty Russell }
8163fa41520SRusty Russell 
8173fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
8183fa41520SRusty Russell {
8193fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
8203fa41520SRusty Russell }
8213fa41520SRusty Russell 
8223fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
8233fa41520SRusty Russell {
8243fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
8253fa41520SRusty Russell }
826