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