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