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 DEFINE_MUTEX(lock);
42 static struct workqueue_struct *stop_machine_wq;
43 static struct stop_machine_data active, idle;
44 static const cpumask_t *active_cpus;
45 static void *stop_machine_work;
47 static void set_state(enum stopmachine_state newstate)
49 /* Reset ack counter. */
50 atomic_set(&thread_ack, num_threads);
55 /* Last one to ack a state moves to the next state. */
56 static void ack_state(void)
58 if (atomic_dec_and_test(&thread_ack))
62 /* This is the actual function which stops the CPU. It runs
63 * in the context of a dedicated stopmachine workqueue. */
64 static void stop_cpu(struct work_struct *unused)
66 enum stopmachine_state curstate = STOPMACHINE_NONE;
67 struct stop_machine_data *smdata = &idle;
68 int cpu = smp_processor_id();
72 if (cpu == cpumask_first(cpu_online_mask))
75 if (cpumask_test_cpu(cpu, active_cpus))
78 /* Simple state machine */
80 /* Chill out and ensure we re-read stopmachine_state. */
82 if (state != curstate) {
85 case STOPMACHINE_DISABLE_IRQ:
90 /* On multiple CPUs only a single error code
91 * is needed to tell that something failed. */
92 err = smdata->fn(smdata->data);
101 } while (curstate != STOPMACHINE_EXIT);
106 /* Callback for CPUs which aren't supposed to do anything. */
107 static int chill(void *unused)
112 int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
114 struct work_struct *sm_work;
117 /* Set up initial state. */
119 num_threads = num_online_cpus();
127 set_state(STOPMACHINE_PREPARE);
129 /* Schedule the stop_cpu work on all cpus: hold this CPU so one
130 * doesn't hit this CPU until we're ready. */
132 for_each_online_cpu(i) {
133 sm_work = percpu_ptr(stop_machine_work, i);
134 INIT_WORK(sm_work, stop_cpu);
135 queue_work_on(i, stop_machine_wq, sm_work);
137 /* This will release the thread on our CPU. */
139 flush_workqueue(stop_machine_wq);
145 int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
149 /* No CPUs can come up or down during this. */
151 ret = __stop_machine(fn, data, cpus);
156 EXPORT_SYMBOL_GPL(stop_machine);
158 static int __init stop_machine_init(void)
160 stop_machine_wq = create_rt_workqueue("kstop");
161 stop_machine_work = alloc_percpu(struct work_struct);
164 core_initcall(stop_machine_init);