Skip to contentSkip to conversation

x86/hvm: Introduce Xen-wide ASID allocator

RFC PATCH In review Teddy Astie 2 participants 11 messages Jun 26, 2025 – Sep 1, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover x86/hvm: Introduce Xen-wide ASID allocator

From: Vaishali Thakkar <[email protected]> (formely vates.tech)
original v2: https://lore.kernel.org/xen-devel/[email protected]/

This is part of the effort to enable AMD SEV technologies in Xen. For AMD SEV support, we need a fixed ASID associated with all vcpus of the same domain throughout the domain's lifetime. This is because for SEV/ SEV-{ES,SNP} VM, the ASID is the index which is associated with the encryption key.

Currently, ASID generation and management is done per-PCPU in Xen. And at the time of each VMENTER, the ASID associated with vcpus of the domain is changed. This implementation is incompatible with SEV technologies for the above mentioned reasons. In a discussion with Andrew Cooper, it came up that it'll be nice to have fixed ASIDs not only for SEV VMs but also for all VMs. Because it opens up the opportunity to use features like TLBSYNC/INVLPGB (Section 5.5.3 in AMD Architecture manual[0]) and Intel RAR [1] for broadcasting the TLB Invalidations.

Changes since v2:
-----------------
A lot has changed since v2, though the concept is still the same. The asid management logic is a lot simpler, by relying a bitmap to track all used ASIDs, and freeing them individually instead of using a ASID reclaim strategy.

When a vCPU needs to be flushed, it is signaled through vcpu.needs_tlb_flush, indicating that the next time the guest is re-entered, a TLB flush will have to be issued (either using per-context invvpid on Intel or flush-by-asid tlb_control on AMD).

Changes were tested on Intel Haswell, KVM AMD virtual machine and AMD EPYC Genoa.

Teddy Astie (3):
vmx: Rewrite vpid_sync_vcpu_gva
vmx: Introduce vcpu single context VPID invalidation x86/hvm: Introduce Xen-wide ASID allocator

 xen/arch/x86/flushtlb.c                |  31 +++---
 xen/arch/x86/hvm/asid.c                | 148 +++++++++----------------
 xen/arch/x86/hvm/emulate.c             |   2 +-
 xen/arch/x86/hvm/hvm.c                 |  14 ++-
 xen/arch/x86/hvm/nestedhvm.c           |   6 +-
 xen/arch/x86/hvm/svm/asid.c            |  77 ++++++++-----
 xen/arch/x86/hvm/svm/nestedsvm.c       |   1 -
 xen/arch/x86/hvm/svm/svm.c             |  36 +++---
 xen/arch/x86/hvm/svm/svm.h             |   4 -
 xen/arch/x86/hvm/vmx/vmcs.c            |   5 +-
 xen/arch/x86/hvm/vmx/vmx.c             |  68 ++++++------
 xen/arch/x86/hvm/vmx/vvmx.c            |   5 +-
 xen/arch/x86/include/asm/flushtlb.h    |   7 --
 xen/arch/x86/include/asm/hvm/asid.h    |  25 ++---
 xen/arch/x86/include/asm/hvm/domain.h  |   1 +
 xen/arch/x86/include/asm/hvm/hvm.h     |  15 +--
 xen/arch/x86/include/asm/hvm/svm/svm.h |   5 +
 xen/arch/x86/include/asm/hvm/vcpu.h    |  10 +-
 xen/arch/x86/include/asm/hvm/vmx/vmx.h |  44 ++++----
 xen/arch/x86/mm/hap/hap.c              |   6 +-
 xen/arch/x86/mm/p2m.c                  |   7 +-
 xen/arch/x86/mm/paging.c               |   2 +-
 xen/arch/x86/mm/shadow/hvm.c           |   1 +
 xen/arch/x86/mm/shadow/multi.c         |  12 +-
 xen/include/xen/sched.h                |   2 +
 25 files changed, 249 insertions(+), 285 deletions(-)
Signature · 9 lines
2.50.0



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw

RFC PATCH v3 1/3 vmx: Rewrite vpid_sync_vcpu_gva

Rewrite this function such as it doesn't rely on goto, also change the type of "type" to match the __invvpid function call.

---
 xen/arch/x86/include/asm/hvm/vmx/vmx.h | 29 +++++++++-----------------
 1 file changed, 10 insertions(+), 19 deletions(-)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +10 -19
index 56bea252cc..8559343857 100644
@@ -287,10 +287,10 @@ extern uint8_t posted_intr_vector;
 #define cpu_has_vmx_vpid_invvpid_single_context_retaining_global    \
     (vmx_caps.vpid & VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL)
 
-#define INVVPID_INDIVIDUAL_ADDR                 0
-#define INVVPID_SINGLE_CONTEXT                  1
-#define INVVPID_ALL_CONTEXT                     2
-#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3
+#define INVVPID_INDIVIDUAL_ADDR                 0UL
+#define INVVPID_SINGLE_CONTEXT                  1UL
+#define INVVPID_ALL_CONTEXT                     2UL
+#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3UL
 
 static always_inline void __vmptrld(u64 addr)
 {
@@ -454,25 +454,16 @@ void ept_sync_domain(struct p2m_domain *p2m);
 
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
 {
-    int type = INVVPID_INDIVIDUAL_ADDR;
+    unsigned long type;
 
-    /*
-     * If individual address invalidation is not supported, we escalate to
-     * use single context invalidation.
-     */
+    /* Use the most precise invalidation type available. */
     if ( likely(cpu_has_vmx_vpid_invvpid_individual_addr) )
-        goto execute_invvpid;
-
-    type = INVVPID_SINGLE_CONTEXT;
-
-    /*
-     * If single context invalidation is not supported, we escalate to
-     * use all context invalidation.
-     */
-    if ( !cpu_has_vmx_vpid_invvpid_single_context )
+        type = INVVPID_INDIVIDUAL_ADDR;
+    else if ( likely(cpu_has_vmx_vpid_invvpid_single_context) )
+        type = INVVPID_SINGLE_CONTEXT;
+    else
         type = INVVPID_ALL_CONTEXT;
 
-execute_invvpid:
     __invvpid(type, v->arch.hvm.n1asid.asid, (u64)gva);
 }
 
Signature · 9 lines
2.50.0



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Reviewed-by on v3 1/3
#2 lore raw

Re: [RFC PATCH v3 1/3] vmx: Rewrite vpid_sync_vcpu_gva

Quoted 2 lines · Teddy Astie

Rewrite this function such as it doesn't rely on goto, also change the type of "type" to match the __invvpid function call.

While this type change is probably okay (and benign to code generation), ...

Quoted 4 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -0
@@ -287,10 +287,10 @@ extern uint8_t posted_intr_vector;
 #define cpu_has_vmx_vpid_invvpid_single_context_retaining_global    \

xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 293

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -4
     (vmx_caps.vpid & VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL)
 
-#define INVVPID_INDIVIDUAL_ADDR                 0
-#define INVVPID_SINGLE_CONTEXT                  1
-#define INVVPID_ALL_CONTEXT                     2
-#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3
+#define INVVPID_INDIVIDUAL_ADDR                 0UL
+#define INVVPID_SINGLE_CONTEXT                  1UL
+#define INVVPID_ALL_CONTEXT                     2UL
+#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3UL

... I don't follow the need for these extra adjustments. Preferably with them dropped

Jan

#3 lore raw

Re: [RFC PATCH v3 1/3] vmx: Rewrite vpid_sync_vcpu_gva

Le 31/07/2025 à 17:54, Jan Beulich a écrit :

Quoted 12 lines · Teddy Astie
Quoted 2 lines

Rewrite this function such as it doesn't rely on goto, also change the type of "type" to match the __invvpid function call.

While this type change is probably okay (and benign to code generation), ...

Quoted 6 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -0
@@ -287,10 +287,10 @@ extern uint8_t posted_intr_vector;
  #define cpu_has_vmx_vpid_invvpid_single_context_retaining_global    \
      (vmx_caps.vpid & VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL)
 
Quoted 11 lines

xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 293

Quoted 8 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -4
-#define INVVPID_INDIVIDUAL_ADDR                 0
-#define INVVPID_SINGLE_CONTEXT                  1
-#define INVVPID_ALL_CONTEXT                     2
-#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3
+#define INVVPID_INDIVIDUAL_ADDR                 0UL
+#define INVVPID_SINGLE_CONTEXT                  1UL
+#define INVVPID_ALL_CONTEXT                     2UL
+#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3UL

... I don't follow the need for these extra adjustments. Preferably with them dropped

With the type change from int to unsigned long to match __invvpid() parameter, IIUC MISRA rule 7.2 requires that integer literals that are used for unsigned must have the proper suffix.

Quoted 4 lines

Jan

Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#4 lore raw

Re: [RFC PATCH v3 1/3] vmx: Rewrite vpid_sync_vcpu_gva

Quoted 25 lines · Teddy Astie

Le 31/07/2025 à 17:54, Jan Beulich a écrit :

Quoted 12 lines · Teddy Astie
Quoted 2 lines

Rewrite this function such as it doesn't rely on goto, also change the type of "type" to match the __invvpid function call.

While this type change is probably okay (and benign to code generation), ...

Quoted 6 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -0
@@ -287,10 +287,10 @@ extern uint8_t posted_intr_vector;
  #define cpu_has_vmx_vpid_invvpid_single_context_retaining_global    \
      (vmx_caps.vpid & VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL)
 
Quoted 11 lines

xen/arch/x86/include/asm/hvm/vmx/vmx.h extern uint8_t posted_intr_vector; · line 293

Quoted 8 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -4
-#define INVVPID_INDIVIDUAL_ADDR                 0
-#define INVVPID_SINGLE_CONTEXT                  1
-#define INVVPID_ALL_CONTEXT                     2
-#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3
+#define INVVPID_INDIVIDUAL_ADDR                 0UL
+#define INVVPID_SINGLE_CONTEXT                  1UL
+#define INVVPID_ALL_CONTEXT                     2UL
+#define INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 3UL

... I don't follow the need for these extra adjustments. Preferably with them dropped

Quoted 3 lines

With the type change from int to unsigned long to match __invvpid() parameter, IIUC MISRA rule 7.2 requires that integer literals that are used for unsigned must have the proper suffix.

No, it doesn't. Please re-read carefully what docs/misra/rules.rst says in this regard.

Jan

#5 lore raw

RFC PATCH v3 2/3 vmx: Introduce vcpu single context VPID invalidation

In reply to Teddy Astie

Introduce vpid_sync_vcpu_context to do a single-context invalidation on the vpid attached to the vcpu as a alternative to per-gva and all-context invlidations.

---
 xen/arch/x86/include/asm/hvm/vmx/vmx.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +13 -0
index 8559343857..173311bfbb 100644
@@ -452,6 +452,19 @@ static inline void ept_sync_all(void)
 
 void ept_sync_domain(struct p2m_domain *p2m);
 
+static inline void vpid_sync_vcpu_context(struct vcpu *v)
+{
+    unsigned long type;
+
+    /* Use the most precise invalidation type available. */
+    if ( likely(cpu_has_vmx_vpid_invvpid_single_context) )
+        type = INVVPID_SINGLE_CONTEXT;
+    else
+        type = INVVPID_ALL_CONTEXT;
+
+    __invvpid(type, v->arch.hvm.n1asid.asid, 0);
+}
+
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
 {
     unsigned long type;
Signature · 9 lines
2.50.0



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#6 lore raw

Re: [RFC PATCH v3 2/3] vmx: Introduce vcpu single context VPID invalidation

Quoted 5 lines · Teddy Astie

Introduce vpid_sync_vcpu_context to do a single-context invalidation on the vpid attached to the vcpu as a alternative to per-gva and all-context invlidations.

Looks okay to me, but I'm not convinced we want to put it in possibly long before a first user appears.

xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline void ept_sync_all(void) · line 455

Quoted 7 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1 -0
@@ -452,6 +452,19 @@ static inline void ept_sync_all(void)
 
 void ept_sync_domain(struct p2m_domain *p2m);
 
+static inline void vpid_sync_vcpu_context(struct vcpu *v)

Andrew - is this again a case where you would object to me asking for the pointer to be pointer-to-const?

Jan

Quoted 15 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +12 -0
+{
+    unsigned long type;
+
+    /* Use the most precise invalidation type available. */
+    if ( likely(cpu_has_vmx_vpid_invvpid_single_context) )
+        type = INVVPID_SINGLE_CONTEXT;
+    else
+        type = INVVPID_ALL_CONTEXT;
+
+    __invvpid(type, v->arch.hvm.n1asid.asid, 0);
+}
+
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
 {
     unsigned long type;
#7 lore raw

RFC PATCH v3 3/3 x86/hvm: Introduce Xen-wide ASID allocator

In reply to Teddy Astie

From: Vaishali Thakkar <[email protected]> (formely vates.tech)

Currently ASID generation and management is done per-PCPU. This scheme is incompatible with SEV technologies as SEV VMs need to have a fixed ASID associated with all vcpus of the VM throughout it's lifetime.

This commit introduces a Xen-wide allocator which initializes the asids at the start of xen and allows to have a fixed asids throughout the lifecycle of all domains. Having a fixed asid for non-SEV domains also presents us with the opportunity to further take use of AMD instructions like TLBSYNC and INVLPGB for broadcasting the TLB invalidations.

Introduce vcpu->needs_tlb_flush attribute to schedule a guest TLB flush for the next VMRUN/VMENTER. This will be later be done using either TLB_CONTROL field (AMD) or INVEPT (Intel). This flush method is used in place of the current ASID swapping logic.

---
CC: Oleksii Kurochko <[email protected]>

Should the ASID/VPID/VMID management logic code being shared accross x86/ARM/RISC-V ?

Is it possible to have multiples vCPUs of a same domain simultaneously scheduled on top of a single pCPU ? If so, it would need a special consideration for this corner case, such as we don't miss a TLB flush in such cases.

I get various stability when testing shadow paging in these patches, unsure what's the exact root case. HAP works perfectly fine though.

TODO:
- Intel: Don't assign the VPID at each VMENTER, though we need to rethink how we manage VMCS with nested virtualization / altp2m for changing this behavior.
- AMD: Consider hot-plug of CPU with ERRATA_170. (is it possible ?)
- Consider cases where we don't have enough ASIDs (e.g Xen as nested guest)
- Nested virtualization ASID management

Changes since v2:
- Moved hvm_asid_domain_create to hvm_domain_initialise
- Added __ro_after_init for bitmaps
- Make hvm_asid_init unsigned int __init
- Drop functions hvm_asid_flush_domain_asid and hvm_asid_flush_vcpu
- Mark ASID 0 permenantly
- Add a logic to move asids from reclaim_bitmap->asid_bitmap
- Misc styling fixes - remove unncessary trailing spaces/printks
- Simplified asid bitmap management
It is only called once per domain, so it doesn't need to have a complicated logic.
- Drop hvm_asid_data structure which doesn't serve a purpose anymore.
- Introduce and use vcpu->needs_tlb_flush to indicate that a guest TLB flush is needed before waking the vcpu. It is used to set TLB_CONTROL (AMD) field properly or make a appropriate invept (Intel).
- Only assign ASID once (see TODO for Intel side)
- Check the ERRATA_170 for each CPU present.
- rewrote guest_flush_tlb_mask for this new mecanism

Changes since v1:
- Introudce hvm_asid_bitmap as discussed at Xen-summit
- Introduce hvm_reclaim_bitmap for reusing ASIDs
- Assign the asid to the domain at the domain creation via hvm_asid_domain_create
- Corrected the use of CPUID in the svm_asid_init function
- Adjusted the code in nested virtualization related files to use new scheme. As discussed at the Xen-summit, this is not tested.
- Addressed Jan's comments about using uniform style for accessing domains via v->domain
- Allow to flush at the vcpu level in HAP code
- Documented the sketch of implementation for the new scheme
- Remove min_asid as for this patch, we are not demonstarting it's usecase
- Arrange includes in multiple files as per Jan's feedback

---
 xen/arch/x86/flushtlb.c                |  31 +++---
 xen/arch/x86/hvm/asid.c                | 148 +++++++++----------------
 xen/arch/x86/hvm/emulate.c             |   2 +-
 xen/arch/x86/hvm/hvm.c                 |  14 ++-
 xen/arch/x86/hvm/nestedhvm.c           |   6 +-
 xen/arch/x86/hvm/svm/asid.c            |  77 ++++++++-----
 xen/arch/x86/hvm/svm/nestedsvm.c       |   1 -
 xen/arch/x86/hvm/svm/svm.c             |  36 +++---
 xen/arch/x86/hvm/svm/svm.h             |   4 -
 xen/arch/x86/hvm/vmx/vmcs.c            |   5 +-
 xen/arch/x86/hvm/vmx/vmx.c             |  68 ++++++------
 xen/arch/x86/hvm/vmx/vvmx.c            |   5 +-
 xen/arch/x86/include/asm/flushtlb.h    |   7 --
 xen/arch/x86/include/asm/hvm/asid.h    |  25 ++---
 xen/arch/x86/include/asm/hvm/domain.h  |   1 +
 xen/arch/x86/include/asm/hvm/hvm.h     |  15 +--
 xen/arch/x86/include/asm/hvm/svm/svm.h |   5 +
 xen/arch/x86/include/asm/hvm/vcpu.h    |  10 +-
 xen/arch/x86/include/asm/hvm/vmx/vmx.h |   4 +-
 xen/arch/x86/mm/hap/hap.c              |   6 +-
 xen/arch/x86/mm/p2m.c                  |   7 +-
 xen/arch/x86/mm/paging.c               |   2 +-
 xen/arch/x86/mm/shadow/hvm.c           |   1 +
 xen/arch/x86/mm/shadow/multi.c         |  12 +-
 xen/include/xen/sched.h                |   2 +
 25 files changed, 227 insertions(+), 267 deletions(-)
xen/arch/x86/flushtlb.c +17 -14
index 796b25dad6..8ddb4dc6cb 100644
@@ -13,6 +13,7 @@
 #include <xen/softirq.h>
 #include <asm/cache.h>
 #include <asm/flushtlb.h>
+#include <asm/hvm/hvm.h>
 #include <asm/invpcid.h>
 #include <asm/nops.h>
 #include <asm/page.h>
@@ -124,7 +125,6 @@ void switch_cr3_cr4(unsigned long cr3, unsigned long cr4)
 
     if ( tlb_clk_enabled )
         t = pre_flush();
-    hvm_flush_guest_tlbs();
 
     old_cr4 = read_cr4();
     ASSERT(!(old_cr4 & X86_CR4_PCIDE) || !(old_cr4 & X86_CR4_PGE));
@@ -229,9 +229,6 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
             do_tlb_flush();
     }
 
-    if ( flags & FLUSH_HVM_ASID_CORE )
-        hvm_flush_guest_tlbs();
-
     if ( flags & (FLUSH_CACHE | FLUSH_CACHE_WRITEBACK) )
     {
         const struct cpuinfo_x86 *c = &current_cpu_data;
@@ -321,18 +318,24 @@ void cache_writeback(const void *addr, unsigned int size)
     asm volatile ("sfence" ::: "memory");
 }
 
-unsigned int guest_flush_tlb_flags(const struct domain *d)
+void guest_flush_tlb_mask(const struct domain *d, const cpumask_t *mask)
 {
-    bool shadow = paging_mode_shadow(d);
-    bool asid = is_hvm_domain(d) && (cpu_has_svm || shadow);
+    struct vcpu *v;
 
-    return (shadow ? FLUSH_TLB : 0) | (asid ? FLUSH_HVM_ASID_CORE : 0);
-}
+    if ( is_pv_domain(d) )
+    {
+        flush_tlb_mask(mask);
+        return;
+    }
 
-void guest_flush_tlb_mask(const struct domain *d, const cpumask_t *mask)
-{
-    unsigned int flags = guest_flush_tlb_flags(d);
+    for_each_vcpu(d, v)
+        if ( cpumask_test_cpu(v->vcpu_id, mask) )
+            v->needs_tlb_flush = true;
 
-    if ( flags )
-        flush_mask(mask, flags);
+    smp_wmb();
+
+    if ( paging_mode_shadow(d) )
+        flush_tlb_mask(mask);
+    else
+        on_selected_cpus(mask, NULL, NULL, 0);
 }
xen/arch/x86/hvm/asid.c +50 -98

183 lines not on this page · raw message

xen/arch/x86/hvm/emulate.c +1 -1
index 91f004d233..6ed8e03475 100644
@@ -2666,7 +2666,7 @@ static int cf_check hvmemul_tlb_op(
     case x86emul_invpcid:
         if ( x86emul_invpcid_type(aux) != X86_INVPCID_INDIV_ADDR )
         {
-            hvm_asid_flush_vcpu(current);
+            current->needs_tlb_flush = true;
             break;
         }
         aux = x86emul_invpcid_pcid(aux);
xen/arch/x86/hvm/hvm.c +12 -2
index 4cb2e13046..6cac062ba6 100644
@@ -702,6 +702,10 @@ int hvm_domain_initialise(struct domain *d,
     if ( rc )
         goto fail2;
 
+    rc = hvm_asid_alloc(&d->arch.hvm.asid);
+    if ( rc )
+        goto fail2;
+
     rc = alternative_call(hvm_funcs.domain_initialise, d);
     if ( rc != 0 )
         goto fail2;
@@ -782,8 +786,9 @@ void hvm_domain_destroy(struct domain *d)
         list_del(&ioport->list);
         xfree(ioport);
     }
-
+    hvm_asid_free(&d->arch.hvm.asid);
     destroy_vpci_mmcfg(d);
+
 }
 
 static int cf_check hvm_save_tsc_adjust(struct vcpu *v, hvm_domain_context_t *h)
@@ -1603,7 +1608,7 @@ int hvm_vcpu_initialise(struct vcpu *v)
     int rc;
     struct domain *d = v->domain;
 
-    hvm_asid_flush_vcpu(v);
+    v->needs_tlb_flush = true;
 
     spin_lock_init(&v->arch.hvm.tm_lock);
     INIT_LIST_HEAD(&v->arch.hvm.tm_list);
@@ -4134,6 +4139,11 @@ static void hvm_s3_resume(struct domain *d)
     }
 }
 
+int hvm_flush_tlb(const unsigned long *vcpu_bitmap)
+{
+    return current->domain->arch.paging.flush_tlb(vcpu_bitmap);
+}
+
 static int hvmop_flush_tlb_all(void)
 {
     if ( !is_hvm_domain(current->domain) )
xen/arch/x86/hvm/nestedhvm.c +2 -4
index bddd77d810..ed1b9303ad 100644
@@ -36,13 +36,11 @@ nestedhvm_vcpu_reset(struct vcpu *v)
     hvm_unmap_guest_frame(nv->nv_vvmcx, 1);
     nv->nv_vvmcx = NULL;
     nv->nv_vvmcxaddr = INVALID_PADDR;
-    nv->nv_flushp2m = 0;
+    nv->nv_flushp2m = true;
     nv->nv_p2m = NULL;
     nv->stale_np2m = false;
     nv->np2m_generation = 0;
 
-    hvm_asid_flush_vcpu_asid(&nv->nv_n2asid);
-
     alternative_vcall(hvm_funcs.nhvm_vcpu_reset, v);
 
     /* vcpu is in host mode */
@@ -86,7 +84,7 @@ static void cf_check nestedhvm_flushtlb_ipi(void *info)
      * This is cheaper than flush_tlb_local() and has
      * the same desired effect.
      */
-    hvm_asid_flush_core();
+    WARN_ON(hvm_flush_tlb(NULL));
     vcpu_nestedhvm(v).nv_p2m = NULL;
     vcpu_nestedhvm(v).stale_np2m = true;
 }
xen/arch/x86/hvm/svm/asid.c +49 -28

105 lines not on this page · raw message

xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
index dc2b6a4253..975b796d01 100644
@@ -699,7 +699,6 @@ nsvm_vcpu_vmentry(struct vcpu *v, struct cpu_user_regs *regs,
     if ( svm->ns_asid != vmcb_get_asid(ns_vmcb))
     {
         nv->nv_flushp2m = 1;
-        hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(v).nv_n2asid);
         svm->ns_asid = vmcb_get_asid(ns_vmcb);
     }
 
xen/arch/x86/hvm/svm/svm.c +22 -14
index e33a38c1e4..4ec6caee27 100644
@@ -26,6 +26,7 @@
 #include <asm/hvm/monitor.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/support.h>
+#include <asm/hvm/asid.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/svmdebug.h>
 #include <asm/hvm/svm/vmcb.h>
@@ -183,14 +184,17 @@ static void cf_check svm_update_guest_cr(
         if ( !nestedhvm_enabled(v->domain) )
         {
             if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) )
-                hvm_asid_flush_vcpu(v);
+                v->needs_tlb_flush = true;
         }
         else if ( nestedhvm_vmswitch_in_progress(v) )
             ; /* CR3 switches during VMRUN/VMEXIT do not flush the TLB. */
         else if ( !(flags & HVM_UPDATE_GUEST_CR3_NOFLUSH) )
-            hvm_asid_flush_vcpu_asid(
-                nestedhvm_vcpu_in_guestmode(v)
-                ? &vcpu_nestedhvm(v).nv_n2asid : &v->arch.hvm.n1asid);
+        {
+            if (nestedhvm_vcpu_in_guestmode(v))
+                vcpu_nestedhvm(v).nv_flushp2m = true;
+            else
+                v->needs_tlb_flush = true;
+        }
         break;
     case 4:
         value = HVM_CR4_HOST_MASK;
@@ -991,8 +995,7 @@ static void noreturn cf_check svm_do_resume(void)
         v->arch.hvm.svm.launch_core = smp_processor_id();
         hvm_migrate_timers(v);
         hvm_migrate_pirqs(v);
-        /* Migrating to another ASID domain.  Request a new ASID. */
-        hvm_asid_flush_vcpu(v);
+        v->needs_tlb_flush = true;
     }
 
     if ( !vcpu_guestmode && !vlapic_hw_disabled(vlapic) )
@@ -1019,13 +1022,15 @@ void asmlinkage svm_vmenter_helper(void)
 
     ASSERT(hvmemul_cache_disabled(curr));
 
-    svm_asid_handle_vmrun();
-
     TRACE_TIME(TRC_HVM_VMENTRY |
                (nestedhvm_vcpu_in_guestmode(curr) ? TRC_HVM_NESTEDFLAG : 0));
 
     svm_sync_vmcb(curr, vmcb_needs_vmsave);
 
+    if ( test_and_clear_bool(curr->needs_tlb_flush) ||
+         paging_mode_shadow(curr->domain) )
+        svm_vcpu_set_tlb_control(curr);
+
     vmcb->rax = regs->rax;
     vmcb->rip = regs->rip;
     vmcb->rsp = regs->rsp;
@@ -1146,6 +1151,8 @@ static int cf_check svm_vcpu_initialise(struct vcpu *v)
         return rc;
     }
 
+    svm_vcpu_assign_asid(v);
+
     return 0;
 }
 
@@ -1572,9 +1579,6 @@ static int _svm_cpu_up(bool bsp)
     /* check for erratum 383 */
     svm_init_erratum_383(c);
 
-    /* Initialize core's ASID handling. */
-    svm_asid_init(c);
-
     /* Initialize OSVW bits to be used by guests */
     svm_host_osvw_init();
 
@@ -2338,7 +2342,7 @@ static void svm_invlpga_intercept(
 {
     svm_invlpga(linear,
                 (asid == 0)
-                ? v->arch.hvm.n1asid.asid
+                ? v->domain->arch.hvm.asid.asid
                 : vcpu_nestedhvm(v).nv_n2asid.asid);
 }
 
@@ -2360,8 +2364,8 @@ static bool cf_check is_invlpg(
 
 static void cf_check svm_invlpg(struct vcpu *v, unsigned long linear)
 {
-    /* Safe fallback. Take a new ASID. */
-    hvm_asid_flush_vcpu(v);
+    /* Schedule a tlb flush on the VCPU. */
+    v->needs_tlb_flush = true;
 }
 
 static bool cf_check svm_get_pending_event(
@@ -2528,6 +2532,8 @@ const struct hvm_function_table * __init start_svm(void)
     svm_function_table.caps.hap_superpage_2mb = true;
     svm_function_table.caps.hap_superpage_1gb = cpu_has_page1gb;
 
+    svm_asid_init();
+
     return &svm_function_table;
 }
 
@@ -2584,6 +2590,8 @@ void asmlinkage svm_vmexit_handler(void)
                    (vlapic_get_reg(vlapic, APIC_TASKPRI) & 0x0F));
     }
 
+    svm_vcpu_clear_tlb_control(v);
+
     exit_reason = vmcb->exitcode;
 
     if ( hvm_long_mode_active(v) )
xen/arch/x86/hvm/svm/svm.h +0 -4
index f5b0312d2d..92145c6d7b 100644
@@ -12,12 +12,8 @@
 #include <xen/types.h>
 
 struct cpu_user_regs;
-struct cpuinfo_x86;
 struct vcpu;
 
-void svm_asid_init(const struct cpuinfo_x86 *c);
-void svm_asid_handle_vmrun(void);
-
 unsigned long *svm_msrbit(unsigned long *msr_bitmap, uint32_t msr);
 void __update_guest_eip(struct cpu_user_regs *regs, unsigned int inst_len);
 
xen/arch/x86/hvm/vmx/vmcs.c +1 -4
index 57d49364db..01372c8ee2 100644
@@ -760,8 +760,6 @@ static int _vmx_cpu_up(bool bsp)
 
     this_cpu(vmxon) = 1;
 
-    hvm_asid_init(cpu_has_vmx_vpid ? (1u << VMCS_VPID_WIDTH) : 0);
-
     if ( cpu_has_vmx_ept )
         ept_sync_all();
 
@@ -1944,7 +1942,7 @@ void cf_check vmx_do_resume(void)
          */
         v->arch.hvm.vmx.hostenv_migrated = 1;
 
-        hvm_asid_flush_vcpu(v);
+        v->needs_tlb_flush = true;
     }
 
     debug_state = v->domain->debugger_attached
@@ -2157,7 +2155,6 @@ void vmcs_dump_vcpu(struct vcpu *v)
          (SECONDARY_EXEC_ENABLE_VPID | SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )
         printk("Virtual processor ID = 0x%04x VMfunc controls = %016lx\n",
                vmr16(VIRTUAL_PROCESSOR_ID), vmr(VM_FUNCTION_CONTROL));
-
     vmx_vmcs_exit(v);
 }
 
xen/arch/x86/hvm/vmx/vmx.c +34 -34

138 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +3 -2
index 3c848ef546..13d638a1ea 100644
@@ -12,6 +12,7 @@
 
 #include <asm/mtrr.h>
 #include <asm/p2m.h>
+#include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
 #include <asm/hvm/vmx/vmx.h>
 #include <asm/hvm/vmx/vvmx.h>
@@ -1254,7 +1255,7 @@ static void virtual_vmentry(struct cpu_user_regs *regs)
 
         if ( nvmx->guest_vpid != new_vpid )
         {
-            hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(v).nv_n2asid);
+            v->needs_tlb_flush = true;
             nvmx->guest_vpid = new_vpid;
         }
     }
@@ -2056,7 +2057,7 @@ static int nvmx_handle_invvpid(struct cpu_user_regs *regs)
     case INVVPID_INDIVIDUAL_ADDR:
     case INVVPID_SINGLE_CONTEXT:
     case INVVPID_ALL_CONTEXT:
-        hvm_asid_flush_vcpu_asid(&vcpu_nestedhvm(current).nv_n2asid);
+        hvm_flush_tlb(NULL);
         break;
     default:
         vmfail(regs, VMX_INSN_INVEPT_INVVPID_INVALID_OP);
xen/arch/x86/include/asm/flushtlb.h +0 -7
index cd625f9114..dc7327d051 100644
@@ -120,12 +120,6 @@ void switch_cr3_cr4(unsigned long cr3, unsigned long cr4);
 #define FLUSH_VCPU_STATE 0x1000
  /* Flush the per-cpu root page table */
 #define FLUSH_ROOT_PGTBL 0x2000
-#if CONFIG_HVM
- /* Flush all HVM guests linear TLB (using ASID/VPID) */
-#define FLUSH_HVM_ASID_CORE 0x4000
-#else
-#define FLUSH_HVM_ASID_CORE 0
-#endif
 #if defined(CONFIG_PV) || defined(CONFIG_SHADOW_PAGING)
 /*
  * Adding this to the flags passed to flush_area_mask will prevent using the
@@ -185,7 +179,6 @@ void flush_area_mask(const cpumask_t *mask, const void *va,
 
 static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) {}
 
-unsigned int guest_flush_tlb_flags(const struct domain *d);
 void guest_flush_tlb_mask(const struct domain *d, const cpumask_t *mask);
 
 #endif /* __FLUSHTLB_H__ */
xen/arch/x86/include/asm/hvm/asid.h +10 -15
index 17c58353d1..d59b3e3ea2 100644
@@ -8,25 +8,20 @@
 #ifndef __ASM_X86_HVM_ASID_H__
 #define __ASM_X86_HVM_ASID_H__
 
+#include <xen/stdbool.h>
+#include <xen/stdint.h>
 
-struct vcpu;
-struct hvm_vcpu_asid;
+struct hvm_asid {
+    uint32_t asid;
+};
 
-/* Initialise ASID management for the current physical CPU. */
-void hvm_asid_init(int nasids);
+extern bool asid_enabled;
 
-/* Invalidate a particular ASID allocation: forces re-allocation. */
-void hvm_asid_flush_vcpu_asid(struct hvm_vcpu_asid *asid);
+/* Initialise ASID management distributed across all CPUs. */
+int hvm_asid_init(unsigned long nasids);
 
-/* Invalidate all ASID allocations for specified VCPU: forces re-allocation. */
-void hvm_asid_flush_vcpu(struct vcpu *v);
-
-/* Flush all ASIDs on this processor core. */
-void hvm_asid_flush_core(void);
-
-/* Called before entry to guest context. Checks ASID allocation, returns a
- * boolean indicating whether all ASIDs must be flushed. */
-bool hvm_asid_handle_vmenter(struct hvm_vcpu_asid *asid);
+int hvm_asid_alloc(struct hvm_asid *asid);
+void hvm_asid_free(struct hvm_asid *asid);
 
 #endif /* __ASM_X86_HVM_ASID_H__ */
 
xen/arch/x86/include/asm/hvm/domain.h +1 -0
index 333501d5f2..80adc93b91 100644
@@ -140,6 +140,7 @@ struct hvm_domain {
     } write_map;
 
     struct hvm_pi_ops pi_ops;
+    struct hvm_asid asid;
 
     union {
         struct vmx_domain vmx;
xen/arch/x86/include/asm/hvm/hvm.h +2 -13
index bf8bc2e100..7af111cb39 100644
@@ -268,6 +268,8 @@ int hvm_domain_initialise(struct domain *d,
 void hvm_domain_relinquish_resources(struct domain *d);
 void hvm_domain_destroy(struct domain *d);
 
+int hvm_flush_tlb(const unsigned long *vcpu_bitmap);
+
 int hvm_vcpu_initialise(struct vcpu *v);
 void hvm_vcpu_destroy(struct vcpu *v);
 void hvm_vcpu_down(struct vcpu *v);
@@ -483,17 +485,6 @@ static inline void hvm_set_tsc_offset(struct vcpu *v, uint64_t offset,
     alternative_vcall(hvm_funcs.set_tsc_offset, v, offset, at_tsc);
 }
 
-/*
- * Called to ensure than all guest-specific mappings in a tagged TLB are 
- * flushed; does *not* flush Xen's TLB entries, and on processors without a 
- * tagged TLB it will be a noop.
- */
-static inline void hvm_flush_guest_tlbs(void)
-{
-    if ( hvm_enabled )
-        hvm_asid_flush_core();
-}
-
 static inline unsigned int
 hvm_get_cpl(struct vcpu *v)
 {
@@ -881,8 +872,6 @@ static inline int hvm_cpu_up(void)
 
 static inline void hvm_cpu_down(void) {}
 
-static inline void hvm_flush_guest_tlbs(void) {}
-
 static inline void hvm_invlpg(const struct vcpu *v, unsigned long linear)
 {
     ASSERT_UNREACHABLE();
xen/arch/x86/include/asm/hvm/svm/svm.h +5 -0
index 4eeeb25da9..a610ebbb17 100644
@@ -9,6 +9,11 @@
 #ifndef __ASM_X86_HVM_SVM_H__
 #define __ASM_X86_HVM_SVM_H__
 
+void svm_asid_init(void);
+void svm_vcpu_assign_asid(struct vcpu *v);
+void svm_vcpu_set_tlb_control(struct vcpu *v);
+void svm_vcpu_clear_tlb_control(struct vcpu *v);
+
 /*
  * PV context switch helpers.  Prefetching the VMCB area itself has been shown
  * to be useful for performance.
xen/arch/x86/include/asm/hvm/vcpu.h +2 -8
index 196fed6d5d..960bea6734 100644
@@ -9,6 +9,7 @@
 #define __ASM_X86_HVM_VCPU_H__
 
 #include <xen/tasklet.h>
+#include <asm/hvm/asid.h>
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vmx/vmcs.h>
 #include <asm/hvm/vmx/vvmx.h>
@@ -17,11 +18,6 @@
 #include <asm/mtrr.h>
 #include <public/hvm/ioreq.h>
 
-struct hvm_vcpu_asid {
-    uint64_t generation;
-    uint32_t asid;
-};
-
 struct hvm_vcpu_io {
     /*
      * HVM emulation:
@@ -79,7 +75,7 @@ struct nestedvcpu {
     bool stale_np2m; /* True when p2m_base in VMCx02 is no longer valid */
     uint64_t np2m_generation;
 
-    struct hvm_vcpu_asid nv_n2asid;
+    struct hvm_asid nv_n2asid;
 
     bool nv_vmentry_pending;
     bool nv_vmexit_pending;
@@ -141,8 +137,6 @@ struct hvm_vcpu {
     /* (MFN) hypervisor page table */
     pagetable_t         monitor_table;
 
-    struct hvm_vcpu_asid n1asid;
-
     u64                 msr_tsc_adjust;
 
     union {
xen/arch/x86/include/asm/hvm/vmx/vmx.h +2 -2
index 173311bfbb..2938cc245f 100644
@@ -462,7 +462,7 @@ static inline void vpid_sync_vcpu_context(struct vcpu *v)
     else
         type = INVVPID_ALL_CONTEXT;
 
-    __invvpid(type, v->arch.hvm.n1asid.asid, 0);
+    __invvpid(type, v->domain->arch.hvm.asid.asid, 0);
 }
 
 static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
@@ -477,7 +477,7 @@ static inline void vpid_sync_vcpu_gva(struct vcpu *v, unsigned long gva)
     else
         type = INVVPID_ALL_CONTEXT;
 
-    __invvpid(type, v->arch.hvm.n1asid.asid, (u64)gva);
+    __invvpid(type, v->domain->arch.hvm.asid.asid, (u64)gva);
 }
 
 static inline void vpid_sync_all(void)
xen/arch/x86/mm/hap/hap.c +2 -4
index ec5043a8aa..b1d7afee1d 100644
@@ -739,7 +739,7 @@ static bool cf_check flush_tlb(const unsigned long *vcpu_bitmap)
         if ( !flush_vcpu(v, vcpu_bitmap) )
             continue;
 
-        hvm_asid_flush_vcpu(v);
+        v->needs_tlb_flush = true;
 
         cpu = read_atomic(&v->dirty_cpu);
         if ( cpu != this_cpu && is_vcpu_dirty_cpu(cpu) && v->is_running )
@@ -748,9 +748,7 @@ static bool cf_check flush_tlb(const unsigned long *vcpu_bitmap)
 
     /*
      * Trigger a vmexit on all pCPUs with dirty vCPU state in order to force an
-     * ASID/VPID change and hence accomplish a guest TLB flush. Note that vCPUs
-     * not currently running will already be flushed when scheduled because of
-     * the ASID tickle done in the loop above.
+     * ASID/VPID flush and hence accomplish a guest TLB flush.
      */
     on_selected_cpus(mask, NULL, NULL, 0);
 
xen/arch/x86/mm/p2m.c +4 -3
index b9a7c2dc53..857968ea0d 100644
@@ -25,6 +25,7 @@
 #include <asm/p2m.h>
 #include <asm/mem_sharing.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/vcpu.h>
 #include <asm/altp2m.h>
 #include <asm/vm_event.h>
 #include <xsm/xsm.h>
@@ -1416,7 +1417,7 @@ p2m_flush(struct vcpu *v, struct p2m_domain *p2m)
     ASSERT(v->domain == p2m->domain);
     vcpu_nestedhvm(v).nv_p2m = NULL;
     p2m_flush_table(p2m);
-    hvm_asid_flush_vcpu(v);
+    v->needs_tlb_flush = true;
 }
 
 void
@@ -1475,7 +1476,7 @@ static void assign_np2m(struct vcpu *v, struct p2m_domain *p2m)
 
 static void nvcpu_flush(struct vcpu *v)
 {
-    hvm_asid_flush_vcpu(v);
+    v->needs_tlb_flush = true;
     vcpu_nestedhvm(v).stale_np2m = true;
 }
 
@@ -1595,7 +1596,7 @@ void np2m_schedule(int dir)
             if ( !np2m_valid )
             {
                 /* This vCPU's np2m was flushed while it was not runnable */
-                hvm_asid_flush_core();
+                curr->needs_tlb_flush = true;
                 vcpu_nestedhvm(curr).nv_p2m = NULL;
             }
             else
xen/arch/x86/mm/paging.c +1 -1
index c77f4c1dac..26b6ce9e9b 100644
@@ -964,7 +964,7 @@ void paging_update_nestedmode(struct vcpu *v)
     else
         /* TODO: shadow-on-shadow */
         v->arch.paging.nestedmode = NULL;
-    hvm_asid_flush_vcpu(v);
+    v->needs_tlb_flush = true;
 }
 
 int __init paging_set_allocation(struct domain *d, unsigned int pages,
xen/arch/x86/mm/shadow/hvm.c +1 -0
index 114957a3e1..f98591f976 100644
@@ -737,6 +737,7 @@ bool cf_check shadow_flush_tlb(const unsigned long *vcpu_bitmap)
             continue;
 
         paging_update_cr3(v, false);
+        v->needs_tlb_flush = true;
 
         cpu = read_atomic(&v->dirty_cpu);
         if ( is_vcpu_dirty_cpu(cpu) )
xen/arch/x86/mm/shadow/multi.c +4 -8
index 7be9c180ec..b55f4bca08 100644
@@ -79,12 +79,6 @@ const char *const fetch_type_names[] = {
 # define for_each_shadow_table(v, i) for ( (i) = 0; (i) < 1; ++(i) )
 #endif
 
-/* Helper to perform a local TLB flush. */
-static void sh_flush_local(const struct domain *d)
-{
-    flush_local(guest_flush_tlb_flags(d));
-}
-
 #if GUEST_PAGING_LEVELS >= 4 && defined(CONFIG_PV32)
 #define ASSERT_VALID_L2(t) \
     ASSERT((t) == SH_type_l2_shadow || (t) == SH_type_l2h_shadow)
@@ -2926,7 +2920,8 @@ static bool cf_check sh_invlpg(struct vcpu *v, unsigned long linear)
     if ( mfn_to_page(sl1mfn)->u.sh.type
          == SH_type_fl1_shadow )
     {
-        sh_flush_local(v->domain);
+        v->needs_tlb_flush = true;
+        flush_tlb_local();
         return false;
     }
 
@@ -3143,7 +3138,8 @@ sh_update_linear_entries(struct vcpu *v)
      * linear pagetable to read a top-level shadow page table entry. But,
      * without this change, it would fetch the wrong value due to a stale TLB.
      */
-    sh_flush_local(d);
+    v->needs_tlb_flush = true;
+    flush_tlb_local();
 }
 
 static pagetable_t cf_check sh_update_cr3(struct vcpu *v, bool noflush)
xen/include/xen/sched.h +2 -0
index fe53d4fab7..55b0561988 100644
@@ -228,6 +228,8 @@ struct vcpu
     bool             defer_shutdown;
     /* VCPU is paused following shutdown request (d->is_shutting_down)? */
     bool             paused_for_shutdown;
+    /* VCPU needs its TLB flushed before waking. */
+    bool             needs_tlb_flush;
     /* VCPU need affinity restored */
     uint8_t          affinity_broken;
 #define VCPU_AFFINITY_OVERRIDE    0x01
Signature · 9 lines
2.50.0



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#8 lore raw
Quoted 14 lines · Teddy Astie

From: Vaishali Thakkar <[email protected]> (formely vates.tech)

Currently ASID generation and management is done per-PCPU. This scheme is incompatible with SEV technologies as SEV VMs need to have a fixed ASID associated with all vcpus of the VM throughout it's lifetime.

This commit introduces a Xen-wide allocator which initializes the asids at the start of xen and allows to have a fixed asids throughout the lifecycle of all domains. Having a fixed asid for non-SEV domains also presents us with the opportunity to further take use of AMD instructions like TLBSYNC and INVLPGB for broadcasting the TLB invalidations.

Quoted 7 lines

Introduce vcpu->needs_tlb_flush attribute to schedule a guest TLB flush for the next VMRUN/VMENTER. This will be later be done using either TLB_CONTROL field (AMD) or INVEPT (Intel). This flush method is used in place of the current ASID swapping logic.

Not sure whether you may legitimately alter pre-existing S-o-b. In any event the two S-o-b looks to be in the wrong order; like most other tags they want to be sorted chronologically.

Quoted 5 lines

---
CC: Oleksii Kurochko <[email protected]>

Should the ASID/VPID/VMID management logic code being shared accross x86/ARM/RISC-V ?

If all present and future architectures agree on how exactly they want to use these IDs. Which I'm unsure of. RISC-V is now vaguely following the original x86 model.

Quoted 4 lines

Is it possible to have multiples vCPUs of a same domain simultaneously scheduled on top of a single pCPU ? If so, it would need a special consideration for this corner case, such as we don't miss a TLB flush in such cases.

No, how would two entities be able to run on a single pCPU at any single point in time?

Quoted 10 lines

I get various stability when testing shadow paging in these patches, unsure what's the exact root case. HAP works perfectly fine though.

TODO:
- Intel: Don't assign the VPID at each VMENTER, though we need to rethink how we manage VMCS with nested virtualization / altp2m for changing this behavior.
- AMD: Consider hot-plug of CPU with ERRATA_170. (is it possible ?)
- Consider cases where we don't have enough ASIDs (e.g Xen as nested guest)
- Nested virtualization ASID management

For these last two points - maybe we really need a mixed model?

Quoted 19 lines
---
 xen/arch/x86/flushtlb.c                |  31 +++---
 xen/arch/x86/hvm/asid.c                | 148 +++++++++----------------
 xen/arch/x86/hvm/emulate.c             |   2 +-
 xen/arch/x86/hvm/hvm.c                 |  14 ++-
 xen/arch/x86/hvm/nestedhvm.c           |   6 +-
 xen/arch/x86/hvm/svm/asid.c            |  77 ++++++++-----
 xen/arch/x86/hvm/svm/nestedsvm.c       |   1 -
 xen/arch/x86/hvm/svm/svm.c             |  36 +++---
 xen/arch/x86/hvm/svm/svm.h             |   4 -
 xen/arch/x86/hvm/vmx/vmcs.c            |   5 +-
 xen/arch/x86/hvm/vmx/vmx.c             |  68 ++++++------
 xen/arch/x86/hvm/vmx/vvmx.c            |   5 +-
 xen/arch/x86/include/asm/flushtlb.h    |   7 --
 xen/arch/x86/include/asm/hvm/asid.h    |  25 ++---
 xen/arch/x86/include/asm/hvm/domain.h  |   1 +
 xen/arch/x86/include/asm/hvm/hvm.h     |  15 +--
 xen/arch/x86/include/asm/hvm/svm/svm.h |   5 +
 xen/arch/x86/include/asm/hvm/vcpu.h    |  10 +-
Quoted 8 lines
 xen/arch/x86/include/asm/hvm/vmx/vmx.h |   4 +-
 xen/arch/x86/mm/hap/hap.c              |   6 +-
 xen/arch/x86/mm/p2m.c                  |   7 +-
 xen/arch/x86/mm/paging.c               |   2 +-
 xen/arch/x86/mm/shadow/hvm.c           |   1 +
 xen/arch/x86/mm/shadow/multi.c         |  12 +-
 xen/include/xen/sched.h                |   2 +
 25 files changed, 227 insertions(+), 267 deletions(-)

I think I said this to Vaishali already: This really wants splitting up some, to become halfway reviewable.

Jan

#9 lore raw

Le 28/08/2025 à 15:05, Jan Beulich a écrit :

Quoted 20 lines · Teddy Astie
Quoted 19 lines

From: Vaishali Thakkar <[email protected]> (formely vates.tech)

Currently ASID generation and management is done per-PCPU. This scheme is incompatible with SEV technologies as SEV VMs need to have a fixed ASID associated with all vcpus of the VM throughout it's lifetime.

This commit introduces a Xen-wide allocator which initializes the asids at the start of xen and allows to have a fixed asids throughout the lifecycle of all domains. Having a fixed asid for non-SEV domains also presents us with the opportunity to further take use of AMD instructions like TLBSYNC and INVLPGB for broadcasting the TLB invalidations.

Introduce vcpu->needs_tlb_flush attribute to schedule a guest TLB flush for the next VMRUN/VMENTER. This will be later be done using either TLB_CONTROL field (AMD) or INVEPT (Intel). This flush method is used in place of the current ASID swapping logic.

Quoted 7 lines
Quoted 2 lines

Not sure whether you may legitimately alter pre-existing S-o-b. In any event the two S-o-b looks to be in the wrong order; like most other tags they want to be sorted chronologically.

Yes, I misordered it, will fix it for the next version.

Quoted 10 lines
Quoted 5 lines

---
CC: Oleksii Kurochko <[email protected]>

Should the ASID/VPID/VMID management logic code being shared accross x86/ARM/RISC-V ?

If all present and future architectures agree on how exactly they want to use these IDs. Which I'm unsure of. RISC-V is now vaguely following the original x86 model.

Quoted 8 lines
Quoted 4 lines

Is it possible to have multiples vCPUs of a same domain simultaneously scheduled on top of a single pCPU ? If so, it would need a special consideration for this corner case, such as we don't miss a TLB flush in such cases.

No, how would two entities be able to run on a single pCPU at any single point in time?

It was more concerning regarding having 2 vCPUs of the same domain (thus sharing the same ASID) running consecutively, e.g having on the same core
dom1.vcpu1 -> dom1.vcpu2

without a appropriate TLB flush; because the address space may not be consistent between 2 vCPUs.

I found a approach to fix it by tracking per domain which vCPU last ran on each pCPU so that in case of mismatch, we need to TLB flush. Along with explictely flush the TLB when the vCPU is migrated, because in the case too the TLB can be inconsistent for the ASID.

Quoted 11 lines
Quoted 10 lines

I get various stability when testing shadow paging in these patches, unsure what's the exact root case. HAP works perfectly fine though.

TODO:
- Intel: Don't assign the VPID at each VMENTER, though we need to rethink how we manage VMCS with nested virtualization / altp2m for changing this behavior.
- AMD: Consider hot-plug of CPU with ERRATA_170. (is it possible ?)
- Consider cases where we don't have enough ASIDs (e.g Xen as nested guest)
- Nested virtualization ASID management

Quoted 2 lines

For these last two points - maybe we really need a mixed model?

Mixed model would not allow future support for broadcast TLB invalidation (even for hypervisor use) with e.g AMD INVLPGB or (future) Intel Remote Action Request.

Quoted 28 lines
Quoted 19 lines
---
  xen/arch/x86/flushtlb.c                |  31 +++---
  xen/arch/x86/hvm/asid.c                | 148 +++++++++----------------
  xen/arch/x86/hvm/emulate.c             |   2 +-
  xen/arch/x86/hvm/hvm.c                 |  14 ++-
  xen/arch/x86/hvm/nestedhvm.c           |   6 +-
  xen/arch/x86/hvm/svm/asid.c            |  77 ++++++++-----
  xen/arch/x86/hvm/svm/nestedsvm.c       |   1 -
  xen/arch/x86/hvm/svm/svm.c             |  36 +++---
  xen/arch/x86/hvm/svm/svm.h             |   4 -
  xen/arch/x86/hvm/vmx/vmcs.c            |   5 +-
  xen/arch/x86/hvm/vmx/vmx.c             |  68 ++++++------
  xen/arch/x86/hvm/vmx/vvmx.c            |   5 +-
  xen/arch/x86/include/asm/flushtlb.h    |   7 --
  xen/arch/x86/include/asm/hvm/asid.h    |  25 ++---
  xen/arch/x86/include/asm/hvm/domain.h  |   1 +
  xen/arch/x86/include/asm/hvm/hvm.h     |  15 +--
  xen/arch/x86/include/asm/hvm/svm/svm.h |   5 +
  xen/arch/x86/include/asm/hvm/vcpu.h    |  10 +-
Quoted 8 lines
  xen/arch/x86/include/asm/hvm/vmx/vmx.h |   4 +-
  xen/arch/x86/mm/hap/hap.c              |   6 +-
  xen/arch/x86/mm/p2m.c                  |   7 +-
  xen/arch/x86/mm/paging.c               |   2 +-
  xen/arch/x86/mm/shadow/hvm.c           |   1 +
  xen/arch/x86/mm/shadow/multi.c         |  12 +-
  xen/include/xen/sched.h                |   2 +
  25 files changed, 227 insertions(+), 267 deletions(-)
Quoted 3 lines

I think I said this to Vaishali already: This really wants splitting up some, to become halfway reviewable.

Indeed, I am looking for a approach for splitting the change without breaking things in-between patches.

Quoted 2 lines

Jan

Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#10 lore raw
Quoted 15 lines · Teddy Astie

Le 28/08/2025 à 15:05, Jan Beulich a écrit :

Quoted 9 lines · Teddy Astie
Quoted 4 lines

Is it possible to have multiples vCPUs of a same domain simultaneously scheduled on top of a single pCPU ? If so, it would need a special consideration for this corner case, such as we don't miss a TLB flush in such cases.

No, how would two entities be able to run on a single pCPU at any single point in time?

It was more concerning regarding having 2 vCPUs of the same domain (thus sharing the same ASID) running consecutively, e.g having on the same core
dom1.vcpu1 -> dom1.vcpu2

Quoted 7 lines

without a appropriate TLB flush; because the address space may not be consistent between 2 vCPUs.

I found a approach to fix it by tracking per domain which vCPU last ran on each pCPU so that in case of mismatch, we need to TLB flush. Along with explictely flush the TLB when the vCPU is migrated, because in the case too the TLB can be inconsistent for the ASID.

Yet that wants constraining to the case where the address spaces are indeed at risk of being different, i.e. when distinct P2Ms are in use on the two vCPU-s.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

I get various stability when testing shadow paging in these patches, unsure what's the exact root case. HAP works perfectly fine though.

TODO:
- Intel: Don't assign the VPID at each VMENTER, though we need to rethink how we manage VMCS with nested virtualization / altp2m for changing this behavior.
- AMD: Consider hot-plug of CPU with ERRATA_170. (is it possible ?)
- Consider cases where we don't have enough ASIDs (e.g Xen as nested guest)
- Nested virtualization ASID management

Quoted 6 lines
Quoted 2 lines

For these last two points - maybe we really need a mixed model?

Mixed model would not allow future support for broadcast TLB invalidation (even for hypervisor use) with e.g AMD INVLPGB or (future) Intel Remote Action Request.

Why? For a VM using the traditional model we wouldn't be able to leverage those, but for others we could. And imo it's better to be able to run a VM at all, even if not with all possible accelerations, than to refuse running it.

Jan