diff options
Diffstat (limited to 'kernel/timer.c')
-rw-r--r-- | kernel/timer.c | 532 |
1 files changed, 64 insertions, 468 deletions
diff --git a/kernel/timer.c b/kernel/timer.c index b22bd39740dd..a6c580ac084b 100644 --- a/kernel/timer.c +++ b/kernel/timer.c | |||
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * linux/kernel/timer.c | 2 | * linux/kernel/timer.c |
3 | * | 3 | * |
4 | * Kernel internal timers, kernel timekeeping, basic process system calls | 4 | * Kernel internal timers, basic process system calls |
5 | * | 5 | * |
6 | * Copyright (C) 1991, 1992 Linus Torvalds | 6 | * Copyright (C) 1991, 1992 Linus Torvalds |
7 | * | 7 | * |
@@ -74,7 +74,7 @@ struct tvec_t_base_s { | |||
74 | tvec_t tv3; | 74 | tvec_t tv3; |
75 | tvec_t tv4; | 75 | tvec_t tv4; |
76 | tvec_t tv5; | 76 | tvec_t tv5; |
77 | } ____cacheline_aligned_in_smp; | 77 | } ____cacheline_aligned; |
78 | 78 | ||
79 | typedef struct tvec_t_base_s tvec_base_t; | 79 | typedef struct tvec_t_base_s tvec_base_t; |
80 | 80 | ||
@@ -82,6 +82,37 @@ tvec_base_t boot_tvec_bases; | |||
82 | EXPORT_SYMBOL(boot_tvec_bases); | 82 | EXPORT_SYMBOL(boot_tvec_bases); |
83 | static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases; | 83 | static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases; |
84 | 84 | ||
85 | /* | ||
86 | * Note that all tvec_bases is 2 byte aligned and lower bit of | ||
87 | * base in timer_list is guaranteed to be zero. Use the LSB for | ||
88 | * the new flag to indicate whether the timer is deferrable | ||
89 | */ | ||
90 | #define TBASE_DEFERRABLE_FLAG (0x1) | ||
91 | |||
92 | /* Functions below help us manage 'deferrable' flag */ | ||
93 | static inline unsigned int tbase_get_deferrable(tvec_base_t *base) | ||
94 | { | ||
95 | return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG); | ||
96 | } | ||
97 | |||
98 | static inline tvec_base_t *tbase_get_base(tvec_base_t *base) | ||
99 | { | ||
100 | return ((tvec_base_t *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG)); | ||
101 | } | ||
102 | |||
103 | static inline void timer_set_deferrable(struct timer_list *timer) | ||
104 | { | ||
105 | timer->base = ((tvec_base_t *)((unsigned long)(timer->base) | | ||
106 | TBASE_DEFERRABLE_FLAG)); | ||
107 | } | ||
108 | |||
109 | static inline void | ||
110 | timer_set_base(struct timer_list *timer, tvec_base_t *new_base) | ||
111 | { | ||
112 | timer->base = (tvec_base_t *)((unsigned long)(new_base) | | ||
113 | tbase_get_deferrable(timer->base)); | ||
114 | } | ||
115 | |||
85 | /** | 116 | /** |
86 | * __round_jiffies - function to round jiffies to a full second | 117 | * __round_jiffies - function to round jiffies to a full second |
87 | * @j: the time in (absolute) jiffies that should be rounded | 118 | * @j: the time in (absolute) jiffies that should be rounded |
@@ -295,6 +326,13 @@ void fastcall init_timer(struct timer_list *timer) | |||
295 | } | 326 | } |
296 | EXPORT_SYMBOL(init_timer); | 327 | EXPORT_SYMBOL(init_timer); |
297 | 328 | ||
329 | void fastcall init_timer_deferrable(struct timer_list *timer) | ||
330 | { | ||
331 | init_timer(timer); | ||
332 | timer_set_deferrable(timer); | ||
333 | } | ||
334 | EXPORT_SYMBOL(init_timer_deferrable); | ||
335 | |||
298 | static inline void detach_timer(struct timer_list *timer, | 336 | static inline void detach_timer(struct timer_list *timer, |
299 | int clear_pending) | 337 | int clear_pending) |
300 | { | 338 | { |
@@ -325,10 +363,11 @@ static tvec_base_t *lock_timer_base(struct timer_list *timer, | |||
325 | tvec_base_t *base; | 363 | tvec_base_t *base; |
326 | 364 | ||
327 | for (;;) { | 365 | for (;;) { |
328 | base = timer->base; | 366 | tvec_base_t *prelock_base = timer->base; |
367 | base = tbase_get_base(prelock_base); | ||
329 | if (likely(base != NULL)) { | 368 | if (likely(base != NULL)) { |
330 | spin_lock_irqsave(&base->lock, *flags); | 369 | spin_lock_irqsave(&base->lock, *flags); |
331 | if (likely(base == timer->base)) | 370 | if (likely(prelock_base == timer->base)) |
332 | return base; | 371 | return base; |
333 | /* The timer has migrated to another CPU */ | 372 | /* The timer has migrated to another CPU */ |
334 | spin_unlock_irqrestore(&base->lock, *flags); | 373 | spin_unlock_irqrestore(&base->lock, *flags); |
@@ -365,11 +404,11 @@ int __mod_timer(struct timer_list *timer, unsigned long expires) | |||
365 | */ | 404 | */ |
366 | if (likely(base->running_timer != timer)) { | 405 | if (likely(base->running_timer != timer)) { |
367 | /* See the comment in lock_timer_base() */ | 406 | /* See the comment in lock_timer_base() */ |
368 | timer->base = NULL; | 407 | timer_set_base(timer, NULL); |
369 | spin_unlock(&base->lock); | 408 | spin_unlock(&base->lock); |
370 | base = new_base; | 409 | base = new_base; |
371 | spin_lock(&base->lock); | 410 | spin_lock(&base->lock); |
372 | timer->base = base; | 411 | timer_set_base(timer, base); |
373 | } | 412 | } |
374 | } | 413 | } |
375 | 414 | ||
@@ -397,7 +436,7 @@ void add_timer_on(struct timer_list *timer, int cpu) | |||
397 | timer_stats_timer_set_start_info(timer); | 436 | timer_stats_timer_set_start_info(timer); |
398 | BUG_ON(timer_pending(timer) || !timer->function); | 437 | BUG_ON(timer_pending(timer) || !timer->function); |
399 | spin_lock_irqsave(&base->lock, flags); | 438 | spin_lock_irqsave(&base->lock, flags); |
400 | timer->base = base; | 439 | timer_set_base(timer, base); |
401 | internal_add_timer(base, timer); | 440 | internal_add_timer(base, timer); |
402 | spin_unlock_irqrestore(&base->lock, flags); | 441 | spin_unlock_irqrestore(&base->lock, flags); |
403 | } | 442 | } |
@@ -550,7 +589,7 @@ static int cascade(tvec_base_t *base, tvec_t *tv, int index) | |||
550 | * don't have to detach them individually. | 589 | * don't have to detach them individually. |
551 | */ | 590 | */ |
552 | list_for_each_entry_safe(timer, tmp, &tv_list, entry) { | 591 | list_for_each_entry_safe(timer, tmp, &tv_list, entry) { |
553 | BUG_ON(timer->base != base); | 592 | BUG_ON(tbase_get_base(timer->base) != base); |
554 | internal_add_timer(base, timer); | 593 | internal_add_timer(base, timer); |
555 | } | 594 | } |
556 | 595 | ||
@@ -590,7 +629,7 @@ static inline void __run_timers(tvec_base_t *base) | |||
590 | void (*fn)(unsigned long); | 629 | void (*fn)(unsigned long); |
591 | unsigned long data; | 630 | unsigned long data; |
592 | 631 | ||
593 | timer = list_entry(head->next,struct timer_list,entry); | 632 | timer = list_first_entry(head, struct timer_list,entry); |
594 | fn = timer->function; | 633 | fn = timer->function; |
595 | data = timer->data; | 634 | data = timer->data; |
596 | 635 | ||
@@ -636,6 +675,9 @@ static unsigned long __next_timer_interrupt(tvec_base_t *base) | |||
636 | index = slot = timer_jiffies & TVR_MASK; | 675 | index = slot = timer_jiffies & TVR_MASK; |
637 | do { | 676 | do { |
638 | list_for_each_entry(nte, base->tv1.vec + slot, entry) { | 677 | list_for_each_entry(nte, base->tv1.vec + slot, entry) { |
678 | if (tbase_get_deferrable(nte->base)) | ||
679 | continue; | ||
680 | |||
639 | found = 1; | 681 | found = 1; |
640 | expires = nte->expires; | 682 | expires = nte->expires; |
641 | /* Look at the cascade bucket(s)? */ | 683 | /* Look at the cascade bucket(s)? */ |
@@ -752,455 +794,6 @@ unsigned long next_timer_interrupt(void) | |||
752 | 794 | ||
753 | #endif | 795 | #endif |
754 | 796 | ||
755 | /******************************************************************/ | ||
756 | |||
757 | /* | ||
758 | * The current time | ||
759 | * wall_to_monotonic is what we need to add to xtime (or xtime corrected | ||
760 | * for sub jiffie times) to get to monotonic time. Monotonic is pegged | ||
761 | * at zero at system boot time, so wall_to_monotonic will be negative, | ||
762 | * however, we will ALWAYS keep the tv_nsec part positive so we can use | ||
763 | * the usual normalization. | ||
764 | */ | ||
765 | struct timespec xtime __attribute__ ((aligned (16))); | ||
766 | struct timespec wall_to_monotonic __attribute__ ((aligned (16))); | ||
767 | |||
768 | EXPORT_SYMBOL(xtime); | ||
769 | |||
770 | |||
771 | /* XXX - all of this timekeeping code should be later moved to time.c */ | ||
772 | #include <linux/clocksource.h> | ||
773 | static struct clocksource *clock; /* pointer to current clocksource */ | ||
774 | |||
775 | #ifdef CONFIG_GENERIC_TIME | ||
776 | /** | ||
777 | * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook | ||
778 | * | ||
779 | * private function, must hold xtime_lock lock when being | ||
780 | * called. Returns the number of nanoseconds since the | ||
781 | * last call to update_wall_time() (adjusted by NTP scaling) | ||
782 | */ | ||
783 | static inline s64 __get_nsec_offset(void) | ||
784 | { | ||
785 | cycle_t cycle_now, cycle_delta; | ||
786 | s64 ns_offset; | ||
787 | |||
788 | /* read clocksource: */ | ||
789 | cycle_now = clocksource_read(clock); | ||
790 | |||
791 | /* calculate the delta since the last update_wall_time: */ | ||
792 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | ||
793 | |||
794 | /* convert to nanoseconds: */ | ||
795 | ns_offset = cyc2ns(clock, cycle_delta); | ||
796 | |||
797 | return ns_offset; | ||
798 | } | ||
799 | |||
800 | /** | ||
801 | * __get_realtime_clock_ts - Returns the time of day in a timespec | ||
802 | * @ts: pointer to the timespec to be set | ||
803 | * | ||
804 | * Returns the time of day in a timespec. Used by | ||
805 | * do_gettimeofday() and get_realtime_clock_ts(). | ||
806 | */ | ||
807 | static inline void __get_realtime_clock_ts(struct timespec *ts) | ||
808 | { | ||
809 | unsigned long seq; | ||
810 | s64 nsecs; | ||
811 | |||
812 | do { | ||
813 | seq = read_seqbegin(&xtime_lock); | ||
814 | |||
815 | *ts = xtime; | ||
816 | nsecs = __get_nsec_offset(); | ||
817 | |||
818 | } while (read_seqretry(&xtime_lock, seq)); | ||
819 | |||
820 | timespec_add_ns(ts, nsecs); | ||
821 | } | ||
822 | |||
823 | /** | ||
824 | * getnstimeofday - Returns the time of day in a timespec | ||
825 | * @ts: pointer to the timespec to be set | ||
826 | * | ||
827 | * Returns the time of day in a timespec. | ||
828 | */ | ||
829 | void getnstimeofday(struct timespec *ts) | ||
830 | { | ||
831 | __get_realtime_clock_ts(ts); | ||
832 | } | ||
833 | |||
834 | EXPORT_SYMBOL(getnstimeofday); | ||
835 | |||
836 | /** | ||
837 | * do_gettimeofday - Returns the time of day in a timeval | ||
838 | * @tv: pointer to the timeval to be set | ||
839 | * | ||
840 | * NOTE: Users should be converted to using get_realtime_clock_ts() | ||
841 | */ | ||
842 | void do_gettimeofday(struct timeval *tv) | ||
843 | { | ||
844 | struct timespec now; | ||
845 | |||
846 | __get_realtime_clock_ts(&now); | ||
847 | tv->tv_sec = now.tv_sec; | ||
848 | tv->tv_usec = now.tv_nsec/1000; | ||
849 | } | ||
850 | |||
851 | EXPORT_SYMBOL(do_gettimeofday); | ||
852 | /** | ||
853 | * do_settimeofday - Sets the time of day | ||
854 | * @tv: pointer to the timespec variable containing the new time | ||
855 | * | ||
856 | * Sets the time of day to the new time and update NTP and notify hrtimers | ||
857 | */ | ||
858 | int do_settimeofday(struct timespec *tv) | ||
859 | { | ||
860 | unsigned long flags; | ||
861 | time_t wtm_sec, sec = tv->tv_sec; | ||
862 | long wtm_nsec, nsec = tv->tv_nsec; | ||
863 | |||
864 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | ||
865 | return -EINVAL; | ||
866 | |||
867 | write_seqlock_irqsave(&xtime_lock, flags); | ||
868 | |||
869 | nsec -= __get_nsec_offset(); | ||
870 | |||
871 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); | ||
872 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); | ||
873 | |||
874 | set_normalized_timespec(&xtime, sec, nsec); | ||
875 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | ||
876 | |||
877 | clock->error = 0; | ||
878 | ntp_clear(); | ||
879 | |||
880 | update_vsyscall(&xtime, clock); | ||
881 | |||
882 | write_sequnlock_irqrestore(&xtime_lock, flags); | ||
883 | |||
884 | /* signal hrtimers about time change */ | ||
885 | clock_was_set(); | ||
886 | |||
887 | return 0; | ||
888 | } | ||
889 | |||
890 | EXPORT_SYMBOL(do_settimeofday); | ||
891 | |||
892 | /** | ||
893 | * change_clocksource - Swaps clocksources if a new one is available | ||
894 | * | ||
895 | * Accumulates current time interval and initializes new clocksource | ||
896 | */ | ||
897 | static void change_clocksource(void) | ||
898 | { | ||
899 | struct clocksource *new; | ||
900 | cycle_t now; | ||
901 | u64 nsec; | ||
902 | |||
903 | new = clocksource_get_next(); | ||
904 | |||
905 | if (clock == new) | ||
906 | return; | ||
907 | |||
908 | now = clocksource_read(new); | ||
909 | nsec = __get_nsec_offset(); | ||
910 | timespec_add_ns(&xtime, nsec); | ||
911 | |||
912 | clock = new; | ||
913 | clock->cycle_last = now; | ||
914 | |||
915 | clock->error = 0; | ||
916 | clock->xtime_nsec = 0; | ||
917 | clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); | ||
918 | |||
919 | tick_clock_notify(); | ||
920 | |||
921 | printk(KERN_INFO "Time: %s clocksource has been installed.\n", | ||
922 | clock->name); | ||
923 | } | ||
924 | #else | ||
925 | static inline void change_clocksource(void) { } | ||
926 | #endif | ||
927 | |||
928 | /** | ||
929 | * timekeeping_is_continuous - check to see if timekeeping is free running | ||
930 | */ | ||
931 | int timekeeping_is_continuous(void) | ||
932 | { | ||
933 | unsigned long seq; | ||
934 | int ret; | ||
935 | |||
936 | do { | ||
937 | seq = read_seqbegin(&xtime_lock); | ||
938 | |||
939 | ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; | ||
940 | |||
941 | } while (read_seqretry(&xtime_lock, seq)); | ||
942 | |||
943 | return ret; | ||
944 | } | ||
945 | |||
946 | /** | ||
947 | * read_persistent_clock - Return time in seconds from the persistent clock. | ||
948 | * | ||
949 | * Weak dummy function for arches that do not yet support it. | ||
950 | * Returns seconds from epoch using the battery backed persistent clock. | ||
951 | * Returns zero if unsupported. | ||
952 | * | ||
953 | * XXX - Do be sure to remove it once all arches implement it. | ||
954 | */ | ||
955 | unsigned long __attribute__((weak)) read_persistent_clock(void) | ||
956 | { | ||
957 | return 0; | ||
958 | } | ||
959 | |||
960 | /* | ||
961 | * timekeeping_init - Initializes the clocksource and common timekeeping values | ||
962 | */ | ||
963 | void __init timekeeping_init(void) | ||
964 | { | ||
965 | unsigned long flags; | ||
966 | unsigned long sec = read_persistent_clock(); | ||
967 | |||
968 | write_seqlock_irqsave(&xtime_lock, flags); | ||
969 | |||
970 | ntp_clear(); | ||
971 | |||
972 | clock = clocksource_get_next(); | ||
973 | clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); | ||
974 | clock->cycle_last = clocksource_read(clock); | ||
975 | |||
976 | xtime.tv_sec = sec; | ||
977 | xtime.tv_nsec = 0; | ||
978 | set_normalized_timespec(&wall_to_monotonic, | ||
979 | -xtime.tv_sec, -xtime.tv_nsec); | ||
980 | |||
981 | write_sequnlock_irqrestore(&xtime_lock, flags); | ||
982 | } | ||
983 | |||
984 | /* flag for if timekeeping is suspended */ | ||
985 | static int timekeeping_suspended; | ||
986 | /* time in seconds when suspend began */ | ||
987 | static unsigned long timekeeping_suspend_time; | ||
988 | |||
989 | /** | ||
990 | * timekeeping_resume - Resumes the generic timekeeping subsystem. | ||
991 | * @dev: unused | ||
992 | * | ||
993 | * This is for the generic clocksource timekeeping. | ||
994 | * xtime/wall_to_monotonic/jiffies/etc are | ||
995 | * still managed by arch specific suspend/resume code. | ||
996 | */ | ||
997 | static int timekeeping_resume(struct sys_device *dev) | ||
998 | { | ||
999 | unsigned long flags; | ||
1000 | unsigned long now = read_persistent_clock(); | ||
1001 | |||
1002 | write_seqlock_irqsave(&xtime_lock, flags); | ||
1003 | |||
1004 | if (now && (now > timekeeping_suspend_time)) { | ||
1005 | unsigned long sleep_length = now - timekeeping_suspend_time; | ||
1006 | |||
1007 | xtime.tv_sec += sleep_length; | ||
1008 | wall_to_monotonic.tv_sec -= sleep_length; | ||
1009 | } | ||
1010 | /* re-base the last cycle value */ | ||
1011 | clock->cycle_last = clocksource_read(clock); | ||
1012 | clock->error = 0; | ||
1013 | timekeeping_suspended = 0; | ||
1014 | write_sequnlock_irqrestore(&xtime_lock, flags); | ||
1015 | |||
1016 | touch_softlockup_watchdog(); | ||
1017 | |||
1018 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); | ||
1019 | |||
1020 | /* Resume hrtimers */ | ||
1021 | hres_timers_resume(); | ||
1022 | |||
1023 | return 0; | ||
1024 | } | ||
1025 | |||
1026 | static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) | ||
1027 | { | ||
1028 | unsigned long flags; | ||
1029 | |||
1030 | write_seqlock_irqsave(&xtime_lock, flags); | ||
1031 | timekeeping_suspended = 1; | ||
1032 | timekeeping_suspend_time = read_persistent_clock(); | ||
1033 | write_sequnlock_irqrestore(&xtime_lock, flags); | ||
1034 | |||
1035 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | ||
1036 | |||
1037 | return 0; | ||
1038 | } | ||
1039 | |||
1040 | /* sysfs resume/suspend bits for timekeeping */ | ||
1041 | static struct sysdev_class timekeeping_sysclass = { | ||
1042 | .resume = timekeeping_resume, | ||
1043 | .suspend = timekeeping_suspend, | ||
1044 | set_kset_name("timekeeping"), | ||
1045 | }; | ||
1046 | |||
1047 | static struct sys_device device_timer = { | ||
1048 | .id = 0, | ||
1049 | .cls = &timekeeping_sysclass, | ||
1050 | }; | ||
1051 | |||
1052 | static int __init timekeeping_init_device(void) | ||
1053 | { | ||
1054 | int error = sysdev_class_register(&timekeeping_sysclass); | ||
1055 | if (!error) | ||
1056 | error = sysdev_register(&device_timer); | ||
1057 | return error; | ||
1058 | } | ||
1059 | |||
1060 | device_initcall(timekeeping_init_device); | ||
1061 | |||
1062 | /* | ||
1063 | * If the error is already larger, we look ahead even further | ||
1064 | * to compensate for late or lost adjustments. | ||
1065 | */ | ||
1066 | static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, | ||
1067 | s64 *offset) | ||
1068 | { | ||
1069 | s64 tick_error, i; | ||
1070 | u32 look_ahead, adj; | ||
1071 | s32 error2, mult; | ||
1072 | |||
1073 | /* | ||
1074 | * Use the current error value to determine how much to look ahead. | ||
1075 | * The larger the error the slower we adjust for it to avoid problems | ||
1076 | * with losing too many ticks, otherwise we would overadjust and | ||
1077 | * produce an even larger error. The smaller the adjustment the | ||
1078 | * faster we try to adjust for it, as lost ticks can do less harm | ||
1079 | * here. This is tuned so that an error of about 1 msec is adusted | ||
1080 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). | ||
1081 | */ | ||
1082 | error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ); | ||
1083 | error2 = abs(error2); | ||
1084 | for (look_ahead = 0; error2 > 0; look_ahead++) | ||
1085 | error2 >>= 2; | ||
1086 | |||
1087 | /* | ||
1088 | * Now calculate the error in (1 << look_ahead) ticks, but first | ||
1089 | * remove the single look ahead already included in the error. | ||
1090 | */ | ||
1091 | tick_error = current_tick_length() >> | ||
1092 | (TICK_LENGTH_SHIFT - clock->shift + 1); | ||
1093 | tick_error -= clock->xtime_interval >> 1; | ||
1094 | error = ((error - tick_error) >> look_ahead) + tick_error; | ||
1095 | |||
1096 | /* Finally calculate the adjustment shift value. */ | ||
1097 | i = *interval; | ||
1098 | mult = 1; | ||
1099 | if (error < 0) { | ||
1100 | error = -error; | ||
1101 | *interval = -*interval; | ||
1102 | *offset = -*offset; | ||
1103 | mult = -1; | ||
1104 | } | ||
1105 | for (adj = 0; error > i; adj++) | ||
1106 | error >>= 1; | ||
1107 | |||
1108 | *interval <<= adj; | ||
1109 | *offset <<= adj; | ||
1110 | return mult << adj; | ||
1111 | } | ||
1112 | |||
1113 | /* | ||
1114 | * Adjust the multiplier to reduce the error value, | ||
1115 | * this is optimized for the most common adjustments of -1,0,1, | ||
1116 | * for other values we can do a bit more work. | ||
1117 | */ | ||
1118 | static void clocksource_adjust(struct clocksource *clock, s64 offset) | ||
1119 | { | ||
1120 | s64 error, interval = clock->cycle_interval; | ||
1121 | int adj; | ||
1122 | |||
1123 | error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1); | ||
1124 | if (error > interval) { | ||
1125 | error >>= 2; | ||
1126 | if (likely(error <= interval)) | ||
1127 | adj = 1; | ||
1128 | else | ||
1129 | adj = clocksource_bigadjust(error, &interval, &offset); | ||
1130 | } else if (error < -interval) { | ||
1131 | error >>= 2; | ||
1132 | if (likely(error >= -interval)) { | ||
1133 | adj = -1; | ||
1134 | interval = -interval; | ||
1135 | offset = -offset; | ||
1136 | } else | ||
1137 | adj = clocksource_bigadjust(error, &interval, &offset); | ||
1138 | } else | ||
1139 | return; | ||
1140 | |||
1141 | clock->mult += adj; | ||
1142 | clock->xtime_interval += interval; | ||
1143 | clock->xtime_nsec -= offset; | ||
1144 | clock->error -= (interval - offset) << | ||
1145 | (TICK_LENGTH_SHIFT - clock->shift); | ||
1146 | } | ||
1147 | |||
1148 | /** | ||
1149 | * update_wall_time - Uses the current clocksource to increment the wall time | ||
1150 | * | ||
1151 | * Called from the timer interrupt, must hold a write on xtime_lock. | ||
1152 | */ | ||
1153 | static void update_wall_time(void) | ||
1154 | { | ||
1155 | cycle_t offset; | ||
1156 | |||
1157 | /* Make sure we're fully resumed: */ | ||
1158 | if (unlikely(timekeeping_suspended)) | ||
1159 | return; | ||
1160 | |||
1161 | #ifdef CONFIG_GENERIC_TIME | ||
1162 | offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask; | ||
1163 | #else | ||
1164 | offset = clock->cycle_interval; | ||
1165 | #endif | ||
1166 | clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift; | ||
1167 | |||
1168 | /* normally this loop will run just once, however in the | ||
1169 | * case of lost or late ticks, it will accumulate correctly. | ||
1170 | */ | ||
1171 | while (offset >= clock->cycle_interval) { | ||
1172 | /* accumulate one interval */ | ||
1173 | clock->xtime_nsec += clock->xtime_interval; | ||
1174 | clock->cycle_last += clock->cycle_interval; | ||
1175 | offset -= clock->cycle_interval; | ||
1176 | |||
1177 | if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) { | ||
1178 | clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift; | ||
1179 | xtime.tv_sec++; | ||
1180 | second_overflow(); | ||
1181 | } | ||
1182 | |||
1183 | /* interpolator bits */ | ||
1184 | time_interpolator_update(clock->xtime_interval | ||
1185 | >> clock->shift); | ||
1186 | |||
1187 | /* accumulate error between NTP and clock interval */ | ||
1188 | clock->error += current_tick_length(); | ||
1189 | clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift); | ||
1190 | } | ||
1191 | |||
1192 | /* correct the clock when NTP error is too big */ | ||
1193 | clocksource_adjust(clock, offset); | ||
1194 | |||
1195 | /* store full nanoseconds into xtime */ | ||
1196 | xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift; | ||
1197 | clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift; | ||
1198 | |||
1199 | /* check to see if there is a new clocksource to use */ | ||
1200 | change_clocksource(); | ||
1201 | update_vsyscall(&xtime, clock); | ||
1202 | } | ||
1203 | |||
1204 | /* | 797 | /* |
1205 | * Called from the timer interrupt handler to charge one tick to the current | 798 | * Called from the timer interrupt handler to charge one tick to the current |
1206 | * process. user_tick is 1 if the tick is user time, 0 for system. | 799 | * process. user_tick is 1 if the tick is user time, 0 for system. |
@@ -1264,14 +857,6 @@ static inline void calc_load(unsigned long ticks) | |||
1264 | } | 857 | } |
1265 | 858 | ||
1266 | /* | 859 | /* |
1267 | * This read-write spinlock protects us from races in SMP while | ||
1268 | * playing with xtime and avenrun. | ||
1269 | */ | ||
1270 | __attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); | ||
1271 | |||
1272 | EXPORT_SYMBOL(xtime_lock); | ||
1273 | |||
1274 | /* | ||
1275 | * This function runs timers and the timer-tq in bottom half context. | 860 | * This function runs timers and the timer-tq in bottom half context. |
1276 | */ | 861 | */ |
1277 | static void run_timer_softirq(struct softirq_action *h) | 862 | static void run_timer_softirq(struct softirq_action *h) |
@@ -1617,6 +1202,13 @@ static int __devinit init_timers_cpu(int cpu) | |||
1617 | cpu_to_node(cpu)); | 1202 | cpu_to_node(cpu)); |
1618 | if (!base) | 1203 | if (!base) |
1619 | return -ENOMEM; | 1204 | return -ENOMEM; |
1205 | |||
1206 | /* Make sure that tvec_base is 2 byte aligned */ | ||
1207 | if (tbase_get_deferrable(base)) { | ||
1208 | WARN_ON(1); | ||
1209 | kfree(base); | ||
1210 | return -ENOMEM; | ||
1211 | } | ||
1620 | memset(base, 0, sizeof(*base)); | 1212 | memset(base, 0, sizeof(*base)); |
1621 | per_cpu(tvec_bases, cpu) = base; | 1213 | per_cpu(tvec_bases, cpu) = base; |
1622 | } else { | 1214 | } else { |
@@ -1656,9 +1248,9 @@ static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head) | |||
1656 | struct timer_list *timer; | 1248 | struct timer_list *timer; |
1657 | 1249 | ||
1658 | while (!list_empty(head)) { | 1250 | while (!list_empty(head)) { |
1659 | timer = list_entry(head->next, struct timer_list, entry); | 1251 | timer = list_first_entry(head, struct timer_list, entry); |
1660 | detach_timer(timer, 0); | 1252 | detach_timer(timer, 0); |
1661 | timer->base = new_base; | 1253 | timer_set_base(timer, new_base); |
1662 | internal_add_timer(new_base, timer); | 1254 | internal_add_timer(new_base, timer); |
1663 | } | 1255 | } |
1664 | } | 1256 | } |
@@ -1701,11 +1293,13 @@ static int __cpuinit timer_cpu_notify(struct notifier_block *self, | |||
1701 | long cpu = (long)hcpu; | 1293 | long cpu = (long)hcpu; |
1702 | switch(action) { | 1294 | switch(action) { |
1703 | case CPU_UP_PREPARE: | 1295 | case CPU_UP_PREPARE: |
1296 | case CPU_UP_PREPARE_FROZEN: | ||
1704 | if (init_timers_cpu(cpu) < 0) | 1297 | if (init_timers_cpu(cpu) < 0) |
1705 | return NOTIFY_BAD; | 1298 | return NOTIFY_BAD; |
1706 | break; | 1299 | break; |
1707 | #ifdef CONFIG_HOTPLUG_CPU | 1300 | #ifdef CONFIG_HOTPLUG_CPU |
1708 | case CPU_DEAD: | 1301 | case CPU_DEAD: |
1302 | case CPU_DEAD_FROZEN: | ||
1709 | migrate_timers(cpu); | 1303 | migrate_timers(cpu); |
1710 | break; | 1304 | break; |
1711 | #endif | 1305 | #endif |
@@ -1905,6 +1499,8 @@ unregister_time_interpolator(struct time_interpolator *ti) | |||
1905 | prev = &curr->next; | 1499 | prev = &curr->next; |
1906 | } | 1500 | } |
1907 | 1501 | ||
1502 | clocksource_resume(); | ||
1503 | |||
1908 | write_seqlock_irqsave(&xtime_lock, flags); | 1504 | write_seqlock_irqsave(&xtime_lock, flags); |
1909 | if (ti == time_interpolator) { | 1505 | if (ti == time_interpolator) { |
1910 | /* we lost the best time-interpolator: */ | 1506 | /* we lost the best time-interpolator: */ |