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/uaccess.h>
16 /* Since we effect priority and affinity (both of which are visible
17 * to, and settable by outside processes) we do indirection via a
20 /* Thread to stop each CPU in user context. */
21 enum stopmachine_state {
24 STOPMACHINE_DISABLE_IRQ,
29 struct stop_machine_data {
32 struct completion done;
36 static enum stopmachine_state stopmachine_state;
37 static unsigned int stopmachine_num_threads;
38 static atomic_t stopmachine_thread_ack;
40 static int stopmachine(void *cpu)
42 int irqs_disabled = 0;
45 cpumask_of_cpu_ptr(cpumask, (int)(long)cpu);
47 set_cpus_allowed_ptr(current, cpumask);
49 /* Ack: we are alive */
50 smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
51 atomic_inc(&stopmachine_thread_ack);
53 /* Simple state machine */
54 while (stopmachine_state != STOPMACHINE_EXIT) {
55 if (stopmachine_state == STOPMACHINE_DISABLE_IRQ
60 /* Ack: irqs disabled. */
61 smp_mb(); /* Must read state first. */
62 atomic_inc(&stopmachine_thread_ack);
63 } else if (stopmachine_state == STOPMACHINE_PREPARE
65 /* Everyone is in place, hold CPU. */
68 smp_mb(); /* Must read state first. */
69 atomic_inc(&stopmachine_thread_ack);
70 } else if (stopmachine_state == STOPMACHINE_RUN && !ran) {
71 smdata.fn(smdata.data);
73 smp_mb(); /* Must read state first. */
74 atomic_inc(&stopmachine_thread_ack);
76 /* Yield in first stage: migration threads need to
77 * help our sisters onto their CPUs. */
78 if (!prepared && !irqs_disabled)
83 /* Ack: we are exiting. */
84 smp_mb(); /* Must read state first. */
85 atomic_inc(&stopmachine_thread_ack);
95 /* Change the thread state */
96 static void stopmachine_set_state(enum stopmachine_state state)
98 atomic_set(&stopmachine_thread_ack, 0);
100 stopmachine_state = state;
101 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
105 static int stop_machine(void)
109 atomic_set(&stopmachine_thread_ack, 0);
110 stopmachine_num_threads = 0;
111 stopmachine_state = STOPMACHINE_WAIT;
113 for_each_online_cpu(i) {
114 if (i == raw_smp_processor_id())
116 ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
119 stopmachine_num_threads++;
122 /* Wait for them all to come to life. */
123 while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads) {
128 /* If some failed, kill them all. */
130 stopmachine_set_state(STOPMACHINE_EXIT);
134 /* Now they are all started, make them hold the CPUs, ready. */
136 stopmachine_set_state(STOPMACHINE_PREPARE);
138 /* Make them disable irqs. */
141 stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);
146 static void restart_machine(void)
148 stopmachine_set_state(STOPMACHINE_EXIT);
150 preempt_enable_no_resched();
153 static void run_other_cpus(void)
155 stopmachine_set_state(STOPMACHINE_RUN);
158 static int do_stop(void *_smdata)
160 struct stop_machine_data *smdata = _smdata;
163 ret = stop_machine();
165 ret = smdata->fn(smdata->data);
171 /* We're done: you can kthread_stop us now */
172 complete(&smdata->done);
174 /* Wait for kthread_stop */
175 set_current_state(TASK_INTERRUPTIBLE);
176 while (!kthread_should_stop()) {
178 set_current_state(TASK_INTERRUPTIBLE);
180 __set_current_state(TASK_RUNNING);
184 struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
187 static DEFINE_MUTEX(stopmachine_mutex);
188 struct stop_machine_data smdata;
189 struct task_struct *p;
191 mutex_lock(&stopmachine_mutex);
195 smdata.run_all = (cpu == ALL_CPUS) ? 1 : 0;
196 init_completion(&smdata.done);
198 smp_wmb(); /* make sure other cpus see smdata updates */
200 /* If they don't care which CPU fn runs on, bind to any online one. */
201 if (cpu == NR_CPUS || cpu == ALL_CPUS)
202 cpu = raw_smp_processor_id();
204 p = kthread_create(do_stop, &smdata, "kstopmachine");
206 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
208 /* One high-prio thread per cpu. We'll do this one. */
209 sched_setscheduler_nocheck(p, SCHED_FIFO, ¶m);
210 kthread_bind(p, cpu);
212 wait_for_completion(&smdata.done);
214 mutex_unlock(&stopmachine_mutex);
218 int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
220 struct task_struct *p;
223 /* No CPUs can come up or down during this. */
225 p = __stop_machine_run(fn, data, cpu);
227 ret = kthread_stop(p);
234 EXPORT_SYMBOL_GPL(stop_machine_run);