MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / kernel / stop_machine.c
blob9610403ce2cf2252c886029bbeebd89e66b7d289
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>
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. */
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,
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);
27 static int stopmachine(void *cpu)
29 int irqs_disabled = 0;
30 int prepared = 0;
32 set_cpus_allowed(current, cpumask_of_cpu((int)(long)cpu));
34 /* Ack: we are alive */
35 mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
36 atomic_inc(&stopmachine_thread_ack);
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);
55 cpu_relax();
58 /* Ack: we are exiting. */
59 mb(); /* Must read state first. */
60 atomic_inc(&stopmachine_thread_ack);
62 if (irqs_disabled)
63 local_irq_enable();
64 if (prepared)
65 preempt_enable();
67 return 0;
70 /* Change the thread state */
71 static void stopmachine_set_state(enum stopmachine_state state)
73 atomic_set(&stopmachine_thread_ack, 0);
74 wmb();
75 stopmachine_state = state;
76 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
77 cpu_relax();
80 static int stop_machine(void)
82 int i, ret = 0;
83 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
85 /* One high-prio thread per cpu. We'll do this one. */
86 sys_sched_setscheduler(current->pid, SCHED_FIFO, &param);
88 atomic_set(&stopmachine_thread_ack, 0);
89 stopmachine_num_threads = 0;
90 stopmachine_state = STOPMACHINE_WAIT;
92 for_each_online_cpu(i) {
93 if (i == smp_processor_id())
94 continue;
95 ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
96 if (ret < 0)
97 break;
98 stopmachine_num_threads++;
101 /* Wait for them all to come to life. */
102 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
103 yield();
105 /* If some failed, kill them all. */
106 if (ret < 0) {
107 stopmachine_set_state(STOPMACHINE_EXIT);
108 up(&stopmachine_mutex);
109 return ret;
112 /* Don't schedule us away at this point, please. */
113 local_irq_disable();
115 /* Now they are all started, make them hold the CPUs, ready. */
116 stopmachine_set_state(STOPMACHINE_PREPARE);
118 /* Make them disable irqs. */
119 stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
121 return 0;
124 static void restart_machine(void)
126 stopmachine_set_state(STOPMACHINE_EXIT);
127 local_irq_enable();
130 struct stop_machine_data
132 int (*fn)(void *);
133 void *data;
134 struct completion done;
137 static int do_stop(void *_smdata)
139 struct stop_machine_data *smdata = _smdata;
140 int ret;
142 ret = stop_machine();
143 if (ret == 0) {
144 ret = smdata->fn(smdata->data);
145 restart_machine();
148 /* We're done: you can kthread_stop us now */
149 complete(&smdata->done);
151 /* Wait for kthread_stop */
152 set_current_state(TASK_INTERRUPTIBLE);
153 while (!kthread_should_stop()) {
154 schedule();
155 set_current_state(TASK_INTERRUPTIBLE);
157 __set_current_state(TASK_RUNNING);
158 return ret;
161 struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
162 unsigned int cpu)
164 struct stop_machine_data smdata;
165 struct task_struct *p;
167 smdata.fn = fn;
168 smdata.data = data;
169 init_completion(&smdata.done);
171 down(&stopmachine_mutex);
173 /* If they don't care which CPU fn runs on, bind to any online one. */
174 if (cpu == NR_CPUS)
175 cpu = smp_processor_id();
177 p = kthread_create(do_stop, &smdata, "kstopmachine");
178 if (!IS_ERR(p)) {
179 kthread_bind(p, cpu);
180 wake_up_process(p);
181 wait_for_completion(&smdata.done);
183 up(&stopmachine_mutex);
184 return p;
187 int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
189 struct task_struct *p;
190 int ret;
192 /* No CPUs can come up or down during this. */
193 lock_cpu_hotplug();
194 p = __stop_machine_run(fn, data, cpu);
195 if (!IS_ERR(p))
196 ret = kthread_stop(p);
197 else
198 ret = PTR_ERR(p);
199 unlock_cpu_hotplug();
201 return ret;