xref: /linux-6.15/kernel/cpu.c (revision 31950eb6)
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;
37*31950eb6SLinus Torvalds } cpu_hotplug = {
38*31950eb6SLinus Torvalds 	.active_writer = NULL,
39*31950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
40*31950eb6SLinus Torvalds 	.refcount = 0,
41*31950eb6SLinus 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) {
1541da177e4SLinus Torvalds 		if (task_cpu(p) == cpu &&
1551da177e4SLinus Torvalds 		    (!cputime_eq(p->utime, cputime_zero) ||
1561da177e4SLinus Torvalds 		     !cputime_eq(p->stime, cputime_zero)))
1571da177e4SLinus Torvalds 			printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
158e7407dccSHeiko Carstens 				(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();
2128bb78442SRafael J. Wysocki 	err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
213e7407dccSHeiko Carstens 					hcpu, -1, &nr_calls);
2141da177e4SLinus Torvalds 	if (err == NOTIFY_BAD) {
215a0d8cdb6SAkinobu Mita 		nr_calls--;
2168bb78442SRafael J. Wysocki 		__raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
2178bb78442SRafael J. Wysocki 					  hcpu, nr_calls, NULL);
2181da177e4SLinus Torvalds 		printk("%s: attempt to take down CPU %u failed\n",
219af1f16d0SHarvey Harrison 				__func__, cpu);
220baaca49fSGautham R Shenoy 		err = -EINVAL;
221baaca49fSGautham R Shenoy 		goto out_release;
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	/* Ensure that we are not runnable on dying cpu */
225e0b582ecSRusty Russell 	cpumask_copy(old_allowed, &current->cpus_allowed);
226e0b582ecSRusty Russell 	set_cpus_allowed_ptr(current,
227e0b582ecSRusty Russell 			     cpumask_of(cpumask_any_but(cpu_online_mask, cpu)));
2281da177e4SLinus Torvalds 
229e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
23004321587SRusty Russell 	if (err) {
2311da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
2328bb78442SRafael J. Wysocki 		if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
233e7407dccSHeiko Carstens 					    hcpu) == NOTIFY_BAD)
2341da177e4SLinus Torvalds 			BUG();
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 		goto out_allowed;
2371da177e4SLinus Torvalds 	}
23804321587SRusty Russell 	BUG_ON(cpu_online(cpu));
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* Wait for it to sleep (leaving idle task). */
2411da177e4SLinus Torvalds 	while (!idle_cpu(cpu))
2421da177e4SLinus Torvalds 		yield();
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/* This actually kills the CPU. */
2451da177e4SLinus Torvalds 	__cpu_die(cpu);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
2488bb78442SRafael J. Wysocki 	if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
2498bb78442SRafael J. Wysocki 				    hcpu) == NOTIFY_BAD)
2501da177e4SLinus Torvalds 		BUG();
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	check_for_tasks(cpu);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds out_allowed:
255e0b582ecSRusty Russell 	set_cpus_allowed_ptr(current, old_allowed);
256baaca49fSGautham R Shenoy out_release:
257d221938cSGautham R Shenoy 	cpu_hotplug_done();
2583da1c84cSOleg Nesterov 	if (!err) {
2593da1c84cSOleg Nesterov 		if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
2603da1c84cSOleg Nesterov 					    hcpu) == NOTIFY_BAD)
2613da1c84cSOleg Nesterov 			BUG();
2623da1c84cSOleg Nesterov 	}
263e0b582ecSRusty Russell 	free_cpumask_var(old_allowed);
264e3920fb4SRafael J. Wysocki 	return err;
265e3920fb4SRafael J. Wysocki }
266e3920fb4SRafael J. Wysocki 
267514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
268e3920fb4SRafael J. Wysocki {
2699ea09af3SHeiko Carstens 	int err;
270e3920fb4SRafael J. Wysocki 
2719ea09af3SHeiko Carstens 	err = stop_machine_create();
2729ea09af3SHeiko Carstens 	if (err)
2739ea09af3SHeiko Carstens 		return err;
274d221938cSGautham R Shenoy 	cpu_maps_update_begin();
275e761b772SMax Krasnyansky 
276e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
277e3920fb4SRafael J. Wysocki 		err = -EBUSY;
278e761b772SMax Krasnyansky 		goto out;
279e761b772SMax Krasnyansky 	}
280e761b772SMax Krasnyansky 
2812b17fa50SRusty Russell 	set_cpu_active(cpu, false);
282e761b772SMax Krasnyansky 
28339b0fad7SMax Krasnyansky 	/*
28439b0fad7SMax Krasnyansky 	 * Make sure the all cpus did the reschedule and are not
285e0b582ecSRusty Russell 	 * using stale version of the cpu_active_mask.
28639b0fad7SMax Krasnyansky 	 * This is not strictly necessary becuase stop_machine()
28739b0fad7SMax Krasnyansky 	 * that we run down the line already provides the required
28839b0fad7SMax Krasnyansky 	 * synchronization. But it's really a side effect and we do not
28939b0fad7SMax Krasnyansky 	 * want to depend on the innards of the stop_machine here.
29039b0fad7SMax Krasnyansky 	 */
29139b0fad7SMax Krasnyansky 	synchronize_sched();
29239b0fad7SMax Krasnyansky 
2938bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
294e3920fb4SRafael J. Wysocki 
295e761b772SMax Krasnyansky 	if (cpu_online(cpu))
2962b17fa50SRusty Russell 		set_cpu_active(cpu, true);
297e761b772SMax Krasnyansky 
298e761b772SMax Krasnyansky out:
299d221938cSGautham R Shenoy 	cpu_maps_update_done();
3009ea09af3SHeiko Carstens 	stop_machine_destroy();
3011da177e4SLinus Torvalds 	return err;
3021da177e4SLinus Torvalds }
303b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
3041da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
3051da177e4SLinus Torvalds 
306e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
3078bb78442SRafael J. Wysocki static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
3081da177e4SLinus Torvalds {
309baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
3101da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
3118bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
3121da177e4SLinus Torvalds 
313e3920fb4SRafael J. Wysocki 	if (cpu_online(cpu) || !cpu_present(cpu))
314e3920fb4SRafael J. Wysocki 		return -EINVAL;
31590d45d17SAshok Raj 
316d221938cSGautham R Shenoy 	cpu_hotplug_begin();
3178bb78442SRafael J. Wysocki 	ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
318baaca49fSGautham R Shenoy 							-1, &nr_calls);
3191da177e4SLinus Torvalds 	if (ret == NOTIFY_BAD) {
320a0d8cdb6SAkinobu Mita 		nr_calls--;
3211da177e4SLinus Torvalds 		printk("%s: attempt to bring up CPU %u failed\n",
322af1f16d0SHarvey Harrison 				__func__, cpu);
3231da177e4SLinus Torvalds 		ret = -EINVAL;
3241da177e4SLinus Torvalds 		goto out_notify;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
3281da177e4SLinus Torvalds 	ret = __cpu_up(cpu);
3291da177e4SLinus Torvalds 	if (ret != 0)
3301da177e4SLinus Torvalds 		goto out_notify;
3316978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
3321da177e4SLinus Torvalds 
3332b17fa50SRusty Russell 	set_cpu_active(cpu, true);
334279ef6bbSDmitry Adamushko 
3351da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
3368bb78442SRafael J. Wysocki 	raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds out_notify:
3391da177e4SLinus Torvalds 	if (ret != 0)
340baaca49fSGautham R Shenoy 		__raw_notifier_call_chain(&cpu_chain,
3418bb78442SRafael J. Wysocki 				CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
342d221938cSGautham R Shenoy 	cpu_hotplug_done();
343e3920fb4SRafael J. Wysocki 
3441da177e4SLinus Torvalds 	return ret;
3451da177e4SLinus Torvalds }
346e3920fb4SRafael J. Wysocki 
347b282b6f8SGautham R Shenoy int __cpuinit cpu_up(unsigned int cpu)
348e3920fb4SRafael J. Wysocki {
349e3920fb4SRafael J. Wysocki 	int err = 0;
350e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
35173e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "can't online cpu %d because it is not "
35273e753a5SKAMEZAWA Hiroyuki 			"configured as may-hotadd at boot time\n", cpu);
3533ee1062bSHeiko Carstens #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
35473e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "please check additional_cpus= boot "
35573e753a5SKAMEZAWA Hiroyuki 				"parameter\n");
35673e753a5SKAMEZAWA Hiroyuki #endif
35773e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
35873e753a5SKAMEZAWA Hiroyuki 	}
359e3920fb4SRafael J. Wysocki 
360d221938cSGautham R Shenoy 	cpu_maps_update_begin();
361e761b772SMax Krasnyansky 
362e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
363e3920fb4SRafael J. Wysocki 		err = -EBUSY;
364e761b772SMax Krasnyansky 		goto out;
365e761b772SMax Krasnyansky 	}
366e761b772SMax Krasnyansky 
3678bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
368e3920fb4SRafael J. Wysocki 
369e761b772SMax Krasnyansky out:
370d221938cSGautham R Shenoy 	cpu_maps_update_done();
371e3920fb4SRafael J. Wysocki 	return err;
372e3920fb4SRafael J. Wysocki }
373e3920fb4SRafael J. Wysocki 
374f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
375e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
376e3920fb4SRafael J. Wysocki 
377e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
378e3920fb4SRafael J. Wysocki {
379a0e280e0SHeiko Carstens 	int cpu, first_cpu, error;
380e3920fb4SRafael J. Wysocki 
381a0e280e0SHeiko Carstens 	error = stop_machine_create();
382a0e280e0SHeiko Carstens 	if (error)
383a0e280e0SHeiko Carstens 		return error;
384d221938cSGautham R Shenoy 	cpu_maps_update_begin();
385e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
386e3920fb4SRafael J. Wysocki 	/* We take down all of the non-boot CPUs in one shot to avoid races
387e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
388e3920fb4SRafael J. Wysocki 	 */
389e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
390e3920fb4SRafael J. Wysocki 	printk("Disabling non-boot CPUs ...\n");
391e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
392e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
393e3920fb4SRafael J. Wysocki 			continue;
3948bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
395e3920fb4SRafael J. Wysocki 		if (!error) {
396e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
397e3920fb4SRafael J. Wysocki 			printk("CPU%d is down\n", cpu);
398e3920fb4SRafael J. Wysocki 		} else {
399e3920fb4SRafael J. Wysocki 			printk(KERN_ERR "Error taking CPU%d down: %d\n",
400e3920fb4SRafael J. Wysocki 				cpu, error);
401e3920fb4SRafael J. Wysocki 			break;
402e3920fb4SRafael J. Wysocki 		}
403e3920fb4SRafael J. Wysocki 	}
404e3920fb4SRafael J. Wysocki 	if (!error) {
405e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
406e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
407e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
408e3920fb4SRafael J. Wysocki 	} else {
409e1d9fd2eSIngo Molnar 		printk(KERN_ERR "Non-boot CPUs are not disabled\n");
410e3920fb4SRafael J. Wysocki 	}
411d221938cSGautham R Shenoy 	cpu_maps_update_done();
412a0e280e0SHeiko Carstens 	stop_machine_destroy();
413e3920fb4SRafael J. Wysocki 	return error;
414e3920fb4SRafael J. Wysocki }
415e3920fb4SRafael J. Wysocki 
416fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
417e3920fb4SRafael J. Wysocki {
418e3920fb4SRafael J. Wysocki 	int cpu, error;
419e3920fb4SRafael J. Wysocki 
420e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
421d221938cSGautham R Shenoy 	cpu_maps_update_begin();
422e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
423e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
4241d64b9cbSRafael J. Wysocki 		goto out;
425e3920fb4SRafael J. Wysocki 
426e3920fb4SRafael J. Wysocki 	printk("Enabling non-boot CPUs ...\n");
427e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
4288bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
429e3920fb4SRafael J. Wysocki 		if (!error) {
430e3920fb4SRafael J. Wysocki 			printk("CPU%d is up\n", cpu);
431e3920fb4SRafael J. Wysocki 			continue;
432e3920fb4SRafael J. Wysocki 		}
4331d64b9cbSRafael J. Wysocki 		printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
434e3920fb4SRafael J. Wysocki 	}
435e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
4361d64b9cbSRafael J. Wysocki out:
437d221938cSGautham R Shenoy 	cpu_maps_update_done();
438e3920fb4SRafael J. Wysocki }
439e0b582ecSRusty Russell 
440e0b582ecSRusty Russell static int alloc_frozen_cpus(void)
441e0b582ecSRusty Russell {
442e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
443e0b582ecSRusty Russell 		return -ENOMEM;
444e0b582ecSRusty Russell 	return 0;
445e0b582ecSRusty Russell }
446e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
447f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
44868f4f1ecSMax Krasnyansky 
449e545a614SManfred Spraul /**
450e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
451e545a614SManfred Spraul  * @cpu: cpu that just started
452e545a614SManfred Spraul  *
453e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
454e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
455e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
456e545a614SManfred Spraul  */
45784196414SAl Viro void __cpuinit notify_cpu_starting(unsigned int cpu)
458e545a614SManfred Spraul {
459e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
460e545a614SManfred Spraul 
461e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
462e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
463e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
464e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
465e545a614SManfred Spraul 	raw_notifier_call_chain(&cpu_chain, val, (void *)(long)cpu);
466e545a614SManfred Spraul }
467e545a614SManfred Spraul 
46868f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
469b8d317d1SMike Travis 
470e56b3bc7SLinus Torvalds /*
471e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
472e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
473e56b3bc7SLinus Torvalds  *
474e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
475e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
476e56b3bc7SLinus Torvalds  */
477b8d317d1SMike Travis 
478e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
479e56b3bc7SLinus Torvalds #define MASK_DECLARE_1(x)	[x+1][0] = 1UL << (x)
480e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
481e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
482e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
483b8d317d1SMike Travis 
484e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
485b8d317d1SMike Travis 
486e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
487e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
488e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
489e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
490e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
491b8d317d1SMike Travis #endif
492b8d317d1SMike Travis };
493e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
4942d3854a3SRusty Russell 
4952d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
4962d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
497b3199c02SRusty Russell 
498b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
499b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
500b3199c02SRusty Russell 	= CPU_BITS_ALL;
501b3199c02SRusty Russell #else
502b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
503b3199c02SRusty Russell #endif
504b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
505b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
506b3199c02SRusty Russell 
507b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
508b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
509b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
510b3199c02SRusty Russell 
511b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
512b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
513b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
514b3199c02SRusty Russell 
515b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
516b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
517b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
5183fa41520SRusty Russell 
5193fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
5203fa41520SRusty Russell {
5213fa41520SRusty Russell 	if (possible)
5223fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
5233fa41520SRusty Russell 	else
5243fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
5253fa41520SRusty Russell }
5263fa41520SRusty Russell 
5273fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
5283fa41520SRusty Russell {
5293fa41520SRusty Russell 	if (present)
5303fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
5313fa41520SRusty Russell 	else
5323fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
5333fa41520SRusty Russell }
5343fa41520SRusty Russell 
5353fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
5363fa41520SRusty Russell {
5373fa41520SRusty Russell 	if (online)
5383fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
5393fa41520SRusty Russell 	else
5403fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
5413fa41520SRusty Russell }
5423fa41520SRusty Russell 
5433fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
5443fa41520SRusty Russell {
5453fa41520SRusty Russell 	if (active)
5463fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
5473fa41520SRusty Russell 	else
5483fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
5493fa41520SRusty Russell }
5503fa41520SRusty Russell 
5513fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
5523fa41520SRusty Russell {
5533fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
5543fa41520SRusty Russell }
5553fa41520SRusty Russell 
5563fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
5573fa41520SRusty Russell {
5583fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
5593fa41520SRusty Russell }
5603fa41520SRusty Russell 
5613fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
5623fa41520SRusty Russell {
5633fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
5643fa41520SRusty Russell }
565