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