x86/vdso: Add VCLOCK_HVCLOCK vDSO clock read method
authorVitaly Kuznetsov <vkuznets@redhat.com>
Fri, 3 Mar 2017 13:21:42 +0000 (14:21 +0100)
committerThomas Gleixner <tglx@linutronix.de>
Sat, 11 Mar 2017 13:47:28 +0000 (14:47 +0100)
Hyper-V TSC page clocksource is suitable for vDSO, however, the protocol
defined by the hypervisor is different from VCLOCK_PVCLOCK. Implement the
required support by adding hvclock_page VVAR.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Dexuan Cui <decui@microsoft.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: devel@linuxdriverproject.org
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: virtualization@lists.linux-foundation.org
Link: http://lkml.kernel.org/r/20170303132142.25595-4-vkuznets@redhat.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
arch/x86/entry/vdso/vclock_gettime.c
arch/x86/entry/vdso/vdso-layout.lds.S
arch/x86/entry/vdso/vdso2c.c
arch/x86/entry/vdso/vma.c
arch/x86/hyperv/hv_init.c
arch/x86/include/asm/clocksource.h
arch/x86/include/asm/vdso.h

index 9d4d6e1383117b5091a172b1c373eb2dc7ed3c2e..fa8dbfcf7ed37f5677d9185d16e9addb114cf83f 100644 (file)
@@ -17,6 +17,7 @@
 #include <asm/unistd.h>
 #include <asm/msr.h>
 #include <asm/pvclock.h>
+#include <asm/mshyperv.h>
 #include <linux/math64.h>
 #include <linux/time.h>
 #include <linux/kernel.h>
@@ -32,6 +33,11 @@ extern u8 pvclock_page
        __attribute__((visibility("hidden")));
 #endif
 
+#ifdef CONFIG_HYPERV_TSCPAGE
+extern u8 hvclock_page
+       __attribute__((visibility("hidden")));
+#endif
+
 #ifndef BUILD_VDSO32
 
 notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
@@ -141,6 +147,20 @@ static notrace u64 vread_pvclock(int *mode)
        return last;
 }
 #endif
+#ifdef CONFIG_HYPERV_TSCPAGE
+static notrace u64 vread_hvclock(int *mode)
+{
+       const struct ms_hyperv_tsc_page *tsc_pg =
+               (const struct ms_hyperv_tsc_page *)&hvclock_page;
+       u64 current_tick = hv_read_tsc_page(tsc_pg);
+
+       if (current_tick != U64_MAX)
+               return current_tick;
+
+       *mode = VCLOCK_NONE;
+       return 0;
+}
+#endif
 
 notrace static u64 vread_tsc(void)
 {
@@ -172,6 +192,10 @@ notrace static inline u64 vgetsns(int *mode)
 #ifdef CONFIG_PARAVIRT_CLOCK
        else if (gtod->vclock_mode == VCLOCK_PVCLOCK)
                cycles = vread_pvclock(mode);
+#endif
+#ifdef CONFIG_HYPERV_TSCPAGE
+       else if (gtod->vclock_mode == VCLOCK_HVCLOCK)
+               cycles = vread_hvclock(mode);
 #endif
        else
                return 0;
index a708aa90b507fce048e05676f073f7b9f3347e59..8ebb4b6454fed6131511a3ee6a62f55ebf3de049 100644 (file)
@@ -25,7 +25,7 @@ SECTIONS
         * segment.
         */
 
-       vvar_start = . - 2 * PAGE_SIZE;
+       vvar_start = . - 3 * PAGE_SIZE;
        vvar_page = vvar_start;
 
        /* Place all vvars at the offsets in asm/vvar.h. */
@@ -36,6 +36,7 @@ SECTIONS
 #undef EMIT_VVAR
 
        pvclock_page = vvar_start + PAGE_SIZE;
+       hvclock_page = vvar_start + 2 * PAGE_SIZE;
 
        . = SIZEOF_HEADERS;
 
index 491020b2826d9d1e3907e14524d9bbea46c2b726..0780a443a53b5fbc8e5e202fd7eb585c445eef65 100644 (file)
@@ -74,6 +74,7 @@ enum {
        sym_vvar_page,
        sym_hpet_page,
        sym_pvclock_page,
+       sym_hvclock_page,
        sym_VDSO_FAKE_SECTION_TABLE_START,
        sym_VDSO_FAKE_SECTION_TABLE_END,
 };
@@ -82,6 +83,7 @@ const int special_pages[] = {
        sym_vvar_page,
        sym_hpet_page,
        sym_pvclock_page,
+       sym_hvclock_page,
 };
 
 struct vdso_sym {
@@ -94,6 +96,7 @@ struct vdso_sym required_syms[] = {
        [sym_vvar_page] = {"vvar_page", true},
        [sym_hpet_page] = {"hpet_page", true},
        [sym_pvclock_page] = {"pvclock_page", true},
+       [sym_hvclock_page] = {"hvclock_page", true},
        [sym_VDSO_FAKE_SECTION_TABLE_START] = {
                "VDSO_FAKE_SECTION_TABLE_START", false
        },
index 226ca70dc6bd43b04e15477c8574b5d48d0d4ff8..faf80fdeeacc3fac2e99804c855ec502d37db1d4 100644 (file)
@@ -22,6 +22,7 @@
 #include <asm/page.h>
 #include <asm/desc.h>
 #include <asm/cpufeature.h>
+#include <asm/mshyperv.h>
 
 #if defined(CONFIG_X86_64)
 unsigned int __read_mostly vdso64_enabled = 1;
@@ -121,6 +122,12 @@ static int vvar_fault(const struct vm_special_mapping *sm,
                                vmf->address,
                                __pa(pvti) >> PAGE_SHIFT);
                }
+       } else if (sym_offset == image->sym_hvclock_page) {
+               struct ms_hyperv_tsc_page *tsc_pg = hv_get_tsc_page();
+
+               if (tsc_pg && vclock_was_used(VCLOCK_HVCLOCK))
+                       ret = vm_insert_pfn(vma, vmf->address,
+                                           vmalloc_to_pfn(tsc_pg));
        }
 
        if (ret == 0 || ret == -EBUSY)
index 7f5152356a59eadd536d0811e96a97500875b83a..2b01421f7d0ff0fb49e8fedbb8764b6fd6be04f4 100644 (file)
@@ -132,6 +132,9 @@ void hyperv_init(void)
                tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
 
                wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
+
+               hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
+
                clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
                return;
        }
index eae33c7170c868993ffc3b861ca30ddc14285aa7..47bea8cadbd0b6b7f39f9ef97f92037b63ce8c09 100644 (file)
@@ -6,7 +6,8 @@
 #define VCLOCK_NONE    0       /* No vDSO clock available.             */
 #define VCLOCK_TSC     1       /* vDSO should use vread_tsc.           */
 #define VCLOCK_PVCLOCK 2       /* vDSO should use vread_pvclock.       */
-#define VCLOCK_MAX     2
+#define VCLOCK_HVCLOCK 3       /* vDSO should use vread_hvclock.       */
+#define VCLOCK_MAX     3
 
 struct arch_clocksource_data {
        int vclock_mode;
index 2444189cbe28fdfae754876ca5dd25fd30b4b4c9..bccdf4938ddf3df0d78dd20a2b40623960f47f01 100644 (file)
@@ -20,6 +20,7 @@ struct vdso_image {
        long sym_vvar_page;
        long sym_hpet_page;
        long sym_pvclock_page;
+       long sym_hvclock_page;
        long sym_VDSO32_NOTE_MASK;
        long sym___kernel_sigreturn;
        long sym___kernel_rt_sigreturn;