Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 2 | #ifndef _LINUX_TIME64_H |
| 3 | #define _LINUX_TIME64_H |
| 4 | |
Xunlei Pang | 30f3b3f | 2015-04-09 09:04:40 +0800 | [diff] [blame] | 5 | #include <linux/math64.h> |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 6 | |
| 7 | typedef __s64 time64_t; |
Vegard Nossum | 469e857f3 | 2016-08-12 20:14:09 +0200 | [diff] [blame] | 8 | typedef __u64 timeu64_t; |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 9 | |
Deepa Dinamani | acf8870 | 2018-03-13 21:03:30 -0700 | [diff] [blame] | 10 | /* CONFIG_64BIT_TIME enables new 64 bit time_t syscalls in the compat path |
| 11 | * and 32-bit emulation. |
| 12 | */ |
| 13 | #ifndef CONFIG_64BIT_TIME |
| 14 | #define __kernel_timespec timespec |
Deepa Dinamani | d0dd63a | 2018-06-16 22:11:42 -0700 | [diff] [blame] | 15 | #define __kernel_itimerspec itimerspec |
Deepa Dinamani | acf8870 | 2018-03-13 21:03:30 -0700 | [diff] [blame] | 16 | #endif |
| 17 | |
| 18 | #include <uapi/linux/time.h> |
| 19 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 20 | struct timespec64 { |
| 21 | time64_t tv_sec; /* seconds */ |
| 22 | long tv_nsec; /* nanoseconds */ |
| 23 | }; |
Baolin Wang | 19a46fe | 2015-07-29 19:58:15 +0800 | [diff] [blame] | 24 | |
| 25 | struct itimerspec64 { |
| 26 | struct timespec64 it_interval; |
| 27 | struct timespec64 it_value; |
| 28 | }; |
| 29 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 30 | /* Parameters used to convert the timespec values: */ |
| 31 | #define MSEC_PER_SEC 1000L |
| 32 | #define USEC_PER_MSEC 1000L |
| 33 | #define NSEC_PER_USEC 1000L |
| 34 | #define NSEC_PER_MSEC 1000000L |
| 35 | #define USEC_PER_SEC 1000000L |
| 36 | #define NSEC_PER_SEC 1000000000L |
| 37 | #define FSEC_PER_SEC 1000000000000000LL |
| 38 | |
| 39 | /* Located here for timespec[64]_valid_strict */ |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 40 | #define TIME64_MAX ((s64)~((u64)1 << 63)) |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 41 | #define KTIME_MAX ((s64)~((u64)1 << 63)) |
| 42 | #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) |
| 43 | |
Thomas Gleixner | dc0f37b | 2019-03-23 11:36:19 +0100 | [diff] [blame] | 44 | /* |
| 45 | * Limits for settimeofday(): |
| 46 | * |
| 47 | * To prevent setting the time close to the wraparound point time setting |
| 48 | * is limited so a reasonable uptime can be accomodated. Uptime of 30 years |
| 49 | * should be really sufficient, which means the cutoff is 2232. At that |
| 50 | * point the cutoff is just a small part of the larger problem. |
| 51 | */ |
| 52 | #define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600) |
| 53 | #define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX) |
| 54 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 55 | static inline int timespec64_equal(const struct timespec64 *a, |
| 56 | const struct timespec64 *b) |
| 57 | { |
| 58 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * lhs < rhs: return <0 |
| 63 | * lhs == rhs: return 0 |
| 64 | * lhs > rhs: return >0 |
| 65 | */ |
| 66 | static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs) |
| 67 | { |
| 68 | if (lhs->tv_sec < rhs->tv_sec) |
| 69 | return -1; |
| 70 | if (lhs->tv_sec > rhs->tv_sec) |
| 71 | return 1; |
| 72 | return lhs->tv_nsec - rhs->tv_nsec; |
| 73 | } |
| 74 | |
| 75 | extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec); |
| 76 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 77 | static inline struct timespec64 timespec64_add(struct timespec64 lhs, |
| 78 | struct timespec64 rhs) |
| 79 | { |
| 80 | struct timespec64 ts_delta; |
| 81 | set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, |
| 82 | lhs.tv_nsec + rhs.tv_nsec); |
| 83 | return ts_delta; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * sub = lhs - rhs, in normalized form |
| 88 | */ |
| 89 | static inline struct timespec64 timespec64_sub(struct timespec64 lhs, |
| 90 | struct timespec64 rhs) |
| 91 | { |
| 92 | struct timespec64 ts_delta; |
| 93 | set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, |
| 94 | lhs.tv_nsec - rhs.tv_nsec); |
| 95 | return ts_delta; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Returns true if the timespec64 is norm, false if denorm: |
| 100 | */ |
| 101 | static inline bool timespec64_valid(const struct timespec64 *ts) |
| 102 | { |
| 103 | /* Dates before 1970 are bogus */ |
| 104 | if (ts->tv_sec < 0) |
| 105 | return false; |
| 106 | /* Can't have more nanoseconds then a second */ |
| 107 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 108 | return false; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | static inline bool timespec64_valid_strict(const struct timespec64 *ts) |
| 113 | { |
| 114 | if (!timespec64_valid(ts)) |
| 115 | return false; |
| 116 | /* Disallow values that could overflow ktime_t */ |
| 117 | if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) |
| 118 | return false; |
| 119 | return true; |
| 120 | } |
| 121 | |
Thomas Gleixner | dc0f37b | 2019-03-23 11:36:19 +0100 | [diff] [blame] | 122 | static inline bool timespec64_valid_settod(const struct timespec64 *ts) |
| 123 | { |
| 124 | if (!timespec64_valid(ts)) |
| 125 | return false; |
| 126 | /* Disallow values which cause overflow issues vs. CLOCK_REALTIME */ |
| 127 | if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX) |
| 128 | return false; |
| 129 | return true; |
| 130 | } |
| 131 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 132 | /** |
| 133 | * timespec64_to_ns - Convert timespec64 to nanoseconds |
| 134 | * @ts: pointer to the timespec64 variable to be converted |
| 135 | * |
| 136 | * Returns the scalar nanosecond representation of the timespec64 |
| 137 | * parameter. |
| 138 | */ |
| 139 | static inline s64 timespec64_to_ns(const struct timespec64 *ts) |
| 140 | { |
| 141 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * ns_to_timespec64 - Convert nanoseconds to timespec64 |
| 146 | * @nsec: the nanoseconds value to be converted |
| 147 | * |
| 148 | * Returns the timespec64 representation of the nsec parameter. |
| 149 | */ |
| 150 | extern struct timespec64 ns_to_timespec64(const s64 nsec); |
| 151 | |
| 152 | /** |
| 153 | * timespec64_add_ns - Adds nanoseconds to a timespec64 |
| 154 | * @a: pointer to timespec64 to be incremented |
| 155 | * @ns: unsigned nanoseconds value to be added |
| 156 | * |
| 157 | * This must always be inlined because its used from the x86-64 vdso, |
| 158 | * which cannot call other kernel functions. |
| 159 | */ |
| 160 | static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns) |
| 161 | { |
| 162 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); |
| 163 | a->tv_nsec = ns; |
| 164 | } |
| 165 | |
Deepa Dinamani | 8e4f70e | 2016-05-19 17:09:08 -0700 | [diff] [blame] | 166 | /* |
| 167 | * timespec64_add_safe assumes both values are positive and checks for |
| 168 | * overflow. It will return TIME64_MAX in case of overflow. |
| 169 | */ |
| 170 | extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs, |
| 171 | const struct timespec64 rhs); |
| 172 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 173 | #endif /* _LINUX_TIME64_H */ |