1 // SPDX-License-Identifier: GPL-2.0
3 * Generic userspace implementations of gettimeofday() and similar.
5 #include <vdso/datapage.h>
6 #include <vdso/helpers.h>
8 #ifndef vdso_calc_delta
10 * Default implementation which works for all sane clocksources. That
11 * obviously excludes x86/TSC.
13 static __always_inline
14 u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
16 return ((cycles - last) & mask) * mult;
21 static __always_inline u64 vdso_shift_ns(u64 ns, u32 shift)
27 #ifndef __arch_vdso_hres_capable
28 static inline bool __arch_vdso_hres_capable(void)
34 #ifndef vdso_clocksource_ok
35 static inline bool vdso_clocksource_ok(const struct vdso_data *vd)
37 return vd->clock_mode != VDSO_CLOCKMODE_NONE;
41 #ifndef vdso_cycles_ok
42 static inline bool vdso_cycles_ok(u64 cycles)
49 static __always_inline int do_hres_timens(const struct vdso_data *vdns, clockid_t clk,
50 struct __kernel_timespec *ts)
52 const struct vdso_data *vd;
53 const struct timens_offset *offs = &vdns->offset[clk];
54 const struct vdso_timestamp *vdso_ts;
59 vd = vdns - (clk == CLOCK_MONOTONIC_RAW ? CS_RAW : CS_HRES_COARSE);
60 vd = __arch_get_timens_vdso_data(vd);
61 if (clk != CLOCK_MONOTONIC_RAW)
62 vd = &vd[CS_HRES_COARSE];
65 vdso_ts = &vd->basetime[clk];
68 seq = vdso_read_begin(vd);
70 if (unlikely(!vdso_clocksource_ok(vd)))
73 cycles = __arch_get_hw_counter(vd->clock_mode, vd);
74 if (unlikely(!vdso_cycles_ok(cycles)))
77 last = vd->cycle_last;
78 ns += vdso_calc_delta(cycles, last, vd->mask, vd->mult);
79 ns = vdso_shift_ns(ns, vd->shift);
81 } while (unlikely(vdso_read_retry(vd, seq)));
83 /* Add the namespace offset */
88 * Do this outside the loop: a race inside the loop could result
89 * in __iter_div_u64_rem() being extremely slow.
91 ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
97 static __always_inline
98 const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd)
103 static __always_inline int do_hres_timens(const struct vdso_data *vdns, clockid_t clk,
104 struct __kernel_timespec *ts)
110 static __always_inline int do_hres(const struct vdso_data *vd, clockid_t clk,
111 struct __kernel_timespec *ts)
113 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
114 u64 cycles, last, sec, ns;
117 /* Allows to compile the high resolution parts out */
118 if (!__arch_vdso_hres_capable())
123 * Open coded to handle VDSO_CLOCKMODE_TIMENS. Time namespace
124 * enabled tasks have a special VVAR page installed which
125 * has vd->seq set to 1 and vd->clock_mode set to
126 * VDSO_CLOCKMODE_TIMENS. For non time namespace affected tasks
127 * this does not affect performance because if vd->seq is
128 * odd, i.e. a concurrent update is in progress the extra
129 * check for vd->clock_mode is just a few extra
130 * instructions while spin waiting for vd->seq to become
133 while (unlikely((seq = READ_ONCE(vd->seq)) & 1)) {
134 if (IS_ENABLED(CONFIG_TIME_NS) &&
135 vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
136 return do_hres_timens(vd, clk, ts);
141 if (unlikely(!vdso_clocksource_ok(vd)))
144 cycles = __arch_get_hw_counter(vd->clock_mode, vd);
145 if (unlikely(!vdso_cycles_ok(cycles)))
148 last = vd->cycle_last;
149 ns += vdso_calc_delta(cycles, last, vd->mask, vd->mult);
150 ns = vdso_shift_ns(ns, vd->shift);
152 } while (unlikely(vdso_read_retry(vd, seq)));
155 * Do this outside the loop: a race inside the loop could result
156 * in __iter_div_u64_rem() being extremely slow.
158 ts->tv_sec = sec + __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
164 #ifdef CONFIG_TIME_NS
165 static __always_inline int do_coarse_timens(const struct vdso_data *vdns, clockid_t clk,
166 struct __kernel_timespec *ts)
168 const struct vdso_data *vd = __arch_get_timens_vdso_data(vdns);
169 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
170 const struct timens_offset *offs = &vdns->offset[clk];
176 seq = vdso_read_begin(vd);
178 nsec = vdso_ts->nsec;
179 } while (unlikely(vdso_read_retry(vd, seq)));
181 /* Add the namespace offset */
186 * Do this outside the loop: a race inside the loop could result
187 * in __iter_div_u64_rem() being extremely slow.
189 ts->tv_sec = sec + __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec);
194 static __always_inline int do_coarse_timens(const struct vdso_data *vdns, clockid_t clk,
195 struct __kernel_timespec *ts)
201 static __always_inline int do_coarse(const struct vdso_data *vd, clockid_t clk,
202 struct __kernel_timespec *ts)
204 const struct vdso_timestamp *vdso_ts = &vd->basetime[clk];
209 * Open coded to handle VDSO_CLOCK_TIMENS. See comment in
212 while ((seq = READ_ONCE(vd->seq)) & 1) {
213 if (IS_ENABLED(CONFIG_TIME_NS) &&
214 vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
215 return do_coarse_timens(vd, clk, ts);
220 ts->tv_sec = vdso_ts->sec;
221 ts->tv_nsec = vdso_ts->nsec;
222 } while (unlikely(vdso_read_retry(vd, seq)));
227 static __always_inline int
228 __cvdso_clock_gettime_common(const struct vdso_data *vd, clockid_t clock,
229 struct __kernel_timespec *ts)
233 /* Check for negative values or invalid clocks */
234 if (unlikely((u32) clock >= MAX_CLOCKS))
238 * Convert the clockid to a bitmask and use it to check which
239 * clocks are handled in the VDSO directly.
242 if (likely(msk & VDSO_HRES))
243 vd = &vd[CS_HRES_COARSE];
244 else if (msk & VDSO_COARSE)
245 return do_coarse(&vd[CS_HRES_COARSE], clock, ts);
246 else if (msk & VDSO_RAW)
251 return do_hres(vd, clock, ts);
254 static __maybe_unused int
255 __cvdso_clock_gettime_data(const struct vdso_data *vd, clockid_t clock,
256 struct __kernel_timespec *ts)
258 int ret = __cvdso_clock_gettime_common(vd, clock, ts);
261 return clock_gettime_fallback(clock, ts);
265 static __maybe_unused int
266 __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
268 return __cvdso_clock_gettime_data(__arch_get_vdso_data(), clock, ts);
272 static __maybe_unused int
273 __cvdso_clock_gettime32_data(const struct vdso_data *vd, clockid_t clock,
274 struct old_timespec32 *res)
276 struct __kernel_timespec ts;
279 ret = __cvdso_clock_gettime_common(vd, clock, &ts);
282 return clock_gettime32_fallback(clock, res);
285 res->tv_sec = ts.tv_sec;
286 res->tv_nsec = ts.tv_nsec;
291 static __maybe_unused int
292 __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
294 return __cvdso_clock_gettime32_data(__arch_get_vdso_data(), clock, res);
296 #endif /* BUILD_VDSO32 */
298 static __maybe_unused int
299 __cvdso_gettimeofday_data(const struct vdso_data *vd,
300 struct __kernel_old_timeval *tv, struct timezone *tz)
303 if (likely(tv != NULL)) {
304 struct __kernel_timespec ts;
306 if (do_hres(&vd[CS_HRES_COARSE], CLOCK_REALTIME, &ts))
307 return gettimeofday_fallback(tv, tz);
309 tv->tv_sec = ts.tv_sec;
310 tv->tv_usec = (u32)ts.tv_nsec / NSEC_PER_USEC;
313 if (unlikely(tz != NULL)) {
314 if (IS_ENABLED(CONFIG_TIME_NS) &&
315 vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
316 vd = __arch_get_timens_vdso_data(vd);
318 tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
319 tz->tz_dsttime = vd[CS_HRES_COARSE].tz_dsttime;
325 static __maybe_unused int
326 __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
328 return __cvdso_gettimeofday_data(__arch_get_vdso_data(), tv, tz);
332 static __maybe_unused __kernel_old_time_t
333 __cvdso_time_data(const struct vdso_data *vd, __kernel_old_time_t *time)
335 __kernel_old_time_t t;
337 if (IS_ENABLED(CONFIG_TIME_NS) &&
338 vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
339 vd = __arch_get_timens_vdso_data(vd);
341 t = READ_ONCE(vd[CS_HRES_COARSE].basetime[CLOCK_REALTIME].sec);
349 static __maybe_unused __kernel_old_time_t __cvdso_time(__kernel_old_time_t *time)
351 return __cvdso_time_data(__arch_get_vdso_data(), time);
353 #endif /* VDSO_HAS_TIME */
355 #ifdef VDSO_HAS_CLOCK_GETRES
356 static __maybe_unused
357 int __cvdso_clock_getres_common(const struct vdso_data *vd, clockid_t clock,
358 struct __kernel_timespec *res)
363 /* Check for negative values or invalid clocks */
364 if (unlikely((u32) clock >= MAX_CLOCKS))
367 if (IS_ENABLED(CONFIG_TIME_NS) &&
368 vd->clock_mode == VDSO_CLOCKMODE_TIMENS)
369 vd = __arch_get_timens_vdso_data(vd);
372 * Convert the clockid to a bitmask and use it to check which
373 * clocks are handled in the VDSO directly.
376 if (msk & (VDSO_HRES | VDSO_RAW)) {
378 * Preserves the behaviour of posix_get_hrtimer_res().
380 ns = READ_ONCE(vd[CS_HRES_COARSE].hrtimer_res);
381 } else if (msk & VDSO_COARSE) {
383 * Preserves the behaviour of posix_get_coarse_res().
397 static __maybe_unused
398 int __cvdso_clock_getres_data(const struct vdso_data *vd, clockid_t clock,
399 struct __kernel_timespec *res)
401 int ret = __cvdso_clock_getres_common(vd, clock, res);
404 return clock_getres_fallback(clock, res);
408 static __maybe_unused
409 int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
411 return __cvdso_clock_getres_data(__arch_get_vdso_data(), clock, res);
415 static __maybe_unused int
416 __cvdso_clock_getres_time32_data(const struct vdso_data *vd, clockid_t clock,
417 struct old_timespec32 *res)
419 struct __kernel_timespec ts;
422 ret = __cvdso_clock_getres_common(vd, clock, &ts);
425 return clock_getres32_fallback(clock, res);
428 res->tv_sec = ts.tv_sec;
429 res->tv_nsec = ts.tv_nsec;
434 static __maybe_unused int
435 __cvdso_clock_getres_time32(clockid_t clock, struct old_timespec32 *res)
437 return __cvdso_clock_getres_time32_data(__arch_get_vdso_data(),
440 #endif /* BUILD_VDSO32 */
441 #endif /* VDSO_HAS_CLOCK_GETRES */