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

rtc: Avoid accumulating time drift in suspend/resume

Because the RTC interface is only a second granular interface,
each time we read from the RTC for suspend/resume, we introduce a
half second (on average) of error.

In order to avoid this error accumulating as the system is suspended
over and over, this patch measures the time delta between the RTC
and the system CLOCK_REALTIME.

If the delta is less then 2 seconds from the last suspend, we compensate
by using the previous time delta (keeping it close). If it is larger
then 2 seconds, we assume the clock was set or has been changed, so we
do no correction and update the delta.

Note: If NTP is running, ths could seem to "fight" with the NTP corrected
time, where as if the system time was off by 1 second, and NTP slewed the
value in, a suspend/resume cycle could undo this correction, by trying to
restore the previous offset from the RTC. However, without this patch,
since each read could cause almost a full second worth of error, its
possible to get almost 2 seconds of error just from the suspend/resume
cycle alone, so this about equal to any offset added by the compensation.

Further on systems that suspend/resume frequently, this should keep time
closer then NTP could compensate for if the errors were allowed to
accumulate.

Credits to Arve Hjønnevåg for suggesting this solution.

This patch also improves some of the variable names and adds more clear
comments.

CC: Arve Hjønnevåg <arve@android.com>
CC: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Stultz <john.stultz@linaro.org>

+48 -17
+48 -17
drivers/rtc/class.c
··· 41 41 * system's wall clock; restore it on resume(). 42 42 */ 43 43 44 - static time_t oldtime; 45 - static struct timespec oldts; 44 + static struct timespec old_rtc, old_system, old_delta; 45 + 46 46 47 47 static int rtc_suspend(struct device *dev, pm_message_t mesg) 48 48 { 49 49 struct rtc_device *rtc = to_rtc_device(dev); 50 50 struct rtc_time tm; 51 - 51 + struct timespec delta, delta_delta; 52 52 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) 53 53 return 0; 54 54 55 + /* snapshot the current RTC and system time at suspend*/ 55 56 rtc_read_time(rtc, &tm); 56 - ktime_get_ts(&oldts); 57 - rtc_tm_to_time(&tm, &oldtime); 57 + getnstimeofday(&old_system); 58 + rtc_tm_to_time(&tm, &old_rtc.tv_sec); 59 + 60 + 61 + /* 62 + * To avoid drift caused by repeated suspend/resumes, 63 + * which each can add ~1 second drift error, 64 + * try to compensate so the difference in system time 65 + * and rtc time stays close to constant. 66 + */ 67 + delta = timespec_sub(old_system, old_rtc); 68 + delta_delta = timespec_sub(delta, old_delta); 69 + if (abs(delta_delta.tv_sec) >= 2) { 70 + /* 71 + * if delta_delta is too large, assume time correction 72 + * has occured and set old_delta to the current delta. 73 + */ 74 + old_delta = delta; 75 + } else { 76 + /* Otherwise try to adjust old_system to compensate */ 77 + old_system = timespec_sub(old_system, delta_delta); 78 + } 58 79 59 80 return 0; 60 81 } ··· 84 63 { 85 64 struct rtc_device *rtc = to_rtc_device(dev); 86 65 struct rtc_time tm; 87 - time_t newtime; 88 - struct timespec time; 89 - struct timespec newts; 66 + struct timespec new_system, new_rtc; 67 + struct timespec sleep_time; 90 68 91 69 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) 92 70 return 0; 93 71 94 - ktime_get_ts(&newts); 72 + /* snapshot the current rtc and system time at resume */ 73 + getnstimeofday(&new_system); 95 74 rtc_read_time(rtc, &tm); 96 75 if (rtc_valid_tm(&tm) != 0) { 97 76 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev)); 98 77 return 0; 99 78 } 100 - rtc_tm_to_time(&tm, &newtime); 101 - if (newtime <= oldtime) { 102 - if (newtime < oldtime) 79 + rtc_tm_to_time(&tm, &new_rtc.tv_sec); 80 + new_rtc.tv_nsec = 0; 81 + 82 + if (new_rtc.tv_sec <= old_rtc.tv_sec) { 83 + if (new_rtc.tv_sec < old_rtc.tv_sec) 103 84 pr_debug("%s: time travel!\n", dev_name(&rtc->dev)); 104 85 return 0; 105 86 } 106 - /* calculate the RTC time delta */ 107 - set_normalized_timespec(&time, newtime - oldtime, 0); 108 87 109 - /* subtract kernel time between rtc_suspend to rtc_resume */ 110 - time = timespec_sub(time, timespec_sub(newts, oldts)); 88 + /* calculate the RTC time delta (sleep time)*/ 89 + sleep_time = timespec_sub(new_rtc, old_rtc); 111 90 112 - timekeeping_inject_sleeptime(&time); 91 + /* 92 + * Since these RTC suspend/resume handlers are not called 93 + * at the very end of suspend or the start of resume, 94 + * some run-time may pass on either sides of the sleep time 95 + * so subtract kernel run-time between rtc_suspend to rtc_resume 96 + * to keep things accurate. 97 + */ 98 + sleep_time = timespec_sub(sleep_time, 99 + timespec_sub(new_system, old_system)); 100 + 101 + timekeeping_inject_sleeptime(&sleep_time); 113 102 return 0; 114 103 } 115 104