time: Always make sure wall_to_monotonic isn't positive

Two issues were found on an IMX6 development board without an
enabled RTC device(resulting in the boot time and monotonic
time being initialized to 0).

Issue 1:exportfs -a generate:
       "exportfs: /opt/nfs/arm does not support NFS export"
Issue 2:cat /proc/stat:
       "btime 4294967236"

The same issues can be reproduced on x86 after running the
following code:
	int main(void)
	{
	    struct timeval val;
	    int ret;

	    val.tv_sec = 0;
	    val.tv_usec = 0;
	    ret = settimeofday(&val, NULL);
	    return 0;
	}

Two issues are different symptoms of same problem:
The reason is a positive wall_to_monotonic pushes boot time back
to the time before Epoch, and getboottime will return negative
value.

In symptom 1:
          negative boot time cause get_expiry() to overflow time_t
          when input expire time is 2147483647, then cache_flush()
          always clears entries just added in ip_map_parse.
In symptom 2:
          show_stat() uses "unsigned long" to print negative btime
          value returned by getboottime.

This patch fix the problem by prohibiting time from being set to a value which
would cause a negative boot time. As a result one can't set the CLOCK_REALTIME
time prior to (1970 + system uptime).

Change-Id: I31c2093baf48f9cdef49a8ec515d6fb193de5ebc
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Wang YanQing <udknight@gmail.com>
[jstultz: reworded commit message]
Signed-off-by: John Stultz <john.stultz@linaro.org>
Git-commit: e1d7ba8735551ed79c7a0463a042353574b96da3
Git-repo: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
[neeraju@codeaurora.org: resolve merge conflicts]
Signed-off-by: Neeraj Upadhyay <neeraju@codeaurora.org>
This commit is contained in:
Wang YanQing 2015-06-23 18:38:54 +08:00 committed by Gerrit - the friendly Code Review server
parent d31d4288a7
commit 4353589c71
1 changed files with 10 additions and 3 deletions

View File

@ -491,6 +491,7 @@ int do_settimeofday(const struct timespec *tv)
struct timekeeper *tk = &timekeeper;
struct timespec ts_delta, xt;
unsigned long flags;
int ret = 0;
if (!timespec_valid_strict(tv))
return -EINVAL;
@ -504,10 +505,15 @@ int do_settimeofday(const struct timespec *tv)
ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
if (timespec_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
ret = -EINVAL;
goto out;
}
tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
tk_set_xtime(tk, tv);
out:
timekeeping_update(tk, true, true);
write_seqcount_end(&timekeeper_seq);
@ -516,7 +522,7 @@ int do_settimeofday(const struct timespec *tv)
/* signal hrtimers about time change */
clock_was_set();
return 0;
return ret;
}
EXPORT_SYMBOL(do_settimeofday);
@ -543,7 +549,8 @@ int timekeeping_inject_offset(struct timespec *ts)
/* Make sure the proposed value is valid */
tmp = timespec_add(tk_xtime(tk), *ts);
if (!timespec_valid_strict(&tmp)) {
if (timespec_compare(&tk->wall_to_monotonic, ts) > 0 ||
!timespec_valid_strict(&tmp)) {
ret = -EINVAL;
goto error;
}