time64.h 4.6 KB

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