Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull timer core update from Thomas Gleixner:
- Bug fixes (one for a longstanding dead loop issue)
- Rework of time related vsyscalls
- Alarm timer updates
- Jiffies updates to remove compile time dependencies

* 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
timekeeping: Cast raw_interval to u64 to avoid shift overflow
timers: Fix endless looping between cascade() and internal_add_timer()
time/jiffies: bring back unconditional LATCH definition
time: Convert x86_64 to using new update_vsyscall
time: Only do nanosecond rounding on GENERIC_TIME_VSYSCALL_OLD systems
time: Introduce new GENERIC_TIME_VSYSCALL
time: Convert CONFIG_GENERIC_TIME_VSYSCALL to CONFIG_GENERIC_TIME_VSYSCALL_OLD
time: Move update_vsyscall definitions to timekeeper_internal.h
time: Move timekeeper structure to timekeeper_internal.h for vsyscall changes
jiffies: Remove compile time assumptions about CLOCK_TICK_RATE
jiffies: Kill unused TICK_USEC_TO_NSEC
alarmtimer: Rename alarmtimer_remove to alarmtimer_dequeue
alarmtimer: Remove unused helpers & defines
alarmtimer: Use hrtimer per-alarm instead of per-base
alarmtimer: Implement minimum alarm interval for allowing suspend

+288 -264
+1 -1
arch/ia64/Kconfig
··· 39 39 select ARCH_TASK_STRUCT_ALLOCATOR 40 40 select ARCH_THREAD_INFO_ALLOCATOR 41 41 select ARCH_CLOCKSOURCE_DATA 42 - select GENERIC_TIME_VSYSCALL 42 + select GENERIC_TIME_VSYSCALL_OLD 43 43 default y 44 44 help 45 45 The Itanium Processor Family is Intel's 64-bit successor to
+2 -2
arch/ia64/kernel/time.c
··· 19 19 #include <linux/interrupt.h> 20 20 #include <linux/efi.h> 21 21 #include <linux/timex.h> 22 - #include <linux/clocksource.h> 22 + #include <linux/timekeeper_internal.h> 23 23 #include <linux/platform_device.h> 24 24 25 25 #include <asm/machvec.h> ··· 454 454 { 455 455 } 456 456 457 - void update_vsyscall(struct timespec *wall, struct timespec *wtm, 457 + void update_vsyscall_old(struct timespec *wall, struct timespec *wtm, 458 458 struct clocksource *c, u32 mult) 459 459 { 460 460 write_seqcount_begin(&fsyscall_gtod_data.seq);
+1 -1
arch/powerpc/Kconfig
··· 137 137 select ARCH_HAVE_NMI_SAFE_CMPXCHG 138 138 select GENERIC_SMP_IDLE_THREAD 139 139 select GENERIC_CMOS_UPDATE 140 - select GENERIC_TIME_VSYSCALL 140 + select GENERIC_TIME_VSYSCALL_OLD 141 141 select GENERIC_CLOCKEVENTS 142 142 select GENERIC_STRNCPY_FROM_USER 143 143 select GENERIC_STRNLEN_USER
+2 -2
arch/powerpc/kernel/time.c
··· 73 73 /* powerpc clocksource/clockevent code */ 74 74 75 75 #include <linux/clockchips.h> 76 - #include <linux/clocksource.h> 76 + #include <linux/timekeeper_internal.h> 77 77 78 78 static cycle_t rtc_read(struct clocksource *); 79 79 static struct clocksource clocksource_rtc = { ··· 727 727 return (cycle_t)get_tb(); 728 728 } 729 729 730 - void update_vsyscall(struct timespec *wall_time, struct timespec *wtm, 730 + void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm, 731 731 struct clocksource *clock, u32 mult) 732 732 { 733 733 u64 new_tb_to_xs, new_stamp_xsec;
+1 -1
arch/s390/Kconfig
··· 131 131 select HAVE_UID16 if 32BIT 132 132 select ARCH_WANT_IPC_PARSE_VERSION 133 133 select GENERIC_SMP_IDLE_THREAD 134 - select GENERIC_TIME_VSYSCALL 134 + select GENERIC_TIME_VSYSCALL_OLD 135 135 select GENERIC_CLOCKEVENTS 136 136 select KTIME_SCALAR if 32BIT 137 137 select HAVE_ARCH_SECCOMP_FILTER
+2 -2
arch/s390/kernel/time.c
··· 34 34 #include <linux/profile.h> 35 35 #include <linux/timex.h> 36 36 #include <linux/notifier.h> 37 - #include <linux/clocksource.h> 37 + #include <linux/timekeeper_internal.h> 38 38 #include <linux/clockchips.h> 39 39 #include <linux/gfp.h> 40 40 #include <linux/kprobes.h> ··· 219 219 return &clocksource_tod; 220 220 } 221 221 222 - void update_vsyscall(struct timespec *wall_time, struct timespec *wtm, 222 + void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm, 223 223 struct clocksource *clock, u32 mult) 224 224 { 225 225 if (clock != &clocksource_tod)
+2 -2
arch/x86/include/asm/vgtod.h
··· 17 17 18 18 /* open coded 'struct timespec' */ 19 19 time_t wall_time_sec; 20 - u32 wall_time_nsec; 21 - u32 monotonic_time_nsec; 20 + u64 wall_time_snsec; 21 + u64 monotonic_time_snsec; 22 22 time_t monotonic_time_sec; 23 23 24 24 struct timezone sys_tz;
+3
arch/x86/kernel/setup.c
··· 68 68 #include <linux/percpu.h> 69 69 #include <linux/crash_dump.h> 70 70 #include <linux/tboot.h> 71 + #include <linux/jiffies.h> 71 72 72 73 #include <video/edid.h> 73 74 ··· 1033 1032 mcheck_init(); 1034 1033 1035 1034 arch_init_ideal_nops(); 1035 + 1036 + register_refined_jiffies(CLOCK_TICK_RATE); 1036 1037 } 1037 1038 1038 1039 #ifdef CONFIG_X86_32
+28 -19
arch/x86/kernel/vsyscall_64.c
··· 28 28 #include <linux/jiffies.h> 29 29 #include <linux/sysctl.h> 30 30 #include <linux/topology.h> 31 - #include <linux/clocksource.h> 31 + #include <linux/timekeeper_internal.h> 32 32 #include <linux/getcpu.h> 33 33 #include <linux/cpu.h> 34 34 #include <linux/smp.h> ··· 82 82 vsyscall_gtod_data.sys_tz = sys_tz; 83 83 } 84 84 85 - void update_vsyscall(struct timespec *wall_time, struct timespec *wtm, 86 - struct clocksource *clock, u32 mult) 85 + void update_vsyscall(struct timekeeper *tk) 87 86 { 88 - struct timespec monotonic; 87 + struct vsyscall_gtod_data *vdata = &vsyscall_gtod_data; 89 88 90 - write_seqcount_begin(&vsyscall_gtod_data.seq); 89 + write_seqcount_begin(&vdata->seq); 91 90 92 91 /* copy vsyscall data */ 93 - vsyscall_gtod_data.clock.vclock_mode = clock->archdata.vclock_mode; 94 - vsyscall_gtod_data.clock.cycle_last = clock->cycle_last; 95 - vsyscall_gtod_data.clock.mask = clock->mask; 96 - vsyscall_gtod_data.clock.mult = mult; 97 - vsyscall_gtod_data.clock.shift = clock->shift; 92 + vdata->clock.vclock_mode = tk->clock->archdata.vclock_mode; 93 + vdata->clock.cycle_last = tk->clock->cycle_last; 94 + vdata->clock.mask = tk->clock->mask; 95 + vdata->clock.mult = tk->mult; 96 + vdata->clock.shift = tk->shift; 98 97 99 - vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec; 100 - vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec; 98 + vdata->wall_time_sec = tk->xtime_sec; 99 + vdata->wall_time_snsec = tk->xtime_nsec; 101 100 102 - monotonic = timespec_add(*wall_time, *wtm); 103 - vsyscall_gtod_data.monotonic_time_sec = monotonic.tv_sec; 104 - vsyscall_gtod_data.monotonic_time_nsec = monotonic.tv_nsec; 101 + vdata->monotonic_time_sec = tk->xtime_sec 102 + + tk->wall_to_monotonic.tv_sec; 103 + vdata->monotonic_time_snsec = tk->xtime_nsec 104 + + (tk->wall_to_monotonic.tv_nsec 105 + << tk->shift); 106 + while (vdata->monotonic_time_snsec >= 107 + (((u64)NSEC_PER_SEC) << tk->shift)) { 108 + vdata->monotonic_time_snsec -= 109 + ((u64)NSEC_PER_SEC) << tk->shift; 110 + vdata->monotonic_time_sec++; 111 + } 105 112 106 - vsyscall_gtod_data.wall_time_coarse = __current_kernel_time(); 107 - vsyscall_gtod_data.monotonic_time_coarse = 108 - timespec_add(vsyscall_gtod_data.wall_time_coarse, *wtm); 113 + vdata->wall_time_coarse.tv_sec = tk->xtime_sec; 114 + vdata->wall_time_coarse.tv_nsec = (long)(tk->xtime_nsec >> tk->shift); 109 115 110 - write_seqcount_end(&vsyscall_gtod_data.seq); 116 + vdata->monotonic_time_coarse = timespec_add(vdata->wall_time_coarse, 117 + tk->wall_to_monotonic); 118 + 119 + write_seqcount_end(&vdata->seq); 111 120 } 112 121 113 122 static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
+14 -8
arch/x86/vdso/vclock_gettime.c
··· 80 80 } 81 81 82 82 83 - notrace static inline long vgetns(void) 83 + notrace static inline u64 vgetsns(void) 84 84 { 85 85 long v; 86 86 cycles_t cycles; ··· 91 91 else 92 92 return 0; 93 93 v = (cycles - gtod->clock.cycle_last) & gtod->clock.mask; 94 - return (v * gtod->clock.mult) >> gtod->clock.shift; 94 + return v * gtod->clock.mult; 95 95 } 96 96 97 97 /* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ 98 98 notrace static int __always_inline do_realtime(struct timespec *ts) 99 99 { 100 - unsigned long seq, ns; 100 + unsigned long seq; 101 + u64 ns; 101 102 int mode; 102 103 104 + ts->tv_nsec = 0; 103 105 do { 104 106 seq = read_seqcount_begin(&gtod->seq); 105 107 mode = gtod->clock.vclock_mode; 106 108 ts->tv_sec = gtod->wall_time_sec; 107 - ts->tv_nsec = gtod->wall_time_nsec; 108 - ns = vgetns(); 109 + ns = gtod->wall_time_snsec; 110 + ns += vgetsns(); 111 + ns >>= gtod->clock.shift; 109 112 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); 110 113 111 114 timespec_add_ns(ts, ns); ··· 117 114 118 115 notrace static int do_monotonic(struct timespec *ts) 119 116 { 120 - unsigned long seq, ns; 117 + unsigned long seq; 118 + u64 ns; 121 119 int mode; 122 120 121 + ts->tv_nsec = 0; 123 122 do { 124 123 seq = read_seqcount_begin(&gtod->seq); 125 124 mode = gtod->clock.vclock_mode; 126 125 ts->tv_sec = gtod->monotonic_time_sec; 127 - ts->tv_nsec = gtod->monotonic_time_nsec; 128 - ns = vgetns(); 126 + ns = gtod->monotonic_time_snsec; 127 + ns += vgetsns(); 128 + ns >>= gtod->clock.shift; 129 129 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); 130 130 timespec_add_ns(ts, ns); 131 131
+2 -29
include/linux/alarmtimer.h
··· 21 21 22 22 #define ALARMTIMER_STATE_INACTIVE 0x00 23 23 #define ALARMTIMER_STATE_ENQUEUED 0x01 24 - #define ALARMTIMER_STATE_CALLBACK 0x02 25 24 26 25 /** 27 26 * struct alarm - Alarm timer structure ··· 34 35 */ 35 36 struct alarm { 36 37 struct timerqueue_node node; 38 + struct hrtimer timer; 37 39 enum alarmtimer_restart (*function)(struct alarm *, ktime_t now); 38 40 enum alarmtimer_type type; 39 41 int state; ··· 43 43 44 44 void alarm_init(struct alarm *alarm, enum alarmtimer_type type, 45 45 enum alarmtimer_restart (*function)(struct alarm *, ktime_t)); 46 - void alarm_start(struct alarm *alarm, ktime_t start); 46 + int alarm_start(struct alarm *alarm, ktime_t start); 47 47 int alarm_try_to_cancel(struct alarm *alarm); 48 48 int alarm_cancel(struct alarm *alarm); 49 49 50 50 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval); 51 - 52 - /* 53 - * A alarmtimer is active, when it is enqueued into timerqueue or the 54 - * callback function is running. 55 - */ 56 - static inline int alarmtimer_active(const struct alarm *timer) 57 - { 58 - return timer->state != ALARMTIMER_STATE_INACTIVE; 59 - } 60 - 61 - /* 62 - * Helper function to check, whether the timer is on one of the queues 63 - */ 64 - static inline int alarmtimer_is_queued(struct alarm *timer) 65 - { 66 - return timer->state & ALARMTIMER_STATE_ENQUEUED; 67 - } 68 - 69 - /* 70 - * Helper function to check, whether the timer is running the callback 71 - * function 72 - */ 73 - static inline int alarmtimer_callback_running(struct alarm *timer) 74 - { 75 - return timer->state & ALARMTIMER_STATE_CALLBACK; 76 - } 77 - 78 51 79 52 /* Provide way to access the rtc device being used by alarmtimers */ 80 53 struct rtc_device *alarmtimer_get_rtcdev(void);
-16
include/linux/clocksource.h
··· 319 319 __clocksource_updatefreq_scale(cs, 1000, khz); 320 320 } 321 321 322 - #ifdef CONFIG_GENERIC_TIME_VSYSCALL 323 - extern void 324 - update_vsyscall(struct timespec *ts, struct timespec *wtm, 325 - struct clocksource *c, u32 mult); 326 - extern void update_vsyscall_tz(void); 327 - #else 328 - static inline void 329 - update_vsyscall(struct timespec *ts, struct timespec *wtm, 330 - struct clocksource *c, u32 mult) 331 - { 332 - } 333 - 334 - static inline void update_vsyscall_tz(void) 335 - { 336 - } 337 - #endif 338 322 339 323 extern void timekeeping_notify(struct clocksource *clock); 340 324
+3 -17
include/linux/jiffies.h
··· 51 51 #define SH_DIV(NOM,DEN,LSH) ( (((NOM) / (DEN)) << (LSH)) \ 52 52 + ((((NOM) % (DEN)) << (LSH)) + (DEN) / 2) / (DEN)) 53 53 54 - #ifdef CLOCK_TICK_RATE 55 54 /* LATCH is used in the interval timer and ftape setup. */ 56 - # define LATCH ((CLOCK_TICK_RATE + HZ/2) / HZ) /* For divider */ 55 + #define LATCH ((CLOCK_TICK_RATE + HZ/2) / HZ) /* For divider */ 57 56 58 - /* 59 - * HZ is the requested value. However the CLOCK_TICK_RATE may not allow 60 - * for exactly HZ. So SHIFTED_HZ is high res HZ ("<< 8" is for accuracy) 61 - */ 62 - # define SHIFTED_HZ (SH_DIV(CLOCK_TICK_RATE, LATCH, 8)) 63 - #else 64 - # define SHIFTED_HZ (HZ << 8) 65 - #endif 57 + extern int register_refined_jiffies(long clock_tick_rate); 66 58 67 59 /* TICK_NSEC is the time between ticks in nsec assuming SHIFTED_HZ */ 68 - #define TICK_NSEC (SH_DIV(1000000UL * 1000, SHIFTED_HZ, 8)) 60 + #define TICK_NSEC ((NSEC_PER_SEC+HZ/2)/HZ) 69 61 70 62 /* TICK_USEC is the time between ticks in usec assuming fake USER_HZ */ 71 63 #define TICK_USEC ((1000000UL + USER_HZ/2) / USER_HZ) 72 - 73 - /* 74 - * TICK_USEC_TO_NSEC is the time between ticks in nsec assuming SHIFTED_HZ and 75 - * a value TUSEC for TICK_USEC (can be set bij adjtimex) 76 - */ 77 - #define TICK_USEC_TO_NSEC(TUSEC) (SH_DIV(TUSEC * USER_HZ * 1000, SHIFTED_HZ, 8)) 78 64 79 65 /* some arch's have a small-data section that can be accessed register-relative 80 66 * but that can only take up to, say, 4-byte variables. jiffies being part of
+108
include/linux/timekeeper_internal.h
··· 1 + /* 2 + * You SHOULD NOT be including this unless you're vsyscall 3 + * handling code or timekeeping internal code! 4 + */ 5 + 6 + #ifndef _LINUX_TIMEKEEPER_INTERNAL_H 7 + #define _LINUX_TIMEKEEPER_INTERNAL_H 8 + 9 + #include <linux/clocksource.h> 10 + #include <linux/jiffies.h> 11 + #include <linux/time.h> 12 + 13 + /* Structure holding internal timekeeping values. */ 14 + struct timekeeper { 15 + /* Current clocksource used for timekeeping. */ 16 + struct clocksource *clock; 17 + /* NTP adjusted clock multiplier */ 18 + u32 mult; 19 + /* The shift value of the current clocksource. */ 20 + u32 shift; 21 + /* Number of clock cycles in one NTP interval. */ 22 + cycle_t cycle_interval; 23 + /* Number of clock shifted nano seconds in one NTP interval. */ 24 + u64 xtime_interval; 25 + /* shifted nano seconds left over when rounding cycle_interval */ 26 + s64 xtime_remainder; 27 + /* Raw nano seconds accumulated per NTP interval. */ 28 + u32 raw_interval; 29 + 30 + /* Current CLOCK_REALTIME time in seconds */ 31 + u64 xtime_sec; 32 + /* Clock shifted nano seconds */ 33 + u64 xtime_nsec; 34 + 35 + /* Difference between accumulated time and NTP time in ntp 36 + * shifted nano seconds. */ 37 + s64 ntp_error; 38 + /* Shift conversion between clock shifted nano seconds and 39 + * ntp shifted nano seconds. */ 40 + u32 ntp_error_shift; 41 + 42 + /* 43 + * wall_to_monotonic is what we need to add to xtime (or xtime corrected 44 + * for sub jiffie times) to get to monotonic time. Monotonic is pegged 45 + * at zero at system boot time, so wall_to_monotonic will be negative, 46 + * however, we will ALWAYS keep the tv_nsec part positive so we can use 47 + * the usual normalization. 48 + * 49 + * wall_to_monotonic is moved after resume from suspend for the 50 + * monotonic time not to jump. We need to add total_sleep_time to 51 + * wall_to_monotonic to get the real boot based time offset. 52 + * 53 + * - wall_to_monotonic is no longer the boot time, getboottime must be 54 + * used instead. 55 + */ 56 + struct timespec wall_to_monotonic; 57 + /* Offset clock monotonic -> clock realtime */ 58 + ktime_t offs_real; 59 + /* time spent in suspend */ 60 + struct timespec total_sleep_time; 61 + /* Offset clock monotonic -> clock boottime */ 62 + ktime_t offs_boot; 63 + /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ 64 + struct timespec raw_time; 65 + /* Seqlock for all timekeeper values */ 66 + seqlock_t lock; 67 + }; 68 + 69 + static inline struct timespec tk_xtime(struct timekeeper *tk) 70 + { 71 + struct timespec ts; 72 + 73 + ts.tv_sec = tk->xtime_sec; 74 + ts.tv_nsec = (long)(tk->xtime_nsec >> tk->shift); 75 + return ts; 76 + } 77 + 78 + 79 + #ifdef CONFIG_GENERIC_TIME_VSYSCALL 80 + 81 + extern void update_vsyscall(struct timekeeper *tk); 82 + extern void update_vsyscall_tz(void); 83 + 84 + #elif defined(CONFIG_GENERIC_TIME_VSYSCALL_OLD) 85 + 86 + extern void update_vsyscall_old(struct timespec *ts, struct timespec *wtm, 87 + struct clocksource *c, u32 mult); 88 + extern void update_vsyscall_tz(void); 89 + 90 + static inline void update_vsyscall(struct timekeeper *tk) 91 + { 92 + struct timespec xt; 93 + 94 + xt = tk_xtime(tk); 95 + update_vsyscall_old(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); 96 + } 97 + 98 + #else 99 + 100 + static inline void update_vsyscall(struct timekeeper *tk) 101 + { 102 + } 103 + static inline void update_vsyscall_tz(void) 104 + { 105 + } 106 + #endif 107 + 108 + #endif /* _LINUX_TIMEKEEPER_INTERNAL_H */
+1 -1
kernel/time.c
··· 30 30 #include <linux/export.h> 31 31 #include <linux/timex.h> 32 32 #include <linux/capability.h> 33 - #include <linux/clocksource.h> 33 + #include <linux/timekeeper_internal.h> 34 34 #include <linux/errno.h> 35 35 #include <linux/syscalls.h> 36 36 #include <linux/security.h>
+4
kernel/time/Kconfig
··· 16 16 config GENERIC_TIME_VSYSCALL 17 17 bool 18 18 19 + # Timekeeping vsyscall support 20 + config GENERIC_TIME_VSYSCALL_OLD 21 + bool 22 + 19 23 # ktime_t scalar 64bit nsec representation 20 24 config KTIME_SCALAR 21 25 bool
+43 -75
kernel/time/alarmtimer.c
··· 37 37 static struct alarm_base { 38 38 spinlock_t lock; 39 39 struct timerqueue_head timerqueue; 40 - struct hrtimer timer; 41 40 ktime_t (*gettime)(void); 42 41 clockid_t base_clockid; 43 42 } alarm_bases[ALARM_NUMTYPE]; ··· 44 45 /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */ 45 46 static ktime_t freezer_delta; 46 47 static DEFINE_SPINLOCK(freezer_delta_lock); 48 + 49 + static struct wakeup_source *ws; 47 50 48 51 #ifdef CONFIG_RTC_CLASS 49 52 /* rtc timer and device for setting alarm wakeups at suspend */ ··· 131 130 * @base: pointer to the base where the timer is being run 132 131 * @alarm: pointer to alarm being enqueued. 133 132 * 134 - * Adds alarm to a alarm_base timerqueue and if necessary sets 135 - * an hrtimer to run. 133 + * Adds alarm to a alarm_base timerqueue 136 134 * 137 135 * Must hold base->lock when calling. 138 136 */ 139 137 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm) 140 138 { 139 + if (alarm->state & ALARMTIMER_STATE_ENQUEUED) 140 + timerqueue_del(&base->timerqueue, &alarm->node); 141 + 141 142 timerqueue_add(&base->timerqueue, &alarm->node); 142 143 alarm->state |= ALARMTIMER_STATE_ENQUEUED; 143 - 144 - if (&alarm->node == timerqueue_getnext(&base->timerqueue)) { 145 - hrtimer_try_to_cancel(&base->timer); 146 - hrtimer_start(&base->timer, alarm->node.expires, 147 - HRTIMER_MODE_ABS); 148 - } 149 144 } 150 145 151 146 /** 152 - * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue 147 + * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue 153 148 * @base: pointer to the base where the timer is running 154 149 * @alarm: pointer to alarm being removed 155 150 * 156 - * Removes alarm to a alarm_base timerqueue and if necessary sets 157 - * a new timer to run. 151 + * Removes alarm to a alarm_base timerqueue 158 152 * 159 153 * Must hold base->lock when calling. 160 154 */ 161 - static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm) 155 + static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm) 162 156 { 163 - struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue); 164 - 165 157 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED)) 166 158 return; 167 159 168 160 timerqueue_del(&base->timerqueue, &alarm->node); 169 161 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED; 170 - 171 - if (next == &alarm->node) { 172 - hrtimer_try_to_cancel(&base->timer); 173 - next = timerqueue_getnext(&base->timerqueue); 174 - if (!next) 175 - return; 176 - hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS); 177 - } 178 162 } 179 163 180 164 ··· 174 188 */ 175 189 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) 176 190 { 177 - struct alarm_base *base = container_of(timer, struct alarm_base, timer); 178 - struct timerqueue_node *next; 191 + struct alarm *alarm = container_of(timer, struct alarm, timer); 192 + struct alarm_base *base = &alarm_bases[alarm->type]; 179 193 unsigned long flags; 180 - ktime_t now; 181 194 int ret = HRTIMER_NORESTART; 182 195 int restart = ALARMTIMER_NORESTART; 183 196 184 197 spin_lock_irqsave(&base->lock, flags); 185 - now = base->gettime(); 186 - while ((next = timerqueue_getnext(&base->timerqueue))) { 187 - struct alarm *alarm; 188 - ktime_t expired = next->expires; 198 + alarmtimer_dequeue(base, alarm); 199 + spin_unlock_irqrestore(&base->lock, flags); 189 200 190 - if (expired.tv64 > now.tv64) 191 - break; 201 + if (alarm->function) 202 + restart = alarm->function(alarm, base->gettime()); 192 203 193 - alarm = container_of(next, struct alarm, node); 194 - 195 - timerqueue_del(&base->timerqueue, &alarm->node); 196 - alarm->state &= ~ALARMTIMER_STATE_ENQUEUED; 197 - 198 - alarm->state |= ALARMTIMER_STATE_CALLBACK; 199 - spin_unlock_irqrestore(&base->lock, flags); 200 - if (alarm->function) 201 - restart = alarm->function(alarm, now); 202 - spin_lock_irqsave(&base->lock, flags); 203 - alarm->state &= ~ALARMTIMER_STATE_CALLBACK; 204 - 205 - if (restart != ALARMTIMER_NORESTART) { 206 - timerqueue_add(&base->timerqueue, &alarm->node); 207 - alarm->state |= ALARMTIMER_STATE_ENQUEUED; 208 - } 209 - } 210 - 211 - if (next) { 212 - hrtimer_set_expires(&base->timer, next->expires); 204 + spin_lock_irqsave(&base->lock, flags); 205 + if (restart != ALARMTIMER_NORESTART) { 206 + hrtimer_set_expires(&alarm->timer, alarm->node.expires); 207 + alarmtimer_enqueue(base, alarm); 213 208 ret = HRTIMER_RESTART; 214 209 } 215 210 spin_unlock_irqrestore(&base->lock, flags); ··· 217 250 unsigned long flags; 218 251 struct rtc_device *rtc; 219 252 int i; 253 + int ret; 220 254 221 255 spin_lock_irqsave(&freezer_delta_lock, flags); 222 256 min = freezer_delta; ··· 247 279 if (min.tv64 == 0) 248 280 return 0; 249 281 250 - /* XXX - Should we enforce a minimum sleep time? */ 251 - WARN_ON(min.tv64 < NSEC_PER_SEC); 282 + if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { 283 + __pm_wakeup_event(ws, 2 * MSEC_PER_SEC); 284 + return -EBUSY; 285 + } 252 286 253 287 /* Setup an rtc timer to fire that far in the future */ 254 288 rtc_timer_cancel(rtc, &rtctimer); ··· 258 288 now = rtc_tm_to_ktime(tm); 259 289 now = ktime_add(now, min); 260 290 261 - rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0)); 262 - 263 - return 0; 291 + /* Set alarm, if in the past reject suspend briefly to handle */ 292 + ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0)); 293 + if (ret < 0) 294 + __pm_wakeup_event(ws, MSEC_PER_SEC); 295 + return ret; 264 296 } 265 297 #else 266 298 static int alarmtimer_suspend(struct device *dev) ··· 296 324 enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) 297 325 { 298 326 timerqueue_init(&alarm->node); 327 + hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid, 328 + HRTIMER_MODE_ABS); 329 + alarm->timer.function = alarmtimer_fired; 299 330 alarm->function = function; 300 331 alarm->type = type; 301 332 alarm->state = ALARMTIMER_STATE_INACTIVE; ··· 309 334 * @alarm: ptr to alarm to set 310 335 * @start: time to run the alarm 311 336 */ 312 - void alarm_start(struct alarm *alarm, ktime_t start) 337 + int alarm_start(struct alarm *alarm, ktime_t start) 313 338 { 314 339 struct alarm_base *base = &alarm_bases[alarm->type]; 315 340 unsigned long flags; 341 + int ret; 316 342 317 343 spin_lock_irqsave(&base->lock, flags); 318 - if (alarmtimer_active(alarm)) 319 - alarmtimer_remove(base, alarm); 320 344 alarm->node.expires = start; 321 345 alarmtimer_enqueue(base, alarm); 346 + ret = hrtimer_start(&alarm->timer, alarm->node.expires, 347 + HRTIMER_MODE_ABS); 322 348 spin_unlock_irqrestore(&base->lock, flags); 349 + return ret; 323 350 } 324 351 325 352 /** ··· 335 358 { 336 359 struct alarm_base *base = &alarm_bases[alarm->type]; 337 360 unsigned long flags; 338 - int ret = -1; 361 + int ret; 362 + 339 363 spin_lock_irqsave(&base->lock, flags); 340 - 341 - if (alarmtimer_callback_running(alarm)) 342 - goto out; 343 - 344 - if (alarmtimer_is_queued(alarm)) { 345 - alarmtimer_remove(base, alarm); 346 - ret = 1; 347 - } else 348 - ret = 0; 349 - out: 364 + ret = hrtimer_try_to_cancel(&alarm->timer); 365 + if (ret >= 0) 366 + alarmtimer_dequeue(base, alarm); 350 367 spin_unlock_irqrestore(&base->lock, flags); 351 368 return ret; 352 369 } ··· 773 802 for (i = 0; i < ALARM_NUMTYPE; i++) { 774 803 timerqueue_init_head(&alarm_bases[i].timerqueue); 775 804 spin_lock_init(&alarm_bases[i].lock); 776 - hrtimer_init(&alarm_bases[i].timer, 777 - alarm_bases[i].base_clockid, 778 - HRTIMER_MODE_ABS); 779 - alarm_bases[i].timer.function = alarmtimer_fired; 780 805 } 781 806 782 807 error = alarmtimer_rtc_interface_setup(); ··· 788 821 error = PTR_ERR(pdev); 789 822 goto out_drv; 790 823 } 824 + ws = wakeup_source_register("alarmtimer"); 791 825 return 0; 792 826 793 827 out_drv:
+31 -1
kernel/time/jiffies.c
··· 37 37 * requested HZ value. It is also not recommended 38 38 * for "tick-less" systems. 39 39 */ 40 - #define NSEC_PER_JIFFY ((u32)((((u64)NSEC_PER_SEC)<<8)/SHIFTED_HZ)) 40 + #define NSEC_PER_JIFFY ((NSEC_PER_SEC+HZ/2)/HZ) 41 41 42 42 /* Since jiffies uses a simple NSEC_PER_JIFFY multiplier 43 43 * conversion, the .shift value could be zero. However ··· 94 94 struct clocksource * __init __weak clocksource_default_clock(void) 95 95 { 96 96 return &clocksource_jiffies; 97 + } 98 + 99 + struct clocksource refined_jiffies; 100 + 101 + int register_refined_jiffies(long cycles_per_second) 102 + { 103 + u64 nsec_per_tick, shift_hz; 104 + long cycles_per_tick; 105 + 106 + 107 + 108 + refined_jiffies = clocksource_jiffies; 109 + refined_jiffies.name = "refined-jiffies"; 110 + refined_jiffies.rating++; 111 + 112 + /* Calc cycles per tick */ 113 + cycles_per_tick = (cycles_per_second + HZ/2)/HZ; 114 + /* shift_hz stores hz<<8 for extra accuracy */ 115 + shift_hz = (u64)cycles_per_second << 8; 116 + shift_hz += cycles_per_tick/2; 117 + do_div(shift_hz, cycles_per_tick); 118 + /* Calculate nsec_per_tick using shift_hz */ 119 + nsec_per_tick = (u64)NSEC_PER_SEC << 8; 120 + nsec_per_tick += (u32)shift_hz/2; 121 + do_div(nsec_per_tick, (u32)shift_hz); 122 + 123 + refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT; 124 + 125 + clocksource_register(&refined_jiffies); 126 + return 0; 97 127 }
+34 -83
kernel/time/timekeeping.c
··· 8 8 * 9 9 */ 10 10 11 + #include <linux/timekeeper_internal.h> 11 12 #include <linux/module.h> 12 13 #include <linux/interrupt.h> 13 14 #include <linux/percpu.h> ··· 22 21 #include <linux/tick.h> 23 22 #include <linux/stop_machine.h> 24 23 25 - /* Structure holding internal timekeeping values. */ 26 - struct timekeeper { 27 - /* Current clocksource used for timekeeping. */ 28 - struct clocksource *clock; 29 - /* NTP adjusted clock multiplier */ 30 - u32 mult; 31 - /* The shift value of the current clocksource. */ 32 - u32 shift; 33 - /* Number of clock cycles in one NTP interval. */ 34 - cycle_t cycle_interval; 35 - /* Number of clock shifted nano seconds in one NTP interval. */ 36 - u64 xtime_interval; 37 - /* shifted nano seconds left over when rounding cycle_interval */ 38 - s64 xtime_remainder; 39 - /* Raw nano seconds accumulated per NTP interval. */ 40 - u32 raw_interval; 41 - 42 - /* Current CLOCK_REALTIME time in seconds */ 43 - u64 xtime_sec; 44 - /* Clock shifted nano seconds */ 45 - u64 xtime_nsec; 46 - 47 - /* Difference between accumulated time and NTP time in ntp 48 - * shifted nano seconds. */ 49 - s64 ntp_error; 50 - /* Shift conversion between clock shifted nano seconds and 51 - * ntp shifted nano seconds. */ 52 - u32 ntp_error_shift; 53 - 54 - /* 55 - * wall_to_monotonic is what we need to add to xtime (or xtime corrected 56 - * for sub jiffie times) to get to monotonic time. Monotonic is pegged 57 - * at zero at system boot time, so wall_to_monotonic will be negative, 58 - * however, we will ALWAYS keep the tv_nsec part positive so we can use 59 - * the usual normalization. 60 - * 61 - * wall_to_monotonic is moved after resume from suspend for the 62 - * monotonic time not to jump. We need to add total_sleep_time to 63 - * wall_to_monotonic to get the real boot based time offset. 64 - * 65 - * - wall_to_monotonic is no longer the boot time, getboottime must be 66 - * used instead. 67 - */ 68 - struct timespec wall_to_monotonic; 69 - /* Offset clock monotonic -> clock realtime */ 70 - ktime_t offs_real; 71 - /* time spent in suspend */ 72 - struct timespec total_sleep_time; 73 - /* Offset clock monotonic -> clock boottime */ 74 - ktime_t offs_boot; 75 - /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ 76 - struct timespec raw_time; 77 - /* Seqlock for all timekeeper values */ 78 - seqlock_t lock; 79 - }; 80 24 81 25 static struct timekeeper timekeeper; 82 26 ··· 40 94 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift; 41 95 tk->xtime_sec++; 42 96 } 43 - } 44 - 45 - static struct timespec tk_xtime(struct timekeeper *tk) 46 - { 47 - struct timespec ts; 48 - 49 - ts.tv_sec = tk->xtime_sec; 50 - ts.tv_nsec = (long)(tk->xtime_nsec >> tk->shift); 51 - return ts; 52 97 } 53 98 54 99 static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts) ··· 183 246 /* must hold write on timekeeper.lock */ 184 247 static void timekeeping_update(struct timekeeper *tk, bool clearntp) 185 248 { 186 - struct timespec xt; 187 - 188 249 if (clearntp) { 189 250 tk->ntp_error = 0; 190 251 ntp_clear(); 191 252 } 192 - xt = tk_xtime(tk); 193 - update_vsyscall(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); 253 + update_vsyscall(tk); 194 254 } 195 255 196 256 /** ··· 1047 1113 accumulate_nsecs_to_secs(tk); 1048 1114 1049 1115 /* Accumulate raw time */ 1050 - raw_nsecs = tk->raw_interval << shift; 1116 + raw_nsecs = (u64)tk->raw_interval << shift; 1051 1117 raw_nsecs += tk->raw_time.tv_nsec; 1052 1118 if (raw_nsecs >= NSEC_PER_SEC) { 1053 1119 u64 raw_secs = raw_nsecs; ··· 1064 1130 return offset; 1065 1131 } 1066 1132 1133 + #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD 1134 + static inline void old_vsyscall_fixup(struct timekeeper *tk) 1135 + { 1136 + s64 remainder; 1137 + 1138 + /* 1139 + * Store only full nanoseconds into xtime_nsec after rounding 1140 + * it up and add the remainder to the error difference. 1141 + * XXX - This is necessary to avoid small 1ns inconsistnecies caused 1142 + * by truncating the remainder in vsyscalls. However, it causes 1143 + * additional work to be done in timekeeping_adjust(). Once 1144 + * the vsyscall implementations are converted to use xtime_nsec 1145 + * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD 1146 + * users are removed, this can be killed. 1147 + */ 1148 + remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1); 1149 + tk->xtime_nsec -= remainder; 1150 + tk->xtime_nsec += 1ULL << tk->shift; 1151 + tk->ntp_error += remainder << tk->ntp_error_shift; 1152 + 1153 + } 1154 + #else 1155 + #define old_vsyscall_fixup(tk) 1156 + #endif 1157 + 1158 + 1159 + 1067 1160 /** 1068 1161 * update_wall_time - Uses the current clocksource to increment the wall time 1069 1162 * ··· 1102 1141 cycle_t offset; 1103 1142 int shift = 0, maxshift; 1104 1143 unsigned long flags; 1105 - s64 remainder; 1106 1144 1107 1145 write_seqlock_irqsave(&tk->lock, flags); 1108 1146 ··· 1143 1183 /* correct the clock when NTP error is too big */ 1144 1184 timekeeping_adjust(tk, offset); 1145 1185 1146 - 1147 1186 /* 1148 - * Store only full nanoseconds into xtime_nsec after rounding 1149 - * it up and add the remainder to the error difference. 1150 - * XXX - This is necessary to avoid small 1ns inconsistnecies caused 1151 - * by truncating the remainder in vsyscalls. However, it causes 1152 - * additional work to be done in timekeeping_adjust(). Once 1153 - * the vsyscall implementations are converted to use xtime_nsec 1154 - * (shifted nanoseconds), this can be killed. 1155 - */ 1156 - remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1); 1157 - tk->xtime_nsec -= remainder; 1158 - tk->xtime_nsec += 1ULL << tk->shift; 1159 - tk->ntp_error += remainder << tk->ntp_error_shift; 1187 + * XXX This can be killed once everyone converts 1188 + * to the new update_vsyscall. 1189 + */ 1190 + old_vsyscall_fixup(tk); 1160 1191 1161 1192 /* 1162 1193 * Finally, make sure that after the rounding
+6 -4
kernel/timer.c
··· 63 63 #define TVR_SIZE (1 << TVR_BITS) 64 64 #define TVN_MASK (TVN_SIZE - 1) 65 65 #define TVR_MASK (TVR_SIZE - 1) 66 + #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1)) 66 67 67 68 struct tvec { 68 69 struct list_head vec[TVN_SIZE]; ··· 360 359 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); 361 360 } else { 362 361 int i; 363 - /* If the timeout is larger than 0xffffffff on 64-bit 364 - * architectures then we use the maximum timeout: 362 + /* If the timeout is larger than MAX_TVAL (on 64-bit 363 + * architectures or with CONFIG_BASE_SMALL=1) then we 364 + * use the maximum timeout. 365 365 */ 366 - if (idx > 0xffffffffUL) { 367 - idx = 0xffffffffUL; 366 + if (idx > MAX_TVAL) { 367 + idx = MAX_TVAL; 368 368 expires = idx + base->timer_jiffies; 369 369 } 370 370 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;