xref: /linux-6.15/kernel/cpu.c (revision b728ca06)
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;
67a19423b9SGautham R. Shenoy 
68a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
69a19423b9SGautham R. Shenoy 	struct lockdep_map dep_map;
70a19423b9SGautham R. Shenoy #endif
7131950eb6SLinus Torvalds } cpu_hotplug = {
7231950eb6SLinus Torvalds 	.active_writer = NULL,
7331950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
7431950eb6SLinus Torvalds 	.refcount = 0,
75a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
76a19423b9SGautham R. Shenoy 	.dep_map = {.name = "cpu_hotplug.lock" },
77a19423b9SGautham R. Shenoy #endif
7831950eb6SLinus Torvalds };
79d221938cSGautham R Shenoy 
80a19423b9SGautham R. Shenoy /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
81a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
82a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
83a19423b9SGautham R. Shenoy #define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)
84a19423b9SGautham R. Shenoy 
8586ef5c9aSGautham R Shenoy void get_online_cpus(void)
86a9d9baa1SAshok Raj {
87d221938cSGautham R Shenoy 	might_sleep();
88d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
89aa953877SLinus Torvalds 		return;
90a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire_read();
91d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
92d221938cSGautham R Shenoy 	cpu_hotplug.refcount++;
93d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
94d221938cSGautham R Shenoy 
95a9d9baa1SAshok Raj }
9686ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
97a9d9baa1SAshok Raj 
9886ef5c9aSGautham R Shenoy void put_online_cpus(void)
99a9d9baa1SAshok Raj {
100d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
101aa953877SLinus Torvalds 		return;
102d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
103075663d1SSrivatsa S. Bhat 
104075663d1SSrivatsa S. Bhat 	if (WARN_ON(!cpu_hotplug.refcount))
105075663d1SSrivatsa S. Bhat 		cpu_hotplug.refcount++; /* try to fix things up */
106075663d1SSrivatsa S. Bhat 
107d2ba7e2aSOleg Nesterov 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
108d2ba7e2aSOleg Nesterov 		wake_up_process(cpu_hotplug.active_writer);
109d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
110a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
111d221938cSGautham R Shenoy 
112a9d9baa1SAshok Raj }
11386ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
114a9d9baa1SAshok Raj 
115d221938cSGautham R Shenoy /*
116d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
117d221938cSGautham R Shenoy  * refcount goes to zero.
118d221938cSGautham R Shenoy  *
119d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
120d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
121d221938cSGautham R Shenoy  *
122d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
123d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
124d221938cSGautham R Shenoy  *
125d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
126d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
127d221938cSGautham R Shenoy  *   writer.
128d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
129d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
130d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
131d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
132d221938cSGautham R Shenoy  *
133d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
13486ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
135d221938cSGautham R Shenoy  *
136d221938cSGautham R Shenoy  */
137b9d10be7SToshi Kani void cpu_hotplug_begin(void)
138d221938cSGautham R Shenoy {
139d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = current;
140d2ba7e2aSOleg Nesterov 
141a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire();
142d2ba7e2aSOleg Nesterov 	for (;;) {
143d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
144d2ba7e2aSOleg Nesterov 		if (likely(!cpu_hotplug.refcount))
145d2ba7e2aSOleg Nesterov 			break;
146d2ba7e2aSOleg Nesterov 		__set_current_state(TASK_UNINTERRUPTIBLE);
147d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
148d221938cSGautham R Shenoy 		schedule();
149d221938cSGautham R Shenoy 	}
150d221938cSGautham R Shenoy }
151d221938cSGautham R Shenoy 
152b9d10be7SToshi Kani void cpu_hotplug_done(void)
153d221938cSGautham R Shenoy {
154d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
155d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
156a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
157d221938cSGautham R Shenoy }
15879a6cdebSLai Jiangshan 
15916e53dbfSSrivatsa S. Bhat /*
16016e53dbfSSrivatsa S. Bhat  * Wait for currently running CPU hotplug operations to complete (if any) and
16116e53dbfSSrivatsa S. Bhat  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
16216e53dbfSSrivatsa S. Bhat  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
16316e53dbfSSrivatsa S. Bhat  * hotplug path before performing hotplug operations. So acquiring that lock
16416e53dbfSSrivatsa S. Bhat  * guarantees mutual exclusion from any currently running hotplug operations.
16516e53dbfSSrivatsa S. Bhat  */
16616e53dbfSSrivatsa S. Bhat void cpu_hotplug_disable(void)
16716e53dbfSSrivatsa S. Bhat {
16816e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
16916e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 1;
17016e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
17116e53dbfSSrivatsa S. Bhat }
17216e53dbfSSrivatsa S. Bhat 
17316e53dbfSSrivatsa S. Bhat void cpu_hotplug_enable(void)
17416e53dbfSSrivatsa S. Bhat {
17516e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
17616e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 0;
17716e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
17816e53dbfSSrivatsa S. Bhat }
17916e53dbfSSrivatsa S. Bhat 
180b9d10be7SToshi Kani #endif	/* CONFIG_HOTPLUG_CPU */
18179a6cdebSLai Jiangshan 
1821da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
183f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
1841da177e4SLinus Torvalds {
185bd5349cfSNeil Brown 	int ret;
186d221938cSGautham R Shenoy 	cpu_maps_update_begin();
187bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
188d221938cSGautham R Shenoy 	cpu_maps_update_done();
189bd5349cfSNeil Brown 	return ret;
1901da177e4SLinus Torvalds }
19165edc68cSChandra Seetharaman 
19293ae4f97SSrivatsa S. Bhat int __ref __register_cpu_notifier(struct notifier_block *nb)
19393ae4f97SSrivatsa S. Bhat {
19493ae4f97SSrivatsa S. Bhat 	return raw_notifier_chain_register(&cpu_chain, nb);
19593ae4f97SSrivatsa S. Bhat }
19693ae4f97SSrivatsa S. Bhat 
197e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
198e9fb7631SAkinobu Mita 			int *nr_calls)
199e9fb7631SAkinobu Mita {
200e6bde73bSAkinobu Mita 	int ret;
201e6bde73bSAkinobu Mita 
202e6bde73bSAkinobu Mita 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
203e9fb7631SAkinobu Mita 					nr_calls);
204e6bde73bSAkinobu Mita 
205e6bde73bSAkinobu Mita 	return notifier_to_errno(ret);
206e9fb7631SAkinobu Mita }
207e9fb7631SAkinobu Mita 
208e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v)
209e9fb7631SAkinobu Mita {
210e9fb7631SAkinobu Mita 	return __cpu_notify(val, v, -1, NULL);
211e9fb7631SAkinobu Mita }
212e9fb7631SAkinobu Mita 
21300b9b0afSLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
21400b9b0afSLinus Torvalds 
215e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v)
216e9fb7631SAkinobu Mita {
21700b9b0afSLinus Torvalds 	BUG_ON(cpu_notify(val, v));
218e9fb7631SAkinobu Mita }
2191da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
22093ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__register_cpu_notifier);
2211da177e4SLinus Torvalds 
2229647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
2231da177e4SLinus Torvalds {
224d221938cSGautham R Shenoy 	cpu_maps_update_begin();
225bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
226d221938cSGautham R Shenoy 	cpu_maps_update_done();
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
2291da177e4SLinus Torvalds 
23093ae4f97SSrivatsa S. Bhat void __ref __unregister_cpu_notifier(struct notifier_block *nb)
23193ae4f97SSrivatsa S. Bhat {
23293ae4f97SSrivatsa S. Bhat 	raw_notifier_chain_unregister(&cpu_chain, nb);
23393ae4f97SSrivatsa S. Bhat }
23493ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__unregister_cpu_notifier);
23593ae4f97SSrivatsa S. Bhat 
236e4cc2f87SAnton Vorontsov /**
237e4cc2f87SAnton Vorontsov  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
238e4cc2f87SAnton Vorontsov  * @cpu: a CPU id
239e4cc2f87SAnton Vorontsov  *
240e4cc2f87SAnton Vorontsov  * This function walks all processes, finds a valid mm struct for each one and
241e4cc2f87SAnton Vorontsov  * then clears a corresponding bit in mm's cpumask.  While this all sounds
242e4cc2f87SAnton Vorontsov  * trivial, there are various non-obvious corner cases, which this function
243e4cc2f87SAnton Vorontsov  * tries to solve in a safe manner.
244e4cc2f87SAnton Vorontsov  *
245e4cc2f87SAnton Vorontsov  * Also note that the function uses a somewhat relaxed locking scheme, so it may
246e4cc2f87SAnton Vorontsov  * be called only for an already offlined CPU.
247e4cc2f87SAnton Vorontsov  */
248cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu)
249cb79295eSAnton Vorontsov {
250cb79295eSAnton Vorontsov 	struct task_struct *p;
251cb79295eSAnton Vorontsov 
252cb79295eSAnton Vorontsov 	/*
253cb79295eSAnton Vorontsov 	 * This function is called after the cpu is taken down and marked
254cb79295eSAnton Vorontsov 	 * offline, so its not like new tasks will ever get this cpu set in
255cb79295eSAnton Vorontsov 	 * their mm mask. -- Peter Zijlstra
256cb79295eSAnton Vorontsov 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
257cb79295eSAnton Vorontsov 	 * full-fledged tasklist_lock.
258cb79295eSAnton Vorontsov 	 */
259e4cc2f87SAnton Vorontsov 	WARN_ON(cpu_online(cpu));
260cb79295eSAnton Vorontsov 	rcu_read_lock();
261cb79295eSAnton Vorontsov 	for_each_process(p) {
262cb79295eSAnton Vorontsov 		struct task_struct *t;
263cb79295eSAnton Vorontsov 
264e4cc2f87SAnton Vorontsov 		/*
265e4cc2f87SAnton Vorontsov 		 * Main thread might exit, but other threads may still have
266e4cc2f87SAnton Vorontsov 		 * a valid mm. Find one.
267e4cc2f87SAnton Vorontsov 		 */
268cb79295eSAnton Vorontsov 		t = find_lock_task_mm(p);
269cb79295eSAnton Vorontsov 		if (!t)
270cb79295eSAnton Vorontsov 			continue;
271cb79295eSAnton Vorontsov 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
272cb79295eSAnton Vorontsov 		task_unlock(t);
273cb79295eSAnton Vorontsov 	}
274cb79295eSAnton Vorontsov 	rcu_read_unlock();
275cb79295eSAnton Vorontsov }
276cb79295eSAnton Vorontsov 
277*b728ca06SKirill Tkhai static inline void check_for_tasks(int dead_cpu)
2781da177e4SLinus Torvalds {
279*b728ca06SKirill Tkhai 	struct task_struct *g, *p;
2801da177e4SLinus Torvalds 
281*b728ca06SKirill Tkhai 	read_lock_irq(&tasklist_lock);
282*b728ca06SKirill Tkhai 	do_each_thread(g, p) {
283*b728ca06SKirill Tkhai 		if (!p->on_rq)
284*b728ca06SKirill Tkhai 			continue;
285*b728ca06SKirill Tkhai 		/*
286*b728ca06SKirill Tkhai 		 * We do the check with unlocked task_rq(p)->lock.
287*b728ca06SKirill Tkhai 		 * Order the reading to do not warn about a task,
288*b728ca06SKirill Tkhai 		 * which was running on this cpu in the past, and
289*b728ca06SKirill Tkhai 		 * it's just been woken on another cpu.
290*b728ca06SKirill Tkhai 		 */
291*b728ca06SKirill Tkhai 		rmb();
292*b728ca06SKirill Tkhai 		if (task_cpu(p) != dead_cpu)
293*b728ca06SKirill Tkhai 			continue;
294*b728ca06SKirill Tkhai 
29584117da5SFabian Frederick 		pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
296*b728ca06SKirill Tkhai 			p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
297*b728ca06SKirill Tkhai 	} while_each_thread(g, p);
298*b728ca06SKirill Tkhai 	read_unlock_irq(&tasklist_lock);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
301db912f96SAvi Kivity struct take_cpu_down_param {
302db912f96SAvi Kivity 	unsigned long mod;
303db912f96SAvi Kivity 	void *hcpu;
304db912f96SAvi Kivity };
305db912f96SAvi Kivity 
3061da177e4SLinus Torvalds /* Take this CPU down. */
307514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
3081da177e4SLinus Torvalds {
309db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
3101da177e4SLinus Torvalds 	int err;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
3131da177e4SLinus Torvalds 	err = __cpu_disable();
3141da177e4SLinus Torvalds 	if (err < 0)
315f3705136SZwane Mwaikambo 		return err;
316f3705136SZwane Mwaikambo 
317e9fb7631SAkinobu Mita 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
31814e568e7SThomas Gleixner 	/* Park the stopper thread */
31914e568e7SThomas Gleixner 	kthread_park(current);
320f3705136SZwane Mwaikambo 	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
324514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
3251da177e4SLinus Torvalds {
326e7407dccSHeiko Carstens 	int err, nr_calls = 0;
327e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
3288bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
329db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
330db912f96SAvi Kivity 		.mod = mod,
331db912f96SAvi Kivity 		.hcpu = hcpu,
332db912f96SAvi Kivity 	};
3331da177e4SLinus Torvalds 
334e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
335e3920fb4SRafael J. Wysocki 		return -EBUSY;
3361da177e4SLinus Torvalds 
337e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
338e3920fb4SRafael J. Wysocki 		return -EINVAL;
3391da177e4SLinus Torvalds 
340d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3414d51985eSMichael Rodriguez 
342e9fb7631SAkinobu Mita 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
343e6bde73bSAkinobu Mita 	if (err) {
344a0d8cdb6SAkinobu Mita 		nr_calls--;
345e9fb7631SAkinobu Mita 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
34684117da5SFabian Frederick 		pr_warn("%s: attempt to take down CPU %u failed\n",
347af1f16d0SHarvey Harrison 			__func__, cpu);
348baaca49fSGautham R Shenoy 		goto out_release;
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 
3516acce3efSPeter Zijlstra 	/*
3526acce3efSPeter Zijlstra 	 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
3536acce3efSPeter Zijlstra 	 * and RCU users of this state to go away such that all new such users
3546acce3efSPeter Zijlstra 	 * will observe it.
3556acce3efSPeter Zijlstra 	 *
3566acce3efSPeter Zijlstra 	 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
3576acce3efSPeter Zijlstra 	 * not imply sync_sched(), so explicitly call both.
358106dd5afSMichael wang 	 *
359106dd5afSMichael wang 	 * Do sync before park smpboot threads to take care the rcu boost case.
3606acce3efSPeter Zijlstra 	 */
3616acce3efSPeter Zijlstra #ifdef CONFIG_PREEMPT
3626acce3efSPeter Zijlstra 	synchronize_sched();
3636acce3efSPeter Zijlstra #endif
3646acce3efSPeter Zijlstra 	synchronize_rcu();
3656acce3efSPeter Zijlstra 
366106dd5afSMichael wang 	smpboot_park_threads(cpu);
367106dd5afSMichael wang 
3686acce3efSPeter Zijlstra 	/*
3696acce3efSPeter Zijlstra 	 * So now all preempt/rcu users must observe !cpu_active().
3706acce3efSPeter Zijlstra 	 */
3716acce3efSPeter Zijlstra 
372e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
37304321587SRusty Russell 	if (err) {
3741da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
375f97f8f06SThomas Gleixner 		smpboot_unpark_threads(cpu);
376e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
3776a1bdc1bSOleg Nesterov 		goto out_release;
3781da177e4SLinus Torvalds 	}
37904321587SRusty Russell 	BUG_ON(cpu_online(cpu));
3801da177e4SLinus Torvalds 
38148c5ccaeSPeter Zijlstra 	/*
38248c5ccaeSPeter Zijlstra 	 * The migration_call() CPU_DYING callback will have removed all
38348c5ccaeSPeter Zijlstra 	 * runnable tasks from the cpu, there's only the idle task left now
38448c5ccaeSPeter Zijlstra 	 * that the migration thread is done doing the stop_machine thing.
38551a96c77SPeter Zijlstra 	 *
38651a96c77SPeter Zijlstra 	 * Wait for the stop thread to go away.
38748c5ccaeSPeter Zijlstra 	 */
38851a96c77SPeter Zijlstra 	while (!idle_cpu(cpu))
38951a96c77SPeter Zijlstra 		cpu_relax();
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	/* This actually kills the CPU. */
3921da177e4SLinus Torvalds 	__cpu_die(cpu);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
395e9fb7631SAkinobu Mita 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	check_for_tasks(cpu);
3981da177e4SLinus Torvalds 
399baaca49fSGautham R Shenoy out_release:
400d221938cSGautham R Shenoy 	cpu_hotplug_done();
401e9fb7631SAkinobu Mita 	if (!err)
402e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
403e3920fb4SRafael J. Wysocki 	return err;
404e3920fb4SRafael J. Wysocki }
405e3920fb4SRafael J. Wysocki 
406514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
407e3920fb4SRafael J. Wysocki {
4089ea09af3SHeiko Carstens 	int err;
409e3920fb4SRafael J. Wysocki 
410d221938cSGautham R Shenoy 	cpu_maps_update_begin();
411e761b772SMax Krasnyansky 
412e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
413e3920fb4SRafael J. Wysocki 		err = -EBUSY;
414e761b772SMax Krasnyansky 		goto out;
415e761b772SMax Krasnyansky 	}
416e761b772SMax Krasnyansky 
4178bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
418e3920fb4SRafael J. Wysocki 
419e761b772SMax Krasnyansky out:
420d221938cSGautham R Shenoy 	cpu_maps_update_done();
4211da177e4SLinus Torvalds 	return err;
4221da177e4SLinus Torvalds }
423b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
4241da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
4251da177e4SLinus Torvalds 
426e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
4270db0628dSPaul Gortmaker static int _cpu_up(unsigned int cpu, int tasks_frozen)
4281da177e4SLinus Torvalds {
429baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
4301da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
4318bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
4323bb5d2eeSSuresh Siddha 	struct task_struct *idle;
4331da177e4SLinus Torvalds 
434d221938cSGautham R Shenoy 	cpu_hotplug_begin();
43538498a67SThomas Gleixner 
4365e5041f3SYasuaki Ishimatsu 	if (cpu_online(cpu) || !cpu_present(cpu)) {
4375e5041f3SYasuaki Ishimatsu 		ret = -EINVAL;
4385e5041f3SYasuaki Ishimatsu 		goto out;
4395e5041f3SYasuaki Ishimatsu 	}
4405e5041f3SYasuaki Ishimatsu 
4413bb5d2eeSSuresh Siddha 	idle = idle_thread_get(cpu);
4423bb5d2eeSSuresh Siddha 	if (IS_ERR(idle)) {
4433bb5d2eeSSuresh Siddha 		ret = PTR_ERR(idle);
44438498a67SThomas Gleixner 		goto out;
4453bb5d2eeSSuresh Siddha 	}
44638498a67SThomas Gleixner 
447f97f8f06SThomas Gleixner 	ret = smpboot_create_threads(cpu);
448f97f8f06SThomas Gleixner 	if (ret)
449f97f8f06SThomas Gleixner 		goto out;
450f97f8f06SThomas Gleixner 
451e9fb7631SAkinobu Mita 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
452e6bde73bSAkinobu Mita 	if (ret) {
453a0d8cdb6SAkinobu Mita 		nr_calls--;
45484117da5SFabian Frederick 		pr_warn("%s: attempt to bring up CPU %u failed\n",
455af1f16d0SHarvey Harrison 			__func__, cpu);
4561da177e4SLinus Torvalds 		goto out_notify;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
4603bb5d2eeSSuresh Siddha 	ret = __cpu_up(cpu, idle);
4611da177e4SLinus Torvalds 	if (ret != 0)
4621da177e4SLinus Torvalds 		goto out_notify;
4636978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
4641da177e4SLinus Torvalds 
465f97f8f06SThomas Gleixner 	/* Wake the per cpu threads */
466f97f8f06SThomas Gleixner 	smpboot_unpark_threads(cpu);
467f97f8f06SThomas Gleixner 
4681da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
469e9fb7631SAkinobu Mita 	cpu_notify(CPU_ONLINE | mod, hcpu);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds out_notify:
4721da177e4SLinus Torvalds 	if (ret != 0)
473e9fb7631SAkinobu Mita 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
47438498a67SThomas Gleixner out:
475d221938cSGautham R Shenoy 	cpu_hotplug_done();
476e3920fb4SRafael J. Wysocki 
4771da177e4SLinus Torvalds 	return ret;
4781da177e4SLinus Torvalds }
479e3920fb4SRafael J. Wysocki 
4800db0628dSPaul Gortmaker int cpu_up(unsigned int cpu)
481e3920fb4SRafael J. Wysocki {
482e3920fb4SRafael J. Wysocki 	int err = 0;
483cf23422bSminskey guo 
484e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
48584117da5SFabian Frederick 		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
48684117da5SFabian Frederick 		       cpu);
48787d5e023SChen Gong #if defined(CONFIG_IA64)
48884117da5SFabian Frederick 		pr_err("please check additional_cpus= boot parameter\n");
48973e753a5SKAMEZAWA Hiroyuki #endif
49073e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
49173e753a5SKAMEZAWA Hiroyuki 	}
492e3920fb4SRafael J. Wysocki 
49301b0f197SToshi Kani 	err = try_online_node(cpu_to_node(cpu));
494cf23422bSminskey guo 	if (err)
495cf23422bSminskey guo 		return err;
496cf23422bSminskey guo 
497d221938cSGautham R Shenoy 	cpu_maps_update_begin();
498e761b772SMax Krasnyansky 
499e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
500e3920fb4SRafael J. Wysocki 		err = -EBUSY;
501e761b772SMax Krasnyansky 		goto out;
502e761b772SMax Krasnyansky 	}
503e761b772SMax Krasnyansky 
5048bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
505e3920fb4SRafael J. Wysocki 
506e761b772SMax Krasnyansky out:
507d221938cSGautham R Shenoy 	cpu_maps_update_done();
508e3920fb4SRafael J. Wysocki 	return err;
509e3920fb4SRafael J. Wysocki }
510a513f6baSPaul E. McKenney EXPORT_SYMBOL_GPL(cpu_up);
511e3920fb4SRafael J. Wysocki 
512f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
513e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
514e3920fb4SRafael J. Wysocki 
515e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
516e3920fb4SRafael J. Wysocki {
517e9a5f426SRafael J. Wysocki 	int cpu, first_cpu, error = 0;
518e3920fb4SRafael J. Wysocki 
519d221938cSGautham R Shenoy 	cpu_maps_update_begin();
520e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
5219ee349adSXiaotian Feng 	/*
5229ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
523e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
524e3920fb4SRafael J. Wysocki 	 */
525e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5266ad4c188SPeter Zijlstra 
52784117da5SFabian Frederick 	pr_info("Disabling non-boot CPUs ...\n");
528e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
529e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
530e3920fb4SRafael J. Wysocki 			continue;
531bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
5328bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
533bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
534feae3203SMike Travis 		if (!error)
535e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
536feae3203SMike Travis 		else {
53784117da5SFabian Frederick 			pr_err("Error taking CPU%d down: %d\n", cpu, error);
538e3920fb4SRafael J. Wysocki 			break;
539e3920fb4SRafael J. Wysocki 		}
540e3920fb4SRafael J. Wysocki 	}
54186886e55SJoseph Cihula 
542e3920fb4SRafael J. Wysocki 	if (!error) {
543e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
544e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
545e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
546e3920fb4SRafael J. Wysocki 	} else {
54784117da5SFabian Frederick 		pr_err("Non-boot CPUs are not disabled\n");
548e3920fb4SRafael J. Wysocki 	}
549d221938cSGautham R Shenoy 	cpu_maps_update_done();
550e3920fb4SRafael J. Wysocki 	return error;
551e3920fb4SRafael J. Wysocki }
552e3920fb4SRafael J. Wysocki 
553d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
554d0af9eedSSuresh Siddha {
555d0af9eedSSuresh Siddha }
556d0af9eedSSuresh Siddha 
557d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
558d0af9eedSSuresh Siddha {
559d0af9eedSSuresh Siddha }
560d0af9eedSSuresh Siddha 
561fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
562e3920fb4SRafael J. Wysocki {
563e3920fb4SRafael J. Wysocki 	int cpu, error;
564e3920fb4SRafael J. Wysocki 
565e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
566d221938cSGautham R Shenoy 	cpu_maps_update_begin();
567e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
568e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
5691d64b9cbSRafael J. Wysocki 		goto out;
570e3920fb4SRafael J. Wysocki 
57184117da5SFabian Frederick 	pr_info("Enabling non-boot CPUs ...\n");
572d0af9eedSSuresh Siddha 
573d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
574d0af9eedSSuresh Siddha 
575e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
576bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
5778bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
578bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
579e3920fb4SRafael J. Wysocki 		if (!error) {
58084117da5SFabian Frederick 			pr_info("CPU%d is up\n", cpu);
581e3920fb4SRafael J. Wysocki 			continue;
582e3920fb4SRafael J. Wysocki 		}
58384117da5SFabian Frederick 		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
584e3920fb4SRafael J. Wysocki 	}
585d0af9eedSSuresh Siddha 
586d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
587d0af9eedSSuresh Siddha 
588e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5891d64b9cbSRafael J. Wysocki out:
590d221938cSGautham R Shenoy 	cpu_maps_update_done();
591e3920fb4SRafael J. Wysocki }
592e0b582ecSRusty Russell 
593d7268a31SFenghua Yu static int __init alloc_frozen_cpus(void)
594e0b582ecSRusty Russell {
595e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
596e0b582ecSRusty Russell 		return -ENOMEM;
597e0b582ecSRusty Russell 	return 0;
598e0b582ecSRusty Russell }
599e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
60079cfbdfaSSrivatsa S. Bhat 
60179cfbdfaSSrivatsa S. Bhat /*
60279cfbdfaSSrivatsa S. Bhat  * When callbacks for CPU hotplug notifications are being executed, we must
60379cfbdfaSSrivatsa S. Bhat  * ensure that the state of the system with respect to the tasks being frozen
60479cfbdfaSSrivatsa S. Bhat  * or not, as reported by the notification, remains unchanged *throughout the
60579cfbdfaSSrivatsa S. Bhat  * duration* of the execution of the callbacks.
60679cfbdfaSSrivatsa S. Bhat  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
60779cfbdfaSSrivatsa S. Bhat  *
60879cfbdfaSSrivatsa S. Bhat  * This synchronization is implemented by mutually excluding regular CPU
60979cfbdfaSSrivatsa S. Bhat  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
61079cfbdfaSSrivatsa S. Bhat  * Hibernate notifications.
61179cfbdfaSSrivatsa S. Bhat  */
61279cfbdfaSSrivatsa S. Bhat static int
61379cfbdfaSSrivatsa S. Bhat cpu_hotplug_pm_callback(struct notifier_block *nb,
61479cfbdfaSSrivatsa S. Bhat 			unsigned long action, void *ptr)
61579cfbdfaSSrivatsa S. Bhat {
61679cfbdfaSSrivatsa S. Bhat 	switch (action) {
61779cfbdfaSSrivatsa S. Bhat 
61879cfbdfaSSrivatsa S. Bhat 	case PM_SUSPEND_PREPARE:
61979cfbdfaSSrivatsa S. Bhat 	case PM_HIBERNATION_PREPARE:
62016e53dbfSSrivatsa S. Bhat 		cpu_hotplug_disable();
62179cfbdfaSSrivatsa S. Bhat 		break;
62279cfbdfaSSrivatsa S. Bhat 
62379cfbdfaSSrivatsa S. Bhat 	case PM_POST_SUSPEND:
62479cfbdfaSSrivatsa S. Bhat 	case PM_POST_HIBERNATION:
62516e53dbfSSrivatsa S. Bhat 		cpu_hotplug_enable();
62679cfbdfaSSrivatsa S. Bhat 		break;
62779cfbdfaSSrivatsa S. Bhat 
62879cfbdfaSSrivatsa S. Bhat 	default:
62979cfbdfaSSrivatsa S. Bhat 		return NOTIFY_DONE;
63079cfbdfaSSrivatsa S. Bhat 	}
63179cfbdfaSSrivatsa S. Bhat 
63279cfbdfaSSrivatsa S. Bhat 	return NOTIFY_OK;
63379cfbdfaSSrivatsa S. Bhat }
63479cfbdfaSSrivatsa S. Bhat 
63579cfbdfaSSrivatsa S. Bhat 
636d7268a31SFenghua Yu static int __init cpu_hotplug_pm_sync_init(void)
63779cfbdfaSSrivatsa S. Bhat {
6386e32d479SFenghua Yu 	/*
6396e32d479SFenghua Yu 	 * cpu_hotplug_pm_callback has higher priority than x86
6406e32d479SFenghua Yu 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
6416e32d479SFenghua Yu 	 * to disable cpu hotplug to avoid cpu hotplug race.
6426e32d479SFenghua Yu 	 */
64379cfbdfaSSrivatsa S. Bhat 	pm_notifier(cpu_hotplug_pm_callback, 0);
64479cfbdfaSSrivatsa S. Bhat 	return 0;
64579cfbdfaSSrivatsa S. Bhat }
64679cfbdfaSSrivatsa S. Bhat core_initcall(cpu_hotplug_pm_sync_init);
64779cfbdfaSSrivatsa S. Bhat 
648f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
64968f4f1ecSMax Krasnyansky 
650e545a614SManfred Spraul /**
651e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
652e545a614SManfred Spraul  * @cpu: cpu that just started
653e545a614SManfred Spraul  *
654e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
655e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
656e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
657e545a614SManfred Spraul  */
6580db0628dSPaul Gortmaker void notify_cpu_starting(unsigned int cpu)
659e545a614SManfred Spraul {
660e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
661e545a614SManfred Spraul 
662e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
663e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
664e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
665e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
666e9fb7631SAkinobu Mita 	cpu_notify(val, (void *)(long)cpu);
667e545a614SManfred Spraul }
668e545a614SManfred Spraul 
66968f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
670b8d317d1SMike Travis 
671e56b3bc7SLinus Torvalds /*
672e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
673e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
674e56b3bc7SLinus Torvalds  *
675e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
676e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
677e56b3bc7SLinus Torvalds  */
678b8d317d1SMike Travis 
679e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
6804d51985eSMichael Rodriguez #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
681e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
682e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
683e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
684b8d317d1SMike Travis 
685e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
686b8d317d1SMike Travis 
687e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
688e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
689e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
690e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
691e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
692b8d317d1SMike Travis #endif
693b8d317d1SMike Travis };
694e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
6952d3854a3SRusty Russell 
6962d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
6972d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
698b3199c02SRusty Russell 
699b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
700b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
701b3199c02SRusty Russell 	= CPU_BITS_ALL;
702b3199c02SRusty Russell #else
703b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
704b3199c02SRusty Russell #endif
705b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
706b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
707b3199c02SRusty Russell 
708b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
709b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
710b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
711b3199c02SRusty Russell 
712b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
713b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
714b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
715b3199c02SRusty Russell 
716b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
717b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
718b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
7193fa41520SRusty Russell 
7203fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
7213fa41520SRusty Russell {
7223fa41520SRusty Russell 	if (possible)
7233fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
7243fa41520SRusty Russell 	else
7253fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
7263fa41520SRusty Russell }
7273fa41520SRusty Russell 
7283fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
7293fa41520SRusty Russell {
7303fa41520SRusty Russell 	if (present)
7313fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
7323fa41520SRusty Russell 	else
7333fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
7343fa41520SRusty Russell }
7353fa41520SRusty Russell 
7363fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
7373fa41520SRusty Russell {
7386acbfb96SLai Jiangshan 	if (online) {
7393fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
7406acbfb96SLai Jiangshan 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7416acbfb96SLai Jiangshan 	} else {
7423fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
7433fa41520SRusty Russell 	}
7446acbfb96SLai Jiangshan }
7453fa41520SRusty Russell 
7463fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
7473fa41520SRusty Russell {
7483fa41520SRusty Russell 	if (active)
7493fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7503fa41520SRusty Russell 	else
7513fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
7523fa41520SRusty Russell }
7533fa41520SRusty Russell 
7543fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
7553fa41520SRusty Russell {
7563fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
7573fa41520SRusty Russell }
7583fa41520SRusty Russell 
7593fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
7603fa41520SRusty Russell {
7613fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
7623fa41520SRusty Russell }
7633fa41520SRusty Russell 
7643fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
7653fa41520SRusty Russell {
7663fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
7673fa41520SRusty Russell }
768