Re: [RFC 6/6] arm64: hyperv: Enable vDSO

From: Vitaly Kuznetsov
Date: Tue Dec 17 2019 - 09:10:26 EST


Boqun Feng <boqun.feng@xxxxxxxxx> writes:

> Similar to x86, add a new vclock_mode VCLOCK_HVCLOCK, and reuse the
> hv_read_tsc_page() for userspace to read tsc page clocksource.
>
> Signed-off-by: Boqun Feng (Microsoft) <boqun.feng@xxxxxxxxx>
> ---
> arch/arm64/include/asm/clocksource.h | 3 ++-
> arch/arm64/include/asm/mshyperv.h | 2 +-
> arch/arm64/include/asm/vdso/gettimeofday.h | 19 +++++++++++++++++++
> 3 files changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm64/include/asm/clocksource.h b/arch/arm64/include/asm/clocksource.h
> index fbe80057468c..c6acd45fe748 100644
> --- a/arch/arm64/include/asm/clocksource.h
> +++ b/arch/arm64/include/asm/clocksource.h
> @@ -4,7 +4,8 @@
>
> #define VCLOCK_NONE 0 /* No vDSO clock available. */
> #define VCLOCK_CNTVCT 1 /* vDSO should use cntvcnt */
> -#define VCLOCK_MAX 1
> +#define VCLOCK_HVCLOCK 2 /* vDSO should use vread_hvclock() */
> +#define VCLOCK_MAX 2
>
> struct arch_clocksource_data {
> int vclock_mode;
> diff --git a/arch/arm64/include/asm/mshyperv.h b/arch/arm64/include/asm/mshyperv.h
> index 0afb00e3501d..7c85dd816dca 100644
> --- a/arch/arm64/include/asm/mshyperv.h
> +++ b/arch/arm64/include/asm/mshyperv.h
> @@ -90,7 +90,7 @@ extern void hv_get_vpreg_128(u32 reg, struct hv_get_vp_register_output *result);
> #define hv_set_reference_tsc(val) \
> hv_set_vpreg(HV_REGISTER_REFERENCE_TSC, val)
> #define hv_set_clocksource_vdso(val) \
> - ((val).archdata.vclock_mode = VCLOCK_NONE)
> + ((val).archdata.vclock_mode = VCLOCK_HVCLOCK)
>
> #if IS_ENABLED(CONFIG_HYPERV)
> #define hv_enable_stimer0_percpu_irq(irq) enable_percpu_irq(irq, 0)
> diff --git a/arch/arm64/include/asm/vdso/gettimeofday.h b/arch/arm64/include/asm/vdso/gettimeofday.h
> index e6e3fe0488c7..7e689b903f4d 100644
> --- a/arch/arm64/include/asm/vdso/gettimeofday.h
> +++ b/arch/arm64/include/asm/vdso/gettimeofday.h
> @@ -67,6 +67,20 @@ int clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
> return ret;
> }
>
> +#ifdef CONFIG_HYPERV_TIMER
> +/* This will override the default hv_get_raw_timer() */
> +#define hv_get_raw_timer() __arch_counter_get_cntvct()
> +#include <clocksource/hyperv_timer.h>
> +
> +extern struct ms_hyperv_tsc_page
> +_hvclock_page __attribute__((visibility("hidden")));
> +
> +static u64 vread_hvclock(void)
> +{
> + return hv_read_tsc_page(&_hvclock_page);
> +}
> +#endif

The function is almost the same on x86 (&_hvclock_page ->
&hvclock_page), would it maybe make sense to move this to arch neutral
clocksource/hyperv_timer.h?

> +
> static __always_inline u64 __arch_get_hw_counter(s32 clock_mode)
> {
> u64 res;
> @@ -78,6 +92,11 @@ static __always_inline u64 __arch_get_hw_counter(s32 clock_mode)
> if (clock_mode == VCLOCK_NONE)
> return __VDSO_USE_SYSCALL;
>
> +#ifdef CONFIG_HYPERV_TIMER
> + if (likely(clock_mode == VCLOCK_HVCLOCK))
> + return vread_hvclock();

I'm not sure likely() is justified here: it'll make ALL builds which
enable CONFIG_HYPERV_TIMER (e.g. distro kernels) to prefer
VCLOCK_HVCLOCK, even if the kernel is not running on Hyper-V.

> +#endif
> +
> /*
> * This isb() is required to prevent that the counter value
> * is speculated.

--
Vitaly