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