]>
Commit | Line | Data |
---|---|---|
1 | /* CPU control. | |
2 | * (C) 2001, 2002, 2003, 2004 Rusty Russell | |
3 | * | |
4 | * This code is licenced under the GPL. | |
5 | */ | |
6 | #include <linux/proc_fs.h> | |
7 | #include <linux/smp.h> | |
8 | #include <linux/init.h> | |
9 | #include <linux/notifier.h> | |
10 | #include <linux/sched/signal.h> | |
11 | #include <linux/sched/hotplug.h> | |
12 | #include <linux/sched/task.h> | |
13 | #include <linux/sched/smt.h> | |
14 | #include <linux/unistd.h> | |
15 | #include <linux/cpu.h> | |
16 | #include <linux/oom.h> | |
17 | #include <linux/rcupdate.h> | |
18 | #include <linux/export.h> | |
19 | #include <linux/bug.h> | |
20 | #include <linux/kthread.h> | |
21 | #include <linux/stop_machine.h> | |
22 | #include <linux/mutex.h> | |
23 | #include <linux/gfp.h> | |
24 | #include <linux/suspend.h> | |
25 | #include <linux/lockdep.h> | |
26 | #include <linux/tick.h> | |
27 | #include <linux/irq.h> | |
28 | #include <linux/nmi.h> | |
29 | #include <linux/smpboot.h> | |
30 | #include <linux/relay.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/percpu-rwsem.h> | |
33 | ||
34 | #include <trace/events/power.h> | |
35 | #define CREATE_TRACE_POINTS | |
36 | #include <trace/events/cpuhp.h> | |
37 | ||
38 | #include "smpboot.h" | |
39 | ||
40 | /** | |
41 | * cpuhp_cpu_state - Per cpu hotplug state storage | |
42 | * @state: The current cpu state | |
43 | * @target: The target state | |
44 | * @thread: Pointer to the hotplug thread | |
45 | * @should_run: Thread should execute | |
46 | * @rollback: Perform a rollback | |
47 | * @single: Single callback invocation | |
48 | * @bringup: Single callback bringup or teardown selector | |
49 | * @cb_state: The state for a single callback (install/uninstall) | |
50 | * @result: Result of the operation | |
51 | * @done_up: Signal completion to the issuer of the task for cpu-up | |
52 | * @done_down: Signal completion to the issuer of the task for cpu-down | |
53 | */ | |
54 | struct cpuhp_cpu_state { | |
55 | enum cpuhp_state state; | |
56 | enum cpuhp_state target; | |
57 | enum cpuhp_state fail; | |
58 | #ifdef CONFIG_SMP | |
59 | struct task_struct *thread; | |
60 | bool should_run; | |
61 | bool rollback; | |
62 | bool single; | |
63 | bool bringup; | |
64 | bool booted_once; | |
65 | struct hlist_node *node; | |
66 | struct hlist_node *last; | |
67 | enum cpuhp_state cb_state; | |
68 | int result; | |
69 | struct completion done_up; | |
70 | struct completion done_down; | |
71 | #endif | |
72 | }; | |
73 | ||
74 | static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = { | |
75 | .fail = CPUHP_INVALID, | |
76 | }; | |
77 | ||
78 | #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP) | |
79 | static struct lockdep_map cpuhp_state_up_map = | |
80 | STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map); | |
81 | static struct lockdep_map cpuhp_state_down_map = | |
82 | STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map); | |
83 | ||
84 | ||
85 | static inline void cpuhp_lock_acquire(bool bringup) | |
86 | { | |
87 | lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map); | |
88 | } | |
89 | ||
90 | static inline void cpuhp_lock_release(bool bringup) | |
91 | { | |
92 | lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map); | |
93 | } | |
94 | #else | |
95 | ||
96 | static inline void cpuhp_lock_acquire(bool bringup) { } | |
97 | static inline void cpuhp_lock_release(bool bringup) { } | |
98 | ||
99 | #endif | |
100 | ||
101 | /** | |
102 | * cpuhp_step - Hotplug state machine step | |
103 | * @name: Name of the step | |
104 | * @startup: Startup function of the step | |
105 | * @teardown: Teardown function of the step | |
106 | * @skip_onerr: Do not invoke the functions on error rollback | |
107 | * Will go away once the notifiers are gone | |
108 | * @cant_stop: Bringup/teardown can't be stopped at this step | |
109 | */ | |
110 | struct cpuhp_step { | |
111 | const char *name; | |
112 | union { | |
113 | int (*single)(unsigned int cpu); | |
114 | int (*multi)(unsigned int cpu, | |
115 | struct hlist_node *node); | |
116 | } startup; | |
117 | union { | |
118 | int (*single)(unsigned int cpu); | |
119 | int (*multi)(unsigned int cpu, | |
120 | struct hlist_node *node); | |
121 | } teardown; | |
122 | struct hlist_head list; | |
123 | bool skip_onerr; | |
124 | bool cant_stop; | |
125 | bool multi_instance; | |
126 | }; | |
127 | ||
128 | static DEFINE_MUTEX(cpuhp_state_mutex); | |
129 | static struct cpuhp_step cpuhp_bp_states[]; | |
130 | static struct cpuhp_step cpuhp_ap_states[]; | |
131 | ||
132 | static bool cpuhp_is_ap_state(enum cpuhp_state state) | |
133 | { | |
134 | /* | |
135 | * The extra check for CPUHP_TEARDOWN_CPU is only for documentation | |
136 | * purposes as that state is handled explicitly in cpu_down. | |
137 | */ | |
138 | return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU; | |
139 | } | |
140 | ||
141 | static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state) | |
142 | { | |
143 | struct cpuhp_step *sp; | |
144 | ||
145 | sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states; | |
146 | return sp + state; | |
147 | } | |
148 | ||
149 | /** | |
150 | * cpuhp_invoke_callback _ Invoke the callbacks for a given state | |
151 | * @cpu: The cpu for which the callback should be invoked | |
152 | * @state: The state to do callbacks for | |
153 | * @bringup: True if the bringup callback should be invoked | |
154 | * @node: For multi-instance, do a single entry callback for install/remove | |
155 | * @lastp: For multi-instance rollback, remember how far we got | |
156 | * | |
157 | * Called from cpu hotplug and from the state register machinery. | |
158 | */ | |
159 | static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state, | |
160 | bool bringup, struct hlist_node *node, | |
161 | struct hlist_node **lastp) | |
162 | { | |
163 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
164 | struct cpuhp_step *step = cpuhp_get_step(state); | |
165 | int (*cbm)(unsigned int cpu, struct hlist_node *node); | |
166 | int (*cb)(unsigned int cpu); | |
167 | int ret, cnt; | |
168 | ||
169 | if (st->fail == state) { | |
170 | st->fail = CPUHP_INVALID; | |
171 | ||
172 | if (!(bringup ? step->startup.single : step->teardown.single)) | |
173 | return 0; | |
174 | ||
175 | return -EAGAIN; | |
176 | } | |
177 | ||
178 | if (!step->multi_instance) { | |
179 | WARN_ON_ONCE(lastp && *lastp); | |
180 | cb = bringup ? step->startup.single : step->teardown.single; | |
181 | if (!cb) | |
182 | return 0; | |
183 | trace_cpuhp_enter(cpu, st->target, state, cb); | |
184 | ret = cb(cpu); | |
185 | trace_cpuhp_exit(cpu, st->state, state, ret); | |
186 | return ret; | |
187 | } | |
188 | cbm = bringup ? step->startup.multi : step->teardown.multi; | |
189 | if (!cbm) | |
190 | return 0; | |
191 | ||
192 | /* Single invocation for instance add/remove */ | |
193 | if (node) { | |
194 | WARN_ON_ONCE(lastp && *lastp); | |
195 | trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node); | |
196 | ret = cbm(cpu, node); | |
197 | trace_cpuhp_exit(cpu, st->state, state, ret); | |
198 | return ret; | |
199 | } | |
200 | ||
201 | /* State transition. Invoke on all instances */ | |
202 | cnt = 0; | |
203 | hlist_for_each(node, &step->list) { | |
204 | if (lastp && node == *lastp) | |
205 | break; | |
206 | ||
207 | trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node); | |
208 | ret = cbm(cpu, node); | |
209 | trace_cpuhp_exit(cpu, st->state, state, ret); | |
210 | if (ret) { | |
211 | if (!lastp) | |
212 | goto err; | |
213 | ||
214 | *lastp = node; | |
215 | return ret; | |
216 | } | |
217 | cnt++; | |
218 | } | |
219 | if (lastp) | |
220 | *lastp = NULL; | |
221 | return 0; | |
222 | err: | |
223 | /* Rollback the instances if one failed */ | |
224 | cbm = !bringup ? step->startup.multi : step->teardown.multi; | |
225 | if (!cbm) | |
226 | return ret; | |
227 | ||
228 | hlist_for_each(node, &step->list) { | |
229 | if (!cnt--) | |
230 | break; | |
231 | ||
232 | trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node); | |
233 | ret = cbm(cpu, node); | |
234 | trace_cpuhp_exit(cpu, st->state, state, ret); | |
235 | /* | |
236 | * Rollback must not fail, | |
237 | */ | |
238 | WARN_ON_ONCE(ret); | |
239 | } | |
240 | return ret; | |
241 | } | |
242 | ||
243 | #ifdef CONFIG_SMP | |
244 | static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup) | |
245 | { | |
246 | struct completion *done = bringup ? &st->done_up : &st->done_down; | |
247 | wait_for_completion(done); | |
248 | } | |
249 | ||
250 | static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup) | |
251 | { | |
252 | struct completion *done = bringup ? &st->done_up : &st->done_down; | |
253 | complete(done); | |
254 | } | |
255 | ||
256 | /* | |
257 | * The former STARTING/DYING states, ran with IRQs disabled and must not fail. | |
258 | */ | |
259 | static bool cpuhp_is_atomic_state(enum cpuhp_state state) | |
260 | { | |
261 | return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE; | |
262 | } | |
263 | ||
264 | /* Serializes the updates to cpu_online_mask, cpu_present_mask */ | |
265 | static DEFINE_MUTEX(cpu_add_remove_lock); | |
266 | bool cpuhp_tasks_frozen; | |
267 | EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen); | |
268 | ||
269 | /* | |
270 | * The following two APIs (cpu_maps_update_begin/done) must be used when | |
271 | * attempting to serialize the updates to cpu_online_mask & cpu_present_mask. | |
272 | */ | |
273 | void cpu_maps_update_begin(void) | |
274 | { | |
275 | mutex_lock(&cpu_add_remove_lock); | |
276 | } | |
277 | ||
278 | void cpu_maps_update_done(void) | |
279 | { | |
280 | mutex_unlock(&cpu_add_remove_lock); | |
281 | } | |
282 | ||
283 | /* | |
284 | * If set, cpu_up and cpu_down will return -EBUSY and do nothing. | |
285 | * Should always be manipulated under cpu_add_remove_lock | |
286 | */ | |
287 | static int cpu_hotplug_disabled; | |
288 | ||
289 | #ifdef CONFIG_HOTPLUG_CPU | |
290 | ||
291 | DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock); | |
292 | ||
293 | void cpus_read_lock(void) | |
294 | { | |
295 | percpu_down_read(&cpu_hotplug_lock); | |
296 | } | |
297 | EXPORT_SYMBOL_GPL(cpus_read_lock); | |
298 | ||
299 | void cpus_read_unlock(void) | |
300 | { | |
301 | percpu_up_read(&cpu_hotplug_lock); | |
302 | } | |
303 | EXPORT_SYMBOL_GPL(cpus_read_unlock); | |
304 | ||
305 | void cpus_write_lock(void) | |
306 | { | |
307 | percpu_down_write(&cpu_hotplug_lock); | |
308 | } | |
309 | ||
310 | void cpus_write_unlock(void) | |
311 | { | |
312 | percpu_up_write(&cpu_hotplug_lock); | |
313 | } | |
314 | ||
315 | void lockdep_assert_cpus_held(void) | |
316 | { | |
317 | percpu_rwsem_assert_held(&cpu_hotplug_lock); | |
318 | } | |
319 | ||
320 | /* | |
321 | * Wait for currently running CPU hotplug operations to complete (if any) and | |
322 | * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects | |
323 | * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the | |
324 | * hotplug path before performing hotplug operations. So acquiring that lock | |
325 | * guarantees mutual exclusion from any currently running hotplug operations. | |
326 | */ | |
327 | void cpu_hotplug_disable(void) | |
328 | { | |
329 | cpu_maps_update_begin(); | |
330 | cpu_hotplug_disabled++; | |
331 | cpu_maps_update_done(); | |
332 | } | |
333 | EXPORT_SYMBOL_GPL(cpu_hotplug_disable); | |
334 | ||
335 | static void __cpu_hotplug_enable(void) | |
336 | { | |
337 | if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n")) | |
338 | return; | |
339 | cpu_hotplug_disabled--; | |
340 | } | |
341 | ||
342 | void cpu_hotplug_enable(void) | |
343 | { | |
344 | cpu_maps_update_begin(); | |
345 | __cpu_hotplug_enable(); | |
346 | cpu_maps_update_done(); | |
347 | } | |
348 | EXPORT_SYMBOL_GPL(cpu_hotplug_enable); | |
349 | #endif /* CONFIG_HOTPLUG_CPU */ | |
350 | ||
351 | /* | |
352 | * Architectures that need SMT-specific errata handling during SMT hotplug | |
353 | * should override this. | |
354 | */ | |
355 | void __weak arch_smt_update(void) { } | |
356 | ||
357 | #ifdef CONFIG_HOTPLUG_SMT | |
358 | enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED; | |
359 | EXPORT_SYMBOL_GPL(cpu_smt_control); | |
360 | ||
361 | static bool cpu_smt_available __read_mostly; | |
362 | ||
363 | void __init cpu_smt_disable(bool force) | |
364 | { | |
365 | if (cpu_smt_control == CPU_SMT_FORCE_DISABLED || | |
366 | cpu_smt_control == CPU_SMT_NOT_SUPPORTED) | |
367 | return; | |
368 | ||
369 | if (force) { | |
370 | pr_info("SMT: Force disabled\n"); | |
371 | cpu_smt_control = CPU_SMT_FORCE_DISABLED; | |
372 | } else { | |
373 | cpu_smt_control = CPU_SMT_DISABLED; | |
374 | } | |
375 | } | |
376 | ||
377 | /* | |
378 | * The decision whether SMT is supported can only be done after the full | |
379 | * CPU identification. Called from architecture code before non boot CPUs | |
380 | * are brought up. | |
381 | */ | |
382 | void __init cpu_smt_check_topology_early(void) | |
383 | { | |
384 | if (!topology_smt_supported()) | |
385 | cpu_smt_control = CPU_SMT_NOT_SUPPORTED; | |
386 | } | |
387 | ||
388 | /* | |
389 | * If SMT was disabled by BIOS, detect it here, after the CPUs have been | |
390 | * brought online. This ensures the smt/l1tf sysfs entries are consistent | |
391 | * with reality. cpu_smt_available is set to true during the bringup of non | |
392 | * boot CPUs when a SMT sibling is detected. Note, this may overwrite | |
393 | * cpu_smt_control's previous setting. | |
394 | */ | |
395 | void __init cpu_smt_check_topology(void) | |
396 | { | |
397 | if (!cpu_smt_available) | |
398 | cpu_smt_control = CPU_SMT_NOT_SUPPORTED; | |
399 | } | |
400 | ||
401 | static int __init smt_cmdline_disable(char *str) | |
402 | { | |
403 | cpu_smt_disable(str && !strcmp(str, "force")); | |
404 | return 0; | |
405 | } | |
406 | early_param("nosmt", smt_cmdline_disable); | |
407 | ||
408 | static inline bool cpu_smt_allowed(unsigned int cpu) | |
409 | { | |
410 | if (topology_is_primary_thread(cpu)) | |
411 | return true; | |
412 | ||
413 | /* | |
414 | * If the CPU is not a 'primary' thread and the booted_once bit is | |
415 | * set then the processor has SMT support. Store this information | |
416 | * for the late check of SMT support in cpu_smt_check_topology(). | |
417 | */ | |
418 | if (per_cpu(cpuhp_state, cpu).booted_once) | |
419 | cpu_smt_available = true; | |
420 | ||
421 | if (cpu_smt_control == CPU_SMT_ENABLED) | |
422 | return true; | |
423 | ||
424 | /* | |
425 | * On x86 it's required to boot all logical CPUs at least once so | |
426 | * that the init code can get a chance to set CR4.MCE on each | |
427 | * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any | |
428 | * core will shutdown the machine. | |
429 | */ | |
430 | return !per_cpu(cpuhp_state, cpu).booted_once; | |
431 | } | |
432 | #else | |
433 | static inline bool cpu_smt_allowed(unsigned int cpu) { return true; } | |
434 | #endif | |
435 | ||
436 | static inline enum cpuhp_state | |
437 | cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target) | |
438 | { | |
439 | enum cpuhp_state prev_state = st->state; | |
440 | ||
441 | st->rollback = false; | |
442 | st->last = NULL; | |
443 | ||
444 | st->target = target; | |
445 | st->single = false; | |
446 | st->bringup = st->state < target; | |
447 | ||
448 | return prev_state; | |
449 | } | |
450 | ||
451 | static inline void | |
452 | cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state) | |
453 | { | |
454 | st->rollback = true; | |
455 | ||
456 | /* | |
457 | * If we have st->last we need to undo partial multi_instance of this | |
458 | * state first. Otherwise start undo at the previous state. | |
459 | */ | |
460 | if (!st->last) { | |
461 | if (st->bringup) | |
462 | st->state--; | |
463 | else | |
464 | st->state++; | |
465 | } | |
466 | ||
467 | st->target = prev_state; | |
468 | st->bringup = !st->bringup; | |
469 | } | |
470 | ||
471 | /* Regular hotplug invocation of the AP hotplug thread */ | |
472 | static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st) | |
473 | { | |
474 | if (!st->single && st->state == st->target) | |
475 | return; | |
476 | ||
477 | st->result = 0; | |
478 | /* | |
479 | * Make sure the above stores are visible before should_run becomes | |
480 | * true. Paired with the mb() above in cpuhp_thread_fun() | |
481 | */ | |
482 | smp_mb(); | |
483 | st->should_run = true; | |
484 | wake_up_process(st->thread); | |
485 | wait_for_ap_thread(st, st->bringup); | |
486 | } | |
487 | ||
488 | static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target) | |
489 | { | |
490 | enum cpuhp_state prev_state; | |
491 | int ret; | |
492 | ||
493 | prev_state = cpuhp_set_state(st, target); | |
494 | __cpuhp_kick_ap(st); | |
495 | if ((ret = st->result)) { | |
496 | cpuhp_reset_state(st, prev_state); | |
497 | __cpuhp_kick_ap(st); | |
498 | } | |
499 | ||
500 | return ret; | |
501 | } | |
502 | ||
503 | static int bringup_wait_for_ap(unsigned int cpu) | |
504 | { | |
505 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
506 | ||
507 | /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */ | |
508 | wait_for_ap_thread(st, true); | |
509 | if (WARN_ON_ONCE((!cpu_online(cpu)))) | |
510 | return -ECANCELED; | |
511 | ||
512 | /* Unpark the stopper thread and the hotplug thread of the target cpu */ | |
513 | stop_machine_unpark(cpu); | |
514 | kthread_unpark(st->thread); | |
515 | ||
516 | /* | |
517 | * SMT soft disabling on X86 requires to bring the CPU out of the | |
518 | * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The | |
519 | * CPU marked itself as booted_once in cpu_notify_starting() so the | |
520 | * cpu_smt_allowed() check will now return false if this is not the | |
521 | * primary sibling. | |
522 | */ | |
523 | if (!cpu_smt_allowed(cpu)) | |
524 | return -ECANCELED; | |
525 | ||
526 | if (st->target <= CPUHP_AP_ONLINE_IDLE) | |
527 | return 0; | |
528 | ||
529 | return cpuhp_kick_ap(st, st->target); | |
530 | } | |
531 | ||
532 | static int bringup_cpu(unsigned int cpu) | |
533 | { | |
534 | struct task_struct *idle = idle_thread_get(cpu); | |
535 | int ret; | |
536 | ||
537 | /* | |
538 | * Some architectures have to walk the irq descriptors to | |
539 | * setup the vector space for the cpu which comes online. | |
540 | * Prevent irq alloc/free across the bringup. | |
541 | */ | |
542 | irq_lock_sparse(); | |
543 | ||
544 | /* Arch-specific enabling code. */ | |
545 | ret = __cpu_up(cpu, idle); | |
546 | irq_unlock_sparse(); | |
547 | if (ret) | |
548 | return ret; | |
549 | return bringup_wait_for_ap(cpu); | |
550 | } | |
551 | ||
552 | /* | |
553 | * Hotplug state machine related functions | |
554 | */ | |
555 | ||
556 | static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st) | |
557 | { | |
558 | for (st->state--; st->state > st->target; st->state--) { | |
559 | struct cpuhp_step *step = cpuhp_get_step(st->state); | |
560 | ||
561 | if (!step->skip_onerr) | |
562 | cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL); | |
563 | } | |
564 | } | |
565 | ||
566 | static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st, | |
567 | enum cpuhp_state target) | |
568 | { | |
569 | enum cpuhp_state prev_state = st->state; | |
570 | int ret = 0; | |
571 | ||
572 | while (st->state < target) { | |
573 | st->state++; | |
574 | ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL); | |
575 | if (ret) { | |
576 | st->target = prev_state; | |
577 | undo_cpu_up(cpu, st); | |
578 | break; | |
579 | } | |
580 | } | |
581 | return ret; | |
582 | } | |
583 | ||
584 | /* | |
585 | * The cpu hotplug threads manage the bringup and teardown of the cpus | |
586 | */ | |
587 | static void cpuhp_create(unsigned int cpu) | |
588 | { | |
589 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
590 | ||
591 | init_completion(&st->done_up); | |
592 | init_completion(&st->done_down); | |
593 | } | |
594 | ||
595 | static int cpuhp_should_run(unsigned int cpu) | |
596 | { | |
597 | struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); | |
598 | ||
599 | return st->should_run; | |
600 | } | |
601 | ||
602 | /* | |
603 | * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke | |
604 | * callbacks when a state gets [un]installed at runtime. | |
605 | * | |
606 | * Each invocation of this function by the smpboot thread does a single AP | |
607 | * state callback. | |
608 | * | |
609 | * It has 3 modes of operation: | |
610 | * - single: runs st->cb_state | |
611 | * - up: runs ++st->state, while st->state < st->target | |
612 | * - down: runs st->state--, while st->state > st->target | |
613 | * | |
614 | * When complete or on error, should_run is cleared and the completion is fired. | |
615 | */ | |
616 | static void cpuhp_thread_fun(unsigned int cpu) | |
617 | { | |
618 | struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); | |
619 | bool bringup = st->bringup; | |
620 | enum cpuhp_state state; | |
621 | ||
622 | /* | |
623 | * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures | |
624 | * that if we see ->should_run we also see the rest of the state. | |
625 | */ | |
626 | smp_mb(); | |
627 | ||
628 | if (WARN_ON_ONCE(!st->should_run)) | |
629 | return; | |
630 | ||
631 | cpuhp_lock_acquire(bringup); | |
632 | ||
633 | if (st->single) { | |
634 | state = st->cb_state; | |
635 | st->should_run = false; | |
636 | } else { | |
637 | if (bringup) { | |
638 | st->state++; | |
639 | state = st->state; | |
640 | st->should_run = (st->state < st->target); | |
641 | WARN_ON_ONCE(st->state > st->target); | |
642 | } else { | |
643 | state = st->state; | |
644 | st->state--; | |
645 | st->should_run = (st->state > st->target); | |
646 | WARN_ON_ONCE(st->state < st->target); | |
647 | } | |
648 | } | |
649 | ||
650 | WARN_ON_ONCE(!cpuhp_is_ap_state(state)); | |
651 | ||
652 | if (st->rollback) { | |
653 | struct cpuhp_step *step = cpuhp_get_step(state); | |
654 | if (step->skip_onerr) | |
655 | goto next; | |
656 | } | |
657 | ||
658 | if (cpuhp_is_atomic_state(state)) { | |
659 | local_irq_disable(); | |
660 | st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last); | |
661 | local_irq_enable(); | |
662 | ||
663 | /* | |
664 | * STARTING/DYING must not fail! | |
665 | */ | |
666 | WARN_ON_ONCE(st->result); | |
667 | } else { | |
668 | st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last); | |
669 | } | |
670 | ||
671 | if (st->result) { | |
672 | /* | |
673 | * If we fail on a rollback, we're up a creek without no | |
674 | * paddle, no way forward, no way back. We loose, thanks for | |
675 | * playing. | |
676 | */ | |
677 | WARN_ON_ONCE(st->rollback); | |
678 | st->should_run = false; | |
679 | } | |
680 | ||
681 | next: | |
682 | cpuhp_lock_release(bringup); | |
683 | ||
684 | if (!st->should_run) | |
685 | complete_ap_thread(st, bringup); | |
686 | } | |
687 | ||
688 | /* Invoke a single callback on a remote cpu */ | |
689 | static int | |
690 | cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup, | |
691 | struct hlist_node *node) | |
692 | { | |
693 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
694 | int ret; | |
695 | ||
696 | if (!cpu_online(cpu)) | |
697 | return 0; | |
698 | ||
699 | cpuhp_lock_acquire(false); | |
700 | cpuhp_lock_release(false); | |
701 | ||
702 | cpuhp_lock_acquire(true); | |
703 | cpuhp_lock_release(true); | |
704 | ||
705 | /* | |
706 | * If we are up and running, use the hotplug thread. For early calls | |
707 | * we invoke the thread function directly. | |
708 | */ | |
709 | if (!st->thread) | |
710 | return cpuhp_invoke_callback(cpu, state, bringup, node, NULL); | |
711 | ||
712 | st->rollback = false; | |
713 | st->last = NULL; | |
714 | ||
715 | st->node = node; | |
716 | st->bringup = bringup; | |
717 | st->cb_state = state; | |
718 | st->single = true; | |
719 | ||
720 | __cpuhp_kick_ap(st); | |
721 | ||
722 | /* | |
723 | * If we failed and did a partial, do a rollback. | |
724 | */ | |
725 | if ((ret = st->result) && st->last) { | |
726 | st->rollback = true; | |
727 | st->bringup = !bringup; | |
728 | ||
729 | __cpuhp_kick_ap(st); | |
730 | } | |
731 | ||
732 | /* | |
733 | * Clean up the leftovers so the next hotplug operation wont use stale | |
734 | * data. | |
735 | */ | |
736 | st->node = st->last = NULL; | |
737 | return ret; | |
738 | } | |
739 | ||
740 | static int cpuhp_kick_ap_work(unsigned int cpu) | |
741 | { | |
742 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
743 | enum cpuhp_state prev_state = st->state; | |
744 | int ret; | |
745 | ||
746 | cpuhp_lock_acquire(false); | |
747 | cpuhp_lock_release(false); | |
748 | ||
749 | cpuhp_lock_acquire(true); | |
750 | cpuhp_lock_release(true); | |
751 | ||
752 | trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work); | |
753 | ret = cpuhp_kick_ap(st, st->target); | |
754 | trace_cpuhp_exit(cpu, st->state, prev_state, ret); | |
755 | ||
756 | return ret; | |
757 | } | |
758 | ||
759 | static struct smp_hotplug_thread cpuhp_threads = { | |
760 | .store = &cpuhp_state.thread, | |
761 | .create = &cpuhp_create, | |
762 | .thread_should_run = cpuhp_should_run, | |
763 | .thread_fn = cpuhp_thread_fun, | |
764 | .thread_comm = "cpuhp/%u", | |
765 | .selfparking = true, | |
766 | }; | |
767 | ||
768 | void __init cpuhp_threads_init(void) | |
769 | { | |
770 | BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads)); | |
771 | kthread_unpark(this_cpu_read(cpuhp_state.thread)); | |
772 | } | |
773 | ||
774 | #ifdef CONFIG_HOTPLUG_CPU | |
775 | /** | |
776 | * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU | |
777 | * @cpu: a CPU id | |
778 | * | |
779 | * This function walks all processes, finds a valid mm struct for each one and | |
780 | * then clears a corresponding bit in mm's cpumask. While this all sounds | |
781 | * trivial, there are various non-obvious corner cases, which this function | |
782 | * tries to solve in a safe manner. | |
783 | * | |
784 | * Also note that the function uses a somewhat relaxed locking scheme, so it may | |
785 | * be called only for an already offlined CPU. | |
786 | */ | |
787 | void clear_tasks_mm_cpumask(int cpu) | |
788 | { | |
789 | struct task_struct *p; | |
790 | ||
791 | /* | |
792 | * This function is called after the cpu is taken down and marked | |
793 | * offline, so its not like new tasks will ever get this cpu set in | |
794 | * their mm mask. -- Peter Zijlstra | |
795 | * Thus, we may use rcu_read_lock() here, instead of grabbing | |
796 | * full-fledged tasklist_lock. | |
797 | */ | |
798 | WARN_ON(cpu_online(cpu)); | |
799 | rcu_read_lock(); | |
800 | for_each_process(p) { | |
801 | struct task_struct *t; | |
802 | ||
803 | /* | |
804 | * Main thread might exit, but other threads may still have | |
805 | * a valid mm. Find one. | |
806 | */ | |
807 | t = find_lock_task_mm(p); | |
808 | if (!t) | |
809 | continue; | |
810 | cpumask_clear_cpu(cpu, mm_cpumask(t->mm)); | |
811 | task_unlock(t); | |
812 | } | |
813 | rcu_read_unlock(); | |
814 | } | |
815 | ||
816 | /* Take this CPU down. */ | |
817 | static int take_cpu_down(void *_param) | |
818 | { | |
819 | struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); | |
820 | enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE); | |
821 | int err, cpu = smp_processor_id(); | |
822 | int ret; | |
823 | ||
824 | /* Ensure this CPU doesn't handle any more interrupts. */ | |
825 | err = __cpu_disable(); | |
826 | if (err < 0) | |
827 | return err; | |
828 | ||
829 | /* | |
830 | * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not | |
831 | * do this step again. | |
832 | */ | |
833 | WARN_ON(st->state != CPUHP_TEARDOWN_CPU); | |
834 | st->state--; | |
835 | /* Invoke the former CPU_DYING callbacks */ | |
836 | for (; st->state > target; st->state--) { | |
837 | ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL); | |
838 | /* | |
839 | * DYING must not fail! | |
840 | */ | |
841 | WARN_ON_ONCE(ret); | |
842 | } | |
843 | ||
844 | /* Give up timekeeping duties */ | |
845 | tick_handover_do_timer(); | |
846 | /* Park the stopper thread */ | |
847 | stop_machine_park(cpu); | |
848 | return 0; | |
849 | } | |
850 | ||
851 | static int takedown_cpu(unsigned int cpu) | |
852 | { | |
853 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
854 | int err; | |
855 | ||
856 | /* Park the smpboot threads */ | |
857 | kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread); | |
858 | ||
859 | /* | |
860 | * Prevent irq alloc/free while the dying cpu reorganizes the | |
861 | * interrupt affinities. | |
862 | */ | |
863 | irq_lock_sparse(); | |
864 | ||
865 | /* | |
866 | * So now all preempt/rcu users must observe !cpu_active(). | |
867 | */ | |
868 | err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu)); | |
869 | if (err) { | |
870 | /* CPU refused to die */ | |
871 | irq_unlock_sparse(); | |
872 | /* Unpark the hotplug thread so we can rollback there */ | |
873 | kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread); | |
874 | return err; | |
875 | } | |
876 | BUG_ON(cpu_online(cpu)); | |
877 | ||
878 | /* | |
879 | * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed | |
880 | * all runnable tasks from the CPU, there's only the idle task left now | |
881 | * that the migration thread is done doing the stop_machine thing. | |
882 | * | |
883 | * Wait for the stop thread to go away. | |
884 | */ | |
885 | wait_for_ap_thread(st, false); | |
886 | BUG_ON(st->state != CPUHP_AP_IDLE_DEAD); | |
887 | ||
888 | /* Interrupts are moved away from the dying cpu, reenable alloc/free */ | |
889 | irq_unlock_sparse(); | |
890 | ||
891 | hotplug_cpu__broadcast_tick_pull(cpu); | |
892 | /* This actually kills the CPU. */ | |
893 | __cpu_die(cpu); | |
894 | ||
895 | tick_cleanup_dead_cpu(cpu); | |
896 | rcutree_migrate_callbacks(cpu); | |
897 | return 0; | |
898 | } | |
899 | ||
900 | static void cpuhp_complete_idle_dead(void *arg) | |
901 | { | |
902 | struct cpuhp_cpu_state *st = arg; | |
903 | ||
904 | complete_ap_thread(st, false); | |
905 | } | |
906 | ||
907 | void cpuhp_report_idle_dead(void) | |
908 | { | |
909 | struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); | |
910 | ||
911 | BUG_ON(st->state != CPUHP_AP_OFFLINE); | |
912 | rcu_report_dead(smp_processor_id()); | |
913 | st->state = CPUHP_AP_IDLE_DEAD; | |
914 | /* | |
915 | * We cannot call complete after rcu_report_dead() so we delegate it | |
916 | * to an online cpu. | |
917 | */ | |
918 | smp_call_function_single(cpumask_first(cpu_online_mask), | |
919 | cpuhp_complete_idle_dead, st, 0); | |
920 | } | |
921 | ||
922 | static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st) | |
923 | { | |
924 | for (st->state++; st->state < st->target; st->state++) { | |
925 | struct cpuhp_step *step = cpuhp_get_step(st->state); | |
926 | ||
927 | if (!step->skip_onerr) | |
928 | cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL); | |
929 | } | |
930 | } | |
931 | ||
932 | static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st, | |
933 | enum cpuhp_state target) | |
934 | { | |
935 | enum cpuhp_state prev_state = st->state; | |
936 | int ret = 0; | |
937 | ||
938 | for (; st->state > target; st->state--) { | |
939 | ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL); | |
940 | if (ret) { | |
941 | st->target = prev_state; | |
942 | undo_cpu_down(cpu, st); | |
943 | break; | |
944 | } | |
945 | } | |
946 | return ret; | |
947 | } | |
948 | ||
949 | /* Requires cpu_add_remove_lock to be held */ | |
950 | static int __ref _cpu_down(unsigned int cpu, int tasks_frozen, | |
951 | enum cpuhp_state target) | |
952 | { | |
953 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
954 | int prev_state, ret = 0; | |
955 | ||
956 | if (num_online_cpus() == 1) | |
957 | return -EBUSY; | |
958 | ||
959 | if (!cpu_present(cpu)) | |
960 | return -EINVAL; | |
961 | ||
962 | cpus_write_lock(); | |
963 | ||
964 | cpuhp_tasks_frozen = tasks_frozen; | |
965 | ||
966 | prev_state = cpuhp_set_state(st, target); | |
967 | /* | |
968 | * If the current CPU state is in the range of the AP hotplug thread, | |
969 | * then we need to kick the thread. | |
970 | */ | |
971 | if (st->state > CPUHP_TEARDOWN_CPU) { | |
972 | st->target = max((int)target, CPUHP_TEARDOWN_CPU); | |
973 | ret = cpuhp_kick_ap_work(cpu); | |
974 | /* | |
975 | * The AP side has done the error rollback already. Just | |
976 | * return the error code.. | |
977 | */ | |
978 | if (ret) | |
979 | goto out; | |
980 | ||
981 | /* | |
982 | * We might have stopped still in the range of the AP hotplug | |
983 | * thread. Nothing to do anymore. | |
984 | */ | |
985 | if (st->state > CPUHP_TEARDOWN_CPU) | |
986 | goto out; | |
987 | ||
988 | st->target = target; | |
989 | } | |
990 | /* | |
991 | * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need | |
992 | * to do the further cleanups. | |
993 | */ | |
994 | ret = cpuhp_down_callbacks(cpu, st, target); | |
995 | if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) { | |
996 | cpuhp_reset_state(st, prev_state); | |
997 | __cpuhp_kick_ap(st); | |
998 | } | |
999 | ||
1000 | out: | |
1001 | cpus_write_unlock(); | |
1002 | /* | |
1003 | * Do post unplug cleanup. This is still protected against | |
1004 | * concurrent CPU hotplug via cpu_add_remove_lock. | |
1005 | */ | |
1006 | lockup_detector_cleanup(); | |
1007 | arch_smt_update(); | |
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target) | |
1012 | { | |
1013 | if (cpu_hotplug_disabled) | |
1014 | return -EBUSY; | |
1015 | return _cpu_down(cpu, 0, target); | |
1016 | } | |
1017 | ||
1018 | static int do_cpu_down(unsigned int cpu, enum cpuhp_state target) | |
1019 | { | |
1020 | int err; | |
1021 | ||
1022 | cpu_maps_update_begin(); | |
1023 | err = cpu_down_maps_locked(cpu, target); | |
1024 | cpu_maps_update_done(); | |
1025 | return err; | |
1026 | } | |
1027 | ||
1028 | int cpu_down(unsigned int cpu) | |
1029 | { | |
1030 | return do_cpu_down(cpu, CPUHP_OFFLINE); | |
1031 | } | |
1032 | EXPORT_SYMBOL(cpu_down); | |
1033 | ||
1034 | #else | |
1035 | #define takedown_cpu NULL | |
1036 | #endif /*CONFIG_HOTPLUG_CPU*/ | |
1037 | ||
1038 | /** | |
1039 | * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU | |
1040 | * @cpu: cpu that just started | |
1041 | * | |
1042 | * It must be called by the arch code on the new cpu, before the new cpu | |
1043 | * enables interrupts and before the "boot" cpu returns from __cpu_up(). | |
1044 | */ | |
1045 | void notify_cpu_starting(unsigned int cpu) | |
1046 | { | |
1047 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1048 | enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE); | |
1049 | int ret; | |
1050 | ||
1051 | rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */ | |
1052 | st->booted_once = true; | |
1053 | while (st->state < target) { | |
1054 | st->state++; | |
1055 | ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL); | |
1056 | /* | |
1057 | * STARTING must not fail! | |
1058 | */ | |
1059 | WARN_ON_ONCE(ret); | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | /* | |
1064 | * Called from the idle task. Wake up the controlling task which brings the | |
1065 | * stopper and the hotplug thread of the upcoming CPU up and then delegates | |
1066 | * the rest of the online bringup to the hotplug thread. | |
1067 | */ | |
1068 | void cpuhp_online_idle(enum cpuhp_state state) | |
1069 | { | |
1070 | struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); | |
1071 | ||
1072 | /* Happens for the boot cpu */ | |
1073 | if (state != CPUHP_AP_ONLINE_IDLE) | |
1074 | return; | |
1075 | ||
1076 | st->state = CPUHP_AP_ONLINE_IDLE; | |
1077 | complete_ap_thread(st, true); | |
1078 | } | |
1079 | ||
1080 | /* Requires cpu_add_remove_lock to be held */ | |
1081 | static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target) | |
1082 | { | |
1083 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1084 | struct task_struct *idle; | |
1085 | int ret = 0; | |
1086 | ||
1087 | cpus_write_lock(); | |
1088 | ||
1089 | if (!cpu_present(cpu)) { | |
1090 | ret = -EINVAL; | |
1091 | goto out; | |
1092 | } | |
1093 | ||
1094 | /* | |
1095 | * The caller of do_cpu_up might have raced with another | |
1096 | * caller. Ignore it for now. | |
1097 | */ | |
1098 | if (st->state >= target) | |
1099 | goto out; | |
1100 | ||
1101 | if (st->state == CPUHP_OFFLINE) { | |
1102 | /* Let it fail before we try to bring the cpu up */ | |
1103 | idle = idle_thread_get(cpu); | |
1104 | if (IS_ERR(idle)) { | |
1105 | ret = PTR_ERR(idle); | |
1106 | goto out; | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | cpuhp_tasks_frozen = tasks_frozen; | |
1111 | ||
1112 | cpuhp_set_state(st, target); | |
1113 | /* | |
1114 | * If the current CPU state is in the range of the AP hotplug thread, | |
1115 | * then we need to kick the thread once more. | |
1116 | */ | |
1117 | if (st->state > CPUHP_BRINGUP_CPU) { | |
1118 | ret = cpuhp_kick_ap_work(cpu); | |
1119 | /* | |
1120 | * The AP side has done the error rollback already. Just | |
1121 | * return the error code.. | |
1122 | */ | |
1123 | if (ret) | |
1124 | goto out; | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * Try to reach the target state. We max out on the BP at | |
1129 | * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is | |
1130 | * responsible for bringing it up to the target state. | |
1131 | */ | |
1132 | target = min((int)target, CPUHP_BRINGUP_CPU); | |
1133 | ret = cpuhp_up_callbacks(cpu, st, target); | |
1134 | out: | |
1135 | cpus_write_unlock(); | |
1136 | arch_smt_update(); | |
1137 | return ret; | |
1138 | } | |
1139 | ||
1140 | static int do_cpu_up(unsigned int cpu, enum cpuhp_state target) | |
1141 | { | |
1142 | int err = 0; | |
1143 | ||
1144 | if (!cpu_possible(cpu)) { | |
1145 | pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n", | |
1146 | cpu); | |
1147 | #if defined(CONFIG_IA64) | |
1148 | pr_err("please check additional_cpus= boot parameter\n"); | |
1149 | #endif | |
1150 | return -EINVAL; | |
1151 | } | |
1152 | ||
1153 | err = try_online_node(cpu_to_node(cpu)); | |
1154 | if (err) | |
1155 | return err; | |
1156 | ||
1157 | cpu_maps_update_begin(); | |
1158 | ||
1159 | if (cpu_hotplug_disabled) { | |
1160 | err = -EBUSY; | |
1161 | goto out; | |
1162 | } | |
1163 | if (!cpu_smt_allowed(cpu)) { | |
1164 | err = -EPERM; | |
1165 | goto out; | |
1166 | } | |
1167 | ||
1168 | err = _cpu_up(cpu, 0, target); | |
1169 | out: | |
1170 | cpu_maps_update_done(); | |
1171 | return err; | |
1172 | } | |
1173 | ||
1174 | int cpu_up(unsigned int cpu) | |
1175 | { | |
1176 | return do_cpu_up(cpu, CPUHP_ONLINE); | |
1177 | } | |
1178 | EXPORT_SYMBOL_GPL(cpu_up); | |
1179 | ||
1180 | #ifdef CONFIG_PM_SLEEP_SMP | |
1181 | static cpumask_var_t frozen_cpus; | |
1182 | ||
1183 | int freeze_secondary_cpus(int primary) | |
1184 | { | |
1185 | int cpu, error = 0; | |
1186 | ||
1187 | cpu_maps_update_begin(); | |
1188 | if (!cpu_online(primary)) | |
1189 | primary = cpumask_first(cpu_online_mask); | |
1190 | /* | |
1191 | * We take down all of the non-boot CPUs in one shot to avoid races | |
1192 | * with the userspace trying to use the CPU hotplug at the same time | |
1193 | */ | |
1194 | cpumask_clear(frozen_cpus); | |
1195 | ||
1196 | pr_info("Disabling non-boot CPUs ...\n"); | |
1197 | for_each_online_cpu(cpu) { | |
1198 | if (cpu == primary) | |
1199 | continue; | |
1200 | trace_suspend_resume(TPS("CPU_OFF"), cpu, true); | |
1201 | error = _cpu_down(cpu, 1, CPUHP_OFFLINE); | |
1202 | trace_suspend_resume(TPS("CPU_OFF"), cpu, false); | |
1203 | if (!error) | |
1204 | cpumask_set_cpu(cpu, frozen_cpus); | |
1205 | else { | |
1206 | pr_err("Error taking CPU%d down: %d\n", cpu, error); | |
1207 | break; | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | if (!error) | |
1212 | BUG_ON(num_online_cpus() > 1); | |
1213 | else | |
1214 | pr_err("Non-boot CPUs are not disabled\n"); | |
1215 | ||
1216 | /* | |
1217 | * Make sure the CPUs won't be enabled by someone else. We need to do | |
1218 | * this even in case of failure as all disable_nonboot_cpus() users are | |
1219 | * supposed to do enable_nonboot_cpus() on the failure path. | |
1220 | */ | |
1221 | cpu_hotplug_disabled++; | |
1222 | ||
1223 | cpu_maps_update_done(); | |
1224 | return error; | |
1225 | } | |
1226 | ||
1227 | void __weak arch_enable_nonboot_cpus_begin(void) | |
1228 | { | |
1229 | } | |
1230 | ||
1231 | void __weak arch_enable_nonboot_cpus_end(void) | |
1232 | { | |
1233 | } | |
1234 | ||
1235 | void enable_nonboot_cpus(void) | |
1236 | { | |
1237 | int cpu, error; | |
1238 | ||
1239 | /* Allow everyone to use the CPU hotplug again */ | |
1240 | cpu_maps_update_begin(); | |
1241 | __cpu_hotplug_enable(); | |
1242 | if (cpumask_empty(frozen_cpus)) | |
1243 | goto out; | |
1244 | ||
1245 | pr_info("Enabling non-boot CPUs ...\n"); | |
1246 | ||
1247 | arch_enable_nonboot_cpus_begin(); | |
1248 | ||
1249 | for_each_cpu(cpu, frozen_cpus) { | |
1250 | trace_suspend_resume(TPS("CPU_ON"), cpu, true); | |
1251 | error = _cpu_up(cpu, 1, CPUHP_ONLINE); | |
1252 | trace_suspend_resume(TPS("CPU_ON"), cpu, false); | |
1253 | if (!error) { | |
1254 | pr_info("CPU%d is up\n", cpu); | |
1255 | continue; | |
1256 | } | |
1257 | pr_warn("Error taking CPU%d up: %d\n", cpu, error); | |
1258 | } | |
1259 | ||
1260 | arch_enable_nonboot_cpus_end(); | |
1261 | ||
1262 | cpumask_clear(frozen_cpus); | |
1263 | out: | |
1264 | cpu_maps_update_done(); | |
1265 | } | |
1266 | ||
1267 | static int __init alloc_frozen_cpus(void) | |
1268 | { | |
1269 | if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO)) | |
1270 | return -ENOMEM; | |
1271 | return 0; | |
1272 | } | |
1273 | core_initcall(alloc_frozen_cpus); | |
1274 | ||
1275 | /* | |
1276 | * When callbacks for CPU hotplug notifications are being executed, we must | |
1277 | * ensure that the state of the system with respect to the tasks being frozen | |
1278 | * or not, as reported by the notification, remains unchanged *throughout the | |
1279 | * duration* of the execution of the callbacks. | |
1280 | * Hence we need to prevent the freezer from racing with regular CPU hotplug. | |
1281 | * | |
1282 | * This synchronization is implemented by mutually excluding regular CPU | |
1283 | * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/ | |
1284 | * Hibernate notifications. | |
1285 | */ | |
1286 | static int | |
1287 | cpu_hotplug_pm_callback(struct notifier_block *nb, | |
1288 | unsigned long action, void *ptr) | |
1289 | { | |
1290 | switch (action) { | |
1291 | ||
1292 | case PM_SUSPEND_PREPARE: | |
1293 | case PM_HIBERNATION_PREPARE: | |
1294 | cpu_hotplug_disable(); | |
1295 | break; | |
1296 | ||
1297 | case PM_POST_SUSPEND: | |
1298 | case PM_POST_HIBERNATION: | |
1299 | cpu_hotplug_enable(); | |
1300 | break; | |
1301 | ||
1302 | default: | |
1303 | return NOTIFY_DONE; | |
1304 | } | |
1305 | ||
1306 | return NOTIFY_OK; | |
1307 | } | |
1308 | ||
1309 | ||
1310 | static int __init cpu_hotplug_pm_sync_init(void) | |
1311 | { | |
1312 | /* | |
1313 | * cpu_hotplug_pm_callback has higher priority than x86 | |
1314 | * bsp_pm_callback which depends on cpu_hotplug_pm_callback | |
1315 | * to disable cpu hotplug to avoid cpu hotplug race. | |
1316 | */ | |
1317 | pm_notifier(cpu_hotplug_pm_callback, 0); | |
1318 | return 0; | |
1319 | } | |
1320 | core_initcall(cpu_hotplug_pm_sync_init); | |
1321 | ||
1322 | #endif /* CONFIG_PM_SLEEP_SMP */ | |
1323 | ||
1324 | int __boot_cpu_id; | |
1325 | ||
1326 | #endif /* CONFIG_SMP */ | |
1327 | ||
1328 | /* Boot processor state steps */ | |
1329 | static struct cpuhp_step cpuhp_bp_states[] = { | |
1330 | [CPUHP_OFFLINE] = { | |
1331 | .name = "offline", | |
1332 | .startup.single = NULL, | |
1333 | .teardown.single = NULL, | |
1334 | }, | |
1335 | #ifdef CONFIG_SMP | |
1336 | [CPUHP_CREATE_THREADS]= { | |
1337 | .name = "threads:prepare", | |
1338 | .startup.single = smpboot_create_threads, | |
1339 | .teardown.single = NULL, | |
1340 | .cant_stop = true, | |
1341 | }, | |
1342 | [CPUHP_PERF_PREPARE] = { | |
1343 | .name = "perf:prepare", | |
1344 | .startup.single = perf_event_init_cpu, | |
1345 | .teardown.single = perf_event_exit_cpu, | |
1346 | }, | |
1347 | [CPUHP_WORKQUEUE_PREP] = { | |
1348 | .name = "workqueue:prepare", | |
1349 | .startup.single = workqueue_prepare_cpu, | |
1350 | .teardown.single = NULL, | |
1351 | }, | |
1352 | [CPUHP_HRTIMERS_PREPARE] = { | |
1353 | .name = "hrtimers:prepare", | |
1354 | .startup.single = hrtimers_prepare_cpu, | |
1355 | .teardown.single = hrtimers_dead_cpu, | |
1356 | }, | |
1357 | [CPUHP_SMPCFD_PREPARE] = { | |
1358 | .name = "smpcfd:prepare", | |
1359 | .startup.single = smpcfd_prepare_cpu, | |
1360 | .teardown.single = smpcfd_dead_cpu, | |
1361 | }, | |
1362 | [CPUHP_RELAY_PREPARE] = { | |
1363 | .name = "relay:prepare", | |
1364 | .startup.single = relay_prepare_cpu, | |
1365 | .teardown.single = NULL, | |
1366 | }, | |
1367 | [CPUHP_SLAB_PREPARE] = { | |
1368 | .name = "slab:prepare", | |
1369 | .startup.single = slab_prepare_cpu, | |
1370 | .teardown.single = slab_dead_cpu, | |
1371 | }, | |
1372 | [CPUHP_RCUTREE_PREP] = { | |
1373 | .name = "RCU/tree:prepare", | |
1374 | .startup.single = rcutree_prepare_cpu, | |
1375 | .teardown.single = rcutree_dead_cpu, | |
1376 | }, | |
1377 | /* | |
1378 | * On the tear-down path, timers_dead_cpu() must be invoked | |
1379 | * before blk_mq_queue_reinit_notify() from notify_dead(), | |
1380 | * otherwise a RCU stall occurs. | |
1381 | */ | |
1382 | [CPUHP_TIMERS_PREPARE] = { | |
1383 | .name = "timers:dead", | |
1384 | .startup.single = timers_prepare_cpu, | |
1385 | .teardown.single = timers_dead_cpu, | |
1386 | }, | |
1387 | /* Kicks the plugged cpu into life */ | |
1388 | [CPUHP_BRINGUP_CPU] = { | |
1389 | .name = "cpu:bringup", | |
1390 | .startup.single = bringup_cpu, | |
1391 | .teardown.single = NULL, | |
1392 | .cant_stop = true, | |
1393 | }, | |
1394 | /* | |
1395 | * Handled on controll processor until the plugged processor manages | |
1396 | * this itself. | |
1397 | */ | |
1398 | [CPUHP_TEARDOWN_CPU] = { | |
1399 | .name = "cpu:teardown", | |
1400 | .startup.single = NULL, | |
1401 | .teardown.single = takedown_cpu, | |
1402 | .cant_stop = true, | |
1403 | }, | |
1404 | #else | |
1405 | [CPUHP_BRINGUP_CPU] = { }, | |
1406 | #endif | |
1407 | }; | |
1408 | ||
1409 | /* Application processor state steps */ | |
1410 | static struct cpuhp_step cpuhp_ap_states[] = { | |
1411 | #ifdef CONFIG_SMP | |
1412 | /* Final state before CPU kills itself */ | |
1413 | [CPUHP_AP_IDLE_DEAD] = { | |
1414 | .name = "idle:dead", | |
1415 | }, | |
1416 | /* | |
1417 | * Last state before CPU enters the idle loop to die. Transient state | |
1418 | * for synchronization. | |
1419 | */ | |
1420 | [CPUHP_AP_OFFLINE] = { | |
1421 | .name = "ap:offline", | |
1422 | .cant_stop = true, | |
1423 | }, | |
1424 | /* First state is scheduler control. Interrupts are disabled */ | |
1425 | [CPUHP_AP_SCHED_STARTING] = { | |
1426 | .name = "sched:starting", | |
1427 | .startup.single = sched_cpu_starting, | |
1428 | .teardown.single = sched_cpu_dying, | |
1429 | }, | |
1430 | [CPUHP_AP_RCUTREE_DYING] = { | |
1431 | .name = "RCU/tree:dying", | |
1432 | .startup.single = NULL, | |
1433 | .teardown.single = rcutree_dying_cpu, | |
1434 | }, | |
1435 | [CPUHP_AP_SMPCFD_DYING] = { | |
1436 | .name = "smpcfd:dying", | |
1437 | .startup.single = NULL, | |
1438 | .teardown.single = smpcfd_dying_cpu, | |
1439 | }, | |
1440 | /* Entry state on starting. Interrupts enabled from here on. Transient | |
1441 | * state for synchronsization */ | |
1442 | [CPUHP_AP_ONLINE] = { | |
1443 | .name = "ap:online", | |
1444 | }, | |
1445 | /* Handle smpboot threads park/unpark */ | |
1446 | [CPUHP_AP_SMPBOOT_THREADS] = { | |
1447 | .name = "smpboot/threads:online", | |
1448 | .startup.single = smpboot_unpark_threads, | |
1449 | .teardown.single = smpboot_park_threads, | |
1450 | }, | |
1451 | [CPUHP_AP_IRQ_AFFINITY_ONLINE] = { | |
1452 | .name = "irq/affinity:online", | |
1453 | .startup.single = irq_affinity_online_cpu, | |
1454 | .teardown.single = NULL, | |
1455 | }, | |
1456 | [CPUHP_AP_PERF_ONLINE] = { | |
1457 | .name = "perf:online", | |
1458 | .startup.single = perf_event_init_cpu, | |
1459 | .teardown.single = perf_event_exit_cpu, | |
1460 | }, | |
1461 | [CPUHP_AP_WORKQUEUE_ONLINE] = { | |
1462 | .name = "workqueue:online", | |
1463 | .startup.single = workqueue_online_cpu, | |
1464 | .teardown.single = workqueue_offline_cpu, | |
1465 | }, | |
1466 | [CPUHP_AP_RCUTREE_ONLINE] = { | |
1467 | .name = "RCU/tree:online", | |
1468 | .startup.single = rcutree_online_cpu, | |
1469 | .teardown.single = rcutree_offline_cpu, | |
1470 | }, | |
1471 | #endif | |
1472 | /* | |
1473 | * The dynamically registered state space is here | |
1474 | */ | |
1475 | ||
1476 | #ifdef CONFIG_SMP | |
1477 | /* Last state is scheduler control setting the cpu active */ | |
1478 | [CPUHP_AP_ACTIVE] = { | |
1479 | .name = "sched:active", | |
1480 | .startup.single = sched_cpu_activate, | |
1481 | .teardown.single = sched_cpu_deactivate, | |
1482 | }, | |
1483 | #endif | |
1484 | ||
1485 | /* CPU is fully up and running. */ | |
1486 | [CPUHP_ONLINE] = { | |
1487 | .name = "online", | |
1488 | .startup.single = NULL, | |
1489 | .teardown.single = NULL, | |
1490 | }, | |
1491 | }; | |
1492 | ||
1493 | /* Sanity check for callbacks */ | |
1494 | static int cpuhp_cb_check(enum cpuhp_state state) | |
1495 | { | |
1496 | if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE) | |
1497 | return -EINVAL; | |
1498 | return 0; | |
1499 | } | |
1500 | ||
1501 | /* | |
1502 | * Returns a free for dynamic slot assignment of the Online state. The states | |
1503 | * are protected by the cpuhp_slot_states mutex and an empty slot is identified | |
1504 | * by having no name assigned. | |
1505 | */ | |
1506 | static int cpuhp_reserve_state(enum cpuhp_state state) | |
1507 | { | |
1508 | enum cpuhp_state i, end; | |
1509 | struct cpuhp_step *step; | |
1510 | ||
1511 | switch (state) { | |
1512 | case CPUHP_AP_ONLINE_DYN: | |
1513 | step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN; | |
1514 | end = CPUHP_AP_ONLINE_DYN_END; | |
1515 | break; | |
1516 | case CPUHP_BP_PREPARE_DYN: | |
1517 | step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN; | |
1518 | end = CPUHP_BP_PREPARE_DYN_END; | |
1519 | break; | |
1520 | default: | |
1521 | return -EINVAL; | |
1522 | } | |
1523 | ||
1524 | for (i = state; i <= end; i++, step++) { | |
1525 | if (!step->name) | |
1526 | return i; | |
1527 | } | |
1528 | WARN(1, "No more dynamic states available for CPU hotplug\n"); | |
1529 | return -ENOSPC; | |
1530 | } | |
1531 | ||
1532 | static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name, | |
1533 | int (*startup)(unsigned int cpu), | |
1534 | int (*teardown)(unsigned int cpu), | |
1535 | bool multi_instance) | |
1536 | { | |
1537 | /* (Un)Install the callbacks for further cpu hotplug operations */ | |
1538 | struct cpuhp_step *sp; | |
1539 | int ret = 0; | |
1540 | ||
1541 | /* | |
1542 | * If name is NULL, then the state gets removed. | |
1543 | * | |
1544 | * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on | |
1545 | * the first allocation from these dynamic ranges, so the removal | |
1546 | * would trigger a new allocation and clear the wrong (already | |
1547 | * empty) state, leaving the callbacks of the to be cleared state | |
1548 | * dangling, which causes wreckage on the next hotplug operation. | |
1549 | */ | |
1550 | if (name && (state == CPUHP_AP_ONLINE_DYN || | |
1551 | state == CPUHP_BP_PREPARE_DYN)) { | |
1552 | ret = cpuhp_reserve_state(state); | |
1553 | if (ret < 0) | |
1554 | return ret; | |
1555 | state = ret; | |
1556 | } | |
1557 | sp = cpuhp_get_step(state); | |
1558 | if (name && sp->name) | |
1559 | return -EBUSY; | |
1560 | ||
1561 | sp->startup.single = startup; | |
1562 | sp->teardown.single = teardown; | |
1563 | sp->name = name; | |
1564 | sp->multi_instance = multi_instance; | |
1565 | INIT_HLIST_HEAD(&sp->list); | |
1566 | return ret; | |
1567 | } | |
1568 | ||
1569 | static void *cpuhp_get_teardown_cb(enum cpuhp_state state) | |
1570 | { | |
1571 | return cpuhp_get_step(state)->teardown.single; | |
1572 | } | |
1573 | ||
1574 | /* | |
1575 | * Call the startup/teardown function for a step either on the AP or | |
1576 | * on the current CPU. | |
1577 | */ | |
1578 | static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup, | |
1579 | struct hlist_node *node) | |
1580 | { | |
1581 | struct cpuhp_step *sp = cpuhp_get_step(state); | |
1582 | int ret; | |
1583 | ||
1584 | /* | |
1585 | * If there's nothing to do, we done. | |
1586 | * Relies on the union for multi_instance. | |
1587 | */ | |
1588 | if ((bringup && !sp->startup.single) || | |
1589 | (!bringup && !sp->teardown.single)) | |
1590 | return 0; | |
1591 | /* | |
1592 | * The non AP bound callbacks can fail on bringup. On teardown | |
1593 | * e.g. module removal we crash for now. | |
1594 | */ | |
1595 | #ifdef CONFIG_SMP | |
1596 | if (cpuhp_is_ap_state(state)) | |
1597 | ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node); | |
1598 | else | |
1599 | ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL); | |
1600 | #else | |
1601 | ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL); | |
1602 | #endif | |
1603 | BUG_ON(ret && !bringup); | |
1604 | return ret; | |
1605 | } | |
1606 | ||
1607 | /* | |
1608 | * Called from __cpuhp_setup_state on a recoverable failure. | |
1609 | * | |
1610 | * Note: The teardown callbacks for rollback are not allowed to fail! | |
1611 | */ | |
1612 | static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state, | |
1613 | struct hlist_node *node) | |
1614 | { | |
1615 | int cpu; | |
1616 | ||
1617 | /* Roll back the already executed steps on the other cpus */ | |
1618 | for_each_present_cpu(cpu) { | |
1619 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1620 | int cpustate = st->state; | |
1621 | ||
1622 | if (cpu >= failedcpu) | |
1623 | break; | |
1624 | ||
1625 | /* Did we invoke the startup call on that cpu ? */ | |
1626 | if (cpustate >= state) | |
1627 | cpuhp_issue_call(cpu, state, false, node); | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state, | |
1632 | struct hlist_node *node, | |
1633 | bool invoke) | |
1634 | { | |
1635 | struct cpuhp_step *sp; | |
1636 | int cpu; | |
1637 | int ret; | |
1638 | ||
1639 | lockdep_assert_cpus_held(); | |
1640 | ||
1641 | sp = cpuhp_get_step(state); | |
1642 | if (sp->multi_instance == false) | |
1643 | return -EINVAL; | |
1644 | ||
1645 | mutex_lock(&cpuhp_state_mutex); | |
1646 | ||
1647 | if (!invoke || !sp->startup.multi) | |
1648 | goto add_node; | |
1649 | ||
1650 | /* | |
1651 | * Try to call the startup callback for each present cpu | |
1652 | * depending on the hotplug state of the cpu. | |
1653 | */ | |
1654 | for_each_present_cpu(cpu) { | |
1655 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1656 | int cpustate = st->state; | |
1657 | ||
1658 | if (cpustate < state) | |
1659 | continue; | |
1660 | ||
1661 | ret = cpuhp_issue_call(cpu, state, true, node); | |
1662 | if (ret) { | |
1663 | if (sp->teardown.multi) | |
1664 | cpuhp_rollback_install(cpu, state, node); | |
1665 | goto unlock; | |
1666 | } | |
1667 | } | |
1668 | add_node: | |
1669 | ret = 0; | |
1670 | hlist_add_head(node, &sp->list); | |
1671 | unlock: | |
1672 | mutex_unlock(&cpuhp_state_mutex); | |
1673 | return ret; | |
1674 | } | |
1675 | ||
1676 | int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node, | |
1677 | bool invoke) | |
1678 | { | |
1679 | int ret; | |
1680 | ||
1681 | cpus_read_lock(); | |
1682 | ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke); | |
1683 | cpus_read_unlock(); | |
1684 | return ret; | |
1685 | } | |
1686 | EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance); | |
1687 | ||
1688 | /** | |
1689 | * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state | |
1690 | * @state: The state to setup | |
1691 | * @invoke: If true, the startup function is invoked for cpus where | |
1692 | * cpu state >= @state | |
1693 | * @startup: startup callback function | |
1694 | * @teardown: teardown callback function | |
1695 | * @multi_instance: State is set up for multiple instances which get | |
1696 | * added afterwards. | |
1697 | * | |
1698 | * The caller needs to hold cpus read locked while calling this function. | |
1699 | * Returns: | |
1700 | * On success: | |
1701 | * Positive state number if @state is CPUHP_AP_ONLINE_DYN | |
1702 | * 0 for all other states | |
1703 | * On failure: proper (negative) error code | |
1704 | */ | |
1705 | int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state, | |
1706 | const char *name, bool invoke, | |
1707 | int (*startup)(unsigned int cpu), | |
1708 | int (*teardown)(unsigned int cpu), | |
1709 | bool multi_instance) | |
1710 | { | |
1711 | int cpu, ret = 0; | |
1712 | bool dynstate; | |
1713 | ||
1714 | lockdep_assert_cpus_held(); | |
1715 | ||
1716 | if (cpuhp_cb_check(state) || !name) | |
1717 | return -EINVAL; | |
1718 | ||
1719 | mutex_lock(&cpuhp_state_mutex); | |
1720 | ||
1721 | ret = cpuhp_store_callbacks(state, name, startup, teardown, | |
1722 | multi_instance); | |
1723 | ||
1724 | dynstate = state == CPUHP_AP_ONLINE_DYN; | |
1725 | if (ret > 0 && dynstate) { | |
1726 | state = ret; | |
1727 | ret = 0; | |
1728 | } | |
1729 | ||
1730 | if (ret || !invoke || !startup) | |
1731 | goto out; | |
1732 | ||
1733 | /* | |
1734 | * Try to call the startup callback for each present cpu | |
1735 | * depending on the hotplug state of the cpu. | |
1736 | */ | |
1737 | for_each_present_cpu(cpu) { | |
1738 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1739 | int cpustate = st->state; | |
1740 | ||
1741 | if (cpustate < state) | |
1742 | continue; | |
1743 | ||
1744 | ret = cpuhp_issue_call(cpu, state, true, NULL); | |
1745 | if (ret) { | |
1746 | if (teardown) | |
1747 | cpuhp_rollback_install(cpu, state, NULL); | |
1748 | cpuhp_store_callbacks(state, NULL, NULL, NULL, false); | |
1749 | goto out; | |
1750 | } | |
1751 | } | |
1752 | out: | |
1753 | mutex_unlock(&cpuhp_state_mutex); | |
1754 | /* | |
1755 | * If the requested state is CPUHP_AP_ONLINE_DYN, return the | |
1756 | * dynamically allocated state in case of success. | |
1757 | */ | |
1758 | if (!ret && dynstate) | |
1759 | return state; | |
1760 | return ret; | |
1761 | } | |
1762 | EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked); | |
1763 | ||
1764 | int __cpuhp_setup_state(enum cpuhp_state state, | |
1765 | const char *name, bool invoke, | |
1766 | int (*startup)(unsigned int cpu), | |
1767 | int (*teardown)(unsigned int cpu), | |
1768 | bool multi_instance) | |
1769 | { | |
1770 | int ret; | |
1771 | ||
1772 | cpus_read_lock(); | |
1773 | ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup, | |
1774 | teardown, multi_instance); | |
1775 | cpus_read_unlock(); | |
1776 | return ret; | |
1777 | } | |
1778 | EXPORT_SYMBOL(__cpuhp_setup_state); | |
1779 | ||
1780 | int __cpuhp_state_remove_instance(enum cpuhp_state state, | |
1781 | struct hlist_node *node, bool invoke) | |
1782 | { | |
1783 | struct cpuhp_step *sp = cpuhp_get_step(state); | |
1784 | int cpu; | |
1785 | ||
1786 | BUG_ON(cpuhp_cb_check(state)); | |
1787 | ||
1788 | if (!sp->multi_instance) | |
1789 | return -EINVAL; | |
1790 | ||
1791 | cpus_read_lock(); | |
1792 | mutex_lock(&cpuhp_state_mutex); | |
1793 | ||
1794 | if (!invoke || !cpuhp_get_teardown_cb(state)) | |
1795 | goto remove; | |
1796 | /* | |
1797 | * Call the teardown callback for each present cpu depending | |
1798 | * on the hotplug state of the cpu. This function is not | |
1799 | * allowed to fail currently! | |
1800 | */ | |
1801 | for_each_present_cpu(cpu) { | |
1802 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1803 | int cpustate = st->state; | |
1804 | ||
1805 | if (cpustate >= state) | |
1806 | cpuhp_issue_call(cpu, state, false, node); | |
1807 | } | |
1808 | ||
1809 | remove: | |
1810 | hlist_del(node); | |
1811 | mutex_unlock(&cpuhp_state_mutex); | |
1812 | cpus_read_unlock(); | |
1813 | ||
1814 | return 0; | |
1815 | } | |
1816 | EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance); | |
1817 | ||
1818 | /** | |
1819 | * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state | |
1820 | * @state: The state to remove | |
1821 | * @invoke: If true, the teardown function is invoked for cpus where | |
1822 | * cpu state >= @state | |
1823 | * | |
1824 | * The caller needs to hold cpus read locked while calling this function. | |
1825 | * The teardown callback is currently not allowed to fail. Think | |
1826 | * about module removal! | |
1827 | */ | |
1828 | void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke) | |
1829 | { | |
1830 | struct cpuhp_step *sp = cpuhp_get_step(state); | |
1831 | int cpu; | |
1832 | ||
1833 | BUG_ON(cpuhp_cb_check(state)); | |
1834 | ||
1835 | lockdep_assert_cpus_held(); | |
1836 | ||
1837 | mutex_lock(&cpuhp_state_mutex); | |
1838 | if (sp->multi_instance) { | |
1839 | WARN(!hlist_empty(&sp->list), | |
1840 | "Error: Removing state %d which has instances left.\n", | |
1841 | state); | |
1842 | goto remove; | |
1843 | } | |
1844 | ||
1845 | if (!invoke || !cpuhp_get_teardown_cb(state)) | |
1846 | goto remove; | |
1847 | ||
1848 | /* | |
1849 | * Call the teardown callback for each present cpu depending | |
1850 | * on the hotplug state of the cpu. This function is not | |
1851 | * allowed to fail currently! | |
1852 | */ | |
1853 | for_each_present_cpu(cpu) { | |
1854 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); | |
1855 | int cpustate = st->state; | |
1856 | ||
1857 | if (cpustate >= state) | |
1858 | cpuhp_issue_call(cpu, state, false, NULL); | |
1859 | } | |
1860 | remove: | |
1861 | cpuhp_store_callbacks(state, NULL, NULL, NULL, false); | |
1862 | mutex_unlock(&cpuhp_state_mutex); | |
1863 | } | |
1864 | EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked); | |
1865 | ||
1866 | void __cpuhp_remove_state(enum cpuhp_state state, bool invoke) | |
1867 | { | |
1868 | cpus_read_lock(); | |
1869 | __cpuhp_remove_state_cpuslocked(state, invoke); | |
1870 | cpus_read_unlock(); | |
1871 | } | |
1872 | EXPORT_SYMBOL(__cpuhp_remove_state); | |
1873 | ||
1874 | #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU) | |
1875 | static ssize_t show_cpuhp_state(struct device *dev, | |
1876 | struct device_attribute *attr, char *buf) | |
1877 | { | |
1878 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); | |
1879 | ||
1880 | return sprintf(buf, "%d\n", st->state); | |
1881 | } | |
1882 | static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL); | |
1883 | ||
1884 | static ssize_t write_cpuhp_target(struct device *dev, | |
1885 | struct device_attribute *attr, | |
1886 | const char *buf, size_t count) | |
1887 | { | |
1888 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); | |
1889 | struct cpuhp_step *sp; | |
1890 | int target, ret; | |
1891 | ||
1892 | ret = kstrtoint(buf, 10, &target); | |
1893 | if (ret) | |
1894 | return ret; | |
1895 | ||
1896 | #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL | |
1897 | if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE) | |
1898 | return -EINVAL; | |
1899 | #else | |
1900 | if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE) | |
1901 | return -EINVAL; | |
1902 | #endif | |
1903 | ||
1904 | ret = lock_device_hotplug_sysfs(); | |
1905 | if (ret) | |
1906 | return ret; | |
1907 | ||
1908 | mutex_lock(&cpuhp_state_mutex); | |
1909 | sp = cpuhp_get_step(target); | |
1910 | ret = !sp->name || sp->cant_stop ? -EINVAL : 0; | |
1911 | mutex_unlock(&cpuhp_state_mutex); | |
1912 | if (ret) | |
1913 | goto out; | |
1914 | ||
1915 | if (st->state < target) | |
1916 | ret = do_cpu_up(dev->id, target); | |
1917 | else | |
1918 | ret = do_cpu_down(dev->id, target); | |
1919 | out: | |
1920 | unlock_device_hotplug(); | |
1921 | return ret ? ret : count; | |
1922 | } | |
1923 | ||
1924 | static ssize_t show_cpuhp_target(struct device *dev, | |
1925 | struct device_attribute *attr, char *buf) | |
1926 | { | |
1927 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); | |
1928 | ||
1929 | return sprintf(buf, "%d\n", st->target); | |
1930 | } | |
1931 | static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target); | |
1932 | ||
1933 | ||
1934 | static ssize_t write_cpuhp_fail(struct device *dev, | |
1935 | struct device_attribute *attr, | |
1936 | const char *buf, size_t count) | |
1937 | { | |
1938 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); | |
1939 | struct cpuhp_step *sp; | |
1940 | int fail, ret; | |
1941 | ||
1942 | ret = kstrtoint(buf, 10, &fail); | |
1943 | if (ret) | |
1944 | return ret; | |
1945 | ||
1946 | /* | |
1947 | * Cannot fail STARTING/DYING callbacks. | |
1948 | */ | |
1949 | if (cpuhp_is_atomic_state(fail)) | |
1950 | return -EINVAL; | |
1951 | ||
1952 | /* | |
1953 | * Cannot fail anything that doesn't have callbacks. | |
1954 | */ | |
1955 | mutex_lock(&cpuhp_state_mutex); | |
1956 | sp = cpuhp_get_step(fail); | |
1957 | if (!sp->startup.single && !sp->teardown.single) | |
1958 | ret = -EINVAL; | |
1959 | mutex_unlock(&cpuhp_state_mutex); | |
1960 | if (ret) | |
1961 | return ret; | |
1962 | ||
1963 | st->fail = fail; | |
1964 | ||
1965 | return count; | |
1966 | } | |
1967 | ||
1968 | static ssize_t show_cpuhp_fail(struct device *dev, | |
1969 | struct device_attribute *attr, char *buf) | |
1970 | { | |
1971 | struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); | |
1972 | ||
1973 | return sprintf(buf, "%d\n", st->fail); | |
1974 | } | |
1975 | ||
1976 | static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail); | |
1977 | ||
1978 | static struct attribute *cpuhp_cpu_attrs[] = { | |
1979 | &dev_attr_state.attr, | |
1980 | &dev_attr_target.attr, | |
1981 | &dev_attr_fail.attr, | |
1982 | NULL | |
1983 | }; | |
1984 | ||
1985 | static const struct attribute_group cpuhp_cpu_attr_group = { | |
1986 | .attrs = cpuhp_cpu_attrs, | |
1987 | .name = "hotplug", | |
1988 | NULL | |
1989 | }; | |
1990 | ||
1991 | static ssize_t show_cpuhp_states(struct device *dev, | |
1992 | struct device_attribute *attr, char *buf) | |
1993 | { | |
1994 | ssize_t cur, res = 0; | |
1995 | int i; | |
1996 | ||
1997 | mutex_lock(&cpuhp_state_mutex); | |
1998 | for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) { | |
1999 | struct cpuhp_step *sp = cpuhp_get_step(i); | |
2000 | ||
2001 | if (sp->name) { | |
2002 | cur = sprintf(buf, "%3d: %s\n", i, sp->name); | |
2003 | buf += cur; | |
2004 | res += cur; | |
2005 | } | |
2006 | } | |
2007 | mutex_unlock(&cpuhp_state_mutex); | |
2008 | return res; | |
2009 | } | |
2010 | static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL); | |
2011 | ||
2012 | static struct attribute *cpuhp_cpu_root_attrs[] = { | |
2013 | &dev_attr_states.attr, | |
2014 | NULL | |
2015 | }; | |
2016 | ||
2017 | static const struct attribute_group cpuhp_cpu_root_attr_group = { | |
2018 | .attrs = cpuhp_cpu_root_attrs, | |
2019 | .name = "hotplug", | |
2020 | NULL | |
2021 | }; | |
2022 | ||
2023 | #ifdef CONFIG_HOTPLUG_SMT | |
2024 | ||
2025 | static const char *smt_states[] = { | |
2026 | [CPU_SMT_ENABLED] = "on", | |
2027 | [CPU_SMT_DISABLED] = "off", | |
2028 | [CPU_SMT_FORCE_DISABLED] = "forceoff", | |
2029 | [CPU_SMT_NOT_SUPPORTED] = "notsupported", | |
2030 | }; | |
2031 | ||
2032 | static ssize_t | |
2033 | show_smt_control(struct device *dev, struct device_attribute *attr, char *buf) | |
2034 | { | |
2035 | return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]); | |
2036 | } | |
2037 | ||
2038 | static void cpuhp_offline_cpu_device(unsigned int cpu) | |
2039 | { | |
2040 | struct device *dev = get_cpu_device(cpu); | |
2041 | ||
2042 | dev->offline = true; | |
2043 | /* Tell user space about the state change */ | |
2044 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); | |
2045 | } | |
2046 | ||
2047 | static void cpuhp_online_cpu_device(unsigned int cpu) | |
2048 | { | |
2049 | struct device *dev = get_cpu_device(cpu); | |
2050 | ||
2051 | dev->offline = false; | |
2052 | /* Tell user space about the state change */ | |
2053 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); | |
2054 | } | |
2055 | ||
2056 | static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) | |
2057 | { | |
2058 | int cpu, ret = 0; | |
2059 | ||
2060 | cpu_maps_update_begin(); | |
2061 | for_each_online_cpu(cpu) { | |
2062 | if (topology_is_primary_thread(cpu)) | |
2063 | continue; | |
2064 | ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE); | |
2065 | if (ret) | |
2066 | break; | |
2067 | /* | |
2068 | * As this needs to hold the cpu maps lock it's impossible | |
2069 | * to call device_offline() because that ends up calling | |
2070 | * cpu_down() which takes cpu maps lock. cpu maps lock | |
2071 | * needs to be held as this might race against in kernel | |
2072 | * abusers of the hotplug machinery (thermal management). | |
2073 | * | |
2074 | * So nothing would update device:offline state. That would | |
2075 | * leave the sysfs entry stale and prevent onlining after | |
2076 | * smt control has been changed to 'off' again. This is | |
2077 | * called under the sysfs hotplug lock, so it is properly | |
2078 | * serialized against the regular offline usage. | |
2079 | */ | |
2080 | cpuhp_offline_cpu_device(cpu); | |
2081 | } | |
2082 | if (!ret) { | |
2083 | cpu_smt_control = ctrlval; | |
2084 | arch_smt_update(); | |
2085 | } | |
2086 | cpu_maps_update_done(); | |
2087 | return ret; | |
2088 | } | |
2089 | ||
2090 | static int cpuhp_smt_enable(void) | |
2091 | { | |
2092 | int cpu, ret = 0; | |
2093 | ||
2094 | cpu_maps_update_begin(); | |
2095 | cpu_smt_control = CPU_SMT_ENABLED; | |
2096 | arch_smt_update(); | |
2097 | for_each_present_cpu(cpu) { | |
2098 | /* Skip online CPUs and CPUs on offline nodes */ | |
2099 | if (cpu_online(cpu) || !node_online(cpu_to_node(cpu))) | |
2100 | continue; | |
2101 | ret = _cpu_up(cpu, 0, CPUHP_ONLINE); | |
2102 | if (ret) | |
2103 | break; | |
2104 | /* See comment in cpuhp_smt_disable() */ | |
2105 | cpuhp_online_cpu_device(cpu); | |
2106 | } | |
2107 | cpu_maps_update_done(); | |
2108 | return ret; | |
2109 | } | |
2110 | ||
2111 | static ssize_t | |
2112 | store_smt_control(struct device *dev, struct device_attribute *attr, | |
2113 | const char *buf, size_t count) | |
2114 | { | |
2115 | int ctrlval, ret; | |
2116 | ||
2117 | if (sysfs_streq(buf, "on")) | |
2118 | ctrlval = CPU_SMT_ENABLED; | |
2119 | else if (sysfs_streq(buf, "off")) | |
2120 | ctrlval = CPU_SMT_DISABLED; | |
2121 | else if (sysfs_streq(buf, "forceoff")) | |
2122 | ctrlval = CPU_SMT_FORCE_DISABLED; | |
2123 | else | |
2124 | return -EINVAL; | |
2125 | ||
2126 | if (cpu_smt_control == CPU_SMT_FORCE_DISABLED) | |
2127 | return -EPERM; | |
2128 | ||
2129 | if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED) | |
2130 | return -ENODEV; | |
2131 | ||
2132 | ret = lock_device_hotplug_sysfs(); | |
2133 | if (ret) | |
2134 | return ret; | |
2135 | ||
2136 | if (ctrlval != cpu_smt_control) { | |
2137 | switch (ctrlval) { | |
2138 | case CPU_SMT_ENABLED: | |
2139 | ret = cpuhp_smt_enable(); | |
2140 | break; | |
2141 | case CPU_SMT_DISABLED: | |
2142 | case CPU_SMT_FORCE_DISABLED: | |
2143 | ret = cpuhp_smt_disable(ctrlval); | |
2144 | break; | |
2145 | } | |
2146 | } | |
2147 | ||
2148 | unlock_device_hotplug(); | |
2149 | return ret ? ret : count; | |
2150 | } | |
2151 | static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control); | |
2152 | ||
2153 | static ssize_t | |
2154 | show_smt_active(struct device *dev, struct device_attribute *attr, char *buf) | |
2155 | { | |
2156 | bool active = topology_max_smt_threads() > 1; | |
2157 | ||
2158 | return snprintf(buf, PAGE_SIZE - 2, "%d\n", active); | |
2159 | } | |
2160 | static DEVICE_ATTR(active, 0444, show_smt_active, NULL); | |
2161 | ||
2162 | static struct attribute *cpuhp_smt_attrs[] = { | |
2163 | &dev_attr_control.attr, | |
2164 | &dev_attr_active.attr, | |
2165 | NULL | |
2166 | }; | |
2167 | ||
2168 | static const struct attribute_group cpuhp_smt_attr_group = { | |
2169 | .attrs = cpuhp_smt_attrs, | |
2170 | .name = "smt", | |
2171 | NULL | |
2172 | }; | |
2173 | ||
2174 | static int __init cpu_smt_state_init(void) | |
2175 | { | |
2176 | return sysfs_create_group(&cpu_subsys.dev_root->kobj, | |
2177 | &cpuhp_smt_attr_group); | |
2178 | } | |
2179 | ||
2180 | #else | |
2181 | static inline int cpu_smt_state_init(void) { return 0; } | |
2182 | #endif | |
2183 | ||
2184 | static int __init cpuhp_sysfs_init(void) | |
2185 | { | |
2186 | int cpu, ret; | |
2187 | ||
2188 | ret = cpu_smt_state_init(); | |
2189 | if (ret) | |
2190 | return ret; | |
2191 | ||
2192 | ret = sysfs_create_group(&cpu_subsys.dev_root->kobj, | |
2193 | &cpuhp_cpu_root_attr_group); | |
2194 | if (ret) | |
2195 | return ret; | |
2196 | ||
2197 | for_each_possible_cpu(cpu) { | |
2198 | struct device *dev = get_cpu_device(cpu); | |
2199 | ||
2200 | if (!dev) | |
2201 | continue; | |
2202 | ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group); | |
2203 | if (ret) | |
2204 | return ret; | |
2205 | } | |
2206 | return 0; | |
2207 | } | |
2208 | device_initcall(cpuhp_sysfs_init); | |
2209 | #endif | |
2210 | ||
2211 | /* | |
2212 | * cpu_bit_bitmap[] is a special, "compressed" data structure that | |
2213 | * represents all NR_CPUS bits binary values of 1<<nr. | |
2214 | * | |
2215 | * It is used by cpumask_of() to get a constant address to a CPU | |
2216 | * mask value that has a single bit set only. | |
2217 | */ | |
2218 | ||
2219 | /* cpu_bit_bitmap[0] is empty - so we can back into it */ | |
2220 | #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x)) | |
2221 | #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1) | |
2222 | #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2) | |
2223 | #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4) | |
2224 | ||
2225 | const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = { | |
2226 | ||
2227 | MASK_DECLARE_8(0), MASK_DECLARE_8(8), | |
2228 | MASK_DECLARE_8(16), MASK_DECLARE_8(24), | |
2229 | #if BITS_PER_LONG > 32 | |
2230 | MASK_DECLARE_8(32), MASK_DECLARE_8(40), | |
2231 | MASK_DECLARE_8(48), MASK_DECLARE_8(56), | |
2232 | #endif | |
2233 | }; | |
2234 | EXPORT_SYMBOL_GPL(cpu_bit_bitmap); | |
2235 | ||
2236 | const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL; | |
2237 | EXPORT_SYMBOL(cpu_all_bits); | |
2238 | ||
2239 | #ifdef CONFIG_INIT_ALL_POSSIBLE | |
2240 | struct cpumask __cpu_possible_mask __read_mostly | |
2241 | = {CPU_BITS_ALL}; | |
2242 | #else | |
2243 | struct cpumask __cpu_possible_mask __read_mostly; | |
2244 | #endif | |
2245 | EXPORT_SYMBOL(__cpu_possible_mask); | |
2246 | ||
2247 | struct cpumask __cpu_online_mask __read_mostly; | |
2248 | EXPORT_SYMBOL(__cpu_online_mask); | |
2249 | ||
2250 | struct cpumask __cpu_present_mask __read_mostly; | |
2251 | EXPORT_SYMBOL(__cpu_present_mask); | |
2252 | ||
2253 | struct cpumask __cpu_active_mask __read_mostly; | |
2254 | EXPORT_SYMBOL(__cpu_active_mask); | |
2255 | ||
2256 | void init_cpu_present(const struct cpumask *src) | |
2257 | { | |
2258 | cpumask_copy(&__cpu_present_mask, src); | |
2259 | } | |
2260 | ||
2261 | void init_cpu_possible(const struct cpumask *src) | |
2262 | { | |
2263 | cpumask_copy(&__cpu_possible_mask, src); | |
2264 | } | |
2265 | ||
2266 | void init_cpu_online(const struct cpumask *src) | |
2267 | { | |
2268 | cpumask_copy(&__cpu_online_mask, src); | |
2269 | } | |
2270 | ||
2271 | /* | |
2272 | * Activate the first processor. | |
2273 | */ | |
2274 | void __init boot_cpu_init(void) | |
2275 | { | |
2276 | int cpu = smp_processor_id(); | |
2277 | ||
2278 | /* Mark the boot cpu "present", "online" etc for SMP and UP case */ | |
2279 | set_cpu_online(cpu, true); | |
2280 | set_cpu_active(cpu, true); | |
2281 | set_cpu_present(cpu, true); | |
2282 | set_cpu_possible(cpu, true); | |
2283 | ||
2284 | #ifdef CONFIG_SMP | |
2285 | __boot_cpu_id = cpu; | |
2286 | #endif | |
2287 | } | |
2288 | ||
2289 | /* | |
2290 | * Must be called _AFTER_ setting up the per_cpu areas | |
2291 | */ | |
2292 | void __init boot_cpu_state_init(void) | |
2293 | { | |
2294 | per_cpu_ptr(&cpuhp_state, smp_processor_id())->booted_once = true; | |
2295 | per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE; | |
2296 | } |