at v4.14 9.0 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_TIME_H 3#define _LINUX_TIME_H 4 5# include <linux/cache.h> 6# include <linux/seqlock.h> 7# include <linux/math64.h> 8# include <linux/time64.h> 9 10extern struct timezone sys_tz; 11 12int get_timespec64(struct timespec64 *ts, 13 const struct timespec __user *uts); 14int put_timespec64(const struct timespec64 *ts, 15 struct timespec __user *uts); 16int get_itimerspec64(struct itimerspec64 *it, 17 const struct itimerspec __user *uit); 18int put_itimerspec64(const struct itimerspec64 *it, 19 struct itimerspec __user *uit); 20 21#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) 22 23static inline int timespec_equal(const struct timespec *a, 24 const struct timespec *b) 25{ 26 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); 27} 28 29/* 30 * lhs < rhs: return <0 31 * lhs == rhs: return 0 32 * lhs > rhs: return >0 33 */ 34static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs) 35{ 36 if (lhs->tv_sec < rhs->tv_sec) 37 return -1; 38 if (lhs->tv_sec > rhs->tv_sec) 39 return 1; 40 return lhs->tv_nsec - rhs->tv_nsec; 41} 42 43static inline int timeval_compare(const struct timeval *lhs, const struct timeval *rhs) 44{ 45 if (lhs->tv_sec < rhs->tv_sec) 46 return -1; 47 if (lhs->tv_sec > rhs->tv_sec) 48 return 1; 49 return lhs->tv_usec - rhs->tv_usec; 50} 51 52extern 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); 55 56/** 57 * Deprecated. Use mktime64(). 58 */ 59static 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) 63{ 64 return mktime64(year, mon, day, hour, min, sec); 65} 66 67extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); 68 69/* 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. 73 */ 74extern struct timespec timespec_add_safe(const struct timespec lhs, 75 const struct timespec rhs); 76 77 78static inline struct timespec timespec_add(struct timespec lhs, 79 struct timespec rhs) 80{ 81 struct timespec ts_delta; 82 set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, 83 lhs.tv_nsec + rhs.tv_nsec); 84 return ts_delta; 85} 86 87/* 88 * sub = lhs - rhs, in normalized form 89 */ 90static inline struct timespec timespec_sub(struct timespec lhs, 91 struct timespec rhs) 92{ 93 struct timespec ts_delta; 94 set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec, 95 lhs.tv_nsec - rhs.tv_nsec); 96 return ts_delta; 97} 98 99/* 100 * Returns true if the timespec is norm, false if denorm: 101 */ 102static inline bool timespec_valid(const struct timespec *ts) 103{ 104 /* Dates before 1970 are bogus */ 105 if (ts->tv_sec < 0) 106 return false; 107 /* Can't have more nanoseconds then a second */ 108 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 109 return false; 110 return true; 111} 112 113static inline bool timespec_valid_strict(const struct timespec *ts) 114{ 115 if (!timespec_valid(ts)) 116 return false; 117 /* Disallow values that could overflow ktime_t */ 118 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) 119 return false; 120 return true; 121} 122 123static inline bool timeval_valid(const struct timeval *tv) 124{ 125 /* Dates before 1970 are bogus */ 126 if (tv->tv_sec < 0) 127 return false; 128 129 /* Can't have more microseconds then a second */ 130 if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) 131 return false; 132 133 return true; 134} 135 136extern struct timespec timespec_trunc(struct timespec t, unsigned gran); 137 138/* 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. 143 */ 144static inline bool timeval_inject_offset_valid(const struct timeval *tv) 145{ 146 /* We don't check the tv_sec as it can be positive or negative */ 147 148 /* Can't have more microseconds then a second */ 149 if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) 150 return false; 151 return true; 152} 153 154static inline bool timespec_inject_offset_valid(const struct timespec *ts) 155{ 156 /* We don't check the tv_sec as it can be positive or negative */ 157 158 /* Can't have more nanoseconds then a second */ 159 if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC) 160 return false; 161 return true; 162} 163 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. 172 */ 173#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET 174extern u32 (*arch_gettimeoffset)(void); 175#endif 176 177struct itimerval; 178extern int do_setitimer(int which, struct itimerval *value, 179 struct itimerval *ovalue); 180extern int do_getitimer(int which, struct itimerval *value); 181 182extern long do_utimes(int dfd, const char __user *filename, struct timespec64 *times, int flags); 183 184/* 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. 187 */ 188struct tm { 189 /* 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 192 */ 193 int tm_sec; 194 /* the number of minutes after the hour, in the range 0 to 59*/ 195 int tm_min; 196 /* the number of hours past midnight, in the range 0 to 23 */ 197 int tm_hour; 198 /* the day of the month, in the range 1 to 31 */ 199 int tm_mday; 200 /* the number of months since January, in the range 0 to 11 */ 201 int tm_mon; 202 /* the number of years since 1900 */ 203 long tm_year; 204 /* the number of days since Sunday, in the range 0 to 6 */ 205 int tm_wday; 206 /* the number of days since January 1, in the range 0 to 365 */ 207 int tm_yday; 208}; 209 210void time64_to_tm(time64_t totalsecs, int offset, struct tm *result); 211 212/** 213 * time_to_tm - converts the calendar time to local broken-down time 214 * 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 219 */ 220static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result) 221{ 222 time64_to_tm(totalsecs, offset, result); 223} 224 225/** 226 * timespec_to_ns - Convert timespec to nanoseconds 227 * @ts: pointer to the timespec variable to be converted 228 * 229 * Returns the scalar nanosecond representation of the timespec 230 * parameter. 231 */ 232static inline s64 timespec_to_ns(const struct timespec *ts) 233{ 234 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; 235} 236 237/** 238 * timeval_to_ns - Convert timeval to nanoseconds 239 * @ts: pointer to the timeval variable to be converted 240 * 241 * Returns the scalar nanosecond representation of the timeval 242 * parameter. 243 */ 244static inline s64 timeval_to_ns(const struct timeval *tv) 245{ 246 return ((s64) tv->tv_sec * NSEC_PER_SEC) + 247 tv->tv_usec * NSEC_PER_USEC; 248} 249 250/** 251 * ns_to_timespec - Convert nanoseconds to timespec 252 * @nsec: the nanoseconds value to be converted 253 * 254 * Returns the timespec representation of the nsec parameter. 255 */ 256extern struct timespec ns_to_timespec(const s64 nsec); 257 258/** 259 * ns_to_timeval - Convert nanoseconds to timeval 260 * @nsec: the nanoseconds value to be converted 261 * 262 * Returns the timeval representation of the nsec parameter. 263 */ 264extern struct timeval ns_to_timeval(const s64 nsec); 265 266/** 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 270 * 271 * This must always be inlined because its used from the x86-64 vdso, 272 * which cannot call other kernel functions. 273 */ 274static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) 275{ 276 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); 277 a->tv_nsec = ns; 278} 279 280static inline bool itimerspec64_valid(const struct itimerspec64 *its) 281{ 282 if (!timespec64_valid(&(its->it_interval)) || 283 !timespec64_valid(&(its->it_value))) 284 return false; 285 286 return true; 287} 288 289/** 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 293 * 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. 296 * 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. 301 */ 302#define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0) 303#define time_before32(b, a) time_after32(a, b) 304#endif