1 /* Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
2 * GPL v2 and any later version.
6 #include <linux/kthread.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
9 #include <linux/stop_machine.h>
10 #include <linux/syscalls.h>
11 #include <linux/interrupt.h>
13 #include <asm/atomic.h>
14 #include <asm/semaphore.h>
15 #include <asm/uaccess.h>
17 /* Since we effect priority and affinity (both of which are visible
18 * to, and settable by outside processes) we do indirection via a
21 /* Thread to stop each CPU in user context. */
22 enum stopmachine_state {
25 STOPMACHINE_DISABLE_IRQ,
29 static enum stopmachine_state stopmachine_state;
30 static unsigned int stopmachine_num_threads;
31 static atomic_t stopmachine_thread_ack;
32 static DECLARE_MUTEX(stopmachine_mutex);
34 static int stopmachine(void *cpu)
36 int irqs_disabled = 0;
39 set_cpus_allowed(current, cpumask_of_cpu((int)(long)cpu));
41 /* Ack: we are alive */
42 smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
43 atomic_inc(&stopmachine_thread_ack);
45 /* Simple state machine */
46 while (stopmachine_state != STOPMACHINE_EXIT) {
47 if (stopmachine_state == STOPMACHINE_DISABLE_IRQ
52 /* Ack: irqs disabled. */
53 smp_mb(); /* Must read state first. */
54 atomic_inc(&stopmachine_thread_ack);
55 } else if (stopmachine_state == STOPMACHINE_PREPARE
57 /* Everyone is in place, hold CPU. */
60 smp_mb(); /* Must read state first. */
61 atomic_inc(&stopmachine_thread_ack);
63 /* Yield in first stage: migration threads need to
64 * help our sisters onto their CPUs. */
65 if (!prepared && !irqs_disabled)
71 /* Ack: we are exiting. */
72 smp_mb(); /* Must read state first. */
73 atomic_inc(&stopmachine_thread_ack);
83 /* Change the thread state */
84 static void stopmachine_set_state(enum stopmachine_state state)
86 atomic_set(&stopmachine_thread_ack, 0);
88 stopmachine_state = state;
89 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
93 static int stop_machine(void)
96 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
98 /* One high-prio thread per cpu. We'll do this one. */
99 sched_setscheduler(current, SCHED_FIFO, ¶m);
101 atomic_set(&stopmachine_thread_ack, 0);
102 stopmachine_num_threads = 0;
103 stopmachine_state = STOPMACHINE_WAIT;
105 for_each_online_cpu(i) {
106 if (i == raw_smp_processor_id())
108 ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
111 stopmachine_num_threads++;
114 /* Wait for them all to come to life. */
115 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
118 /* If some failed, kill them all. */
120 stopmachine_set_state(STOPMACHINE_EXIT);
124 /* Now they are all started, make them hold the CPUs, ready. */
126 stopmachine_set_state(STOPMACHINE_PREPARE);
128 /* Make them disable irqs. */
131 stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
136 static void restart_machine(void)
138 stopmachine_set_state(STOPMACHINE_EXIT);
140 preempt_enable_no_resched();
143 struct stop_machine_data
147 struct completion done;
150 static int do_stop(void *_smdata)
152 struct stop_machine_data *smdata = _smdata;
155 ret = stop_machine();
157 ret = smdata->fn(smdata->data);
161 /* We're done: you can kthread_stop us now */
162 complete(&smdata->done);
164 /* Wait for kthread_stop */
165 set_current_state(TASK_INTERRUPTIBLE);
166 while (!kthread_should_stop()) {
168 set_current_state(TASK_INTERRUPTIBLE);
170 __set_current_state(TASK_RUNNING);
174 struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
177 struct stop_machine_data smdata;
178 struct task_struct *p;
182 init_completion(&smdata.done);
184 down(&stopmachine_mutex);
186 /* If they don't care which CPU fn runs on, bind to any online one. */
188 cpu = raw_smp_processor_id();
190 p = kthread_create(do_stop, &smdata, "kstopmachine");
192 kthread_bind(p, cpu);
194 wait_for_completion(&smdata.done);
196 up(&stopmachine_mutex);
200 int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
202 struct task_struct *p;
205 /* No CPUs can come up or down during this. */
207 p = __stop_machine_run(fn, data, cpu);
209 ret = kthread_stop(p);
212 unlock_cpu_hotplug();
216 EXPORT_SYMBOL_GPL(stop_machine_run);