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