blob: 0cd415ee62a262ccadea6b592a7402eb89fc4797 [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;
Rusty Russellffdb5972008-07-28 12:16:28 -050040static DEFINE_MUTEX(lock);
Heiko Carstens9ea09af2008-12-22 12:36:30 +010041/* setup_lock protects refcount, stop_machine_wq and stop_machine_work. */
42static DEFINE_MUTEX(setup_lock);
43/* Users of stop_machine. */
44static int refcount;
Heiko Carstensc9583e52008-10-13 23:50:10 +020045static struct workqueue_struct *stop_machine_wq;
46static struct stop_machine_data active, idle;
47static const cpumask_t *active_cpus;
48static void *stop_machine_work;
49
Rusty Russellffdb5972008-07-28 12:16:28 -050050static void set_state(enum stopmachine_state newstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Rusty Russellffdb5972008-07-28 12:16:28 -050052 /* Reset ack counter. */
53 atomic_set(&thread_ack, num_threads);
54 smp_wmb();
55 state = newstate;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Rusty Russellffdb5972008-07-28 12:16:28 -050058/* Last one to ack a state moves to the next state. */
59static void ack_state(void)
60{
Heiko Carstensc9583e52008-10-13 23:50:10 +020061 if (atomic_dec_and_test(&thread_ack))
62 set_state(state + 1);
Rusty Russellffdb5972008-07-28 12:16:28 -050063}
Andrew Mortond8cb7c12006-07-03 17:32:22 -070064
Heiko Carstensc9583e52008-10-13 23:50:10 +020065/* This is the actual function which stops the CPU. It runs
66 * in the context of a dedicated stopmachine workqueue. */
67static void stop_cpu(struct work_struct *unused)
Rusty Russellffdb5972008-07-28 12:16:28 -050068{
69 enum stopmachine_state curstate = STOPMACHINE_NONE;
Heiko Carstensc9583e52008-10-13 23:50:10 +020070 struct stop_machine_data *smdata = &idle;
71 int cpu = smp_processor_id();
Heiko Carstens8163bca2008-10-22 10:00:26 -050072 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Heiko Carstensc9583e52008-10-13 23:50:10 +020074 if (!active_cpus) {
Rusty Russell41c7bb92009-01-01 10:12:28 +103075 if (cpu == cpumask_first(cpu_online_mask))
Heiko Carstensc9583e52008-10-13 23:50:10 +020076 smdata = &active;
77 } else {
Rusty Russell41c7bb92009-01-01 10:12:28 +103078 if (cpumask_test_cpu(cpu, active_cpus))
Heiko Carstensc9583e52008-10-13 23:50:10 +020079 smdata = &active;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* Simple state machine */
Rusty Russellffdb5972008-07-28 12:16:28 -050082 do {
83 /* Chill out and ensure we re-read stopmachine_state. */
Christian Borntraeger3401a61e2008-05-08 15:20:38 +020084 cpu_relax();
Rusty Russellffdb5972008-07-28 12:16:28 -050085 if (state != curstate) {
86 curstate = state;
87 switch (curstate) {
88 case STOPMACHINE_DISABLE_IRQ:
89 local_irq_disable();
90 hard_irq_disable();
91 break;
92 case STOPMACHINE_RUN:
Heiko Carstens8163bca2008-10-22 10:00:26 -050093 /* On multiple CPUs only a single error code
94 * is needed to tell that something failed. */
95 err = smdata->fn(smdata->data);
96 if (err)
97 smdata->fnret = err;
Rusty Russellffdb5972008-07-28 12:16:28 -050098 break;
99 default:
100 break;
101 }
102 ack_state();
103 }
104 } while (curstate != STOPMACHINE_EXIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Rusty Russellffdb5972008-07-28 12:16:28 -0500106 local_irq_enable();
Rusty Russellffdb5972008-07-28 12:16:28 -0500107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Rusty Russellffdb5972008-07-28 12:16:28 -0500109/* Callback for CPUs which aren't supposed to do anything. */
110static int chill(void *unused)
111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113}
114
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100115int stop_machine_create(void)
116{
117 mutex_lock(&setup_lock);
118 if (refcount)
119 goto done;
120 stop_machine_wq = create_rt_workqueue("kstop");
121 if (!stop_machine_wq)
122 goto err_out;
123 stop_machine_work = alloc_percpu(struct work_struct);
124 if (!stop_machine_work)
125 goto err_out;
126done:
127 refcount++;
128 mutex_unlock(&setup_lock);
129 return 0;
130
131err_out:
132 if (stop_machine_wq)
133 destroy_workqueue(stop_machine_wq);
134 mutex_unlock(&setup_lock);
135 return -ENOMEM;
136}
137EXPORT_SYMBOL_GPL(stop_machine_create);
138
139void stop_machine_destroy(void)
140{
141 mutex_lock(&setup_lock);
142 refcount--;
143 if (refcount)
144 goto done;
145 destroy_workqueue(stop_machine_wq);
146 free_percpu(stop_machine_work);
147done:
148 mutex_unlock(&setup_lock);
149}
150EXPORT_SYMBOL_GPL(stop_machine_destroy);
151
Rusty Russell41c7bb92009-01-01 10:12:28 +1030152int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Heiko Carstensc9583e52008-10-13 23:50:10 +0200154 struct work_struct *sm_work;
Rusty Russelle14c8bf2008-11-17 08:22:18 +1030155 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Heiko Carstensc9583e52008-10-13 23:50:10 +0200157 /* Set up initial state. */
158 mutex_lock(&lock);
159 num_threads = num_online_cpus();
160 active_cpus = cpus;
Rusty Russellffdb5972008-07-28 12:16:28 -0500161 active.fn = fn;
162 active.data = data;
163 active.fnret = 0;
164 idle.fn = chill;
165 idle.data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Rusty Russellffdb5972008-07-28 12:16:28 -0500167 set_state(STOPMACHINE_PREPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Heiko Carstensc9583e52008-10-13 23:50:10 +0200169 /* Schedule the stop_cpu work on all cpus: hold this CPU so one
Rusty Russellffdb5972008-07-28 12:16:28 -0500170 * doesn't hit this CPU until we're ready. */
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500171 get_cpu();
Heiko Carstensc9583e52008-10-13 23:50:10 +0200172 for_each_online_cpu(i) {
173 sm_work = percpu_ptr(stop_machine_work, i);
174 INIT_WORK(sm_work, stop_cpu);
175 queue_work_on(i, stop_machine_wq, sm_work);
176 }
Rusty Russellffdb5972008-07-28 12:16:28 -0500177 /* This will release the thread on our CPU. */
178 put_cpu();
Heiko Carstensc9583e52008-10-13 23:50:10 +0200179 flush_workqueue(stop_machine_wq);
Rusty Russelle14c8bf2008-11-17 08:22:18 +1030180 ret = active.fnret;
Rusty Russellffdb5972008-07-28 12:16:28 -0500181 mutex_unlock(&lock);
Rusty Russelle14c8bf2008-11-17 08:22:18 +1030182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Rusty Russell41c7bb92009-01-01 10:12:28 +1030185int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int ret;
188
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100189 ret = stop_machine_create();
190 if (ret)
191 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* No CPUs can come up or down during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100193 get_online_cpus();
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500194 ret = __stop_machine(fn, data, cpus);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100195 put_online_cpus();
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100196 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return ret;
198}
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500199EXPORT_SYMBOL_GPL(stop_machine);