xref: /linux-6.15/kernel/cpu.c (revision dd56af42)
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)
82*dd56af42SPaul E. McKenney #define cpuhp_lock_acquire_tryread() \
83*dd56af42SPaul E. McKenney 				  lock_map_acquire_tryread(&cpu_hotplug.dep_map)
84a19423b9SGautham R. Shenoy #define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
85a19423b9SGautham R. Shenoy #define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)
86a19423b9SGautham R. Shenoy 
8786ef5c9aSGautham R Shenoy void get_online_cpus(void)
88a9d9baa1SAshok Raj {
89d221938cSGautham R Shenoy 	might_sleep();
90d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
91aa953877SLinus Torvalds 		return;
92a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire_read();
93d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
94d221938cSGautham R Shenoy 	cpu_hotplug.refcount++;
95d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
96a9d9baa1SAshok Raj }
9786ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
98a9d9baa1SAshok Raj 
99*dd56af42SPaul E. McKenney bool try_get_online_cpus(void)
100*dd56af42SPaul E. McKenney {
101*dd56af42SPaul E. McKenney 	if (cpu_hotplug.active_writer == current)
102*dd56af42SPaul E. McKenney 		return true;
103*dd56af42SPaul E. McKenney 	if (!mutex_trylock(&cpu_hotplug.lock))
104*dd56af42SPaul E. McKenney 		return false;
105*dd56af42SPaul E. McKenney 	cpuhp_lock_acquire_tryread();
106*dd56af42SPaul E. McKenney 	cpu_hotplug.refcount++;
107*dd56af42SPaul E. McKenney 	mutex_unlock(&cpu_hotplug.lock);
108*dd56af42SPaul E. McKenney 	return true;
109*dd56af42SPaul E. McKenney }
110*dd56af42SPaul E. McKenney EXPORT_SYMBOL_GPL(try_get_online_cpus);
111*dd56af42SPaul E. McKenney 
11286ef5c9aSGautham R Shenoy void put_online_cpus(void)
113a9d9baa1SAshok Raj {
114d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
115aa953877SLinus Torvalds 		return;
116d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
117075663d1SSrivatsa S. Bhat 
118075663d1SSrivatsa S. Bhat 	if (WARN_ON(!cpu_hotplug.refcount))
119075663d1SSrivatsa S. Bhat 		cpu_hotplug.refcount++; /* try to fix things up */
120075663d1SSrivatsa S. Bhat 
121d2ba7e2aSOleg Nesterov 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
122d2ba7e2aSOleg Nesterov 		wake_up_process(cpu_hotplug.active_writer);
123d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
124a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
125d221938cSGautham R Shenoy 
126a9d9baa1SAshok Raj }
12786ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
128a9d9baa1SAshok Raj 
129d221938cSGautham R Shenoy /*
130d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
131d221938cSGautham R Shenoy  * refcount goes to zero.
132d221938cSGautham R Shenoy  *
133d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
134d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
135d221938cSGautham R Shenoy  *
136d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
137d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
138d221938cSGautham R Shenoy  *
139d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
140d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
141d221938cSGautham R Shenoy  *   writer.
142d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
143d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
144d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
145d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
146d221938cSGautham R Shenoy  *
147d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
14886ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
149d221938cSGautham R Shenoy  *
150d221938cSGautham R Shenoy  */
151b9d10be7SToshi Kani void cpu_hotplug_begin(void)
152d221938cSGautham R Shenoy {
153d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = current;
154d2ba7e2aSOleg Nesterov 
155a19423b9SGautham R. Shenoy 	cpuhp_lock_acquire();
156d2ba7e2aSOleg Nesterov 	for (;;) {
157d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
158d2ba7e2aSOleg Nesterov 		if (likely(!cpu_hotplug.refcount))
159d2ba7e2aSOleg Nesterov 			break;
160d2ba7e2aSOleg Nesterov 		__set_current_state(TASK_UNINTERRUPTIBLE);
161d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
162d221938cSGautham R Shenoy 		schedule();
163d221938cSGautham R Shenoy 	}
164d221938cSGautham R Shenoy }
165d221938cSGautham R Shenoy 
166b9d10be7SToshi Kani void cpu_hotplug_done(void)
167d221938cSGautham R Shenoy {
168d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
169d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
170a19423b9SGautham R. Shenoy 	cpuhp_lock_release();
171d221938cSGautham R Shenoy }
17279a6cdebSLai Jiangshan 
17316e53dbfSSrivatsa S. Bhat /*
17416e53dbfSSrivatsa S. Bhat  * Wait for currently running CPU hotplug operations to complete (if any) and
17516e53dbfSSrivatsa S. Bhat  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
17616e53dbfSSrivatsa S. Bhat  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
17716e53dbfSSrivatsa S. Bhat  * hotplug path before performing hotplug operations. So acquiring that lock
17816e53dbfSSrivatsa S. Bhat  * guarantees mutual exclusion from any currently running hotplug operations.
17916e53dbfSSrivatsa S. Bhat  */
18016e53dbfSSrivatsa S. Bhat void cpu_hotplug_disable(void)
18116e53dbfSSrivatsa S. Bhat {
18216e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
18316e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 1;
18416e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
18516e53dbfSSrivatsa S. Bhat }
18616e53dbfSSrivatsa S. Bhat 
18716e53dbfSSrivatsa S. Bhat void cpu_hotplug_enable(void)
18816e53dbfSSrivatsa S. Bhat {
18916e53dbfSSrivatsa S. Bhat 	cpu_maps_update_begin();
19016e53dbfSSrivatsa S. Bhat 	cpu_hotplug_disabled = 0;
19116e53dbfSSrivatsa S. Bhat 	cpu_maps_update_done();
19216e53dbfSSrivatsa S. Bhat }
19316e53dbfSSrivatsa S. Bhat 
194b9d10be7SToshi Kani #endif	/* CONFIG_HOTPLUG_CPU */
19579a6cdebSLai Jiangshan 
1961da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
197f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
1981da177e4SLinus Torvalds {
199bd5349cfSNeil Brown 	int ret;
200d221938cSGautham R Shenoy 	cpu_maps_update_begin();
201bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
202d221938cSGautham R Shenoy 	cpu_maps_update_done();
203bd5349cfSNeil Brown 	return ret;
2041da177e4SLinus Torvalds }
20565edc68cSChandra Seetharaman 
20693ae4f97SSrivatsa S. Bhat int __ref __register_cpu_notifier(struct notifier_block *nb)
20793ae4f97SSrivatsa S. Bhat {
20893ae4f97SSrivatsa S. Bhat 	return raw_notifier_chain_register(&cpu_chain, nb);
20993ae4f97SSrivatsa S. Bhat }
21093ae4f97SSrivatsa S. Bhat 
211e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
212e9fb7631SAkinobu Mita 			int *nr_calls)
213e9fb7631SAkinobu Mita {
214e6bde73bSAkinobu Mita 	int ret;
215e6bde73bSAkinobu Mita 
216e6bde73bSAkinobu Mita 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
217e9fb7631SAkinobu Mita 					nr_calls);
218e6bde73bSAkinobu Mita 
219e6bde73bSAkinobu Mita 	return notifier_to_errno(ret);
220e9fb7631SAkinobu Mita }
221e9fb7631SAkinobu Mita 
222e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v)
223e9fb7631SAkinobu Mita {
224e9fb7631SAkinobu Mita 	return __cpu_notify(val, v, -1, NULL);
225e9fb7631SAkinobu Mita }
226e9fb7631SAkinobu Mita 
22700b9b0afSLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
22800b9b0afSLinus Torvalds 
229e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v)
230e9fb7631SAkinobu Mita {
23100b9b0afSLinus Torvalds 	BUG_ON(cpu_notify(val, v));
232e9fb7631SAkinobu Mita }
2331da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
23493ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__register_cpu_notifier);
2351da177e4SLinus Torvalds 
2369647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
2371da177e4SLinus Torvalds {
238d221938cSGautham R Shenoy 	cpu_maps_update_begin();
239bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
240d221938cSGautham R Shenoy 	cpu_maps_update_done();
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
2431da177e4SLinus Torvalds 
24493ae4f97SSrivatsa S. Bhat void __ref __unregister_cpu_notifier(struct notifier_block *nb)
24593ae4f97SSrivatsa S. Bhat {
24693ae4f97SSrivatsa S. Bhat 	raw_notifier_chain_unregister(&cpu_chain, nb);
24793ae4f97SSrivatsa S. Bhat }
24893ae4f97SSrivatsa S. Bhat EXPORT_SYMBOL(__unregister_cpu_notifier);
24993ae4f97SSrivatsa S. Bhat 
250e4cc2f87SAnton Vorontsov /**
251e4cc2f87SAnton Vorontsov  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
252e4cc2f87SAnton Vorontsov  * @cpu: a CPU id
253e4cc2f87SAnton Vorontsov  *
254e4cc2f87SAnton Vorontsov  * This function walks all processes, finds a valid mm struct for each one and
255e4cc2f87SAnton Vorontsov  * then clears a corresponding bit in mm's cpumask.  While this all sounds
256e4cc2f87SAnton Vorontsov  * trivial, there are various non-obvious corner cases, which this function
257e4cc2f87SAnton Vorontsov  * tries to solve in a safe manner.
258e4cc2f87SAnton Vorontsov  *
259e4cc2f87SAnton Vorontsov  * Also note that the function uses a somewhat relaxed locking scheme, so it may
260e4cc2f87SAnton Vorontsov  * be called only for an already offlined CPU.
261e4cc2f87SAnton Vorontsov  */
262cb79295eSAnton Vorontsov void clear_tasks_mm_cpumask(int cpu)
263cb79295eSAnton Vorontsov {
264cb79295eSAnton Vorontsov 	struct task_struct *p;
265cb79295eSAnton Vorontsov 
266cb79295eSAnton Vorontsov 	/*
267cb79295eSAnton Vorontsov 	 * This function is called after the cpu is taken down and marked
268cb79295eSAnton Vorontsov 	 * offline, so its not like new tasks will ever get this cpu set in
269cb79295eSAnton Vorontsov 	 * their mm mask. -- Peter Zijlstra
270cb79295eSAnton Vorontsov 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
271cb79295eSAnton Vorontsov 	 * full-fledged tasklist_lock.
272cb79295eSAnton Vorontsov 	 */
273e4cc2f87SAnton Vorontsov 	WARN_ON(cpu_online(cpu));
274cb79295eSAnton Vorontsov 	rcu_read_lock();
275cb79295eSAnton Vorontsov 	for_each_process(p) {
276cb79295eSAnton Vorontsov 		struct task_struct *t;
277cb79295eSAnton Vorontsov 
278e4cc2f87SAnton Vorontsov 		/*
279e4cc2f87SAnton Vorontsov 		 * Main thread might exit, but other threads may still have
280e4cc2f87SAnton Vorontsov 		 * a valid mm. Find one.
281e4cc2f87SAnton Vorontsov 		 */
282cb79295eSAnton Vorontsov 		t = find_lock_task_mm(p);
283cb79295eSAnton Vorontsov 		if (!t)
284cb79295eSAnton Vorontsov 			continue;
285cb79295eSAnton Vorontsov 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
286cb79295eSAnton Vorontsov 		task_unlock(t);
287cb79295eSAnton Vorontsov 	}
288cb79295eSAnton Vorontsov 	rcu_read_unlock();
289cb79295eSAnton Vorontsov }
290cb79295eSAnton Vorontsov 
291b728ca06SKirill Tkhai static inline void check_for_tasks(int dead_cpu)
2921da177e4SLinus Torvalds {
293b728ca06SKirill Tkhai 	struct task_struct *g, *p;
2941da177e4SLinus Torvalds 
295b728ca06SKirill Tkhai 	read_lock_irq(&tasklist_lock);
296b728ca06SKirill Tkhai 	do_each_thread(g, p) {
297b728ca06SKirill Tkhai 		if (!p->on_rq)
298b728ca06SKirill Tkhai 			continue;
299b728ca06SKirill Tkhai 		/*
300b728ca06SKirill Tkhai 		 * We do the check with unlocked task_rq(p)->lock.
301b728ca06SKirill Tkhai 		 * Order the reading to do not warn about a task,
302b728ca06SKirill Tkhai 		 * which was running on this cpu in the past, and
303b728ca06SKirill Tkhai 		 * it's just been woken on another cpu.
304b728ca06SKirill Tkhai 		 */
305b728ca06SKirill Tkhai 		rmb();
306b728ca06SKirill Tkhai 		if (task_cpu(p) != dead_cpu)
307b728ca06SKirill Tkhai 			continue;
308b728ca06SKirill Tkhai 
30984117da5SFabian Frederick 		pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
310b728ca06SKirill Tkhai 			p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
311b728ca06SKirill Tkhai 	} while_each_thread(g, p);
312b728ca06SKirill Tkhai 	read_unlock_irq(&tasklist_lock);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
315db912f96SAvi Kivity struct take_cpu_down_param {
316db912f96SAvi Kivity 	unsigned long mod;
317db912f96SAvi Kivity 	void *hcpu;
318db912f96SAvi Kivity };
319db912f96SAvi Kivity 
3201da177e4SLinus Torvalds /* Take this CPU down. */
321514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
3221da177e4SLinus Torvalds {
323db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
3241da177e4SLinus Torvalds 	int err;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
3271da177e4SLinus Torvalds 	err = __cpu_disable();
3281da177e4SLinus Torvalds 	if (err < 0)
329f3705136SZwane Mwaikambo 		return err;
330f3705136SZwane Mwaikambo 
331e9fb7631SAkinobu Mita 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
33214e568e7SThomas Gleixner 	/* Park the stopper thread */
33314e568e7SThomas Gleixner 	kthread_park(current);
334f3705136SZwane Mwaikambo 	return 0;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
338514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
3391da177e4SLinus Torvalds {
340e7407dccSHeiko Carstens 	int err, nr_calls = 0;
341e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
3428bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
343db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
344db912f96SAvi Kivity 		.mod = mod,
345db912f96SAvi Kivity 		.hcpu = hcpu,
346db912f96SAvi Kivity 	};
3471da177e4SLinus Torvalds 
348e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
349e3920fb4SRafael J. Wysocki 		return -EBUSY;
3501da177e4SLinus Torvalds 
351e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
352e3920fb4SRafael J. Wysocki 		return -EINVAL;
3531da177e4SLinus Torvalds 
354d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3554d51985eSMichael Rodriguez 
356e9fb7631SAkinobu Mita 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
357e6bde73bSAkinobu Mita 	if (err) {
358a0d8cdb6SAkinobu Mita 		nr_calls--;
359e9fb7631SAkinobu Mita 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
36084117da5SFabian Frederick 		pr_warn("%s: attempt to take down CPU %u failed\n",
361af1f16d0SHarvey Harrison 			__func__, cpu);
362baaca49fSGautham R Shenoy 		goto out_release;
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3656acce3efSPeter Zijlstra 	/*
3666acce3efSPeter Zijlstra 	 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
3676acce3efSPeter Zijlstra 	 * and RCU users of this state to go away such that all new such users
3686acce3efSPeter Zijlstra 	 * will observe it.
3696acce3efSPeter Zijlstra 	 *
3706acce3efSPeter Zijlstra 	 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
3716acce3efSPeter Zijlstra 	 * not imply sync_sched(), so explicitly call both.
372106dd5afSMichael wang 	 *
373106dd5afSMichael wang 	 * Do sync before park smpboot threads to take care the rcu boost case.
3746acce3efSPeter Zijlstra 	 */
3756acce3efSPeter Zijlstra #ifdef CONFIG_PREEMPT
3766acce3efSPeter Zijlstra 	synchronize_sched();
3776acce3efSPeter Zijlstra #endif
3786acce3efSPeter Zijlstra 	synchronize_rcu();
3796acce3efSPeter Zijlstra 
380106dd5afSMichael wang 	smpboot_park_threads(cpu);
381106dd5afSMichael wang 
3826acce3efSPeter Zijlstra 	/*
3836acce3efSPeter Zijlstra 	 * So now all preempt/rcu users must observe !cpu_active().
3846acce3efSPeter Zijlstra 	 */
3856acce3efSPeter Zijlstra 
386e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
38704321587SRusty Russell 	if (err) {
3881da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
389f97f8f06SThomas Gleixner 		smpboot_unpark_threads(cpu);
390e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
3916a1bdc1bSOleg Nesterov 		goto out_release;
3921da177e4SLinus Torvalds 	}
39304321587SRusty Russell 	BUG_ON(cpu_online(cpu));
3941da177e4SLinus Torvalds 
39548c5ccaeSPeter Zijlstra 	/*
39648c5ccaeSPeter Zijlstra 	 * The migration_call() CPU_DYING callback will have removed all
39748c5ccaeSPeter Zijlstra 	 * runnable tasks from the cpu, there's only the idle task left now
39848c5ccaeSPeter Zijlstra 	 * that the migration thread is done doing the stop_machine thing.
39951a96c77SPeter Zijlstra 	 *
40051a96c77SPeter Zijlstra 	 * Wait for the stop thread to go away.
40148c5ccaeSPeter Zijlstra 	 */
40251a96c77SPeter Zijlstra 	while (!idle_cpu(cpu))
40351a96c77SPeter Zijlstra 		cpu_relax();
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/* This actually kills the CPU. */
4061da177e4SLinus Torvalds 	__cpu_die(cpu);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
409e9fb7631SAkinobu Mita 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	check_for_tasks(cpu);
4121da177e4SLinus Torvalds 
413baaca49fSGautham R Shenoy out_release:
414d221938cSGautham R Shenoy 	cpu_hotplug_done();
415e9fb7631SAkinobu Mita 	if (!err)
416e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
417e3920fb4SRafael J. Wysocki 	return err;
418e3920fb4SRafael J. Wysocki }
419e3920fb4SRafael J. Wysocki 
420514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
421e3920fb4SRafael J. Wysocki {
4229ea09af3SHeiko Carstens 	int err;
423e3920fb4SRafael J. Wysocki 
424d221938cSGautham R Shenoy 	cpu_maps_update_begin();
425e761b772SMax Krasnyansky 
426e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
427e3920fb4SRafael J. Wysocki 		err = -EBUSY;
428e761b772SMax Krasnyansky 		goto out;
429e761b772SMax Krasnyansky 	}
430e761b772SMax Krasnyansky 
4318bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
432e3920fb4SRafael J. Wysocki 
433e761b772SMax Krasnyansky out:
434d221938cSGautham R Shenoy 	cpu_maps_update_done();
4351da177e4SLinus Torvalds 	return err;
4361da177e4SLinus Torvalds }
437b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
4381da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
4391da177e4SLinus Torvalds 
440e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
4410db0628dSPaul Gortmaker static int _cpu_up(unsigned int cpu, int tasks_frozen)
4421da177e4SLinus Torvalds {
443baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
4441da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
4458bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
4463bb5d2eeSSuresh Siddha 	struct task_struct *idle;
4471da177e4SLinus Torvalds 
448d221938cSGautham R Shenoy 	cpu_hotplug_begin();
44938498a67SThomas Gleixner 
4505e5041f3SYasuaki Ishimatsu 	if (cpu_online(cpu) || !cpu_present(cpu)) {
4515e5041f3SYasuaki Ishimatsu 		ret = -EINVAL;
4525e5041f3SYasuaki Ishimatsu 		goto out;
4535e5041f3SYasuaki Ishimatsu 	}
4545e5041f3SYasuaki Ishimatsu 
4553bb5d2eeSSuresh Siddha 	idle = idle_thread_get(cpu);
4563bb5d2eeSSuresh Siddha 	if (IS_ERR(idle)) {
4573bb5d2eeSSuresh Siddha 		ret = PTR_ERR(idle);
45838498a67SThomas Gleixner 		goto out;
4593bb5d2eeSSuresh Siddha 	}
46038498a67SThomas Gleixner 
461f97f8f06SThomas Gleixner 	ret = smpboot_create_threads(cpu);
462f97f8f06SThomas Gleixner 	if (ret)
463f97f8f06SThomas Gleixner 		goto out;
464f97f8f06SThomas Gleixner 
465e9fb7631SAkinobu Mita 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
466e6bde73bSAkinobu Mita 	if (ret) {
467a0d8cdb6SAkinobu Mita 		nr_calls--;
46884117da5SFabian Frederick 		pr_warn("%s: attempt to bring up CPU %u failed\n",
469af1f16d0SHarvey Harrison 			__func__, cpu);
4701da177e4SLinus Torvalds 		goto out_notify;
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
4743bb5d2eeSSuresh Siddha 	ret = __cpu_up(cpu, idle);
4751da177e4SLinus Torvalds 	if (ret != 0)
4761da177e4SLinus Torvalds 		goto out_notify;
4776978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
4781da177e4SLinus Torvalds 
479f97f8f06SThomas Gleixner 	/* Wake the per cpu threads */
480f97f8f06SThomas Gleixner 	smpboot_unpark_threads(cpu);
481f97f8f06SThomas Gleixner 
4821da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
483e9fb7631SAkinobu Mita 	cpu_notify(CPU_ONLINE | mod, hcpu);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds out_notify:
4861da177e4SLinus Torvalds 	if (ret != 0)
487e9fb7631SAkinobu Mita 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
48838498a67SThomas Gleixner out:
489d221938cSGautham R Shenoy 	cpu_hotplug_done();
490e3920fb4SRafael J. Wysocki 
4911da177e4SLinus Torvalds 	return ret;
4921da177e4SLinus Torvalds }
493e3920fb4SRafael J. Wysocki 
4940db0628dSPaul Gortmaker int cpu_up(unsigned int cpu)
495e3920fb4SRafael J. Wysocki {
496e3920fb4SRafael J. Wysocki 	int err = 0;
497cf23422bSminskey guo 
498e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
49984117da5SFabian Frederick 		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
50084117da5SFabian Frederick 		       cpu);
50187d5e023SChen Gong #if defined(CONFIG_IA64)
50284117da5SFabian Frederick 		pr_err("please check additional_cpus= boot parameter\n");
50373e753a5SKAMEZAWA Hiroyuki #endif
50473e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
50573e753a5SKAMEZAWA Hiroyuki 	}
506e3920fb4SRafael J. Wysocki 
50701b0f197SToshi Kani 	err = try_online_node(cpu_to_node(cpu));
508cf23422bSminskey guo 	if (err)
509cf23422bSminskey guo 		return err;
510cf23422bSminskey guo 
511d221938cSGautham R Shenoy 	cpu_maps_update_begin();
512e761b772SMax Krasnyansky 
513e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
514e3920fb4SRafael J. Wysocki 		err = -EBUSY;
515e761b772SMax Krasnyansky 		goto out;
516e761b772SMax Krasnyansky 	}
517e761b772SMax Krasnyansky 
5188bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
519e3920fb4SRafael J. Wysocki 
520e761b772SMax Krasnyansky out:
521d221938cSGautham R Shenoy 	cpu_maps_update_done();
522e3920fb4SRafael J. Wysocki 	return err;
523e3920fb4SRafael J. Wysocki }
524a513f6baSPaul E. McKenney EXPORT_SYMBOL_GPL(cpu_up);
525e3920fb4SRafael J. Wysocki 
526f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
527e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
528e3920fb4SRafael J. Wysocki 
529e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
530e3920fb4SRafael J. Wysocki {
531e9a5f426SRafael J. Wysocki 	int cpu, first_cpu, error = 0;
532e3920fb4SRafael J. Wysocki 
533d221938cSGautham R Shenoy 	cpu_maps_update_begin();
534e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
5359ee349adSXiaotian Feng 	/*
5369ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
537e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
538e3920fb4SRafael J. Wysocki 	 */
539e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
5406ad4c188SPeter Zijlstra 
54184117da5SFabian Frederick 	pr_info("Disabling non-boot CPUs ...\n");
542e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
543e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
544e3920fb4SRafael J. Wysocki 			continue;
545bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
5468bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
547bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
548feae3203SMike Travis 		if (!error)
549e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
550feae3203SMike Travis 		else {
55184117da5SFabian Frederick 			pr_err("Error taking CPU%d down: %d\n", cpu, error);
552e3920fb4SRafael J. Wysocki 			break;
553e3920fb4SRafael J. Wysocki 		}
554e3920fb4SRafael J. Wysocki 	}
55586886e55SJoseph Cihula 
556e3920fb4SRafael J. Wysocki 	if (!error) {
557e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
558e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
559e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
560e3920fb4SRafael J. Wysocki 	} else {
56184117da5SFabian Frederick 		pr_err("Non-boot CPUs are not disabled\n");
562e3920fb4SRafael J. Wysocki 	}
563d221938cSGautham R Shenoy 	cpu_maps_update_done();
564e3920fb4SRafael J. Wysocki 	return error;
565e3920fb4SRafael J. Wysocki }
566e3920fb4SRafael J. Wysocki 
567d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
568d0af9eedSSuresh Siddha {
569d0af9eedSSuresh Siddha }
570d0af9eedSSuresh Siddha 
571d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
572d0af9eedSSuresh Siddha {
573d0af9eedSSuresh Siddha }
574d0af9eedSSuresh Siddha 
575fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
576e3920fb4SRafael J. Wysocki {
577e3920fb4SRafael J. Wysocki 	int cpu, error;
578e3920fb4SRafael J. Wysocki 
579e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
580d221938cSGautham R Shenoy 	cpu_maps_update_begin();
581e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
582e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
5831d64b9cbSRafael J. Wysocki 		goto out;
584e3920fb4SRafael J. Wysocki 
58584117da5SFabian Frederick 	pr_info("Enabling non-boot CPUs ...\n");
586d0af9eedSSuresh Siddha 
587d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
588d0af9eedSSuresh Siddha 
589e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
590bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
5918bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
592bb3632c6STodd E Brandt 		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
593e3920fb4SRafael J. Wysocki 		if (!error) {
59484117da5SFabian Frederick 			pr_info("CPU%d is up\n", cpu);
595e3920fb4SRafael J. Wysocki 			continue;
596e3920fb4SRafael J. Wysocki 		}
59784117da5SFabian Frederick 		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
598e3920fb4SRafael J. Wysocki 	}
599d0af9eedSSuresh Siddha 
600d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
601d0af9eedSSuresh Siddha 
602e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
6031d64b9cbSRafael J. Wysocki out:
604d221938cSGautham R Shenoy 	cpu_maps_update_done();
605e3920fb4SRafael J. Wysocki }
606e0b582ecSRusty Russell 
607d7268a31SFenghua Yu static int __init alloc_frozen_cpus(void)
608e0b582ecSRusty Russell {
609e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
610e0b582ecSRusty Russell 		return -ENOMEM;
611e0b582ecSRusty Russell 	return 0;
612e0b582ecSRusty Russell }
613e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
61479cfbdfaSSrivatsa S. Bhat 
61579cfbdfaSSrivatsa S. Bhat /*
61679cfbdfaSSrivatsa S. Bhat  * When callbacks for CPU hotplug notifications are being executed, we must
61779cfbdfaSSrivatsa S. Bhat  * ensure that the state of the system with respect to the tasks being frozen
61879cfbdfaSSrivatsa S. Bhat  * or not, as reported by the notification, remains unchanged *throughout the
61979cfbdfaSSrivatsa S. Bhat  * duration* of the execution of the callbacks.
62079cfbdfaSSrivatsa S. Bhat  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
62179cfbdfaSSrivatsa S. Bhat  *
62279cfbdfaSSrivatsa S. Bhat  * This synchronization is implemented by mutually excluding regular CPU
62379cfbdfaSSrivatsa S. Bhat  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
62479cfbdfaSSrivatsa S. Bhat  * Hibernate notifications.
62579cfbdfaSSrivatsa S. Bhat  */
62679cfbdfaSSrivatsa S. Bhat static int
62779cfbdfaSSrivatsa S. Bhat cpu_hotplug_pm_callback(struct notifier_block *nb,
62879cfbdfaSSrivatsa S. Bhat 			unsigned long action, void *ptr)
62979cfbdfaSSrivatsa S. Bhat {
63079cfbdfaSSrivatsa S. Bhat 	switch (action) {
63179cfbdfaSSrivatsa S. Bhat 
63279cfbdfaSSrivatsa S. Bhat 	case PM_SUSPEND_PREPARE:
63379cfbdfaSSrivatsa S. Bhat 	case PM_HIBERNATION_PREPARE:
63416e53dbfSSrivatsa S. Bhat 		cpu_hotplug_disable();
63579cfbdfaSSrivatsa S. Bhat 		break;
63679cfbdfaSSrivatsa S. Bhat 
63779cfbdfaSSrivatsa S. Bhat 	case PM_POST_SUSPEND:
63879cfbdfaSSrivatsa S. Bhat 	case PM_POST_HIBERNATION:
63916e53dbfSSrivatsa S. Bhat 		cpu_hotplug_enable();
64079cfbdfaSSrivatsa S. Bhat 		break;
64179cfbdfaSSrivatsa S. Bhat 
64279cfbdfaSSrivatsa S. Bhat 	default:
64379cfbdfaSSrivatsa S. Bhat 		return NOTIFY_DONE;
64479cfbdfaSSrivatsa S. Bhat 	}
64579cfbdfaSSrivatsa S. Bhat 
64679cfbdfaSSrivatsa S. Bhat 	return NOTIFY_OK;
64779cfbdfaSSrivatsa S. Bhat }
64879cfbdfaSSrivatsa S. Bhat 
64979cfbdfaSSrivatsa S. Bhat 
650d7268a31SFenghua Yu static int __init cpu_hotplug_pm_sync_init(void)
65179cfbdfaSSrivatsa S. Bhat {
6526e32d479SFenghua Yu 	/*
6536e32d479SFenghua Yu 	 * cpu_hotplug_pm_callback has higher priority than x86
6546e32d479SFenghua Yu 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
6556e32d479SFenghua Yu 	 * to disable cpu hotplug to avoid cpu hotplug race.
6566e32d479SFenghua Yu 	 */
65779cfbdfaSSrivatsa S. Bhat 	pm_notifier(cpu_hotplug_pm_callback, 0);
65879cfbdfaSSrivatsa S. Bhat 	return 0;
65979cfbdfaSSrivatsa S. Bhat }
66079cfbdfaSSrivatsa S. Bhat core_initcall(cpu_hotplug_pm_sync_init);
66179cfbdfaSSrivatsa S. Bhat 
662f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
66368f4f1ecSMax Krasnyansky 
664e545a614SManfred Spraul /**
665e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
666e545a614SManfred Spraul  * @cpu: cpu that just started
667e545a614SManfred Spraul  *
668e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
669e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
670e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
671e545a614SManfred Spraul  */
6720db0628dSPaul Gortmaker void notify_cpu_starting(unsigned int cpu)
673e545a614SManfred Spraul {
674e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
675e545a614SManfred Spraul 
676e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
677e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
678e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
679e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
680e9fb7631SAkinobu Mita 	cpu_notify(val, (void *)(long)cpu);
681e545a614SManfred Spraul }
682e545a614SManfred Spraul 
68368f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
684b8d317d1SMike Travis 
685e56b3bc7SLinus Torvalds /*
686e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
687e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
688e56b3bc7SLinus Torvalds  *
689e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
690e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
691e56b3bc7SLinus Torvalds  */
692b8d317d1SMike Travis 
693e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
6944d51985eSMichael Rodriguez #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
695e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
696e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
697e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
698b8d317d1SMike Travis 
699e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
700b8d317d1SMike Travis 
701e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
702e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
703e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
704e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
705e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
706b8d317d1SMike Travis #endif
707b8d317d1SMike Travis };
708e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
7092d3854a3SRusty Russell 
7102d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
7112d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
712b3199c02SRusty Russell 
713b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
714b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
715b3199c02SRusty Russell 	= CPU_BITS_ALL;
716b3199c02SRusty Russell #else
717b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
718b3199c02SRusty Russell #endif
719b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
720b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
721b3199c02SRusty Russell 
722b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
723b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
724b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
725b3199c02SRusty Russell 
726b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
727b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
728b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
729b3199c02SRusty Russell 
730b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
731b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
732b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
7333fa41520SRusty Russell 
7343fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
7353fa41520SRusty Russell {
7363fa41520SRusty Russell 	if (possible)
7373fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
7383fa41520SRusty Russell 	else
7393fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
7403fa41520SRusty Russell }
7413fa41520SRusty Russell 
7423fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
7433fa41520SRusty Russell {
7443fa41520SRusty Russell 	if (present)
7453fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
7463fa41520SRusty Russell 	else
7473fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
7483fa41520SRusty Russell }
7493fa41520SRusty Russell 
7503fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
7513fa41520SRusty Russell {
7526acbfb96SLai Jiangshan 	if (online) {
7533fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
7546acbfb96SLai Jiangshan 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7556acbfb96SLai Jiangshan 	} else {
7563fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
7573fa41520SRusty Russell 	}
7586acbfb96SLai Jiangshan }
7593fa41520SRusty Russell 
7603fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
7613fa41520SRusty Russell {
7623fa41520SRusty Russell 	if (active)
7633fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
7643fa41520SRusty Russell 	else
7653fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
7663fa41520SRusty Russell }
7673fa41520SRusty Russell 
7683fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
7693fa41520SRusty Russell {
7703fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
7713fa41520SRusty Russell }
7723fa41520SRusty Russell 
7733fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
7743fa41520SRusty Russell {
7753fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
7763fa41520SRusty Russell }
7773fa41520SRusty Russell 
7783fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
7793fa41520SRusty Russell {
7803fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
7813fa41520SRusty Russell }
782