cpu.c 53.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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>
10
#include <linux/sched/signal.h>
11
#include <linux/sched/hotplug.h>
12
#include <linux/sched/task.h>
Linus Torvalds's avatar
Linus Torvalds committed
13 14
#include <linux/unistd.h>
#include <linux/cpu.h>
15 16
#include <linux/oom.h>
#include <linux/rcupdate.h>
17
#include <linux/export.h>
18
#include <linux/bug.h>
Linus Torvalds's avatar
Linus Torvalds committed
19 20
#include <linux/kthread.h>
#include <linux/stop_machine.h>
21
#include <linux/mutex.h>
22
#include <linux/gfp.h>
23
#include <linux/suspend.h>
24
#include <linux/lockdep.h>
25
#include <linux/tick.h>
26
#include <linux/irq.h>
27
#include <linux/nmi.h>
28
#include <linux/smpboot.h>
29
#include <linux/relay.h>
30
#include <linux/slab.h>
31
#include <linux/percpu-rwsem.h>
32

33
#include <trace/events/power.h>
34 35
#define CREATE_TRACE_POINTS
#include <trace/events/cpuhp.h>
Linus Torvalds's avatar
Linus Torvalds committed
36

37 38
#include "smpboot.h"

39 40 41 42
/**
 * cpuhp_cpu_state - Per cpu hotplug state storage
 * @state:	The current cpu state
 * @target:	The target state
43 44
 * @thread:	Pointer to the hotplug thread
 * @should_run:	Thread should execute
45
 * @rollback:	Perform a rollback
46 47 48
 * @single:	Single callback invocation
 * @bringup:	Single callback bringup or teardown selector
 * @cb_state:	The state for a single callback (install/uninstall)
49
 * @result:	Result of the operation
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
52 53 54 55
 */
struct cpuhp_cpu_state {
	enum cpuhp_state	state;
	enum cpuhp_state	target;
56
	enum cpuhp_state	fail;
57 58 59
#ifdef CONFIG_SMP
	struct task_struct	*thread;
	bool			should_run;
60
	bool			rollback;
61 62
	bool			single;
	bool			bringup;
63
	bool			booted_once;
64
	struct hlist_node	*node;
65
	struct hlist_node	*last;
66 67
	enum cpuhp_state	cb_state;
	int			result;
68 69
	struct completion	done_up;
	struct completion	done_down;
70
#endif
71 72
};

73 74 75
static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
	.fail = CPUHP_INVALID,
};
76

77
#if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
78 79 80 81 82 83
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);


84
static inline void cpuhp_lock_acquire(bool bringup)
85 86 87 88
{
	lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
}

89
static inline void cpuhp_lock_release(bool bringup)
90 91 92 93 94
{
	lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
}
#else

95 96
static inline void cpuhp_lock_acquire(bool bringup) { }
static inline void cpuhp_lock_release(bool bringup) { }
97

98 99
#endif

100 101 102 103 104 105 106
/**
 * 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
107
 * @cant_stop:	Bringup/teardown can't be stopped at this step
108 109
 */
struct cpuhp_step {
110 111
	const char		*name;
	union {
112 113 114 115
		int		(*single)(unsigned int cpu);
		int		(*multi)(unsigned int cpu,
					 struct hlist_node *node);
	} startup;
116
	union {
117 118 119 120
		int		(*single)(unsigned int cpu);
		int		(*multi)(unsigned int cpu,
					 struct hlist_node *node);
	} teardown;
121 122 123 124
	struct hlist_head	list;
	bool			skip_onerr;
	bool			cant_stop;
	bool			multi_instance;
125 126
};

127
static DEFINE_MUTEX(cpuhp_state_mutex);
128
static struct cpuhp_step cpuhp_hp_states[];
129

130 131
static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
{
132
	return cpuhp_hp_states + state;
133 134
}

135 136 137
/**
 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
 * @cpu:	The cpu for which the callback should be invoked
138
 * @state:	The state to do callbacks for
139
 * @bringup:	True if the bringup callback should be invoked
140 141
 * @node:	For multi-instance, do a single entry callback for install/remove
 * @lastp:	For multi-instance rollback, remember how far we got
142
 *
143
 * Called from cpu hotplug and from the state register machinery.
144
 */
145
static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
146 147
				 bool bringup, struct hlist_node *node,
				 struct hlist_node **lastp)
148 149
{
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
150
	struct cpuhp_step *step = cpuhp_get_step(state);
151 152 153 154
	int (*cbm)(unsigned int cpu, struct hlist_node *node);
	int (*cb)(unsigned int cpu);
	int ret, cnt;

155 156 157 158 159 160 161 162 163
	if (st->fail == state) {
		st->fail = CPUHP_INVALID;

		if (!(bringup ? step->startup.single : step->teardown.single))
			return 0;

		return -EAGAIN;
	}

164
	if (!step->multi_instance) {
165
		WARN_ON_ONCE(lastp && *lastp);
166
		cb = bringup ? step->startup.single : step->teardown.single;
167 168
		if (!cb)
			return 0;
169
		trace_cpuhp_enter(cpu, st->target, state, cb);
170
		ret = cb(cpu);
171
		trace_cpuhp_exit(cpu, st->state, state, ret);
172 173
		return ret;
	}
174
	cbm = bringup ? step->startup.multi : step->teardown.multi;
175 176 177 178 179
	if (!cbm)
		return 0;

	/* Single invocation for instance add/remove */
	if (node) {
180
		WARN_ON_ONCE(lastp && *lastp);
181 182 183 184 185 186 187 188 189
		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) {
190 191 192
		if (lastp && node == *lastp)
			break;

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);
196 197 198 199 200 201 202
		if (ret) {
			if (!lastp)
				goto err;

			*lastp = node;
			return ret;
		}
203 204
		cnt++;
	}
205 206
	if (lastp)
		*lastp = NULL;
207 208 209
	return 0;
err:
	/* Rollback the instances if one failed */
210
	cbm = !bringup ? step->startup.multi : step->teardown.multi;
211 212 213 214 215 216
	if (!cbm)
		return ret;

	hlist_for_each(node, &step->list) {
		if (!cnt--)
			break;
217 218 219 220 221 222 223 224

		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);
225 226 227 228
	}
	return ret;
}

229
#ifdef CONFIG_SMP
230 231 232 233 234 235 236 237 238
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;
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
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;
}

259
/* Serializes the updates to cpu_online_mask, cpu_present_mask */
260
static DEFINE_MUTEX(cpu_add_remove_lock);
261 262
bool cpuhp_tasks_frozen;
EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
Linus Torvalds's avatar
Linus Torvalds committed
263

264
/*
265 266
 * 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.
267 268 269 270 271 272 273 274 275 276
 */
void cpu_maps_update_begin(void)
{
	mutex_lock(&cpu_add_remove_lock);
}

void cpu_maps_update_done(void)
{
	mutex_unlock(&cpu_add_remove_lock);
}
Linus Torvalds's avatar
Linus Torvalds committed
277

278 279
/*
 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
280 281 282 283
 * Should always be manipulated under cpu_add_remove_lock
 */
static int cpu_hotplug_disabled;

284 285
#ifdef CONFIG_HOTPLUG_CPU

286
DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
287

288
void cpus_read_lock(void)
289
{
290
	percpu_down_read(&cpu_hotplug_lock);
291
}
292
EXPORT_SYMBOL_GPL(cpus_read_lock);
293

294
void cpus_read_unlock(void)
295
{
296
	percpu_up_read(&cpu_hotplug_lock);
297
}
298
EXPORT_SYMBOL_GPL(cpus_read_unlock);
299

300
void cpus_write_lock(void)
301
{
302
	percpu_down_write(&cpu_hotplug_lock);
303
}
304

305
void cpus_write_unlock(void)
306
{
307
	percpu_up_write(&cpu_hotplug_lock);
308 309
}

310
void lockdep_assert_cpus_held(void)
311
{
312
	percpu_rwsem_assert_held(&cpu_hotplug_lock);
313
}
314

315 316 317 318 319 320 321 322 323 324
/*
 * 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();
325
	cpu_hotplug_disabled++;
326 327
	cpu_maps_update_done();
}
328
EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
329

330 331 332 333 334 335 336
static void __cpu_hotplug_enable(void)
{
	if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
		return;
	cpu_hotplug_disabled--;
}

337 338 339
void cpu_hotplug_enable(void)
{
	cpu_maps_update_begin();
340
	__cpu_hotplug_enable();
341 342
	cpu_maps_update_done();
}
343
EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
344
#endif	/* CONFIG_HOTPLUG_CPU */
345

346 347
#ifdef CONFIG_HOTPLUG_SMT
enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
348
EXPORT_SYMBOL_GPL(cpu_smt_control);
349

350
void __init cpu_smt_disable(bool force)
351
{
352 353 354 355 356
	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
		cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
		return;

	if (force) {
357 358
		pr_info("SMT: Force disabled\n");
		cpu_smt_control = CPU_SMT_FORCE_DISABLED;
359 360
	} else {
		cpu_smt_control = CPU_SMT_DISABLED;
361
	}
362 363
}

364 365 366 367 368 369 370 371 372 373
/*
 * The decision whether SMT is supported can only be done after the full
 * CPU identification. Called from architecture code.
 */
void __init cpu_smt_check_topology(void)
{
	if (!topology_smt_supported())
		cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
}

374 375 376
static int __init smt_cmdline_disable(char *str)
{
	cpu_smt_disable(str && !strcmp(str, "force"));
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	return 0;
}
early_param("nosmt", smt_cmdline_disable);

static inline bool cpu_smt_allowed(unsigned int cpu)
{
	if (cpu_smt_control == CPU_SMT_ENABLED)
		return true;

	if (topology_is_primary_thread(cpu))
		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

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
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);
450
	wait_for_ap_thread(st, st->bringup);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
}

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;
}
467

468 469 470 471
static int bringup_wait_for_ap(unsigned int cpu)
{
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);

472
	/* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
473
	wait_for_ap_thread(st, true);
474 475
	if (WARN_ON_ONCE((!cpu_online(cpu))))
		return -ECANCELED;
476 477 478 479 480

	/* Unpark the stopper thread and the hotplug thread of the target cpu */
	stop_machine_unpark(cpu);
	kthread_unpark(st->thread);

481 482 483 484 485 486 487 488 489 490
	/*
	 * 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;

491 492 493 494
	if (st->target <= CPUHP_AP_ONLINE_IDLE)
		return 0;

	return cpuhp_kick_ap(st, st->target);
495 496
}

497 498 499 500 501
static int bringup_cpu(unsigned int cpu)
{
	struct task_struct *idle = idle_thread_get(cpu);
	int ret;

502 503 504 505 506 507 508
	/*
	 * 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();

509 510
	/* Arch-specific enabling code. */
	ret = __cpu_up(cpu, idle);
511
	irq_unlock_sparse();
512
	if (ret)
513
		return ret;
514
	return bringup_wait_for_ap(cpu);
515 516
}

517 518 519 520
/*
 * Hotplug state machine related functions
 */

521
static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
522 523
{
	for (st->state--; st->state > st->target; st->state--) {
524
		struct cpuhp_step *step = cpuhp_get_step(st->state);
525 526

		if (!step->skip_onerr)
527
			cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
528 529 530 531
	}
}

static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
532
			      enum cpuhp_state target)
533 534 535 536 537 538
{
	enum cpuhp_state prev_state = st->state;
	int ret = 0;

	while (st->state < target) {
		st->state++;
539
		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
540 541
		if (ret) {
			st->target = prev_state;
542
			undo_cpu_up(cpu, st);
543 544 545 546 547 548
			break;
		}
	}
	return ret;
}

549 550 551 552 553 554 555
/*
 * The cpu hotplug threads manage the bringup and teardown of the cpus
 */
static void cpuhp_create(unsigned int cpu)
{
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);

556 557
	init_completion(&st->done_up);
	init_completion(&st->done_down);
558 559 560 561 562 563 564 565 566 567 568 569
}

static int cpuhp_should_run(unsigned int cpu)
{
	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);

	return st->should_run;
}

/*
 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
 * callbacks when a state gets [un]installed at runtime.
570 571 572 573 574 575 576 577 578 579
 *
 * 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.
580 581 582 583
 */
static void cpuhp_thread_fun(unsigned int cpu)
{
	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
584 585
	bool bringup = st->bringup;
	enum cpuhp_state state;
586 587

	/*
588 589
	 * 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.
590 591 592
	 */
	smp_mb();

593 594
	if (WARN_ON_ONCE(!st->should_run))
		return;
595

596
	cpuhp_lock_acquire(bringup);
597

598
	if (st->single) {
599 600 601 602 603 604 605 606
		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);
607
		} else {
608 609 610 611
			state = st->state;
			st->state--;
			st->should_run = (st->state > st->target);
			WARN_ON_ONCE(st->state < st->target);
612
		}
613 614 615 616 617 618 619 620 621 622 623 624 625 626
	}

	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();
627

628 629 630 631
		/*
		 * STARTING/DYING must not fail!
		 */
		WARN_ON_ONCE(st->result);
632
	} else {
633 634 635 636 637 638 639 640 641 642 643
		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;
644
	}
645 646

next:
647
	cpuhp_lock_release(bringup);
648 649

	if (!st->should_run)
650
		complete_ap_thread(st, bringup);
651 652 653
}

/* Invoke a single callback on a remote cpu */
654
static int
655 656
cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
			 struct hlist_node *node)
657 658
{
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
659
	int ret;
660 661 662 663

	if (!cpu_online(cpu))
		return 0;

664 665 666 667 668
	cpuhp_lock_acquire(false);
	cpuhp_lock_release(false);

	cpuhp_lock_acquire(true);
	cpuhp_lock_release(true);
669

670 671 672 673 674
	/*
	 * If we are up and running, use the hotplug thread. For early calls
	 * we invoke the thread function directly.
	 */
	if (!st->thread)
675
		return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
676

677 678 679 680 681
	st->rollback = false;
	st->last = NULL;

	st->node = node;
	st->bringup = bringup;
682
	st->cb_state = state;
683 684
	st->single = true;

685
	__cpuhp_kick_ap(st);
686 687

	/*
688
	 * If we failed and did a partial, do a rollback.
689
	 */
690 691 692 693 694 695 696
	if ((ret = st->result) && st->last) {
		st->rollback = true;
		st->bringup = !bringup;

		__cpuhp_kick_ap(st);
	}

697 698 699 700 701
	/*
	 * Clean up the leftovers so the next hotplug operation wont use stale
	 * data.
	 */
	st->node = st->last = NULL;
702
	return ret;
703 704 705 706 707
}

static int cpuhp_kick_ap_work(unsigned int cpu)
{
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
708 709
	enum cpuhp_state prev_state = st->state;
	int ret;
710

711 712 713 714 715
	cpuhp_lock_acquire(false);
	cpuhp_lock_release(false);

	cpuhp_lock_acquire(true);
	cpuhp_lock_release(true);
716 717 718 719 720 721

	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;
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
}

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));
}

739
#ifdef CONFIG_HOTPLUG_CPU
740 741 742 743 744 745 746 747 748 749 750 751
/**
 * 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.
 */
752 753 754 755 756 757 758 759 760 761 762
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.
	 */
763
	WARN_ON(cpu_online(cpu));
764 765 766 767
	rcu_read_lock();
	for_each_process(p) {
		struct task_struct *t;

768 769 770 771
		/*
		 * Main thread might exit, but other threads may still have
		 * a valid mm. Find one.
		 */
772 773 774 775 776 777 778 779 780
		t = find_lock_task_mm(p);
		if (!t)
			continue;
		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
		task_unlock(t);
	}
	rcu_read_unlock();
}

Linus Torvalds's avatar
Linus Torvalds committed
781
/* Take this CPU down. */
782
static int take_cpu_down(void *_param)
Linus Torvalds's avatar
Linus Torvalds committed
783
{
784 785
	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
	enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
786
	int err, cpu = smp_processor_id();
787
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
788 789 790 791

	/* Ensure this CPU doesn't handle any more interrupts. */
	err = __cpu_disable();
	if (err < 0)
Zwane Mwaikambo's avatar
Zwane Mwaikambo committed
792
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
793

794 795 796 797 798 799
	/*
	 * 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--;
800
	/* Invoke the former CPU_DYING callbacks */
801 802 803 804 805 806 807
	for (; st->state > target; st->state--) {
		ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
		/*
		 * DYING must not fail!
		 */
		WARN_ON_ONCE(ret);
	}
808

809 810
	/* Give up timekeeping duties */
	tick_handover_do_timer();
811
	/* Park the stopper thread */
812
	stop_machine_park(cpu);
Zwane Mwaikambo's avatar
Zwane Mwaikambo committed
813
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
814 815
}

816
static int takedown_cpu(unsigned int cpu)
Linus Torvalds's avatar
Linus Torvalds committed
817
{
818
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
819
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
820

821
	/* Park the smpboot threads */
822 823
	kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);

824
	/*
825 826
	 * Prevent irq alloc/free while the dying cpu reorganizes the
	 * interrupt affinities.
827
	 */
828
	irq_lock_sparse();
829

830 831 832
	/*
	 * So now all preempt/rcu users must observe !cpu_active().
	 */
833
	err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
834
	if (err) {
835
		/* CPU refused to die */
836
		irq_unlock_sparse();
837 838
		/* Unpark the hotplug thread so we can rollback there */
		kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
839
		return err;
840
	}
841
	BUG_ON(cpu_online(cpu));
Linus Torvalds's avatar
Linus Torvalds committed
842

843
	/*
844 845
	 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
	 * all runnable tasks from the CPU, there's only the idle task left now
846
	 * that the migration thread is done doing the stop_machine thing.
Peter Zijlstra's avatar
Peter Zijlstra committed
847 848
	 *
	 * Wait for the stop thread to go away.
849
	 */
850
	wait_for_ap_thread(st, false);
851
	BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
Linus Torvalds's avatar
Linus Torvalds committed
852

853 854 855
	/* Interrupts are moved away from the dying cpu, reenable alloc/free */
	irq_unlock_sparse();

856
	hotplug_cpu__broadcast_tick_pull(cpu);
Linus Torvalds's avatar
Linus Torvalds committed
857 858 859
	/* This actually kills the CPU. */
	__cpu_die(cpu);

860
	tick_cleanup_dead_cpu(cpu);
861
	rcutree_migrate_callbacks(cpu);
862 863
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
864

865 866 867 868
static void cpuhp_complete_idle_dead(void *arg)
{
	struct cpuhp_cpu_state *st = arg;

869
	complete_ap_thread(st, false);
870 871
}

872 873 874 875 876
void cpuhp_report_idle_dead(void)
{
	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);

	BUG_ON(st->state != CPUHP_AP_OFFLINE);
877
	rcu_report_dead(smp_processor_id());
878 879 880 881 882 883 884
	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);
885 886
}

887 888 889 890
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);
891

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
		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;
			undo_cpu_down(cpu, st);
			break;
		}
	}
	return ret;
}
913

914
/* Requires cpu_add_remove_lock to be held */
915 916
static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
			   enum cpuhp_state target)
917
{
918 919
	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
	int prev_state, ret = 0;
920 921 922 923

	if (num_online_cpus() == 1)
		return -EBUSY;

924
	if (!cpu_present(cpu))
925 926
		return -EINVAL;

927
	cpus_write_lock();
928 929 930

	cpuhp_tasks_frozen = tasks_frozen;

931
	prev_state = cpuhp_set_state(st, target);
932 933 934 935
	/*
	 * If the current CPU state is in the range of the AP hotplug thread,
	 * then we need to kick the thread.
	 */
936
	if (st->state > CPUHP_TEARDOWN_CPU) {
937
		st->target = max((int)target, CPUHP_TEARDOWN_CPU);
938 939 940 941 942 943 944 945 946 947 948 949
		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.
		 */
950
		if (st->state > CPUHP_TEARDOWN_CPU)
951
			goto out;
952 953

		st->target = target;
954 955
	}
	/*
956
	 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
957 958
	 * to do the further cleanups.
	 */
959
	ret = cpuhp_down_callbacks(cpu, st, target);
960
	if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
961 962
		cpuhp_reset_state(st, prev_state);
		__cpuhp_kick_ap(st);
963
	}
964

965
out:
966
	cpus_write_unlock();
967 968 969 970 971
	/*
	 * Do post unplug cleanup. This is still protected against
	 * concurrent CPU hotplug via cpu_add_remove_lock.
	 */
	lockup_detector_cleanup();
972
	return ret;
973 974
}

975 976 977 978 979 980 981
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);
}

982
static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
983
{
984
	int err;
985

986
	cpu_maps_update_begin();
987
	err = cpu_down_maps_locked(cpu, target);
988
	cpu_maps_update_done();