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