blob: 3c7f3b4c453cf57c8e37dd5fadc9f5941f074f0d [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
Thomas Gleixner757c9892016-02-26 18:43:32 +0000105 * @cant_stop: Bringup/teardown can't be stopped at this step
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000106 */
107struct cpuhp_step {
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200108 const char *name;
109 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200110 int (*single)(unsigned int cpu);
111 int (*multi)(unsigned int cpu,
112 struct hlist_node *node);
113 } startup;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200114 union {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200115 int (*single)(unsigned int cpu);
116 int (*multi)(unsigned int cpu,
117 struct hlist_node *node);
118 } teardown;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200119 struct hlist_head list;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200120 bool cant_stop;
121 bool multi_instance;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000122};
123
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +0000124static DEFINE_MUTEX(cpuhp_state_mutex);
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +0800125static struct cpuhp_step cpuhp_hp_states[];
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000126
Thomas Gleixnera7246322016-08-12 19:49:38 +0200127static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
128{
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +0800129 return cpuhp_hp_states + state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200130}
131
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000132/**
133 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
134 * @cpu: The cpu for which the callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200135 * @state: The state to do callbacks for
Thomas Gleixnera7246322016-08-12 19:49:38 +0200136 * @bringup: True if the bringup callback should be invoked
Peter Zijlstra96abb962017-09-20 19:00:16 +0200137 * @node: For multi-instance, do a single entry callback for install/remove
138 * @lastp: For multi-instance rollback, remember how far we got
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000139 *
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200140 * Called from cpu hotplug and from the state register machinery.
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000141 */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200142static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
Peter Zijlstra96abb962017-09-20 19:00:16 +0200143 bool bringup, struct hlist_node *node,
144 struct hlist_node **lastp)
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000145{
146 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200147 struct cpuhp_step *step = cpuhp_get_step(state);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200148 int (*cbm)(unsigned int cpu, struct hlist_node *node);
149 int (*cb)(unsigned int cpu);
150 int ret, cnt;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000151
Peter Zijlstra1db49482017-09-20 19:00:21 +0200152 if (st->fail == state) {
153 st->fail = CPUHP_INVALID;
154
155 if (!(bringup ? step->startup.single : step->teardown.single))
156 return 0;
157
158 return -EAGAIN;
159 }
160
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200161 if (!step->multi_instance) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200162 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200163 cb = bringup ? step->startup.single : step->teardown.single;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200164 if (!cb)
165 return 0;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200166 trace_cpuhp_enter(cpu, st->target, state, cb);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000167 ret = cb(cpu);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200168 trace_cpuhp_exit(cpu, st->state, state, ret);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200169 return ret;
170 }
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200171 cbm = bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200172 if (!cbm)
173 return 0;
174
175 /* Single invocation for instance add/remove */
176 if (node) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200177 WARN_ON_ONCE(lastp && *lastp);
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200178 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
179 ret = cbm(cpu, node);
180 trace_cpuhp_exit(cpu, st->state, state, ret);
181 return ret;
182 }
183
184 /* State transition. Invoke on all instances */
185 cnt = 0;
186 hlist_for_each(node, &step->list) {
Peter Zijlstra96abb962017-09-20 19:00:16 +0200187 if (lastp && node == *lastp)
188 break;
189
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200190 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
191 ret = cbm(cpu, node);
192 trace_cpuhp_exit(cpu, st->state, state, ret);
Peter Zijlstra96abb962017-09-20 19:00:16 +0200193 if (ret) {
194 if (!lastp)
195 goto err;
196
197 *lastp = node;
198 return ret;
199 }
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200200 cnt++;
201 }
Peter Zijlstra96abb962017-09-20 19:00:16 +0200202 if (lastp)
203 *lastp = NULL;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200204 return 0;
205err:
206 /* Rollback the instances if one failed */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +0200207 cbm = !bringup ? step->startup.multi : step->teardown.multi;
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200208 if (!cbm)
209 return ret;
210
211 hlist_for_each(node, &step->list) {
212 if (!cnt--)
213 break;
Peter Zijlstra724a8682017-09-20 19:00:18 +0200214
215 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
216 ret = cbm(cpu, node);
217 trace_cpuhp_exit(cpu, st->state, state, ret);
218 /*
219 * Rollback must not fail,
220 */
221 WARN_ON_ONCE(ret);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000222 }
223 return ret;
224}
225
Rusty Russell98a79d62008-12-13 21:19:41 +1030226#ifdef CONFIG_SMP
Arnd Bergmannfcb30292018-03-15 16:38:04 +0100227static bool cpuhp_is_ap_state(enum cpuhp_state state)
228{
229 /*
230 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
231 * purposes as that state is handled explicitly in cpu_down.
232 */
233 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
234}
235
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200236static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
237{
238 struct completion *done = bringup ? &st->done_up : &st->done_down;
239 wait_for_completion(done);
240}
241
242static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
243{
244 struct completion *done = bringup ? &st->done_up : &st->done_down;
245 complete(done);
246}
247
248/*
249 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
250 */
251static bool cpuhp_is_atomic_state(enum cpuhp_state state)
252{
253 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
254}
255
Rusty Russellb3199c02008-12-30 09:05:14 +1030256/* Serializes the updates to cpu_online_mask, cpu_present_mask */
Linus Torvaldsaa953872006-07-23 12:12:16 -0700257static DEFINE_MUTEX(cpu_add_remove_lock);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000258bool cpuhp_tasks_frozen;
259EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700261/*
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530262 * The following two APIs (cpu_maps_update_begin/done) must be used when
263 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700264 */
265void cpu_maps_update_begin(void)
266{
267 mutex_lock(&cpu_add_remove_lock);
268}
269
270void cpu_maps_update_done(void)
271{
272 mutex_unlock(&cpu_add_remove_lock);
273}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200275/*
276 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700277 * Should always be manipulated under cpu_add_remove_lock
278 */
279static int cpu_hotplug_disabled;
280
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700281#ifdef CONFIG_HOTPLUG_CPU
282
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200283DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530284
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200285void cpus_read_lock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800286{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200287 percpu_down_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800288}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200289EXPORT_SYMBOL_GPL(cpus_read_lock);
Ashok Raj90d45d12005-11-08 21:34:24 -0800290
Waiman Long6f4ceee2018-07-24 14:26:04 -0400291int cpus_read_trylock(void)
292{
293 return percpu_down_read_trylock(&cpu_hotplug_lock);
294}
295EXPORT_SYMBOL_GPL(cpus_read_trylock);
296
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200297void cpus_read_unlock(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800298{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200299 percpu_up_read(&cpu_hotplug_lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800300}
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200301EXPORT_SYMBOL_GPL(cpus_read_unlock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800302
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200303void cpus_write_lock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100304{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200305 percpu_down_write(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100306}
307
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200308void cpus_write_unlock(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100309{
Thomas Gleixnerfc8dffd2017-05-24 10:15:40 +0200310 percpu_up_write(&cpu_hotplug_lock);
311}
312
313void lockdep_assert_cpus_held(void)
314{
315 percpu_rwsem_assert_held(&cpu_hotplug_lock);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100316}
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700317
Peter Zijlstracb921732018-09-11 11:51:27 +0200318static void lockdep_acquire_cpus_lock(void)
319{
320 rwsem_acquire(&cpu_hotplug_lock.rw_sem.dep_map, 0, 0, _THIS_IP_);
321}
322
323static void lockdep_release_cpus_lock(void)
324{
325 rwsem_release(&cpu_hotplug_lock.rw_sem.dep_map, 1, _THIS_IP_);
326}
327
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700328/*
329 * Wait for currently running CPU hotplug operations to complete (if any) and
330 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
331 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
332 * hotplug path before performing hotplug operations. So acquiring that lock
333 * guarantees mutual exclusion from any currently running hotplug operations.
334 */
335void cpu_hotplug_disable(void)
336{
337 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700338 cpu_hotplug_disabled++;
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700339 cpu_maps_update_done();
340}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700341EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700342
Lianwei Wang01b41152016-06-09 23:43:28 -0700343static void __cpu_hotplug_enable(void)
344{
345 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
346 return;
347 cpu_hotplug_disabled--;
348}
349
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700350void cpu_hotplug_enable(void)
351{
352 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -0700353 __cpu_hotplug_enable();
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700354 cpu_maps_update_done();
355}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700356EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
Peter Zijlstracb921732018-09-11 11:51:27 +0200357
358#else
359
360static void lockdep_acquire_cpus_lock(void)
361{
362}
363
364static void lockdep_release_cpus_lock(void)
365{
366}
367
Toshi Kanib9d10be2013-08-12 09:45:53 -0600368#endif /* CONFIG_HOTPLUG_CPU */
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700369
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200370#ifdef CONFIG_HOTPLUG_SMT
371enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -0400372EXPORT_SYMBOL_GPL(cpu_smt_control);
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200373
Thomas Gleixnerbc2d8d262018-08-07 08:19:57 +0200374static bool cpu_smt_available __read_mostly;
375
Jiri Kosina8e1b7062018-07-13 16:23:23 +0200376void __init cpu_smt_disable(bool force)
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200377{
Jiri Kosina8e1b7062018-07-13 16:23:23 +0200378 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
379 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
380 return;
381
382 if (force) {
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200383 pr_info("SMT: Force disabled\n");
384 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
Jiri Kosina8e1b7062018-07-13 16:23:23 +0200385 } else {
Borislav Petkovd0e7d142018-10-04 19:22:27 +0200386 pr_info("SMT: disabled\n");
Jiri Kosina8e1b7062018-07-13 16:23:23 +0200387 cpu_smt_control = CPU_SMT_DISABLED;
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200388 }
Jiri Kosina8e1b7062018-07-13 16:23:23 +0200389}
390
Thomas Gleixnerfee0aed2018-07-13 16:23:24 +0200391/*
392 * The decision whether SMT is supported can only be done after the full
Thomas Gleixnerbc2d8d262018-08-07 08:19:57 +0200393 * CPU identification. Called from architecture code before non boot CPUs
394 * are brought up.
395 */
396void __init cpu_smt_check_topology_early(void)
397{
398 if (!topology_smt_supported())
399 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
400}
401
402/*
403 * If SMT was disabled by BIOS, detect it here, after the CPUs have been
404 * brought online. This ensures the smt/l1tf sysfs entries are consistent
405 * with reality. cpu_smt_available is set to true during the bringup of non
406 * boot CPUs when a SMT sibling is detected. Note, this may overwrite
407 * cpu_smt_control's previous setting.
Thomas Gleixnerfee0aed2018-07-13 16:23:24 +0200408 */
409void __init cpu_smt_check_topology(void)
410{
Thomas Gleixnerbc2d8d262018-08-07 08:19:57 +0200411 if (!cpu_smt_available)
Thomas Gleixnerfee0aed2018-07-13 16:23:24 +0200412 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
413}
414
Jiri Kosina8e1b7062018-07-13 16:23:23 +0200415static int __init smt_cmdline_disable(char *str)
416{
417 cpu_smt_disable(str && !strcmp(str, "force"));
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200418 return 0;
419}
420early_param("nosmt", smt_cmdline_disable);
421
422static inline bool cpu_smt_allowed(unsigned int cpu)
423{
Thomas Gleixnerbc2d8d262018-08-07 08:19:57 +0200424 if (topology_is_primary_thread(cpu))
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200425 return true;
426
Thomas Gleixnerbc2d8d262018-08-07 08:19:57 +0200427 /*
428 * If the CPU is not a 'primary' thread and the booted_once bit is
429 * set then the processor has SMT support. Store this information
430 * for the late check of SMT support in cpu_smt_check_topology().
431 */
432 if (per_cpu(cpuhp_state, cpu).booted_once)
433 cpu_smt_available = true;
434
435 if (cpu_smt_control == CPU_SMT_ENABLED)
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200436 return true;
437
438 /*
439 * On x86 it's required to boot all logical CPUs at least once so
440 * that the init code can get a chance to set CR4.MCE on each
441 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
442 * core will shutdown the machine.
443 */
444 return !per_cpu(cpuhp_state, cpu).booted_once;
445}
446#else
447static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
448#endif
449
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200450static inline enum cpuhp_state
451cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
452{
453 enum cpuhp_state prev_state = st->state;
454
455 st->rollback = false;
456 st->last = NULL;
457
458 st->target = target;
459 st->single = false;
460 st->bringup = st->state < target;
461
462 return prev_state;
463}
464
465static inline void
466cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
467{
468 st->rollback = true;
469
470 /*
471 * If we have st->last we need to undo partial multi_instance of this
472 * state first. Otherwise start undo at the previous state.
473 */
474 if (!st->last) {
475 if (st->bringup)
476 st->state--;
477 else
478 st->state++;
479 }
480
481 st->target = prev_state;
482 st->bringup = !st->bringup;
483}
484
485/* Regular hotplug invocation of the AP hotplug thread */
486static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
487{
488 if (!st->single && st->state == st->target)
489 return;
490
491 st->result = 0;
492 /*
493 * Make sure the above stores are visible before should_run becomes
494 * true. Paired with the mb() above in cpuhp_thread_fun()
495 */
496 smp_mb();
497 st->should_run = true;
498 wake_up_process(st->thread);
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200499 wait_for_ap_thread(st, st->bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200500}
501
502static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
503{
504 enum cpuhp_state prev_state;
505 int ret;
506
507 prev_state = cpuhp_set_state(st, target);
508 __cpuhp_kick_ap(st);
509 if ((ret = st->result)) {
510 cpuhp_reset_state(st, prev_state);
511 __cpuhp_kick_ap(st);
512 }
513
514 return ret;
515}
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200516
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000517static int bringup_wait_for_ap(unsigned int cpu)
518{
519 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
520
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200521 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200522 wait_for_ap_thread(st, true);
Thomas Gleixnerdea1d0f2017-07-11 22:06:24 +0200523 if (WARN_ON_ONCE((!cpu_online(cpu))))
524 return -ECANCELED;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200525
526 /* Unpark the stopper thread and the hotplug thread of the target cpu */
527 stop_machine_unpark(cpu);
528 kthread_unpark(st->thread);
529
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +0200530 /*
531 * SMT soft disabling on X86 requires to bring the CPU out of the
532 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
533 * CPU marked itself as booted_once in cpu_notify_starting() so the
534 * cpu_smt_allowed() check will now return false if this is not the
535 * primary sibling.
536 */
537 if (!cpu_smt_allowed(cpu))
538 return -ECANCELED;
539
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200540 if (st->target <= CPUHP_AP_ONLINE_IDLE)
541 return 0;
542
543 return cpuhp_kick_ap(st, st->target);
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000544}
545
Thomas Gleixnerba997462016-02-26 18:43:24 +0000546static int bringup_cpu(unsigned int cpu)
547{
548 struct task_struct *idle = idle_thread_get(cpu);
549 int ret;
550
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400551 /*
552 * Some architectures have to walk the irq descriptors to
553 * setup the vector space for the cpu which comes online.
554 * Prevent irq alloc/free across the bringup.
555 */
556 irq_lock_sparse();
557
Thomas Gleixnerba997462016-02-26 18:43:24 +0000558 /* Arch-specific enabling code. */
559 ret = __cpu_up(cpu, idle);
Boris Ostrovskyaa877172016-08-03 13:22:28 -0400560 irq_unlock_sparse();
Thomas Gleixner530e9b72016-12-21 20:19:53 +0100561 if (ret)
Thomas Gleixnerba997462016-02-26 18:43:24 +0000562 return ret;
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +0200563 return bringup_wait_for_ap(cpu);
Thomas Gleixnerba997462016-02-26 18:43:24 +0000564}
565
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000566/*
567 * Hotplug state machine related functions
568 */
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000569
Thomas Gleixnera7246322016-08-12 19:49:38 +0200570static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000571{
Mukesh Ojha6fb86d92018-08-28 12:24:54 +0530572 for (st->state--; st->state > st->target; st->state--)
573 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000574}
575
576static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
Thomas Gleixnera7246322016-08-12 19:49:38 +0200577 enum cpuhp_state target)
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000578{
579 enum cpuhp_state prev_state = st->state;
580 int ret = 0;
581
582 while (st->state < target) {
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000583 st->state++;
Peter Zijlstra96abb962017-09-20 19:00:16 +0200584 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000585 if (ret) {
586 st->target = prev_state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200587 undo_cpu_up(cpu, st);
Thomas Gleixner2e1a3482016-02-26 18:43:37 +0000588 break;
589 }
590 }
591 return ret;
592}
593
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000594/*
595 * The cpu hotplug threads manage the bringup and teardown of the cpus
596 */
597static void cpuhp_create(unsigned int cpu)
598{
599 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
600
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200601 init_completion(&st->done_up);
602 init_completion(&st->done_down);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000603}
604
605static int cpuhp_should_run(unsigned int cpu)
606{
607 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
608
609 return st->should_run;
610}
611
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000612/*
613 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
614 * callbacks when a state gets [un]installed at runtime.
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200615 *
616 * Each invocation of this function by the smpboot thread does a single AP
617 * state callback.
618 *
619 * It has 3 modes of operation:
620 * - single: runs st->cb_state
621 * - up: runs ++st->state, while st->state < st->target
622 * - down: runs st->state--, while st->state > st->target
623 *
624 * When complete or on error, should_run is cleared and the completion is fired.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000625 */
626static void cpuhp_thread_fun(unsigned int cpu)
627{
628 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200629 bool bringup = st->bringup;
630 enum cpuhp_state state;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000631
Neeraj Upadhyayf8b75302018-09-05 11:22:07 +0530632 if (WARN_ON_ONCE(!st->should_run))
633 return;
634
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000635 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200636 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
637 * that if we see ->should_run we also see the rest of the state.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000638 */
639 smp_mb();
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200640
Peter Zijlstracb921732018-09-11 11:51:27 +0200641 /*
642 * The BP holds the hotplug lock, but we're now running on the AP,
643 * ensure that anybody asserting the lock is held, will actually find
644 * it so.
645 */
646 lockdep_acquire_cpus_lock();
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200647 cpuhp_lock_acquire(bringup);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200648
Thomas Gleixnera7246322016-08-12 19:49:38 +0200649 if (st->single) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200650 state = st->cb_state;
651 st->should_run = false;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000652 } else {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200653 if (bringup) {
654 st->state++;
655 state = st->state;
656 st->should_run = (st->state < st->target);
657 WARN_ON_ONCE(st->state > st->target);
658 } else {
659 state = st->state;
660 st->state--;
661 st->should_run = (st->state > st->target);
662 WARN_ON_ONCE(st->state < st->target);
663 }
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000664 }
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200665
666 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
667
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200668 if (cpuhp_is_atomic_state(state)) {
669 local_irq_disable();
670 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
671 local_irq_enable();
672
673 /*
674 * STARTING/DYING must not fail!
675 */
676 WARN_ON_ONCE(st->result);
677 } else {
678 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
679 }
680
681 if (st->result) {
682 /*
683 * If we fail on a rollback, we're up a creek without no
684 * paddle, no way forward, no way back. We loose, thanks for
685 * playing.
686 */
687 WARN_ON_ONCE(st->rollback);
688 st->should_run = false;
689 }
690
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200691 cpuhp_lock_release(bringup);
Peter Zijlstracb921732018-09-11 11:51:27 +0200692 lockdep_release_cpus_lock();
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200693
694 if (!st->should_run)
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200695 complete_ap_thread(st, bringup);
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000696}
697
698/* Invoke a single callback on a remote cpu */
Thomas Gleixnera7246322016-08-12 19:49:38 +0200699static int
Thomas Gleixnercf392d12016-08-12 19:49:39 +0200700cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
701 struct hlist_node *node)
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000702{
703 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200704 int ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000705
706 if (!cpu_online(cpu))
707 return 0;
708
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200709 cpuhp_lock_acquire(false);
710 cpuhp_lock_release(false);
711
712 cpuhp_lock_acquire(true);
713 cpuhp_lock_release(true);
Thomas Gleixner49dfe2a2017-05-24 10:15:43 +0200714
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000715 /*
716 * If we are up and running, use the hotplug thread. For early calls
717 * we invoke the thread function directly.
718 */
719 if (!st->thread)
Peter Zijlstra96abb962017-09-20 19:00:16 +0200720 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner6a4e2452016-07-13 17:16:03 +0000721
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200722 st->rollback = false;
723 st->last = NULL;
724
725 st->node = node;
726 st->bringup = bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000727 st->cb_state = state;
Thomas Gleixnera7246322016-08-12 19:49:38 +0200728 st->single = true;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200729
730 __cpuhp_kick_ap(st);
Thomas Gleixnera7246322016-08-12 19:49:38 +0200731
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000732 /*
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200733 * If we failed and did a partial, do a rollback.
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000734 */
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200735 if ((ret = st->result) && st->last) {
736 st->rollback = true;
737 st->bringup = !bringup;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000738
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200739 __cpuhp_kick_ap(st);
740 }
741
Thomas Gleixner1f7c70d2017-10-21 16:06:52 +0200742 /*
743 * Clean up the leftovers so the next hotplug operation wont use stale
744 * data.
745 */
746 st->node = st->last = NULL;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200747 return ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000748}
749
750static int cpuhp_kick_ap_work(unsigned int cpu)
751{
752 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200753 enum cpuhp_state prev_state = st->state;
754 int ret;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000755
Peter Zijlstra5f4b55e2017-09-20 19:00:20 +0200756 cpuhp_lock_acquire(false);
757 cpuhp_lock_release(false);
758
759 cpuhp_lock_acquire(true);
760 cpuhp_lock_release(true);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200761
762 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
763 ret = cpuhp_kick_ap(st, st->target);
764 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
765
766 return ret;
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000767}
768
769static struct smp_hotplug_thread cpuhp_threads = {
770 .store = &cpuhp_state.thread,
771 .create = &cpuhp_create,
772 .thread_should_run = cpuhp_should_run,
773 .thread_fn = cpuhp_thread_fun,
774 .thread_comm = "cpuhp/%u",
775 .selfparking = true,
776};
777
778void __init cpuhp_threads_init(void)
779{
780 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
781 kthread_unpark(this_cpu_read(cpuhp_state.thread));
782}
783
Michal Hocko777c6e02016-12-07 14:54:38 +0100784#ifdef CONFIG_HOTPLUG_CPU
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700785/**
786 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
787 * @cpu: a CPU id
788 *
789 * This function walks all processes, finds a valid mm struct for each one and
790 * then clears a corresponding bit in mm's cpumask. While this all sounds
791 * trivial, there are various non-obvious corner cases, which this function
792 * tries to solve in a safe manner.
793 *
794 * Also note that the function uses a somewhat relaxed locking scheme, so it may
795 * be called only for an already offlined CPU.
796 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700797void clear_tasks_mm_cpumask(int cpu)
798{
799 struct task_struct *p;
800
801 /*
802 * This function is called after the cpu is taken down and marked
803 * offline, so its not like new tasks will ever get this cpu set in
804 * their mm mask. -- Peter Zijlstra
805 * Thus, we may use rcu_read_lock() here, instead of grabbing
806 * full-fledged tasklist_lock.
807 */
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700808 WARN_ON(cpu_online(cpu));
Anton Vorontsovcb792952012-05-31 16:26:22 -0700809 rcu_read_lock();
810 for_each_process(p) {
811 struct task_struct *t;
812
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700813 /*
814 * Main thread might exit, but other threads may still have
815 * a valid mm. Find one.
816 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700817 t = find_lock_task_mm(p);
818 if (!t)
819 continue;
820 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
821 task_unlock(t);
822 }
823 rcu_read_unlock();
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/* Take this CPU down. */
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200827static int take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000829 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
830 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000831 int err, cpu = smp_processor_id();
Peter Zijlstra724a8682017-09-20 19:00:18 +0200832 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* Ensure this CPU doesn't handle any more interrupts. */
835 err = __cpu_disable();
836 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Thomas Gleixnera7246322016-08-12 19:49:38 +0200839 /*
840 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
841 * do this step again.
842 */
843 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
844 st->state--;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000845 /* Invoke the former CPU_DYING callbacks */
Peter Zijlstra724a8682017-09-20 19:00:18 +0200846 for (; st->state > target; st->state--) {
847 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
848 /*
849 * DYING must not fail!
850 */
851 WARN_ON_ONCE(ret);
852 }
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000853
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200854 /* Give up timekeeping duties */
855 tick_handover_do_timer();
Thomas Gleixner14e568e2013-01-31 12:11:14 +0000856 /* Park the stopper thread */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000857 stop_machine_park(cpu);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Thomas Gleixner98458172016-02-26 18:43:25 +0000861static int takedown_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000863 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000864 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Thomas Gleixner2a58c522016-03-10 20:42:08 +0100866 /* Park the smpboot threads */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000867 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
868
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200869 /*
Thomas Gleixnera8994182015-07-05 17:12:30 +0000870 * Prevent irq alloc/free while the dying cpu reorganizes the
871 * interrupt affinities.
872 */
873 irq_lock_sparse();
874
875 /*
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200876 * So now all preempt/rcu users must observe !cpu_active().
877 */
Sebastian Andrzej Siewior210e2132017-05-24 10:15:28 +0200878 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
Rusty Russell04321582008-07-28 12:16:29 -0500879 if (err) {
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200880 /* CPU refused to die */
Thomas Gleixnera8994182015-07-05 17:12:30 +0000881 irq_unlock_sparse();
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +0200882 /* Unpark the hotplug thread so we can rollback there */
883 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
Thomas Gleixner98458172016-02-26 18:43:25 +0000884 return err;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700885 }
Rusty Russell04321582008-07-28 12:16:29 -0500886 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100888 /*
Brendan Jackman5b1ead62017-12-06 10:59:11 +0000889 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
890 * all runnable tasks from the CPU, there's only the idle task left now
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100891 * that the migration thread is done doing the stop_machine thing.
Peter Zijlstra51a96c72010-11-19 20:37:53 +0100892 *
893 * Wait for the stop thread to go away.
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100894 */
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200895 wait_for_ap_thread(st, false);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000896 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Thomas Gleixnera8994182015-07-05 17:12:30 +0000898 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
899 irq_unlock_sparse();
900
Preeti U Murthy345527b2015-03-30 14:59:19 +0530901 hotplug_cpu__broadcast_tick_pull(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* This actually kills the CPU. */
903 __cpu_die(cpu);
904
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200905 tick_cleanup_dead_cpu(cpu);
Paul E. McKenneya58163d2017-06-20 12:11:34 -0700906 rcutree_migrate_callbacks(cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000907 return 0;
908}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100910static void cpuhp_complete_idle_dead(void *arg)
911{
912 struct cpuhp_cpu_state *st = arg;
913
Peter Zijlstra5ebe7742017-09-20 19:00:19 +0200914 complete_ap_thread(st, false);
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100915}
916
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000917void cpuhp_report_idle_dead(void)
918{
919 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
920
921 BUG_ON(st->state != CPUHP_AP_OFFLINE);
Thomas Gleixner27d50c72016-02-26 18:43:44 +0000922 rcu_report_dead(smp_processor_id());
Thomas Gleixner71f87b22016-03-03 10:52:10 +0100923 st->state = CPUHP_AP_IDLE_DEAD;
924 /*
925 * We cannot call complete after rcu_report_dead() so we delegate it
926 * to an online cpu.
927 */
928 smp_call_function_single(cpumask_first(cpu_online_mask),
929 cpuhp_complete_idle_dead, st, 0);
Thomas Gleixnere69aab12016-02-26 18:43:43 +0000930}
931
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200932static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
933{
Mukesh Ojha6fb86d92018-08-28 12:24:54 +0530934 for (st->state++; st->state < st->target; st->state++)
935 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200936}
937
938static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
939 enum cpuhp_state target)
940{
941 enum cpuhp_state prev_state = st->state;
942 int ret = 0;
943
944 for (; st->state > target; st->state--) {
945 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
946 if (ret) {
947 st->target = prev_state;
Thomas Gleixner69fa6eb2018-09-06 15:21:38 +0200948 if (st->state < prev_state)
949 undo_cpu_down(cpu, st);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200950 break;
951 }
952 }
953 return ret;
954}
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000955
Thomas Gleixner98458172016-02-26 18:43:25 +0000956/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000957static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
958 enum cpuhp_state target)
Thomas Gleixner98458172016-02-26 18:43:25 +0000959{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000960 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
961 int prev_state, ret = 0;
Thomas Gleixner98458172016-02-26 18:43:25 +0000962
963 if (num_online_cpus() == 1)
964 return -EBUSY;
965
Thomas Gleixner757c9892016-02-26 18:43:32 +0000966 if (!cpu_present(cpu))
Thomas Gleixner98458172016-02-26 18:43:25 +0000967 return -EINVAL;
968
Thomas Gleixner8f553c42017-05-24 10:15:12 +0200969 cpus_write_lock();
Thomas Gleixner98458172016-02-26 18:43:25 +0000970
971 cpuhp_tasks_frozen = tasks_frozen;
972
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200973 prev_state = cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000974 /*
975 * If the current CPU state is in the range of the AP hotplug thread,
976 * then we need to kick the thread.
977 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000978 if (st->state > CPUHP_TEARDOWN_CPU) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200979 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000980 ret = cpuhp_kick_ap_work(cpu);
981 /*
982 * The AP side has done the error rollback already. Just
983 * return the error code..
984 */
985 if (ret)
986 goto out;
987
988 /*
989 * We might have stopped still in the range of the AP hotplug
990 * thread. Nothing to do anymore.
991 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000992 if (st->state > CPUHP_TEARDOWN_CPU)
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000993 goto out;
Peter Zijlstra4dddfb52017-09-20 19:00:17 +0200994
995 st->target = target;
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000996 }
997 /*
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000998 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
Thomas Gleixner1cf4f622016-02-26 18:43:39 +0000999 * to do the further cleanups.
1000 */
Thomas Gleixnera7246322016-08-12 19:49:38 +02001001 ret = cpuhp_down_callbacks(cpu, st, target);
Thomas Gleixner69fa6eb2018-09-06 15:21:38 +02001002 if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001003 cpuhp_reset_state(st, prev_state);
1004 __cpuhp_kick_ap(st);
Sebastian Andrzej Siewior3b9d6da2016-04-08 14:40:15 +02001005 }
Thomas Gleixner98458172016-02-26 18:43:25 +00001006
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001007out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001008 cpus_write_unlock();
Thomas Gleixner941154b2017-09-12 21:37:04 +02001009 /*
1010 * Do post unplug cleanup. This is still protected against
1011 * concurrent CPU hotplug via cpu_add_remove_lock.
1012 */
1013 lockup_detector_cleanup();
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001014 return ret;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001015}
1016
Thomas Gleixnercc1fe212018-05-29 17:49:05 +02001017static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1018{
1019 if (cpu_hotplug_disabled)
1020 return -EBUSY;
1021 return _cpu_down(cpu, 0, target);
1022}
1023
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001024static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001025{
Heiko Carstens9ea09af2008-12-22 12:36:30 +01001026 int err;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001027
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001028 cpu_maps_update_begin();
Thomas Gleixnercc1fe212018-05-29 17:49:05 +02001029 err = cpu_down_maps_locked(cpu, target);
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001030 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return err;
1032}
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001033
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001034int cpu_down(unsigned int cpu)
1035{
1036 return do_cpu_down(cpu, CPUHP_OFFLINE);
1037}
Zhang Ruib62b8ef2008-04-29 02:35:56 -04001038EXPORT_SYMBOL(cpu_down);
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001039
1040#else
1041#define takedown_cpu NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#endif /*CONFIG_HOTPLUG_CPU*/
1043
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001044/**
Thomas Gleixneree1e7142016-08-18 14:57:16 +02001045 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001046 * @cpu: cpu that just started
1047 *
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001048 * It must be called by the arch code on the new cpu, before the new cpu
1049 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1050 */
1051void notify_cpu_starting(unsigned int cpu)
1052{
1053 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1054 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
Peter Zijlstra724a8682017-09-20 19:00:18 +02001055 int ret;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001056
Sebastian Andrzej Siewior0c6d4572016-08-17 14:21:04 +02001057 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +02001058 st->booted_once = true;
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001059 while (st->state < target) {
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001060 st->state++;
Peter Zijlstra724a8682017-09-20 19:00:18 +02001061 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1062 /*
1063 * STARTING must not fail!
1064 */
1065 WARN_ON_ONCE(ret);
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001066 }
1067}
1068
Thomas Gleixner949338e2016-02-26 18:43:35 +00001069/*
Thomas Gleixner9cd4f1a2017-07-04 22:20:23 +02001070 * Called from the idle task. Wake up the controlling task which brings the
1071 * stopper and the hotplug thread of the upcoming CPU up and then delegates
1072 * the rest of the online bringup to the hotplug thread.
Thomas Gleixner949338e2016-02-26 18:43:35 +00001073 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001074void cpuhp_online_idle(enum cpuhp_state state)
Thomas Gleixner949338e2016-02-26 18:43:35 +00001075{
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001076 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001077
1078 /* Happens for the boot cpu */
1079 if (state != CPUHP_AP_ONLINE_IDLE)
1080 return;
1081
1082 st->state = CPUHP_AP_ONLINE_IDLE;
Peter Zijlstra5ebe7742017-09-20 19:00:19 +02001083 complete_ap_thread(st, true);
Thomas Gleixner949338e2016-02-26 18:43:35 +00001084}
1085
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001086/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001087static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001089 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -07001090 struct task_struct *idle;
Thomas Gleixner2e1a3482016-02-26 18:43:37 +00001091 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001093 cpus_write_lock();
Thomas Gleixner38498a62012-04-20 13:05:44 +00001094
Thomas Gleixner757c9892016-02-26 18:43:32 +00001095 if (!cpu_present(cpu)) {
Yasuaki Ishimatsu5e5041f2012-10-23 01:30:54 +02001096 ret = -EINVAL;
1097 goto out;
1098 }
1099
Thomas Gleixner757c9892016-02-26 18:43:32 +00001100 /*
1101 * The caller of do_cpu_up might have raced with another
1102 * caller. Ignore it for now.
1103 */
1104 if (st->state >= target)
Thomas Gleixner38498a62012-04-20 13:05:44 +00001105 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +00001106
1107 if (st->state == CPUHP_OFFLINE) {
1108 /* Let it fail before we try to bring the cpu up */
1109 idle = idle_thread_get(cpu);
1110 if (IS_ERR(idle)) {
1111 ret = PTR_ERR(idle);
1112 goto out;
1113 }
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -07001114 }
Thomas Gleixner38498a62012-04-20 13:05:44 +00001115
Thomas Gleixnerba997462016-02-26 18:43:24 +00001116 cpuhp_tasks_frozen = tasks_frozen;
1117
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001118 cpuhp_set_state(st, target);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001119 /*
1120 * If the current CPU state is in the range of the AP hotplug thread,
1121 * then we need to kick the thread once more.
1122 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001123 if (st->state > CPUHP_BRINGUP_CPU) {
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001124 ret = cpuhp_kick_ap_work(cpu);
1125 /*
1126 * The AP side has done the error rollback already. Just
1127 * return the error code..
1128 */
1129 if (ret)
1130 goto out;
1131 }
1132
1133 /*
1134 * Try to reach the target state. We max out on the BP at
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001135 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001136 * responsible for bringing it up to the target state.
1137 */
Thomas Gleixner8df3e072016-02-26 18:43:41 +00001138 target = min((int)target, CPUHP_BRINGUP_CPU);
Thomas Gleixnera7246322016-08-12 19:49:38 +02001139 ret = cpuhp_up_callbacks(cpu, st, target);
Thomas Gleixner38498a62012-04-20 13:05:44 +00001140out:
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001141 cpus_write_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return ret;
1143}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001144
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001145static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001146{
1147 int err = 0;
minskey guocf234222010-05-24 14:32:41 -07001148
Rusty Russelle0b582e2009-01-01 10:12:28 +10301149 if (!cpu_possible(cpu)) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001150 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1151 cpu);
Chen Gong87d5e0232010-03-05 13:42:38 -08001152#if defined(CONFIG_IA64)
Fabian Frederick84117da2014-06-04 16:11:17 -07001153 pr_err("please check additional_cpus= boot parameter\n");
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -07001154#endif
1155 return -EINVAL;
1156 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001157
Toshi Kani01b0f192013-11-12 15:07:25 -08001158 err = try_online_node(cpu_to_node(cpu));
1159 if (err)
1160 return err;
minskey guocf234222010-05-24 14:32:41 -07001161
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001162 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001163
Max Krasnyanskye761b772008-07-15 04:43:49 -07001164 if (cpu_hotplug_disabled) {
1165 err = -EBUSY;
1166 goto out;
1167 }
Thomas Gleixner05736e42018-05-29 17:48:27 +02001168 if (!cpu_smt_allowed(cpu)) {
1169 err = -EPERM;
1170 goto out;
1171 }
Max Krasnyanskye761b772008-07-15 04:43:49 -07001172
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001173 err = _cpu_up(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -07001174out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001175 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001176 return err;
1177}
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001178
1179int cpu_up(unsigned int cpu)
1180{
1181 return do_cpu_up(cpu, CPUHP_ONLINE);
1182}
Paul E. McKenneya513f6b2011-12-11 21:54:45 -08001183EXPORT_SYMBOL_GPL(cpu_up);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001184
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001185#ifdef CONFIG_PM_SLEEP_SMP
Rusty Russelle0b582e2009-01-01 10:12:28 +10301186static cpumask_var_t frozen_cpus;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001187
James Morsed391e552016-08-17 13:50:25 +01001188int freeze_secondary_cpus(int primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001189{
James Morsed391e552016-08-17 13:50:25 +01001190 int cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001191
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001192 cpu_maps_update_begin();
James Morsed391e552016-08-17 13:50:25 +01001193 if (!cpu_online(primary))
1194 primary = cpumask_first(cpu_online_mask);
Xiaotian Feng9ee349a2009-12-16 18:04:32 +01001195 /*
1196 * We take down all of the non-boot CPUs in one shot to avoid races
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001197 * with the userspace trying to use the CPU hotplug at the same time
1198 */
Rusty Russelle0b582e2009-01-01 10:12:28 +10301199 cpumask_clear(frozen_cpus);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01001200
Fabian Frederick84117da2014-06-04 16:11:17 -07001201 pr_info("Disabling non-boot CPUs ...\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001202 for_each_online_cpu(cpu) {
James Morsed391e552016-08-17 13:50:25 +01001203 if (cpu == primary)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001204 continue;
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001205 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001206 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001207 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
Mike Travisfeae3202009-11-17 18:22:13 -06001208 if (!error)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301209 cpumask_set_cpu(cpu, frozen_cpus);
Mike Travisfeae3202009-11-17 18:22:13 -06001210 else {
Fabian Frederick84117da2014-06-04 16:11:17 -07001211 pr_err("Error taking CPU%d down: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001212 break;
1213 }
1214 }
Joseph Cihula86886e52009-06-30 19:31:07 -07001215
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001216 if (!error)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001217 BUG_ON(num_online_cpus() > 1);
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001218 else
Fabian Frederick84117da2014-06-04 16:11:17 -07001219 pr_err("Non-boot CPUs are not disabled\n");
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -07001220
1221 /*
1222 * Make sure the CPUs won't be enabled by someone else. We need to do
1223 * this even in case of failure as all disable_nonboot_cpus() users are
1224 * supposed to do enable_nonboot_cpus() on the failure path.
1225 */
1226 cpu_hotplug_disabled++;
1227
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001228 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001229 return error;
1230}
1231
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001232void __weak arch_enable_nonboot_cpus_begin(void)
1233{
1234}
1235
1236void __weak arch_enable_nonboot_cpus_end(void)
1237{
1238}
1239
Mathias Krause71cf5ae2015-07-19 20:06:22 +02001240void enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001241{
1242 int cpu, error;
1243
1244 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001245 cpu_maps_update_begin();
Lianwei Wang01b41152016-06-09 23:43:28 -07001246 __cpu_hotplug_enable();
Rusty Russelle0b582e2009-01-01 10:12:28 +10301247 if (cpumask_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001248 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001249
Fabian Frederick84117da2014-06-04 16:11:17 -07001250 pr_info("Enabling non-boot CPUs ...\n");
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001251
1252 arch_enable_nonboot_cpus_begin();
1253
Rusty Russelle0b582e2009-01-01 10:12:28 +10301254 for_each_cpu(cpu, frozen_cpus) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001255 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +00001256 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001257 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001258 if (!error) {
Fabian Frederick84117da2014-06-04 16:11:17 -07001259 pr_info("CPU%d is up\n", cpu);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001260 continue;
1261 }
Fabian Frederick84117da2014-06-04 16:11:17 -07001262 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001263 }
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001264
1265 arch_enable_nonboot_cpus_end();
1266
Rusty Russelle0b582e2009-01-01 10:12:28 +10301267 cpumask_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -07001268out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +01001269 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -07001270}
Rusty Russelle0b582e2009-01-01 10:12:28 +10301271
Fenghua Yud7268a32011-11-15 21:59:31 +01001272static int __init alloc_frozen_cpus(void)
Rusty Russelle0b582e2009-01-01 10:12:28 +10301273{
1274 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1275 return -ENOMEM;
1276 return 0;
1277}
1278core_initcall(alloc_frozen_cpus);
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001279
1280/*
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001281 * When callbacks for CPU hotplug notifications are being executed, we must
1282 * ensure that the state of the system with respect to the tasks being frozen
1283 * or not, as reported by the notification, remains unchanged *throughout the
1284 * duration* of the execution of the callbacks.
1285 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1286 *
1287 * This synchronization is implemented by mutually excluding regular CPU
1288 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1289 * Hibernate notifications.
1290 */
1291static int
1292cpu_hotplug_pm_callback(struct notifier_block *nb,
1293 unsigned long action, void *ptr)
1294{
1295 switch (action) {
1296
1297 case PM_SUSPEND_PREPARE:
1298 case PM_HIBERNATION_PREPARE:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001299 cpu_hotplug_disable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001300 break;
1301
1302 case PM_POST_SUSPEND:
1303 case PM_POST_HIBERNATION:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -07001304 cpu_hotplug_enable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001305 break;
1306
1307 default:
1308 return NOTIFY_DONE;
1309 }
1310
1311 return NOTIFY_OK;
1312}
1313
1314
Fenghua Yud7268a32011-11-15 21:59:31 +01001315static int __init cpu_hotplug_pm_sync_init(void)
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001316{
Fenghua Yu6e32d472012-11-13 11:32:43 -08001317 /*
1318 * cpu_hotplug_pm_callback has higher priority than x86
1319 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1320 * to disable cpu hotplug to avoid cpu hotplug race.
1321 */
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +01001322 pm_notifier(cpu_hotplug_pm_callback, 0);
1323 return 0;
1324}
1325core_initcall(cpu_hotplug_pm_sync_init);
1326
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -07001327#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001328
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01001329int __boot_cpu_id;
1330
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -07001331#endif /* CONFIG_SMP */
Mike Travisb8d317d2008-07-24 18:21:29 -07001332
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001333/* Boot processor state steps */
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001334static struct cpuhp_step cpuhp_hp_states[] = {
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001335 [CPUHP_OFFLINE] = {
1336 .name = "offline",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001337 .startup.single = NULL,
1338 .teardown.single = NULL,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001339 },
1340#ifdef CONFIG_SMP
1341 [CPUHP_CREATE_THREADS]= {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001342 .name = "threads:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001343 .startup.single = smpboot_create_threads,
1344 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001345 .cant_stop = true,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001346 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001347 [CPUHP_PERF_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001348 .name = "perf:prepare",
1349 .startup.single = perf_event_init_cpu,
1350 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001351 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001352 [CPUHP_WORKQUEUE_PREP] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001353 .name = "workqueue:prepare",
1354 .startup.single = workqueue_prepare_cpu,
1355 .teardown.single = NULL,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001356 },
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001357 [CPUHP_HRTIMERS_PREPARE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001358 .name = "hrtimers:prepare",
1359 .startup.single = hrtimers_prepare_cpu,
1360 .teardown.single = hrtimers_dead_cpu,
Thomas Gleixner27590dc2016-07-15 10:41:04 +02001361 },
Richard Weinberger31487f82016-07-13 17:17:01 +00001362 [CPUHP_SMPCFD_PREPARE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001363 .name = "smpcfd:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001364 .startup.single = smpcfd_prepare_cpu,
1365 .teardown.single = smpcfd_dead_cpu,
Richard Weinberger31487f82016-07-13 17:17:01 +00001366 },
Richard Weinbergere6d49892016-08-18 14:57:17 +02001367 [CPUHP_RELAY_PREPARE] = {
1368 .name = "relay:prepare",
1369 .startup.single = relay_prepare_cpu,
1370 .teardown.single = NULL,
1371 },
Sebastian Andrzej Siewior6731d4f2016-08-23 14:53:19 +02001372 [CPUHP_SLAB_PREPARE] = {
1373 .name = "slab:prepare",
1374 .startup.single = slab_prepare_cpu,
1375 .teardown.single = slab_dead_cpu,
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001376 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001377 [CPUHP_RCUTREE_PREP] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001378 .name = "RCU/tree:prepare",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001379 .startup.single = rcutree_prepare_cpu,
1380 .teardown.single = rcutree_dead_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001381 },
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001382 /*
Richard Cochran4fae16d2016-07-27 11:08:18 +02001383 * On the tear-down path, timers_dead_cpu() must be invoked
1384 * before blk_mq_queue_reinit_notify() from notify_dead(),
1385 * otherwise a RCU stall occurs.
1386 */
Thomas Gleixner26456f82017-12-27 21:37:25 +01001387 [CPUHP_TIMERS_PREPARE] = {
Mukesh Ojhad0180312018-07-24 20:17:48 +05301388 .name = "timers:prepare",
Thomas Gleixner26456f82017-12-27 21:37:25 +01001389 .startup.single = timers_prepare_cpu,
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001390 .teardown.single = timers_dead_cpu,
Richard Cochran4fae16d2016-07-27 11:08:18 +02001391 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001392 /* Kicks the plugged cpu into life */
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001393 [CPUHP_BRINGUP_CPU] = {
1394 .name = "cpu:bringup",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001395 .startup.single = bringup_cpu,
1396 .teardown.single = NULL,
Thomas Gleixner757c9892016-02-26 18:43:32 +00001397 .cant_stop = true,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001398 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001399 /* Final state before CPU kills itself */
1400 [CPUHP_AP_IDLE_DEAD] = {
1401 .name = "idle:dead",
1402 },
1403 /*
1404 * Last state before CPU enters the idle loop to die. Transient state
1405 * for synchronization.
1406 */
1407 [CPUHP_AP_OFFLINE] = {
1408 .name = "ap:offline",
1409 .cant_stop = true,
1410 },
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001411 /* First state is scheduler control. Interrupts are disabled */
1412 [CPUHP_AP_SCHED_STARTING] = {
1413 .name = "sched:starting",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001414 .startup.single = sched_cpu_starting,
1415 .teardown.single = sched_cpu_dying,
Thomas Gleixner9cf72432016-03-10 12:54:09 +01001416 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001417 [CPUHP_AP_RCUTREE_DYING] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001418 .name = "RCU/tree:dying",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001419 .startup.single = NULL,
1420 .teardown.single = rcutree_dying_cpu,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001421 },
Lai Jiangshan46febd32017-11-28 21:19:53 +08001422 [CPUHP_AP_SMPCFD_DYING] = {
1423 .name = "smpcfd:dying",
1424 .startup.single = NULL,
1425 .teardown.single = smpcfd_dying_cpu,
1426 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001427 /* Entry state on starting. Interrupts enabled from here on. Transient
1428 * state for synchronsization */
1429 [CPUHP_AP_ONLINE] = {
1430 .name = "ap:online",
1431 },
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001432 /*
1433 * Handled on controll processor until the plugged processor manages
1434 * this itself.
1435 */
1436 [CPUHP_TEARDOWN_CPU] = {
1437 .name = "cpu:teardown",
1438 .startup.single = NULL,
1439 .teardown.single = takedown_cpu,
1440 .cant_stop = true,
1441 },
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001442 /* Handle smpboot threads park/unpark */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001443 [CPUHP_AP_SMPBOOT_THREADS] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001444 .name = "smpboot/threads:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001445 .startup.single = smpboot_unpark_threads,
Thomas Gleixnerc4de6562018-05-29 19:05:25 +02001446 .teardown.single = smpboot_park_threads,
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001447 },
Thomas Gleixnerc5cb83b2017-06-20 01:37:51 +02001448 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1449 .name = "irq/affinity:online",
1450 .startup.single = irq_affinity_online_cpu,
1451 .teardown.single = NULL,
1452 },
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001453 [CPUHP_AP_PERF_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001454 .name = "perf:online",
1455 .startup.single = perf_event_init_cpu,
1456 .teardown.single = perf_event_exit_cpu,
Thomas Gleixner00e16c32016-07-13 17:16:09 +00001457 },
Peter Zijlstra9cf57732018-06-07 10:52:03 +02001458 [CPUHP_AP_WATCHDOG_ONLINE] = {
1459 .name = "lockup_detector:online",
1460 .startup.single = lockup_detector_online_cpu,
1461 .teardown.single = lockup_detector_offline_cpu,
1462 },
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001463 [CPUHP_AP_WORKQUEUE_ONLINE] = {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001464 .name = "workqueue:online",
1465 .startup.single = workqueue_online_cpu,
1466 .teardown.single = workqueue_offline_cpu,
Thomas Gleixner7ee681b2016-07-13 17:16:29 +00001467 },
Thomas Gleixner4df83742016-07-13 17:17:03 +00001468 [CPUHP_AP_RCUTREE_ONLINE] = {
Thomas Gleixner677f6642016-09-06 16:13:48 +02001469 .name = "RCU/tree:online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001470 .startup.single = rcutree_online_cpu,
1471 .teardown.single = rcutree_offline_cpu,
Thomas Gleixner4df83742016-07-13 17:17:03 +00001472 },
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001473#endif
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001474 /*
1475 * The dynamically registered state space is here
1476 */
1477
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001478#ifdef CONFIG_SMP
1479 /* Last state is scheduler control setting the cpu active */
1480 [CPUHP_AP_ACTIVE] = {
1481 .name = "sched:active",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001482 .startup.single = sched_cpu_activate,
1483 .teardown.single = sched_cpu_deactivate,
Thomas Gleixneraaddd7d2016-03-10 12:54:19 +01001484 },
1485#endif
1486
Thomas Gleixnerd10ef6f2016-03-08 10:36:13 +01001487 /* CPU is fully up and running. */
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001488 [CPUHP_ONLINE] = {
1489 .name = "online",
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001490 .startup.single = NULL,
1491 .teardown.single = NULL,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +00001492 },
1493};
1494
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001495/* Sanity check for callbacks */
1496static int cpuhp_cb_check(enum cpuhp_state state)
1497{
1498 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1499 return -EINVAL;
1500 return 0;
1501}
1502
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001503/*
1504 * Returns a free for dynamic slot assignment of the Online state. The states
1505 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1506 * by having no name assigned.
1507 */
1508static int cpuhp_reserve_state(enum cpuhp_state state)
1509{
Thomas Gleixner4205e472017-01-10 14:01:05 +01001510 enum cpuhp_state i, end;
1511 struct cpuhp_step *step;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001512
Thomas Gleixner4205e472017-01-10 14:01:05 +01001513 switch (state) {
1514 case CPUHP_AP_ONLINE_DYN:
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001515 step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
Thomas Gleixner4205e472017-01-10 14:01:05 +01001516 end = CPUHP_AP_ONLINE_DYN_END;
1517 break;
1518 case CPUHP_BP_PREPARE_DYN:
Lai Jiangshan17a2f1c2017-12-01 21:50:05 +08001519 step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
Thomas Gleixner4205e472017-01-10 14:01:05 +01001520 end = CPUHP_BP_PREPARE_DYN_END;
1521 break;
1522 default:
1523 return -EINVAL;
1524 }
1525
1526 for (i = state; i <= end; i++, step++) {
1527 if (!step->name)
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001528 return i;
1529 }
1530 WARN(1, "No more dynamic states available for CPU hotplug\n");
1531 return -ENOSPC;
1532}
1533
1534static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1535 int (*startup)(unsigned int cpu),
1536 int (*teardown)(unsigned int cpu),
1537 bool multi_instance)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001538{
1539 /* (Un)Install the callbacks for further cpu hotplug operations */
1540 struct cpuhp_step *sp;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001541 int ret = 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001542
Ethan Barnes0c96b272017-07-19 22:36:00 +00001543 /*
1544 * If name is NULL, then the state gets removed.
1545 *
1546 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1547 * the first allocation from these dynamic ranges, so the removal
1548 * would trigger a new allocation and clear the wrong (already
1549 * empty) state, leaving the callbacks of the to be cleared state
1550 * dangling, which causes wreckage on the next hotplug operation.
1551 */
1552 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1553 state == CPUHP_BP_PREPARE_DYN)) {
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001554 ret = cpuhp_reserve_state(state);
1555 if (ret < 0)
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001556 return ret;
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001557 state = ret;
1558 }
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001559 sp = cpuhp_get_step(state);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001560 if (name && sp->name)
1561 return -EBUSY;
1562
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001563 sp->startup.single = startup;
1564 sp->teardown.single = teardown;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001565 sp->name = name;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001566 sp->multi_instance = multi_instance;
1567 INIT_HLIST_HEAD(&sp->list);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001568 return ret;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001569}
1570
1571static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1572{
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001573 return cpuhp_get_step(state)->teardown.single;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001574}
1575
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001576/*
1577 * Call the startup/teardown function for a step either on the AP or
1578 * on the current CPU.
1579 */
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001580static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1581 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001582{
Thomas Gleixnera7246322016-08-12 19:49:38 +02001583 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001584 int ret;
1585
Peter Zijlstra4dddfb52017-09-20 19:00:17 +02001586 /*
1587 * If there's nothing to do, we done.
1588 * Relies on the union for multi_instance.
1589 */
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001590 if ((bringup && !sp->startup.single) ||
1591 (!bringup && !sp->teardown.single))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001592 return 0;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001593 /*
1594 * The non AP bound callbacks can fail on bringup. On teardown
1595 * e.g. module removal we crash for now.
1596 */
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001597#ifdef CONFIG_SMP
1598 if (cpuhp_is_ap_state(state))
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001599 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001600 else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001601 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001602#else
Peter Zijlstra96abb962017-09-20 19:00:16 +02001603 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
Thomas Gleixner1cf4f622016-02-26 18:43:39 +00001604#endif
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001605 BUG_ON(ret && !bringup);
1606 return ret;
1607}
1608
1609/*
1610 * Called from __cpuhp_setup_state on a recoverable failure.
1611 *
1612 * Note: The teardown callbacks for rollback are not allowed to fail!
1613 */
1614static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001615 struct hlist_node *node)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001616{
1617 int cpu;
1618
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001619 /* Roll back the already executed steps on the other cpus */
1620 for_each_present_cpu(cpu) {
1621 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1622 int cpustate = st->state;
1623
1624 if (cpu >= failedcpu)
1625 break;
1626
1627 /* Did we invoke the startup call on that cpu ? */
1628 if (cpustate >= state)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001629 cpuhp_issue_call(cpu, state, false, node);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001630 }
1631}
1632
Thomas Gleixner9805c672017-05-24 10:15:15 +02001633int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1634 struct hlist_node *node,
1635 bool invoke)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001636{
1637 struct cpuhp_step *sp;
1638 int cpu;
1639 int ret;
1640
Thomas Gleixner9805c672017-05-24 10:15:15 +02001641 lockdep_assert_cpus_held();
1642
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001643 sp = cpuhp_get_step(state);
1644 if (sp->multi_instance == false)
1645 return -EINVAL;
1646
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001647 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001648
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001649 if (!invoke || !sp->startup.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001650 goto add_node;
1651
1652 /*
1653 * Try to call the startup callback for each present cpu
1654 * depending on the hotplug state of the cpu.
1655 */
1656 for_each_present_cpu(cpu) {
1657 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1658 int cpustate = st->state;
1659
1660 if (cpustate < state)
1661 continue;
1662
1663 ret = cpuhp_issue_call(cpu, state, true, node);
1664 if (ret) {
Thomas Gleixner3c1627e2016-09-05 15:28:36 +02001665 if (sp->teardown.multi)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001666 cpuhp_rollback_install(cpu, state, node);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001667 goto unlock;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001668 }
1669 }
1670add_node:
1671 ret = 0;
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001672 hlist_add_head(node, &sp->list);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001673unlock:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001674 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner9805c672017-05-24 10:15:15 +02001675 return ret;
1676}
1677
1678int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1679 bool invoke)
1680{
1681 int ret;
1682
1683 cpus_read_lock();
1684 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001685 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001686 return ret;
1687}
1688EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1689
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001690/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001691 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001692 * @state: The state to setup
1693 * @invoke: If true, the startup function is invoked for cpus where
1694 * cpu state >= @state
1695 * @startup: startup callback function
1696 * @teardown: teardown callback function
1697 * @multi_instance: State is set up for multiple instances which get
1698 * added afterwards.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001699 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001700 * The caller needs to hold cpus read locked while calling this function.
Boris Ostrovsky512f0982016-12-15 10:00:57 -05001701 * Returns:
1702 * On success:
1703 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1704 * 0 for all other states
1705 * On failure: proper (negative) error code
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001706 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001707int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
1708 const char *name, bool invoke,
1709 int (*startup)(unsigned int cpu),
1710 int (*teardown)(unsigned int cpu),
1711 bool multi_instance)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001712{
1713 int cpu, ret = 0;
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001714 bool dynstate;
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001715
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001716 lockdep_assert_cpus_held();
1717
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001718 if (cpuhp_cb_check(state) || !name)
1719 return -EINVAL;
1720
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001721 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001722
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001723 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1724 multi_instance);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001725
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001726 dynstate = state == CPUHP_AP_ONLINE_DYN;
1727 if (ret > 0 && dynstate) {
1728 state = ret;
1729 ret = 0;
1730 }
1731
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001732 if (ret || !invoke || !startup)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001733 goto out;
1734
1735 /*
1736 * Try to call the startup callback for each present cpu
1737 * depending on the hotplug state of the cpu.
1738 */
1739 for_each_present_cpu(cpu) {
1740 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1741 int cpustate = st->state;
1742
1743 if (cpustate < state)
1744 continue;
1745
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001746 ret = cpuhp_issue_call(cpu, state, true, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001747 if (ret) {
Thomas Gleixnera7246322016-08-12 19:49:38 +02001748 if (teardown)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001749 cpuhp_rollback_install(cpu, state, NULL);
1750 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001751 goto out;
1752 }
1753 }
1754out:
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001755 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixnerdc280d932016-12-21 20:19:49 +01001756 /*
1757 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1758 * dynamically allocated state in case of success.
1759 */
Thomas Gleixnerb9d9d692016-12-26 22:58:19 +01001760 if (!ret && dynstate)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001761 return state;
1762 return ret;
1763}
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001764EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1765
1766int __cpuhp_setup_state(enum cpuhp_state state,
1767 const char *name, bool invoke,
1768 int (*startup)(unsigned int cpu),
1769 int (*teardown)(unsigned int cpu),
1770 bool multi_instance)
1771{
1772 int ret;
1773
1774 cpus_read_lock();
1775 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1776 teardown, multi_instance);
1777 cpus_read_unlock();
1778 return ret;
1779}
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001780EXPORT_SYMBOL(__cpuhp_setup_state);
1781
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001782int __cpuhp_state_remove_instance(enum cpuhp_state state,
1783 struct hlist_node *node, bool invoke)
1784{
1785 struct cpuhp_step *sp = cpuhp_get_step(state);
1786 int cpu;
1787
1788 BUG_ON(cpuhp_cb_check(state));
1789
1790 if (!sp->multi_instance)
1791 return -EINVAL;
1792
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001793 cpus_read_lock();
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001794 mutex_lock(&cpuhp_state_mutex);
1795
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001796 if (!invoke || !cpuhp_get_teardown_cb(state))
1797 goto remove;
1798 /*
1799 * Call the teardown callback for each present cpu depending
1800 * on the hotplug state of the cpu. This function is not
1801 * allowed to fail currently!
1802 */
1803 for_each_present_cpu(cpu) {
1804 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1805 int cpustate = st->state;
1806
1807 if (cpustate >= state)
1808 cpuhp_issue_call(cpu, state, false, node);
1809 }
1810
1811remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001812 hlist_del(node);
1813 mutex_unlock(&cpuhp_state_mutex);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001814 cpus_read_unlock();
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001815
1816 return 0;
1817}
1818EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001819
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001820/**
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001821 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001822 * @state: The state to remove
1823 * @invoke: If true, the teardown function is invoked for cpus where
1824 * cpu state >= @state
1825 *
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001826 * The caller needs to hold cpus read locked while calling this function.
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001827 * The teardown callback is currently not allowed to fail. Think
1828 * about module removal!
1829 */
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001830void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001831{
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001832 struct cpuhp_step *sp = cpuhp_get_step(state);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001833 int cpu;
1834
1835 BUG_ON(cpuhp_cb_check(state));
1836
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001837 lockdep_assert_cpus_held();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001838
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001839 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001840 if (sp->multi_instance) {
1841 WARN(!hlist_empty(&sp->list),
1842 "Error: Removing state %d which has instances left.\n",
1843 state);
1844 goto remove;
1845 }
1846
Thomas Gleixnera7246322016-08-12 19:49:38 +02001847 if (!invoke || !cpuhp_get_teardown_cb(state))
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001848 goto remove;
1849
1850 /*
1851 * Call the teardown callback for each present cpu depending
1852 * on the hotplug state of the cpu. This function is not
1853 * allowed to fail currently!
1854 */
1855 for_each_present_cpu(cpu) {
1856 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1857 int cpustate = st->state;
1858
1859 if (cpustate >= state)
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001860 cpuhp_issue_call(cpu, state, false, NULL);
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001861 }
1862remove:
Thomas Gleixnercf392d12016-08-12 19:49:39 +02001863 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
Sebastian Andrzej Siewiordc434e052017-03-14 16:06:45 +01001864 mutex_unlock(&cpuhp_state_mutex);
Sebastian Andrzej Siewior71def422017-05-24 10:15:14 +02001865}
1866EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1867
1868void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1869{
1870 cpus_read_lock();
1871 __cpuhp_remove_state_cpuslocked(state, invoke);
Thomas Gleixner8f553c42017-05-24 10:15:12 +02001872 cpus_read_unlock();
Thomas Gleixner5b7aa872016-02-26 18:43:33 +00001873}
1874EXPORT_SYMBOL(__cpuhp_remove_state);
1875
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001876#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1877static ssize_t show_cpuhp_state(struct device *dev,
1878 struct device_attribute *attr, char *buf)
1879{
1880 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1881
1882 return sprintf(buf, "%d\n", st->state);
1883}
1884static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1885
Thomas Gleixner757c9892016-02-26 18:43:32 +00001886static ssize_t write_cpuhp_target(struct device *dev,
1887 struct device_attribute *attr,
1888 const char *buf, size_t count)
1889{
1890 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1891 struct cpuhp_step *sp;
1892 int target, ret;
1893
1894 ret = kstrtoint(buf, 10, &target);
1895 if (ret)
1896 return ret;
1897
1898#ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1899 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1900 return -EINVAL;
1901#else
1902 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1903 return -EINVAL;
1904#endif
1905
1906 ret = lock_device_hotplug_sysfs();
1907 if (ret)
1908 return ret;
1909
1910 mutex_lock(&cpuhp_state_mutex);
1911 sp = cpuhp_get_step(target);
1912 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1913 mutex_unlock(&cpuhp_state_mutex);
1914 if (ret)
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001915 goto out;
Thomas Gleixner757c9892016-02-26 18:43:32 +00001916
1917 if (st->state < target)
1918 ret = do_cpu_up(dev->id, target);
1919 else
1920 ret = do_cpu_down(dev->id, target);
Sebastian Andrzej Siewior40da1b12017-06-02 16:27:14 +02001921out:
Thomas Gleixner757c9892016-02-26 18:43:32 +00001922 unlock_device_hotplug();
1923 return ret ? ret : count;
1924}
1925
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001926static ssize_t show_cpuhp_target(struct device *dev,
1927 struct device_attribute *attr, char *buf)
1928{
1929 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1930
1931 return sprintf(buf, "%d\n", st->target);
1932}
Thomas Gleixner757c9892016-02-26 18:43:32 +00001933static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001934
Peter Zijlstra1db49482017-09-20 19:00:21 +02001935
1936static ssize_t write_cpuhp_fail(struct device *dev,
1937 struct device_attribute *attr,
1938 const char *buf, size_t count)
1939{
1940 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1941 struct cpuhp_step *sp;
1942 int fail, ret;
1943
1944 ret = kstrtoint(buf, 10, &fail);
1945 if (ret)
1946 return ret;
1947
1948 /*
1949 * Cannot fail STARTING/DYING callbacks.
1950 */
1951 if (cpuhp_is_atomic_state(fail))
1952 return -EINVAL;
1953
1954 /*
1955 * Cannot fail anything that doesn't have callbacks.
1956 */
1957 mutex_lock(&cpuhp_state_mutex);
1958 sp = cpuhp_get_step(fail);
1959 if (!sp->startup.single && !sp->teardown.single)
1960 ret = -EINVAL;
1961 mutex_unlock(&cpuhp_state_mutex);
1962 if (ret)
1963 return ret;
1964
1965 st->fail = fail;
1966
1967 return count;
1968}
1969
1970static ssize_t show_cpuhp_fail(struct device *dev,
1971 struct device_attribute *attr, char *buf)
1972{
1973 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1974
1975 return sprintf(buf, "%d\n", st->fail);
1976}
1977
1978static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
1979
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001980static struct attribute *cpuhp_cpu_attrs[] = {
1981 &dev_attr_state.attr,
1982 &dev_attr_target.attr,
Peter Zijlstra1db49482017-09-20 19:00:21 +02001983 &dev_attr_fail.attr,
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001984 NULL
1985};
1986
Arvind Yadav993647a2017-06-29 17:40:47 +05301987static const struct attribute_group cpuhp_cpu_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00001988 .attrs = cpuhp_cpu_attrs,
1989 .name = "hotplug",
1990 NULL
1991};
1992
1993static ssize_t show_cpuhp_states(struct device *dev,
1994 struct device_attribute *attr, char *buf)
1995{
1996 ssize_t cur, res = 0;
1997 int i;
1998
1999 mutex_lock(&cpuhp_state_mutex);
Thomas Gleixner757c9892016-02-26 18:43:32 +00002000 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00002001 struct cpuhp_step *sp = cpuhp_get_step(i);
2002
2003 if (sp->name) {
2004 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2005 buf += cur;
2006 res += cur;
2007 }
2008 }
2009 mutex_unlock(&cpuhp_state_mutex);
2010 return res;
2011}
2012static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
2013
2014static struct attribute *cpuhp_cpu_root_attrs[] = {
2015 &dev_attr_states.attr,
2016 NULL
2017};
2018
Arvind Yadav993647a2017-06-29 17:40:47 +05302019static const struct attribute_group cpuhp_cpu_root_attr_group = {
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00002020 .attrs = cpuhp_cpu_root_attrs,
2021 .name = "hotplug",
2022 NULL
2023};
2024
Thomas Gleixner05736e42018-05-29 17:48:27 +02002025#ifdef CONFIG_HOTPLUG_SMT
2026
2027static const char *smt_states[] = {
2028 [CPU_SMT_ENABLED] = "on",
2029 [CPU_SMT_DISABLED] = "off",
2030 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2031 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2032};
2033
2034static ssize_t
2035show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
2036{
2037 return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
2038}
2039
2040static void cpuhp_offline_cpu_device(unsigned int cpu)
2041{
2042 struct device *dev = get_cpu_device(cpu);
2043
2044 dev->offline = true;
2045 /* Tell user space about the state change */
2046 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2047}
2048
Thomas Gleixner215af542018-07-07 11:40:18 +02002049static void cpuhp_online_cpu_device(unsigned int cpu)
2050{
2051 struct device *dev = get_cpu_device(cpu);
2052
2053 dev->offline = false;
2054 /* Tell user space about the state change */
2055 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2056}
2057
Jiri Kosina53c613f2018-09-25 14:38:55 +02002058/*
2059 * Architectures that need SMT-specific errata handling during SMT hotplug
2060 * should override this.
2061 */
2062void __weak arch_smt_update(void) { };
2063
Thomas Gleixner05736e42018-05-29 17:48:27 +02002064static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2065{
2066 int cpu, ret = 0;
2067
2068 cpu_maps_update_begin();
2069 for_each_online_cpu(cpu) {
2070 if (topology_is_primary_thread(cpu))
2071 continue;
2072 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2073 if (ret)
2074 break;
2075 /*
2076 * As this needs to hold the cpu maps lock it's impossible
2077 * to call device_offline() because that ends up calling
2078 * cpu_down() which takes cpu maps lock. cpu maps lock
2079 * needs to be held as this might race against in kernel
2080 * abusers of the hotplug machinery (thermal management).
2081 *
2082 * So nothing would update device:offline state. That would
2083 * leave the sysfs entry stale and prevent onlining after
2084 * smt control has been changed to 'off' again. This is
2085 * called under the sysfs hotplug lock, so it is properly
2086 * serialized against the regular offline usage.
2087 */
2088 cpuhp_offline_cpu_device(cpu);
2089 }
Jiri Kosina53c613f2018-09-25 14:38:55 +02002090 if (!ret) {
Thomas Gleixner05736e42018-05-29 17:48:27 +02002091 cpu_smt_control = ctrlval;
Jiri Kosina53c613f2018-09-25 14:38:55 +02002092 arch_smt_update();
2093 }
Thomas Gleixner05736e42018-05-29 17:48:27 +02002094 cpu_maps_update_done();
2095 return ret;
2096}
2097
Thomas Gleixner215af542018-07-07 11:40:18 +02002098static int cpuhp_smt_enable(void)
Thomas Gleixner05736e42018-05-29 17:48:27 +02002099{
Thomas Gleixner215af542018-07-07 11:40:18 +02002100 int cpu, ret = 0;
2101
Thomas Gleixner05736e42018-05-29 17:48:27 +02002102 cpu_maps_update_begin();
2103 cpu_smt_control = CPU_SMT_ENABLED;
Jiri Kosina53c613f2018-09-25 14:38:55 +02002104 arch_smt_update();
Thomas Gleixner215af542018-07-07 11:40:18 +02002105 for_each_present_cpu(cpu) {
2106 /* Skip online CPUs and CPUs on offline nodes */
2107 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2108 continue;
2109 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2110 if (ret)
2111 break;
2112 /* See comment in cpuhp_smt_disable() */
2113 cpuhp_online_cpu_device(cpu);
2114 }
Thomas Gleixner05736e42018-05-29 17:48:27 +02002115 cpu_maps_update_done();
Thomas Gleixner215af542018-07-07 11:40:18 +02002116 return ret;
Thomas Gleixner05736e42018-05-29 17:48:27 +02002117}
2118
2119static ssize_t
2120store_smt_control(struct device *dev, struct device_attribute *attr,
2121 const char *buf, size_t count)
2122{
2123 int ctrlval, ret;
2124
2125 if (sysfs_streq(buf, "on"))
2126 ctrlval = CPU_SMT_ENABLED;
2127 else if (sysfs_streq(buf, "off"))
2128 ctrlval = CPU_SMT_DISABLED;
2129 else if (sysfs_streq(buf, "forceoff"))
2130 ctrlval = CPU_SMT_FORCE_DISABLED;
2131 else
2132 return -EINVAL;
2133
2134 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2135 return -EPERM;
2136
2137 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2138 return -ENODEV;
2139
2140 ret = lock_device_hotplug_sysfs();
2141 if (ret)
2142 return ret;
2143
2144 if (ctrlval != cpu_smt_control) {
2145 switch (ctrlval) {
2146 case CPU_SMT_ENABLED:
Thomas Gleixner215af542018-07-07 11:40:18 +02002147 ret = cpuhp_smt_enable();
Thomas Gleixner05736e42018-05-29 17:48:27 +02002148 break;
2149 case CPU_SMT_DISABLED:
2150 case CPU_SMT_FORCE_DISABLED:
2151 ret = cpuhp_smt_disable(ctrlval);
2152 break;
2153 }
2154 }
2155
2156 unlock_device_hotplug();
2157 return ret ? ret : count;
2158}
2159static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2160
2161static ssize_t
2162show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2163{
2164 bool active = topology_max_smt_threads() > 1;
2165
2166 return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
2167}
2168static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2169
2170static struct attribute *cpuhp_smt_attrs[] = {
2171 &dev_attr_control.attr,
2172 &dev_attr_active.attr,
2173 NULL
2174};
2175
2176static const struct attribute_group cpuhp_smt_attr_group = {
2177 .attrs = cpuhp_smt_attrs,
2178 .name = "smt",
2179 NULL
2180};
2181
2182static int __init cpu_smt_state_init(void)
2183{
Thomas Gleixner05736e42018-05-29 17:48:27 +02002184 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2185 &cpuhp_smt_attr_group);
2186}
2187
2188#else
2189static inline int cpu_smt_state_init(void) { return 0; }
2190#endif
2191
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00002192static int __init cpuhp_sysfs_init(void)
2193{
2194 int cpu, ret;
2195
Thomas Gleixner05736e42018-05-29 17:48:27 +02002196 ret = cpu_smt_state_init();
2197 if (ret)
2198 return ret;
2199
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +00002200 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2201 &cpuhp_cpu_root_attr_group);
2202 if (ret)
2203 return ret;
2204
2205 for_each_possible_cpu(cpu) {
2206 struct device *dev = get_cpu_device(cpu);
2207
2208 if (!dev)
2209 continue;
2210 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2211 if (ret)
2212 return ret;
2213 }
2214 return 0;
2215}
2216device_initcall(cpuhp_sysfs_init);
2217#endif
2218
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002219/*
2220 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2221 * represents all NR_CPUS bits binary values of 1<<nr.
2222 *
Rusty Russelle0b582e2009-01-01 10:12:28 +10302223 * It is used by cpumask_of() to get a constant address to a CPU
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002224 * mask value that has a single bit set only.
2225 */
Mike Travisb8d317d2008-07-24 18:21:29 -07002226
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002227/* cpu_bit_bitmap[0] is empty - so we can back into it */
Michael Rodriguez4d519852011-03-22 16:34:07 -07002228#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002229#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2230#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2231#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
Mike Travisb8d317d2008-07-24 18:21:29 -07002232
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002233const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
Mike Travisb8d317d2008-07-24 18:21:29 -07002234
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002235 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2236 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2237#if BITS_PER_LONG > 32
2238 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2239 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
Mike Travisb8d317d2008-07-24 18:21:29 -07002240#endif
2241};
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07002242EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
Rusty Russell2d3854a2008-11-05 13:39:10 +11002243
2244const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2245EXPORT_SYMBOL(cpu_all_bits);
Rusty Russellb3199c02008-12-30 09:05:14 +10302246
2247#ifdef CONFIG_INIT_ALL_POSSIBLE
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002248struct cpumask __cpu_possible_mask __read_mostly
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002249 = {CPU_BITS_ALL};
Rusty Russellb3199c02008-12-30 09:05:14 +10302250#else
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002251struct cpumask __cpu_possible_mask __read_mostly;
Rusty Russellb3199c02008-12-30 09:05:14 +10302252#endif
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002253EXPORT_SYMBOL(__cpu_possible_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10302254
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002255struct cpumask __cpu_online_mask __read_mostly;
2256EXPORT_SYMBOL(__cpu_online_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10302257
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002258struct cpumask __cpu_present_mask __read_mostly;
2259EXPORT_SYMBOL(__cpu_present_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10302260
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08002261struct cpumask __cpu_active_mask __read_mostly;
2262EXPORT_SYMBOL(__cpu_active_mask);
Rusty Russell3fa41522008-12-30 09:05:16 +10302263
Rusty Russell3fa41522008-12-30 09:05:16 +10302264void init_cpu_present(const struct cpumask *src)
2265{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002266 cpumask_copy(&__cpu_present_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10302267}
2268
2269void init_cpu_possible(const struct cpumask *src)
2270{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002271 cpumask_copy(&__cpu_possible_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10302272}
2273
2274void init_cpu_online(const struct cpumask *src)
2275{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08002276 cpumask_copy(&__cpu_online_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10302277}
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002278
2279/*
2280 * Activate the first processor.
2281 */
2282void __init boot_cpu_init(void)
2283{
2284 int cpu = smp_processor_id();
2285
2286 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2287 set_cpu_online(cpu, true);
2288 set_cpu_active(cpu, true);
2289 set_cpu_present(cpu, true);
2290 set_cpu_possible(cpu, true);
Peter Zijlstra8ce371f2017-03-20 12:26:55 +01002291
2292#ifdef CONFIG_SMP
2293 __boot_cpu_id = cpu;
2294#endif
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002295}
2296
2297/*
2298 * Must be called _AFTER_ setting up the per_cpu areas
2299 */
Linus Torvaldsb5b14042018-08-12 12:19:42 -07002300void __init boot_cpu_hotplug_init(void)
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002301{
Abel Vesa269777a2018-08-15 00:26:00 +03002302#ifdef CONFIG_SMP
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +02002303 this_cpu_write(cpuhp_state.booted_once, true);
Abel Vesa269777a2018-08-15 00:26:00 +03002304#endif
Thomas Gleixner0cc3cd22018-06-29 16:05:48 +02002305 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
Thomas Gleixnercff7d372016-02-26 18:43:28 +00002306}