blob: 6f3a3cde8b83ae26573e03aeac06526c1ee2c68b [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 Gleixner941154b2017-09-12 21:37:04 +020027#include <linux/nmi.h>
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000028#include <linux/smpboot.h>
Richard Weinbergere6d49892016-08-18 14:57:17 +020029#include <linux/relay.h>
Sebastian Andrzej Siewior6731d4f2016-08-23 14:53:19 +020030#include <linux/slab.h>
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +020031#include <linux/percpu-rwsem.h>
Thomas Gleixnercff7d372016-02-26 18:43:28 +000032
Todd E Brandtbb3632c2014-06-06 05:40:17 -070033#include <trace/events/power.h>
Thomas Gleixnercff7d372016-02-26 18:43:28 +000034#define CREATE_TRACE_POINTS
35#include <trace/events/cpuhp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Thomas Gleixner38498a62012-04-20 13:05:44 +000037#include "smpboot.h"
38
Thomas Gleixnercff7d372016-02-26 18:43:28 +000039/**
40 * cpuhp_cpu_state - Per cpu hotplug state storage
41 * @state: The current cpu state
42 * @target: The target state
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000043 * @thread: Pointer to the hotplug thread
44 * @should_run: Thread should execute
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +020045 * @rollback: Perform a rollback
Thomas Gleixnera7246322016-08-12 19:49:38 +020046 * @single: Single callback invocation
47 * @bringup: Single callback bringup or teardown selector
48 * @cb_state: The state for a single callback (install/uninstall)
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000049 * @result: Result of the operation
Peter Zijlstra5ebe7742017-09-20 19:00:19 +020050 * @done_up: Signal completion to the issuer of the task for cpu-up
51 * @done_down: Signal completion to the issuer of the task for cpu-down
Thomas Gleixnercff7d372016-02-26 18:43:28 +000052 */
53struct cpuhp_cpu_state {
54 enum cpuhp_state state;
55 enum cpuhp_state target;
Peter Zijlstra1db49482017-09-20 19:00:21 +020056 enum cpuhp_state fail;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000057#ifdef CONFIG_SMP
58 struct task_struct *thread;
59 bool should_run;
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +020060 bool rollback;
Thomas Gleixnera7246322016-08-12 19:49:38 +020061 bool single;
62 bool bringup;
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +020063 bool booted_once;
Thomas Gleixnercf392d12016-08-12 19:49:39 +020064 struct hlist_node *node;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +020065 struct hlist_node *last;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000066 enum cpuhp_state cb_state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000067 int result;
Peter Zijlstra5ebe7742017-09-20 19:00:19 +020068 struct completion done_up;
69 struct completion done_down;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +000070#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +000071};
72
Peter Zijlstra1db49482017-09-20 19:00:21 +020073static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
74 .fail = CPUHP_INVALID,
75};
Thomas Gleixnercff7d372016-02-26 18:43:28 +000076
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +020077#if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +020078static struct lockdep_map cpuhp_state_up_map =
79 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
80static struct lockdep_map cpuhp_state_down_map =
81 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
82
83
Mathieu Malaterre76dc6c02017-12-26 15:08:53 +010084static inline void cpuhp_lock_acquire(bool bringup)
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +020085{
86 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
87}
88
Mathieu Malaterre76dc6c02017-12-26 15:08:53 +010089static inline void cpuhp_lock_release(bool bringup)
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +020090{
91 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
92}
93#else
94
Mathieu Malaterre76dc6c02017-12-26 15:08:53 +010095static inline void cpuhp_lock_acquire(bool bringup) { }
96static inline void cpuhp_lock_release(bool bringup) { }
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +020097
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +020098#endif
99
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000100/**
101 * cpuhp_step - Hotplug state machine step
102 * @name: Name of the step
103 * @startup: Startup function of the step
104 * @teardown: Teardown function of the step
105 * @skip_onerr: Do not invoke the functions on error rollback
106 * Will go away once the notifiers are gone
Thomas Gleixner757c9892016-02-26 18:43:32 +0000107 * @cant_stop: Bringup/teardown can't be stopped at this step
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000108 */
109struct cpuhp_step {
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200110 const char *name;
111 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 } startup;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200116 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200117 int (*single)(unsigned int cpu);
118 int (*multi)(unsigned int cpu,
119 struct hlist_node *node);
120 } teardown;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200121 struct hlist_head list;
122 bool skip_onerr;
123 bool cant_stop;
124 bool multi_instance;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000125};
126
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +0000127static DEFINE_MUTEX(cpuhp_state_mutex);
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +0800128static struct cpuhp_step cpuhp_hp_states[];
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000129
Thomas Gleixnera7246322016-08-12 19:49:38 +0200130static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
131{
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +0800132 return cpuhp_hp_states + state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200133}
134
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000135/**
136 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
137 * @cpu: The cpu for which the callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200138 * @state: The state to do callbacks for
Thomas Gleixnera7246322016-08-12 19:49:38 +0200139 * @bringup: True if the bringup callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200140 * @node: For multi-instance, do a single entry callback for install/remove
141 * @lastp: For multi-instance rollback, remember how far we got
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000142 *
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200143 * Called from cpu hotplug and from the state register machinery.
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000144 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200145static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
Peter Zijlstra96abb962017-09-20 19:00:16 +0200146 bool bringup, struct hlist_node *node,
147 struct hlist_node **lastp)
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000148{
149 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200150 struct cpuhp_step *step = cpuhp_get_step(state);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200151 int (*cbm)(unsigned int cpu, struct hlist_node *node);
152 int (*cb)(unsigned int cpu);
153 int ret, cnt;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000154
Peter Zijlstra1db49482017-09-20 19:00:21 +0200155 if (st->fail == state) {
156 st->fail = CPUHP_INVALID;
157
158 if (!(bringup ? step->startup.single : step->teardown.single))
159 return 0;
160
161 return -EAGAIN;
162 }
163
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200164 if (!step->multi_instance) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200165 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200166 cb = bringup ? step->startup.single : step->teardown.single;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200167 if (!cb)
168 return 0;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200169 trace_cpuhp_enter(cpu, st->target, state, cb);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000170 ret = cb(cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200171 trace_cpuhp_exit(cpu, st->state, state, ret);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200172 return ret;
173 }
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200174 cbm = bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200175 if (!cbm)
176 return 0;
177
178 /* Single invocation for instance add/remove */
179 if (node) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200180 WARN_ON_ONCE(lastp && *lastp);
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);
184 return ret;
185 }
186
187 /* State transition. Invoke on all instances */
188 cnt = 0;
189 hlist_for_each(node, &step->list) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200190 if (lastp && node == *lastp)
191 break;
192
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200193 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
194 ret = cbm(cpu, node);
195 trace_cpuhp_exit(cpu, st->state, state, ret);
Peter Zijlstra96abb962017-09-20 19:00:16 +0200196 if (ret) {
197 if (!lastp)
198 goto err;
199
200 *lastp = node;
201 return ret;
202 }
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200203 cnt++;
204 }
Peter Zijlstra96abb962017-09-20 19:00:16 +0200205 if (lastp)
206 *lastp = NULL;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200207 return 0;
208err:
209 /* Rollback the instances if one failed */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200210 cbm = !bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200211 if (!cbm)
212 return ret;
213
214 hlist_for_each(node, &step->list) {
215 if (!cnt--)
216 break;
Peter Zijlstra724a8682017-09-20 19:00:18 +0200217
218 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
219 ret = cbm(cpu, node);
220 trace_cpuhp_exit(cpu, st->state, state, ret);
221 /*
222 * Rollback must not fail,
223 */
224 WARN_ON_ONCE(ret);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000225 }
226 return ret;
227}
228
Rusty Russell98a79d62008-12-13 21:19:41 +1030229#ifdef CONFIG_SMP
Arnd Bergmannfcb30292018-03-15 16:38:04 +0100230static bool cpuhp_is_ap_state(enum cpuhp_state state)
231{
232 /*
233 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
234 * purposes as that state is handled explicitly in cpu_down.
235 */
236 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
237}
238
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200239static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
240{
241 struct completion *done = bringup ? &st->done_up : &st->done_down;
242 wait_for_completion(done);
243}
244
245static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
246{
247 struct completion *done = bringup ? &st->done_up : &st->done_down;
248 complete(done);
249}
250
251/*
252 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
253 */
254static bool cpuhp_is_atomic_state(enum cpuhp_state state)
255{
256 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
257}
258
Rusty Russellb3199c02008-12-30 09:05:14 +1030259/* Serializes the updates to cpu_online_mask, cpu_present_mask */
Linus Torvaldsaa953872006-07-23 12:12:16 -0700260static DEFINE_MUTEX(cpu_add_remove_lock);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000261bool cpuhp_tasks_frozen;
262EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700264/*
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530265 * The following two APIs (cpu_maps_update_begin/done) must be used when
266 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700267 */
268void cpu_maps_update_begin(void)
269{
270 mutex_lock(&cpu_add_remove_lock);
271}
272
273void cpu_maps_update_done(void)
274{
275 mutex_unlock(&cpu_add_remove_lock);
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200278/*
279 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700280 * Should always be manipulated under cpu_add_remove_lock
281 */
282static int cpu_hotplug_disabled;
283
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700284#ifdef CONFIG_HOTPLUG_CPU
285
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200286DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530287
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200288void cpus_read_lock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800289{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200290 percpu_down_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800291}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200292EXPORT_SYMBOL_GPL(cpus_read_lock);
Ashok Raj90d45d12005-11-08 21:34:24 -0800293
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200294void cpus_read_unlock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800295{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200296 percpu_up_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800297}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200298EXPORT_SYMBOL_GPL(cpus_read_unlock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800299
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200300void cpus_write_lock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100301{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200302 percpu_down_write(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100303}
304
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200305void cpus_write_unlock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100306{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200307 percpu_up_write(&cpu_hotplug_lock);
308}
309
310void lockdep_assert_cpus_held(void)
311{
312 percpu_rwsem_assert_held(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100313}
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700314
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700315/*
316 * Wait for currently running CPU hotplug operations to complete (if any) and
317 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
318 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
319 * hotplug path before performing hotplug operations. So acquiring that lock
320 * guarantees mutual exclusion from any currently running hotplug operations.
321 */
322void cpu_hotplug_disable(void)
323{
324 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700325 cpu_hotplug_disabled++;
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700326 cpu_maps_update_done();
327}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700328EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700329
Lianwei Wang01b41152016-06-09 23:43:28 -0700330static void __cpu_hotplug_enable(void)
331{
332 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
333 return;
334 cpu_hotplug_disabled--;
335}
336
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700337void cpu_hotplug_enable(void)
338{
339 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -0700340 __cpu_hotplug_enable();
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700341 cpu_maps_update_done();
342}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700343EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
Toshi Kanib9d10be2013-08-12 09:45:53 -0600344#endif /* CONFIG_HOTPLUG_CPU */
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700345
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200346#ifdef CONFIG_HOTPLUG_SMT
347enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
348
349static int __init smt_cmdline_disable(char *str)
350{
351 cpu_smt_control = CPU_SMT_DISABLED;
352 if (str && !strcmp(str, "force")) {
353 pr_info("SMT: Force disabled\n");
354 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
355 }
356 return 0;
357}
358early_param("nosmt", smt_cmdline_disable);
359
360static inline bool cpu_smt_allowed(unsigned int cpu)
361{
362 if (cpu_smt_control == CPU_SMT_ENABLED)
363 return true;
364
365 if (topology_is_primary_thread(cpu))
366 return true;
367
368 /*
369 * On x86 it's required to boot all logical CPUs at least once so
370 * that the init code can get a chance to set CR4.MCE on each
371 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
372 * core will shutdown the machine.
373 */
374 return !per_cpu(cpuhp_state, cpu).booted_once;
375}
376#else
377static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
378#endif
379
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200380static inline enum cpuhp_state
381cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
382{
383 enum cpuhp_state prev_state = st->state;
384
385 st->rollback = false;
386 st->last = NULL;
387
388 st->target = target;
389 st->single = false;
390 st->bringup = st->state < target;
391
392 return prev_state;
393}
394
395static inline void
396cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
397{
398 st->rollback = true;
399
400 /*
401 * If we have st->last we need to undo partial multi_instance of this
402 * state first. Otherwise start undo at the previous state.
403 */
404 if (!st->last) {
405 if (st->bringup)
406 st->state--;
407 else
408 st->state++;
409 }
410
411 st->target = prev_state;
412 st->bringup = !st->bringup;
413}
414
415/* Regular hotplug invocation of the AP hotplug thread */
416static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
417{
418 if (!st->single && st->state == st->target)
419 return;
420
421 st->result = 0;
422 /*
423 * Make sure the above stores are visible before should_run becomes
424 * true. Paired with the mb() above in cpuhp_thread_fun()
425 */
426 smp_mb();
427 st->should_run = true;
428 wake_up_process(st->thread);
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200429 wait_for_ap_thread(st, st->bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200430}
431
432static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
433{
434 enum cpuhp_state prev_state;
435 int ret;
436
437 prev_state = cpuhp_set_state(st, target);
438 __cpuhp_kick_ap(st);
439 if ((ret = st->result)) {
440 cpuhp_reset_state(st, prev_state);
441 __cpuhp_kick_ap(st);
442 }
443
444 return ret;
445}
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200446
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000447static int bringup_wait_for_ap(unsigned int cpu)
448{
449 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
450
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200451 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200452 wait_for_ap_thread(st, true);
Thomas Gleixnerdea1d0f2017-07-11 22:06:24 +0200453 if (WARN_ON_ONCE((!cpu_online(cpu))))
454 return -ECANCELED;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200455
456 /* Unpark the stopper thread and the hotplug thread of the target cpu */
457 stop_machine_unpark(cpu);
458 kthread_unpark(st->thread);
459
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200460 /*
461 * SMT soft disabling on X86 requires to bring the CPU out of the
462 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
463 * CPU marked itself as booted_once in cpu_notify_starting() so the
464 * cpu_smt_allowed() check will now return false if this is not the
465 * primary sibling.
466 */
467 if (!cpu_smt_allowed(cpu))
468 return -ECANCELED;
469
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200470 if (st->target <= CPUHP_AP_ONLINE_IDLE)
471 return 0;
472
473 return cpuhp_kick_ap(st, st->target);
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000474}
475
Thomas Gleixnerba997462016-02-26 18:43:24 +0000476static int bringup_cpu(unsigned int cpu)
477{
478 struct task_struct *idle = idle_thread_get(cpu);
479 int ret;
480
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400481 /*
482 * Some architectures have to walk the irq descriptors to
483 * setup the vector space for the cpu which comes online.
484 * Prevent irq alloc/free across the bringup.
485 */
486 irq_lock_sparse();
487
Thomas Gleixnerba997462016-02-26 18:43:24 +0000488 /* Arch-specific enabling code. */
489 ret = __cpu_up(cpu, idle);
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400490 irq_unlock_sparse();
Thomas Gleixner530e9b72016-12-21 20:19:53 +0100491 if (ret)
Thomas Gleixnerba997462016-02-26 18:43:24 +0000492 return ret;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200493 return bringup_wait_for_ap(cpu);
Thomas Gleixnerba997462016-02-26 18:43:24 +0000494}
495
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000496/*
497 * Hotplug state machine related functions
498 */
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000499
Thomas Gleixnera7246322016-08-12 19:49:38 +0200500static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000501{
502 for (st->state--; st->state > st->target; st->state--) {
Thomas Gleixnera7246322016-08-12 19:49:38 +0200503 struct cpuhp_step *step = cpuhp_get_step(st->state);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000504
505 if (!step->skip_onerr)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200506 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000507 }
508}
509
510static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
Thomas Gleixnera7246322016-08-12 19:49:38 +0200511 enum cpuhp_state target)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000512{
513 enum cpuhp_state prev_state = st->state;
514 int ret = 0;
515
516 while (st->state < target) {
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000517 st->state++;
Peter Zijlstra96abb962017-09-20 19:00:16 +0200518 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000519 if (ret) {
520 st->target = prev_state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200521 undo_cpu_up(cpu, st);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000522 break;
523 }
524 }
525 return ret;
526}
527
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000528/*
529 * The cpu hotplug threads manage the bringup and teardown of the cpus
530 */
531static void cpuhp_create(unsigned int cpu)
532{
533 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
534
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200535 init_completion(&st->done_up);
536 init_completion(&st->done_down);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000537}
538
539static int cpuhp_should_run(unsigned int cpu)
540{
541 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
542
543 return st->should_run;
544}
545
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000546/*
547 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
548 * callbacks when a state gets [un]installed at runtime.
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200549 *
550 * Each invocation of this function by the smpboot thread does a single AP
551 * state callback.
552 *
553 * It has 3 modes of operation:
554 * - single: runs st->cb_state
555 * - up: runs ++st->state, while st->state < st->target
556 * - down: runs st->state--, while st->state > st->target
557 *
558 * When complete or on error, should_run is cleared and the completion is fired.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000559 */
560static void cpuhp_thread_fun(unsigned int cpu)
561{
562 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200563 bool bringup = st->bringup;
564 enum cpuhp_state state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000565
566 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200567 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
568 * that if we see ->should_run we also see the rest of the state.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000569 */
570 smp_mb();
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200571
572 if (WARN_ON_ONCE(!st->should_run))
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000573 return;
574
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200575 cpuhp_lock_acquire(bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200576
Thomas Gleixnera7246322016-08-12 19:49:38 +0200577 if (st->single) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200578 state = st->cb_state;
579 st->should_run = false;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000580 } else {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200581 if (bringup) {
582 st->state++;
583 state = st->state;
584 st->should_run = (st->state < st->target);
585 WARN_ON_ONCE(st->state > st->target);
586 } else {
587 state = st->state;
588 st->state--;
589 st->should_run = (st->state > st->target);
590 WARN_ON_ONCE(st->state < st->target);
591 }
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000592 }
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200593
594 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
595
596 if (st->rollback) {
597 struct cpuhp_step *step = cpuhp_get_step(state);
598 if (step->skip_onerr)
599 goto next;
600 }
601
602 if (cpuhp_is_atomic_state(state)) {
603 local_irq_disable();
604 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
605 local_irq_enable();
606
607 /*
608 * STARTING/DYING must not fail!
609 */
610 WARN_ON_ONCE(st->result);
611 } else {
612 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
613 }
614
615 if (st->result) {
616 /*
617 * If we fail on a rollback, we're up a creek without no
618 * paddle, no way forward, no way back. We loose, thanks for
619 * playing.
620 */
621 WARN_ON_ONCE(st->rollback);
622 st->should_run = false;
623 }
624
625next:
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200626 cpuhp_lock_release(bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200627
628 if (!st->should_run)
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200629 complete_ap_thread(st, bringup);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000630}
631
632/* Invoke a single callback on a remote cpu */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200633static int
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200634cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
635 struct hlist_node *node)
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000636{
637 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200638 int ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000639
640 if (!cpu_online(cpu))
641 return 0;
642
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200643 cpuhp_lock_acquire(false);
644 cpuhp_lock_release(false);
645
646 cpuhp_lock_acquire(true);
647 cpuhp_lock_release(true);
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200648
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000649 /*
650 * If we are up and running, use the hotplug thread. For early calls
651 * we invoke the thread function directly.
652 */
653 if (!st->thread)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200654 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000655
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200656 st->rollback = false;
657 st->last = NULL;
658
659 st->node = node;
660 st->bringup = bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000661 st->cb_state = state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200662 st->single = true;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200663
664 __cpuhp_kick_ap(st);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200665
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000666 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200667 * If we failed and did a partial, do a rollback.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000668 */
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200669 if ((ret = st->result) && st->last) {
670 st->rollback = true;
671 st->bringup = !bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000672
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200673 __cpuhp_kick_ap(st);
674 }
675
Thomas Gleixner1f7c70d2017-10-21 16:06:52 +0200676 /*
677 * Clean up the leftovers so the next hotplug operation wont use stale
678 * data.
679 */
680 st->node = st->last = NULL;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200681 return ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000682}
683
684static int cpuhp_kick_ap_work(unsigned int cpu)
685{
686 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200687 enum cpuhp_state prev_state = st->state;
688 int ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000689
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200690 cpuhp_lock_acquire(false);
691 cpuhp_lock_release(false);
692
693 cpuhp_lock_acquire(true);
694 cpuhp_lock_release(true);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200695
696 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
697 ret = cpuhp_kick_ap(st, st->target);
698 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
699
700 return ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000701}
702
703static struct smp_hotplug_thread cpuhp_threads = {
704 .store = &cpuhp_state.thread,
705 .create = &cpuhp_create,
706 .thread_should_run = cpuhp_should_run,
707 .thread_fn = cpuhp_thread_fun,
708 .thread_comm = "cpuhp/%u",
709 .selfparking = true,
710};
711
712void __init cpuhp_threads_init(void)
713{
714 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
715 kthread_unpark(this_cpu_read(cpuhp_state.thread));
716}
717
Michal Hocko777c6e02016-12-07 14:54:38 +0100718#ifdef CONFIG_HOTPLUG_CPU
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700719/**
720 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
721 * @cpu: a CPU id
722 *
723 * This function walks all processes, finds a valid mm struct for each one and
724 * then clears a corresponding bit in mm's cpumask. While this all sounds
725 * trivial, there are various non-obvious corner cases, which this function
726 * tries to solve in a safe manner.
727 *
728 * Also note that the function uses a somewhat relaxed locking scheme, so it may
729 * be called only for an already offlined CPU.
730 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700731void clear_tasks_mm_cpumask(int cpu)
732{
733 struct task_struct *p;
734
735 /*
736 * This function is called after the cpu is taken down and marked
737 * offline, so its not like new tasks will ever get this cpu set in
738 * their mm mask. -- Peter Zijlstra
739 * Thus, we may use rcu_read_lock() here, instead of grabbing
740 * full-fledged tasklist_lock.
741 */
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700742 WARN_ON(cpu_online(cpu));
Anton Vorontsovcb792952012-05-31 16:26:22 -0700743 rcu_read_lock();
744 for_each_process(p) {
745 struct task_struct *t;
746
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700747 /*
748 * Main thread might exit, but other threads may still have
749 * a valid mm. Find one.
750 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700751 t = find_lock_task_mm(p);
752 if (!t)
753 continue;
754 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
755 task_unlock(t);
756 }
757 rcu_read_unlock();
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/* Take this CPU down. */
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200761static int take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000763 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
764 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000765 int err, cpu = smp_processor_id();
Peter Zijlstra724a8682017-09-20 19:00:18 +0200766 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* Ensure this CPU doesn't handle any more interrupts. */
769 err = __cpu_disable();
770 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Thomas Gleixnera7246322016-08-12 19:49:38 +0200773 /*
774 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
775 * do this step again.
776 */
777 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
778 st->state--;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000779 /* Invoke the former CPU_DYING callbacks */
Peter Zijlstra724a8682017-09-20 19:00:18 +0200780 for (; st->state > target; st->state--) {
781 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
782 /*
783 * DYING must not fail!
784 */
785 WARN_ON_ONCE(ret);
786 }
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000787
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200788 /* Give up timekeeping duties */
789 tick_handover_do_timer();
Thomas Gleixner14e568e2013-01-31 12:11:14 +0000790 /* Park the stopper thread */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000791 stop_machine_park(cpu);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Thomas Gleixner98458172016-02-26 18:43:25 +0000795static int takedown_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000797 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000798 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Thomas Gleixner2a58c522016-03-10 20:42:08 +0100800 /* Park the smpboot threads */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000801 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
802
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200803 /*
Thomas Gleixnera8994182015-07-05 17:12:30 +0000804 * Prevent irq alloc/free while the dying cpu reorganizes the
805 * interrupt affinities.
806 */
807 irq_lock_sparse();
808
809 /*
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200810 * So now all preempt/rcu users must observe !cpu_active().
811 */
Sebastian Andrzej Siewior210e2132017-05-24 10:15:28 +0200812 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
Rusty Russell04321582008-07-28 12:16:29 -0500813 if (err) {
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200814 /* CPU refused to die */
Thomas Gleixnera8994182015-07-05 17:12:30 +0000815 irq_unlock_sparse();
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200816 /* Unpark the hotplug thread so we can rollback there */
817 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
Thomas Gleixner98458172016-02-26 18:43:25 +0000818 return err;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700819 }
Rusty Russell04321582008-07-28 12:16:29 -0500820 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100822 /*
Brendan Jackman5b1ead62017-12-06 10:59:11 +0000823 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
824 * all runnable tasks from the CPU, there's only the idle task left now
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100825 * that the migration thread is done doing the stop_machine thing.
Peter Zijlstra51a96c72010-11-19 20:37:53 +0100826 *
827 * Wait for the stop thread to go away.
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100828 */
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200829 wait_for_ap_thread(st, false);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000830 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Thomas Gleixnera8994182015-07-05 17:12:30 +0000832 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
833 irq_unlock_sparse();
834
Preeti U Murthy345527b2015-03-30 14:59:19 +0530835 hotplug_cpu__broadcast_tick_pull(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* This actually kills the CPU. */
837 __cpu_die(cpu);
838
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200839 tick_cleanup_dead_cpu(cpu);
Paul E. McKenneya58163d2017-06-20 12:11:34 -0700840 rcutree_migrate_callbacks(cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000841 return 0;
842}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100844static void cpuhp_complete_idle_dead(void *arg)
845{
846 struct cpuhp_cpu_state *st = arg;
847
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200848 complete_ap_thread(st, false);
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100849}
850
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000851void cpuhp_report_idle_dead(void)
852{
853 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
854
855 BUG_ON(st->state != CPUHP_AP_OFFLINE);
Thomas Gleixner27d50c72016-02-26 18:43:44 +0000856 rcu_report_dead(smp_processor_id());
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100857 st->state = CPUHP_AP_IDLE_DEAD;
858 /*
859 * We cannot call complete after rcu_report_dead() so we delegate it
860 * to an online cpu.
861 */
862 smp_call_function_single(cpumask_first(cpu_online_mask),
863 cpuhp_complete_idle_dead, st, 0);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000864}
865
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200866static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
867{
868 for (st->state++; st->state < st->target; st->state++) {
869 struct cpuhp_step *step = cpuhp_get_step(st->state);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000870
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200871 if (!step->skip_onerr)
872 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
873 }
874}
875
876static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
877 enum cpuhp_state target)
878{
879 enum cpuhp_state prev_state = st->state;
880 int ret = 0;
881
882 for (; st->state > target; st->state--) {
883 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
884 if (ret) {
885 st->target = prev_state;
886 undo_cpu_down(cpu, st);
887 break;
888 }
889 }
890 return ret;
891}
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000892
Thomas Gleixner98458172016-02-26 18:43:25 +0000893/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000894static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
895 enum cpuhp_state target)
Thomas Gleixner98458172016-02-26 18:43:25 +0000896{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000897 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
898 int prev_state, ret = 0;
Thomas Gleixner98458172016-02-26 18:43:25 +0000899
900 if (num_online_cpus() == 1)
901 return -EBUSY;
902
Thomas Gleixner757c9892016-02-26 18:43:32 +0000903 if (!cpu_present(cpu))
Thomas Gleixner98458172016-02-26 18:43:25 +0000904 return -EINVAL;
905
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200906 cpus_write_lock();
Thomas Gleixner98458172016-02-26 18:43:25 +0000907
908 cpuhp_tasks_frozen = tasks_frozen;
909
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200910 prev_state = cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000911 /*
912 * If the current CPU state is in the range of the AP hotplug thread,
913 * then we need to kick the thread.
914 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000915 if (st->state > CPUHP_TEARDOWN_CPU) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200916 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000917 ret = cpuhp_kick_ap_work(cpu);
918 /*
919 * The AP side has done the error rollback already. Just
920 * return the error code..
921 */
922 if (ret)
923 goto out;
924
925 /*
926 * We might have stopped still in the range of the AP hotplug
927 * thread. Nothing to do anymore.
928 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000929 if (st->state > CPUHP_TEARDOWN_CPU)
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000930 goto out;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200931
932 st->target = target;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000933 }
934 /*
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000935 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000936 * to do the further cleanups.
937 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200938 ret = cpuhp_down_callbacks(cpu, st, target);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200939 if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200940 cpuhp_reset_state(st, prev_state);
941 __cpuhp_kick_ap(st);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200942 }
Thomas Gleixner98458172016-02-26 18:43:25 +0000943
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000944out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200945 cpus_write_unlock();
Thomas Gleixner941154b2017-09-12 21:37:04 +0200946 /*
947 * Do post unplug cleanup. This is still protected against
948 * concurrent CPU hotplug via cpu_add_remove_lock.
949 */
950 lockup_detector_cleanup();
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000951 return ret;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700952}
953
Thomas Gleixnercc1fe212018-05-29 17:49:05 +0200954static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
955{
956 if (cpu_hotplug_disabled)
957 return -EBUSY;
958 return _cpu_down(cpu, 0, target);
959}
960
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000961static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700962{
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100963 int err;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700964
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100965 cpu_maps_update_begin();
Thomas Gleixnercc1fe212018-05-29 17:49:05 +0200966 err = cpu_down_maps_locked(cpu, target);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100967 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return err;
969}
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200970
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000971int cpu_down(unsigned int cpu)
972{
973 return do_cpu_down(cpu, CPUHP_OFFLINE);
974}
Zhang Ruib62b8ef2008-04-29 02:35:56 -0400975EXPORT_SYMBOL(cpu_down);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200976
977#else
978#define takedown_cpu NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979#endif /*CONFIG_HOTPLUG_CPU*/
980
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000981/**
Thomas Gleixneree1e7142016-08-18 14:57:16 +0200982 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000983 * @cpu: cpu that just started
984 *
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000985 * It must be called by the arch code on the new cpu, before the new cpu
986 * enables interrupts and before the "boot" cpu returns from __cpu_up().
987 */
988void notify_cpu_starting(unsigned int cpu)
989{
990 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
991 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
Peter Zijlstra724a8682017-09-20 19:00:18 +0200992 int ret;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000993
Sebastian Andrzej Siewior0c6d4572016-08-17 14:21:04 +0200994 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200995 st->booted_once = true;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000996 while (st->state < target) {
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000997 st->state++;
Peter Zijlstra724a8682017-09-20 19:00:18 +0200998 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
999 /*
1000 * STARTING must not fail!
1001 */
1002 WARN_ON_ONCE(ret);
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001003 }
1004}
1005
Thomas Gleixner949338e2016-02-26 18:43:35 +00001006/*
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +02001007 * Called from the idle task. Wake up the controlling task which brings the
1008 * stopper and the hotplug thread of the upcoming CPU up and then delegates
1009 * the rest of the online bringup to the hotplug thread.
Thomas Gleixner949338e2016-02-26 18:43:35 +00001010 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001011void cpuhp_online_idle(enum cpuhp_state state)
Thomas Gleixner949338e2016-02-26 18:43:35 +00001012{
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001013 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001014
1015 /* Happens for the boot cpu */
1016 if (state != CPUHP_AP_ONLINE_IDLE)
1017 return;
1018
1019 st->state = CPUHP_AP_ONLINE_IDLE;
Peter Zijlstra5ebe7742017-09-20 19:00:19 +02001020 complete_ap_thread(st, true);
Thomas Gleixner949338e2016-02-26 18:43:35 +00001021}
1022
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001023/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001024static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001026 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -07001027 struct task_struct *idle;
Thomas Gleixner2e1a3482016-02-26 18:43:37 +00001028 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001030 cpus_write_lock();
Thomas Gleixner38498a62012-04-20 13:05:44 +00001031
Thomas Gleixner757c9892016-02-26 18:43:32 +00001032 if (!cpu_present(cpu)) {
Yasuaki Ishimatsu5e5041f2012-10-23 01:30:54 +02001033 ret = -EINVAL;
1034 goto out;
1035 }
1036
Thomas Gleixner757c9892016-02-26 18:43:32 +00001037 /*
1038 * The caller of do_cpu_up might have raced with another
1039 * caller. Ignore it for now.
1040 */
1041 if (st->state >= target)
Thomas Gleixner38498a62012-04-20 13:05:44 +00001042 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +00001043
1044 if (st->state == CPUHP_OFFLINE) {
1045 /* Let it fail before we try to bring the cpu up */
1046 idle = idle_thread_get(cpu);
1047 if (IS_ERR(idle)) {
1048 ret = PTR_ERR(idle);
1049 goto out;
1050 }
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -07001051 }
Thomas Gleixner38498a62012-04-20 13:05:44 +00001052
Thomas Gleixnerba997462016-02-26 18:43:24 +00001053 cpuhp_tasks_frozen = tasks_frozen;
1054
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001055 cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001056 /*
1057 * If the current CPU state is in the range of the AP hotplug thread,
1058 * then we need to kick the thread once more.
1059 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001060 if (st->state > CPUHP_BRINGUP_CPU) {
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001061 ret = cpuhp_kick_ap_work(cpu);
1062 /*
1063 * The AP side has done the error rollback already. Just
1064 * return the error code..
1065 */
1066 if (ret)
1067 goto out;
1068 }
1069
1070 /*
1071 * Try to reach the target state. We max out on the BP at
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001072 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001073 * responsible for bringing it up to the target state.
1074 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001075 target = min((int)target, CPUHP_BRINGUP_CPU);
Thomas Gleixnera7246322016-08-12 19:49:38 +02001076 ret = cpuhp_up_callbacks(cpu, st, target);
Thomas Gleixner38498a62012-04-20 13:05:44 +00001077out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001078 cpus_write_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return ret;
1080}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001081
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001082static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001083{
1084 int err = 0;
minskey guocf234222010-05-24 14:32:41 -07001085
Rusty Russelle0b582e2009-01-01 10:12:28 +10301086 if (!cpu_possible(cpu)) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001087 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1088 cpu);
Chen Gong87d5e0232010-03-05 13:42:38 -08001089#if defined(CONFIG_IA64)
Fabian Frederick84117da2014-06-04 16:11:17 -07001090 pr_err("please check additional_cpus= boot parameter\n");
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -07001091#endif
1092 return -EINVAL;
1093 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001094
Toshi Kani01b0f192013-11-12 15:07:25 -08001095 err = try_online_node(cpu_to_node(cpu));
1096 if (err)
1097 return err;
minskey guocf234222010-05-24 14:32:41 -07001098
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001099 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001100
Max Krasnyanskye761b772008-07-15 04:43:49 -07001101 if (cpu_hotplug_disabled) {
1102 err = -EBUSY;
1103 goto out;
1104 }
Thomas Gleixner05736e42018-05-29 17:48:27 +02001105 if (!cpu_smt_allowed(cpu)) {
1106 err = -EPERM;
1107 goto out;
1108 }
Max Krasnyanskye761b772008-07-15 04:43:49 -07001109
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001110 err = _cpu_up(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -07001111out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001112 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001113 return err;
1114}
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001115
1116int cpu_up(unsigned int cpu)
1117{
1118 return do_cpu_up(cpu, CPUHP_ONLINE);
1119}
Paul E. McKenneya513f6b2011-12-11 21:54:45 -08001120EXPORT_SYMBOL_GPL(cpu_up);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001121
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001122#ifdef CONFIG_PM_SLEEP_SMP
Rusty Russelle0b582e2009-01-01 10:12:28 +10301123static cpumask_var_t frozen_cpus;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001124
James Morsed391e552016-08-17 13:50:25 +01001125int freeze_secondary_cpus(int primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001126{
James Morsed391e552016-08-17 13:50:25 +01001127 int cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001128
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001129 cpu_maps_update_begin();
James Morsed391e552016-08-17 13:50:25 +01001130 if (!cpu_online(primary))
1131 primary = cpumask_first(cpu_online_mask);
Xiaotian Feng9ee349a2009-12-16 18:04:32 +01001132 /*
1133 * We take down all of the non-boot CPUs in one shot to avoid races
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001134 * with the userspace trying to use the CPU hotplug at the same time
1135 */
Rusty Russelle0b582e2009-01-01 10:12:28 +10301136 cpumask_clear(frozen_cpus);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01001137
Fabian Frederick84117da2014-06-04 16:11:17 -07001138 pr_info("Disabling non-boot CPUs ...\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001139 for_each_online_cpu(cpu) {
James Morsed391e552016-08-17 13:50:25 +01001140 if (cpu == primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001141 continue;
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001142 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001143 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001144 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
Mike Travisfeae3202009-11-17 18:22:13 -06001145 if (!error)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301146 cpumask_set_cpu(cpu, frozen_cpus);
Mike Travisfeae3202009-11-17 18:22:13 -06001147 else {
Fabian Frederick84117da2014-06-04 16:11:17 -07001148 pr_err("Error taking CPU%d down: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001149 break;
1150 }
1151 }
Joseph Cihula86886e52009-06-30 19:31:07 -07001152
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001153 if (!error)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001154 BUG_ON(num_online_cpus() > 1);
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001155 else
Fabian Frederick84117da2014-06-04 16:11:17 -07001156 pr_err("Non-boot CPUs are not disabled\n");
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001157
1158 /*
1159 * Make sure the CPUs won't be enabled by someone else. We need to do
1160 * this even in case of failure as all disable_nonboot_cpus() users are
1161 * supposed to do enable_nonboot_cpus() on the failure path.
1162 */
1163 cpu_hotplug_disabled++;
1164
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001165 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001166 return error;
1167}
1168
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001169void __weak arch_enable_nonboot_cpus_begin(void)
1170{
1171}
1172
1173void __weak arch_enable_nonboot_cpus_end(void)
1174{
1175}
1176
Mathias Krause71cf5ae2015-07-19 20:06:22 +02001177void enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001178{
1179 int cpu, error;
1180
1181 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001182 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -07001183 __cpu_hotplug_enable();
Rusty Russelle0b582e2009-01-01 10:12:28 +10301184 if (cpumask_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001185 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001186
Fabian Frederick84117da2014-06-04 16:11:17 -07001187 pr_info("Enabling non-boot CPUs ...\n");
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001188
1189 arch_enable_nonboot_cpus_begin();
1190
Rusty Russelle0b582e2009-01-01 10:12:28 +10301191 for_each_cpu(cpu, frozen_cpus) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001192 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001193 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001194 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001195 if (!error) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001196 pr_info("CPU%d is up\n", cpu);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001197 continue;
1198 }
Fabian Frederick84117da2014-06-04 16:11:17 -07001199 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001200 }
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001201
1202 arch_enable_nonboot_cpus_end();
1203
Rusty Russelle0b582e2009-01-01 10:12:28 +10301204 cpumask_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001205out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001206 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001207}
Rusty Russelle0b582e2009-01-01 10:12:28 +10301208
Fenghua Yud7268a32011-11-15 21:59:31 +01001209static int __init alloc_frozen_cpus(void)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301210{
1211 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1212 return -ENOMEM;
1213 return 0;
1214}
1215core_initcall(alloc_frozen_cpus);
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001216
1217/*
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001218 * When callbacks for CPU hotplug notifications are being executed, we must
1219 * ensure that the state of the system with respect to the tasks being frozen
1220 * or not, as reported by the notification, remains unchanged *throughout the
1221 * duration* of the execution of the callbacks.
1222 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1223 *
1224 * This synchronization is implemented by mutually excluding regular CPU
1225 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1226 * Hibernate notifications.
1227 */
1228static int
1229cpu_hotplug_pm_callback(struct notifier_block *nb,
1230 unsigned long action, void *ptr)
1231{
1232 switch (action) {
1233
1234 case PM_SUSPEND_PREPARE:
1235 case PM_HIBERNATION_PREPARE:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001236 cpu_hotplug_disable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001237 break;
1238
1239 case PM_POST_SUSPEND:
1240 case PM_POST_HIBERNATION:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001241 cpu_hotplug_enable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001242 break;
1243
1244 default:
1245 return NOTIFY_DONE;
1246 }
1247
1248 return NOTIFY_OK;
1249}
1250
1251
Fenghua Yud7268a32011-11-15 21:59:31 +01001252static int __init cpu_hotplug_pm_sync_init(void)
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001253{
Fenghua Yu6e32d472012-11-13 11:32:43 -08001254 /*
1255 * cpu_hotplug_pm_callback has higher priority than x86
1256 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1257 * to disable cpu hotplug to avoid cpu hotplug race.
1258 */
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001259 pm_notifier(cpu_hotplug_pm_callback, 0);
1260 return 0;
1261}
1262core_initcall(cpu_hotplug_pm_sync_init);
1263
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001264#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001265
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01001266int __boot_cpu_id;
1267
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001268#endif /* CONFIG_SMP */
Mike Travisb8d317d2008-07-24 18:21:29 -07001269
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001270/* Boot processor state steps */
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001271static struct cpuhp_step cpuhp_hp_states[] = {
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001272 [CPUHP_OFFLINE] = {
1273 .name = "offline",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001274 .startup.single = NULL,
1275 .teardown.single = NULL,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001276 },
1277#ifdef CONFIG_SMP
1278 [CPUHP_CREATE_THREADS]= {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001279 .name = "threads:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001280 .startup.single = smpboot_create_threads,
1281 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001282 .cant_stop = true,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001283 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001284 [CPUHP_PERF_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001285 .name = "perf:prepare",
1286 .startup.single = perf_event_init_cpu,
1287 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001288 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001289 [CPUHP_WORKQUEUE_PREP] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001290 .name = "workqueue:prepare",
1291 .startup.single = workqueue_prepare_cpu,
1292 .teardown.single = NULL,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001293 },
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001294 [CPUHP_HRTIMERS_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001295 .name = "hrtimers:prepare",
1296 .startup.single = hrtimers_prepare_cpu,
1297 .teardown.single = hrtimers_dead_cpu,
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001298 },
Richard Weinberger31487f82016-07-13 17:17:01 +00001299 [CPUHP_SMPCFD_PREPARE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001300 .name = "smpcfd:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001301 .startup.single = smpcfd_prepare_cpu,
1302 .teardown.single = smpcfd_dead_cpu,
Richard Weinberger31487f82016-07-13 17:17:01 +00001303 },
Richard Weinbergere6d49892016-08-18 14:57:17 +02001304 [CPUHP_RELAY_PREPARE] = {
1305 .name = "relay:prepare",
1306 .startup.single = relay_prepare_cpu,
1307 .teardown.single = NULL,
1308 },
Sebastian Andrzej Siewior6731d4f2016-08-23 14:53:19 +02001309 [CPUHP_SLAB_PREPARE] = {
1310 .name = "slab:prepare",
1311 .startup.single = slab_prepare_cpu,
1312 .teardown.single = slab_dead_cpu,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001313 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001314 [CPUHP_RCUTREE_PREP] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001315 .name = "RCU/tree:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001316 .startup.single = rcutree_prepare_cpu,
1317 .teardown.single = rcutree_dead_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001318 },
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001319 /*
Richard Cochran4fae16d2016-07-27 11:08:18 +02001320 * On the tear-down path, timers_dead_cpu() must be invoked
1321 * before blk_mq_queue_reinit_notify() from notify_dead(),
1322 * otherwise a RCU stall occurs.
1323 */
Thomas Gleixner26456f82017-12-27 21:37:25 +01001324 [CPUHP_TIMERS_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001325 .name = "timers:dead",
Thomas Gleixner26456f82017-12-27 21:37:25 +01001326 .startup.single = timers_prepare_cpu,
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001327 .teardown.single = timers_dead_cpu,
Richard Cochran4fae16d2016-07-27 11:08:18 +02001328 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001329 /* Kicks the plugged cpu into life */
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001330 [CPUHP_BRINGUP_CPU] = {
1331 .name = "cpu:bringup",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001332 .startup.single = bringup_cpu,
1333 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001334 .cant_stop = true,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001335 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001336 /* Final state before CPU kills itself */
1337 [CPUHP_AP_IDLE_DEAD] = {
1338 .name = "idle:dead",
1339 },
1340 /*
1341 * Last state before CPU enters the idle loop to die. Transient state
1342 * for synchronization.
1343 */
1344 [CPUHP_AP_OFFLINE] = {
1345 .name = "ap:offline",
1346 .cant_stop = true,
1347 },
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001348 /* First state is scheduler control. Interrupts are disabled */
1349 [CPUHP_AP_SCHED_STARTING] = {
1350 .name = "sched:starting",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001351 .startup.single = sched_cpu_starting,
1352 .teardown.single = sched_cpu_dying,
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001353 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001354 [CPUHP_AP_RCUTREE_DYING] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001355 .name = "RCU/tree:dying",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001356 .startup.single = NULL,
1357 .teardown.single = rcutree_dying_cpu,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001358 },
Lai Jiangshan46febd32017-11-28 21:19:53 +08001359 [CPUHP_AP_SMPCFD_DYING] = {
1360 .name = "smpcfd:dying",
1361 .startup.single = NULL,
1362 .teardown.single = smpcfd_dying_cpu,
1363 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001364 /* Entry state on starting. Interrupts enabled from here on. Transient
1365 * state for synchronsization */
1366 [CPUHP_AP_ONLINE] = {
1367 .name = "ap:online",
1368 },
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001369 /*
1370 * Handled on controll processor until the plugged processor manages
1371 * this itself.
1372 */
1373 [CPUHP_TEARDOWN_CPU] = {
1374 .name = "cpu:teardown",
1375 .startup.single = NULL,
1376 .teardown.single = takedown_cpu,
1377 .cant_stop = true,
1378 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001379 /* Handle smpboot threads park/unpark */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001380 [CPUHP_AP_SMPBOOT_THREADS] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001381 .name = "smpboot/threads:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001382 .startup.single = smpboot_unpark_threads,
Thomas Gleixnerc4de6562018-05-29 19:05:25 +02001383 .teardown.single = smpboot_park_threads,
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001384 },
Thomas Gleixnerc5cb83b2017-06-20 01:37:51 +02001385 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1386 .name = "irq/affinity:online",
1387 .startup.single = irq_affinity_online_cpu,
1388 .teardown.single = NULL,
1389 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001390 [CPUHP_AP_PERF_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001391 .name = "perf:online",
1392 .startup.single = perf_event_init_cpu,
1393 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001394 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001395 [CPUHP_AP_WORKQUEUE_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001396 .name = "workqueue:online",
1397 .startup.single = workqueue_online_cpu,
1398 .teardown.single = workqueue_offline_cpu,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001399 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001400 [CPUHP_AP_RCUTREE_ONLINE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001401 .name = "RCU/tree:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001402 .startup.single = rcutree_online_cpu,
1403 .teardown.single = rcutree_offline_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001404 },
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001405#endif
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001406 /*
1407 * The dynamically registered state space is here
1408 */
1409
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001410#ifdef CONFIG_SMP
1411 /* Last state is scheduler control setting the cpu active */
1412 [CPUHP_AP_ACTIVE] = {
1413 .name = "sched:active",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001414 .startup.single = sched_cpu_activate,
1415 .teardown.single = sched_cpu_deactivate,
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001416 },
1417#endif
1418
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001419 /* CPU is fully up and running. */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001420 [CPUHP_ONLINE] = {
1421 .name = "online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001422 .startup.single = NULL,
1423 .teardown.single = NULL,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001424 },
1425};
1426
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001427/* Sanity check for callbacks */
1428static int cpuhp_cb_check(enum cpuhp_state state)
1429{
1430 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1431 return -EINVAL;
1432 return 0;
1433}
1434
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001435/*
1436 * Returns a free for dynamic slot assignment of the Online state. The states
1437 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1438 * by having no name assigned.
1439 */
1440static int cpuhp_reserve_state(enum cpuhp_state state)
1441{
Thomas Gleixner4205e472017-01-10 14:01:05 +01001442 enum cpuhp_state i, end;
1443 struct cpuhp_step *step;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001444
Thomas Gleixner4205e472017-01-10 14:01:05 +01001445 switch (state) {
1446 case CPUHP_AP_ONLINE_DYN:
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001447 step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
Thomas Gleixner4205e472017-01-10 14:01:05 +01001448 end = CPUHP_AP_ONLINE_DYN_END;
1449 break;
1450 case CPUHP_BP_PREPARE_DYN:
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001451 step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
Thomas Gleixner4205e472017-01-10 14:01:05 +01001452 end = CPUHP_BP_PREPARE_DYN_END;
1453 break;
1454 default:
1455 return -EINVAL;
1456 }
1457
1458 for (i = state; i <= end; i++, step++) {
1459 if (!step->name)
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001460 return i;
1461 }
1462 WARN(1, "No more dynamic states available for CPU hotplug\n");
1463 return -ENOSPC;
1464}
1465
1466static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1467 int (*startup)(unsigned int cpu),
1468 int (*teardown)(unsigned int cpu),
1469 bool multi_instance)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001470{
1471 /* (Un)Install the callbacks for further cpu hotplug operations */
1472 struct cpuhp_step *sp;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001473 int ret = 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001474
Ethan Barnes0c96b272017-07-19 22:36:00 +00001475 /*
1476 * If name is NULL, then the state gets removed.
1477 *
1478 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1479 * the first allocation from these dynamic ranges, so the removal
1480 * would trigger a new allocation and clear the wrong (already
1481 * empty) state, leaving the callbacks of the to be cleared state
1482 * dangling, which causes wreckage on the next hotplug operation.
1483 */
1484 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1485 state == CPUHP_BP_PREPARE_DYN)) {
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001486 ret = cpuhp_reserve_state(state);
1487 if (ret < 0)
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001488 return ret;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001489 state = ret;
1490 }
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001491 sp = cpuhp_get_step(state);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001492 if (name && sp->name)
1493 return -EBUSY;
1494
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001495 sp->startup.single = startup;
1496 sp->teardown.single = teardown;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001497 sp->name = name;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001498 sp->multi_instance = multi_instance;
1499 INIT_HLIST_HEAD(&sp->list);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001500 return ret;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001501}
1502
1503static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1504{
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001505 return cpuhp_get_step(state)->teardown.single;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001506}
1507
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001508/*
1509 * Call the startup/teardown function for a step either on the AP or
1510 * on the current CPU.
1511 */
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001512static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1513 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001514{
Thomas Gleixnera7246322016-08-12 19:49:38 +02001515 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001516 int ret;
1517
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001518 /*
1519 * If there's nothing to do, we done.
1520 * Relies on the union for multi_instance.
1521 */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001522 if ((bringup && !sp->startup.single) ||
1523 (!bringup && !sp->teardown.single))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001524 return 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001525 /*
1526 * The non AP bound callbacks can fail on bringup. On teardown
1527 * e.g. module removal we crash for now.
1528 */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001529#ifdef CONFIG_SMP
1530 if (cpuhp_is_ap_state(state))
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001531 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001532 else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001533 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001534#else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001535 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001536#endif
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001537 BUG_ON(ret && !bringup);
1538 return ret;
1539}
1540
1541/*
1542 * Called from __cpuhp_setup_state on a recoverable failure.
1543 *
1544 * Note: The teardown callbacks for rollback are not allowed to fail!
1545 */
1546static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001547 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001548{
1549 int cpu;
1550
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001551 /* Roll back the already executed steps on the other cpus */
1552 for_each_present_cpu(cpu) {
1553 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1554 int cpustate = st->state;
1555
1556 if (cpu >= failedcpu)
1557 break;
1558
1559 /* Did we invoke the startup call on that cpu ? */
1560 if (cpustate >= state)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001561 cpuhp_issue_call(cpu, state, false, node);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001562 }
1563}
1564
Thomas Gleixner9805c672017-05-24 10:15:15 +02001565int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1566 struct hlist_node *node,
1567 bool invoke)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001568{
1569 struct cpuhp_step *sp;
1570 int cpu;
1571 int ret;
1572
Thomas Gleixner9805c672017-05-24 10:15:15 +02001573 lockdep_assert_cpus_held();
1574
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001575 sp = cpuhp_get_step(state);
1576 if (sp->multi_instance == false)
1577 return -EINVAL;
1578
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001579 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001580
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001581 if (!invoke || !sp->startup.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001582 goto add_node;
1583
1584 /*
1585 * Try to call the startup callback for each present cpu
1586 * depending on the hotplug state of the cpu.
1587 */
1588 for_each_present_cpu(cpu) {
1589 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1590 int cpustate = st->state;
1591
1592 if (cpustate < state)
1593 continue;
1594
1595 ret = cpuhp_issue_call(cpu, state, true, node);
1596 if (ret) {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001597 if (sp->teardown.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001598 cpuhp_rollback_install(cpu, state, node);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001599 goto unlock;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001600 }
1601 }
1602add_node:
1603 ret = 0;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001604 hlist_add_head(node, &sp->list);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001605unlock:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001606 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner9805c672017-05-24 10:15:15 +02001607 return ret;
1608}
1609
1610int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1611 bool invoke)
1612{
1613 int ret;
1614
1615 cpus_read_lock();
1616 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001617 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001618 return ret;
1619}
1620EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1621
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001622/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001623 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001624 * @state: The state to setup
1625 * @invoke: If true, the startup function is invoked for cpus where
1626 * cpu state >= @state
1627 * @startup: startup callback function
1628 * @teardown: teardown callback function
1629 * @multi_instance: State is set up for multiple instances which get
1630 * added afterwards.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001631 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001632 * The caller needs to hold cpus read locked while calling this function.
Boris Ostrovsky512f0982016-12-15 10:00:57 -05001633 * Returns:
1634 * On success:
1635 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1636 * 0 for all other states
1637 * On failure: proper (negative) error code
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001638 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001639int __cpuhp_setup_state_cpuslocked(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)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001644{
1645 int cpu, ret = 0;
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001646 bool dynstate;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001647
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001648 lockdep_assert_cpus_held();
1649
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001650 if (cpuhp_cb_check(state) || !name)
1651 return -EINVAL;
1652
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001653 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001654
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001655 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1656 multi_instance);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001657
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001658 dynstate = state == CPUHP_AP_ONLINE_DYN;
1659 if (ret > 0 && dynstate) {
1660 state = ret;
1661 ret = 0;
1662 }
1663
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001664 if (ret || !invoke || !startup)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001665 goto out;
1666
1667 /*
1668 * Try to call the startup callback for each present cpu
1669 * depending on the hotplug state of the cpu.
1670 */
1671 for_each_present_cpu(cpu) {
1672 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1673 int cpustate = st->state;
1674
1675 if (cpustate < state)
1676 continue;
1677
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001678 ret = cpuhp_issue_call(cpu, state, true, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001679 if (ret) {
Thomas Gleixnera7246322016-08-12 19:49:38 +02001680 if (teardown)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001681 cpuhp_rollback_install(cpu, state, NULL);
1682 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001683 goto out;
1684 }
1685 }
1686out:
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001687 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001688 /*
1689 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1690 * dynamically allocated state in case of success.
1691 */
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001692 if (!ret && dynstate)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001693 return state;
1694 return ret;
1695}
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001696EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1697
1698int __cpuhp_setup_state(enum cpuhp_state state,
1699 const char *name, bool invoke,
1700 int (*startup)(unsigned int cpu),
1701 int (*teardown)(unsigned int cpu),
1702 bool multi_instance)
1703{
1704 int ret;
1705
1706 cpus_read_lock();
1707 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1708 teardown, multi_instance);
1709 cpus_read_unlock();
1710 return ret;
1711}
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001712EXPORT_SYMBOL(__cpuhp_setup_state);
1713
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001714int __cpuhp_state_remove_instance(enum cpuhp_state state,
1715 struct hlist_node *node, bool invoke)
1716{
1717 struct cpuhp_step *sp = cpuhp_get_step(state);
1718 int cpu;
1719
1720 BUG_ON(cpuhp_cb_check(state));
1721
1722 if (!sp->multi_instance)
1723 return -EINVAL;
1724
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001725 cpus_read_lock();
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001726 mutex_lock(&cpuhp_state_mutex);
1727
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001728 if (!invoke || !cpuhp_get_teardown_cb(state))
1729 goto remove;
1730 /*
1731 * Call the teardown callback for each present cpu depending
1732 * on the hotplug state of the cpu. This function is not
1733 * allowed to fail currently!
1734 */
1735 for_each_present_cpu(cpu) {
1736 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1737 int cpustate = st->state;
1738
1739 if (cpustate >= state)
1740 cpuhp_issue_call(cpu, state, false, node);
1741 }
1742
1743remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001744 hlist_del(node);
1745 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001746 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001747
1748 return 0;
1749}
1750EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001751
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001752/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001753 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001754 * @state: The state to remove
1755 * @invoke: If true, the teardown function is invoked for cpus where
1756 * cpu state >= @state
1757 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001758 * The caller needs to hold cpus read locked while calling this function.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001759 * The teardown callback is currently not allowed to fail. Think
1760 * about module removal!
1761 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001762void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001763{
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001764 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001765 int cpu;
1766
1767 BUG_ON(cpuhp_cb_check(state));
1768
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001769 lockdep_assert_cpus_held();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001770
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001771 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001772 if (sp->multi_instance) {
1773 WARN(!hlist_empty(&sp->list),
1774 "Error: Removing state %d which has instances left.\n",
1775 state);
1776 goto remove;
1777 }
1778
Thomas Gleixnera7246322016-08-12 19:49:38 +02001779 if (!invoke || !cpuhp_get_teardown_cb(state))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001780 goto remove;
1781
1782 /*
1783 * Call the teardown callback for each present cpu depending
1784 * on the hotplug state of the cpu. This function is not
1785 * allowed to fail currently!
1786 */
1787 for_each_present_cpu(cpu) {
1788 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1789 int cpustate = st->state;
1790
1791 if (cpustate >= state)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001792 cpuhp_issue_call(cpu, state, false, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001793 }
1794remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001795 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001796 mutex_unlock(&cpuhp_state_mutex);
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001797}
1798EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1799
1800void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1801{
1802 cpus_read_lock();
1803 __cpuhp_remove_state_cpuslocked(state, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001804 cpus_read_unlock();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001805}
1806EXPORT_SYMBOL(__cpuhp_remove_state);
1807
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001808#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1809static ssize_t show_cpuhp_state(struct device *dev,
1810 struct device_attribute *attr, char *buf)
1811{
1812 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1813
1814 return sprintf(buf, "%d\n", st->state);
1815}
1816static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1817
Thomas Gleixner757c9892016-02-26 18:43:32 +00001818static ssize_t write_cpuhp_target(struct device *dev,
1819 struct device_attribute *attr,
1820 const char *buf, size_t count)
1821{
1822 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1823 struct cpuhp_step *sp;
1824 int target, ret;
1825
1826 ret = kstrtoint(buf, 10, &target);
1827 if (ret)
1828 return ret;
1829
1830#ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1831 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1832 return -EINVAL;
1833#else
1834 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1835 return -EINVAL;
1836#endif
1837
1838 ret = lock_device_hotplug_sysfs();
1839 if (ret)
1840 return ret;
1841
1842 mutex_lock(&cpuhp_state_mutex);
1843 sp = cpuhp_get_step(target);
1844 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1845 mutex_unlock(&cpuhp_state_mutex);
1846 if (ret)
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001847 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +00001848
1849 if (st->state < target)
1850 ret = do_cpu_up(dev->id, target);
1851 else
1852 ret = do_cpu_down(dev->id, target);
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001853out:
Thomas Gleixner757c9892016-02-26 18:43:32 +00001854 unlock_device_hotplug();
1855 return ret ? ret : count;
1856}
1857
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001858static ssize_t show_cpuhp_target(struct device *dev,
1859 struct device_attribute *attr, char *buf)
1860{
1861 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1862
1863 return sprintf(buf, "%d\n", st->target);
1864}
Thomas Gleixner757c9892016-02-26 18:43:32 +00001865static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001866
Peter Zijlstra1db49482017-09-20 19:00:21 +02001867
1868static ssize_t write_cpuhp_fail(struct device *dev,
1869 struct device_attribute *attr,
1870 const char *buf, size_t count)
1871{
1872 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1873 struct cpuhp_step *sp;
1874 int fail, ret;
1875
1876 ret = kstrtoint(buf, 10, &fail);
1877 if (ret)
1878 return ret;
1879
1880 /*
1881 * Cannot fail STARTING/DYING callbacks.
1882 */
1883 if (cpuhp_is_atomic_state(fail))
1884 return -EINVAL;
1885
1886 /*
1887 * Cannot fail anything that doesn't have callbacks.
1888 */
1889 mutex_lock(&cpuhp_state_mutex);
1890 sp = cpuhp_get_step(fail);
1891 if (!sp->startup.single && !sp->teardown.single)
1892 ret = -EINVAL;
1893 mutex_unlock(&cpuhp_state_mutex);
1894 if (ret)
1895 return ret;
1896
1897 st->fail = fail;
1898
1899 return count;
1900}
1901
1902static ssize_t show_cpuhp_fail(struct device *dev,
1903 struct device_attribute *attr, char *buf)
1904{
1905 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1906
1907 return sprintf(buf, "%d\n", st->fail);
1908}
1909
1910static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
1911
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001912static struct attribute *cpuhp_cpu_attrs[] = {
1913 &dev_attr_state.attr,
1914 &dev_attr_target.attr,
Peter Zijlstra1db49482017-09-20 19:00:21 +02001915 &dev_attr_fail.attr,
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001916 NULL
1917};
1918
Arvind Yadav993647a2017-06-29 17:40:47 +05301919static const struct attribute_group cpuhp_cpu_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001920 .attrs = cpuhp_cpu_attrs,
1921 .name = "hotplug",
1922 NULL
1923};
1924
1925static ssize_t show_cpuhp_states(struct device *dev,
1926 struct device_attribute *attr, char *buf)
1927{
1928 ssize_t cur, res = 0;
1929 int i;
1930
1931 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner757c9892016-02-26 18:43:32 +00001932 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001933 struct cpuhp_step *sp = cpuhp_get_step(i);
1934
1935 if (sp->name) {
1936 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
1937 buf += cur;
1938 res += cur;
1939 }
1940 }
1941 mutex_unlock(&cpuhp_state_mutex);
1942 return res;
1943}
1944static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
1945
1946static struct attribute *cpuhp_cpu_root_attrs[] = {
1947 &dev_attr_states.attr,
1948 NULL
1949};
1950
Arvind Yadav993647a2017-06-29 17:40:47 +05301951static const struct attribute_group cpuhp_cpu_root_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001952 .attrs = cpuhp_cpu_root_attrs,
1953 .name = "hotplug",
1954 NULL
1955};
1956
Thomas Gleixner05736e42018-05-29 17:48:27 +02001957#ifdef CONFIG_HOTPLUG_SMT
1958
1959static const char *smt_states[] = {
1960 [CPU_SMT_ENABLED] = "on",
1961 [CPU_SMT_DISABLED] = "off",
1962 [CPU_SMT_FORCE_DISABLED] = "forceoff",
1963 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
1964};
1965
1966static ssize_t
1967show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
1968{
1969 return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
1970}
1971
1972static void cpuhp_offline_cpu_device(unsigned int cpu)
1973{
1974 struct device *dev = get_cpu_device(cpu);
1975
1976 dev->offline = true;
1977 /* Tell user space about the state change */
1978 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1979}
1980
1981static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
1982{
1983 int cpu, ret = 0;
1984
1985 cpu_maps_update_begin();
1986 for_each_online_cpu(cpu) {
1987 if (topology_is_primary_thread(cpu))
1988 continue;
1989 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
1990 if (ret)
1991 break;
1992 /*
1993 * As this needs to hold the cpu maps lock it's impossible
1994 * to call device_offline() because that ends up calling
1995 * cpu_down() which takes cpu maps lock. cpu maps lock
1996 * needs to be held as this might race against in kernel
1997 * abusers of the hotplug machinery (thermal management).
1998 *
1999 * So nothing would update device:offline state. That would
2000 * leave the sysfs entry stale and prevent onlining after
2001 * smt control has been changed to 'off' again. This is
2002 * called under the sysfs hotplug lock, so it is properly
2003 * serialized against the regular offline usage.
2004 */
2005 cpuhp_offline_cpu_device(cpu);
2006 }
2007 if (!ret)
2008 cpu_smt_control = ctrlval;
2009 cpu_maps_update_done();
2010 return ret;
2011}
2012
2013static void cpuhp_smt_enable(void)
2014{
2015 cpu_maps_update_begin();
2016 cpu_smt_control = CPU_SMT_ENABLED;
2017 cpu_maps_update_done();
2018}
2019
2020static ssize_t
2021store_smt_control(struct device *dev, struct device_attribute *attr,
2022 const char *buf, size_t count)
2023{
2024 int ctrlval, ret;
2025
2026 if (sysfs_streq(buf, "on"))
2027 ctrlval = CPU_SMT_ENABLED;
2028 else if (sysfs_streq(buf, "off"))
2029 ctrlval = CPU_SMT_DISABLED;
2030 else if (sysfs_streq(buf, "forceoff"))
2031 ctrlval = CPU_SMT_FORCE_DISABLED;
2032 else
2033 return -EINVAL;
2034
2035 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2036 return -EPERM;
2037
2038 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2039 return -ENODEV;
2040
2041 ret = lock_device_hotplug_sysfs();
2042 if (ret)
2043 return ret;
2044
2045 if (ctrlval != cpu_smt_control) {
2046 switch (ctrlval) {
2047 case CPU_SMT_ENABLED:
2048 cpuhp_smt_enable();
2049 break;
2050 case CPU_SMT_DISABLED:
2051 case CPU_SMT_FORCE_DISABLED:
2052 ret = cpuhp_smt_disable(ctrlval);
2053 break;
2054 }
2055 }
2056
2057 unlock_device_hotplug();
2058 return ret ? ret : count;
2059}
2060static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2061
2062static ssize_t
2063show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2064{
2065 bool active = topology_max_smt_threads() > 1;
2066
2067 return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
2068}
2069static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2070
2071static struct attribute *cpuhp_smt_attrs[] = {
2072 &dev_attr_control.attr,
2073 &dev_attr_active.attr,
2074 NULL
2075};
2076
2077static const struct attribute_group cpuhp_smt_attr_group = {
2078 .attrs = cpuhp_smt_attrs,
2079 .name = "smt",
2080 NULL
2081};
2082
2083static int __init cpu_smt_state_init(void)
2084{
2085 if (!topology_smt_supported())
2086 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
2087
2088 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2089 &cpuhp_smt_attr_group);
2090}
2091
2092#else
2093static inline int cpu_smt_state_init(void) { return 0; }
2094#endif
2095
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00002096static int __init cpuhp_sysfs_init(void)
2097{
2098 int cpu, ret;
2099
Thomas Gleixner05736e42018-05-29 17:48:27 +02002100 ret = cpu_smt_state_init();
2101 if (ret)
2102 return ret;
2103
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00002104 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2105 &cpuhp_cpu_root_attr_group);
2106 if (ret)
2107 return ret;
2108
2109 for_each_possible_cpu(cpu) {
2110 struct device *dev = get_cpu_device(cpu);
2111
2112 if (!dev)
2113 continue;
2114 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2115 if (ret)
2116 return ret;
2117 }
2118 return 0;
2119}
2120device_initcall(cpuhp_sysfs_init);
2121#endif
2122
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002123/*
2124 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2125 * represents all NR_CPUS bits binary values of 1<<nr.
2126 *
Rusty Russelle0b582e2009-01-01 10:12:28 +10302127 * It is used by cpumask_of() to get a constant address to a CPU
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002128 * mask value that has a single bit set only.
2129 */
Mike Travisb8d317d2008-07-24 18:21:29 -07002130
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002131/* cpu_bit_bitmap[0] is empty - so we can back into it */
Michael Rodriguez4d519852011-03-22 16:34:07 -07002132#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002133#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2134#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2135#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
Mike Travisb8d317d2008-07-24 18:21:29 -07002136
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002137const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
Mike Travisb8d317d2008-07-24 18:21:29 -07002138
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002139 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2140 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2141#if BITS_PER_LONG > 32
2142 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2143 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
Mike Travisb8d317d2008-07-24 18:21:29 -07002144#endif
2145};
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002146EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
Rusty Russell2d3854a2008-11-05 13:39:10 +11002147
2148const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2149EXPORT_SYMBOL(cpu_all_bits);
Rusty Russellb3199c02008-12-30 09:05:14 +10302150
2151#ifdef CONFIG_INIT_ALL_POSSIBLE
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002152struct cpumask __cpu_possible_mask __read_mostly
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002153 = {CPU_BITS_ALL};
Rusty Russellb3199c02008-12-30 09:05:14 +10302154#else
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002155struct cpumask __cpu_possible_mask __read_mostly;
Rusty Russellb3199c02008-12-30 09:05:14 +10302156#endif
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002157EXPORT_SYMBOL(__cpu_possible_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10302158
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002159struct cpumask __cpu_online_mask __read_mostly;
2160EXPORT_SYMBOL(__cpu_online_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10302161
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002162struct cpumask __cpu_present_mask __read_mostly;
2163EXPORT_SYMBOL(__cpu_present_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10302164
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002165struct cpumask __cpu_active_mask __read_mostly;
2166EXPORT_SYMBOL(__cpu_active_mask);
Rusty Russell3fa41522008-12-30 09:05:16 +10302167
Rusty Russell3fa41522008-12-30 09:05:16 +10302168void init_cpu_present(const struct cpumask *src)
2169{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002170 cpumask_copy(&__cpu_present_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10302171}
2172
2173void init_cpu_possible(const struct cpumask *src)
2174{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002175 cpumask_copy(&__cpu_possible_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10302176}
2177
2178void init_cpu_online(const struct cpumask *src)
2179{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002180 cpumask_copy(&__cpu_online_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10302181}
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002182
2183/*
2184 * Activate the first processor.
2185 */
2186void __init boot_cpu_init(void)
2187{
2188 int cpu = smp_processor_id();
2189
2190 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2191 set_cpu_online(cpu, true);
2192 set_cpu_active(cpu, true);
2193 set_cpu_present(cpu, true);
2194 set_cpu_possible(cpu, true);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01002195
2196#ifdef CONFIG_SMP
2197 __boot_cpu_id = cpu;
2198#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002199}
2200
2201/*
2202 * Must be called _AFTER_ setting up the per_cpu areas
2203 */
2204void __init boot_cpu_state_init(void)
2205{
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +02002206 this_cpu_write(cpuhp_state.booted_once, true);
2207 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002208}