time64.h 4.4 KB

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