blob: 24e8ceacc388cd14e9ff4ae87fcc47d6f8d96b2b [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Heiko Carstensc9583e52008-10-13 23:50:10 +020042static struct workqueue_struct *stop_machine_wq;
43static struct stop_machine_data active, idle;
44static const cpumask_t *active_cpus;
45static void *stop_machine_work;
46
Rusty Russellffdb5972008-07-28 12:16:28 -050047static void set_state(enum stopmachine_state newstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Rusty Russellffdb5972008-07-28 12:16:28 -050049 /* Reset ack counter. */
50 atomic_set(&thread_ack, num_threads);
51 smp_wmb();
52 state = newstate;
53}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Rusty Russellffdb5972008-07-28 12:16:28 -050055/* Last one to ack a state moves to the next state. */
56static void ack_state(void)
57{
Heiko Carstensc9583e52008-10-13 23:50:10 +020058 if (atomic_dec_and_test(&thread_ack))
59 set_state(state + 1);
Rusty Russellffdb5972008-07-28 12:16:28 -050060}
Andrew Mortond8cb7c12006-07-03 17:32:22 -070061
Heiko Carstensc9583e52008-10-13 23:50:10 +020062/* This is the actual function which stops the CPU. It runs
63 * in the context of a dedicated stopmachine workqueue. */
64static void stop_cpu(struct work_struct *unused)
Rusty Russellffdb5972008-07-28 12:16:28 -050065{
66 enum stopmachine_state curstate = STOPMACHINE_NONE;
Heiko Carstensc9583e52008-10-13 23:50:10 +020067 struct stop_machine_data *smdata = &idle;
68 int cpu = smp_processor_id();
Heiko Carstens8163bca2008-10-22 10:00:26 -050069 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Heiko Carstensc9583e52008-10-13 23:50:10 +020071 if (!active_cpus) {
72 if (cpu == first_cpu(cpu_online_map))
73 smdata = &active;
74 } else {
75 if (cpu_isset(cpu, *active_cpus))
76 smdata = &active;
77 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* Simple state machine */
Rusty Russellffdb5972008-07-28 12:16:28 -050079 do {
80 /* Chill out and ensure we re-read stopmachine_state. */
Christian Borntraeger3401a61e2008-05-08 15:20:38 +020081 cpu_relax();
Rusty Russellffdb5972008-07-28 12:16:28 -050082 if (state != curstate) {
83 curstate = state;
84 switch (curstate) {
85 case STOPMACHINE_DISABLE_IRQ:
86 local_irq_disable();
87 hard_irq_disable();
88 break;
89 case STOPMACHINE_RUN:
Heiko Carstens8163bca2008-10-22 10:00:26 -050090 /* On multiple CPUs only a single error code
91 * is needed to tell that something failed. */
92 err = smdata->fn(smdata->data);
93 if (err)
94 smdata->fnret = err;
Rusty Russellffdb5972008-07-28 12:16:28 -050095 break;
96 default:
97 break;
98 }
99 ack_state();
100 }
101 } while (curstate != STOPMACHINE_EXIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Rusty Russellffdb5972008-07-28 12:16:28 -0500103 local_irq_enable();
Rusty Russellffdb5972008-07-28 12:16:28 -0500104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Rusty Russellffdb5972008-07-28 12:16:28 -0500106/* Callback for CPUs which aren't supposed to do anything. */
107static int chill(void *unused)
108{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
110}
111
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500112int __stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Heiko Carstensc9583e52008-10-13 23:50:10 +0200114 struct work_struct *sm_work;
Rusty Russelle14c8bf2008-11-17 08:22:18 +1030115 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Heiko Carstensc9583e52008-10-13 23:50:10 +0200117 /* Set up initial state. */
118 mutex_lock(&lock);
119 num_threads = num_online_cpus();
120 active_cpus = cpus;
Rusty Russellffdb5972008-07-28 12:16:28 -0500121 active.fn = fn;
122 active.data = data;
123 active.fnret = 0;
124 idle.fn = chill;
125 idle.data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Rusty Russellffdb5972008-07-28 12:16:28 -0500127 set_state(STOPMACHINE_PREPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Heiko Carstensc9583e52008-10-13 23:50:10 +0200129 /* Schedule the stop_cpu work on all cpus: hold this CPU so one
Rusty Russellffdb5972008-07-28 12:16:28 -0500130 * doesn't hit this CPU until we're ready. */
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500131 get_cpu();
Heiko Carstensc9583e52008-10-13 23:50:10 +0200132 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);
136 }
Rusty Russellffdb5972008-07-28 12:16:28 -0500137 /* This will release the thread on our CPU. */
138 put_cpu();
Heiko Carstensc9583e52008-10-13 23:50:10 +0200139 flush_workqueue(stop_machine_wq);
Rusty Russelle14c8bf2008-11-17 08:22:18 +1030140 ret = active.fnret;
Rusty Russellffdb5972008-07-28 12:16:28 -0500141 mutex_unlock(&lock);
Rusty Russelle14c8bf2008-11-17 08:22:18 +1030142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500145int stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int ret;
148
149 /* No CPUs can come up or down during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100150 get_online_cpus();
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500151 ret = __stop_machine(fn, data, cpus);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100152 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return ret;
155}
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500156EXPORT_SYMBOL_GPL(stop_machine);
Heiko Carstensc9583e52008-10-13 23:50:10 +0200157
158static int __init stop_machine_init(void)
159{
160 stop_machine_wq = create_rt_workqueue("kstop");
161 stop_machine_work = alloc_percpu(struct work_struct);
162 return 0;
163}
Linus Torvalds4403b402008-10-25 19:53:38 -0700164core_initcall(stop_machine_init);