Skip to contentSkip to conversation

Xen on Hyper-V: Implement L0 assisted TLB flush

PATCH Superseded Wei Liu 4 participants 24 messages Feb 14, 2020 – Feb 17, 2020

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover Xen on Hyper-V: Implement L0 assisted TLB flush

Hi all

This seris is based on Roger's L0 assisted flush series.

I have done some testing against a Linux on Hyper-V in a 32-vcpu VM. All builds were done with -j32.

Building Xen on Linux:

real    0m45.376s
user    2m28.156s
sys     0m51.672s

Building Xen on Linux on Xen on Hyper-V, no assisted flush:

real    3m8.762s
user    10m46.787s
sys     30m14.492s

Building Xen on Linux on Xen on Hyper-V, with assisted flush:

real    0m44.369s
user    3m16.231s
sys     3m3.330s

Building Linux x86_64_defconfig on Linux:

real    0m59.698s
user    21m14.014s
sys     2m58.742s

Building Linux x86_64_defconfig on Linux on Xen on Hyper-V, no assisted flush:

real    2m6.284s
user    31m18.706s
sys     20m31.106s

Building Linux x86_64_defconfig on Linux on Xen on Hyper-V, with assisted flush:

real    1m38.968s
user    28m40.398s
sys     11m20.151s

There are various degrees of improvement depending on the workload. Xen can perhaps be optmised a bit more because it currently doesn't pass the address space id (cr3) to Hyper-V, but that requires reworking TLB flush APIs within Xen.

Wei.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monné <[email protected]>
Cc Michael Kelley <[email protected]>
Cc Paul Durrant <[email protected]>

Wei Liu (3):
x86/hypervisor: pass flags to hypervisor_flush_tlb x86/hyperv: skeleton for L0 assisted TLB flush x86/hyperv: L0 assisted TLB flush

 xen/arch/x86/guest/hyperv/Makefile     |   2 +
 xen/arch/x86/guest/hyperv/hyperv.c     |  17 ++
 xen/arch/x86/guest/hyperv/private.h    |  13 ++
 xen/arch/x86/guest/hyperv/tlb.c        | 211 +++++++++++++++++++++++++
 xen/arch/x86/guest/hyperv/util.c       |  74 +++++++++
 xen/arch/x86/guest/hypervisor.c        |   7 +-
 xen/arch/x86/guest/xen/xen.c           |   2 +-
 xen/arch/x86/smp.c                     |   5 +-
 xen/include/asm-x86/flushtlb.h         |   3 +
 xen/include/asm-x86/guest/hypervisor.h |  10 +-
 10 files changed, 333 insertions(+), 11 deletions(-)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
 create mode 100644 xen/arch/x86/guest/hyperv/util.c
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#1 lore raw

PATCH v2 1/3 x86/hypervisor: pass flags to hypervisor_flush_tlb

Hyper-V's L0 assisted flush has fine-grained control over what gets flushed. We need all the flags available to make the best decisions possible.

No functional change because Xen's implementation doesn't care about what is passed to it.

---
v2:
1. Introduce FLUSH_TLB_FLAGS_MASK

---
 xen/arch/x86/guest/hypervisor.c        |  7 +++++--
 xen/arch/x86/guest/xen/xen.c           |  2 +-
 xen/arch/x86/smp.c                     |  5 ++---
 xen/include/asm-x86/flushtlb.h         |  3 +++
 xen/include/asm-x86/guest/hypervisor.h | 10 +++++-----
 5 files changed, 16 insertions(+), 11 deletions(-)
xen/arch/x86/guest/hypervisor.c +5 -2
index 47e938e287..6ee28c9df1 100644
@@ -75,10 +75,13 @@ void __init hypervisor_e820_fixup(struct e820map *e820)
 }
 
 int hypervisor_flush_tlb(const cpumask_t *mask, const void *va,
-                         unsigned int order)
+                         unsigned int flags)
 {
+    if ( flags & ~FLUSH_TLB_FLAGS_MASK )
+        return -EINVAL;
+
     if ( ops.flush_tlb )
-        return alternative_call(ops.flush_tlb, mask, va, order);
+        return alternative_call(ops.flush_tlb, mask, va, flags);
 
     return -ENOSYS;
 }
xen/arch/x86/guest/xen/xen.c +1 -1
index 5d3427a713..0eb1115c4d 100644
@@ -324,7 +324,7 @@ static void __init e820_fixup(struct e820map *e820)
         pv_shim_fixup_e820(e820);
 }
 
-static int flush_tlb(const cpumask_t *mask, const void *va, unsigned int order)
+static int flush_tlb(const cpumask_t *mask, const void *va, unsigned int flags)
 {
     return xen_hypercall_hvm_op(HVMOP_flush_tlbs, NULL);
 }
xen/arch/x86/smp.c +2 -3
index 9bc925616a..2ab0e30eef 100644
@@ -258,9 +258,8 @@ void flush_area_mask(const cpumask_t *mask, const void *va, unsigned int flags)
          !cpumask_subset(mask, cpumask_of(cpu)) )
     {
         if ( cpu_has_hypervisor &&
-             !(flags & ~(FLUSH_TLB | FLUSH_TLB_GLOBAL | FLUSH_VA_VALID |
-                         FLUSH_ORDER_MASK)) &&
-             !hypervisor_flush_tlb(mask, va, flags & FLUSH_ORDER_MASK) )
+             !(flags & ~FLUSH_TLB_FLAGS_MASK) &&
+             !hypervisor_flush_tlb(mask, va, flags) )
         {
             if ( tlb_clk_enabled )
                 tlb_clk_enabled = false;
xen/include/asm-x86/flushtlb.h +3 -0
index 9773014320..a4de317452 100644
@@ -123,6 +123,9 @@ void switch_cr3_cr4(unsigned long cr3, unsigned long cr4);
  /* Flush all HVM guests linear TLB (using ASID/VPID) */
 #define FLUSH_GUESTS_TLB 0x4000
 
+#define FLUSH_TLB_FLAGS_MASK (FLUSH_TLB | FLUSH_TLB_GLOBAL | FLUSH_VA_VALID | \
+                              FLUSH_ORDER_MASK)
+
 /* Flush local TLBs/caches. */
 unsigned int flush_area_local(const void *va, unsigned int flags);
 #define flush_local(flags) flush_area_local(NULL, flags)
xen/include/asm-x86/guest/hypervisor.h +5 -5
index 432e57c2a0..48d54735d2 100644
@@ -35,7 +35,7 @@ struct hypervisor_ops {
     /* Fix up e820 map */
     void (*e820_fixup)(struct e820map *e820);
     /* L0 assisted TLB flush */
-    int (*flush_tlb)(const cpumask_t *mask, const void *va, unsigned int order);
+    int (*flush_tlb)(const cpumask_t *mask, const void *va, unsigned int flags);
 };
 
 #ifdef CONFIG_GUEST
@@ -48,11 +48,11 @@ void hypervisor_e820_fixup(struct e820map *e820);
 /*
  * L0 assisted TLB flush.
  * mask: cpumask of the dirty vCPUs that should be flushed.
- * va: linear address to flush, or NULL for global flushes.
- * order: order of the linear address pointed by va.
+ * va: linear address to flush, or NULL for entire address space.
+ * flags: flags for flushing, including the order of va.
  */
 int hypervisor_flush_tlb(const cpumask_t *mask, const void *va,
-                         unsigned int order);
+                         unsigned int flags);
 
 #else
 
@@ -65,7 +65,7 @@ static inline int hypervisor_ap_setup(void) { return 0; }
 static inline void hypervisor_resume(void) { ASSERT_UNREACHABLE(); }
 static inline void hypervisor_e820_fixup(struct e820map *e820) {}
 static inline int hypervisor_flush_tlb(const cpumask_t *mask, const void *va,
-                                       unsigned int order)
+                                       unsigned int flags)
 {
     return -ENOSYS;
 }
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/3
#2 lore raw

Re: [Xen-devel] [PATCH v2 1/3] x86/hypervisor: pass flags to hypervisor_flush_tlb

Quoted 8 lines · Wei Liu

Hyper-V's L0 assisted flush has fine-grained control over what gets flushed. We need all the flags available to make the best decisions possible.

No functional change because Xen's implementation doesn't care about what is passed to it.

LGTM:

Just one comment below.

Quoted 16 lines

---
v2:
1. Introduce FLUSH_TLB_FLAGS_MASK

---
 xen/arch/x86/guest/hypervisor.c        |  7 +++++--
 xen/arch/x86/guest/xen/xen.c           |  2 +-
 xen/arch/x86/smp.c                     |  5 ++---
 xen/include/asm-x86/flushtlb.h         |  3 +++
 xen/include/asm-x86/guest/hypervisor.h | 10 +++++-----
 5 files changed, 16 insertions(+), 11 deletions(-)
xen/arch/x86/guest/hypervisor.c +0 -0
index 47e938e287..6ee28c9df1 100644

xen/arch/x86/guest/hypervisor.c void __init hypervisor_e820_fixup(struct e820map *e820) · line 80

Quoted 7 lines
xen/arch/x86/guest/hypervisor.c +2 -1
@@ -75,10 +75,13 @@ void __init hypervisor_e820_fixup(struct e820map *e820)
 }
 
 int hypervisor_flush_tlb(const cpumask_t *mask, const void *va,
-                         unsigned int order)
+                         unsigned int flags)
 {
+    if ( flags & ~FLUSH_TLB_FLAGS_MASK )

I think an ASSERT_UNREACHABLE() would be good here, since you are not supposed to call hypervisor_flush_tlb with non TLB related flags.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by Paul Durrant on v2 1/3
#3 lore raw

Re: [Xen-devel] [PATCH v2 1/3] x86/hypervisor: pass flags to hypervisor_flush_tlb

In reply to Wei Liu
Quoted 10 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 1/3] x86/hypervisor: pass flags to hypervisor_flush_tlb

Quoted 8 lines

Hyper-V's L0 assisted flush has fine-grained control over what gets flushed. We need all the flags available to make the best decisions possible.

No functional change because Xen's implementation doesn't care about what is passed to it.

Quoted 123 lines

---
v2:
1. Introduce FLUSH_TLB_FLAGS_MASK

---
 xen/arch/x86/guest/hypervisor.c        |  7 +++++--
 xen/arch/x86/guest/xen/xen.c           |  2 +-
 xen/arch/x86/smp.c                     |  5 ++---
 xen/include/asm-x86/flushtlb.h         |  3 +++
 xen/include/asm-x86/guest/hypervisor.h | 10 +++++-----
 5 files changed, 16 insertions(+), 11 deletions(-)
xen/arch/x86/guest/hypervisor.c +5 -2
index 47e938e287..6ee28c9df1 100644
@@ -75,10 +75,13 @@ void __init hypervisor_e820_fixup(struct e820map
 *e820)
 }
 
 int hypervisor_flush_tlb(const cpumask_t *mask, const void *va,
-                         unsigned int order)
+                         unsigned int flags)
 {
+    if ( flags & ~FLUSH_TLB_FLAGS_MASK )
+        return -EINVAL;
+
     if ( ops.flush_tlb )
-        return alternative_call(ops.flush_tlb, mask, va, order);
+        return alternative_call(ops.flush_tlb, mask, va, flags);
 
     return -ENOSYS;

}

xen/arch/x86/guest/xen/xen.c +1 -1
index 5d3427a713..0eb1115c4d 100644
@@ -324,7 +324,7 @@ static void __init e820_fixup(struct e820map *e820)
         pv_shim_fixup_e820(e820);
 }
 
-static int flush_tlb(const cpumask_t *mask, const void *va, unsigned int
 order)
+static int flush_tlb(const cpumask_t *mask, const void *va, unsigned int
 flags)
 {
     return xen_hypercall_hvm_op(HVMOP_flush_tlbs, NULL);

}

xen/arch/x86/smp.c +2 -3
index 9bc925616a..2ab0e30eef 100644
@@ -258,9 +258,8 @@ void flush_area_mask(const cpumask_t *mask, const void
 *va, unsigned int flags)
          !cpumask_subset(mask, cpumask_of(cpu)) )
     {
         if ( cpu_has_hypervisor &&
-             !(flags & ~(FLUSH_TLB | FLUSH_TLB_GLOBAL | FLUSH_VA_VALID |
-                         FLUSH_ORDER_MASK)) &&
-             !hypervisor_flush_tlb(mask, va, flags & FLUSH_ORDER_MASK) )
+             !(flags & ~FLUSH_TLB_FLAGS_MASK) &&
+             !hypervisor_flush_tlb(mask, va, flags) )
         {
             if ( tlb_clk_enabled )
                 tlb_clk_enabled = false;
xen/include/asm- renamed from xen/include/asm-x86/flushtlb.h +0 -0

x86/flushtlb.h
index 9773014320..a4de317452 100644

xen/include/asm-x86/flushtlb.h +3 -0
@@ -123,6 +123,9 @@ void switch_cr3_cr4(unsigned long cr3, unsigned long
 cr4);
  /* Flush all HVM guests linear TLB (using ASID/VPID) */
 #define FLUSH_GUESTS_TLB 0x4000
 
+#define FLUSH_TLB_FLAGS_MASK (FLUSH_TLB | FLUSH_TLB_GLOBAL |
 FLUSH_VA_VALID | \
+                              FLUSH_ORDER_MASK)
+
 /* Flush local TLBs/caches. */

unsigned int flush_area_local(const void *va, unsigned int flags); #define flush_local(flags) flush_area_local(NULL, flags)

xen/include/asm- renamed from xen/include/asm-x86/guest/hypervisor.h +0 -0

x86/guest/hypervisor.h
index 432e57c2a0..48d54735d2 100644

xen/include/asm-x86/guest/hypervisor.h +1 -1
@@ -35,7 +35,7 @@ struct hypervisor_ops {
     /* Fix up e820 map */
     void (*e820_fixup)(struct e820map *e820);
     /* L0 assisted TLB flush */
-    int (*flush_tlb)(const cpumask_t *mask, const void *va, unsigned int
 order);
+    int (*flush_tlb)(const cpumask_t *mask, const void *va, unsigned int
 flags);
 };

#ifdef CONFIG_GUEST

xen/include/asm-x86/guest/hypervisor.h +4 -4
@@ -48,11 +48,11 @@ void hypervisor_e820_fixup(struct e820map *e820);
 /*
  * L0 assisted TLB flush.
  * mask: cpumask of the dirty vCPUs that should be flushed.
- * va: linear address to flush, or NULL for global flushes.
- * order: order of the linear address pointed by va.
+ * va: linear address to flush, or NULL for entire address space.
+ * flags: flags for flushing, including the order of va.
  */
 int hypervisor_flush_tlb(const cpumask_t *mask, const void *va,
-                         unsigned int order);
+                         unsigned int flags);
 
 #else
 
@@ -65,7 +65,7 @@ static inline int hypervisor_ap_setup(void) { return 0;
 }
 static inline void hypervisor_resume(void) { ASSERT_UNREACHABLE(); }
 static inline void hypervisor_e820_fixup(struct e820map *e820) {}
 static inline int hypervisor_flush_tlb(const cpumask_t *mask, const void
 *va,
-                                       unsigned int order)
+                                       unsigned int flags)
 {
     return -ENOSYS;
Quoted 3 lines

}
--
2.20.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#4 lore raw

PATCH v2 2/3 x86/hyperv: skeleton for L0 assisted TLB flush

In reply to Wei Liu

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +17 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);
+
+    hv_hcall_page_ready = true;
 }
 
 static int setup_hypercall_pcpu_arg(void)
@@ -199,11 +203,24 @@ static void __init e820_fixup(struct e820map *e820)
         panic("Unable to reserve Hyper-V hypercall range\n");
 }
 
+static int flush_tlb(const cpumask_t *mask, const void *va,
+                     unsigned int flags)
+{
+    if ( !(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED) )
+        return -EOPNOTSUPP;
+
+    if ( !hv_hcall_page_ready || !this_cpu(hv_input_page) )
+        return -ENXIO;
+
+    return hyperv_flush_tlb(mask, va, flags);
+}
+
 static const struct hypervisor_ops __initdata ops = {
     .name = "Hyper-V",
     .setup = setup,
     .ap_setup = ap_setup,
     .e820_fixup = e820_fixup,
+    .flush_tlb = flush_tlb,
 };
 
 /*
xen/arch/x86/guest/hyperv/private.h +4 -0
index 956eff831f..509bedaafa 100644
@@ -22,10 +22,14 @@
 #ifndef __XEN_HYPERV_PRIVIATE_H__
 #define __XEN_HYPERV_PRIVIATE_H__
 
+#include <xen/cpumask.h>
 #include <xen/percpu.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
 DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
+int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
+                     unsigned int flags);
+
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
xen/arch/x86/guest/hyperv/tlb.c new file +41 -0

41 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 2/3
#5 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 7 lines · Wei Liu

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

Just one comment below.

Quoted 20 lines
---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
index 70f4cd5ae0..f9d1f11ae3 100644

xen/arch/x86/guest/hyperv/hyperv.c DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page); · line 36

Quoted 4 lines
xen/arch/x86/guest/hyperv/hyperv.c +1 -0
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;

Since this is static, I would drop the hv_ prefix.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

In reply to Wei Liu
Quoted 48 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 3 lines
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

Paul

Quoted 100 lines
xen/arch/x86/guest/hyperv/hyperv.c +15 -0
+
+    hv_hcall_page_ready = true;
 }
 
 static int setup_hypercall_pcpu_arg(void)
@@ -199,11 +203,24 @@ static void __init e820_fixup(struct e820map *e820)
         panic("Unable to reserve Hyper-V hypercall range\n");
 }
 
+static int flush_tlb(const cpumask_t *mask, const void *va,
+                     unsigned int flags)
+{
+    if ( !(ms_hyperv.hints & HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED) )
+        return -EOPNOTSUPP;
+
+    if ( !hv_hcall_page_ready || !this_cpu(hv_input_page) )
+        return -ENXIO;
+
+    return hyperv_flush_tlb(mask, va, flags);
+}
+
 static const struct hypervisor_ops __initdata ops = {
     .name = "Hyper-V",
     .setup = setup,
     .ap_setup = ap_setup,
     .e820_fixup = e820_fixup,
+    .flush_tlb = flush_tlb,
 };
 
 /*
xen/arch/x86/guest/hyperv/private.h +4 -0
index 956eff831f..509bedaafa 100644
@@ -22,10 +22,14 @@
 #ifndef __XEN_HYPERV_PRIVIATE_H__
 #define __XEN_HYPERV_PRIVIATE_H__
 
+#include <xen/cpumask.h>
 #include <xen/percpu.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
 DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
+int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
+                     unsigned int flags);
+
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
xen/arch/x86/guest/hyperv/tlb.c new file +1 -0
new file mode 100644
index 0000000000..48f527229e
@@ -0,0 +1,41 @@
+/************************************************************************

******
+ * arch/x86/guest/hyperv/tlb.c
+ *
+ * Support for TLB management using hypercalls
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2020 Microsoft.
+ */

xen/arch/x86/guest/hyperv/tlb.c new file +21 -1
+
+#include <xen/cpumask.h>
+#include <xen/errno.h>
+
+#include "private.h"
+
+int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
+                     unsigned int flags)
+{
+    return -EOPNOTSUPP;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--
Quoted 1 line

2.20.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#7 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 50 lines · Paul
Quoted 50 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 51 lines · Wei Liu
Quoted 50 lines · Paul
Quoted 50 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 6 lines
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Note that map_pages_to_xen already performs atomic writes.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#9 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 57 lines · Roger Pau Monné
Quoted 56 lines · Wei Liu
Quoted 54 lines · Paul
Quoted 48 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 3 lines
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

Quoted 5 lines
Quoted 1 line

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Note that map_pages_to_xen already performs atomic writes.

I don't mind making things more explicit though. However unlikely, I may end up putting something in between set_fixmap_x and setting hcall_page_ready, I will need the barrier by then, I may as well put it in now.

Wei.

Quoted 2 lines

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 60 lines · Wei Liu
Quoted 59 lines · Roger Pau Monné
Quoted 57 lines · Wei Liu
Quoted 50 lines · Paul
Quoted 50 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Quoted 8 lines
Quoted 3 lines

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Note that map_pages_to_xen already performs atomic writes.

I don't mind making things more explicit though. However unlikely, I may end up putting something in between set_fixmap_x and setting hcall_page_ready, I will need the barrier by then, I may as well put it in now.

IMO set_fixmap_x should have the necessary barriers (or other synchronization methods) so that on return the address is correctly mapped across all processors, and that it prevents the compiler from moving accesses past it. I would consider a bug of set_fixmap_x not having this behavior and requiring callers to do extra work in order to ensure this.

Ie: something like the snipped below should not require an extra barrier IMO:

set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT); *((unsigned int *)fix_x_to_virt(FIX_X_HYPERV_HCALL)) = 0;

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#11 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 77 lines · Roger Pau Monné
Quoted 60 lines · Wei Liu
Quoted 59 lines · Roger Pau Monné
Quoted 57 lines · Wei Liu
Quoted 50 lines · Paul
Quoted 50 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Quoted 8 lines
Quoted 3 lines

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Note that map_pages_to_xen already performs atomic writes.

I don't mind making things more explicit though. However unlikely, I may end up putting something in between set_fixmap_x and setting hcall_page_ready, I will need the barrier by then, I may as well put it in now.

IMO set_fixmap_x should have the necessary barriers (or other synchronization methods) so that on return the address is correctly mapped across all processors, and that it prevents the compiler from moving accesses past it. I would consider a bug of set_fixmap_x not having this behavior and requiring callers to do extra work in order to ensure this.

Quoted 5 lines

Ie: something like the snipped below should not require an extra barrier IMO:

set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT); *((unsigned int *)fix_x_to_virt(FIX_X_HYPERV_HCALL)) = 0;

That's different though. Compiler can't make the connection between hcall_page_ready and the address returned by set_fixmap_x.

Wei.

Quoted 2 lines

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 78 lines · Wei Liu
Quoted 77 lines · Roger Pau Monné
Quoted 60 lines · Wei Liu
Quoted 59 lines · Roger Pau Monné
Quoted 57 lines · Wei Liu
Quoted 50 lines · Paul
Quoted 50 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Quoted 8 lines
Quoted 3 lines

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Note that map_pages_to_xen already performs atomic writes.

I don't mind making things more explicit though. However unlikely, I may end up putting something in between set_fixmap_x and setting hcall_page_ready, I will need the barrier by then, I may as well put it in now.

IMO set_fixmap_x should have the necessary barriers (or other synchronization methods) so that on return the address is correctly mapped across all processors, and that it prevents the compiler from moving accesses past it. I would consider a bug of set_fixmap_x not having this behavior and requiring callers to do extra work in order to ensure this.

Quoted 8 lines
Quoted 5 lines

Ie: something like the snipped below should not require an extra barrier IMO:

set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT); *((unsigned int *)fix_x_to_virt(FIX_X_HYPERV_HCALL)) = 0;

That's different though. Compiler can't make the connection between hcall_page_ready and the address returned by set_fixmap_x.

I'm not sure the compiler can make a connection between set_fixmap_x and fix_x_to_virt either (as fix_x_to_virt is a simple mathematical operation and FIX_X_HYPERV_HCALL is a constant known at build time).

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#13 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 85 lines · Roger Pau Monné
Quoted 84 lines · Wei Liu
Quoted 82 lines · Roger Pau Monné
Quoted 60 lines · Wei Liu
Quoted 59 lines · Roger Pau Monné
Quoted 57 lines · Wei Liu
Quoted 50 lines · Paul
Quoted 50 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an error number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41 +++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Quoted 8 lines
Quoted 3 lines

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Note that map_pages_to_xen already performs atomic writes.

I don't mind making things more explicit though. However unlikely, I may end up putting something in between set_fixmap_x and setting hcall_page_ready, I will need the barrier by then, I may as well put it in now.

IMO set_fixmap_x should have the necessary barriers (or other synchronization methods) so that on return the address is correctly mapped across all processors, and that it prevents the compiler from moving accesses past it. I would consider a bug of set_fixmap_x not having this behavior and requiring callers to do extra work in order to ensure this.

Ie: something like the snipped below should not require an extra barrier IMO:

set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT); *((unsigned int *)fix_x_to_virt(FIX_X_HYPERV_HCALL)) = 0;

Quoted 6 lines
Quoted 2 lines

That's different though. Compiler can't make the connection between hcall_page_ready and the address returned by set_fixmap_x.

I'm not sure the compiler can make a connection between set_fixmap_x and fix_x_to_virt either (as fix_x_to_virt is a simple mathematical operation and FIX_X_HYPERV_HCALL is a constant known at build time).

Oh, I misread your example, sorry.

Wei.

Quoted 2 lines

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

In reply to Roger Pau Monné
Quoted 65 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 11:41
To: Wei Liu <[email protected]>
Cc: Durrant, Paul <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 10 lines · Wei Liu
Quoted 9 lines · Paul
Quoted 9 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an

error

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41

+++++++++++++++++++++++++++++

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 1 line
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Not really, for the purpose I had in mind. The hv_hcall_page_ready global is specific to this code and we need to make sure the page is actually ready before the code says it is.

Paul

Quoted 3 lines

Note that map_pages_to_xen already performs atomic writes.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 70 lines · Paul
Quoted 70 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 11:41
To: Wei Liu <[email protected]>
Cc: Durrant, Paul <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 10 lines · Wei Liu
Quoted 9 lines · Paul
Quoted 9 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Implement a basic hook for L0 assisted TLB flush. The hook needs to check if prerequisites are met. If they are not met, it returns an

error

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41

+++++++++++++++++++++++++++++

Quoted 33 lines
Quoted 32 lines
Quoted 26 lines
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *, hv_input_page);
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 3 lines
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
@@ -119,6 +121,8 @@ static void __init setup_hypercall_page(void)
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Not really, for the purpose I had in mind. The hv_hcall_page_ready global is specific to this code and we need to make sure the page is actually ready before the code says it is.

But anything that modifies the page tables should already have a barrier if required in order to prevent accesses from being moved ahead of it, or else things would certainly go wrong in many other places?

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 94 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 12:08
To: Durrant, Paul <[email protected]>
Cc: Wei Liu <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 6 lines · Paul
Quoted 6 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 11:41
To: Wei Liu <[email protected]>
Cc: Durrant, Paul <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>;

Wei

Quoted 1 line
Quoted 1 line

Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew

Cooper

Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 11 lines
Quoted 11 lines · Wei Liu
Quoted 9 lines · Paul
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné

<[email protected]>

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Implement a basic hook for L0 assisted TLB flush. The hook needs

to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

check if prerequisites are met. If they are not met, it returns

an

Quoted 15 lines
Quoted 15 lines

error

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41

+++++++++++++++++++++++++++++

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/guest/hyperv/Makefile +0 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/guest/hyperv/hyperv.c DEFINE_PER_CPU_READ_MOSTLY(void *,

Quoted 8 lines
xen/arch/x86/guest/hyperv/Makefile +1 -0
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *,

hv_input_page);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init

Quoted 9 lines
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init

setup_hypercall_page(void)

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init setup_hypercall_page(void) · line 123

Quoted 3 lines
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Quoted 8 lines
Quoted 1 line

Not really, for the purpose I had in mind. The hv_hcall_page_ready

global is specific to this code and we need to make sure the page is actually ready before the code says it is.

But anything that modifies the page tables should already have a barrier if required in order to prevent accesses from being moved ahead of it, or else things would certainly go wrong in many other places?

Oh. I'm not saying that we don't need a barrier there too (and more than a compiler one in that case).

Paul

Quoted 2 lines

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#17 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 98 lines · Paul
Quoted 98 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 12:08
To: Durrant, Paul <[email protected]>
Cc: Wei Liu <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 6 lines · Paul
Quoted 6 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 11:41
To: Wei Liu <[email protected]>
Cc: Durrant, Paul <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>;

Wei

Quoted 1 line
Quoted 1 line

Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew

Cooper

Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 11 lines
Quoted 11 lines · Wei Liu
Quoted 9 lines · Paul
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné

<[email protected]>

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Implement a basic hook for L0 assisted TLB flush. The hook needs

to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

check if prerequisites are met. If they are not met, it returns

an

Quoted 15 lines
Quoted 15 lines

error

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is ready.

---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41

+++++++++++++++++++++++++++++

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/guest/hyperv/Makefile +0 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/guest/hyperv/hyperv.c DEFINE_PER_CPU_READ_MOSTLY(void *,

Quoted 8 lines
xen/arch/x86/guest/hyperv/Makefile +1 -0
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *,

hv_input_page);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init

Quoted 9 lines
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init

setup_hypercall_page(void)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init · line 123

Quoted 3 lines
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Not really, for the purpose I had in mind. The hv_hcall_page_ready

global is specific to this code and we need to make sure the page is actually ready before the code says it is.

Quoted 8 lines
Quoted 4 lines

But anything that modifies the page tables should already have a barrier if required in order to prevent accesses from being moved ahead of it, or else things would certainly go wrong in many other places?

Oh. I'm not saying that we don't need a barrier there too (and more than a compiler one in that case).

The argument Roger has is that set_fixmap_x also contains strong enough barriers to prevent hcall_page_ready to be set before page table is correctly set up.

Since you asked for it, there must be something on your mind that prompted this (maybe it is simply because you were bitten by similar things and wants to be extra sure, maybe you think it is harder to grasp the side effect of set_fixmap_x, maybe something else).

Code is written to be read by humans after all. I would rather be more explicit / redundant to make humans happy than to save a potential barrier / some typing in a code path.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [Xen-devel] [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 133 lines

-----Original Message-----
From: Wei Liu <[email protected]>
Sent: 17 February 2020 12:48
To: Durrant, Paul <[email protected]>
Cc: Roger Pau Monné <[email protected]>; Wei Liu <[email protected]>; Xen Development List <[email protected]>; Michael Kelley <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB flush

Quoted 6 lines · Paul
Quoted 6 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 12:08
To: Durrant, Paul <[email protected]>
Cc: Wei Liu <[email protected]>; Xen Development List <xen- [email protected]>; Michael Kelley <[email protected]>;

Wei

Quoted 1 line
Quoted 1 line

Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew

Cooper

Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted TLB

flush

Quoted 7 lines
Quoted 7 lines · Paul
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Roger Pau Monné <[email protected]>
Sent: 17 February 2020 11:41
To: Wei Liu <[email protected]>
Cc: Durrant, Paul <[email protected]>; Xen Development List

<xen-

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]>; Michael Kelley

<[email protected]>;

Quoted 5 lines
Quoted 5 lines

Wei

Quoted 1 line
Quoted 1 line

Liu <[email protected]>; Jan Beulich <[email protected]>; Andrew

Cooper

Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: Re: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted

TLB

Quoted 10 lines
Quoted 10 lines

flush

Quoted 9 lines
Quoted 9 lines · Wei Liu
Quoted 7 lines · Paul
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Wei Liu <[email protected]> On Behalf Of Wei Liu
Sent: 14 February 2020 13:34
To: Xen Development List <[email protected]>
Cc: Michael Kelley <[email protected]>; Durrant, Paul <[email protected]>; Wei Liu <[email protected]>; Wei

Liu

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné

<[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Subject: [PATCH v2 2/3] x86/hyperv: skeleton for L0 assisted

TLB

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

flush

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Implement a basic hook for L0 assisted TLB flush. The hook

needs

Quoted 2 lines
Quoted 2 lines

to

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

check if prerequisites are met. If they are not met, it

returns

Quoted 5 lines
Quoted 5 lines

an

Quoted 4 lines
Quoted 4 lines

error

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

number to fall back to native flushes.

Introduce a new variable to indicate if hypercall page is

ready.

Quoted 52 lines
Quoted 52 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
---
 xen/arch/x86/guest/hyperv/Makefile  |  1 +
 xen/arch/x86/guest/hyperv/hyperv.c  | 17 ++++++++++++
 xen/arch/x86/guest/hyperv/private.h |  4 +++
 xen/arch/x86/guest/hyperv/tlb.c     | 41

+++++++++++++++++++++++++++++

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
 4 files changed, 63 insertions(+)
 create mode 100644 xen/arch/x86/guest/hyperv/tlb.c
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/guest/hyperv/Makefile +0 -0
index 68170109a9..18902c33e9 100644
@@ -1 +1,2 @@
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/guest/hyperv/hyperv.c DEFINE_PER_CPU_READ_MOSTLY(void *,

Quoted 8 lines
xen/arch/x86/guest/hyperv/Makefile +1 -0
 obj-y += hyperv.o
+obj-y += tlb.o
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
index 70f4cd5ae0..f9d1f11ae3 100644
@@ -33,6 +33,8 @@ DEFINE_PER_CPU_READ_MOSTLY(void *,

hv_input_page);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init

Quoted 9 lines
xen/arch/x86/guest/hyperv/hyperv.c +2 -0
 DEFINE_PER_CPU_READ_MOSTLY(void *, hv_vp_assist);
 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, hv_vp_index);
 
+static bool __read_mostly hv_hcall_page_ready;
+
 static uint64_t generate_guest_id(void)
 {
     union hv_guest_os_id id = {};
@@ -119,6 +121,8 @@ static void __init

setup_hypercall_page(void)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

xen/arch/x86/guest/hyperv/hyperv.c static void __init · line 123

Quoted 3 lines
xen/arch/x86/guest/hyperv/hyperv.c +0 -0
     BUG_ON(!hypercall_msr.enable);
 
     set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT);

Shouldn't this have at least a compiler barrier here?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

OK. I will add a write barrier here.

Hm, shouldn't such barrier be part of set_fixmap_x itself?

Not really, for the purpose I had in mind. The hv_hcall_page_ready

global is specific to this code and we need to make sure the page is actually ready before the code says it is.

Quoted 8 lines
Quoted 4 lines

But anything that modifies the page tables should already have a barrier if required in order to prevent accesses from being moved ahead of it, or else things would certainly go wrong in many other places?

Oh. I'm not saying that we don't need a barrier there too (and more than a compiler one in that case).

The argument Roger has is that set_fixmap_x also contains strong enough barriers to prevent hcall_page_ready to be set before page table is correctly set up.

Quoted 8 lines

Since you asked for it, there must be something on your mind that prompted this (maybe it is simply because you were bitten by similar things and wants to be extra sure, maybe you think it is harder to grasp the side effect of set_fixmap_x, maybe something else).

Code is written to be read by humans after all. I would rather be more explicit / redundant to make humans happy than to save a potential barrier / some typing in a code path.

If set_fixmap_x() is a barriering operation, and that is reasonably obvious (from comments or cursory examination of the code) then that's fine. I have indeed been bitten by this kind of thing in writing PV drivers so just wanted to ensure we didn't run into it here.

Paul

Quoted 2 lines

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#19 lore raw

PATCH v2 3/3 x86/hyperv: L0 assisted TLB flush

In reply to Wei Liu

Implement L0 assisted TLB flush for Xen on Hyper-V. It takes advantage of several hypercalls:

* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST
* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX

Pick the most efficient hypercalls available.

---
v2:
1. Address Roger and Jan's comments re types etc.
2. Fix pointer arithmetic.
3. Misc improvement to code.

---
 xen/arch/x86/guest/hyperv/Makefile  |   1 +
 xen/arch/x86/guest/hyperv/private.h |   9 ++
 xen/arch/x86/guest/hyperv/tlb.c     | 172 +++++++++++++++++++++++++++-
 xen/arch/x86/guest/hyperv/util.c    |  74 ++++++++++++
 4 files changed, 255 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/x86/guest/hyperv/util.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 18902c33e9..0e39410968 100644
@@ -1,2 +1,3 @@
 obj-y += hyperv.o
 obj-y += tlb.o
+obj-y += util.o
xen/arch/x86/guest/hyperv/private.h +9 -0
index 509bedaafa..79a77930a0 100644
@@ -24,12 +24,21 @@
 
 #include <xen/cpumask.h>
 #include <xen/percpu.h>
+#include <xen/types.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
 DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
+static inline unsigned int hv_vp_index(unsigned int cpu)
+{
+    return per_cpu(hv_vp_index, cpu);
+}
+
 int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
                      unsigned int flags);
 
+/* Returns number of banks, -ev if error */
+int cpumask_to_vpset(struct hv_vpset *vpset, const cpumask_t *mask);
+
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
xen/arch/x86/guest/hyperv/tlb.c +171 -1

186 lines not on this page · raw message

xen/arch/x86/guest/hyperv/util.c new file +74 -0

74 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#20 lore raw

Re: [Xen-devel] [PATCH v2 3/3] x86/hyperv: L0 assisted TLB flush

Quoted 11 lines · Wei Liu

Implement L0 assisted TLB flush for Xen on Hyper-V. It takes advantage of several hypercalls:

* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST
* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX

Pick the most efficient hypercalls available.

Thanks! LGTM, I've just got a couple of comments below.

Quoted 83 lines

---
v2:
1. Address Roger and Jan's comments re types etc.
2. Fix pointer arithmetic.
3. Misc improvement to code.

---
 xen/arch/x86/guest/hyperv/Makefile  |   1 +
 xen/arch/x86/guest/hyperv/private.h |   9 ++
 xen/arch/x86/guest/hyperv/tlb.c     | 172 +++++++++++++++++++++++++++-
 xen/arch/x86/guest/hyperv/util.c    |  74 ++++++++++++
 4 files changed, 255 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/x86/guest/hyperv/util.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 18902c33e9..0e39410968 100644
@@ -1,2 +1,3 @@
 obj-y += hyperv.o
 obj-y += tlb.o
+obj-y += util.o
xen/arch/x86/guest/hyperv/private.h +9 -0
index 509bedaafa..79a77930a0 100644
@@ -24,12 +24,21 @@
 
 #include <xen/cpumask.h>
 #include <xen/percpu.h>
+#include <xen/types.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
 DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
+static inline unsigned int hv_vp_index(unsigned int cpu)
+{
+    return per_cpu(hv_vp_index, cpu);
+}
+
 int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
                      unsigned int flags);
 
+/* Returns number of banks, -ev if error */
+int cpumask_to_vpset(struct hv_vpset *vpset, const cpumask_t *mask);
+
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
xen/arch/x86/guest/hyperv/tlb.c +23 -0
index 48f527229e..f68e14f151 100644
@@ -19,15 +19,185 @@
  * Copyright (c) 2020 Microsoft.
  */
 
+#include <xen/cpu.h>
 #include <xen/cpumask.h>
 #include <xen/errno.h>
 
+#include <asm/guest/hyperv.h>
+#include <asm/guest/hyperv-hcall.h>
+#include <asm/guest/hyperv-tlfs.h>
+
 #include "private.h"
 
+/*
+ * It is possible to encode up to 4096 pages using the lower 12 bits
+ * in an element of gva_list
+ */
+#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
+
+static unsigned int fill_gva_list(uint64_t *gva_list, const void *va,
+                                  unsigned int order)
+{
+    unsigned long start = (unsigned long)va;
+    unsigned long end = start + (PAGE_SIZE << order) - 1;
+    unsigned int n = 0;
+
+    do {
+        unsigned long remain = end - start;
+
+        gva_list[n] = start & PAGE_MASK;
+

xen/arch/x86/guest/hyperv/tlb.c line 59

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+        /*
+         * Use lower 12 bits to encode the number of additional pages
+         * to flush
+         */
+        if ( remain >= HV_TLB_FLUSH_UNIT )
+        {
+            gva_list[n] |= ~PAGE_MASK;
+            start += HV_TLB_FLUSH_UNIT;
+        }
+        else if ( remain )

remain is always going to be > 0, since the loop condition is end > start, and hence this can be a plain else.

Quoted 26 lines
xen/arch/x86/guest/hyperv/tlb.c +26 -0
+        {
+            gva_list[n] |= (remain - 1) >> PAGE_SHIFT;
+            start = end;
+        }
+
+        n++;
+    } while ( start < end );
+
+    return n;
+}
+
+static uint64_t flush_tlb_ex(const cpumask_t *mask, const void *va,
+                             unsigned int flags)
+{
+    struct hv_tlb_flush_ex *flush = this_cpu(hv_input_page);
+    int nr_banks;
+    unsigned int max_gvas, order = flags & FLUSH_ORDER_MASK;
+    uint64_t ret;
+
+    if ( !flush || local_irq_is_enabled() )
+    {
+        ASSERT_UNREACHABLE();
+        return ~0ULL;
+    }
+
+    if ( !(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED) )

xen/arch/x86/guest/hyperv/tlb.c line 95

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+        return ~0ULL;
+
+    flush->address_space = 0;
+    flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
+    if ( !(flags & FLUSH_TLB_GLOBAL) )
+        flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
+
+    nr_banks = cpumask_to_vpset(&flush->hv_vp_set, mask);
+    if ( nr_banks < 0 )
+        return ~0ULL;

It would be nice to propagate the error code from cpumask_to_vpset, but since the function can also return HyperV error codes this doesn't make much sense.

xen/arch/x86/guest/hyperv/tlb.c line 108

Quoted 13 lines
xen/arch/x86/guest/hyperv/tlb.c +13 -0
+
+    max_gvas =
+        (PAGE_SIZE - sizeof(*flush) - nr_banks *
+         sizeof(flush->hv_vp_set.bank_contents[0])) /
+        sizeof(uint64_t);       /* gva is represented as uint64_t */
+
+    /*
+     * Flush the entire address space if va is NULL or if there is not
+     * enough space for gva_list.
+     */
+    if ( !va || (PAGE_SIZE << order) / HV_TLB_FLUSH_UNIT > max_gvas )
+        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX, 0,
+                                  nr_banks, virt_to_maddr(flush), 0);

You could just return hv_do_rep_hypercall(...); here, which will avoid the else branch below and the indentation.

Quoted 16 lines
xen/arch/x86/guest/hyperv/tlb.c +15 -0
+    else
+    {
+        uint64_t *gva_list =
+            (uint64_t *)flush + sizeof(*flush) / sizeof(uint64_t) + nr_banks;
+        unsigned int gvas = fill_gva_list(gva_list, va, order);
+
+        BUILD_BUG_ON(sizeof(*flush) % sizeof(uint64_t));
+
+        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
+                                  gvas, nr_banks, virt_to_maddr(flush), 0);
+    }
+
+    return ret;
+}
+
 int hyperv_flush_tlb(const cpumask_t *mask, const void *va,

xen/arch/x86/guest/hyperv/tlb.c line 133

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +7 -1
                      unsigned int flags)
 {
-    return -EOPNOTSUPP;
+    unsigned long irq_flags;
+    struct hv_tlb_flush *flush = this_cpu(hv_input_page);
+    unsigned int max_gvas, order = flags & FLUSH_ORDER_MASK;
+    uint64_t ret;
+
+    ASSERT(flush);
+    ASSERT(!cpumask_empty(mask));

I would also turn this into an if ( ... ) { ASSERT; return -EFOO; }

Quoted 17 lines
xen/arch/x86/guest/hyperv/tlb.c +17 -0
+
+    local_irq_save(irq_flags);
+
+    flush->address_space = 0;
+    flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
+    flush->processor_mask = 0;
+    if ( !(flags & FLUSH_TLB_GLOBAL) )
+        flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
+
+    if ( cpumask_equal(mask, &cpu_online_map) )
+        flush->flags |= HV_FLUSH_ALL_PROCESSORS;
+    else
+    {
+        unsigned int cpu;
+
+        /*
+         * Normally VP indices are in ascending order and match Xen's

xen/arch/x86/guest/hyperv/tlb.c line 160

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+         * idea of CPU ids. Check the last index to see if VP index is
+         * >= 64. If so, we can skip setting up parameters for
+         * non-applicable hypercalls without looking further.
+         */
+        if ( hv_vp_index(cpumask_last(mask)) >= 64 )
+            goto do_ex_hypercall;
+
+        for_each_cpu ( cpu, mask )
+        {
+            uint32_t vpid = hv_vp_index(cpu);

This should be unsigned int now.

xen/arch/x86/guest/hyperv/tlb.c line 172

Quoted 12 lines
xen/arch/x86/guest/hyperv/tlb.c +12 -0
+
+            if ( vpid > ms_hyperv.max_vp_index )
+            {
+                local_irq_restore(irq_flags);
+                return -ENXIO;
+            }
+
+            if ( vpid >= 64 )
+                goto do_ex_hypercall;
+
+            __set_bit(vpid, &flush->processor_mask);
+        }

Would it make sense to abstract this as cpumask_to_processor_mask, since you are adding cpumask_to_vpset below.

xen/arch/x86/guest/hyperv/tlb.c line 175

Quoted 3 lines
xen/arch/x86/guest/hyperv/tlb.c +3 -0
+    }
+
+    max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);

You could init this at declaration, and make it const static since the value can be calculated at compile time AFAICT. Or create a define with it (HV_TLB_FLUSH_MAX_GVAS?). There's no need to store it on the stack.

xen/arch/x86/guest/hyperv/tlb.c line 186

Quoted 11 lines
xen/arch/x86/guest/hyperv/tlb.c +11 -0
+
+    /*
+     * Flush the entire address space if va is NULL or if there is not
+     * enough space for gva_list.
+     */
+    if ( !va || (PAGE_SIZE << order) / HV_TLB_FLUSH_UNIT > max_gvas )
+        ret = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
+                              virt_to_maddr(flush), 0);
+    else
+    {
+        unsigned int gvas = fill_gva_list(flush->gva_list, va, order);

No need for the gvas variable, you can just call fill_gva_list at hv_do_rep_hypercall.

Quoted 56 lines
xen/arch/x86/guest/hyperv/tlb.c +14 -0
+
+        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST, gvas, 0,
+                                  virt_to_maddr(flush), 0);
+    }
+
+    goto done;
+
+ do_ex_hypercall:
+    ret = flush_tlb_ex(mask, va, flags);
+
+ done:
+    local_irq_restore(irq_flags);
+
+    return ret & HV_HYPERCALL_RESULT_MASK ? -ENXIO : 0;
 }
 
 /*
xen/arch/x86/guest/hyperv/util.c new file +33 -0
new file mode 100644
index 0000000000..e092593746
@@ -0,0 +1,74 @@
+/******************************************************************************
+ * arch/x86/guest/hyperv/util.c
+ *
+ * Hyper-V utility functions
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2020 Microsoft.
+ */
+
+#include <xen/cpu.h>
+#include <xen/cpumask.h>
+#include <xen/errno.h>
+
+#include <asm/guest/hyperv.h>
+#include <asm/guest/hyperv-tlfs.h>
+
+#include "private.h"
+
+int cpumask_to_vpset(struct hv_vpset *vpset,
+                     const cpumask_t *mask)
+{

xen/arch/x86/guest/hyperv/util.c line 43

Quoted 10 lines
xen/arch/x86/guest/hyperv/util.c new file +10 -0
+    int nr = 1;
+    unsigned int cpu, vcpu_bank, vcpu_offset;
+    unsigned int max_banks = ms_hyperv.max_vp_index / 64;
+
+    /* Up to 64 banks can be represented by valid_bank_mask */
+    if ( max_banks >= 64 )
+        return -E2BIG;
+
+    /* Clear all banks to avoid flushing unwanted CPUs */
+    for ( vcpu_bank = 0; vcpu_bank <= max_banks; vcpu_bank++ )

I think this is off by one and should be vcpu_bank < max_banks? Or else you are clearing one extra bank.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#21 lore raw

Re: [Xen-devel] [PATCH v2 3/3] x86/hyperv: L0 assisted TLB flush

On Fri, Feb 14, 2020 at 03:42:17PM +0100, Roger Pau Monné wrote:
[...]

Quoted 42 lines
Quoted 42 lines

#endif /* __XEN_HYPERV_PRIVIATE_H__ */

xen/arch/x86/guest/hyperv/tlb.c +28 -0
index 48f527229e..f68e14f151 100644
@@ -19,15 +19,185 @@
  * Copyright (c) 2020 Microsoft.
  */
 
+#include <xen/cpu.h>
 #include <xen/cpumask.h>
 #include <xen/errno.h>
 
+#include <asm/guest/hyperv.h>
+#include <asm/guest/hyperv-hcall.h>
+#include <asm/guest/hyperv-tlfs.h>
+
 #include "private.h"
 
+/*
+ * It is possible to encode up to 4096 pages using the lower 12 bits
+ * in an element of gva_list
+ */
+#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
+
+static unsigned int fill_gva_list(uint64_t *gva_list, const void *va,
+                                  unsigned int order)
+{
+    unsigned long start = (unsigned long)va;
+    unsigned long end = start + (PAGE_SIZE << order) - 1;
+    unsigned int n = 0;
+
+    do {
+        unsigned long remain = end - start;
+
+        gva_list[n] = start & PAGE_MASK;
+
+        /*
+         * Use lower 12 bits to encode the number of additional pages
+         * to flush
+         */
+        if ( remain >= HV_TLB_FLUSH_UNIT )
Quoted 8 lines

xen/arch/x86/guest/hyperv/tlb.c line 59

Quoted 5 lines
xen/arch/x86/guest/hyperv/tlb.c +5 -0
+        {
+            gva_list[n] |= ~PAGE_MASK;
+            start += HV_TLB_FLUSH_UNIT;
+        }
+        else if ( remain )

remain is always going to be > 0, since the loop condition is end > start, and hence this can be a plain else.

Ack.

Quoted 42 lines
Quoted 27 lines
xen/arch/x86/guest/hyperv/tlb.c +26 -0
+        {
+            gva_list[n] |= (remain - 1) >> PAGE_SHIFT;
+            start = end;
+        }
+
+        n++;
+    } while ( start < end );
+
+    return n;
+}
+
+static uint64_t flush_tlb_ex(const cpumask_t *mask, const void *va,
+                             unsigned int flags)
+{
+    struct hv_tlb_flush_ex *flush = this_cpu(hv_input_page);
+    int nr_banks;
+    unsigned int max_gvas, order = flags & FLUSH_ORDER_MASK;
+    uint64_t ret;
+
+    if ( !flush || local_irq_is_enabled() )
+    {
+        ASSERT_UNREACHABLE();
+        return ~0ULL;
+    }
+
+    if ( !(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED) )

xen/arch/x86/guest/hyperv/tlb.c line 95

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+        return ~0ULL;
+
+    flush->address_space = 0;
+    flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
+    if ( !(flags & FLUSH_TLB_GLOBAL) )
+        flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
+
+    nr_banks = cpumask_to_vpset(&flush->hv_vp_set, mask);
+    if ( nr_banks < 0 )
+        return ~0ULL;

It would be nice to propagate the error code from cpumask_to_vpset, but since the function can also return HyperV error codes this doesn't make much sense.

Quoted 16 lines

xen/arch/x86/guest/hyperv/tlb.c line 108

Quoted 13 lines
xen/arch/x86/guest/hyperv/tlb.c +13 -0
+
+    max_gvas =
+        (PAGE_SIZE - sizeof(*flush) - nr_banks *
+         sizeof(flush->hv_vp_set.bank_contents[0])) /
+        sizeof(uint64_t);       /* gva is represented as uint64_t */
+
+    /*
+     * Flush the entire address space if va is NULL or if there is not
+     * enough space for gva_list.
+     */
+    if ( !va || (PAGE_SIZE << order) / HV_TLB_FLUSH_UNIT > max_gvas )
+        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX, 0,
+                                  nr_banks, virt_to_maddr(flush), 0);

You could just return hv_do_rep_hypercall(...); here, which will avoid the else branch below and the indentation.

Ack.

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/guest/hyperv/tlb.c +16 -1
+    else
+    {
+        uint64_t *gva_list =
+            (uint64_t *)flush + sizeof(*flush) / sizeof(uint64_t) + nr_banks;
+        unsigned int gvas = fill_gva_list(gva_list, va, order);
+
+        BUILD_BUG_ON(sizeof(*flush) % sizeof(uint64_t));
+
+        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
+                                  gvas, nr_banks, virt_to_maddr(flush), 0);
+    }
+
+    return ret;
+}
+
 int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
                      unsigned int flags)
 {
-    return -EOPNOTSUPP;
+    unsigned long irq_flags;
Quoted 8 lines

xen/arch/x86/guest/hyperv/tlb.c line 133

Quoted 6 lines
xen/arch/x86/guest/hyperv/tlb.c +6 -0
+    struct hv_tlb_flush *flush = this_cpu(hv_input_page);
+    unsigned int max_gvas, order = flags & FLUSH_ORDER_MASK;
+    uint64_t ret;
+
+    ASSERT(flush);
+    ASSERT(!cpumask_empty(mask));

I would also turn this into an if ( ... ) { ASSERT; return -EFOO; }

Ack.

Quoted 22 lines
Quoted 22 lines
xen/arch/x86/guest/hyperv/tlb.c +21 -0
+
+    local_irq_save(irq_flags);
+
+    flush->address_space = 0;
+    flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
+    flush->processor_mask = 0;
+    if ( !(flags & FLUSH_TLB_GLOBAL) )
+        flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;
+
+    if ( cpumask_equal(mask, &cpu_online_map) )
+        flush->flags |= HV_FLUSH_ALL_PROCESSORS;
+    else
+    {
+        unsigned int cpu;
+
+        /*
+         * Normally VP indices are in ascending order and match Xen's
+         * idea of CPU ids. Check the last index to see if VP index is
+         * >= 64. If so, we can skip setting up parameters for
+         * non-applicable hypercalls without looking further.
+         */
Quoted 8 lines

xen/arch/x86/guest/hyperv/tlb.c line 160

Quoted 6 lines
xen/arch/x86/guest/hyperv/tlb.c +6 -0
+        if ( hv_vp_index(cpumask_last(mask)) >= 64 )
+            goto do_ex_hypercall;
+
+        for_each_cpu ( cpu, mask )
+        {
+            uint32_t vpid = hv_vp_index(cpu);

This should be unsigned int now.

Good catch.

Quoted 17 lines

xen/arch/x86/guest/hyperv/tlb.c line 172

Quoted 13 lines
xen/arch/x86/guest/hyperv/tlb.c +12 -0
+
+            if ( vpid > ms_hyperv.max_vp_index )
+            {
+                local_irq_restore(irq_flags);
+                return -ENXIO;
+            }
+
+            if ( vpid >= 64 )
+                goto do_ex_hypercall;
+
+            __set_bit(vpid, &flush->processor_mask);
+        }

Would it make sense to abstract this as cpumask_to_processor_mask, since you are adding cpumask_to_vpset below.

There is only one usage so far, so I don't think it is necessary.

Quoted 8 lines

xen/arch/x86/guest/hyperv/tlb.c line 175

Quoted 3 lines
xen/arch/x86/guest/hyperv/tlb.c +3 -0
+    }
+
+    max_gvas = (PAGE_SIZE - sizeof(*flush)) / sizeof(flush->gva_list[0]);

You could init this at declaration, and make it const static since the value can be calculated at compile time AFAICT. Or create a define with it (HV_TLB_FLUSH_MAX_GVAS?). There's no need to store it on the stack.

I can introduce a define, but the name you suggested is too generic. If there is variable sized header, the calculation is going to be different.

Quoted 13 lines

xen/arch/x86/guest/hyperv/tlb.c line 186

Quoted 12 lines
xen/arch/x86/guest/hyperv/tlb.c +11 -0
+
+    /*
+     * Flush the entire address space if va is NULL or if there is not
+     * enough space for gva_list.
+     */
+    if ( !va || (PAGE_SIZE << order) / HV_TLB_FLUSH_UNIT > max_gvas )
+        ret = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
+                              virt_to_maddr(flush), 0);
+    else
+    {
+        unsigned int gvas = fill_gva_list(flush->gva_list, va, order);
Quoted 3 lines

No need for the gvas variable, you can just call fill_gva_list at hv_do_rep_hypercall.

Sure.

Quoted 61 lines
Quoted 61 lines
xen/arch/x86/guest/hyperv/tlb.c +14 -0
+
+        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST, gvas, 0,
+                                  virt_to_maddr(flush), 0);
+    }
+
+    goto done;
+
+ do_ex_hypercall:
+    ret = flush_tlb_ex(mask, va, flags);
+
+ done:
+    local_irq_restore(irq_flags);
+
+    return ret & HV_HYPERCALL_RESULT_MASK ? -ENXIO : 0;
 }
 
 /*
xen/arch/x86/guest/hyperv/util.c new file +38 -0
new file mode 100644
index 0000000000..e092593746
@@ -0,0 +1,74 @@
+/******************************************************************************
+ * arch/x86/guest/hyperv/util.c
+ *
+ * Hyper-V utility functions
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (c) 2020 Microsoft.
+ */
+
+#include <xen/cpu.h>
+#include <xen/cpumask.h>
+#include <xen/errno.h>
+
+#include <asm/guest/hyperv.h>
+#include <asm/guest/hyperv-tlfs.h>
+
+#include "private.h"
+
+int cpumask_to_vpset(struct hv_vpset *vpset,
+                     const cpumask_t *mask)
+{
+    int nr = 1;
+    unsigned int cpu, vcpu_bank, vcpu_offset;
+    unsigned int max_banks = ms_hyperv.max_vp_index / 64;
+
+    /* Up to 64 banks can be represented by valid_bank_mask */
Quoted 8 lines

xen/arch/x86/guest/hyperv/util.c line 43

Quoted 5 lines
xen/arch/x86/guest/hyperv/util.c new file +5 -0
+    if ( max_banks >= 64 )
+        return -E2BIG;
+
+    /* Clear all banks to avoid flushing unwanted CPUs */
+    for ( vcpu_bank = 0; vcpu_bank <= max_banks; vcpu_bank++ )

I think this is off by one and should be vcpu_bank < max_banks? Or else you are clearing one extra bank.

If max_vp_index were the maximum vp index, max_banks would be inclusive (for example, max_vp_index is 63, 63/64=0). The code should be correct in that case.

However, when I did another round of self-review, I discovered that the value stored in max_vp_index was actually "the maximum number of virtual processors supported", so it wasn't really the maximum index.

This means I will need to adjust places where max_vp_index is used.

Here, max_banks is not inclusive anymore.

And an earlier place

+ if ( vpid > ms_hyperv.max_vp_index )
+ {

also requires fixing.

I will also see about changing max_{l,v}p_index to more sensible names or add some comments to their definitions.

Wei.

Quoted 2 lines

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

Re: [Xen-devel] [PATCH v2 3/3] x86/hyperv: L0 assisted TLB flush

In reply to Wei Liu

From: Wei Liu <[email protected]> On Behalf Of Wei Liu Sent: Friday, February 14, 2020 4:35 AM

Quoted 82 lines

Implement L0 assisted TLB flush for Xen on Hyper-V. It takes advantage of several hypercalls:

* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST
* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX

Pick the most efficient hypercalls available.

---
v2:
1. Address Roger and Jan's comments re types etc.
2. Fix pointer arithmetic.
3. Misc improvement to code.

---
 xen/arch/x86/guest/hyperv/Makefile  |   1 +
 xen/arch/x86/guest/hyperv/private.h |   9 ++
 xen/arch/x86/guest/hyperv/tlb.c     | 172 +++++++++++++++++++++++++++-
 xen/arch/x86/guest/hyperv/util.c    |  74 ++++++++++++
 4 files changed, 255 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/x86/guest/hyperv/util.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 18902c33e9..0e39410968 100644
@@ -1,2 +1,3 @@
 obj-y += hyperv.o
 obj-y += tlb.o
+obj-y += util.o
xen/arch/x86/guest/hyperv/private.h +9 -0
index 509bedaafa..79a77930a0 100644
@@ -24,12 +24,21 @@
 
 #include <xen/cpumask.h>
 #include <xen/percpu.h>
+#include <xen/types.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
 DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
+static inline unsigned int hv_vp_index(unsigned int cpu)
+{
+    return per_cpu(hv_vp_index, cpu);
+}
+
 int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
                      unsigned int flags);
 
+/* Returns number of banks, -ev if error */
+int cpumask_to_vpset(struct hv_vpset *vpset, const cpumask_t *mask);
+
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
xen/arch/x86/guest/hyperv/tlb.c +10 -0
index 48f527229e..f68e14f151 100644
@@ -19,15 +19,185 @@
  * Copyright (c) 2020 Microsoft.
  */
 
+#include <xen/cpu.h>
 #include <xen/cpumask.h>
 #include <xen/errno.h>
 
+#include <asm/guest/hyperv.h>
+#include <asm/guest/hyperv-hcall.h>
+#include <asm/guest/hyperv-tlfs.h>
+
 #include "private.h"
 
+/*
+ * It is possible to encode up to 4096 pages using the lower 12 bits
+ * in an element of gva_list
+ */
+#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)

xen/arch/x86/guest/hyperv/tlb.c line 46

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+
+static unsigned int fill_gva_list(uint64_t *gva_list, const void *va,
+                                  unsigned int order)
+{
+    unsigned long start = (unsigned long)va;
+    unsigned long end = start + (PAGE_SIZE << order) - 1;
+    unsigned int n = 0;
+
+    do {
+        unsigned long remain = end - start;

The calculated value here isn't actually the remaining bytes in the range to flush -- it's one less than the remaining bytes in the range
to flush because of the -1 in the calculation of 'end'. That difference
will mess up the comparison below against HV_TLB_FLUSH_UNIT in the case that there are exactly 4096 page remaining to be flushed. It should take the "=" case, but won't. Also, the '-1' in 'remain - 1' in the else clause becomes unneeded, and the 'start = end' assignment then propagates the error.

In the parallel code in Linux, if you follow the call sequence to get to fill_gav_list(), the 'end' argument is really the address of the first byte of the first page that isn't in the flush range (i.e., one beyond the true 'end') and so is a bit misnamed.

I think the calculation of 'end' should drop the -1, and perhaps 'end' should be renamed.

Michael

Quoted 14 lines
xen/arch/x86/guest/hyperv/tlb.c +14 -0
+
+        gva_list[n] = start & PAGE_MASK;
+
+        /*
+         * Use lower 12 bits to encode the number of additional pages
+         * to flush
+         */
+        if ( remain >= HV_TLB_FLUSH_UNIT )
+        {
+            gva_list[n] |= ~PAGE_MASK;
+            start += HV_TLB_FLUSH_UNIT;
+        }
+        else if ( remain )
+        {

xen/arch/x86/guest/hyperv/tlb.c line 70

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+            gva_list[n] |= (remain - 1) >> PAGE_SHIFT;
+            start = end;
+        }
+
+        n++;
+    } while ( start < end );
+
+    return n;
+}
+

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#23 lore raw

Re: [Xen-devel] [PATCH v2 3/3] x86/hyperv: L0 assisted TLB flush

Quoted 103 lines · Michael Kelley

From: Wei Liu <[email protected]> On Behalf Of Wei Liu Sent: Friday, February 14, 2020 4:35 AM

Quoted 82 lines

Implement L0 assisted TLB flush for Xen on Hyper-V. It takes advantage of several hypercalls:

* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST
* HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE
* HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX

Pick the most efficient hypercalls available.

---
v2:
1. Address Roger and Jan's comments re types etc.
2. Fix pointer arithmetic.
3. Misc improvement to code.

---
 xen/arch/x86/guest/hyperv/Makefile  |   1 +
 xen/arch/x86/guest/hyperv/private.h |   9 ++
 xen/arch/x86/guest/hyperv/tlb.c     | 172 +++++++++++++++++++++++++++-
 xen/arch/x86/guest/hyperv/util.c    |  74 ++++++++++++
 4 files changed, 255 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/x86/guest/hyperv/util.c
xen/arch/x86/guest/hyperv/Makefile +1 -0
index 18902c33e9..0e39410968 100644
@@ -1,2 +1,3 @@
 obj-y += hyperv.o
 obj-y += tlb.o
+obj-y += util.o
xen/arch/x86/guest/hyperv/private.h +9 -0
index 509bedaafa..79a77930a0 100644
@@ -24,12 +24,21 @@
 
 #include <xen/cpumask.h>
 #include <xen/percpu.h>
+#include <xen/types.h>
 
 DECLARE_PER_CPU(void *, hv_input_page);
 DECLARE_PER_CPU(void *, hv_vp_assist);
 DECLARE_PER_CPU(unsigned int, hv_vp_index);
 
+static inline unsigned int hv_vp_index(unsigned int cpu)
+{
+    return per_cpu(hv_vp_index, cpu);
+}
+
 int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
                      unsigned int flags);
 
+/* Returns number of banks, -ev if error */
+int cpumask_to_vpset(struct hv_vpset *vpset, const cpumask_t *mask);
+
 #endif /* __XEN_HYPERV_PRIVIATE_H__  */
xen/arch/x86/guest/hyperv/tlb.c +10 -0
index 48f527229e..f68e14f151 100644
@@ -19,15 +19,185 @@
  * Copyright (c) 2020 Microsoft.
  */
 
+#include <xen/cpu.h>
 #include <xen/cpumask.h>
 #include <xen/errno.h>
 
+#include <asm/guest/hyperv.h>
+#include <asm/guest/hyperv-hcall.h>
+#include <asm/guest/hyperv-tlfs.h>
+
 #include "private.h"
 
+/*
+ * It is possible to encode up to 4096 pages using the lower 12 bits
+ * in an element of gva_list
+ */
+#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)

xen/arch/x86/guest/hyperv/tlb.c line 46

Quoted 10 lines
xen/arch/x86/guest/hyperv/tlb.c +10 -0
+
+static unsigned int fill_gva_list(uint64_t *gva_list, const void *va,
+                                  unsigned int order)
+{
+    unsigned long start = (unsigned long)va;
+    unsigned long end = start + (PAGE_SIZE << order) - 1;
+    unsigned int n = 0;
+
+    do {
+        unsigned long remain = end - start;

The calculated value here isn't actually the remaining bytes in the range to flush -- it's one less than the remaining bytes in the range
to flush because of the -1 in the calculation of 'end'. That difference
will mess up the comparison below against HV_TLB_FLUSH_UNIT in the case that there are exactly 4096 page remaining to be flushed. It should take the "=" case, but won't. Also, the '-1' in 'remain - 1' in the else clause becomes unneeded, and the 'start = end' assignment then propagates the error.

Quoted 7 lines

In the parallel code in Linux, if you follow the call sequence to get to fill_gav_list(), the 'end' argument is really the address of the first byte of the first page that isn't in the flush range (i.e., one beyond the true 'end') and so is a bit misnamed.

I think the calculation of 'end' should drop the -1, and perhaps 'end' should be renamed.

Thanks for the detailed review. Let me fix this.

Wei.

Quoted 3 lines

Michael

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel