0001
0002
0003
0004
0005
0006 #include <linux/sched/mm.h>
0007 #include <linux/proc_fs.h>
0008 #include <linux/smp.h>
0009 #include <linux/init.h>
0010 #include <linux/notifier.h>
0011 #include <linux/sched/signal.h>
0012 #include <linux/sched/hotplug.h>
0013 #include <linux/sched/isolation.h>
0014 #include <linux/sched/task.h>
0015 #include <linux/sched/smt.h>
0016 #include <linux/unistd.h>
0017 #include <linux/cpu.h>
0018 #include <linux/oom.h>
0019 #include <linux/rcupdate.h>
0020 #include <linux/export.h>
0021 #include <linux/bug.h>
0022 #include <linux/kthread.h>
0023 #include <linux/stop_machine.h>
0024 #include <linux/mutex.h>
0025 #include <linux/gfp.h>
0026 #include <linux/suspend.h>
0027 #include <linux/lockdep.h>
0028 #include <linux/tick.h>
0029 #include <linux/irq.h>
0030 #include <linux/nmi.h>
0031 #include <linux/smpboot.h>
0032 #include <linux/relay.h>
0033 #include <linux/slab.h>
0034 #include <linux/scs.h>
0035 #include <linux/percpu-rwsem.h>
0036 #include <linux/cpuset.h>
0037 #include <linux/random.h>
0038 #include <linux/cc_platform.h>
0039
0040 #include <trace/events/power.h>
0041 #define CREATE_TRACE_POINTS
0042 #include <trace/events/cpuhp.h>
0043
0044 #include "smpboot.h"
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 struct cpuhp_cpu_state {
0066 enum cpuhp_state state;
0067 enum cpuhp_state target;
0068 enum cpuhp_state fail;
0069 #ifdef CONFIG_SMP
0070 struct task_struct *thread;
0071 bool should_run;
0072 bool rollback;
0073 bool single;
0074 bool bringup;
0075 struct hlist_node *node;
0076 struct hlist_node *last;
0077 enum cpuhp_state cb_state;
0078 int result;
0079 struct completion done_up;
0080 struct completion done_down;
0081 #endif
0082 };
0083
0084 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
0085 .fail = CPUHP_INVALID,
0086 };
0087
0088 #ifdef CONFIG_SMP
0089 cpumask_t cpus_booted_once_mask;
0090 #endif
0091
0092 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
0093 static struct lockdep_map cpuhp_state_up_map =
0094 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
0095 static struct lockdep_map cpuhp_state_down_map =
0096 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
0097
0098
0099 static inline void cpuhp_lock_acquire(bool bringup)
0100 {
0101 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
0102 }
0103
0104 static inline void cpuhp_lock_release(bool bringup)
0105 {
0106 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
0107 }
0108 #else
0109
0110 static inline void cpuhp_lock_acquire(bool bringup) { }
0111 static inline void cpuhp_lock_release(bool bringup) { }
0112
0113 #endif
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 struct cpuhp_step {
0124 const char *name;
0125 union {
0126 int (*single)(unsigned int cpu);
0127 int (*multi)(unsigned int cpu,
0128 struct hlist_node *node);
0129 } startup;
0130 union {
0131 int (*single)(unsigned int cpu);
0132 int (*multi)(unsigned int cpu,
0133 struct hlist_node *node);
0134 } teardown;
0135
0136 struct hlist_head list;
0137
0138 bool cant_stop;
0139 bool multi_instance;
0140 };
0141
0142 static DEFINE_MUTEX(cpuhp_state_mutex);
0143 static struct cpuhp_step cpuhp_hp_states[];
0144
0145 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
0146 {
0147 return cpuhp_hp_states + state;
0148 }
0149
0150 static bool cpuhp_step_empty(bool bringup, struct cpuhp_step *step)
0151 {
0152 return bringup ? !step->startup.single : !step->teardown.single;
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
0168 bool bringup, struct hlist_node *node,
0169 struct hlist_node **lastp)
0170 {
0171 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
0172 struct cpuhp_step *step = cpuhp_get_step(state);
0173 int (*cbm)(unsigned int cpu, struct hlist_node *node);
0174 int (*cb)(unsigned int cpu);
0175 int ret, cnt;
0176
0177 if (st->fail == state) {
0178 st->fail = CPUHP_INVALID;
0179 return -EAGAIN;
0180 }
0181
0182 if (cpuhp_step_empty(bringup, step)) {
0183 WARN_ON_ONCE(1);
0184 return 0;
0185 }
0186
0187 if (!step->multi_instance) {
0188 WARN_ON_ONCE(lastp && *lastp);
0189 cb = bringup ? step->startup.single : step->teardown.single;
0190
0191 trace_cpuhp_enter(cpu, st->target, state, cb);
0192 ret = cb(cpu);
0193 trace_cpuhp_exit(cpu, st->state, state, ret);
0194 return ret;
0195 }
0196 cbm = bringup ? step->startup.multi : step->teardown.multi;
0197
0198
0199 if (node) {
0200 WARN_ON_ONCE(lastp && *lastp);
0201 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
0202 ret = cbm(cpu, node);
0203 trace_cpuhp_exit(cpu, st->state, state, ret);
0204 return ret;
0205 }
0206
0207
0208 cnt = 0;
0209 hlist_for_each(node, &step->list) {
0210 if (lastp && node == *lastp)
0211 break;
0212
0213 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
0214 ret = cbm(cpu, node);
0215 trace_cpuhp_exit(cpu, st->state, state, ret);
0216 if (ret) {
0217 if (!lastp)
0218 goto err;
0219
0220 *lastp = node;
0221 return ret;
0222 }
0223 cnt++;
0224 }
0225 if (lastp)
0226 *lastp = NULL;
0227 return 0;
0228 err:
0229
0230 cbm = !bringup ? step->startup.multi : step->teardown.multi;
0231 if (!cbm)
0232 return ret;
0233
0234 hlist_for_each(node, &step->list) {
0235 if (!cnt--)
0236 break;
0237
0238 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
0239 ret = cbm(cpu, node);
0240 trace_cpuhp_exit(cpu, st->state, state, ret);
0241
0242
0243
0244 WARN_ON_ONCE(ret);
0245 }
0246 return ret;
0247 }
0248
0249 #ifdef CONFIG_SMP
0250 static bool cpuhp_is_ap_state(enum cpuhp_state state)
0251 {
0252
0253
0254
0255
0256 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
0257 }
0258
0259 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
0260 {
0261 struct completion *done = bringup ? &st->done_up : &st->done_down;
0262 wait_for_completion(done);
0263 }
0264
0265 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
0266 {
0267 struct completion *done = bringup ? &st->done_up : &st->done_down;
0268 complete(done);
0269 }
0270
0271
0272
0273
0274 static bool cpuhp_is_atomic_state(enum cpuhp_state state)
0275 {
0276 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
0277 }
0278
0279
0280 static DEFINE_MUTEX(cpu_add_remove_lock);
0281 bool cpuhp_tasks_frozen;
0282 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
0283
0284
0285
0286
0287
0288 void cpu_maps_update_begin(void)
0289 {
0290 mutex_lock(&cpu_add_remove_lock);
0291 }
0292
0293 void cpu_maps_update_done(void)
0294 {
0295 mutex_unlock(&cpu_add_remove_lock);
0296 }
0297
0298
0299
0300
0301
0302 static int cpu_hotplug_disabled;
0303
0304 #ifdef CONFIG_HOTPLUG_CPU
0305
0306 DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
0307
0308 void cpus_read_lock(void)
0309 {
0310 percpu_down_read(&cpu_hotplug_lock);
0311 }
0312 EXPORT_SYMBOL_GPL(cpus_read_lock);
0313
0314 int cpus_read_trylock(void)
0315 {
0316 return percpu_down_read_trylock(&cpu_hotplug_lock);
0317 }
0318 EXPORT_SYMBOL_GPL(cpus_read_trylock);
0319
0320 void cpus_read_unlock(void)
0321 {
0322 percpu_up_read(&cpu_hotplug_lock);
0323 }
0324 EXPORT_SYMBOL_GPL(cpus_read_unlock);
0325
0326 void cpus_write_lock(void)
0327 {
0328 percpu_down_write(&cpu_hotplug_lock);
0329 }
0330
0331 void cpus_write_unlock(void)
0332 {
0333 percpu_up_write(&cpu_hotplug_lock);
0334 }
0335
0336 void lockdep_assert_cpus_held(void)
0337 {
0338
0339
0340
0341
0342
0343
0344 if (system_state < SYSTEM_RUNNING)
0345 return;
0346
0347 percpu_rwsem_assert_held(&cpu_hotplug_lock);
0348 }
0349
0350 #ifdef CONFIG_LOCKDEP
0351 int lockdep_is_cpus_held(void)
0352 {
0353 return percpu_rwsem_is_held(&cpu_hotplug_lock);
0354 }
0355 #endif
0356
0357 static void lockdep_acquire_cpus_lock(void)
0358 {
0359 rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);
0360 }
0361
0362 static void lockdep_release_cpus_lock(void)
0363 {
0364 rwsem_release(&cpu_hotplug_lock.dep_map, _THIS_IP_);
0365 }
0366
0367
0368
0369
0370
0371
0372
0373
0374 void cpu_hotplug_disable(void)
0375 {
0376 cpu_maps_update_begin();
0377 cpu_hotplug_disabled++;
0378 cpu_maps_update_done();
0379 }
0380 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
0381
0382 static void __cpu_hotplug_enable(void)
0383 {
0384 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
0385 return;
0386 cpu_hotplug_disabled--;
0387 }
0388
0389 void cpu_hotplug_enable(void)
0390 {
0391 cpu_maps_update_begin();
0392 __cpu_hotplug_enable();
0393 cpu_maps_update_done();
0394 }
0395 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
0396
0397 #else
0398
0399 static void lockdep_acquire_cpus_lock(void)
0400 {
0401 }
0402
0403 static void lockdep_release_cpus_lock(void)
0404 {
0405 }
0406
0407 #endif
0408
0409
0410
0411
0412
0413 void __weak arch_smt_update(void) { }
0414
0415 #ifdef CONFIG_HOTPLUG_SMT
0416 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
0417
0418 void __init cpu_smt_disable(bool force)
0419 {
0420 if (!cpu_smt_possible())
0421 return;
0422
0423 if (force) {
0424 pr_info("SMT: Force disabled\n");
0425 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
0426 } else {
0427 pr_info("SMT: disabled\n");
0428 cpu_smt_control = CPU_SMT_DISABLED;
0429 }
0430 }
0431
0432
0433
0434
0435
0436 void __init cpu_smt_check_topology(void)
0437 {
0438 if (!topology_smt_supported())
0439 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
0440 }
0441
0442 static int __init smt_cmdline_disable(char *str)
0443 {
0444 cpu_smt_disable(str && !strcmp(str, "force"));
0445 return 0;
0446 }
0447 early_param("nosmt", smt_cmdline_disable);
0448
0449 static inline bool cpu_smt_allowed(unsigned int cpu)
0450 {
0451 if (cpu_smt_control == CPU_SMT_ENABLED)
0452 return true;
0453
0454 if (topology_is_primary_thread(cpu))
0455 return true;
0456
0457
0458
0459
0460
0461
0462
0463 return !cpumask_test_cpu(cpu, &cpus_booted_once_mask);
0464 }
0465
0466
0467 bool cpu_smt_possible(void)
0468 {
0469 return cpu_smt_control != CPU_SMT_FORCE_DISABLED &&
0470 cpu_smt_control != CPU_SMT_NOT_SUPPORTED;
0471 }
0472 EXPORT_SYMBOL_GPL(cpu_smt_possible);
0473 #else
0474 static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
0475 #endif
0476
0477 static inline enum cpuhp_state
0478 cpuhp_set_state(int cpu, struct cpuhp_cpu_state *st, enum cpuhp_state target)
0479 {
0480 enum cpuhp_state prev_state = st->state;
0481 bool bringup = st->state < target;
0482
0483 st->rollback = false;
0484 st->last = NULL;
0485
0486 st->target = target;
0487 st->single = false;
0488 st->bringup = bringup;
0489 if (cpu_dying(cpu) != !bringup)
0490 set_cpu_dying(cpu, !bringup);
0491
0492 return prev_state;
0493 }
0494
0495 static inline void
0496 cpuhp_reset_state(int cpu, struct cpuhp_cpu_state *st,
0497 enum cpuhp_state prev_state)
0498 {
0499 bool bringup = !st->bringup;
0500
0501 st->target = prev_state;
0502
0503
0504
0505
0506
0507 if (st->rollback)
0508 return;
0509
0510 st->rollback = true;
0511
0512
0513
0514
0515
0516 if (!st->last) {
0517 if (st->bringup)
0518 st->state--;
0519 else
0520 st->state++;
0521 }
0522
0523 st->bringup = bringup;
0524 if (cpu_dying(cpu) != !bringup)
0525 set_cpu_dying(cpu, !bringup);
0526 }
0527
0528
0529 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
0530 {
0531 if (!st->single && st->state == st->target)
0532 return;
0533
0534 st->result = 0;
0535
0536
0537
0538
0539 smp_mb();
0540 st->should_run = true;
0541 wake_up_process(st->thread);
0542 wait_for_ap_thread(st, st->bringup);
0543 }
0544
0545 static int cpuhp_kick_ap(int cpu, struct cpuhp_cpu_state *st,
0546 enum cpuhp_state target)
0547 {
0548 enum cpuhp_state prev_state;
0549 int ret;
0550
0551 prev_state = cpuhp_set_state(cpu, st, target);
0552 __cpuhp_kick_ap(st);
0553 if ((ret = st->result)) {
0554 cpuhp_reset_state(cpu, st, prev_state);
0555 __cpuhp_kick_ap(st);
0556 }
0557
0558 return ret;
0559 }
0560
0561 static int bringup_wait_for_ap(unsigned int cpu)
0562 {
0563 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
0564
0565
0566 wait_for_ap_thread(st, true);
0567 if (WARN_ON_ONCE((!cpu_online(cpu))))
0568 return -ECANCELED;
0569
0570
0571 kthread_unpark(st->thread);
0572
0573
0574
0575
0576
0577
0578
0579
0580 if (!cpu_smt_allowed(cpu))
0581 return -ECANCELED;
0582
0583 if (st->target <= CPUHP_AP_ONLINE_IDLE)
0584 return 0;
0585
0586 return cpuhp_kick_ap(cpu, st, st->target);
0587 }
0588
0589 static int bringup_cpu(unsigned int cpu)
0590 {
0591 struct task_struct *idle = idle_thread_get(cpu);
0592 int ret;
0593
0594
0595
0596
0597 scs_task_reset(idle);
0598 kasan_unpoison_task_stack(idle);
0599
0600
0601
0602
0603
0604
0605 irq_lock_sparse();
0606
0607
0608 ret = __cpu_up(cpu, idle);
0609 irq_unlock_sparse();
0610 if (ret)
0611 return ret;
0612 return bringup_wait_for_ap(cpu);
0613 }
0614
0615 static int finish_cpu(unsigned int cpu)
0616 {
0617 struct task_struct *idle = idle_thread_get(cpu);
0618 struct mm_struct *mm = idle->active_mm;
0619
0620
0621
0622
0623
0624 if (mm != &init_mm)
0625 idle->active_mm = &init_mm;
0626 mmdrop(mm);
0627 return 0;
0628 }
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641 static bool cpuhp_next_state(bool bringup,
0642 enum cpuhp_state *state_to_run,
0643 struct cpuhp_cpu_state *st,
0644 enum cpuhp_state target)
0645 {
0646 do {
0647 if (bringup) {
0648 if (st->state >= target)
0649 return false;
0650
0651 *state_to_run = ++st->state;
0652 } else {
0653 if (st->state <= target)
0654 return false;
0655
0656 *state_to_run = st->state--;
0657 }
0658
0659 if (!cpuhp_step_empty(bringup, cpuhp_get_step(*state_to_run)))
0660 break;
0661 } while (true);
0662
0663 return true;
0664 }
0665
0666 static int cpuhp_invoke_callback_range(bool bringup,
0667 unsigned int cpu,
0668 struct cpuhp_cpu_state *st,
0669 enum cpuhp_state target)
0670 {
0671 enum cpuhp_state state;
0672 int err = 0;
0673
0674 while (cpuhp_next_state(bringup, &state, st, target)) {
0675 err = cpuhp_invoke_callback(cpu, state, bringup, NULL, NULL);
0676 if (err)
0677 break;
0678 }
0679
0680 return err;
0681 }
0682
0683 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
0684 {
0685 if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
0686 return true;
0687
0688
0689
0690
0691
0692
0693
0694 return st->state <= CPUHP_BRINGUP_CPU;
0695 }
0696
0697 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
0698 enum cpuhp_state target)
0699 {
0700 enum cpuhp_state prev_state = st->state;
0701 int ret = 0;
0702
0703 ret = cpuhp_invoke_callback_range(true, cpu, st, target);
0704 if (ret) {
0705 pr_debug("CPU UP failed (%d) CPU %u state %s (%d)\n",
0706 ret, cpu, cpuhp_get_step(st->state)->name,
0707 st->state);
0708
0709 cpuhp_reset_state(cpu, st, prev_state);
0710 if (can_rollback_cpu(st))
0711 WARN_ON(cpuhp_invoke_callback_range(false, cpu, st,
0712 prev_state));
0713 }
0714 return ret;
0715 }
0716
0717
0718
0719
0720 static int cpuhp_should_run(unsigned int cpu)
0721 {
0722 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
0723
0724 return st->should_run;
0725 }
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741 static void cpuhp_thread_fun(unsigned int cpu)
0742 {
0743 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
0744 bool bringup = st->bringup;
0745 enum cpuhp_state state;
0746
0747 if (WARN_ON_ONCE(!st->should_run))
0748 return;
0749
0750
0751
0752
0753
0754 smp_mb();
0755
0756
0757
0758
0759
0760
0761 lockdep_acquire_cpus_lock();
0762 cpuhp_lock_acquire(bringup);
0763
0764 if (st->single) {
0765 state = st->cb_state;
0766 st->should_run = false;
0767 } else {
0768 st->should_run = cpuhp_next_state(bringup, &state, st, st->target);
0769 if (!st->should_run)
0770 goto end;
0771 }
0772
0773 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
0774
0775 if (cpuhp_is_atomic_state(state)) {
0776 local_irq_disable();
0777 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
0778 local_irq_enable();
0779
0780
0781
0782
0783 WARN_ON_ONCE(st->result);
0784 } else {
0785 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
0786 }
0787
0788 if (st->result) {
0789
0790
0791
0792
0793
0794 WARN_ON_ONCE(st->rollback);
0795 st->should_run = false;
0796 }
0797
0798 end:
0799 cpuhp_lock_release(bringup);
0800 lockdep_release_cpus_lock();
0801
0802 if (!st->should_run)
0803 complete_ap_thread(st, bringup);
0804 }
0805
0806
0807 static int
0808 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
0809 struct hlist_node *node)
0810 {
0811 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
0812 int ret;
0813
0814 if (!cpu_online(cpu))
0815 return 0;
0816
0817 cpuhp_lock_acquire(false);
0818 cpuhp_lock_release(false);
0819
0820 cpuhp_lock_acquire(true);
0821 cpuhp_lock_release(true);
0822
0823
0824
0825
0826
0827 if (!st->thread)
0828 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
0829
0830 st->rollback = false;
0831 st->last = NULL;
0832
0833 st->node = node;
0834 st->bringup = bringup;
0835 st->cb_state = state;
0836 st->single = true;
0837
0838 __cpuhp_kick_ap(st);
0839
0840
0841
0842
0843 if ((ret = st->result) && st->last) {
0844 st->rollback = true;
0845 st->bringup = !bringup;
0846
0847 __cpuhp_kick_ap(st);
0848 }
0849
0850
0851
0852
0853
0854 st->node = st->last = NULL;
0855 return ret;
0856 }
0857
0858 static int cpuhp_kick_ap_work(unsigned int cpu)
0859 {
0860 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
0861 enum cpuhp_state prev_state = st->state;
0862 int ret;
0863
0864 cpuhp_lock_acquire(false);
0865 cpuhp_lock_release(false);
0866
0867 cpuhp_lock_acquire(true);
0868 cpuhp_lock_release(true);
0869
0870 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
0871 ret = cpuhp_kick_ap(cpu, st, st->target);
0872 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
0873
0874 return ret;
0875 }
0876
0877 static struct smp_hotplug_thread cpuhp_threads = {
0878 .store = &cpuhp_state.thread,
0879 .thread_should_run = cpuhp_should_run,
0880 .thread_fn = cpuhp_thread_fun,
0881 .thread_comm = "cpuhp/%u",
0882 .selfparking = true,
0883 };
0884
0885 static __init void cpuhp_init_state(void)
0886 {
0887 struct cpuhp_cpu_state *st;
0888 int cpu;
0889
0890 for_each_possible_cpu(cpu) {
0891 st = per_cpu_ptr(&cpuhp_state, cpu);
0892 init_completion(&st->done_up);
0893 init_completion(&st->done_down);
0894 }
0895 }
0896
0897 void __init cpuhp_threads_init(void)
0898 {
0899 cpuhp_init_state();
0900 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
0901 kthread_unpark(this_cpu_read(cpuhp_state.thread));
0902 }
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927 static void cpu_up_down_serialize_trainwrecks(bool tasks_frozen)
0928 {
0929
0930
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946 if (!tasks_frozen)
0947 cpuset_wait_for_hotplug();
0948 }
0949
0950 #ifdef CONFIG_HOTPLUG_CPU
0951 #ifndef arch_clear_mm_cpumask_cpu
0952 #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
0953 #endif
0954
0955
0956
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967 void clear_tasks_mm_cpumask(int cpu)
0968 {
0969 struct task_struct *p;
0970
0971
0972
0973
0974
0975
0976
0977
0978 WARN_ON(cpu_online(cpu));
0979 rcu_read_lock();
0980 for_each_process(p) {
0981 struct task_struct *t;
0982
0983
0984
0985
0986
0987 t = find_lock_task_mm(p);
0988 if (!t)
0989 continue;
0990 arch_clear_mm_cpumask_cpu(cpu, t->mm);
0991 task_unlock(t);
0992 }
0993 rcu_read_unlock();
0994 }
0995
0996
0997 static int take_cpu_down(void *_param)
0998 {
0999 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1000 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
1001 int err, cpu = smp_processor_id();
1002 int ret;
1003
1004
1005 err = __cpu_disable();
1006 if (err < 0)
1007 return err;
1008
1009
1010
1011
1012
1013 WARN_ON(st->state != (CPUHP_TEARDOWN_CPU - 1));
1014
1015
1016 ret = cpuhp_invoke_callback_range(false, cpu, st, target);
1017
1018
1019
1020
1021 WARN_ON_ONCE(ret);
1022
1023
1024 tick_handover_do_timer();
1025
1026 tick_offline_cpu(cpu);
1027
1028 stop_machine_park(cpu);
1029 return 0;
1030 }
1031
1032 static int takedown_cpu(unsigned int cpu)
1033 {
1034 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1035 int err;
1036
1037
1038 kthread_park(st->thread);
1039
1040
1041
1042
1043
1044 irq_lock_sparse();
1045
1046
1047
1048
1049 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
1050 if (err) {
1051
1052 irq_unlock_sparse();
1053
1054 kthread_unpark(st->thread);
1055 return err;
1056 }
1057 BUG_ON(cpu_online(cpu));
1058
1059
1060
1061
1062
1063
1064
1065
1066 wait_for_ap_thread(st, false);
1067 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1068
1069
1070 irq_unlock_sparse();
1071
1072 hotplug_cpu__broadcast_tick_pull(cpu);
1073
1074 __cpu_die(cpu);
1075
1076 tick_cleanup_dead_cpu(cpu);
1077 rcutree_migrate_callbacks(cpu);
1078 return 0;
1079 }
1080
1081 static void cpuhp_complete_idle_dead(void *arg)
1082 {
1083 struct cpuhp_cpu_state *st = arg;
1084
1085 complete_ap_thread(st, false);
1086 }
1087
1088 void cpuhp_report_idle_dead(void)
1089 {
1090 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1091
1092 BUG_ON(st->state != CPUHP_AP_OFFLINE);
1093 rcu_report_dead(smp_processor_id());
1094 st->state = CPUHP_AP_IDLE_DEAD;
1095
1096
1097
1098
1099 smp_call_function_single(cpumask_first(cpu_online_mask),
1100 cpuhp_complete_idle_dead, st, 0);
1101 }
1102
1103 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
1104 enum cpuhp_state target)
1105 {
1106 enum cpuhp_state prev_state = st->state;
1107 int ret = 0;
1108
1109 ret = cpuhp_invoke_callback_range(false, cpu, st, target);
1110 if (ret) {
1111 pr_debug("CPU DOWN failed (%d) CPU %u state %s (%d)\n",
1112 ret, cpu, cpuhp_get_step(st->state)->name,
1113 st->state);
1114
1115 cpuhp_reset_state(cpu, st, prev_state);
1116
1117 if (st->state < prev_state)
1118 WARN_ON(cpuhp_invoke_callback_range(true, cpu, st,
1119 prev_state));
1120 }
1121
1122 return ret;
1123 }
1124
1125
1126 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
1127 enum cpuhp_state target)
1128 {
1129 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1130 int prev_state, ret = 0;
1131
1132 if (num_online_cpus() == 1)
1133 return -EBUSY;
1134
1135 if (!cpu_present(cpu))
1136 return -EINVAL;
1137
1138 cpus_write_lock();
1139
1140 cpuhp_tasks_frozen = tasks_frozen;
1141
1142 prev_state = cpuhp_set_state(cpu, st, target);
1143
1144
1145
1146
1147 if (st->state > CPUHP_TEARDOWN_CPU) {
1148 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1149 ret = cpuhp_kick_ap_work(cpu);
1150
1151
1152
1153
1154 if (ret)
1155 goto out;
1156
1157
1158
1159
1160
1161 if (st->state > CPUHP_TEARDOWN_CPU)
1162 goto out;
1163
1164 st->target = target;
1165 }
1166
1167
1168
1169
1170 ret = cpuhp_down_callbacks(cpu, st, target);
1171 if (ret && st->state < prev_state) {
1172 if (st->state == CPUHP_TEARDOWN_CPU) {
1173 cpuhp_reset_state(cpu, st, prev_state);
1174 __cpuhp_kick_ap(st);
1175 } else {
1176 WARN(1, "DEAD callback error for CPU%d", cpu);
1177 }
1178 }
1179
1180 out:
1181 cpus_write_unlock();
1182
1183
1184
1185
1186 lockup_detector_cleanup();
1187 arch_smt_update();
1188 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1189 return ret;
1190 }
1191
1192 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1193 {
1194
1195
1196
1197
1198 if (cc_platform_has(CC_ATTR_HOTPLUG_DISABLED))
1199 return -EOPNOTSUPP;
1200 if (cpu_hotplug_disabled)
1201 return -EBUSY;
1202 return _cpu_down(cpu, 0, target);
1203 }
1204
1205 static int cpu_down(unsigned int cpu, enum cpuhp_state target)
1206 {
1207 int err;
1208
1209 cpu_maps_update_begin();
1210 err = cpu_down_maps_locked(cpu, target);
1211 cpu_maps_update_done();
1212 return err;
1213 }
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225 int cpu_device_down(struct device *dev)
1226 {
1227 return cpu_down(dev->id, CPUHP_OFFLINE);
1228 }
1229
1230 int remove_cpu(unsigned int cpu)
1231 {
1232 int ret;
1233
1234 lock_device_hotplug();
1235 ret = device_offline(get_cpu_device(cpu));
1236 unlock_device_hotplug();
1237
1238 return ret;
1239 }
1240 EXPORT_SYMBOL_GPL(remove_cpu);
1241
1242 void smp_shutdown_nonboot_cpus(unsigned int primary_cpu)
1243 {
1244 unsigned int cpu;
1245 int error;
1246
1247 cpu_maps_update_begin();
1248
1249
1250
1251
1252
1253
1254 if (!cpu_online(primary_cpu))
1255 primary_cpu = cpumask_first(cpu_online_mask);
1256
1257 for_each_online_cpu(cpu) {
1258 if (cpu == primary_cpu)
1259 continue;
1260
1261 error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
1262 if (error) {
1263 pr_err("Failed to offline CPU%d - error=%d",
1264 cpu, error);
1265 break;
1266 }
1267 }
1268
1269
1270
1271
1272 BUG_ON(num_online_cpus() > 1);
1273
1274
1275
1276
1277
1278
1279 cpu_hotplug_disabled++;
1280
1281 cpu_maps_update_done();
1282 }
1283
1284 #else
1285 #define takedown_cpu NULL
1286 #endif
1287
1288
1289
1290
1291
1292
1293
1294
1295 void notify_cpu_starting(unsigned int cpu)
1296 {
1297 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1298 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
1299 int ret;
1300
1301 rcu_cpu_starting(cpu);
1302 cpumask_set_cpu(cpu, &cpus_booted_once_mask);
1303 ret = cpuhp_invoke_callback_range(true, cpu, st, target);
1304
1305
1306
1307
1308 WARN_ON_ONCE(ret);
1309 }
1310
1311
1312
1313
1314
1315
1316 void cpuhp_online_idle(enum cpuhp_state state)
1317 {
1318 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1319
1320
1321 if (state != CPUHP_AP_ONLINE_IDLE)
1322 return;
1323
1324
1325
1326
1327
1328 stop_machine_unpark(smp_processor_id());
1329
1330 st->state = CPUHP_AP_ONLINE_IDLE;
1331 complete_ap_thread(st, true);
1332 }
1333
1334
1335 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1336 {
1337 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1338 struct task_struct *idle;
1339 int ret = 0;
1340
1341 cpus_write_lock();
1342
1343 if (!cpu_present(cpu)) {
1344 ret = -EINVAL;
1345 goto out;
1346 }
1347
1348
1349
1350
1351
1352 if (st->state >= target)
1353 goto out;
1354
1355 if (st->state == CPUHP_OFFLINE) {
1356
1357 idle = idle_thread_get(cpu);
1358 if (IS_ERR(idle)) {
1359 ret = PTR_ERR(idle);
1360 goto out;
1361 }
1362 }
1363
1364 cpuhp_tasks_frozen = tasks_frozen;
1365
1366 cpuhp_set_state(cpu, st, target);
1367
1368
1369
1370
1371 if (st->state > CPUHP_BRINGUP_CPU) {
1372 ret = cpuhp_kick_ap_work(cpu);
1373
1374
1375
1376
1377 if (ret)
1378 goto out;
1379 }
1380
1381
1382
1383
1384
1385
1386 target = min((int)target, CPUHP_BRINGUP_CPU);
1387 ret = cpuhp_up_callbacks(cpu, st, target);
1388 out:
1389 cpus_write_unlock();
1390 arch_smt_update();
1391 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1392 return ret;
1393 }
1394
1395 static int cpu_up(unsigned int cpu, enum cpuhp_state target)
1396 {
1397 int err = 0;
1398
1399 if (!cpu_possible(cpu)) {
1400 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1401 cpu);
1402 #if defined(CONFIG_IA64)
1403 pr_err("please check additional_cpus= boot parameter\n");
1404 #endif
1405 return -EINVAL;
1406 }
1407
1408 err = try_online_node(cpu_to_node(cpu));
1409 if (err)
1410 return err;
1411
1412 cpu_maps_update_begin();
1413
1414 if (cpu_hotplug_disabled) {
1415 err = -EBUSY;
1416 goto out;
1417 }
1418 if (!cpu_smt_allowed(cpu)) {
1419 err = -EPERM;
1420 goto out;
1421 }
1422
1423 err = _cpu_up(cpu, 0, target);
1424 out:
1425 cpu_maps_update_done();
1426 return err;
1427 }
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439 int cpu_device_up(struct device *dev)
1440 {
1441 return cpu_up(dev->id, CPUHP_ONLINE);
1442 }
1443
1444 int add_cpu(unsigned int cpu)
1445 {
1446 int ret;
1447
1448 lock_device_hotplug();
1449 ret = device_online(get_cpu_device(cpu));
1450 unlock_device_hotplug();
1451
1452 return ret;
1453 }
1454 EXPORT_SYMBOL_GPL(add_cpu);
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466 int bringup_hibernate_cpu(unsigned int sleep_cpu)
1467 {
1468 int ret;
1469
1470 if (!cpu_online(sleep_cpu)) {
1471 pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
1472 ret = cpu_up(sleep_cpu, CPUHP_ONLINE);
1473 if (ret) {
1474 pr_err("Failed to bring hibernate-CPU up!\n");
1475 return ret;
1476 }
1477 }
1478 return 0;
1479 }
1480
1481 void bringup_nonboot_cpus(unsigned int setup_max_cpus)
1482 {
1483 unsigned int cpu;
1484
1485 for_each_present_cpu(cpu) {
1486 if (num_online_cpus() >= setup_max_cpus)
1487 break;
1488 if (!cpu_online(cpu))
1489 cpu_up(cpu, CPUHP_ONLINE);
1490 }
1491 }
1492
1493 #ifdef CONFIG_PM_SLEEP_SMP
1494 static cpumask_var_t frozen_cpus;
1495
1496 int freeze_secondary_cpus(int primary)
1497 {
1498 int cpu, error = 0;
1499
1500 cpu_maps_update_begin();
1501 if (primary == -1) {
1502 primary = cpumask_first(cpu_online_mask);
1503 if (!housekeeping_cpu(primary, HK_TYPE_TIMER))
1504 primary = housekeeping_any_cpu(HK_TYPE_TIMER);
1505 } else {
1506 if (!cpu_online(primary))
1507 primary = cpumask_first(cpu_online_mask);
1508 }
1509
1510
1511
1512
1513
1514 cpumask_clear(frozen_cpus);
1515
1516 pr_info("Disabling non-boot CPUs ...\n");
1517 for_each_online_cpu(cpu) {
1518 if (cpu == primary)
1519 continue;
1520
1521 if (pm_wakeup_pending()) {
1522 pr_info("Wakeup pending. Abort CPU freeze\n");
1523 error = -EBUSY;
1524 break;
1525 }
1526
1527 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1528 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1529 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1530 if (!error)
1531 cpumask_set_cpu(cpu, frozen_cpus);
1532 else {
1533 pr_err("Error taking CPU%d down: %d\n", cpu, error);
1534 break;
1535 }
1536 }
1537
1538 if (!error)
1539 BUG_ON(num_online_cpus() > 1);
1540 else
1541 pr_err("Non-boot CPUs are not disabled\n");
1542
1543
1544
1545
1546
1547
1548 cpu_hotplug_disabled++;
1549
1550 cpu_maps_update_done();
1551 return error;
1552 }
1553
1554 void __weak arch_thaw_secondary_cpus_begin(void)
1555 {
1556 }
1557
1558 void __weak arch_thaw_secondary_cpus_end(void)
1559 {
1560 }
1561
1562 void thaw_secondary_cpus(void)
1563 {
1564 int cpu, error;
1565
1566
1567 cpu_maps_update_begin();
1568 __cpu_hotplug_enable();
1569 if (cpumask_empty(frozen_cpus))
1570 goto out;
1571
1572 pr_info("Enabling non-boot CPUs ...\n");
1573
1574 arch_thaw_secondary_cpus_begin();
1575
1576 for_each_cpu(cpu, frozen_cpus) {
1577 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1578 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1579 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1580 if (!error) {
1581 pr_info("CPU%d is up\n", cpu);
1582 continue;
1583 }
1584 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1585 }
1586
1587 arch_thaw_secondary_cpus_end();
1588
1589 cpumask_clear(frozen_cpus);
1590 out:
1591 cpu_maps_update_done();
1592 }
1593
1594 static int __init alloc_frozen_cpus(void)
1595 {
1596 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1597 return -ENOMEM;
1598 return 0;
1599 }
1600 core_initcall(alloc_frozen_cpus);
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613 static int
1614 cpu_hotplug_pm_callback(struct notifier_block *nb,
1615 unsigned long action, void *ptr)
1616 {
1617 switch (action) {
1618
1619 case PM_SUSPEND_PREPARE:
1620 case PM_HIBERNATION_PREPARE:
1621 cpu_hotplug_disable();
1622 break;
1623
1624 case PM_POST_SUSPEND:
1625 case PM_POST_HIBERNATION:
1626 cpu_hotplug_enable();
1627 break;
1628
1629 default:
1630 return NOTIFY_DONE;
1631 }
1632
1633 return NOTIFY_OK;
1634 }
1635
1636
1637 static int __init cpu_hotplug_pm_sync_init(void)
1638 {
1639
1640
1641
1642
1643
1644 pm_notifier(cpu_hotplug_pm_callback, 0);
1645 return 0;
1646 }
1647 core_initcall(cpu_hotplug_pm_sync_init);
1648
1649 #endif
1650
1651 int __boot_cpu_id;
1652
1653 #endif
1654
1655
1656 static struct cpuhp_step cpuhp_hp_states[] = {
1657 [CPUHP_OFFLINE] = {
1658 .name = "offline",
1659 .startup.single = NULL,
1660 .teardown.single = NULL,
1661 },
1662 #ifdef CONFIG_SMP
1663 [CPUHP_CREATE_THREADS]= {
1664 .name = "threads:prepare",
1665 .startup.single = smpboot_create_threads,
1666 .teardown.single = NULL,
1667 .cant_stop = true,
1668 },
1669 [CPUHP_PERF_PREPARE] = {
1670 .name = "perf:prepare",
1671 .startup.single = perf_event_init_cpu,
1672 .teardown.single = perf_event_exit_cpu,
1673 },
1674 [CPUHP_RANDOM_PREPARE] = {
1675 .name = "random:prepare",
1676 .startup.single = random_prepare_cpu,
1677 .teardown.single = NULL,
1678 },
1679 [CPUHP_WORKQUEUE_PREP] = {
1680 .name = "workqueue:prepare",
1681 .startup.single = workqueue_prepare_cpu,
1682 .teardown.single = NULL,
1683 },
1684 [CPUHP_HRTIMERS_PREPARE] = {
1685 .name = "hrtimers:prepare",
1686 .startup.single = hrtimers_prepare_cpu,
1687 .teardown.single = hrtimers_dead_cpu,
1688 },
1689 [CPUHP_SMPCFD_PREPARE] = {
1690 .name = "smpcfd:prepare",
1691 .startup.single = smpcfd_prepare_cpu,
1692 .teardown.single = smpcfd_dead_cpu,
1693 },
1694 [CPUHP_RELAY_PREPARE] = {
1695 .name = "relay:prepare",
1696 .startup.single = relay_prepare_cpu,
1697 .teardown.single = NULL,
1698 },
1699 [CPUHP_SLAB_PREPARE] = {
1700 .name = "slab:prepare",
1701 .startup.single = slab_prepare_cpu,
1702 .teardown.single = slab_dead_cpu,
1703 },
1704 [CPUHP_RCUTREE_PREP] = {
1705 .name = "RCU/tree:prepare",
1706 .startup.single = rcutree_prepare_cpu,
1707 .teardown.single = rcutree_dead_cpu,
1708 },
1709
1710
1711
1712
1713
1714 [CPUHP_TIMERS_PREPARE] = {
1715 .name = "timers:prepare",
1716 .startup.single = timers_prepare_cpu,
1717 .teardown.single = timers_dead_cpu,
1718 },
1719
1720 [CPUHP_BRINGUP_CPU] = {
1721 .name = "cpu:bringup",
1722 .startup.single = bringup_cpu,
1723 .teardown.single = finish_cpu,
1724 .cant_stop = true,
1725 },
1726
1727 [CPUHP_AP_IDLE_DEAD] = {
1728 .name = "idle:dead",
1729 },
1730
1731
1732
1733
1734 [CPUHP_AP_OFFLINE] = {
1735 .name = "ap:offline",
1736 .cant_stop = true,
1737 },
1738
1739 [CPUHP_AP_SCHED_STARTING] = {
1740 .name = "sched:starting",
1741 .startup.single = sched_cpu_starting,
1742 .teardown.single = sched_cpu_dying,
1743 },
1744 [CPUHP_AP_RCUTREE_DYING] = {
1745 .name = "RCU/tree:dying",
1746 .startup.single = NULL,
1747 .teardown.single = rcutree_dying_cpu,
1748 },
1749 [CPUHP_AP_SMPCFD_DYING] = {
1750 .name = "smpcfd:dying",
1751 .startup.single = NULL,
1752 .teardown.single = smpcfd_dying_cpu,
1753 },
1754
1755
1756 [CPUHP_AP_ONLINE] = {
1757 .name = "ap:online",
1758 },
1759
1760
1761
1762
1763 [CPUHP_TEARDOWN_CPU] = {
1764 .name = "cpu:teardown",
1765 .startup.single = NULL,
1766 .teardown.single = takedown_cpu,
1767 .cant_stop = true,
1768 },
1769
1770 [CPUHP_AP_SCHED_WAIT_EMPTY] = {
1771 .name = "sched:waitempty",
1772 .startup.single = NULL,
1773 .teardown.single = sched_cpu_wait_empty,
1774 },
1775
1776
1777 [CPUHP_AP_SMPBOOT_THREADS] = {
1778 .name = "smpboot/threads:online",
1779 .startup.single = smpboot_unpark_threads,
1780 .teardown.single = smpboot_park_threads,
1781 },
1782 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1783 .name = "irq/affinity:online",
1784 .startup.single = irq_affinity_online_cpu,
1785 .teardown.single = NULL,
1786 },
1787 [CPUHP_AP_PERF_ONLINE] = {
1788 .name = "perf:online",
1789 .startup.single = perf_event_init_cpu,
1790 .teardown.single = perf_event_exit_cpu,
1791 },
1792 [CPUHP_AP_WATCHDOG_ONLINE] = {
1793 .name = "lockup_detector:online",
1794 .startup.single = lockup_detector_online_cpu,
1795 .teardown.single = lockup_detector_offline_cpu,
1796 },
1797 [CPUHP_AP_WORKQUEUE_ONLINE] = {
1798 .name = "workqueue:online",
1799 .startup.single = workqueue_online_cpu,
1800 .teardown.single = workqueue_offline_cpu,
1801 },
1802 [CPUHP_AP_RANDOM_ONLINE] = {
1803 .name = "random:online",
1804 .startup.single = random_online_cpu,
1805 .teardown.single = NULL,
1806 },
1807 [CPUHP_AP_RCUTREE_ONLINE] = {
1808 .name = "RCU/tree:online",
1809 .startup.single = rcutree_online_cpu,
1810 .teardown.single = rcutree_offline_cpu,
1811 },
1812 #endif
1813
1814
1815
1816
1817 #ifdef CONFIG_SMP
1818
1819 [CPUHP_AP_ACTIVE] = {
1820 .name = "sched:active",
1821 .startup.single = sched_cpu_activate,
1822 .teardown.single = sched_cpu_deactivate,
1823 },
1824 #endif
1825
1826
1827 [CPUHP_ONLINE] = {
1828 .name = "online",
1829 .startup.single = NULL,
1830 .teardown.single = NULL,
1831 },
1832 };
1833
1834
1835 static int cpuhp_cb_check(enum cpuhp_state state)
1836 {
1837 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1838 return -EINVAL;
1839 return 0;
1840 }
1841
1842
1843
1844
1845
1846
1847 static int cpuhp_reserve_state(enum cpuhp_state state)
1848 {
1849 enum cpuhp_state i, end;
1850 struct cpuhp_step *step;
1851
1852 switch (state) {
1853 case CPUHP_AP_ONLINE_DYN:
1854 step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
1855 end = CPUHP_AP_ONLINE_DYN_END;
1856 break;
1857 case CPUHP_BP_PREPARE_DYN:
1858 step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
1859 end = CPUHP_BP_PREPARE_DYN_END;
1860 break;
1861 default:
1862 return -EINVAL;
1863 }
1864
1865 for (i = state; i <= end; i++, step++) {
1866 if (!step->name)
1867 return i;
1868 }
1869 WARN(1, "No more dynamic states available for CPU hotplug\n");
1870 return -ENOSPC;
1871 }
1872
1873 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1874 int (*startup)(unsigned int cpu),
1875 int (*teardown)(unsigned int cpu),
1876 bool multi_instance)
1877 {
1878
1879 struct cpuhp_step *sp;
1880 int ret = 0;
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1892 state == CPUHP_BP_PREPARE_DYN)) {
1893 ret = cpuhp_reserve_state(state);
1894 if (ret < 0)
1895 return ret;
1896 state = ret;
1897 }
1898 sp = cpuhp_get_step(state);
1899 if (name && sp->name)
1900 return -EBUSY;
1901
1902 sp->startup.single = startup;
1903 sp->teardown.single = teardown;
1904 sp->name = name;
1905 sp->multi_instance = multi_instance;
1906 INIT_HLIST_HEAD(&sp->list);
1907 return ret;
1908 }
1909
1910 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1911 {
1912 return cpuhp_get_step(state)->teardown.single;
1913 }
1914
1915
1916
1917
1918
1919 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1920 struct hlist_node *node)
1921 {
1922 struct cpuhp_step *sp = cpuhp_get_step(state);
1923 int ret;
1924
1925
1926
1927
1928
1929 if (cpuhp_step_empty(bringup, sp))
1930 return 0;
1931
1932
1933
1934
1935 #ifdef CONFIG_SMP
1936 if (cpuhp_is_ap_state(state))
1937 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1938 else
1939 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1940 #else
1941 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1942 #endif
1943 BUG_ON(ret && !bringup);
1944 return ret;
1945 }
1946
1947
1948
1949
1950
1951
1952 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
1953 struct hlist_node *node)
1954 {
1955 int cpu;
1956
1957
1958 for_each_present_cpu(cpu) {
1959 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1960 int cpustate = st->state;
1961
1962 if (cpu >= failedcpu)
1963 break;
1964
1965
1966 if (cpustate >= state)
1967 cpuhp_issue_call(cpu, state, false, node);
1968 }
1969 }
1970
1971 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1972 struct hlist_node *node,
1973 bool invoke)
1974 {
1975 struct cpuhp_step *sp;
1976 int cpu;
1977 int ret;
1978
1979 lockdep_assert_cpus_held();
1980
1981 sp = cpuhp_get_step(state);
1982 if (sp->multi_instance == false)
1983 return -EINVAL;
1984
1985 mutex_lock(&cpuhp_state_mutex);
1986
1987 if (!invoke || !sp->startup.multi)
1988 goto add_node;
1989
1990
1991
1992
1993
1994 for_each_present_cpu(cpu) {
1995 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1996 int cpustate = st->state;
1997
1998 if (cpustate < state)
1999 continue;
2000
2001 ret = cpuhp_issue_call(cpu, state, true, node);
2002 if (ret) {
2003 if (sp->teardown.multi)
2004 cpuhp_rollback_install(cpu, state, node);
2005 goto unlock;
2006 }
2007 }
2008 add_node:
2009 ret = 0;
2010 hlist_add_head(node, &sp->list);
2011 unlock:
2012 mutex_unlock(&cpuhp_state_mutex);
2013 return ret;
2014 }
2015
2016 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
2017 bool invoke)
2018 {
2019 int ret;
2020
2021 cpus_read_lock();
2022 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
2023 cpus_read_unlock();
2024 return ret;
2025 }
2026 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
2047 const char *name, bool invoke,
2048 int (*startup)(unsigned int cpu),
2049 int (*teardown)(unsigned int cpu),
2050 bool multi_instance)
2051 {
2052 int cpu, ret = 0;
2053 bool dynstate;
2054
2055 lockdep_assert_cpus_held();
2056
2057 if (cpuhp_cb_check(state) || !name)
2058 return -EINVAL;
2059
2060 mutex_lock(&cpuhp_state_mutex);
2061
2062 ret = cpuhp_store_callbacks(state, name, startup, teardown,
2063 multi_instance);
2064
2065 dynstate = state == CPUHP_AP_ONLINE_DYN;
2066 if (ret > 0 && dynstate) {
2067 state = ret;
2068 ret = 0;
2069 }
2070
2071 if (ret || !invoke || !startup)
2072 goto out;
2073
2074
2075
2076
2077
2078 for_each_present_cpu(cpu) {
2079 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2080 int cpustate = st->state;
2081
2082 if (cpustate < state)
2083 continue;
2084
2085 ret = cpuhp_issue_call(cpu, state, true, NULL);
2086 if (ret) {
2087 if (teardown)
2088 cpuhp_rollback_install(cpu, state, NULL);
2089 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
2090 goto out;
2091 }
2092 }
2093 out:
2094 mutex_unlock(&cpuhp_state_mutex);
2095
2096
2097
2098
2099 if (!ret && dynstate)
2100 return state;
2101 return ret;
2102 }
2103 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
2104
2105 int __cpuhp_setup_state(enum cpuhp_state state,
2106 const char *name, bool invoke,
2107 int (*startup)(unsigned int cpu),
2108 int (*teardown)(unsigned int cpu),
2109 bool multi_instance)
2110 {
2111 int ret;
2112
2113 cpus_read_lock();
2114 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
2115 teardown, multi_instance);
2116 cpus_read_unlock();
2117 return ret;
2118 }
2119 EXPORT_SYMBOL(__cpuhp_setup_state);
2120
2121 int __cpuhp_state_remove_instance(enum cpuhp_state state,
2122 struct hlist_node *node, bool invoke)
2123 {
2124 struct cpuhp_step *sp = cpuhp_get_step(state);
2125 int cpu;
2126
2127 BUG_ON(cpuhp_cb_check(state));
2128
2129 if (!sp->multi_instance)
2130 return -EINVAL;
2131
2132 cpus_read_lock();
2133 mutex_lock(&cpuhp_state_mutex);
2134
2135 if (!invoke || !cpuhp_get_teardown_cb(state))
2136 goto remove;
2137
2138
2139
2140
2141
2142 for_each_present_cpu(cpu) {
2143 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2144 int cpustate = st->state;
2145
2146 if (cpustate >= state)
2147 cpuhp_issue_call(cpu, state, false, node);
2148 }
2149
2150 remove:
2151 hlist_del(node);
2152 mutex_unlock(&cpuhp_state_mutex);
2153 cpus_read_unlock();
2154
2155 return 0;
2156 }
2157 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
2170 {
2171 struct cpuhp_step *sp = cpuhp_get_step(state);
2172 int cpu;
2173
2174 BUG_ON(cpuhp_cb_check(state));
2175
2176 lockdep_assert_cpus_held();
2177
2178 mutex_lock(&cpuhp_state_mutex);
2179 if (sp->multi_instance) {
2180 WARN(!hlist_empty(&sp->list),
2181 "Error: Removing state %d which has instances left.\n",
2182 state);
2183 goto remove;
2184 }
2185
2186 if (!invoke || !cpuhp_get_teardown_cb(state))
2187 goto remove;
2188
2189
2190
2191
2192
2193
2194 for_each_present_cpu(cpu) {
2195 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2196 int cpustate = st->state;
2197
2198 if (cpustate >= state)
2199 cpuhp_issue_call(cpu, state, false, NULL);
2200 }
2201 remove:
2202 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
2203 mutex_unlock(&cpuhp_state_mutex);
2204 }
2205 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
2206
2207 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
2208 {
2209 cpus_read_lock();
2210 __cpuhp_remove_state_cpuslocked(state, invoke);
2211 cpus_read_unlock();
2212 }
2213 EXPORT_SYMBOL(__cpuhp_remove_state);
2214
2215 #ifdef CONFIG_HOTPLUG_SMT
2216 static void cpuhp_offline_cpu_device(unsigned int cpu)
2217 {
2218 struct device *dev = get_cpu_device(cpu);
2219
2220 dev->offline = true;
2221
2222 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2223 }
2224
2225 static void cpuhp_online_cpu_device(unsigned int cpu)
2226 {
2227 struct device *dev = get_cpu_device(cpu);
2228
2229 dev->offline = false;
2230
2231 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2232 }
2233
2234 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2235 {
2236 int cpu, ret = 0;
2237
2238 cpu_maps_update_begin();
2239 for_each_online_cpu(cpu) {
2240 if (topology_is_primary_thread(cpu))
2241 continue;
2242 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2243 if (ret)
2244 break;
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258 cpuhp_offline_cpu_device(cpu);
2259 }
2260 if (!ret)
2261 cpu_smt_control = ctrlval;
2262 cpu_maps_update_done();
2263 return ret;
2264 }
2265
2266 int cpuhp_smt_enable(void)
2267 {
2268 int cpu, ret = 0;
2269
2270 cpu_maps_update_begin();
2271 cpu_smt_control = CPU_SMT_ENABLED;
2272 for_each_present_cpu(cpu) {
2273
2274 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2275 continue;
2276 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2277 if (ret)
2278 break;
2279
2280 cpuhp_online_cpu_device(cpu);
2281 }
2282 cpu_maps_update_done();
2283 return ret;
2284 }
2285 #endif
2286
2287 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
2288 static ssize_t state_show(struct device *dev,
2289 struct device_attribute *attr, char *buf)
2290 {
2291 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2292
2293 return sprintf(buf, "%d\n", st->state);
2294 }
2295 static DEVICE_ATTR_RO(state);
2296
2297 static ssize_t target_store(struct device *dev, struct device_attribute *attr,
2298 const char *buf, size_t count)
2299 {
2300 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2301 struct cpuhp_step *sp;
2302 int target, ret;
2303
2304 ret = kstrtoint(buf, 10, &target);
2305 if (ret)
2306 return ret;
2307
2308 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
2309 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
2310 return -EINVAL;
2311 #else
2312 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
2313 return -EINVAL;
2314 #endif
2315
2316 ret = lock_device_hotplug_sysfs();
2317 if (ret)
2318 return ret;
2319
2320 mutex_lock(&cpuhp_state_mutex);
2321 sp = cpuhp_get_step(target);
2322 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
2323 mutex_unlock(&cpuhp_state_mutex);
2324 if (ret)
2325 goto out;
2326
2327 if (st->state < target)
2328 ret = cpu_up(dev->id, target);
2329 else
2330 ret = cpu_down(dev->id, target);
2331 out:
2332 unlock_device_hotplug();
2333 return ret ? ret : count;
2334 }
2335
2336 static ssize_t target_show(struct device *dev,
2337 struct device_attribute *attr, char *buf)
2338 {
2339 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2340
2341 return sprintf(buf, "%d\n", st->target);
2342 }
2343 static DEVICE_ATTR_RW(target);
2344
2345 static ssize_t fail_store(struct device *dev, struct device_attribute *attr,
2346 const char *buf, size_t count)
2347 {
2348 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2349 struct cpuhp_step *sp;
2350 int fail, ret;
2351
2352 ret = kstrtoint(buf, 10, &fail);
2353 if (ret)
2354 return ret;
2355
2356 if (fail == CPUHP_INVALID) {
2357 st->fail = fail;
2358 return count;
2359 }
2360
2361 if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
2362 return -EINVAL;
2363
2364
2365
2366
2367 if (cpuhp_is_atomic_state(fail))
2368 return -EINVAL;
2369
2370
2371
2372
2373
2374
2375
2376 if (fail <= CPUHP_BRINGUP_CPU && st->state > CPUHP_BRINGUP_CPU)
2377 return -EINVAL;
2378
2379
2380
2381
2382 mutex_lock(&cpuhp_state_mutex);
2383 sp = cpuhp_get_step(fail);
2384 if (!sp->startup.single && !sp->teardown.single)
2385 ret = -EINVAL;
2386 mutex_unlock(&cpuhp_state_mutex);
2387 if (ret)
2388 return ret;
2389
2390 st->fail = fail;
2391
2392 return count;
2393 }
2394
2395 static ssize_t fail_show(struct device *dev,
2396 struct device_attribute *attr, char *buf)
2397 {
2398 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2399
2400 return sprintf(buf, "%d\n", st->fail);
2401 }
2402
2403 static DEVICE_ATTR_RW(fail);
2404
2405 static struct attribute *cpuhp_cpu_attrs[] = {
2406 &dev_attr_state.attr,
2407 &dev_attr_target.attr,
2408 &dev_attr_fail.attr,
2409 NULL
2410 };
2411
2412 static const struct attribute_group cpuhp_cpu_attr_group = {
2413 .attrs = cpuhp_cpu_attrs,
2414 .name = "hotplug",
2415 NULL
2416 };
2417
2418 static ssize_t states_show(struct device *dev,
2419 struct device_attribute *attr, char *buf)
2420 {
2421 ssize_t cur, res = 0;
2422 int i;
2423
2424 mutex_lock(&cpuhp_state_mutex);
2425 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
2426 struct cpuhp_step *sp = cpuhp_get_step(i);
2427
2428 if (sp->name) {
2429 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2430 buf += cur;
2431 res += cur;
2432 }
2433 }
2434 mutex_unlock(&cpuhp_state_mutex);
2435 return res;
2436 }
2437 static DEVICE_ATTR_RO(states);
2438
2439 static struct attribute *cpuhp_cpu_root_attrs[] = {
2440 &dev_attr_states.attr,
2441 NULL
2442 };
2443
2444 static const struct attribute_group cpuhp_cpu_root_attr_group = {
2445 .attrs = cpuhp_cpu_root_attrs,
2446 .name = "hotplug",
2447 NULL
2448 };
2449
2450 #ifdef CONFIG_HOTPLUG_SMT
2451
2452 static ssize_t
2453 __store_smt_control(struct device *dev, struct device_attribute *attr,
2454 const char *buf, size_t count)
2455 {
2456 int ctrlval, ret;
2457
2458 if (sysfs_streq(buf, "on"))
2459 ctrlval = CPU_SMT_ENABLED;
2460 else if (sysfs_streq(buf, "off"))
2461 ctrlval = CPU_SMT_DISABLED;
2462 else if (sysfs_streq(buf, "forceoff"))
2463 ctrlval = CPU_SMT_FORCE_DISABLED;
2464 else
2465 return -EINVAL;
2466
2467 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2468 return -EPERM;
2469
2470 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2471 return -ENODEV;
2472
2473 ret = lock_device_hotplug_sysfs();
2474 if (ret)
2475 return ret;
2476
2477 if (ctrlval != cpu_smt_control) {
2478 switch (ctrlval) {
2479 case CPU_SMT_ENABLED:
2480 ret = cpuhp_smt_enable();
2481 break;
2482 case CPU_SMT_DISABLED:
2483 case CPU_SMT_FORCE_DISABLED:
2484 ret = cpuhp_smt_disable(ctrlval);
2485 break;
2486 }
2487 }
2488
2489 unlock_device_hotplug();
2490 return ret ? ret : count;
2491 }
2492
2493 #else
2494 static ssize_t
2495 __store_smt_control(struct device *dev, struct device_attribute *attr,
2496 const char *buf, size_t count)
2497 {
2498 return -ENODEV;
2499 }
2500 #endif
2501
2502 static const char *smt_states[] = {
2503 [CPU_SMT_ENABLED] = "on",
2504 [CPU_SMT_DISABLED] = "off",
2505 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2506 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2507 [CPU_SMT_NOT_IMPLEMENTED] = "notimplemented",
2508 };
2509
2510 static ssize_t control_show(struct device *dev,
2511 struct device_attribute *attr, char *buf)
2512 {
2513 const char *state = smt_states[cpu_smt_control];
2514
2515 return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
2516 }
2517
2518 static ssize_t control_store(struct device *dev, struct device_attribute *attr,
2519 const char *buf, size_t count)
2520 {
2521 return __store_smt_control(dev, attr, buf, count);
2522 }
2523 static DEVICE_ATTR_RW(control);
2524
2525 static ssize_t active_show(struct device *dev,
2526 struct device_attribute *attr, char *buf)
2527 {
2528 return snprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
2529 }
2530 static DEVICE_ATTR_RO(active);
2531
2532 static struct attribute *cpuhp_smt_attrs[] = {
2533 &dev_attr_control.attr,
2534 &dev_attr_active.attr,
2535 NULL
2536 };
2537
2538 static const struct attribute_group cpuhp_smt_attr_group = {
2539 .attrs = cpuhp_smt_attrs,
2540 .name = "smt",
2541 NULL
2542 };
2543
2544 static int __init cpu_smt_sysfs_init(void)
2545 {
2546 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2547 &cpuhp_smt_attr_group);
2548 }
2549
2550 static int __init cpuhp_sysfs_init(void)
2551 {
2552 int cpu, ret;
2553
2554 ret = cpu_smt_sysfs_init();
2555 if (ret)
2556 return ret;
2557
2558 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2559 &cpuhp_cpu_root_attr_group);
2560 if (ret)
2561 return ret;
2562
2563 for_each_possible_cpu(cpu) {
2564 struct device *dev = get_cpu_device(cpu);
2565
2566 if (!dev)
2567 continue;
2568 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2569 if (ret)
2570 return ret;
2571 }
2572 return 0;
2573 }
2574 device_initcall(cpuhp_sysfs_init);
2575 #endif
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
2587 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2588 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2589 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
2590
2591 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
2592
2593 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2594 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2595 #if BITS_PER_LONG > 32
2596 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2597 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
2598 #endif
2599 };
2600 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2601
2602 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2603 EXPORT_SYMBOL(cpu_all_bits);
2604
2605 #ifdef CONFIG_INIT_ALL_POSSIBLE
2606 struct cpumask __cpu_possible_mask __read_mostly
2607 = {CPU_BITS_ALL};
2608 #else
2609 struct cpumask __cpu_possible_mask __read_mostly;
2610 #endif
2611 EXPORT_SYMBOL(__cpu_possible_mask);
2612
2613 struct cpumask __cpu_online_mask __read_mostly;
2614 EXPORT_SYMBOL(__cpu_online_mask);
2615
2616 struct cpumask __cpu_present_mask __read_mostly;
2617 EXPORT_SYMBOL(__cpu_present_mask);
2618
2619 struct cpumask __cpu_active_mask __read_mostly;
2620 EXPORT_SYMBOL(__cpu_active_mask);
2621
2622 struct cpumask __cpu_dying_mask __read_mostly;
2623 EXPORT_SYMBOL(__cpu_dying_mask);
2624
2625 atomic_t __num_online_cpus __read_mostly;
2626 EXPORT_SYMBOL(__num_online_cpus);
2627
2628 void init_cpu_present(const struct cpumask *src)
2629 {
2630 cpumask_copy(&__cpu_present_mask, src);
2631 }
2632
2633 void init_cpu_possible(const struct cpumask *src)
2634 {
2635 cpumask_copy(&__cpu_possible_mask, src);
2636 }
2637
2638 void init_cpu_online(const struct cpumask *src)
2639 {
2640 cpumask_copy(&__cpu_online_mask, src);
2641 }
2642
2643 void set_cpu_online(unsigned int cpu, bool online)
2644 {
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655 if (online) {
2656 if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
2657 atomic_inc(&__num_online_cpus);
2658 } else {
2659 if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
2660 atomic_dec(&__num_online_cpus);
2661 }
2662 }
2663
2664
2665
2666
2667 void __init boot_cpu_init(void)
2668 {
2669 int cpu = smp_processor_id();
2670
2671
2672 set_cpu_online(cpu, true);
2673 set_cpu_active(cpu, true);
2674 set_cpu_present(cpu, true);
2675 set_cpu_possible(cpu, true);
2676
2677 #ifdef CONFIG_SMP
2678 __boot_cpu_id = cpu;
2679 #endif
2680 }
2681
2682
2683
2684
2685 void __init boot_cpu_hotplug_init(void)
2686 {
2687 #ifdef CONFIG_SMP
2688 cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask);
2689 #endif
2690 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
2691 }
2692
2693
2694
2695
2696
2697 enum cpu_mitigations {
2698 CPU_MITIGATIONS_OFF,
2699 CPU_MITIGATIONS_AUTO,
2700 CPU_MITIGATIONS_AUTO_NOSMT,
2701 };
2702
2703 static enum cpu_mitigations cpu_mitigations __ro_after_init =
2704 CPU_MITIGATIONS_AUTO;
2705
2706 static int __init mitigations_parse_cmdline(char *arg)
2707 {
2708 if (!strcmp(arg, "off"))
2709 cpu_mitigations = CPU_MITIGATIONS_OFF;
2710 else if (!strcmp(arg, "auto"))
2711 cpu_mitigations = CPU_MITIGATIONS_AUTO;
2712 else if (!strcmp(arg, "auto,nosmt"))
2713 cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
2714 else
2715 pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
2716 arg);
2717
2718 return 0;
2719 }
2720 early_param("mitigations", mitigations_parse_cmdline);
2721
2722
2723 bool cpu_mitigations_off(void)
2724 {
2725 return cpu_mitigations == CPU_MITIGATIONS_OFF;
2726 }
2727 EXPORT_SYMBOL_GPL(cpu_mitigations_off);
2728
2729
2730 bool cpu_mitigations_auto_nosmt(void)
2731 {
2732 return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
2733 }
2734 EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);