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
,
28 static enum stopmachine_state stopmachine_state
;
29 static unsigned int stopmachine_num_threads
;
30 static atomic_t stopmachine_thread_ack
;
32 static int stopmachine(void *cpu
)
34 int irqs_disabled
= 0;
37 set_cpus_allowed_ptr(current
, &cpumask_of_cpu((int)(long)cpu
));
39 /* Ack: we are alive */
40 smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
41 atomic_inc(&stopmachine_thread_ack
);
43 /* Simple state machine */
44 while (stopmachine_state
!= STOPMACHINE_EXIT
) {
45 if (stopmachine_state
== STOPMACHINE_DISABLE_IRQ
50 /* Ack: irqs disabled. */
51 smp_mb(); /* Must read state first. */
52 atomic_inc(&stopmachine_thread_ack
);
53 } else if (stopmachine_state
== STOPMACHINE_PREPARE
55 /* Everyone is in place, hold CPU. */
58 smp_mb(); /* Must read state first. */
59 atomic_inc(&stopmachine_thread_ack
);
61 /* Yield in first stage: migration threads need to
62 * help our sisters onto their CPUs. */
63 if (!prepared
&& !irqs_disabled
)
69 /* Ack: we are exiting. */
70 smp_mb(); /* Must read state first. */
71 atomic_inc(&stopmachine_thread_ack
);
81 /* Change the thread state */
82 static void stopmachine_set_state(enum stopmachine_state state
)
84 atomic_set(&stopmachine_thread_ack
, 0);
86 stopmachine_state
= state
;
87 while (atomic_read(&stopmachine_thread_ack
) != stopmachine_num_threads
)
91 static int stop_machine(void)
95 atomic_set(&stopmachine_thread_ack
, 0);
96 stopmachine_num_threads
= 0;
97 stopmachine_state
= STOPMACHINE_WAIT
;
99 for_each_online_cpu(i
) {
100 if (i
== raw_smp_processor_id())
102 ret
= kernel_thread(stopmachine
, (void *)(long)i
,CLONE_KERNEL
);
105 stopmachine_num_threads
++;
108 /* Wait for them all to come to life. */
109 while (atomic_read(&stopmachine_thread_ack
) != stopmachine_num_threads
)
112 /* If some failed, kill them all. */
114 stopmachine_set_state(STOPMACHINE_EXIT
);
118 /* Now they are all started, make them hold the CPUs, ready. */
120 stopmachine_set_state(STOPMACHINE_PREPARE
);
122 /* Make them disable irqs. */
125 stopmachine_set_state(STOPMACHINE_DISABLE_IRQ
);
130 static void restart_machine(void)
132 stopmachine_set_state(STOPMACHINE_EXIT
);
134 preempt_enable_no_resched();
137 struct stop_machine_data
{
140 struct completion done
;
143 static int do_stop(void *_smdata
)
145 struct stop_machine_data
*smdata
= _smdata
;
148 ret
= stop_machine();
150 ret
= smdata
->fn(smdata
->data
);
154 /* We're done: you can kthread_stop us now */
155 complete(&smdata
->done
);
157 /* Wait for kthread_stop */
158 set_current_state(TASK_INTERRUPTIBLE
);
159 while (!kthread_should_stop()) {
161 set_current_state(TASK_INTERRUPTIBLE
);
163 __set_current_state(TASK_RUNNING
);
167 struct task_struct
*__stop_machine_run(int (*fn
)(void *), void *data
,
170 static DEFINE_MUTEX(stopmachine_mutex
);
171 struct stop_machine_data smdata
;
172 struct task_struct
*p
;
176 init_completion(&smdata
.done
);
178 mutex_lock(&stopmachine_mutex
);
180 /* If they don't care which CPU fn runs on, bind to any online one. */
182 cpu
= raw_smp_processor_id();
184 p
= kthread_create(do_stop
, &smdata
, "kstopmachine");
186 struct sched_param param
= { .sched_priority
= MAX_RT_PRIO
-1 };
188 /* One high-prio thread per cpu. We'll do this one. */
189 sched_setscheduler(p
, SCHED_FIFO
, ¶m
);
190 kthread_bind(p
, cpu
);
192 wait_for_completion(&smdata
.done
);
194 mutex_unlock(&stopmachine_mutex
);
198 int stop_machine_run(int (*fn
)(void *), void *data
, unsigned int cpu
)
200 struct task_struct
*p
;
203 /* No CPUs can come up or down during this. */
205 p
= __stop_machine_run(fn
, data
, cpu
);
207 ret
= kthread_stop(p
);
214 EXPORT_SYMBOL_GPL(stop_machine_run
);