| Rusty Russell | e5582ca | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 1 | /* Copyright 2005 Rusty Russell rusty@rustcorp.com.au IBM Corporation. | 
 | 2 |  * GPL v2 and any later version. | 
 | 3 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/cpu.h> | 
 | 5 | #include <linux/err.h> | 
| Prarit Bhargava | ee527cd | 2007-05-08 00:25:08 -0700 | [diff] [blame] | 6 | #include <linux/kthread.h> | 
 | 7 | #include <linux/module.h> | 
 | 8 | #include <linux/sched.h> | 
 | 9 | #include <linux/stop_machine.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/syscalls.h> | 
| Benjamin Herrenschmidt | a12bb44 | 2007-05-10 22:22:47 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> | 
 | 12 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/atomic.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/uaccess.h> | 
 | 15 |  | 
 | 16 | /* Since we effect priority and affinity (both of which are visible | 
 | 17 |  * to, and settable by outside processes) we do indirection via a | 
 | 18 |  * kthread. */ | 
 | 19 |  | 
 | 20 | /* Thread to stop each CPU in user context. */ | 
 | 21 | enum stopmachine_state { | 
 | 22 | 	STOPMACHINE_WAIT, | 
 | 23 | 	STOPMACHINE_PREPARE, | 
 | 24 | 	STOPMACHINE_DISABLE_IRQ, | 
 | 25 | 	STOPMACHINE_EXIT, | 
 | 26 | }; | 
 | 27 |  | 
 | 28 | static enum stopmachine_state stopmachine_state; | 
 | 29 | static unsigned int stopmachine_num_threads; | 
 | 30 | static atomic_t stopmachine_thread_ack; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
| Andrew Morton | d8cb7c1 | 2006-07-03 17:32:22 -0700 | [diff] [blame] | 32 | static int stopmachine(void *cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { | 
 | 34 | 	int irqs_disabled = 0; | 
 | 35 | 	int prepared = 0; | 
 | 36 |  | 
| Mike Travis | f70316d | 2008-04-04 18:11:06 -0700 | [diff] [blame] | 37 | 	set_cpus_allowed_ptr(current, &cpumask_of_cpu((int)(long)cpu)); | 
| Andrew Morton | d8cb7c1 | 2006-07-03 17:32:22 -0700 | [diff] [blame] | 38 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | 	/* Ack: we are alive */ | 
| akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 40 | 	smp_mb(); /* Theoretically the ack = 0 might not be on this CPU yet. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | 	atomic_inc(&stopmachine_thread_ack); | 
 | 42 |  | 
 | 43 | 	/* Simple state machine */ | 
 | 44 | 	while (stopmachine_state != STOPMACHINE_EXIT) { | 
 | 45 | 		if (stopmachine_state == STOPMACHINE_DISABLE_IRQ  | 
 | 46 | 		    && !irqs_disabled) { | 
 | 47 | 			local_irq_disable(); | 
| Benjamin Herrenschmidt | a12bb44 | 2007-05-10 22:22:47 -0700 | [diff] [blame] | 48 | 			hard_irq_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | 			irqs_disabled = 1; | 
 | 50 | 			/* Ack: irqs disabled. */ | 
| akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 51 | 			smp_mb(); /* Must read state first. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | 			atomic_inc(&stopmachine_thread_ack); | 
 | 53 | 		} else if (stopmachine_state == STOPMACHINE_PREPARE | 
 | 54 | 			   && !prepared) { | 
 | 55 | 			/* Everyone is in place, hold CPU. */ | 
 | 56 | 			preempt_disable(); | 
 | 57 | 			prepared = 1; | 
| akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 58 | 			smp_mb(); /* Must read state first. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | 			atomic_inc(&stopmachine_thread_ack); | 
 | 60 | 		} | 
 | 61 | 		/* Yield in first stage: migration threads need to | 
 | 62 | 		 * help our sisters onto their CPUs. */ | 
 | 63 | 		if (!prepared && !irqs_disabled) | 
 | 64 | 			yield(); | 
| Christian Borntraeger | 3401a61 | 2008-05-08 15:20:38 +0200 | [diff] [blame] | 65 | 		cpu_relax(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | 	} | 
 | 67 |  | 
 | 68 | 	/* Ack: we are exiting. */ | 
| akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 69 | 	smp_mb(); /* Must read state first. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 	atomic_inc(&stopmachine_thread_ack); | 
 | 71 |  | 
 | 72 | 	if (irqs_disabled) | 
 | 73 | 		local_irq_enable(); | 
 | 74 | 	if (prepared) | 
 | 75 | 		preempt_enable(); | 
 | 76 |  | 
 | 77 | 	return 0; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | /* Change the thread state */ | 
 | 81 | static void stopmachine_set_state(enum stopmachine_state state) | 
 | 82 | { | 
 | 83 | 	atomic_set(&stopmachine_thread_ack, 0); | 
| akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 84 | 	smp_wmb(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | 	stopmachine_state = state; | 
 | 86 | 	while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads) | 
 | 87 | 		cpu_relax(); | 
 | 88 | } | 
 | 89 |  | 
 | 90 | static int stop_machine(void) | 
 | 91 | { | 
| Andrew Morton | d8cb7c1 | 2006-07-03 17:32:22 -0700 | [diff] [blame] | 92 | 	int i, ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 |  | 
 | 94 | 	atomic_set(&stopmachine_thread_ack, 0); | 
 | 95 | 	stopmachine_num_threads = 0; | 
 | 96 | 	stopmachine_state = STOPMACHINE_WAIT; | 
 | 97 |  | 
 | 98 | 	for_each_online_cpu(i) { | 
| Ingo Molnar | 39c715b | 2005-06-21 17:14:34 -0700 | [diff] [blame] | 99 | 		if (i == raw_smp_processor_id()) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | 			continue; | 
| Andrew Morton | d8cb7c1 | 2006-07-03 17:32:22 -0700 | [diff] [blame] | 101 | 		ret = kernel_thread(stopmachine, (void *)(long)i,CLONE_KERNEL); | 
 | 102 | 		if (ret < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | 			break; | 
 | 104 | 		stopmachine_num_threads++; | 
 | 105 | 	} | 
 | 106 |  | 
 | 107 | 	/* Wait for them all to come to life. */ | 
| Christian Borntraeger | 3401a61 | 2008-05-08 15:20:38 +0200 | [diff] [blame] | 108 | 	while (atomic_read(&stopmachine_thread_ack) != stopmachine_num_threads) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | 		yield(); | 
| Christian Borntraeger | 3401a61 | 2008-05-08 15:20:38 +0200 | [diff] [blame] | 110 | 		cpu_relax(); | 
 | 111 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 |  | 
 | 113 | 	/* If some failed, kill them all. */ | 
 | 114 | 	if (ret < 0) { | 
 | 115 | 		stopmachine_set_state(STOPMACHINE_EXIT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 		return ret; | 
 | 117 | 	} | 
 | 118 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | 	/* Now they are all started, make them hold the CPUs, ready. */ | 
| Kirill Korotaev | 4557398 | 2005-11-13 16:07:30 -0800 | [diff] [blame] | 120 | 	preempt_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 	stopmachine_set_state(STOPMACHINE_PREPARE); | 
 | 122 |  | 
 | 123 | 	/* Make them disable irqs. */ | 
| Kirill Korotaev | 4557398 | 2005-11-13 16:07:30 -0800 | [diff] [blame] | 124 | 	local_irq_disable(); | 
| Benjamin Herrenschmidt | a12bb44 | 2007-05-10 22:22:47 -0700 | [diff] [blame] | 125 | 	hard_irq_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 	stopmachine_set_state(STOPMACHINE_DISABLE_IRQ); | 
 | 127 |  | 
 | 128 | 	return 0; | 
 | 129 | } | 
 | 130 |  | 
 | 131 | static void restart_machine(void) | 
 | 132 | { | 
 | 133 | 	stopmachine_set_state(STOPMACHINE_EXIT); | 
 | 134 | 	local_irq_enable(); | 
| Kirill Korotaev | 4557398 | 2005-11-13 16:07:30 -0800 | [diff] [blame] | 135 | 	preempt_enable_no_resched(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } | 
 | 137 |  | 
| Pavel Machek | f526448 | 2008-04-21 22:15:06 +0000 | [diff] [blame] | 138 | struct stop_machine_data { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | 	int (*fn)(void *); | 
 | 140 | 	void *data; | 
 | 141 | 	struct completion done; | 
 | 142 | }; | 
 | 143 |  | 
 | 144 | static int do_stop(void *_smdata) | 
 | 145 | { | 
 | 146 | 	struct stop_machine_data *smdata = _smdata; | 
 | 147 | 	int ret; | 
 | 148 |  | 
 | 149 | 	ret = stop_machine(); | 
 | 150 | 	if (ret == 0) { | 
 | 151 | 		ret = smdata->fn(smdata->data); | 
 | 152 | 		restart_machine(); | 
 | 153 | 	} | 
 | 154 |  | 
 | 155 | 	/* We're done: you can kthread_stop us now */ | 
 | 156 | 	complete(&smdata->done); | 
 | 157 |  | 
 | 158 | 	/* Wait for kthread_stop */ | 
 | 159 | 	set_current_state(TASK_INTERRUPTIBLE); | 
 | 160 | 	while (!kthread_should_stop()) { | 
 | 161 | 		schedule(); | 
 | 162 | 		set_current_state(TASK_INTERRUPTIBLE); | 
 | 163 | 	} | 
 | 164 | 	__set_current_state(TASK_RUNNING); | 
 | 165 | 	return ret; | 
 | 166 | } | 
 | 167 |  | 
 | 168 | struct task_struct *__stop_machine_run(int (*fn)(void *), void *data, | 
 | 169 | 				       unsigned int cpu) | 
 | 170 | { | 
| Daniel Walker | 6c6080f | 2008-02-06 01:37:41 -0800 | [diff] [blame] | 171 | 	static DEFINE_MUTEX(stopmachine_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | 	struct stop_machine_data smdata; | 
 | 173 | 	struct task_struct *p; | 
 | 174 |  | 
 | 175 | 	smdata.fn = fn; | 
 | 176 | 	smdata.data = data; | 
 | 177 | 	init_completion(&smdata.done); | 
 | 178 |  | 
| Daniel Walker | 6c6080f | 2008-02-06 01:37:41 -0800 | [diff] [blame] | 179 | 	mutex_lock(&stopmachine_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 |  | 
 | 181 | 	/* If they don't care which CPU fn runs on, bind to any online one. */ | 
 | 182 | 	if (cpu == NR_CPUS) | 
| Ingo Molnar | 39c715b | 2005-06-21 17:14:34 -0700 | [diff] [blame] | 183 | 		cpu = raw_smp_processor_id(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 |  | 
 | 185 | 	p = kthread_create(do_stop, &smdata, "kstopmachine"); | 
 | 186 | 	if (!IS_ERR(p)) { | 
| Satoru Takeuchi | 85653af | 2007-07-15 23:39:47 -0700 | [diff] [blame] | 187 | 		struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; | 
 | 188 |  | 
 | 189 | 		/* One high-prio thread per cpu.  We'll do this one. */ | 
 | 190 | 		sched_setscheduler(p, SCHED_FIFO, ¶m); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | 		kthread_bind(p, cpu); | 
 | 192 | 		wake_up_process(p); | 
 | 193 | 		wait_for_completion(&smdata.done); | 
 | 194 | 	} | 
| Daniel Walker | 6c6080f | 2008-02-06 01:37:41 -0800 | [diff] [blame] | 195 | 	mutex_unlock(&stopmachine_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 	return p; | 
 | 197 | } | 
 | 198 |  | 
 | 199 | int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu) | 
 | 200 | { | 
 | 201 | 	struct task_struct *p; | 
 | 202 | 	int ret; | 
 | 203 |  | 
 | 204 | 	/* No CPUs can come up or down during this. */ | 
| Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 205 | 	get_online_cpus(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 	p = __stop_machine_run(fn, data, cpu); | 
 | 207 | 	if (!IS_ERR(p)) | 
 | 208 | 		ret = kthread_stop(p); | 
 | 209 | 	else | 
 | 210 | 		ret = PTR_ERR(p); | 
| Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 211 | 	put_online_cpus(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 |  | 
 | 213 | 	return ret; | 
 | 214 | } | 
| Prarit Bhargava | ee527cd | 2007-05-08 00:25:08 -0700 | [diff] [blame] | 215 | EXPORT_SYMBOL_GPL(stop_machine_run); |