xref: /OK3568_Linux_fs/kernel/include/linux/time64.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_TIME64_H
3*4882a593Smuzhiyun #define _LINUX_TIME64_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/math64.h>
6*4882a593Smuzhiyun #include <vdso/time64.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun typedef __s64 time64_t;
9*4882a593Smuzhiyun typedef __u64 timeu64_t;
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <uapi/linux/time.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun struct timespec64 {
14*4882a593Smuzhiyun 	time64_t	tv_sec;			/* seconds */
15*4882a593Smuzhiyun 	long		tv_nsec;		/* nanoseconds */
16*4882a593Smuzhiyun };
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct itimerspec64 {
19*4882a593Smuzhiyun 	struct timespec64 it_interval;
20*4882a593Smuzhiyun 	struct timespec64 it_value;
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Located here for timespec[64]_valid_strict */
24*4882a593Smuzhiyun #define TIME64_MAX			((s64)~((u64)1 << 63))
25*4882a593Smuzhiyun #define TIME64_MIN			(-TIME64_MAX - 1)
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define KTIME_MAX			((s64)~((u64)1 << 63))
28*4882a593Smuzhiyun #define KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * Limits for settimeofday():
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * To prevent setting the time close to the wraparound point time setting
34*4882a593Smuzhiyun  * is limited so a reasonable uptime can be accomodated. Uptime of 30 years
35*4882a593Smuzhiyun  * should be really sufficient, which means the cutoff is 2232. At that
36*4882a593Smuzhiyun  * point the cutoff is just a small part of the larger problem.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define TIME_UPTIME_SEC_MAX		(30LL * 365 * 24 *3600)
39*4882a593Smuzhiyun #define TIME_SETTOD_SEC_MAX		(KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX)
40*4882a593Smuzhiyun 
timespec64_equal(const struct timespec64 * a,const struct timespec64 * b)41*4882a593Smuzhiyun static inline int timespec64_equal(const struct timespec64 *a,
42*4882a593Smuzhiyun 				   const struct timespec64 *b)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * lhs < rhs:  return <0
49*4882a593Smuzhiyun  * lhs == rhs: return 0
50*4882a593Smuzhiyun  * lhs > rhs:  return >0
51*4882a593Smuzhiyun  */
timespec64_compare(const struct timespec64 * lhs,const struct timespec64 * rhs)52*4882a593Smuzhiyun static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	if (lhs->tv_sec < rhs->tv_sec)
55*4882a593Smuzhiyun 		return -1;
56*4882a593Smuzhiyun 	if (lhs->tv_sec > rhs->tv_sec)
57*4882a593Smuzhiyun 		return 1;
58*4882a593Smuzhiyun 	return lhs->tv_nsec - rhs->tv_nsec;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
62*4882a593Smuzhiyun 
timespec64_add(struct timespec64 lhs,struct timespec64 rhs)63*4882a593Smuzhiyun static inline struct timespec64 timespec64_add(struct timespec64 lhs,
64*4882a593Smuzhiyun 						struct timespec64 rhs)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct timespec64 ts_delta;
67*4882a593Smuzhiyun 	set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
68*4882a593Smuzhiyun 				lhs.tv_nsec + rhs.tv_nsec);
69*4882a593Smuzhiyun 	return ts_delta;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun  * sub = lhs - rhs, in normalized form
74*4882a593Smuzhiyun  */
timespec64_sub(struct timespec64 lhs,struct timespec64 rhs)75*4882a593Smuzhiyun static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
76*4882a593Smuzhiyun 						struct timespec64 rhs)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct timespec64 ts_delta;
79*4882a593Smuzhiyun 	set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
80*4882a593Smuzhiyun 				lhs.tv_nsec - rhs.tv_nsec);
81*4882a593Smuzhiyun 	return ts_delta;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * Returns true if the timespec64 is norm, false if denorm:
86*4882a593Smuzhiyun  */
timespec64_valid(const struct timespec64 * ts)87*4882a593Smuzhiyun static inline bool timespec64_valid(const struct timespec64 *ts)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	/* Dates before 1970 are bogus */
90*4882a593Smuzhiyun 	if (ts->tv_sec < 0)
91*4882a593Smuzhiyun 		return false;
92*4882a593Smuzhiyun 	/* Can't have more nanoseconds then a second */
93*4882a593Smuzhiyun 	if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
94*4882a593Smuzhiyun 		return false;
95*4882a593Smuzhiyun 	return true;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
timespec64_valid_strict(const struct timespec64 * ts)98*4882a593Smuzhiyun static inline bool timespec64_valid_strict(const struct timespec64 *ts)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	if (!timespec64_valid(ts))
101*4882a593Smuzhiyun 		return false;
102*4882a593Smuzhiyun 	/* Disallow values that could overflow ktime_t */
103*4882a593Smuzhiyun 	if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
104*4882a593Smuzhiyun 		return false;
105*4882a593Smuzhiyun 	return true;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
timespec64_valid_settod(const struct timespec64 * ts)108*4882a593Smuzhiyun static inline bool timespec64_valid_settod(const struct timespec64 *ts)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	if (!timespec64_valid(ts))
111*4882a593Smuzhiyun 		return false;
112*4882a593Smuzhiyun 	/* Disallow values which cause overflow issues vs. CLOCK_REALTIME */
113*4882a593Smuzhiyun 	if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX)
114*4882a593Smuzhiyun 		return false;
115*4882a593Smuzhiyun 	return true;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun  * timespec64_to_ns - Convert timespec64 to nanoseconds
120*4882a593Smuzhiyun  * @ts:		pointer to the timespec64 variable to be converted
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * Returns the scalar nanosecond representation of the timespec64
123*4882a593Smuzhiyun  * parameter.
124*4882a593Smuzhiyun  */
timespec64_to_ns(const struct timespec64 * ts)125*4882a593Smuzhiyun static inline s64 timespec64_to_ns(const struct timespec64 *ts)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	/* Prevent multiplication overflow */
128*4882a593Smuzhiyun 	if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
129*4882a593Smuzhiyun 		return KTIME_MAX;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /**
135*4882a593Smuzhiyun  * ns_to_timespec64 - Convert nanoseconds to timespec64
136*4882a593Smuzhiyun  * @nsec:	the nanoseconds value to be converted
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  * Returns the timespec64 representation of the nsec parameter.
139*4882a593Smuzhiyun  */
140*4882a593Smuzhiyun extern struct timespec64 ns_to_timespec64(const s64 nsec);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun  * timespec64_add_ns - Adds nanoseconds to a timespec64
144*4882a593Smuzhiyun  * @a:		pointer to timespec64 to be incremented
145*4882a593Smuzhiyun  * @ns:		unsigned nanoseconds value to be added
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * This must always be inlined because its used from the x86-64 vdso,
148*4882a593Smuzhiyun  * which cannot call other kernel functions.
149*4882a593Smuzhiyun  */
timespec64_add_ns(struct timespec64 * a,u64 ns)150*4882a593Smuzhiyun static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
153*4882a593Smuzhiyun 	a->tv_nsec = ns;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun  * timespec64_add_safe assumes both values are positive and checks for
158*4882a593Smuzhiyun  * overflow. It will return TIME64_MAX in case of overflow.
159*4882a593Smuzhiyun  */
160*4882a593Smuzhiyun extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
161*4882a593Smuzhiyun 					 const struct timespec64 rhs);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun #endif /* _LINUX_TIME64_H */
164