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