stop_machine.c 3.8 KB
Newer Older
R
Rusty Russell 已提交
1
/* Copyright 2008, 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
R
Rusty Russell 已提交
2 3
 * GPL v2 and any later version.
 */
L
Linus Torvalds 已提交
4 5
#include <linux/cpu.h>
#include <linux/err.h>
6 7 8 9
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/stop_machine.h>
L
Linus Torvalds 已提交
10
#include <linux/syscalls.h>
11 12
#include <linux/interrupt.h>

L
Linus Torvalds 已提交
13 14 15
#include <asm/atomic.h>
#include <asm/uaccess.h>

R
Rusty Russell 已提交
16
/* This controls the threads on each CPU. */
L
Linus Torvalds 已提交
17
enum stopmachine_state {
R
Rusty Russell 已提交
18 19 20
	/* Dummy starting state for thread. */
	STOPMACHINE_NONE,
	/* Awaiting everyone to be scheduled. */
L
Linus Torvalds 已提交
21
	STOPMACHINE_PREPARE,
R
Rusty Russell 已提交
22
	/* Disable interrupts. */
L
Linus Torvalds 已提交
23
	STOPMACHINE_DISABLE_IRQ,
R
Rusty Russell 已提交
24
	/* Run the function */
J
Jason Baron 已提交
25
	STOPMACHINE_RUN,
R
Rusty Russell 已提交
26
	/* Exit */
L
Linus Torvalds 已提交
27 28
	STOPMACHINE_EXIT,
};
R
Rusty Russell 已提交
29
static enum stopmachine_state state;
L
Linus Torvalds 已提交
30

J
Jason Baron 已提交
31 32 33
struct stop_machine_data {
	int (*fn)(void *);
	void *data;
R
Rusty Russell 已提交
34 35
	int fnret;
};
J
Jason Baron 已提交
36

R
Rusty Russell 已提交
37 38 39 40
/* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
static unsigned int num_threads;
static atomic_t thread_ack;
static DEFINE_MUTEX(lock);
L
Linus Torvalds 已提交
41

42 43 44 45 46
static struct workqueue_struct *stop_machine_wq;
static struct stop_machine_data active, idle;
static const cpumask_t *active_cpus;
static void *stop_machine_work;

R
Rusty Russell 已提交
47
static void set_state(enum stopmachine_state newstate)
L
Linus Torvalds 已提交
48
{
R
Rusty Russell 已提交
49 50 51 52
	/* Reset ack counter. */
	atomic_set(&thread_ack, num_threads);
	smp_wmb();
	state = newstate;
L
Linus Torvalds 已提交
53 54
}

R
Rusty Russell 已提交
55 56
/* Last one to ack a state moves to the next state. */
static void ack_state(void)
L
Linus Torvalds 已提交
57
{
58 59
	if (atomic_dec_and_test(&thread_ack))
		set_state(state + 1);
L
Linus Torvalds 已提交
60 61
}

62 63 64
/* This is the actual function which stops the CPU. It runs
 * in the context of a dedicated stopmachine workqueue. */
static void stop_cpu(struct work_struct *unused)
L
Linus Torvalds 已提交
65
{
R
Rusty Russell 已提交
66
	enum stopmachine_state curstate = STOPMACHINE_NONE;
67 68
	struct stop_machine_data *smdata = &idle;
	int cpu = smp_processor_id();
69
	int err;
70 71 72 73 74 75 76 77

	if (!active_cpus) {
		if (cpu == first_cpu(cpu_online_map))
			smdata = &active;
	} else {
		if (cpu_isset(cpu, *active_cpus))
			smdata = &active;
	}
R
Rusty Russell 已提交
78 79 80
	/* Simple state machine */
	do {
		/* Chill out and ensure we re-read stopmachine_state. */
81
		cpu_relax();
R
Rusty Russell 已提交
82 83 84 85 86 87 88 89
		if (state != curstate) {
			curstate = state;
			switch (curstate) {
			case STOPMACHINE_DISABLE_IRQ:
				local_irq_disable();
				hard_irq_disable();
				break;
			case STOPMACHINE_RUN:
90 91 92 93 94
				/* On multiple CPUs only a single error code
				 * is needed to tell that something failed. */
				err = smdata->fn(smdata->data);
				if (err)
					smdata->fnret = err;
R
Rusty Russell 已提交
95 96 97 98 99 100 101
				break;
			default:
				break;
			}
			ack_state();
		}
	} while (curstate != STOPMACHINE_EXIT);
L
Linus Torvalds 已提交
102 103 104 105

	local_irq_enable();
}

R
Rusty Russell 已提交
106 107
/* Callback for CPUs which aren't supposed to do anything. */
static int chill(void *unused)
J
Jason Baron 已提交
108
{
R
Rusty Russell 已提交
109
	return 0;
J
Jason Baron 已提交
110
}
L
Linus Torvalds 已提交
111

112
int __stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
L
Linus Torvalds 已提交
113
{
114
	struct work_struct *sm_work;
115
	int i, ret;
R
Rusty Russell 已提交
116

117 118 119 120
	/* Set up initial state. */
	mutex_lock(&lock);
	num_threads = num_online_cpus();
	active_cpus = cpus;
R
Rusty Russell 已提交
121 122 123 124 125 126 127
	active.fn = fn;
	active.data = data;
	active.fnret = 0;
	idle.fn = chill;
	idle.data = NULL;

	set_state(STOPMACHINE_PREPARE);
L
Linus Torvalds 已提交
128

129
	/* Schedule the stop_cpu work on all cpus: hold this CPU so one
R
Rusty Russell 已提交
130
	 * doesn't hit this CPU until we're ready. */
131
	get_cpu();
132 133 134 135 136
	for_each_online_cpu(i) {
		sm_work = percpu_ptr(stop_machine_work, i);
		INIT_WORK(sm_work, stop_cpu);
		queue_work_on(i, stop_machine_wq, sm_work);
	}
R
Rusty Russell 已提交
137 138
	/* This will release the thread on our CPU. */
	put_cpu();
139
	flush_workqueue(stop_machine_wq);
140
	ret = active.fnret;
R
Rusty Russell 已提交
141
	mutex_unlock(&lock);
142
	return ret;
L
Linus Torvalds 已提交
143 144
}

145
int stop_machine(int (*fn)(void *), void *data, const cpumask_t *cpus)
L
Linus Torvalds 已提交
146 147 148 149
{
	int ret;

	/* No CPUs can come up or down during this. */
150
	get_online_cpus();
151
	ret = __stop_machine(fn, data, cpus);
152
	put_online_cpus();
L
Linus Torvalds 已提交
153 154 155

	return ret;
}
156
EXPORT_SYMBOL_GPL(stop_machine);
157 158 159 160 161 162 163

static int __init stop_machine_init(void)
{
	stop_machine_wq = create_rt_workqueue("kstop");
	stop_machine_work = alloc_percpu(struct work_struct);
	return 0;
}
164
core_initcall(stop_machine_init);