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
23837ced3aSVladimir Oltean /* Parameters used to convert the timespec values: */
24837ced3aSVladimir Oltean #define PSEC_PER_NSEC 1000L
25837ced3aSVladimir Oltean
26361a3bf0SJohn Stultz /* Located here for timespec[64]_valid_strict */
27833f32d7SJohn Stultz #define TIME64_MAX ((s64)~((u64)1 << 63))
28188d20bcSDeepa Dinamani #define TIME64_MIN (-TIME64_MAX - 1)
29188d20bcSDeepa Dinamani
30361a3bf0SJohn Stultz #define KTIME_MAX ((s64)~((u64)1 << 63))
3139ff83f2SLukas Hannen #define KTIME_MIN (-KTIME_MAX - 1)
32361a3bf0SJohn Stultz #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
3339ff83f2SLukas Hannen #define KTIME_SEC_MIN (KTIME_MIN / NSEC_PER_SEC)
34361a3bf0SJohn Stultz
357a8e61f8SThomas Gleixner /*
367a8e61f8SThomas Gleixner * Limits for settimeofday():
377a8e61f8SThomas Gleixner *
387a8e61f8SThomas Gleixner * To prevent setting the time close to the wraparound point time setting
397a8e61f8SThomas Gleixner * is limited so a reasonable uptime can be accomodated. Uptime of 30 years
407a8e61f8SThomas Gleixner * should be really sufficient, which means the cutoff is 2232. At that
417a8e61f8SThomas Gleixner * point the cutoff is just a small part of the larger problem.
427a8e61f8SThomas Gleixner */
437a8e61f8SThomas Gleixner #define TIME_UPTIME_SEC_MAX (30LL * 365 * 24 *3600)
447a8e61f8SThomas Gleixner #define TIME_SETTOD_SEC_MAX (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX)
457a8e61f8SThomas Gleixner
timespec64_equal(const struct timespec64 * a,const struct timespec64 * b)46361a3bf0SJohn Stultz static inline int timespec64_equal(const struct timespec64 *a,
47361a3bf0SJohn Stultz const struct timespec64 *b)
48361a3bf0SJohn Stultz {
49361a3bf0SJohn Stultz return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
50361a3bf0SJohn Stultz }
51361a3bf0SJohn Stultz
52361a3bf0SJohn Stultz /*
53361a3bf0SJohn Stultz * lhs < rhs: return <0
54361a3bf0SJohn Stultz * lhs == rhs: return 0
55361a3bf0SJohn Stultz * lhs > rhs: return >0
56361a3bf0SJohn Stultz */
timespec64_compare(const struct timespec64 * lhs,const struct timespec64 * rhs)57361a3bf0SJohn Stultz static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
58361a3bf0SJohn Stultz {
59361a3bf0SJohn Stultz if (lhs->tv_sec < rhs->tv_sec)
60361a3bf0SJohn Stultz return -1;
61361a3bf0SJohn Stultz if (lhs->tv_sec > rhs->tv_sec)
62361a3bf0SJohn Stultz return 1;
63361a3bf0SJohn Stultz return lhs->tv_nsec - rhs->tv_nsec;
64361a3bf0SJohn Stultz }
65361a3bf0SJohn Stultz
66361a3bf0SJohn Stultz extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
67361a3bf0SJohn Stultz
timespec64_add(struct timespec64 lhs,struct timespec64 rhs)68361a3bf0SJohn Stultz static inline struct timespec64 timespec64_add(struct timespec64 lhs,
69361a3bf0SJohn Stultz struct timespec64 rhs)
70361a3bf0SJohn Stultz {
71361a3bf0SJohn Stultz struct timespec64 ts_delta;
72361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
73361a3bf0SJohn Stultz lhs.tv_nsec + rhs.tv_nsec);
74361a3bf0SJohn Stultz return ts_delta;
75361a3bf0SJohn Stultz }
76361a3bf0SJohn Stultz
77361a3bf0SJohn Stultz /*
78361a3bf0SJohn Stultz * sub = lhs - rhs, in normalized form
79361a3bf0SJohn Stultz */
timespec64_sub(struct timespec64 lhs,struct timespec64 rhs)80361a3bf0SJohn Stultz static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
81361a3bf0SJohn Stultz struct timespec64 rhs)
82361a3bf0SJohn Stultz {
83361a3bf0SJohn Stultz struct timespec64 ts_delta;
84361a3bf0SJohn Stultz set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
85361a3bf0SJohn Stultz lhs.tv_nsec - rhs.tv_nsec);
86361a3bf0SJohn Stultz return ts_delta;
87361a3bf0SJohn Stultz }
88361a3bf0SJohn Stultz
89361a3bf0SJohn Stultz /*
90361a3bf0SJohn Stultz * Returns true if the timespec64 is norm, false if denorm:
91361a3bf0SJohn Stultz */
timespec64_valid(const struct timespec64 * ts)92361a3bf0SJohn Stultz static inline bool timespec64_valid(const struct timespec64 *ts)
93361a3bf0SJohn Stultz {
94361a3bf0SJohn Stultz /* Dates before 1970 are bogus */
95361a3bf0SJohn Stultz if (ts->tv_sec < 0)
96361a3bf0SJohn Stultz return false;
97361a3bf0SJohn Stultz /* Can't have more nanoseconds then a second */
98361a3bf0SJohn Stultz if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
99361a3bf0SJohn Stultz return false;
100361a3bf0SJohn Stultz return true;
101361a3bf0SJohn Stultz }
102361a3bf0SJohn Stultz
timespec64_valid_strict(const struct timespec64 * ts)103361a3bf0SJohn Stultz static inline bool timespec64_valid_strict(const struct timespec64 *ts)
104361a3bf0SJohn Stultz {
105361a3bf0SJohn Stultz if (!timespec64_valid(ts))
106361a3bf0SJohn Stultz return false;
107361a3bf0SJohn Stultz /* Disallow values that could overflow ktime_t */
108361a3bf0SJohn Stultz if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
109361a3bf0SJohn Stultz return false;
110361a3bf0SJohn Stultz return true;
111361a3bf0SJohn Stultz }
112361a3bf0SJohn Stultz
timespec64_valid_settod(const struct timespec64 * ts)1137a8e61f8SThomas Gleixner static inline bool timespec64_valid_settod(const struct timespec64 *ts)
1147a8e61f8SThomas Gleixner {
1157a8e61f8SThomas Gleixner if (!timespec64_valid(ts))
1167a8e61f8SThomas Gleixner return false;
1177a8e61f8SThomas Gleixner /* Disallow values which cause overflow issues vs. CLOCK_REALTIME */
1187a8e61f8SThomas Gleixner if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX)
1197a8e61f8SThomas Gleixner return false;
1207a8e61f8SThomas Gleixner return true;
1217a8e61f8SThomas Gleixner }
1227a8e61f8SThomas Gleixner
123361a3bf0SJohn Stultz /**
124361a3bf0SJohn Stultz * timespec64_to_ns - Convert timespec64 to nanoseconds
125361a3bf0SJohn Stultz * @ts: pointer to the timespec64 variable to be converted
126361a3bf0SJohn Stultz *
127361a3bf0SJohn Stultz * Returns the scalar nanosecond representation of the timespec64
128361a3bf0SJohn Stultz * parameter.
129361a3bf0SJohn Stultz */
timespec64_to_ns(const struct timespec64 * ts)130361a3bf0SJohn Stultz static inline s64 timespec64_to_ns(const struct timespec64 *ts)
131361a3bf0SJohn Stultz {
13239ff83f2SLukas Hannen /* Prevent multiplication overflow / underflow */
13339ff83f2SLukas Hannen if (ts->tv_sec >= KTIME_SEC_MAX)
134cb477557SZeng Tao return KTIME_MAX;
135cb477557SZeng Tao
13639ff83f2SLukas Hannen if (ts->tv_sec <= KTIME_SEC_MIN)
13739ff83f2SLukas Hannen return KTIME_MIN;
13839ff83f2SLukas Hannen
139361a3bf0SJohn Stultz return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
140361a3bf0SJohn Stultz }
141361a3bf0SJohn Stultz
142361a3bf0SJohn Stultz /**
143361a3bf0SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64
144361a3bf0SJohn Stultz * @nsec: the nanoseconds value to be converted
145361a3bf0SJohn Stultz *
146361a3bf0SJohn Stultz * Returns the timespec64 representation of the nsec parameter.
147361a3bf0SJohn Stultz */
148*46dae32fSYoungmin Nam extern struct timespec64 ns_to_timespec64(s64 nsec);
149361a3bf0SJohn Stultz
150361a3bf0SJohn Stultz /**
151361a3bf0SJohn Stultz * timespec64_add_ns - Adds nanoseconds to a timespec64
152361a3bf0SJohn Stultz * @a: pointer to timespec64 to be incremented
153361a3bf0SJohn Stultz * @ns: unsigned nanoseconds value to be added
154361a3bf0SJohn Stultz *
155361a3bf0SJohn Stultz * This must always be inlined because its used from the x86-64 vdso,
156361a3bf0SJohn Stultz * which cannot call other kernel functions.
157361a3bf0SJohn Stultz */
timespec64_add_ns(struct timespec64 * a,u64 ns)158361a3bf0SJohn Stultz static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
159361a3bf0SJohn Stultz {
160361a3bf0SJohn Stultz a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
161361a3bf0SJohn Stultz a->tv_nsec = ns;
162361a3bf0SJohn Stultz }
163361a3bf0SJohn Stultz
1648e4f70e2SDeepa Dinamani /*
1658e4f70e2SDeepa Dinamani * timespec64_add_safe assumes both values are positive and checks for
1668e4f70e2SDeepa Dinamani * overflow. It will return TIME64_MAX in case of overflow.
1678e4f70e2SDeepa Dinamani */
1688e4f70e2SDeepa Dinamani extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
1698e4f70e2SDeepa Dinamani const struct timespec64 rhs);
1708e4f70e2SDeepa Dinamani
171361a3bf0SJohn Stultz #endif /* _LINUX_TIME64_H */
172