xref: /linux-6.15/kernel/cpu.c (revision 6acbfb96)
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>
231da177e4SLinus Torvalds 
2438498a67SThomas Gleixner #include "smpboot.h"
2538498a67SThomas Gleixner 
2698a79d6aSRusty Russell #ifdef CONFIG_SMP
27b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */
28aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock);
291da177e4SLinus Torvalds 
3079a6cdebSLai Jiangshan /*
3193ae4f97SSrivatsa S. Bhat  * The following two APIs (cpu_maps_update_begin/done) must be used when
3293ae4f97SSrivatsa S. Bhat  * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
3393ae4f97SSrivatsa S. Bhat  * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
3493ae4f97SSrivatsa S. Bhat  * hotplug callback (un)registration performed using __register_cpu_notifier()
3593ae4f97SSrivatsa S. Bhat  * or __unregister_cpu_notifier().
3679a6cdebSLai Jiangshan  */
3779a6cdebSLai Jiangshan void cpu_maps_update_begin(void)
3879a6cdebSLai Jiangshan {
3979a6cdebSLai Jiangshan 	mutex_lock(&cpu_add_remove_lock);
4079a6cdebSLai Jiangshan }
4193ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_begin);
4279a6cdebSLai Jiangshan 
4379a6cdebSLai Jiangshan void cpu_maps_update_done(void)
4479a6cdebSLai Jiangshan {
4579a6cdebSLai Jiangshan 	mutex_unlock(&cpu_add_remove_lock);
4679a6cdebSLai Jiangshan }
4793ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(cpu_notifier_register_done);
4879a6cdebSLai Jiangshan 
495c113fbeSDaniel J Blueman static RAW_NOTIFIER_HEAD(cpu_chain);
501da177e4SLinus Torvalds 
51e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
52e3920fb4SRafael J. Wysocki  * Should always be manipulated under cpu_add_remove_lock
53e3920fb4SRafael J. Wysocki  */
54e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled;
55e3920fb4SRafael J. Wysocki 
5679a6cdebSLai Jiangshan #ifdef CONFIG_HOTPLUG_CPU
5779a6cdebSLai Jiangshan 
58d221938cSGautham R Shenoy static struct {
59d221938cSGautham R Shenoy 	struct task_struct *active_writer;
60d221938cSGautham R Shenoy 	struct mutex lock; /* Synchronizes accesses to refcount, */
61d221938cSGautham R Shenoy 	/*
62d221938cSGautham R Shenoy 	 * Also blocks the new readers during
63d221938cSGautham R Shenoy 	 * an ongoing cpu hotplug operation.
64d221938cSGautham R Shenoy 	 */
65d221938cSGautham R Shenoy 	int refcount;
66a19423b9SGautham R. Shenoy 
67a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
68a19423b9SGautham R. Shenoy 	struct lockdep_map dep_map;
69a19423b9SGautham R. Shenoy #endif
7031950eb6SLinus Torvalds } cpu_hotplug = {
7131950eb6SLinus Torvalds 	.active_writer = NULL,
7231950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
7331950eb6SLinus Torvalds 	.refcount = 0,
74a19423b9SGautham R. Shenoy #ifdef CONFIG_DEBUG_LOCK_ALLOC
75a19423b9SGautham R. Shenoy 	.dep_map = {.name = "cpu_hotplug.lock" },
76a19423b9SGautham R. Shenoy #endif
7731950eb6SLinus Torvalds };
78d221938cSGautham R Shenoy 
79a19423b9SGautham R. Shenoy /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
80a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
81a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
82a19423b9SGautham R. Shenoy #define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)
83a19423b9SGautham R. Shenoy 
8486ef5c9aSGautham R Shenoy void get_online_cpus(void)
85a9d9baa1SAshok Raj {
86d221938cSGautham R Shenoy 	might_sleep();
87d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
88aa953877SLinus Torvalds 		return;
89a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire_read();
90d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
91d221938cSGautham R Shenoy 	cpu_hotplug.refcount++;
92d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
93d221938cSGautham R Shenoy 
94a9d9baa1SAshok Raj }
9586ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
96a9d9baa1SAshok Raj 
9786ef5c9aSGautham R Shenoy void put_online_cpus(void)
98a9d9baa1SAshok Raj {
99d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
100aa953877SLinus Torvalds 		return;
101d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
102075663d1SSrivatsa S. Bhat 
103075663d1SSrivatsa S. Bhat 	if (WARN_ON(!cpu_hotplug.refcount))
104075663d1SSrivatsa S. Bhat 		cpu_hotplug.refcount++; /* try to fix things up */
105075663d1SSrivatsa S. Bhat 
106d2ba7e2aSOleg Nesterov 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
107d2ba7e2aSOleg Nesterov 		wake_up_process(cpu_hotplug.active_writer);
108d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
109a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
110d221938cSGautham R Shenoy 
111a9d9baa1SAshok Raj }
11286ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
113a9d9baa1SAshok Raj 
114d221938cSGautham R Shenoy /*
115d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
116d221938cSGautham R Shenoy  * refcount goes to zero.
117d221938cSGautham R Shenoy  *
118d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
119d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
120d221938cSGautham R Shenoy  *
121d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
122d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
123d221938cSGautham R Shenoy  *
124d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
125d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
126d221938cSGautham R Shenoy  *   writer.
127d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
128d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
129d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
130d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
131d221938cSGautham R Shenoy  *
132d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
13386ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
134d221938cSGautham R Shenoy  *
135d221938cSGautham R Shenoy  */
136b9d10be7SToshi Kani void cpu_hotplug_begin(void)
137d221938cSGautham R Shenoy {
138d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = current;
139d2ba7e2aSOleg Nesterov 
140a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire();
141d2ba7e2aSOleg Nesterov 	for (;;) {
142d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
143d2ba7e2aSOleg Nesterov 		if (likely(!cpu_hotplug.refcount))
144d2ba7e2aSOleg Nesterov 			break;
145d2ba7e2aSOleg Nesterov 		__set_current_state(TASK_UNINTERRUPTIBLE);
146d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
147d221938cSGautham R Shenoy 		schedule();
148d221938cSGautham R Shenoy 	}
149d221938cSGautham R Shenoy }
150d221938cSGautham R Shenoy 
151b9d10be7SToshi Kani void cpu_hotplug_done(void)
152d221938cSGautham R Shenoy {
153d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
154d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
155a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
156d221938cSGautham R Shenoy }
15779a6cdebSLai Jiangshan 
15816e53dbfSSrivatsa S. Bhat /*
15916e53dbfSSrivatsa S. Bhat  * Wait for currently running CPU hotplug operations to complete (if any) and
16016e53dbfSSrivatsa S. Bhat  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
16116e53dbfSSrivatsa S. Bhat  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
16216e53dbfSSrivatsa S. Bhat  * hotplug path before performing hotplug operations. So acquiring that lock
16316e53dbfSSrivatsa S. Bhat  * guarantees mutual exclusion from any currently running hotplug operations.
16416e53dbfSSrivatsa S. Bhat  */
16516e53dbfSSrivatsa S. Bhat void cpu_hotplug_disable(void)
16616e53dbfSSrivatsa S. Bhat {
16716e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
16816e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 1;
16916e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
17016e53dbfSSrivatsa S. Bhat }
17116e53dbfSSrivatsa S. Bhat 
17216e53dbfSSrivatsa S. Bhat void cpu_hotplug_enable(void)
17316e53dbfSSrivatsa S. Bhat {
17416e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
17516e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 0;
17616e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
17716e53dbfSSrivatsa S. Bhat }
17816e53dbfSSrivatsa S. Bhat 
179b9d10be7SToshi Kani #endif	/* CONFIG_HOTPLUG_CPU */
18079a6cdebSLai Jiangshan 
1811da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
182f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
1831da177e4SLinus Torvalds {
184bd5349cfSNeil Brown 	int ret;
185d221938cSGautham R Shenoy 	cpu_maps_update_begin();
186bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
187d221938cSGautham R Shenoy 	cpu_maps_update_done();
188bd5349cfSNeil Brown 	return ret;
1891da177e4SLinus Torvalds }
19065edc68cSChandra Seetharaman 
19193ae4f97SSrivatsa S. Bhat int __ref __register_cpu_notifier(struct notifier_block *nb)
19293ae4f97SSrivatsa S. Bhat {
19393ae4f97SSrivatsa S. Bhat 	return raw_notifier_chain_register(&cpu_chain, nb);
19493ae4f97SSrivatsa S. Bhat }
19593ae4f97SSrivatsa S. Bhat 
196e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
197e9fb7631SAkinobu Mita 			int *nr_calls)
198e9fb7631SAkinobu Mita {
199e6bde73bSAkinobu Mita 	int ret;
200e6bde73bSAkinobu Mita 
201e6bde73bSAkinobu Mita 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
202e9fb7631SAkinobu Mita 					nr_calls);
203e6bde73bSAkinobu Mita 
204e6bde73bSAkinobu Mita 	return notifier_to_errno(ret);
205e9fb7631SAkinobu Mita }
206e9fb7631SAkinobu Mita 
207e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v)
208e9fb7631SAkinobu Mita {
209e9fb7631SAkinobu Mita 	return __cpu_notify(val, v, -1, NULL);
210e9fb7631SAkinobu Mita }
211e9fb7631SAkinobu Mita 
21200b9b0afSLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
21300b9b0afSLinus Torvalds 
214e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v)
215e9fb7631SAkinobu Mita {
21600b9b0afSLinus Torvalds 	BUG_ON(cpu_notify(val, v));
217e9fb7631SAkinobu Mita }
2181da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
21993ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__register_cpu_notifier);
2201da177e4SLinus Torvalds 
2219647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
2221da177e4SLinus Torvalds {
223d221938cSGautham R Shenoy 	cpu_maps_update_begin();
224bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
225d221938cSGautham R Shenoy 	cpu_maps_update_done();
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
2281da177e4SLinus Torvalds 
22993ae4f97SSrivatsa S. Bhat void __ref __unregister_cpu_notifier(struct notifier_block *nb)
23093ae4f97SSrivatsa S. Bhat {
23193ae4f97SSrivatsa S. Bhat 	raw_notifier_chain_unregister(&cpu_chain, nb);
23293ae4f97SSrivatsa S. Bhat }
23393ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__unregister_cpu_notifier);
23493ae4f97SSrivatsa S. Bhat 
235e4cc2f87SAnton Vorontsov /**
236e4cc2f87SAnton Vorontsov  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
237e4cc2f87SAnton Vorontsov  * @cpu: a CPU id
238e4cc2f87SAnton Vorontsov  *
239e4cc2f87SAnton Vorontsov  * This function walks all processes, finds a valid mm struct for each one and
240e4cc2f87SAnton Vorontsov  * then clears a corresponding bit in mm's cpumask.  While this all sounds
241e4cc2f87SAnton Vorontsov  * trivial, there are various non-obvious corner cases, which this function
242e4cc2f87SAnton Vorontsov  * tries to solve in a safe manner.
243e4cc2f87SAnton Vorontsov  *
244e4cc2f87SAnton Vorontsov  * Also note that the function uses a somewhat relaxed locking scheme, so it may
245e4cc2f87SAnton Vorontsov  * be called only for an already offlined CPU.
246e4cc2f87SAnton Vorontsov  */
247cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu)
248cb79295eSAnton Vorontsov {
249cb79295eSAnton Vorontsov 	struct task_struct *p;
250cb79295eSAnton Vorontsov 
251cb79295eSAnton Vorontsov 	/*
252cb79295eSAnton Vorontsov 	 * This function is called after the cpu is taken down and marked
253cb79295eSAnton Vorontsov 	 * offline, so its not like new tasks will ever get this cpu set in
254cb79295eSAnton Vorontsov 	 * their mm mask. -- Peter Zijlstra
255cb79295eSAnton Vorontsov 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
256cb79295eSAnton Vorontsov 	 * full-fledged tasklist_lock.
257cb79295eSAnton Vorontsov 	 */
258e4cc2f87SAnton Vorontsov 	WARN_ON(cpu_online(cpu));
259cb79295eSAnton Vorontsov 	rcu_read_lock();
260cb79295eSAnton Vorontsov 	for_each_process(p) {
261cb79295eSAnton Vorontsov 		struct task_struct *t;
262cb79295eSAnton Vorontsov 
263e4cc2f87SAnton Vorontsov 		/*
264e4cc2f87SAnton Vorontsov 		 * Main thread might exit, but other threads may still have
265e4cc2f87SAnton Vorontsov 		 * a valid mm. Find one.
266e4cc2f87SAnton Vorontsov 		 */
267cb79295eSAnton Vorontsov 		t = find_lock_task_mm(p);
268cb79295eSAnton Vorontsov 		if (!t)
269cb79295eSAnton Vorontsov 			continue;
270cb79295eSAnton Vorontsov 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
271cb79295eSAnton Vorontsov 		task_unlock(t);
272cb79295eSAnton Vorontsov 	}
273cb79295eSAnton Vorontsov 	rcu_read_unlock();
274cb79295eSAnton Vorontsov }
275cb79295eSAnton Vorontsov 
2761da177e4SLinus Torvalds static inline void check_for_tasks(int cpu)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	struct task_struct *p;
2796fac4829SFrederic Weisbecker 	cputime_t utime, stime;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
2821da177e4SLinus Torvalds 	for_each_process(p) {
2836fac4829SFrederic Weisbecker 		task_cputime(p, &utime, &stime);
28411854247SPeter Zijlstra 		if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
2856fac4829SFrederic Weisbecker 		    (utime || stime))
2869d3cfc4cSFrans Pop 			printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d "
2879d3cfc4cSFrans Pop 				"(state = %ld, flags = %x)\n",
288ba25f9dcSPavel Emelyanov 				p->comm, task_pid_nr(p), cpu,
289ba25f9dcSPavel Emelyanov 				p->state, p->flags);
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294db912f96SAvi Kivity struct take_cpu_down_param {
295db912f96SAvi Kivity 	unsigned long mod;
296db912f96SAvi Kivity 	void *hcpu;
297db912f96SAvi Kivity };
298db912f96SAvi Kivity 
2991da177e4SLinus Torvalds /* Take this CPU down. */
300514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
3011da177e4SLinus Torvalds {
302db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
3031da177e4SLinus Torvalds 	int err;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
3061da177e4SLinus Torvalds 	err = __cpu_disable();
3071da177e4SLinus Torvalds 	if (err < 0)
308f3705136SZwane Mwaikambo 		return err;
309f3705136SZwane Mwaikambo 
310e9fb7631SAkinobu Mita 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
31114e568e7SThomas Gleixner 	/* Park the stopper thread */
31214e568e7SThomas Gleixner 	kthread_park(current);
313f3705136SZwane Mwaikambo 	return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
317514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
3181da177e4SLinus Torvalds {
319e7407dccSHeiko Carstens 	int err, nr_calls = 0;
320e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
3218bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
322db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
323db912f96SAvi Kivity 		.mod = mod,
324db912f96SAvi Kivity 		.hcpu = hcpu,
325db912f96SAvi Kivity 	};
3261da177e4SLinus Torvalds 
327e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
328e3920fb4SRafael J. Wysocki 		return -EBUSY;
3291da177e4SLinus Torvalds 
330e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
331e3920fb4SRafael J. Wysocki 		return -EINVAL;
3321da177e4SLinus Torvalds 
333d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3344d51985eSMichael Rodriguez 
335e9fb7631SAkinobu Mita 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
336e6bde73bSAkinobu Mita 	if (err) {
337a0d8cdb6SAkinobu Mita 		nr_calls--;
338e9fb7631SAkinobu Mita 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
3391da177e4SLinus Torvalds 		printk("%s: attempt to take down CPU %u failed\n",
340af1f16d0SHarvey Harrison 				__func__, cpu);
341baaca49fSGautham R Shenoy 		goto out_release;
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3446acce3efSPeter Zijlstra 	/*
3456acce3efSPeter Zijlstra 	 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
3466acce3efSPeter Zijlstra 	 * and RCU users of this state to go away such that all new such users
3476acce3efSPeter Zijlstra 	 * will observe it.
3486acce3efSPeter Zijlstra 	 *
3496acce3efSPeter Zijlstra 	 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
3506acce3efSPeter Zijlstra 	 * not imply sync_sched(), so explicitly call both.
351106dd5afSMichael wang 	 *
352106dd5afSMichael wang 	 * Do sync before park smpboot threads to take care the rcu boost case.
3536acce3efSPeter Zijlstra 	 */
3546acce3efSPeter Zijlstra #ifdef CONFIG_PREEMPT
3556acce3efSPeter Zijlstra 	synchronize_sched();
3566acce3efSPeter Zijlstra #endif
3576acce3efSPeter Zijlstra 	synchronize_rcu();
3586acce3efSPeter Zijlstra 
359106dd5afSMichael wang 	smpboot_park_threads(cpu);
360106dd5afSMichael wang 
3616acce3efSPeter Zijlstra 	/*
3626acce3efSPeter Zijlstra 	 * So now all preempt/rcu users must observe !cpu_active().
3636acce3efSPeter Zijlstra 	 */
3646acce3efSPeter Zijlstra 
365e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
36604321587SRusty Russell 	if (err) {
3671da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
368f97f8f06SThomas Gleixner 		smpboot_unpark_threads(cpu);
369e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
3706a1bdc1bSOleg Nesterov 		goto out_release;
3711da177e4SLinus Torvalds 	}
37204321587SRusty Russell 	BUG_ON(cpu_online(cpu));
3731da177e4SLinus Torvalds 
37448c5ccaeSPeter Zijlstra 	/*
37548c5ccaeSPeter Zijlstra 	 * The migration_call() CPU_DYING callback will have removed all
37648c5ccaeSPeter Zijlstra 	 * runnable tasks from the cpu, there's only the idle task left now
37748c5ccaeSPeter Zijlstra 	 * that the migration thread is done doing the stop_machine thing.
37851a96c77SPeter Zijlstra 	 *
37951a96c77SPeter Zijlstra 	 * Wait for the stop thread to go away.
38048c5ccaeSPeter Zijlstra 	 */
38151a96c77SPeter Zijlstra 	while (!idle_cpu(cpu))
38251a96c77SPeter Zijlstra 		cpu_relax();
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* This actually kills the CPU. */
3851da177e4SLinus Torvalds 	__cpu_die(cpu);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
388e9fb7631SAkinobu Mita 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	check_for_tasks(cpu);
3911da177e4SLinus Torvalds 
392baaca49fSGautham R Shenoy out_release:
393d221938cSGautham R Shenoy 	cpu_hotplug_done();
394e9fb7631SAkinobu Mita 	if (!err)
395e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
396e3920fb4SRafael J. Wysocki 	return err;
397e3920fb4SRafael J. Wysocki }
398e3920fb4SRafael J. Wysocki 
399514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
400e3920fb4SRafael J. Wysocki {
4019ea09af3SHeiko Carstens 	int err;
402e3920fb4SRafael J. Wysocki 
403d221938cSGautham R Shenoy 	cpu_maps_update_begin();
404e761b772SMax Krasnyansky 
405e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
406e3920fb4SRafael J. Wysocki 		err = -EBUSY;
407e761b772SMax Krasnyansky 		goto out;
408e761b772SMax Krasnyansky 	}
409e761b772SMax Krasnyansky 
4108bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
411e3920fb4SRafael J. Wysocki 
412e761b772SMax Krasnyansky out:
413d221938cSGautham R Shenoy 	cpu_maps_update_done();
4141da177e4SLinus Torvalds 	return err;
4151da177e4SLinus Torvalds }
416b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
4171da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
4181da177e4SLinus Torvalds 
419e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
4200db0628dSPaul Gortmaker static int _cpu_up(unsigned int cpu, int tasks_frozen)
4211da177e4SLinus Torvalds {
422baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
4231da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
4248bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
4253bb5d2eeSSuresh Siddha 	struct task_struct *idle;
4261da177e4SLinus Torvalds 
427d221938cSGautham R Shenoy 	cpu_hotplug_begin();
42838498a67SThomas Gleixner 
4295e5041f3SYasuaki Ishimatsu 	if (cpu_online(cpu) || !cpu_present(cpu)) {
4305e5041f3SYasuaki Ishimatsu 		ret = -EINVAL;
4315e5041f3SYasuaki Ishimatsu 		goto out;
4325e5041f3SYasuaki Ishimatsu 	}
4335e5041f3SYasuaki Ishimatsu 
4343bb5d2eeSSuresh Siddha 	idle = idle_thread_get(cpu);
4353bb5d2eeSSuresh Siddha 	if (IS_ERR(idle)) {
4363bb5d2eeSSuresh Siddha 		ret = PTR_ERR(idle);
43738498a67SThomas Gleixner 		goto out;
4383bb5d2eeSSuresh Siddha 	}
43938498a67SThomas Gleixner 
440f97f8f06SThomas Gleixner 	ret = smpboot_create_threads(cpu);
441f97f8f06SThomas Gleixner 	if (ret)
442f97f8f06SThomas Gleixner 		goto out;
443f97f8f06SThomas Gleixner 
444e9fb7631SAkinobu Mita 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
445e6bde73bSAkinobu Mita 	if (ret) {
446a0d8cdb6SAkinobu Mita 		nr_calls--;
4474d51985eSMichael Rodriguez 		printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
448af1f16d0SHarvey Harrison 				__func__, cpu);
4491da177e4SLinus Torvalds 		goto out_notify;
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
4533bb5d2eeSSuresh Siddha 	ret = __cpu_up(cpu, idle);
4541da177e4SLinus Torvalds 	if (ret != 0)
4551da177e4SLinus Torvalds 		goto out_notify;
4566978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
4571da177e4SLinus Torvalds 
458f97f8f06SThomas Gleixner 	/* Wake the per cpu threads */
459f97f8f06SThomas Gleixner 	smpboot_unpark_threads(cpu);
460f97f8f06SThomas Gleixner 
4611da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
462e9fb7631SAkinobu Mita 	cpu_notify(CPU_ONLINE | mod, hcpu);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds out_notify:
4651da177e4SLinus Torvalds 	if (ret != 0)
466e9fb7631SAkinobu Mita 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
46738498a67SThomas Gleixner out:
468d221938cSGautham R Shenoy 	cpu_hotplug_done();
469e3920fb4SRafael J. Wysocki 
4701da177e4SLinus Torvalds 	return ret;
4711da177e4SLinus Torvalds }
472e3920fb4SRafael J. Wysocki 
4730db0628dSPaul Gortmaker int cpu_up(unsigned int cpu)
474e3920fb4SRafael J. Wysocki {
475e3920fb4SRafael J. Wysocki 	int err = 0;
476cf23422bSminskey guo 
477e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
47873e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "can't online cpu %d because it is not "
47973e753a5SKAMEZAWA Hiroyuki 			"configured as may-hotadd at boot time\n", cpu);
48087d5e023SChen Gong #if defined(CONFIG_IA64)
48173e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "please check additional_cpus= boot "
48273e753a5SKAMEZAWA Hiroyuki 				"parameter\n");
48373e753a5SKAMEZAWA Hiroyuki #endif
48473e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
48573e753a5SKAMEZAWA Hiroyuki 	}
486e3920fb4SRafael J. Wysocki 
48701b0f197SToshi Kani 	err = try_online_node(cpu_to_node(cpu));
488cf23422bSminskey guo 	if (err)
489cf23422bSminskey guo 		return err;
490cf23422bSminskey guo 
491d221938cSGautham R Shenoy 	cpu_maps_update_begin();
492e761b772SMax Krasnyansky 
493e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
494e3920fb4SRafael J. Wysocki 		err = -EBUSY;
495e761b772SMax Krasnyansky 		goto out;
496e761b772SMax Krasnyansky 	}
497e761b772SMax Krasnyansky 
4988bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
499e3920fb4SRafael J. Wysocki 
500e761b772SMax Krasnyansky out:
501d221938cSGautham R Shenoy 	cpu_maps_update_done();
502e3920fb4SRafael J. Wysocki 	return err;
503e3920fb4SRafael J. Wysocki }
504a513f6baSPaul E. McKenney EXPORT_SYMBOL_GPL(cpu_up);
505e3920fb4SRafael J. Wysocki 
506f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
507e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
508e3920fb4SRafael J. Wysocki 
509e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
510e3920fb4SRafael J. Wysocki {
511e9a5f426SRafael J. Wysocki 	int cpu, first_cpu, error = 0;
512e3920fb4SRafael J. Wysocki 
513d221938cSGautham R Shenoy 	cpu_maps_update_begin();
514e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
5159ee349adSXiaotian Feng 	/*
5169ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
517e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
518e3920fb4SRafael J. Wysocki 	 */
519e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5206ad4c188SPeter Zijlstra 
521e3920fb4SRafael J. Wysocki 	printk("Disabling non-boot CPUs ...\n");
522e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
523e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
524e3920fb4SRafael J. Wysocki 			continue;
5258bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
526feae3203SMike Travis 		if (!error)
527e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
528feae3203SMike Travis 		else {
529e3920fb4SRafael J. Wysocki 			printk(KERN_ERR "Error taking CPU%d down: %d\n",
530e3920fb4SRafael J. Wysocki 				cpu, error);
531e3920fb4SRafael J. Wysocki 			break;
532e3920fb4SRafael J. Wysocki 		}
533e3920fb4SRafael J. Wysocki 	}
53486886e55SJoseph Cihula 
535e3920fb4SRafael J. Wysocki 	if (!error) {
536e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
537e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
538e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
539e3920fb4SRafael J. Wysocki 	} else {
540e1d9fd2eSIngo Molnar 		printk(KERN_ERR "Non-boot CPUs are not disabled\n");
541e3920fb4SRafael J. Wysocki 	}
542d221938cSGautham R Shenoy 	cpu_maps_update_done();
543e3920fb4SRafael J. Wysocki 	return error;
544e3920fb4SRafael J. Wysocki }
545e3920fb4SRafael J. Wysocki 
546d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
547d0af9eedSSuresh Siddha {
548d0af9eedSSuresh Siddha }
549d0af9eedSSuresh Siddha 
550d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
551d0af9eedSSuresh Siddha {
552d0af9eedSSuresh Siddha }
553d0af9eedSSuresh Siddha 
554fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
555e3920fb4SRafael J. Wysocki {
556e3920fb4SRafael J. Wysocki 	int cpu, error;
557e3920fb4SRafael J. Wysocki 
558e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
559d221938cSGautham R Shenoy 	cpu_maps_update_begin();
560e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
561e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
5621d64b9cbSRafael J. Wysocki 		goto out;
563e3920fb4SRafael J. Wysocki 
5644d51985eSMichael Rodriguez 	printk(KERN_INFO "Enabling non-boot CPUs ...\n");
565d0af9eedSSuresh Siddha 
566d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
567d0af9eedSSuresh Siddha 
568e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
5698bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
570e3920fb4SRafael J. Wysocki 		if (!error) {
5714d51985eSMichael Rodriguez 			printk(KERN_INFO "CPU%d is up\n", cpu);
572e3920fb4SRafael J. Wysocki 			continue;
573e3920fb4SRafael J. Wysocki 		}
5741d64b9cbSRafael J. Wysocki 		printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
575e3920fb4SRafael J. Wysocki 	}
576d0af9eedSSuresh Siddha 
577d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
578d0af9eedSSuresh Siddha 
579e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5801d64b9cbSRafael J. Wysocki out:
581d221938cSGautham R Shenoy 	cpu_maps_update_done();
582e3920fb4SRafael J. Wysocki }
583e0b582ecSRusty Russell 
584d7268a31SFenghua Yu static int __init alloc_frozen_cpus(void)
585e0b582ecSRusty Russell {
586e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
587e0b582ecSRusty Russell 		return -ENOMEM;
588e0b582ecSRusty Russell 	return 0;
589e0b582ecSRusty Russell }
590e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
59179cfbdfaSSrivatsa S. Bhat 
59279cfbdfaSSrivatsa S. Bhat /*
59379cfbdfaSSrivatsa S. Bhat  * When callbacks for CPU hotplug notifications are being executed, we must
59479cfbdfaSSrivatsa S. Bhat  * ensure that the state of the system with respect to the tasks being frozen
59579cfbdfaSSrivatsa S. Bhat  * or not, as reported by the notification, remains unchanged *throughout the
59679cfbdfaSSrivatsa S. Bhat  * duration* of the execution of the callbacks.
59779cfbdfaSSrivatsa S. Bhat  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
59879cfbdfaSSrivatsa S. Bhat  *
59979cfbdfaSSrivatsa S. Bhat  * This synchronization is implemented by mutually excluding regular CPU
60079cfbdfaSSrivatsa S. Bhat  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
60179cfbdfaSSrivatsa S. Bhat  * Hibernate notifications.
60279cfbdfaSSrivatsa S. Bhat  */
60379cfbdfaSSrivatsa S. Bhat static int
60479cfbdfaSSrivatsa S. Bhat cpu_hotplug_pm_callback(struct notifier_block *nb,
60579cfbdfaSSrivatsa S. Bhat 			unsigned long action, void *ptr)
60679cfbdfaSSrivatsa S. Bhat {
60779cfbdfaSSrivatsa S. Bhat 	switch (action) {
60879cfbdfaSSrivatsa S. Bhat 
60979cfbdfaSSrivatsa S. Bhat 	case PM_SUSPEND_PREPARE:
61079cfbdfaSSrivatsa S. Bhat 	case PM_HIBERNATION_PREPARE:
61116e53dbfSSrivatsa S. Bhat 		cpu_hotplug_disable();
61279cfbdfaSSrivatsa S. Bhat 		break;
61379cfbdfaSSrivatsa S. Bhat 
61479cfbdfaSSrivatsa S. Bhat 	case PM_POST_SUSPEND:
61579cfbdfaSSrivatsa S. Bhat 	case PM_POST_HIBERNATION:
61616e53dbfSSrivatsa S. Bhat 		cpu_hotplug_enable();
61779cfbdfaSSrivatsa S. Bhat 		break;
61879cfbdfaSSrivatsa S. Bhat 
61979cfbdfaSSrivatsa S. Bhat 	default:
62079cfbdfaSSrivatsa S. Bhat 		return NOTIFY_DONE;
62179cfbdfaSSrivatsa S. Bhat 	}
62279cfbdfaSSrivatsa S. Bhat 
62379cfbdfaSSrivatsa S. Bhat 	return NOTIFY_OK;
62479cfbdfaSSrivatsa S. Bhat }
62579cfbdfaSSrivatsa S. Bhat 
62679cfbdfaSSrivatsa S. Bhat 
627d7268a31SFenghua Yu static int __init cpu_hotplug_pm_sync_init(void)
62879cfbdfaSSrivatsa S. Bhat {
6296e32d479SFenghua Yu 	/*
6306e32d479SFenghua Yu 	 * cpu_hotplug_pm_callback has higher priority than x86
6316e32d479SFenghua Yu 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
6326e32d479SFenghua Yu 	 * to disable cpu hotplug to avoid cpu hotplug race.
6336e32d479SFenghua Yu 	 */
63479cfbdfaSSrivatsa S. Bhat 	pm_notifier(cpu_hotplug_pm_callback, 0);
63579cfbdfaSSrivatsa S. Bhat 	return 0;
63679cfbdfaSSrivatsa S. Bhat }
63779cfbdfaSSrivatsa S. Bhat core_initcall(cpu_hotplug_pm_sync_init);
63879cfbdfaSSrivatsa S. Bhat 
639f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
64068f4f1ecSMax Krasnyansky 
641e545a614SManfred Spraul /**
642e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
643e545a614SManfred Spraul  * @cpu: cpu that just started
644e545a614SManfred Spraul  *
645e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
646e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
647e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
648e545a614SManfred Spraul  */
6490db0628dSPaul Gortmaker void notify_cpu_starting(unsigned int cpu)
650e545a614SManfred Spraul {
651e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
652e545a614SManfred Spraul 
653e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
654e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
655e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
656e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
657e9fb7631SAkinobu Mita 	cpu_notify(val, (void *)(long)cpu);
658e545a614SManfred Spraul }
659e545a614SManfred Spraul 
66068f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
661b8d317d1SMike Travis 
662e56b3bc7SLinus Torvalds /*
663e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
664e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
665e56b3bc7SLinus Torvalds  *
666e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
667e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
668e56b3bc7SLinus Torvalds  */
669b8d317d1SMike Travis 
670e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
6714d51985eSMichael Rodriguez #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
672e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
673e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
674e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
675b8d317d1SMike Travis 
676e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
677b8d317d1SMike Travis 
678e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
679e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
680e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
681e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
682e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
683b8d317d1SMike Travis #endif
684b8d317d1SMike Travis };
685e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
6862d3854a3SRusty Russell 
6872d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
6882d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
689b3199c02SRusty Russell 
690b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
691b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
692b3199c02SRusty Russell 	= CPU_BITS_ALL;
693b3199c02SRusty Russell #else
694b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
695b3199c02SRusty Russell #endif
696b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
697b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
698b3199c02SRusty Russell 
699b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
700b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
701b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
702b3199c02SRusty Russell 
703b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
704b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
705b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
706b3199c02SRusty Russell 
707b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
708b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
709b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
7103fa41520SRusty Russell 
7113fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
7123fa41520SRusty Russell {
7133fa41520SRusty Russell 	if (possible)
7143fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
7153fa41520SRusty Russell 	else
7163fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
7173fa41520SRusty Russell }
7183fa41520SRusty Russell 
7193fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
7203fa41520SRusty Russell {
7213fa41520SRusty Russell 	if (present)
7223fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
7233fa41520SRusty Russell 	else
7243fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
7253fa41520SRusty Russell }
7263fa41520SRusty Russell 
7273fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
7283fa41520SRusty Russell {
729*6acbfb96SLai Jiangshan 	if (online) {
7303fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
731*6acbfb96SLai Jiangshan 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
732*6acbfb96SLai Jiangshan 	} else {
7333fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
7343fa41520SRusty Russell 	}
735*6acbfb96SLai Jiangshan }
7363fa41520SRusty Russell 
7373fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
7383fa41520SRusty Russell {
7393fa41520SRusty Russell 	if (active)
7403fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7413fa41520SRusty Russell 	else
7423fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
7433fa41520SRusty Russell }
7443fa41520SRusty Russell 
7453fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
7463fa41520SRusty Russell {
7473fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
7483fa41520SRusty Russell }
7493fa41520SRusty Russell 
7503fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
7513fa41520SRusty Russell {
7523fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
7533fa41520SRusty Russell }
7543fa41520SRusty Russell 
7553fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
7563fa41520SRusty Russell {
7573fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
7583fa41520SRusty Russell }
759