1 /* CPU control. 2 * (C) 2001, 2002, 2003, 2004 Rusty Russell 3 * 4 * This code is licenced under the GPL. 5 */ 6 #include <linux/sched/mm.h> 7 #include <linux/proc_fs.h> 8 #include <linux/smp.h> 9 #include <linux/init.h> 10 #include <linux/notifier.h> 11 #include <linux/sched/signal.h> 12 #include <linux/sched/hotplug.h> 13 #include <linux/sched/isolation.h> 14 #include <linux/sched/task.h> 15 #include <linux/sched/smt.h> 16 #include <linux/unistd.h> 17 #include <linux/cpu.h> 18 #include <linux/oom.h> 19 #include <linux/rcupdate.h> 20 #include <linux/delay.h> 21 #include <linux/export.h> 22 #include <linux/bug.h> 23 #include <linux/kthread.h> 24 #include <linux/stop_machine.h> 25 #include <linux/mutex.h> 26 #include <linux/gfp.h> 27 #include <linux/suspend.h> 28 #include <linux/lockdep.h> 29 #include <linux/tick.h> 30 #include <linux/irq.h> 31 #include <linux/nmi.h> 32 #include <linux/smpboot.h> 33 #include <linux/relay.h> 34 #include <linux/slab.h> 35 #include <linux/scs.h> 36 #include <linux/percpu-rwsem.h> 37 #include <linux/cpuset.h> 38 #include <linux/random.h> 39 #include <linux/cc_platform.h> 40 #include <linux/parser.h> 41 42 #include <trace/events/power.h> 43 #define CREATE_TRACE_POINTS 44 #include <trace/events/cpuhp.h> 45 46 #include "smpboot.h" 47 48 /** 49 * struct cpuhp_cpu_state - Per cpu hotplug state storage 50 * @state: The current cpu state 51 * @target: The target state 52 * @fail: Current CPU hotplug callback state 53 * @thread: Pointer to the hotplug thread 54 * @should_run: Thread should execute 55 * @rollback: Perform a rollback 56 * @single: Single callback invocation 57 * @bringup: Single callback bringup or teardown selector 58 * @node: Remote CPU node; for multi-instance, do a 59 * single entry callback for install/remove 60 * @last: For multi-instance rollback, remember how far we got 61 * @cb_state: The state for a single callback (install/uninstall) 62 * @result: Result of the operation 63 * @ap_sync_state: State for AP synchronization 64 * @done_up: Signal completion to the issuer of the task for cpu-up 65 * @done_down: Signal completion to the issuer of the task for cpu-down 66 */ 67 struct cpuhp_cpu_state { 68 enum cpuhp_state state; 69 enum cpuhp_state target; 70 enum cpuhp_state fail; 71 #ifdef CONFIG_SMP 72 struct task_struct *thread; 73 bool should_run; 74 bool rollback; 75 bool single; 76 bool bringup; 77 struct hlist_node *node; 78 struct hlist_node *last; 79 enum cpuhp_state cb_state; 80 int result; 81 atomic_t ap_sync_state; 82 struct completion done_up; 83 struct completion done_down; 84 #endif 85 }; 86 87 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = { 88 .fail = CPUHP_INVALID, 89 }; 90 91 #ifdef CONFIG_SMP 92 cpumask_t cpus_booted_once_mask; 93 #endif 94 95 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP) 96 static struct lockdep_map cpuhp_state_up_map = 97 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map); 98 static struct lockdep_map cpuhp_state_down_map = 99 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map); 100 101 102 static inline void cpuhp_lock_acquire(bool bringup) 103 { 104 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map); 105 } 106 107 static inline void cpuhp_lock_release(bool bringup) 108 { 109 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map); 110 } 111 #else 112 113 static inline void cpuhp_lock_acquire(bool bringup) { } 114 static inline void cpuhp_lock_release(bool bringup) { } 115 116 #endif 117 118 /** 119 * struct cpuhp_step - Hotplug state machine step 120 * @name: Name of the step 121 * @startup: Startup function of the step 122 * @teardown: Teardown function of the step 123 * @cant_stop: Bringup/teardown can't be stopped at this step 124 * @multi_instance: State has multiple instances which get added afterwards 125 */ 126 struct cpuhp_step { 127 const char *name; 128 union { 129 int (*single)(unsigned int cpu); 130 int (*multi)(unsigned int cpu, 131 struct hlist_node *node); 132 } startup; 133 union { 134 int (*single)(unsigned int cpu); 135 int (*multi)(unsigned int cpu, 136 struct hlist_node *node); 137 } teardown; 138 /* private: */ 139 struct hlist_head list; 140 /* public: */ 141 bool cant_stop; 142 bool multi_instance; 143 }; 144 145 static DEFINE_MUTEX(cpuhp_state_mutex); 146 static struct cpuhp_step cpuhp_hp_states[]; 147 148 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state) 149 { 150 return cpuhp_hp_states + state; 151 } 152 153 static bool cpuhp_step_empty(bool bringup, struct cpuhp_step *step) 154 { 155 return bringup ? !step->startup.single : !step->teardown.single; 156 } 157 158 /** 159 * cpuhp_invoke_callback - Invoke the callbacks for a given state 160 * @cpu: The cpu for which the callback should be invoked 161 * @state: The state to do callbacks for 162 * @bringup: True if the bringup callback should be invoked 163 * @node: For multi-instance, do a single entry callback for install/remove 164 * @lastp: For multi-instance rollback, remember how far we got 165 * 166 * Called from cpu hotplug and from the state register machinery. 167 * 168 * Return: %0 on success or a negative errno code 169 */ 170 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state, 171 bool bringup, struct hlist_node *node, 172 struct hlist_node **lastp) 173 { 174 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 175 struct cpuhp_step *step = cpuhp_get_step(state); 176 int (*cbm)(unsigned int cpu, struct hlist_node *node); 177 int (*cb)(unsigned int cpu); 178 int ret, cnt; 179 180 if (st->fail == state) { 181 st->fail = CPUHP_INVALID; 182 return -EAGAIN; 183 } 184 185 if (cpuhp_step_empty(bringup, step)) { 186 WARN_ON_ONCE(1); 187 return 0; 188 } 189 190 if (!step->multi_instance) { 191 WARN_ON_ONCE(lastp && *lastp); 192 cb = bringup ? step->startup.single : step->teardown.single; 193 194 trace_cpuhp_enter(cpu, st->target, state, cb); 195 ret = cb(cpu); 196 trace_cpuhp_exit(cpu, st->state, state, ret); 197 return ret; 198 } 199 cbm = bringup ? step->startup.multi : step->teardown.multi; 200 201 /* Single invocation for instance add/remove */ 202 if (node) { 203 WARN_ON_ONCE(lastp && *lastp); 204 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node); 205 ret = cbm(cpu, node); 206 trace_cpuhp_exit(cpu, st->state, state, ret); 207 return ret; 208 } 209 210 /* State transition. Invoke on all instances */ 211 cnt = 0; 212 hlist_for_each(node, &step->list) { 213 if (lastp && node == *lastp) 214 break; 215 216 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node); 217 ret = cbm(cpu, node); 218 trace_cpuhp_exit(cpu, st->state, state, ret); 219 if (ret) { 220 if (!lastp) 221 goto err; 222 223 *lastp = node; 224 return ret; 225 } 226 cnt++; 227 } 228 if (lastp) 229 *lastp = NULL; 230 return 0; 231 err: 232 /* Rollback the instances if one failed */ 233 cbm = !bringup ? step->startup.multi : step->teardown.multi; 234 if (!cbm) 235 return ret; 236 237 hlist_for_each(node, &step->list) { 238 if (!cnt--) 239 break; 240 241 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node); 242 ret = cbm(cpu, node); 243 trace_cpuhp_exit(cpu, st->state, state, ret); 244 /* 245 * Rollback must not fail, 246 */ 247 WARN_ON_ONCE(ret); 248 } 249 return ret; 250 } 251 252 /* 253 * The former STARTING/DYING states, ran with IRQs disabled and must not fail. 254 */ 255 static bool cpuhp_is_atomic_state(enum cpuhp_state state) 256 { 257 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE; 258 } 259 260 #ifdef CONFIG_SMP 261 static bool cpuhp_is_ap_state(enum cpuhp_state state) 262 { 263 /* 264 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation 265 * purposes as that state is handled explicitly in cpu_down. 266 */ 267 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU; 268 } 269 270 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup) 271 { 272 struct completion *done = bringup ? &st->done_up : &st->done_down; 273 wait_for_completion(done); 274 } 275 276 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup) 277 { 278 struct completion *done = bringup ? &st->done_up : &st->done_down; 279 complete(done); 280 } 281 282 /* Synchronization state management */ 283 enum cpuhp_sync_state { 284 SYNC_STATE_DEAD, 285 SYNC_STATE_KICKED, 286 SYNC_STATE_SHOULD_DIE, 287 SYNC_STATE_ALIVE, 288 SYNC_STATE_SHOULD_ONLINE, 289 SYNC_STATE_ONLINE, 290 }; 291 292 #ifdef CONFIG_HOTPLUG_CORE_SYNC 293 /** 294 * cpuhp_ap_update_sync_state - Update synchronization state during bringup/teardown 295 * @state: The synchronization state to set 296 * 297 * No synchronization point. Just update of the synchronization state, but implies 298 * a full barrier so that the AP changes are visible before the control CPU proceeds. 299 */ 300 static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state) 301 { 302 atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state); 303 304 (void)atomic_xchg(st, state); 305 } 306 307 void __weak arch_cpuhp_sync_state_poll(void) { cpu_relax(); } 308 309 static bool cpuhp_wait_for_sync_state(unsigned int cpu, enum cpuhp_sync_state state, 310 enum cpuhp_sync_state next_state) 311 { 312 atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu); 313 ktime_t now, end, start = ktime_get(); 314 int sync; 315 316 end = start + 10ULL * NSEC_PER_SEC; 317 318 sync = atomic_read(st); 319 while (1) { 320 if (sync == state) { 321 if (!atomic_try_cmpxchg(st, &sync, next_state)) 322 continue; 323 return true; 324 } 325 326 now = ktime_get(); 327 if (now > end) { 328 /* Timeout. Leave the state unchanged */ 329 return false; 330 } else if (now - start < NSEC_PER_MSEC) { 331 /* Poll for one millisecond */ 332 arch_cpuhp_sync_state_poll(); 333 } else { 334 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 335 } 336 sync = atomic_read(st); 337 } 338 return true; 339 } 340 #else /* CONFIG_HOTPLUG_CORE_SYNC */ 341 static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state) { } 342 #endif /* !CONFIG_HOTPLUG_CORE_SYNC */ 343 344 #ifdef CONFIG_HOTPLUG_CORE_SYNC_DEAD 345 /** 346 * cpuhp_ap_report_dead - Update synchronization state to DEAD 347 * 348 * No synchronization point. Just update of the synchronization state. 349 */ 350 void cpuhp_ap_report_dead(void) 351 { 352 cpuhp_ap_update_sync_state(SYNC_STATE_DEAD); 353 } 354 355 void __weak arch_cpuhp_cleanup_dead_cpu(unsigned int cpu) { } 356 357 /* 358 * Late CPU shutdown synchronization point. Cannot use cpuhp_state::done_down 359 * because the AP cannot issue complete() at this stage. 360 */ 361 static void cpuhp_bp_sync_dead(unsigned int cpu) 362 { 363 atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu); 364 int sync = atomic_read(st); 365 366 do { 367 /* CPU can have reported dead already. Don't overwrite that! */ 368 if (sync == SYNC_STATE_DEAD) 369 break; 370 } while (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_SHOULD_DIE)); 371 372 if (cpuhp_wait_for_sync_state(cpu, SYNC_STATE_DEAD, SYNC_STATE_DEAD)) { 373 /* CPU reached dead state. Invoke the cleanup function */ 374 arch_cpuhp_cleanup_dead_cpu(cpu); 375 return; 376 } 377 378 /* No further action possible. Emit message and give up. */ 379 pr_err("CPU%u failed to report dead state\n", cpu); 380 } 381 #else /* CONFIG_HOTPLUG_CORE_SYNC_DEAD */ 382 static inline void cpuhp_bp_sync_dead(unsigned int cpu) { } 383 #endif /* !CONFIG_HOTPLUG_CORE_SYNC_DEAD */ 384 385 #ifdef CONFIG_HOTPLUG_CORE_SYNC_FULL 386 /** 387 * cpuhp_ap_sync_alive - Synchronize AP with the control CPU once it is alive 388 * 389 * Updates the AP synchronization state to SYNC_STATE_ALIVE and waits 390 * for the BP to release it. 391 */ 392 void cpuhp_ap_sync_alive(void) 393 { 394 atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state); 395 396 cpuhp_ap_update_sync_state(SYNC_STATE_ALIVE); 397 398 /* Wait for the control CPU to release it. */ 399 while (atomic_read(st) != SYNC_STATE_SHOULD_ONLINE) 400 cpu_relax(); 401 } 402 403 static bool cpuhp_can_boot_ap(unsigned int cpu) 404 { 405 atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu); 406 int sync = atomic_read(st); 407 408 again: 409 switch (sync) { 410 case SYNC_STATE_DEAD: 411 /* CPU is properly dead */ 412 break; 413 case SYNC_STATE_KICKED: 414 /* CPU did not come up in previous attempt */ 415 break; 416 case SYNC_STATE_ALIVE: 417 /* CPU is stuck cpuhp_ap_sync_alive(). */ 418 break; 419 default: 420 /* CPU failed to report online or dead and is in limbo state. */ 421 return false; 422 } 423 424 /* Prepare for booting */ 425 if (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_KICKED)) 426 goto again; 427 428 return true; 429 } 430 431 void __weak arch_cpuhp_cleanup_kick_cpu(unsigned int cpu) { } 432 433 /* 434 * Early CPU bringup synchronization point. Cannot use cpuhp_state::done_up 435 * because the AP cannot issue complete() so early in the bringup. 436 */ 437 static int cpuhp_bp_sync_alive(unsigned int cpu) 438 { 439 int ret = 0; 440 441 if (!IS_ENABLED(CONFIG_HOTPLUG_CORE_SYNC_FULL)) 442 return 0; 443 444 if (!cpuhp_wait_for_sync_state(cpu, SYNC_STATE_ALIVE, SYNC_STATE_SHOULD_ONLINE)) { 445 pr_err("CPU%u failed to report alive state\n", cpu); 446 ret = -EIO; 447 } 448 449 /* Let the architecture cleanup the kick alive mechanics. */ 450 arch_cpuhp_cleanup_kick_cpu(cpu); 451 return ret; 452 } 453 #else /* CONFIG_HOTPLUG_CORE_SYNC_FULL */ 454 static inline int cpuhp_bp_sync_alive(unsigned int cpu) { return 0; } 455 static inline bool cpuhp_can_boot_ap(unsigned int cpu) { return true; } 456 #endif /* !CONFIG_HOTPLUG_CORE_SYNC_FULL */ 457 458 /* Serializes the updates to cpu_online_mask, cpu_present_mask */ 459 static DEFINE_MUTEX(cpu_add_remove_lock); 460 bool cpuhp_tasks_frozen; 461 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen); 462 463 /* 464 * The following two APIs (cpu_maps_update_begin/done) must be used when 465 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask. 466 */ 467 void cpu_maps_update_begin(void) 468 { 469 mutex_lock(&cpu_add_remove_lock); 470 } 471 472 void cpu_maps_update_done(void) 473 { 474 mutex_unlock(&cpu_add_remove_lock); 475 } 476 477 /* 478 * If set, cpu_up and cpu_down will return -EBUSY and do nothing. 479 * Should always be manipulated under cpu_add_remove_lock 480 */ 481 static int cpu_hotplug_disabled; 482 483 #ifdef CONFIG_HOTPLUG_CPU 484 485 DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock); 486 487 static bool cpu_hotplug_offline_disabled __ro_after_init; 488 489 void cpus_read_lock(void) 490 { 491 percpu_down_read(&cpu_hotplug_lock); 492 } 493 EXPORT_SYMBOL_GPL(cpus_read_lock); 494 495 int cpus_read_trylock(void) 496 { 497 return percpu_down_read_trylock(&cpu_hotplug_lock); 498 } 499 EXPORT_SYMBOL_GPL(cpus_read_trylock); 500 501 void cpus_read_unlock(void) 502 { 503 percpu_up_read(&cpu_hotplug_lock); 504 } 505 EXPORT_SYMBOL_GPL(cpus_read_unlock); 506 507 void cpus_write_lock(void) 508 { 509 percpu_down_write(&cpu_hotplug_lock); 510 } 511 512 void cpus_write_unlock(void) 513 { 514 percpu_up_write(&cpu_hotplug_lock); 515 } 516 517 void lockdep_assert_cpus_held(void) 518 { 519 /* 520 * We can't have hotplug operations before userspace starts running, 521 * and some init codepaths will knowingly not take the hotplug lock. 522 * This is all valid, so mute lockdep until it makes sense to report 523 * unheld locks. 524 */ 525 if (system_state < SYSTEM_RUNNING) 526 return; 527 528 percpu_rwsem_assert_held(&cpu_hotplug_lock); 529 } 530 EXPORT_SYMBOL_GPL(lockdep_assert_cpus_held); 531 532 #ifdef CONFIG_LOCKDEP 533 int lockdep_is_cpus_held(void) 534 { 535 return percpu_rwsem_is_held(&cpu_hotplug_lock); 536 } 537 538 int lockdep_is_cpus_write_held(void) 539 { 540 return percpu_rwsem_is_write_held(&cpu_hotplug_lock); 541 } 542 #endif 543 544 static void lockdep_acquire_cpus_lock(void) 545 { 546 rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_); 547 } 548 549 static void lockdep_release_cpus_lock(void) 550 { 551 rwsem_release(&cpu_hotplug_lock.dep_map, _THIS_IP_); 552 } 553 554 /* Declare CPU offlining not supported */ 555 void cpu_hotplug_disable_offlining(void) 556 { 557 cpu_maps_update_begin(); 558 cpu_hotplug_offline_disabled = true; 559 cpu_maps_update_done(); 560 } 561 562 /* 563 * Wait for currently running CPU hotplug operations to complete (if any) and 564 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects 565 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the 566 * hotplug path before performing hotplug operations. So acquiring that lock 567 * guarantees mutual exclusion from any currently running hotplug operations. 568 */ 569 void cpu_hotplug_disable(void) 570 { 571 cpu_maps_update_begin(); 572 cpu_hotplug_disabled++; 573 cpu_maps_update_done(); 574 } 575 EXPORT_SYMBOL_GPL(cpu_hotplug_disable); 576 577 static void __cpu_hotplug_enable(void) 578 { 579 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n")) 580 return; 581 cpu_hotplug_disabled--; 582 } 583 584 void cpu_hotplug_enable(void) 585 { 586 cpu_maps_update_begin(); 587 __cpu_hotplug_enable(); 588 cpu_maps_update_done(); 589 } 590 EXPORT_SYMBOL_GPL(cpu_hotplug_enable); 591 592 #else 593 594 static void lockdep_acquire_cpus_lock(void) 595 { 596 } 597 598 static void lockdep_release_cpus_lock(void) 599 { 600 } 601 602 #endif /* CONFIG_HOTPLUG_CPU */ 603 604 /* 605 * Architectures that need SMT-specific errata handling during SMT hotplug 606 * should override this. 607 */ 608 void __weak arch_smt_update(void) { } 609 610 #ifdef CONFIG_HOTPLUG_SMT 611 612 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED; 613 static unsigned int cpu_smt_max_threads __ro_after_init; 614 unsigned int cpu_smt_num_threads __read_mostly = UINT_MAX; 615 616 void __init cpu_smt_disable(bool force) 617 { 618 if (!cpu_smt_possible()) 619 return; 620 621 if (force) { 622 pr_info("SMT: Force disabled\n"); 623 cpu_smt_control = CPU_SMT_FORCE_DISABLED; 624 } else { 625 pr_info("SMT: disabled\n"); 626 cpu_smt_control = CPU_SMT_DISABLED; 627 } 628 cpu_smt_num_threads = 1; 629 } 630 631 /* 632 * The decision whether SMT is supported can only be done after the full 633 * CPU identification. Called from architecture code. 634 */ 635 void __init cpu_smt_set_num_threads(unsigned int num_threads, 636 unsigned int max_threads) 637 { 638 WARN_ON(!num_threads || (num_threads > max_threads)); 639 640 if (max_threads == 1) 641 cpu_smt_control = CPU_SMT_NOT_SUPPORTED; 642 643 cpu_smt_max_threads = max_threads; 644 645 /* 646 * If SMT has been disabled via the kernel command line or SMT is 647 * not supported, set cpu_smt_num_threads to 1 for consistency. 648 * If enabled, take the architecture requested number of threads 649 * to bring up into account. 650 */ 651 if (cpu_smt_control != CPU_SMT_ENABLED) 652 cpu_smt_num_threads = 1; 653 else if (num_threads < cpu_smt_num_threads) 654 cpu_smt_num_threads = num_threads; 655 } 656 657 static int __init smt_cmdline_disable(char *str) 658 { 659 cpu_smt_disable(str && !strcmp(str, "force")); 660 return 0; 661 } 662 early_param("nosmt", smt_cmdline_disable); 663 664 /* 665 * For Archicture supporting partial SMT states check if the thread is allowed. 666 * Otherwise this has already been checked through cpu_smt_max_threads when 667 * setting the SMT level. 668 */ 669 static inline bool cpu_smt_thread_allowed(unsigned int cpu) 670 { 671 #ifdef CONFIG_SMT_NUM_THREADS_DYNAMIC 672 return topology_smt_thread_allowed(cpu); 673 #else 674 return true; 675 #endif 676 } 677 678 static inline bool cpu_bootable(unsigned int cpu) 679 { 680 if (cpu_smt_control == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu)) 681 return true; 682 683 /* All CPUs are bootable if controls are not configured */ 684 if (cpu_smt_control == CPU_SMT_NOT_IMPLEMENTED) 685 return true; 686 687 /* All CPUs are bootable if CPU is not SMT capable */ 688 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED) 689 return true; 690 691 if (topology_is_primary_thread(cpu)) 692 return true; 693 694 /* 695 * On x86 it's required to boot all logical CPUs at least once so 696 * that the init code can get a chance to set CR4.MCE on each 697 * CPU. Otherwise, a broadcasted MCE observing CR4.MCE=0b on any 698 * core will shutdown the machine. 699 */ 700 return !cpumask_test_cpu(cpu, &cpus_booted_once_mask); 701 } 702 703 /* Returns true if SMT is supported and not forcefully (irreversibly) disabled */ 704 bool cpu_smt_possible(void) 705 { 706 return cpu_smt_control != CPU_SMT_FORCE_DISABLED && 707 cpu_smt_control != CPU_SMT_NOT_SUPPORTED; 708 } 709 EXPORT_SYMBOL_GPL(cpu_smt_possible); 710 711 #else 712 static inline bool cpu_bootable(unsigned int cpu) { return true; } 713 #endif 714 715 static inline enum cpuhp_state 716 cpuhp_set_state(int cpu, struct cpuhp_cpu_state *st, enum cpuhp_state target) 717 { 718 enum cpuhp_state prev_state = st->state; 719 bool bringup = st->state < target; 720 721 st->rollback = false; 722 st->last = NULL; 723 724 st->target = target; 725 st->single = false; 726 st->bringup = bringup; 727 if (cpu_dying(cpu) != !bringup) 728 set_cpu_dying(cpu, !bringup); 729 730 return prev_state; 731 } 732 733 static inline void 734 cpuhp_reset_state(int cpu, struct cpuhp_cpu_state *st, 735 enum cpuhp_state prev_state) 736 { 737 bool bringup = !st->bringup; 738 739 st->target = prev_state; 740 741 /* 742 * Already rolling back. No need invert the bringup value or to change 743 * the current state. 744 */ 745 if (st->rollback) 746 return; 747 748 st->rollback = true; 749 750 /* 751 * If we have st->last we need to undo partial multi_instance of this 752 * state first. Otherwise start undo at the previous state. 753 */ 754 if (!st->last) { 755 if (st->bringup) 756 st->state--; 757 else 758 st->state++; 759 } 760 761 st->bringup = bringup; 762 if (cpu_dying(cpu) != !bringup) 763 set_cpu_dying(cpu, !bringup); 764 } 765 766 /* Regular hotplug invocation of the AP hotplug thread */ 767 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st) 768 { 769 if (!st->single && st->state == st->target) 770 return; 771 772 st->result = 0; 773 /* 774 * Make sure the above stores are visible before should_run becomes 775 * true. Paired with the mb() above in cpuhp_thread_fun() 776 */ 777 smp_mb(); 778 st->should_run = true; 779 wake_up_process(st->thread); 780 wait_for_ap_thread(st, st->bringup); 781 } 782 783 static int cpuhp_kick_ap(int cpu, struct cpuhp_cpu_state *st, 784 enum cpuhp_state target) 785 { 786 enum cpuhp_state prev_state; 787 int ret; 788 789 prev_state = cpuhp_set_state(cpu, st, target); 790 __cpuhp_kick_ap(st); 791 if ((ret = st->result)) { 792 cpuhp_reset_state(cpu, st, prev_state); 793 __cpuhp_kick_ap(st); 794 } 795 796 return ret; 797 } 798 799 static int bringup_wait_for_ap_online(unsigned int cpu) 800 { 801 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 802 803 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */ 804 wait_for_ap_thread(st, true); 805 if (WARN_ON_ONCE((!cpu_online(cpu)))) 806 return -ECANCELED; 807 808 /* Unpark the hotplug thread of the target cpu */ 809 kthread_unpark(st->thread); 810 811 /* 812 * SMT soft disabling on X86 requires to bring the CPU out of the 813 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The 814 * CPU marked itself as booted_once in notify_cpu_starting() so the 815 * cpu_bootable() check will now return false if this is not the 816 * primary sibling. 817 */ 818 if (!cpu_bootable(cpu)) 819 return -ECANCELED; 820 return 0; 821 } 822 823 #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP 824 static int cpuhp_kick_ap_alive(unsigned int cpu) 825 { 826 if (!cpuhp_can_boot_ap(cpu)) 827 return -EAGAIN; 828 829 return arch_cpuhp_kick_ap_alive(cpu, idle_thread_get(cpu)); 830 } 831 832 static int cpuhp_bringup_ap(unsigned int cpu) 833 { 834 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 835 int ret; 836 837 /* 838 * Some architectures have to walk the irq descriptors to 839 * setup the vector space for the cpu which comes online. 840 * Prevent irq alloc/free across the bringup. 841 */ 842 irq_lock_sparse(); 843 844 ret = cpuhp_bp_sync_alive(cpu); 845 if (ret) 846 goto out_unlock; 847 848 ret = bringup_wait_for_ap_online(cpu); 849 if (ret) 850 goto out_unlock; 851 852 irq_unlock_sparse(); 853 854 if (st->target <= CPUHP_AP_ONLINE_IDLE) 855 return 0; 856 857 return cpuhp_kick_ap(cpu, st, st->target); 858 859 out_unlock: 860 irq_unlock_sparse(); 861 return ret; 862 } 863 #else 864 static int bringup_cpu(unsigned int cpu) 865 { 866 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 867 struct task_struct *idle = idle_thread_get(cpu); 868 int ret; 869 870 if (!cpuhp_can_boot_ap(cpu)) 871 return -EAGAIN; 872 873 /* 874 * Some architectures have to walk the irq descriptors to 875 * setup the vector space for the cpu which comes online. 876 * 877 * Prevent irq alloc/free across the bringup by acquiring the 878 * sparse irq lock. Hold it until the upcoming CPU completes the 879 * startup in cpuhp_online_idle() which allows to avoid 880 * intermediate synchronization points in the architecture code. 881 */ 882 irq_lock_sparse(); 883 884 ret = __cpu_up(cpu, idle); 885 if (ret) 886 goto out_unlock; 887 888 ret = cpuhp_bp_sync_alive(cpu); 889 if (ret) 890 goto out_unlock; 891 892 ret = bringup_wait_for_ap_online(cpu); 893 if (ret) 894 goto out_unlock; 895 896 irq_unlock_sparse(); 897 898 if (st->target <= CPUHP_AP_ONLINE_IDLE) 899 return 0; 900 901 return cpuhp_kick_ap(cpu, st, st->target); 902 903 out_unlock: 904 irq_unlock_sparse(); 905 return ret; 906 } 907 #endif 908 909 static int finish_cpu(unsigned int cpu) 910 { 911 struct task_struct *idle = idle_thread_get(cpu); 912 struct mm_struct *mm = idle->active_mm; 913 914 /* 915 * sched_force_init_mm() ensured the use of &init_mm, 916 * drop that refcount now that the CPU has stopped. 917 */ 918 WARN_ON(mm != &init_mm); 919 idle->active_mm = NULL; 920 mmdrop_lazy_tlb(mm); 921 922 return 0; 923 } 924 925 /* 926 * Hotplug state machine related functions 927 */ 928 929 /* 930 * Get the next state to run. Empty ones will be skipped. Returns true if a 931 * state must be run. 932 * 933 * st->state will be modified ahead of time, to match state_to_run, as if it 934 * has already ran. 935 */ 936 static bool cpuhp_next_state(bool bringup, 937 enum cpuhp_state *state_to_run, 938 struct cpuhp_cpu_state *st, 939 enum cpuhp_state target) 940 { 941 do { 942 if (bringup) { 943 if (st->state >= target) 944 return false; 945 946 *state_to_run = ++st->state; 947 } else { 948 if (st->state <= target) 949 return false; 950 951 *state_to_run = st->state--; 952 } 953 954 if (!cpuhp_step_empty(bringup, cpuhp_get_step(*state_to_run))) 955 break; 956 } while (true); 957 958 return true; 959 } 960 961 static int __cpuhp_invoke_callback_range(bool bringup, 962 unsigned int cpu, 963 struct cpuhp_cpu_state *st, 964 enum cpuhp_state target, 965 bool nofail) 966 { 967 enum cpuhp_state state; 968 int ret = 0; 969 970 while (cpuhp_next_state(bringup, &state, st, target)) { 971 int err; 972 973 err = cpuhp_invoke_callback(cpu, state, bringup, NULL, NULL); 974 if (!err) 975 continue; 976 977 if (nofail) { 978 pr_warn("CPU %u %s state %s (%d) failed (%d)\n", 979 cpu, bringup ? "UP" : "DOWN", 980 cpuhp_get_step(st->state)->name, 981 st->state, err); 982 ret = -1; 983 } else { 984 ret = err; 985 break; 986 } 987 } 988 989 return ret; 990 } 991 992 static inline int cpuhp_invoke_callback_range(bool bringup, 993 unsigned int cpu, 994 struct cpuhp_cpu_state *st, 995 enum cpuhp_state target) 996 { 997 return __cpuhp_invoke_callback_range(bringup, cpu, st, target, false); 998 } 999 1000 static inline void cpuhp_invoke_callback_range_nofail(bool bringup, 1001 unsigned int cpu, 1002 struct cpuhp_cpu_state *st, 1003 enum cpuhp_state target) 1004 { 1005 __cpuhp_invoke_callback_range(bringup, cpu, st, target, true); 1006 } 1007 1008 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st) 1009 { 1010 if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) 1011 return true; 1012 /* 1013 * When CPU hotplug is disabled, then taking the CPU down is not 1014 * possible because takedown_cpu() and the architecture and 1015 * subsystem specific mechanisms are not available. So the CPU 1016 * which would be completely unplugged again needs to stay around 1017 * in the current state. 1018 */ 1019 return st->state <= CPUHP_BRINGUP_CPU; 1020 } 1021 1022 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st, 1023 enum cpuhp_state target) 1024 { 1025 enum cpuhp_state prev_state = st->state; 1026 int ret = 0; 1027 1028 ret = cpuhp_invoke_callback_range(true, cpu, st, target); 1029 if (ret) { 1030 pr_debug("CPU UP failed (%d) CPU %u state %s (%d)\n", 1031 ret, cpu, cpuhp_get_step(st->state)->name, 1032 st->state); 1033 1034 cpuhp_reset_state(cpu, st, prev_state); 1035 if (can_rollback_cpu(st)) 1036 WARN_ON(cpuhp_invoke_callback_range(false, cpu, st, 1037 prev_state)); 1038 } 1039 return ret; 1040 } 1041 1042 /* 1043 * The cpu hotplug threads manage the bringup and teardown of the cpus 1044 */ 1045 static int cpuhp_should_run(unsigned int cpu) 1046 { 1047 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 1048 1049 return st->should_run; 1050 } 1051 1052 /* 1053 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke 1054 * callbacks when a state gets [un]installed at runtime. 1055 * 1056 * Each invocation of this function by the smpboot thread does a single AP 1057 * state callback. 1058 * 1059 * It has 3 modes of operation: 1060 * - single: runs st->cb_state 1061 * - up: runs ++st->state, while st->state < st->target 1062 * - down: runs st->state--, while st->state > st->target 1063 * 1064 * When complete or on error, should_run is cleared and the completion is fired. 1065 */ 1066 static void cpuhp_thread_fun(unsigned int cpu) 1067 { 1068 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 1069 bool bringup = st->bringup; 1070 enum cpuhp_state state; 1071 1072 if (WARN_ON_ONCE(!st->should_run)) 1073 return; 1074 1075 /* 1076 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures 1077 * that if we see ->should_run we also see the rest of the state. 1078 */ 1079 smp_mb(); 1080 1081 /* 1082 * The BP holds the hotplug lock, but we're now running on the AP, 1083 * ensure that anybody asserting the lock is held, will actually find 1084 * it so. 1085 */ 1086 lockdep_acquire_cpus_lock(); 1087 cpuhp_lock_acquire(bringup); 1088 1089 if (st->single) { 1090 state = st->cb_state; 1091 st->should_run = false; 1092 } else { 1093 st->should_run = cpuhp_next_state(bringup, &state, st, st->target); 1094 if (!st->should_run) 1095 goto end; 1096 } 1097 1098 WARN_ON_ONCE(!cpuhp_is_ap_state(state)); 1099 1100 if (cpuhp_is_atomic_state(state)) { 1101 local_irq_disable(); 1102 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last); 1103 local_irq_enable(); 1104 1105 /* 1106 * STARTING/DYING must not fail! 1107 */ 1108 WARN_ON_ONCE(st->result); 1109 } else { 1110 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last); 1111 } 1112 1113 if (st->result) { 1114 /* 1115 * If we fail on a rollback, we're up a creek without no 1116 * paddle, no way forward, no way back. We loose, thanks for 1117 * playing. 1118 */ 1119 WARN_ON_ONCE(st->rollback); 1120 st->should_run = false; 1121 } 1122 1123 end: 1124 cpuhp_lock_release(bringup); 1125 lockdep_release_cpus_lock(); 1126 1127 if (!st->should_run) 1128 complete_ap_thread(st, bringup); 1129 } 1130 1131 /* Invoke a single callback on a remote cpu */ 1132 static int 1133 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup, 1134 struct hlist_node *node) 1135 { 1136 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1137 int ret; 1138 1139 if (!cpu_online(cpu)) 1140 return 0; 1141 1142 cpuhp_lock_acquire(false); 1143 cpuhp_lock_release(false); 1144 1145 cpuhp_lock_acquire(true); 1146 cpuhp_lock_release(true); 1147 1148 /* 1149 * If we are up and running, use the hotplug thread. For early calls 1150 * we invoke the thread function directly. 1151 */ 1152 if (!st->thread) 1153 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL); 1154 1155 st->rollback = false; 1156 st->last = NULL; 1157 1158 st->node = node; 1159 st->bringup = bringup; 1160 st->cb_state = state; 1161 st->single = true; 1162 1163 __cpuhp_kick_ap(st); 1164 1165 /* 1166 * If we failed and did a partial, do a rollback. 1167 */ 1168 if ((ret = st->result) && st->last) { 1169 st->rollback = true; 1170 st->bringup = !bringup; 1171 1172 __cpuhp_kick_ap(st); 1173 } 1174 1175 /* 1176 * Clean up the leftovers so the next hotplug operation wont use stale 1177 * data. 1178 */ 1179 st->node = st->last = NULL; 1180 return ret; 1181 } 1182 1183 static int cpuhp_kick_ap_work(unsigned int cpu) 1184 { 1185 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1186 enum cpuhp_state prev_state = st->state; 1187 int ret; 1188 1189 cpuhp_lock_acquire(false); 1190 cpuhp_lock_release(false); 1191 1192 cpuhp_lock_acquire(true); 1193 cpuhp_lock_release(true); 1194 1195 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work); 1196 ret = cpuhp_kick_ap(cpu, st, st->target); 1197 trace_cpuhp_exit(cpu, st->state, prev_state, ret); 1198 1199 return ret; 1200 } 1201 1202 static struct smp_hotplug_thread cpuhp_threads = { 1203 .store = &cpuhp_state.thread, 1204 .thread_should_run = cpuhp_should_run, 1205 .thread_fn = cpuhp_thread_fun, 1206 .thread_comm = "cpuhp/%u", 1207 .selfparking = true, 1208 }; 1209 1210 static __init void cpuhp_init_state(void) 1211 { 1212 struct cpuhp_cpu_state *st; 1213 int cpu; 1214 1215 for_each_possible_cpu(cpu) { 1216 st = per_cpu_ptr(&cpuhp_state, cpu); 1217 init_completion(&st->done_up); 1218 init_completion(&st->done_down); 1219 } 1220 } 1221 1222 void __init cpuhp_threads_init(void) 1223 { 1224 cpuhp_init_state(); 1225 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads)); 1226 kthread_unpark(this_cpu_read(cpuhp_state.thread)); 1227 } 1228 1229 #ifdef CONFIG_HOTPLUG_CPU 1230 #ifndef arch_clear_mm_cpumask_cpu 1231 #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm)) 1232 #endif 1233 1234 /** 1235 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU 1236 * @cpu: a CPU id 1237 * 1238 * This function walks all processes, finds a valid mm struct for each one and 1239 * then clears a corresponding bit in mm's cpumask. While this all sounds 1240 * trivial, there are various non-obvious corner cases, which this function 1241 * tries to solve in a safe manner. 1242 * 1243 * Also note that the function uses a somewhat relaxed locking scheme, so it may 1244 * be called only for an already offlined CPU. 1245 */ 1246 void clear_tasks_mm_cpumask(int cpu) 1247 { 1248 struct task_struct *p; 1249 1250 /* 1251 * This function is called after the cpu is taken down and marked 1252 * offline, so its not like new tasks will ever get this cpu set in 1253 * their mm mask. -- Peter Zijlstra 1254 * Thus, we may use rcu_read_lock() here, instead of grabbing 1255 * full-fledged tasklist_lock. 1256 */ 1257 WARN_ON(cpu_online(cpu)); 1258 rcu_read_lock(); 1259 for_each_process(p) { 1260 struct task_struct *t; 1261 1262 /* 1263 * Main thread might exit, but other threads may still have 1264 * a valid mm. Find one. 1265 */ 1266 t = find_lock_task_mm(p); 1267 if (!t) 1268 continue; 1269 arch_clear_mm_cpumask_cpu(cpu, t->mm); 1270 task_unlock(t); 1271 } 1272 rcu_read_unlock(); 1273 } 1274 1275 /* Take this CPU down. */ 1276 static int take_cpu_down(void *_param) 1277 { 1278 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 1279 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE); 1280 int err, cpu = smp_processor_id(); 1281 1282 /* Ensure this CPU doesn't handle any more interrupts. */ 1283 err = __cpu_disable(); 1284 if (err < 0) 1285 return err; 1286 1287 /* 1288 * Must be called from CPUHP_TEARDOWN_CPU, which means, as we are going 1289 * down, that the current state is CPUHP_TEARDOWN_CPU - 1. 1290 */ 1291 WARN_ON(st->state != (CPUHP_TEARDOWN_CPU - 1)); 1292 1293 /* 1294 * Invoke the former CPU_DYING callbacks. DYING must not fail! 1295 */ 1296 cpuhp_invoke_callback_range_nofail(false, cpu, st, target); 1297 1298 /* Park the stopper thread */ 1299 stop_machine_park(cpu); 1300 return 0; 1301 } 1302 1303 static int takedown_cpu(unsigned int cpu) 1304 { 1305 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1306 int err; 1307 1308 /* Park the smpboot threads */ 1309 kthread_park(st->thread); 1310 1311 /* 1312 * Prevent irq alloc/free while the dying cpu reorganizes the 1313 * interrupt affinities. 1314 */ 1315 irq_lock_sparse(); 1316 1317 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu)); 1318 if (err) { 1319 /* CPU refused to die */ 1320 irq_unlock_sparse(); 1321 /* Unpark the hotplug thread so we can rollback there */ 1322 kthread_unpark(st->thread); 1323 return err; 1324 } 1325 BUG_ON(cpu_online(cpu)); 1326 1327 /* 1328 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed 1329 * all runnable tasks from the CPU, there's only the idle task left now 1330 * that the migration thread is done doing the stop_machine thing. 1331 * 1332 * Wait for the stop thread to go away. 1333 */ 1334 wait_for_ap_thread(st, false); 1335 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD); 1336 1337 /* Interrupts are moved away from the dying cpu, reenable alloc/free */ 1338 irq_unlock_sparse(); 1339 1340 hotplug_cpu__broadcast_tick_pull(cpu); 1341 /* This actually kills the CPU. */ 1342 __cpu_die(cpu); 1343 1344 cpuhp_bp_sync_dead(cpu); 1345 1346 lockdep_cleanup_dead_cpu(cpu, idle_thread_get(cpu)); 1347 1348 /* 1349 * Callbacks must be re-integrated right away to the RCU state machine. 1350 * Otherwise an RCU callback could block a further teardown function 1351 * waiting for its completion. 1352 */ 1353 rcutree_migrate_callbacks(cpu); 1354 1355 return 0; 1356 } 1357 1358 static void cpuhp_complete_idle_dead(void *arg) 1359 { 1360 struct cpuhp_cpu_state *st = arg; 1361 1362 complete_ap_thread(st, false); 1363 } 1364 1365 void cpuhp_report_idle_dead(void) 1366 { 1367 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 1368 1369 BUG_ON(st->state != CPUHP_AP_OFFLINE); 1370 tick_assert_timekeeping_handover(); 1371 rcutree_report_cpu_dead(); 1372 st->state = CPUHP_AP_IDLE_DEAD; 1373 /* 1374 * We cannot call complete after rcutree_report_cpu_dead() so we delegate it 1375 * to an online cpu. 1376 */ 1377 smp_call_function_single(cpumask_first(cpu_online_mask), 1378 cpuhp_complete_idle_dead, st, 0); 1379 } 1380 1381 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st, 1382 enum cpuhp_state target) 1383 { 1384 enum cpuhp_state prev_state = st->state; 1385 int ret = 0; 1386 1387 ret = cpuhp_invoke_callback_range(false, cpu, st, target); 1388 if (ret) { 1389 pr_debug("CPU DOWN failed (%d) CPU %u state %s (%d)\n", 1390 ret, cpu, cpuhp_get_step(st->state)->name, 1391 st->state); 1392 1393 cpuhp_reset_state(cpu, st, prev_state); 1394 1395 if (st->state < prev_state) 1396 WARN_ON(cpuhp_invoke_callback_range(true, cpu, st, 1397 prev_state)); 1398 } 1399 1400 return ret; 1401 } 1402 1403 /* Requires cpu_add_remove_lock to be held */ 1404 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen, 1405 enum cpuhp_state target) 1406 { 1407 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1408 int prev_state, ret = 0; 1409 1410 if (num_online_cpus() == 1) 1411 return -EBUSY; 1412 1413 if (!cpu_present(cpu)) 1414 return -EINVAL; 1415 1416 cpus_write_lock(); 1417 1418 /* 1419 * Keep at least one housekeeping cpu onlined to avoid generating 1420 * an empty sched_domain span. 1421 */ 1422 if (cpumask_any_and(cpu_online_mask, 1423 housekeeping_cpumask(HK_TYPE_DOMAIN)) >= nr_cpu_ids) { 1424 ret = -EBUSY; 1425 goto out; 1426 } 1427 1428 cpuhp_tasks_frozen = tasks_frozen; 1429 1430 prev_state = cpuhp_set_state(cpu, st, target); 1431 /* 1432 * If the current CPU state is in the range of the AP hotplug thread, 1433 * then we need to kick the thread. 1434 */ 1435 if (st->state > CPUHP_TEARDOWN_CPU) { 1436 st->target = max((int)target, CPUHP_TEARDOWN_CPU); 1437 ret = cpuhp_kick_ap_work(cpu); 1438 /* 1439 * The AP side has done the error rollback already. Just 1440 * return the error code.. 1441 */ 1442 if (ret) 1443 goto out; 1444 1445 /* 1446 * We might have stopped still in the range of the AP hotplug 1447 * thread. Nothing to do anymore. 1448 */ 1449 if (st->state > CPUHP_TEARDOWN_CPU) 1450 goto out; 1451 1452 st->target = target; 1453 } 1454 /* 1455 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need 1456 * to do the further cleanups. 1457 */ 1458 ret = cpuhp_down_callbacks(cpu, st, target); 1459 if (ret && st->state < prev_state) { 1460 if (st->state == CPUHP_TEARDOWN_CPU) { 1461 cpuhp_reset_state(cpu, st, prev_state); 1462 __cpuhp_kick_ap(st); 1463 } else { 1464 WARN(1, "DEAD callback error for CPU%d", cpu); 1465 } 1466 } 1467 1468 out: 1469 cpus_write_unlock(); 1470 arch_smt_update(); 1471 return ret; 1472 } 1473 1474 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target) 1475 { 1476 /* 1477 * If the platform does not support hotplug, report it explicitly to 1478 * differentiate it from a transient offlining failure. 1479 */ 1480 if (cpu_hotplug_offline_disabled) 1481 return -EOPNOTSUPP; 1482 if (cpu_hotplug_disabled) 1483 return -EBUSY; 1484 return _cpu_down(cpu, 0, target); 1485 } 1486 1487 static int cpu_down(unsigned int cpu, enum cpuhp_state target) 1488 { 1489 int err; 1490 1491 cpu_maps_update_begin(); 1492 err = cpu_down_maps_locked(cpu, target); 1493 cpu_maps_update_done(); 1494 return err; 1495 } 1496 1497 /** 1498 * cpu_device_down - Bring down a cpu device 1499 * @dev: Pointer to the cpu device to offline 1500 * 1501 * This function is meant to be used by device core cpu subsystem only. 1502 * 1503 * Other subsystems should use remove_cpu() instead. 1504 * 1505 * Return: %0 on success or a negative errno code 1506 */ 1507 int cpu_device_down(struct device *dev) 1508 { 1509 return cpu_down(dev->id, CPUHP_OFFLINE); 1510 } 1511 1512 int remove_cpu(unsigned int cpu) 1513 { 1514 int ret; 1515 1516 lock_device_hotplug(); 1517 ret = device_offline(get_cpu_device(cpu)); 1518 unlock_device_hotplug(); 1519 1520 return ret; 1521 } 1522 EXPORT_SYMBOL_GPL(remove_cpu); 1523 1524 void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) 1525 { 1526 unsigned int cpu; 1527 int error; 1528 1529 cpu_maps_update_begin(); 1530 1531 /* 1532 * Make certain the cpu I'm about to reboot on is online. 1533 * 1534 * This is inline to what migrate_to_reboot_cpu() already do. 1535 */ 1536 if (!cpu_online(primary_cpu)) 1537 primary_cpu = cpumask_first(cpu_online_mask); 1538 1539 for_each_online_cpu(cpu) { 1540 if (cpu == primary_cpu) 1541 continue; 1542 1543 error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE); 1544 if (error) { 1545 pr_err("Failed to offline CPU%d - error=%d", 1546 cpu, error); 1547 break; 1548 } 1549 } 1550 1551 /* 1552 * Ensure all but the reboot CPU are offline. 1553 */ 1554 BUG_ON(num_online_cpus() > 1); 1555 1556 /* 1557 * Make sure the CPUs won't be enabled by someone else after this 1558 * point. Kexec will reboot to a new kernel shortly resetting 1559 * everything along the way. 1560 */ 1561 cpu_hotplug_disabled++; 1562 1563 cpu_maps_update_done(); 1564 } 1565 1566 #else 1567 #define takedown_cpu NULL 1568 #endif /*CONFIG_HOTPLUG_CPU*/ 1569 1570 /** 1571 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU 1572 * @cpu: cpu that just started 1573 * 1574 * It must be called by the arch code on the new cpu, before the new cpu 1575 * enables interrupts and before the "boot" cpu returns from __cpu_up(). 1576 */ 1577 void notify_cpu_starting(unsigned int cpu) 1578 { 1579 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1580 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE); 1581 1582 rcutree_report_cpu_starting(cpu); /* Enables RCU usage on this CPU. */ 1583 cpumask_set_cpu(cpu, &cpus_booted_once_mask); 1584 1585 /* 1586 * STARTING must not fail! 1587 */ 1588 cpuhp_invoke_callback_range_nofail(true, cpu, st, target); 1589 } 1590 1591 /* 1592 * Called from the idle task. Wake up the controlling task which brings the 1593 * hotplug thread of the upcoming CPU up and then delegates the rest of the 1594 * online bringup to the hotplug thread. 1595 */ 1596 void cpuhp_online_idle(enum cpuhp_state state) 1597 { 1598 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 1599 1600 /* Happens for the boot cpu */ 1601 if (state != CPUHP_AP_ONLINE_IDLE) 1602 return; 1603 1604 cpuhp_ap_update_sync_state(SYNC_STATE_ONLINE); 1605 1606 /* 1607 * Unpark the stopper thread before we start the idle loop (and start 1608 * scheduling); this ensures the stopper task is always available. 1609 */ 1610 stop_machine_unpark(smp_processor_id()); 1611 1612 st->state = CPUHP_AP_ONLINE_IDLE; 1613 complete_ap_thread(st, true); 1614 } 1615 1616 /* Requires cpu_add_remove_lock to be held */ 1617 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target) 1618 { 1619 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1620 struct task_struct *idle; 1621 int ret = 0; 1622 1623 cpus_write_lock(); 1624 1625 if (!cpu_present(cpu)) { 1626 ret = -EINVAL; 1627 goto out; 1628 } 1629 1630 /* 1631 * The caller of cpu_up() might have raced with another 1632 * caller. Nothing to do. 1633 */ 1634 if (st->state >= target) 1635 goto out; 1636 1637 if (st->state == CPUHP_OFFLINE) { 1638 /* Let it fail before we try to bring the cpu up */ 1639 idle = idle_thread_get(cpu); 1640 if (IS_ERR(idle)) { 1641 ret = PTR_ERR(idle); 1642 goto out; 1643 } 1644 1645 /* 1646 * Reset stale stack state from the last time this CPU was online. 1647 */ 1648 scs_task_reset(idle); 1649 kasan_unpoison_task_stack(idle); 1650 } 1651 1652 cpuhp_tasks_frozen = tasks_frozen; 1653 1654 cpuhp_set_state(cpu, st, target); 1655 /* 1656 * If the current CPU state is in the range of the AP hotplug thread, 1657 * then we need to kick the thread once more. 1658 */ 1659 if (st->state > CPUHP_BRINGUP_CPU) { 1660 ret = cpuhp_kick_ap_work(cpu); 1661 /* 1662 * The AP side has done the error rollback already. Just 1663 * return the error code.. 1664 */ 1665 if (ret) 1666 goto out; 1667 } 1668 1669 /* 1670 * Try to reach the target state. We max out on the BP at 1671 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is 1672 * responsible for bringing it up to the target state. 1673 */ 1674 target = min((int)target, CPUHP_BRINGUP_CPU); 1675 ret = cpuhp_up_callbacks(cpu, st, target); 1676 out: 1677 cpus_write_unlock(); 1678 arch_smt_update(); 1679 return ret; 1680 } 1681 1682 static int cpu_up(unsigned int cpu, enum cpuhp_state target) 1683 { 1684 int err = 0; 1685 1686 if (!cpu_possible(cpu)) { 1687 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n", 1688 cpu); 1689 return -EINVAL; 1690 } 1691 1692 err = try_online_node(cpu_to_node(cpu)); 1693 if (err) 1694 return err; 1695 1696 cpu_maps_update_begin(); 1697 1698 if (cpu_hotplug_disabled) { 1699 err = -EBUSY; 1700 goto out; 1701 } 1702 if (!cpu_bootable(cpu)) { 1703 err = -EPERM; 1704 goto out; 1705 } 1706 1707 err = _cpu_up(cpu, 0, target); 1708 out: 1709 cpu_maps_update_done(); 1710 return err; 1711 } 1712 1713 /** 1714 * cpu_device_up - Bring up a cpu device 1715 * @dev: Pointer to the cpu device to online 1716 * 1717 * This function is meant to be used by device core cpu subsystem only. 1718 * 1719 * Other subsystems should use add_cpu() instead. 1720 * 1721 * Return: %0 on success or a negative errno code 1722 */ 1723 int cpu_device_up(struct device *dev) 1724 { 1725 return cpu_up(dev->id, CPUHP_ONLINE); 1726 } 1727 1728 int add_cpu(unsigned int cpu) 1729 { 1730 int ret; 1731 1732 lock_device_hotplug(); 1733 ret = device_online(get_cpu_device(cpu)); 1734 unlock_device_hotplug(); 1735 1736 return ret; 1737 } 1738 EXPORT_SYMBOL_GPL(add_cpu); 1739 1740 /** 1741 * bringup_hibernate_cpu - Bring up the CPU that we hibernated on 1742 * @sleep_cpu: The cpu we hibernated on and should be brought up. 1743 * 1744 * On some architectures like arm64, we can hibernate on any CPU, but on 1745 * wake up the CPU we hibernated on might be offline as a side effect of 1746 * using maxcpus= for example. 1747 * 1748 * Return: %0 on success or a negative errno code 1749 */ 1750 int bringup_hibernate_cpu(unsigned int sleep_cpu) 1751 { 1752 int ret; 1753 1754 if (!cpu_online(sleep_cpu)) { 1755 pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n"); 1756 ret = cpu_up(sleep_cpu, CPUHP_ONLINE); 1757 if (ret) { 1758 pr_err("Failed to bring hibernate-CPU up!\n"); 1759 return ret; 1760 } 1761 } 1762 return 0; 1763 } 1764 1765 static void __init cpuhp_bringup_mask(const struct cpumask *mask, unsigned int ncpus, 1766 enum cpuhp_state target) 1767 { 1768 unsigned int cpu; 1769 1770 for_each_cpu(cpu, mask) { 1771 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1772 1773 if (cpu_up(cpu, target) && can_rollback_cpu(st)) { 1774 /* 1775 * If this failed then cpu_up() might have only 1776 * rolled back to CPUHP_BP_KICK_AP for the final 1777 * online. Clean it up. NOOP if already rolled back. 1778 */ 1779 WARN_ON(cpuhp_invoke_callback_range(false, cpu, st, CPUHP_OFFLINE)); 1780 } 1781 1782 if (!--ncpus) 1783 break; 1784 } 1785 } 1786 1787 #ifdef CONFIG_HOTPLUG_PARALLEL 1788 static bool __cpuhp_parallel_bringup __ro_after_init = true; 1789 1790 static int __init parallel_bringup_parse_param(char *arg) 1791 { 1792 return kstrtobool(arg, &__cpuhp_parallel_bringup); 1793 } 1794 early_param("cpuhp.parallel", parallel_bringup_parse_param); 1795 1796 #ifdef CONFIG_HOTPLUG_SMT 1797 static inline bool cpuhp_smt_aware(void) 1798 { 1799 return cpu_smt_max_threads > 1; 1800 } 1801 1802 static inline const struct cpumask *cpuhp_get_primary_thread_mask(void) 1803 { 1804 return cpu_primary_thread_mask; 1805 } 1806 #else 1807 static inline bool cpuhp_smt_aware(void) 1808 { 1809 return false; 1810 } 1811 static inline const struct cpumask *cpuhp_get_primary_thread_mask(void) 1812 { 1813 return cpu_none_mask; 1814 } 1815 #endif 1816 1817 bool __weak arch_cpuhp_init_parallel_bringup(void) 1818 { 1819 return true; 1820 } 1821 1822 /* 1823 * On architectures which have enabled parallel bringup this invokes all BP 1824 * prepare states for each of the to be onlined APs first. The last state 1825 * sends the startup IPI to the APs. The APs proceed through the low level 1826 * bringup code in parallel and then wait for the control CPU to release 1827 * them one by one for the final onlining procedure. 1828 * 1829 * This avoids waiting for each AP to respond to the startup IPI in 1830 * CPUHP_BRINGUP_CPU. 1831 */ 1832 static bool __init cpuhp_bringup_cpus_parallel(unsigned int ncpus) 1833 { 1834 const struct cpumask *mask = cpu_present_mask; 1835 1836 if (__cpuhp_parallel_bringup) 1837 __cpuhp_parallel_bringup = arch_cpuhp_init_parallel_bringup(); 1838 if (!__cpuhp_parallel_bringup) 1839 return false; 1840 1841 if (cpuhp_smt_aware()) { 1842 const struct cpumask *pmask = cpuhp_get_primary_thread_mask(); 1843 static struct cpumask tmp_mask __initdata; 1844 1845 /* 1846 * X86 requires to prevent that SMT siblings stopped while 1847 * the primary thread does a microcode update for various 1848 * reasons. Bring the primary threads up first. 1849 */ 1850 cpumask_and(&tmp_mask, mask, pmask); 1851 cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_BP_KICK_AP); 1852 cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_ONLINE); 1853 /* Account for the online CPUs */ 1854 ncpus -= num_online_cpus(); 1855 if (!ncpus) 1856 return true; 1857 /* Create the mask for secondary CPUs */ 1858 cpumask_andnot(&tmp_mask, mask, pmask); 1859 mask = &tmp_mask; 1860 } 1861 1862 /* Bring the not-yet started CPUs up */ 1863 cpuhp_bringup_mask(mask, ncpus, CPUHP_BP_KICK_AP); 1864 cpuhp_bringup_mask(mask, ncpus, CPUHP_ONLINE); 1865 return true; 1866 } 1867 #else 1868 static inline bool cpuhp_bringup_cpus_parallel(unsigned int ncpus) { return false; } 1869 #endif /* CONFIG_HOTPLUG_PARALLEL */ 1870 1871 void __init bringup_nonboot_cpus(unsigned int max_cpus) 1872 { 1873 if (!max_cpus) 1874 return; 1875 1876 /* Try parallel bringup optimization if enabled */ 1877 if (cpuhp_bringup_cpus_parallel(max_cpus)) 1878 return; 1879 1880 /* Full per CPU serialized bringup */ 1881 cpuhp_bringup_mask(cpu_present_mask, max_cpus, CPUHP_ONLINE); 1882 } 1883 1884 #ifdef CONFIG_PM_SLEEP_SMP 1885 static cpumask_var_t frozen_cpus; 1886 1887 int freeze_secondary_cpus(int primary) 1888 { 1889 int cpu, error = 0; 1890 1891 cpu_maps_update_begin(); 1892 if (primary == -1) { 1893 primary = cpumask_first(cpu_online_mask); 1894 if (!housekeeping_cpu(primary, HK_TYPE_TIMER)) 1895 primary = housekeeping_any_cpu(HK_TYPE_TIMER); 1896 } else { 1897 if (!cpu_online(primary)) 1898 primary = cpumask_first(cpu_online_mask); 1899 } 1900 1901 /* 1902 * We take down all of the non-boot CPUs in one shot to avoid races 1903 * with the userspace trying to use the CPU hotplug at the same time 1904 */ 1905 cpumask_clear(frozen_cpus); 1906 1907 pr_info("Disabling non-boot CPUs ...\n"); 1908 for (cpu = nr_cpu_ids - 1; cpu >= 0; cpu--) { 1909 if (!cpu_online(cpu) || cpu == primary) 1910 continue; 1911 1912 if (pm_wakeup_pending()) { 1913 pr_info("Wakeup pending. Abort CPU freeze\n"); 1914 error = -EBUSY; 1915 break; 1916 } 1917 1918 trace_suspend_resume(TPS("CPU_OFF"), cpu, true); 1919 error = _cpu_down(cpu, 1, CPUHP_OFFLINE); 1920 trace_suspend_resume(TPS("CPU_OFF"), cpu, false); 1921 if (!error) 1922 cpumask_set_cpu(cpu, frozen_cpus); 1923 else { 1924 pr_err("Error taking CPU%d down: %d\n", cpu, error); 1925 break; 1926 } 1927 } 1928 1929 if (!error) 1930 BUG_ON(num_online_cpus() > 1); 1931 else 1932 pr_err("Non-boot CPUs are not disabled\n"); 1933 1934 /* 1935 * Make sure the CPUs won't be enabled by someone else. We need to do 1936 * this even in case of failure as all freeze_secondary_cpus() users are 1937 * supposed to do thaw_secondary_cpus() on the failure path. 1938 */ 1939 cpu_hotplug_disabled++; 1940 1941 cpu_maps_update_done(); 1942 return error; 1943 } 1944 1945 void __weak arch_thaw_secondary_cpus_begin(void) 1946 { 1947 } 1948 1949 void __weak arch_thaw_secondary_cpus_end(void) 1950 { 1951 } 1952 1953 void thaw_secondary_cpus(void) 1954 { 1955 int cpu, error; 1956 1957 /* Allow everyone to use the CPU hotplug again */ 1958 cpu_maps_update_begin(); 1959 __cpu_hotplug_enable(); 1960 if (cpumask_empty(frozen_cpus)) 1961 goto out; 1962 1963 pr_info("Enabling non-boot CPUs ...\n"); 1964 1965 arch_thaw_secondary_cpus_begin(); 1966 1967 for_each_cpu(cpu, frozen_cpus) { 1968 trace_suspend_resume(TPS("CPU_ON"), cpu, true); 1969 error = _cpu_up(cpu, 1, CPUHP_ONLINE); 1970 trace_suspend_resume(TPS("CPU_ON"), cpu, false); 1971 if (!error) { 1972 pr_info("CPU%d is up\n", cpu); 1973 continue; 1974 } 1975 pr_warn("Error taking CPU%d up: %d\n", cpu, error); 1976 } 1977 1978 arch_thaw_secondary_cpus_end(); 1979 1980 cpumask_clear(frozen_cpus); 1981 out: 1982 cpu_maps_update_done(); 1983 } 1984 1985 static int __init alloc_frozen_cpus(void) 1986 { 1987 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO)) 1988 return -ENOMEM; 1989 return 0; 1990 } 1991 core_initcall(alloc_frozen_cpus); 1992 1993 /* 1994 * When callbacks for CPU hotplug notifications are being executed, we must 1995 * ensure that the state of the system with respect to the tasks being frozen 1996 * or not, as reported by the notification, remains unchanged *throughout the 1997 * duration* of the execution of the callbacks. 1998 * Hence we need to prevent the freezer from racing with regular CPU hotplug. 1999 * 2000 * This synchronization is implemented by mutually excluding regular CPU 2001 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/ 2002 * Hibernate notifications. 2003 */ 2004 static int 2005 cpu_hotplug_pm_callback(struct notifier_block *nb, 2006 unsigned long action, void *ptr) 2007 { 2008 switch (action) { 2009 2010 case PM_SUSPEND_PREPARE: 2011 case PM_HIBERNATION_PREPARE: 2012 cpu_hotplug_disable(); 2013 break; 2014 2015 case PM_POST_SUSPEND: 2016 case PM_POST_HIBERNATION: 2017 cpu_hotplug_enable(); 2018 break; 2019 2020 default: 2021 return NOTIFY_DONE; 2022 } 2023 2024 return NOTIFY_OK; 2025 } 2026 2027 2028 static int __init cpu_hotplug_pm_sync_init(void) 2029 { 2030 /* 2031 * cpu_hotplug_pm_callback has higher priority than x86 2032 * bsp_pm_callback which depends on cpu_hotplug_pm_callback 2033 * to disable cpu hotplug to avoid cpu hotplug race. 2034 */ 2035 pm_notifier(cpu_hotplug_pm_callback, 0); 2036 return 0; 2037 } 2038 core_initcall(cpu_hotplug_pm_sync_init); 2039 2040 #endif /* CONFIG_PM_SLEEP_SMP */ 2041 2042 int __boot_cpu_id; 2043 2044 #endif /* CONFIG_SMP */ 2045 2046 /* Boot processor state steps */ 2047 static struct cpuhp_step cpuhp_hp_states[] = { 2048 [CPUHP_OFFLINE] = { 2049 .name = "offline", 2050 .startup.single = NULL, 2051 .teardown.single = NULL, 2052 }, 2053 #ifdef CONFIG_SMP 2054 [CPUHP_CREATE_THREADS]= { 2055 .name = "threads:prepare", 2056 .startup.single = smpboot_create_threads, 2057 .teardown.single = NULL, 2058 .cant_stop = true, 2059 }, 2060 [CPUHP_RANDOM_PREPARE] = { 2061 .name = "random:prepare", 2062 .startup.single = random_prepare_cpu, 2063 .teardown.single = NULL, 2064 }, 2065 [CPUHP_WORKQUEUE_PREP] = { 2066 .name = "workqueue:prepare", 2067 .startup.single = workqueue_prepare_cpu, 2068 .teardown.single = NULL, 2069 }, 2070 [CPUHP_HRTIMERS_PREPARE] = { 2071 .name = "hrtimers:prepare", 2072 .startup.single = hrtimers_prepare_cpu, 2073 .teardown.single = NULL, 2074 }, 2075 [CPUHP_SMPCFD_PREPARE] = { 2076 .name = "smpcfd:prepare", 2077 .startup.single = smpcfd_prepare_cpu, 2078 .teardown.single = smpcfd_dead_cpu, 2079 }, 2080 [CPUHP_RELAY_PREPARE] = { 2081 .name = "relay:prepare", 2082 .startup.single = relay_prepare_cpu, 2083 .teardown.single = NULL, 2084 }, 2085 [CPUHP_RCUTREE_PREP] = { 2086 .name = "RCU/tree:prepare", 2087 .startup.single = rcutree_prepare_cpu, 2088 .teardown.single = rcutree_dead_cpu, 2089 }, 2090 /* 2091 * On the tear-down path, timers_dead_cpu() must be invoked 2092 * before blk_mq_queue_reinit_notify() from notify_dead(), 2093 * otherwise a RCU stall occurs. 2094 */ 2095 [CPUHP_TIMERS_PREPARE] = { 2096 .name = "timers:prepare", 2097 .startup.single = timers_prepare_cpu, 2098 .teardown.single = timers_dead_cpu, 2099 }, 2100 2101 #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP 2102 /* 2103 * Kicks the AP alive. AP will wait in cpuhp_ap_sync_alive() until 2104 * the next step will release it. 2105 */ 2106 [CPUHP_BP_KICK_AP] = { 2107 .name = "cpu:kick_ap", 2108 .startup.single = cpuhp_kick_ap_alive, 2109 }, 2110 2111 /* 2112 * Waits for the AP to reach cpuhp_ap_sync_alive() and then 2113 * releases it for the complete bringup. 2114 */ 2115 [CPUHP_BRINGUP_CPU] = { 2116 .name = "cpu:bringup", 2117 .startup.single = cpuhp_bringup_ap, 2118 .teardown.single = finish_cpu, 2119 .cant_stop = true, 2120 }, 2121 #else 2122 /* 2123 * All-in-one CPU bringup state which includes the kick alive. 2124 */ 2125 [CPUHP_BRINGUP_CPU] = { 2126 .name = "cpu:bringup", 2127 .startup.single = bringup_cpu, 2128 .teardown.single = finish_cpu, 2129 .cant_stop = true, 2130 }, 2131 #endif 2132 /* Final state before CPU kills itself */ 2133 [CPUHP_AP_IDLE_DEAD] = { 2134 .name = "idle:dead", 2135 }, 2136 /* 2137 * Last state before CPU enters the idle loop to die. Transient state 2138 * for synchronization. 2139 */ 2140 [CPUHP_AP_OFFLINE] = { 2141 .name = "ap:offline", 2142 .cant_stop = true, 2143 }, 2144 /* First state is scheduler control. Interrupts are disabled */ 2145 [CPUHP_AP_SCHED_STARTING] = { 2146 .name = "sched:starting", 2147 .startup.single = sched_cpu_starting, 2148 .teardown.single = sched_cpu_dying, 2149 }, 2150 [CPUHP_AP_RCUTREE_DYING] = { 2151 .name = "RCU/tree:dying", 2152 .startup.single = NULL, 2153 .teardown.single = rcutree_dying_cpu, 2154 }, 2155 [CPUHP_AP_SMPCFD_DYING] = { 2156 .name = "smpcfd:dying", 2157 .startup.single = NULL, 2158 .teardown.single = smpcfd_dying_cpu, 2159 }, 2160 [CPUHP_AP_HRTIMERS_DYING] = { 2161 .name = "hrtimers:dying", 2162 .startup.single = hrtimers_cpu_starting, 2163 .teardown.single = hrtimers_cpu_dying, 2164 }, 2165 [CPUHP_AP_TICK_DYING] = { 2166 .name = "tick:dying", 2167 .startup.single = NULL, 2168 .teardown.single = tick_cpu_dying, 2169 }, 2170 /* Entry state on starting. Interrupts enabled from here on. Transient 2171 * state for synchronsization */ 2172 [CPUHP_AP_ONLINE] = { 2173 .name = "ap:online", 2174 }, 2175 /* 2176 * Handled on control processor until the plugged processor manages 2177 * this itself. 2178 */ 2179 [CPUHP_TEARDOWN_CPU] = { 2180 .name = "cpu:teardown", 2181 .startup.single = NULL, 2182 .teardown.single = takedown_cpu, 2183 .cant_stop = true, 2184 }, 2185 2186 [CPUHP_AP_SCHED_WAIT_EMPTY] = { 2187 .name = "sched:waitempty", 2188 .startup.single = NULL, 2189 .teardown.single = sched_cpu_wait_empty, 2190 }, 2191 2192 /* Handle smpboot threads park/unpark */ 2193 [CPUHP_AP_SMPBOOT_THREADS] = { 2194 .name = "smpboot/threads:online", 2195 .startup.single = smpboot_unpark_threads, 2196 .teardown.single = smpboot_park_threads, 2197 }, 2198 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = { 2199 .name = "irq/affinity:online", 2200 .startup.single = irq_affinity_online_cpu, 2201 .teardown.single = NULL, 2202 }, 2203 [CPUHP_AP_PERF_ONLINE] = { 2204 .name = "perf:online", 2205 .startup.single = perf_event_init_cpu, 2206 .teardown.single = perf_event_exit_cpu, 2207 }, 2208 [CPUHP_AP_WATCHDOG_ONLINE] = { 2209 .name = "lockup_detector:online", 2210 .startup.single = lockup_detector_online_cpu, 2211 .teardown.single = lockup_detector_offline_cpu, 2212 }, 2213 [CPUHP_AP_WORKQUEUE_ONLINE] = { 2214 .name = "workqueue:online", 2215 .startup.single = workqueue_online_cpu, 2216 .teardown.single = workqueue_offline_cpu, 2217 }, 2218 [CPUHP_AP_RANDOM_ONLINE] = { 2219 .name = "random:online", 2220 .startup.single = random_online_cpu, 2221 .teardown.single = NULL, 2222 }, 2223 [CPUHP_AP_RCUTREE_ONLINE] = { 2224 .name = "RCU/tree:online", 2225 .startup.single = rcutree_online_cpu, 2226 .teardown.single = rcutree_offline_cpu, 2227 }, 2228 #endif 2229 /* 2230 * The dynamically registered state space is here 2231 */ 2232 2233 #ifdef CONFIG_SMP 2234 /* Last state is scheduler control setting the cpu active */ 2235 [CPUHP_AP_ACTIVE] = { 2236 .name = "sched:active", 2237 .startup.single = sched_cpu_activate, 2238 .teardown.single = sched_cpu_deactivate, 2239 }, 2240 #endif 2241 2242 /* CPU is fully up and running. */ 2243 [CPUHP_ONLINE] = { 2244 .name = "online", 2245 .startup.single = NULL, 2246 .teardown.single = NULL, 2247 }, 2248 }; 2249 2250 /* Sanity check for callbacks */ 2251 static int cpuhp_cb_check(enum cpuhp_state state) 2252 { 2253 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE) 2254 return -EINVAL; 2255 return 0; 2256 } 2257 2258 /* 2259 * Returns a free for dynamic slot assignment of the Online state. The states 2260 * are protected by the cpuhp_slot_states mutex and an empty slot is identified 2261 * by having no name assigned. 2262 */ 2263 static int cpuhp_reserve_state(enum cpuhp_state state) 2264 { 2265 enum cpuhp_state i, end; 2266 struct cpuhp_step *step; 2267 2268 switch (state) { 2269 case CPUHP_AP_ONLINE_DYN: 2270 step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN; 2271 end = CPUHP_AP_ONLINE_DYN_END; 2272 break; 2273 case CPUHP_BP_PREPARE_DYN: 2274 step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN; 2275 end = CPUHP_BP_PREPARE_DYN_END; 2276 break; 2277 default: 2278 return -EINVAL; 2279 } 2280 2281 for (i = state; i <= end; i++, step++) { 2282 if (!step->name) 2283 return i; 2284 } 2285 WARN(1, "No more dynamic states available for CPU hotplug\n"); 2286 return -ENOSPC; 2287 } 2288 2289 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name, 2290 int (*startup)(unsigned int cpu), 2291 int (*teardown)(unsigned int cpu), 2292 bool multi_instance) 2293 { 2294 /* (Un)Install the callbacks for further cpu hotplug operations */ 2295 struct cpuhp_step *sp; 2296 int ret = 0; 2297 2298 /* 2299 * If name is NULL, then the state gets removed. 2300 * 2301 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on 2302 * the first allocation from these dynamic ranges, so the removal 2303 * would trigger a new allocation and clear the wrong (already 2304 * empty) state, leaving the callbacks of the to be cleared state 2305 * dangling, which causes wreckage on the next hotplug operation. 2306 */ 2307 if (name && (state == CPUHP_AP_ONLINE_DYN || 2308 state == CPUHP_BP_PREPARE_DYN)) { 2309 ret = cpuhp_reserve_state(state); 2310 if (ret < 0) 2311 return ret; 2312 state = ret; 2313 } 2314 sp = cpuhp_get_step(state); 2315 if (name && sp->name) 2316 return -EBUSY; 2317 2318 sp->startup.single = startup; 2319 sp->teardown.single = teardown; 2320 sp->name = name; 2321 sp->multi_instance = multi_instance; 2322 INIT_HLIST_HEAD(&sp->list); 2323 return ret; 2324 } 2325 2326 static void *cpuhp_get_teardown_cb(enum cpuhp_state state) 2327 { 2328 return cpuhp_get_step(state)->teardown.single; 2329 } 2330 2331 /* 2332 * Call the startup/teardown function for a step either on the AP or 2333 * on the current CPU. 2334 */ 2335 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup, 2336 struct hlist_node *node) 2337 { 2338 struct cpuhp_step *sp = cpuhp_get_step(state); 2339 int ret; 2340 2341 /* 2342 * If there's nothing to do, we done. 2343 * Relies on the union for multi_instance. 2344 */ 2345 if (cpuhp_step_empty(bringup, sp)) 2346 return 0; 2347 /* 2348 * The non AP bound callbacks can fail on bringup. On teardown 2349 * e.g. module removal we crash for now. 2350 */ 2351 #ifdef CONFIG_SMP 2352 if (cpuhp_is_ap_state(state)) 2353 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node); 2354 else 2355 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL); 2356 #else 2357 if (cpuhp_is_atomic_state(state)) { 2358 guard(irqsave)(); 2359 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL); 2360 /* STARTING/DYING must not fail! */ 2361 WARN_ON_ONCE(ret); 2362 } else { 2363 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL); 2364 } 2365 #endif 2366 BUG_ON(ret && !bringup); 2367 return ret; 2368 } 2369 2370 /* 2371 * Called from __cpuhp_setup_state on a recoverable failure. 2372 * 2373 * Note: The teardown callbacks for rollback are not allowed to fail! 2374 */ 2375 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state, 2376 struct hlist_node *node) 2377 { 2378 int cpu; 2379 2380 /* Roll back the already executed steps on the other cpus */ 2381 for_each_present_cpu(cpu) { 2382 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 2383 int cpustate = st->state; 2384 2385 if (cpu >= failedcpu) 2386 break; 2387 2388 /* Did we invoke the startup call on that cpu ? */ 2389 if (cpustate >= state) 2390 cpuhp_issue_call(cpu, state, false, node); 2391 } 2392 } 2393 2394 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state, 2395 struct hlist_node *node, 2396 bool invoke) 2397 { 2398 struct cpuhp_step *sp; 2399 int cpu; 2400 int ret; 2401 2402 lockdep_assert_cpus_held(); 2403 2404 sp = cpuhp_get_step(state); 2405 if (sp->multi_instance == false) 2406 return -EINVAL; 2407 2408 mutex_lock(&cpuhp_state_mutex); 2409 2410 if (!invoke || !sp->startup.multi) 2411 goto add_node; 2412 2413 /* 2414 * Try to call the startup callback for each present cpu 2415 * depending on the hotplug state of the cpu. 2416 */ 2417 for_each_present_cpu(cpu) { 2418 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 2419 int cpustate = st->state; 2420 2421 if (cpustate < state) 2422 continue; 2423 2424 ret = cpuhp_issue_call(cpu, state, true, node); 2425 if (ret) { 2426 if (sp->teardown.multi) 2427 cpuhp_rollback_install(cpu, state, node); 2428 goto unlock; 2429 } 2430 } 2431 add_node: 2432 ret = 0; 2433 hlist_add_head(node, &sp->list); 2434 unlock: 2435 mutex_unlock(&cpuhp_state_mutex); 2436 return ret; 2437 } 2438 2439 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node, 2440 bool invoke) 2441 { 2442 int ret; 2443 2444 cpus_read_lock(); 2445 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke); 2446 cpus_read_unlock(); 2447 return ret; 2448 } 2449 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance); 2450 2451 /** 2452 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state 2453 * @state: The state to setup 2454 * @name: Name of the step 2455 * @invoke: If true, the startup function is invoked for cpus where 2456 * cpu state >= @state 2457 * @startup: startup callback function 2458 * @teardown: teardown callback function 2459 * @multi_instance: State is set up for multiple instances which get 2460 * added afterwards. 2461 * 2462 * The caller needs to hold cpus read locked while calling this function. 2463 * Return: 2464 * On success: 2465 * Positive state number if @state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN; 2466 * 0 for all other states 2467 * On failure: proper (negative) error code 2468 */ 2469 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state, 2470 const char *name, bool invoke, 2471 int (*startup)(unsigned int cpu), 2472 int (*teardown)(unsigned int cpu), 2473 bool multi_instance) 2474 { 2475 int cpu, ret = 0; 2476 bool dynstate; 2477 2478 lockdep_assert_cpus_held(); 2479 2480 if (cpuhp_cb_check(state) || !name) 2481 return -EINVAL; 2482 2483 mutex_lock(&cpuhp_state_mutex); 2484 2485 ret = cpuhp_store_callbacks(state, name, startup, teardown, 2486 multi_instance); 2487 2488 dynstate = state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN; 2489 if (ret > 0 && dynstate) { 2490 state = ret; 2491 ret = 0; 2492 } 2493 2494 if (ret || !invoke || !startup) 2495 goto out; 2496 2497 /* 2498 * Try to call the startup callback for each present cpu 2499 * depending on the hotplug state of the cpu. 2500 */ 2501 for_each_present_cpu(cpu) { 2502 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 2503 int cpustate = st->state; 2504 2505 if (cpustate < state) 2506 continue; 2507 2508 ret = cpuhp_issue_call(cpu, state, true, NULL); 2509 if (ret) { 2510 if (teardown) 2511 cpuhp_rollback_install(cpu, state, NULL); 2512 cpuhp_store_callbacks(state, NULL, NULL, NULL, false); 2513 goto out; 2514 } 2515 } 2516 out: 2517 mutex_unlock(&cpuhp_state_mutex); 2518 /* 2519 * If the requested state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN, 2520 * return the dynamically allocated state in case of success. 2521 */ 2522 if (!ret && dynstate) 2523 return state; 2524 return ret; 2525 } 2526 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked); 2527 2528 int __cpuhp_setup_state(enum cpuhp_state state, 2529 const char *name, bool invoke, 2530 int (*startup)(unsigned int cpu), 2531 int (*teardown)(unsigned int cpu), 2532 bool multi_instance) 2533 { 2534 int ret; 2535 2536 cpus_read_lock(); 2537 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup, 2538 teardown, multi_instance); 2539 cpus_read_unlock(); 2540 return ret; 2541 } 2542 EXPORT_SYMBOL(__cpuhp_setup_state); 2543 2544 int __cpuhp_state_remove_instance(enum cpuhp_state state, 2545 struct hlist_node *node, bool invoke) 2546 { 2547 struct cpuhp_step *sp = cpuhp_get_step(state); 2548 int cpu; 2549 2550 BUG_ON(cpuhp_cb_check(state)); 2551 2552 if (!sp->multi_instance) 2553 return -EINVAL; 2554 2555 cpus_read_lock(); 2556 mutex_lock(&cpuhp_state_mutex); 2557 2558 if (!invoke || !cpuhp_get_teardown_cb(state)) 2559 goto remove; 2560 /* 2561 * Call the teardown callback for each present cpu depending 2562 * on the hotplug state of the cpu. This function is not 2563 * allowed to fail currently! 2564 */ 2565 for_each_present_cpu(cpu) { 2566 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 2567 int cpustate = st->state; 2568 2569 if (cpustate >= state) 2570 cpuhp_issue_call(cpu, state, false, node); 2571 } 2572 2573 remove: 2574 hlist_del(node); 2575 mutex_unlock(&cpuhp_state_mutex); 2576 cpus_read_unlock(); 2577 2578 return 0; 2579 } 2580 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance); 2581 2582 /** 2583 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state 2584 * @state: The state to remove 2585 * @invoke: If true, the teardown function is invoked for cpus where 2586 * cpu state >= @state 2587 * 2588 * The caller needs to hold cpus read locked while calling this function. 2589 * The teardown callback is currently not allowed to fail. Think 2590 * about module removal! 2591 */ 2592 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke) 2593 { 2594 struct cpuhp_step *sp = cpuhp_get_step(state); 2595 int cpu; 2596 2597 BUG_ON(cpuhp_cb_check(state)); 2598 2599 lockdep_assert_cpus_held(); 2600 2601 mutex_lock(&cpuhp_state_mutex); 2602 if (sp->multi_instance) { 2603 WARN(!hlist_empty(&sp->list), 2604 "Error: Removing state %d which has instances left.\n", 2605 state); 2606 goto remove; 2607 } 2608 2609 if (!invoke || !cpuhp_get_teardown_cb(state)) 2610 goto remove; 2611 2612 /* 2613 * Call the teardown callback for each present cpu depending 2614 * on the hotplug state of the cpu. This function is not 2615 * allowed to fail currently! 2616 */ 2617 for_each_present_cpu(cpu) { 2618 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 2619 int cpustate = st->state; 2620 2621 if (cpustate >= state) 2622 cpuhp_issue_call(cpu, state, false, NULL); 2623 } 2624 remove: 2625 cpuhp_store_callbacks(state, NULL, NULL, NULL, false); 2626 mutex_unlock(&cpuhp_state_mutex); 2627 } 2628 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked); 2629 2630 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke) 2631 { 2632 cpus_read_lock(); 2633 __cpuhp_remove_state_cpuslocked(state, invoke); 2634 cpus_read_unlock(); 2635 } 2636 EXPORT_SYMBOL(__cpuhp_remove_state); 2637 2638 #ifdef CONFIG_HOTPLUG_SMT 2639 static void cpuhp_offline_cpu_device(unsigned int cpu) 2640 { 2641 struct device *dev = get_cpu_device(cpu); 2642 2643 dev->offline = true; 2644 /* Tell user space about the state change */ 2645 kobject_uevent(&dev->kobj, KOBJ_OFFLINE); 2646 } 2647 2648 static void cpuhp_online_cpu_device(unsigned int cpu) 2649 { 2650 struct device *dev = get_cpu_device(cpu); 2651 2652 dev->offline = false; 2653 /* Tell user space about the state change */ 2654 kobject_uevent(&dev->kobj, KOBJ_ONLINE); 2655 } 2656 2657 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) 2658 { 2659 int cpu, ret = 0; 2660 2661 cpu_maps_update_begin(); 2662 for_each_online_cpu(cpu) { 2663 if (topology_is_primary_thread(cpu)) 2664 continue; 2665 /* 2666 * Disable can be called with CPU_SMT_ENABLED when changing 2667 * from a higher to lower number of SMT threads per core. 2668 */ 2669 if (ctrlval == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu)) 2670 continue; 2671 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE); 2672 if (ret) 2673 break; 2674 /* 2675 * As this needs to hold the cpu maps lock it's impossible 2676 * to call device_offline() because that ends up calling 2677 * cpu_down() which takes cpu maps lock. cpu maps lock 2678 * needs to be held as this might race against in kernel 2679 * abusers of the hotplug machinery (thermal management). 2680 * 2681 * So nothing would update device:offline state. That would 2682 * leave the sysfs entry stale and prevent onlining after 2683 * smt control has been changed to 'off' again. This is 2684 * called under the sysfs hotplug lock, so it is properly 2685 * serialized against the regular offline usage. 2686 */ 2687 cpuhp_offline_cpu_device(cpu); 2688 } 2689 if (!ret) 2690 cpu_smt_control = ctrlval; 2691 cpu_maps_update_done(); 2692 return ret; 2693 } 2694 2695 /* Check if the core a CPU belongs to is online */ 2696 #if !defined(topology_is_core_online) 2697 static inline bool topology_is_core_online(unsigned int cpu) 2698 { 2699 return true; 2700 } 2701 #endif 2702 2703 int cpuhp_smt_enable(void) 2704 { 2705 int cpu, ret = 0; 2706 2707 cpu_maps_update_begin(); 2708 cpu_smt_control = CPU_SMT_ENABLED; 2709 for_each_present_cpu(cpu) { 2710 /* Skip online CPUs and CPUs on offline nodes */ 2711 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu))) 2712 continue; 2713 if (!cpu_smt_thread_allowed(cpu) || !topology_is_core_online(cpu)) 2714 continue; 2715 ret = _cpu_up(cpu, 0, CPUHP_ONLINE); 2716 if (ret) 2717 break; 2718 /* See comment in cpuhp_smt_disable() */ 2719 cpuhp_online_cpu_device(cpu); 2720 } 2721 cpu_maps_update_done(); 2722 return ret; 2723 } 2724 #endif 2725 2726 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU) 2727 static ssize_t state_show(struct device *dev, 2728 struct device_attribute *attr, char *buf) 2729 { 2730 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 2731 2732 return sprintf(buf, "%d\n", st->state); 2733 } 2734 static DEVICE_ATTR_RO(state); 2735 2736 static ssize_t target_store(struct device *dev, struct device_attribute *attr, 2737 const char *buf, size_t count) 2738 { 2739 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 2740 struct cpuhp_step *sp; 2741 int target, ret; 2742 2743 ret = kstrtoint(buf, 10, &target); 2744 if (ret) 2745 return ret; 2746 2747 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL 2748 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE) 2749 return -EINVAL; 2750 #else 2751 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE) 2752 return -EINVAL; 2753 #endif 2754 2755 ret = lock_device_hotplug_sysfs(); 2756 if (ret) 2757 return ret; 2758 2759 mutex_lock(&cpuhp_state_mutex); 2760 sp = cpuhp_get_step(target); 2761 ret = !sp->name || sp->cant_stop ? -EINVAL : 0; 2762 mutex_unlock(&cpuhp_state_mutex); 2763 if (ret) 2764 goto out; 2765 2766 if (st->state < target) 2767 ret = cpu_up(dev->id, target); 2768 else if (st->state > target) 2769 ret = cpu_down(dev->id, target); 2770 else if (WARN_ON(st->target != target)) 2771 st->target = target; 2772 out: 2773 unlock_device_hotplug(); 2774 return ret ? ret : count; 2775 } 2776 2777 static ssize_t target_show(struct device *dev, 2778 struct device_attribute *attr, char *buf) 2779 { 2780 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 2781 2782 return sprintf(buf, "%d\n", st->target); 2783 } 2784 static DEVICE_ATTR_RW(target); 2785 2786 static ssize_t fail_store(struct device *dev, struct device_attribute *attr, 2787 const char *buf, size_t count) 2788 { 2789 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 2790 struct cpuhp_step *sp; 2791 int fail, ret; 2792 2793 ret = kstrtoint(buf, 10, &fail); 2794 if (ret) 2795 return ret; 2796 2797 if (fail == CPUHP_INVALID) { 2798 st->fail = fail; 2799 return count; 2800 } 2801 2802 if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE) 2803 return -EINVAL; 2804 2805 /* 2806 * Cannot fail STARTING/DYING callbacks. 2807 */ 2808 if (cpuhp_is_atomic_state(fail)) 2809 return -EINVAL; 2810 2811 /* 2812 * DEAD callbacks cannot fail... 2813 * ... neither can CPUHP_BRINGUP_CPU during hotunplug. The latter 2814 * triggering STARTING callbacks, a failure in this state would 2815 * hinder rollback. 2816 */ 2817 if (fail <= CPUHP_BRINGUP_CPU && st->state > CPUHP_BRINGUP_CPU) 2818 return -EINVAL; 2819 2820 /* 2821 * Cannot fail anything that doesn't have callbacks. 2822 */ 2823 mutex_lock(&cpuhp_state_mutex); 2824 sp = cpuhp_get_step(fail); 2825 if (!sp->startup.single && !sp->teardown.single) 2826 ret = -EINVAL; 2827 mutex_unlock(&cpuhp_state_mutex); 2828 if (ret) 2829 return ret; 2830 2831 st->fail = fail; 2832 2833 return count; 2834 } 2835 2836 static ssize_t fail_show(struct device *dev, 2837 struct device_attribute *attr, char *buf) 2838 { 2839 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 2840 2841 return sprintf(buf, "%d\n", st->fail); 2842 } 2843 2844 static DEVICE_ATTR_RW(fail); 2845 2846 static struct attribute *cpuhp_cpu_attrs[] = { 2847 &dev_attr_state.attr, 2848 &dev_attr_target.attr, 2849 &dev_attr_fail.attr, 2850 NULL 2851 }; 2852 2853 static const struct attribute_group cpuhp_cpu_attr_group = { 2854 .attrs = cpuhp_cpu_attrs, 2855 .name = "hotplug", 2856 }; 2857 2858 static ssize_t states_show(struct device *dev, 2859 struct device_attribute *attr, char *buf) 2860 { 2861 ssize_t cur, res = 0; 2862 int i; 2863 2864 mutex_lock(&cpuhp_state_mutex); 2865 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) { 2866 struct cpuhp_step *sp = cpuhp_get_step(i); 2867 2868 if (sp->name) { 2869 cur = sprintf(buf, "%3d: %s\n", i, sp->name); 2870 buf += cur; 2871 res += cur; 2872 } 2873 } 2874 mutex_unlock(&cpuhp_state_mutex); 2875 return res; 2876 } 2877 static DEVICE_ATTR_RO(states); 2878 2879 static struct attribute *cpuhp_cpu_root_attrs[] = { 2880 &dev_attr_states.attr, 2881 NULL 2882 }; 2883 2884 static const struct attribute_group cpuhp_cpu_root_attr_group = { 2885 .attrs = cpuhp_cpu_root_attrs, 2886 .name = "hotplug", 2887 }; 2888 2889 #ifdef CONFIG_HOTPLUG_SMT 2890 2891 static bool cpu_smt_num_threads_valid(unsigned int threads) 2892 { 2893 if (IS_ENABLED(CONFIG_SMT_NUM_THREADS_DYNAMIC)) 2894 return threads >= 1 && threads <= cpu_smt_max_threads; 2895 return threads == 1 || threads == cpu_smt_max_threads; 2896 } 2897 2898 static ssize_t 2899 __store_smt_control(struct device *dev, struct device_attribute *attr, 2900 const char *buf, size_t count) 2901 { 2902 int ctrlval, ret, num_threads, orig_threads; 2903 bool force_off; 2904 2905 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED) 2906 return -EPERM; 2907 2908 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED) 2909 return -ENODEV; 2910 2911 if (sysfs_streq(buf, "on")) { 2912 ctrlval = CPU_SMT_ENABLED; 2913 num_threads = cpu_smt_max_threads; 2914 } else if (sysfs_streq(buf, "off")) { 2915 ctrlval = CPU_SMT_DISABLED; 2916 num_threads = 1; 2917 } else if (sysfs_streq(buf, "forceoff")) { 2918 ctrlval = CPU_SMT_FORCE_DISABLED; 2919 num_threads = 1; 2920 } else if (kstrtoint(buf, 10, &num_threads) == 0) { 2921 if (num_threads == 1) 2922 ctrlval = CPU_SMT_DISABLED; 2923 else if (cpu_smt_num_threads_valid(num_threads)) 2924 ctrlval = CPU_SMT_ENABLED; 2925 else 2926 return -EINVAL; 2927 } else { 2928 return -EINVAL; 2929 } 2930 2931 ret = lock_device_hotplug_sysfs(); 2932 if (ret) 2933 return ret; 2934 2935 orig_threads = cpu_smt_num_threads; 2936 cpu_smt_num_threads = num_threads; 2937 2938 force_off = ctrlval != cpu_smt_control && ctrlval == CPU_SMT_FORCE_DISABLED; 2939 2940 if (num_threads > orig_threads) 2941 ret = cpuhp_smt_enable(); 2942 else if (num_threads < orig_threads || force_off) 2943 ret = cpuhp_smt_disable(ctrlval); 2944 2945 unlock_device_hotplug(); 2946 return ret ? ret : count; 2947 } 2948 2949 #else /* !CONFIG_HOTPLUG_SMT */ 2950 static ssize_t 2951 __store_smt_control(struct device *dev, struct device_attribute *attr, 2952 const char *buf, size_t count) 2953 { 2954 return -ENODEV; 2955 } 2956 #endif /* CONFIG_HOTPLUG_SMT */ 2957 2958 static const char *smt_states[] = { 2959 [CPU_SMT_ENABLED] = "on", 2960 [CPU_SMT_DISABLED] = "off", 2961 [CPU_SMT_FORCE_DISABLED] = "forceoff", 2962 [CPU_SMT_NOT_SUPPORTED] = "notsupported", 2963 [CPU_SMT_NOT_IMPLEMENTED] = "notimplemented", 2964 }; 2965 2966 static ssize_t control_show(struct device *dev, 2967 struct device_attribute *attr, char *buf) 2968 { 2969 const char *state = smt_states[cpu_smt_control]; 2970 2971 #ifdef CONFIG_HOTPLUG_SMT 2972 /* 2973 * If SMT is enabled but not all threads are enabled then show the 2974 * number of threads. If all threads are enabled show "on". Otherwise 2975 * show the state name. 2976 */ 2977 if (cpu_smt_control == CPU_SMT_ENABLED && 2978 cpu_smt_num_threads != cpu_smt_max_threads) 2979 return sysfs_emit(buf, "%d\n", cpu_smt_num_threads); 2980 #endif 2981 2982 return sysfs_emit(buf, "%s\n", state); 2983 } 2984 2985 static ssize_t control_store(struct device *dev, struct device_attribute *attr, 2986 const char *buf, size_t count) 2987 { 2988 return __store_smt_control(dev, attr, buf, count); 2989 } 2990 static DEVICE_ATTR_RW(control); 2991 2992 static ssize_t active_show(struct device *dev, 2993 struct device_attribute *attr, char *buf) 2994 { 2995 return sysfs_emit(buf, "%d\n", sched_smt_active()); 2996 } 2997 static DEVICE_ATTR_RO(active); 2998 2999 static struct attribute *cpuhp_smt_attrs[] = { 3000 &dev_attr_control.attr, 3001 &dev_attr_active.attr, 3002 NULL 3003 }; 3004 3005 static const struct attribute_group cpuhp_smt_attr_group = { 3006 .attrs = cpuhp_smt_attrs, 3007 .name = "smt", 3008 }; 3009 3010 static int __init cpu_smt_sysfs_init(void) 3011 { 3012 struct device *dev_root; 3013 int ret = -ENODEV; 3014 3015 dev_root = bus_get_dev_root(&cpu_subsys); 3016 if (dev_root) { 3017 ret = sysfs_create_group(&dev_root->kobj, &cpuhp_smt_attr_group); 3018 put_device(dev_root); 3019 } 3020 return ret; 3021 } 3022 3023 static int __init cpuhp_sysfs_init(void) 3024 { 3025 struct device *dev_root; 3026 int cpu, ret; 3027 3028 ret = cpu_smt_sysfs_init(); 3029 if (ret) 3030 return ret; 3031 3032 dev_root = bus_get_dev_root(&cpu_subsys); 3033 if (dev_root) { 3034 ret = sysfs_create_group(&dev_root->kobj, &cpuhp_cpu_root_attr_group); 3035 put_device(dev_root); 3036 if (ret) 3037 return ret; 3038 } 3039 3040 for_each_possible_cpu(cpu) { 3041 struct device *dev = get_cpu_device(cpu); 3042 3043 if (!dev) 3044 continue; 3045 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group); 3046 if (ret) 3047 return ret; 3048 } 3049 return 0; 3050 } 3051 device_initcall(cpuhp_sysfs_init); 3052 #endif /* CONFIG_SYSFS && CONFIG_HOTPLUG_CPU */ 3053 3054 /* 3055 * cpu_bit_bitmap[] is a special, "compressed" data structure that 3056 * represents all NR_CPUS bits binary values of 1<<nr. 3057 * 3058 * It is used by cpumask_of() to get a constant address to a CPU 3059 * mask value that has a single bit set only. 3060 */ 3061 3062 /* cpu_bit_bitmap[0] is empty - so we can back into it */ 3063 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x)) 3064 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1) 3065 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2) 3066 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4) 3067 3068 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = { 3069 3070 MASK_DECLARE_8(0), MASK_DECLARE_8(8), 3071 MASK_DECLARE_8(16), MASK_DECLARE_8(24), 3072 #if BITS_PER_LONG > 32 3073 MASK_DECLARE_8(32), MASK_DECLARE_8(40), 3074 MASK_DECLARE_8(48), MASK_DECLARE_8(56), 3075 #endif 3076 }; 3077 EXPORT_SYMBOL_GPL(cpu_bit_bitmap); 3078 3079 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL; 3080 EXPORT_SYMBOL(cpu_all_bits); 3081 3082 #ifdef CONFIG_INIT_ALL_POSSIBLE 3083 struct cpumask __cpu_possible_mask __ro_after_init 3084 = {CPU_BITS_ALL}; 3085 unsigned int __num_possible_cpus __ro_after_init = NR_CPUS; 3086 #else 3087 struct cpumask __cpu_possible_mask __ro_after_init; 3088 unsigned int __num_possible_cpus __ro_after_init; 3089 #endif 3090 EXPORT_SYMBOL(__cpu_possible_mask); 3091 EXPORT_SYMBOL(__num_possible_cpus); 3092 3093 struct cpumask __cpu_online_mask __read_mostly; 3094 EXPORT_SYMBOL(__cpu_online_mask); 3095 3096 struct cpumask __cpu_enabled_mask __read_mostly; 3097 EXPORT_SYMBOL(__cpu_enabled_mask); 3098 3099 struct cpumask __cpu_present_mask __read_mostly; 3100 EXPORT_SYMBOL(__cpu_present_mask); 3101 3102 struct cpumask __cpu_active_mask __read_mostly; 3103 EXPORT_SYMBOL(__cpu_active_mask); 3104 3105 struct cpumask __cpu_dying_mask __read_mostly; 3106 EXPORT_SYMBOL(__cpu_dying_mask); 3107 3108 atomic_t __num_online_cpus __read_mostly; 3109 EXPORT_SYMBOL(__num_online_cpus); 3110 3111 void init_cpu_present(const struct cpumask *src) 3112 { 3113 cpumask_copy(&__cpu_present_mask, src); 3114 } 3115 3116 void init_cpu_possible(const struct cpumask *src) 3117 { 3118 cpumask_copy(&__cpu_possible_mask, src); 3119 __num_possible_cpus = cpumask_weight(&__cpu_possible_mask); 3120 } 3121 3122 void set_cpu_online(unsigned int cpu, bool online) 3123 { 3124 /* 3125 * atomic_inc/dec() is required to handle the horrid abuse of this 3126 * function by the reboot and kexec code which invoke it from 3127 * IPI/NMI broadcasts when shutting down CPUs. Invocation from 3128 * regular CPU hotplug is properly serialized. 3129 * 3130 * Note, that the fact that __num_online_cpus is of type atomic_t 3131 * does not protect readers which are not serialized against 3132 * concurrent hotplug operations. 3133 */ 3134 if (online) { 3135 if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask)) 3136 atomic_inc(&__num_online_cpus); 3137 } else { 3138 if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask)) 3139 atomic_dec(&__num_online_cpus); 3140 } 3141 } 3142 3143 /* 3144 * This should be marked __init, but there is a boatload of call sites 3145 * which need to be fixed up to do so. Sigh... 3146 */ 3147 void set_cpu_possible(unsigned int cpu, bool possible) 3148 { 3149 if (possible) { 3150 if (!cpumask_test_and_set_cpu(cpu, &__cpu_possible_mask)) 3151 __num_possible_cpus++; 3152 } else { 3153 if (cpumask_test_and_clear_cpu(cpu, &__cpu_possible_mask)) 3154 __num_possible_cpus--; 3155 } 3156 } 3157 3158 /* 3159 * Activate the first processor. 3160 */ 3161 void __init boot_cpu_init(void) 3162 { 3163 int cpu = smp_processor_id(); 3164 3165 /* Mark the boot cpu "present", "online" etc for SMP and UP case */ 3166 set_cpu_online(cpu, true); 3167 set_cpu_active(cpu, true); 3168 set_cpu_present(cpu, true); 3169 set_cpu_possible(cpu, true); 3170 3171 #ifdef CONFIG_SMP 3172 __boot_cpu_id = cpu; 3173 #endif 3174 } 3175 3176 /* 3177 * Must be called _AFTER_ setting up the per_cpu areas 3178 */ 3179 void __init boot_cpu_hotplug_init(void) 3180 { 3181 #ifdef CONFIG_SMP 3182 cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask); 3183 atomic_set(this_cpu_ptr(&cpuhp_state.ap_sync_state), SYNC_STATE_ONLINE); 3184 #endif 3185 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE); 3186 this_cpu_write(cpuhp_state.target, CPUHP_ONLINE); 3187 } 3188 3189 #ifdef CONFIG_CPU_MITIGATIONS 3190 /* 3191 * All except the cross-thread attack vector are mitigated by default. 3192 * Cross-thread mitigation often requires disabling SMT which is expensive 3193 * so cross-thread mitigations are only partially enabled by default. 3194 * 3195 * Guest-to-Host and Guest-to-Guest vectors are only needed if KVM support is 3196 * present. 3197 */ 3198 static bool attack_vectors[NR_CPU_ATTACK_VECTORS] __ro_after_init = { 3199 [CPU_MITIGATE_USER_KERNEL] = true, 3200 [CPU_MITIGATE_USER_USER] = true, 3201 [CPU_MITIGATE_GUEST_HOST] = IS_ENABLED(CONFIG_KVM), 3202 [CPU_MITIGATE_GUEST_GUEST] = IS_ENABLED(CONFIG_KVM), 3203 }; 3204 3205 bool cpu_attack_vector_mitigated(enum cpu_attack_vectors v) 3206 { 3207 if (v < NR_CPU_ATTACK_VECTORS) 3208 return attack_vectors[v]; 3209 3210 WARN_ONCE(1, "Invalid attack vector %d\n", v); 3211 return false; 3212 } 3213 3214 /* 3215 * There are 3 global options, 'off', 'auto', 'auto,nosmt'. These may optionally 3216 * be combined with attack-vector disables which follow them. 3217 * 3218 * Examples: 3219 * mitigations=auto,no_user_kernel,no_user_user,no_cross_thread 3220 * mitigations=auto,nosmt,no_guest_host,no_guest_guest 3221 * 3222 * mitigations=off is equivalent to disabling all attack vectors. 3223 */ 3224 enum cpu_mitigations { 3225 CPU_MITIGATIONS_OFF, 3226 CPU_MITIGATIONS_AUTO, 3227 CPU_MITIGATIONS_AUTO_NOSMT, 3228 }; 3229 3230 enum { 3231 NO_USER_KERNEL, 3232 NO_USER_USER, 3233 NO_GUEST_HOST, 3234 NO_GUEST_GUEST, 3235 NO_CROSS_THREAD, 3236 NR_VECTOR_PARAMS, 3237 }; 3238 3239 enum smt_mitigations smt_mitigations __ro_after_init = SMT_MITIGATIONS_AUTO; 3240 static enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO; 3241 3242 static const match_table_t global_mitigations = { 3243 { CPU_MITIGATIONS_AUTO_NOSMT, "auto,nosmt"}, 3244 { CPU_MITIGATIONS_AUTO, "auto"}, 3245 { CPU_MITIGATIONS_OFF, "off"}, 3246 }; 3247 3248 static const match_table_t vector_mitigations = { 3249 { NO_USER_KERNEL, "no_user_kernel"}, 3250 { NO_USER_USER, "no_user_user"}, 3251 { NO_GUEST_HOST, "no_guest_host"}, 3252 { NO_GUEST_GUEST, "no_guest_guest"}, 3253 { NO_CROSS_THREAD, "no_cross_thread"}, 3254 { NR_VECTOR_PARAMS, NULL}, 3255 }; 3256 3257 static int __init mitigations_parse_global_opt(char *arg) 3258 { 3259 int i; 3260 3261 for (i = 0; i < ARRAY_SIZE(global_mitigations); i++) { 3262 const char *pattern = global_mitigations[i].pattern; 3263 3264 if (!strncmp(arg, pattern, strlen(pattern))) { 3265 cpu_mitigations = global_mitigations[i].token; 3266 return strlen(pattern); 3267 } 3268 } 3269 3270 return 0; 3271 } 3272 3273 static int __init mitigations_parse_cmdline(char *arg) 3274 { 3275 char *s, *p; 3276 int len; 3277 3278 len = mitigations_parse_global_opt(arg); 3279 3280 if (cpu_mitigations_off()) { 3281 memset(attack_vectors, 0, sizeof(attack_vectors)); 3282 smt_mitigations = SMT_MITIGATIONS_OFF; 3283 } else if (cpu_mitigations_auto_nosmt()) { 3284 smt_mitigations = SMT_MITIGATIONS_ON; 3285 } 3286 3287 p = arg + len; 3288 3289 if (!*p) 3290 return 0; 3291 3292 /* Attack vector controls may come after the ',' */ 3293 if (*p++ != ',' || !IS_ENABLED(CONFIG_ARCH_HAS_CPU_ATTACK_VECTORS)) { 3294 pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n", arg); 3295 return 0; 3296 } 3297 3298 while ((s = strsep(&p, ",")) != NULL) { 3299 switch (match_token(s, vector_mitigations, NULL)) { 3300 case NO_USER_KERNEL: 3301 attack_vectors[CPU_MITIGATE_USER_KERNEL] = false; 3302 break; 3303 case NO_USER_USER: 3304 attack_vectors[CPU_MITIGATE_USER_USER] = false; 3305 break; 3306 case NO_GUEST_HOST: 3307 attack_vectors[CPU_MITIGATE_GUEST_HOST] = false; 3308 break; 3309 case NO_GUEST_GUEST: 3310 attack_vectors[CPU_MITIGATE_GUEST_GUEST] = false; 3311 break; 3312 case NO_CROSS_THREAD: 3313 smt_mitigations = SMT_MITIGATIONS_OFF; 3314 break; 3315 default: 3316 pr_crit("Unsupported mitigations options %s\n", s); 3317 return 0; 3318 } 3319 } 3320 3321 return 0; 3322 } 3323 3324 /* mitigations=off */ 3325 bool cpu_mitigations_off(void) 3326 { 3327 return cpu_mitigations == CPU_MITIGATIONS_OFF; 3328 } 3329 EXPORT_SYMBOL_GPL(cpu_mitigations_off); 3330 3331 /* mitigations=auto,nosmt */ 3332 bool cpu_mitigations_auto_nosmt(void) 3333 { 3334 return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT; 3335 } 3336 EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt); 3337 #else 3338 static int __init mitigations_parse_cmdline(char *arg) 3339 { 3340 pr_crit("Kernel compiled without mitigations, ignoring 'mitigations'; system may still be vulnerable\n"); 3341 return 0; 3342 } 3343 #endif 3344 early_param("mitigations", mitigations_parse_cmdline); 3345