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> 6b72a9c5eSVincenzo Frascino #include <vdso/time64.h> 7361a3bf0SJohn Stultz 8361a3bf0SJohn Stultz typedef __s64 time64_t; 9469e857fSVegard Nossum typedef __u64 timeu64_t; 10361a3bf0SJohn Stultz 11acf8870aSDeepa Dinamani #include <uapi/linux/time.h> 12acf8870aSDeepa Dinamani 13361a3bf0SJohn Stultz struct timespec64 { 14361a3bf0SJohn Stultz time64_t tv_sec; /* seconds */ 15361a3bf0SJohn Stultz long tv_nsec; /* nanoseconds */ 16361a3bf0SJohn Stultz }; 1719a46fe5SBaolin Wang 1819a46fe5SBaolin Wang struct itimerspec64 { 1919a46fe5SBaolin Wang struct timespec64 it_interval; 2019a46fe5SBaolin Wang struct timespec64 it_value; 2119a46fe5SBaolin Wang }; 2219a46fe5SBaolin Wang 23361a3bf0SJohn Stultz /* Located here for timespec[64]_valid_strict */ 24833f32d7SJohn Stultz #define TIME64_MAX ((s64)~((u64)1 << 63)) 25188d20bcSDeepa Dinamani #define TIME64_MIN (-TIME64_MAX - 1) 26188d20bcSDeepa Dinamani 27361a3bf0SJohn Stultz #define KTIME_MAX ((s64)~((u64)1 << 63)) 28*39ff83f2SLukas Hannen #define KTIME_MIN (-KTIME_MAX - 1) 29361a3bf0SJohn Stultz #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 30*39ff83f2SLukas Hannen #define KTIME_SEC_MIN (KTIME_MIN / NSEC_PER_SEC) 31361a3bf0SJohn Stultz 327a8e61f8SThomas Gleixner /* 337a8e61f8SThomas Gleixner * Limits for settimeofday(): 347a8e61f8SThomas Gleixner * 357a8e61f8SThomas Gleixner * To prevent setting the time close to the wraparound point time setting 367a8e61f8SThomas Gleixner * is limited so a reasonable uptime can be accomodated. Uptime of 30 years 377a8e61f8SThomas Gleixner * should be really sufficient, which means the cutoff is 2232. At that 387a8e61f8SThomas Gleixner * point the cutoff is just a small part of the larger problem. 397a8e61f8SThomas Gleixner */ 407a8e61f8SThomas Gleixner #define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600) 417a8e61f8SThomas Gleixner #define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX) 427a8e61f8SThomas Gleixner 43361a3bf0SJohn Stultz static inline int timespec64_equal(const struct timespec64 *a, 44361a3bf0SJohn Stultz const struct timespec64 *b) 45361a3bf0SJohn Stultz { 46361a3bf0SJohn Stultz return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); 47361a3bf0SJohn Stultz } 48361a3bf0SJohn Stultz 49361a3bf0SJohn Stultz /* 50361a3bf0SJohn Stultz * lhs < rhs: return <0 51361a3bf0SJohn Stultz * lhs == rhs: return 0 52361a3bf0SJohn Stultz * lhs > rhs: return >0 53361a3bf0SJohn Stultz */ 54361a3bf0SJohn Stultz static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs) 55361a3bf0SJohn Stultz { 56361a3bf0SJohn Stultz if (lhs->tv_sec < rhs->tv_sec) 57361a3bf0SJohn Stultz return -1; 58361a3bf0SJohn Stultz if (lhs->tv_sec > rhs->tv_sec) 59361a3bf0SJohn Stultz return 1; 60361a3bf0SJohn Stultz return lhs->tv_nsec - rhs->tv_nsec; 61361a3bf0SJohn Stultz } 62361a3bf0SJohn Stultz 63361a3bf0SJohn Stultz extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec); 64361a3bf0SJohn Stultz 65361a3bf0SJohn Stultz static inline struct timespec64 timespec64_add(struct timespec64 lhs, 66361a3bf0SJohn Stultz struct timespec64 rhs) 67361a3bf0SJohn Stultz { 68361a3bf0SJohn Stultz struct timespec64 ts_delta; 69361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, 70361a3bf0SJohn Stultz lhs.tv_nsec + rhs.tv_nsec); 71361a3bf0SJohn Stultz return ts_delta; 72361a3bf0SJohn Stultz } 73361a3bf0SJohn Stultz 74361a3bf0SJohn Stultz /* 75361a3bf0SJohn Stultz * sub = lhs - rhs, in normalized form 76361a3bf0SJohn Stultz */ 77361a3bf0SJohn Stultz static inline struct timespec64 timespec64_sub(struct timespec64 lhs, 78361a3bf0SJohn Stultz struct timespec64 rhs) 79361a3bf0SJohn Stultz { 80361a3bf0SJohn Stultz struct timespec64 ts_delta; 81361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, 82361a3bf0SJohn Stultz lhs.tv_nsec - rhs.tv_nsec); 83361a3bf0SJohn Stultz return ts_delta; 84361a3bf0SJohn Stultz } 85361a3bf0SJohn Stultz 86361a3bf0SJohn Stultz /* 87361a3bf0SJohn Stultz * Returns true if the timespec64 is norm, false if denorm: 88361a3bf0SJohn Stultz */ 89361a3bf0SJohn Stultz static inline bool timespec64_valid(const struct timespec64 *ts) 90361a3bf0SJohn Stultz { 91361a3bf0SJohn Stultz /* Dates before 1970 are bogus */ 92361a3bf0SJohn Stultz if (ts->tv_sec < 0) 93361a3bf0SJohn Stultz return false; 94361a3bf0SJohn Stultz /* Can't have more nanoseconds then a second */ 95361a3bf0SJohn Stultz if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 96361a3bf0SJohn Stultz return false; 97361a3bf0SJohn Stultz return true; 98361a3bf0SJohn Stultz } 99361a3bf0SJohn Stultz 100361a3bf0SJohn Stultz static inline bool timespec64_valid_strict(const struct timespec64 *ts) 101361a3bf0SJohn Stultz { 102361a3bf0SJohn Stultz if (!timespec64_valid(ts)) 103361a3bf0SJohn Stultz return false; 104361a3bf0SJohn Stultz /* Disallow values that could overflow ktime_t */ 105361a3bf0SJohn Stultz if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) 106361a3bf0SJohn Stultz return false; 107361a3bf0SJohn Stultz return true; 108361a3bf0SJohn Stultz } 109361a3bf0SJohn Stultz 1107a8e61f8SThomas Gleixner static inline bool timespec64_valid_settod(const struct timespec64 *ts) 1117a8e61f8SThomas Gleixner { 1127a8e61f8SThomas Gleixner if (!timespec64_valid(ts)) 1137a8e61f8SThomas Gleixner return false; 1147a8e61f8SThomas Gleixner /* Disallow values which cause overflow issues vs. CLOCK_REALTIME */ 1157a8e61f8SThomas Gleixner if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX) 1167a8e61f8SThomas Gleixner return false; 1177a8e61f8SThomas Gleixner return true; 1187a8e61f8SThomas Gleixner } 1197a8e61f8SThomas Gleixner 120361a3bf0SJohn Stultz /** 121361a3bf0SJohn Stultz * timespec64_to_ns - Convert timespec64 to nanoseconds 122361a3bf0SJohn Stultz * @ts: pointer to the timespec64 variable to be converted 123361a3bf0SJohn Stultz * 124361a3bf0SJohn Stultz * Returns the scalar nanosecond representation of the timespec64 125361a3bf0SJohn Stultz * parameter. 126361a3bf0SJohn Stultz */ 127361a3bf0SJohn Stultz static inline s64 timespec64_to_ns(const struct timespec64 *ts) 128361a3bf0SJohn Stultz { 129*39ff83f2SLukas Hannen /* Prevent multiplication overflow / underflow */ 130*39ff83f2SLukas Hannen if (ts->tv_sec >= KTIME_SEC_MAX) 131cb477557SZeng Tao return KTIME_MAX; 132cb477557SZeng Tao 133*39ff83f2SLukas Hannen if (ts->tv_sec <= KTIME_SEC_MIN) 134*39ff83f2SLukas Hannen return KTIME_MIN; 135*39ff83f2SLukas Hannen 136361a3bf0SJohn Stultz return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; 137361a3bf0SJohn Stultz } 138361a3bf0SJohn Stultz 139361a3bf0SJohn Stultz /** 140361a3bf0SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64 141361a3bf0SJohn Stultz * @nsec: the nanoseconds value to be converted 142361a3bf0SJohn Stultz * 143361a3bf0SJohn Stultz * Returns the timespec64 representation of the nsec parameter. 144361a3bf0SJohn Stultz */ 145361a3bf0SJohn Stultz extern struct timespec64 ns_to_timespec64(const s64 nsec); 146361a3bf0SJohn Stultz 147361a3bf0SJohn Stultz /** 148361a3bf0SJohn Stultz * timespec64_add_ns - Adds nanoseconds to a timespec64 149361a3bf0SJohn Stultz * @a: pointer to timespec64 to be incremented 150361a3bf0SJohn Stultz * @ns: unsigned nanoseconds value to be added 151361a3bf0SJohn Stultz * 152361a3bf0SJohn Stultz * This must always be inlined because its used from the x86-64 vdso, 153361a3bf0SJohn Stultz * which cannot call other kernel functions. 154361a3bf0SJohn Stultz */ 155361a3bf0SJohn Stultz static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns) 156361a3bf0SJohn Stultz { 157361a3bf0SJohn Stultz a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); 158361a3bf0SJohn Stultz a->tv_nsec = ns; 159361a3bf0SJohn Stultz } 160361a3bf0SJohn Stultz 1618e4f70e2SDeepa Dinamani /* 1628e4f70e2SDeepa Dinamani * timespec64_add_safe assumes both values are positive and checks for 1638e4f70e2SDeepa Dinamani * overflow. It will return TIME64_MAX in case of overflow. 1648e4f70e2SDeepa Dinamani */ 1658e4f70e2SDeepa Dinamani extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs, 1668e4f70e2SDeepa Dinamani const struct timespec64 rhs); 1678e4f70e2SDeepa Dinamani 168361a3bf0SJohn Stultz #endif /* _LINUX_TIME64_H */ 169