Blame view

kernel/cpu.c 54.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /* CPU control.
   * (C) 2001, 2002, 2003, 2004 Rusty Russell
   *
   * This code is licenced under the GPL.
   */
  #include <linux/proc_fs.h>
  #include <linux/smp.h>
  #include <linux/init.h>
  #include <linux/notifier.h>
3f07c0144   Ingo Molnar   sched/headers: Pr...
10
  #include <linux/sched/signal.h>
ef8bd77f3   Ingo Molnar   sched/headers: Pr...
11
  #include <linux/sched/hotplug.h>
299300258   Ingo Molnar   sched/headers: Pr...
12
  #include <linux/sched/task.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
  #include <linux/unistd.h>
  #include <linux/cpu.h>
cb79295e2   Anton Vorontsov   cpu: introduce cl...
15
16
  #include <linux/oom.h>
  #include <linux/rcupdate.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
17
  #include <linux/export.h>
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
18
  #include <linux/bug.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
  #include <linux/kthread.h>
  #include <linux/stop_machine.h>
81615b624   Ingo Molnar   [PATCH] Convert k...
21
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/gfp.h>
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
23
  #include <linux/suspend.h>
a19423b98   Gautham R. Shenoy   CPU hotplug: Add ...
24
  #include <linux/lockdep.h>
345527b1e   Preeti U Murthy   clockevents: Fix ...
25
  #include <linux/tick.h>
a89941816   Thomas Gleixner   hotplug: Prevent ...
26
  #include <linux/irq.h>
941154bd6   Thomas Gleixner   watchdog/hardlock...
27
  #include <linux/nmi.h>
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
28
  #include <linux/smpboot.h>
e6d4989a9   Richard Weinberger   relayfs: Convert ...
29
  #include <linux/relay.h>
6731d4f12   Sebastian Andrzej Siewior   slab: Convert to ...
30
  #include <linux/slab.h>
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
31
  #include <linux/percpu-rwsem.h>
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
32

bb3632c61   Todd E Brandt   PM / sleep: trace...
33
  #include <trace/events/power.h>
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
34
35
  #define CREATE_TRACE_POINTS
  #include <trace/events/cpuhp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36

38498a67a   Thomas Gleixner   smp: Add generic ...
37
  #include "smpboot.h"
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
38
39
40
41
  /**
   * cpuhp_cpu_state - Per cpu hotplug state storage
   * @state:	The current cpu state
   * @target:	The target state
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
42
43
   * @thread:	Pointer to the hotplug thread
   * @should_run:	Thread should execute
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
44
   * @rollback:	Perform a rollback
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
45
46
47
   * @single:	Single callback invocation
   * @bringup:	Single callback bringup or teardown selector
   * @cb_state:	The state for a single callback (install/uninstall)
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
48
   * @result:	Result of the operation
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
49
50
   * @done_up:	Signal completion to the issuer of the task for cpu-up
   * @done_down:	Signal completion to the issuer of the task for cpu-down
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
51
52
53
54
   */
  struct cpuhp_cpu_state {
  	enum cpuhp_state	state;
  	enum cpuhp_state	target;
1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
55
  	enum cpuhp_state	fail;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
56
57
58
  #ifdef CONFIG_SMP
  	struct task_struct	*thread;
  	bool			should_run;
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
59
  	bool			rollback;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
60
61
  	bool			single;
  	bool			bringup;
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
62
  	bool			booted_once;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
63
  	struct hlist_node	*node;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
64
  	struct hlist_node	*last;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
65
  	enum cpuhp_state	cb_state;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
66
  	int			result;
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
67
68
  	struct completion	done_up;
  	struct completion	done_down;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
69
  #endif
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
70
  };
1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
71
72
73
  static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
  	.fail = CPUHP_INVALID,
  };
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
74

49dfe2a67   Thomas Gleixner   cpuhotplug: Link ...
75
  #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  static struct lockdep_map cpuhp_state_up_map =
  	STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
  static struct lockdep_map cpuhp_state_down_map =
  	STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
  
  
  static void inline cpuhp_lock_acquire(bool bringup)
  {
  	lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
  }
  
  static void inline cpuhp_lock_release(bool bringup)
  {
  	lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
  }
  #else
  
  static void inline cpuhp_lock_acquire(bool bringup) { }
  static void inline cpuhp_lock_release(bool bringup) { }
49dfe2a67   Thomas Gleixner   cpuhotplug: Link ...
95
  #endif
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
96
97
98
99
100
101
102
  /**
   * cpuhp_step - Hotplug state machine step
   * @name:	Name of the step
   * @startup:	Startup function of the step
   * @teardown:	Teardown function of the step
   * @skip_onerr:	Do not invoke the functions on error rollback
   *		Will go away once the notifiers	are gone
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
103
   * @cant_stop:	Bringup/teardown can't be stopped at this step
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
104
105
   */
  struct cpuhp_step {
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
106
107
  	const char		*name;
  	union {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
108
109
110
111
  		int		(*single)(unsigned int cpu);
  		int		(*multi)(unsigned int cpu,
  					 struct hlist_node *node);
  	} startup;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
112
  	union {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
113
114
115
116
  		int		(*single)(unsigned int cpu);
  		int		(*multi)(unsigned int cpu,
  					 struct hlist_node *node);
  	} teardown;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
117
118
119
120
  	struct hlist_head	list;
  	bool			skip_onerr;
  	bool			cant_stop;
  	bool			multi_instance;
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
121
  };
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
122
  static DEFINE_MUTEX(cpuhp_state_mutex);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
123
  static struct cpuhp_step cpuhp_bp_states[];
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
124
  static struct cpuhp_step cpuhp_ap_states[];
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
125

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  static bool cpuhp_is_ap_state(enum cpuhp_state state)
  {
  	/*
  	 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  	 * purposes as that state is handled explicitly in cpu_down.
  	 */
  	return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  }
  
  static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  {
  	struct cpuhp_step *sp;
  
  	sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
  	return sp + state;
  }
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
142
143
144
  /**
   * cpuhp_invoke_callback _ Invoke the callbacks for a given state
   * @cpu:	The cpu for which the callback should be invoked
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
145
   * @state:	The state to do callbacks for
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
146
   * @bringup:	True if the bringup callback should be invoked
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
147
148
   * @node:	For multi-instance, do a single entry callback for install/remove
   * @lastp:	For multi-instance rollback, remember how far we got
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
149
   *
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
150
   * Called from cpu hotplug and from the state register machinery.
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
151
   */
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
152
  static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
153
154
  				 bool bringup, struct hlist_node *node,
  				 struct hlist_node **lastp)
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
155
156
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
157
  	struct cpuhp_step *step = cpuhp_get_step(state);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
158
159
160
  	int (*cbm)(unsigned int cpu, struct hlist_node *node);
  	int (*cb)(unsigned int cpu);
  	int ret, cnt;
1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
161
162
163
164
165
166
167
168
  	if (st->fail == state) {
  		st->fail = CPUHP_INVALID;
  
  		if (!(bringup ? step->startup.single : step->teardown.single))
  			return 0;
  
  		return -EAGAIN;
  	}
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
169
  	if (!step->multi_instance) {
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
170
  		WARN_ON_ONCE(lastp && *lastp);
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
171
  		cb = bringup ? step->startup.single : step->teardown.single;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
172
173
  		if (!cb)
  			return 0;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
174
  		trace_cpuhp_enter(cpu, st->target, state, cb);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
175
  		ret = cb(cpu);
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
176
  		trace_cpuhp_exit(cpu, st->state, state, ret);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
177
178
  		return ret;
  	}
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
179
  	cbm = bringup ? step->startup.multi : step->teardown.multi;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
180
181
182
183
184
  	if (!cbm)
  		return 0;
  
  	/* Single invocation for instance add/remove */
  	if (node) {
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
185
  		WARN_ON_ONCE(lastp && *lastp);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
186
187
188
189
190
191
192
193
194
  		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  		ret = cbm(cpu, node);
  		trace_cpuhp_exit(cpu, st->state, state, ret);
  		return ret;
  	}
  
  	/* State transition. Invoke on all instances */
  	cnt = 0;
  	hlist_for_each(node, &step->list) {
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
195
196
  		if (lastp && node == *lastp)
  			break;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
197
198
199
  		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  		ret = cbm(cpu, node);
  		trace_cpuhp_exit(cpu, st->state, state, ret);
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
200
201
202
203
204
205
206
  		if (ret) {
  			if (!lastp)
  				goto err;
  
  			*lastp = node;
  			return ret;
  		}
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
207
208
  		cnt++;
  	}
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
209
210
  	if (lastp)
  		*lastp = NULL;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
211
212
213
  	return 0;
  err:
  	/* Rollback the instances if one failed */
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
214
  	cbm = !bringup ? step->startup.multi : step->teardown.multi;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
215
216
217
218
219
220
  	if (!cbm)
  		return ret;
  
  	hlist_for_each(node, &step->list) {
  		if (!cnt--)
  			break;
724a86881   Peter Zijlstra   smp/hotplug: Call...
221
222
223
224
225
226
227
228
  
  		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  		ret = cbm(cpu, node);
  		trace_cpuhp_exit(cpu, st->state, state, ret);
  		/*
  		 * Rollback must not fail,
  		 */
  		WARN_ON_ONCE(ret);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
229
230
231
  	}
  	return ret;
  }
98a79d6a5   Rusty Russell   cpumask: centrali...
232
  #ifdef CONFIG_SMP
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
  {
  	struct completion *done = bringup ? &st->done_up : &st->done_down;
  	wait_for_completion(done);
  }
  
  static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
  {
  	struct completion *done = bringup ? &st->done_up : &st->done_down;
  	complete(done);
  }
  
  /*
   * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
   */
  static bool cpuhp_is_atomic_state(enum cpuhp_state state)
  {
  	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
  }
b3199c025   Rusty Russell   cpumask: switch o...
252
  /* Serializes the updates to cpu_online_mask, cpu_present_mask */
aa9538777   Linus Torvalds   cpu hotplug: simp...
253
  static DEFINE_MUTEX(cpu_add_remove_lock);
090e77c39   Thomas Gleixner   cpu/hotplug: Rest...
254
255
  bool cpuhp_tasks_frozen;
  EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256

79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
257
  /*
93ae4f978   Srivatsa S. Bhat   CPU hotplug: Prov...
258
259
   * The following two APIs (cpu_maps_update_begin/done) must be used when
   * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
260
261
262
263
264
265
266
267
268
269
   */
  void cpu_maps_update_begin(void)
  {
  	mutex_lock(&cpu_add_remove_lock);
  }
  
  void cpu_maps_update_done(void)
  {
  	mutex_unlock(&cpu_add_remove_lock);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270

fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
271
272
  /*
   * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
273
274
275
   * Should always be manipulated under cpu_add_remove_lock
   */
  static int cpu_hotplug_disabled;
79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
276
  #ifdef CONFIG_HOTPLUG_CPU
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
277
  DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
a19423b98   Gautham R. Shenoy   CPU hotplug: Add ...
278

8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
279
  void cpus_read_lock(void)
a9d9baa1e   Ashok Raj   [PATCH] clean up ...
280
  {
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
281
  	percpu_down_read(&cpu_hotplug_lock);
a9d9baa1e   Ashok Raj   [PATCH] clean up ...
282
  }
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
283
  EXPORT_SYMBOL_GPL(cpus_read_lock);
90d45d17f   Ashok Raj   [PATCH] cpu hotpl...
284

8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
285
  void cpus_read_unlock(void)
a9d9baa1e   Ashok Raj   [PATCH] clean up ...
286
  {
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
287
  	percpu_up_read(&cpu_hotplug_lock);
a9d9baa1e   Ashok Raj   [PATCH] clean up ...
288
  }
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
289
  EXPORT_SYMBOL_GPL(cpus_read_unlock);
a9d9baa1e   Ashok Raj   [PATCH] clean up ...
290

8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
291
  void cpus_write_lock(void)
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
292
  {
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
293
  	percpu_down_write(&cpu_hotplug_lock);
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
294
  }
87af9e7ff   David Hildenbrand   hotplugcpu: Avoid...
295

8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
296
  void cpus_write_unlock(void)
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
297
  {
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
298
  	percpu_up_write(&cpu_hotplug_lock);
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
299
  }
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
300
  void lockdep_assert_cpus_held(void)
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
301
  {
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
302
  	percpu_rwsem_assert_held(&cpu_hotplug_lock);
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
303
  }
79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
304

16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
305
306
307
308
309
310
311
312
313
314
  /*
   * Wait for currently running CPU hotplug operations to complete (if any) and
   * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
   * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
   * hotplug path before performing hotplug operations. So acquiring that lock
   * guarantees mutual exclusion from any currently running hotplug operations.
   */
  void cpu_hotplug_disable(void)
  {
  	cpu_maps_update_begin();
89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
315
  	cpu_hotplug_disabled++;
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
316
317
  	cpu_maps_update_done();
  }
32145c467   Vitaly Kuznetsov   cpu-hotplug: expo...
318
  EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
319

01b411590   Lianwei Wang   cpu/hotplug: Hand...
320
321
322
323
324
325
326
  static void __cpu_hotplug_enable(void)
  {
  	if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable
  "))
  		return;
  	cpu_hotplug_disabled--;
  }
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
327
328
329
  void cpu_hotplug_enable(void)
  {
  	cpu_maps_update_begin();
01b411590   Lianwei Wang   cpu/hotplug: Hand...
330
  	__cpu_hotplug_enable();
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
331
332
  	cpu_maps_update_done();
  }
32145c467   Vitaly Kuznetsov   cpu-hotplug: expo...
333
  EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
b9d10be7a   Toshi Kani   ACPI / processor:...
334
  #endif	/* CONFIG_HOTPLUG_CPU */
79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
335

7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
336
337
  #ifdef CONFIG_HOTPLUG_SMT
  enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
c2fdbbb47   Konrad Rzeszutek Wilk   x86/KVM: Warn use...
338
  EXPORT_SYMBOL_GPL(cpu_smt_control);
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
339

9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
340
  static bool cpu_smt_available __read_mostly;
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
341
  void __init cpu_smt_disable(bool force)
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
342
  {
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
343
344
345
346
347
  	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  		cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  		return;
  
  	if (force) {
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
348
349
350
  		pr_info("SMT: Force disabled
  ");
  		cpu_smt_control = CPU_SMT_FORCE_DISABLED;
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
351
352
  	} else {
  		cpu_smt_control = CPU_SMT_DISABLED;
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
353
  	}
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
354
  }
476d29ab7   Thomas Gleixner   cpu/hotplug: Set ...
355
356
  /*
   * The decision whether SMT is supported can only be done after the full
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
357
358
   * CPU identification. Called from architecture code before non boot CPUs
   * are brought up.
476d29ab7   Thomas Gleixner   cpu/hotplug: Set ...
359
   */
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
360
  void __init cpu_smt_check_topology_early(void)
476d29ab7   Thomas Gleixner   cpu/hotplug: Set ...
361
362
363
364
  {
  	if (!topology_smt_supported())
  		cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  }
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
365
366
367
368
369
370
371
372
373
374
375
376
  /*
   * If SMT was disabled by BIOS, detect it here, after the CPUs have been
   * brought online. This ensures the smt/l1tf sysfs entries are consistent
   * with reality. cpu_smt_available is set to true during the bringup of non
   * boot CPUs when a SMT sibling is detected. Note, this may overwrite
   * cpu_smt_control's previous setting.
   */
  void __init cpu_smt_check_topology(void)
  {
  	if (!cpu_smt_available)
  		cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  }
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
377
378
379
  static int __init smt_cmdline_disable(char *str)
  {
  	cpu_smt_disable(str && !strcmp(str, "force"));
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
380
381
382
383
384
385
  	return 0;
  }
  early_param("nosmt", smt_cmdline_disable);
  
  static inline bool cpu_smt_allowed(unsigned int cpu)
  {
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
386
  	if (topology_is_primary_thread(cpu))
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
387
  		return true;
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
388
389
390
391
392
393
394
395
396
  	/*
  	 * If the CPU is not a 'primary' thread and the booted_once bit is
  	 * set then the processor has SMT support. Store this information
  	 * for the late check of SMT support in cpu_smt_check_topology().
  	 */
  	if (per_cpu(cpuhp_state, cpu).booted_once)
  		cpu_smt_available = true;
  
  	if (cpu_smt_control == CPU_SMT_ENABLED)
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
397
398
399
400
401
402
403
404
405
406
407
408
409
  		return true;
  
  	/*
  	 * On x86 it's required to boot all logical CPUs at least once so
  	 * that the init code can get a chance to set CR4.MCE on each
  	 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
  	 * core will shutdown the machine.
  	 */
  	return !per_cpu(cpuhp_state, cpu).booted_once;
  }
  #else
  static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
  #endif
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  static inline enum cpuhp_state
  cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
  {
  	enum cpuhp_state prev_state = st->state;
  
  	st->rollback = false;
  	st->last = NULL;
  
  	st->target = target;
  	st->single = false;
  	st->bringup = st->state < target;
  
  	return prev_state;
  }
  
  static inline void
  cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
  {
  	st->rollback = true;
  
  	/*
  	 * If we have st->last we need to undo partial multi_instance of this
  	 * state first. Otherwise start undo at the previous state.
  	 */
  	if (!st->last) {
  		if (st->bringup)
  			st->state--;
  		else
  			st->state++;
  	}
  
  	st->target = prev_state;
  	st->bringup = !st->bringup;
  }
  
  /* Regular hotplug invocation of the AP hotplug thread */
  static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
  {
  	if (!st->single && st->state == st->target)
  		return;
  
  	st->result = 0;
  	/*
  	 * Make sure the above stores are visible before should_run becomes
  	 * true. Paired with the mb() above in cpuhp_thread_fun()
  	 */
  	smp_mb();
  	st->should_run = true;
  	wake_up_process(st->thread);
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
459
  	wait_for_ap_thread(st, st->bringup);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  }
  
  static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
  {
  	enum cpuhp_state prev_state;
  	int ret;
  
  	prev_state = cpuhp_set_state(st, target);
  	__cpuhp_kick_ap(st);
  	if ((ret = st->result)) {
  		cpuhp_reset_state(st, prev_state);
  		__cpuhp_kick_ap(st);
  	}
  
  	return ret;
  }
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
476

8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
477
478
479
  static int bringup_wait_for_ap(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
480
  	/* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
481
  	wait_for_ap_thread(st, true);
dea1d0f5f   Thomas Gleixner   smp/hotplug: Repl...
482
483
  	if (WARN_ON_ONCE((!cpu_online(cpu))))
  		return -ECANCELED;
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
484
485
486
487
  
  	/* Unpark the stopper thread and the hotplug thread of the target cpu */
  	stop_machine_unpark(cpu);
  	kthread_unpark(st->thread);
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
488
489
490
491
492
493
494
495
496
  	/*
  	 * SMT soft disabling on X86 requires to bring the CPU out of the
  	 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit.  The
  	 * CPU marked itself as booted_once in cpu_notify_starting() so the
  	 * cpu_smt_allowed() check will now return false if this is not the
  	 * primary sibling.
  	 */
  	if (!cpu_smt_allowed(cpu))
  		return -ECANCELED;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
497
498
499
500
  	if (st->target <= CPUHP_AP_ONLINE_IDLE)
  		return 0;
  
  	return cpuhp_kick_ap(st, st->target);
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
501
  }
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
502
503
504
505
  static int bringup_cpu(unsigned int cpu)
  {
  	struct task_struct *idle = idle_thread_get(cpu);
  	int ret;
aa877175e   Boris Ostrovsky   cpu/hotplug: Prev...
506
507
508
509
510
511
  	/*
  	 * Some architectures have to walk the irq descriptors to
  	 * setup the vector space for the cpu which comes online.
  	 * Prevent irq alloc/free across the bringup.
  	 */
  	irq_lock_sparse();
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
512
513
  	/* Arch-specific enabling code. */
  	ret = __cpu_up(cpu, idle);
aa877175e   Boris Ostrovsky   cpu/hotplug: Prev...
514
  	irq_unlock_sparse();
530e9b76a   Thomas Gleixner   cpu/hotplug: Remo...
515
  	if (ret)
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
516
  		return ret;
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
517
  	return bringup_wait_for_ap(cpu);
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
518
  }
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
519
520
521
  /*
   * Hotplug state machine related functions
   */
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
522

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
523
  static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
524
525
  {
  	for (st->state--; st->state > st->target; st->state--) {
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
526
  		struct cpuhp_step *step = cpuhp_get_step(st->state);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
527
528
  
  		if (!step->skip_onerr)
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
529
  			cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
530
531
532
533
  	}
  }
  
  static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
534
  			      enum cpuhp_state target)
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
535
536
537
538
539
  {
  	enum cpuhp_state prev_state = st->state;
  	int ret = 0;
  
  	while (st->state < target) {
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
540
  		st->state++;
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
541
  		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
542
543
  		if (ret) {
  			st->target = prev_state;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
544
  			undo_cpu_up(cpu, st);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
545
546
547
548
549
  			break;
  		}
  	}
  	return ret;
  }
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
550
551
552
553
554
555
  /*
   * The cpu hotplug threads manage the bringup and teardown of the cpus
   */
  static void cpuhp_create(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
556
557
  	init_completion(&st->done_up);
  	init_completion(&st->done_down);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
558
559
560
561
562
563
564
565
  }
  
  static int cpuhp_should_run(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  
  	return st->should_run;
  }
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
566
567
568
  /*
   * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
   * callbacks when a state gets [un]installed at runtime.
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
569
570
571
572
573
574
575
576
577
578
   *
   * Each invocation of this function by the smpboot thread does a single AP
   * state callback.
   *
   * It has 3 modes of operation:
   *  - single: runs st->cb_state
   *  - up:     runs ++st->state, while st->state < st->target
   *  - down:   runs st->state--, while st->state > st->target
   *
   * When complete or on error, should_run is cleared and the completion is fired.
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
579
580
581
582
   */
  static void cpuhp_thread_fun(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
583
584
  	bool bringup = st->bringup;
  	enum cpuhp_state state;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
585

cb2625854   Neeraj Upadhyay   cpu/hotplug: Adju...
586
587
  	if (WARN_ON_ONCE(!st->should_run))
  		return;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
588
  	/*
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
589
590
  	 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
  	 * that if we see ->should_run we also see the rest of the state.
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
591
592
  	 */
  	smp_mb();
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
593

5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
594
  	cpuhp_lock_acquire(bringup);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
595

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
596
  	if (st->single) {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
597
598
599
600
601
602
603
604
  		state = st->cb_state;
  		st->should_run = false;
  	} else {
  		if (bringup) {
  			st->state++;
  			state = st->state;
  			st->should_run = (st->state < st->target);
  			WARN_ON_ONCE(st->state > st->target);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
605
  		} else {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
606
607
608
609
  			state = st->state;
  			st->state--;
  			st->should_run = (st->state > st->target);
  			WARN_ON_ONCE(st->state < st->target);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
610
  		}
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  	}
  
  	WARN_ON_ONCE(!cpuhp_is_ap_state(state));
  
  	if (st->rollback) {
  		struct cpuhp_step *step = cpuhp_get_step(state);
  		if (step->skip_onerr)
  			goto next;
  	}
  
  	if (cpuhp_is_atomic_state(state)) {
  		local_irq_disable();
  		st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
  		local_irq_enable();
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
625

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
626
627
628
629
  		/*
  		 * STARTING/DYING must not fail!
  		 */
  		WARN_ON_ONCE(st->result);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
630
  	} else {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
631
632
633
634
635
636
637
638
639
640
641
  		st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
  	}
  
  	if (st->result) {
  		/*
  		 * If we fail on a rollback, we're up a creek without no
  		 * paddle, no way forward, no way back. We loose, thanks for
  		 * playing.
  		 */
  		WARN_ON_ONCE(st->rollback);
  		st->should_run = false;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
642
  	}
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
643
644
  
  next:
5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
645
  	cpuhp_lock_release(bringup);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
646
647
  
  	if (!st->should_run)
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
648
  		complete_ap_thread(st, bringup);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
649
650
651
  }
  
  /* Invoke a single callback on a remote cpu */
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
652
  static int
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
653
654
  cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
  			 struct hlist_node *node)
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
655
656
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
657
  	int ret;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
658
659
660
  
  	if (!cpu_online(cpu))
  		return 0;
5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
661
662
663
664
665
  	cpuhp_lock_acquire(false);
  	cpuhp_lock_release(false);
  
  	cpuhp_lock_acquire(true);
  	cpuhp_lock_release(true);
49dfe2a67   Thomas Gleixner   cpuhotplug: Link ...
666

6a4e24518   Thomas Gleixner   cpu/hotplug: Hand...
667
668
669
670
671
  	/*
  	 * If we are up and running, use the hotplug thread. For early calls
  	 * we invoke the thread function directly.
  	 */
  	if (!st->thread)
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
672
  		return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
6a4e24518   Thomas Gleixner   cpu/hotplug: Hand...
673

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
674
675
676
677
678
  	st->rollback = false;
  	st->last = NULL;
  
  	st->node = node;
  	st->bringup = bringup;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
679
  	st->cb_state = state;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
680
  	st->single = true;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
681

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
682
  	__cpuhp_kick_ap(st);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
683

4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
684
  	/*
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
685
  	 * If we failed and did a partial, do a rollback.
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
686
  	 */
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
687
688
689
690
691
692
  	if ((ret = st->result) && st->last) {
  		st->rollback = true;
  		st->bringup = !bringup;
  
  		__cpuhp_kick_ap(st);
  	}
1f7c70d6b   Thomas Gleixner   cpu/hotplug: Rese...
693
694
695
696
697
  	/*
  	 * Clean up the leftovers so the next hotplug operation wont use stale
  	 * data.
  	 */
  	st->node = st->last = NULL;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
698
  	return ret;
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
699
700
701
702
703
  }
  
  static int cpuhp_kick_ap_work(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
704
705
  	enum cpuhp_state prev_state = st->state;
  	int ret;
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
706

5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
707
708
709
710
711
  	cpuhp_lock_acquire(false);
  	cpuhp_lock_release(false);
  
  	cpuhp_lock_acquire(true);
  	cpuhp_lock_release(true);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
712
713
714
715
716
717
  
  	trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
  	ret = cpuhp_kick_ap(st, st->target);
  	trace_cpuhp_exit(cpu, st->state, prev_state, ret);
  
  	return ret;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  }
  
  static struct smp_hotplug_thread cpuhp_threads = {
  	.store			= &cpuhp_state.thread,
  	.create			= &cpuhp_create,
  	.thread_should_run	= cpuhp_should_run,
  	.thread_fn		= cpuhp_thread_fun,
  	.thread_comm		= "cpuhp/%u",
  	.selfparking		= true,
  };
  
  void __init cpuhp_threads_init(void)
  {
  	BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  	kthread_unpark(this_cpu_read(cpuhp_state.thread));
  }
777c6e0da   Michal Hocko   hotplug: Make reg...
734
  #ifdef CONFIG_HOTPLUG_CPU
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
735
736
737
738
739
740
741
742
743
744
745
746
  /**
   * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
   * @cpu: a CPU id
   *
   * This function walks all processes, finds a valid mm struct for each one and
   * then clears a corresponding bit in mm's cpumask.  While this all sounds
   * trivial, there are various non-obvious corner cases, which this function
   * tries to solve in a safe manner.
   *
   * Also note that the function uses a somewhat relaxed locking scheme, so it may
   * be called only for an already offlined CPU.
   */
cb79295e2   Anton Vorontsov   cpu: introduce cl...
747
748
749
750
751
752
753
754
755
756
757
  void clear_tasks_mm_cpumask(int cpu)
  {
  	struct task_struct *p;
  
  	/*
  	 * This function is called after the cpu is taken down and marked
  	 * offline, so its not like new tasks will ever get this cpu set in
  	 * their mm mask. -- Peter Zijlstra
  	 * Thus, we may use rcu_read_lock() here, instead of grabbing
  	 * full-fledged tasklist_lock.
  	 */
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
758
  	WARN_ON(cpu_online(cpu));
cb79295e2   Anton Vorontsov   cpu: introduce cl...
759
760
761
  	rcu_read_lock();
  	for_each_process(p) {
  		struct task_struct *t;
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
762
763
764
765
  		/*
  		 * Main thread might exit, but other threads may still have
  		 * a valid mm. Find one.
  		 */
cb79295e2   Anton Vorontsov   cpu: introduce cl...
766
767
768
769
770
771
772
773
  		t = find_lock_task_mm(p);
  		if (!t)
  			continue;
  		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  		task_unlock(t);
  	}
  	rcu_read_unlock();
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
  /* Take this CPU down. */
71cf5aeeb   Mathias Krause   kernel, cpu: Remo...
775
  static int take_cpu_down(void *_param)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776
  {
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
777
778
  	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  	enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
090e77c39   Thomas Gleixner   cpu/hotplug: Rest...
779
  	int err, cpu = smp_processor_id();
724a86881   Peter Zijlstra   smp/hotplug: Call...
780
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
784
  	/* Ensure this CPU doesn't handle any more interrupts. */
  	err = __cpu_disable();
  	if (err < 0)
f37051364   Zwane Mwaikambo   [PATCH] i386 CPU ...
785
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
787
788
789
790
791
792
  	/*
  	 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
  	 * do this step again.
  	 */
  	WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
  	st->state--;
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
793
  	/* Invoke the former CPU_DYING callbacks */
724a86881   Peter Zijlstra   smp/hotplug: Call...
794
795
796
797
798
799
800
  	for (; st->state > target; st->state--) {
  		ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
  		/*
  		 * DYING must not fail!
  		 */
  		WARN_ON_ONCE(ret);
  	}
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
801

52c063d1a   Thomas Gleixner   clockevents: Make...
802
803
  	/* Give up timekeeping duties */
  	tick_handover_do_timer();
14e568e78   Thomas Gleixner   stop_machine: Use...
804
  	/* Park the stopper thread */
090e77c39   Thomas Gleixner   cpu/hotplug: Rest...
805
  	stop_machine_park(cpu);
f37051364   Zwane Mwaikambo   [PATCH] i386 CPU ...
806
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
  }
984581728   Thomas Gleixner   cpu/hotplug: Spli...
808
  static int takedown_cpu(unsigned int cpu)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809
  {
e69aab131   Thomas Gleixner   cpu/hotplug: Make...
810
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
984581728   Thomas Gleixner   cpu/hotplug: Spli...
811
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812

2a58c527b   Thomas Gleixner   cpu/hotplug: Fix ...
813
  	/* Park the smpboot threads */
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
814
  	kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
6acce3ef8   Peter Zijlstra   sched: Remove get...
815
  	/*
a89941816   Thomas Gleixner   hotplug: Prevent ...
816
817
  	 * Prevent irq alloc/free while the dying cpu reorganizes the
  	 * interrupt affinities.
6acce3ef8   Peter Zijlstra   sched: Remove get...
818
  	 */
a89941816   Thomas Gleixner   hotplug: Prevent ...
819
  	irq_lock_sparse();
6acce3ef8   Peter Zijlstra   sched: Remove get...
820

a89941816   Thomas Gleixner   hotplug: Prevent ...
821
822
823
  	/*
  	 * So now all preempt/rcu users must observe !cpu_active().
  	 */
210e21331   Sebastian Andrzej Siewior   cpu/hotplug: Use ...
824
  	err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
043215875   Rusty Russell   Hotplug CPU: don'...
825
  	if (err) {
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
826
  		/* CPU refused to die */
a89941816   Thomas Gleixner   hotplug: Prevent ...
827
  		irq_unlock_sparse();
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
828
829
  		/* Unpark the hotplug thread so we can rollback there */
  		kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
984581728   Thomas Gleixner   cpu/hotplug: Spli...
830
  		return err;
8fa1d7d3b   Satoru Takeuchi   [PATCH] cpu-hotpl...
831
  	}
043215875   Rusty Russell   Hotplug CPU: don'...
832
  	BUG_ON(cpu_online(cpu));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833

48c5ccae8   Peter Zijlstra   sched: Simplify c...
834
  	/*
ee1e714b9   Thomas Gleixner   cpu/hotplug: Remo...
835
  	 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
48c5ccae8   Peter Zijlstra   sched: Simplify c...
836
837
  	 * runnable tasks from the cpu, there's only the idle task left now
  	 * that the migration thread is done doing the stop_machine thing.
51a96c778   Peter Zijlstra   cpu: Remove incor...
838
839
  	 *
  	 * Wait for the stop thread to go away.
48c5ccae8   Peter Zijlstra   sched: Simplify c...
840
  	 */
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
841
  	wait_for_ap_thread(st, false);
e69aab131   Thomas Gleixner   cpu/hotplug: Make...
842
  	BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843

a89941816   Thomas Gleixner   hotplug: Prevent ...
844
845
  	/* Interrupts are moved away from the dying cpu, reenable alloc/free */
  	irq_unlock_sparse();
345527b1e   Preeti U Murthy   clockevents: Fix ...
846
  	hotplug_cpu__broadcast_tick_pull(cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
848
  	/* This actually kills the CPU. */
  	__cpu_die(cpu);
a49b116dc   Thomas Gleixner   clockevents: Clea...
849
  	tick_cleanup_dead_cpu(cpu);
a58163d8c   Paul E. McKenney   rcu: Migrate call...
850
  	rcutree_migrate_callbacks(cpu);
984581728   Thomas Gleixner   cpu/hotplug: Spli...
851
852
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
853

71f87b2fc   Thomas Gleixner   cpu/hotplug: Plug...
854
855
856
  static void cpuhp_complete_idle_dead(void *arg)
  {
  	struct cpuhp_cpu_state *st = arg;
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
857
  	complete_ap_thread(st, false);
71f87b2fc   Thomas Gleixner   cpu/hotplug: Plug...
858
  }
e69aab131   Thomas Gleixner   cpu/hotplug: Make...
859
860
861
862
863
  void cpuhp_report_idle_dead(void)
  {
  	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  
  	BUG_ON(st->state != CPUHP_AP_OFFLINE);
27d50c7ee   Thomas Gleixner   rcu: Make CPU_DYI...
864
  	rcu_report_dead(smp_processor_id());
71f87b2fc   Thomas Gleixner   cpu/hotplug: Plug...
865
866
867
868
869
870
871
  	st->state = CPUHP_AP_IDLE_DEAD;
  	/*
  	 * We cannot call complete after rcu_report_dead() so we delegate it
  	 * to an online cpu.
  	 */
  	smp_call_function_single(cpumask_first(cpu_online_mask),
  				 cpuhp_complete_idle_dead, st, 0);
e69aab131   Thomas Gleixner   cpu/hotplug: Make...
872
  }
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
873
874
875
876
  static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
  {
  	for (st->state++; st->state < st->target; st->state++) {
  		struct cpuhp_step *step = cpuhp_get_step(st->state);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
877

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  		if (!step->skip_onerr)
  			cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
  	}
  }
  
  static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  				enum cpuhp_state target)
  {
  	enum cpuhp_state prev_state = st->state;
  	int ret = 0;
  
  	for (; st->state > target; st->state--) {
  		ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
  		if (ret) {
  			st->target = prev_state;
1d92a611d   Thomas Gleixner   cpu/hotplug: Prev...
893
894
  			if (st->state < prev_state)
  				undo_cpu_down(cpu, st);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
895
896
897
898
899
  			break;
  		}
  	}
  	return ret;
  }
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
900

984581728   Thomas Gleixner   cpu/hotplug: Spli...
901
  /* Requires cpu_add_remove_lock to be held */
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
902
903
  static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  			   enum cpuhp_state target)
984581728   Thomas Gleixner   cpu/hotplug: Spli...
904
  {
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
905
906
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  	int prev_state, ret = 0;
984581728   Thomas Gleixner   cpu/hotplug: Spli...
907
908
909
  
  	if (num_online_cpus() == 1)
  		return -EBUSY;
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
910
  	if (!cpu_present(cpu))
984581728   Thomas Gleixner   cpu/hotplug: Spli...
911
  		return -EINVAL;
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
912
  	cpus_write_lock();
984581728   Thomas Gleixner   cpu/hotplug: Spli...
913
914
  
  	cpuhp_tasks_frozen = tasks_frozen;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
915
  	prev_state = cpuhp_set_state(st, target);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
916
917
918
919
  	/*
  	 * If the current CPU state is in the range of the AP hotplug thread,
  	 * then we need to kick the thread.
  	 */
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
920
  	if (st->state > CPUHP_TEARDOWN_CPU) {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
921
  		st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
922
923
924
925
926
927
928
929
930
931
932
933
  		ret = cpuhp_kick_ap_work(cpu);
  		/*
  		 * The AP side has done the error rollback already. Just
  		 * return the error code..
  		 */
  		if (ret)
  			goto out;
  
  		/*
  		 * We might have stopped still in the range of the AP hotplug
  		 * thread. Nothing to do anymore.
  		 */
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
934
  		if (st->state > CPUHP_TEARDOWN_CPU)
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
935
  			goto out;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
936
937
  
  		st->target = target;
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
938
939
  	}
  	/*
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
940
  	 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
941
942
  	 * to do the further cleanups.
  	 */
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
943
  	ret = cpuhp_down_callbacks(cpu, st, target);
1d92a611d   Thomas Gleixner   cpu/hotplug: Prev...
944
  	if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
945
946
  		cpuhp_reset_state(st, prev_state);
  		__cpuhp_kick_ap(st);
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
947
  	}
984581728   Thomas Gleixner   cpu/hotplug: Spli...
948

1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
949
  out:
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
950
  	cpus_write_unlock();
941154bd6   Thomas Gleixner   watchdog/hardlock...
951
952
953
954
955
  	/*
  	 * Do post unplug cleanup. This is still protected against
  	 * concurrent CPU hotplug via cpu_add_remove_lock.
  	 */
  	lockup_detector_cleanup();
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
956
  	return ret;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
957
  }
6beba29c6   Thomas Gleixner   cpu/hotplug: Spli...
958
959
960
961
962
963
  static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
  {
  	if (cpu_hotplug_disabled)
  		return -EBUSY;
  	return _cpu_down(cpu, 0, target);
  }
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
964
  static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
965
  {
9ea09af3b   Heiko Carstens   stop_machine: int...
966
  	int err;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
967

d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
968
  	cpu_maps_update_begin();
6beba29c6   Thomas Gleixner   cpu/hotplug: Spli...
969
  	err = cpu_down_maps_locked(cpu, target);
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
970
  	cpu_maps_update_done();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
972
  	return err;
  }
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
973

af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
974
975
976
977
  int cpu_down(unsigned int cpu)
  {
  	return do_cpu_down(cpu, CPUHP_OFFLINE);
  }
b62b8ef90   Zhang Rui   force offline the...
978
  EXPORT_SYMBOL(cpu_down);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
979
980
981
  
  #else
  #define takedown_cpu		NULL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
  #endif /*CONFIG_HOTPLUG_CPU*/
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
983
  /**
ee1e714b9   Thomas Gleixner   cpu/hotplug: Remo...
984
   * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
985
986
   * @cpu: cpu that just started
   *
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
987
988
989
990
991
992
993
   * It must be called by the arch code on the new cpu, before the new cpu
   * enables interrupts and before the "boot" cpu returns from __cpu_up().
   */
  void notify_cpu_starting(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  	enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
724a86881   Peter Zijlstra   smp/hotplug: Call...
994
  	int ret;
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
995

0c6d4576c   Sebastian Andrzej Siewior   cpu/hotplug: Get ...
996
  	rcu_cpu_starting(cpu);	/* Enables RCU usage on this CPU. */
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
997
  	st->booted_once = true;
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
998
  	while (st->state < target) {
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
999
  		st->state++;
724a86881   Peter Zijlstra   smp/hotplug: Call...
1000
1001
1002
1003
1004
  		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
  		/*
  		 * STARTING must not fail!
  		 */
  		WARN_ON_ONCE(ret);
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1005
1006
  	}
  }
949338e35   Thomas Gleixner   cpu/hotplug: Move...
1007
  /*
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
1008
1009
1010
   * Called from the idle task. Wake up the controlling task which brings the
   * stopper and the hotplug thread of the upcoming CPU up and then delegates
   * the rest of the online bringup to the hotplug thread.
949338e35   Thomas Gleixner   cpu/hotplug: Move...
1011
   */
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1012
  void cpuhp_online_idle(enum cpuhp_state state)
949338e35   Thomas Gleixner   cpu/hotplug: Move...
1013
  {
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1014
  	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1015
1016
1017
1018
1019
1020
  
  	/* Happens for the boot cpu */
  	if (state != CPUHP_AP_ONLINE_IDLE)
  		return;
  
  	st->state = CPUHP_AP_ONLINE_IDLE;
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
1021
  	complete_ap_thread(st, true);
949338e35   Thomas Gleixner   cpu/hotplug: Move...
1022
  }
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1023
  /* Requires cpu_add_remove_lock to be held */
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1024
  static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1025
  {
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1026
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
1027
  	struct task_struct *idle;
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
1028
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1029

8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1030
  	cpus_write_lock();
38498a67a   Thomas Gleixner   smp: Add generic ...
1031

757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1032
  	if (!cpu_present(cpu)) {
5e5041f35   Yasuaki Ishimatsu   ACPI / processor:...
1033
1034
1035
  		ret = -EINVAL;
  		goto out;
  	}
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1036
1037
1038
1039
1040
  	/*
  	 * The caller of do_cpu_up might have raced with another
  	 * caller. Ignore it for now.
  	 */
  	if (st->state >= target)
38498a67a   Thomas Gleixner   smp: Add generic ...
1041
  		goto out;
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1042
1043
1044
1045
1046
1047
1048
1049
  
  	if (st->state == CPUHP_OFFLINE) {
  		/* Let it fail before we try to bring the cpu up */
  		idle = idle_thread_get(cpu);
  		if (IS_ERR(idle)) {
  			ret = PTR_ERR(idle);
  			goto out;
  		}
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
1050
  	}
38498a67a   Thomas Gleixner   smp: Add generic ...
1051

ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
1052
  	cpuhp_tasks_frozen = tasks_frozen;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
1053
  	cpuhp_set_state(st, target);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1054
1055
1056
1057
  	/*
  	 * If the current CPU state is in the range of the AP hotplug thread,
  	 * then we need to kick the thread once more.
  	 */
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1058
  	if (st->state > CPUHP_BRINGUP_CPU) {
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
  		ret = cpuhp_kick_ap_work(cpu);
  		/*
  		 * The AP side has done the error rollback already. Just
  		 * return the error code..
  		 */
  		if (ret)
  			goto out;
  	}
  
  	/*
  	 * Try to reach the target state. We max out on the BP at
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1070
  	 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1071
1072
  	 * responsible for bringing it up to the target state.
  	 */
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1073
  	target = min((int)target, CPUHP_BRINGUP_CPU);
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1074
  	ret = cpuhp_up_callbacks(cpu, st, target);
38498a67a   Thomas Gleixner   smp: Add generic ...
1075
  out:
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1076
  	cpus_write_unlock();
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1077
1078
  	return ret;
  }
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1079
  static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1080
1081
  {
  	int err = 0;
cf23422b9   minskey guo   cpu/mem hotplug: ...
1082

e0b582ec5   Rusty Russell   cpumask: convert ...
1083
  	if (!cpu_possible(cpu)) {
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1084
1085
1086
  		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time
  ",
  		       cpu);
87d5e0236   Chen Gong   kernel/cpu.c: del...
1087
  #if defined(CONFIG_IA64)
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1088
1089
  		pr_err("please check additional_cpus= boot parameter
  ");
73e753a50   KAMEZAWA Hiroyuki   CPU HOTPLUG: avoi...
1090
1091
1092
  #endif
  		return -EINVAL;
  	}
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1093

01b0f1970   Toshi Kani   cpu/mem hotplug: ...
1094
1095
1096
  	err = try_online_node(cpu_to_node(cpu));
  	if (err)
  		return err;
cf23422b9   minskey guo   cpu/mem hotplug: ...
1097

d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1098
  	cpu_maps_update_begin();
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1099
1100
  
  	if (cpu_hotplug_disabled) {
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1101
  		err = -EBUSY;
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1102
1103
  		goto out;
  	}
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
1104
1105
1106
1107
  	if (!cpu_smt_allowed(cpu)) {
  		err = -EPERM;
  		goto out;
  	}
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1108

af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1109
  	err = _cpu_up(cpu, 0, target);
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1110
  out:
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1111
  	cpu_maps_update_done();
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1112
1113
  	return err;
  }
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1114
1115
1116
1117
1118
  
  int cpu_up(unsigned int cpu)
  {
  	return do_cpu_up(cpu, CPUHP_ONLINE);
  }
a513f6bab   Paul E. McKenney   cpu: Export cpu_up()
1119
  EXPORT_SYMBOL_GPL(cpu_up);
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1120

f3de4be9d   Rafael J. Wysocki   PM: Fix dependenc...
1121
  #ifdef CONFIG_PM_SLEEP_SMP
e0b582ec5   Rusty Russell   cpumask: convert ...
1122
  static cpumask_var_t frozen_cpus;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1123

d391e5522   James Morse   cpu/hotplug: Allo...
1124
  int freeze_secondary_cpus(int primary)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1125
  {
d391e5522   James Morse   cpu/hotplug: Allo...
1126
  	int cpu, error = 0;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1127

d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1128
  	cpu_maps_update_begin();
d391e5522   James Morse   cpu/hotplug: Allo...
1129
1130
  	if (!cpu_online(primary))
  		primary = cpumask_first(cpu_online_mask);
9ee349ad6   Xiaotian Feng   sched: Fix set_cp...
1131
1132
  	/*
  	 * We take down all of the non-boot CPUs in one shot to avoid races
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1133
1134
  	 * with the userspace trying to use the CPU hotplug at the same time
  	 */
e0b582ec5   Rusty Russell   cpumask: convert ...
1135
  	cpumask_clear(frozen_cpus);
6ad4c1888   Peter Zijlstra   sched: Fix balanc...
1136

84117da5b   Fabian Frederick   kernel/cpu.c: con...
1137
1138
  	pr_info("Disabling non-boot CPUs ...
  ");
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1139
  	for_each_online_cpu(cpu) {
d391e5522   James Morse   cpu/hotplug: Allo...
1140
  		if (cpu == primary)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1141
  			continue;
bb3632c61   Todd E Brandt   PM / sleep: trace...
1142
  		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1143
  		error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
bb3632c61   Todd E Brandt   PM / sleep: trace...
1144
  		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
feae3203d   Mike Travis   timers, init: Lim...
1145
  		if (!error)
e0b582ec5   Rusty Russell   cpumask: convert ...
1146
  			cpumask_set_cpu(cpu, frozen_cpus);
feae3203d   Mike Travis   timers, init: Lim...
1147
  		else {
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1148
1149
  			pr_err("Error taking CPU%d down: %d
  ", cpu, error);
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1150
1151
1152
  			break;
  		}
  	}
86886e55b   Joseph Cihula   x86, intel_txt: I...
1153

89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
1154
  	if (!error)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1155
  		BUG_ON(num_online_cpus() > 1);
89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
1156
  	else
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1157
1158
  		pr_err("Non-boot CPUs are not disabled
  ");
89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
1159
1160
1161
1162
1163
1164
1165
  
  	/*
  	 * Make sure the CPUs won't be enabled by someone else. We need to do
  	 * this even in case of failure as all disable_nonboot_cpus() users are
  	 * supposed to do enable_nonboot_cpus() on the failure path.
  	 */
  	cpu_hotplug_disabled++;
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1166
  	cpu_maps_update_done();
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1167
1168
  	return error;
  }
d0af9eed5   Suresh Siddha   x86, pat/mtrr: Re...
1169
1170
1171
1172
1173
1174
1175
  void __weak arch_enable_nonboot_cpus_begin(void)
  {
  }
  
  void __weak arch_enable_nonboot_cpus_end(void)
  {
  }
71cf5aeeb   Mathias Krause   kernel, cpu: Remo...
1176
  void enable_nonboot_cpus(void)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1177
1178
1179
1180
  {
  	int cpu, error;
  
  	/* Allow everyone to use the CPU hotplug again */
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1181
  	cpu_maps_update_begin();
01b411590   Lianwei Wang   cpu/hotplug: Hand...
1182
  	__cpu_hotplug_enable();
e0b582ec5   Rusty Russell   cpumask: convert ...
1183
  	if (cpumask_empty(frozen_cpus))
1d64b9cb1   Rafael J. Wysocki   [PATCH] Fix micro...
1184
  		goto out;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1185

84117da5b   Fabian Frederick   kernel/cpu.c: con...
1186
1187
  	pr_info("Enabling non-boot CPUs ...
  ");
d0af9eed5   Suresh Siddha   x86, pat/mtrr: Re...
1188
1189
  
  	arch_enable_nonboot_cpus_begin();
e0b582ec5   Rusty Russell   cpumask: convert ...
1190
  	for_each_cpu(cpu, frozen_cpus) {
bb3632c61   Todd E Brandt   PM / sleep: trace...
1191
  		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1192
  		error = _cpu_up(cpu, 1, CPUHP_ONLINE);
bb3632c61   Todd E Brandt   PM / sleep: trace...
1193
  		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1194
  		if (!error) {
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1195
1196
  			pr_info("CPU%d is up
  ", cpu);
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1197
1198
  			continue;
  		}
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1199
1200
  		pr_warn("Error taking CPU%d up: %d
  ", cpu, error);
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1201
  	}
d0af9eed5   Suresh Siddha   x86, pat/mtrr: Re...
1202
1203
  
  	arch_enable_nonboot_cpus_end();
e0b582ec5   Rusty Russell   cpumask: convert ...
1204
  	cpumask_clear(frozen_cpus);
1d64b9cb1   Rafael J. Wysocki   [PATCH] Fix micro...
1205
  out:
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1206
  	cpu_maps_update_done();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1207
  }
e0b582ec5   Rusty Russell   cpumask: convert ...
1208

d7268a31c   Fenghua Yu   CPU: Add right qu...
1209
  static int __init alloc_frozen_cpus(void)
e0b582ec5   Rusty Russell   cpumask: convert ...
1210
1211
1212
1213
1214
1215
  {
  	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  		return -ENOMEM;
  	return 0;
  }
  core_initcall(alloc_frozen_cpus);
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1216
1217
  
  /*
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
   * When callbacks for CPU hotplug notifications are being executed, we must
   * ensure that the state of the system with respect to the tasks being frozen
   * or not, as reported by the notification, remains unchanged *throughout the
   * duration* of the execution of the callbacks.
   * Hence we need to prevent the freezer from racing with regular CPU hotplug.
   *
   * This synchronization is implemented by mutually excluding regular CPU
   * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
   * Hibernate notifications.
   */
  static int
  cpu_hotplug_pm_callback(struct notifier_block *nb,
  			unsigned long action, void *ptr)
  {
  	switch (action) {
  
  	case PM_SUSPEND_PREPARE:
  	case PM_HIBERNATION_PREPARE:
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
1236
  		cpu_hotplug_disable();
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1237
1238
1239
1240
  		break;
  
  	case PM_POST_SUSPEND:
  	case PM_POST_HIBERNATION:
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
1241
  		cpu_hotplug_enable();
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1242
1243
1244
1245
1246
1247
1248
1249
  		break;
  
  	default:
  		return NOTIFY_DONE;
  	}
  
  	return NOTIFY_OK;
  }
d7268a31c   Fenghua Yu   CPU: Add right qu...
1250
  static int __init cpu_hotplug_pm_sync_init(void)
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1251
  {
6e32d479d   Fenghua Yu   kernel/cpu.c: Add...
1252
1253
1254
1255
1256
  	/*
  	 * cpu_hotplug_pm_callback has higher priority than x86
  	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  	 * to disable cpu hotplug to avoid cpu hotplug race.
  	 */
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1257
1258
1259
1260
  	pm_notifier(cpu_hotplug_pm_callback, 0);
  	return 0;
  }
  core_initcall(cpu_hotplug_pm_sync_init);
f3de4be9d   Rafael J. Wysocki   PM: Fix dependenc...
1261
  #endif /* CONFIG_PM_SLEEP_SMP */
68f4f1ec0   Max Krasnyansky   sched: Move cpu m...
1262

8ce371f98   Peter Zijlstra   lockdep: Fix per-...
1263
  int __boot_cpu_id;
68f4f1ec0   Max Krasnyansky   sched: Move cpu m...
1264
  #endif /* CONFIG_SMP */
b8d317d10   Mike Travis   cpumask: make cpu...
1265

cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1266
1267
1268
1269
  /* Boot processor state steps */
  static struct cpuhp_step cpuhp_bp_states[] = {
  	[CPUHP_OFFLINE] = {
  		.name			= "offline",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1270
1271
  		.startup.single		= NULL,
  		.teardown.single	= NULL,
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1272
1273
1274
  	},
  #ifdef CONFIG_SMP
  	[CPUHP_CREATE_THREADS]= {
677f66465   Thomas Gleixner   cpu/hotplug: Make...
1275
  		.name			= "threads:prepare",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1276
1277
  		.startup.single		= smpboot_create_threads,
  		.teardown.single	= NULL,
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1278
  		.cant_stop		= true,
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1279
  	},
00e16c3d6   Thomas Gleixner   perf/core: Conver...
1280
  	[CPUHP_PERF_PREPARE] = {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1281
1282
1283
  		.name			= "perf:prepare",
  		.startup.single		= perf_event_init_cpu,
  		.teardown.single	= perf_event_exit_cpu,
00e16c3d6   Thomas Gleixner   perf/core: Conver...
1284
  	},
7ee681b25   Thomas Gleixner   workqueue: Conver...
1285
  	[CPUHP_WORKQUEUE_PREP] = {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1286
1287
1288
  		.name			= "workqueue:prepare",
  		.startup.single		= workqueue_prepare_cpu,
  		.teardown.single	= NULL,
7ee681b25   Thomas Gleixner   workqueue: Conver...
1289
  	},
27590dc17   Thomas Gleixner   hrtimer: Convert ...
1290
  	[CPUHP_HRTIMERS_PREPARE] = {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1291
1292
1293
  		.name			= "hrtimers:prepare",
  		.startup.single		= hrtimers_prepare_cpu,
  		.teardown.single	= hrtimers_dead_cpu,
27590dc17   Thomas Gleixner   hrtimer: Convert ...
1294
  	},
31487f832   Richard Weinberger   smp/cfd: Convert ...
1295
  	[CPUHP_SMPCFD_PREPARE] = {
677f66465   Thomas Gleixner   cpu/hotplug: Make...
1296
  		.name			= "smpcfd:prepare",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1297
1298
  		.startup.single		= smpcfd_prepare_cpu,
  		.teardown.single	= smpcfd_dead_cpu,
31487f832   Richard Weinberger   smp/cfd: Convert ...
1299
  	},
e6d4989a9   Richard Weinberger   relayfs: Convert ...
1300
1301
1302
1303
1304
  	[CPUHP_RELAY_PREPARE] = {
  		.name			= "relay:prepare",
  		.startup.single		= relay_prepare_cpu,
  		.teardown.single	= NULL,
  	},
6731d4f12   Sebastian Andrzej Siewior   slab: Convert to ...
1305
1306
1307
1308
  	[CPUHP_SLAB_PREPARE] = {
  		.name			= "slab:prepare",
  		.startup.single		= slab_prepare_cpu,
  		.teardown.single	= slab_dead_cpu,
31487f832   Richard Weinberger   smp/cfd: Convert ...
1309
  	},
4df837425   Thomas Gleixner   rcu: Convert rcut...
1310
  	[CPUHP_RCUTREE_PREP] = {
677f66465   Thomas Gleixner   cpu/hotplug: Make...
1311
  		.name			= "RCU/tree:prepare",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1312
1313
  		.startup.single		= rcutree_prepare_cpu,
  		.teardown.single	= rcutree_dead_cpu,
4df837425   Thomas Gleixner   rcu: Convert rcut...
1314
  	},
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1315
  	/*
4fae16dff   Richard Cochran   timers/core: Corr...
1316
1317
1318
1319
  	 * On the tear-down path, timers_dead_cpu() must be invoked
  	 * before blk_mq_queue_reinit_notify() from notify_dead(),
  	 * otherwise a RCU stall occurs.
  	 */
6fae6de72   Thomas Gleixner   timers: Reinitial...
1320
  	[CPUHP_TIMERS_PREPARE] = {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1321
  		.name			= "timers:dead",
6fae6de72   Thomas Gleixner   timers: Reinitial...
1322
  		.startup.single		= timers_prepare_cpu,
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1323
  		.teardown.single	= timers_dead_cpu,
4fae16dff   Richard Cochran   timers/core: Corr...
1324
  	},
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1325
  	/* Kicks the plugged cpu into life */
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1326
1327
  	[CPUHP_BRINGUP_CPU] = {
  		.name			= "cpu:bringup",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1328
1329
  		.startup.single		= bringup_cpu,
  		.teardown.single	= NULL,
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1330
  		.cant_stop		= true,
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1331
  	},
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1332
1333
1334
1335
  	/*
  	 * Handled on controll processor until the plugged processor manages
  	 * this itself.
  	 */
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1336
1337
  	[CPUHP_TEARDOWN_CPU] = {
  		.name			= "cpu:teardown",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1338
1339
  		.startup.single		= NULL,
  		.teardown.single	= takedown_cpu,
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1340
  		.cant_stop		= true,
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1341
  	},
a7c734140   Thomas Gleixner   cpu/hotplug: Keep...
1342
1343
  #else
  	[CPUHP_BRINGUP_CPU] = { },
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1344
  #endif
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
1345
  };
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1346
1347
1348
  /* Application processor state steps */
  static struct cpuhp_step cpuhp_ap_states[] = {
  #ifdef CONFIG_SMP
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
  	/* Final state before CPU kills itself */
  	[CPUHP_AP_IDLE_DEAD] = {
  		.name			= "idle:dead",
  	},
  	/*
  	 * Last state before CPU enters the idle loop to die. Transient state
  	 * for synchronization.
  	 */
  	[CPUHP_AP_OFFLINE] = {
  		.name			= "ap:offline",
  		.cant_stop		= true,
  	},
9cf7243d5   Thomas Gleixner   sched: Make set_c...
1361
1362
1363
  	/* First state is scheduler control. Interrupts are disabled */
  	[CPUHP_AP_SCHED_STARTING] = {
  		.name			= "sched:starting",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1364
1365
  		.startup.single		= sched_cpu_starting,
  		.teardown.single	= sched_cpu_dying,
9cf7243d5   Thomas Gleixner   sched: Make set_c...
1366
  	},
4df837425   Thomas Gleixner   rcu: Convert rcut...
1367
  	[CPUHP_AP_RCUTREE_DYING] = {
677f66465   Thomas Gleixner   cpu/hotplug: Make...
1368
  		.name			= "RCU/tree:dying",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1369
1370
  		.startup.single		= NULL,
  		.teardown.single	= rcutree_dying_cpu,
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1371
  	},
b68df97ec   Lai Jiangshan   smp/hotplug: Move...
1372
1373
1374
1375
1376
  	[CPUHP_AP_SMPCFD_DYING] = {
  		.name			= "smpcfd:dying",
  		.startup.single		= NULL,
  		.teardown.single	= smpcfd_dying_cpu,
  	},
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1377
1378
1379
1380
1381
1382
  	/* Entry state on starting. Interrupts enabled from here on. Transient
  	 * state for synchronsization */
  	[CPUHP_AP_ONLINE] = {
  		.name			= "ap:online",
  	},
  	/* Handle smpboot threads park/unpark */
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1383
  	[CPUHP_AP_SMPBOOT_THREADS] = {
677f66465   Thomas Gleixner   cpu/hotplug: Make...
1384
  		.name			= "smpboot/threads:online",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1385
  		.startup.single		= smpboot_unpark_threads,
26a6dcc71   Thomas Gleixner   cpu/hotplug: Make...
1386
  		.teardown.single	= smpboot_park_threads,
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1387
  	},
c5cb83bb3   Thomas Gleixner   genirq/cpuhotplug...
1388
1389
1390
1391
1392
  	[CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
  		.name			= "irq/affinity:online",
  		.startup.single		= irq_affinity_online_cpu,
  		.teardown.single	= NULL,
  	},
00e16c3d6   Thomas Gleixner   perf/core: Conver...
1393
  	[CPUHP_AP_PERF_ONLINE] = {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1394
1395
1396
  		.name			= "perf:online",
  		.startup.single		= perf_event_init_cpu,
  		.teardown.single	= perf_event_exit_cpu,
00e16c3d6   Thomas Gleixner   perf/core: Conver...
1397
  	},
7ee681b25   Thomas Gleixner   workqueue: Conver...
1398
  	[CPUHP_AP_WORKQUEUE_ONLINE] = {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1399
1400
1401
  		.name			= "workqueue:online",
  		.startup.single		= workqueue_online_cpu,
  		.teardown.single	= workqueue_offline_cpu,
7ee681b25   Thomas Gleixner   workqueue: Conver...
1402
  	},
4df837425   Thomas Gleixner   rcu: Convert rcut...
1403
  	[CPUHP_AP_RCUTREE_ONLINE] = {
677f66465   Thomas Gleixner   cpu/hotplug: Make...
1404
  		.name			= "RCU/tree:online",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1405
1406
  		.startup.single		= rcutree_online_cpu,
  		.teardown.single	= rcutree_offline_cpu,
4df837425   Thomas Gleixner   rcu: Convert rcut...
1407
  	},
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1408
  #endif
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1409
1410
1411
  	/*
  	 * The dynamically registered state space is here
  	 */
aaddd7d1c   Thomas Gleixner   sched/hotplug: Ma...
1412
1413
1414
1415
  #ifdef CONFIG_SMP
  	/* Last state is scheduler control setting the cpu active */
  	[CPUHP_AP_ACTIVE] = {
  		.name			= "sched:active",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1416
1417
  		.startup.single		= sched_cpu_activate,
  		.teardown.single	= sched_cpu_deactivate,
aaddd7d1c   Thomas Gleixner   sched/hotplug: Ma...
1418
1419
  	},
  #endif
d10ef6f93   Thomas Gleixner   cpu/hotplug: Docu...
1420
  	/* CPU is fully up and running. */
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1421
1422
  	[CPUHP_ONLINE] = {
  		.name			= "online",
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1423
1424
  		.startup.single		= NULL,
  		.teardown.single	= NULL,
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1425
1426
  	},
  };
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1427
1428
1429
1430
1431
1432
1433
  /* Sanity check for callbacks */
  static int cpuhp_cb_check(enum cpuhp_state state)
  {
  	if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  		return -EINVAL;
  	return 0;
  }
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1434
1435
1436
1437
1438
1439
1440
  /*
   * Returns a free for dynamic slot assignment of the Online state. The states
   * are protected by the cpuhp_slot_states mutex and an empty slot is identified
   * by having no name assigned.
   */
  static int cpuhp_reserve_state(enum cpuhp_state state)
  {
4205e4786   Thomas Gleixner   cpu/hotplug: Prov...
1441
1442
  	enum cpuhp_state i, end;
  	struct cpuhp_step *step;
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1443

4205e4786   Thomas Gleixner   cpu/hotplug: Prov...
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
  	switch (state) {
  	case CPUHP_AP_ONLINE_DYN:
  		step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN;
  		end = CPUHP_AP_ONLINE_DYN_END;
  		break;
  	case CPUHP_BP_PREPARE_DYN:
  		step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN;
  		end = CPUHP_BP_PREPARE_DYN_END;
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	for (i = state; i <= end; i++, step++) {
  		if (!step->name)
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
  			return i;
  	}
  	WARN(1, "No more dynamic states available for CPU hotplug
  ");
  	return -ENOSPC;
  }
  
  static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
  				 int (*startup)(unsigned int cpu),
  				 int (*teardown)(unsigned int cpu),
  				 bool multi_instance)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1470
1471
1472
  {
  	/* (Un)Install the callbacks for further cpu hotplug operations */
  	struct cpuhp_step *sp;
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1473
  	int ret = 0;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1474

0c96b2730   Ethan Barnes   smp/hotplug: Hand...
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
  	/*
  	 * If name is NULL, then the state gets removed.
  	 *
  	 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
  	 * the first allocation from these dynamic ranges, so the removal
  	 * would trigger a new allocation and clear the wrong (already
  	 * empty) state, leaving the callbacks of the to be cleared state
  	 * dangling, which causes wreckage on the next hotplug operation.
  	 */
  	if (name && (state == CPUHP_AP_ONLINE_DYN ||
  		     state == CPUHP_BP_PREPARE_DYN)) {
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1486
1487
  		ret = cpuhp_reserve_state(state);
  		if (ret < 0)
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1488
  			return ret;
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1489
1490
  		state = ret;
  	}
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1491
  	sp = cpuhp_get_step(state);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1492
1493
  	if (name && sp->name)
  		return -EBUSY;
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1494
1495
  	sp->startup.single = startup;
  	sp->teardown.single = teardown;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1496
  	sp->name = name;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1497
1498
  	sp->multi_instance = multi_instance;
  	INIT_HLIST_HEAD(&sp->list);
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1499
  	return ret;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1500
1501
1502
1503
  }
  
  static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1504
  	return cpuhp_get_step(state)->teardown.single;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1505
  }
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1506
1507
1508
1509
  /*
   * Call the startup/teardown function for a step either on the AP or
   * on the current CPU.
   */
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1510
1511
  static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  			    struct hlist_node *node)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1512
  {
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1513
  	struct cpuhp_step *sp = cpuhp_get_step(state);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1514
  	int ret;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
1515
1516
1517
1518
  	/*
  	 * If there's nothing to do, we done.
  	 * Relies on the union for multi_instance.
  	 */
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1519
1520
  	if ((bringup && !sp->startup.single) ||
  	    (!bringup && !sp->teardown.single))
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1521
  		return 0;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1522
1523
1524
1525
  	/*
  	 * The non AP bound callbacks can fail on bringup. On teardown
  	 * e.g. module removal we crash for now.
  	 */
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1526
1527
  #ifdef CONFIG_SMP
  	if (cpuhp_is_ap_state(state))
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1528
  		ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1529
  	else
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
1530
  		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1531
  #else
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
1532
  	ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1533
  #endif
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
  	BUG_ON(ret && !bringup);
  	return ret;
  }
  
  /*
   * Called from __cpuhp_setup_state on a recoverable failure.
   *
   * Note: The teardown callbacks for rollback are not allowed to fail!
   */
  static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1544
  				   struct hlist_node *node)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1545
1546
  {
  	int cpu;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
  	/* Roll back the already executed steps on the other cpus */
  	for_each_present_cpu(cpu) {
  		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  		int cpustate = st->state;
  
  		if (cpu >= failedcpu)
  			break;
  
  		/* Did we invoke the startup call on that cpu ? */
  		if (cpustate >= state)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1557
  			cpuhp_issue_call(cpu, state, false, node);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1558
1559
  	}
  }
9805c6733   Thomas Gleixner   cpu/hotplug: Add ...
1560
1561
1562
  int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
  					  struct hlist_node *node,
  					  bool invoke)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1563
1564
1565
1566
  {
  	struct cpuhp_step *sp;
  	int cpu;
  	int ret;
9805c6733   Thomas Gleixner   cpu/hotplug: Add ...
1567
  	lockdep_assert_cpus_held();
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1568
1569
1570
  	sp = cpuhp_get_step(state);
  	if (sp->multi_instance == false)
  		return -EINVAL;
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1571
  	mutex_lock(&cpuhp_state_mutex);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1572

3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1573
  	if (!invoke || !sp->startup.multi)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
  		goto add_node;
  
  	/*
  	 * Try to call the startup callback for each present cpu
  	 * depending on the hotplug state of the cpu.
  	 */
  	for_each_present_cpu(cpu) {
  		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  		int cpustate = st->state;
  
  		if (cpustate < state)
  			continue;
  
  		ret = cpuhp_issue_call(cpu, state, true, node);
  		if (ret) {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1589
  			if (sp->teardown.multi)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1590
  				cpuhp_rollback_install(cpu, state, node);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1591
  			goto unlock;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1592
1593
1594
1595
  		}
  	}
  add_node:
  	ret = 0;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1596
  	hlist_add_head(node, &sp->list);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1597
  unlock:
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1598
  	mutex_unlock(&cpuhp_state_mutex);
9805c6733   Thomas Gleixner   cpu/hotplug: Add ...
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
  	return ret;
  }
  
  int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  			       bool invoke)
  {
  	int ret;
  
  	cpus_read_lock();
  	ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1609
  	cpus_read_unlock();
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1610
1611
1612
  	return ret;
  }
  EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1613
  /**
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1614
   * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1615
1616
1617
1618
1619
1620
1621
   * @state:		The state to setup
   * @invoke:		If true, the startup function is invoked for cpus where
   *			cpu state >= @state
   * @startup:		startup callback function
   * @teardown:		teardown callback function
   * @multi_instance:	State is set up for multiple instances which get
   *			added afterwards.
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1622
   *
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1623
   * The caller needs to hold cpus read locked while calling this function.
512f09801   Boris Ostrovsky   cpu/hotplug: Clar...
1624
1625
1626
1627
1628
   * Returns:
   *   On success:
   *      Positive state number if @state is CPUHP_AP_ONLINE_DYN
   *      0 for all other states
   *   On failure: proper (negative) error code
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1629
   */
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1630
1631
1632
1633
1634
  int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
  				   const char *name, bool invoke,
  				   int (*startup)(unsigned int cpu),
  				   int (*teardown)(unsigned int cpu),
  				   bool multi_instance)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1635
1636
  {
  	int cpu, ret = 0;
b9d9d6911   Thomas Gleixner   smp/hotplug: Undo...
1637
  	bool dynstate;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1638

71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1639
  	lockdep_assert_cpus_held();
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1640
1641
  	if (cpuhp_cb_check(state) || !name)
  		return -EINVAL;
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1642
  	mutex_lock(&cpuhp_state_mutex);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1643

dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1644
1645
  	ret = cpuhp_store_callbacks(state, name, startup, teardown,
  				    multi_instance);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1646

b9d9d6911   Thomas Gleixner   smp/hotplug: Undo...
1647
1648
1649
1650
1651
  	dynstate = state == CPUHP_AP_ONLINE_DYN;
  	if (ret > 0 && dynstate) {
  		state = ret;
  		ret = 0;
  	}
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1652
  	if (ret || !invoke || !startup)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
  		goto out;
  
  	/*
  	 * Try to call the startup callback for each present cpu
  	 * depending on the hotplug state of the cpu.
  	 */
  	for_each_present_cpu(cpu) {
  		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  		int cpustate = st->state;
  
  		if (cpustate < state)
  			continue;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1665
  		ret = cpuhp_issue_call(cpu, state, true, NULL);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1666
  		if (ret) {
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1667
  			if (teardown)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1668
1669
  				cpuhp_rollback_install(cpu, state, NULL);
  			cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1670
1671
1672
1673
  			goto out;
  		}
  	}
  out:
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1674
  	mutex_unlock(&cpuhp_state_mutex);
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1675
1676
1677
1678
  	/*
  	 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
  	 * dynamically allocated state in case of success.
  	 */
b9d9d6911   Thomas Gleixner   smp/hotplug: Undo...
1679
  	if (!ret && dynstate)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1680
1681
1682
  		return state;
  	return ret;
  }
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
  EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
  
  int __cpuhp_setup_state(enum cpuhp_state state,
  			const char *name, bool invoke,
  			int (*startup)(unsigned int cpu),
  			int (*teardown)(unsigned int cpu),
  			bool multi_instance)
  {
  	int ret;
  
  	cpus_read_lock();
  	ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
  					     teardown, multi_instance);
  	cpus_read_unlock();
  	return ret;
  }
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1699
  EXPORT_SYMBOL(__cpuhp_setup_state);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
  int __cpuhp_state_remove_instance(enum cpuhp_state state,
  				  struct hlist_node *node, bool invoke)
  {
  	struct cpuhp_step *sp = cpuhp_get_step(state);
  	int cpu;
  
  	BUG_ON(cpuhp_cb_check(state));
  
  	if (!sp->multi_instance)
  		return -EINVAL;
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1710
  	cpus_read_lock();
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1711
  	mutex_lock(&cpuhp_state_mutex);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
  	if (!invoke || !cpuhp_get_teardown_cb(state))
  		goto remove;
  	/*
  	 * Call the teardown callback for each present cpu depending
  	 * on the hotplug state of the cpu. This function is not
  	 * allowed to fail currently!
  	 */
  	for_each_present_cpu(cpu) {
  		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  		int cpustate = st->state;
  
  		if (cpustate >= state)
  			cpuhp_issue_call(cpu, state, false, node);
  	}
  
  remove:
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1728
1729
  	hlist_del(node);
  	mutex_unlock(&cpuhp_state_mutex);
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1730
  	cpus_read_unlock();
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1731
1732
1733
1734
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1735

5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1736
  /**
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1737
   * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1738
1739
1740
1741
   * @state:	The state to remove
   * @invoke:	If true, the teardown function is invoked for cpus where
   *		cpu state >= @state
   *
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1742
   * The caller needs to hold cpus read locked while calling this function.
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1743
1744
1745
   * The teardown callback is currently not allowed to fail. Think
   * about module removal!
   */
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1746
  void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1747
  {
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1748
  	struct cpuhp_step *sp = cpuhp_get_step(state);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1749
1750
1751
  	int cpu;
  
  	BUG_ON(cpuhp_cb_check(state));
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1752
  	lockdep_assert_cpus_held();
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1753

dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1754
  	mutex_lock(&cpuhp_state_mutex);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1755
1756
1757
1758
1759
1760
1761
  	if (sp->multi_instance) {
  		WARN(!hlist_empty(&sp->list),
  		     "Error: Removing state %d which has instances left.
  ",
  		     state);
  		goto remove;
  	}
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1762
  	if (!invoke || !cpuhp_get_teardown_cb(state))
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
  		goto remove;
  
  	/*
  	 * Call the teardown callback for each present cpu depending
  	 * on the hotplug state of the cpu. This function is not
  	 * allowed to fail currently!
  	 */
  	for_each_present_cpu(cpu) {
  		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  		int cpustate = st->state;
  
  		if (cpustate >= state)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1775
  			cpuhp_issue_call(cpu, state, false, NULL);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1776
1777
  	}
  remove:
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1778
  	cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1779
  	mutex_unlock(&cpuhp_state_mutex);
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1780
1781
1782
1783
1784
1785
1786
  }
  EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
  
  void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  {
  	cpus_read_lock();
  	__cpuhp_remove_state_cpuslocked(state, invoke);
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1787
  	cpus_read_unlock();
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1788
1789
  }
  EXPORT_SYMBOL(__cpuhp_remove_state);
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
  #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  static ssize_t show_cpuhp_state(struct device *dev,
  				struct device_attribute *attr, char *buf)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  
  	return sprintf(buf, "%d
  ", st->state);
  }
  static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
  static ssize_t write_cpuhp_target(struct device *dev,
  				  struct device_attribute *attr,
  				  const char *buf, size_t count)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  	struct cpuhp_step *sp;
  	int target, ret;
  
  	ret = kstrtoint(buf, 10, &target);
  	if (ret)
  		return ret;
  
  #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  	if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  		return -EINVAL;
  #else
  	if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  		return -EINVAL;
  #endif
  
  	ret = lock_device_hotplug_sysfs();
  	if (ret)
  		return ret;
  
  	mutex_lock(&cpuhp_state_mutex);
  	sp = cpuhp_get_step(target);
  	ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  	mutex_unlock(&cpuhp_state_mutex);
  	if (ret)
40da1b11f   Sebastian Andrzej Siewior   cpu/hotplug: Drop...
1829
  		goto out;
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1830
1831
1832
1833
1834
  
  	if (st->state < target)
  		ret = do_cpu_up(dev->id, target);
  	else
  		ret = do_cpu_down(dev->id, target);
40da1b11f   Sebastian Andrzej Siewior   cpu/hotplug: Drop...
1835
  out:
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1836
1837
1838
  	unlock_device_hotplug();
  	return ret ? ret : count;
  }
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1839
1840
1841
1842
1843
1844
1845
1846
  static ssize_t show_cpuhp_target(struct device *dev,
  				 struct device_attribute *attr, char *buf)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  
  	return sprintf(buf, "%d
  ", st->target);
  }
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1847
  static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1848

1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
  
  static ssize_t write_cpuhp_fail(struct device *dev,
  				struct device_attribute *attr,
  				const char *buf, size_t count)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  	struct cpuhp_step *sp;
  	int fail, ret;
  
  	ret = kstrtoint(buf, 10, &fail);
  	if (ret)
  		return ret;
  
  	/*
  	 * Cannot fail STARTING/DYING callbacks.
  	 */
  	if (cpuhp_is_atomic_state(fail))
  		return -EINVAL;
  
  	/*
  	 * Cannot fail anything that doesn't have callbacks.
  	 */
  	mutex_lock(&cpuhp_state_mutex);
  	sp = cpuhp_get_step(fail);
  	if (!sp->startup.single && !sp->teardown.single)
  		ret = -EINVAL;
  	mutex_unlock(&cpuhp_state_mutex);
  	if (ret)
  		return ret;
  
  	st->fail = fail;
  
  	return count;
  }
  
  static ssize_t show_cpuhp_fail(struct device *dev,
  			       struct device_attribute *attr, char *buf)
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  
  	return sprintf(buf, "%d
  ", st->fail);
  }
  
  static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1894
1895
1896
  static struct attribute *cpuhp_cpu_attrs[] = {
  	&dev_attr_state.attr,
  	&dev_attr_target.attr,
1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
1897
  	&dev_attr_fail.attr,
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1898
1899
  	NULL
  };
993647a29   Arvind Yadav   cpu/hotplug: Cons...
1900
  static const struct attribute_group cpuhp_cpu_attr_group = {
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
  	.attrs = cpuhp_cpu_attrs,
  	.name = "hotplug",
  	NULL
  };
  
  static ssize_t show_cpuhp_states(struct device *dev,
  				 struct device_attribute *attr, char *buf)
  {
  	ssize_t cur, res = 0;
  	int i;
  
  	mutex_lock(&cpuhp_state_mutex);
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1913
  	for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
  		struct cpuhp_step *sp = cpuhp_get_step(i);
  
  		if (sp->name) {
  			cur = sprintf(buf, "%3d: %s
  ", i, sp->name);
  			buf += cur;
  			res += cur;
  		}
  	}
  	mutex_unlock(&cpuhp_state_mutex);
  	return res;
  }
  static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  
  static struct attribute *cpuhp_cpu_root_attrs[] = {
  	&dev_attr_states.attr,
  	NULL
  };
993647a29   Arvind Yadav   cpu/hotplug: Cons...
1932
  static const struct attribute_group cpuhp_cpu_root_attr_group = {
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1933
1934
1935
1936
  	.attrs = cpuhp_cpu_root_attrs,
  	.name = "hotplug",
  	NULL
  };
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
  #ifdef CONFIG_HOTPLUG_SMT
  
  static const char *smt_states[] = {
  	[CPU_SMT_ENABLED]		= "on",
  	[CPU_SMT_DISABLED]		= "off",
  	[CPU_SMT_FORCE_DISABLED]	= "forceoff",
  	[CPU_SMT_NOT_SUPPORTED]		= "notsupported",
  };
  
  static ssize_t
  show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	return snprintf(buf, PAGE_SIZE - 2, "%s
  ", smt_states[cpu_smt_control]);
  }
  
  static void cpuhp_offline_cpu_device(unsigned int cpu)
  {
  	struct device *dev = get_cpu_device(cpu);
  
  	dev->offline = true;
  	/* Tell user space about the state change */
  	kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  }
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
1961
1962
1963
1964
1965
1966
1967
1968
  static void cpuhp_online_cpu_device(unsigned int cpu)
  {
  	struct device *dev = get_cpu_device(cpu);
  
  	dev->offline = false;
  	/* Tell user space about the state change */
  	kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  }
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
  static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
  {
  	int cpu, ret = 0;
  
  	cpu_maps_update_begin();
  	for_each_online_cpu(cpu) {
  		if (topology_is_primary_thread(cpu))
  			continue;
  		ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  		if (ret)
  			break;
  		/*
  		 * As this needs to hold the cpu maps lock it's impossible
  		 * to call device_offline() because that ends up calling
  		 * cpu_down() which takes cpu maps lock. cpu maps lock
  		 * needs to be held as this might race against in kernel
  		 * abusers of the hotplug machinery (thermal management).
  		 *
  		 * So nothing would update device:offline state. That would
  		 * leave the sysfs entry stale and prevent onlining after
  		 * smt control has been changed to 'off' again. This is
  		 * called under the sysfs hotplug lock, so it is properly
  		 * serialized against the regular offline usage.
  		 */
  		cpuhp_offline_cpu_device(cpu);
  	}
  	if (!ret)
  		cpu_smt_control = ctrlval;
  	cpu_maps_update_done();
  	return ret;
  }
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2000
  static int cpuhp_smt_enable(void)
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2001
  {
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2002
  	int cpu, ret = 0;
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2003
2004
  	cpu_maps_update_begin();
  	cpu_smt_control = CPU_SMT_ENABLED;
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
  	for_each_present_cpu(cpu) {
  		/* Skip online CPUs and CPUs on offline nodes */
  		if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
  			continue;
  		ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
  		if (ret)
  			break;
  		/* See comment in cpuhp_smt_disable() */
  		cpuhp_online_cpu_device(cpu);
  	}
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2015
  	cpu_maps_update_done();
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2016
  	return ret;
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
  }
  
  static ssize_t
  store_smt_control(struct device *dev, struct device_attribute *attr,
  		  const char *buf, size_t count)
  {
  	int ctrlval, ret;
  
  	if (sysfs_streq(buf, "on"))
  		ctrlval = CPU_SMT_ENABLED;
  	else if (sysfs_streq(buf, "off"))
  		ctrlval = CPU_SMT_DISABLED;
  	else if (sysfs_streq(buf, "forceoff"))
  		ctrlval = CPU_SMT_FORCE_DISABLED;
  	else
  		return -EINVAL;
  
  	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
  		return -EPERM;
  
  	if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  		return -ENODEV;
  
  	ret = lock_device_hotplug_sysfs();
  	if (ret)
  		return ret;
  
  	if (ctrlval != cpu_smt_control) {
  		switch (ctrlval) {
  		case CPU_SMT_ENABLED:
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2047
  			ret = cpuhp_smt_enable();
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
  			break;
  		case CPU_SMT_DISABLED:
  		case CPU_SMT_FORCE_DISABLED:
  			ret = cpuhp_smt_disable(ctrlval);
  			break;
  		}
  	}
  
  	unlock_device_hotplug();
  	return ret ? ret : count;
  }
  static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
  
  static ssize_t
  show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	bool active = topology_max_smt_threads() > 1;
  
  	return snprintf(buf, PAGE_SIZE - 2, "%d
  ", active);
  }
  static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
  
  static struct attribute *cpuhp_smt_attrs[] = {
  	&dev_attr_control.attr,
  	&dev_attr_active.attr,
  	NULL
  };
  
  static const struct attribute_group cpuhp_smt_attr_group = {
  	.attrs = cpuhp_smt_attrs,
  	.name = "smt",
  	NULL
  };
  
  static int __init cpu_smt_state_init(void)
  {
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2085
2086
2087
2088
2089
2090
2091
  	return sysfs_create_group(&cpu_subsys.dev_root->kobj,
  				  &cpuhp_smt_attr_group);
  }
  
  #else
  static inline int cpu_smt_state_init(void) { return 0; }
  #endif
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
2092
2093
2094
  static int __init cpuhp_sysfs_init(void)
  {
  	int cpu, ret;
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2095
2096
2097
  	ret = cpu_smt_state_init();
  	if (ret)
  		return ret;
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
  	ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  				 &cpuhp_cpu_root_attr_group);
  	if (ret)
  		return ret;
  
  	for_each_possible_cpu(cpu) {
  		struct device *dev = get_cpu_device(cpu);
  
  		if (!dev)
  			continue;
  		ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  		if (ret)
  			return ret;
  	}
  	return 0;
  }
  device_initcall(cpuhp_sysfs_init);
  #endif
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2116
2117
2118
2119
  /*
   * cpu_bit_bitmap[] is a special, "compressed" data structure that
   * represents all NR_CPUS bits binary values of 1<<nr.
   *
e0b582ec5   Rusty Russell   cpumask: convert ...
2120
   * It is used by cpumask_of() to get a constant address to a CPU
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2121
2122
   * mask value that has a single bit set only.
   */
b8d317d10   Mike Travis   cpumask: make cpu...
2123

e56b3bc79   Linus Torvalds   cpu masks: optimi...
2124
  /* cpu_bit_bitmap[0] is empty - so we can back into it */
4d51985e4   Michael Rodriguez   kernel/cpu.c: fix...
2125
  #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2126
2127
2128
  #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
b8d317d10   Mike Travis   cpumask: make cpu...
2129

e56b3bc79   Linus Torvalds   cpu masks: optimi...
2130
2131
2132
2133
2134
2135
2136
  const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  
  	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
  	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
  #if BITS_PER_LONG > 32
  	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
  	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
b8d317d10   Mike Travis   cpumask: make cpu...
2137
2138
  #endif
  };
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2139
  EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2d3854a37   Rusty Russell   cpumask: introduc...
2140
2141
2142
  
  const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  EXPORT_SYMBOL(cpu_all_bits);
b3199c025   Rusty Russell   cpumask: switch o...
2143
2144
  
  #ifdef CONFIG_INIT_ALL_POSSIBLE
4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2145
  struct cpumask __cpu_possible_mask __read_mostly
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2146
  	= {CPU_BITS_ALL};
b3199c025   Rusty Russell   cpumask: switch o...
2147
  #else
4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2148
  struct cpumask __cpu_possible_mask __read_mostly;
b3199c025   Rusty Russell   cpumask: switch o...
2149
  #endif
4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2150
  EXPORT_SYMBOL(__cpu_possible_mask);
b3199c025   Rusty Russell   cpumask: switch o...
2151

4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2152
2153
  struct cpumask __cpu_online_mask __read_mostly;
  EXPORT_SYMBOL(__cpu_online_mask);
b3199c025   Rusty Russell   cpumask: switch o...
2154

4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2155
2156
  struct cpumask __cpu_present_mask __read_mostly;
  EXPORT_SYMBOL(__cpu_present_mask);
b3199c025   Rusty Russell   cpumask: switch o...
2157

4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2158
2159
  struct cpumask __cpu_active_mask __read_mostly;
  EXPORT_SYMBOL(__cpu_active_mask);
3fa415206   Rusty Russell   cpumask: make set...
2160

3fa415206   Rusty Russell   cpumask: make set...
2161
2162
  void init_cpu_present(const struct cpumask *src)
  {
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2163
  	cpumask_copy(&__cpu_present_mask, src);
3fa415206   Rusty Russell   cpumask: make set...
2164
2165
2166
2167
  }
  
  void init_cpu_possible(const struct cpumask *src)
  {
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2168
  	cpumask_copy(&__cpu_possible_mask, src);
3fa415206   Rusty Russell   cpumask: make set...
2169
2170
2171
2172
  }
  
  void init_cpu_online(const struct cpumask *src)
  {
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2173
  	cpumask_copy(&__cpu_online_mask, src);
3fa415206   Rusty Russell   cpumask: make set...
2174
  }
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
  
  /*
   * Activate the first processor.
   */
  void __init boot_cpu_init(void)
  {
  	int cpu = smp_processor_id();
  
  	/* Mark the boot cpu "present", "online" etc for SMP and UP case */
  	set_cpu_online(cpu, true);
  	set_cpu_active(cpu, true);
  	set_cpu_present(cpu, true);
  	set_cpu_possible(cpu, true);
8ce371f98   Peter Zijlstra   lockdep: Fix per-...
2188
2189
2190
2191
  
  #ifdef CONFIG_SMP
  	__boot_cpu_id = cpu;
  #endif
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2192
2193
2194
2195
2196
  }
  
  /*
   * Must be called _AFTER_ setting up the per_cpu areas
   */
b7722f4ac   Linus Torvalds   init: rename and ...
2197
  void __init boot_cpu_hotplug_init(void)
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2198
  {
7bdbaba8e   Abel Vesa   cpu/hotplug: Non-...
2199
  #ifdef CONFIG_SMP
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
2200
  	this_cpu_write(cpuhp_state.booted_once, true);
7bdbaba8e   Abel Vesa   cpu/hotplug: Non-...
2201
  #endif
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
2202
  	this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2203
  }