blob: a473bd0cb71b756f628604a123c800adf814f2fc [file] [log] [blame]
Rusty Russelle5582ca2006-09-29 02:01:35 -07001/* Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
2 * GPL v2 and any later version.
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/cpu.h>
5#include <linux/err.h>
Prarit Bhargavaee527cd2007-05-08 00:25:08 -07006#include <linux/kthread.h>
7#include <linux/module.h>
8#include <linux/sched.h>
9#include <linux/stop_machine.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/syscalls.h>
Benjamin Herrenschmidta12bb442007-05-10 22:22:47 -070011#include <linux/interrupt.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/uaccess.h>
15
16/* Since we effect priority and affinity (both of which are visible
17 * to, and settable by outside processes) we do indirection via a
18 * kthread. */
19
20/* Thread to stop each CPU in user context. */
21enum stopmachine_state {
22 STOPMACHINE_WAIT,
23 STOPMACHINE_PREPARE,
24 STOPMACHINE_DISABLE_IRQ,
Jason Baron5c2aed62008-02-28 11:33:03 -050025 STOPMACHINE_RUN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 STOPMACHINE_EXIT,
27};
28
Jason Baron5c2aed62008-02-28 11:33:03 -050029struct stop_machine_data {
30 int (*fn)(void *);
31 void *data;
32 struct completion done;
33 int run_all;
34} smdata;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static enum stopmachine_state stopmachine_state;
37static unsigned int stopmachine_num_threads;
38static atomic_t stopmachine_thread_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Andrew Mortond8cb7c12006-07-03 17:32:22 -070040static int stopmachine(void *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int irqs_disabled = 0;
43 int prepared = 0;
Jason Baron5c2aed62008-02-28 11:33:03 -050044 int ran = 0;
Mike Travis65c01182008-07-15 14:14:30 -070045 cpumask_of_cpu_ptr(cpumask, (int)(long)cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Mike Travis65c01182008-07-15 14:14:30 -070047 set_cpus_allowed_ptr(current, cpumask);
Andrew Mortond8cb7c12006-07-03 17:32:22 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* Ack: we are alive */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070050 smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 atomic_inc(&stopmachine_thread_ack);
52
53 /* Simple state machine */
54 while (stopmachine_state != STOPMACHINE_EXIT) {
55 if (stopmachine_state == STOPMACHINE_DISABLE_IRQ
56 && !irqs_disabled) {
57 local_irq_disable();
Benjamin Herrenschmidta12bb442007-05-10 22:22:47 -070058 hard_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 irqs_disabled = 1;
60 /* Ack: irqs disabled. */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070061 smp_mb(); /* Must read state first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 atomic_inc(&stopmachine_thread_ack);
63 } else if (stopmachine_state == STOPMACHINE_PREPARE
64 && !prepared) {
65 /* Everyone is in place, hold CPU. */
66 preempt_disable();
67 prepared = 1;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070068 smp_mb(); /* Must read state first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 atomic_inc(&stopmachine_thread_ack);
Jason Baron5c2aed62008-02-28 11:33:03 -050070 } else if (stopmachine_state == STOPMACHINE_RUN && !ran) {
71 smdata.fn(smdata.data);
72 ran = 1;
73 smp_mb(); /* Must read state first. */
74 atomic_inc(&stopmachine_thread_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 /* Yield in first stage: migration threads need to
77 * help our sisters onto their CPUs. */
78 if (!prepared && !irqs_disabled)
79 yield();
Christian Borntraeger3401a61e2008-05-08 15:20:38 +020080 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
83 /* Ack: we are exiting. */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070084 smp_mb(); /* Must read state first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 atomic_inc(&stopmachine_thread_ack);
86
87 if (irqs_disabled)
88 local_irq_enable();
89 if (prepared)
90 preempt_enable();
91
92 return 0;
93}
94
95/* Change the thread state */
96static void stopmachine_set_state(enum stopmachine_state state)
97{
98 atomic_set(&stopmachine_thread_ack, 0);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070099 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 stopmachine_state = state;
101 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
102 cpu_relax();
103}
104
105static int stop_machine(void)
106{
Andrew Mortond8cb7c12006-07-03 17:32:22 -0700107 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 atomic_set(&stopmachine_thread_ack, 0);
110 stopmachine_num_threads = 0;
111 stopmachine_state = STOPMACHINE_WAIT;
112
113 for_each_online_cpu(i) {
Ingo Molnar39c715b2005-06-21 17:14:34 -0700114 if (i == raw_smp_processor_id())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 continue;
Andrew Mortond8cb7c12006-07-03 17:32:22 -0700116 ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
117 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119 stopmachine_num_threads++;
120 }
121
122 /* Wait for them all to come to life. */
Christian Borntraeger3401a61e2008-05-08 15:20:38 +0200123 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 yield();
Christian Borntraeger3401a61e2008-05-08 15:20:38 +0200125 cpu_relax();
126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* If some failed, kill them all. */
129 if (ret < 0) {
130 stopmachine_set_state(STOPMACHINE_EXIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return ret;
132 }
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Now they are all started, make them hold the CPUs, ready. */
Kirill Korotaev45573982005-11-13 16:07:30 -0800135 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 stopmachine_set_state(STOPMACHINE_PREPARE);
137
138 /* Make them disable irqs. */
Kirill Korotaev45573982005-11-13 16:07:30 -0800139 local_irq_disable();
Benjamin Herrenschmidta12bb442007-05-10 22:22:47 -0700140 hard_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
142
143 return 0;
144}
145
146static void restart_machine(void)
147{
148 stopmachine_set_state(STOPMACHINE_EXIT);
149 local_irq_enable();
Kirill Korotaev45573982005-11-13 16:07:30 -0800150 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Jason Baron5c2aed62008-02-28 11:33:03 -0500153static void run_other_cpus(void)
154{
155 stopmachine_set_state(STOPMACHINE_RUN);
156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158static int do_stop(void *_smdata)
159{
160 struct stop_machine_data *smdata = _smdata;
161 int ret;
162
163 ret = stop_machine();
164 if (ret == 0) {
165 ret = smdata->fn(smdata->data);
Jason Baron5c2aed62008-02-28 11:33:03 -0500166 if (smdata->run_all)
167 run_other_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 restart_machine();
169 }
170
171 /* We're done: you can kthread_stop us now */
172 complete(&smdata->done);
173
174 /* Wait for kthread_stop */
175 set_current_state(TASK_INTERRUPTIBLE);
176 while (!kthread_should_stop()) {
177 schedule();
178 set_current_state(TASK_INTERRUPTIBLE);
179 }
180 __set_current_state(TASK_RUNNING);
181 return ret;
182}
183
184struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
185 unsigned int cpu)
186{
Daniel Walker6c6080f2008-02-06 01:37:41 -0800187 static DEFINE_MUTEX(stopmachine_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct stop_machine_data smdata;
189 struct task_struct *p;
190
Daniel Walker6c6080f2008-02-06 01:37:41 -0800191 mutex_lock(&stopmachine_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jason Baron5c2aed62008-02-28 11:33:03 -0500193 smdata.fn = fn;
194 smdata.data = data;
195 smdata.run_all = (cpu == ALL_CPUS) ? 1 : 0;
196 init_completion(&smdata.done);
197
198 smp_wmb(); /* make sure other cpus see smdata updates */
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* If they don't care which CPU fn runs on, bind to any online one. */
Jason Baron5c2aed62008-02-28 11:33:03 -0500201 if (cpu == NR_CPUS || cpu == ALL_CPUS)
Ingo Molnar39c715b2005-06-21 17:14:34 -0700202 cpu = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 p = kthread_create(do_stop, &smdata, "kstopmachine");
205 if (!IS_ERR(p)) {
Satoru Takeuchi85653af2007-07-15 23:39:47 -0700206 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
207
208 /* One high-prio thread per cpu. We'll do this one. */
Rusty Russell961ccdd2008-06-23 13:55:38 +1000209 sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 kthread_bind(p, cpu);
211 wake_up_process(p);
212 wait_for_completion(&smdata.done);
213 }
Daniel Walker6c6080f2008-02-06 01:37:41 -0800214 mutex_unlock(&stopmachine_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return p;
216}
217
218int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
219{
220 struct task_struct *p;
221 int ret;
222
223 /* No CPUs can come up or down during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100224 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 p = __stop_machine_run(fn, data, cpu);
226 if (!IS_ERR(p))
227 ret = kthread_stop(p);
228 else
229 ret = PTR_ERR(p);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100230 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return ret;
233}
Prarit Bhargavaee527cd2007-05-08 00:25:08 -0700234EXPORT_SYMBOL_GPL(stop_machine_run);