Procházet zdrojové kódy

Merge branch 'fortglx/4.3/time' of https://git.linaro.org/people/john.stultz/linux into timers/core

- A handful or y2038 related items
- A walltime to monotonic limit
- Small fixes for timespec_trunc() and timer_list output
Thomas Gleixner před 10 roky
rodič
revize
05ddaa4d6d

+ 19 - 3
include/linux/jiffies.h

@@ -416,9 +416,25 @@ static inline unsigned long usecs_to_jiffies(const unsigned int u)
 	}
 	}
 }
 }
 
 
-extern unsigned long timespec_to_jiffies(const struct timespec *value);
-extern void jiffies_to_timespec(const unsigned long jiffies,
-				struct timespec *value);
+extern unsigned long timespec64_to_jiffies(const struct timespec64 *value);
+extern void jiffies_to_timespec64(const unsigned long jiffies,
+				  struct timespec64 *value);
+static inline unsigned long timespec_to_jiffies(const struct timespec *value)
+{
+	struct timespec64 ts = timespec_to_timespec64(*value);
+
+	return timespec64_to_jiffies(&ts);
+}
+
+static inline void jiffies_to_timespec(const unsigned long jiffies,
+				       struct timespec *value)
+{
+	struct timespec64 ts;
+
+	jiffies_to_timespec64(jiffies, &ts);
+	*value = timespec64_to_timespec(ts);
+}
+
 extern unsigned long timeval_to_jiffies(const struct timeval *value);
 extern unsigned long timeval_to_jiffies(const struct timeval *value);
 extern void jiffies_to_timeval(const unsigned long jiffies,
 extern void jiffies_to_timeval(const unsigned long jiffies,
 			       struct timeval *value);
 			       struct timeval *value);

+ 35 - 0
include/linux/time64.h

@@ -12,11 +12,18 @@ typedef __s64 time64_t;
  */
  */
 #if __BITS_PER_LONG == 64
 #if __BITS_PER_LONG == 64
 # define timespec64 timespec
 # define timespec64 timespec
+#define itimerspec64 itimerspec
 #else
 #else
 struct timespec64 {
 struct timespec64 {
 	time64_t	tv_sec;			/* seconds */
 	time64_t	tv_sec;			/* seconds */
 	long		tv_nsec;		/* nanoseconds */
 	long		tv_nsec;		/* nanoseconds */
 };
 };
+
+struct itimerspec64 {
+	struct timespec64 it_interval;
+	struct timespec64 it_value;
+};
+
 #endif
 #endif
 
 
 /* Parameters used to convert the timespec values: */
 /* Parameters used to convert the timespec values: */
@@ -45,6 +52,16 @@ static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
 	return ts;
 	return ts;
 }
 }
 
 
+static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
+{
+	return *its64;
+}
+
+static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
+{
+	return *its;
+}
+
 # define timespec64_equal		timespec_equal
 # define timespec64_equal		timespec_equal
 # define timespec64_compare		timespec_compare
 # define timespec64_compare		timespec_compare
 # define set_normalized_timespec64	set_normalized_timespec
 # define set_normalized_timespec64	set_normalized_timespec
@@ -77,6 +94,24 @@ static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
 	return ret;
 	return ret;
 }
 }
 
 
+static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
+{
+	struct itimerspec ret;
+
+	ret.it_interval = timespec64_to_timespec(its64->it_interval);
+	ret.it_value = timespec64_to_timespec(its64->it_value);
+	return ret;
+}
+
+static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
+{
+	struct itimerspec64 ret;
+
+	ret.it_interval = timespec_to_timespec64(its->it_interval);
+	ret.it_value = timespec_to_timespec64(its->it_value);
+	return ret;
+}
+
 static inline int timespec64_equal(const struct timespec64 *a,
 static inline int timespec64_equal(const struct timespec64 *a,
 				   const struct timespec64 *b)
 				   const struct timespec64 *b)
 {
 {

+ 8 - 1
include/linux/timekeeping.h

@@ -18,10 +18,17 @@ extern int do_sys_settimeofday(const struct timespec *tv,
  * Kernel time accessors
  * Kernel time accessors
  */
  */
 unsigned long get_seconds(void);
 unsigned long get_seconds(void);
-struct timespec current_kernel_time(void);
+struct timespec64 current_kernel_time64(void);
 /* does not take xtime_lock */
 /* does not take xtime_lock */
 struct timespec __current_kernel_time(void);
 struct timespec __current_kernel_time(void);
 
 
+static inline struct timespec current_kernel_time(void)
+{
+	struct timespec64 now = current_kernel_time64();
+
+	return timespec64_to_timespec(now);
+}
+
 /*
 /*
  * timespec based interfaces
  * timespec based interfaces
  */
  */

+ 5 - 0
kernel/time/ntp.c

@@ -487,6 +487,11 @@ out:
 }
 }
 
 
 #ifdef CONFIG_GENERIC_CMOS_UPDATE
 #ifdef CONFIG_GENERIC_CMOS_UPDATE
+int __weak update_persistent_clock(struct timespec now)
+{
+	return -ENODEV;
+}
+
 int __weak update_persistent_clock64(struct timespec64 now64)
 int __weak update_persistent_clock64(struct timespec64 now64)
 {
 {
 	struct timespec now;
 	struct timespec now;

+ 21 - 22
kernel/time/time.c

@@ -287,26 +287,20 @@ EXPORT_SYMBOL(jiffies_to_usecs);
  * @t: Timespec
  * @t: Timespec
  * @gran: Granularity in ns.
  * @gran: Granularity in ns.
  *
  *
- * Truncate a timespec to a granularity. gran must be smaller than a second.
- * Always rounds down.
- *
- * This function should be only used for timestamps returned by
- * current_kernel_time() or CURRENT_TIME, not with do_gettimeofday() because
- * it doesn't handle the better resolution of the latter.
+ * Truncate a timespec to a granularity. Always rounds down. gran must
+ * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
  */
  */
 struct timespec timespec_trunc(struct timespec t, unsigned gran)
 struct timespec timespec_trunc(struct timespec t, unsigned gran)
 {
 {
-	/*
-	 * Division is pretty slow so avoid it for common cases.
-	 * Currently current_kernel_time() never returns better than
-	 * jiffies resolution. Exploit that.
-	 */
-	if (gran <= jiffies_to_usecs(1) * 1000) {
+	/* Avoid division in the common cases 1 ns and 1 s. */
+	if (gran == 1) {
 		/* nothing */
 		/* nothing */
-	} else if (gran == 1000000000) {
+	} else if (gran == NSEC_PER_SEC) {
 		t.tv_nsec = 0;
 		t.tv_nsec = 0;
-	} else {
+	} else if (gran > 1 && gran < NSEC_PER_SEC) {
 		t.tv_nsec -= t.tv_nsec % gran;
 		t.tv_nsec -= t.tv_nsec % gran;
+	} else {
+		WARN(1, "illegal file time granularity: %u", gran);
 	}
 	}
 	return t;
 	return t;
 }
 }
@@ -546,7 +540,7 @@ EXPORT_SYMBOL(__usecs_to_jiffies);
  * value to a scaled second value.
  * value to a scaled second value.
  */
  */
 static unsigned long
 static unsigned long
-__timespec_to_jiffies(unsigned long sec, long nsec)
+__timespec64_to_jiffies(u64 sec, long nsec)
 {
 {
 	nsec = nsec + TICK_NSEC - 1;
 	nsec = nsec + TICK_NSEC - 1;
 
 
@@ -554,22 +548,27 @@ __timespec_to_jiffies(unsigned long sec, long nsec)
 		sec = MAX_SEC_IN_JIFFIES;
 		sec = MAX_SEC_IN_JIFFIES;
 		nsec = 0;
 		nsec = 0;
 	}
 	}
-	return (((u64)sec * SEC_CONVERSION) +
+	return ((sec * SEC_CONVERSION) +
 		(((u64)nsec * NSEC_CONVERSION) >>
 		(((u64)nsec * NSEC_CONVERSION) >>
 		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
 		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
 
 
 }
 }
 
 
-unsigned long
-timespec_to_jiffies(const struct timespec *value)
+static unsigned long
+__timespec_to_jiffies(unsigned long sec, long nsec)
 {
 {
-	return __timespec_to_jiffies(value->tv_sec, value->tv_nsec);
+	return __timespec64_to_jiffies((u64)sec, nsec);
 }
 }
 
 
-EXPORT_SYMBOL(timespec_to_jiffies);
+unsigned long
+timespec64_to_jiffies(const struct timespec64 *value)
+{
+	return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec);
+}
+EXPORT_SYMBOL(timespec64_to_jiffies);
 
 
 void
 void
-jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
+jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
 {
 {
 	/*
 	/*
 	 * Convert jiffies to nanoseconds and separate with
 	 * Convert jiffies to nanoseconds and separate with
@@ -580,7 +579,7 @@ jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
 				    NSEC_PER_SEC, &rem);
 				    NSEC_PER_SEC, &rem);
 	value->tv_nsec = rem;
 	value->tv_nsec = rem;
 }
 }
-EXPORT_SYMBOL(jiffies_to_timespec);
+EXPORT_SYMBOL(jiffies_to_timespec64);
 
 
 /*
 /*
  * We could use a similar algorithm to timespec_to_jiffies (with a
  * We could use a similar algorithm to timespec_to_jiffies (with a

+ 13 - 6
kernel/time/timekeeping.c

@@ -911,6 +911,7 @@ int do_settimeofday64(const struct timespec64 *ts)
 	struct timekeeper *tk = &tk_core.timekeeper;
 	struct timekeeper *tk = &tk_core.timekeeper;
 	struct timespec64 ts_delta, xt;
 	struct timespec64 ts_delta, xt;
 	unsigned long flags;
 	unsigned long flags;
+	int ret = 0;
 
 
 	if (!timespec64_valid_strict(ts))
 	if (!timespec64_valid_strict(ts))
 		return -EINVAL;
 		return -EINVAL;
@@ -924,10 +925,15 @@ int do_settimeofday64(const struct timespec64 *ts)
 	ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
 	ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
 	ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
 	ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
 
 
+	if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
+		ret = -EINVAL;
+		goto out;
+	}
+
 	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
 	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
 
 
 	tk_set_xtime(tk, ts);
 	tk_set_xtime(tk, ts);
-
+out:
 	timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
 	timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
 
 
 	write_seqcount_end(&tk_core.seq);
 	write_seqcount_end(&tk_core.seq);
@@ -936,7 +942,7 @@ int do_settimeofday64(const struct timespec64 *ts)
 	/* signal hrtimers about time change */
 	/* signal hrtimers about time change */
 	clock_was_set();
 	clock_was_set();
 
 
-	return 0;
+	return ret;
 }
 }
 EXPORT_SYMBOL(do_settimeofday64);
 EXPORT_SYMBOL(do_settimeofday64);
 
 
@@ -965,7 +971,8 @@ int timekeeping_inject_offset(struct timespec *ts)
 
 
 	/* Make sure the proposed value is valid */
 	/* Make sure the proposed value is valid */
 	tmp = timespec64_add(tk_xtime(tk),  ts64);
 	tmp = timespec64_add(tk_xtime(tk),  ts64);
-	if (!timespec64_valid_strict(&tmp)) {
+	if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 ||
+	    !timespec64_valid_strict(&tmp)) {
 		ret = -EINVAL;
 		ret = -EINVAL;
 		goto error;
 		goto error;
 	}
 	}
@@ -1874,7 +1881,7 @@ struct timespec __current_kernel_time(void)
 	return timespec64_to_timespec(tk_xtime(tk));
 	return timespec64_to_timespec(tk_xtime(tk));
 }
 }
 
 
-struct timespec current_kernel_time(void)
+struct timespec64 current_kernel_time64(void)
 {
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 	struct timekeeper *tk = &tk_core.timekeeper;
 	struct timespec64 now;
 	struct timespec64 now;
@@ -1886,9 +1893,9 @@ struct timespec current_kernel_time(void)
 		now = tk_xtime(tk);
 		now = tk_xtime(tk);
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 
 
-	return timespec64_to_timespec(now);
+	return now;
 }
 }
-EXPORT_SYMBOL(current_kernel_time);
+EXPORT_SYMBOL(current_kernel_time64);
 
 
 struct timespec64 get_monotonic_coarse64(void)
 struct timespec64 get_monotonic_coarse64(void)
 {
 {

+ 1 - 1
kernel/time/timer_list.c

@@ -137,7 +137,7 @@ print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
 		   (unsigned long long) ktime_to_ns(base->offset));
 		   (unsigned long long) ktime_to_ns(base->offset));
 #endif
 #endif
 	SEQ_printf(m,   "active timers:\n");
 	SEQ_printf(m,   "active timers:\n");
-	print_active_timers(m, base, now);
+	print_active_timers(m, base, now + ktime_to_ns(base->offset));
 }
 }
 
 
 static void print_cpu(struct seq_file *m, int cpu, u64 now)
 static void print_cpu(struct seq_file *m, int cpu, u64 now)