blob: 738b411ff2d33dee86042404917640bcb2932ed5 [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,
25 STOPMACHINE_EXIT,
26};
27
28static enum stopmachine_state stopmachine_state;
29static unsigned int stopmachine_num_threads;
30static atomic_t stopmachine_thread_ack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Andrew Mortond8cb7c12006-07-03 17:32:22 -070032static int stopmachine(void *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 int irqs_disabled = 0;
35 int prepared = 0;
Mike Travis65c01182008-07-15 14:14:30 -070036 cpumask_of_cpu_ptr(cpumask, (int)(long)cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Mike Travis65c01182008-07-15 14:14:30 -070038 set_cpus_allowed_ptr(current, cpumask);
Andrew Mortond8cb7c12006-07-03 17:32:22 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /* Ack: we are alive */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070041 smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 atomic_inc(&stopmachine_thread_ack);
43
44 /* Simple state machine */
45 while (stopmachine_state != STOPMACHINE_EXIT) {
46 if (stopmachine_state == STOPMACHINE_DISABLE_IRQ
47 && !irqs_disabled) {
48 local_irq_disable();
Benjamin Herrenschmidta12bb442007-05-10 22:22:47 -070049 hard_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 irqs_disabled = 1;
51 /* Ack: irqs disabled. */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070052 smp_mb(); /* Must read state first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 atomic_inc(&stopmachine_thread_ack);
54 } else if (stopmachine_state == STOPMACHINE_PREPARE
55 && !prepared) {
56 /* Everyone is in place, hold CPU. */
57 preempt_disable();
58 prepared = 1;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070059 smp_mb(); /* Must read state first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 atomic_inc(&stopmachine_thread_ack);
61 }
62 /* Yield in first stage: migration threads need to
63 * help our sisters onto their CPUs. */
64 if (!prepared && !irqs_disabled)
65 yield();
Christian Borntraeger3401a61e2008-05-08 15:20:38 +020066 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68
69 /* Ack: we are exiting. */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070070 smp_mb(); /* Must read state first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 atomic_inc(&stopmachine_thread_ack);
72
73 if (irqs_disabled)
74 local_irq_enable();
75 if (prepared)
76 preempt_enable();
77
78 return 0;
79}
80
81/* Change the thread state */
82static void stopmachine_set_state(enum stopmachine_state state)
83{
84 atomic_set(&stopmachine_thread_ack, 0);
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070085 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 stopmachine_state = state;
87 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
88 cpu_relax();
89}
90
91static int stop_machine(void)
92{
Andrew Mortond8cb7c12006-07-03 17:32:22 -070093 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 atomic_set(&stopmachine_thread_ack, 0);
96 stopmachine_num_threads = 0;
97 stopmachine_state = STOPMACHINE_WAIT;
98
99 for_each_online_cpu(i) {
Ingo Molnar39c715b2005-06-21 17:14:34 -0700100 if (i == raw_smp_processor_id())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 continue;
Andrew Mortond8cb7c12006-07-03 17:32:22 -0700102 ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
103 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 break;
105 stopmachine_num_threads++;
106 }
107
108 /* Wait for them all to come to life. */
Christian Borntraeger3401a61e2008-05-08 15:20:38 +0200109 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 yield();
Christian Borntraeger3401a61e2008-05-08 15:20:38 +0200111 cpu_relax();
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /* If some failed, kill them all. */
115 if (ret < 0) {
116 stopmachine_set_state(STOPMACHINE_EXIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ret;
118 }
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* Now they are all started, make them hold the CPUs, ready. */
Kirill Korotaev45573982005-11-13 16:07:30 -0800121 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 stopmachine_set_state(STOPMACHINE_PREPARE);
123
124 /* Make them disable irqs. */
Kirill Korotaev45573982005-11-13 16:07:30 -0800125 local_irq_disable();
Benjamin Herrenschmidta12bb442007-05-10 22:22:47 -0700126 hard_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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();
Kirill Korotaev45573982005-11-13 16:07:30 -0800136 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Pavel Machekf5264482008-04-21 22:15:06 +0000139struct stop_machine_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int (*fn)(void *);
141 void *data;
142 struct completion done;
143};
144
145static int do_stop(void *_smdata)
146{
147 struct stop_machine_data *smdata = _smdata;
148 int ret;
149
150 ret = stop_machine();
151 if (ret == 0) {
152 ret = smdata->fn(smdata->data);
153 restart_machine();
154 }
155
156 /* We're done: you can kthread_stop us now */
157 complete(&smdata->done);
158
159 /* Wait for kthread_stop */
160 set_current_state(TASK_INTERRUPTIBLE);
161 while (!kthread_should_stop()) {
162 schedule();
163 set_current_state(TASK_INTERRUPTIBLE);
164 }
165 __set_current_state(TASK_RUNNING);
166 return ret;
167}
168
169struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
170 unsigned int cpu)
171{
Daniel Walker6c6080f2008-02-06 01:37:41 -0800172 static DEFINE_MUTEX(stopmachine_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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
Daniel Walker6c6080f2008-02-06 01:37:41 -0800180 mutex_lock(&stopmachine_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* If they don't care which CPU fn runs on, bind to any online one. */
183 if (cpu == NR_CPUS)
Ingo Molnar39c715b2005-06-21 17:14:34 -0700184 cpu = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 p = kthread_create(do_stop, &smdata, "kstopmachine");
187 if (!IS_ERR(p)) {
Satoru Takeuchi85653af2007-07-15 23:39:47 -0700188 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
189
190 /* One high-prio thread per cpu. We'll do this one. */
Rusty Russell961ccdd2008-06-23 13:55:38 +1000191 sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 kthread_bind(p, cpu);
193 wake_up_process(p);
194 wait_for_completion(&smdata.done);
195 }
Daniel Walker6c6080f2008-02-06 01:37:41 -0800196 mutex_unlock(&stopmachine_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return p;
198}
199
200int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
201{
202 struct task_struct *p;
203 int ret;
204
205 /* No CPUs can come up or down during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100206 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 p = __stop_machine_run(fn, data, cpu);
208 if (!IS_ERR(p))
209 ret = kthread_stop(p);
210 else
211 ret = PTR_ERR(p);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100212 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 return ret;
215}
Prarit Bhargavaee527cd2007-05-08 00:25:08 -0700216EXPORT_SYMBOL_GPL(stop_machine_run);