time64.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef _LINUX_TIME64_H
  2. #define _LINUX_TIME64_H
  3. #include <uapi/linux/time.h>
  4. #include <linux/math64.h>
  5. typedef __s64 time64_t;
  6. typedef __u64 timeu64_t;
  7. /*
  8. * This wants to go into uapi/linux/time.h once we agreed about the
  9. * userspace interfaces.
  10. */
  11. #if __BITS_PER_LONG == 64
  12. # define timespec64 timespec
  13. #define itimerspec64 itimerspec
  14. #else
  15. struct timespec64 {
  16. time64_t tv_sec; /* seconds */
  17. long tv_nsec; /* nanoseconds */
  18. };
  19. struct itimerspec64 {
  20. struct timespec64 it_interval;
  21. struct timespec64 it_value;
  22. };
  23. #endif
  24. /* Parameters used to convert the timespec values: */
  25. #define MSEC_PER_SEC 1000L
  26. #define USEC_PER_MSEC 1000L
  27. #define NSEC_PER_USEC 1000L
  28. #define NSEC_PER_MSEC 1000000L
  29. #define USEC_PER_SEC 1000000L
  30. #define NSEC_PER_SEC 1000000000L
  31. #define FSEC_PER_SEC 1000000000000000LL
  32. /* Located here for timespec[64]_valid_strict */
  33. #define TIME64_MAX ((s64)~((u64)1 << 63))
  34. #define KTIME_MAX ((s64)~((u64)1 << 63))
  35. #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
  36. #if __BITS_PER_LONG == 64
  37. static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
  38. {
  39. return ts64;
  40. }
  41. static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
  42. {
  43. return ts;
  44. }
  45. static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
  46. {
  47. return *its64;
  48. }
  49. static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
  50. {
  51. return *its;
  52. }
  53. # define timespec64_equal timespec_equal
  54. # define timespec64_compare timespec_compare
  55. # define set_normalized_timespec64 set_normalized_timespec
  56. # define timespec64_add timespec_add
  57. # define timespec64_sub timespec_sub
  58. # define timespec64_valid timespec_valid
  59. # define timespec64_valid_strict timespec_valid_strict
  60. # define timespec64_to_ns timespec_to_ns
  61. # define ns_to_timespec64 ns_to_timespec
  62. # define timespec64_add_ns timespec_add_ns
  63. #else
  64. static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
  65. {
  66. struct timespec ret;
  67. ret.tv_sec = (time_t)ts64.tv_sec;
  68. ret.tv_nsec = ts64.tv_nsec;
  69. return ret;
  70. }
  71. static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
  72. {
  73. struct timespec64 ret;
  74. ret.tv_sec = ts.tv_sec;
  75. ret.tv_nsec = ts.tv_nsec;
  76. return ret;
  77. }
  78. static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
  79. {
  80. struct itimerspec ret;
  81. ret.it_interval = timespec64_to_timespec(its64->it_interval);
  82. ret.it_value = timespec64_to_timespec(its64->it_value);
  83. return ret;
  84. }
  85. static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
  86. {
  87. struct itimerspec64 ret;
  88. ret.it_interval = timespec_to_timespec64(its->it_interval);
  89. ret.it_value = timespec_to_timespec64(its->it_value);
  90. return ret;
  91. }
  92. static inline int timespec64_equal(const struct timespec64 *a,
  93. const struct timespec64 *b)
  94. {
  95. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  96. }
  97. /*
  98. * lhs < rhs: return <0
  99. * lhs == rhs: return 0
  100. * lhs > rhs: return >0
  101. */
  102. static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
  103. {
  104. if (lhs->tv_sec < rhs->tv_sec)
  105. return -1;
  106. if (lhs->tv_sec > rhs->tv_sec)
  107. return 1;
  108. return lhs->tv_nsec - rhs->tv_nsec;
  109. }
  110. extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
  111. static inline struct timespec64 timespec64_add(struct timespec64 lhs,
  112. struct timespec64 rhs)
  113. {
  114. struct timespec64 ts_delta;
  115. set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
  116. lhs.tv_nsec + rhs.tv_nsec);
  117. return ts_delta;
  118. }
  119. /*
  120. * sub = lhs - rhs, in normalized form
  121. */
  122. static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
  123. struct timespec64 rhs)
  124. {
  125. struct timespec64 ts_delta;
  126. set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
  127. lhs.tv_nsec - rhs.tv_nsec);
  128. return ts_delta;
  129. }
  130. /*
  131. * Returns true if the timespec64 is norm, false if denorm:
  132. */
  133. static inline bool timespec64_valid(const struct timespec64 *ts)
  134. {
  135. /* Dates before 1970 are bogus */
  136. if (ts->tv_sec < 0)
  137. return false;
  138. /* Can't have more nanoseconds then a second */
  139. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  140. return false;
  141. return true;
  142. }
  143. static inline bool timespec64_valid_strict(const struct timespec64 *ts)
  144. {
  145. if (!timespec64_valid(ts))
  146. return false;
  147. /* Disallow values that could overflow ktime_t */
  148. if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
  149. return false;
  150. return true;
  151. }
  152. /**
  153. * timespec64_to_ns - Convert timespec64 to nanoseconds
  154. * @ts: pointer to the timespec64 variable to be converted
  155. *
  156. * Returns the scalar nanosecond representation of the timespec64
  157. * parameter.
  158. */
  159. static inline s64 timespec64_to_ns(const struct timespec64 *ts)
  160. {
  161. return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  162. }
  163. /**
  164. * ns_to_timespec64 - Convert nanoseconds to timespec64
  165. * @nsec: the nanoseconds value to be converted
  166. *
  167. * Returns the timespec64 representation of the nsec parameter.
  168. */
  169. extern struct timespec64 ns_to_timespec64(const s64 nsec);
  170. /**
  171. * timespec64_add_ns - Adds nanoseconds to a timespec64
  172. * @a: pointer to timespec64 to be incremented
  173. * @ns: unsigned nanoseconds value to be added
  174. *
  175. * This must always be inlined because its used from the x86-64 vdso,
  176. * which cannot call other kernel functions.
  177. */
  178. static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
  179. {
  180. a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
  181. a->tv_nsec = ns;
  182. }
  183. #endif
  184. /*
  185. * timespec64_add_safe assumes both values are positive and checks for
  186. * overflow. It will return TIME64_MAX in case of overflow.
  187. */
  188. extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
  189. const struct timespec64 rhs);
  190. #endif /* _LINUX_TIME64_H */