1 /* SPDX-License-Identifier: GPL-2.0 */
5 # include <linux/cache.h>
6 # include <linux/seqlock.h>
7 # include <linux/math64.h>
8 # include <linux/time64.h>
10 extern struct timezone sys_tz
;
12 int get_timespec64(struct timespec64
*ts
,
13 const struct timespec __user
*uts
);
14 int put_timespec64(const struct timespec64
*ts
,
15 struct timespec __user
*uts
);
16 int get_itimerspec64(struct itimerspec64
*it
,
17 const struct itimerspec __user
*uit
);
18 int put_itimerspec64(const struct itimerspec64
*it
,
19 struct itimerspec __user
*uit
);
21 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
23 static inline int timespec_equal(const struct timespec
*a
,
24 const struct timespec
*b
)
26 return (a
->tv_sec
== b
->tv_sec
) && (a
->tv_nsec
== b
->tv_nsec
);
30 * lhs < rhs: return <0
31 * lhs == rhs: return 0
32 * lhs > rhs: return >0
34 static inline int timespec_compare(const struct timespec
*lhs
, const struct timespec
*rhs
)
36 if (lhs
->tv_sec
< rhs
->tv_sec
)
38 if (lhs
->tv_sec
> rhs
->tv_sec
)
40 return lhs
->tv_nsec
- rhs
->tv_nsec
;
43 static inline int timeval_compare(const struct timeval
*lhs
, const struct timeval
*rhs
)
45 if (lhs
->tv_sec
< rhs
->tv_sec
)
47 if (lhs
->tv_sec
> rhs
->tv_sec
)
49 return lhs
->tv_usec
- rhs
->tv_usec
;
52 extern time64_t
mktime64(const unsigned int year
, const unsigned int mon
,
53 const unsigned int day
, const unsigned int hour
,
54 const unsigned int min
, const unsigned int sec
);
57 * Deprecated. Use mktime64().
59 static inline unsigned long mktime(const unsigned int year
,
60 const unsigned int mon
, const unsigned int day
,
61 const unsigned int hour
, const unsigned int min
,
62 const unsigned int sec
)
64 return mktime64(year
, mon
, day
, hour
, min
, sec
);
67 extern void set_normalized_timespec(struct timespec
*ts
, time_t sec
, s64 nsec
);
70 * timespec_add_safe assumes both values are positive and checks
71 * for overflow. It will return TIME_T_MAX if the reutrn would be
72 * smaller then either of the arguments.
74 extern struct timespec
timespec_add_safe(const struct timespec lhs
,
75 const struct timespec rhs
);
78 static inline struct timespec
timespec_add(struct timespec lhs
,
81 struct timespec ts_delta
;
82 set_normalized_timespec(&ts_delta
, lhs
.tv_sec
+ rhs
.tv_sec
,
83 lhs
.tv_nsec
+ rhs
.tv_nsec
);
88 * sub = lhs - rhs, in normalized form
90 static inline struct timespec
timespec_sub(struct timespec lhs
,
93 struct timespec ts_delta
;
94 set_normalized_timespec(&ts_delta
, lhs
.tv_sec
- rhs
.tv_sec
,
95 lhs
.tv_nsec
- rhs
.tv_nsec
);
100 * Returns true if the timespec is norm, false if denorm:
102 static inline bool timespec_valid(const struct timespec
*ts
)
104 /* Dates before 1970 are bogus */
107 /* Can't have more nanoseconds then a second */
108 if ((unsigned long)ts
->tv_nsec
>= NSEC_PER_SEC
)
113 static inline bool timespec_valid_strict(const struct timespec
*ts
)
115 if (!timespec_valid(ts
))
117 /* Disallow values that could overflow ktime_t */
118 if ((unsigned long long)ts
->tv_sec
>= KTIME_SEC_MAX
)
123 static inline bool timeval_valid(const struct timeval
*tv
)
125 /* Dates before 1970 are bogus */
129 /* Can't have more microseconds then a second */
130 if (tv
->tv_usec
< 0 || tv
->tv_usec
>= USEC_PER_SEC
)
136 extern struct timespec
timespec_trunc(struct timespec t
, unsigned gran
);
139 * Validates if a timespec/timeval used to inject a time offset is valid.
140 * Offsets can be postive or negative. The value of the timeval/timespec
141 * is the sum of its fields, but *NOTE*: the field tv_usec/tv_nsec must
142 * always be non-negative.
144 static inline bool timeval_inject_offset_valid(const struct timeval
*tv
)
146 /* We don't check the tv_sec as it can be positive or negative */
148 /* Can't have more microseconds then a second */
149 if (tv
->tv_usec
< 0 || tv
->tv_usec
>= USEC_PER_SEC
)
154 static inline bool timespec_inject_offset_valid(const struct timespec
*ts
)
156 /* We don't check the tv_sec as it can be positive or negative */
158 /* Can't have more nanoseconds then a second */
159 if (ts
->tv_nsec
< 0 || ts
->tv_nsec
>= NSEC_PER_SEC
)
164 /* Some architectures do not supply their own clocksource.
165 * This is mainly the case in architectures that get their
166 * inter-tick times by reading the counter on their interval
167 * timer. Since these timers wrap every tick, they're not really
168 * useful as clocksources. Wrapping them to act like one is possible
169 * but not very efficient. So we provide a callout these arches
170 * can implement for use with the jiffies clocksource to provide
171 * finer then tick granular time.
173 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
174 extern u32 (*arch_gettimeoffset
)(void);
178 extern int do_setitimer(int which
, struct itimerval
*value
,
179 struct itimerval
*ovalue
);
180 extern int do_getitimer(int which
, struct itimerval
*value
);
182 extern long do_utimes(int dfd
, const char __user
*filename
, struct timespec64
*times
, int flags
);
185 * Similar to the struct tm in userspace <time.h>, but it needs to be here so
186 * that the kernel source is self contained.
190 * the number of seconds after the minute, normally in the range
191 * 0 to 59, but can be up to 60 to allow for leap seconds
194 /* the number of minutes after the hour, in the range 0 to 59*/
196 /* the number of hours past midnight, in the range 0 to 23 */
198 /* the day of the month, in the range 1 to 31 */
200 /* the number of months since January, in the range 0 to 11 */
202 /* the number of years since 1900 */
204 /* the number of days since Sunday, in the range 0 to 6 */
206 /* the number of days since January 1, in the range 0 to 365 */
210 void time64_to_tm(time64_t totalsecs
, int offset
, struct tm
*result
);
213 * time_to_tm - converts the calendar time to local broken-down time
215 * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970,
216 * Coordinated Universal Time (UTC).
217 * @offset offset seconds adding to totalsecs.
218 * @result pointer to struct tm variable to receive broken-down time
220 static inline void time_to_tm(time_t totalsecs
, int offset
, struct tm
*result
)
222 time64_to_tm(totalsecs
, offset
, result
);
226 * timespec_to_ns - Convert timespec to nanoseconds
227 * @ts: pointer to the timespec variable to be converted
229 * Returns the scalar nanosecond representation of the timespec
232 static inline s64
timespec_to_ns(const struct timespec
*ts
)
234 return ((s64
) ts
->tv_sec
* NSEC_PER_SEC
) + ts
->tv_nsec
;
238 * timeval_to_ns - Convert timeval to nanoseconds
239 * @ts: pointer to the timeval variable to be converted
241 * Returns the scalar nanosecond representation of the timeval
244 static inline s64
timeval_to_ns(const struct timeval
*tv
)
246 return ((s64
) tv
->tv_sec
* NSEC_PER_SEC
) +
247 tv
->tv_usec
* NSEC_PER_USEC
;
251 * ns_to_timespec - Convert nanoseconds to timespec
252 * @nsec: the nanoseconds value to be converted
254 * Returns the timespec representation of the nsec parameter.
256 extern struct timespec
ns_to_timespec(const s64 nsec
);
259 * ns_to_timeval - Convert nanoseconds to timeval
260 * @nsec: the nanoseconds value to be converted
262 * Returns the timeval representation of the nsec parameter.
264 extern struct timeval
ns_to_timeval(const s64 nsec
);
267 * timespec_add_ns - Adds nanoseconds to a timespec
268 * @a: pointer to timespec to be incremented
269 * @ns: unsigned nanoseconds value to be added
271 * This must always be inlined because its used from the x86-64 vdso,
272 * which cannot call other kernel functions.
274 static __always_inline
void timespec_add_ns(struct timespec
*a
, u64 ns
)
276 a
->tv_sec
+= __iter_div_u64_rem(a
->tv_nsec
+ ns
, NSEC_PER_SEC
, &ns
);
280 static inline bool itimerspec64_valid(const struct itimerspec64
*its
)
282 if (!timespec64_valid(&(its
->it_interval
)) ||
283 !timespec64_valid(&(its
->it_value
)))
290 * time_after32 - compare two 32-bit relative times
291 * @a: the time which may be after @b
292 * @b: the time which may be before @a
294 * time_after32(a, b) returns true if the time @a is after time @b.
295 * time_before32(b, a) returns true if the time @b is before time @a.
297 * Similar to time_after(), compare two 32-bit timestamps for relative
298 * times. This is useful for comparing 32-bit seconds values that can't
299 * be converted to 64-bit values (e.g. due to disk format or wire protocol
300 * issues) when it is known that the times are less than 68 years apart.
302 #define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
303 #define time_before32(b, a) time_after32(a, b)