| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * kernel/stop_machine.c | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2008, 2005	IBM Corporation. | 
|  | 5 | * Copyright (C) 2008, 2005	Rusty Russell rusty@rustcorp.com.au | 
|  | 6 | * Copyright (C) 2010		SUSE Linux Products GmbH | 
|  | 7 | * Copyright (C) 2010		Tejun Heo <tj@kernel.org> | 
|  | 8 | * | 
|  | 9 | * This file is released under the GPLv2 and any later version. | 
|  | 10 | */ | 
|  | 11 | #include <linux/completion.h> | 
|  | 12 | #include <linux/cpu.h> | 
|  | 13 | #include <linux/init.h> | 
|  | 14 | #include <linux/kthread.h> | 
|  | 15 | #include <linux/export.h> | 
|  | 16 | #include <linux/percpu.h> | 
|  | 17 | #include <linux/sched.h> | 
|  | 18 | #include <linux/stop_machine.h> | 
|  | 19 | #include <linux/interrupt.h> | 
|  | 20 | #include <linux/kallsyms.h> | 
|  | 21 |  | 
|  | 22 | #include <linux/atomic.h> | 
|  | 23 |  | 
|  | 24 | /* | 
|  | 25 | * Structure to determine completion condition and record errors.  May | 
|  | 26 | * be shared by works on different cpus. | 
|  | 27 | */ | 
|  | 28 | struct cpu_stop_done { | 
|  | 29 | atomic_t		nr_todo;	/* nr left to execute */ | 
|  | 30 | bool			executed;	/* actually executed? */ | 
|  | 31 | int			ret;		/* collected return value */ | 
|  | 32 | struct task_struct	*waiter;	/* woken when nr_todo reaches 0 */ | 
|  | 33 | }; | 
|  | 34 |  | 
|  | 35 | /* the actual stopper, one per every possible cpu, enabled on online cpus */ | 
|  | 36 | struct cpu_stopper { | 
|  | 37 | raw_spinlock_t		lock; | 
|  | 38 | bool			enabled;	/* is this stopper enabled? */ | 
|  | 39 | struct list_head	works;		/* list of pending works */ | 
|  | 40 | struct task_struct	*thread;	/* stopper thread */ | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper); | 
|  | 44 | static bool stop_machine_initialized = false; | 
|  | 45 |  | 
|  | 46 | static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo) | 
|  | 47 | { | 
|  | 48 | memset(done, 0, sizeof(*done)); | 
|  | 49 | atomic_set(&done->nr_todo, nr_todo); | 
|  | 50 | done->waiter = current; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | /* signal completion unless @done is NULL */ | 
|  | 54 | static void cpu_stop_signal_done(struct cpu_stop_done *done, bool executed) | 
|  | 55 | { | 
|  | 56 | if (done) { | 
|  | 57 | if (executed) | 
|  | 58 | done->executed = true; | 
|  | 59 | if (atomic_dec_and_test(&done->nr_todo)) { | 
|  | 60 | wake_up_process(done->waiter); | 
|  | 61 | done->waiter = NULL; | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | /* queue @work to @stopper.  if offline, @work is completed immediately */ | 
|  | 67 | static void cpu_stop_queue_work(struct cpu_stopper *stopper, | 
|  | 68 | struct cpu_stop_work *work) | 
|  | 69 | { | 
|  | 70 | unsigned long flags; | 
|  | 71 |  | 
|  | 72 | raw_spin_lock_irqsave(&stopper->lock, flags); | 
|  | 73 |  | 
|  | 74 | if (stopper->enabled) { | 
|  | 75 | list_add_tail(&work->list, &stopper->works); | 
|  | 76 | wake_up_process(stopper->thread); | 
|  | 77 | } else | 
|  | 78 | cpu_stop_signal_done(work->done, false); | 
|  | 79 |  | 
|  | 80 | raw_spin_unlock_irqrestore(&stopper->lock, flags); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | static void wait_for_stop_done(struct cpu_stop_done *done) | 
|  | 84 | { | 
|  | 85 | set_current_state(TASK_UNINTERRUPTIBLE); | 
|  | 86 | while (atomic_read(&done->nr_todo)) { | 
|  | 87 | schedule(); | 
|  | 88 | set_current_state(TASK_UNINTERRUPTIBLE); | 
|  | 89 | } | 
|  | 90 | /* | 
|  | 91 | * We need to wait until cpu_stop_signal_done() has cleared | 
|  | 92 | * done->waiter. | 
|  | 93 | */ | 
|  | 94 | while (done->waiter) | 
|  | 95 | cpu_relax(); | 
|  | 96 | set_current_state(TASK_RUNNING); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | /** | 
|  | 100 | * stop_one_cpu - stop a cpu | 
|  | 101 | * @cpu: cpu to stop | 
|  | 102 | * @fn: function to execute | 
|  | 103 | * @arg: argument to @fn | 
|  | 104 | * | 
|  | 105 | * Execute @fn(@arg) on @cpu.  @fn is run in a process context with | 
|  | 106 | * the highest priority preempting any task on the cpu and | 
|  | 107 | * monopolizing it.  This function returns after the execution is | 
|  | 108 | * complete. | 
|  | 109 | * | 
|  | 110 | * This function doesn't guarantee @cpu stays online till @fn | 
|  | 111 | * completes.  If @cpu goes down in the middle, execution may happen | 
|  | 112 | * partially or fully on different cpus.  @fn should either be ready | 
|  | 113 | * for that or the caller should ensure that @cpu stays online until | 
|  | 114 | * this function completes. | 
|  | 115 | * | 
|  | 116 | * CONTEXT: | 
|  | 117 | * Might sleep. | 
|  | 118 | * | 
|  | 119 | * RETURNS: | 
|  | 120 | * -ENOENT if @fn(@arg) was not executed because @cpu was offline; | 
|  | 121 | * otherwise, the return value of @fn. | 
|  | 122 | */ | 
|  | 123 | int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) | 
|  | 124 | { | 
|  | 125 | struct cpu_stop_done done; | 
|  | 126 | struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done }; | 
|  | 127 |  | 
|  | 128 | cpu_stop_init_done(&done, 1); | 
|  | 129 | cpu_stop_queue_work(&per_cpu(cpu_stopper, cpu), &work); | 
|  | 130 | wait_for_stop_done(&done); | 
|  | 131 | return done.executed ? done.ret : -ENOENT; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | /** | 
|  | 135 | * stop_one_cpu_nowait - stop a cpu but don't wait for completion | 
|  | 136 | * @cpu: cpu to stop | 
|  | 137 | * @fn: function to execute | 
|  | 138 | * @arg: argument to @fn | 
|  | 139 | * | 
|  | 140 | * Similar to stop_one_cpu() but doesn't wait for completion.  The | 
|  | 141 | * caller is responsible for ensuring @work_buf is currently unused | 
|  | 142 | * and will remain untouched until stopper starts executing @fn. | 
|  | 143 | * | 
|  | 144 | * CONTEXT: | 
|  | 145 | * Don't care. | 
|  | 146 | */ | 
|  | 147 | void stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg, | 
|  | 148 | struct cpu_stop_work *work_buf) | 
|  | 149 | { | 
|  | 150 | *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, }; | 
|  | 151 | cpu_stop_queue_work(&per_cpu(cpu_stopper, cpu), work_buf); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | /* static data for stop_cpus */ | 
|  | 155 | static DEFINE_MUTEX(stop_cpus_mutex); | 
|  | 156 | static DEFINE_MUTEX(stopper_lock); | 
|  | 157 | static DEFINE_PER_CPU(struct cpu_stop_work, stop_cpus_work); | 
|  | 158 |  | 
|  | 159 | static void queue_stop_cpus_work(const struct cpumask *cpumask, | 
|  | 160 | cpu_stop_fn_t fn, void *arg, | 
|  | 161 | struct cpu_stop_done *done, bool inactive) | 
|  | 162 | { | 
|  | 163 | struct cpu_stop_work *work; | 
|  | 164 | unsigned int cpu; | 
|  | 165 |  | 
|  | 166 | /* initialize works and done */ | 
|  | 167 | for_each_cpu(cpu, cpumask) { | 
|  | 168 | work = &per_cpu(stop_cpus_work, cpu); | 
|  | 169 | work->fn = fn; | 
|  | 170 | work->arg = arg; | 
|  | 171 | work->done = done; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | /* | 
|  | 175 | * Make sure that all work is queued on all cpus before we | 
|  | 176 | * any of the cpus can execute it. | 
|  | 177 | */ | 
|  | 178 | if (!inactive) { | 
|  | 179 | mutex_lock(&stopper_lock); | 
|  | 180 | } else { | 
|  | 181 | while (!mutex_trylock(&stopper_lock)) | 
|  | 182 | cpu_relax(); | 
|  | 183 | } | 
|  | 184 | for_each_cpu(cpu, cpumask) | 
|  | 185 | cpu_stop_queue_work(&per_cpu(cpu_stopper, cpu), | 
|  | 186 | &per_cpu(stop_cpus_work, cpu)); | 
|  | 187 | mutex_unlock(&stopper_lock); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | static int __stop_cpus(const struct cpumask *cpumask, | 
|  | 191 | cpu_stop_fn_t fn, void *arg) | 
|  | 192 | { | 
|  | 193 | struct cpu_stop_done done; | 
|  | 194 |  | 
|  | 195 | cpu_stop_init_done(&done, cpumask_weight(cpumask)); | 
|  | 196 | queue_stop_cpus_work(cpumask, fn, arg, &done, false); | 
|  | 197 | wait_for_stop_done(&done); | 
|  | 198 | return done.executed ? done.ret : -ENOENT; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | /** | 
|  | 202 | * stop_cpus - stop multiple cpus | 
|  | 203 | * @cpumask: cpus to stop | 
|  | 204 | * @fn: function to execute | 
|  | 205 | * @arg: argument to @fn | 
|  | 206 | * | 
|  | 207 | * Execute @fn(@arg) on online cpus in @cpumask.  On each target cpu, | 
|  | 208 | * @fn is run in a process context with the highest priority | 
|  | 209 | * preempting any task on the cpu and monopolizing it.  This function | 
|  | 210 | * returns after all executions are complete. | 
|  | 211 | * | 
|  | 212 | * This function doesn't guarantee the cpus in @cpumask stay online | 
|  | 213 | * till @fn completes.  If some cpus go down in the middle, execution | 
|  | 214 | * on the cpu may happen partially or fully on different cpus.  @fn | 
|  | 215 | * should either be ready for that or the caller should ensure that | 
|  | 216 | * the cpus stay online until this function completes. | 
|  | 217 | * | 
|  | 218 | * All stop_cpus() calls are serialized making it safe for @fn to wait | 
|  | 219 | * for all cpus to start executing it. | 
|  | 220 | * | 
|  | 221 | * CONTEXT: | 
|  | 222 | * Might sleep. | 
|  | 223 | * | 
|  | 224 | * RETURNS: | 
|  | 225 | * -ENOENT if @fn(@arg) was not executed at all because all cpus in | 
|  | 226 | * @cpumask were offline; otherwise, 0 if all executions of @fn | 
|  | 227 | * returned 0, any non zero return value if any returned non zero. | 
|  | 228 | */ | 
|  | 229 | int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) | 
|  | 230 | { | 
|  | 231 | int ret; | 
|  | 232 |  | 
|  | 233 | /* static works are used, process one request at a time */ | 
|  | 234 | mutex_lock(&stop_cpus_mutex); | 
|  | 235 | ret = __stop_cpus(cpumask, fn, arg); | 
|  | 236 | mutex_unlock(&stop_cpus_mutex); | 
|  | 237 | return ret; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | /** | 
|  | 241 | * try_stop_cpus - try to stop multiple cpus | 
|  | 242 | * @cpumask: cpus to stop | 
|  | 243 | * @fn: function to execute | 
|  | 244 | * @arg: argument to @fn | 
|  | 245 | * | 
|  | 246 | * Identical to stop_cpus() except that it fails with -EAGAIN if | 
|  | 247 | * someone else is already using the facility. | 
|  | 248 | * | 
|  | 249 | * CONTEXT: | 
|  | 250 | * Might sleep. | 
|  | 251 | * | 
|  | 252 | * RETURNS: | 
|  | 253 | * -EAGAIN if someone else is already stopping cpus, -ENOENT if | 
|  | 254 | * @fn(@arg) was not executed at all because all cpus in @cpumask were | 
|  | 255 | * offline; otherwise, 0 if all executions of @fn returned 0, any non | 
|  | 256 | * zero return value if any returned non zero. | 
|  | 257 | */ | 
|  | 258 | int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) | 
|  | 259 | { | 
|  | 260 | int ret; | 
|  | 261 |  | 
|  | 262 | /* static works are used, process one request at a time */ | 
|  | 263 | if (!mutex_trylock(&stop_cpus_mutex)) | 
|  | 264 | return -EAGAIN; | 
|  | 265 | ret = __stop_cpus(cpumask, fn, arg); | 
|  | 266 | mutex_unlock(&stop_cpus_mutex); | 
|  | 267 | return ret; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | static int cpu_stopper_thread(void *data) | 
|  | 271 | { | 
|  | 272 | struct cpu_stopper *stopper = data; | 
|  | 273 | struct cpu_stop_work *work; | 
|  | 274 | int ret; | 
|  | 275 |  | 
|  | 276 | repeat: | 
|  | 277 | set_current_state(TASK_INTERRUPTIBLE);	/* mb paired w/ kthread_stop */ | 
|  | 278 |  | 
|  | 279 | if (kthread_should_stop()) { | 
|  | 280 | __set_current_state(TASK_RUNNING); | 
|  | 281 | return 0; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | work = NULL; | 
|  | 285 | raw_spin_lock_irq(&stopper->lock); | 
|  | 286 | if (!list_empty(&stopper->works)) { | 
|  | 287 | work = list_first_entry(&stopper->works, | 
|  | 288 | struct cpu_stop_work, list); | 
|  | 289 | list_del_init(&work->list); | 
|  | 290 | } | 
|  | 291 | raw_spin_unlock_irq(&stopper->lock); | 
|  | 292 |  | 
|  | 293 | if (work) { | 
|  | 294 | cpu_stop_fn_t fn = work->fn; | 
|  | 295 | void *arg = work->arg; | 
|  | 296 | struct cpu_stop_done *done = work->done; | 
|  | 297 | char ksym_buf[KSYM_NAME_LEN] __maybe_unused; | 
|  | 298 |  | 
|  | 299 | __set_current_state(TASK_RUNNING); | 
|  | 300 |  | 
|  | 301 | /* | 
|  | 302 | * Wait until the stopper finished scheduling on all | 
|  | 303 | * cpus | 
|  | 304 | */ | 
|  | 305 | mutex_lock(&stopper_lock); | 
|  | 306 | /* | 
|  | 307 | * Let other cpu threads continue as well | 
|  | 308 | */ | 
|  | 309 | mutex_unlock(&stopper_lock); | 
|  | 310 |  | 
|  | 311 | /* cpu stop callbacks are not allowed to sleep */ | 
|  | 312 | preempt_disable(); | 
|  | 313 |  | 
|  | 314 | ret = fn(arg); | 
|  | 315 | if (ret) | 
|  | 316 | done->ret = ret; | 
|  | 317 |  | 
|  | 318 | /* restore preemption and check it's still balanced */ | 
|  | 319 | preempt_enable(); | 
|  | 320 | WARN_ONCE(preempt_count(), | 
|  | 321 | "cpu_stop: %s(%p) leaked preempt count\n", | 
|  | 322 | kallsyms_lookup((unsigned long)fn, NULL, NULL, NULL, | 
|  | 323 | ksym_buf), arg); | 
|  | 324 |  | 
|  | 325 | /* | 
|  | 326 | * Make sure that the wakeup and setting done->waiter | 
|  | 327 | * to NULL is atomic. | 
|  | 328 | */ | 
|  | 329 | local_irq_disable(); | 
|  | 330 | cpu_stop_signal_done(done, true); | 
|  | 331 | local_irq_enable(); | 
|  | 332 | } else | 
|  | 333 | schedule(); | 
|  | 334 |  | 
|  | 335 | goto repeat; | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | extern void sched_set_stop_task(int cpu, struct task_struct *stop); | 
|  | 339 |  | 
|  | 340 | /* manage stopper for a cpu, mostly lifted from sched migration thread mgmt */ | 
|  | 341 | static int __cpuinit cpu_stop_cpu_callback(struct notifier_block *nfb, | 
|  | 342 | unsigned long action, void *hcpu) | 
|  | 343 | { | 
|  | 344 | unsigned int cpu = (unsigned long)hcpu; | 
|  | 345 | struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); | 
|  | 346 | struct task_struct *p; | 
|  | 347 |  | 
|  | 348 | switch (action & ~CPU_TASKS_FROZEN) { | 
|  | 349 | case CPU_UP_PREPARE: | 
|  | 350 | BUG_ON(stopper->thread || stopper->enabled || | 
|  | 351 | !list_empty(&stopper->works)); | 
|  | 352 | p = kthread_create_on_node(cpu_stopper_thread, | 
|  | 353 | stopper, | 
|  | 354 | cpu_to_node(cpu), | 
|  | 355 | "migration/%d", cpu); | 
|  | 356 | if (IS_ERR(p)) | 
|  | 357 | return notifier_from_errno(PTR_ERR(p)); | 
|  | 358 | get_task_struct(p); | 
|  | 359 | p->flags |= PF_STOMPER; | 
|  | 360 | kthread_bind(p, cpu); | 
|  | 361 | sched_set_stop_task(cpu, p); | 
|  | 362 | stopper->thread = p; | 
|  | 363 | break; | 
|  | 364 |  | 
|  | 365 | case CPU_ONLINE: | 
|  | 366 | /* strictly unnecessary, as first user will wake it */ | 
|  | 367 | wake_up_process(stopper->thread); | 
|  | 368 | /* mark enabled */ | 
|  | 369 | raw_spin_lock_irq(&stopper->lock); | 
|  | 370 | stopper->enabled = true; | 
|  | 371 | raw_spin_unlock_irq(&stopper->lock); | 
|  | 372 | break; | 
|  | 373 |  | 
|  | 374 | #ifdef CONFIG_HOTPLUG_CPU | 
|  | 375 | case CPU_UP_CANCELED: | 
|  | 376 | case CPU_POST_DEAD: | 
|  | 377 | { | 
|  | 378 | struct cpu_stop_work *work; | 
|  | 379 |  | 
|  | 380 | sched_set_stop_task(cpu, NULL); | 
|  | 381 | /* kill the stopper */ | 
|  | 382 | kthread_stop(stopper->thread); | 
|  | 383 | /* drain remaining works */ | 
|  | 384 | raw_spin_lock_irq(&stopper->lock); | 
|  | 385 | list_for_each_entry(work, &stopper->works, list) | 
|  | 386 | cpu_stop_signal_done(work->done, false); | 
|  | 387 | stopper->enabled = false; | 
|  | 388 | raw_spin_unlock_irq(&stopper->lock); | 
|  | 389 | /* release the stopper */ | 
|  | 390 | put_task_struct(stopper->thread); | 
|  | 391 | stopper->thread = NULL; | 
|  | 392 | break; | 
|  | 393 | } | 
|  | 394 | #endif | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | return NOTIFY_OK; | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | /* | 
|  | 401 | * Give it a higher priority so that cpu stopper is available to other | 
|  | 402 | * cpu notifiers.  It currently shares the same priority as sched | 
|  | 403 | * migration_notifier. | 
|  | 404 | */ | 
|  | 405 | static struct notifier_block __cpuinitdata cpu_stop_cpu_notifier = { | 
|  | 406 | .notifier_call	= cpu_stop_cpu_callback, | 
|  | 407 | .priority	= 10, | 
|  | 408 | }; | 
|  | 409 |  | 
|  | 410 | static int __init cpu_stop_init(void) | 
|  | 411 | { | 
|  | 412 | void *bcpu = (void *)(long)smp_processor_id(); | 
|  | 413 | unsigned int cpu; | 
|  | 414 | int err; | 
|  | 415 |  | 
|  | 416 | for_each_possible_cpu(cpu) { | 
|  | 417 | struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); | 
|  | 418 |  | 
|  | 419 | raw_spin_lock_init(&stopper->lock); | 
|  | 420 | INIT_LIST_HEAD(&stopper->works); | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | /* start one for the boot cpu */ | 
|  | 424 | err = cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_UP_PREPARE, | 
|  | 425 | bcpu); | 
|  | 426 | BUG_ON(err != NOTIFY_OK); | 
|  | 427 | cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_ONLINE, bcpu); | 
|  | 428 | register_cpu_notifier(&cpu_stop_cpu_notifier); | 
|  | 429 |  | 
|  | 430 | stop_machine_initialized = true; | 
|  | 431 |  | 
|  | 432 | return 0; | 
|  | 433 | } | 
|  | 434 | early_initcall(cpu_stop_init); | 
|  | 435 |  | 
|  | 436 | #ifdef CONFIG_STOP_MACHINE | 
|  | 437 |  | 
|  | 438 | /* This controls the threads on each CPU. */ | 
|  | 439 | enum stopmachine_state { | 
|  | 440 | /* Dummy starting state for thread. */ | 
|  | 441 | STOPMACHINE_NONE, | 
|  | 442 | /* Awaiting everyone to be scheduled. */ | 
|  | 443 | STOPMACHINE_PREPARE, | 
|  | 444 | /* Disable interrupts. */ | 
|  | 445 | STOPMACHINE_DISABLE_IRQ, | 
|  | 446 | /* Run the function */ | 
|  | 447 | STOPMACHINE_RUN, | 
|  | 448 | /* Exit */ | 
|  | 449 | STOPMACHINE_EXIT, | 
|  | 450 | }; | 
|  | 451 |  | 
|  | 452 | struct stop_machine_data { | 
|  | 453 | int			(*fn)(void *); | 
|  | 454 | void			*data; | 
|  | 455 | /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */ | 
|  | 456 | unsigned int		num_threads; | 
|  | 457 | const struct cpumask	*active_cpus; | 
|  | 458 |  | 
|  | 459 | enum stopmachine_state	state; | 
|  | 460 | atomic_t		thread_ack; | 
|  | 461 | }; | 
|  | 462 |  | 
|  | 463 | static void set_state(struct stop_machine_data *smdata, | 
|  | 464 | enum stopmachine_state newstate) | 
|  | 465 | { | 
|  | 466 | /* Reset ack counter. */ | 
|  | 467 | atomic_set(&smdata->thread_ack, smdata->num_threads); | 
|  | 468 | smp_wmb(); | 
|  | 469 | smdata->state = newstate; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | /* Last one to ack a state moves to the next state. */ | 
|  | 473 | static void ack_state(struct stop_machine_data *smdata) | 
|  | 474 | { | 
|  | 475 | if (atomic_dec_and_test(&smdata->thread_ack)) | 
|  | 476 | set_state(smdata, smdata->state + 1); | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | /* This is the cpu_stop function which stops the CPU. */ | 
|  | 480 | static int stop_machine_cpu_stop(void *data) | 
|  | 481 | { | 
|  | 482 | struct stop_machine_data *smdata = data; | 
|  | 483 | enum stopmachine_state curstate = STOPMACHINE_NONE; | 
|  | 484 | int cpu = smp_processor_id(), err = 0; | 
|  | 485 | unsigned long flags; | 
|  | 486 | bool is_active; | 
|  | 487 |  | 
|  | 488 | /* | 
|  | 489 | * When called from stop_machine_from_inactive_cpu(), irq might | 
|  | 490 | * already be disabled.  Save the state and restore it on exit. | 
|  | 491 | */ | 
|  | 492 | local_save_flags(flags); | 
|  | 493 |  | 
|  | 494 | if (!smdata->active_cpus) | 
|  | 495 | is_active = cpu == cpumask_first(cpu_online_mask); | 
|  | 496 | else | 
|  | 497 | is_active = cpumask_test_cpu(cpu, smdata->active_cpus); | 
|  | 498 |  | 
|  | 499 | /* Simple state machine */ | 
|  | 500 | do { | 
|  | 501 | /* Chill out and ensure we re-read stopmachine_state. */ | 
|  | 502 | cpu_relax(); | 
|  | 503 | if (smdata->state != curstate) { | 
|  | 504 | curstate = smdata->state; | 
|  | 505 | switch (curstate) { | 
|  | 506 | case STOPMACHINE_DISABLE_IRQ: | 
|  | 507 | local_irq_disable(); | 
|  | 508 | hard_irq_disable(); | 
|  | 509 | break; | 
|  | 510 | case STOPMACHINE_RUN: | 
|  | 511 | if (is_active) | 
|  | 512 | err = smdata->fn(smdata->data); | 
|  | 513 | break; | 
|  | 514 | default: | 
|  | 515 | break; | 
|  | 516 | } | 
|  | 517 | ack_state(smdata); | 
|  | 518 | } | 
|  | 519 | } while (curstate != STOPMACHINE_EXIT); | 
|  | 520 |  | 
|  | 521 | local_irq_restore(flags); | 
|  | 522 | return err; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus) | 
|  | 526 | { | 
|  | 527 | struct stop_machine_data smdata = { .fn = fn, .data = data, | 
|  | 528 | .num_threads = num_online_cpus(), | 
|  | 529 | .active_cpus = cpus }; | 
|  | 530 |  | 
|  | 531 | if (!stop_machine_initialized) { | 
|  | 532 | /* | 
|  | 533 | * Handle the case where stop_machine() is called | 
|  | 534 | * early in boot before stop_machine() has been | 
|  | 535 | * initialized. | 
|  | 536 | */ | 
|  | 537 | unsigned long flags; | 
|  | 538 | int ret; | 
|  | 539 |  | 
|  | 540 | WARN_ON_ONCE(smdata.num_threads != 1); | 
|  | 541 |  | 
|  | 542 | local_irq_save(flags); | 
|  | 543 | hard_irq_disable(); | 
|  | 544 | ret = (*fn)(data); | 
|  | 545 | local_irq_restore(flags); | 
|  | 546 |  | 
|  | 547 | return ret; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | /* Set the initial state and stop all online cpus. */ | 
|  | 551 | set_state(&smdata, STOPMACHINE_PREPARE); | 
|  | 552 | return stop_cpus(cpu_online_mask, stop_machine_cpu_stop, &smdata); | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | int stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus) | 
|  | 556 | { | 
|  | 557 | int ret; | 
|  | 558 |  | 
|  | 559 | /* No CPUs can come up or down during this. */ | 
|  | 560 | get_online_cpus(); | 
|  | 561 | ret = __stop_machine(fn, data, cpus); | 
|  | 562 | put_online_cpus(); | 
|  | 563 | return ret; | 
|  | 564 | } | 
|  | 565 | EXPORT_SYMBOL_GPL(stop_machine); | 
|  | 566 |  | 
|  | 567 | /** | 
|  | 568 | * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU | 
|  | 569 | * @fn: the function to run | 
|  | 570 | * @data: the data ptr for the @fn() | 
|  | 571 | * @cpus: the cpus to run the @fn() on (NULL = any online cpu) | 
|  | 572 | * | 
|  | 573 | * This is identical to stop_machine() but can be called from a CPU which | 
|  | 574 | * is not active.  The local CPU is in the process of hotplug (so no other | 
|  | 575 | * CPU hotplug can start) and not marked active and doesn't have enough | 
|  | 576 | * context to sleep. | 
|  | 577 | * | 
|  | 578 | * This function provides stop_machine() functionality for such state by | 
|  | 579 | * using busy-wait for synchronization and executing @fn directly for local | 
|  | 580 | * CPU. | 
|  | 581 | * | 
|  | 582 | * CONTEXT: | 
|  | 583 | * Local CPU is inactive.  Temporarily stops all active CPUs. | 
|  | 584 | * | 
|  | 585 | * RETURNS: | 
|  | 586 | * 0 if all executions of @fn returned 0, any non zero return value if any | 
|  | 587 | * returned non zero. | 
|  | 588 | */ | 
|  | 589 | int stop_machine_from_inactive_cpu(int (*fn)(void *), void *data, | 
|  | 590 | const struct cpumask *cpus) | 
|  | 591 | { | 
|  | 592 | struct stop_machine_data smdata = { .fn = fn, .data = data, | 
|  | 593 | .active_cpus = cpus }; | 
|  | 594 | struct cpu_stop_done done; | 
|  | 595 | int ret; | 
|  | 596 |  | 
|  | 597 | /* Local CPU must be inactive and CPU hotplug in progress. */ | 
|  | 598 | BUG_ON(cpu_active(raw_smp_processor_id())); | 
|  | 599 | smdata.num_threads = num_active_cpus() + 1;	/* +1 for local */ | 
|  | 600 |  | 
|  | 601 | /* No proper task established and can't sleep - busy wait for lock. */ | 
|  | 602 | while (!mutex_trylock(&stop_cpus_mutex)) | 
|  | 603 | cpu_relax(); | 
|  | 604 |  | 
|  | 605 | /* Schedule work on other CPUs and execute directly for local CPU */ | 
|  | 606 | set_state(&smdata, STOPMACHINE_PREPARE); | 
|  | 607 | cpu_stop_init_done(&done, num_active_cpus()); | 
|  | 608 | queue_stop_cpus_work(cpu_active_mask, stop_machine_cpu_stop, &smdata, | 
|  | 609 | &done, true); | 
|  | 610 | ret = stop_machine_cpu_stop(&smdata); | 
|  | 611 |  | 
|  | 612 | /* Busy wait for completion. */ | 
|  | 613 | while (atomic_read(&done.nr_todo)) | 
|  | 614 | cpu_relax(); | 
|  | 615 |  | 
|  | 616 | mutex_unlock(&stop_cpus_mutex); | 
|  | 617 | return ret ?: done.ret; | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | #endif	/* CONFIG_STOP_MACHINE */ |