blob: 1979b8927b8662215e3248bbf1c7918d1395586d [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>
10#include <linux/sched.h>
11#include <linux/unistd.h>
12#include <linux/cpu.h>
Anton Vorontsovcb792952012-05-31 16:26:22 -070013#include <linux/oom.h>
14#include <linux/rcupdate.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Anton Vorontsove4cc2f82012-05-31 16:26:26 -070016#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kthread.h>
18#include <linux/stop_machine.h>
Ingo Molnar81615b622006-06-26 00:24:32 -070019#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +010021#include <linux/suspend.h>
Gautham R. Shenoya19423b2014-03-11 02:04:03 +053022#include <linux/lockdep.h>
Preeti U Murthy345527b2015-03-30 14:59:19 +053023#include <linux/tick.h>
Thomas Gleixnera8994182015-07-05 17:12:30 +000024#include <linux/irq.h>
Thomas Gleixnercff7d372016-02-26 18:43:28 +000025
Todd E Brandtbb3632c2014-06-06 05:40:17 -070026#include <trace/events/power.h>
Thomas Gleixnercff7d372016-02-26 18:43:28 +000027#define CREATE_TRACE_POINTS
28#include <trace/events/cpuhp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Gleixner38498a62012-04-20 13:05:44 +000030#include "smpboot.h"
31
Thomas Gleixnercff7d372016-02-26 18:43:28 +000032/**
33 * cpuhp_cpu_state - Per cpu hotplug state storage
34 * @state: The current cpu state
35 * @target: The target state
36 */
37struct cpuhp_cpu_state {
38 enum cpuhp_state state;
39 enum cpuhp_state target;
40};
41
42static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
43
44/**
45 * cpuhp_step - Hotplug state machine step
46 * @name: Name of the step
47 * @startup: Startup function of the step
48 * @teardown: Teardown function of the step
49 * @skip_onerr: Do not invoke the functions on error rollback
50 * Will go away once the notifiers are gone
51 */
52struct cpuhp_step {
53 const char *name;
54 int (*startup)(unsigned int cpu);
55 int (*teardown)(unsigned int cpu);
56 bool skip_onerr;
57};
58
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +000059static DEFINE_MUTEX(cpuhp_state_mutex);
Thomas Gleixnercff7d372016-02-26 18:43:28 +000060static struct cpuhp_step cpuhp_bp_states[];
Thomas Gleixner4baa0af2016-02-26 18:43:29 +000061static struct cpuhp_step cpuhp_ap_states[];
Thomas Gleixnercff7d372016-02-26 18:43:28 +000062
63/**
64 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
65 * @cpu: The cpu for which the callback should be invoked
66 * @step: The step in the state machine
67 * @cb: The callback function to invoke
68 *
69 * Called from cpu hotplug and from the state register machinery
70 */
71static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state step,
72 int (*cb)(unsigned int))
73{
74 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
75 int ret = 0;
76
77 if (cb) {
78 trace_cpuhp_enter(cpu, st->target, step, cb);
79 ret = cb(cpu);
80 trace_cpuhp_exit(cpu, st->state, step, ret);
81 }
82 return ret;
83}
84
Rusty Russell98a79d62008-12-13 21:19:41 +103085#ifdef CONFIG_SMP
Rusty Russellb3199c02008-12-30 09:05:14 +103086/* Serializes the updates to cpu_online_mask, cpu_present_mask */
Linus Torvaldsaa953872006-07-23 12:12:16 -070087static DEFINE_MUTEX(cpu_add_remove_lock);
Thomas Gleixner090e77c2016-02-26 18:43:23 +000088bool cpuhp_tasks_frozen;
89EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Lai Jiangshan79a6cde2010-05-26 14:43:36 -070091/*
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +053092 * The following two APIs (cpu_maps_update_begin/done) must be used when
93 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
94 * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
95 * hotplug callback (un)registration performed using __register_cpu_notifier()
96 * or __unregister_cpu_notifier().
Lai Jiangshan79a6cde2010-05-26 14:43:36 -070097 */
98void cpu_maps_update_begin(void)
99{
100 mutex_lock(&cpu_add_remove_lock);
101}
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530102EXPORT_SYMBOL(cpu_notifier_register_begin);
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700103
104void cpu_maps_update_done(void)
105{
106 mutex_unlock(&cpu_add_remove_lock);
107}
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530108EXPORT_SYMBOL(cpu_notifier_register_done);
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700109
Daniel J Blueman5c113fb2010-06-01 12:15:11 +0100110static RAW_NOTIFIER_HEAD(cpu_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700112/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
113 * Should always be manipulated under cpu_add_remove_lock
114 */
115static int cpu_hotplug_disabled;
116
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700117#ifdef CONFIG_HOTPLUG_CPU
118
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100119static struct {
120 struct task_struct *active_writer;
David Hildenbrand87af9e72014-12-12 10:11:44 +0100121 /* wait queue to wake up the active_writer */
122 wait_queue_head_t wq;
123 /* verifies that no writer will get active while readers are active */
124 struct mutex lock;
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100125 /*
126 * Also blocks the new readers during
127 * an ongoing cpu hotplug operation.
128 */
David Hildenbrand87af9e72014-12-12 10:11:44 +0100129 atomic_t refcount;
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530130
131#ifdef CONFIG_DEBUG_LOCK_ALLOC
132 struct lockdep_map dep_map;
133#endif
Linus Torvalds31950eb2009-06-22 21:18:12 -0700134} cpu_hotplug = {
135 .active_writer = NULL,
David Hildenbrand87af9e72014-12-12 10:11:44 +0100136 .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
Linus Torvalds31950eb2009-06-22 21:18:12 -0700137 .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530138#ifdef CONFIG_DEBUG_LOCK_ALLOC
139 .dep_map = {.name = "cpu_hotplug.lock" },
140#endif
Linus Torvalds31950eb2009-06-22 21:18:12 -0700141};
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100142
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530143/* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
144#define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
Paul E. McKenneydd56af42014-08-25 20:25:06 -0700145#define cpuhp_lock_acquire_tryread() \
146 lock_map_acquire_tryread(&cpu_hotplug.dep_map)
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530147#define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
148#define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
149
Paul E. McKenney62db99f2014-10-22 14:51:49 -0700150
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100151void get_online_cpus(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800152{
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100153 might_sleep();
154 if (cpu_hotplug.active_writer == current)
Linus Torvaldsaa953872006-07-23 12:12:16 -0700155 return;
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530156 cpuhp_lock_acquire_read();
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100157 mutex_lock(&cpu_hotplug.lock);
David Hildenbrand87af9e72014-12-12 10:11:44 +0100158 atomic_inc(&cpu_hotplug.refcount);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100159 mutex_unlock(&cpu_hotplug.lock);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800160}
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100161EXPORT_SYMBOL_GPL(get_online_cpus);
Ashok Raj90d45d12005-11-08 21:34:24 -0800162
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100163void put_online_cpus(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -0800164{
David Hildenbrand87af9e72014-12-12 10:11:44 +0100165 int refcount;
166
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100167 if (cpu_hotplug.active_writer == current)
Linus Torvaldsaa953872006-07-23 12:12:16 -0700168 return;
Srivatsa S. Bhat075663d2012-10-08 16:28:20 -0700169
David Hildenbrand87af9e72014-12-12 10:11:44 +0100170 refcount = atomic_dec_return(&cpu_hotplug.refcount);
171 if (WARN_ON(refcount < 0)) /* try to fix things up */
172 atomic_inc(&cpu_hotplug.refcount);
Srivatsa S. Bhat075663d2012-10-08 16:28:20 -0700173
David Hildenbrand87af9e72014-12-12 10:11:44 +0100174 if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
175 wake_up(&cpu_hotplug.wq);
176
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530177 cpuhp_lock_release();
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100178
Ashok Raja9d9baa2005-11-28 13:43:46 -0800179}
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100180EXPORT_SYMBOL_GPL(put_online_cpus);
Ashok Raja9d9baa2005-11-28 13:43:46 -0800181
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100182/*
183 * This ensures that the hotplug operation can begin only when the
184 * refcount goes to zero.
185 *
186 * Note that during a cpu-hotplug operation, the new readers, if any,
187 * will be blocked by the cpu_hotplug.lock
188 *
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700189 * Since cpu_hotplug_begin() is always called after invoking
190 * cpu_maps_update_begin(), we can be sure that only one writer is active.
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100191 *
192 * Note that theoretically, there is a possibility of a livelock:
193 * - Refcount goes to zero, last reader wakes up the sleeping
194 * writer.
195 * - Last reader unlocks the cpu_hotplug.lock.
196 * - A new reader arrives at this moment, bumps up the refcount.
197 * - The writer acquires the cpu_hotplug.lock finds the refcount
198 * non zero and goes to sleep again.
199 *
200 * However, this is very difficult to achieve in practice since
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100201 * get_online_cpus() not an api which is called all that often.
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100202 *
203 */
Toshi Kanib9d10be2013-08-12 09:45:53 -0600204void cpu_hotplug_begin(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100205{
David Hildenbrand87af9e72014-12-12 10:11:44 +0100206 DEFINE_WAIT(wait);
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700207
David Hildenbrand87af9e72014-12-12 10:11:44 +0100208 cpu_hotplug.active_writer = current;
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530209 cpuhp_lock_acquire();
David Hildenbrand87af9e72014-12-12 10:11:44 +0100210
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700211 for (;;) {
212 mutex_lock(&cpu_hotplug.lock);
David Hildenbrand87af9e72014-12-12 10:11:44 +0100213 prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
214 if (likely(!atomic_read(&cpu_hotplug.refcount)))
215 break;
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100216 mutex_unlock(&cpu_hotplug.lock);
217 schedule();
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100218 }
David Hildenbrand87af9e72014-12-12 10:11:44 +0100219 finish_wait(&cpu_hotplug.wq, &wait);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100220}
221
Toshi Kanib9d10be2013-08-12 09:45:53 -0600222void cpu_hotplug_done(void)
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100223{
224 cpu_hotplug.active_writer = NULL;
225 mutex_unlock(&cpu_hotplug.lock);
Gautham R. Shenoya19423b2014-03-11 02:04:03 +0530226 cpuhp_lock_release();
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100227}
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700228
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700229/*
230 * Wait for currently running CPU hotplug operations to complete (if any) and
231 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
232 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
233 * hotplug path before performing hotplug operations. So acquiring that lock
234 * guarantees mutual exclusion from any currently running hotplug operations.
235 */
236void cpu_hotplug_disable(void)
237{
238 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700239 cpu_hotplug_disabled++;
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700240 cpu_maps_update_done();
241}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700242EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700243
244void cpu_hotplug_enable(void)
245{
246 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700247 WARN_ON(--cpu_hotplug_disabled < 0);
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700248 cpu_maps_update_done();
249}
Vitaly Kuznetsov32145c42015-08-05 00:52:47 -0700250EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
Toshi Kanib9d10be2013-08-12 09:45:53 -0600251#endif /* CONFIG_HOTPLUG_CPU */
Lai Jiangshan79a6cde2010-05-26 14:43:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* Need to know about CPUs going up/down? */
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200254int register_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Neil Brownbd5349c2006-10-17 00:10:35 -0700256 int ret;
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100257 cpu_maps_update_begin();
Neil Brownbd5349c2006-10-17 00:10:35 -0700258 ret = raw_notifier_chain_register(&cpu_chain, nb);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100259 cpu_maps_update_done();
Neil Brownbd5349c2006-10-17 00:10:35 -0700260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Chandra Seetharaman65edc682006-06-27 02:54:08 -0700262
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200263int __register_cpu_notifier(struct notifier_block *nb)
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530264{
265 return raw_notifier_chain_register(&cpu_chain, nb);
266}
267
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000268static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
Akinobu Mitae9fb7632010-05-26 14:43:28 -0700269 int *nr_calls)
270{
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000271 unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
272 void *hcpu = (void *)(long)cpu;
273
Akinobu Mitae6bde732010-05-26 14:43:29 -0700274 int ret;
275
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000276 ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
Akinobu Mitae9fb7632010-05-26 14:43:28 -0700277 nr_calls);
Akinobu Mitae6bde732010-05-26 14:43:29 -0700278
279 return notifier_to_errno(ret);
Akinobu Mitae9fb7632010-05-26 14:43:28 -0700280}
281
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000282static int cpu_notify(unsigned long val, unsigned int cpu)
Akinobu Mitae9fb7632010-05-26 14:43:28 -0700283{
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000284 return __cpu_notify(val, cpu, -1, NULL);
Akinobu Mitae9fb7632010-05-26 14:43:28 -0700285}
286
Thomas Gleixnerba997462016-02-26 18:43:24 +0000287/* Notifier wrappers for transitioning to state machine */
288static int notify_prepare(unsigned int cpu)
289{
290 int nr_calls = 0;
291 int ret;
292
293 ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
294 if (ret) {
295 nr_calls--;
296 printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
297 __func__, cpu);
298 __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
299 }
300 return ret;
301}
302
303static int notify_online(unsigned int cpu)
304{
305 cpu_notify(CPU_ONLINE, cpu);
306 return 0;
307}
308
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000309static int notify_starting(unsigned int cpu)
310{
311 cpu_notify(CPU_STARTING, cpu);
312 return 0;
313}
314
Thomas Gleixnerba997462016-02-26 18:43:24 +0000315static int bringup_cpu(unsigned int cpu)
316{
317 struct task_struct *idle = idle_thread_get(cpu);
318 int ret;
319
320 /* Arch-specific enabling code. */
321 ret = __cpu_up(cpu, idle);
322 if (ret) {
323 cpu_notify(CPU_UP_CANCELED, cpu);
324 return ret;
325 }
326 BUG_ON(!cpu_online(cpu));
327 return 0;
328}
329
Linus Torvalds00b9b0a2010-05-27 10:32:08 -0700330#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331EXPORT_SYMBOL(register_cpu_notifier);
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530332EXPORT_SYMBOL(__register_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200334void unregister_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100336 cpu_maps_update_begin();
Neil Brownbd5349c2006-10-17 00:10:35 -0700337 raw_notifier_chain_unregister(&cpu_chain, nb);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100338 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340EXPORT_SYMBOL(unregister_cpu_notifier);
341
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200342void __unregister_cpu_notifier(struct notifier_block *nb)
Srivatsa S. Bhat93ae4f92014-03-11 02:04:14 +0530343{
344 raw_notifier_chain_unregister(&cpu_chain, nb);
345}
346EXPORT_SYMBOL(__unregister_cpu_notifier);
347
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700348/**
349 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
350 * @cpu: a CPU id
351 *
352 * This function walks all processes, finds a valid mm struct for each one and
353 * then clears a corresponding bit in mm's cpumask. While this all sounds
354 * trivial, there are various non-obvious corner cases, which this function
355 * tries to solve in a safe manner.
356 *
357 * Also note that the function uses a somewhat relaxed locking scheme, so it may
358 * be called only for an already offlined CPU.
359 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700360void clear_tasks_mm_cpumask(int cpu)
361{
362 struct task_struct *p;
363
364 /*
365 * This function is called after the cpu is taken down and marked
366 * offline, so its not like new tasks will ever get this cpu set in
367 * their mm mask. -- Peter Zijlstra
368 * Thus, we may use rcu_read_lock() here, instead of grabbing
369 * full-fledged tasklist_lock.
370 */
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700371 WARN_ON(cpu_online(cpu));
Anton Vorontsovcb792952012-05-31 16:26:22 -0700372 rcu_read_lock();
373 for_each_process(p) {
374 struct task_struct *t;
375
Anton Vorontsove4cc2f82012-05-31 16:26:26 -0700376 /*
377 * Main thread might exit, but other threads may still have
378 * a valid mm. Find one.
379 */
Anton Vorontsovcb792952012-05-31 16:26:22 -0700380 t = find_lock_task_mm(p);
381 if (!t)
382 continue;
383 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
384 task_unlock(t);
385 }
386 rcu_read_unlock();
387}
388
Kirill Tkhaib728ca02014-06-25 12:19:55 +0400389static inline void check_for_tasks(int dead_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Kirill Tkhaib728ca02014-06-25 12:19:55 +0400391 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Oleg Nesterova75a6062015-09-10 15:07:50 +0200393 read_lock(&tasklist_lock);
394 for_each_process_thread(g, p) {
Kirill Tkhaib728ca02014-06-25 12:19:55 +0400395 if (!p->on_rq)
396 continue;
397 /*
398 * We do the check with unlocked task_rq(p)->lock.
399 * Order the reading to do not warn about a task,
400 * which was running on this cpu in the past, and
401 * it's just been woken on another cpu.
402 */
403 rmb();
404 if (task_cpu(p) != dead_cpu)
405 continue;
406
407 pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
408 p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
Oleg Nesterova75a6062015-09-10 15:07:50 +0200409 }
410 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Thomas Gleixner98458172016-02-26 18:43:25 +0000413static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
414{
415 BUG_ON(cpu_notify(val, cpu));
416}
417
418static int notify_down_prepare(unsigned int cpu)
419{
420 int err, nr_calls = 0;
421
422 err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
423 if (err) {
424 nr_calls--;
425 __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
426 pr_warn("%s: attempt to take down CPU %u failed\n",
427 __func__, cpu);
428 }
429 return err;
430}
431
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000432static int notify_dying(unsigned int cpu)
433{
434 cpu_notify(CPU_DYING, cpu);
435 return 0;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/* Take this CPU down. */
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200439static int take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000441 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
442 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000443 int err, cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* Ensure this CPU doesn't handle any more interrupts. */
446 err = __cpu_disable();
447 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000450 /* Invoke the former CPU_DYING callbacks */
451 for (; st->state > target; st->state--) {
452 struct cpuhp_step *step = cpuhp_ap_states + st->state;
453
454 cpuhp_invoke_callback(cpu, st->state, step->teardown);
455 }
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200456 /* Give up timekeeping duties */
457 tick_handover_do_timer();
Thomas Gleixner14e568e2013-01-31 12:11:14 +0000458 /* Park the stopper thread */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000459 stop_machine_park(cpu);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Thomas Gleixner98458172016-02-26 18:43:25 +0000463static int takedown_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Thomas Gleixner98458172016-02-26 18:43:25 +0000465 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200467 /*
468 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
469 * and RCU users of this state to go away such that all new such users
470 * will observe it.
471 *
472 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
Paul E. McKenney779de6c2015-06-10 13:34:41 -0700473 * not imply sync_sched(), so wait for both.
Michael wang106dd5a2013-11-13 11:10:56 +0800474 *
475 * Do sync before park smpboot threads to take care the rcu boost case.
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200476 */
Paul E. McKenney779de6c2015-06-10 13:34:41 -0700477 if (IS_ENABLED(CONFIG_PREEMPT))
478 synchronize_rcu_mult(call_rcu, call_rcu_sched);
479 else
480 synchronize_rcu();
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200481
Michael wang106dd5a2013-11-13 11:10:56 +0800482 smpboot_park_threads(cpu);
483
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200484 /*
Thomas Gleixnera8994182015-07-05 17:12:30 +0000485 * Prevent irq alloc/free while the dying cpu reorganizes the
486 * interrupt affinities.
487 */
488 irq_lock_sparse();
489
490 /*
Peter Zijlstra6acce3e2013-10-11 14:38:20 +0200491 * So now all preempt/rcu users must observe !cpu_active().
492 */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000493 err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
Rusty Russell04321582008-07-28 12:16:29 -0500494 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* CPU didn't die: tell everyone. Can't complain. */
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000496 cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
Thomas Gleixnera8994182015-07-05 17:12:30 +0000497 irq_unlock_sparse();
Thomas Gleixner98458172016-02-26 18:43:25 +0000498 return err;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700499 }
Rusty Russell04321582008-07-28 12:16:29 -0500500 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100502 /*
503 * The migration_call() CPU_DYING callback will have removed all
504 * runnable tasks from the cpu, there's only the idle task left now
505 * that the migration thread is done doing the stop_machine thing.
Peter Zijlstra51a96c72010-11-19 20:37:53 +0100506 *
507 * Wait for the stop thread to go away.
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +0100508 */
Paul E. McKenney528a25b2015-01-28 14:09:43 -0800509 while (!per_cpu(cpu_dead_idle, cpu))
Peter Zijlstra51a96c72010-11-19 20:37:53 +0100510 cpu_relax();
Paul E. McKenney528a25b2015-01-28 14:09:43 -0800511 smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */
512 per_cpu(cpu_dead_idle, cpu) = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Thomas Gleixnera8994182015-07-05 17:12:30 +0000514 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
515 irq_unlock_sparse();
516
Preeti U Murthy345527b2015-03-30 14:59:19 +0530517 hotplug_cpu__broadcast_tick_pull(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* This actually kills the CPU. */
519 __cpu_die(cpu);
520
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200521 tick_cleanup_dead_cpu(cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000522 return 0;
523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Thomas Gleixner98458172016-02-26 18:43:25 +0000525static int notify_dead(unsigned int cpu)
526{
527 cpu_notify_nofail(CPU_DEAD, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 check_for_tasks(cpu);
Thomas Gleixner98458172016-02-26 18:43:25 +0000529 return 0;
530}
531
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000532#else
533#define notify_down_prepare NULL
534#define takedown_cpu NULL
535#define notify_dead NULL
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000536#define notify_dying NULL
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000537#endif
538
539#ifdef CONFIG_HOTPLUG_CPU
540static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
541{
542 for (st->state++; st->state < st->target; st->state++) {
543 struct cpuhp_step *step = cpuhp_bp_states + st->state;
544
545 if (!step->skip_onerr)
546 cpuhp_invoke_callback(cpu, st->state, step->startup);
547 }
548}
549
Thomas Gleixner98458172016-02-26 18:43:25 +0000550/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000551static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
552 enum cpuhp_state target)
Thomas Gleixner98458172016-02-26 18:43:25 +0000553{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000554 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
555 int prev_state, ret = 0;
556 bool hasdied = false;
Thomas Gleixner98458172016-02-26 18:43:25 +0000557
558 if (num_online_cpus() == 1)
559 return -EBUSY;
560
561 if (!cpu_online(cpu))
562 return -EINVAL;
563
564 cpu_hotplug_begin();
565
566 cpuhp_tasks_frozen = tasks_frozen;
567
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000568 prev_state = st->state;
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000569 st->target = target;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000570 for (; st->state > st->target; st->state--) {
571 struct cpuhp_step *step = cpuhp_bp_states + st->state;
Thomas Gleixner98458172016-02-26 18:43:25 +0000572
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000573 ret = cpuhp_invoke_callback(cpu, st->state, step->teardown);
574 if (ret) {
575 st->target = prev_state;
576 undo_cpu_down(cpu, st);
577 break;
578 }
579 }
580 hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100582 cpu_hotplug_done();
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000583 /* This post dead nonsense must die */
584 if (!ret && hasdied)
Thomas Gleixner090e77c2016-02-26 18:43:23 +0000585 cpu_notify_nofail(CPU_POST_DEAD, cpu);
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000586 return ret;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700587}
588
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000589static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700590{
Heiko Carstens9ea09af2008-12-22 12:36:30 +0100591 int err;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700592
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100593 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700594
Max Krasnyanskye761b772008-07-15 04:43:49 -0700595 if (cpu_hotplug_disabled) {
596 err = -EBUSY;
597 goto out;
598 }
599
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000600 err = _cpu_down(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -0700601
Max Krasnyanskye761b772008-07-15 04:43:49 -0700602out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100603 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return err;
605}
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000606int cpu_down(unsigned int cpu)
607{
608 return do_cpu_down(cpu, CPUHP_OFFLINE);
609}
Zhang Ruib62b8ef2008-04-29 02:35:56 -0400610EXPORT_SYMBOL(cpu_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif /*CONFIG_HOTPLUG_CPU*/
612
Paul E. McKenney00df35f2015-04-12 08:06:55 -0700613/*
614 * Unpark per-CPU smpboot kthreads at CPU-online time.
615 */
616static int smpboot_thread_call(struct notifier_block *nfb,
617 unsigned long action, void *hcpu)
618{
619 int cpu = (long)hcpu;
620
621 switch (action & ~CPU_TASKS_FROZEN) {
622
Paul E. McKenney64eaf9742015-04-15 12:45:41 -0700623 case CPU_DOWN_FAILED:
Paul E. McKenney00df35f2015-04-12 08:06:55 -0700624 case CPU_ONLINE:
625 smpboot_unpark_threads(cpu);
626 break;
627
628 default:
629 break;
630 }
631
632 return NOTIFY_OK;
633}
634
635static struct notifier_block smpboot_thread_notifier = {
636 .notifier_call = smpboot_thread_call,
637 .priority = CPU_PRI_SMPBOOT,
638};
639
Paul Gortmaker927da9d2015-04-27 18:47:58 -0400640void smpboot_thread_init(void)
Paul E. McKenney00df35f2015-04-12 08:06:55 -0700641{
642 register_cpu_notifier(&smpboot_thread_notifier);
643}
644
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000645/**
646 * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
647 * @cpu: cpu that just started
648 *
649 * This function calls the cpu_chain notifiers with CPU_STARTING.
650 * It must be called by the arch code on the new cpu, before the new cpu
651 * enables interrupts and before the "boot" cpu returns from __cpu_up().
652 */
653void notify_cpu_starting(unsigned int cpu)
654{
655 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
656 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
657
658 while (st->state < target) {
659 struct cpuhp_step *step;
660
661 st->state++;
662 step = cpuhp_ap_states + st->state;
663 cpuhp_invoke_callback(cpu, st->state, step->startup);
664 }
665}
666
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000667static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
668{
669 for (st->state--; st->state > st->target; st->state--) {
670 struct cpuhp_step *step = cpuhp_bp_states + st->state;
671
672 if (!step->skip_onerr)
673 cpuhp_invoke_callback(cpu, st->state, step->teardown);
674 }
675}
676
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700677/* Requires cpu_add_remove_lock to be held */
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000678static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000680 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700681 struct task_struct *idle;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000682 int prev_state, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100684 cpu_hotplug_begin();
Thomas Gleixner38498a62012-04-20 13:05:44 +0000685
Yasuaki Ishimatsu5e5041f2012-10-23 01:30:54 +0200686 if (cpu_online(cpu) || !cpu_present(cpu)) {
687 ret = -EINVAL;
688 goto out;
689 }
690
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000691 /* Let it fail before we try to bring the cpu up */
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700692 idle = idle_thread_get(cpu);
693 if (IS_ERR(idle)) {
694 ret = PTR_ERR(idle);
Thomas Gleixner38498a62012-04-20 13:05:44 +0000695 goto out;
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700696 }
Thomas Gleixner38498a62012-04-20 13:05:44 +0000697
Thomas Gleixnerba997462016-02-26 18:43:24 +0000698 cpuhp_tasks_frozen = tasks_frozen;
699
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000700 prev_state = st->state;
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000701 st->target = target;
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000702 while (st->state < st->target) {
703 struct cpuhp_step *step;
Thomas Gleixnerf97f8f02012-07-16 10:42:36 +0000704
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000705 st->state++;
706 step = cpuhp_bp_states + st->state;
707 ret = cpuhp_invoke_callback(cpu, st->state, step->startup);
708 if (ret) {
709 st->target = prev_state;
710 undo_cpu_up(cpu, st);
711 break;
712 }
713 }
Thomas Gleixner38498a62012-04-20 13:05:44 +0000714out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100715 cpu_hotplug_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return ret;
717}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700718
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000719static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700720{
721 int err = 0;
minskey guocf234222010-05-24 14:32:41 -0700722
Rusty Russelle0b582e2009-01-01 10:12:28 +1030723 if (!cpu_possible(cpu)) {
Fabian Frederick84117da2014-06-04 16:11:17 -0700724 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
725 cpu);
Chen Gong87d5e0232010-03-05 13:42:38 -0800726#if defined(CONFIG_IA64)
Fabian Frederick84117da2014-06-04 16:11:17 -0700727 pr_err("please check additional_cpus= boot parameter\n");
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -0700728#endif
729 return -EINVAL;
730 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700731
Toshi Kani01b0f192013-11-12 15:07:25 -0800732 err = try_online_node(cpu_to_node(cpu));
733 if (err)
734 return err;
minskey guocf234222010-05-24 14:32:41 -0700735
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100736 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700737
Max Krasnyanskye761b772008-07-15 04:43:49 -0700738 if (cpu_hotplug_disabled) {
739 err = -EBUSY;
740 goto out;
741 }
742
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000743 err = _cpu_up(cpu, 0, target);
Max Krasnyanskye761b772008-07-15 04:43:49 -0700744out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100745 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700746 return err;
747}
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000748
749int cpu_up(unsigned int cpu)
750{
751 return do_cpu_up(cpu, CPUHP_ONLINE);
752}
Paul E. McKenneya513f6b2011-12-11 21:54:45 -0800753EXPORT_SYMBOL_GPL(cpu_up);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700754
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700755#ifdef CONFIG_PM_SLEEP_SMP
Rusty Russelle0b582e2009-01-01 10:12:28 +1030756static cpumask_var_t frozen_cpus;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700757
758int disable_nonboot_cpus(void)
759{
Rafael J. Wysockie9a5f422010-05-27 22:16:22 +0200760 int cpu, first_cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700761
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100762 cpu_maps_update_begin();
Rusty Russelle0b582e2009-01-01 10:12:28 +1030763 first_cpu = cpumask_first(cpu_online_mask);
Xiaotian Feng9ee349a2009-12-16 18:04:32 +0100764 /*
765 * We take down all of the non-boot CPUs in one shot to avoid races
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700766 * with the userspace trying to use the CPU hotplug at the same time
767 */
Rusty Russelle0b582e2009-01-01 10:12:28 +1030768 cpumask_clear(frozen_cpus);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +0100769
Fabian Frederick84117da2014-06-04 16:11:17 -0700770 pr_info("Disabling non-boot CPUs ...\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700771 for_each_online_cpu(cpu) {
772 if (cpu == first_cpu)
773 continue;
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700774 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000775 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700776 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
Mike Travisfeae3202009-11-17 18:22:13 -0600777 if (!error)
Rusty Russelle0b582e2009-01-01 10:12:28 +1030778 cpumask_set_cpu(cpu, frozen_cpus);
Mike Travisfeae3202009-11-17 18:22:13 -0600779 else {
Fabian Frederick84117da2014-06-04 16:11:17 -0700780 pr_err("Error taking CPU%d down: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700781 break;
782 }
783 }
Joseph Cihula86886e52009-06-30 19:31:07 -0700784
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700785 if (!error)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700786 BUG_ON(num_online_cpus() > 1);
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700787 else
Fabian Frederick84117da2014-06-04 16:11:17 -0700788 pr_err("Non-boot CPUs are not disabled\n");
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700789
790 /*
791 * Make sure the CPUs won't be enabled by someone else. We need to do
792 * this even in case of failure as all disable_nonboot_cpus() users are
793 * supposed to do enable_nonboot_cpus() on the failure path.
794 */
795 cpu_hotplug_disabled++;
796
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100797 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700798 return error;
799}
800
Suresh Siddhad0af9ee2009-08-19 18:05:36 -0700801void __weak arch_enable_nonboot_cpus_begin(void)
802{
803}
804
805void __weak arch_enable_nonboot_cpus_end(void)
806{
807}
808
Mathias Krause71cf5ae2015-07-19 20:06:22 +0200809void enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700810{
811 int cpu, error;
812
813 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100814 cpu_maps_update_begin();
Vitaly Kuznetsov89af7ba2015-08-05 00:52:46 -0700815 WARN_ON(--cpu_hotplug_disabled < 0);
Rusty Russelle0b582e2009-01-01 10:12:28 +1030816 if (cpumask_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700817 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700818
Fabian Frederick84117da2014-06-04 16:11:17 -0700819 pr_info("Enabling non-boot CPUs ...\n");
Suresh Siddhad0af9ee2009-08-19 18:05:36 -0700820
821 arch_enable_nonboot_cpus_begin();
822
Rusty Russelle0b582e2009-01-01 10:12:28 +1030823 for_each_cpu(cpu, frozen_cpus) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700824 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
Thomas Gleixneraf1f4042016-02-26 18:43:30 +0000825 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700826 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700827 if (!error) {
Fabian Frederick84117da2014-06-04 16:11:17 -0700828 pr_info("CPU%d is up\n", cpu);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700829 continue;
830 }
Fabian Frederick84117da2014-06-04 16:11:17 -0700831 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700832 }
Suresh Siddhad0af9ee2009-08-19 18:05:36 -0700833
834 arch_enable_nonboot_cpus_end();
835
Rusty Russelle0b582e2009-01-01 10:12:28 +1030836 cpumask_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700837out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100838 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700839}
Rusty Russelle0b582e2009-01-01 10:12:28 +1030840
Fenghua Yud7268a32011-11-15 21:59:31 +0100841static int __init alloc_frozen_cpus(void)
Rusty Russelle0b582e2009-01-01 10:12:28 +1030842{
843 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
844 return -ENOMEM;
845 return 0;
846}
847core_initcall(alloc_frozen_cpus);
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +0100848
849/*
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +0100850 * When callbacks for CPU hotplug notifications are being executed, we must
851 * ensure that the state of the system with respect to the tasks being frozen
852 * or not, as reported by the notification, remains unchanged *throughout the
853 * duration* of the execution of the callbacks.
854 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
855 *
856 * This synchronization is implemented by mutually excluding regular CPU
857 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
858 * Hibernate notifications.
859 */
860static int
861cpu_hotplug_pm_callback(struct notifier_block *nb,
862 unsigned long action, void *ptr)
863{
864 switch (action) {
865
866 case PM_SUSPEND_PREPARE:
867 case PM_HIBERNATION_PREPARE:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700868 cpu_hotplug_disable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +0100869 break;
870
871 case PM_POST_SUSPEND:
872 case PM_POST_HIBERNATION:
Srivatsa S. Bhat16e53db2013-06-12 14:04:36 -0700873 cpu_hotplug_enable();
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +0100874 break;
875
876 default:
877 return NOTIFY_DONE;
878 }
879
880 return NOTIFY_OK;
881}
882
883
Fenghua Yud7268a32011-11-15 21:59:31 +0100884static int __init cpu_hotplug_pm_sync_init(void)
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +0100885{
Fenghua Yu6e32d472012-11-13 11:32:43 -0800886 /*
887 * cpu_hotplug_pm_callback has higher priority than x86
888 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
889 * to disable cpu hotplug to avoid cpu hotplug race.
890 */
Srivatsa S. Bhat79cfbdf2011-11-03 00:59:25 +0100891 pm_notifier(cpu_hotplug_pm_callback, 0);
892 return 0;
893}
894core_initcall(cpu_hotplug_pm_sync_init);
895
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700896#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -0700897
898#endif /* CONFIG_SMP */
Mike Travisb8d317d2008-07-24 18:21:29 -0700899
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000900/* Boot processor state steps */
901static struct cpuhp_step cpuhp_bp_states[] = {
902 [CPUHP_OFFLINE] = {
903 .name = "offline",
904 .startup = NULL,
905 .teardown = NULL,
906 },
907#ifdef CONFIG_SMP
908 [CPUHP_CREATE_THREADS]= {
909 .name = "threads:create",
910 .startup = smpboot_create_threads,
911 .teardown = NULL,
912 },
913 [CPUHP_NOTIFY_PREPARE] = {
914 .name = "notify:prepare",
915 .startup = notify_prepare,
916 .teardown = notify_dead,
917 .skip_onerr = true,
918 },
919 [CPUHP_BRINGUP_CPU] = {
920 .name = "cpu:bringup",
921 .startup = bringup_cpu,
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000922 .teardown = NULL,
923 },
924 [CPUHP_TEARDOWN_CPU] = {
925 .name = "cpu:teardown",
926 .startup = NULL,
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000927 .teardown = takedown_cpu,
Thomas Gleixnercff7d372016-02-26 18:43:28 +0000928 },
929 [CPUHP_NOTIFY_ONLINE] = {
930 .name = "notify:online",
931 .startup = notify_online,
932 .teardown = notify_down_prepare,
933 },
934#endif
935 [CPUHP_ONLINE] = {
936 .name = "online",
937 .startup = NULL,
938 .teardown = NULL,
939 },
940};
941
Thomas Gleixner4baa0af2016-02-26 18:43:29 +0000942/* Application processor state steps */
943static struct cpuhp_step cpuhp_ap_states[] = {
944#ifdef CONFIG_SMP
945 [CPUHP_AP_NOTIFY_STARTING] = {
946 .name = "notify:starting",
947 .startup = notify_starting,
948 .teardown = notify_dying,
949 .skip_onerr = true,
950 },
951#endif
952 [CPUHP_ONLINE] = {
953 .name = "online",
954 .startup = NULL,
955 .teardown = NULL,
956 },
957};
958
Thomas Gleixner98f8cdc2016-02-26 18:43:31 +0000959static bool cpuhp_is_ap_state(enum cpuhp_state state)
960{
961 return (state > CPUHP_AP_OFFLINE && state < CPUHP_AP_ONLINE);
962}
963
964static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
965{
966 struct cpuhp_step *sp;
967
968 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
969 return sp + state;
970}
971
972#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
973static ssize_t show_cpuhp_state(struct device *dev,
974 struct device_attribute *attr, char *buf)
975{
976 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
977
978 return sprintf(buf, "%d\n", st->state);
979}
980static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
981
982static ssize_t show_cpuhp_target(struct device *dev,
983 struct device_attribute *attr, char *buf)
984{
985 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
986
987 return sprintf(buf, "%d\n", st->target);
988}
989static DEVICE_ATTR(target, 0444, show_cpuhp_target, NULL);
990
991static struct attribute *cpuhp_cpu_attrs[] = {
992 &dev_attr_state.attr,
993 &dev_attr_target.attr,
994 NULL
995};
996
997static struct attribute_group cpuhp_cpu_attr_group = {
998 .attrs = cpuhp_cpu_attrs,
999 .name = "hotplug",
1000 NULL
1001};
1002
1003static ssize_t show_cpuhp_states(struct device *dev,
1004 struct device_attribute *attr, char *buf)
1005{
1006 ssize_t cur, res = 0;
1007 int i;
1008
1009 mutex_lock(&cpuhp_state_mutex);
1010 for (i = 0; i <= CPUHP_ONLINE; i++) {
1011 struct cpuhp_step *sp = cpuhp_get_step(i);
1012
1013 if (sp->name) {
1014 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
1015 buf += cur;
1016 res += cur;
1017 }
1018 }
1019 mutex_unlock(&cpuhp_state_mutex);
1020 return res;
1021}
1022static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
1023
1024static struct attribute *cpuhp_cpu_root_attrs[] = {
1025 &dev_attr_states.attr,
1026 NULL
1027};
1028
1029static struct attribute_group cpuhp_cpu_root_attr_group = {
1030 .attrs = cpuhp_cpu_root_attrs,
1031 .name = "hotplug",
1032 NULL
1033};
1034
1035static int __init cpuhp_sysfs_init(void)
1036{
1037 int cpu, ret;
1038
1039 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
1040 &cpuhp_cpu_root_attr_group);
1041 if (ret)
1042 return ret;
1043
1044 for_each_possible_cpu(cpu) {
1045 struct device *dev = get_cpu_device(cpu);
1046
1047 if (!dev)
1048 continue;
1049 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
1050 if (ret)
1051 return ret;
1052 }
1053 return 0;
1054}
1055device_initcall(cpuhp_sysfs_init);
1056#endif
1057
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001058/*
1059 * cpu_bit_bitmap[] is a special, "compressed" data structure that
1060 * represents all NR_CPUS bits binary values of 1<<nr.
1061 *
Rusty Russelle0b582e2009-01-01 10:12:28 +10301062 * It is used by cpumask_of() to get a constant address to a CPU
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001063 * mask value that has a single bit set only.
1064 */
Mike Travisb8d317d2008-07-24 18:21:29 -07001065
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001066/* cpu_bit_bitmap[0] is empty - so we can back into it */
Michael Rodriguez4d519852011-03-22 16:34:07 -07001067#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001068#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
1069#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
1070#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
Mike Travisb8d317d2008-07-24 18:21:29 -07001071
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001072const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
Mike Travisb8d317d2008-07-24 18:21:29 -07001073
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001074 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
1075 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
1076#if BITS_PER_LONG > 32
1077 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
1078 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
Mike Travisb8d317d2008-07-24 18:21:29 -07001079#endif
1080};
Linus Torvaldse56b3bc2008-07-28 11:32:33 -07001081EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
Rusty Russell2d3854a2008-11-05 13:39:10 +11001082
1083const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
1084EXPORT_SYMBOL(cpu_all_bits);
Rusty Russellb3199c02008-12-30 09:05:14 +10301085
1086#ifdef CONFIG_INIT_ALL_POSSIBLE
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001087struct cpumask __cpu_possible_mask __read_mostly
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001088 = {CPU_BITS_ALL};
Rusty Russellb3199c02008-12-30 09:05:14 +10301089#else
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001090struct cpumask __cpu_possible_mask __read_mostly;
Rusty Russellb3199c02008-12-30 09:05:14 +10301091#endif
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001092EXPORT_SYMBOL(__cpu_possible_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301093
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001094struct cpumask __cpu_online_mask __read_mostly;
1095EXPORT_SYMBOL(__cpu_online_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301096
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001097struct cpumask __cpu_present_mask __read_mostly;
1098EXPORT_SYMBOL(__cpu_present_mask);
Rusty Russellb3199c02008-12-30 09:05:14 +10301099
Rasmus Villemoes4b804c82016-01-20 15:00:19 -08001100struct cpumask __cpu_active_mask __read_mostly;
1101EXPORT_SYMBOL(__cpu_active_mask);
Rusty Russell3fa41522008-12-30 09:05:16 +10301102
Rusty Russell3fa41522008-12-30 09:05:16 +10301103void init_cpu_present(const struct cpumask *src)
1104{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001105 cpumask_copy(&__cpu_present_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301106}
1107
1108void init_cpu_possible(const struct cpumask *src)
1109{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001110 cpumask_copy(&__cpu_possible_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301111}
1112
1113void init_cpu_online(const struct cpumask *src)
1114{
Rasmus Villemoesc4c54dd2016-01-20 15:00:16 -08001115 cpumask_copy(&__cpu_online_mask, src);
Rusty Russell3fa41522008-12-30 09:05:16 +10301116}
Thomas Gleixnercff7d372016-02-26 18:43:28 +00001117
1118/*
1119 * Activate the first processor.
1120 */
1121void __init boot_cpu_init(void)
1122{
1123 int cpu = smp_processor_id();
1124
1125 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
1126 set_cpu_online(cpu, true);
1127 set_cpu_active(cpu, true);
1128 set_cpu_present(cpu, true);
1129 set_cpu_possible(cpu, true);
1130}
1131
1132/*
1133 * Must be called _AFTER_ setting up the per_cpu areas
1134 */
1135void __init boot_cpu_state_init(void)
1136{
1137 per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
1138}