blob: 0d96887ba4e0bfc95d0523f01aa20117fd8ef3ab [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
John Stultz361a3bf2014-07-16 21:03:58 +00002#ifndef _LINUX_TIME64_H
3#define _LINUX_TIME64_H
4
Xunlei Pang30f3b3f2015-04-09 09:04:40 +08005#include <linux/math64.h>
John Stultz361a3bf2014-07-16 21:03:58 +00006
7typedef __s64 time64_t;
Vegard Nossum469e857f32016-08-12 20:14:09 +02008typedef __u64 timeu64_t;
John Stultz361a3bf2014-07-16 21:03:58 +00009
Deepa Dinamaniacf88702018-03-13 21:03:30 -070010/* 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 Stultz361a3bf2014-07-16 21:03:58 +000019#if __BITS_PER_LONG == 64
Arnd Bergmannabc8f962017-10-19 13:14:48 +020020/* this trick allows us to optimize out timespec64_to_timespec */
John Stultz361a3bf2014-07-16 21:03:58 +000021# define timespec64 timespec
Baolin Wang19a46fe2015-07-29 19:58:15 +080022#define itimerspec64 itimerspec
John Stultz361a3bf2014-07-16 21:03:58 +000023#else
24struct timespec64 {
25 time64_t tv_sec; /* seconds */
26 long tv_nsec; /* nanoseconds */
27};
Baolin Wang19a46fe2015-07-29 19:58:15 +080028
29struct itimerspec64 {
30 struct timespec64 it_interval;
31 struct timespec64 it_value;
32};
33
John Stultz361a3bf2014-07-16 21:03:58 +000034#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 Stultz833f32d2015-06-11 15:54:55 -070046#define TIME64_MAX ((s64)~((u64)1 << 63))
John Stultz361a3bf2014-07-16 21:03:58 +000047#define KTIME_MAX ((s64)~((u64)1 << 63))
48#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
49
John Stultz361a3bf2014-07-16 21:03:58 +000050static 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 */
61static 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
70extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
71
John Stultz361a3bf2014-07-16 21:03:58 +000072static 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 */
84static 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 */
96static 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
107static 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 */
124static 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 */
135extern 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 */
145static __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 Dinamani8e4f70e2016-05-19 17:09:08 -0700151/*
152 * timespec64_add_safe assumes both values are positive and checks for
153 * overflow. It will return TIME64_MAX in case of overflow.
154 */
155extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
156 const struct timespec64 rhs);
157
John Stultz361a3bf2014-07-16 21:03:58 +0000158#endif /* _LINUX_TIME64_H */