1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel_stat.h>
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/percpu.h>
26#include <linux/init.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
29#include <linux/pid_namespace.h>
30#include <linux/notifier.h>
31#include <linux/thread_info.h>
32#include <linux/time.h>
33#include <linux/jiffies.h>
34#include <linux/posix-timers.h>
35#include <linux/cpu.h>
36#include <linux/syscalls.h>
37#include <linux/delay.h>
38#include <linux/tick.h>
39#include <linux/kallsyms.h>
40
41#include <asm/uaccess.h>
42#include <asm/unistd.h>
43#include <asm/div64.h>
44#include <asm/timex.h>
45#include <asm/io.h>
46
47u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
48
49EXPORT_SYMBOL(jiffies_64);
50
51
52
53
54#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
55#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
56#define TVN_SIZE (1 << TVN_BITS)
57#define TVR_SIZE (1 << TVR_BITS)
58#define TVN_MASK (TVN_SIZE - 1)
59#define TVR_MASK (TVR_SIZE - 1)
60
61struct tvec {
62 struct list_head vec[TVN_SIZE];
63};
64
65struct tvec_root {
66 struct list_head vec[TVR_SIZE];
67};
68
69struct tvec_base {
70 spinlock_t lock;
71 struct timer_list *running_timer;
72 unsigned long timer_jiffies;
73 struct tvec_root tv1;
74 struct tvec tv2;
75 struct tvec tv3;
76 struct tvec tv4;
77 struct tvec tv5;
78} ____cacheline_aligned;
79
80struct tvec_base boot_tvec_bases;
81EXPORT_SYMBOL(boot_tvec_bases);
82static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
83
84
85
86
87
88
89#define TBASE_DEFERRABLE_FLAG (0x1)
90
91
92static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
93{
94 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
95}
96
97static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
98{
99 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
100}
101
102static inline void timer_set_deferrable(struct timer_list *timer)
103{
104 timer->base = ((struct tvec_base *)((unsigned long)(timer->base) |
105 TBASE_DEFERRABLE_FLAG));
106}
107
108static inline void
109timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
110{
111 timer->base = (struct tvec_base *)((unsigned long)(new_base) |
112 tbase_get_deferrable(timer->base));
113}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135unsigned long __round_jiffies(unsigned long j, int cpu)
136{
137 int rem;
138 unsigned long original = j;
139
140
141
142
143
144
145
146
147
148 j += cpu * 3;
149
150 rem = j % HZ;
151
152
153
154
155
156
157
158 if (rem < HZ/4)
159 j = j - rem;
160 else
161 j = j - rem + HZ;
162
163
164 j -= cpu * 3;
165
166 if (j <= jiffies)
167 return original;
168 return j;
169}
170EXPORT_SYMBOL_GPL(__round_jiffies);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192unsigned long __round_jiffies_relative(unsigned long j, int cpu)
193{
194
195
196
197
198
199
200 return __round_jiffies(j + jiffies, cpu) - jiffies;
201}
202EXPORT_SYMBOL_GPL(__round_jiffies_relative);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219unsigned long round_jiffies(unsigned long j)
220{
221 return __round_jiffies(j, raw_smp_processor_id());
222}
223EXPORT_SYMBOL_GPL(round_jiffies);
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240unsigned long round_jiffies_relative(unsigned long j)
241{
242 return __round_jiffies_relative(j, raw_smp_processor_id());
243}
244EXPORT_SYMBOL_GPL(round_jiffies_relative);
245
246
247static inline void set_running_timer(struct tvec_base *base,
248 struct timer_list *timer)
249{
250#ifdef CONFIG_SMP
251 base->running_timer = timer;
252#endif
253}
254
255static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
256{
257 unsigned long expires = timer->expires;
258 unsigned long idx = expires - base->timer_jiffies;
259 struct list_head *vec;
260
261 if (idx < TVR_SIZE) {
262 int i = expires & TVR_MASK;
263 vec = base->tv1.vec + i;
264 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
265 int i = (expires >> TVR_BITS) & TVN_MASK;
266 vec = base->tv2.vec + i;
267 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
268 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
269 vec = base->tv3.vec + i;
270 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
271 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
272 vec = base->tv4.vec + i;
273 } else if ((signed long) idx < 0) {
274
275
276
277
278 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
279 } else {
280 int i;
281
282
283
284 if (idx > 0xffffffffUL) {
285 idx = 0xffffffffUL;
286 expires = idx + base->timer_jiffies;
287 }
288 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
289 vec = base->tv5.vec + i;
290 }
291
292
293
294 list_add_tail(&timer->entry, vec);
295}
296
297#ifdef CONFIG_TIMER_STATS
298void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
299{
300 if (timer->start_site)
301 return;
302
303 timer->start_site = addr;
304 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
305 timer->start_pid = current->pid;
306}
307
308static void timer_stats_account_timer(struct timer_list *timer)
309{
310 unsigned int flag = 0;
311
312 if (unlikely(tbase_get_deferrable(timer->base)))
313 flag |= TIMER_STATS_FLAG_DEFERRABLE;
314
315 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
316 timer->function, timer->start_comm, flag);
317}
318
319#else
320static void timer_stats_account_timer(struct timer_list *timer) {}
321#endif
322
323
324
325
326
327
328
329
330void init_timer(struct timer_list *timer)
331{
332 timer->entry.next = NULL;
333 timer->base = __raw_get_cpu_var(tvec_bases);
334#ifdef CONFIG_TIMER_STATS
335 timer->start_site = NULL;
336 timer->start_pid = -1;
337 memset(timer->start_comm, 0, TASK_COMM_LEN);
338#endif
339}
340EXPORT_SYMBOL(init_timer);
341
342void init_timer_deferrable(struct timer_list *timer)
343{
344 init_timer(timer);
345 timer_set_deferrable(timer);
346}
347EXPORT_SYMBOL(init_timer_deferrable);
348
349static inline void detach_timer(struct timer_list *timer,
350 int clear_pending)
351{
352 struct list_head *entry = &timer->entry;
353
354 __list_del(entry->prev, entry->next);
355 if (clear_pending)
356 entry->next = NULL;
357 entry->prev = LIST_POISON2;
358}
359
360
361
362
363
364
365
366
367
368
369
370
371
372static struct tvec_base *lock_timer_base(struct timer_list *timer,
373 unsigned long *flags)
374 __acquires(timer->base->lock)
375{
376 struct tvec_base *base;
377
378 for (;;) {
379 struct tvec_base *prelock_base = timer->base;
380 base = tbase_get_base(prelock_base);
381 if (likely(base != NULL)) {
382 spin_lock_irqsave(&base->lock, *flags);
383 if (likely(prelock_base == timer->base))
384 return base;
385
386 spin_unlock_irqrestore(&base->lock, *flags);
387 }
388 cpu_relax();
389 }
390}
391
392int __mod_timer(struct timer_list *timer, unsigned long expires)
393{
394 struct tvec_base *base, *new_base;
395 unsigned long flags;
396 int ret = 0;
397
398 timer_stats_timer_set_start_info(timer);
399 BUG_ON(!timer->function);
400
401 base = lock_timer_base(timer, &flags);
402
403 if (timer_pending(timer)) {
404 detach_timer(timer, 0);
405 ret = 1;
406 }
407
408 new_base = __get_cpu_var(tvec_bases);
409
410 if (base != new_base) {
411
412
413
414
415
416
417
418 if (likely(base->running_timer != timer)) {
419
420 timer_set_base(timer, NULL);
421 spin_unlock(&base->lock);
422 base = new_base;
423 spin_lock(&base->lock);
424 timer_set_base(timer, base);
425 }
426 }
427
428 timer->expires = expires;
429 internal_add_timer(base, timer);
430 spin_unlock_irqrestore(&base->lock, flags);
431
432 return ret;
433}
434
435EXPORT_SYMBOL(__mod_timer);
436
437
438
439
440
441
442
443
444void add_timer_on(struct timer_list *timer, int cpu)
445{
446 struct tvec_base *base = per_cpu(tvec_bases, cpu);
447 unsigned long flags;
448
449 timer_stats_timer_set_start_info(timer);
450 BUG_ON(timer_pending(timer) || !timer->function);
451 spin_lock_irqsave(&base->lock, flags);
452 timer_set_base(timer, base);
453 internal_add_timer(base, timer);
454
455
456
457
458
459
460
461
462 wake_up_idle_cpu(cpu);
463 spin_unlock_irqrestore(&base->lock, flags);
464}
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486int mod_timer(struct timer_list *timer, unsigned long expires)
487{
488 BUG_ON(!timer->function);
489
490 timer_stats_timer_set_start_info(timer);
491
492
493
494
495
496 if (timer->expires == expires && timer_pending(timer))
497 return 1;
498
499 return __mod_timer(timer, expires);
500}
501
502EXPORT_SYMBOL(mod_timer);
503
504
505
506
507
508
509
510
511
512
513
514
515int del_timer(struct timer_list *timer)
516{
517 struct tvec_base *base;
518 unsigned long flags;
519 int ret = 0;
520
521 timer_stats_timer_clear_start_info(timer);
522 if (timer_pending(timer)) {
523 base = lock_timer_base(timer, &flags);
524 if (timer_pending(timer)) {
525 detach_timer(timer, 1);
526 ret = 1;
527 }
528 spin_unlock_irqrestore(&base->lock, flags);
529 }
530
531 return ret;
532}
533
534EXPORT_SYMBOL(del_timer);
535
536#ifdef CONFIG_SMP
537
538
539
540
541
542
543
544
545
546int try_to_del_timer_sync(struct timer_list *timer)
547{
548 struct tvec_base *base;
549 unsigned long flags;
550 int ret = -1;
551
552 base = lock_timer_base(timer, &flags);
553
554 if (base->running_timer == timer)
555 goto out;
556
557 ret = 0;
558 if (timer_pending(timer)) {
559 detach_timer(timer, 1);
560 ret = 1;
561 }
562out:
563 spin_unlock_irqrestore(&base->lock, flags);
564
565 return ret;
566}
567
568EXPORT_SYMBOL(try_to_del_timer_sync);
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587int del_timer_sync(struct timer_list *timer)
588{
589 for (;;) {
590 int ret = try_to_del_timer_sync(timer);
591 if (ret >= 0)
592 return ret;
593 cpu_relax();
594 }
595}
596
597EXPORT_SYMBOL(del_timer_sync);
598#endif
599
600static int cascade(struct tvec_base *base, struct tvec *tv, int index)
601{
602
603 struct timer_list *timer, *tmp;
604 struct list_head tv_list;
605
606 list_replace_init(tv->vec + index, &tv_list);
607
608
609
610
611
612 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
613 BUG_ON(tbase_get_base(timer->base) != base);
614 internal_add_timer(base, timer);
615 }
616
617 return index;
618}
619
620#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
621
622
623
624
625
626
627
628
629static inline void __run_timers(struct tvec_base *base)
630{
631 struct timer_list *timer;
632
633 spin_lock_irq(&base->lock);
634 while (time_after_eq(jiffies, base->timer_jiffies)) {
635 struct list_head work_list;
636 struct list_head *head = &work_list;
637 int index = base->timer_jiffies & TVR_MASK;
638
639
640
641
642 if (!index &&
643 (!cascade(base, &base->tv2, INDEX(0))) &&
644 (!cascade(base, &base->tv3, INDEX(1))) &&
645 !cascade(base, &base->tv4, INDEX(2)))
646 cascade(base, &base->tv5, INDEX(3));
647 ++base->timer_jiffies;
648 list_replace_init(base->tv1.vec + index, &work_list);
649 while (!list_empty(head)) {
650 void (*fn)(unsigned long);
651 unsigned long data;
652
653 timer = list_first_entry(head, struct timer_list,entry);
654 fn = timer->function;
655 data = timer->data;
656
657 timer_stats_account_timer(timer);
658
659 set_running_timer(base, timer);
660 detach_timer(timer, 1);
661 spin_unlock_irq(&base->lock);
662 {
663 int preempt_count = preempt_count();
664 fn(data);
665 if (preempt_count != preempt_count()) {
666 printk(KERN_ERR "huh, entered %p "
667 "with preempt_count %08x, exited"
668 " with %08x?\n",
669 fn, preempt_count,
670 preempt_count());
671 BUG();
672 }
673 }
674 spin_lock_irq(&base->lock);
675 }
676 }
677 set_running_timer(base, NULL);
678 spin_unlock_irq(&base->lock);
679}
680
681#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
682
683
684
685
686
687static unsigned long __next_timer_interrupt(struct tvec_base *base)
688{
689 unsigned long timer_jiffies = base->timer_jiffies;
690 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
691 int index, slot, array, found = 0;
692 struct timer_list *nte;
693 struct tvec *varray[4];
694
695
696 index = slot = timer_jiffies & TVR_MASK;
697 do {
698 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
699 if (tbase_get_deferrable(nte->base))
700 continue;
701
702 found = 1;
703 expires = nte->expires;
704
705 if (!index || slot < index)
706 goto cascade;
707 return expires;
708 }
709 slot = (slot + 1) & TVR_MASK;
710 } while (slot != index);
711
712cascade:
713
714 if (index)
715 timer_jiffies += TVR_SIZE - index;
716 timer_jiffies >>= TVR_BITS;
717
718
719 varray[0] = &base->tv2;
720 varray[1] = &base->tv3;
721 varray[2] = &base->tv4;
722 varray[3] = &base->tv5;
723
724 for (array = 0; array < 4; array++) {
725 struct tvec *varp = varray[array];
726
727 index = slot = timer_jiffies & TVN_MASK;
728 do {
729 list_for_each_entry(nte, varp->vec + slot, entry) {
730 found = 1;
731 if (time_before(nte->expires, expires))
732 expires = nte->expires;
733 }
734
735
736
737
738 if (found) {
739
740 if (!index || slot < index)
741 break;
742 return expires;
743 }
744 slot = (slot + 1) & TVN_MASK;
745 } while (slot != index);
746
747 if (index)
748 timer_jiffies += TVN_SIZE - index;
749 timer_jiffies >>= TVN_BITS;
750 }
751 return expires;
752}
753
754
755
756
757
758static unsigned long cmp_next_hrtimer_event(unsigned long now,
759 unsigned long expires)
760{
761 ktime_t hr_delta = hrtimer_get_next_event();
762 struct timespec tsdelta;
763 unsigned long delta;
764
765 if (hr_delta.tv64 == KTIME_MAX)
766 return expires;
767
768
769
770
771 if (hr_delta.tv64 <= 0)
772 return now + 1;
773
774 tsdelta = ktime_to_timespec(hr_delta);
775 delta = timespec_to_jiffies(&tsdelta);
776
777
778
779
780
781 if (delta > NEXT_TIMER_MAX_DELTA)
782 delta = NEXT_TIMER_MAX_DELTA;
783
784
785
786
787
788
789
790 if (delta < 1)
791 delta = 1;
792 now += delta;
793 if (time_before(now, expires))
794 return now;
795 return expires;
796}
797
798
799
800
801
802unsigned long get_next_timer_interrupt(unsigned long now)
803{
804 struct tvec_base *base = __get_cpu_var(tvec_bases);
805 unsigned long expires;
806
807 spin_lock(&base->lock);
808 expires = __next_timer_interrupt(base);
809 spin_unlock(&base->lock);
810
811 if (time_before_eq(expires, now))
812 return now;
813
814 return cmp_next_hrtimer_event(now, expires);
815}
816
817#ifdef CONFIG_NO_IDLE_HZ
818unsigned long next_timer_interrupt(void)
819{
820 return get_next_timer_interrupt(jiffies);
821}
822#endif
823
824#endif
825
826#ifndef CONFIG_VIRT_CPU_ACCOUNTING
827void account_process_tick(struct task_struct *p, int user_tick)
828{
829 cputime_t one_jiffy = jiffies_to_cputime(1);
830
831 if (user_tick) {
832 account_user_time(p, one_jiffy);
833 account_user_time_scaled(p, cputime_to_scaled(one_jiffy));
834 } else {
835 account_system_time(p, HARDIRQ_OFFSET, one_jiffy);
836 account_system_time_scaled(p, cputime_to_scaled(one_jiffy));
837 }
838}
839#endif
840
841
842
843
844
845void update_process_times(int user_tick)
846{
847 struct task_struct *p = current;
848 int cpu = smp_processor_id();
849
850
851 account_process_tick(p, user_tick);
852 run_local_timers();
853 if (rcu_pending(cpu))
854 rcu_check_callbacks(cpu, user_tick);
855 scheduler_tick();
856 run_posix_cpu_timers(p);
857}
858
859
860
861
862static unsigned long count_active_tasks(void)
863{
864 return nr_active() * FIXED_1;
865}
866
867
868
869
870
871
872
873
874
875unsigned long avenrun[3];
876
877EXPORT_SYMBOL(avenrun);
878
879
880
881
882
883static inline void calc_load(unsigned long ticks)
884{
885 unsigned long active_tasks;
886 static int count = LOAD_FREQ;
887
888 count -= ticks;
889 if (unlikely(count < 0)) {
890 active_tasks = count_active_tasks();
891 do {
892 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
893 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
894 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
895 count += LOAD_FREQ;
896 } while (count < 0);
897 }
898}
899
900
901
902
903static void run_timer_softirq(struct softirq_action *h)
904{
905 struct tvec_base *base = __get_cpu_var(tvec_bases);
906
907 hrtimer_run_pending();
908
909 if (time_after_eq(jiffies, base->timer_jiffies))
910 __run_timers(base);
911}
912
913
914
915
916void run_local_timers(void)
917{
918 hrtimer_run_queues();
919 raise_softirq(TIMER_SOFTIRQ);
920 softlockup_tick();
921}
922
923
924
925
926
927static inline void update_times(unsigned long ticks)
928{
929 update_wall_time();
930 calc_load(ticks);
931}
932
933
934
935
936
937
938
939void do_timer(unsigned long ticks)
940{
941 jiffies_64 += ticks;
942 update_times(ticks);
943}
944
945#ifdef __ARCH_WANT_SYS_ALARM
946
947
948
949
950
951asmlinkage unsigned long sys_alarm(unsigned int seconds)
952{
953 return alarm_setitimer(seconds);
954}
955
956#endif
957
958#ifndef __alpha__
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974asmlinkage long sys_getpid(void)
975{
976 return task_tgid_vnr(current);
977}
978
979
980
981
982
983
984
985asmlinkage long sys_getppid(void)
986{
987 int pid;
988
989 rcu_read_lock();
990 pid = task_tgid_vnr(current->real_parent);
991 rcu_read_unlock();
992
993 return pid;
994}
995
996asmlinkage long sys_getuid(void)
997{
998
999 return current->uid;
1000}
1001
1002asmlinkage long sys_geteuid(void)
1003{
1004
1005 return current->euid;
1006}
1007
1008asmlinkage long sys_getgid(void)
1009{
1010
1011 return current->gid;
1012}
1013
1014asmlinkage long sys_getegid(void)
1015{
1016
1017 return current->egid;
1018}
1019
1020#endif
1021
1022static void process_timeout(unsigned long __data)
1023{
1024 wake_up_process((struct task_struct *)__data);
1025}
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053signed long __sched schedule_timeout(signed long timeout)
1054{
1055 struct timer_list timer;
1056 unsigned long expire;
1057
1058 switch (timeout)
1059 {
1060 case MAX_SCHEDULE_TIMEOUT:
1061
1062
1063
1064
1065
1066
1067
1068 schedule();
1069 goto out;
1070 default:
1071
1072
1073
1074
1075
1076
1077
1078 if (timeout < 0) {
1079 printk(KERN_ERR "schedule_timeout: wrong timeout "
1080 "value %lx\n", timeout);
1081 dump_stack();
1082 current->state = TASK_RUNNING;
1083 goto out;
1084 }
1085 }
1086
1087 expire = timeout + jiffies;
1088
1089 setup_timer(&timer, process_timeout, (unsigned long)current);
1090 __mod_timer(&timer, expire);
1091 schedule();
1092 del_singleshot_timer_sync(&timer);
1093
1094 timeout = expire - jiffies;
1095
1096 out:
1097 return timeout < 0 ? 0 : timeout;
1098}
1099EXPORT_SYMBOL(schedule_timeout);
1100
1101
1102
1103
1104
1105signed long __sched schedule_timeout_interruptible(signed long timeout)
1106{
1107 __set_current_state(TASK_INTERRUPTIBLE);
1108 return schedule_timeout(timeout);
1109}
1110EXPORT_SYMBOL(schedule_timeout_interruptible);
1111
1112signed long __sched schedule_timeout_killable(signed long timeout)
1113{
1114 __set_current_state(TASK_KILLABLE);
1115 return schedule_timeout(timeout);
1116}
1117EXPORT_SYMBOL(schedule_timeout_killable);
1118
1119signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1120{
1121 __set_current_state(TASK_UNINTERRUPTIBLE);
1122 return schedule_timeout(timeout);
1123}
1124EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1125
1126
1127asmlinkage long sys_gettid(void)
1128{
1129 return task_pid_vnr(current);
1130}
1131
1132
1133
1134
1135
1136int do_sysinfo(struct sysinfo *info)
1137{
1138 unsigned long mem_total, sav_total;
1139 unsigned int mem_unit, bitcount;
1140 unsigned long seq;
1141
1142 memset(info, 0, sizeof(struct sysinfo));
1143
1144 do {
1145 struct timespec tp;
1146 seq = read_seqbegin(&xtime_lock);
1147
1148
1149
1150
1151
1152
1153
1154
1155 getnstimeofday(&tp);
1156 tp.tv_sec += wall_to_monotonic.tv_sec;
1157 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1158 monotonic_to_bootbased(&tp);
1159 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1160 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1161 tp.tv_sec++;
1162 }
1163 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1164
1165 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1166 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1167 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1168
1169 info->procs = nr_threads;
1170 } while (read_seqretry(&xtime_lock, seq));
1171
1172 si_meminfo(info);
1173 si_swapinfo(info);
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184 mem_total = info->totalram + info->totalswap;
1185 if (mem_total < info->totalram || mem_total < info->totalswap)
1186 goto out;
1187 bitcount = 0;
1188 mem_unit = info->mem_unit;
1189 while (mem_unit > 1) {
1190 bitcount++;
1191 mem_unit >>= 1;
1192 sav_total = mem_total;
1193 mem_total <<= 1;
1194 if (mem_total < sav_total)
1195 goto out;
1196 }
1197
1198
1199
1200
1201
1202
1203
1204
1205 info->mem_unit = 1;
1206 info->totalram <<= bitcount;
1207 info->freeram <<= bitcount;
1208 info->sharedram <<= bitcount;
1209 info->bufferram <<= bitcount;
1210 info->totalswap <<= bitcount;
1211 info->freeswap <<= bitcount;
1212 info->totalhigh <<= bitcount;
1213 info->freehigh <<= bitcount;
1214
1215out:
1216 return 0;
1217}
1218
1219asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1220{
1221 struct sysinfo val;
1222
1223 do_sysinfo(&val);
1224
1225 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1226 return -EFAULT;
1227
1228 return 0;
1229}
1230
1231
1232
1233
1234
1235
1236static struct lock_class_key base_lock_keys[NR_CPUS];
1237
1238static int __cpuinit init_timers_cpu(int cpu)
1239{
1240 int j;
1241 struct tvec_base *base;
1242 static char __cpuinitdata tvec_base_done[NR_CPUS];
1243
1244 if (!tvec_base_done[cpu]) {
1245 static char boot_done;
1246
1247 if (boot_done) {
1248
1249
1250
1251 base = kmalloc_node(sizeof(*base),
1252 GFP_KERNEL | __GFP_ZERO,
1253 cpu_to_node(cpu));
1254 if (!base)
1255 return -ENOMEM;
1256
1257
1258 if (tbase_get_deferrable(base)) {
1259 WARN_ON(1);
1260 kfree(base);
1261 return -ENOMEM;
1262 }
1263 per_cpu(tvec_bases, cpu) = base;
1264 } else {
1265
1266
1267
1268
1269
1270
1271 boot_done = 1;
1272 base = &boot_tvec_bases;
1273 }
1274 tvec_base_done[cpu] = 1;
1275 } else {
1276 base = per_cpu(tvec_bases, cpu);
1277 }
1278
1279 spin_lock_init(&base->lock);
1280 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1281
1282 for (j = 0; j < TVN_SIZE; j++) {
1283 INIT_LIST_HEAD(base->tv5.vec + j);
1284 INIT_LIST_HEAD(base->tv4.vec + j);
1285 INIT_LIST_HEAD(base->tv3.vec + j);
1286 INIT_LIST_HEAD(base->tv2.vec + j);
1287 }
1288 for (j = 0; j < TVR_SIZE; j++)
1289 INIT_LIST_HEAD(base->tv1.vec + j);
1290
1291 base->timer_jiffies = jiffies;
1292 return 0;
1293}
1294
1295#ifdef CONFIG_HOTPLUG_CPU
1296static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
1297{
1298 struct timer_list *timer;
1299
1300 while (!list_empty(head)) {
1301 timer = list_first_entry(head, struct timer_list, entry);
1302 detach_timer(timer, 0);
1303 timer_set_base(timer, new_base);
1304 internal_add_timer(new_base, timer);
1305 }
1306}
1307
1308static void __cpuinit migrate_timers(int cpu)
1309{
1310 struct tvec_base *old_base;
1311 struct tvec_base *new_base;
1312 int i;
1313
1314 BUG_ON(cpu_online(cpu));
1315 old_base = per_cpu(tvec_bases, cpu);
1316 new_base = get_cpu_var(tvec_bases);
1317
1318 local_irq_disable();
1319 double_spin_lock(&new_base->lock, &old_base->lock,
1320 smp_processor_id() < cpu);
1321
1322 BUG_ON(old_base->running_timer);
1323
1324 for (i = 0; i < TVR_SIZE; i++)
1325 migrate_timer_list(new_base, old_base->tv1.vec + i);
1326 for (i = 0; i < TVN_SIZE; i++) {
1327 migrate_timer_list(new_base, old_base->tv2.vec + i);
1328 migrate_timer_list(new_base, old_base->tv3.vec + i);
1329 migrate_timer_list(new_base, old_base->tv4.vec + i);
1330 migrate_timer_list(new_base, old_base->tv5.vec + i);
1331 }
1332
1333 double_spin_unlock(&new_base->lock, &old_base->lock,
1334 smp_processor_id() < cpu);
1335 local_irq_enable();
1336 put_cpu_var(tvec_bases);
1337}
1338#endif
1339
1340static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1341 unsigned long action, void *hcpu)
1342{
1343 long cpu = (long)hcpu;
1344 switch(action) {
1345 case CPU_UP_PREPARE:
1346 case CPU_UP_PREPARE_FROZEN:
1347 if (init_timers_cpu(cpu) < 0)
1348 return NOTIFY_BAD;
1349 break;
1350#ifdef CONFIG_HOTPLUG_CPU
1351 case CPU_DEAD:
1352 case CPU_DEAD_FROZEN:
1353 migrate_timers(cpu);
1354 break;
1355#endif
1356 default:
1357 break;
1358 }
1359 return NOTIFY_OK;
1360}
1361
1362static struct notifier_block __cpuinitdata timers_nb = {
1363 .notifier_call = timer_cpu_notify,
1364};
1365
1366
1367void __init init_timers(void)
1368{
1369 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1370 (void *)(long)smp_processor_id());
1371
1372 init_timer_stats();
1373
1374 BUG_ON(err == NOTIFY_BAD);
1375 register_cpu_notifier(&timers_nb);
1376 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1377}
1378
1379
1380
1381
1382
1383void msleep(unsigned int msecs)
1384{
1385 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1386
1387 while (timeout)
1388 timeout = schedule_timeout_uninterruptible(timeout);
1389}
1390
1391EXPORT_SYMBOL(msleep);
1392
1393
1394
1395
1396
1397unsigned long msleep_interruptible(unsigned int msecs)
1398{
1399 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1400
1401 while (timeout && !signal_pending(current))
1402 timeout = schedule_timeout_interruptible(timeout);
1403 return jiffies_to_msecs(timeout);
1404}
1405
1406EXPORT_SYMBOL(msleep_interruptible);
1407