Skip to content

Commit dbe7aa6

Browse files
darbyShawKAGA-KOKO
authored andcommitted
timekeeping: Provide y2038 safe accessor to the seconds portion of CLOCK_REALTIME
ktime_get_real_seconds() is the replacement function for get_seconds() returning the seconds portion of CLOCK_REALTIME in a time64_t. For 64bit the function is equivivalent to get_seconds(), but for 32bit it protects the readout with the timekeeper sequence count. This is required because 32-bit machines cannot access 64-bit tk->xtime_sec variable atomically. [tglx: Massaged changelog and added docbook comment ] Signed-off-by: Heena Sirwani <heenasirwani@gmail.com> Reviewed-by: Arnd Bergman <arnd@arndb.de> Cc: John Stultz <john.stultz@linaro.org> Cc: opw-kernel@googlegroups.com Link: http://lkml.kernel.org/r/7adcfaa8962b8ad58785d9a2456c3f77d93c0ffb.1414578445.git.heenasirwani@gmail.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 9e3680b commit dbe7aa6

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

include/linux/timekeeping.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct timespec get_monotonic_coarse(void);
2929
extern void getrawmonotonic(struct timespec *ts);
3030
extern void ktime_get_ts64(struct timespec64 *ts);
3131
extern time64_t ktime_get_seconds(void);
32+
extern time64_t ktime_get_real_seconds(void);
3233

3334
extern int __getnstimeofday64(struct timespec64 *tv);
3435
extern void getnstimeofday64(struct timespec64 *tv);

kernel/time/timekeeping.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,36 @@ time64_t ktime_get_seconds(void)
676676
}
677677
EXPORT_SYMBOL_GPL(ktime_get_seconds);
678678

679+
/**
680+
* ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
681+
*
682+
* Returns the wall clock seconds since 1970. This replaces the
683+
* get_seconds() interface which is not y2038 safe on 32bit systems.
684+
*
685+
* For 64bit systems the fast access to tk->xtime_sec is preserved. On
686+
* 32bit systems the access must be protected with the sequence
687+
* counter to provide "atomic" access to the 64bit tk->xtime_sec
688+
* value.
689+
*/
690+
time64_t ktime_get_real_seconds(void)
691+
{
692+
struct timekeeper *tk = &tk_core.timekeeper;
693+
time64_t seconds;
694+
unsigned int seq;
695+
696+
if (IS_ENABLED(CONFIG_64BIT))
697+
return tk->xtime_sec;
698+
699+
do {
700+
seq = read_seqcount_begin(&tk_core.seq);
701+
seconds = tk->xtime_sec;
702+
703+
} while (read_seqcount_retry(&tk_core.seq, seq));
704+
705+
return seconds;
706+
}
707+
EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
708+
679709
#ifdef CONFIG_NTP_PPS
680710

681711
/**

0 commit comments

Comments
 (0)