blob: 6bbe261b851f34b0fc3bb8c419aac8fcdd9489a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +010011#include <linux/sched/hotplug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010012#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/unistd.h>
14#include <linux/cpu.h>
Anton Vorontsovcb792952012-05-31 16:26:22 -070015#include <linux/oom.h>
16#include <linux/rcupdate.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040017#include <linux/export.h>
Anton Vorontsove4cc2f82012-05-31 16:26:26 -070018#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kthread.h>
20#include <linux/stop_machine.h>
Ingo Molnar81615b622006-06-26 00:24:32 -070021#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +010023#include <linux/suspend.h>
Gautham R. Shenoya19423b2014-03-11 02:04:03 +053024#include <linux/lockdep.h>
Preeti U Murthy345527b2015-03-30 14:59:19 +053025#include <linux/tick.h>
Thomas Gleixnera8994182015-07-05 17:12:30 +000026#include <linux/irq.h>
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000027#include <linux/smpboot.h>
Richard Weinbergere6d49892016-08-18 14:57:17 +020028#include <linux/relay.h>
Sebastian Andrzej Siewior6731d4f2016-08-23 14:53:19 +020029#include <linux/slab.h>
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +020030#include <linux/percpu-rwsem.h>
Thomas Gleixnercff7d372016-02-26 18:43:28 +000031
Todd E Brandtbb3632c2014-06-06 05:40:17 -070032#include <trace/events/power.h>
Thomas Gleixnercff7d372016-02-26 18:43:28 +000033#define CREATE_TRACE_POINTS
34#include <trace/events/cpuhp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Gleixner38498a62012-04-20 13:05:44 +000036#include "smpboot.h"
37
Thomas Gleixnercff7d372016-02-26 18:43:28 +000038/**
39 * cpuhp_cpu_state - Per cpu hotplug state storage
40 * @state: The current cpu state
41 * @target: The target state
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000042 * @thread: Pointer to the hotplug thread
43 * @should_run: Thread should execute
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +020044 * @rollback: Perform a rollback
Thomas Gleixnera7246322016-08-12 19:49:38 +020045 * @single: Single callback invocation
46 * @bringup: Single callback bringup or teardown selector
47 * @cb_state: The state for a single callback (install/uninstall)
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000048 * @result: Result of the operation
Peter Zijlstra5ebe7742017-09-20 19:00:19 +020049 * @done_up: Signal completion to the issuer of the task for cpu-up
50 * @done_down: Signal completion to the issuer of the task for cpu-down
Thomas Gleixnercff7d372016-02-26 18:43:28 +000051 */
52struct cpuhp_cpu_state {
53 enum cpuhp_state state;
54 enum cpuhp_state target;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000055#ifdef CONFIG_SMP
56 struct task_struct *thread;
57 bool should_run;
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +020058 bool rollback;
Thomas Gleixnera7246322016-08-12 19:49:38 +020059 bool single;
60 bool bringup;
Thomas Gleixnercf392d12016-08-12 19:49:39 +020061 struct hlist_node *node;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +020062 struct hlist_node *last;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000063 enum cpuhp_state cb_state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000064 int result;
Peter Zijlstra5ebe7742017-09-20 19:00:19 +020065 struct completion done_up;
66 struct completion done_down;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000067#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +000068};
69
70static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
71
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +020072#if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +020073static struct lockdep_map cpuhp_state_up_map =
74 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
75static struct lockdep_map cpuhp_state_down_map =
76 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
77
78
79static void inline cpuhp_lock_acquire(bool bringup)
80{
81 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
82}
83
84static void inline cpuhp_lock_release(bool bringup)
85{
86 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
87}
88#else
89
90static void inline cpuhp_lock_acquire(bool bringup) { }
91static void inline cpuhp_lock_release(bool bringup) { }
92
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +020093#endif
94
Thomas Gleixnercff7d372016-02-26 18:43:28 +000095/**
96 * cpuhp_step - Hotplug state machine step
97 * @name: Name of the step
98 * @startup: Startup function of the step
99 * @teardown: Teardown function of the step
100 * @skip_onerr: Do not invoke the functions on error rollback
101 * Will go away once the notifiers are gone
Thomas Gleixner757c9892016-02-26 18:43:32 +0000102 * @cant_stop: Bringup/teardown can't be stopped at this step
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000103 */
104struct cpuhp_step {
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200105 const char *name;
106 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200107 int (*single)(unsigned int cpu);
108 int (*multi)(unsigned int cpu,
109 struct hlist_node *node);
110 } startup;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200111 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200112 int (*single)(unsigned int cpu);
113 int (*multi)(unsigned int cpu,
114 struct hlist_node *node);
115 } teardown;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200116 struct hlist_head list;
117 bool skip_onerr;
118 bool cant_stop;
119 bool multi_instance;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000120};
121
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +0000122static DEFINE_MUTEX(cpuhp_state_mutex);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000123static struct cpuhp_step cpuhp_bp_states[];
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000124static struct cpuhp_step cpuhp_ap_states[];
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000125
Thomas Gleixnera7246322016-08-12 19:49:38 +0200126static bool cpuhp_is_ap_state(enum cpuhp_state state)
127{
128 /*
129 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
130 * purposes as that state is handled explicitly in cpu_down.
131 */
132 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
133}
134
135static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
136{
137 struct cpuhp_step *sp;
138
139 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
140 return sp + state;
141}
142
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000143/**
144 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
145 * @cpu: The cpu for which the callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200146 * @state: The state to do callbacks for
Thomas Gleixnera7246322016-08-12 19:49:38 +0200147 * @bringup: True if the bringup callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200148 * @node: For multi-instance, do a single entry callback for install/remove
149 * @lastp: For multi-instance rollback, remember how far we got
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000150 *
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200151 * Called from cpu hotplug and from the state register machinery.
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000152 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200153static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
Peter Zijlstra96abb962017-09-20 19:00:16 +0200154 bool bringup, struct hlist_node *node,
155 struct hlist_node **lastp)
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000156{
157 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200158 struct cpuhp_step *step = cpuhp_get_step(state);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200159 int (*cbm)(unsigned int cpu, struct hlist_node *node);
160 int (*cb)(unsigned int cpu);
161 int ret, cnt;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000162
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200163 if (!step->multi_instance) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200164 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200165 cb = bringup ? step->startup.single : step->teardown.single;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200166 if (!cb)
167 return 0;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200168 trace_cpuhp_enter(cpu, st->target, state, cb);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000169 ret = cb(cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200170 trace_cpuhp_exit(cpu, st->state, state, ret);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200171 return ret;
172 }
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200173 cbm = bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200174 if (!cbm)
175 return 0;
176
177 /* Single invocation for instance add/remove */
178 if (node) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200179 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200180 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
181 ret = cbm(cpu, node);
182 trace_cpuhp_exit(cpu, st->state, state, ret);
183 return ret;
184 }
185
186 /* State transition. Invoke on all instances */
187 cnt = 0;
188 hlist_for_each(node, &step->list) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200189 if (lastp && node == *lastp)
190 break;
191
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200192 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
193 ret = cbm(cpu, node);
194 trace_cpuhp_exit(cpu, st->state, state, ret);
Peter Zijlstra96abb962017-09-20 19:00:16 +0200195 if (ret) {
196 if (!lastp)
197 goto err;
198
199 *lastp = node;
200 return ret;
201 }
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200202 cnt++;
203 }
Peter Zijlstra96abb962017-09-20 19:00:16 +0200204 if (lastp)
205 *lastp = NULL;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200206 return 0;
207err:
208 /* Rollback the instances if one failed */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200209 cbm = !bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200210 if (!cbm)
211 return ret;
212
213 hlist_for_each(node, &step->list) {
214 if (!cnt--)
215 break;
Peter Zijlstra724a8682017-09-20 19:00:18 +0200216
217 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
218 ret = cbm(cpu, node);
219 trace_cpuhp_exit(cpu, st->state, state, ret);
220 /*
221 * Rollback must not fail,
222 */
223 WARN_ON_ONCE(ret);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000224 }
225 return ret;
226}
227
Rusty Russell98a79d62008-12-13 21:19:41 +1030228#ifdef CONFIG_SMP
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200229static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
230{
231 struct completion *done = bringup ? &st->done_up : &st->done_down;
232 wait_for_completion(done);
233}
234
235static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
236{
237 struct completion *done = bringup ? &st->done_up : &st->done_down;
238 complete(done);
239}
240
241/*
242 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
243 */
244static bool cpuhp_is_atomic_state(enum cpuhp_state state)
245{
246 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
247}
248
Rusty Russellb3199c02008-12-30 09:05:14 +1030249/* Serializes the updates to cpu_online_mask, cpu_present_mask */
Linus Torvaldsaa953872006-07-23 12:12:16 -0700250static DEFINE_MUTEX(cpu_add_remove_lock);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000251bool cpuhp_tasks_frozen;
252EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700254/*
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530255 * The following two APIs (cpu_maps_update_begin/done) must be used when
256 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700257 */
258void cpu_maps_update_begin(void)
259{
260 mutex_lock(&cpu_add_remove_lock);
261}
262
263void cpu_maps_update_done(void)
264{
265 mutex_unlock(&cpu_add_remove_lock);
266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200268/*
269 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700270 * Should always be manipulated under cpu_add_remove_lock
271 */
272static int cpu_hotplug_disabled;
273
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700274#ifdef CONFIG_HOTPLUG_CPU
275
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200276DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530277
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200278void cpus_read_lock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800279{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200280 percpu_down_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800281}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200282EXPORT_SYMBOL_GPL(cpus_read_lock);
Ashok Raj90d45d12005-11-08 21:34:24 -0800283
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200284void cpus_read_unlock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800285{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200286 percpu_up_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800287}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200288EXPORT_SYMBOL_GPL(cpus_read_unlock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800289
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200290void cpus_write_lock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100291{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200292 percpu_down_write(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100293}
294
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200295void cpus_write_unlock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100296{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200297 percpu_up_write(&cpu_hotplug_lock);
298}
299
300void lockdep_assert_cpus_held(void)
301{
302 percpu_rwsem_assert_held(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100303}
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700304
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700305/*
306 * Wait for currently running CPU hotplug operations to complete (if any) and
307 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
308 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
309 * hotplug path before performing hotplug operations. So acquiring that lock
310 * guarantees mutual exclusion from any currently running hotplug operations.
311 */
312void cpu_hotplug_disable(void)
313{
314 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700315 cpu_hotplug_disabled++;
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700316 cpu_maps_update_done();
317}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700318EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700319
Lianwei Wang01b41152016-06-09 23:43:28 -0700320static void __cpu_hotplug_enable(void)
321{
322 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
323 return;
324 cpu_hotplug_disabled--;
325}
326
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700327void cpu_hotplug_enable(void)
328{
329 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -0700330 __cpu_hotplug_enable();
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700331 cpu_maps_update_done();
332}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700333EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
Toshi Kanib9d10be2013-08-12 09:45:53 -0600334#endif /* CONFIG_HOTPLUG_CPU */
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700335
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200336static inline enum cpuhp_state
337cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
338{
339 enum cpuhp_state prev_state = st->state;
340
341 st->rollback = false;
342 st->last = NULL;
343
344 st->target = target;
345 st->single = false;
346 st->bringup = st->state < target;
347
348 return prev_state;
349}
350
351static inline void
352cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
353{
354 st->rollback = true;
355
356 /*
357 * If we have st->last we need to undo partial multi_instance of this
358 * state first. Otherwise start undo at the previous state.
359 */
360 if (!st->last) {
361 if (st->bringup)
362 st->state--;
363 else
364 st->state++;
365 }
366
367 st->target = prev_state;
368 st->bringup = !st->bringup;
369}
370
371/* Regular hotplug invocation of the AP hotplug thread */
372static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
373{
374 if (!st->single && st->state == st->target)
375 return;
376
377 st->result = 0;
378 /*
379 * Make sure the above stores are visible before should_run becomes
380 * true. Paired with the mb() above in cpuhp_thread_fun()
381 */
382 smp_mb();
383 st->should_run = true;
384 wake_up_process(st->thread);
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200385 wait_for_ap_thread(st, st->bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200386}
387
388static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
389{
390 enum cpuhp_state prev_state;
391 int ret;
392
393 prev_state = cpuhp_set_state(st, target);
394 __cpuhp_kick_ap(st);
395 if ((ret = st->result)) {
396 cpuhp_reset_state(st, prev_state);
397 __cpuhp_kick_ap(st);
398 }
399
400 return ret;
401}
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200402
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000403static int bringup_wait_for_ap(unsigned int cpu)
404{
405 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
406
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200407 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200408 wait_for_ap_thread(st, true);
Thomas Gleixnerdea1d0f2017-07-11 22:06:24 +0200409 if (WARN_ON_ONCE((!cpu_online(cpu))))
410 return -ECANCELED;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200411
412 /* Unpark the stopper thread and the hotplug thread of the target cpu */
413 stop_machine_unpark(cpu);
414 kthread_unpark(st->thread);
415
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200416 if (st->target <= CPUHP_AP_ONLINE_IDLE)
417 return 0;
418
419 return cpuhp_kick_ap(st, st->target);
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000420}
421
Thomas Gleixnerba997462016-02-26 18:43:24 +0000422static int bringup_cpu(unsigned int cpu)
423{
424 struct task_struct *idle = idle_thread_get(cpu);
425 int ret;
426
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400427 /*
428 * Some architectures have to walk the irq descriptors to
429 * setup the vector space for the cpu which comes online.
430 * Prevent irq alloc/free across the bringup.
431 */
432 irq_lock_sparse();
433
Thomas Gleixnerba997462016-02-26 18:43:24 +0000434 /* Arch-specific enabling code. */
435 ret = __cpu_up(cpu, idle);
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400436 irq_unlock_sparse();
Thomas Gleixner530e9b72016-12-21 20:19:53 +0100437 if (ret)
Thomas Gleixnerba997462016-02-26 18:43:24 +0000438 return ret;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200439 return bringup_wait_for_ap(cpu);
Thomas Gleixnerba997462016-02-26 18:43:24 +0000440}
441
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000442/*
443 * Hotplug state machine related functions
444 */
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000445
Thomas Gleixnera7246322016-08-12 19:49:38 +0200446static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000447{
448 for (st->state--; st->state > st->target; st->state--) {
Thomas Gleixnera7246322016-08-12 19:49:38 +0200449 struct cpuhp_step *step = cpuhp_get_step(st->state);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000450
451 if (!step->skip_onerr)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200452 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000453 }
454}
455
456static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
Thomas Gleixnera7246322016-08-12 19:49:38 +0200457 enum cpuhp_state target)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000458{
459 enum cpuhp_state prev_state = st->state;
460 int ret = 0;
461
462 while (st->state < target) {
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000463 st->state++;
Peter Zijlstra96abb962017-09-20 19:00:16 +0200464 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000465 if (ret) {
466 st->target = prev_state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200467 undo_cpu_up(cpu, st);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000468 break;
469 }
470 }
471 return ret;
472}
473
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000474/*
475 * The cpu hotplug threads manage the bringup and teardown of the cpus
476 */
477static void cpuhp_create(unsigned int cpu)
478{
479 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
480
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200481 init_completion(&st->done_up);
482 init_completion(&st->done_down);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000483}
484
485static int cpuhp_should_run(unsigned int cpu)
486{
487 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
488
489 return st->should_run;
490}
491
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000492/*
493 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
494 * callbacks when a state gets [un]installed at runtime.
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200495 *
496 * Each invocation of this function by the smpboot thread does a single AP
497 * state callback.
498 *
499 * It has 3 modes of operation:
500 * - single: runs st->cb_state
501 * - up: runs ++st->state, while st->state < st->target
502 * - down: runs st->state--, while st->state > st->target
503 *
504 * When complete or on error, should_run is cleared and the completion is fired.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000505 */
506static void cpuhp_thread_fun(unsigned int cpu)
507{
508 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200509 bool bringup = st->bringup;
510 enum cpuhp_state state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000511
512 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200513 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
514 * that if we see ->should_run we also see the rest of the state.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000515 */
516 smp_mb();
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200517
518 if (WARN_ON_ONCE(!st->should_run))
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000519 return;
520
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200521 cpuhp_lock_acquire(bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200522
Thomas Gleixnera7246322016-08-12 19:49:38 +0200523 if (st->single) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200524 state = st->cb_state;
525 st->should_run = false;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000526 } else {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200527 if (bringup) {
528 st->state++;
529 state = st->state;
530 st->should_run = (st->state < st->target);
531 WARN_ON_ONCE(st->state > st->target);
532 } else {
533 state = st->state;
534 st->state--;
535 st->should_run = (st->state > st->target);
536 WARN_ON_ONCE(st->state < st->target);
537 }
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000538 }
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200539
540 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
541
542 if (st->rollback) {
543 struct cpuhp_step *step = cpuhp_get_step(state);
544 if (step->skip_onerr)
545 goto next;
546 }
547
548 if (cpuhp_is_atomic_state(state)) {
549 local_irq_disable();
550 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
551 local_irq_enable();
552
553 /*
554 * STARTING/DYING must not fail!
555 */
556 WARN_ON_ONCE(st->result);
557 } else {
558 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
559 }
560
561 if (st->result) {
562 /*
563 * If we fail on a rollback, we're up a creek without no
564 * paddle, no way forward, no way back. We loose, thanks for
565 * playing.
566 */
567 WARN_ON_ONCE(st->rollback);
568 st->should_run = false;
569 }
570
571next:
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200572 cpuhp_lock_release(bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200573
574 if (!st->should_run)
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200575 complete_ap_thread(st, bringup);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000576}
577
578/* Invoke a single callback on a remote cpu */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200579static int
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200580cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
581 struct hlist_node *node)
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000582{
583 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200584 int ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000585
586 if (!cpu_online(cpu))
587 return 0;
588
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200589 cpuhp_lock_acquire(false);
590 cpuhp_lock_release(false);
591
592 cpuhp_lock_acquire(true);
593 cpuhp_lock_release(true);
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200594
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000595 /*
596 * If we are up and running, use the hotplug thread. For early calls
597 * we invoke the thread function directly.
598 */
599 if (!st->thread)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200600 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000601
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200602 st->rollback = false;
603 st->last = NULL;
604
605 st->node = node;
606 st->bringup = bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000607 st->cb_state = state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200608 st->single = true;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200609
610 __cpuhp_kick_ap(st);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200611
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000612 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200613 * If we failed and did a partial, do a rollback.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000614 */
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200615 if ((ret = st->result) && st->last) {
616 st->rollback = true;
617 st->bringup = !bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000618
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200619 __cpuhp_kick_ap(st);
620 }
621
622 return ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000623}
624
625static int cpuhp_kick_ap_work(unsigned int cpu)
626{
627 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200628 enum cpuhp_state prev_state = st->state;
629 int ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000630
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200631 cpuhp_lock_acquire(false);
632 cpuhp_lock_release(false);
633
634 cpuhp_lock_acquire(true);
635 cpuhp_lock_release(true);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200636
637 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
638 ret = cpuhp_kick_ap(st, st->target);
639 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
640
641 return ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000642}
643
644static struct smp_hotplug_thread cpuhp_threads = {
645 .store = &cpuhp_state.thread,
646 .create = &cpuhp_create,
647 .thread_should_run = cpuhp_should_run,
648 .thread_fn = cpuhp_thread_fun,
649 .thread_comm = "cpuhp/%u",
650 .selfparking = true,
651};
652
653void __init cpuhp_threads_init(void)
654{
655 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
656 kthread_unpark(this_cpu_read(cpuhp_state.thread));
657}
658
Michal Hocko777c6e02016-12-07 14:54:38 +0100659#ifdef CONFIG_HOTPLUG_CPU
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700660/**
661 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
662 * @cpu: a CPU id
663 *
664 * This function walks all processes, finds a valid mm struct for each one and
665 * then clears a corresponding bit in mm's cpumask. While this all sounds
666 * trivial, there are various non-obvious corner cases, which this function
667 * tries to solve in a safe manner.
668 *
669 * Also note that the function uses a somewhat relaxed locking scheme, so it may
670 * be called only for an already offlined CPU.
671 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700672void clear_tasks_mm_cpumask(int cpu)
673{
674 struct task_struct *p;
675
676 /*
677 * This function is called after the cpu is taken down and marked
678 * offline, so its not like new tasks will ever get this cpu set in
679 * their mm mask. -- Peter Zijlstra
680 * Thus, we may use rcu_read_lock() here, instead of grabbing
681 * full-fledged tasklist_lock.
682 */
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700683 WARN_ON(cpu_online(cpu));
Anton Vorontsovcb792952012-05-31 16:26:22 -0700684 rcu_read_lock();
685 for_each_process(p) {
686 struct task_struct *t;
687
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700688 /*
689 * Main thread might exit, but other threads may still have
690 * a valid mm. Find one.
691 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700692 t = find_lock_task_mm(p);
693 if (!t)
694 continue;
695 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
696 task_unlock(t);
697 }
698 rcu_read_unlock();
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/* Take this CPU down. */
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200702static int take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000704 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
705 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000706 int err, cpu = smp_processor_id();
Peter Zijlstra724a8682017-09-20 19:00:18 +0200707 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* Ensure this CPU doesn't handle any more interrupts. */
710 err = __cpu_disable();
711 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Thomas Gleixnera7246322016-08-12 19:49:38 +0200714 /*
715 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
716 * do this step again.
717 */
718 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
719 st->state--;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000720 /* Invoke the former CPU_DYING callbacks */
Peter Zijlstra724a8682017-09-20 19:00:18 +0200721 for (; st->state > target; st->state--) {
722 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
723 /*
724 * DYING must not fail!
725 */
726 WARN_ON_ONCE(ret);
727 }
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000728
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200729 /* Give up timekeeping duties */
730 tick_handover_do_timer();
Thomas Gleixner14e568e2013-01-31 12:11:14 +0000731 /* Park the stopper thread */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000732 stop_machine_park(cpu);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Thomas Gleixner98458172016-02-26 18:43:25 +0000736static int takedown_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000738 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000739 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Thomas Gleixner2a58c522016-03-10 20:42:08 +0100741 /* Park the smpboot threads */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000742 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
Thomas Gleixner2a58c522016-03-10 20:42:08 +0100743 smpboot_park_threads(cpu);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000744
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200745 /*
Thomas Gleixnera8994182015-07-05 17:12:30 +0000746 * Prevent irq alloc/free while the dying cpu reorganizes the
747 * interrupt affinities.
748 */
749 irq_lock_sparse();
750
751 /*
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200752 * So now all preempt/rcu users must observe !cpu_active().
753 */
Sebastian Andrzej Siewior210e2132017-05-24 10:15:28 +0200754 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
Rusty Russell04321582008-07-28 12:16:29 -0500755 if (err) {
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200756 /* CPU refused to die */
Thomas Gleixnera8994182015-07-05 17:12:30 +0000757 irq_unlock_sparse();
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200758 /* Unpark the hotplug thread so we can rollback there */
759 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
Thomas Gleixner98458172016-02-26 18:43:25 +0000760 return err;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700761 }
Rusty Russell04321582008-07-28 12:16:29 -0500762 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100764 /*
Thomas Gleixneree1e7142016-08-18 14:57:16 +0200765 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100766 * runnable tasks from the cpu, there's only the idle task left now
767 * that the migration thread is done doing the stop_machine thing.
Peter Zijlstra51a96c72010-11-19 20:37:53 +0100768 *
769 * Wait for the stop thread to go away.
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100770 */
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200771 wait_for_ap_thread(st, false);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000772 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Thomas Gleixnera8994182015-07-05 17:12:30 +0000774 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
775 irq_unlock_sparse();
776
Preeti U Murthy345527b2015-03-30 14:59:19 +0530777 hotplug_cpu__broadcast_tick_pull(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* This actually kills the CPU. */
779 __cpu_die(cpu);
780
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200781 tick_cleanup_dead_cpu(cpu);
Paul E. McKenneya58163d2017-06-20 12:11:34 -0700782 rcutree_migrate_callbacks(cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000783 return 0;
784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100786static void cpuhp_complete_idle_dead(void *arg)
787{
788 struct cpuhp_cpu_state *st = arg;
789
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200790 complete_ap_thread(st, false);
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100791}
792
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000793void cpuhp_report_idle_dead(void)
794{
795 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
796
797 BUG_ON(st->state != CPUHP_AP_OFFLINE);
Thomas Gleixner27d50c72016-02-26 18:43:44 +0000798 rcu_report_dead(smp_processor_id());
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100799 st->state = CPUHP_AP_IDLE_DEAD;
800 /*
801 * We cannot call complete after rcu_report_dead() so we delegate it
802 * to an online cpu.
803 */
804 smp_call_function_single(cpumask_first(cpu_online_mask),
805 cpuhp_complete_idle_dead, st, 0);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000806}
807
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200808static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
809{
810 for (st->state++; st->state < st->target; st->state++) {
811 struct cpuhp_step *step = cpuhp_get_step(st->state);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000812
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200813 if (!step->skip_onerr)
814 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
815 }
816}
817
818static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
819 enum cpuhp_state target)
820{
821 enum cpuhp_state prev_state = st->state;
822 int ret = 0;
823
824 for (; st->state > target; st->state--) {
825 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
826 if (ret) {
827 st->target = prev_state;
828 undo_cpu_down(cpu, st);
829 break;
830 }
831 }
832 return ret;
833}
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000834
Thomas Gleixner98458172016-02-26 18:43:25 +0000835/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000836static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
837 enum cpuhp_state target)
Thomas Gleixner98458172016-02-26 18:43:25 +0000838{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000839 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
840 int prev_state, ret = 0;
Thomas Gleixner98458172016-02-26 18:43:25 +0000841
842 if (num_online_cpus() == 1)
843 return -EBUSY;
844
Thomas Gleixner757c9892016-02-26 18:43:32 +0000845 if (!cpu_present(cpu))
Thomas Gleixner98458172016-02-26 18:43:25 +0000846 return -EINVAL;
847
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200848 cpus_write_lock();
Thomas Gleixner98458172016-02-26 18:43:25 +0000849
850 cpuhp_tasks_frozen = tasks_frozen;
851
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200852 prev_state = cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000853 /*
854 * If the current CPU state is in the range of the AP hotplug thread,
855 * then we need to kick the thread.
856 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000857 if (st->state > CPUHP_TEARDOWN_CPU) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200858 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000859 ret = cpuhp_kick_ap_work(cpu);
860 /*
861 * The AP side has done the error rollback already. Just
862 * return the error code..
863 */
864 if (ret)
865 goto out;
866
867 /*
868 * We might have stopped still in the range of the AP hotplug
869 * thread. Nothing to do anymore.
870 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000871 if (st->state > CPUHP_TEARDOWN_CPU)
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000872 goto out;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200873
874 st->target = target;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000875 }
876 /*
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000877 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000878 * to do the further cleanups.
879 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200880 ret = cpuhp_down_callbacks(cpu, st, target);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200881 if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200882 cpuhp_reset_state(st, prev_state);
883 __cpuhp_kick_ap(st);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200884 }
Thomas Gleixner98458172016-02-26 18:43:25 +0000885
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000886out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200887 cpus_write_unlock();
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000888 return ret;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700889}
890
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000891static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700892{
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100893 int err;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700894
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100895 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700896
Max Krasnyanskye761b772008-07-15 04:43:49 -0700897 if (cpu_hotplug_disabled) {
898 err = -EBUSY;
899 goto out;
900 }
901
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000902 err = _cpu_down(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -0700903
Max Krasnyanskye761b772008-07-15 04:43:49 -0700904out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100905 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return err;
907}
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200908
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000909int cpu_down(unsigned int cpu)
910{
911 return do_cpu_down(cpu, CPUHP_OFFLINE);
912}
Zhang Ruib62b8ef2008-04-29 02:35:56 -0400913EXPORT_SYMBOL(cpu_down);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200914
915#else
916#define takedown_cpu NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917#endif /*CONFIG_HOTPLUG_CPU*/
918
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000919/**
Thomas Gleixneree1e7142016-08-18 14:57:16 +0200920 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000921 * @cpu: cpu that just started
922 *
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000923 * It must be called by the arch code on the new cpu, before the new cpu
924 * enables interrupts and before the "boot" cpu returns from __cpu_up().
925 */
926void notify_cpu_starting(unsigned int cpu)
927{
928 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
929 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
Peter Zijlstra724a8682017-09-20 19:00:18 +0200930 int ret;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000931
Sebastian Andrzej Siewior0c6d4572016-08-17 14:21:04 +0200932 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000933 while (st->state < target) {
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000934 st->state++;
Peter Zijlstra724a8682017-09-20 19:00:18 +0200935 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
936 /*
937 * STARTING must not fail!
938 */
939 WARN_ON_ONCE(ret);
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000940 }
941}
942
Thomas Gleixner949338e2016-02-26 18:43:35 +0000943/*
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200944 * Called from the idle task. Wake up the controlling task which brings the
945 * stopper and the hotplug thread of the upcoming CPU up and then delegates
946 * the rest of the online bringup to the hotplug thread.
Thomas Gleixner949338e2016-02-26 18:43:35 +0000947 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000948void cpuhp_online_idle(enum cpuhp_state state)
Thomas Gleixner949338e2016-02-26 18:43:35 +0000949{
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000950 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000951
952 /* Happens for the boot cpu */
953 if (state != CPUHP_AP_ONLINE_IDLE)
954 return;
955
956 st->state = CPUHP_AP_ONLINE_IDLE;
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200957 complete_ap_thread(st, true);
Thomas Gleixner949338e2016-02-26 18:43:35 +0000958}
959
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700960/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000961static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000963 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700964 struct task_struct *idle;
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000965 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200967 cpus_write_lock();
Thomas Gleixner38498a62012-04-20 13:05:44 +0000968
Thomas Gleixner757c9892016-02-26 18:43:32 +0000969 if (!cpu_present(cpu)) {
Yasuaki Ishimatsu5e5041f2012-10-23 01:30:54 +0200970 ret = -EINVAL;
971 goto out;
972 }
973
Thomas Gleixner757c9892016-02-26 18:43:32 +0000974 /*
975 * The caller of do_cpu_up might have raced with another
976 * caller. Ignore it for now.
977 */
978 if (st->state >= target)
Thomas Gleixner38498a62012-04-20 13:05:44 +0000979 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +0000980
981 if (st->state == CPUHP_OFFLINE) {
982 /* Let it fail before we try to bring the cpu up */
983 idle = idle_thread_get(cpu);
984 if (IS_ERR(idle)) {
985 ret = PTR_ERR(idle);
986 goto out;
987 }
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700988 }
Thomas Gleixner38498a62012-04-20 13:05:44 +0000989
Thomas Gleixnerba997462016-02-26 18:43:24 +0000990 cpuhp_tasks_frozen = tasks_frozen;
991
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200992 cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000993 /*
994 * If the current CPU state is in the range of the AP hotplug thread,
995 * then we need to kick the thread once more.
996 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000997 if (st->state > CPUHP_BRINGUP_CPU) {
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000998 ret = cpuhp_kick_ap_work(cpu);
999 /*
1000 * The AP side has done the error rollback already. Just
1001 * return the error code..
1002 */
1003 if (ret)
1004 goto out;
1005 }
1006
1007 /*
1008 * Try to reach the target state. We max out on the BP at
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001009 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001010 * responsible for bringing it up to the target state.
1011 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001012 target = min((int)target, CPUHP_BRINGUP_CPU);
Thomas Gleixnera7246322016-08-12 19:49:38 +02001013 ret = cpuhp_up_callbacks(cpu, st, target);
Thomas Gleixner38498a62012-04-20 13:05:44 +00001014out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001015 cpus_write_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return ret;
1017}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001018
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001019static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001020{
1021 int err = 0;
minskey guocf234222010-05-24 14:32:41 -07001022
Rusty Russelle0b582e2009-01-01 10:12:28 +10301023 if (!cpu_possible(cpu)) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001024 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1025 cpu);
Chen Gong87d5e0232010-03-05 13:42:38 -08001026#if defined(CONFIG_IA64)
Fabian Frederick84117da2014-06-04 16:11:17 -07001027 pr_err("please check additional_cpus= boot parameter\n");
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -07001028#endif
1029 return -EINVAL;
1030 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001031
Toshi Kani01b0f192013-11-12 15:07:25 -08001032 err = try_online_node(cpu_to_node(cpu));
1033 if (err)
1034 return err;
minskey guocf234222010-05-24 14:32:41 -07001035
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001036 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001037
Max Krasnyanskye761b772008-07-15 04:43:49 -07001038 if (cpu_hotplug_disabled) {
1039 err = -EBUSY;
1040 goto out;
1041 }
1042
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001043 err = _cpu_up(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -07001044out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001045 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001046 return err;
1047}
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001048
1049int cpu_up(unsigned int cpu)
1050{
1051 return do_cpu_up(cpu, CPUHP_ONLINE);
1052}
Paul E. McKenneya513f6b2011-12-11 21:54:45 -08001053EXPORT_SYMBOL_GPL(cpu_up);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001054
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001055#ifdef CONFIG_PM_SLEEP_SMP
Rusty Russelle0b582e2009-01-01 10:12:28 +10301056static cpumask_var_t frozen_cpus;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001057
James Morsed391e552016-08-17 13:50:25 +01001058int freeze_secondary_cpus(int primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001059{
James Morsed391e552016-08-17 13:50:25 +01001060 int cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001061
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001062 cpu_maps_update_begin();
James Morsed391e552016-08-17 13:50:25 +01001063 if (!cpu_online(primary))
1064 primary = cpumask_first(cpu_online_mask);
Xiaotian Feng9ee349a2009-12-16 18:04:32 +01001065 /*
1066 * We take down all of the non-boot CPUs in one shot to avoid races
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001067 * with the userspace trying to use the CPU hotplug at the same time
1068 */
Rusty Russelle0b582e2009-01-01 10:12:28 +10301069 cpumask_clear(frozen_cpus);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01001070
Fabian Frederick84117da2014-06-04 16:11:17 -07001071 pr_info("Disabling non-boot CPUs ...\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001072 for_each_online_cpu(cpu) {
James Morsed391e552016-08-17 13:50:25 +01001073 if (cpu == primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001074 continue;
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001075 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001076 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001077 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
Mike Travisfeae3202009-11-17 18:22:13 -06001078 if (!error)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301079 cpumask_set_cpu(cpu, frozen_cpus);
Mike Travisfeae3202009-11-17 18:22:13 -06001080 else {
Fabian Frederick84117da2014-06-04 16:11:17 -07001081 pr_err("Error taking CPU%d down: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001082 break;
1083 }
1084 }
Joseph Cihula86886e52009-06-30 19:31:07 -07001085
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001086 if (!error)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001087 BUG_ON(num_online_cpus() > 1);
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001088 else
Fabian Frederick84117da2014-06-04 16:11:17 -07001089 pr_err("Non-boot CPUs are not disabled\n");
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001090
1091 /*
1092 * Make sure the CPUs won't be enabled by someone else. We need to do
1093 * this even in case of failure as all disable_nonboot_cpus() users are
1094 * supposed to do enable_nonboot_cpus() on the failure path.
1095 */
1096 cpu_hotplug_disabled++;
1097
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001098 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001099 return error;
1100}
1101
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001102void __weak arch_enable_nonboot_cpus_begin(void)
1103{
1104}
1105
1106void __weak arch_enable_nonboot_cpus_end(void)
1107{
1108}
1109
Mathias Krause71cf5ae2015-07-19 20:06:22 +02001110void enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001111{
1112 int cpu, error;
1113
1114 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001115 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -07001116 __cpu_hotplug_enable();
Rusty Russelle0b582e2009-01-01 10:12:28 +10301117 if (cpumask_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001118 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001119
Fabian Frederick84117da2014-06-04 16:11:17 -07001120 pr_info("Enabling non-boot CPUs ...\n");
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001121
1122 arch_enable_nonboot_cpus_begin();
1123
Rusty Russelle0b582e2009-01-01 10:12:28 +10301124 for_each_cpu(cpu, frozen_cpus) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001125 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001126 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001127 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001128 if (!error) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001129 pr_info("CPU%d is up\n", cpu);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001130 continue;
1131 }
Fabian Frederick84117da2014-06-04 16:11:17 -07001132 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001133 }
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001134
1135 arch_enable_nonboot_cpus_end();
1136
Rusty Russelle0b582e2009-01-01 10:12:28 +10301137 cpumask_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001138out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001139 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001140}
Rusty Russelle0b582e2009-01-01 10:12:28 +10301141
Fenghua Yud7268a32011-11-15 21:59:31 +01001142static int __init alloc_frozen_cpus(void)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301143{
1144 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1145 return -ENOMEM;
1146 return 0;
1147}
1148core_initcall(alloc_frozen_cpus);
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001149
1150/*
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001151 * When callbacks for CPU hotplug notifications are being executed, we must
1152 * ensure that the state of the system with respect to the tasks being frozen
1153 * or not, as reported by the notification, remains unchanged *throughout the
1154 * duration* of the execution of the callbacks.
1155 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1156 *
1157 * This synchronization is implemented by mutually excluding regular CPU
1158 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1159 * Hibernate notifications.
1160 */
1161static int
1162cpu_hotplug_pm_callback(struct notifier_block *nb,
1163 unsigned long action, void *ptr)
1164{
1165 switch (action) {
1166
1167 case PM_SUSPEND_PREPARE:
1168 case PM_HIBERNATION_PREPARE:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001169 cpu_hotplug_disable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001170 break;
1171
1172 case PM_POST_SUSPEND:
1173 case PM_POST_HIBERNATION:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001174 cpu_hotplug_enable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001175 break;
1176
1177 default:
1178 return NOTIFY_DONE;
1179 }
1180
1181 return NOTIFY_OK;
1182}
1183
1184
Fenghua Yud7268a32011-11-15 21:59:31 +01001185static int __init cpu_hotplug_pm_sync_init(void)
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001186{
Fenghua Yu6e32d472012-11-13 11:32:43 -08001187 /*
1188 * cpu_hotplug_pm_callback has higher priority than x86
1189 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1190 * to disable cpu hotplug to avoid cpu hotplug race.
1191 */
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001192 pm_notifier(cpu_hotplug_pm_callback, 0);
1193 return 0;
1194}
1195core_initcall(cpu_hotplug_pm_sync_init);
1196
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001197#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001198
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01001199int __boot_cpu_id;
1200
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001201#endif /* CONFIG_SMP */
Mike Travisb8d317d2008-07-24 18:21:29 -07001202
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001203/* Boot processor state steps */
1204static struct cpuhp_step cpuhp_bp_states[] = {
1205 [CPUHP_OFFLINE] = {
1206 .name = "offline",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001207 .startup.single = NULL,
1208 .teardown.single = NULL,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001209 },
1210#ifdef CONFIG_SMP
1211 [CPUHP_CREATE_THREADS]= {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001212 .name = "threads:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001213 .startup.single = smpboot_create_threads,
1214 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001215 .cant_stop = true,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001216 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001217 [CPUHP_PERF_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001218 .name = "perf:prepare",
1219 .startup.single = perf_event_init_cpu,
1220 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001221 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001222 [CPUHP_WORKQUEUE_PREP] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001223 .name = "workqueue:prepare",
1224 .startup.single = workqueue_prepare_cpu,
1225 .teardown.single = NULL,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001226 },
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001227 [CPUHP_HRTIMERS_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001228 .name = "hrtimers:prepare",
1229 .startup.single = hrtimers_prepare_cpu,
1230 .teardown.single = hrtimers_dead_cpu,
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001231 },
Richard Weinberger31487f82016-07-13 17:17:01 +00001232 [CPUHP_SMPCFD_PREPARE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001233 .name = "smpcfd:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001234 .startup.single = smpcfd_prepare_cpu,
1235 .teardown.single = smpcfd_dead_cpu,
Richard Weinberger31487f82016-07-13 17:17:01 +00001236 },
Richard Weinbergere6d49892016-08-18 14:57:17 +02001237 [CPUHP_RELAY_PREPARE] = {
1238 .name = "relay:prepare",
1239 .startup.single = relay_prepare_cpu,
1240 .teardown.single = NULL,
1241 },
Sebastian Andrzej Siewior6731d4f2016-08-23 14:53:19 +02001242 [CPUHP_SLAB_PREPARE] = {
1243 .name = "slab:prepare",
1244 .startup.single = slab_prepare_cpu,
1245 .teardown.single = slab_dead_cpu,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001246 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001247 [CPUHP_RCUTREE_PREP] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001248 .name = "RCU/tree:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001249 .startup.single = rcutree_prepare_cpu,
1250 .teardown.single = rcutree_dead_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001251 },
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001252 /*
Richard Cochran4fae16d2016-07-27 11:08:18 +02001253 * On the tear-down path, timers_dead_cpu() must be invoked
1254 * before blk_mq_queue_reinit_notify() from notify_dead(),
1255 * otherwise a RCU stall occurs.
1256 */
1257 [CPUHP_TIMERS_DEAD] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001258 .name = "timers:dead",
1259 .startup.single = NULL,
1260 .teardown.single = timers_dead_cpu,
Richard Cochran4fae16d2016-07-27 11:08:18 +02001261 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001262 /* Kicks the plugged cpu into life */
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001263 [CPUHP_BRINGUP_CPU] = {
1264 .name = "cpu:bringup",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001265 .startup.single = bringup_cpu,
1266 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001267 .cant_stop = true,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001268 },
Richard Weinberger31487f82016-07-13 17:17:01 +00001269 [CPUHP_AP_SMPCFD_DYING] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001270 .name = "smpcfd:dying",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001271 .startup.single = NULL,
1272 .teardown.single = smpcfd_dying_cpu,
Richard Weinberger31487f82016-07-13 17:17:01 +00001273 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001274 /*
1275 * Handled on controll processor until the plugged processor manages
1276 * this itself.
1277 */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001278 [CPUHP_TEARDOWN_CPU] = {
1279 .name = "cpu:teardown",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001280 .startup.single = NULL,
1281 .teardown.single = takedown_cpu,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001282 .cant_stop = true,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001283 },
Thomas Gleixnera7c734142016-07-12 21:59:23 +02001284#else
1285 [CPUHP_BRINGUP_CPU] = { },
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001286#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001287};
1288
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001289/* Application processor state steps */
1290static struct cpuhp_step cpuhp_ap_states[] = {
1291#ifdef CONFIG_SMP
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001292 /* Final state before CPU kills itself */
1293 [CPUHP_AP_IDLE_DEAD] = {
1294 .name = "idle:dead",
1295 },
1296 /*
1297 * Last state before CPU enters the idle loop to die. Transient state
1298 * for synchronization.
1299 */
1300 [CPUHP_AP_OFFLINE] = {
1301 .name = "ap:offline",
1302 .cant_stop = true,
1303 },
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001304 /* First state is scheduler control. Interrupts are disabled */
1305 [CPUHP_AP_SCHED_STARTING] = {
1306 .name = "sched:starting",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001307 .startup.single = sched_cpu_starting,
1308 .teardown.single = sched_cpu_dying,
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001309 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001310 [CPUHP_AP_RCUTREE_DYING] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001311 .name = "RCU/tree:dying",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001312 .startup.single = NULL,
1313 .teardown.single = rcutree_dying_cpu,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001314 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001315 /* Entry state on starting. Interrupts enabled from here on. Transient
1316 * state for synchronsization */
1317 [CPUHP_AP_ONLINE] = {
1318 .name = "ap:online",
1319 },
1320 /* Handle smpboot threads park/unpark */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001321 [CPUHP_AP_SMPBOOT_THREADS] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001322 .name = "smpboot/threads:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001323 .startup.single = smpboot_unpark_threads,
1324 .teardown.single = NULL,
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001325 },
Thomas Gleixnerc5cb83b2017-06-20 01:37:51 +02001326 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1327 .name = "irq/affinity:online",
1328 .startup.single = irq_affinity_online_cpu,
1329 .teardown.single = NULL,
1330 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001331 [CPUHP_AP_PERF_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001332 .name = "perf:online",
1333 .startup.single = perf_event_init_cpu,
1334 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001335 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001336 [CPUHP_AP_WORKQUEUE_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001337 .name = "workqueue:online",
1338 .startup.single = workqueue_online_cpu,
1339 .teardown.single = workqueue_offline_cpu,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001340 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001341 [CPUHP_AP_RCUTREE_ONLINE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001342 .name = "RCU/tree:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001343 .startup.single = rcutree_online_cpu,
1344 .teardown.single = rcutree_offline_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001345 },
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001346#endif
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001347 /*
1348 * The dynamically registered state space is here
1349 */
1350
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001351#ifdef CONFIG_SMP
1352 /* Last state is scheduler control setting the cpu active */
1353 [CPUHP_AP_ACTIVE] = {
1354 .name = "sched:active",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001355 .startup.single = sched_cpu_activate,
1356 .teardown.single = sched_cpu_deactivate,
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001357 },
1358#endif
1359
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001360 /* CPU is fully up and running. */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001361 [CPUHP_ONLINE] = {
1362 .name = "online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001363 .startup.single = NULL,
1364 .teardown.single = NULL,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001365 },
1366};
1367
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001368/* Sanity check for callbacks */
1369static int cpuhp_cb_check(enum cpuhp_state state)
1370{
1371 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1372 return -EINVAL;
1373 return 0;
1374}
1375
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001376/*
1377 * Returns a free for dynamic slot assignment of the Online state. The states
1378 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1379 * by having no name assigned.
1380 */
1381static int cpuhp_reserve_state(enum cpuhp_state state)
1382{
Thomas Gleixner4205e472017-01-10 14:01:05 +01001383 enum cpuhp_state i, end;
1384 struct cpuhp_step *step;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001385
Thomas Gleixner4205e472017-01-10 14:01:05 +01001386 switch (state) {
1387 case CPUHP_AP_ONLINE_DYN:
1388 step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN;
1389 end = CPUHP_AP_ONLINE_DYN_END;
1390 break;
1391 case CPUHP_BP_PREPARE_DYN:
1392 step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN;
1393 end = CPUHP_BP_PREPARE_DYN_END;
1394 break;
1395 default:
1396 return -EINVAL;
1397 }
1398
1399 for (i = state; i <= end; i++, step++) {
1400 if (!step->name)
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001401 return i;
1402 }
1403 WARN(1, "No more dynamic states available for CPU hotplug\n");
1404 return -ENOSPC;
1405}
1406
1407static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1408 int (*startup)(unsigned int cpu),
1409 int (*teardown)(unsigned int cpu),
1410 bool multi_instance)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001411{
1412 /* (Un)Install the callbacks for further cpu hotplug operations */
1413 struct cpuhp_step *sp;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001414 int ret = 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001415
Ethan Barnes0c96b272017-07-19 22:36:00 +00001416 /*
1417 * If name is NULL, then the state gets removed.
1418 *
1419 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1420 * the first allocation from these dynamic ranges, so the removal
1421 * would trigger a new allocation and clear the wrong (already
1422 * empty) state, leaving the callbacks of the to be cleared state
1423 * dangling, which causes wreckage on the next hotplug operation.
1424 */
1425 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1426 state == CPUHP_BP_PREPARE_DYN)) {
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001427 ret = cpuhp_reserve_state(state);
1428 if (ret < 0)
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001429 return ret;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001430 state = ret;
1431 }
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001432 sp = cpuhp_get_step(state);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001433 if (name && sp->name)
1434 return -EBUSY;
1435
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001436 sp->startup.single = startup;
1437 sp->teardown.single = teardown;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001438 sp->name = name;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001439 sp->multi_instance = multi_instance;
1440 INIT_HLIST_HEAD(&sp->list);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001441 return ret;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001442}
1443
1444static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1445{
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001446 return cpuhp_get_step(state)->teardown.single;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001447}
1448
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001449/*
1450 * Call the startup/teardown function for a step either on the AP or
1451 * on the current CPU.
1452 */
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001453static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1454 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001455{
Thomas Gleixnera7246322016-08-12 19:49:38 +02001456 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001457 int ret;
1458
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001459 /*
1460 * If there's nothing to do, we done.
1461 * Relies on the union for multi_instance.
1462 */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001463 if ((bringup && !sp->startup.single) ||
1464 (!bringup && !sp->teardown.single))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001465 return 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001466 /*
1467 * The non AP bound callbacks can fail on bringup. On teardown
1468 * e.g. module removal we crash for now.
1469 */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001470#ifdef CONFIG_SMP
1471 if (cpuhp_is_ap_state(state))
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001472 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001473 else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001474 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001475#else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001476 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001477#endif
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001478 BUG_ON(ret && !bringup);
1479 return ret;
1480}
1481
1482/*
1483 * Called from __cpuhp_setup_state on a recoverable failure.
1484 *
1485 * Note: The teardown callbacks for rollback are not allowed to fail!
1486 */
1487static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001488 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001489{
1490 int cpu;
1491
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001492 /* Roll back the already executed steps on the other cpus */
1493 for_each_present_cpu(cpu) {
1494 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1495 int cpustate = st->state;
1496
1497 if (cpu >= failedcpu)
1498 break;
1499
1500 /* Did we invoke the startup call on that cpu ? */
1501 if (cpustate >= state)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001502 cpuhp_issue_call(cpu, state, false, node);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001503 }
1504}
1505
Thomas Gleixner9805c672017-05-24 10:15:15 +02001506int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1507 struct hlist_node *node,
1508 bool invoke)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001509{
1510 struct cpuhp_step *sp;
1511 int cpu;
1512 int ret;
1513
Thomas Gleixner9805c672017-05-24 10:15:15 +02001514 lockdep_assert_cpus_held();
1515
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001516 sp = cpuhp_get_step(state);
1517 if (sp->multi_instance == false)
1518 return -EINVAL;
1519
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001520 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001521
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001522 if (!invoke || !sp->startup.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001523 goto add_node;
1524
1525 /*
1526 * Try to call the startup callback for each present cpu
1527 * depending on the hotplug state of the cpu.
1528 */
1529 for_each_present_cpu(cpu) {
1530 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1531 int cpustate = st->state;
1532
1533 if (cpustate < state)
1534 continue;
1535
1536 ret = cpuhp_issue_call(cpu, state, true, node);
1537 if (ret) {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001538 if (sp->teardown.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001539 cpuhp_rollback_install(cpu, state, node);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001540 goto unlock;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001541 }
1542 }
1543add_node:
1544 ret = 0;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001545 hlist_add_head(node, &sp->list);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001546unlock:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001547 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner9805c672017-05-24 10:15:15 +02001548 return ret;
1549}
1550
1551int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1552 bool invoke)
1553{
1554 int ret;
1555
1556 cpus_read_lock();
1557 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001558 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001559 return ret;
1560}
1561EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1562
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001563/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001564 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001565 * @state: The state to setup
1566 * @invoke: If true, the startup function is invoked for cpus where
1567 * cpu state >= @state
1568 * @startup: startup callback function
1569 * @teardown: teardown callback function
1570 * @multi_instance: State is set up for multiple instances which get
1571 * added afterwards.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001572 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001573 * The caller needs to hold cpus read locked while calling this function.
Boris Ostrovsky512f0982016-12-15 10:00:57 -05001574 * Returns:
1575 * On success:
1576 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1577 * 0 for all other states
1578 * On failure: proper (negative) error code
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001579 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001580int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
1581 const char *name, bool invoke,
1582 int (*startup)(unsigned int cpu),
1583 int (*teardown)(unsigned int cpu),
1584 bool multi_instance)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001585{
1586 int cpu, ret = 0;
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001587 bool dynstate;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001588
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001589 lockdep_assert_cpus_held();
1590
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001591 if (cpuhp_cb_check(state) || !name)
1592 return -EINVAL;
1593
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001594 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001595
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001596 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1597 multi_instance);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001598
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001599 dynstate = state == CPUHP_AP_ONLINE_DYN;
1600 if (ret > 0 && dynstate) {
1601 state = ret;
1602 ret = 0;
1603 }
1604
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001605 if (ret || !invoke || !startup)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001606 goto out;
1607
1608 /*
1609 * Try to call the startup callback for each present cpu
1610 * depending on the hotplug state of the cpu.
1611 */
1612 for_each_present_cpu(cpu) {
1613 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1614 int cpustate = st->state;
1615
1616 if (cpustate < state)
1617 continue;
1618
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001619 ret = cpuhp_issue_call(cpu, state, true, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001620 if (ret) {
Thomas Gleixnera7246322016-08-12 19:49:38 +02001621 if (teardown)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001622 cpuhp_rollback_install(cpu, state, NULL);
1623 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001624 goto out;
1625 }
1626 }
1627out:
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001628 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001629 /*
1630 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1631 * dynamically allocated state in case of success.
1632 */
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001633 if (!ret && dynstate)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001634 return state;
1635 return ret;
1636}
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001637EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1638
1639int __cpuhp_setup_state(enum cpuhp_state state,
1640 const char *name, bool invoke,
1641 int (*startup)(unsigned int cpu),
1642 int (*teardown)(unsigned int cpu),
1643 bool multi_instance)
1644{
1645 int ret;
1646
1647 cpus_read_lock();
1648 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1649 teardown, multi_instance);
1650 cpus_read_unlock();
1651 return ret;
1652}
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001653EXPORT_SYMBOL(__cpuhp_setup_state);
1654
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001655int __cpuhp_state_remove_instance(enum cpuhp_state state,
1656 struct hlist_node *node, bool invoke)
1657{
1658 struct cpuhp_step *sp = cpuhp_get_step(state);
1659 int cpu;
1660
1661 BUG_ON(cpuhp_cb_check(state));
1662
1663 if (!sp->multi_instance)
1664 return -EINVAL;
1665
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001666 cpus_read_lock();
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001667 mutex_lock(&cpuhp_state_mutex);
1668
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001669 if (!invoke || !cpuhp_get_teardown_cb(state))
1670 goto remove;
1671 /*
1672 * Call the teardown callback for each present cpu depending
1673 * on the hotplug state of the cpu. This function is not
1674 * allowed to fail currently!
1675 */
1676 for_each_present_cpu(cpu) {
1677 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1678 int cpustate = st->state;
1679
1680 if (cpustate >= state)
1681 cpuhp_issue_call(cpu, state, false, node);
1682 }
1683
1684remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001685 hlist_del(node);
1686 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001687 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001688
1689 return 0;
1690}
1691EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001692
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001693/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001694 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001695 * @state: The state to remove
1696 * @invoke: If true, the teardown function is invoked for cpus where
1697 * cpu state >= @state
1698 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001699 * The caller needs to hold cpus read locked while calling this function.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001700 * The teardown callback is currently not allowed to fail. Think
1701 * about module removal!
1702 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001703void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001704{
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001705 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001706 int cpu;
1707
1708 BUG_ON(cpuhp_cb_check(state));
1709
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001710 lockdep_assert_cpus_held();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001711
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001712 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001713 if (sp->multi_instance) {
1714 WARN(!hlist_empty(&sp->list),
1715 "Error: Removing state %d which has instances left.\n",
1716 state);
1717 goto remove;
1718 }
1719
Thomas Gleixnera7246322016-08-12 19:49:38 +02001720 if (!invoke || !cpuhp_get_teardown_cb(state))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001721 goto remove;
1722
1723 /*
1724 * Call the teardown callback for each present cpu depending
1725 * on the hotplug state of the cpu. This function is not
1726 * allowed to fail currently!
1727 */
1728 for_each_present_cpu(cpu) {
1729 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1730 int cpustate = st->state;
1731
1732 if (cpustate >= state)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001733 cpuhp_issue_call(cpu, state, false, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001734 }
1735remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001736 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001737 mutex_unlock(&cpuhp_state_mutex);
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001738}
1739EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1740
1741void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1742{
1743 cpus_read_lock();
1744 __cpuhp_remove_state_cpuslocked(state, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001745 cpus_read_unlock();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001746}
1747EXPORT_SYMBOL(__cpuhp_remove_state);
1748
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001749#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1750static ssize_t show_cpuhp_state(struct device *dev,
1751 struct device_attribute *attr, char *buf)
1752{
1753 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1754
1755 return sprintf(buf, "%d\n", st->state);
1756}
1757static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1758
Thomas Gleixner757c9892016-02-26 18:43:32 +00001759static ssize_t write_cpuhp_target(struct device *dev,
1760 struct device_attribute *attr,
1761 const char *buf, size_t count)
1762{
1763 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1764 struct cpuhp_step *sp;
1765 int target, ret;
1766
1767 ret = kstrtoint(buf, 10, &target);
1768 if (ret)
1769 return ret;
1770
1771#ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1772 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1773 return -EINVAL;
1774#else
1775 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1776 return -EINVAL;
1777#endif
1778
1779 ret = lock_device_hotplug_sysfs();
1780 if (ret)
1781 return ret;
1782
1783 mutex_lock(&cpuhp_state_mutex);
1784 sp = cpuhp_get_step(target);
1785 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1786 mutex_unlock(&cpuhp_state_mutex);
1787 if (ret)
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001788 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +00001789
1790 if (st->state < target)
1791 ret = do_cpu_up(dev->id, target);
1792 else
1793 ret = do_cpu_down(dev->id, target);
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001794out:
Thomas Gleixner757c9892016-02-26 18:43:32 +00001795 unlock_device_hotplug();
1796 return ret ? ret : count;
1797}
1798
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001799static ssize_t show_cpuhp_target(struct device *dev,
1800 struct device_attribute *attr, char *buf)
1801{
1802 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1803
1804 return sprintf(buf, "%d\n", st->target);
1805}
Thomas Gleixner757c9892016-02-26 18:43:32 +00001806static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001807
1808static struct attribute *cpuhp_cpu_attrs[] = {
1809 &dev_attr_state.attr,
1810 &dev_attr_target.attr,
1811 NULL
1812};
1813
Arvind Yadav993647a2017-06-29 17:40:47 +05301814static const struct attribute_group cpuhp_cpu_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001815 .attrs = cpuhp_cpu_attrs,
1816 .name = "hotplug",
1817 NULL
1818};
1819
1820static ssize_t show_cpuhp_states(struct device *dev,
1821 struct device_attribute *attr, char *buf)
1822{
1823 ssize_t cur, res = 0;
1824 int i;
1825
1826 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner757c9892016-02-26 18:43:32 +00001827 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001828 struct cpuhp_step *sp = cpuhp_get_step(i);
1829
1830 if (sp->name) {
1831 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
1832 buf += cur;
1833 res += cur;
1834 }
1835 }
1836 mutex_unlock(&cpuhp_state_mutex);
1837 return res;
1838}
1839static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
1840
1841static struct attribute *cpuhp_cpu_root_attrs[] = {
1842 &dev_attr_states.attr,
1843 NULL
1844};
1845
Arvind Yadav993647a2017-06-29 17:40:47 +05301846static const struct attribute_group cpuhp_cpu_root_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001847 .attrs = cpuhp_cpu_root_attrs,
1848 .name = "hotplug",
1849 NULL
1850};
1851
1852static int __init cpuhp_sysfs_init(void)
1853{
1854 int cpu, ret;
1855
1856 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
1857 &cpuhp_cpu_root_attr_group);
1858 if (ret)
1859 return ret;
1860
1861 for_each_possible_cpu(cpu) {
1862 struct device *dev = get_cpu_device(cpu);
1863
1864 if (!dev)
1865 continue;
1866 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
1867 if (ret)
1868 return ret;
1869 }
1870 return 0;
1871}
1872device_initcall(cpuhp_sysfs_init);
1873#endif
1874
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001875/*
1876 * cpu_bit_bitmap[] is a special, "compressed" data structure that
1877 * represents all NR_CPUS bits binary values of 1<<nr.
1878 *
Rusty Russelle0b582e2009-01-01 10:12:28 +10301879 * It is used by cpumask_of() to get a constant address to a CPU
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001880 * mask value that has a single bit set only.
1881 */
Mike Travisb8d317d2008-07-24 18:21:29 -07001882
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001883/* cpu_bit_bitmap[0] is empty - so we can back into it */
Michael Rodriguez4d519852011-03-22 16:34:07 -07001884#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001885#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
1886#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
1887#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
Mike Travisb8d317d2008-07-24 18:21:29 -07001888
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001889const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
Mike Travisb8d317d2008-07-24 18:21:29 -07001890
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001891 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
1892 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
1893#if BITS_PER_LONG > 32
1894 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
1895 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
Mike Travisb8d317d2008-07-24 18:21:29 -07001896#endif
1897};
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001898EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001899
1900const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
1901EXPORT_SYMBOL(cpu_all_bits);
Rusty Russellb3199c02008-12-30 09:05:14 +10301902
1903#ifdef CONFIG_INIT_ALL_POSSIBLE
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001904struct cpumask __cpu_possible_mask __read_mostly
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001905 = {CPU_BITS_ALL};
Rusty Russellb3199c02008-12-30 09:05:14 +10301906#else
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001907struct cpumask __cpu_possible_mask __read_mostly;
Rusty Russellb3199c02008-12-30 09:05:14 +10301908#endif
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001909EXPORT_SYMBOL(__cpu_possible_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301910
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001911struct cpumask __cpu_online_mask __read_mostly;
1912EXPORT_SYMBOL(__cpu_online_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301913
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001914struct cpumask __cpu_present_mask __read_mostly;
1915EXPORT_SYMBOL(__cpu_present_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301916
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001917struct cpumask __cpu_active_mask __read_mostly;
1918EXPORT_SYMBOL(__cpu_active_mask);
Rusty Russell3fa41522008-12-30 09:05:16 +10301919
Rusty Russell3fa41522008-12-30 09:05:16 +10301920void init_cpu_present(const struct cpumask *src)
1921{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001922 cpumask_copy(&__cpu_present_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301923}
1924
1925void init_cpu_possible(const struct cpumask *src)
1926{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001927 cpumask_copy(&__cpu_possible_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301928}
1929
1930void init_cpu_online(const struct cpumask *src)
1931{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001932 cpumask_copy(&__cpu_online_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301933}
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001934
1935/*
1936 * Activate the first processor.
1937 */
1938void __init boot_cpu_init(void)
1939{
1940 int cpu = smp_processor_id();
1941
1942 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
1943 set_cpu_online(cpu, true);
1944 set_cpu_active(cpu, true);
1945 set_cpu_present(cpu, true);
1946 set_cpu_possible(cpu, true);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01001947
1948#ifdef CONFIG_SMP
1949 __boot_cpu_id = cpu;
1950#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001951}
1952
1953/*
1954 * Must be called _AFTER_ setting up the per_cpu areas
1955 */
1956void __init boot_cpu_state_init(void)
1957{
1958 per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
1959}