blob: 35882dccc94347ccf451e13c4bb4193fe73a91eb [file] [log] [blame]
Rusty Russellffdb5972008-07-28 12:16:28 -05001/* Copyright 2008, 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
Rusty Russelle5582ca2006-09-29 02:01:35 -07002 * GPL v2 and any later version.
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/cpu.h>
5#include <linux/err.h>
Prarit Bhargavaee527cd2007-05-08 00:25:08 -07006#include <linux/kthread.h>
7#include <linux/module.h>
8#include <linux/sched.h>
9#include <linux/stop_machine.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/syscalls.h>
Benjamin Herrenschmidta12bb442007-05-10 22:22:47 -070011#include <linux/interrupt.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/uaccess.h>
15
Rusty Russellffdb5972008-07-28 12:16:28 -050016/* This controls the threads on each CPU. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017enum stopmachine_state {
Rusty Russellffdb5972008-07-28 12:16:28 -050018 /* Dummy starting state for thread. */
19 STOPMACHINE_NONE,
20 /* Awaiting everyone to be scheduled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 STOPMACHINE_PREPARE,
Rusty Russellffdb5972008-07-28 12:16:28 -050022 /* Disable interrupts. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 STOPMACHINE_DISABLE_IRQ,
Rusty Russellffdb5972008-07-28 12:16:28 -050024 /* Run the function */
Jason Baron5c2aed62008-02-28 11:33:03 -050025 STOPMACHINE_RUN,
Rusty Russellffdb5972008-07-28 12:16:28 -050026 /* Exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 STOPMACHINE_EXIT,
28};
Rusty Russellffdb5972008-07-28 12:16:28 -050029static enum stopmachine_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jason Baron5c2aed62008-02-28 11:33:03 -050031struct stop_machine_data {
32 int (*fn)(void *);
33 void *data;
Rusty Russellffdb5972008-07-28 12:16:28 -050034 int fnret;
35};
Jason Baron5c2aed62008-02-28 11:33:03 -050036
Rusty Russellffdb5972008-07-28 12:16:28 -050037/* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
38static unsigned int num_threads;
39static atomic_t thread_ack;
40static struct completion finished;
41static DEFINE_MUTEX(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Rusty Russellffdb5972008-07-28 12:16:28 -050043static void set_state(enum stopmachine_state newstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Rusty Russellffdb5972008-07-28 12:16:28 -050045 /* Reset ack counter. */
46 atomic_set(&thread_ack, num_threads);
47 smp_wmb();
48 state = newstate;
49}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Rusty Russellffdb5972008-07-28 12:16:28 -050051/* Last one to ack a state moves to the next state. */
52static void ack_state(void)
53{
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)
57 complete(&finished);
58 else
59 set_state(state + 1);
60 }
61}
Andrew Mortond8cb7c12006-07-03 17:32:22 -070062
Rusty Russellffdb5972008-07-28 12:16:28 -050063/* 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. */
65static int stop_cpu(struct stop_machine_data *smdata)
66{
67 enum stopmachine_state curstate = STOPMACHINE_NONE;
68 int uninitialized_var(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* Simple state machine */
Rusty Russellffdb5972008-07-28 12:16:28 -050071 do {
72 /* Chill out and ensure we re-read stopmachine_state. */
Christian Borntraeger3401a61e2008-05-08 15:20:38 +020073 cpu_relax();
Rusty Russellffdb5972008-07-28 12:16:28 -050074 if (state != curstate) {
75 curstate = state;
76 switch (curstate) {
77 case STOPMACHINE_DISABLE_IRQ:
78 local_irq_disable();
79 hard_irq_disable();
80 break;
81 case STOPMACHINE_RUN:
82 /* |= allows error detection if functions on
83 * multiple CPUs. */
84 smdata->fnret |= smdata->fn(smdata->data);
85 break;
86 default:
87 break;
88 }
89 ack_state();
90 }
91 } while (curstate != STOPMACHINE_EXIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Rusty Russellffdb5972008-07-28 12:16:28 -050093 local_irq_enable();
94 do_exit(0);
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rusty Russellffdb5972008-07-28 12:16:28 -050097/* Callback for CPUs which aren't supposed to do anything. */
98static int chill(void *unused)
99{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
Rusty Russellffdb5972008-07-28 12:16:28 -0500103int __stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Rusty Russellffdb5972008-07-28 12:16:28 -0500105 int i, err;
106 struct stop_machine_data active, idle;
107 struct task_struct **threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Rusty Russellffdb5972008-07-28 12:16:28 -0500109 active.fn = fn;
110 active.data = data;
111 active.fnret = 0;
112 idle.fn = chill;
113 idle.data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Rusty Russellffdb5972008-07-28 12:16:28 -0500115 /* If they don't care which cpu fn runs on, just pick one. */
116 if (cpu == NR_CPUS)
117 cpu = any_online_cpu(cpu_online_map);
118
119 /* This could be too big for stack on large machines. */
120 threads = kcalloc(NR_CPUS, sizeof(threads[0]), GFP_KERNEL);
121 if (!threads)
122 return -ENOMEM;
123
124 /* Set up initial state. */
125 mutex_lock(&lock);
126 init_completion(&finished);
127 num_threads = num_online_cpus();
128 set_state(STOPMACHINE_PREPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 for_each_online_cpu(i) {
Rusty Russellffdb5972008-07-28 12:16:28 -0500131 struct stop_machine_data *smdata;
Satoru Takeuchi85653af2007-07-15 23:39:47 -0700132 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
133
Rusty Russellffdb5972008-07-28 12:16:28 -0500134 if (cpu == ALL_CPUS || i == cpu)
135 smdata = &active;
136 else
137 smdata = &idle;
138
139 threads[i] = kthread_create((void *)stop_cpu, smdata, "kstop%u",
140 i);
141 if (IS_ERR(threads[i])) {
142 err = PTR_ERR(threads[i]);
143 threads[i] = NULL;
144 goto kill_threads;
145 }
146
147 /* Place it onto correct cpu. */
148 kthread_bind(threads[i], i);
149
150 /* Make it highest prio. */
151 if (sched_setscheduler_nocheck(threads[i], SCHED_FIFO, &param))
152 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Rusty Russellffdb5972008-07-28 12:16:28 -0500154
155 /* We've created all the threads. Wake them all: hold this CPU so one
156 * doesn't hit this CPU until we're ready. */
157 cpu = get_cpu();
158 for_each_online_cpu(i)
159 wake_up_process(threads[i]);
160
161 /* This will release the thread on our CPU. */
162 put_cpu();
163 wait_for_completion(&finished);
164 mutex_unlock(&lock);
165
166 kfree(threads);
167
168 return active.fnret;
169
170kill_threads:
171 for_each_online_cpu(i)
172 if (threads[i])
173 kthread_stop(threads[i]);
174 mutex_unlock(&lock);
175
176 kfree(threads);
177 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int ret;
183
184 /* No CPUs can come up or down during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100185 get_online_cpus();
Rusty Russellffdb5972008-07-28 12:16:28 -0500186 ret = __stop_machine_run(fn, data, cpu);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100187 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return ret;
190}
Prarit Bhargavaee527cd2007-05-08 00:25:08 -0700191EXPORT_SYMBOL_GPL(stop_machine_run);