1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2361a3bf0SJohn Stultz #ifndef _LINUX_TIME64_H 3361a3bf0SJohn Stultz #define _LINUX_TIME64_H 4361a3bf0SJohn Stultz 530f3b3f9SXunlei Pang #include <linux/math64.h> 6361a3bf0SJohn Stultz 7361a3bf0SJohn Stultz typedef __s64 time64_t; 8469e857fSVegard Nossum typedef __u64 timeu64_t; 9361a3bf0SJohn Stultz 10acf8870aSDeepa Dinamani #include <uapi/linux/time.h> 11acf8870aSDeepa Dinamani 12361a3bf0SJohn Stultz struct timespec64 { 13361a3bf0SJohn Stultz time64_t tv_sec; /* seconds */ 14361a3bf0SJohn Stultz long tv_nsec; /* nanoseconds */ 15361a3bf0SJohn Stultz }; 1619a46fe5SBaolin Wang 1719a46fe5SBaolin Wang struct itimerspec64 { 1819a46fe5SBaolin Wang struct timespec64 it_interval; 1919a46fe5SBaolin Wang struct timespec64 it_value; 2019a46fe5SBaolin Wang }; 2119a46fe5SBaolin Wang 22361a3bf0SJohn Stultz /* Parameters used to convert the timespec values: */ 23361a3bf0SJohn Stultz #define MSEC_PER_SEC 1000L 24361a3bf0SJohn Stultz #define USEC_PER_MSEC 1000L 25361a3bf0SJohn Stultz #define NSEC_PER_USEC 1000L 26361a3bf0SJohn Stultz #define NSEC_PER_MSEC 1000000L 27361a3bf0SJohn Stultz #define USEC_PER_SEC 1000000L 28361a3bf0SJohn Stultz #define NSEC_PER_SEC 1000000000L 29361a3bf0SJohn Stultz #define FSEC_PER_SEC 1000000000000000LL 30361a3bf0SJohn Stultz 31361a3bf0SJohn Stultz /* Located here for timespec[64]_valid_strict */ 32833f32d7SJohn Stultz #define TIME64_MAX ((s64)~((u64)1 << 63)) 33361a3bf0SJohn Stultz #define KTIME_MAX ((s64)~((u64)1 << 63)) 34361a3bf0SJohn Stultz #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 35361a3bf0SJohn Stultz 36*7a8e61f8SThomas Gleixner /* 37*7a8e61f8SThomas Gleixner * Limits for settimeofday(): 38*7a8e61f8SThomas Gleixner * 39*7a8e61f8SThomas Gleixner * To prevent setting the time close to the wraparound point time setting 40*7a8e61f8SThomas Gleixner * is limited so a reasonable uptime can be accomodated. Uptime of 30 years 41*7a8e61f8SThomas Gleixner * should be really sufficient, which means the cutoff is 2232. At that 42*7a8e61f8SThomas Gleixner * point the cutoff is just a small part of the larger problem. 43*7a8e61f8SThomas Gleixner */ 44*7a8e61f8SThomas Gleixner #define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600) 45*7a8e61f8SThomas Gleixner #define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX) 46*7a8e61f8SThomas Gleixner 47361a3bf0SJohn Stultz static inline int timespec64_equal(const struct timespec64 *a, 48361a3bf0SJohn Stultz const struct timespec64 *b) 49361a3bf0SJohn Stultz { 50361a3bf0SJohn Stultz return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); 51361a3bf0SJohn Stultz } 52361a3bf0SJohn Stultz 53361a3bf0SJohn Stultz /* 54361a3bf0SJohn Stultz * lhs < rhs: return <0 55361a3bf0SJohn Stultz * lhs == rhs: return 0 56361a3bf0SJohn Stultz * lhs > rhs: return >0 57361a3bf0SJohn Stultz */ 58361a3bf0SJohn Stultz static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs) 59361a3bf0SJohn Stultz { 60361a3bf0SJohn Stultz if (lhs->tv_sec < rhs->tv_sec) 61361a3bf0SJohn Stultz return -1; 62361a3bf0SJohn Stultz if (lhs->tv_sec > rhs->tv_sec) 63361a3bf0SJohn Stultz return 1; 64361a3bf0SJohn Stultz return lhs->tv_nsec - rhs->tv_nsec; 65361a3bf0SJohn Stultz } 66361a3bf0SJohn Stultz 67361a3bf0SJohn Stultz extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec); 68361a3bf0SJohn Stultz 69361a3bf0SJohn Stultz static inline struct timespec64 timespec64_add(struct timespec64 lhs, 70361a3bf0SJohn Stultz struct timespec64 rhs) 71361a3bf0SJohn Stultz { 72361a3bf0SJohn Stultz struct timespec64 ts_delta; 73361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, 74361a3bf0SJohn Stultz lhs.tv_nsec + rhs.tv_nsec); 75361a3bf0SJohn Stultz return ts_delta; 76361a3bf0SJohn Stultz } 77361a3bf0SJohn Stultz 78361a3bf0SJohn Stultz /* 79361a3bf0SJohn Stultz * sub = lhs - rhs, in normalized form 80361a3bf0SJohn Stultz */ 81361a3bf0SJohn Stultz static inline struct timespec64 timespec64_sub(struct timespec64 lhs, 82361a3bf0SJohn Stultz struct timespec64 rhs) 83361a3bf0SJohn Stultz { 84361a3bf0SJohn Stultz struct timespec64 ts_delta; 85361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, 86361a3bf0SJohn Stultz lhs.tv_nsec - rhs.tv_nsec); 87361a3bf0SJohn Stultz return ts_delta; 88361a3bf0SJohn Stultz } 89361a3bf0SJohn Stultz 90361a3bf0SJohn Stultz /* 91361a3bf0SJohn Stultz * Returns true if the timespec64 is norm, false if denorm: 92361a3bf0SJohn Stultz */ 93361a3bf0SJohn Stultz static inline bool timespec64_valid(const struct timespec64 *ts) 94361a3bf0SJohn Stultz { 95361a3bf0SJohn Stultz /* Dates before 1970 are bogus */ 96361a3bf0SJohn Stultz if (ts->tv_sec < 0) 97361a3bf0SJohn Stultz return false; 98361a3bf0SJohn Stultz /* Can't have more nanoseconds then a second */ 99361a3bf0SJohn Stultz if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 100361a3bf0SJohn Stultz return false; 101361a3bf0SJohn Stultz return true; 102361a3bf0SJohn Stultz } 103361a3bf0SJohn Stultz 104361a3bf0SJohn Stultz static inline bool timespec64_valid_strict(const struct timespec64 *ts) 105361a3bf0SJohn Stultz { 106361a3bf0SJohn Stultz if (!timespec64_valid(ts)) 107361a3bf0SJohn Stultz return false; 108361a3bf0SJohn Stultz /* Disallow values that could overflow ktime_t */ 109361a3bf0SJohn Stultz if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) 110361a3bf0SJohn Stultz return false; 111361a3bf0SJohn Stultz return true; 112361a3bf0SJohn Stultz } 113361a3bf0SJohn Stultz 114*7a8e61f8SThomas Gleixner static inline bool timespec64_valid_settod(const struct timespec64 *ts) 115*7a8e61f8SThomas Gleixner { 116*7a8e61f8SThomas Gleixner if (!timespec64_valid(ts)) 117*7a8e61f8SThomas Gleixner return false; 118*7a8e61f8SThomas Gleixner /* Disallow values which cause overflow issues vs. CLOCK_REALTIME */ 119*7a8e61f8SThomas Gleixner if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX) 120*7a8e61f8SThomas Gleixner return false; 121*7a8e61f8SThomas Gleixner return true; 122*7a8e61f8SThomas Gleixner } 123*7a8e61f8SThomas Gleixner 124361a3bf0SJohn Stultz /** 125361a3bf0SJohn Stultz * timespec64_to_ns - Convert timespec64 to nanoseconds 126361a3bf0SJohn Stultz * @ts: pointer to the timespec64 variable to be converted 127361a3bf0SJohn Stultz * 128361a3bf0SJohn Stultz * Returns the scalar nanosecond representation of the timespec64 129361a3bf0SJohn Stultz * parameter. 130361a3bf0SJohn Stultz */ 131361a3bf0SJohn Stultz static inline s64 timespec64_to_ns(const struct timespec64 *ts) 132361a3bf0SJohn Stultz { 133361a3bf0SJohn Stultz return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; 134361a3bf0SJohn Stultz } 135361a3bf0SJohn Stultz 136361a3bf0SJohn Stultz /** 137361a3bf0SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64 138361a3bf0SJohn Stultz * @nsec: the nanoseconds value to be converted 139361a3bf0SJohn Stultz * 140361a3bf0SJohn Stultz * Returns the timespec64 representation of the nsec parameter. 141361a3bf0SJohn Stultz */ 142361a3bf0SJohn Stultz extern struct timespec64 ns_to_timespec64(const s64 nsec); 143361a3bf0SJohn Stultz 144361a3bf0SJohn Stultz /** 145361a3bf0SJohn Stultz * timespec64_add_ns - Adds nanoseconds to a timespec64 146361a3bf0SJohn Stultz * @a: pointer to timespec64 to be incremented 147361a3bf0SJohn Stultz * @ns: unsigned nanoseconds value to be added 148361a3bf0SJohn Stultz * 149361a3bf0SJohn Stultz * This must always be inlined because its used from the x86-64 vdso, 150361a3bf0SJohn Stultz * which cannot call other kernel functions. 151361a3bf0SJohn Stultz */ 152361a3bf0SJohn Stultz static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns) 153361a3bf0SJohn Stultz { 154361a3bf0SJohn Stultz a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); 155361a3bf0SJohn Stultz a->tv_nsec = ns; 156361a3bf0SJohn Stultz } 157361a3bf0SJohn Stultz 1588e4f70e2SDeepa Dinamani /* 1598e4f70e2SDeepa Dinamani * timespec64_add_safe assumes both values are positive and checks for 1608e4f70e2SDeepa Dinamani * overflow. It will return TIME64_MAX in case of overflow. 1618e4f70e2SDeepa Dinamani */ 1628e4f70e2SDeepa Dinamani extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs, 1638e4f70e2SDeepa Dinamani const struct timespec64 rhs); 1648e4f70e2SDeepa Dinamani 165361a3bf0SJohn Stultz #endif /* _LINUX_TIME64_H */ 166