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> 6*b72a9c5eSVincenzo 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)) 28361a3bf0SJohn Stultz #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 29361a3bf0SJohn Stultz 307a8e61f8SThomas Gleixner /* 317a8e61f8SThomas Gleixner * Limits for settimeofday(): 327a8e61f8SThomas Gleixner * 337a8e61f8SThomas Gleixner * To prevent setting the time close to the wraparound point time setting 347a8e61f8SThomas Gleixner * is limited so a reasonable uptime can be accomodated. Uptime of 30 years 357a8e61f8SThomas Gleixner * should be really sufficient, which means the cutoff is 2232. At that 367a8e61f8SThomas Gleixner * point the cutoff is just a small part of the larger problem. 377a8e61f8SThomas Gleixner */ 387a8e61f8SThomas Gleixner #define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600) 397a8e61f8SThomas Gleixner #define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX) 407a8e61f8SThomas Gleixner 41361a3bf0SJohn Stultz static inline int timespec64_equal(const struct timespec64 *a, 42361a3bf0SJohn Stultz const struct timespec64 *b) 43361a3bf0SJohn Stultz { 44361a3bf0SJohn Stultz return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); 45361a3bf0SJohn Stultz } 46361a3bf0SJohn Stultz 47361a3bf0SJohn Stultz /* 48361a3bf0SJohn Stultz * lhs < rhs: return <0 49361a3bf0SJohn Stultz * lhs == rhs: return 0 50361a3bf0SJohn Stultz * lhs > rhs: return >0 51361a3bf0SJohn Stultz */ 52361a3bf0SJohn Stultz static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs) 53361a3bf0SJohn Stultz { 54361a3bf0SJohn Stultz if (lhs->tv_sec < rhs->tv_sec) 55361a3bf0SJohn Stultz return -1; 56361a3bf0SJohn Stultz if (lhs->tv_sec > rhs->tv_sec) 57361a3bf0SJohn Stultz return 1; 58361a3bf0SJohn Stultz return lhs->tv_nsec - rhs->tv_nsec; 59361a3bf0SJohn Stultz } 60361a3bf0SJohn Stultz 61361a3bf0SJohn Stultz extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec); 62361a3bf0SJohn Stultz 63361a3bf0SJohn Stultz static inline struct timespec64 timespec64_add(struct timespec64 lhs, 64361a3bf0SJohn Stultz struct timespec64 rhs) 65361a3bf0SJohn Stultz { 66361a3bf0SJohn Stultz struct timespec64 ts_delta; 67361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, 68361a3bf0SJohn Stultz lhs.tv_nsec + rhs.tv_nsec); 69361a3bf0SJohn Stultz return ts_delta; 70361a3bf0SJohn Stultz } 71361a3bf0SJohn Stultz 72361a3bf0SJohn Stultz /* 73361a3bf0SJohn Stultz * sub = lhs - rhs, in normalized form 74361a3bf0SJohn Stultz */ 75361a3bf0SJohn Stultz static inline struct timespec64 timespec64_sub(struct timespec64 lhs, 76361a3bf0SJohn Stultz struct timespec64 rhs) 77361a3bf0SJohn Stultz { 78361a3bf0SJohn Stultz struct timespec64 ts_delta; 79361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, 80361a3bf0SJohn Stultz lhs.tv_nsec - rhs.tv_nsec); 81361a3bf0SJohn Stultz return ts_delta; 82361a3bf0SJohn Stultz } 83361a3bf0SJohn Stultz 84361a3bf0SJohn Stultz /* 85361a3bf0SJohn Stultz * Returns true if the timespec64 is norm, false if denorm: 86361a3bf0SJohn Stultz */ 87361a3bf0SJohn Stultz static inline bool timespec64_valid(const struct timespec64 *ts) 88361a3bf0SJohn Stultz { 89361a3bf0SJohn Stultz /* Dates before 1970 are bogus */ 90361a3bf0SJohn Stultz if (ts->tv_sec < 0) 91361a3bf0SJohn Stultz return false; 92361a3bf0SJohn Stultz /* Can't have more nanoseconds then a second */ 93361a3bf0SJohn Stultz if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 94361a3bf0SJohn Stultz return false; 95361a3bf0SJohn Stultz return true; 96361a3bf0SJohn Stultz } 97361a3bf0SJohn Stultz 98361a3bf0SJohn Stultz static inline bool timespec64_valid_strict(const struct timespec64 *ts) 99361a3bf0SJohn Stultz { 100361a3bf0SJohn Stultz if (!timespec64_valid(ts)) 101361a3bf0SJohn Stultz return false; 102361a3bf0SJohn Stultz /* Disallow values that could overflow ktime_t */ 103361a3bf0SJohn Stultz if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) 104361a3bf0SJohn Stultz return false; 105361a3bf0SJohn Stultz return true; 106361a3bf0SJohn Stultz } 107361a3bf0SJohn Stultz 1087a8e61f8SThomas Gleixner static inline bool timespec64_valid_settod(const struct timespec64 *ts) 1097a8e61f8SThomas Gleixner { 1107a8e61f8SThomas Gleixner if (!timespec64_valid(ts)) 1117a8e61f8SThomas Gleixner return false; 1127a8e61f8SThomas Gleixner /* Disallow values which cause overflow issues vs. CLOCK_REALTIME */ 1137a8e61f8SThomas Gleixner if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX) 1147a8e61f8SThomas Gleixner return false; 1157a8e61f8SThomas Gleixner return true; 1167a8e61f8SThomas Gleixner } 1177a8e61f8SThomas Gleixner 118361a3bf0SJohn Stultz /** 119361a3bf0SJohn Stultz * timespec64_to_ns - Convert timespec64 to nanoseconds 120361a3bf0SJohn Stultz * @ts: pointer to the timespec64 variable to be converted 121361a3bf0SJohn Stultz * 122361a3bf0SJohn Stultz * Returns the scalar nanosecond representation of the timespec64 123361a3bf0SJohn Stultz * parameter. 124361a3bf0SJohn Stultz */ 125361a3bf0SJohn Stultz static inline s64 timespec64_to_ns(const struct timespec64 *ts) 126361a3bf0SJohn Stultz { 127361a3bf0SJohn Stultz return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; 128361a3bf0SJohn Stultz } 129361a3bf0SJohn Stultz 130361a3bf0SJohn Stultz /** 131361a3bf0SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64 132361a3bf0SJohn Stultz * @nsec: the nanoseconds value to be converted 133361a3bf0SJohn Stultz * 134361a3bf0SJohn Stultz * Returns the timespec64 representation of the nsec parameter. 135361a3bf0SJohn Stultz */ 136361a3bf0SJohn Stultz extern struct timespec64 ns_to_timespec64(const s64 nsec); 137361a3bf0SJohn Stultz 138361a3bf0SJohn Stultz /** 139361a3bf0SJohn Stultz * timespec64_add_ns - Adds nanoseconds to a timespec64 140361a3bf0SJohn Stultz * @a: pointer to timespec64 to be incremented 141361a3bf0SJohn Stultz * @ns: unsigned nanoseconds value to be added 142361a3bf0SJohn Stultz * 143361a3bf0SJohn Stultz * This must always be inlined because its used from the x86-64 vdso, 144361a3bf0SJohn Stultz * which cannot call other kernel functions. 145361a3bf0SJohn Stultz */ 146361a3bf0SJohn Stultz static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns) 147361a3bf0SJohn Stultz { 148361a3bf0SJohn Stultz a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); 149361a3bf0SJohn Stultz a->tv_nsec = ns; 150361a3bf0SJohn Stultz } 151361a3bf0SJohn Stultz 1528e4f70e2SDeepa Dinamani /* 1538e4f70e2SDeepa Dinamani * timespec64_add_safe assumes both values are positive and checks for 1548e4f70e2SDeepa Dinamani * overflow. It will return TIME64_MAX in case of overflow. 1558e4f70e2SDeepa Dinamani */ 1568e4f70e2SDeepa Dinamani extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs, 1578e4f70e2SDeepa Dinamani const struct timespec64 rhs); 1588e4f70e2SDeepa Dinamani 159361a3bf0SJohn Stultz #endif /* _LINUX_TIME64_H */ 160