xref: /linux-6.15/kernel/cpu.c (revision e6bde73b)
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>
175a0e3ad6STejun Heo #include <linux/gfp.h>
181da177e4SLinus Torvalds 
1998a79d6aSRusty Russell #ifdef CONFIG_SMP
20b3199c02SRusty Russell /* Serializes the updates to cpu_online_mask, cpu_present_mask */
21aa953877SLinus Torvalds static DEFINE_MUTEX(cpu_add_remove_lock);
221da177e4SLinus Torvalds 
23bd5349cfSNeil Brown static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
241da177e4SLinus Torvalds 
25e3920fb4SRafael J. Wysocki /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
26e3920fb4SRafael J. Wysocki  * Should always be manipulated under cpu_add_remove_lock
27e3920fb4SRafael J. Wysocki  */
28e3920fb4SRafael J. Wysocki static int cpu_hotplug_disabled;
29e3920fb4SRafael J. Wysocki 
30d221938cSGautham R Shenoy static struct {
31d221938cSGautham R Shenoy 	struct task_struct *active_writer;
32d221938cSGautham R Shenoy 	struct mutex lock; /* Synchronizes accesses to refcount, */
33d221938cSGautham R Shenoy 	/*
34d221938cSGautham R Shenoy 	 * Also blocks the new readers during
35d221938cSGautham R Shenoy 	 * an ongoing cpu hotplug operation.
36d221938cSGautham R Shenoy 	 */
37d221938cSGautham R Shenoy 	int refcount;
3831950eb6SLinus Torvalds } cpu_hotplug = {
3931950eb6SLinus Torvalds 	.active_writer = NULL,
4031950eb6SLinus Torvalds 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
4131950eb6SLinus Torvalds 	.refcount = 0,
4231950eb6SLinus Torvalds };
43d221938cSGautham R Shenoy 
44d221938cSGautham R Shenoy #ifdef CONFIG_HOTPLUG_CPU
45a9d9baa1SAshok Raj 
4686ef5c9aSGautham R Shenoy void get_online_cpus(void)
47a9d9baa1SAshok Raj {
48d221938cSGautham R Shenoy 	might_sleep();
49d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
50aa953877SLinus Torvalds 		return;
51d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
52d221938cSGautham R Shenoy 	cpu_hotplug.refcount++;
53d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
54d221938cSGautham R Shenoy 
55a9d9baa1SAshok Raj }
5686ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(get_online_cpus);
57a9d9baa1SAshok Raj 
5886ef5c9aSGautham R Shenoy void put_online_cpus(void)
59a9d9baa1SAshok Raj {
60d221938cSGautham R Shenoy 	if (cpu_hotplug.active_writer == current)
61aa953877SLinus Torvalds 		return;
62d221938cSGautham R Shenoy 	mutex_lock(&cpu_hotplug.lock);
63d2ba7e2aSOleg Nesterov 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
64d2ba7e2aSOleg Nesterov 		wake_up_process(cpu_hotplug.active_writer);
65d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
66d221938cSGautham R Shenoy 
67a9d9baa1SAshok Raj }
6886ef5c9aSGautham R Shenoy EXPORT_SYMBOL_GPL(put_online_cpus);
69a9d9baa1SAshok Raj 
70a9d9baa1SAshok Raj #endif	/* CONFIG_HOTPLUG_CPU */
7190d45d17SAshok Raj 
72d221938cSGautham R Shenoy /*
73d221938cSGautham R Shenoy  * The following two API's must be used when attempting
74b3199c02SRusty Russell  * to serialize the updates to cpu_online_mask, cpu_present_mask.
75d221938cSGautham R Shenoy  */
76d221938cSGautham R Shenoy void cpu_maps_update_begin(void)
77d221938cSGautham R Shenoy {
78d221938cSGautham R Shenoy 	mutex_lock(&cpu_add_remove_lock);
79d221938cSGautham R Shenoy }
80d221938cSGautham R Shenoy 
81d221938cSGautham R Shenoy void cpu_maps_update_done(void)
82d221938cSGautham R Shenoy {
83d221938cSGautham R Shenoy 	mutex_unlock(&cpu_add_remove_lock);
84d221938cSGautham R Shenoy }
85d221938cSGautham R Shenoy 
86d221938cSGautham R Shenoy /*
87d221938cSGautham R Shenoy  * This ensures that the hotplug operation can begin only when the
88d221938cSGautham R Shenoy  * refcount goes to zero.
89d221938cSGautham R Shenoy  *
90d221938cSGautham R Shenoy  * Note that during a cpu-hotplug operation, the new readers, if any,
91d221938cSGautham R Shenoy  * will be blocked by the cpu_hotplug.lock
92d221938cSGautham R Shenoy  *
93d2ba7e2aSOleg Nesterov  * Since cpu_hotplug_begin() is always called after invoking
94d2ba7e2aSOleg Nesterov  * cpu_maps_update_begin(), we can be sure that only one writer is active.
95d221938cSGautham R Shenoy  *
96d221938cSGautham R Shenoy  * Note that theoretically, there is a possibility of a livelock:
97d221938cSGautham R Shenoy  * - Refcount goes to zero, last reader wakes up the sleeping
98d221938cSGautham R Shenoy  *   writer.
99d221938cSGautham R Shenoy  * - Last reader unlocks the cpu_hotplug.lock.
100d221938cSGautham R Shenoy  * - A new reader arrives at this moment, bumps up the refcount.
101d221938cSGautham R Shenoy  * - The writer acquires the cpu_hotplug.lock finds the refcount
102d221938cSGautham R Shenoy  *   non zero and goes to sleep again.
103d221938cSGautham R Shenoy  *
104d221938cSGautham R Shenoy  * However, this is very difficult to achieve in practice since
10586ef5c9aSGautham R Shenoy  * get_online_cpus() not an api which is called all that often.
106d221938cSGautham R Shenoy  *
107d221938cSGautham R Shenoy  */
108d221938cSGautham R Shenoy static void cpu_hotplug_begin(void)
109d221938cSGautham R Shenoy {
110d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = current;
111d2ba7e2aSOleg Nesterov 
112d2ba7e2aSOleg Nesterov 	for (;;) {
113d2ba7e2aSOleg Nesterov 		mutex_lock(&cpu_hotplug.lock);
114d2ba7e2aSOleg Nesterov 		if (likely(!cpu_hotplug.refcount))
115d2ba7e2aSOleg Nesterov 			break;
116d2ba7e2aSOleg Nesterov 		__set_current_state(TASK_UNINTERRUPTIBLE);
117d221938cSGautham R Shenoy 		mutex_unlock(&cpu_hotplug.lock);
118d221938cSGautham R Shenoy 		schedule();
119d221938cSGautham R Shenoy 	}
120d221938cSGautham R Shenoy }
121d221938cSGautham R Shenoy 
122d221938cSGautham R Shenoy static void cpu_hotplug_done(void)
123d221938cSGautham R Shenoy {
124d221938cSGautham R Shenoy 	cpu_hotplug.active_writer = NULL;
125d221938cSGautham R Shenoy 	mutex_unlock(&cpu_hotplug.lock);
126d221938cSGautham R Shenoy }
1271da177e4SLinus Torvalds /* Need to know about CPUs going up/down? */
128f7b16c10SSam Ravnborg int __ref register_cpu_notifier(struct notifier_block *nb)
1291da177e4SLinus Torvalds {
130bd5349cfSNeil Brown 	int ret;
131d221938cSGautham R Shenoy 	cpu_maps_update_begin();
132bd5349cfSNeil Brown 	ret = raw_notifier_chain_register(&cpu_chain, nb);
133d221938cSGautham R Shenoy 	cpu_maps_update_done();
134bd5349cfSNeil Brown 	return ret;
1351da177e4SLinus Torvalds }
13665edc68cSChandra Seetharaman 
137e9fb7631SAkinobu Mita static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
138e9fb7631SAkinobu Mita 			int *nr_calls)
139e9fb7631SAkinobu Mita {
140*e6bde73bSAkinobu Mita 	int ret;
141*e6bde73bSAkinobu Mita 
142*e6bde73bSAkinobu Mita 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
143e9fb7631SAkinobu Mita 					nr_calls);
144*e6bde73bSAkinobu Mita 
145*e6bde73bSAkinobu Mita 	return notifier_to_errno(ret);
146e9fb7631SAkinobu Mita }
147e9fb7631SAkinobu Mita 
148e9fb7631SAkinobu Mita static int cpu_notify(unsigned long val, void *v)
149e9fb7631SAkinobu Mita {
150e9fb7631SAkinobu Mita 	return __cpu_notify(val, v, -1, NULL);
151e9fb7631SAkinobu Mita }
152e9fb7631SAkinobu Mita 
153e9fb7631SAkinobu Mita static void cpu_notify_nofail(unsigned long val, void *v)
154e9fb7631SAkinobu Mita {
155e9fb7631SAkinobu Mita 	int err;
156e9fb7631SAkinobu Mita 
157e9fb7631SAkinobu Mita 	err = cpu_notify(val, v);
158*e6bde73bSAkinobu Mita 	BUG_ON(err);
159e9fb7631SAkinobu Mita }
160e9fb7631SAkinobu Mita 
16165edc68cSChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
16265edc68cSChandra Seetharaman 
1631da177e4SLinus Torvalds EXPORT_SYMBOL(register_cpu_notifier);
1641da177e4SLinus Torvalds 
1659647155fSSam Ravnborg void __ref unregister_cpu_notifier(struct notifier_block *nb)
1661da177e4SLinus Torvalds {
167d221938cSGautham R Shenoy 	cpu_maps_update_begin();
168bd5349cfSNeil Brown 	raw_notifier_chain_unregister(&cpu_chain, nb);
169d221938cSGautham R Shenoy 	cpu_maps_update_done();
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_cpu_notifier);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static inline void check_for_tasks(int cpu)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct task_struct *p;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
1781da177e4SLinus Torvalds 	for_each_process(p) {
17911854247SPeter Zijlstra 		if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
1801da177e4SLinus Torvalds 		    (!cputime_eq(p->utime, cputime_zero) ||
1811da177e4SLinus Torvalds 		     !cputime_eq(p->stime, cputime_zero)))
1829d3cfc4cSFrans Pop 			printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d "
1839d3cfc4cSFrans Pop 				"(state = %ld, flags = %x)\n",
184ba25f9dcSPavel Emelyanov 				p->comm, task_pid_nr(p), cpu,
185ba25f9dcSPavel Emelyanov 				p->state, p->flags);
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
190db912f96SAvi Kivity struct take_cpu_down_param {
1916a1bdc1bSOleg Nesterov 	struct task_struct *caller;
192db912f96SAvi Kivity 	unsigned long mod;
193db912f96SAvi Kivity 	void *hcpu;
194db912f96SAvi Kivity };
195db912f96SAvi Kivity 
1961da177e4SLinus Torvalds /* Take this CPU down. */
197514a20a5SSam Ravnborg static int __ref take_cpu_down(void *_param)
1981da177e4SLinus Torvalds {
199db912f96SAvi Kivity 	struct take_cpu_down_param *param = _param;
2006a1bdc1bSOleg Nesterov 	unsigned int cpu = (unsigned long)param->hcpu;
2011da177e4SLinus Torvalds 	int err;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	/* Ensure this CPU doesn't handle any more interrupts. */
2041da177e4SLinus Torvalds 	err = __cpu_disable();
2051da177e4SLinus Torvalds 	if (err < 0)
206f3705136SZwane Mwaikambo 		return err;
207f3705136SZwane Mwaikambo 
208e9fb7631SAkinobu Mita 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
2093ba35573SManfred Spraul 
2106a1bdc1bSOleg Nesterov 	if (task_cpu(param->caller) == cpu)
2116a1bdc1bSOleg Nesterov 		move_task_off_dead_cpu(cpu, param->caller);
2121da177e4SLinus Torvalds 	/* Force idle task to run as soon as we yield: it should
2131da177e4SLinus Torvalds 	   immediately notice cpu is offline and die quickly. */
2141da177e4SLinus Torvalds 	sched_idle_next();
215f3705136SZwane Mwaikambo 	return 0;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
218e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
219514a20a5SSam Ravnborg static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
2201da177e4SLinus Torvalds {
221e7407dccSHeiko Carstens 	int err, nr_calls = 0;
222e7407dccSHeiko Carstens 	void *hcpu = (void *)(long)cpu;
2238bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
224db912f96SAvi Kivity 	struct take_cpu_down_param tcd_param = {
2256a1bdc1bSOleg Nesterov 		.caller = current,
226db912f96SAvi Kivity 		.mod = mod,
227db912f96SAvi Kivity 		.hcpu = hcpu,
228db912f96SAvi Kivity 	};
2291da177e4SLinus Torvalds 
230e3920fb4SRafael J. Wysocki 	if (num_online_cpus() == 1)
231e3920fb4SRafael J. Wysocki 		return -EBUSY;
2321da177e4SLinus Torvalds 
233e3920fb4SRafael J. Wysocki 	if (!cpu_online(cpu))
234e3920fb4SRafael J. Wysocki 		return -EINVAL;
2351da177e4SLinus Torvalds 
236d221938cSGautham R Shenoy 	cpu_hotplug_begin();
2379ee349adSXiaotian Feng 	set_cpu_active(cpu, false);
238e9fb7631SAkinobu Mita 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
239*e6bde73bSAkinobu Mita 	if (err) {
2406ad4c188SPeter Zijlstra 		set_cpu_active(cpu, true);
2416ad4c188SPeter Zijlstra 
242a0d8cdb6SAkinobu Mita 		nr_calls--;
243e9fb7631SAkinobu Mita 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
2441da177e4SLinus Torvalds 		printk("%s: attempt to take down CPU %u failed\n",
245af1f16d0SHarvey Harrison 				__func__, cpu);
246baaca49fSGautham R Shenoy 		goto out_release;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
249e0b582ecSRusty Russell 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
25004321587SRusty Russell 	if (err) {
2516ad4c188SPeter Zijlstra 		set_cpu_active(cpu, true);
2521da177e4SLinus Torvalds 		/* CPU didn't die: tell everyone.  Can't complain. */
253e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
2541da177e4SLinus Torvalds 
2556a1bdc1bSOleg Nesterov 		goto out_release;
2561da177e4SLinus Torvalds 	}
25704321587SRusty Russell 	BUG_ON(cpu_online(cpu));
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* Wait for it to sleep (leaving idle task). */
2601da177e4SLinus Torvalds 	while (!idle_cpu(cpu))
2611da177e4SLinus Torvalds 		yield();
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* This actually kills the CPU. */
2641da177e4SLinus Torvalds 	__cpu_die(cpu);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* CPU is completely dead: tell everyone.  Too late to complain. */
267e9fb7631SAkinobu Mita 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	check_for_tasks(cpu);
2701da177e4SLinus Torvalds 
271baaca49fSGautham R Shenoy out_release:
272d221938cSGautham R Shenoy 	cpu_hotplug_done();
273e9fb7631SAkinobu Mita 	if (!err)
274e9fb7631SAkinobu Mita 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
275e3920fb4SRafael J. Wysocki 	return err;
276e3920fb4SRafael J. Wysocki }
277e3920fb4SRafael J. Wysocki 
278514a20a5SSam Ravnborg int __ref cpu_down(unsigned int cpu)
279e3920fb4SRafael J. Wysocki {
2809ea09af3SHeiko Carstens 	int err;
281e3920fb4SRafael J. Wysocki 
282d221938cSGautham R Shenoy 	cpu_maps_update_begin();
283e761b772SMax Krasnyansky 
284e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
285e3920fb4SRafael J. Wysocki 		err = -EBUSY;
286e761b772SMax Krasnyansky 		goto out;
287e761b772SMax Krasnyansky 	}
288e761b772SMax Krasnyansky 
2898bb78442SRafael J. Wysocki 	err = _cpu_down(cpu, 0);
290e3920fb4SRafael J. Wysocki 
291e761b772SMax Krasnyansky out:
292d221938cSGautham R Shenoy 	cpu_maps_update_done();
2931da177e4SLinus Torvalds 	return err;
2941da177e4SLinus Torvalds }
295b62b8ef9SZhang Rui EXPORT_SYMBOL(cpu_down);
2961da177e4SLinus Torvalds #endif /*CONFIG_HOTPLUG_CPU*/
2971da177e4SLinus Torvalds 
298e3920fb4SRafael J. Wysocki /* Requires cpu_add_remove_lock to be held */
2998bb78442SRafael J. Wysocki static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
3001da177e4SLinus Torvalds {
301baaca49fSGautham R Shenoy 	int ret, nr_calls = 0;
3021da177e4SLinus Torvalds 	void *hcpu = (void *)(long)cpu;
3038bb78442SRafael J. Wysocki 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
3041da177e4SLinus Torvalds 
305e3920fb4SRafael J. Wysocki 	if (cpu_online(cpu) || !cpu_present(cpu))
306e3920fb4SRafael J. Wysocki 		return -EINVAL;
30790d45d17SAshok Raj 
308d221938cSGautham R Shenoy 	cpu_hotplug_begin();
309e9fb7631SAkinobu Mita 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
310*e6bde73bSAkinobu Mita 	if (ret) {
311a0d8cdb6SAkinobu Mita 		nr_calls--;
3121da177e4SLinus Torvalds 		printk("%s: attempt to bring up CPU %u failed\n",
313af1f16d0SHarvey Harrison 				__func__, cpu);
3141da177e4SLinus Torvalds 		goto out_notify;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/* Arch-specific enabling code. */
3181da177e4SLinus Torvalds 	ret = __cpu_up(cpu);
3191da177e4SLinus Torvalds 	if (ret != 0)
3201da177e4SLinus Torvalds 		goto out_notify;
3216978c705SEric Sesterhenn 	BUG_ON(!cpu_online(cpu));
3221da177e4SLinus Torvalds 
3232b17fa50SRusty Russell 	set_cpu_active(cpu, true);
324279ef6bbSDmitry Adamushko 
3251da177e4SLinus Torvalds 	/* Now call notifier in preparation. */
326e9fb7631SAkinobu Mita 	cpu_notify(CPU_ONLINE | mod, hcpu);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds out_notify:
3291da177e4SLinus Torvalds 	if (ret != 0)
330e9fb7631SAkinobu Mita 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
331d221938cSGautham R Shenoy 	cpu_hotplug_done();
332e3920fb4SRafael J. Wysocki 
3331da177e4SLinus Torvalds 	return ret;
3341da177e4SLinus Torvalds }
335e3920fb4SRafael J. Wysocki 
336b282b6f8SGautham R Shenoy int __cpuinit cpu_up(unsigned int cpu)
337e3920fb4SRafael J. Wysocki {
338e3920fb4SRafael J. Wysocki 	int err = 0;
339cf23422bSminskey guo 
340cf23422bSminskey guo #ifdef	CONFIG_MEMORY_HOTPLUG
341cf23422bSminskey guo 	int nid;
342cf23422bSminskey guo 	pg_data_t	*pgdat;
343cf23422bSminskey guo #endif
344cf23422bSminskey guo 
345e0b582ecSRusty Russell 	if (!cpu_possible(cpu)) {
34673e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "can't online cpu %d because it is not "
34773e753a5SKAMEZAWA Hiroyuki 			"configured as may-hotadd at boot time\n", cpu);
34887d5e023SChen Gong #if defined(CONFIG_IA64)
34973e753a5SKAMEZAWA Hiroyuki 		printk(KERN_ERR "please check additional_cpus= boot "
35073e753a5SKAMEZAWA Hiroyuki 				"parameter\n");
35173e753a5SKAMEZAWA Hiroyuki #endif
35273e753a5SKAMEZAWA Hiroyuki 		return -EINVAL;
35373e753a5SKAMEZAWA Hiroyuki 	}
354e3920fb4SRafael J. Wysocki 
355cf23422bSminskey guo #ifdef	CONFIG_MEMORY_HOTPLUG
356cf23422bSminskey guo 	nid = cpu_to_node(cpu);
357cf23422bSminskey guo 	if (!node_online(nid)) {
358cf23422bSminskey guo 		err = mem_online_node(nid);
359cf23422bSminskey guo 		if (err)
360cf23422bSminskey guo 			return err;
361cf23422bSminskey guo 	}
362cf23422bSminskey guo 
363cf23422bSminskey guo 	pgdat = NODE_DATA(nid);
364cf23422bSminskey guo 	if (!pgdat) {
365cf23422bSminskey guo 		printk(KERN_ERR
366cf23422bSminskey guo 			"Can't online cpu %d due to NULL pgdat\n", cpu);
367cf23422bSminskey guo 		return -ENOMEM;
368cf23422bSminskey guo 	}
369cf23422bSminskey guo 
3704eaf3f64SHaicheng Li 	if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
3714eaf3f64SHaicheng Li 		mutex_lock(&zonelists_mutex);
3721f522509SHaicheng Li 		build_all_zonelists(NULL);
3734eaf3f64SHaicheng Li 		mutex_unlock(&zonelists_mutex);
3744eaf3f64SHaicheng Li 	}
375cf23422bSminskey guo #endif
376cf23422bSminskey guo 
377d221938cSGautham R Shenoy 	cpu_maps_update_begin();
378e761b772SMax Krasnyansky 
379e761b772SMax Krasnyansky 	if (cpu_hotplug_disabled) {
380e3920fb4SRafael J. Wysocki 		err = -EBUSY;
381e761b772SMax Krasnyansky 		goto out;
382e761b772SMax Krasnyansky 	}
383e761b772SMax Krasnyansky 
3848bb78442SRafael J. Wysocki 	err = _cpu_up(cpu, 0);
385e3920fb4SRafael J. Wysocki 
386e761b772SMax Krasnyansky out:
387d221938cSGautham R Shenoy 	cpu_maps_update_done();
388e3920fb4SRafael J. Wysocki 	return err;
389e3920fb4SRafael J. Wysocki }
390e3920fb4SRafael J. Wysocki 
391f3de4be9SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_SMP
392e0b582ecSRusty Russell static cpumask_var_t frozen_cpus;
393e3920fb4SRafael J. Wysocki 
394e3920fb4SRafael J. Wysocki int disable_nonboot_cpus(void)
395e3920fb4SRafael J. Wysocki {
39669575d38SShane Wang 	int cpu, first_cpu, error;
397e3920fb4SRafael J. Wysocki 
398d221938cSGautham R Shenoy 	cpu_maps_update_begin();
399e0b582ecSRusty Russell 	first_cpu = cpumask_first(cpu_online_mask);
4009ee349adSXiaotian Feng 	/*
4019ee349adSXiaotian Feng 	 * We take down all of the non-boot CPUs in one shot to avoid races
402e3920fb4SRafael J. Wysocki 	 * with the userspace trying to use the CPU hotplug at the same time
403e3920fb4SRafael J. Wysocki 	 */
404e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
4056ad4c188SPeter Zijlstra 
406e3920fb4SRafael J. Wysocki 	printk("Disabling non-boot CPUs ...\n");
407e3920fb4SRafael J. Wysocki 	for_each_online_cpu(cpu) {
408e3920fb4SRafael J. Wysocki 		if (cpu == first_cpu)
409e3920fb4SRafael J. Wysocki 			continue;
4108bb78442SRafael J. Wysocki 		error = _cpu_down(cpu, 1);
411feae3203SMike Travis 		if (!error)
412e0b582ecSRusty Russell 			cpumask_set_cpu(cpu, frozen_cpus);
413feae3203SMike Travis 		else {
414e3920fb4SRafael J. Wysocki 			printk(KERN_ERR "Error taking CPU%d down: %d\n",
415e3920fb4SRafael J. Wysocki 				cpu, error);
416e3920fb4SRafael J. Wysocki 			break;
417e3920fb4SRafael J. Wysocki 		}
418e3920fb4SRafael J. Wysocki 	}
41986886e55SJoseph Cihula 
420e3920fb4SRafael J. Wysocki 	if (!error) {
421e3920fb4SRafael J. Wysocki 		BUG_ON(num_online_cpus() > 1);
422e3920fb4SRafael J. Wysocki 		/* Make sure the CPUs won't be enabled by someone else */
423e3920fb4SRafael J. Wysocki 		cpu_hotplug_disabled = 1;
424e3920fb4SRafael J. Wysocki 	} else {
425e1d9fd2eSIngo Molnar 		printk(KERN_ERR "Non-boot CPUs are not disabled\n");
426e3920fb4SRafael J. Wysocki 	}
427d221938cSGautham R Shenoy 	cpu_maps_update_done();
428e3920fb4SRafael J. Wysocki 	return error;
429e3920fb4SRafael J. Wysocki }
430e3920fb4SRafael J. Wysocki 
431d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_begin(void)
432d0af9eedSSuresh Siddha {
433d0af9eedSSuresh Siddha }
434d0af9eedSSuresh Siddha 
435d0af9eedSSuresh Siddha void __weak arch_enable_nonboot_cpus_end(void)
436d0af9eedSSuresh Siddha {
437d0af9eedSSuresh Siddha }
438d0af9eedSSuresh Siddha 
439fa7303e2SSam Ravnborg void __ref enable_nonboot_cpus(void)
440e3920fb4SRafael J. Wysocki {
441e3920fb4SRafael J. Wysocki 	int cpu, error;
442e3920fb4SRafael J. Wysocki 
443e3920fb4SRafael J. Wysocki 	/* Allow everyone to use the CPU hotplug again */
444d221938cSGautham R Shenoy 	cpu_maps_update_begin();
445e3920fb4SRafael J. Wysocki 	cpu_hotplug_disabled = 0;
446e0b582ecSRusty Russell 	if (cpumask_empty(frozen_cpus))
4471d64b9cbSRafael J. Wysocki 		goto out;
448e3920fb4SRafael J. Wysocki 
449e3920fb4SRafael J. Wysocki 	printk("Enabling non-boot CPUs ...\n");
450d0af9eedSSuresh Siddha 
451d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_begin();
452d0af9eedSSuresh Siddha 
453e0b582ecSRusty Russell 	for_each_cpu(cpu, frozen_cpus) {
4548bb78442SRafael J. Wysocki 		error = _cpu_up(cpu, 1);
455e3920fb4SRafael J. Wysocki 		if (!error) {
456e3920fb4SRafael J. Wysocki 			printk("CPU%d is up\n", cpu);
457e3920fb4SRafael J. Wysocki 			continue;
458e3920fb4SRafael J. Wysocki 		}
4591d64b9cbSRafael J. Wysocki 		printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
460e3920fb4SRafael J. Wysocki 	}
461d0af9eedSSuresh Siddha 
462d0af9eedSSuresh Siddha 	arch_enable_nonboot_cpus_end();
463d0af9eedSSuresh Siddha 
464e0b582ecSRusty Russell 	cpumask_clear(frozen_cpus);
4651d64b9cbSRafael J. Wysocki out:
466d221938cSGautham R Shenoy 	cpu_maps_update_done();
467e3920fb4SRafael J. Wysocki }
468e0b582ecSRusty Russell 
469e0b582ecSRusty Russell static int alloc_frozen_cpus(void)
470e0b582ecSRusty Russell {
471e0b582ecSRusty Russell 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
472e0b582ecSRusty Russell 		return -ENOMEM;
473e0b582ecSRusty Russell 	return 0;
474e0b582ecSRusty Russell }
475e0b582ecSRusty Russell core_initcall(alloc_frozen_cpus);
476f3de4be9SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP_SMP */
47768f4f1ecSMax Krasnyansky 
478e545a614SManfred Spraul /**
479e545a614SManfred Spraul  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
480e545a614SManfred Spraul  * @cpu: cpu that just started
481e545a614SManfred Spraul  *
482e545a614SManfred Spraul  * This function calls the cpu_chain notifiers with CPU_STARTING.
483e545a614SManfred Spraul  * It must be called by the arch code on the new cpu, before the new cpu
484e545a614SManfred Spraul  * enables interrupts and before the "boot" cpu returns from __cpu_up().
485e545a614SManfred Spraul  */
48684196414SAl Viro void __cpuinit notify_cpu_starting(unsigned int cpu)
487e545a614SManfred Spraul {
488e545a614SManfred Spraul 	unsigned long val = CPU_STARTING;
489e545a614SManfred Spraul 
490e545a614SManfred Spraul #ifdef CONFIG_PM_SLEEP_SMP
491e0b582ecSRusty Russell 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
492e545a614SManfred Spraul 		val = CPU_STARTING_FROZEN;
493e545a614SManfred Spraul #endif /* CONFIG_PM_SLEEP_SMP */
494e9fb7631SAkinobu Mita 	cpu_notify(val, (void *)(long)cpu);
495e545a614SManfred Spraul }
496e545a614SManfred Spraul 
49768f4f1ecSMax Krasnyansky #endif /* CONFIG_SMP */
498b8d317d1SMike Travis 
499e56b3bc7SLinus Torvalds /*
500e56b3bc7SLinus Torvalds  * cpu_bit_bitmap[] is a special, "compressed" data structure that
501e56b3bc7SLinus Torvalds  * represents all NR_CPUS bits binary values of 1<<nr.
502e56b3bc7SLinus Torvalds  *
503e0b582ecSRusty Russell  * It is used by cpumask_of() to get a constant address to a CPU
504e56b3bc7SLinus Torvalds  * mask value that has a single bit set only.
505e56b3bc7SLinus Torvalds  */
506b8d317d1SMike Travis 
507e56b3bc7SLinus Torvalds /* cpu_bit_bitmap[0] is empty - so we can back into it */
508e56b3bc7SLinus Torvalds #define MASK_DECLARE_1(x)	[x+1][0] = 1UL << (x)
509e56b3bc7SLinus Torvalds #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
510e56b3bc7SLinus Torvalds #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
511e56b3bc7SLinus Torvalds #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
512b8d317d1SMike Travis 
513e56b3bc7SLinus Torvalds const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
514b8d317d1SMike Travis 
515e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
516e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
517e56b3bc7SLinus Torvalds #if BITS_PER_LONG > 32
518e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
519e56b3bc7SLinus Torvalds 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
520b8d317d1SMike Travis #endif
521b8d317d1SMike Travis };
522e56b3bc7SLinus Torvalds EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
5232d3854a3SRusty Russell 
5242d3854a3SRusty Russell const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
5252d3854a3SRusty Russell EXPORT_SYMBOL(cpu_all_bits);
526b3199c02SRusty Russell 
527b3199c02SRusty Russell #ifdef CONFIG_INIT_ALL_POSSIBLE
528b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
529b3199c02SRusty Russell 	= CPU_BITS_ALL;
530b3199c02SRusty Russell #else
531b3199c02SRusty Russell static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
532b3199c02SRusty Russell #endif
533b3199c02SRusty Russell const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
534b3199c02SRusty Russell EXPORT_SYMBOL(cpu_possible_mask);
535b3199c02SRusty Russell 
536b3199c02SRusty Russell static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
537b3199c02SRusty Russell const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
538b3199c02SRusty Russell EXPORT_SYMBOL(cpu_online_mask);
539b3199c02SRusty Russell 
540b3199c02SRusty Russell static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
541b3199c02SRusty Russell const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
542b3199c02SRusty Russell EXPORT_SYMBOL(cpu_present_mask);
543b3199c02SRusty Russell 
544b3199c02SRusty Russell static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
545b3199c02SRusty Russell const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
546b3199c02SRusty Russell EXPORT_SYMBOL(cpu_active_mask);
5473fa41520SRusty Russell 
5483fa41520SRusty Russell void set_cpu_possible(unsigned int cpu, bool possible)
5493fa41520SRusty Russell {
5503fa41520SRusty Russell 	if (possible)
5513fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
5523fa41520SRusty Russell 	else
5533fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
5543fa41520SRusty Russell }
5553fa41520SRusty Russell 
5563fa41520SRusty Russell void set_cpu_present(unsigned int cpu, bool present)
5573fa41520SRusty Russell {
5583fa41520SRusty Russell 	if (present)
5593fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
5603fa41520SRusty Russell 	else
5613fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
5623fa41520SRusty Russell }
5633fa41520SRusty Russell 
5643fa41520SRusty Russell void set_cpu_online(unsigned int cpu, bool online)
5653fa41520SRusty Russell {
5663fa41520SRusty Russell 	if (online)
5673fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
5683fa41520SRusty Russell 	else
5693fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
5703fa41520SRusty Russell }
5713fa41520SRusty Russell 
5723fa41520SRusty Russell void set_cpu_active(unsigned int cpu, bool active)
5733fa41520SRusty Russell {
5743fa41520SRusty Russell 	if (active)
5753fa41520SRusty Russell 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
5763fa41520SRusty Russell 	else
5773fa41520SRusty Russell 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
5783fa41520SRusty Russell }
5793fa41520SRusty Russell 
5803fa41520SRusty Russell void init_cpu_present(const struct cpumask *src)
5813fa41520SRusty Russell {
5823fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_present_bits), src);
5833fa41520SRusty Russell }
5843fa41520SRusty Russell 
5853fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src)
5863fa41520SRusty Russell {
5873fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
5883fa41520SRusty Russell }
5893fa41520SRusty Russell 
5903fa41520SRusty Russell void init_cpu_online(const struct cpumask *src)
5913fa41520SRusty Russell {
5923fa41520SRusty Russell 	cpumask_copy(to_cpumask(cpu_online_bits), src);
5933fa41520SRusty Russell }
594