• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* CPU control.
2  * (C) 2001, 2002, 2003, 2004 Rusty Russell
3  *
4  * This code is licenced under the GPL.
5  */
6 #include <linux/proc_fs.h>
7 #include <linux/smp.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/oom.h>
14 #include <linux/rcupdate.h>
15 #include <linux/export.h>
16 #include <linux/bug.h>
17 #include <linux/kthread.h>
18 #include <linux/stop_machine.h>
19 #include <linux/mutex.h>
20 #include <linux/gfp.h>
21 #include <linux/suspend.h>
22 
23 #include <trace/events/sched.h>
24 
25 #include "smpboot.h"
26 
27 #ifdef CONFIG_SMP
28 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
29 static DEFINE_MUTEX(cpu_add_remove_lock);
30 
31 /*
32  * The following two APIs (cpu_maps_update_begin/done) must be used when
33  * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
34  * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
35  * hotplug callback (un)registration performed using __register_cpu_notifier()
36  * or __unregister_cpu_notifier().
37  */
cpu_maps_update_begin(void)38 void cpu_maps_update_begin(void)
39 {
40 	mutex_lock(&cpu_add_remove_lock);
41 }
42 EXPORT_SYMBOL(cpu_notifier_register_begin);
43 
cpu_maps_update_done(void)44 void cpu_maps_update_done(void)
45 {
46 	mutex_unlock(&cpu_add_remove_lock);
47 }
48 EXPORT_SYMBOL(cpu_notifier_register_done);
49 
50 static RAW_NOTIFIER_HEAD(cpu_chain);
51 
52 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
53  * Should always be manipulated under cpu_add_remove_lock
54  */
55 static int cpu_hotplug_disabled;
56 
57 #ifdef CONFIG_HOTPLUG_CPU
58 
59 static struct {
60 	struct task_struct *active_writer;
61 	struct mutex lock; /* Synchronizes accesses to refcount, */
62 	/*
63 	 * Also blocks the new readers during
64 	 * an ongoing cpu hotplug operation.
65 	 */
66 	int refcount;
67 } cpu_hotplug = {
68 	.active_writer = NULL,
69 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
70 	.refcount = 0,
71 };
72 
get_online_cpus(void)73 void get_online_cpus(void)
74 {
75 	might_sleep();
76 	if (cpu_hotplug.active_writer == current)
77 		return;
78 	mutex_lock(&cpu_hotplug.lock);
79 	cpu_hotplug.refcount++;
80 	mutex_unlock(&cpu_hotplug.lock);
81 
82 }
83 EXPORT_SYMBOL_GPL(get_online_cpus);
84 
put_online_cpus(void)85 void put_online_cpus(void)
86 {
87 	if (cpu_hotplug.active_writer == current)
88 		return;
89 	mutex_lock(&cpu_hotplug.lock);
90 
91 	if (WARN_ON(!cpu_hotplug.refcount))
92 		cpu_hotplug.refcount++; /* try to fix things up */
93 
94 	if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
95 		wake_up_process(cpu_hotplug.active_writer);
96 	mutex_unlock(&cpu_hotplug.lock);
97 
98 }
99 EXPORT_SYMBOL_GPL(put_online_cpus);
100 
101 /*
102  * This ensures that the hotplug operation can begin only when the
103  * refcount goes to zero.
104  *
105  * Note that during a cpu-hotplug operation, the new readers, if any,
106  * will be blocked by the cpu_hotplug.lock
107  *
108  * Since cpu_hotplug_begin() is always called after invoking
109  * cpu_maps_update_begin(), we can be sure that only one writer is active.
110  *
111  * Note that theoretically, there is a possibility of a livelock:
112  * - Refcount goes to zero, last reader wakes up the sleeping
113  *   writer.
114  * - Last reader unlocks the cpu_hotplug.lock.
115  * - A new reader arrives at this moment, bumps up the refcount.
116  * - The writer acquires the cpu_hotplug.lock finds the refcount
117  *   non zero and goes to sleep again.
118  *
119  * However, this is very difficult to achieve in practice since
120  * get_online_cpus() not an api which is called all that often.
121  *
122  */
cpu_hotplug_begin(void)123 static void cpu_hotplug_begin(void)
124 {
125 	cpu_hotplug.active_writer = current;
126 
127 	for (;;) {
128 		mutex_lock(&cpu_hotplug.lock);
129 		if (likely(!cpu_hotplug.refcount))
130 			break;
131 		__set_current_state(TASK_UNINTERRUPTIBLE);
132 		mutex_unlock(&cpu_hotplug.lock);
133 		schedule();
134 	}
135 }
136 
cpu_hotplug_done(void)137 static void cpu_hotplug_done(void)
138 {
139 	cpu_hotplug.active_writer = NULL;
140 	mutex_unlock(&cpu_hotplug.lock);
141 }
142 
143 /*
144  * Wait for currently running CPU hotplug operations to complete (if any) and
145  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
146  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
147  * hotplug path before performing hotplug operations. So acquiring that lock
148  * guarantees mutual exclusion from any currently running hotplug operations.
149  */
cpu_hotplug_disable(void)150 void cpu_hotplug_disable(void)
151 {
152 	cpu_maps_update_begin();
153 	cpu_hotplug_disabled = 1;
154 	cpu_maps_update_done();
155 }
156 
cpu_hotplug_enable(void)157 void cpu_hotplug_enable(void)
158 {
159 	cpu_maps_update_begin();
160 	cpu_hotplug_disabled = 0;
161 	cpu_maps_update_done();
162 }
163 
164 #else /* #if CONFIG_HOTPLUG_CPU */
cpu_hotplug_begin(void)165 static void cpu_hotplug_begin(void) {}
cpu_hotplug_done(void)166 static void cpu_hotplug_done(void) {}
167 #endif	/* #else #if CONFIG_HOTPLUG_CPU */
168 
169 /* Need to know about CPUs going up/down? */
register_cpu_notifier(struct notifier_block * nb)170 int __ref register_cpu_notifier(struct notifier_block *nb)
171 {
172 	int ret;
173 	cpu_maps_update_begin();
174 	ret = raw_notifier_chain_register(&cpu_chain, nb);
175 	cpu_maps_update_done();
176 	return ret;
177 }
178 
__register_cpu_notifier(struct notifier_block * nb)179 int __ref __register_cpu_notifier(struct notifier_block *nb)
180 {
181 	return raw_notifier_chain_register(&cpu_chain, nb);
182 }
183 
__cpu_notify(unsigned long val,void * v,int nr_to_call,int * nr_calls)184 static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
185 			int *nr_calls)
186 {
187 	int ret;
188 
189 	ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
190 					nr_calls);
191 
192 	return notifier_to_errno(ret);
193 }
194 
cpu_notify(unsigned long val,void * v)195 static int cpu_notify(unsigned long val, void *v)
196 {
197 	return __cpu_notify(val, v, -1, NULL);
198 }
199 
200 #ifdef CONFIG_HOTPLUG_CPU
201 
cpu_notify_nofail(unsigned long val,void * v)202 static void cpu_notify_nofail(unsigned long val, void *v)
203 {
204 	BUG_ON(cpu_notify(val, v));
205 }
206 EXPORT_SYMBOL(register_cpu_notifier);
207 EXPORT_SYMBOL(__register_cpu_notifier);
208 
unregister_cpu_notifier(struct notifier_block * nb)209 void __ref unregister_cpu_notifier(struct notifier_block *nb)
210 {
211 	cpu_maps_update_begin();
212 	raw_notifier_chain_unregister(&cpu_chain, nb);
213 	cpu_maps_update_done();
214 }
215 EXPORT_SYMBOL(unregister_cpu_notifier);
216 
__unregister_cpu_notifier(struct notifier_block * nb)217 void __ref __unregister_cpu_notifier(struct notifier_block *nb)
218 {
219 	raw_notifier_chain_unregister(&cpu_chain, nb);
220 }
221 EXPORT_SYMBOL(__unregister_cpu_notifier);
222 
223 /**
224  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
225  * @cpu: a CPU id
226  *
227  * This function walks all processes, finds a valid mm struct for each one and
228  * then clears a corresponding bit in mm's cpumask.  While this all sounds
229  * trivial, there are various non-obvious corner cases, which this function
230  * tries to solve in a safe manner.
231  *
232  * Also note that the function uses a somewhat relaxed locking scheme, so it may
233  * be called only for an already offlined CPU.
234  */
clear_tasks_mm_cpumask(int cpu)235 void clear_tasks_mm_cpumask(int cpu)
236 {
237 	struct task_struct *p;
238 
239 	/*
240 	 * This function is called after the cpu is taken down and marked
241 	 * offline, so its not like new tasks will ever get this cpu set in
242 	 * their mm mask. -- Peter Zijlstra
243 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
244 	 * full-fledged tasklist_lock.
245 	 */
246 	WARN_ON(cpu_online(cpu));
247 	rcu_read_lock();
248 	for_each_process(p) {
249 		struct task_struct *t;
250 
251 		/*
252 		 * Main thread might exit, but other threads may still have
253 		 * a valid mm. Find one.
254 		 */
255 		t = find_lock_task_mm(p);
256 		if (!t)
257 			continue;
258 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
259 		task_unlock(t);
260 	}
261 	rcu_read_unlock();
262 }
263 
check_for_tasks(int cpu)264 static inline void check_for_tasks(int cpu)
265 {
266 	struct task_struct *p;
267 	cputime_t utime, stime;
268 
269 	write_lock_irq(&tasklist_lock);
270 	for_each_process(p) {
271 		task_cputime(p, &utime, &stime);
272 		if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
273 		    (utime || stime))
274 			printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d "
275 				"(state = %ld, flags = %x)\n",
276 				p->comm, task_pid_nr(p), cpu,
277 				p->state, p->flags);
278 	}
279 	write_unlock_irq(&tasklist_lock);
280 }
281 
282 struct take_cpu_down_param {
283 	unsigned long mod;
284 	void *hcpu;
285 };
286 
287 /* Take this CPU down. */
take_cpu_down(void * _param)288 static int __ref take_cpu_down(void *_param)
289 {
290 	struct take_cpu_down_param *param = _param;
291 	int err;
292 
293 	/* Ensure this CPU doesn't handle any more interrupts. */
294 	err = __cpu_disable();
295 	if (err < 0)
296 		return err;
297 
298 	cpu_notify(CPU_DYING | param->mod, param->hcpu);
299 	/* Park the stopper thread */
300 	kthread_park(current);
301 	return 0;
302 }
303 
304 /* Requires cpu_add_remove_lock to be held */
_cpu_down(unsigned int cpu,int tasks_frozen)305 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
306 {
307 	int err, nr_calls = 0;
308 	void *hcpu = (void *)(long)cpu;
309 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
310 	struct take_cpu_down_param tcd_param = {
311 		.mod = mod,
312 		.hcpu = hcpu,
313 	};
314 
315 	if (num_online_cpus() == 1)
316 		return -EBUSY;
317 
318 	if (!cpu_online(cpu))
319 		return -EINVAL;
320 
321 	cpu_hotplug_begin();
322 
323 	err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
324 	if (err) {
325 		nr_calls--;
326 		__cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
327 		printk("%s: attempt to take down CPU %u failed\n",
328 				__func__, cpu);
329 		goto out_release;
330 	}
331 	smpboot_park_threads(cpu);
332 
333 	err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
334 	if (err) {
335 		/* CPU didn't die: tell everyone.  Can't complain. */
336 		smpboot_unpark_threads(cpu);
337 		cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
338 		goto out_release;
339 	}
340 	BUG_ON(cpu_online(cpu));
341 
342 	/*
343 	 * The migration_call() CPU_DYING callback will have removed all
344 	 * runnable tasks from the cpu, there's only the idle task left now
345 	 * that the migration thread is done doing the stop_machine thing.
346 	 *
347 	 * Wait for the stop thread to go away.
348 	 */
349 	while (!idle_cpu(cpu))
350 		cpu_relax();
351 
352 	/* This actually kills the CPU. */
353 	__cpu_die(cpu);
354 
355 	/* CPU is completely dead: tell everyone.  Too late to complain. */
356 	cpu_notify_nofail(CPU_DEAD | mod, hcpu);
357 
358 	check_for_tasks(cpu);
359 
360 out_release:
361 	cpu_hotplug_done();
362 	trace_sched_cpu_hotplug(cpu, err, 0);
363 	if (!err)
364 		cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
365 	return err;
366 }
367 
cpu_down(unsigned int cpu)368 int __ref cpu_down(unsigned int cpu)
369 {
370 	int err;
371 
372 	cpu_maps_update_begin();
373 
374 	if (cpu_hotplug_disabled) {
375 		err = -EBUSY;
376 		goto out;
377 	}
378 
379 	err = _cpu_down(cpu, 0);
380 
381 out:
382 	cpu_maps_update_done();
383 	return err;
384 }
385 EXPORT_SYMBOL(cpu_down);
386 #endif /*CONFIG_HOTPLUG_CPU*/
387 
388 /* Requires cpu_add_remove_lock to be held */
_cpu_up(unsigned int cpu,int tasks_frozen)389 static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
390 {
391 	int ret, nr_calls = 0;
392 	void *hcpu = (void *)(long)cpu;
393 	unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
394 	struct task_struct *idle;
395 
396 	cpu_hotplug_begin();
397 
398 	if (cpu_online(cpu) || !cpu_present(cpu)) {
399 		ret = -EINVAL;
400 		goto out;
401 	}
402 
403 	idle = idle_thread_get(cpu);
404 	if (IS_ERR(idle)) {
405 		ret = PTR_ERR(idle);
406 		goto out;
407 	}
408 
409 	ret = smpboot_create_threads(cpu);
410 	if (ret)
411 		goto out;
412 
413 	ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
414 	if (ret) {
415 		nr_calls--;
416 		printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
417 				__func__, cpu);
418 		goto out_notify;
419 	}
420 
421 	/* Arch-specific enabling code. */
422 	ret = __cpu_up(cpu, idle);
423 	if (ret != 0)
424 		goto out_notify;
425 	BUG_ON(!cpu_online(cpu));
426 
427 	/* Wake the per cpu threads */
428 	smpboot_unpark_threads(cpu);
429 
430 	/* Now call notifier in preparation. */
431 	cpu_notify(CPU_ONLINE | mod, hcpu);
432 
433 out_notify:
434 	if (ret != 0)
435 		__cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
436 out:
437 	cpu_hotplug_done();
438 	trace_sched_cpu_hotplug(cpu, ret, 1);
439 
440 	return ret;
441 }
442 
cpu_up(unsigned int cpu)443 int __cpuinit cpu_up(unsigned int cpu)
444 {
445 	int err = 0;
446 
447 #ifdef	CONFIG_MEMORY_HOTPLUG
448 	int nid;
449 	pg_data_t	*pgdat;
450 #endif
451 
452 	if (!cpu_possible(cpu)) {
453 		printk(KERN_ERR "can't online cpu %d because it is not "
454 			"configured as may-hotadd at boot time\n", cpu);
455 #if defined(CONFIG_IA64)
456 		printk(KERN_ERR "please check additional_cpus= boot "
457 				"parameter\n");
458 #endif
459 		return -EINVAL;
460 	}
461 
462 #ifdef	CONFIG_MEMORY_HOTPLUG
463 	nid = cpu_to_node(cpu);
464 	if (!node_online(nid)) {
465 		err = mem_online_node(nid);
466 		if (err)
467 			return err;
468 	}
469 
470 	pgdat = NODE_DATA(nid);
471 	if (!pgdat) {
472 		printk(KERN_ERR
473 			"Can't online cpu %d due to NULL pgdat\n", cpu);
474 		return -ENOMEM;
475 	}
476 
477 	if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
478 		mutex_lock(&zonelists_mutex);
479 		build_all_zonelists(NULL, NULL);
480 		mutex_unlock(&zonelists_mutex);
481 	}
482 #endif
483 
484 	cpu_maps_update_begin();
485 
486 	if (cpu_hotplug_disabled) {
487 		err = -EBUSY;
488 		goto out;
489 	}
490 
491 	err = _cpu_up(cpu, 0);
492 
493 out:
494 	cpu_maps_update_done();
495 	return err;
496 }
497 EXPORT_SYMBOL_GPL(cpu_up);
498 
499 #ifdef CONFIG_PM_SLEEP_SMP
500 static cpumask_var_t frozen_cpus;
501 
disable_nonboot_cpus(void)502 int disable_nonboot_cpus(void)
503 {
504 	int cpu, first_cpu, error = 0;
505 
506 	cpu_maps_update_begin();
507 	first_cpu = cpumask_first(cpu_online_mask);
508 	/*
509 	 * We take down all of the non-boot CPUs in one shot to avoid races
510 	 * with the userspace trying to use the CPU hotplug at the same time
511 	 */
512 	cpumask_clear(frozen_cpus);
513 
514 	printk("Disabling non-boot CPUs ...\n");
515 	for_each_online_cpu(cpu) {
516 		if (cpu == first_cpu)
517 			continue;
518 		error = _cpu_down(cpu, 1);
519 		if (!error)
520 			cpumask_set_cpu(cpu, frozen_cpus);
521 		else {
522 			printk(KERN_ERR "Error taking CPU%d down: %d\n",
523 				cpu, error);
524 			break;
525 		}
526 	}
527 
528 	if (!error) {
529 		BUG_ON(num_online_cpus() > 1);
530 		/* Make sure the CPUs won't be enabled by someone else */
531 		cpu_hotplug_disabled = 1;
532 	} else {
533 		printk(KERN_ERR "Non-boot CPUs are not disabled\n");
534 	}
535 	cpu_maps_update_done();
536 	return error;
537 }
538 
arch_enable_nonboot_cpus_begin(void)539 void __weak arch_enable_nonboot_cpus_begin(void)
540 {
541 }
542 
arch_enable_nonboot_cpus_end(void)543 void __weak arch_enable_nonboot_cpus_end(void)
544 {
545 }
546 
enable_nonboot_cpus(void)547 void __ref enable_nonboot_cpus(void)
548 {
549 	int cpu, error;
550 	struct device *cpu_device;
551 
552 	/* Allow everyone to use the CPU hotplug again */
553 	cpu_maps_update_begin();
554 	cpu_hotplug_disabled = 0;
555 	if (cpumask_empty(frozen_cpus))
556 		goto out;
557 
558 	printk(KERN_INFO "Enabling non-boot CPUs ...\n");
559 
560 	arch_enable_nonboot_cpus_begin();
561 
562 	for_each_cpu(cpu, frozen_cpus) {
563 		error = _cpu_up(cpu, 1);
564 		if (!error) {
565 			printk(KERN_INFO "CPU%d is up\n", cpu);
566 			cpu_device = get_cpu_device(cpu);
567 			if (!cpu_device)
568 				pr_err("%s: failed to get cpu%d device\n",
569 				       __func__, cpu);
570 			else
571 				kobject_uevent(&cpu_device->kobj, KOBJ_ONLINE);
572 			continue;
573 		}
574 		printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
575 	}
576 
577 	arch_enable_nonboot_cpus_end();
578 
579 	cpumask_clear(frozen_cpus);
580 out:
581 	cpu_maps_update_done();
582 }
583 
alloc_frozen_cpus(void)584 static int __init alloc_frozen_cpus(void)
585 {
586 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
587 		return -ENOMEM;
588 	return 0;
589 }
590 core_initcall(alloc_frozen_cpus);
591 
592 /*
593  * When callbacks for CPU hotplug notifications are being executed, we must
594  * ensure that the state of the system with respect to the tasks being frozen
595  * or not, as reported by the notification, remains unchanged *throughout the
596  * duration* of the execution of the callbacks.
597  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
598  *
599  * This synchronization is implemented by mutually excluding regular CPU
600  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
601  * Hibernate notifications.
602  */
603 static int
cpu_hotplug_pm_callback(struct notifier_block * nb,unsigned long action,void * ptr)604 cpu_hotplug_pm_callback(struct notifier_block *nb,
605 			unsigned long action, void *ptr)
606 {
607 	switch (action) {
608 
609 	case PM_SUSPEND_PREPARE:
610 	case PM_HIBERNATION_PREPARE:
611 		cpu_hotplug_disable();
612 		break;
613 
614 	case PM_POST_SUSPEND:
615 	case PM_POST_HIBERNATION:
616 		cpu_hotplug_enable();
617 		break;
618 
619 	default:
620 		return NOTIFY_DONE;
621 	}
622 
623 	return NOTIFY_OK;
624 }
625 
626 
cpu_hotplug_pm_sync_init(void)627 static int __init cpu_hotplug_pm_sync_init(void)
628 {
629 	/*
630 	 * cpu_hotplug_pm_callback has higher priority than x86
631 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
632 	 * to disable cpu hotplug to avoid cpu hotplug race.
633 	 */
634 	pm_notifier(cpu_hotplug_pm_callback, 0);
635 	return 0;
636 }
637 core_initcall(cpu_hotplug_pm_sync_init);
638 
639 #endif /* CONFIG_PM_SLEEP_SMP */
640 
641 /**
642  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
643  * @cpu: cpu that just started
644  *
645  * This function calls the cpu_chain notifiers with CPU_STARTING.
646  * It must be called by the arch code on the new cpu, before the new cpu
647  * enables interrupts and before the "boot" cpu returns from __cpu_up().
648  */
notify_cpu_starting(unsigned int cpu)649 void __cpuinit notify_cpu_starting(unsigned int cpu)
650 {
651 	unsigned long val = CPU_STARTING;
652 
653 #ifdef CONFIG_PM_SLEEP_SMP
654 	if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
655 		val = CPU_STARTING_FROZEN;
656 #endif /* CONFIG_PM_SLEEP_SMP */
657 	cpu_notify(val, (void *)(long)cpu);
658 }
659 
660 #endif /* CONFIG_SMP */
661 
662 /*
663  * cpu_bit_bitmap[] is a special, "compressed" data structure that
664  * represents all NR_CPUS bits binary values of 1<<nr.
665  *
666  * It is used by cpumask_of() to get a constant address to a CPU
667  * mask value that has a single bit set only.
668  */
669 
670 /* cpu_bit_bitmap[0] is empty - so we can back into it */
671 #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
672 #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
673 #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
674 #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
675 
676 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
677 
678 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
679 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
680 #if BITS_PER_LONG > 32
681 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
682 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
683 #endif
684 };
685 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
686 
687 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
688 EXPORT_SYMBOL(cpu_all_bits);
689 
690 #ifdef CONFIG_INIT_ALL_POSSIBLE
691 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
692 	= CPU_BITS_ALL;
693 #else
694 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
695 #endif
696 const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
697 EXPORT_SYMBOL(cpu_possible_mask);
698 
699 static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
700 const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
701 EXPORT_SYMBOL(cpu_online_mask);
702 
703 static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
704 const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
705 EXPORT_SYMBOL(cpu_present_mask);
706 
707 static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
708 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
709 EXPORT_SYMBOL(cpu_active_mask);
710 
set_cpu_possible(unsigned int cpu,bool possible)711 void set_cpu_possible(unsigned int cpu, bool possible)
712 {
713 	if (possible)
714 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
715 	else
716 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
717 }
718 
set_cpu_present(unsigned int cpu,bool present)719 void set_cpu_present(unsigned int cpu, bool present)
720 {
721 	if (present)
722 		cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
723 	else
724 		cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
725 }
726 
set_cpu_online(unsigned int cpu,bool online)727 void set_cpu_online(unsigned int cpu, bool online)
728 {
729 	if (online)
730 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
731 	else
732 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
733 }
734 
set_cpu_active(unsigned int cpu,bool active)735 void set_cpu_active(unsigned int cpu, bool active)
736 {
737 	if (active)
738 		cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
739 	else
740 		cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
741 }
742 
init_cpu_present(const struct cpumask * src)743 void init_cpu_present(const struct cpumask *src)
744 {
745 	cpumask_copy(to_cpumask(cpu_present_bits), src);
746 }
747 
init_cpu_possible(const struct cpumask * src)748 void init_cpu_possible(const struct cpumask *src)
749 {
750 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
751 }
752 
init_cpu_online(const struct cpumask * src)753 void init_cpu_online(const struct cpumask *src)
754 {
755 	cpumask_copy(to_cpumask(cpu_online_bits), src);
756 }
757 
758 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
759 
idle_notifier_register(struct notifier_block * n)760 void idle_notifier_register(struct notifier_block *n)
761 {
762 	atomic_notifier_chain_register(&idle_notifier, n);
763 }
764 EXPORT_SYMBOL_GPL(idle_notifier_register);
765 
idle_notifier_unregister(struct notifier_block * n)766 void idle_notifier_unregister(struct notifier_block *n)
767 {
768 	atomic_notifier_chain_unregister(&idle_notifier, n);
769 }
770 EXPORT_SYMBOL_GPL(idle_notifier_unregister);
771 
idle_notifier_call_chain(unsigned long val)772 void idle_notifier_call_chain(unsigned long val)
773 {
774 	atomic_notifier_call_chain(&idle_notifier, val, NULL);
775 }
776 EXPORT_SYMBOL_GPL(idle_notifier_call_chain);
777