1 /* Copyright 2008, 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 /* This controls the threads on each CPU. */
17 enum stopmachine_state {
18 /* Dummy starting state for thread. */
20 /* Awaiting everyone to be scheduled. */
22 /* Disable interrupts. */
23 STOPMACHINE_DISABLE_IRQ,
24 /* Run the function */
29 static enum stopmachine_state state;
31 struct stop_machine_data {
37 /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
38 static unsigned int num_threads;
39 static atomic_t thread_ack;
40 static struct completion finished;
41 static DEFINE_MUTEX(lock);
43 static void set_state(enum stopmachine_state newstate)
45 /* Reset ack counter. */
46 atomic_set(&thread_ack, num_threads);
51 /* Last one to ack a state moves to the next state. */
52 static void ack_state(void)
54 if (atomic_dec_and_test(&thread_ack)) {
55 /* If we're the last one to ack the EXIT, we're finished. */
56 if (state == STOPMACHINE_EXIT)
63 /* This is the actual thread which stops the CPU. It exits by itself rather
64 * than waiting for kthread_stop(), because it's easier for hotplug CPU. */
65 static int stop_cpu(struct stop_machine_data *smdata)
67 enum stopmachine_state curstate = STOPMACHINE_NONE;
69 /* Simple state machine */
71 /* Chill out and ensure we re-read stopmachine_state. */
73 if (state != curstate) {
76 case STOPMACHINE_DISABLE_IRQ:
81 /* |= allows error detection if functions on
83 smdata->fnret |= smdata->fn(smdata->data);
90 } while (curstate != STOPMACHINE_EXIT);
96 /* Callback for CPUs which aren't supposed to do anything. */
97 static int chill(void *unused)
102 int __stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
105 struct stop_machine_data active, idle;
106 struct task_struct **threads;
114 /* This could be too big for stack on large machines. */
115 threads = kcalloc(NR_CPUS, sizeof(threads[0]), GFP_KERNEL);
119 /* Set up initial state. */
121 init_completion(&finished);
122 num_threads = num_online_cpus();
123 set_state(STOPMACHINE_PREPARE);
125 for_each_online_cpu(i) {
126 struct stop_machine_data *smdata = &idle;
127 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
130 if (i == first_cpu(cpu_online_map))
133 if (cpu_isset(i, *cpus))
137 threads[i] = kthread_create((void *)stop_cpu, smdata, "kstop%u",
139 if (IS_ERR(threads[i])) {
140 err = PTR_ERR(threads[i]);
145 /* Place it onto correct cpu. */
146 kthread_bind(threads[i], i);
148 /* Make it highest prio. */
149 if (sched_setscheduler_nocheck(threads[i], SCHED_FIFO, ¶m))
153 /* We've created all the threads. Wake them all: hold this CPU so one
154 * doesn't hit this CPU until we're ready. */
156 for_each_online_cpu(i)
157 wake_up_process(threads[i]);
159 /* This will release the thread on our CPU. */
161 wait_for_completion(&finished);
169 for_each_online_cpu(i)
171 kthread_stop(threads[i]);
178 int stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
182 /* No CPUs can come up or down during this. */
184 ret = __stop_machine(fn, data, cpus);
189 EXPORT_SYMBOL_GPL(stop_machine);