Skip to content

Commit 05ddaa4

Browse files
committed
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
2 parents b48362d + 9ca3085 commit 05ddaa4

File tree

7 files changed

+102
-33
lines changed

7 files changed

+102
-33
lines changed

include/linux/jiffies.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,25 @@ static inline unsigned long usecs_to_jiffies(const unsigned int u)
416416
}
417417
}
418418

419-
extern unsigned long timespec_to_jiffies(const struct timespec *value);
420-
extern void jiffies_to_timespec(const unsigned long jiffies,
421-
struct timespec *value);
419+
extern unsigned long timespec64_to_jiffies(const struct timespec64 *value);
420+
extern void jiffies_to_timespec64(const unsigned long jiffies,
421+
struct timespec64 *value);
422+
static inline unsigned long timespec_to_jiffies(const struct timespec *value)
423+
{
424+
struct timespec64 ts = timespec_to_timespec64(*value);
425+
426+
return timespec64_to_jiffies(&ts);
427+
}
428+
429+
static inline void jiffies_to_timespec(const unsigned long jiffies,
430+
struct timespec *value)
431+
{
432+
struct timespec64 ts;
433+
434+
jiffies_to_timespec64(jiffies, &ts);
435+
*value = timespec64_to_timespec(ts);
436+
}
437+
422438
extern unsigned long timeval_to_jiffies(const struct timeval *value);
423439
extern void jiffies_to_timeval(const unsigned long jiffies,
424440
struct timeval *value);

include/linux/time64.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ typedef __s64 time64_t;
1212
*/
1313
#if __BITS_PER_LONG == 64
1414
# define timespec64 timespec
15+
#define itimerspec64 itimerspec
1516
#else
1617
struct timespec64 {
1718
time64_t tv_sec; /* seconds */
1819
long tv_nsec; /* nanoseconds */
1920
};
21+
22+
struct itimerspec64 {
23+
struct timespec64 it_interval;
24+
struct timespec64 it_value;
25+
};
26+
2027
#endif
2128

2229
/* Parameters used to convert the timespec values: */
@@ -45,6 +52,16 @@ static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
4552
return ts;
4653
}
4754

55+
static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
56+
{
57+
return *its64;
58+
}
59+
60+
static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
61+
{
62+
return *its;
63+
}
64+
4865
# define timespec64_equal timespec_equal
4966
# define timespec64_compare timespec_compare
5067
# define set_normalized_timespec64 set_normalized_timespec
@@ -77,6 +94,24 @@ static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
7794
return ret;
7895
}
7996

97+
static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
98+
{
99+
struct itimerspec ret;
100+
101+
ret.it_interval = timespec64_to_timespec(its64->it_interval);
102+
ret.it_value = timespec64_to_timespec(its64->it_value);
103+
return ret;
104+
}
105+
106+
static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
107+
{
108+
struct itimerspec64 ret;
109+
110+
ret.it_interval = timespec_to_timespec64(its->it_interval);
111+
ret.it_value = timespec_to_timespec64(its->it_value);
112+
return ret;
113+
}
114+
80115
static inline int timespec64_equal(const struct timespec64 *a,
81116
const struct timespec64 *b)
82117
{

include/linux/timekeeping.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ extern int do_sys_settimeofday(const struct timespec *tv,
1818
* Kernel time accessors
1919
*/
2020
unsigned long get_seconds(void);
21-
struct timespec current_kernel_time(void);
21+
struct timespec64 current_kernel_time64(void);
2222
/* does not take xtime_lock */
2323
struct timespec __current_kernel_time(void);
2424

25+
static inline struct timespec current_kernel_time(void)
26+
{
27+
struct timespec64 now = current_kernel_time64();
28+
29+
return timespec64_to_timespec(now);
30+
}
31+
2532
/*
2633
* timespec based interfaces
2734
*/

kernel/time/ntp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ int second_overflow(unsigned long secs)
487487
}
488488

489489
#ifdef CONFIG_GENERIC_CMOS_UPDATE
490+
int __weak update_persistent_clock(struct timespec now)
491+
{
492+
return -ENODEV;
493+
}
494+
490495
int __weak update_persistent_clock64(struct timespec64 now64)
491496
{
492497
struct timespec now;

kernel/time/time.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -287,26 +287,20 @@ EXPORT_SYMBOL(jiffies_to_usecs);
287287
* @t: Timespec
288288
* @gran: Granularity in ns.
289289
*
290-
* Truncate a timespec to a granularity. gran must be smaller than a second.
291-
* Always rounds down.
292-
*
293-
* This function should be only used for timestamps returned by
294-
* current_kernel_time() or CURRENT_TIME, not with do_gettimeofday() because
295-
* it doesn't handle the better resolution of the latter.
290+
* Truncate a timespec to a granularity. Always rounds down. gran must
291+
* not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
296292
*/
297293
struct timespec timespec_trunc(struct timespec t, unsigned gran)
298294
{
299-
/*
300-
* Division is pretty slow so avoid it for common cases.
301-
* Currently current_kernel_time() never returns better than
302-
* jiffies resolution. Exploit that.
303-
*/
304-
if (gran <= jiffies_to_usecs(1) * 1000) {
295+
/* Avoid division in the common cases 1 ns and 1 s. */
296+
if (gran == 1) {
305297
/* nothing */
306-
} else if (gran == 1000000000) {
298+
} else if (gran == NSEC_PER_SEC) {
307299
t.tv_nsec = 0;
308-
} else {
300+
} else if (gran > 1 && gran < NSEC_PER_SEC) {
309301
t.tv_nsec -= t.tv_nsec % gran;
302+
} else {
303+
WARN(1, "illegal file time granularity: %u", gran);
310304
}
311305
return t;
312306
}
@@ -546,30 +540,35 @@ EXPORT_SYMBOL(__usecs_to_jiffies);
546540
* value to a scaled second value.
547541
*/
548542
static unsigned long
549-
__timespec_to_jiffies(unsigned long sec, long nsec)
543+
__timespec64_to_jiffies(u64 sec, long nsec)
550544
{
551545
nsec = nsec + TICK_NSEC - 1;
552546

553547
if (sec >= MAX_SEC_IN_JIFFIES){
554548
sec = MAX_SEC_IN_JIFFIES;
555549
nsec = 0;
556550
}
557-
return (((u64)sec * SEC_CONVERSION) +
551+
return ((sec * SEC_CONVERSION) +
558552
(((u64)nsec * NSEC_CONVERSION) >>
559553
(NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
560554

561555
}
562556

563-
unsigned long
564-
timespec_to_jiffies(const struct timespec *value)
557+
static unsigned long
558+
__timespec_to_jiffies(unsigned long sec, long nsec)
565559
{
566-
return __timespec_to_jiffies(value->tv_sec, value->tv_nsec);
560+
return __timespec64_to_jiffies((u64)sec, nsec);
567561
}
568562

569-
EXPORT_SYMBOL(timespec_to_jiffies);
563+
unsigned long
564+
timespec64_to_jiffies(const struct timespec64 *value)
565+
{
566+
return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec);
567+
}
568+
EXPORT_SYMBOL(timespec64_to_jiffies);
570569

571570
void
572-
jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
571+
jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
573572
{
574573
/*
575574
* Convert jiffies to nanoseconds and separate with
@@ -580,7 +579,7 @@ jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
580579
NSEC_PER_SEC, &rem);
581580
value->tv_nsec = rem;
582581
}
583-
EXPORT_SYMBOL(jiffies_to_timespec);
582+
EXPORT_SYMBOL(jiffies_to_timespec64);
584583

585584
/*
586585
* We could use a similar algorithm to timespec_to_jiffies (with a

kernel/time/timekeeping.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ int do_settimeofday64(const struct timespec64 *ts)
911911
struct timekeeper *tk = &tk_core.timekeeper;
912912
struct timespec64 ts_delta, xt;
913913
unsigned long flags;
914+
int ret = 0;
914915

915916
if (!timespec64_valid_strict(ts))
916917
return -EINVAL;
@@ -924,10 +925,15 @@ int do_settimeofday64(const struct timespec64 *ts)
924925
ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
925926
ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
926927

928+
if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
929+
ret = -EINVAL;
930+
goto out;
931+
}
932+
927933
tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
928934

929935
tk_set_xtime(tk, ts);
930-
936+
out:
931937
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
932938

933939
write_seqcount_end(&tk_core.seq);
@@ -936,7 +942,7 @@ int do_settimeofday64(const struct timespec64 *ts)
936942
/* signal hrtimers about time change */
937943
clock_was_set();
938944

939-
return 0;
945+
return ret;
940946
}
941947
EXPORT_SYMBOL(do_settimeofday64);
942948

@@ -965,7 +971,8 @@ int timekeeping_inject_offset(struct timespec *ts)
965971

966972
/* Make sure the proposed value is valid */
967973
tmp = timespec64_add(tk_xtime(tk), ts64);
968-
if (!timespec64_valid_strict(&tmp)) {
974+
if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 ||
975+
!timespec64_valid_strict(&tmp)) {
969976
ret = -EINVAL;
970977
goto error;
971978
}
@@ -1874,7 +1881,7 @@ struct timespec __current_kernel_time(void)
18741881
return timespec64_to_timespec(tk_xtime(tk));
18751882
}
18761883

1877-
struct timespec current_kernel_time(void)
1884+
struct timespec64 current_kernel_time64(void)
18781885
{
18791886
struct timekeeper *tk = &tk_core.timekeeper;
18801887
struct timespec64 now;
@@ -1886,9 +1893,9 @@ struct timespec current_kernel_time(void)
18861893
now = tk_xtime(tk);
18871894
} while (read_seqcount_retry(&tk_core.seq, seq));
18881895

1889-
return timespec64_to_timespec(now);
1896+
return now;
18901897
}
1891-
EXPORT_SYMBOL(current_kernel_time);
1898+
EXPORT_SYMBOL(current_kernel_time64);
18921899

18931900
struct timespec64 get_monotonic_coarse64(void)
18941901
{

kernel/time/timer_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
137137
(unsigned long long) ktime_to_ns(base->offset));
138138
#endif
139139
SEQ_printf(m, "active timers:\n");
140-
print_active_timers(m, base, now);
140+
print_active_timers(m, base, now + ktime_to_ns(base->offset));
141141
}
142142

143143
static void print_cpu(struct seq_file *m, int cpu, u64 now)

0 commit comments

Comments
 (0)