xref: /linux-6.15/kernel/cpu.c (revision 9d3cfc4c)
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>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/kthread.h>
151da177e4SLinus Torvalds #include <linux/stop_machine.h>
1681615b62SIngo Molnar #include <linux/mutex.h>
171da177e4SLinus Torvalds 
1898a79d6aSRusty Russell #ifdef CONFIG_SMP
19b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */
20aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock);
211da177e4SLinus Torvalds 
22bd5349cfSNeil Brown static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
231da177e4SLinus Torvalds 
24e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
25e3920fb4SRafael J. Wysocki  * Should always be manipulated under cpu_add_remove_lock
26e3920fb4SRafael J. Wysocki  */
27e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled;
28e3920fb4SRafael J. Wysocki 
29d221938cSGautham R Shenoy static struct {
30d221938cSGautham R Shenoy 	struct task_struct *active_writer;
31d221938cSGautham R Shenoy 	struct mutex lock; /* Synchronizes accesses to refcount, */
32d221938cSGautham R Shenoy 	/*
33d221938cSGautham R Shenoy 	 * Also blocks the new readers during
34d221938cSGautham R Shenoy 	 * an ongoing cpu hotplug operation.
35d221938cSGautham R Shenoy 	 */
36d221938cSGautham R Shenoy 	int refcount;
3731950eb6SLinus Torvalds } cpu_hotplug = {
3831950eb6SLinus Torvalds 	.active_writer = NULL,
3931950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
4031950eb6SLinus Torvalds 	.refcount = 0,
4131950eb6SLinus Torvalds };
42d221938cSGautham R Shenoy 
43d221938cSGautham R Shenoy #ifdef CONFIG_HOTPLUG_CPU
44a9d9baa1SAshok Raj 
4586ef5c9aSGautham R Shenoy void get_online_cpus(void)
46a9d9baa1SAshok Raj {
47d221938cSGautham R Shenoy 	might_sleep();
48d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
49aa953877SLinus Torvalds 		return;
50d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
51d221938cSGautham R Shenoy 	cpu_hotplug.refcount++;
52d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
53d221938cSGautham R Shenoy 
54a9d9baa1SAshok Raj }
5586ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
56a9d9baa1SAshok Raj 
5786ef5c9aSGautham R Shenoy void put_online_cpus(void)
58a9d9baa1SAshok Raj {
59d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
60aa953877SLinus Torvalds 		return;
61d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
62d2ba7e2aSOleg Nesterov 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
63d2ba7e2aSOleg Nesterov 		wake_up_process(cpu_hotplug.active_writer);
64d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
65d221938cSGautham R Shenoy 
66a9d9baa1SAshok Raj }
6786ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
68a9d9baa1SAshok Raj 
69a9d9baa1SAshok Raj #endif	/* CONFIG_HOTPLUG_CPU */
7090d45d17SAshok Raj 
71d221938cSGautham R Shenoy /*
72d221938cSGautham R Shenoy  * The following two API's must be used when attempting
73b3199c02SRusty Russell  * to serialize the updates to cpu_online_mask, cpu_present_mask.
74d221938cSGautham R Shenoy  */
75d221938cSGautham R Shenoy void cpu_maps_update_begin(void)
76d221938cSGautham R Shenoy {
77d221938cSGautham R Shenoy 	mutex_lock(&cpu_add_remove_lock);
78d221938cSGautham R Shenoy }
79d221938cSGautham R Shenoy 
80d221938cSGautham R Shenoy void cpu_maps_update_done(void)
81d221938cSGautham R Shenoy {
82d221938cSGautham R Shenoy 	mutex_unlock(&cpu_add_remove_lock);
83d221938cSGautham R Shenoy }
84d221938cSGautham R Shenoy 
85d221938cSGautham R Shenoy /*
86d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
87d221938cSGautham R Shenoy  * refcount goes to zero.
88d221938cSGautham R Shenoy  *
89d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
90d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
91d221938cSGautham R Shenoy  *
92d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
93d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
94d221938cSGautham R Shenoy  *
95d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
96d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
97d221938cSGautham R Shenoy  *   writer.
98d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
99d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
100d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
101d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
102d221938cSGautham R Shenoy  *
103d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
10486ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
105d221938cSGautham R Shenoy  *
106d221938cSGautham R Shenoy  */
107d221938cSGautham R Shenoy static void cpu_hotplug_begin(void)
108d221938cSGautham R Shenoy {
109d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = current;
110d2ba7e2aSOleg Nesterov 
111d2ba7e2aSOleg Nesterov 	for (;;) {
112d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
113d2ba7e2aSOleg Nesterov 		if (likely(!cpu_hotplug.refcount))
114d2ba7e2aSOleg Nesterov 			break;
115d2ba7e2aSOleg Nesterov 		__set_current_state(TASK_UNINTERRUPTIBLE);
116d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
117d221938cSGautham R Shenoy 		schedule();
118d221938cSGautham R Shenoy 	}
119d221938cSGautham R Shenoy }
120d221938cSGautham R Shenoy 
121d221938cSGautham R Shenoy static void cpu_hotplug_done(void)
122d221938cSGautham R Shenoy {
123d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
124d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
125d221938cSGautham R Shenoy }
1261da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
127f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
1281da177e4SLinus Torvalds {
129bd5349cfSNeil Brown 	int ret;
130d221938cSGautham R Shenoy 	cpu_maps_update_begin();
131bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
132d221938cSGautham R Shenoy 	cpu_maps_update_done();
133bd5349cfSNeil Brown 	return ret;
1341da177e4SLinus Torvalds }
13565edc68cSChandra Seetharaman 
13665edc68cSChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
13765edc68cSChandra Seetharaman 
1381da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
1391da177e4SLinus Torvalds 
1409647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
1411da177e4SLinus Torvalds {
142d221938cSGautham R Shenoy 	cpu_maps_update_begin();
143bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
144d221938cSGautham R Shenoy 	cpu_maps_update_done();
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static inline void check_for_tasks(int cpu)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct task_struct *p;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
1531da177e4SLinus Torvalds 	for_each_process(p) {
15411854247SPeter Zijlstra 		if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
1551da177e4SLinus Torvalds 		    (!cputime_eq(p->utime, cputime_zero) ||
1561da177e4SLinus Torvalds 		     !cputime_eq(p->stime, cputime_zero)))
157*9d3cfc4cSFrans Pop 			printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d "
158*9d3cfc4cSFrans Pop 				"(state = %ld, flags = %x)\n",
159ba25f9dcSPavel Emelyanov 				p->comm, task_pid_nr(p), cpu,
160ba25f9dcSPavel Emelyanov 				p->state, p->flags);
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
165db912f96SAvi Kivity struct take_cpu_down_param {
166db912f96SAvi Kivity 	unsigned long mod;
167db912f96SAvi Kivity 	void *hcpu;
168db912f96SAvi Kivity };
169db912f96SAvi Kivity 
1701da177e4SLinus Torvalds /* Take this CPU down. */
171514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
1721da177e4SLinus Torvalds {
173db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
1741da177e4SLinus Torvalds 	int err;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
1771da177e4SLinus Torvalds 	err = __cpu_disable();
1781da177e4SLinus Torvalds 	if (err < 0)
179f3705136SZwane Mwaikambo 		return err;
180f3705136SZwane Mwaikambo 
1813ba35573SManfred Spraul 	raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
1823ba35573SManfred Spraul 				param->hcpu);
1833ba35573SManfred Spraul 
1841da177e4SLinus Torvalds 	/* Force idle task to run as soon as we yield: it should
1851da177e4SLinus Torvalds 	   immediately notice cpu is offline and die quickly. */
1861da177e4SLinus Torvalds 	sched_idle_next();
187f3705136SZwane Mwaikambo 	return 0;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
190e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
191514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
1921da177e4SLinus Torvalds {
193e7407dccSHeiko Carstens 	int err, nr_calls = 0;
194e0b582ecSRusty Russell 	cpumask_var_t old_allowed;
195e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
1968bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
197db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
198db912f96SAvi Kivity 		.mod = mod,
199db912f96SAvi Kivity 		.hcpu = hcpu,
200db912f96SAvi Kivity 	};
2011da177e4SLinus Torvalds 
202e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
203e3920fb4SRafael J. Wysocki 		return -EBUSY;
2041da177e4SLinus Torvalds 
205e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
206e3920fb4SRafael J. Wysocki 		return -EINVAL;
2071da177e4SLinus Torvalds 
208e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&old_allowed, GFP_KERNEL))
209e0b582ecSRusty Russell 		return -ENOMEM;
210e0b582ecSRusty Russell 
211d221938cSGautham R Shenoy 	cpu_hotplug_begin();
2129ee349adSXiaotian Feng 	set_cpu_active(cpu, false);
2138bb78442SRafael J. Wysocki 	err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
214e7407dccSHeiko Carstens 					hcpu, -1, &nr_calls);
2151da177e4SLinus Torvalds 	if (err == NOTIFY_BAD) {
2166ad4c188SPeter Zijlstra 		set_cpu_active(cpu, true);
2176ad4c188SPeter Zijlstra 
218a0d8cdb6SAkinobu Mita 		nr_calls--;
2198bb78442SRafael J. Wysocki 		__raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
2208bb78442SRafael J. Wysocki 					  hcpu, nr_calls, NULL);
2211da177e4SLinus Torvalds 		printk("%s: attempt to take down CPU %u failed\n",
222af1f16d0SHarvey Harrison 				__func__, cpu);
223baaca49fSGautham R Shenoy 		err = -EINVAL;
224baaca49fSGautham R Shenoy 		goto out_release;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	/* Ensure that we are not runnable on dying cpu */
228e0b582ecSRusty Russell 	cpumask_copy(old_allowed, &current->cpus_allowed);
2296ad4c188SPeter Zijlstra 	set_cpus_allowed_ptr(current, cpu_active_mask);
2301da177e4SLinus Torvalds 
231e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
23204321587SRusty Russell 	if (err) {
2336ad4c188SPeter Zijlstra 		set_cpu_active(cpu, true);
2341da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
2358bb78442SRafael J. Wysocki 		if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
236e7407dccSHeiko Carstens 					    hcpu) == NOTIFY_BAD)
2371da177e4SLinus Torvalds 			BUG();
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 		goto out_allowed;
2401da177e4SLinus Torvalds 	}
24104321587SRusty Russell 	BUG_ON(cpu_online(cpu));
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* Wait for it to sleep (leaving idle task). */
2441da177e4SLinus Torvalds 	while (!idle_cpu(cpu))
2451da177e4SLinus Torvalds 		yield();
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* This actually kills the CPU. */
2481da177e4SLinus Torvalds 	__cpu_die(cpu);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
2518bb78442SRafael J. Wysocki 	if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
2528bb78442SRafael J. Wysocki 				    hcpu) == NOTIFY_BAD)
2531da177e4SLinus Torvalds 		BUG();
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	check_for_tasks(cpu);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds out_allowed:
258e0b582ecSRusty Russell 	set_cpus_allowed_ptr(current, old_allowed);
259baaca49fSGautham R Shenoy out_release:
260d221938cSGautham R Shenoy 	cpu_hotplug_done();
2613da1c84cSOleg Nesterov 	if (!err) {
2623da1c84cSOleg Nesterov 		if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
2633da1c84cSOleg Nesterov 					    hcpu) == NOTIFY_BAD)
2643da1c84cSOleg Nesterov 			BUG();
2653da1c84cSOleg Nesterov 	}
266e0b582ecSRusty Russell 	free_cpumask_var(old_allowed);
267e3920fb4SRafael J. Wysocki 	return err;
268e3920fb4SRafael J. Wysocki }
269e3920fb4SRafael J. Wysocki 
270514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
271e3920fb4SRafael J. Wysocki {
2729ea09af3SHeiko Carstens 	int err;
273e3920fb4SRafael J. Wysocki 
2749ea09af3SHeiko Carstens 	err = stop_machine_create();
2759ea09af3SHeiko Carstens 	if (err)
2769ea09af3SHeiko Carstens 		return err;
277d221938cSGautham R Shenoy 	cpu_maps_update_begin();
278e761b772SMax Krasnyansky 
279e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
280e3920fb4SRafael J. Wysocki 		err = -EBUSY;
281e761b772SMax Krasnyansky 		goto out;
282e761b772SMax Krasnyansky 	}
283e761b772SMax Krasnyansky 
2848bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
285e3920fb4SRafael J. Wysocki 
286e761b772SMax Krasnyansky out:
287d221938cSGautham R Shenoy 	cpu_maps_update_done();
2889ea09af3SHeiko Carstens 	stop_machine_destroy();
2891da177e4SLinus Torvalds 	return err;
2901da177e4SLinus Torvalds }
291b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
2921da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
2931da177e4SLinus Torvalds 
294e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
2958bb78442SRafael J. Wysocki static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
2961da177e4SLinus Torvalds {
297baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
2981da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
2998bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
3001da177e4SLinus Torvalds 
301e3920fb4SRafael J. Wysocki 	if (cpu_online(cpu) || !cpu_present(cpu))
302e3920fb4SRafael J. Wysocki 		return -EINVAL;
30390d45d17SAshok Raj 
304d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3058bb78442SRafael J. Wysocki 	ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
306baaca49fSGautham R Shenoy 							-1, &nr_calls);
3071da177e4SLinus Torvalds 	if (ret == NOTIFY_BAD) {
308a0d8cdb6SAkinobu Mita 		nr_calls--;
3091da177e4SLinus Torvalds 		printk("%s: attempt to bring up CPU %u failed\n",
310af1f16d0SHarvey Harrison 				__func__, cpu);
3111da177e4SLinus Torvalds 		ret = -EINVAL;
3121da177e4SLinus Torvalds 		goto out_notify;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
3161da177e4SLinus Torvalds 	ret = __cpu_up(cpu);
3171da177e4SLinus Torvalds 	if (ret != 0)
3181da177e4SLinus Torvalds 		goto out_notify;
3196978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
3201da177e4SLinus Torvalds 
3212b17fa50SRusty Russell 	set_cpu_active(cpu, true);
322279ef6bbSDmitry Adamushko 
3231da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
3248bb78442SRafael J. Wysocki 	raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds out_notify:
3271da177e4SLinus Torvalds 	if (ret != 0)
328baaca49fSGautham R Shenoy 		__raw_notifier_call_chain(&cpu_chain,
3298bb78442SRafael J. Wysocki 				CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
330d221938cSGautham R Shenoy 	cpu_hotplug_done();
331e3920fb4SRafael J. Wysocki 
3321da177e4SLinus Torvalds 	return ret;
3331da177e4SLinus Torvalds }
334e3920fb4SRafael J. Wysocki 
335b282b6f8SGautham R Shenoy int __cpuinit cpu_up(unsigned int cpu)
336e3920fb4SRafael J. Wysocki {
337e3920fb4SRafael J. Wysocki 	int err = 0;
338e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
33973e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "can't online cpu %d because it is not "
34073e753a5SKAMEZAWA Hiroyuki 			"configured as may-hotadd at boot time\n", cpu);
3413ee1062bSHeiko Carstens #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
34273e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "please check additional_cpus= boot "
34373e753a5SKAMEZAWA Hiroyuki 				"parameter\n");
34473e753a5SKAMEZAWA Hiroyuki #endif
34573e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
34673e753a5SKAMEZAWA Hiroyuki 	}
347e3920fb4SRafael J. Wysocki 
348d221938cSGautham R Shenoy 	cpu_maps_update_begin();
349e761b772SMax Krasnyansky 
350e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
351e3920fb4SRafael J. Wysocki 		err = -EBUSY;
352e761b772SMax Krasnyansky 		goto out;
353e761b772SMax Krasnyansky 	}
354e761b772SMax Krasnyansky 
3558bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
356e3920fb4SRafael J. Wysocki 
357e761b772SMax Krasnyansky out:
358d221938cSGautham R Shenoy 	cpu_maps_update_done();
359e3920fb4SRafael J. Wysocki 	return err;
360e3920fb4SRafael J. Wysocki }
361e3920fb4SRafael J. Wysocki 
362f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
363e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
364e3920fb4SRafael J. Wysocki 
365e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
366e3920fb4SRafael J. Wysocki {
36769575d38SShane Wang 	int cpu, first_cpu, error;
368e3920fb4SRafael J. Wysocki 
369a0e280e0SHeiko Carstens 	error = stop_machine_create();
370a0e280e0SHeiko Carstens 	if (error)
371a0e280e0SHeiko Carstens 		return error;
372d221938cSGautham R Shenoy 	cpu_maps_update_begin();
373e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
3749ee349adSXiaotian Feng 	/*
3759ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
376e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
377e3920fb4SRafael J. Wysocki 	 */
378e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
3796ad4c188SPeter Zijlstra 
380e3920fb4SRafael J. Wysocki 	printk("Disabling non-boot CPUs ...\n");
381e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
382e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
383e3920fb4SRafael J. Wysocki 			continue;
3848bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
385feae3203SMike Travis 		if (!error)
386e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
387feae3203SMike Travis 		else {
388e3920fb4SRafael J. Wysocki 			printk(KERN_ERR "Error taking CPU%d down: %d\n",
389e3920fb4SRafael J. Wysocki 				cpu, error);
390e3920fb4SRafael J. Wysocki 			break;
391e3920fb4SRafael J. Wysocki 		}
392e3920fb4SRafael J. Wysocki 	}
39386886e55SJoseph Cihula 
394e3920fb4SRafael J. Wysocki 	if (!error) {
395e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
396e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
397e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
398e3920fb4SRafael J. Wysocki 	} else {
399e1d9fd2eSIngo Molnar 		printk(KERN_ERR "Non-boot CPUs are not disabled\n");
400e3920fb4SRafael J. Wysocki 	}
401d221938cSGautham R Shenoy 	cpu_maps_update_done();
402a0e280e0SHeiko Carstens 	stop_machine_destroy();
403e3920fb4SRafael J. Wysocki 	return error;
404e3920fb4SRafael J. Wysocki }
405e3920fb4SRafael J. Wysocki 
406d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
407d0af9eedSSuresh Siddha {
408d0af9eedSSuresh Siddha }
409d0af9eedSSuresh Siddha 
410d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
411d0af9eedSSuresh Siddha {
412d0af9eedSSuresh Siddha }
413d0af9eedSSuresh Siddha 
414fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
415e3920fb4SRafael J. Wysocki {
416e3920fb4SRafael J. Wysocki 	int cpu, error;
417e3920fb4SRafael J. Wysocki 
418e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
419d221938cSGautham R Shenoy 	cpu_maps_update_begin();
420e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
421e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
4221d64b9cbSRafael J. Wysocki 		goto out;
423e3920fb4SRafael J. Wysocki 
424e3920fb4SRafael J. Wysocki 	printk("Enabling non-boot CPUs ...\n");
425d0af9eedSSuresh Siddha 
426d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
427d0af9eedSSuresh Siddha 
428e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
4298bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
430e3920fb4SRafael J. Wysocki 		if (!error) {
431e3920fb4SRafael J. Wysocki 			printk("CPU%d is up\n", cpu);
432e3920fb4SRafael J. Wysocki 			continue;
433e3920fb4SRafael J. Wysocki 		}
4341d64b9cbSRafael J. Wysocki 		printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
435e3920fb4SRafael J. Wysocki 	}
436d0af9eedSSuresh Siddha 
437d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
438d0af9eedSSuresh Siddha 
439e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
4401d64b9cbSRafael J. Wysocki out:
441d221938cSGautham R Shenoy 	cpu_maps_update_done();
442e3920fb4SRafael J. Wysocki }
443e0b582ecSRusty Russell 
444e0b582ecSRusty Russell static int alloc_frozen_cpus(void)
445e0b582ecSRusty Russell {
446e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
447e0b582ecSRusty Russell 		return -ENOMEM;
448e0b582ecSRusty Russell 	return 0;
449e0b582ecSRusty Russell }
450e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
451f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
45268f4f1ecSMax Krasnyansky 
453e545a614SManfred Spraul /**
454e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
455e545a614SManfred Spraul  * @cpu: cpu that just started
456e545a614SManfred Spraul  *
457e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
458e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
459e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
460e545a614SManfred Spraul  */
46184196414SAl Viro void __cpuinit notify_cpu_starting(unsigned int cpu)
462e545a614SManfred Spraul {
463e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
464e545a614SManfred Spraul 
465e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
466e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
467e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
468e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
469e545a614SManfred Spraul 	raw_notifier_call_chain(&cpu_chain, val, (void *)(long)cpu);
470e545a614SManfred Spraul }
471e545a614SManfred Spraul 
47268f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
473b8d317d1SMike Travis 
474e56b3bc7SLinus Torvalds /*
475e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
476e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
477e56b3bc7SLinus Torvalds  *
478e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
479e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
480e56b3bc7SLinus Torvalds  */
481b8d317d1SMike Travis 
482e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
483e56b3bc7SLinus Torvalds #define MASK_DECLARE_1(x)	[x+1][0] = 1UL << (x)
484e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
485e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
486e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
487b8d317d1SMike Travis 
488e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
489b8d317d1SMike Travis 
490e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
491e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
492e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
493e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
494e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
495b8d317d1SMike Travis #endif
496b8d317d1SMike Travis };
497e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
4982d3854a3SRusty Russell 
4992d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
5002d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
501b3199c02SRusty Russell 
502b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
503b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
504b3199c02SRusty Russell 	= CPU_BITS_ALL;
505b3199c02SRusty Russell #else
506b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
507b3199c02SRusty Russell #endif
508b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
509b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
510b3199c02SRusty Russell 
511b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
512b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
513b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
514b3199c02SRusty Russell 
515b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
516b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
517b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
518b3199c02SRusty Russell 
519b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
520b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
521b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
5223fa41520SRusty Russell 
5233fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
5243fa41520SRusty Russell {
5253fa41520SRusty Russell 	if (possible)
5263fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
5273fa41520SRusty Russell 	else
5283fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
5293fa41520SRusty Russell }
5303fa41520SRusty Russell 
5313fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
5323fa41520SRusty Russell {
5333fa41520SRusty Russell 	if (present)
5343fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
5353fa41520SRusty Russell 	else
5363fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
5373fa41520SRusty Russell }
5383fa41520SRusty Russell 
5393fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
5403fa41520SRusty Russell {
5413fa41520SRusty Russell 	if (online)
5423fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
5433fa41520SRusty Russell 	else
5443fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
5453fa41520SRusty Russell }
5463fa41520SRusty Russell 
5473fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
5483fa41520SRusty Russell {
5493fa41520SRusty Russell 	if (active)
5503fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
5513fa41520SRusty Russell 	else
5523fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
5533fa41520SRusty Russell }
5543fa41520SRusty Russell 
5553fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
5563fa41520SRusty Russell {
5573fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
5583fa41520SRusty Russell }
5593fa41520SRusty Russell 
5603fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
5613fa41520SRusty Russell {
5623fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
5633fa41520SRusty Russell }
5643fa41520SRusty Russell 
5653fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
5663fa41520SRusty Russell {
5673fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
5683fa41520SRusty Russell }
569