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