xref: /openbmc/linux/kernel/cpu.c (revision d5cb9783536a41df9f9cba5b0a1d78047ed787f7)
1 /* CPU control.
2  * (C) 2001, 2002, 2003, 2004 Rusty Russell
3  *
4  * This code is licenced under the GPL.
5  */
6 #include <linux/proc_fs.h>
7 #include <linux/smp.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/module.h>
14 #include <linux/kthread.h>
15 #include <linux/stop_machine.h>
16 #include <asm/semaphore.h>
17 
18 /* This protects CPUs going up and down... */
19 DECLARE_MUTEX(cpucontrol);
20 EXPORT_SYMBOL_GPL(cpucontrol);
21 
22 static struct notifier_block *cpu_chain;
23 
24 /* Need to know about CPUs going up/down? */
25 int register_cpu_notifier(struct notifier_block *nb)
26 {
27 	int ret;
28 
29 	if ((ret = down_interruptible(&cpucontrol)) != 0)
30 		return ret;
31 	ret = notifier_chain_register(&cpu_chain, nb);
32 	up(&cpucontrol);
33 	return ret;
34 }
35 EXPORT_SYMBOL(register_cpu_notifier);
36 
37 void unregister_cpu_notifier(struct notifier_block *nb)
38 {
39 	down(&cpucontrol);
40 	notifier_chain_unregister(&cpu_chain, nb);
41 	up(&cpucontrol);
42 }
43 EXPORT_SYMBOL(unregister_cpu_notifier);
44 
45 #ifdef CONFIG_HOTPLUG_CPU
46 static inline void check_for_tasks(int cpu)
47 {
48 	struct task_struct *p;
49 
50 	write_lock_irq(&tasklist_lock);
51 	for_each_process(p) {
52 		if (task_cpu(p) == cpu &&
53 		    (!cputime_eq(p->utime, cputime_zero) ||
54 		     !cputime_eq(p->stime, cputime_zero)))
55 			printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
56 				(state = %ld, flags = %lx) \n",
57 				 p->comm, p->pid, cpu, p->state, p->flags);
58 	}
59 	write_unlock_irq(&tasklist_lock);
60 }
61 
62 /* Take this CPU down. */
63 static int take_cpu_down(void *unused)
64 {
65 	int err;
66 
67 	/* Ensure this CPU doesn't handle any more interrupts. */
68 	err = __cpu_disable();
69 	if (err < 0)
70 		return err;
71 
72 	/* Force idle task to run as soon as we yield: it should
73 	   immediately notice cpu is offline and die quickly. */
74 	sched_idle_next();
75 	return 0;
76 }
77 
78 int cpu_down(unsigned int cpu)
79 {
80 	int err;
81 	struct task_struct *p;
82 	cpumask_t old_allowed, tmp;
83 
84 	if ((err = lock_cpu_hotplug_interruptible()) != 0)
85 		return err;
86 
87 	if (num_online_cpus() == 1) {
88 		err = -EBUSY;
89 		goto out;
90 	}
91 
92 	if (!cpu_online(cpu)) {
93 		err = -EINVAL;
94 		goto out;
95 	}
96 
97 	err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
98 						(void *)(long)cpu);
99 	if (err == NOTIFY_BAD) {
100 		printk("%s: attempt to take down CPU %u failed\n",
101 				__FUNCTION__, cpu);
102 		err = -EINVAL;
103 		goto out;
104 	}
105 
106 	/* Ensure that we are not runnable on dying cpu */
107 	old_allowed = current->cpus_allowed;
108 	tmp = CPU_MASK_ALL;
109 	cpu_clear(cpu, tmp);
110 	set_cpus_allowed(current, tmp);
111 
112 	p = __stop_machine_run(take_cpu_down, NULL, cpu);
113 	if (IS_ERR(p)) {
114 		/* CPU didn't die: tell everyone.  Can't complain. */
115 		if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
116 				(void *)(long)cpu) == NOTIFY_BAD)
117 			BUG();
118 
119 		err = PTR_ERR(p);
120 		goto out_allowed;
121 	}
122 
123 	if (cpu_online(cpu))
124 		goto out_thread;
125 
126 	/* Wait for it to sleep (leaving idle task). */
127 	while (!idle_cpu(cpu))
128 		yield();
129 
130 	/* This actually kills the CPU. */
131 	__cpu_die(cpu);
132 
133 	/* Move it here so it can run. */
134 	kthread_bind(p, get_cpu());
135 	put_cpu();
136 
137 	/* CPU is completely dead: tell everyone.  Too late to complain. */
138 	if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
139 	    == NOTIFY_BAD)
140 		BUG();
141 
142 	check_for_tasks(cpu);
143 
144 out_thread:
145 	err = kthread_stop(p);
146 out_allowed:
147 	set_cpus_allowed(current, old_allowed);
148 out:
149 	unlock_cpu_hotplug();
150 	return err;
151 }
152 #endif /*CONFIG_HOTPLUG_CPU*/
153 
154 int __devinit cpu_up(unsigned int cpu)
155 {
156 	int ret;
157 	void *hcpu = (void *)(long)cpu;
158 
159 	if ((ret = down_interruptible(&cpucontrol)) != 0)
160 		return ret;
161 
162 	if (cpu_online(cpu) || !cpu_present(cpu)) {
163 		ret = -EINVAL;
164 		goto out;
165 	}
166 	ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
167 	if (ret == NOTIFY_BAD) {
168 		printk("%s: attempt to bring up CPU %u failed\n",
169 				__FUNCTION__, cpu);
170 		ret = -EINVAL;
171 		goto out_notify;
172 	}
173 
174 	/* Arch-specific enabling code. */
175 	ret = __cpu_up(cpu);
176 	if (ret != 0)
177 		goto out_notify;
178 	if (!cpu_online(cpu))
179 		BUG();
180 
181 	/* Now call notifier in preparation. */
182 	notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
183 
184 out_notify:
185 	if (ret != 0)
186 		notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
187 out:
188 	up(&cpucontrol);
189 	return ret;
190 }
191