gettimeofday.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic userspace implementations of gettimeofday() and similar.
  4. */
  5. #include <vdso/auxclock.h>
  6. #include <vdso/datapage.h>
  7. #include <vdso/helpers.h>
  8. /* Bring in default accessors */
  9. #include <vdso/vsyscall.h>
  10. #ifndef vdso_calc_ns
  11. #ifdef VDSO_DELTA_NOMASK
  12. # define VDSO_DELTA_MASK(vd) ULLONG_MAX
  13. #else
  14. # define VDSO_DELTA_MASK(vd) (vd->mask)
  15. #endif
  16. #ifdef CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT
  17. static __always_inline bool vdso_delta_ok(const struct vdso_clock *vc, u64 delta)
  18. {
  19. return delta < vc->max_cycles;
  20. }
  21. #else
  22. static __always_inline bool vdso_delta_ok(const struct vdso_clock *vc, u64 delta)
  23. {
  24. return true;
  25. }
  26. #endif
  27. #ifndef vdso_shift_ns
  28. static __always_inline u64 vdso_shift_ns(u64 ns, u32 shift)
  29. {
  30. return ns >> shift;
  31. }
  32. #endif
  33. /*
  34. * Default implementation which works for all sane clocksources. That
  35. * obviously excludes x86/TSC.
  36. */
  37. static __always_inline u64 vdso_calc_ns(const struct vdso_clock *vc, u64 cycles, u64 base)
  38. {
  39. u64 delta = (cycles - vc->cycle_last) & VDSO_DELTA_MASK(vc);
  40. if (likely(vdso_delta_ok(vc, delta)))
  41. return vdso_shift_ns((delta * vc->mult) + base, vc->shift);
  42. return mul_u64_u32_add_u64_shr(delta, vc->mult, base, vc->shift);
  43. }
  44. #endif /* vdso_calc_ns */
  45. #ifndef __arch_vdso_hres_capable
  46. static inline bool __arch_vdso_hres_capable(void)
  47. {
  48. return true;
  49. }
  50. #endif
  51. #ifndef vdso_clocksource_ok
  52. static inline bool vdso_clocksource_ok(const struct vdso_clock *vc)
  53. {
  54. return vc->clock_mode != VDSO_CLOCKMODE_NONE;
  55. }
  56. #endif
  57. #ifndef vdso_cycles_ok
  58. static inline bool vdso_cycles_ok(u64 cycles)
  59. {
  60. return true;
  61. }
  62. #endif
  63. static __always_inline bool vdso_clockid_valid(clockid_t clock)
  64. {
  65. /* Check for negative values or invalid clocks */
  66. return likely((u32) clock <= CLOCK_AUX_LAST);
  67. }
  68. /*
  69. * Must not be invoked within the sequence read section as a race inside
  70. * that loop could result in __iter_div_u64_rem() being extremely slow.
  71. */
  72. static __always_inline void vdso_set_timespec(struct __kernel_timespec *ts, u64 sec, u64 ns)
  73. {
  74. ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
  75. ts->tv_nsec = ns;
  76. }
  77. static __always_inline
  78. bool vdso_get_timestamp(const struct vdso_time_data *vd, const struct vdso_clock *vc,
  79. unsigned int clkidx, u64 *sec, u64 *ns)
  80. {
  81. const struct vdso_timestamp *vdso_ts = &vc->basetime[clkidx];
  82. u64 cycles;
  83. if (unlikely(!vdso_clocksource_ok(vc)))
  84. return false;
  85. cycles = __arch_get_hw_counter(vc->clock_mode, vd);
  86. if (unlikely(!vdso_cycles_ok(cycles)))
  87. return false;
  88. *ns = vdso_calc_ns(vc, cycles, vdso_ts->nsec);
  89. *sec = vdso_ts->sec;
  90. return true;
  91. }
  92. static __always_inline
  93. const struct vdso_time_data *__arch_get_vdso_u_timens_data(const struct vdso_time_data *vd)
  94. {
  95. return (void *)vd + PAGE_SIZE;
  96. }
  97. static __always_inline
  98. bool do_hres_timens(const struct vdso_time_data *vdns, const struct vdso_clock *vcns,
  99. clockid_t clk, struct __kernel_timespec *ts)
  100. {
  101. const struct vdso_time_data *vd = __arch_get_vdso_u_timens_data(vdns);
  102. const struct timens_offset *offs = &vcns->offset[clk];
  103. const struct vdso_clock *vc = vd->clock_data;
  104. u32 seq;
  105. s64 sec;
  106. u64 ns;
  107. if (clk != CLOCK_MONOTONIC_RAW)
  108. vc = &vc[CS_HRES_COARSE];
  109. else
  110. vc = &vc[CS_RAW];
  111. do {
  112. seq = vdso_read_begin(vc);
  113. if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
  114. return false;
  115. } while (unlikely(vdso_read_retry(vc, seq)));
  116. /* Add the namespace offset */
  117. sec += offs->sec;
  118. ns += offs->nsec;
  119. vdso_set_timespec(ts, sec, ns);
  120. return true;
  121. }
  122. static __always_inline
  123. bool do_hres(const struct vdso_time_data *vd, const struct vdso_clock *vc,
  124. clockid_t clk, struct __kernel_timespec *ts)
  125. {
  126. u64 sec, ns;
  127. u32 seq;
  128. /* Allows to compile the high resolution parts out */
  129. if (!__arch_vdso_hres_capable())
  130. return false;
  131. do {
  132. /*
  133. * Open coded function vdso_read_begin() to handle
  134. * VDSO_CLOCKMODE_TIMENS. Time namespace enabled tasks have a
  135. * special VVAR page installed which has vc->seq set to 1 and
  136. * vc->clock_mode set to VDSO_CLOCKMODE_TIMENS. For non time
  137. * namespace affected tasks this does not affect performance
  138. * because if vc->seq is odd, i.e. a concurrent update is in
  139. * progress the extra check for vc->clock_mode is just a few
  140. * extra instructions while spin waiting for vc->seq to become
  141. * even again.
  142. */
  143. while (unlikely((seq = READ_ONCE(vc->seq)) & 1)) {
  144. if (IS_ENABLED(CONFIG_TIME_NS) &&
  145. vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
  146. return do_hres_timens(vd, vc, clk, ts);
  147. cpu_relax();
  148. }
  149. smp_rmb();
  150. if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
  151. return false;
  152. } while (unlikely(vdso_read_retry(vc, seq)));
  153. vdso_set_timespec(ts, sec, ns);
  154. return true;
  155. }
  156. static __always_inline
  157. bool do_coarse_timens(const struct vdso_time_data *vdns, const struct vdso_clock *vcns,
  158. clockid_t clk, struct __kernel_timespec *ts)
  159. {
  160. const struct vdso_time_data *vd = __arch_get_vdso_u_timens_data(vdns);
  161. const struct timens_offset *offs = &vcns->offset[clk];
  162. const struct vdso_clock *vc = vd->clock_data;
  163. const struct vdso_timestamp *vdso_ts;
  164. u64 nsec;
  165. s64 sec;
  166. s32 seq;
  167. vdso_ts = &vc->basetime[clk];
  168. do {
  169. seq = vdso_read_begin(vc);
  170. sec = vdso_ts->sec;
  171. nsec = vdso_ts->nsec;
  172. } while (unlikely(vdso_read_retry(vc, seq)));
  173. /* Add the namespace offset */
  174. sec += offs->sec;
  175. nsec += offs->nsec;
  176. vdso_set_timespec(ts, sec, nsec);
  177. return true;
  178. }
  179. static __always_inline
  180. bool do_coarse(const struct vdso_time_data *vd, const struct vdso_clock *vc,
  181. clockid_t clk, struct __kernel_timespec *ts)
  182. {
  183. const struct vdso_timestamp *vdso_ts = &vc->basetime[clk];
  184. u32 seq;
  185. do {
  186. /*
  187. * Open coded function vdso_read_begin() to handle
  188. * VDSO_CLOCK_TIMENS. See comment in do_hres().
  189. */
  190. while ((seq = READ_ONCE(vc->seq)) & 1) {
  191. if (IS_ENABLED(CONFIG_TIME_NS) &&
  192. vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
  193. return do_coarse_timens(vd, vc, clk, ts);
  194. cpu_relax();
  195. }
  196. smp_rmb();
  197. ts->tv_sec = vdso_ts->sec;
  198. ts->tv_nsec = vdso_ts->nsec;
  199. } while (unlikely(vdso_read_retry(vc, seq)));
  200. return true;
  201. }
  202. static __always_inline
  203. bool do_aux(const struct vdso_time_data *vd, clockid_t clock, struct __kernel_timespec *ts)
  204. {
  205. const struct vdso_clock *vc;
  206. u32 seq, idx;
  207. u64 sec, ns;
  208. if (!IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
  209. return false;
  210. idx = clock - CLOCK_AUX;
  211. vc = &vd->aux_clock_data[idx];
  212. do {
  213. /*
  214. * Open coded function vdso_read_begin() to handle
  215. * VDSO_CLOCK_TIMENS. See comment in do_hres().
  216. */
  217. while ((seq = READ_ONCE(vc->seq)) & 1) {
  218. if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS) {
  219. vd = __arch_get_vdso_u_timens_data(vd);
  220. vc = &vd->aux_clock_data[idx];
  221. /* Re-read from the real time data page */
  222. continue;
  223. }
  224. cpu_relax();
  225. }
  226. smp_rmb();
  227. /* Auxclock disabled? */
  228. if (vc->clock_mode == VDSO_CLOCKMODE_NONE)
  229. return false;
  230. if (!vdso_get_timestamp(vd, vc, VDSO_BASE_AUX, &sec, &ns))
  231. return false;
  232. } while (unlikely(vdso_read_retry(vc, seq)));
  233. vdso_set_timespec(ts, sec, ns);
  234. return true;
  235. }
  236. static __always_inline bool
  237. __cvdso_clock_gettime_common(const struct vdso_time_data *vd, clockid_t clock,
  238. struct __kernel_timespec *ts)
  239. {
  240. const struct vdso_clock *vc = vd->clock_data;
  241. u32 msk;
  242. if (!vdso_clockid_valid(clock))
  243. return false;
  244. /*
  245. * Convert the clockid to a bitmask and use it to check which
  246. * clocks are handled in the VDSO directly.
  247. */
  248. msk = 1U << clock;
  249. if (likely(msk & VDSO_HRES))
  250. vc = &vc[CS_HRES_COARSE];
  251. else if (msk & VDSO_COARSE)
  252. return do_coarse(vd, &vc[CS_HRES_COARSE], clock, ts);
  253. else if (msk & VDSO_RAW)
  254. vc = &vc[CS_RAW];
  255. else if (msk & VDSO_AUX)
  256. return do_aux(vd, clock, ts);
  257. else
  258. return false;
  259. return do_hres(vd, vc, clock, ts);
  260. }
  261. static __maybe_unused int
  262. __cvdso_clock_gettime_data(const struct vdso_time_data *vd, clockid_t clock,
  263. struct __kernel_timespec *ts)
  264. {
  265. bool ok;
  266. ok = __cvdso_clock_gettime_common(vd, clock, ts);
  267. if (unlikely(!ok))
  268. return clock_gettime_fallback(clock, ts);
  269. return 0;
  270. }
  271. static __maybe_unused int
  272. __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
  273. {
  274. return __cvdso_clock_gettime_data(__arch_get_vdso_u_time_data(), clock, ts);
  275. }
  276. #ifdef BUILD_VDSO32
  277. static __maybe_unused int
  278. __cvdso_clock_gettime32_data(const struct vdso_time_data *vd, clockid_t clock,
  279. struct old_timespec32 *res)
  280. {
  281. struct __kernel_timespec ts;
  282. bool ok;
  283. ok = __cvdso_clock_gettime_common(vd, clock, &ts);
  284. if (unlikely(!ok))
  285. return clock_gettime32_fallback(clock, res);
  286. /* For ok == true */
  287. res->tv_sec = ts.tv_sec;
  288. res->tv_nsec = ts.tv_nsec;
  289. return 0;
  290. }
  291. static __maybe_unused int
  292. __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
  293. {
  294. return __cvdso_clock_gettime32_data(__arch_get_vdso_u_time_data(), clock, res);
  295. }
  296. #endif /* BUILD_VDSO32 */
  297. static __maybe_unused int
  298. __cvdso_gettimeofday_data(const struct vdso_time_data *vd,
  299. struct __kernel_old_timeval *tv, struct timezone *tz)
  300. {
  301. const struct vdso_clock *vc = vd->clock_data;
  302. if (likely(tv != NULL)) {
  303. struct __kernel_timespec ts;
  304. if (!do_hres(vd, &vc[CS_HRES_COARSE], CLOCK_REALTIME, &ts))
  305. return gettimeofday_fallback(tv, tz);
  306. tv->tv_sec = ts.tv_sec;
  307. tv->tv_usec = (u32)ts.tv_nsec / NSEC_PER_USEC;
  308. }
  309. if (unlikely(tz != NULL)) {
  310. if (IS_ENABLED(CONFIG_TIME_NS) &&
  311. vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
  312. vd = __arch_get_vdso_u_timens_data(vd);
  313. tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
  314. tz->tz_dsttime = vd[CS_HRES_COARSE].tz_dsttime;
  315. }
  316. return 0;
  317. }
  318. static __maybe_unused int
  319. __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
  320. {
  321. return __cvdso_gettimeofday_data(__arch_get_vdso_u_time_data(), tv, tz);
  322. }
  323. #ifdef VDSO_HAS_TIME
  324. static __maybe_unused __kernel_old_time_t
  325. __cvdso_time_data(const struct vdso_time_data *vd, __kernel_old_time_t *time)
  326. {
  327. const struct vdso_clock *vc = vd->clock_data;
  328. __kernel_old_time_t t;
  329. if (IS_ENABLED(CONFIG_TIME_NS) &&
  330. vc->clock_mode == VDSO_CLOCKMODE_TIMENS) {
  331. vd = __arch_get_vdso_u_timens_data(vd);
  332. vc = vd->clock_data;
  333. }
  334. t = READ_ONCE(vc[CS_HRES_COARSE].basetime[CLOCK_REALTIME].sec);
  335. if (time)
  336. *time = t;
  337. return t;
  338. }
  339. static __maybe_unused __kernel_old_time_t __cvdso_time(__kernel_old_time_t *time)
  340. {
  341. return __cvdso_time_data(__arch_get_vdso_u_time_data(), time);
  342. }
  343. #endif /* VDSO_HAS_TIME */
  344. #ifdef VDSO_HAS_CLOCK_GETRES
  345. static __always_inline
  346. bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t clock,
  347. struct __kernel_timespec *res)
  348. {
  349. const struct vdso_clock *vc = vd->clock_data;
  350. u32 msk;
  351. u64 ns;
  352. if (!vdso_clockid_valid(clock))
  353. return false;
  354. if (IS_ENABLED(CONFIG_TIME_NS) &&
  355. vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
  356. vd = __arch_get_vdso_u_timens_data(vd);
  357. /*
  358. * Convert the clockid to a bitmask and use it to check which
  359. * clocks are handled in the VDSO directly.
  360. */
  361. msk = 1U << clock;
  362. if (msk & (VDSO_HRES | VDSO_RAW)) {
  363. /*
  364. * Preserves the behaviour of posix_get_hrtimer_res().
  365. */
  366. ns = READ_ONCE(vd->hrtimer_res);
  367. } else if (msk & VDSO_COARSE) {
  368. /*
  369. * Preserves the behaviour of posix_get_coarse_res().
  370. */
  371. ns = LOW_RES_NSEC;
  372. } else if (msk & VDSO_AUX) {
  373. ns = aux_clock_resolution_ns();
  374. } else {
  375. return false;
  376. }
  377. if (likely(res)) {
  378. res->tv_sec = 0;
  379. res->tv_nsec = ns;
  380. }
  381. return true;
  382. }
  383. static __maybe_unused
  384. int __cvdso_clock_getres_data(const struct vdso_time_data *vd, clockid_t clock,
  385. struct __kernel_timespec *res)
  386. {
  387. bool ok;
  388. ok = __cvdso_clock_getres_common(vd, clock, res);
  389. if (unlikely(!ok))
  390. return clock_getres_fallback(clock, res);
  391. return 0;
  392. }
  393. static __maybe_unused
  394. int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
  395. {
  396. return __cvdso_clock_getres_data(__arch_get_vdso_u_time_data(), clock, res);
  397. }
  398. #ifdef BUILD_VDSO32
  399. static __maybe_unused int
  400. __cvdso_clock_getres_time32_data(const struct vdso_time_data *vd, clockid_t clock,
  401. struct old_timespec32 *res)
  402. {
  403. struct __kernel_timespec ts;
  404. bool ok;
  405. ok = __cvdso_clock_getres_common(vd, clock, &ts);
  406. if (unlikely(!ok))
  407. return clock_getres32_fallback(clock, res);
  408. if (likely(res)) {
  409. res->tv_sec = ts.tv_sec;
  410. res->tv_nsec = ts.tv_nsec;
  411. }
  412. return 0;
  413. }
  414. static __maybe_unused int
  415. __cvdso_clock_getres_time32(clockid_t clock, struct old_timespec32 *res)
  416. {
  417. return __cvdso_clock_getres_time32_data(__arch_get_vdso_u_time_data(),
  418. clock, res);
  419. }
  420. #endif /* BUILD_VDSO32 */
  421. #endif /* VDSO_HAS_CLOCK_GETRES */