cpu.c 19.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/cpu.h>
13 14
#include <linux/oom.h>
#include <linux/rcupdate.h>
15
#include <linux/export.h>
16
#include <linux/bug.h>
L
Linus Torvalds 已提交
17 18
#include <linux/kthread.h>
#include <linux/stop_machine.h>
19
#include <linux/mutex.h>
20
#include <linux/gfp.h>
21
#include <linux/suspend.h>
22
#include <linux/lockdep.h>
23
#include <linux/tick.h>
24
#include <linux/irq.h>
25
#include <trace/events/power.h>
L
Linus Torvalds 已提交
26

27 28
#include "smpboot.h"

29
#ifdef CONFIG_SMP
30
/* Serializes the updates to cpu_online_mask, cpu_present_mask */
31
static DEFINE_MUTEX(cpu_add_remove_lock);
32 33
bool cpuhp_tasks_frozen;
EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
L
Linus Torvalds 已提交
34

35
/*
36 37 38 39 40
 * 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.
 * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
 * hotplug callback (un)registration performed using __register_cpu_notifier()
 * or __unregister_cpu_notifier().
41 42 43 44 45
 */
void cpu_maps_update_begin(void)
{
	mutex_lock(&cpu_add_remove_lock);
}
46
EXPORT_SYMBOL(cpu_notifier_register_begin);
47 48 49 50 51

void cpu_maps_update_done(void)
{
	mutex_unlock(&cpu_add_remove_lock);
}
52
EXPORT_SYMBOL(cpu_notifier_register_done);
53

54
static RAW_NOTIFIER_HEAD(cpu_chain);
L
Linus Torvalds 已提交
55

56 57 58 59 60
/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
 * Should always be manipulated under cpu_add_remove_lock
 */
static int cpu_hotplug_disabled;

61 62
#ifdef CONFIG_HOTPLUG_CPU

63 64
static struct {
	struct task_struct *active_writer;
65 66 67 68
	/* wait queue to wake up the active_writer */
	wait_queue_head_t wq;
	/* verifies that no writer will get active while readers are active */
	struct mutex lock;
69 70 71 72
	/*
	 * Also blocks the new readers during
	 * an ongoing cpu hotplug operation.
	 */
73
	atomic_t refcount;
74 75 76 77

#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lockdep_map dep_map;
#endif
78 79
} cpu_hotplug = {
	.active_writer = NULL,
80
	.wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
81
	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
82 83 84
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	.dep_map = {.name = "cpu_hotplug.lock" },
#endif
85
};
86

87 88
/* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
#define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
89 90
#define cpuhp_lock_acquire_tryread() \
				  lock_map_acquire_tryread(&cpu_hotplug.dep_map)
91 92 93
#define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
#define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)

94

95
void get_online_cpus(void)
96
{
97 98
	might_sleep();
	if (cpu_hotplug.active_writer == current)
99
		return;
100
	cpuhp_lock_acquire_read();
101
	mutex_lock(&cpu_hotplug.lock);
102
	atomic_inc(&cpu_hotplug.refcount);
103
	mutex_unlock(&cpu_hotplug.lock);
104
}
105
EXPORT_SYMBOL_GPL(get_online_cpus);
106

107
void put_online_cpus(void)
108
{
109 110
	int refcount;

111
	if (cpu_hotplug.active_writer == current)
112
		return;
113

114 115 116 117 118 119
	refcount = atomic_dec_return(&cpu_hotplug.refcount);
	if (WARN_ON(refcount < 0)) /* try to fix things up */
		atomic_inc(&cpu_hotplug.refcount);

	if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
		wake_up(&cpu_hotplug.wq);
120

121
	cpuhp_lock_release();
122

123
}
124
EXPORT_SYMBOL_GPL(put_online_cpus);
125

126 127 128 129 130 131 132
/*
 * This ensures that the hotplug operation can begin only when the
 * refcount goes to zero.
 *
 * Note that during a cpu-hotplug operation, the new readers, if any,
 * will be blocked by the cpu_hotplug.lock
 *
133 134
 * Since cpu_hotplug_begin() is always called after invoking
 * cpu_maps_update_begin(), we can be sure that only one writer is active.
135 136 137 138 139 140 141 142 143 144
 *
 * Note that theoretically, there is a possibility of a livelock:
 * - Refcount goes to zero, last reader wakes up the sleeping
 *   writer.
 * - Last reader unlocks the cpu_hotplug.lock.
 * - A new reader arrives at this moment, bumps up the refcount.
 * - The writer acquires the cpu_hotplug.lock finds the refcount
 *   non zero and goes to sleep again.
 *
 * However, this is very difficult to achieve in practice since
145
 * get_online_cpus() not an api which is called all that often.
146 147
 *
 */
148
void cpu_hotplug_begin(void)
149
{
150
	DEFINE_WAIT(wait);
151

152
	cpu_hotplug.active_writer = current;
153
	cpuhp_lock_acquire();
154

155 156
	for (;;) {
		mutex_lock(&cpu_hotplug.lock);
157 158 159
		prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
		if (likely(!atomic_read(&cpu_hotplug.refcount)))
				break;
160 161 162
		mutex_unlock(&cpu_hotplug.lock);
		schedule();
	}
163
	finish_wait(&cpu_hotplug.wq, &wait);
164 165
}

166
void cpu_hotplug_done(void)
167 168 169
{
	cpu_hotplug.active_writer = NULL;
	mutex_unlock(&cpu_hotplug.lock);
170
	cpuhp_lock_release();
171
}
172

173 174 175 176 177 178 179 180 181 182
/*
 * 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();
183
	cpu_hotplug_disabled++;
184 185
	cpu_maps_update_done();
}
186
EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
187 188 189 190

void cpu_hotplug_enable(void)
{
	cpu_maps_update_begin();
191
	WARN_ON(--cpu_hotplug_disabled < 0);
192 193
	cpu_maps_update_done();
}
194
EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
195
#endif	/* CONFIG_HOTPLUG_CPU */
196

L
Linus Torvalds 已提交
197
/* Need to know about CPUs going up/down? */
198
int register_cpu_notifier(struct notifier_block *nb)
L
Linus Torvalds 已提交
199
{
200
	int ret;
201
	cpu_maps_update_begin();
202
	ret = raw_notifier_chain_register(&cpu_chain, nb);
203
	cpu_maps_update_done();
204
	return ret;
L
Linus Torvalds 已提交
205
}
206

207
int __register_cpu_notifier(struct notifier_block *nb)
208 209 210 211
{
	return raw_notifier_chain_register(&cpu_chain, nb);
}

212
static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
213 214
			int *nr_calls)
{
215 216 217
	unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
	void *hcpu = (void *)(long)cpu;

218 219
	int ret;

220
	ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
221
					nr_calls);
222 223

	return notifier_to_errno(ret);
224 225
}

226
static int cpu_notify(unsigned long val, unsigned int cpu)
227
{
228
	return __cpu_notify(val, cpu, -1, NULL);
229 230
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/* Notifier wrappers for transitioning to state machine */
static int notify_prepare(unsigned int cpu)
{
	int nr_calls = 0;
	int ret;

	ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
	if (ret) {
		nr_calls--;
		printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
				__func__, cpu);
		__cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
	}
	return ret;
}

static int notify_online(unsigned int cpu)
{
	cpu_notify(CPU_ONLINE, cpu);
	return 0;
}

static int bringup_cpu(unsigned int cpu)
{
	struct task_struct *idle = idle_thread_get(cpu);
	int ret;

	/* Arch-specific enabling code. */
	ret = __cpu_up(cpu, idle);
	if (ret) {
		cpu_notify(CPU_UP_CANCELED, cpu);
		return ret;
	}
	BUG_ON(!cpu_online(cpu));
	return 0;
}

268
#ifdef CONFIG_HOTPLUG_CPU
L
Linus Torvalds 已提交
269
EXPORT_SYMBOL(register_cpu_notifier);
270
EXPORT_SYMBOL(__register_cpu_notifier);
L
Linus Torvalds 已提交
271

272
void unregister_cpu_notifier(struct notifier_block *nb)
L
Linus Torvalds 已提交
273
{
274
	cpu_maps_update_begin();
275
	raw_notifier_chain_unregister(&cpu_chain, nb);
276
	cpu_maps_update_done();
L
Linus Torvalds 已提交
277 278 279
}
EXPORT_SYMBOL(unregister_cpu_notifier);

280
void __unregister_cpu_notifier(struct notifier_block *nb)
281 282 283 284 285
{
	raw_notifier_chain_unregister(&cpu_chain, nb);
}
EXPORT_SYMBOL(__unregister_cpu_notifier);

286 287 288 289 290 291 292 293 294 295 296 297
/**
 * 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.
 */
298 299 300 301 302 303 304 305 306 307 308
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.
	 */
309
	WARN_ON(cpu_online(cpu));
310 311 312 313
	rcu_read_lock();
	for_each_process(p) {
		struct task_struct *t;

314 315 316 317
		/*
		 * Main thread might exit, but other threads may still have
		 * a valid mm. Find one.
		 */
318 319 320 321 322 323 324 325 326
		t = find_lock_task_mm(p);
		if (!t)
			continue;
		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
		task_unlock(t);
	}
	rcu_read_unlock();
}

K
Kirill Tkhai 已提交
327
static inline void check_for_tasks(int dead_cpu)
L
Linus Torvalds 已提交
328
{
K
Kirill Tkhai 已提交
329
	struct task_struct *g, *p;
L
Linus Torvalds 已提交
330

331 332
	read_lock(&tasklist_lock);
	for_each_process_thread(g, p) {
K
Kirill Tkhai 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346
		if (!p->on_rq)
			continue;
		/*
		 * We do the check with unlocked task_rq(p)->lock.
		 * Order the reading to do not warn about a task,
		 * which was running on this cpu in the past, and
		 * it's just been woken on another cpu.
		 */
		rmb();
		if (task_cpu(p) != dead_cpu)
			continue;

		pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
			p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
347 348
	}
	read_unlock(&tasklist_lock);
L
Linus Torvalds 已提交
349 350
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
{
	BUG_ON(cpu_notify(val, cpu));
}

static int notify_down_prepare(unsigned int cpu)
{
	int err, nr_calls = 0;

	err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
	if (err) {
		nr_calls--;
		__cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
		pr_warn("%s: attempt to take down CPU %u failed\n",
				__func__, cpu);
	}
	return err;
}

L
Linus Torvalds 已提交
370
/* Take this CPU down. */
371
static int take_cpu_down(void *_param)
L
Linus Torvalds 已提交
372
{
373
	int err, cpu = smp_processor_id();
L
Linus Torvalds 已提交
374 375 376 377

	/* Ensure this CPU doesn't handle any more interrupts. */
	err = __cpu_disable();
	if (err < 0)
Z
Zwane Mwaikambo 已提交
378
		return err;
L
Linus Torvalds 已提交
379

380
	cpu_notify(CPU_DYING, cpu);
381 382
	/* Give up timekeeping duties */
	tick_handover_do_timer();
383
	/* Park the stopper thread */
384
	stop_machine_park(cpu);
Z
Zwane Mwaikambo 已提交
385
	return 0;
L
Linus Torvalds 已提交
386 387
}

388
static int takedown_cpu(unsigned int cpu)
L
Linus Torvalds 已提交
389
{
390
	int err;
L
Linus Torvalds 已提交
391

392 393 394 395 396 397
	/*
	 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
	 * and RCU users of this state to go away such that all new such users
	 * will observe it.
	 *
	 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
398
	 * not imply sync_sched(), so wait for both.
399 400
	 *
	 * Do sync before park smpboot threads to take care the rcu boost case.
401
	 */
402 403 404 405
	if (IS_ENABLED(CONFIG_PREEMPT))
		synchronize_rcu_mult(call_rcu, call_rcu_sched);
	else
		synchronize_rcu();
406

407 408
	smpboot_park_threads(cpu);

409
	/*
410 411
	 * Prevent irq alloc/free while the dying cpu reorganizes the
	 * interrupt affinities.
412
	 */
413
	irq_lock_sparse();
414

415 416 417
	/*
	 * So now all preempt/rcu users must observe !cpu_active().
	 */
418
	err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
419
	if (err) {
L
Linus Torvalds 已提交
420
		/* CPU didn't die: tell everyone.  Can't complain. */
421
		cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
422
		irq_unlock_sparse();
423
		return err;
424
	}
425
	BUG_ON(cpu_online(cpu));
L
Linus Torvalds 已提交
426

427 428 429 430
	/*
	 * The migration_call() CPU_DYING callback will have removed all
	 * runnable tasks from the cpu, there's only the idle task left now
	 * that the migration thread is done doing the stop_machine thing.
P
Peter Zijlstra 已提交
431 432
	 *
	 * Wait for the stop thread to go away.
433
	 */
434
	while (!per_cpu(cpu_dead_idle, cpu))
P
Peter Zijlstra 已提交
435
		cpu_relax();
436 437
	smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */
	per_cpu(cpu_dead_idle, cpu) = false;
L
Linus Torvalds 已提交
438

439 440 441
	/* Interrupts are moved away from the dying cpu, reenable alloc/free */
	irq_unlock_sparse();

442
	hotplug_cpu__broadcast_tick_pull(cpu);
L
Linus Torvalds 已提交
443 444 445
	/* This actually kills the CPU. */
	__cpu_die(cpu);

446
	tick_cleanup_dead_cpu(cpu);
447 448
	return 0;
}
L
Linus Torvalds 已提交
449

450 451 452
static int notify_dead(unsigned int cpu)
{
	cpu_notify_nofail(CPU_DEAD, cpu);
L
Linus Torvalds 已提交
453
	check_for_tasks(cpu);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	return 0;
}

/* Requires cpu_add_remove_lock to be held */
static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
{
	int err;

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

	if (!cpu_online(cpu))
		return -EINVAL;

	cpu_hotplug_begin();

	cpuhp_tasks_frozen = tasks_frozen;

	err = notify_down_prepare(cpu);
	if (err)
		goto out_release;
	err = takedown_cpu(cpu);
	if (err)
		goto out_release;

	notify_dead(cpu);
L
Linus Torvalds 已提交
480

481
out_release:
482
	cpu_hotplug_done();
483
	if (!err)
484
		cpu_notify_nofail(CPU_POST_DEAD, cpu);
485 486 487
	return err;
}

488
int cpu_down(unsigned int cpu)
489
{
490
	int err;
491

492
	cpu_maps_update_begin();
493 494

	if (cpu_hotplug_disabled) {
495
		err = -EBUSY;
496 497 498 499
		goto out;
	}

	err = _cpu_down(cpu, 0);
500

501
out:
502
	cpu_maps_update_done();
L
Linus Torvalds 已提交
503 504
	return err;
}
505
EXPORT_SYMBOL(cpu_down);
L
Linus Torvalds 已提交
506 507
#endif /*CONFIG_HOTPLUG_CPU*/

508 509 510 511 512 513 514 515 516 517
/*
 * Unpark per-CPU smpboot kthreads at CPU-online time.
 */
static int smpboot_thread_call(struct notifier_block *nfb,
			       unsigned long action, void *hcpu)
{
	int cpu = (long)hcpu;

	switch (action & ~CPU_TASKS_FROZEN) {

518
	case CPU_DOWN_FAILED:
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	case CPU_ONLINE:
		smpboot_unpark_threads(cpu);
		break;

	default:
		break;
	}

	return NOTIFY_OK;
}

static struct notifier_block smpboot_thread_notifier = {
	.notifier_call = smpboot_thread_call,
	.priority = CPU_PRI_SMPBOOT,
};

535
void smpboot_thread_init(void)
536 537 538 539
{
	register_cpu_notifier(&smpboot_thread_notifier);
}

540
/* Requires cpu_add_remove_lock to be held */
541
static int _cpu_up(unsigned int cpu, int tasks_frozen)
L
Linus Torvalds 已提交
542
{
543
	struct task_struct *idle;
544
	int ret;
L
Linus Torvalds 已提交
545

546
	cpu_hotplug_begin();
547

548 549 550 551 552
	if (cpu_online(cpu) || !cpu_present(cpu)) {
		ret = -EINVAL;
		goto out;
	}

553 554 555
	idle = idle_thread_get(cpu);
	if (IS_ERR(idle)) {
		ret = PTR_ERR(idle);
556
		goto out;
557
	}
558

559 560
	cpuhp_tasks_frozen = tasks_frozen;

561 562 563 564
	ret = smpboot_create_threads(cpu);
	if (ret)
		goto out;

565 566 567
	ret = notify_prepare(cpu);
	if (ret)
		goto out;
L
Linus Torvalds 已提交
568

569 570 571
	ret = bringup_cpu(cpu);
	if (ret)
		goto out;
L
Linus Torvalds 已提交
572

573
	notify_online(cpu);
574
out:
575
	cpu_hotplug_done();
576 577 578 579

	return ret;
}

580
int cpu_up(unsigned int cpu)
581 582
{
	int err = 0;
583

R
Rusty Russell 已提交
584
	if (!cpu_possible(cpu)) {
585 586
		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
		       cpu);
587
#if defined(CONFIG_IA64)
588
		pr_err("please check additional_cpus= boot parameter\n");
589 590 591
#endif
		return -EINVAL;
	}
592

593 594 595
	err = try_online_node(cpu_to_node(cpu));
	if (err)
		return err;
596

597
	cpu_maps_update_begin();
598 599

	if (cpu_hotplug_disabled) {
600
		err = -EBUSY;
601 602 603 604 605 606
		goto out;
	}

	err = _cpu_up(cpu, 0);

out:
607
	cpu_maps_update_done();
608 609
	return err;
}
P
Paul E. McKenney 已提交
610
EXPORT_SYMBOL_GPL(cpu_up);
611

612
#ifdef CONFIG_PM_SLEEP_SMP
R
Rusty Russell 已提交
613
static cpumask_var_t frozen_cpus;
614 615 616

int disable_nonboot_cpus(void)
{
617
	int cpu, first_cpu, error = 0;
618

619
	cpu_maps_update_begin();
R
Rusty Russell 已提交
620
	first_cpu = cpumask_first(cpu_online_mask);
621 622
	/*
	 * We take down all of the non-boot CPUs in one shot to avoid races
623 624
	 * with the userspace trying to use the CPU hotplug at the same time
	 */
R
Rusty Russell 已提交
625
	cpumask_clear(frozen_cpus);
626

627
	pr_info("Disabling non-boot CPUs ...\n");
628 629 630
	for_each_online_cpu(cpu) {
		if (cpu == first_cpu)
			continue;
631
		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
632
		error = _cpu_down(cpu, 1);
633
		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
634
		if (!error)
R
Rusty Russell 已提交
635
			cpumask_set_cpu(cpu, frozen_cpus);
636
		else {
637
			pr_err("Error taking CPU%d down: %d\n", cpu, error);
638 639 640
			break;
		}
	}
641

642
	if (!error)
643
		BUG_ON(num_online_cpus() > 1);
644
	else
645
		pr_err("Non-boot CPUs are not disabled\n");
646 647 648 649 650 651 652 653

	/*
	 * 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++;

654
	cpu_maps_update_done();
655 656 657
	return error;
}

658 659 660 661 662 663 664 665
void __weak arch_enable_nonboot_cpus_begin(void)
{
}

void __weak arch_enable_nonboot_cpus_end(void)
{
}

666
void enable_nonboot_cpus(void)
667 668 669 670
{
	int cpu, error;

	/* Allow everyone to use the CPU hotplug again */
671
	cpu_maps_update_begin();
672
	WARN_ON(--cpu_hotplug_disabled < 0);
R
Rusty Russell 已提交
673
	if (cpumask_empty(frozen_cpus))
674
		goto out;
675

676
	pr_info("Enabling non-boot CPUs ...\n");
677 678 679

	arch_enable_nonboot_cpus_begin();

R
Rusty Russell 已提交
680
	for_each_cpu(cpu, frozen_cpus) {
681
		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
682
		error = _cpu_up(cpu, 1);
683
		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
684
		if (!error) {
685
			pr_info("CPU%d is up\n", cpu);
686 687
			continue;
		}
688
		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
689
	}
690 691 692

	arch_enable_nonboot_cpus_end();

R
Rusty Russell 已提交
693
	cpumask_clear(frozen_cpus);
694
out:
695
	cpu_maps_update_done();
L
Linus Torvalds 已提交
696
}
R
Rusty Russell 已提交
697

698
static int __init alloc_frozen_cpus(void)
R
Rusty Russell 已提交
699 700 701 702 703 704
{
	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
		return -ENOMEM;
	return 0;
}
core_initcall(alloc_frozen_cpus);
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

/*
 * 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:
725
		cpu_hotplug_disable();
726 727 728 729
		break;

	case PM_POST_SUSPEND:
	case PM_POST_HIBERNATION:
730
		cpu_hotplug_enable();
731 732 733 734 735 736 737 738 739 740
		break;

	default:
		return NOTIFY_DONE;
	}

	return NOTIFY_OK;
}


741
static int __init cpu_hotplug_pm_sync_init(void)
742
{
743 744 745 746 747
	/*
	 * 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.
	 */
748 749 750 751 752
	pm_notifier(cpu_hotplug_pm_callback, 0);
	return 0;
}
core_initcall(cpu_hotplug_pm_sync_init);

753
#endif /* CONFIG_PM_SLEEP_SMP */
754

755 756 757 758 759 760 761 762
/**
 * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
 * @cpu: cpu that just started
 *
 * This function calls the cpu_chain notifiers with CPU_STARTING.
 * 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().
 */
763
void notify_cpu_starting(unsigned int cpu)
764
{
765
	cpu_notify(CPU_STARTING, cpu);
766 767
}

768
#endif /* CONFIG_SMP */
769

770 771 772 773
/*
 * cpu_bit_bitmap[] is a special, "compressed" data structure that
 * represents all NR_CPUS bits binary values of 1<<nr.
 *
R
Rusty Russell 已提交
774
 * It is used by cpumask_of() to get a constant address to a CPU
775 776
 * mask value that has a single bit set only.
 */
777

778
/* cpu_bit_bitmap[0] is empty - so we can back into it */
779
#define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
780 781 782
#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)
783

784 785 786 787 788 789 790
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),
791 792
#endif
};
793
EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
794 795 796

const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
EXPORT_SYMBOL(cpu_all_bits);
797 798

#ifdef CONFIG_INIT_ALL_POSSIBLE
799
struct cpumask __cpu_possible_mask __read_mostly
800
	= {CPU_BITS_ALL};
801
#else
802
struct cpumask __cpu_possible_mask __read_mostly;
803
#endif
804
EXPORT_SYMBOL(__cpu_possible_mask);
805

806 807
struct cpumask __cpu_online_mask __read_mostly;
EXPORT_SYMBOL(__cpu_online_mask);
808

809 810
struct cpumask __cpu_present_mask __read_mostly;
EXPORT_SYMBOL(__cpu_present_mask);
811

812 813
struct cpumask __cpu_active_mask __read_mostly;
EXPORT_SYMBOL(__cpu_active_mask);
814 815 816

void init_cpu_present(const struct cpumask *src)
{
817
	cpumask_copy(&__cpu_present_mask, src);
818 819 820 821
}

void init_cpu_possible(const struct cpumask *src)
{
822
	cpumask_copy(&__cpu_possible_mask, src);
823 824 825 826
}

void init_cpu_online(const struct cpumask *src)
{
827
	cpumask_copy(&__cpu_online_mask, src);
828
}