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 |
| 15 | #endif |
| 16 | |
| 17 | #include <uapi/linux/time.h> |
| 18 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 19 | #if __BITS_PER_LONG == 64 |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 20 | /* this trick allows us to optimize out timespec64_to_timespec */ |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 21 | # define timespec64 timespec |
Baolin Wang | 19a46fe | 2015-07-29 19:58:15 +0800 | [diff] [blame] | 22 | #define itimerspec64 itimerspec |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 23 | #else |
| 24 | struct timespec64 { |
| 25 | time64_t tv_sec; /* seconds */ |
| 26 | long tv_nsec; /* nanoseconds */ |
| 27 | }; |
Baolin Wang | 19a46fe | 2015-07-29 19:58:15 +0800 | [diff] [blame] | 28 | |
| 29 | struct itimerspec64 { |
| 30 | struct timespec64 it_interval; |
| 31 | struct timespec64 it_value; |
| 32 | }; |
| 33 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | /* Parameters used to convert the timespec values: */ |
| 37 | #define MSEC_PER_SEC 1000L |
| 38 | #define USEC_PER_MSEC 1000L |
| 39 | #define NSEC_PER_USEC 1000L |
| 40 | #define NSEC_PER_MSEC 1000000L |
| 41 | #define USEC_PER_SEC 1000000L |
| 42 | #define NSEC_PER_SEC 1000000000L |
| 43 | #define FSEC_PER_SEC 1000000000000000LL |
| 44 | |
| 45 | /* Located here for timespec[64]_valid_strict */ |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 46 | #define TIME64_MAX ((s64)~((u64)1 << 63)) |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 47 | #define KTIME_MAX ((s64)~((u64)1 << 63)) |
| 48 | #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) |
| 49 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 50 | static inline int timespec64_equal(const struct timespec64 *a, |
| 51 | const struct timespec64 *b) |
| 52 | { |
| 53 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * lhs < rhs: return <0 |
| 58 | * lhs == rhs: return 0 |
| 59 | * lhs > rhs: return >0 |
| 60 | */ |
| 61 | static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs) |
| 62 | { |
| 63 | if (lhs->tv_sec < rhs->tv_sec) |
| 64 | return -1; |
| 65 | if (lhs->tv_sec > rhs->tv_sec) |
| 66 | return 1; |
| 67 | return lhs->tv_nsec - rhs->tv_nsec; |
| 68 | } |
| 69 | |
| 70 | extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec); |
| 71 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 72 | static inline struct timespec64 timespec64_add(struct timespec64 lhs, |
| 73 | struct timespec64 rhs) |
| 74 | { |
| 75 | struct timespec64 ts_delta; |
| 76 | set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, |
| 77 | lhs.tv_nsec + rhs.tv_nsec); |
| 78 | return ts_delta; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * sub = lhs - rhs, in normalized form |
| 83 | */ |
| 84 | static inline struct timespec64 timespec64_sub(struct timespec64 lhs, |
| 85 | struct timespec64 rhs) |
| 86 | { |
| 87 | struct timespec64 ts_delta; |
| 88 | set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, |
| 89 | lhs.tv_nsec - rhs.tv_nsec); |
| 90 | return ts_delta; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Returns true if the timespec64 is norm, false if denorm: |
| 95 | */ |
| 96 | static inline bool timespec64_valid(const struct timespec64 *ts) |
| 97 | { |
| 98 | /* Dates before 1970 are bogus */ |
| 99 | if (ts->tv_sec < 0) |
| 100 | return false; |
| 101 | /* Can't have more nanoseconds then a second */ |
| 102 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 103 | return false; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | static inline bool timespec64_valid_strict(const struct timespec64 *ts) |
| 108 | { |
| 109 | if (!timespec64_valid(ts)) |
| 110 | return false; |
| 111 | /* Disallow values that could overflow ktime_t */ |
| 112 | if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) |
| 113 | return false; |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * timespec64_to_ns - Convert timespec64 to nanoseconds |
| 119 | * @ts: pointer to the timespec64 variable to be converted |
| 120 | * |
| 121 | * Returns the scalar nanosecond representation of the timespec64 |
| 122 | * parameter. |
| 123 | */ |
| 124 | static inline s64 timespec64_to_ns(const struct timespec64 *ts) |
| 125 | { |
| 126 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * ns_to_timespec64 - Convert nanoseconds to timespec64 |
| 131 | * @nsec: the nanoseconds value to be converted |
| 132 | * |
| 133 | * Returns the timespec64 representation of the nsec parameter. |
| 134 | */ |
| 135 | extern struct timespec64 ns_to_timespec64(const s64 nsec); |
| 136 | |
| 137 | /** |
| 138 | * timespec64_add_ns - Adds nanoseconds to a timespec64 |
| 139 | * @a: pointer to timespec64 to be incremented |
| 140 | * @ns: unsigned nanoseconds value to be added |
| 141 | * |
| 142 | * This must always be inlined because its used from the x86-64 vdso, |
| 143 | * which cannot call other kernel functions. |
| 144 | */ |
| 145 | static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns) |
| 146 | { |
| 147 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); |
| 148 | a->tv_nsec = ns; |
| 149 | } |
| 150 | |
Deepa Dinamani | 8e4f70e | 2016-05-19 17:09:08 -0700 | [diff] [blame] | 151 | /* |
| 152 | * timespec64_add_safe assumes both values are positive and checks for |
| 153 | * overflow. It will return TIME64_MAX in case of overflow. |
| 154 | */ |
| 155 | extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs, |
| 156 | const struct timespec64 rhs); |
| 157 | |
John Stultz | 361a3bf | 2014-07-16 21:03:58 +0000 | [diff] [blame] | 158 | #endif /* _LINUX_TIME64_H */ |