vserver 1.9.5.x5
[linux-2.6.git] / kernel / stop_machine.c
1 #include <linux/stop_machine.h>
2 #include <linux/kthread.h>
3 #include <linux/sched.h>
4 #include <linux/cpu.h>
5 #include <linux/err.h>
6 #include <linux/syscalls.h>
7 #include <asm/atomic.h>
8 #include <asm/semaphore.h>
9
10 /* Since we effect priority and affinity (both of which are visible
11  * to, and settable by outside processes) we do indirection via a
12  * kthread. */
13
14 /* Thread to stop each CPU in user context. */
15 enum stopmachine_state {
16         STOPMACHINE_WAIT,
17         STOPMACHINE_PREPARE,
18         STOPMACHINE_DISABLE_IRQ,
19         STOPMACHINE_EXIT,
20 };
21
22 static enum stopmachine_state stopmachine_state;
23 static unsigned int stopmachine_num_threads;
24 static atomic_t stopmachine_thread_ack;
25 static DECLARE_MUTEX(stopmachine_mutex);
26
27 static int stopmachine(void *cpu)
28 {
29         int irqs_disabled = 0;
30         int prepared = 0;
31
32         set_cpus_allowed(current, cpumask_of_cpu((int)(long)cpu));
33
34         /* Ack: we are alive */
35         mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
36         atomic_inc(&stopmachine_thread_ack);
37
38         /* Simple state machine */
39         while (stopmachine_state != STOPMACHINE_EXIT) {
40                 if (stopmachine_state == STOPMACHINE_DISABLE_IRQ 
41                     && !irqs_disabled) {
42                         local_irq_disable();
43                         irqs_disabled = 1;
44                         /* Ack: irqs disabled. */
45                         mb(); /* Must read state first. */
46                         atomic_inc(&stopmachine_thread_ack);
47                 } else if (stopmachine_state == STOPMACHINE_PREPARE
48                            && !prepared) {
49                         /* Everyone is in place, hold CPU. */
50                         preempt_disable();
51                         prepared = 1;
52                         mb(); /* Must read state first. */
53                         atomic_inc(&stopmachine_thread_ack);
54                 }
55                 /* Yield in first stage: migration threads need to
56                  * help our sisters onto their CPUs. */
57                 if (!prepared && !irqs_disabled)
58                         yield();
59                 else
60                         cpu_relax();
61         }
62
63         /* Ack: we are exiting. */
64         mb(); /* Must read state first. */
65         atomic_inc(&stopmachine_thread_ack);
66
67         if (irqs_disabled)
68                 local_irq_enable();
69         if (prepared)
70                 preempt_enable();
71
72         return 0;
73 }
74
75 /* Change the thread state */
76 static void stopmachine_set_state(enum stopmachine_state state)
77 {
78         atomic_set(&stopmachine_thread_ack, 0);
79         wmb();
80         stopmachine_state = state;
81         while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
82                 cpu_relax();
83 }
84
85 static int stop_machine(void)
86 {
87         int i, ret = 0;
88         struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
89
90         /* One high-prio thread per cpu.  We'll do this one. */
91         sys_sched_setscheduler(current->pid, SCHED_FIFO, &param);
92
93         atomic_set(&stopmachine_thread_ack, 0);
94         stopmachine_num_threads = 0;
95         stopmachine_state = STOPMACHINE_WAIT;
96
97         for_each_online_cpu(i) {
98                 if (i == _smp_processor_id())
99                         continue;
100                 ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
101                 if (ret < 0)
102                         break;
103                 stopmachine_num_threads++;
104         }
105
106         /* Wait for them all to come to life. */
107         while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
108                 yield();
109
110         /* If some failed, kill them all. */
111         if (ret < 0) {
112                 stopmachine_set_state(STOPMACHINE_EXIT);
113                 up(&stopmachine_mutex);
114                 return ret;
115         }
116
117         /* Don't schedule us away at this point, please. */
118         local_irq_disable();
119
120         /* Now they are all started, make them hold the CPUs, ready. */
121         stopmachine_set_state(STOPMACHINE_PREPARE);
122
123         /* Make them disable irqs. */
124         stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
125
126         return 0;
127 }
128
129 static void restart_machine(void)
130 {
131         stopmachine_set_state(STOPMACHINE_EXIT);
132         local_irq_enable();
133 }
134
135 struct stop_machine_data
136 {
137         int (*fn)(void *);
138         void *data;
139         struct completion done;
140 };
141
142 static int do_stop(void *_smdata)
143 {
144         struct stop_machine_data *smdata = _smdata;
145         int ret;
146
147         ret = stop_machine();
148         if (ret == 0) {
149                 ret = smdata->fn(smdata->data);
150                 restart_machine();
151         }
152
153         /* We're done: you can kthread_stop us now */
154         complete(&smdata->done);
155
156         /* Wait for kthread_stop */
157         set_current_state(TASK_INTERRUPTIBLE);
158         while (!kthread_should_stop()) {
159                 schedule();
160                 set_current_state(TASK_INTERRUPTIBLE);
161         }
162         __set_current_state(TASK_RUNNING);
163         return ret;
164 }
165
166 struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
167                                        unsigned int cpu)
168 {
169         struct stop_machine_data smdata;
170         struct task_struct *p;
171
172         smdata.fn = fn;
173         smdata.data = data;
174         init_completion(&smdata.done);
175
176         down(&stopmachine_mutex);
177
178         /* If they don't care which CPU fn runs on, bind to any online one. */
179         if (cpu == NR_CPUS)
180                 cpu = _smp_processor_id();
181
182         p = kthread_create(do_stop, &smdata, "kstopmachine");
183         if (!IS_ERR(p)) {
184                 kthread_bind(p, cpu);
185                 wake_up_process(p);
186                 wait_for_completion(&smdata.done);
187         }
188         up(&stopmachine_mutex);
189         return p;
190 }
191
192 int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
193 {
194         struct task_struct *p;
195         int ret;
196
197         /* No CPUs can come up or down during this. */
198         lock_cpu_hotplug();
199         p = __stop_machine_run(fn, data, cpu);
200         if (!IS_ERR(p))
201                 ret = kthread_stop(p);
202         else
203                 ret = PTR_ERR(p);
204         unlock_cpu_hotplug();
205
206         return ret;
207 }