1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * kernel/stop_machine.c 4 * 5 * Copyright (C) 2008, 2005 IBM Corporation. 6 * Copyright (C) 2008, 2005 Rusty Russell [email protected] 7 * Copyright (C) 2010 SUSE Linux Products GmbH 8 * Copyright (C) 2010 Tejun Heo <[email protected]> 9 */ 10 #include <linux/completion.h> 11 #include <linux/cpu.h> 12 #include <linux/init.h> 13 #include <linux/kthread.h> 14 #include <linux/export.h> 15 #include <linux/percpu.h> 16 #include <linux/sched.h> 17 #include <linux/stop_machine.h> 18 #include <linux/interrupt.h> 19 #include <linux/kallsyms.h> 20 #include <linux/smpboot.h> 21 #include <linux/atomic.h> 22 #include <linux/nmi.h> 23 #include <linux/sched/wake_q.h> 24 25 /* 26 * Structure to determine completion condition and record errors. May 27 * be shared by works on different cpus. 28 */ 29 struct cpu_stop_done { 30 atomic_t nr_todo; /* nr left to execute */ 31 int ret; /* collected return value */ 32 struct completion completion; /* fired if nr_todo reaches 0 */ 33 }; 34 35 /* the actual stopper, one per every possible cpu, enabled on online cpus */ 36 struct cpu_stopper { 37 struct task_struct *thread; 38 39 raw_spinlock_t lock; 40 bool enabled; /* is this stopper enabled? */ 41 struct list_head works; /* list of pending works */ 42 43 struct cpu_stop_work stop_work; /* for stop_cpus */ 44 }; 45 46 static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper); 47 static bool stop_machine_initialized = false; 48 49 /* static data for stop_cpus */ 50 static DEFINE_MUTEX(stop_cpus_mutex); 51 static bool stop_cpus_in_progress; 52 53 static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo) 54 { 55 memset(done, 0, sizeof(*done)); 56 atomic_set(&done->nr_todo, nr_todo); 57 init_completion(&done->completion); 58 } 59 60 /* signal completion unless @done is NULL */ 61 static void cpu_stop_signal_done(struct cpu_stop_done *done) 62 { 63 if (atomic_dec_and_test(&done->nr_todo)) 64 complete(&done->completion); 65 } 66 67 static void __cpu_stop_queue_work(struct cpu_stopper *stopper, 68 struct cpu_stop_work *work, 69 struct wake_q_head *wakeq) 70 { 71 list_add_tail(&work->list, &stopper->works); 72 wake_q_add(wakeq, stopper->thread); 73 } 74 75 /* queue @work to @stopper. if offline, @work is completed immediately */ 76 static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work) 77 { 78 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 79 DEFINE_WAKE_Q(wakeq); 80 unsigned long flags; 81 bool enabled; 82 83 preempt_disable(); 84 raw_spin_lock_irqsave(&stopper->lock, flags); 85 enabled = stopper->enabled; 86 if (enabled) 87 __cpu_stop_queue_work(stopper, work, &wakeq); 88 else if (work->done) 89 cpu_stop_signal_done(work->done); 90 raw_spin_unlock_irqrestore(&stopper->lock, flags); 91 92 wake_up_q(&wakeq); 93 preempt_enable(); 94 95 return enabled; 96 } 97 98 /** 99 * stop_one_cpu - stop a cpu 100 * @cpu: cpu to stop 101 * @fn: function to execute 102 * @arg: argument to @fn 103 * 104 * Execute @fn(@arg) on @cpu. @fn is run in a process context with 105 * the highest priority preempting any task on the cpu and 106 * monopolizing it. This function returns after the execution is 107 * complete. 108 * 109 * This function doesn't guarantee @cpu stays online till @fn 110 * completes. If @cpu goes down in the middle, execution may happen 111 * partially or fully on different cpus. @fn should either be ready 112 * for that or the caller should ensure that @cpu stays online until 113 * this function completes. 114 * 115 * CONTEXT: 116 * Might sleep. 117 * 118 * RETURNS: 119 * -ENOENT if @fn(@arg) was not executed because @cpu was offline; 120 * otherwise, the return value of @fn. 121 */ 122 int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) 123 { 124 struct cpu_stop_done done; 125 struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done }; 126 127 cpu_stop_init_done(&done, 1); 128 if (!cpu_stop_queue_work(cpu, &work)) 129 return -ENOENT; 130 /* 131 * In case @cpu == smp_proccessor_id() we can avoid a sleep+wakeup 132 * cycle by doing a preemption: 133 */ 134 cond_resched(); 135 wait_for_completion(&done.completion); 136 return done.ret; 137 } 138 139 /* This controls the threads on each CPU. */ 140 enum multi_stop_state { 141 /* Dummy starting state for thread. */ 142 MULTI_STOP_NONE, 143 /* Awaiting everyone to be scheduled. */ 144 MULTI_STOP_PREPARE, 145 /* Disable interrupts. */ 146 MULTI_STOP_DISABLE_IRQ, 147 /* Run the function */ 148 MULTI_STOP_RUN, 149 /* Exit */ 150 MULTI_STOP_EXIT, 151 }; 152 153 struct multi_stop_data { 154 cpu_stop_fn_t fn; 155 void *data; 156 /* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */ 157 unsigned int num_threads; 158 const struct cpumask *active_cpus; 159 160 enum multi_stop_state state; 161 atomic_t thread_ack; 162 }; 163 164 static void set_state(struct multi_stop_data *msdata, 165 enum multi_stop_state newstate) 166 { 167 /* Reset ack counter. */ 168 atomic_set(&msdata->thread_ack, msdata->num_threads); 169 smp_wmb(); 170 msdata->state = newstate; 171 } 172 173 /* Last one to ack a state moves to the next state. */ 174 static void ack_state(struct multi_stop_data *msdata) 175 { 176 if (atomic_dec_and_test(&msdata->thread_ack)) 177 set_state(msdata, msdata->state + 1); 178 } 179 180 /* This is the cpu_stop function which stops the CPU. */ 181 static int multi_cpu_stop(void *data) 182 { 183 struct multi_stop_data *msdata = data; 184 enum multi_stop_state curstate = MULTI_STOP_NONE; 185 int cpu = smp_processor_id(), err = 0; 186 const struct cpumask *cpumask; 187 unsigned long flags; 188 bool is_active; 189 190 /* 191 * When called from stop_machine_from_inactive_cpu(), irq might 192 * already be disabled. Save the state and restore it on exit. 193 */ 194 local_save_flags(flags); 195 196 if (!msdata->active_cpus) { 197 cpumask = cpu_online_mask; 198 is_active = cpu == cpumask_first(cpumask); 199 } else { 200 cpumask = msdata->active_cpus; 201 is_active = cpumask_test_cpu(cpu, cpumask); 202 } 203 204 /* Simple state machine */ 205 do { 206 /* Chill out and ensure we re-read multi_stop_state. */ 207 cpu_relax_yield(cpumask); 208 if (msdata->state != curstate) { 209 curstate = msdata->state; 210 switch (curstate) { 211 case MULTI_STOP_DISABLE_IRQ: 212 local_irq_disable(); 213 hard_irq_disable(); 214 break; 215 case MULTI_STOP_RUN: 216 if (is_active) 217 err = msdata->fn(msdata->data); 218 break; 219 default: 220 break; 221 } 222 ack_state(msdata); 223 } else if (curstate > MULTI_STOP_PREPARE) { 224 /* 225 * At this stage all other CPUs we depend on must spin 226 * in the same loop. Any reason for hard-lockup should 227 * be detected and reported on their side. 228 */ 229 touch_nmi_watchdog(); 230 } 231 } while (curstate != MULTI_STOP_EXIT); 232 233 local_irq_restore(flags); 234 return err; 235 } 236 237 static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1, 238 int cpu2, struct cpu_stop_work *work2) 239 { 240 struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1); 241 struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2); 242 DEFINE_WAKE_Q(wakeq); 243 int err; 244 245 retry: 246 /* 247 * The waking up of stopper threads has to happen in the same 248 * scheduling context as the queueing. Otherwise, there is a 249 * possibility of one of the above stoppers being woken up by another 250 * CPU, and preempting us. This will cause us to not wake up the other 251 * stopper forever. 252 */ 253 preempt_disable(); 254 raw_spin_lock_irq(&stopper1->lock); 255 raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING); 256 257 if (!stopper1->enabled || !stopper2->enabled) { 258 err = -ENOENT; 259 goto unlock; 260 } 261 262 /* 263 * Ensure that if we race with __stop_cpus() the stoppers won't get 264 * queued up in reverse order leading to system deadlock. 265 * 266 * We can't miss stop_cpus_in_progress if queue_stop_cpus_work() has 267 * queued a work on cpu1 but not on cpu2, we hold both locks. 268 * 269 * It can be falsely true but it is safe to spin until it is cleared, 270 * queue_stop_cpus_work() does everything under preempt_disable(). 271 */ 272 if (unlikely(stop_cpus_in_progress)) { 273 err = -EDEADLK; 274 goto unlock; 275 } 276 277 err = 0; 278 __cpu_stop_queue_work(stopper1, work1, &wakeq); 279 __cpu_stop_queue_work(stopper2, work2, &wakeq); 280 281 unlock: 282 raw_spin_unlock(&stopper2->lock); 283 raw_spin_unlock_irq(&stopper1->lock); 284 285 if (unlikely(err == -EDEADLK)) { 286 preempt_enable(); 287 288 while (stop_cpus_in_progress) 289 cpu_relax(); 290 291 goto retry; 292 } 293 294 wake_up_q(&wakeq); 295 preempt_enable(); 296 297 return err; 298 } 299 /** 300 * stop_two_cpus - stops two cpus 301 * @cpu1: the cpu to stop 302 * @cpu2: the other cpu to stop 303 * @fn: function to execute 304 * @arg: argument to @fn 305 * 306 * Stops both the current and specified CPU and runs @fn on one of them. 307 * 308 * returns when both are completed. 309 */ 310 int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg) 311 { 312 struct cpu_stop_done done; 313 struct cpu_stop_work work1, work2; 314 struct multi_stop_data msdata; 315 316 msdata = (struct multi_stop_data){ 317 .fn = fn, 318 .data = arg, 319 .num_threads = 2, 320 .active_cpus = cpumask_of(cpu1), 321 }; 322 323 work1 = work2 = (struct cpu_stop_work){ 324 .fn = multi_cpu_stop, 325 .arg = &msdata, 326 .done = &done 327 }; 328 329 cpu_stop_init_done(&done, 2); 330 set_state(&msdata, MULTI_STOP_PREPARE); 331 332 if (cpu1 > cpu2) 333 swap(cpu1, cpu2); 334 if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) 335 return -ENOENT; 336 337 wait_for_completion(&done.completion); 338 return done.ret; 339 } 340 341 /** 342 * stop_one_cpu_nowait - stop a cpu but don't wait for completion 343 * @cpu: cpu to stop 344 * @fn: function to execute 345 * @arg: argument to @fn 346 * @work_buf: pointer to cpu_stop_work structure 347 * 348 * Similar to stop_one_cpu() but doesn't wait for completion. The 349 * caller is responsible for ensuring @work_buf is currently unused 350 * and will remain untouched until stopper starts executing @fn. 351 * 352 * CONTEXT: 353 * Don't care. 354 * 355 * RETURNS: 356 * true if cpu_stop_work was queued successfully and @fn will be called, 357 * false otherwise. 358 */ 359 bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg, 360 struct cpu_stop_work *work_buf) 361 { 362 *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, }; 363 return cpu_stop_queue_work(cpu, work_buf); 364 } 365 366 static bool queue_stop_cpus_work(const struct cpumask *cpumask, 367 cpu_stop_fn_t fn, void *arg, 368 struct cpu_stop_done *done) 369 { 370 struct cpu_stop_work *work; 371 unsigned int cpu; 372 bool queued = false; 373 374 /* 375 * Disable preemption while queueing to avoid getting 376 * preempted by a stopper which might wait for other stoppers 377 * to enter @fn which can lead to deadlock. 378 */ 379 preempt_disable(); 380 stop_cpus_in_progress = true; 381 for_each_cpu(cpu, cpumask) { 382 work = &per_cpu(cpu_stopper.stop_work, cpu); 383 work->fn = fn; 384 work->arg = arg; 385 work->done = done; 386 if (cpu_stop_queue_work(cpu, work)) 387 queued = true; 388 } 389 stop_cpus_in_progress = false; 390 preempt_enable(); 391 392 return queued; 393 } 394 395 static int __stop_cpus(const struct cpumask *cpumask, 396 cpu_stop_fn_t fn, void *arg) 397 { 398 struct cpu_stop_done done; 399 400 cpu_stop_init_done(&done, cpumask_weight(cpumask)); 401 if (!queue_stop_cpus_work(cpumask, fn, arg, &done)) 402 return -ENOENT; 403 wait_for_completion(&done.completion); 404 return done.ret; 405 } 406 407 /** 408 * stop_cpus - stop multiple cpus 409 * @cpumask: cpus to stop 410 * @fn: function to execute 411 * @arg: argument to @fn 412 * 413 * Execute @fn(@arg) on online cpus in @cpumask. On each target cpu, 414 * @fn is run in a process context with the highest priority 415 * preempting any task on the cpu and monopolizing it. This function 416 * returns after all executions are complete. 417 * 418 * This function doesn't guarantee the cpus in @cpumask stay online 419 * till @fn completes. If some cpus go down in the middle, execution 420 * on the cpu may happen partially or fully on different cpus. @fn 421 * should either be ready for that or the caller should ensure that 422 * the cpus stay online until this function completes. 423 * 424 * All stop_cpus() calls are serialized making it safe for @fn to wait 425 * for all cpus to start executing it. 426 * 427 * CONTEXT: 428 * Might sleep. 429 * 430 * RETURNS: 431 * -ENOENT if @fn(@arg) was not executed at all because all cpus in 432 * @cpumask were offline; otherwise, 0 if all executions of @fn 433 * returned 0, any non zero return value if any returned non zero. 434 */ 435 int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) 436 { 437 int ret; 438 439 /* static works are used, process one request at a time */ 440 mutex_lock(&stop_cpus_mutex); 441 ret = __stop_cpus(cpumask, fn, arg); 442 mutex_unlock(&stop_cpus_mutex); 443 return ret; 444 } 445 446 /** 447 * try_stop_cpus - try to stop multiple cpus 448 * @cpumask: cpus to stop 449 * @fn: function to execute 450 * @arg: argument to @fn 451 * 452 * Identical to stop_cpus() except that it fails with -EAGAIN if 453 * someone else is already using the facility. 454 * 455 * CONTEXT: 456 * Might sleep. 457 * 458 * RETURNS: 459 * -EAGAIN if someone else is already stopping cpus, -ENOENT if 460 * @fn(@arg) was not executed at all because all cpus in @cpumask were 461 * offline; otherwise, 0 if all executions of @fn returned 0, any non 462 * zero return value if any returned non zero. 463 */ 464 int try_stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg) 465 { 466 int ret; 467 468 /* static works are used, process one request at a time */ 469 if (!mutex_trylock(&stop_cpus_mutex)) 470 return -EAGAIN; 471 ret = __stop_cpus(cpumask, fn, arg); 472 mutex_unlock(&stop_cpus_mutex); 473 return ret; 474 } 475 476 static int cpu_stop_should_run(unsigned int cpu) 477 { 478 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 479 unsigned long flags; 480 int run; 481 482 raw_spin_lock_irqsave(&stopper->lock, flags); 483 run = !list_empty(&stopper->works); 484 raw_spin_unlock_irqrestore(&stopper->lock, flags); 485 return run; 486 } 487 488 static void cpu_stopper_thread(unsigned int cpu) 489 { 490 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 491 struct cpu_stop_work *work; 492 493 repeat: 494 work = NULL; 495 raw_spin_lock_irq(&stopper->lock); 496 if (!list_empty(&stopper->works)) { 497 work = list_first_entry(&stopper->works, 498 struct cpu_stop_work, list); 499 list_del_init(&work->list); 500 } 501 raw_spin_unlock_irq(&stopper->lock); 502 503 if (work) { 504 cpu_stop_fn_t fn = work->fn; 505 void *arg = work->arg; 506 struct cpu_stop_done *done = work->done; 507 int ret; 508 509 /* cpu stop callbacks must not sleep, make in_atomic() == T */ 510 preempt_count_inc(); 511 ret = fn(arg); 512 if (done) { 513 if (ret) 514 done->ret = ret; 515 cpu_stop_signal_done(done); 516 } 517 preempt_count_dec(); 518 WARN_ONCE(preempt_count(), 519 "cpu_stop: %ps(%p) leaked preempt count\n", fn, arg); 520 goto repeat; 521 } 522 } 523 524 void stop_machine_park(int cpu) 525 { 526 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 527 /* 528 * Lockless. cpu_stopper_thread() will take stopper->lock and flush 529 * the pending works before it parks, until then it is fine to queue 530 * the new works. 531 */ 532 stopper->enabled = false; 533 kthread_park(stopper->thread); 534 } 535 536 extern void sched_set_stop_task(int cpu, struct task_struct *stop); 537 538 static void cpu_stop_create(unsigned int cpu) 539 { 540 sched_set_stop_task(cpu, per_cpu(cpu_stopper.thread, cpu)); 541 } 542 543 static void cpu_stop_park(unsigned int cpu) 544 { 545 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 546 547 WARN_ON(!list_empty(&stopper->works)); 548 } 549 550 void stop_machine_unpark(int cpu) 551 { 552 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 553 554 stopper->enabled = true; 555 kthread_unpark(stopper->thread); 556 } 557 558 static struct smp_hotplug_thread cpu_stop_threads = { 559 .store = &cpu_stopper.thread, 560 .thread_should_run = cpu_stop_should_run, 561 .thread_fn = cpu_stopper_thread, 562 .thread_comm = "migration/%u", 563 .create = cpu_stop_create, 564 .park = cpu_stop_park, 565 .selfparking = true, 566 }; 567 568 static int __init cpu_stop_init(void) 569 { 570 unsigned int cpu; 571 572 for_each_possible_cpu(cpu) { 573 struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu); 574 575 raw_spin_lock_init(&stopper->lock); 576 INIT_LIST_HEAD(&stopper->works); 577 } 578 579 BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads)); 580 stop_machine_unpark(raw_smp_processor_id()); 581 stop_machine_initialized = true; 582 return 0; 583 } 584 early_initcall(cpu_stop_init); 585 586 int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data, 587 const struct cpumask *cpus) 588 { 589 struct multi_stop_data msdata = { 590 .fn = fn, 591 .data = data, 592 .num_threads = num_online_cpus(), 593 .active_cpus = cpus, 594 }; 595 596 lockdep_assert_cpus_held(); 597 598 if (!stop_machine_initialized) { 599 /* 600 * Handle the case where stop_machine() is called 601 * early in boot before stop_machine() has been 602 * initialized. 603 */ 604 unsigned long flags; 605 int ret; 606 607 WARN_ON_ONCE(msdata.num_threads != 1); 608 609 local_irq_save(flags); 610 hard_irq_disable(); 611 ret = (*fn)(data); 612 local_irq_restore(flags); 613 614 return ret; 615 } 616 617 /* Set the initial state and stop all online cpus. */ 618 set_state(&msdata, MULTI_STOP_PREPARE); 619 return stop_cpus(cpu_online_mask, multi_cpu_stop, &msdata); 620 } 621 622 int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus) 623 { 624 int ret; 625 626 /* No CPUs can come up or down during this. */ 627 cpus_read_lock(); 628 ret = stop_machine_cpuslocked(fn, data, cpus); 629 cpus_read_unlock(); 630 return ret; 631 } 632 EXPORT_SYMBOL_GPL(stop_machine); 633 634 /** 635 * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU 636 * @fn: the function to run 637 * @data: the data ptr for the @fn() 638 * @cpus: the cpus to run the @fn() on (NULL = any online cpu) 639 * 640 * This is identical to stop_machine() but can be called from a CPU which 641 * is not active. The local CPU is in the process of hotplug (so no other 642 * CPU hotplug can start) and not marked active and doesn't have enough 643 * context to sleep. 644 * 645 * This function provides stop_machine() functionality for such state by 646 * using busy-wait for synchronization and executing @fn directly for local 647 * CPU. 648 * 649 * CONTEXT: 650 * Local CPU is inactive. Temporarily stops all active CPUs. 651 * 652 * RETURNS: 653 * 0 if all executions of @fn returned 0, any non zero return value if any 654 * returned non zero. 655 */ 656 int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data, 657 const struct cpumask *cpus) 658 { 659 struct multi_stop_data msdata = { .fn = fn, .data = data, 660 .active_cpus = cpus }; 661 struct cpu_stop_done done; 662 int ret; 663 664 /* Local CPU must be inactive and CPU hotplug in progress. */ 665 BUG_ON(cpu_active(raw_smp_processor_id())); 666 msdata.num_threads = num_active_cpus() + 1; /* +1 for local */ 667 668 /* No proper task established and can't sleep - busy wait for lock. */ 669 while (!mutex_trylock(&stop_cpus_mutex)) 670 cpu_relax(); 671 672 /* Schedule work on other CPUs and execute directly for local CPU */ 673 set_state(&msdata, MULTI_STOP_PREPARE); 674 cpu_stop_init_done(&done, num_active_cpus()); 675 queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata, 676 &done); 677 ret = multi_cpu_stop(&msdata); 678 679 /* Busy wait for completion. */ 680 while (!completion_done(&done.completion)) 681 cpu_relax(); 682 683 mutex_unlock(&stop_cpus_mutex); 684 return ret ?: done.ret; 685 } 686