blob: 1139063de5afbe3a1459f4fa91751911bb2a7177 [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
49 * @done: Signal completion to the issuer of the task
Thomas Gleixnercff7d372016-02-26 18:43:28 +000050 */
51struct cpuhp_cpu_state {
52 enum cpuhp_state state;
53 enum cpuhp_state target;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000054#ifdef CONFIG_SMP
55 struct task_struct *thread;
56 bool should_run;
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +020057 bool rollback;
Thomas Gleixnera7246322016-08-12 19:49:38 +020058 bool single;
59 bool bringup;
Thomas Gleixnercf392d12016-08-12 19:49:39 +020060 struct hlist_node *node;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +020061 struct hlist_node *last;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000062 enum cpuhp_state cb_state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000063 int result;
64 struct completion done;
65#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +000066};
67
68static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
69
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +020070#if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
71static struct lock_class_key cpuhp_state_key;
72static struct lockdep_map cpuhp_state_lock_map =
73 STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key);
74#endif
75
Thomas Gleixnercff7d372016-02-26 18:43:28 +000076/**
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
Thomas Gleixner757c9892016-02-26 18:43:32 +000083 * @cant_stop: Bringup/teardown can't be stopped at this step
Thomas Gleixnercff7d372016-02-26 18:43:28 +000084 */
85struct cpuhp_step {
Thomas Gleixnercf392d12016-08-12 19:49:39 +020086 const char *name;
87 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +020088 int (*single)(unsigned int cpu);
89 int (*multi)(unsigned int cpu,
90 struct hlist_node *node);
91 } startup;
Thomas Gleixnercf392d12016-08-12 19:49:39 +020092 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +020093 int (*single)(unsigned int cpu);
94 int (*multi)(unsigned int cpu,
95 struct hlist_node *node);
96 } teardown;
Thomas Gleixnercf392d12016-08-12 19:49:39 +020097 struct hlist_head list;
98 bool skip_onerr;
99 bool cant_stop;
100 bool multi_instance;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000101};
102
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +0000103static DEFINE_MUTEX(cpuhp_state_mutex);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000104static struct cpuhp_step cpuhp_bp_states[];
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000105static struct cpuhp_step cpuhp_ap_states[];
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000106
Thomas Gleixnera7246322016-08-12 19:49:38 +0200107static 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
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200116/*
117 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
118 */
119static bool cpuhp_is_atomic_state(enum cpuhp_state state)
120{
121 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
122}
123
Thomas Gleixnera7246322016-08-12 19:49:38 +0200124static 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
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000132/**
133 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
134 * @cpu: The cpu for which the callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200135 * @state: The state to do callbacks for
Thomas Gleixnera7246322016-08-12 19:49:38 +0200136 * @bringup: True if the bringup callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200137 * @node: For multi-instance, do a single entry callback for install/remove
138 * @lastp: For multi-instance rollback, remember how far we got
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000139 *
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200140 * Called from cpu hotplug and from the state register machinery.
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000141 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200142static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
Peter Zijlstra96abb962017-09-20 19:00:16 +0200143 bool bringup, struct hlist_node *node,
144 struct hlist_node **lastp)
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000145{
146 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200147 struct cpuhp_step *step = cpuhp_get_step(state);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200148 int (*cbm)(unsigned int cpu, struct hlist_node *node);
149 int (*cb)(unsigned int cpu);
150 int ret, cnt;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000151
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200152 if (!step->multi_instance) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200153 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200154 cb = bringup ? step->startup.single : step->teardown.single;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200155 if (!cb)
156 return 0;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200157 trace_cpuhp_enter(cpu, st->target, state, cb);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000158 ret = cb(cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200159 trace_cpuhp_exit(cpu, st->state, state, ret);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200160 return ret;
161 }
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200162 cbm = bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200163 if (!cbm)
164 return 0;
165
166 /* Single invocation for instance add/remove */
167 if (node) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200168 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200169 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) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200178 if (lastp && node == *lastp)
179 break;
180
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200181 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
182 ret = cbm(cpu, node);
183 trace_cpuhp_exit(cpu, st->state, state, ret);
Peter Zijlstra96abb962017-09-20 19:00:16 +0200184 if (ret) {
185 if (!lastp)
186 goto err;
187
188 *lastp = node;
189 return ret;
190 }
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200191 cnt++;
192 }
Peter Zijlstra96abb962017-09-20 19:00:16 +0200193 if (lastp)
194 *lastp = NULL;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200195 return 0;
196err:
197 /* Rollback the instances if one failed */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200198 cbm = !bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200199 if (!cbm)
200 return ret;
201
202 hlist_for_each(node, &step->list) {
203 if (!cnt--)
204 break;
205 cbm(cpu, node);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000206 }
207 return ret;
208}
209
Rusty Russell98a79d62008-12-13 21:19:41 +1030210#ifdef CONFIG_SMP
Rusty Russellb3199c02008-12-30 09:05:14 +1030211/* Serializes the updates to cpu_online_mask, cpu_present_mask */
Linus Torvaldsaa953872006-07-23 12:12:16 -0700212static DEFINE_MUTEX(cpu_add_remove_lock);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000213bool cpuhp_tasks_frozen;
214EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700216/*
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530217 * 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.
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700219 */
220void cpu_maps_update_begin(void)
221{
222 mutex_lock(&cpu_add_remove_lock);
223}
224
225void cpu_maps_update_done(void)
226{
227 mutex_unlock(&cpu_add_remove_lock);
228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200230/*
231 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700232 * Should always be manipulated under cpu_add_remove_lock
233 */
234static int cpu_hotplug_disabled;
235
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700236#ifdef CONFIG_HOTPLUG_CPU
237
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200238DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530239
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200240void cpus_read_lock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800241{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200242 percpu_down_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800243}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200244EXPORT_SYMBOL_GPL(cpus_read_lock);
Ashok Raj90d45d12005-11-08 21:34:24 -0800245
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200246void cpus_read_unlock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800247{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200248 percpu_up_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800249}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200250EXPORT_SYMBOL_GPL(cpus_read_unlock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800251
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200252void cpus_write_lock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100253{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200254 percpu_down_write(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100255}
256
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200257void cpus_write_unlock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100258{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200259 percpu_up_write(&cpu_hotplug_lock);
260}
261
262void lockdep_assert_cpus_held(void)
263{
264 percpu_rwsem_assert_held(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100265}
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700266
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700267/*
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 */
274void cpu_hotplug_disable(void)
275{
276 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700277 cpu_hotplug_disabled++;
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700278 cpu_maps_update_done();
279}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700280EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700281
Lianwei Wang01b41152016-06-09 23:43:28 -0700282static 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
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700289void cpu_hotplug_enable(void)
290{
291 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -0700292 __cpu_hotplug_enable();
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700293 cpu_maps_update_done();
294}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700295EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
Toshi Kanib9d10be2013-08-12 09:45:53 -0600296#endif /* CONFIG_HOTPLUG_CPU */
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700297
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200298static inline enum cpuhp_state
299cpuhp_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
313static inline void
314cpuhp_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 */
334static 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
350static 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}
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200364
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000365static int bringup_wait_for_ap(unsigned int cpu)
366{
367 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
368
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200369 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000370 wait_for_completion(&st->done);
Thomas Gleixnerdea1d0f2017-07-11 22:06:24 +0200371 if (WARN_ON_ONCE((!cpu_online(cpu))))
372 return -ECANCELED;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200373
374 /* Unpark the stopper thread and the hotplug thread of the target cpu */
375 stop_machine_unpark(cpu);
376 kthread_unpark(st->thread);
377
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200378 if (st->target <= CPUHP_AP_ONLINE_IDLE)
379 return 0;
380
381 return cpuhp_kick_ap(st, st->target);
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000382}
383
Thomas Gleixnerba997462016-02-26 18:43:24 +0000384static int bringup_cpu(unsigned int cpu)
385{
386 struct task_struct *idle = idle_thread_get(cpu);
387 int ret;
388
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400389 /*
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
Thomas Gleixnerba997462016-02-26 18:43:24 +0000396 /* Arch-specific enabling code. */
397 ret = __cpu_up(cpu, idle);
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400398 irq_unlock_sparse();
Thomas Gleixner530e9b72016-12-21 20:19:53 +0100399 if (ret)
Thomas Gleixnerba997462016-02-26 18:43:24 +0000400 return ret;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200401 return bringup_wait_for_ap(cpu);
Thomas Gleixnerba997462016-02-26 18:43:24 +0000402}
403
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000404/*
405 * Hotplug state machine related functions
406 */
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000407
Thomas Gleixnera7246322016-08-12 19:49:38 +0200408static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000409{
410 for (st->state--; st->state > st->target; st->state--) {
Thomas Gleixnera7246322016-08-12 19:49:38 +0200411 struct cpuhp_step *step = cpuhp_get_step(st->state);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000412
413 if (!step->skip_onerr)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200414 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000415 }
416}
417
418static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
Thomas Gleixnera7246322016-08-12 19:49:38 +0200419 enum cpuhp_state target)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000420{
421 enum cpuhp_state prev_state = st->state;
422 int ret = 0;
423
424 while (st->state < target) {
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000425 st->state++;
Peter Zijlstra96abb962017-09-20 19:00:16 +0200426 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000427 if (ret) {
428 st->target = prev_state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200429 undo_cpu_up(cpu, st);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000430 break;
431 }
432 }
433 return ret;
434}
435
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000436/*
437 * The cpu hotplug threads manage the bringup and teardown of the cpus
438 */
439static 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
446static 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
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000453/*
454 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
455 * callbacks when a state gets [un]installed at runtime.
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200456 *
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.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000466 */
467static void cpuhp_thread_fun(unsigned int cpu)
468{
469 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200470 bool bringup = st->bringup;
471 enum cpuhp_state state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000472
473 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200474 * 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.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000476 */
477 smp_mb();
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200478
479 if (WARN_ON_ONCE(!st->should_run))
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000480 return;
481
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200482 lock_map_acquire(&cpuhp_state_lock_map);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200483
Thomas Gleixnera7246322016-08-12 19:49:38 +0200484 if (st->single) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200485 state = st->cb_state;
486 st->should_run = false;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000487 } else {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200488 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);
493 } else {
494 state = st->state;
495 st->state--;
496 st->should_run = (st->state > st->target);
497 WARN_ON_ONCE(st->state < st->target);
498 }
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000499 }
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200500
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();
513
514 /*
515 * STARTING/DYING must not fail!
516 */
517 WARN_ON_ONCE(st->result);
518 } else {
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;
530 }
531
532next:
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200533 lock_map_release(&cpuhp_state_lock_map);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200534
535 if (!st->should_run)
536 complete(&st->done);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000537}
538
539/* Invoke a single callback on a remote cpu */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200540static int
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200541cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
542 struct hlist_node *node)
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000543{
544 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200545 int ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000546
547 if (!cpu_online(cpu))
548 return 0;
549
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200550 lock_map_acquire(&cpuhp_state_lock_map);
551 lock_map_release(&cpuhp_state_lock_map);
552
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000553 /*
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)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200558 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000559
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200560 st->rollback = false;
561 st->last = NULL;
562
563 st->node = node;
564 st->bringup = bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000565 st->cb_state = state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200566 st->single = true;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200567
568 __cpuhp_kick_ap(st);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200569
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000570 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200571 * If we failed and did a partial, do a rollback.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000572 */
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200573 if ((ret = st->result) && st->last) {
574 st->rollback = true;
575 st->bringup = !bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000576
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200577 __cpuhp_kick_ap(st);
578 }
579
580 return ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000581}
582
583static int cpuhp_kick_ap_work(unsigned int cpu)
584{
585 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200586 enum cpuhp_state prev_state = st->state;
587 int ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000588
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200589 lock_map_acquire(&cpuhp_state_lock_map);
590 lock_map_release(&cpuhp_state_lock_map);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200591
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;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000597}
598
599static 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
608void __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
Michal Hocko777c6e02016-12-07 14:54:38 +0100614#ifdef CONFIG_HOTPLUG_CPU
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700615/**
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 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700627void 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 */
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700638 WARN_ON(cpu_online(cpu));
Anton Vorontsovcb792952012-05-31 16:26:22 -0700639 rcu_read_lock();
640 for_each_process(p) {
641 struct task_struct *t;
642
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700643 /*
644 * Main thread might exit, but other threads may still have
645 * a valid mm. Find one.
646 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700647 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/* Take this CPU down. */
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200657static int take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000659 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
660 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000661 int err, cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* Ensure this CPU doesn't handle any more interrupts. */
664 err = __cpu_disable();
665 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Thomas Gleixnera7246322016-08-12 19:49:38 +0200668 /*
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--;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000674 /* Invoke the former CPU_DYING callbacks */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200675 for (; st->state > target; st->state--)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200676 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000677
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200678 /* Give up timekeeping duties */
679 tick_handover_do_timer();
Thomas Gleixner14e568e2013-01-31 12:11:14 +0000680 /* Park the stopper thread */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000681 stop_machine_park(cpu);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Thomas Gleixner98458172016-02-26 18:43:25 +0000685static int takedown_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000687 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000688 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Thomas Gleixner2a58c522016-03-10 20:42:08 +0100690 /* Park the smpboot threads */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000691 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
Thomas Gleixner2a58c522016-03-10 20:42:08 +0100692 smpboot_park_threads(cpu);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000693
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200694 /*
Thomas Gleixnera8994182015-07-05 17:12:30 +0000695 * Prevent irq alloc/free while the dying cpu reorganizes the
696 * interrupt affinities.
697 */
698 irq_lock_sparse();
699
700 /*
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200701 * So now all preempt/rcu users must observe !cpu_active().
702 */
Sebastian Andrzej Siewior210e2132017-05-24 10:15:28 +0200703 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
Rusty Russell04321582008-07-28 12:16:29 -0500704 if (err) {
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200705 /* CPU refused to die */
Thomas Gleixnera8994182015-07-05 17:12:30 +0000706 irq_unlock_sparse();
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200707 /* Unpark the hotplug thread so we can rollback there */
708 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
Thomas Gleixner98458172016-02-26 18:43:25 +0000709 return err;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700710 }
Rusty Russell04321582008-07-28 12:16:29 -0500711 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100713 /*
Thomas Gleixneree1e7142016-08-18 14:57:16 +0200714 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100715 * 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.
Peter Zijlstra51a96c72010-11-19 20:37:53 +0100717 *
718 * Wait for the stop thread to go away.
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100719 */
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000720 wait_for_completion(&st->done);
721 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Thomas Gleixnera8994182015-07-05 17:12:30 +0000723 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
724 irq_unlock_sparse();
725
Preeti U Murthy345527b2015-03-30 14:59:19 +0530726 hotplug_cpu__broadcast_tick_pull(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* This actually kills the CPU. */
728 __cpu_die(cpu);
729
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200730 tick_cleanup_dead_cpu(cpu);
Paul E. McKenneya58163d2017-06-20 12:11:34 -0700731 rcutree_migrate_callbacks(cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000732 return 0;
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100735static void cpuhp_complete_idle_dead(void *arg)
736{
737 struct cpuhp_cpu_state *st = arg;
738
739 complete(&st->done);
740}
741
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000742void 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);
Thomas Gleixner27d50c72016-02-26 18:43:44 +0000747 rcu_report_dead(smp_processor_id());
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100748 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);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000755}
756
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200757static 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);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000761
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200762 if (!step->skip_onerr)
763 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
764 }
765}
766
767static 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}
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000783
Thomas Gleixner98458172016-02-26 18:43:25 +0000784/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000785static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
786 enum cpuhp_state target)
Thomas Gleixner98458172016-02-26 18:43:25 +0000787{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000788 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
789 int prev_state, ret = 0;
Thomas Gleixner98458172016-02-26 18:43:25 +0000790
791 if (num_online_cpus() == 1)
792 return -EBUSY;
793
Thomas Gleixner757c9892016-02-26 18:43:32 +0000794 if (!cpu_present(cpu))
Thomas Gleixner98458172016-02-26 18:43:25 +0000795 return -EINVAL;
796
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200797 cpus_write_lock();
Thomas Gleixner98458172016-02-26 18:43:25 +0000798
799 cpuhp_tasks_frozen = tasks_frozen;
800
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200801 prev_state = cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000802 /*
803 * If the current CPU state is in the range of the AP hotplug thread,
804 * then we need to kick the thread.
805 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000806 if (st->state > CPUHP_TEARDOWN_CPU) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200807 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000808 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 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000820 if (st->state > CPUHP_TEARDOWN_CPU)
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000821 goto out;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200822
823 st->target = target;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000824 }
825 /*
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000826 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000827 * to do the further cleanups.
828 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200829 ret = cpuhp_down_callbacks(cpu, st, target);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200830 if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200831 cpuhp_reset_state(st, prev_state);
832 __cpuhp_kick_ap(st);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200833 }
Thomas Gleixner98458172016-02-26 18:43:25 +0000834
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000835out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200836 cpus_write_unlock();
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000837 return ret;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700838}
839
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000840static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700841{
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100842 int err;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700843
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100844 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700845
Max Krasnyanskye761b772008-07-15 04:43:49 -0700846 if (cpu_hotplug_disabled) {
847 err = -EBUSY;
848 goto out;
849 }
850
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000851 err = _cpu_down(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -0700852
Max Krasnyanskye761b772008-07-15 04:43:49 -0700853out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100854 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return err;
856}
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200857
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000858int cpu_down(unsigned int cpu)
859{
860 return do_cpu_down(cpu, CPUHP_OFFLINE);
861}
Zhang Ruib62b8ef2008-04-29 02:35:56 -0400862EXPORT_SYMBOL(cpu_down);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200863
864#else
865#define takedown_cpu NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866#endif /*CONFIG_HOTPLUG_CPU*/
867
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000868/**
Thomas Gleixneree1e7142016-08-18 14:57:16 +0200869 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000870 * @cpu: cpu that just started
871 *
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000872 * 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 */
875void 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
Sebastian Andrzej Siewior0c6d4572016-08-17 14:21:04 +0200880 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000881 while (st->state < target) {
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000882 st->state++;
Peter Zijlstra96abb962017-09-20 19:00:16 +0200883 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000884 }
885}
886
Thomas Gleixner949338e2016-02-26 18:43:35 +0000887/*
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200888 * 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.
Thomas Gleixner949338e2016-02-26 18:43:35 +0000891 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000892void cpuhp_online_idle(enum cpuhp_state state)
Thomas Gleixner949338e2016-02-26 18:43:35 +0000893{
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000894 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000895
896 /* Happens for the boot cpu */
897 if (state != CPUHP_AP_ONLINE_IDLE)
898 return;
899
900 st->state = CPUHP_AP_ONLINE_IDLE;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200901 complete(&st->done);
Thomas Gleixner949338e2016-02-26 18:43:35 +0000902}
903
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700904/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000905static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000907 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700908 struct task_struct *idle;
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000909 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200911 cpus_write_lock();
Thomas Gleixner38498a62012-04-20 13:05:44 +0000912
Thomas Gleixner757c9892016-02-26 18:43:32 +0000913 if (!cpu_present(cpu)) {
Yasuaki Ishimatsu5e5041f2012-10-23 01:30:54 +0200914 ret = -EINVAL;
915 goto out;
916 }
917
Thomas Gleixner757c9892016-02-26 18:43:32 +0000918 /*
919 * The caller of do_cpu_up might have raced with another
920 * caller. Ignore it for now.
921 */
922 if (st->state >= target)
Thomas Gleixner38498a62012-04-20 13:05:44 +0000923 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +0000924
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 }
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700932 }
Thomas Gleixner38498a62012-04-20 13:05:44 +0000933
Thomas Gleixnerba997462016-02-26 18:43:24 +0000934 cpuhp_tasks_frozen = tasks_frozen;
935
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200936 cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000937 /*
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 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000941 if (st->state > CPUHP_BRINGUP_CPU) {
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000942 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
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000953 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000954 * responsible for bringing it up to the target state.
955 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000956 target = min((int)target, CPUHP_BRINGUP_CPU);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200957 ret = cpuhp_up_callbacks(cpu, st, target);
Thomas Gleixner38498a62012-04-20 13:05:44 +0000958out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200959 cpus_write_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return ret;
961}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700962
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000963static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700964{
965 int err = 0;
minskey guocf234222010-05-24 14:32:41 -0700966
Rusty Russelle0b582e2009-01-01 10:12:28 +1030967 if (!cpu_possible(cpu)) {
Fabian Frederick84117da2014-06-04 16:11:17 -0700968 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
969 cpu);
Chen Gong87d5e0232010-03-05 13:42:38 -0800970#if defined(CONFIG_IA64)
Fabian Frederick84117da2014-06-04 16:11:17 -0700971 pr_err("please check additional_cpus= boot parameter\n");
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -0700972#endif
973 return -EINVAL;
974 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700975
Toshi Kani01b0f192013-11-12 15:07:25 -0800976 err = try_online_node(cpu_to_node(cpu));
977 if (err)
978 return err;
minskey guocf234222010-05-24 14:32:41 -0700979
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100980 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700981
Max Krasnyanskye761b772008-07-15 04:43:49 -0700982 if (cpu_hotplug_disabled) {
983 err = -EBUSY;
984 goto out;
985 }
986
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000987 err = _cpu_up(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -0700988out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100989 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700990 return err;
991}
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000992
993int cpu_up(unsigned int cpu)
994{
995 return do_cpu_up(cpu, CPUHP_ONLINE);
996}
Paul E. McKenneya513f6b2011-12-11 21:54:45 -0800997EXPORT_SYMBOL_GPL(cpu_up);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700998
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700999#ifdef CONFIG_PM_SLEEP_SMP
Rusty Russelle0b582e2009-01-01 10:12:28 +10301000static cpumask_var_t frozen_cpus;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001001
James Morsed391e552016-08-17 13:50:25 +01001002int freeze_secondary_cpus(int primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001003{
James Morsed391e552016-08-17 13:50:25 +01001004 int cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001005
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001006 cpu_maps_update_begin();
James Morsed391e552016-08-17 13:50:25 +01001007 if (!cpu_online(primary))
1008 primary = cpumask_first(cpu_online_mask);
Xiaotian Feng9ee349a2009-12-16 18:04:32 +01001009 /*
1010 * We take down all of the non-boot CPUs in one shot to avoid races
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001011 * with the userspace trying to use the CPU hotplug at the same time
1012 */
Rusty Russelle0b582e2009-01-01 10:12:28 +10301013 cpumask_clear(frozen_cpus);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01001014
Fabian Frederick84117da2014-06-04 16:11:17 -07001015 pr_info("Disabling non-boot CPUs ...\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001016 for_each_online_cpu(cpu) {
James Morsed391e552016-08-17 13:50:25 +01001017 if (cpu == primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001018 continue;
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001019 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001020 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001021 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
Mike Travisfeae3202009-11-17 18:22:13 -06001022 if (!error)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301023 cpumask_set_cpu(cpu, frozen_cpus);
Mike Travisfeae3202009-11-17 18:22:13 -06001024 else {
Fabian Frederick84117da2014-06-04 16:11:17 -07001025 pr_err("Error taking CPU%d down: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001026 break;
1027 }
1028 }
Joseph Cihula86886e52009-06-30 19:31:07 -07001029
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001030 if (!error)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001031 BUG_ON(num_online_cpus() > 1);
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001032 else
Fabian Frederick84117da2014-06-04 16:11:17 -07001033 pr_err("Non-boot CPUs are not disabled\n");
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001034
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
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001042 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001043 return error;
1044}
1045
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001046void __weak arch_enable_nonboot_cpus_begin(void)
1047{
1048}
1049
1050void __weak arch_enable_nonboot_cpus_end(void)
1051{
1052}
1053
Mathias Krause71cf5ae2015-07-19 20:06:22 +02001054void enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001055{
1056 int cpu, error;
1057
1058 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001059 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -07001060 __cpu_hotplug_enable();
Rusty Russelle0b582e2009-01-01 10:12:28 +10301061 if (cpumask_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001062 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001063
Fabian Frederick84117da2014-06-04 16:11:17 -07001064 pr_info("Enabling non-boot CPUs ...\n");
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001065
1066 arch_enable_nonboot_cpus_begin();
1067
Rusty Russelle0b582e2009-01-01 10:12:28 +10301068 for_each_cpu(cpu, frozen_cpus) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001069 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001070 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001071 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001072 if (!error) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001073 pr_info("CPU%d is up\n", cpu);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001074 continue;
1075 }
Fabian Frederick84117da2014-06-04 16:11:17 -07001076 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001077 }
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001078
1079 arch_enable_nonboot_cpus_end();
1080
Rusty Russelle0b582e2009-01-01 10:12:28 +10301081 cpumask_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001082out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001083 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001084}
Rusty Russelle0b582e2009-01-01 10:12:28 +10301085
Fenghua Yud7268a32011-11-15 21:59:31 +01001086static int __init alloc_frozen_cpus(void)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301087{
1088 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1089 return -ENOMEM;
1090 return 0;
1091}
1092core_initcall(alloc_frozen_cpus);
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001093
1094/*
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001095 * 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 */
1105static int
1106cpu_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:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001113 cpu_hotplug_disable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001114 break;
1115
1116 case PM_POST_SUSPEND:
1117 case PM_POST_HIBERNATION:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001118 cpu_hotplug_enable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001119 break;
1120
1121 default:
1122 return NOTIFY_DONE;
1123 }
1124
1125 return NOTIFY_OK;
1126}
1127
1128
Fenghua Yud7268a32011-11-15 21:59:31 +01001129static int __init cpu_hotplug_pm_sync_init(void)
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001130{
Fenghua Yu6e32d472012-11-13 11:32:43 -08001131 /*
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 */
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001136 pm_notifier(cpu_hotplug_pm_callback, 0);
1137 return 0;
1138}
1139core_initcall(cpu_hotplug_pm_sync_init);
1140
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001141#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001142
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01001143int __boot_cpu_id;
1144
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001145#endif /* CONFIG_SMP */
Mike Travisb8d317d2008-07-24 18:21:29 -07001146
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001147/* Boot processor state steps */
1148static struct cpuhp_step cpuhp_bp_states[] = {
1149 [CPUHP_OFFLINE] = {
1150 .name = "offline",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001151 .startup.single = NULL,
1152 .teardown.single = NULL,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001153 },
1154#ifdef CONFIG_SMP
1155 [CPUHP_CREATE_THREADS]= {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001156 .name = "threads:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001157 .startup.single = smpboot_create_threads,
1158 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001159 .cant_stop = true,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001160 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001161 [CPUHP_PERF_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001162 .name = "perf:prepare",
1163 .startup.single = perf_event_init_cpu,
1164 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001165 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001166 [CPUHP_WORKQUEUE_PREP] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001167 .name = "workqueue:prepare",
1168 .startup.single = workqueue_prepare_cpu,
1169 .teardown.single = NULL,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001170 },
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001171 [CPUHP_HRTIMERS_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001172 .name = "hrtimers:prepare",
1173 .startup.single = hrtimers_prepare_cpu,
1174 .teardown.single = hrtimers_dead_cpu,
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001175 },
Richard Weinberger31487f82016-07-13 17:17:01 +00001176 [CPUHP_SMPCFD_PREPARE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001177 .name = "smpcfd:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001178 .startup.single = smpcfd_prepare_cpu,
1179 .teardown.single = smpcfd_dead_cpu,
Richard Weinberger31487f82016-07-13 17:17:01 +00001180 },
Richard Weinbergere6d49892016-08-18 14:57:17 +02001181 [CPUHP_RELAY_PREPARE] = {
1182 .name = "relay:prepare",
1183 .startup.single = relay_prepare_cpu,
1184 .teardown.single = NULL,
1185 },
Sebastian Andrzej Siewior6731d4f2016-08-23 14:53:19 +02001186 [CPUHP_SLAB_PREPARE] = {
1187 .name = "slab:prepare",
1188 .startup.single = slab_prepare_cpu,
1189 .teardown.single = slab_dead_cpu,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001190 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001191 [CPUHP_RCUTREE_PREP] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001192 .name = "RCU/tree:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001193 .startup.single = rcutree_prepare_cpu,
1194 .teardown.single = rcutree_dead_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001195 },
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001196 /*
Richard Cochran4fae16d2016-07-27 11:08:18 +02001197 * 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] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001202 .name = "timers:dead",
1203 .startup.single = NULL,
1204 .teardown.single = timers_dead_cpu,
Richard Cochran4fae16d2016-07-27 11:08:18 +02001205 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001206 /* Kicks the plugged cpu into life */
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001207 [CPUHP_BRINGUP_CPU] = {
1208 .name = "cpu:bringup",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001209 .startup.single = bringup_cpu,
1210 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001211 .cant_stop = true,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001212 },
Richard Weinberger31487f82016-07-13 17:17:01 +00001213 [CPUHP_AP_SMPCFD_DYING] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001214 .name = "smpcfd:dying",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001215 .startup.single = NULL,
1216 .teardown.single = smpcfd_dying_cpu,
Richard Weinberger31487f82016-07-13 17:17:01 +00001217 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001218 /*
1219 * Handled on controll processor until the plugged processor manages
1220 * this itself.
1221 */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001222 [CPUHP_TEARDOWN_CPU] = {
1223 .name = "cpu:teardown",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001224 .startup.single = NULL,
1225 .teardown.single = takedown_cpu,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001226 .cant_stop = true,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001227 },
Thomas Gleixnera7c734142016-07-12 21:59:23 +02001228#else
1229 [CPUHP_BRINGUP_CPU] = { },
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001230#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001231};
1232
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001233/* Application processor state steps */
1234static struct cpuhp_step cpuhp_ap_states[] = {
1235#ifdef CONFIG_SMP
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001236 /* 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 },
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001248 /* First state is scheduler control. Interrupts are disabled */
1249 [CPUHP_AP_SCHED_STARTING] = {
1250 .name = "sched:starting",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001251 .startup.single = sched_cpu_starting,
1252 .teardown.single = sched_cpu_dying,
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001253 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001254 [CPUHP_AP_RCUTREE_DYING] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001255 .name = "RCU/tree:dying",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001256 .startup.single = NULL,
1257 .teardown.single = rcutree_dying_cpu,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001258 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001259 /* 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 */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001265 [CPUHP_AP_SMPBOOT_THREADS] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001266 .name = "smpboot/threads:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001267 .startup.single = smpboot_unpark_threads,
1268 .teardown.single = NULL,
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001269 },
Thomas Gleixnerc5cb83b2017-06-20 01:37:51 +02001270 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1271 .name = "irq/affinity:online",
1272 .startup.single = irq_affinity_online_cpu,
1273 .teardown.single = NULL,
1274 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001275 [CPUHP_AP_PERF_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001276 .name = "perf:online",
1277 .startup.single = perf_event_init_cpu,
1278 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001279 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001280 [CPUHP_AP_WORKQUEUE_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001281 .name = "workqueue:online",
1282 .startup.single = workqueue_online_cpu,
1283 .teardown.single = workqueue_offline_cpu,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001284 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001285 [CPUHP_AP_RCUTREE_ONLINE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001286 .name = "RCU/tree:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001287 .startup.single = rcutree_online_cpu,
1288 .teardown.single = rcutree_offline_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001289 },
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001290#endif
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001291 /*
1292 * The dynamically registered state space is here
1293 */
1294
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001295#ifdef CONFIG_SMP
1296 /* Last state is scheduler control setting the cpu active */
1297 [CPUHP_AP_ACTIVE] = {
1298 .name = "sched:active",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001299 .startup.single = sched_cpu_activate,
1300 .teardown.single = sched_cpu_deactivate,
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001301 },
1302#endif
1303
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001304 /* CPU is fully up and running. */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001305 [CPUHP_ONLINE] = {
1306 .name = "online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001307 .startup.single = NULL,
1308 .teardown.single = NULL,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001309 },
1310};
1311
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001312/* Sanity check for callbacks */
1313static 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
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001320/*
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 */
1325static int cpuhp_reserve_state(enum cpuhp_state state)
1326{
Thomas Gleixner4205e472017-01-10 14:01:05 +01001327 enum cpuhp_state i, end;
1328 struct cpuhp_step *step;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001329
Thomas Gleixner4205e472017-01-10 14:01:05 +01001330 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)
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001345 return i;
1346 }
1347 WARN(1, "No more dynamic states available for CPU hotplug\n");
1348 return -ENOSPC;
1349}
1350
1351static 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)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001355{
1356 /* (Un)Install the callbacks for further cpu hotplug operations */
1357 struct cpuhp_step *sp;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001358 int ret = 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001359
Ethan Barnes0c96b272017-07-19 22:36:00 +00001360 /*
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)) {
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001371 ret = cpuhp_reserve_state(state);
1372 if (ret < 0)
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001373 return ret;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001374 state = ret;
1375 }
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001376 sp = cpuhp_get_step(state);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001377 if (name && sp->name)
1378 return -EBUSY;
1379
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001380 sp->startup.single = startup;
1381 sp->teardown.single = teardown;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001382 sp->name = name;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001383 sp->multi_instance = multi_instance;
1384 INIT_HLIST_HEAD(&sp->list);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001385 return ret;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001386}
1387
1388static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1389{
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001390 return cpuhp_get_step(state)->teardown.single;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001391}
1392
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001393/*
1394 * Call the startup/teardown function for a step either on the AP or
1395 * on the current CPU.
1396 */
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001397static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1398 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001399{
Thomas Gleixnera7246322016-08-12 19:49:38 +02001400 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001401 int ret;
1402
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001403 /*
1404 * If there's nothing to do, we done.
1405 * Relies on the union for multi_instance.
1406 */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001407 if ((bringup && !sp->startup.single) ||
1408 (!bringup && !sp->teardown.single))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001409 return 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001410 /*
1411 * The non AP bound callbacks can fail on bringup. On teardown
1412 * e.g. module removal we crash for now.
1413 */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001414#ifdef CONFIG_SMP
1415 if (cpuhp_is_ap_state(state))
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001416 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001417 else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001418 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001419#else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001420 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001421#endif
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001422 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 */
1431static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001432 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001433{
1434 int cpu;
1435
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001436 /* 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)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001446 cpuhp_issue_call(cpu, state, false, node);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001447 }
1448}
1449
Thomas Gleixner9805c672017-05-24 10:15:15 +02001450int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1451 struct hlist_node *node,
1452 bool invoke)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001453{
1454 struct cpuhp_step *sp;
1455 int cpu;
1456 int ret;
1457
Thomas Gleixner9805c672017-05-24 10:15:15 +02001458 lockdep_assert_cpus_held();
1459
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001460 sp = cpuhp_get_step(state);
1461 if (sp->multi_instance == false)
1462 return -EINVAL;
1463
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001464 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001465
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001466 if (!invoke || !sp->startup.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001467 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) {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001482 if (sp->teardown.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001483 cpuhp_rollback_install(cpu, state, node);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001484 goto unlock;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001485 }
1486 }
1487add_node:
1488 ret = 0;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001489 hlist_add_head(node, &sp->list);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001490unlock:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001491 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner9805c672017-05-24 10:15:15 +02001492 return ret;
1493}
1494
1495int __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);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001502 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001503 return ret;
1504}
1505EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1506
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001507/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001508 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001509 * @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.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001516 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001517 * The caller needs to hold cpus read locked while calling this function.
Boris Ostrovsky512f0982016-12-15 10:00:57 -05001518 * 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
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001523 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001524int __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)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001529{
1530 int cpu, ret = 0;
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001531 bool dynstate;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001532
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001533 lockdep_assert_cpus_held();
1534
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001535 if (cpuhp_cb_check(state) || !name)
1536 return -EINVAL;
1537
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001538 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001539
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001540 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1541 multi_instance);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001542
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001543 dynstate = state == CPUHP_AP_ONLINE_DYN;
1544 if (ret > 0 && dynstate) {
1545 state = ret;
1546 ret = 0;
1547 }
1548
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001549 if (ret || !invoke || !startup)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001550 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
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001563 ret = cpuhp_issue_call(cpu, state, true, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001564 if (ret) {
Thomas Gleixnera7246322016-08-12 19:49:38 +02001565 if (teardown)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001566 cpuhp_rollback_install(cpu, state, NULL);
1567 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001568 goto out;
1569 }
1570 }
1571out:
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001572 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001573 /*
1574 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1575 * dynamically allocated state in case of success.
1576 */
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001577 if (!ret && dynstate)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001578 return state;
1579 return ret;
1580}
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001581EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1582
1583int __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}
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001597EXPORT_SYMBOL(__cpuhp_setup_state);
1598
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001599int __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
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001610 cpus_read_lock();
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001611 mutex_lock(&cpuhp_state_mutex);
1612
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001613 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
1628remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001629 hlist_del(node);
1630 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001631 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001632
1633 return 0;
1634}
1635EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001636
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001637/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001638 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001639 * @state: The state to remove
1640 * @invoke: If true, the teardown function is invoked for cpus where
1641 * cpu state >= @state
1642 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001643 * The caller needs to hold cpus read locked while calling this function.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001644 * The teardown callback is currently not allowed to fail. Think
1645 * about module removal!
1646 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001647void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001648{
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001649 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001650 int cpu;
1651
1652 BUG_ON(cpuhp_cb_check(state));
1653
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001654 lockdep_assert_cpus_held();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001655
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001656 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001657 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
Thomas Gleixnera7246322016-08-12 19:49:38 +02001664 if (!invoke || !cpuhp_get_teardown_cb(state))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001665 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)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001677 cpuhp_issue_call(cpu, state, false, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001678 }
1679remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001680 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001681 mutex_unlock(&cpuhp_state_mutex);
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001682}
1683EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1684
1685void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1686{
1687 cpus_read_lock();
1688 __cpuhp_remove_state_cpuslocked(state, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001689 cpus_read_unlock();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001690}
1691EXPORT_SYMBOL(__cpuhp_remove_state);
1692
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001693#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1694static 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}
1701static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1702
Thomas Gleixner757c9892016-02-26 18:43:32 +00001703static 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)
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001732 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +00001733
1734 if (st->state < target)
1735 ret = do_cpu_up(dev->id, target);
1736 else
1737 ret = do_cpu_down(dev->id, target);
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001738out:
Thomas Gleixner757c9892016-02-26 18:43:32 +00001739 unlock_device_hotplug();
1740 return ret ? ret : count;
1741}
1742
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001743static 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}
Thomas Gleixner757c9892016-02-26 18:43:32 +00001750static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001751
1752static struct attribute *cpuhp_cpu_attrs[] = {
1753 &dev_attr_state.attr,
1754 &dev_attr_target.attr,
1755 NULL
1756};
1757
Arvind Yadav993647a2017-06-29 17:40:47 +05301758static const struct attribute_group cpuhp_cpu_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001759 .attrs = cpuhp_cpu_attrs,
1760 .name = "hotplug",
1761 NULL
1762};
1763
1764static 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);
Thomas Gleixner757c9892016-02-26 18:43:32 +00001771 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001772 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}
1783static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
1784
1785static struct attribute *cpuhp_cpu_root_attrs[] = {
1786 &dev_attr_states.attr,
1787 NULL
1788};
1789
Arvind Yadav993647a2017-06-29 17:40:47 +05301790static const struct attribute_group cpuhp_cpu_root_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001791 .attrs = cpuhp_cpu_root_attrs,
1792 .name = "hotplug",
1793 NULL
1794};
1795
1796static 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}
1816device_initcall(cpuhp_sysfs_init);
1817#endif
1818
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001819/*
1820 * cpu_bit_bitmap[] is a special, "compressed" data structure that
1821 * represents all NR_CPUS bits binary values of 1<<nr.
1822 *
Rusty Russelle0b582e2009-01-01 10:12:28 +10301823 * It is used by cpumask_of() to get a constant address to a CPU
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001824 * mask value that has a single bit set only.
1825 */
Mike Travisb8d317d2008-07-24 18:21:29 -07001826
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001827/* cpu_bit_bitmap[0] is empty - so we can back into it */
Michael Rodriguez4d519852011-03-22 16:34:07 -07001828#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001829#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)
Mike Travisb8d317d2008-07-24 18:21:29 -07001832
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001833const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
Mike Travisb8d317d2008-07-24 18:21:29 -07001834
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001835 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),
Mike Travisb8d317d2008-07-24 18:21:29 -07001840#endif
1841};
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001842EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001843
1844const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
1845EXPORT_SYMBOL(cpu_all_bits);
Rusty Russellb3199c02008-12-30 09:05:14 +10301846
1847#ifdef CONFIG_INIT_ALL_POSSIBLE
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001848struct cpumask __cpu_possible_mask __read_mostly
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001849 = {CPU_BITS_ALL};
Rusty Russellb3199c02008-12-30 09:05:14 +10301850#else
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001851struct cpumask __cpu_possible_mask __read_mostly;
Rusty Russellb3199c02008-12-30 09:05:14 +10301852#endif
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001853EXPORT_SYMBOL(__cpu_possible_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301854
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001855struct cpumask __cpu_online_mask __read_mostly;
1856EXPORT_SYMBOL(__cpu_online_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301857
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001858struct cpumask __cpu_present_mask __read_mostly;
1859EXPORT_SYMBOL(__cpu_present_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301860
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001861struct cpumask __cpu_active_mask __read_mostly;
1862EXPORT_SYMBOL(__cpu_active_mask);
Rusty Russell3fa41522008-12-30 09:05:16 +10301863
Rusty Russell3fa41522008-12-30 09:05:16 +10301864void init_cpu_present(const struct cpumask *src)
1865{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001866 cpumask_copy(&__cpu_present_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301867}
1868
1869void init_cpu_possible(const struct cpumask *src)
1870{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001871 cpumask_copy(&__cpu_possible_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301872}
1873
1874void init_cpu_online(const struct cpumask *src)
1875{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001876 cpumask_copy(&__cpu_online_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301877}
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001878
1879/*
1880 * Activate the first processor.
1881 */
1882void __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);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01001891
1892#ifdef CONFIG_SMP
1893 __boot_cpu_id = cpu;
1894#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001895}
1896
1897/*
1898 * Must be called _AFTER_ setting up the per_cpu areas
1899 */
1900void __init boot_cpu_state_init(void)
1901{
1902 per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
1903}