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