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