stop_machine.c 5.4 KB
Newer Older
R
Rusty Russell 已提交
1 2 3
/* Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation.
 * 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 16 17 18 19 20 21 22 23 24
#include <asm/atomic.h>
#include <asm/uaccess.h>

/* Since we effect priority and affinity (both of which are visible
 * to, and settable by outside processes) we do indirection via a
 * kthread. */

/* Thread to stop each CPU in user context. */
enum stopmachine_state {
	STOPMACHINE_WAIT,
	STOPMACHINE_PREPARE,
	STOPMACHINE_DISABLE_IRQ,
J
Jason Baron 已提交
25
	STOPMACHINE_RUN,
L
Linus Torvalds 已提交
26 27 28
	STOPMACHINE_EXIT,
};

J
Jason Baron 已提交
29 30 31 32 33 34 35
struct stop_machine_data {
	int (*fn)(void *);
	void *data;
	struct completion done;
	int run_all;
} smdata;

L
Linus Torvalds 已提交
36 37 38 39
static enum stopmachine_state stopmachine_state;
static unsigned int stopmachine_num_threads;
static atomic_t stopmachine_thread_ack;

40
static int stopmachine(void *cpu)
L
Linus Torvalds 已提交
41 42 43
{
	int irqs_disabled = 0;
	int prepared = 0;
J
Jason Baron 已提交
44
	int ran = 0;
45
	cpumask_of_cpu_ptr(cpumask, (int)(long)cpu);
L
Linus Torvalds 已提交
46

47
	set_cpus_allowed_ptr(current, cpumask);
48

L
Linus Torvalds 已提交
49
	/* Ack: we are alive */
50
	smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */
L
Linus Torvalds 已提交
51 52 53 54 55 56 57
	atomic_inc(&stopmachine_thread_ack);

	/* Simple state machine */
	while (stopmachine_state != STOPMACHINE_EXIT) {
		if (stopmachine_state == STOPMACHINE_DISABLE_IRQ 
		    && !irqs_disabled) {
			local_irq_disable();
58
			hard_irq_disable();
L
Linus Torvalds 已提交
59 60
			irqs_disabled = 1;
			/* Ack: irqs disabled. */
61
			smp_mb(); /* Must read state first. */
L
Linus Torvalds 已提交
62 63 64 65 66 67
			atomic_inc(&stopmachine_thread_ack);
		} else if (stopmachine_state == STOPMACHINE_PREPARE
			   && !prepared) {
			/* Everyone is in place, hold CPU. */
			preempt_disable();
			prepared = 1;
68
			smp_mb(); /* Must read state first. */
L
Linus Torvalds 已提交
69
			atomic_inc(&stopmachine_thread_ack);
J
Jason Baron 已提交
70 71 72 73 74
		} else if (stopmachine_state == STOPMACHINE_RUN && !ran) {
			smdata.fn(smdata.data);
			ran = 1;
			smp_mb(); /* Must read state first. */
			atomic_inc(&stopmachine_thread_ack);
L
Linus Torvalds 已提交
75 76 77 78 79
		}
		/* Yield in first stage: migration threads need to
		 * help our sisters onto their CPUs. */
		if (!prepared && !irqs_disabled)
			yield();
80
		cpu_relax();
L
Linus Torvalds 已提交
81 82 83
	}

	/* Ack: we are exiting. */
84
	smp_mb(); /* Must read state first. */
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	atomic_inc(&stopmachine_thread_ack);

	if (irqs_disabled)
		local_irq_enable();
	if (prepared)
		preempt_enable();

	return 0;
}

/* Change the thread state */
static void stopmachine_set_state(enum stopmachine_state state)
{
	atomic_set(&stopmachine_thread_ack, 0);
99
	smp_wmb();
L
Linus Torvalds 已提交
100 101 102 103 104 105 106
	stopmachine_state = state;
	while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads)
		cpu_relax();
}

static int stop_machine(void)
{
107
	int i, ret = 0;
L
Linus Torvalds 已提交
108 109 110 111 112 113

	atomic_set(&stopmachine_thread_ack, 0);
	stopmachine_num_threads = 0;
	stopmachine_state = STOPMACHINE_WAIT;

	for_each_online_cpu(i) {
I
Ingo Molnar 已提交
114
		if (i == raw_smp_processor_id())
L
Linus Torvalds 已提交
115
			continue;
116 117
		ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL);
		if (ret < 0)
L
Linus Torvalds 已提交
118 119 120 121 122
			break;
		stopmachine_num_threads++;
	}

	/* Wait for them all to come to life. */
123
	while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads) {
L
Linus Torvalds 已提交
124
		yield();
125 126
		cpu_relax();
	}
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134

	/* If some failed, kill them all. */
	if (ret < 0) {
		stopmachine_set_state(STOPMACHINE_EXIT);
		return ret;
	}

	/* Now they are all started, make them hold the CPUs, ready. */
135
	preempt_disable();
L
Linus Torvalds 已提交
136 137 138
	stopmachine_set_state(STOPMACHINE_PREPARE);

	/* Make them disable irqs. */
139
	local_irq_disable();
140
	hard_irq_disable();
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149
	stopmachine_set_state(STOPMACHINE_DISABLE_IRQ);

	return 0;
}

static void restart_machine(void)
{
	stopmachine_set_state(STOPMACHINE_EXIT);
	local_irq_enable();
150
	preempt_enable_no_resched();
L
Linus Torvalds 已提交
151 152
}

J
Jason Baron 已提交
153 154 155 156
static void run_other_cpus(void)
{
	stopmachine_set_state(STOPMACHINE_RUN);
}
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164 165

static int do_stop(void *_smdata)
{
	struct stop_machine_data *smdata = _smdata;
	int ret;

	ret = stop_machine();
	if (ret == 0) {
		ret = smdata->fn(smdata->data);
J
Jason Baron 已提交
166 167
		if (smdata->run_all)
			run_other_cpus();
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		restart_machine();
	}

	/* We're done: you can kthread_stop us now */
	complete(&smdata->done);

	/* Wait for kthread_stop */
	set_current_state(TASK_INTERRUPTIBLE);
	while (!kthread_should_stop()) {
		schedule();
		set_current_state(TASK_INTERRUPTIBLE);
	}
	__set_current_state(TASK_RUNNING);
	return ret;
}

struct task_struct *__stop_machine_run(int (*fn)(void *), void *data,
				       unsigned int cpu)
{
D
Daniel Walker 已提交
187
	static DEFINE_MUTEX(stopmachine_mutex);
L
Linus Torvalds 已提交
188 189 190
	struct stop_machine_data smdata;
	struct task_struct *p;

J
Jason Baron 已提交
191 192
	mutex_lock(&stopmachine_mutex);

L
Linus Torvalds 已提交
193 194
	smdata.fn = fn;
	smdata.data = data;
J
Jason Baron 已提交
195
	smdata.run_all = (cpu == ALL_CPUS) ? 1 : 0;
L
Linus Torvalds 已提交
196 197
	init_completion(&smdata.done);

J
Jason Baron 已提交
198
	smp_wmb(); /* make sure other cpus see smdata updates */
L
Linus Torvalds 已提交
199 200

	/* If they don't care which CPU fn runs on, bind to any online one. */
J
Jason Baron 已提交
201
	if (cpu == NR_CPUS || cpu == ALL_CPUS)
I
Ingo Molnar 已提交
202
		cpu = raw_smp_processor_id();
L
Linus Torvalds 已提交
203 204 205

	p = kthread_create(do_stop, &smdata, "kstopmachine");
	if (!IS_ERR(p)) {
206 207 208
		struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };

		/* One high-prio thread per cpu.  We'll do this one. */
209
		sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
L
Linus Torvalds 已提交
210 211 212 213
		kthread_bind(p, cpu);
		wake_up_process(p);
		wait_for_completion(&smdata.done);
	}
D
Daniel Walker 已提交
214
	mutex_unlock(&stopmachine_mutex);
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223
	return p;
}

int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
{
	struct task_struct *p;
	int ret;

	/* No CPUs can come up or down during this. */
224
	get_online_cpus();
L
Linus Torvalds 已提交
225 226 227 228 229
	p = __stop_machine_run(fn, data, cpu);
	if (!IS_ERR(p))
		ret = kthread_stop(p);
	else
		ret = PTR_ERR(p);
230
	put_online_cpus();
L
Linus Torvalds 已提交
231 232 233

	return ret;
}
234
EXPORT_SYMBOL_GPL(stop_machine_run);