Blame view

kernel/cpu.c 55 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>
36a4c5fc9   Thomas Gleixner   x86/speculation: ...
13
  #include <linux/sched/smt.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
  #include <linux/unistd.h>
  #include <linux/cpu.h>
cb79295e2   Anton Vorontsov   cpu: introduce cl...
16
17
  #include <linux/oom.h>
  #include <linux/rcupdate.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
18
  #include <linux/export.h>
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
19
  #include <linux/bug.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include <linux/kthread.h>
  #include <linux/stop_machine.h>
81615b624   Ingo Molnar   [PATCH] Convert k...
22
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/gfp.h>
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
24
  #include <linux/suspend.h>
a19423b98   Gautham R. Shenoy   CPU hotplug: Add ...
25
  #include <linux/lockdep.h>
345527b1e   Preeti U Murthy   clockevents: Fix ...
26
  #include <linux/tick.h>
a89941816   Thomas Gleixner   hotplug: Prevent ...
27
  #include <linux/irq.h>
941154bd6   Thomas Gleixner   watchdog/hardlock...
28
  #include <linux/nmi.h>
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
29
  #include <linux/smpboot.h>
e6d4989a9   Richard Weinberger   relayfs: Convert ...
30
  #include <linux/relay.h>
6731d4f12   Sebastian Andrzej Siewior   slab: Convert to ...
31
  #include <linux/slab.h>
fc8dffd37   Thomas Gleixner   cpu/hotplug: Conv...
32
  #include <linux/percpu-rwsem.h>
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
33

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

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

49dfe2a67   Thomas Gleixner   cpuhotplug: Link ...
76
  #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  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 ...
96
  #endif
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
97
98
99
100
101
102
103
  /**
   * 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...
104
   * @cant_stop:	Bringup/teardown can't be stopped at this step
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
105
106
   */
  struct cpuhp_step {
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
107
108
  	const char		*name;
  	union {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
109
110
111
112
  		int		(*single)(unsigned int cpu);
  		int		(*multi)(unsigned int cpu,
  					 struct hlist_node *node);
  	} startup;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
113
  	union {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
114
115
116
117
  		int		(*single)(unsigned int cpu);
  		int		(*multi)(unsigned int cpu,
  					 struct hlist_node *node);
  	} teardown;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
118
119
120
121
  	struct hlist_head	list;
  	bool			skip_onerr;
  	bool			cant_stop;
  	bool			multi_instance;
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
122
  };
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
123
  static DEFINE_MUTEX(cpuhp_state_mutex);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
124
  static struct cpuhp_step cpuhp_bp_states[];
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
125
  static struct cpuhp_step cpuhp_ap_states[];
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
126

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  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...
143
144
145
  /**
   * 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...
146
   * @state:	The state to do callbacks for
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
147
   * @bringup:	True if the bringup callback should be invoked
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
148
149
   * @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...
150
   *
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
151
   * Called from cpu hotplug and from the state register machinery.
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
152
   */
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
153
  static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
154
155
  				 bool bringup, struct hlist_node *node,
  				 struct hlist_node **lastp)
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
156
157
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
158
  	struct cpuhp_step *step = cpuhp_get_step(state);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
159
160
161
  	int (*cbm)(unsigned int cpu, struct hlist_node *node);
  	int (*cb)(unsigned int cpu);
  	int ret, cnt;
1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
162
163
164
165
166
167
168
169
  	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 ...
170
  	if (!step->multi_instance) {
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
171
  		WARN_ON_ONCE(lastp && *lastp);
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
172
  		cb = bringup ? step->startup.single : step->teardown.single;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
173
174
  		if (!cb)
  			return 0;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
175
  		trace_cpuhp_enter(cpu, st->target, state, cb);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
176
  		ret = cb(cpu);
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
177
  		trace_cpuhp_exit(cpu, st->state, state, ret);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
178
179
  		return ret;
  	}
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
180
  	cbm = bringup ? step->startup.multi : step->teardown.multi;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
181
182
183
184
185
  	if (!cbm)
  		return 0;
  
  	/* Single invocation for instance add/remove */
  	if (node) {
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
186
  		WARN_ON_ONCE(lastp && *lastp);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
187
188
189
190
191
192
193
194
195
  		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...
196
197
  		if (lastp && node == *lastp)
  			break;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
198
199
200
  		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...
201
202
203
204
205
206
207
  		if (ret) {
  			if (!lastp)
  				goto err;
  
  			*lastp = node;
  			return ret;
  		}
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
208
209
  		cnt++;
  	}
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
210
211
  	if (lastp)
  		*lastp = NULL;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
212
213
214
  	return 0;
  err:
  	/* Rollback the instances if one failed */
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
215
  	cbm = !bringup ? step->startup.multi : step->teardown.multi;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
216
217
218
219
220
221
  	if (!cbm)
  		return ret;
  
  	hlist_for_each(node, &step->list) {
  		if (!cnt--)
  			break;
724a86881   Peter Zijlstra   smp/hotplug: Call...
222
223
224
225
226
227
228
229
  
  		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...
230
231
232
  	}
  	return ret;
  }
98a79d6a5   Rusty Russell   cpumask: centrali...
233
  #ifdef CONFIG_SMP
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  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...
253
  /* Serializes the updates to cpu_online_mask, cpu_present_mask */
aa9538777   Linus Torvalds   cpu hotplug: simp...
254
  static DEFINE_MUTEX(cpu_add_remove_lock);
090e77c39   Thomas Gleixner   cpu/hotplug: Rest...
255
256
  bool cpuhp_tasks_frozen;
  EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257

79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
258
  /*
93ae4f978   Srivatsa S. Bhat   CPU hotplug: Prov...
259
260
   * 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...
261
262
263
264
265
266
267
268
269
270
   */
  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
271

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

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

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

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

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

16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
306
307
308
309
310
311
312
313
314
315
  /*
   * 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...
316
  	cpu_hotplug_disabled++;
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
317
318
  	cpu_maps_update_done();
  }
32145c467   Vitaly Kuznetsov   cpu-hotplug: expo...
319
  EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
320

01b411590   Lianwei Wang   cpu/hotplug: Hand...
321
322
323
324
325
326
327
  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...
328
329
330
  void cpu_hotplug_enable(void)
  {
  	cpu_maps_update_begin();
01b411590   Lianwei Wang   cpu/hotplug: Hand...
331
  	__cpu_hotplug_enable();
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
332
333
  	cpu_maps_update_done();
  }
32145c467   Vitaly Kuznetsov   cpu-hotplug: expo...
334
  EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
b9d10be7a   Toshi Kani   ACPI / processor:...
335
  #endif	/* CONFIG_HOTPLUG_CPU */
79a6cdeb7   Lai Jiangshan   cpuhotplug: do no...
336

36a4c5fc9   Thomas Gleixner   x86/speculation: ...
337
338
339
340
341
  /*
   * Architectures that need SMT-specific errata handling during SMT hotplug
   * should override this.
   */
  void __weak arch_smt_update(void) { }
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
342
343
  #ifdef CONFIG_HOTPLUG_SMT
  enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
c2fdbbb47   Konrad Rzeszutek Wilk   x86/KVM: Warn use...
344
  EXPORT_SYMBOL_GPL(cpu_smt_control);
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
345

9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
346
  static bool cpu_smt_available __read_mostly;
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
347
  void __init cpu_smt_disable(bool force)
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
348
  {
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
349
350
351
352
353
  	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  		cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  		return;
  
  	if (force) {
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
354
355
356
  		pr_info("SMT: Force disabled
  ");
  		cpu_smt_control = CPU_SMT_FORCE_DISABLED;
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
357
358
  	} else {
  		cpu_smt_control = CPU_SMT_DISABLED;
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
359
  	}
8e41ddda3   Jiri Kosina   cpu/hotplug: Expo...
360
  }
476d29ab7   Thomas Gleixner   cpu/hotplug: Set ...
361
362
  /*
   * The decision whether SMT is supported can only be done after the full
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
363
364
   * CPU identification. Called from architecture code before non boot CPUs
   * are brought up.
476d29ab7   Thomas Gleixner   cpu/hotplug: Set ...
365
   */
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
366
  void __init cpu_smt_check_topology_early(void)
476d29ab7   Thomas Gleixner   cpu/hotplug: Set ...
367
368
369
370
  {
  	if (!topology_smt_supported())
  		cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  }
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
371
372
373
374
375
376
377
378
379
380
381
382
  /*
   * 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...
383
384
385
  static int __init smt_cmdline_disable(char *str)
  {
  	cpu_smt_disable(str && !strcmp(str, "force"));
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
386
387
388
389
390
391
  	return 0;
  }
  early_param("nosmt", smt_cmdline_disable);
  
  static inline bool cpu_smt_allowed(unsigned int cpu)
  {
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
392
  	if (topology_is_primary_thread(cpu))
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
393
  		return true;
9eb0a3cce   Thomas Gleixner   cpu/hotplug: Fix ...
394
395
396
397
398
399
400
401
402
  	/*
  	 * 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...
403
404
405
406
407
408
409
410
411
412
413
414
415
  		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...
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
459
460
461
462
463
464
  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...
465
  	wait_for_ap_thread(st, st->bringup);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  }
  
  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...
482

8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
483
484
485
  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...
486
  	/* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
487
  	wait_for_ap_thread(st, true);
dea1d0f5f   Thomas Gleixner   smp/hotplug: Repl...
488
489
  	if (WARN_ON_ONCE((!cpu_online(cpu))))
  		return -ECANCELED;
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
490
491
492
493
  
  	/* 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...
494
495
496
497
498
499
500
501
502
  	/*
  	 * 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...
503
504
505
506
  	if (st->target <= CPUHP_AP_ONLINE_IDLE)
  		return 0;
  
  	return cpuhp_kick_ap(st, st->target);
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
507
  }
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
508
509
510
511
  static int bringup_cpu(unsigned int cpu)
  {
  	struct task_struct *idle = idle_thread_get(cpu);
  	int ret;
aa877175e   Boris Ostrovsky   cpu/hotplug: Prev...
512
513
514
515
516
517
  	/*
  	 * 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...
518
519
  	/* Arch-specific enabling code. */
  	ret = __cpu_up(cpu, idle);
aa877175e   Boris Ostrovsky   cpu/hotplug: Prev...
520
  	irq_unlock_sparse();
530e9b76a   Thomas Gleixner   cpu/hotplug: Remo...
521
  	if (ret)
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
522
  		return ret;
9cd4f1a4e   Thomas Gleixner   smp/hotplug: Move...
523
  	return bringup_wait_for_ap(cpu);
ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
524
  }
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
525
526
527
  /*
   * Hotplug state machine related functions
   */
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
528

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
529
  static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
530
531
  {
  	for (st->state--; st->state > st->target; st->state--) {
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
532
  		struct cpuhp_step *step = cpuhp_get_step(st->state);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
533
534
  
  		if (!step->skip_onerr)
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
535
  			cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
536
537
538
539
  	}
  }
  
  static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
540
  			      enum cpuhp_state target)
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
541
542
543
544
545
  {
  	enum cpuhp_state prev_state = st->state;
  	int ret = 0;
  
  	while (st->state < target) {
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
546
  		st->state++;
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
547
  		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
548
549
  		if (ret) {
  			st->target = prev_state;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
550
  			undo_cpu_up(cpu, st);
2e1a3483c   Thomas Gleixner   cpu/hotplug: Spli...
551
552
553
554
555
  			break;
  		}
  	}
  	return ret;
  }
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
556
557
558
559
560
561
  /*
   * 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...
562
563
  	init_completion(&st->done_up);
  	init_completion(&st->done_down);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
564
565
566
567
568
569
570
571
  }
  
  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...
572
573
574
  /*
   * 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...
575
576
577
578
579
580
581
582
583
584
   *
   * 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...
585
586
587
588
   */
  static void cpuhp_thread_fun(unsigned int cpu)
  {
  	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
589
590
  	bool bringup = st->bringup;
  	enum cpuhp_state state;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
591

cb2625854   Neeraj Upadhyay   cpu/hotplug: Adju...
592
593
  	if (WARN_ON_ONCE(!st->should_run))
  		return;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
594
  	/*
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
595
596
  	 * 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...
597
598
  	 */
  	smp_mb();
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
599

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

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
602
  	if (st->single) {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
603
604
605
606
607
608
609
610
  		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...
611
  		} else {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
612
613
614
615
  			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...
616
  		}
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  	}
  
  	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 ...
631

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
632
633
634
635
  		/*
  		 * STARTING/DYING must not fail!
  		 */
  		WARN_ON_ONCE(st->result);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
636
  	} else {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
637
638
639
640
641
642
643
644
645
646
647
  		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...
648
  	}
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
649
650
  
  next:
5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
651
  	cpuhp_lock_release(bringup);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
652
653
  
  	if (!st->should_run)
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
654
  		complete_ap_thread(st, bringup);
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
655
656
657
  }
  
  /* Invoke a single callback on a remote cpu */
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
658
  static int
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
659
660
  cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
  			 struct hlist_node *node)
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
661
662
  {
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
663
  	int ret;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
664
665
666
  
  	if (!cpu_online(cpu))
  		return 0;
5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
667
668
669
670
671
  	cpuhp_lock_acquire(false);
  	cpuhp_lock_release(false);
  
  	cpuhp_lock_acquire(true);
  	cpuhp_lock_release(true);
49dfe2a67   Thomas Gleixner   cpuhotplug: Link ...
672

6a4e24518   Thomas Gleixner   cpu/hotplug: Hand...
673
674
675
676
677
  	/*
  	 * 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...
678
  		return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
6a4e24518   Thomas Gleixner   cpu/hotplug: Hand...
679

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
680
681
682
683
684
  	st->rollback = false;
  	st->last = NULL;
  
  	st->node = node;
  	st->bringup = bringup;
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
685
  	st->cb_state = state;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
686
  	st->single = true;
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
687

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

4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
690
  	/*
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
691
  	 * If we failed and did a partial, do a rollback.
4cb28ced2   Thomas Gleixner   cpu/hotplug: Crea...
692
  	 */
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
693
694
695
696
697
698
  	if ((ret = st->result) && st->last) {
  		st->rollback = true;
  		st->bringup = !bringup;
  
  		__cpuhp_kick_ap(st);
  	}
1f7c70d6b   Thomas Gleixner   cpu/hotplug: Rese...
699
700
701
702
703
  	/*
  	 * Clean up the leftovers so the next hotplug operation wont use stale
  	 * data.
  	 */
  	st->node = st->last = NULL;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
704
  	return ret;
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
705
706
707
708
709
  }
  
  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...
710
711
  	enum cpuhp_state prev_state = st->state;
  	int ret;
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
712

5f4b55e10   Peter Zijlstra   smp/hotplug: Diff...
713
714
715
716
717
  	cpuhp_lock_acquire(false);
  	cpuhp_lock_release(false);
  
  	cpuhp_lock_acquire(true);
  	cpuhp_lock_release(true);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
718
719
720
721
722
723
  
  	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...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
  }
  
  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...
740
  #ifdef CONFIG_HOTPLUG_CPU
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
741
742
743
744
745
746
747
748
749
750
751
752
  /**
   * 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...
753
754
755
756
757
758
759
760
761
762
763
  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...
764
  	WARN_ON(cpu_online(cpu));
cb79295e2   Anton Vorontsov   cpu: introduce cl...
765
766
767
  	rcu_read_lock();
  	for_each_process(p) {
  		struct task_struct *t;
e4cc2f873   Anton Vorontsov   kernel/cpu.c: doc...
768
769
770
771
  		/*
  		 * Main thread might exit, but other threads may still have
  		 * a valid mm. Find one.
  		 */
cb79295e2   Anton Vorontsov   cpu: introduce cl...
772
773
774
775
776
777
778
779
  		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
780
  /* Take this CPU down. */
71cf5aeeb   Mathias Krause   kernel, cpu: Remo...
781
  static int take_cpu_down(void *_param)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
  {
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
783
784
  	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...
785
  	int err, cpu = smp_processor_id();
724a86881   Peter Zijlstra   smp/hotplug: Call...
786
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
  	/* Ensure this CPU doesn't handle any more interrupts. */
  	err = __cpu_disable();
  	if (err < 0)
f37051364   Zwane Mwaikambo   [PATCH] i386 CPU ...
791
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792

a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
793
794
795
796
797
798
  	/*
  	 * 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...
799
  	/* Invoke the former CPU_DYING callbacks */
724a86881   Peter Zijlstra   smp/hotplug: Call...
800
801
802
803
804
805
806
  	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...
807

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

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

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

48c5ccae8   Peter Zijlstra   sched: Simplify c...
840
  	/*
ee1e714b9   Thomas Gleixner   cpu/hotplug: Remo...
841
  	 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
48c5ccae8   Peter Zijlstra   sched: Simplify c...
842
843
  	 * 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...
844
845
  	 *
  	 * Wait for the stop thread to go away.
48c5ccae8   Peter Zijlstra   sched: Simplify c...
846
  	 */
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
847
  	wait_for_ap_thread(st, false);
e69aab131   Thomas Gleixner   cpu/hotplug: Make...
848
  	BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849

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

71f87b2fc   Thomas Gleixner   cpu/hotplug: Plug...
860
861
862
  static void cpuhp_complete_idle_dead(void *arg)
  {
  	struct cpuhp_cpu_state *st = arg;
5ebe7742f   Peter Zijlstra   smp/hotplug: Diff...
863
  	complete_ap_thread(st, false);
71f87b2fc   Thomas Gleixner   cpu/hotplug: Plug...
864
  }
e69aab131   Thomas Gleixner   cpu/hotplug: Make...
865
866
867
868
869
  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...
870
  	rcu_report_dead(smp_processor_id());
71f87b2fc   Thomas Gleixner   cpu/hotplug: Plug...
871
872
873
874
875
876
877
  	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...
878
  }
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
879
880
881
882
  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...
883

4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
  		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...
899
900
  			if (st->state < prev_state)
  				undo_cpu_down(cpu, st);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
901
902
903
904
905
  			break;
  		}
  	}
  	return ret;
  }
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
906

984581728   Thomas Gleixner   cpu/hotplug: Spli...
907
  /* Requires cpu_add_remove_lock to be held */
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
908
909
  static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  			   enum cpuhp_state target)
984581728   Thomas Gleixner   cpu/hotplug: Spli...
910
  {
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
911
912
  	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  	int prev_state, ret = 0;
984581728   Thomas Gleixner   cpu/hotplug: Spli...
913
914
915
  
  	if (num_online_cpus() == 1)
  		return -EBUSY;
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
916
  	if (!cpu_present(cpu))
984581728   Thomas Gleixner   cpu/hotplug: Spli...
917
  		return -EINVAL;
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
918
  	cpus_write_lock();
984581728   Thomas Gleixner   cpu/hotplug: Spli...
919
920
  
  	cpuhp_tasks_frozen = tasks_frozen;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
921
  	prev_state = cpuhp_set_state(st, target);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
922
923
924
925
  	/*
  	 * 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 ...
926
  	if (st->state > CPUHP_TEARDOWN_CPU) {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
927
  		st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
928
929
930
931
932
933
934
935
936
937
938
939
  		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 ...
940
  		if (st->state > CPUHP_TEARDOWN_CPU)
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
941
  			goto out;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
942
943
  
  		st->target = target;
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
944
945
  	}
  	/*
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
946
  	 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
947
948
  	 * to do the further cleanups.
  	 */
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
949
  	ret = cpuhp_down_callbacks(cpu, st, target);
1d92a611d   Thomas Gleixner   cpu/hotplug: Prev...
950
  	if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
951
952
  		cpuhp_reset_state(st, prev_state);
  		__cpuhp_kick_ap(st);
3b9d6da67   Sebastian Andrzej Siewior   cpu/hotplug: Fix ...
953
  	}
984581728   Thomas Gleixner   cpu/hotplug: Spli...
954

1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
955
  out:
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
956
  	cpus_write_unlock();
941154bd6   Thomas Gleixner   watchdog/hardlock...
957
958
959
960
961
  	/*
  	 * Do post unplug cleanup. This is still protected against
  	 * concurrent CPU hotplug via cpu_add_remove_lock.
  	 */
  	lockup_detector_cleanup();
36a4c5fc9   Thomas Gleixner   x86/speculation: ...
962
  	arch_smt_update();
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
963
  	return ret;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
964
  }
6beba29c6   Thomas Gleixner   cpu/hotplug: Spli...
965
966
967
968
969
970
  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...
971
  static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
972
  {
9ea09af3b   Heiko Carstens   stop_machine: int...
973
  	int err;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
974

d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
975
  	cpu_maps_update_begin();
6beba29c6   Thomas Gleixner   cpu/hotplug: Spli...
976
  	err = cpu_down_maps_locked(cpu, target);
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
977
  	cpu_maps_update_done();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
978
979
  	return err;
  }
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
980

af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
981
982
983
984
  int cpu_down(unsigned int cpu)
  {
  	return do_cpu_down(cpu, CPUHP_OFFLINE);
  }
b62b8ef90   Zhang Rui   force offline the...
985
  EXPORT_SYMBOL(cpu_down);
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
986
987
988
  
  #else
  #define takedown_cpu		NULL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
989
  #endif /*CONFIG_HOTPLUG_CPU*/
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
990
  /**
ee1e714b9   Thomas Gleixner   cpu/hotplug: Remo...
991
   * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
992
993
   * @cpu: cpu that just started
   *
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
994
995
996
997
998
999
1000
   * 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...
1001
  	int ret;
4baa0afc6   Thomas Gleixner   cpu/hotplug: Conv...
1002

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

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

757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1039
  	if (!cpu_present(cpu)) {
5e5041f35   Yasuaki Ishimatsu   ACPI / processor:...
1040
1041
1042
  		ret = -EINVAL;
  		goto out;
  	}
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1043
1044
1045
1046
1047
  	/*
  	 * 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 ...
1048
  		goto out;
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1049
1050
1051
1052
1053
1054
1055
1056
  
  	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...
1057
  	}
38498a67a   Thomas Gleixner   smp: Add generic ...
1058

ba9974624   Thomas Gleixner   cpu/hotplug: Rest...
1059
  	cpuhp_tasks_frozen = tasks_frozen;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
1060
  	cpuhp_set_state(st, target);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1061
1062
1063
1064
  	/*
  	 * 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 ...
1065
  	if (st->state > CPUHP_BRINGUP_CPU) {
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
  		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 ...
1077
  	 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1078
1079
  	 * responsible for bringing it up to the target state.
  	 */
8df3e07e7   Thomas Gleixner   cpu/hotplug: Let ...
1080
  	target = min((int)target, CPUHP_BRINGUP_CPU);
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1081
  	ret = cpuhp_up_callbacks(cpu, st, target);
38498a67a   Thomas Gleixner   smp: Add generic ...
1082
  out:
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1083
  	cpus_write_unlock();
36a4c5fc9   Thomas Gleixner   x86/speculation: ...
1084
  	arch_smt_update();
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1085
1086
  	return ret;
  }
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1087
  static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1088
1089
  {
  	int err = 0;
cf23422b9   minskey guo   cpu/mem hotplug: ...
1090

e0b582ec5   Rusty Russell   cpumask: convert ...
1091
  	if (!cpu_possible(cpu)) {
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1092
1093
1094
  		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...
1095
  #if defined(CONFIG_IA64)
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1096
1097
  		pr_err("please check additional_cpus= boot parameter
  ");
73e753a50   KAMEZAWA Hiroyuki   CPU HOTPLUG: avoi...
1098
1099
1100
  #endif
  		return -EINVAL;
  	}
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1101

01b0f1970   Toshi Kani   cpu/mem hotplug: ...
1102
1103
1104
  	err = try_online_node(cpu_to_node(cpu));
  	if (err)
  		return err;
cf23422b9   minskey guo   cpu/mem hotplug: ...
1105

d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1106
  	cpu_maps_update_begin();
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1107
1108
  
  	if (cpu_hotplug_disabled) {
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1109
  		err = -EBUSY;
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1110
1111
  		goto out;
  	}
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
1112
1113
1114
1115
  	if (!cpu_smt_allowed(cpu)) {
  		err = -EPERM;
  		goto out;
  	}
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1116

af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1117
  	err = _cpu_up(cpu, 0, target);
e761b7725   Max Krasnyansky   cpu hotplug, sche...
1118
  out:
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1119
  	cpu_maps_update_done();
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1120
1121
  	return err;
  }
af1f40457   Thomas Gleixner   cpu/hotplug: Hand...
1122
1123
1124
1125
1126
  
  int cpu_up(unsigned int cpu)
  {
  	return do_cpu_up(cpu, CPUHP_ONLINE);
  }
a513f6bab   Paul E. McKenney   cpu: Export cpu_up()
1127
  EXPORT_SYMBOL_GPL(cpu_up);
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1128

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

d391e5522   James Morse   cpu/hotplug: Allo...
1132
  int freeze_secondary_cpus(int primary)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1133
  {
d391e5522   James Morse   cpu/hotplug: Allo...
1134
  	int cpu, error = 0;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1135

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

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

89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
1162
  	if (!error)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1163
  		BUG_ON(num_online_cpus() > 1);
89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
1164
  	else
84117da5b   Fabian Frederick   kernel/cpu.c: con...
1165
1166
  		pr_err("Non-boot CPUs are not disabled
  ");
89af7ba57   Vitaly Kuznetsov   cpu-hotplug: conv...
1167
1168
1169
1170
1171
1172
1173
  
  	/*
  	 * 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...
1174
  	cpu_maps_update_done();
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1175
1176
  	return error;
  }
d0af9eed5   Suresh Siddha   x86, pat/mtrr: Re...
1177
1178
1179
1180
1181
1182
1183
  void __weak arch_enable_nonboot_cpus_begin(void)
  {
  }
  
  void __weak arch_enable_nonboot_cpus_end(void)
  {
  }
71cf5aeeb   Mathias Krause   kernel, cpu: Remo...
1184
  void enable_nonboot_cpus(void)
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1185
1186
1187
1188
  {
  	int cpu, error;
  
  	/* Allow everyone to use the CPU hotplug again */
d221938c0   Gautham R Shenoy   cpu-hotplug: refc...
1189
  	cpu_maps_update_begin();
01b411590   Lianwei Wang   cpu/hotplug: Hand...
1190
  	__cpu_hotplug_enable();
e0b582ec5   Rusty Russell   cpumask: convert ...
1191
  	if (cpumask_empty(frozen_cpus))
1d64b9cb1   Rafael J. Wysocki   [PATCH] Fix micro...
1192
  		goto out;
e3920fb42   Rafael J. Wysocki   [PATCH] Disable C...
1193

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

d7268a31c   Fenghua Yu   CPU: Add right qu...
1217
  static int __init alloc_frozen_cpus(void)
e0b582ec5   Rusty Russell   cpumask: convert ...
1218
1219
1220
1221
1222
1223
  {
  	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...
1224
1225
  
  /*
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
   * 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...
1244
  		cpu_hotplug_disable();
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1245
1246
1247
1248
  		break;
  
  	case PM_POST_SUSPEND:
  	case PM_POST_HIBERNATION:
16e53dbf1   Srivatsa S. Bhat   CPU hotplug: prov...
1249
  		cpu_hotplug_enable();
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1250
1251
1252
1253
1254
1255
1256
1257
  		break;
  
  	default:
  		return NOTIFY_DONE;
  	}
  
  	return NOTIFY_OK;
  }
d7268a31c   Fenghua Yu   CPU: Add right qu...
1258
  static int __init cpu_hotplug_pm_sync_init(void)
79cfbdfa8   Srivatsa S. Bhat   PM / Sleep: Fix r...
1259
  {
6e32d479d   Fenghua Yu   kernel/cpu.c: Add...
1260
1261
1262
1263
1264
  	/*
  	 * 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...
1265
1266
1267
1268
  	pm_notifier(cpu_hotplug_pm_callback, 0);
  	return 0;
  }
  core_initcall(cpu_hotplug_pm_sync_init);
f3de4be9d   Rafael J. Wysocki   PM: Fix dependenc...
1269
  #endif /* CONFIG_PM_SLEEP_SMP */
68f4f1ec0   Max Krasnyansky   sched: Move cpu m...
1270

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

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

4205e4786   Thomas Gleixner   cpu/hotplug: Prov...
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
  	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...
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
  			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...
1478
1479
1480
  {
  	/* (Un)Install the callbacks for further cpu hotplug operations */
  	struct cpuhp_step *sp;
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1481
  	int ret = 0;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1482

0c96b2730   Ethan Barnes   smp/hotplug: Hand...
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
  	/*
  	 * 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...
1494
1495
  		ret = cpuhp_reserve_state(state);
  		if (ret < 0)
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1496
  			return ret;
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1497
1498
  		state = ret;
  	}
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1499
  	sp = cpuhp_get_step(state);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1500
1501
  	if (name && sp->name)
  		return -EBUSY;
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1502
1503
  	sp->startup.single = startup;
  	sp->teardown.single = teardown;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1504
  	sp->name = name;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1505
1506
  	sp->multi_instance = multi_instance;
  	INIT_HLIST_HEAD(&sp->list);
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1507
  	return ret;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1508
1509
1510
1511
  }
  
  static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  {
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1512
  	return cpuhp_get_step(state)->teardown.single;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1513
  }
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1514
1515
1516
1517
  /*
   * Call the startup/teardown function for a step either on the AP or
   * on the current CPU.
   */
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1518
1519
  static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  			    struct hlist_node *node)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1520
  {
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1521
  	struct cpuhp_step *sp = cpuhp_get_step(state);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1522
  	int ret;
4dddfb5fa   Peter Zijlstra   smp/hotplug: Rewr...
1523
1524
1525
1526
  	/*
  	 * If there's nothing to do, we done.
  	 * Relies on the union for multi_instance.
  	 */
3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1527
1528
  	if ((bringup && !sp->startup.single) ||
  	    (!bringup && !sp->teardown.single))
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1529
  		return 0;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1530
1531
1532
1533
  	/*
  	 * 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...
1534
1535
  #ifdef CONFIG_SMP
  	if (cpuhp_is_ap_state(state))
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1536
  		ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1537
  	else
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
1538
  		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1539
  #else
96abb9685   Peter Zijlstra   smp/hotplug: Allo...
1540
  	ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1cf4f629d   Thomas Gleixner   cpu/hotplug: Move...
1541
  #endif
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
  	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 ...
1552
  				   struct hlist_node *node)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1553
1554
  {
  	int cpu;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
  	/* 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 ...
1565
  			cpuhp_issue_call(cpu, state, false, node);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1566
1567
  	}
  }
9805c6733   Thomas Gleixner   cpu/hotplug: Add ...
1568
1569
1570
  int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
  					  struct hlist_node *node,
  					  bool invoke)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1571
1572
1573
1574
  {
  	struct cpuhp_step *sp;
  	int cpu;
  	int ret;
9805c6733   Thomas Gleixner   cpu/hotplug: Add ...
1575
  	lockdep_assert_cpus_held();
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1576
1577
1578
  	sp = cpuhp_get_step(state);
  	if (sp->multi_instance == false)
  		return -EINVAL;
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1579
  	mutex_lock(&cpuhp_state_mutex);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1580

3c1627e99   Thomas Gleixner   cpu/hotplug: Repl...
1581
  	if (!invoke || !sp->startup.multi)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
  		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...
1597
  			if (sp->teardown.multi)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1598
  				cpuhp_rollback_install(cpu, state, node);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1599
  			goto unlock;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1600
1601
1602
1603
  		}
  	}
  add_node:
  	ret = 0;
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1604
  	hlist_add_head(node, &sp->list);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1605
  unlock:
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1606
  	mutex_unlock(&cpuhp_state_mutex);
9805c6733   Thomas Gleixner   cpu/hotplug: Add ...
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
  	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...
1617
  	cpus_read_unlock();
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1618
1619
1620
  	return ret;
  }
  EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1621
  /**
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1622
   * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1623
1624
1625
1626
1627
1628
1629
   * @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...
1630
   *
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1631
   * The caller needs to hold cpus read locked while calling this function.
512f09801   Boris Ostrovsky   cpu/hotplug: Clar...
1632
1633
1634
1635
1636
   * 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...
1637
   */
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1638
1639
1640
1641
1642
  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...
1643
1644
  {
  	int cpu, ret = 0;
b9d9d6911   Thomas Gleixner   smp/hotplug: Undo...
1645
  	bool dynstate;
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1646

71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1647
  	lockdep_assert_cpus_held();
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1648
1649
  	if (cpuhp_cb_check(state) || !name)
  		return -EINVAL;
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1650
  	mutex_lock(&cpuhp_state_mutex);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1651

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

b9d9d6911   Thomas Gleixner   smp/hotplug: Undo...
1655
1656
1657
1658
1659
  	dynstate = state == CPUHP_AP_ONLINE_DYN;
  	if (ret > 0 && dynstate) {
  		state = ret;
  		ret = 0;
  	}
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1660
  	if (ret || !invoke || !startup)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
  		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 ...
1673
  		ret = cpuhp_issue_call(cpu, state, true, NULL);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1674
  		if (ret) {
a724632ca   Thomas Gleixner   cpu/hotplug: Rewo...
1675
  			if (teardown)
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1676
1677
  				cpuhp_rollback_install(cpu, state, NULL);
  			cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1678
1679
1680
1681
  			goto out;
  		}
  	}
  out:
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1682
  	mutex_unlock(&cpuhp_state_mutex);
dc280d936   Thomas Gleixner   cpu/hotplug: Prev...
1683
1684
1685
1686
  	/*
  	 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
  	 * dynamically allocated state in case of success.
  	 */
b9d9d6911   Thomas Gleixner   smp/hotplug: Undo...
1687
  	if (!ret && dynstate)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1688
1689
1690
  		return state;
  	return ret;
  }
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
  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...
1707
  EXPORT_SYMBOL(__cpuhp_setup_state);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
  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...
1718
  	cpus_read_lock();
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1719
  	mutex_lock(&cpuhp_state_mutex);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
  	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 ...
1736
1737
  	hlist_del(node);
  	mutex_unlock(&cpuhp_state_mutex);
8f553c498   Thomas Gleixner   cpu/hotplug: Prov...
1738
  	cpus_read_unlock();
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1739
1740
1741
1742
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1743

5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1744
  /**
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1745
   * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1746
1747
1748
1749
   * @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...
1750
   * The caller needs to hold cpus read locked while calling this function.
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1751
1752
1753
   * The teardown callback is currently not allowed to fail. Think
   * about module removal!
   */
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1754
  void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1755
  {
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1756
  	struct cpuhp_step *sp = cpuhp_get_step(state);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1757
1758
1759
  	int cpu;
  
  	BUG_ON(cpuhp_cb_check(state));
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1760
  	lockdep_assert_cpus_held();
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1761

dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1762
  	mutex_lock(&cpuhp_state_mutex);
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1763
1764
1765
1766
1767
1768
1769
  	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...
1770
  	if (!invoke || !cpuhp_get_teardown_cb(state))
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
  		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 ...
1783
  			cpuhp_issue_call(cpu, state, false, NULL);
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1784
1785
  	}
  remove:
cf392d10b   Thomas Gleixner   cpu/hotplug: Add ...
1786
  	cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
dc434e056   Sebastian Andrzej Siewior   cpu/hotplug: Seri...
1787
  	mutex_unlock(&cpuhp_state_mutex);
71def423f   Sebastian Andrzej Siewior   cpu/hotplug: Prov...
1788
1789
1790
1791
1792
1793
1794
  }
  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...
1795
  	cpus_read_unlock();
5b7aa87e0   Thomas Gleixner   cpu/hotplug: Impl...
1796
1797
  }
  EXPORT_SYMBOL(__cpuhp_remove_state);
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
  #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...
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
  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...
1837
  		goto out;
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1838
1839
1840
1841
1842
  
  	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...
1843
  out:
757c989b9   Thomas Gleixner   cpu/hotplug: Make...
1844
1845
1846
  	unlock_device_hotplug();
  	return ret ? ret : count;
  }
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1847
1848
1849
1850
1851
1852
1853
1854
  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...
1855
  static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1856

1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
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
1894
1895
1896
1897
1898
1899
1900
1901
  
  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 ...
1902
1903
1904
  static struct attribute *cpuhp_cpu_attrs[] = {
  	&dev_attr_state.attr,
  	&dev_attr_target.attr,
1db49484f   Peter Zijlstra   smp/hotplug: Hotp...
1905
  	&dev_attr_fail.attr,
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1906
1907
  	NULL
  };
993647a29   Arvind Yadav   cpu/hotplug: Cons...
1908
  static const struct attribute_group cpuhp_cpu_attr_group = {
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
  	.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...
1921
  	for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
  		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...
1940
  static const struct attribute_group cpuhp_cpu_root_attr_group = {
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
1941
1942
1943
1944
  	.attrs = cpuhp_cpu_root_attrs,
  	.name = "hotplug",
  	NULL
  };
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
  #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...
1969
1970
1971
1972
1973
1974
1975
1976
  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...
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
  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);
  	}
300a6f27d   Jiri Kosina   x86/speculation: ...
2003
  	if (!ret) {
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2004
  		cpu_smt_control = ctrlval;
300a6f27d   Jiri Kosina   x86/speculation: ...
2005
2006
  		arch_smt_update();
  	}
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2007
2008
2009
  	cpu_maps_update_done();
  	return ret;
  }
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2010
  static int cpuhp_smt_enable(void)
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2011
  {
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2012
  	int cpu, ret = 0;
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2013
2014
  	cpu_maps_update_begin();
  	cpu_smt_control = CPU_SMT_ENABLED;
300a6f27d   Jiri Kosina   x86/speculation: ...
2015
  	arch_smt_update();
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
  	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...
2026
  	cpu_maps_update_done();
ac10995a7   Thomas Gleixner   cpu/hotplug: Onli...
2027
  	return ret;
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
  }
  
  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...
2058
  			ret = cpuhp_smt_enable();
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
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
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
  			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...
2096
2097
2098
2099
2100
2101
2102
  	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 ...
2103
2104
2105
  static int __init cpuhp_sysfs_init(void)
  {
  	int cpu, ret;
c5ac43ee8   Thomas Gleixner   cpu/hotplug: Prov...
2106
2107
2108
  	ret = cpu_smt_state_init();
  	if (ret)
  		return ret;
98f8cdce1   Thomas Gleixner   cpu/hotplug: Add ...
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
  	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...
2127
2128
2129
2130
  /*
   * 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 ...
2131
   * It is used by cpumask_of() to get a constant address to a CPU
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2132
2133
   * mask value that has a single bit set only.
   */
b8d317d10   Mike Travis   cpumask: make cpu...
2134

e56b3bc79   Linus Torvalds   cpu masks: optimi...
2135
  /* cpu_bit_bitmap[0] is empty - so we can back into it */
4d51985e4   Michael Rodriguez   kernel/cpu.c: fix...
2136
  #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2137
2138
2139
  #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...
2140

e56b3bc79   Linus Torvalds   cpu masks: optimi...
2141
2142
2143
2144
2145
2146
2147
  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...
2148
2149
  #endif
  };
e56b3bc79   Linus Torvalds   cpu masks: optimi...
2150
  EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2d3854a37   Rusty Russell   cpumask: introduc...
2151
2152
2153
  
  const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  EXPORT_SYMBOL(cpu_all_bits);
b3199c025   Rusty Russell   cpumask: switch o...
2154
2155
  
  #ifdef CONFIG_INIT_ALL_POSSIBLE
4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2156
  struct cpumask __cpu_possible_mask __read_mostly
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2157
  	= {CPU_BITS_ALL};
b3199c025   Rusty Russell   cpumask: switch o...
2158
  #else
4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2159
  struct cpumask __cpu_possible_mask __read_mostly;
b3199c025   Rusty Russell   cpumask: switch o...
2160
  #endif
4b804c85d   Rasmus Villemoes   kernel/cpu.c: exp...
2161
  EXPORT_SYMBOL(__cpu_possible_mask);
b3199c025   Rusty Russell   cpumask: switch o...
2162

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

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

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

3fa415206   Rusty Russell   cpumask: make set...
2172
2173
  void init_cpu_present(const struct cpumask *src)
  {
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2174
  	cpumask_copy(&__cpu_present_mask, src);
3fa415206   Rusty Russell   cpumask: make set...
2175
2176
2177
2178
  }
  
  void init_cpu_possible(const struct cpumask *src)
  {
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2179
  	cpumask_copy(&__cpu_possible_mask, src);
3fa415206   Rusty Russell   cpumask: make set...
2180
2181
2182
2183
  }
  
  void init_cpu_online(const struct cpumask *src)
  {
c4c54dd1c   Rasmus Villemoes   kernel/cpu.c: cha...
2184
  	cpumask_copy(&__cpu_online_mask, src);
3fa415206   Rusty Russell   cpumask: make set...
2185
  }
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
  
  /*
   * 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-...
2199
2200
2201
2202
  
  #ifdef CONFIG_SMP
  	__boot_cpu_id = cpu;
  #endif
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2203
2204
2205
2206
2207
  }
  
  /*
   * Must be called _AFTER_ setting up the per_cpu areas
   */
b7722f4ac   Linus Torvalds   init: rename and ...
2208
  void __init boot_cpu_hotplug_init(void)
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2209
  {
7bdbaba8e   Abel Vesa   cpu/hotplug: Non-...
2210
  #ifdef CONFIG_SMP
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
2211
  	this_cpu_write(cpuhp_state.booted_once, true);
7bdbaba8e   Abel Vesa   cpu/hotplug: Non-...
2212
  #endif
7f2229c92   Thomas Gleixner   cpu/hotplug: Boot...
2213
  	this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
cff7d378d   Thomas Gleixner   cpu/hotplug: Conv...
2214
  }