Skip to contentSkip to conversation

x86/hvm: vmx: refactor cache disable mode code

PATCH Superseded Grygorii Strashko 2 participants 14 messages Oct 29, 2025 – Nov 6, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/hvm: vmx: refactor cache disable mode code

From: Grygorii Strashko <[email protected]>

The cache disable mode code (hvm_shadow_handle_cd()) is relevant only for Intel VT-x (VMX), hence:
- move hvm_shadow_handle_cd()/ hvm_set_uc_mode()/domain_exit_uc_mode() in VMX code
- account for SHADOW_PAGING when use hvm_shadow_handle_cd()
- move cache disable mode data into VMX specific structures

Based on discussion [1].

[1] https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/

Grygorii Strashko (3):
x86/hvm: move hvm_shadow_handle_cd() in vmx code x86/hvm: vmx: account for SHADOW_PAGING when use
hvm_shadow_handle_cd()
x86/hvm: vmx: refactor cache disable mode data

 xen/arch/x86/hvm/hvm.c                  | 59 ---------------------
 xen/arch/x86/hvm/vmx/vmx.c              | 70 +++++++++++++++++++++++--
 xen/arch/x86/include/asm/hvm/domain.h   |  6 ---
 xen/arch/x86/include/asm/hvm/hvm.h      |  3 ++
 xen/arch/x86/include/asm/hvm/support.h  |  2 -
 xen/arch/x86/include/asm/hvm/vcpu.h     |  3 --
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 14 +++++
 xen/arch/x86/include/asm/mtrr.h         |  3 --
 xen/arch/x86/mm/shadow/multi.c          |  2 +-
 9 files changed, 83 insertions(+), 79 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 x86/hvm: move hvm_shadow_handle_cd() in vmx code

From: Grygorii Strashko <[email protected]>

Functions:
hvm_shadow_handle_cd()
hvm_set_uc_mode()
domain_exit_uc_mode()
are used only by Intel VMX code, so move them in VMX code.

While here:
- minor format change in domain_exit_uc_mode()
- s/(0/1)/(false/true) for bool types

No functional changes.

---
 xen/arch/x86/hvm/hvm.c                 | 58 -------------------------
 xen/arch/x86/hvm/vmx/vmx.c             | 59 ++++++++++++++++++++++++++
 xen/arch/x86/include/asm/hvm/support.h |  2 -
 3 files changed, 59 insertions(+), 60 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -58
index da3cde1ff0e6..9caca93e5f56 100644
@@ -2168,30 +2168,6 @@ int hvm_set_efer(uint64_t value)
     return X86EMUL_OKAY;
 }
 
-/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
-static bool domain_exit_uc_mode(struct vcpu *v)
-{
-    struct domain *d = v->domain;
-    struct vcpu *vs;
-
-    for_each_vcpu ( d, vs )
-    {
-        if ( (vs == v) || !vs->is_initialised )
-            continue;
-        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
-             mtrr_pat_not_equal(vs, v) )
-            return 0;
-    }
-
-    return 1;
-}
-
-static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
-{
-    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
-    shadow_blow_tables_per_domain(v->domain);
-}
-
 int hvm_mov_to_cr(unsigned int cr, unsigned int gpr)
 {
     struct vcpu *curr = current;
@@ -2273,40 +2249,6 @@ int hvm_mov_from_cr(unsigned int cr, unsigned int gpr)
     return X86EMUL_UNHANDLEABLE;
 }
 
-void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
-{
-    if ( value & X86_CR0_CD )
-    {
-        /* Entering no fill cache mode. */
-        spin_lock(&v->domain->arch.hvm.uc_lock);
-        v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
-
-        if ( !v->domain->arch.hvm.is_in_uc_mode )
-        {
-            domain_pause_nosync(v->domain);
-
-            /* Flush physical caches. */
-            flush_all(FLUSH_CACHE_EVICT);
-            hvm_set_uc_mode(v, 1);
-
-            domain_unpause(v->domain);
-        }
-        spin_unlock(&v->domain->arch.hvm.uc_lock);
-    }
-    else if ( !(value & X86_CR0_CD) &&
-              (v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
-    {
-        /* Exit from no fill cache mode. */
-        spin_lock(&v->domain->arch.hvm.uc_lock);
-        v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
-
-        if ( domain_exit_uc_mode(v) )
-            hvm_set_uc_mode(v, 0);
-
-        spin_unlock(&v->domain->arch.hvm.uc_lock);
-    }
-}
-
 static void hvm_update_cr(struct vcpu *v, unsigned int cr, unsigned long value)
 {
     v->arch.hvm.guest_cr[cr] = value;
xen/arch/x86/hvm/vmx/vmx.c +59 -0

71 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/support.h +0 -2
index 2a7ba36af06f..9e9fa6295567 100644
@@ -119,8 +119,6 @@ void hvm_rdtsc_intercept(struct cpu_user_regs *regs);
 
 int __must_check hvm_handle_xsetbv(u32 index, u64 new_bv);
 
-void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value);
-
 /*
  * These functions all return X86EMUL return codes.  For hvm_set_*(), the
  * caller is responsible for injecting #GP[0] if X86EMUL_EXCEPTION is
Signature · 1 line
2.34.1
#2 lore raw

Re: [XEN][PATCH 1/3] x86/hvm: move hvm_shadow_handle_cd() in vmx code

Quoted 7 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

Functions:
hvm_shadow_handle_cd()
hvm_set_uc_mode()
domain_exit_uc_mode()
are used only by Intel VMX code, so move them in VMX code.

Nit: I think both in the title and here you mean "to" or "into".

Quoted 7 lines

While here:
- minor format change in domain_exit_uc_mode()
- s/(0/1)/(false/true) for bool types

No functional changes.

You did read Andrew's request to also move the involved structure field(s), didn't you? Oh, wait - maybe that's going to be the subject of patch 3. While often splitting steps helps, I'm not sure that's very useful here. You're touching again immediately what you just have moved, all to reach a single goal.

Quoted 17 lines
xen/arch/x86/hvm/vmx/vmx.c +13 -0
@@ -1421,6 +1422,64 @@ static void cf_check vmx_set_segment_register(
     vmx_vmcs_exit(v);
 }
 
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu(d, vs)
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return false;

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1447

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+    }
+
+    return true;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}

Similarly I wonder whether this function wouldn't better change to taking struct domain * right away. "v" itself is only ever used to get hold of its domain. At the call sites this will then make obvious that this is a domain-wide operation.

Quoted 23 lines
xen/arch/x86/hvm/vmx/vmx.c +23 -0
+static void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
+{
+    if ( value & X86_CR0_CD )
+    {
+        /* Entering no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
+
+        if ( !v->domain->arch.hvm.is_in_uc_mode )
+        {
+            domain_pause_nosync(v->domain);
+
+            /* Flush physical caches. */
+            flush_all(FLUSH_CACHE_EVICT);
+            hvm_set_uc_mode(v, true);
+
+            domain_unpause(v->domain);
+        }
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+    else if ( !(value & X86_CR0_CD) &&
+              (v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+    {

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1481

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+        /* Exit from no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
+
+        if ( domain_exit_uc_mode(v) )
+            hvm_set_uc_mode(v, false);
+
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+}

This function, in turn, could do with a local struct domain *d.

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1485

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
 static int cf_check vmx_set_guest_pat(struct vcpu *v, u64 gpat)
 {
     if ( !paging_mode_hap(v->domain) ||

Why did you put the code above this function? It's solely a helper of vmx_handle_cd(), so would imo best be placed immediately ahead of that one.

Bottom line: The change could go in as is, but imo it would be nice if it was tidied some while moving.

Jan

#3 lore raw

Re: [XEN][PATCH 1/3] x86/hvm: move hvm_shadow_handle_cd() in vmx code

Hi Jan,

Quoted 15 lines · Jan Beulich
Quoted 7 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

Functions:
hvm_shadow_handle_cd()
hvm_set_uc_mode()
domain_exit_uc_mode()
are used only by Intel VMX code, so move them in VMX code.

Nit: I think both in the title and here you mean "to" or "into".

Quoted 4 lines

While here:
- minor format change in domain_exit_uc_mode()
- s/(0/1)/(false/true) for bool types

Quoted 6 lines
Quoted 3 lines

No functional changes.

You did read Andrew's request to also move the involved structure field(s), didn't you? Oh, wait - maybe that's going to be the subject of patch 3.

yes. it is patch 3 - It is not small.
And I really wanted this patch to contain as less modifications as possible on top of code moving.

Quoted 29 lines

While
often splitting steps helps, I'm not sure that's very useful here. You're touching again immediately what you just have moved, all to reach a single goal.

Quoted 24 lines
xen/arch/x86/hvm/vmx/vmx.c +20 -0
@@ -1421,6 +1422,64 @@ static void cf_check vmx_set_segment_register(
      vmx_vmcs_exit(v);
  }
 
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu(d, vs)
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return false;
+    }
+
+    return true;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1447

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +3 -0
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}

Similarly I wonder whether this function wouldn't better change to taking struct domain * right away. "v" itself is only ever used to get hold of its domain. At the call sites this will then make obvious that this is a domain-wide operation.

Agree. but..
In this patch I wanted to minimize changes and do modifications step by step.

I can add additional patch such as "rework struct domain access in cache disable mode code". Will it work?

Quoted 37 lines
Quoted 24 lines
xen/arch/x86/hvm/vmx/vmx.c +23 -0
+static void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
+{
+    if ( value & X86_CR0_CD )
+    {
+        /* Entering no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
+
+        if ( !v->domain->arch.hvm.is_in_uc_mode )
+        {
+            domain_pause_nosync(v->domain);
+
+            /* Flush physical caches. */
+            flush_all(FLUSH_CACHE_EVICT);
+            hvm_set_uc_mode(v, true);
+
+            domain_unpause(v->domain);
+        }
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+    else if ( !(value & X86_CR0_CD) &&
+              (v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+    {

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1481

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+        /* Exit from no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
+
+        if ( domain_exit_uc_mode(v) )
+            hvm_set_uc_mode(v, false);
+
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+}

This function, in turn, could do with a local struct domain *d.

Quoted 6 lines

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1485

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
  static int cf_check vmx_set_guest_pat(struct vcpu *v, u64 gpat)
  {
      if ( !paging_mode_hap(v->domain) ||

Why did you put the code above this function? It's solely a helper of vmx_handle_cd(), so would imo best be placed immediately ahead of that one.

Right. Hence vmx_x_guest_pat() are also used by vmx_handle_cd() I decided to put before them.

Quoted 3 lines

Bottom line: The change could go in as is, but imo it would be nice if it was tidied some while moving.

I'd be very much appreciated if this could happen.

Signature · 2 lines
Best regards,
-grygorii
#4 lore raw

Re: [XEN][PATCH 1/3] x86/hvm: move hvm_shadow_handle_cd() in vmx code

Quoted 18 lines · Grygorii Strashko
Quoted 17 lines · Jan Beulich
Quoted 7 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

Functions:
hvm_shadow_handle_cd()
hvm_set_uc_mode()
domain_exit_uc_mode()
are used only by Intel VMX code, so move them in VMX code.

Nit: I think both in the title and here you mean "to" or "into".

Quoted 6 lines

While here:
- minor format change in domain_exit_uc_mode()
- s/(0/1)/(false/true) for bool types

No functional changes.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

You did read Andrew's request to also move the involved structure field(s), didn't you? Oh, wait - maybe that's going to be the subject of patch 3.

yes. it is patch 3 - It is not small.
And I really wanted this patch to contain as less modifications as possible on top of code moving.

I wonder what other x86 maintainers think here.

Quoted 81 lines
Quoted 29 lines

While
often splitting steps helps, I'm not sure that's very useful here. You're touching again immediately what you just have moved, all to reach a single goal.

Quoted 24 lines
xen/arch/x86/hvm/vmx/vmx.c +20 -0
@@ -1421,6 +1422,64 @@ static void cf_check vmx_set_segment_register(
      vmx_vmcs_exit(v);
  }
 
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu(d, vs)
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return false;
+    }
+
+    return true;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1447

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +3 -0
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}

Similarly I wonder whether this function wouldn't better change to taking struct domain * right away. "v" itself is only ever used to get hold of its domain. At the call sites this will then make obvious that this is a domain-wide operation.

Agree. but..
In this patch I wanted to minimize changes and do modifications step by step.

I can add additional patch such as "rework struct domain access in cache disable mode code". Will it work?

Quoted 37 lines
Quoted 24 lines
xen/arch/x86/hvm/vmx/vmx.c +23 -0
+static void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
+{
+    if ( value & X86_CR0_CD )
+    {
+        /* Entering no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
+
+        if ( !v->domain->arch.hvm.is_in_uc_mode )
+        {
+            domain_pause_nosync(v->domain);
+
+            /* Flush physical caches. */
+            flush_all(FLUSH_CACHE_EVICT);
+            hvm_set_uc_mode(v, true);
+
+            domain_unpause(v->domain);
+        }
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+    else if ( !(value & X86_CR0_CD) &&
+              (v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+    {

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1481

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+        /* Exit from no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
+
+        if ( domain_exit_uc_mode(v) )
+            hvm_set_uc_mode(v, false);
+
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+}

This function, in turn, could do with a local struct domain *d.

Quoted 8 lines
Quoted 6 lines

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1485

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
  static int cf_check vmx_set_guest_pat(struct vcpu *v, u64 gpat)
  {
      if ( !paging_mode_hap(v->domain) ||

Why did you put the code above this function? It's solely a helper of vmx_handle_cd(), so would imo best be placed immediately ahead of that one.

Right. Hence vmx_x_guest_pat() are also used by vmx_handle_cd() I decided to put before them.

The main purpose of vmx_set_guest_pat() is, however, its use as a hook function. It's merely an optimization that the function is called directly by VMX code.

Quoted 4 lines
Quoted 2 lines

Bottom line: The change could go in as is, but imo it would be nice if it was tidied some while moving.

I'd be very much appreciated if this could happen.

"this" being what out of the two or more possible options? (I take it you mean "could go in as is", but that's guesswork.)

Jan

#5 lore raw

Re: [XEN][PATCH 1/3] x86/hvm: move hvm_shadow_handle_cd() in vmx code

Hi

Quoted 21 lines · Jan Beulich
Quoted 20 lines · Grygorii Strashko
Quoted 19 lines · Jan Beulich
Quoted 7 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

Functions:

   hvm_shadow_handle_cd()
   hvm_set_uc_mode()
   domain_exit_uc_mode()

are used only by Intel VMX code, so move them in VMX code.

Nit: I think both in the title and here you mean "to" or "into".

Quoted 7 lines

While here:
- minor format change in domain_exit_uc_mode()
- s/(0/1)/(false/true) for bool types

No functional changes.

Quoted 52 lines
Quoted 6 lines
Quoted 2 lines

You did read Andrew's request to also move the involved structure field(s), didn't you? Oh, wait - maybe that's going to be the subject of patch 3.

yes. it is patch 3 - It is not small.
And I really wanted this patch to contain as less modifications as possible on top of code moving.

I wonder what other x86 maintainers think here.

Quoted 43 lines
Quoted 29 lines

While
often splitting steps helps, I'm not sure that's very useful here. You're touching again immediately what you just have moved, all to reach a single goal.

Quoted 24 lines
xen/arch/x86/hvm/vmx/vmx.c +20 -0
@@ -1421,6 +1422,64 @@ static void cf_check vmx_set_segment_register(
       vmx_vmcs_exit(v);
   }
 
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu(d, vs)
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return false;
+    }
+
+    return true;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1447

Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +3 -0
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}

Similarly I wonder whether this function wouldn't better change to taking struct domain * right away. "v" itself is only ever used to get hold of its domain. At the call sites this will then make obvious that this is a domain-wide operation.

Agree. but..
In this patch I wanted to minimize changes and do modifications step by step.

I can add additional patch such as "rework struct domain access in cache disable mode code". Will it work?

I'm planning to resend with:
- incorporating struct domain * as parameter in hvm_set_uc_mode()

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/x86/hvm/vmx/vmx.c +27 -0
+static void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
+{
+    if ( value & X86_CR0_CD )
+    {
+        /* Entering no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
+
+        if ( !v->domain->arch.hvm.is_in_uc_mode )
+        {
+            domain_pause_nosync(v->domain);
+
+            /* Flush physical caches. */
+            flush_all(FLUSH_CACHE_EVICT);
+            hvm_set_uc_mode(v, true);
+
+            domain_unpause(v->domain);
+        }
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+    else if ( !(value & X86_CR0_CD) &&
+              (v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+    {
+        /* Exit from no fill cache mode. */
+        spin_lock(&v->domain->arch.hvm.uc_lock);
+        v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_segment_register( · line 1481

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+        if ( domain_exit_uc_mode(v) )
+            hvm_set_uc_mode(v, false);
+
+        spin_unlock(&v->domain->arch.hvm.uc_lock);
+    }
+}

This function, in turn, could do with a local struct domain *d.

- incorporating struct domain * as parameter local var

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
   static int cf_check vmx_set_guest_pat(struct vcpu *v, u64 gpat)
   {
       if ( !paging_mode_hap(v->domain) ||
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Why did you put the code above this function? It's solely a helper of vmx_handle_cd(), so would imo best be placed immediately ahead of that one.

Right. Hence vmx_x_guest_pat() are also used by vmx_handle_cd() I decided to put before them.

The main purpose of vmx_set_guest_pat() is, however, its use as a hook function. It's merely an optimization that the function is called directly by VMX code.

- moving code before vmx_handle_cd().

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Bottom line: The change could go in as is, but imo it would be nice if it was tidied some while moving.

I'd be very much appreciated if this could happen.

"this" being what out of the two or more possible options? (I take it you mean "could go in as is", but that's guesswork.)

I'm not goint to squah rest of the series.

Signature · 2 lines
Best regards,
-grygorii
#6 lore raw

PATCH 2/3 x86/hvm: vmx: account for SHADOW_PAGING when use hvm_shadow_handle_cd()

In reply to Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The hvm_shadow_handle_cd() can be used only with SHADOW_PAGING=y, so guard hvm_shadow_handle_cd() call with IS_ENABLED(CONFIG_SHADOW_PAGING).

bloat-o-meter
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-290 (-290)

---
 xen/arch/x86/hvm/vmx/vmx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/vmx/vmx.c +1 -1
index a7ee0519957a..05b394840e59 100644
@@ -1506,7 +1506,7 @@ static int cf_check vmx_get_guest_pat(struct vcpu *v, u64 *gpat)
 
 static void cf_check vmx_handle_cd(struct vcpu *v, unsigned long value)
 {
-    if ( !paging_mode_hap(v->domain) )
+    if ( IS_ENABLED(CONFIG_SHADOW_PAGING) && !paging_mode_hap(v->domain) )
     {
         /*
          * For shadow, 'load IA32_PAT' VM-entry control is 0, so it cannot
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#7 lore raw

Re: [XEN][PATCH 2/3] x86/hvm: vmx: account for SHADOW_PAGING when use hvm_shadow_handle_cd()

Quoted 9 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The hvm_shadow_handle_cd() can be used only with SHADOW_PAGING=y, so guard hvm_shadow_handle_cd() call with IS_ENABLED(CONFIG_SHADOW_PAGING).

bloat-o-meter
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-290 (-290)

Requested-by: <me> (or some other of the available tags)?

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_get_guest_pat(struct vcpu *v, u64 *gpat) · line 1509

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -1506,7 +1506,7 @@ static int cf_check vmx_get_guest_pat(struct vcpu *v, u64 *gpat)
 
 static void cf_check vmx_handle_cd(struct vcpu *v, unsigned long value)
 {
-    if ( !paging_mode_hap(v->domain) )
+    if ( IS_ENABLED(CONFIG_SHADOW_PAGING) && !paging_mode_hap(v->domain) )

I don't think there's a need for IS_ENABLED() here - you can simply switch to using paging_mode_shadow(). That would be more correct anyway, for the abstract case of there being a 3rd paging mode. With the adjustment:

Jan

#8 lore raw

Re: [XEN][PATCH 2/3] x86/hvm: vmx: account for SHADOW_PAGING when use hvm_shadow_handle_cd()

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The hvm_shadow_handle_cd() can be used only with SHADOW_PAGING=y, so guard hvm_shadow_handle_cd() call with IS_ENABLED(CONFIG_SHADOW_PAGING).

Quoted 6 lines
Quoted 4 lines

bloat-o-meter
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-290 (-290)

Requested-by: <me> (or some other of the available tags)?

Will do. or Suggested-by:?

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -1506,7 +1506,7 @@ static int cf_check vmx_get_guest_pat(struct vcpu *v, u64 *gpat)
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_get_guest_pat(struct vcpu *v, u64 *gpat) · line 1509

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
  static void cf_check vmx_handle_cd(struct vcpu *v, unsigned long value)
  {
-    if ( !paging_mode_hap(v->domain) )
+    if ( IS_ENABLED(CONFIG_SHADOW_PAGING) && !paging_mode_hap(v->domain) )

I don't think there's a need for IS_ENABLED() here - you can simply switch to using paging_mode_shadow(). That would be more correct anyway, for the abstract case of there being a 3rd paging mode.

Will do. I've seen it, but was not sure :(

Quoted 2 lines

With the adjustment:

Signature · 2 lines
Best regards,
-grygorii
#9 lore raw

Re: [XEN][PATCH 2/3] x86/hvm: vmx: account for SHADOW_PAGING when use hvm_shadow_handle_cd()

Quoted 9 lines · Grygorii Strashko
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The hvm_shadow_handle_cd() can be used only with SHADOW_PAGING=y, so guard hvm_shadow_handle_cd() call with IS_ENABLED(CONFIG_SHADOW_PAGING).

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

bloat-o-meter
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-290 (-290)

Requested-by: <me> (or some other of the available tags)?

Will do. or Suggested-by:?

Your choice.

Jan

#10 lore raw

PATCH 3/3 x86/hvm: vmx: refactor cache disable mode data

In reply to Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The Cache Disable mode data is used only by VMX code, so move it from common HVM structures into VMX specific structures:
- move "uc_lock", "is_in_uc_mode" fields from struct hvm_domain to struct vmx_domain;
- move "cache_mode" field from struct hvm_vcpu to struct vmx_vcpu.

Hence, the "is_in_uc_mode" field is used directly in mm/shadow/multi.c _sh_propagate(), introduce the hvm_is_in_uc_mode() macro to avoid direct access to this field and account for INTEL_VMX configuration.

---
 xen/arch/x86/hvm/hvm.c                  |  1 -
 xen/arch/x86/hvm/vmx/vmx.c              | 29 +++++++++++++------------
 xen/arch/x86/include/asm/hvm/domain.h   |  6 -----
 xen/arch/x86/include/asm/hvm/hvm.h      |  3 +++
 xen/arch/x86/include/asm/hvm/vcpu.h     |  3 ---
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 14 ++++++++++++
 xen/arch/x86/include/asm/mtrr.h         |  3 ---
 xen/arch/x86/mm/shadow/multi.c          |  2 +-
 8 files changed, 33 insertions(+), 28 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -1
index 9caca93e5f56..c09fb2ba6873 100644
@@ -608,7 +608,6 @@ int hvm_domain_initialise(struct domain *d,
     }
 
     spin_lock_init(&d->arch.hvm.irq_lock);
-    spin_lock_init(&d->arch.hvm.uc_lock);
     spin_lock_init(&d->arch.hvm.write_map.lock);
     rwlock_init(&d->arch.hvm.mmcfg_lock);
     INIT_LIST_HEAD(&d->arch.hvm.write_map.list);
xen/arch/x86/hvm/vmx/vmx.c +15 -14
index 05b394840e59..23dbf60ec37f 100644
@@ -583,6 +583,7 @@ static int cf_check vmx_domain_initialise(struct domain *d)
     int rc;
 
     d->arch.ctxt_switch = &csw;
+    spin_lock_init(&d->arch.hvm.vmx.uc_lock);
 
     /*
      * Work around CVE-2018-12207?  The hardware domain is already permitted
@@ -1432,7 +1433,7 @@ static bool domain_exit_uc_mode(struct vcpu *v)
     {
         if ( (vs == v) || !vs->is_initialised )
             continue;
-        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+        if ( (vs->arch.hvm.vmx.cache_mode == NO_FILL_CACHE_MODE) ||
              mtrr_pat_not_equal(vs, v) )
             return false;
     }
@@ -1442,7 +1443,7 @@ static bool domain_exit_uc_mode(struct vcpu *v)
 
 static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
 {
-    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    v->domain->arch.hvm.vmx.is_in_uc_mode = is_in_uc_mode;
     shadow_blow_tables_per_domain(v->domain);
 }
 
@@ -1451,10 +1452,10 @@ static void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
     if ( value & X86_CR0_CD )
     {
         /* Entering no fill cache mode. */
-        spin_lock(&v->domain->arch.hvm.uc_lock);
-        v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
+        spin_lock(&v->domain->arch.hvm.vmx.uc_lock);
+        v->arch.hvm.vmx.cache_mode = NO_FILL_CACHE_MODE;
 
-        if ( !v->domain->arch.hvm.is_in_uc_mode )
+        if ( !v->domain->arch.hvm.vmx.is_in_uc_mode )
         {
             domain_pause_nosync(v->domain);
 
@@ -1464,26 +1465,26 @@ static void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
 
             domain_unpause(v->domain);
         }
-        spin_unlock(&v->domain->arch.hvm.uc_lock);
+        spin_unlock(&v->domain->arch.hvm.vmx.uc_lock);
     }
     else if ( !(value & X86_CR0_CD) &&
-              (v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+              (v->arch.hvm.vmx.cache_mode == NO_FILL_CACHE_MODE) )
     {
         /* Exit from no fill cache mode. */
-        spin_lock(&v->domain->arch.hvm.uc_lock);
-        v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
+        spin_lock(&v->domain->arch.hvm.vmx.uc_lock);
+        v->arch.hvm.vmx.cache_mode = NORMAL_CACHE_MODE;
 
         if ( domain_exit_uc_mode(v) )
             hvm_set_uc_mode(v, false);
 
-        spin_unlock(&v->domain->arch.hvm.uc_lock);
+        spin_unlock(&v->domain->arch.hvm.vmx.uc_lock);
     }
 }
 
 static int cf_check vmx_set_guest_pat(struct vcpu *v, u64 gpat)
 {
     if ( !paging_mode_hap(v->domain) ||
-         unlikely(v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+         unlikely(v->arch.hvm.vmx.cache_mode == NO_FILL_CACHE_MODE) )
         return 0;
 
     vmx_vmcs_enter(v);
@@ -1495,7 +1496,7 @@ static int cf_check vmx_set_guest_pat(struct vcpu *v, u64 gpat)
 static int cf_check vmx_get_guest_pat(struct vcpu *v, u64 *gpat)
 {
     if ( !paging_mode_hap(v->domain) ||
-         unlikely(v->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) )
+         unlikely(v->arch.hvm.vmx.cache_mode == NO_FILL_CACHE_MODE) )
         return 0;
 
     vmx_vmcs_enter(v);
@@ -1541,11 +1542,11 @@ static void cf_check vmx_handle_cd(struct vcpu *v, unsigned long value)
 
             wbinvd();               /* flush possibly polluted cache */
             hvm_asid_flush_vcpu(v); /* invalidate memory type cached in TLB */
-            v->arch.hvm.cache_mode = NO_FILL_CACHE_MODE;
+            v->arch.hvm.vmx.cache_mode = NO_FILL_CACHE_MODE;
         }
         else
         {
-            v->arch.hvm.cache_mode = NORMAL_CACHE_MODE;
+            v->arch.hvm.vmx.cache_mode = NORMAL_CACHE_MODE;
             vmx_set_guest_pat(v, *pat);
             if ( !is_iommu_enabled(v->domain) || iommu_snoop )
                 vmx_clear_msr_intercept(v, MSR_IA32_CR_PAT, VMX_MSR_RW);
xen/arch/x86/include/asm/hvm/domain.h +0 -6
index 95d9336a28f0..83be2bd1c29c 100644
@@ -97,12 +97,6 @@ struct hvm_domain {
     /* VRAM dirty support.  Protect with the domain paging lock. */
     struct sh_dirty_vram *dirty_vram;
 
-    /* If one of vcpus of this domain is in no_fill_mode or
-     * mtrr/pat between vcpus is not the same, set is_in_uc_mode
-     */
-    spinlock_t             uc_lock;
-    bool                   is_in_uc_mode;
-
     bool                   is_s3_suspended;
 
     /* Compatibility setting for a bug in x2APIC LDR */
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
index 6f174ef658f1..ce31401a5d53 100644
@@ -392,6 +392,9 @@ static inline bool using_svm(void)
     return IS_ENABLED(CONFIG_AMD_SVM) && cpu_has_svm;
 }
 
+#define hvm_is_in_uc_mode(d) \
+    (using_vmx() && (d)->arch.hvm.vmx.is_in_uc_mode)
+
 #ifdef CONFIG_HVM
 
 #define hvm_get_guest_tsc(v) hvm_get_guest_tsc_fixed(v, 0)
xen/arch/x86/include/asm/hvm/vcpu.h +0 -3
index 9ed9eaff3bc5..eae9ac53767b 100644
@@ -168,9 +168,6 @@ struct hvm_vcpu {
 
     u8                  evtchn_upcall_vector;
 
-    /* Which cache mode is this VCPU in (CR0:CD/NW)? */
-    u8                  cache_mode;
-
     struct hvm_vcpu_io  hvm_io;
 
     /* Pending hw/sw interrupt (.vector = -1 means nothing pending). */
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +14 -0
index ecd91389302c..44a535d16207 100644
@@ -46,7 +46,9 @@ struct ept_data {
 
 #define _VMX_DOMAIN_PML_ENABLED    0
 #define VMX_DOMAIN_PML_ENABLED     (1ul << _VMX_DOMAIN_PML_ENABLED)
+
 struct vmx_domain {
+    spinlock_t uc_lock;
     mfn_t apic_access_mfn;
     /* VMX_DOMAIN_* */
     unsigned int status;
@@ -56,6 +58,12 @@ struct vmx_domain {
      * around CVE-2018-12207 as appropriate.
      */
     bool exec_sp;
+    /*
+     * If one of vcpus of this domain is in no_fill_mode or
+     * mtrr/pat between vcpus is not the same, set is_in_uc_mode.
+     * Protected by uc_lock.
+     */
+    bool is_in_uc_mode;
 };
 
 /*
@@ -93,6 +101,9 @@ struct pi_blocking_vcpu {
     spinlock_t           *lock;
 };
 
+#define NORMAL_CACHE_MODE          0
+#define NO_FILL_CACHE_MODE         2
+
 struct vmx_vcpu {
     /* Physical address of VMCS. */
     paddr_t              vmcs_pa;
@@ -156,6 +167,9 @@ struct vmx_vcpu {
 
     uint8_t              lbr_flags;
 
+    /* Which cache mode is this VCPU in (CR0:CD/NW)? */
+    uint8_t              cache_mode;
+
     /* Bitmask of segments that we can't safely use in virtual 8086 mode */
     uint16_t             vm86_segment_mask;
     /* Shadow CS, SS, DS, ES, FS, GS, TR while in virtual 8086 mode */
xen/arch/x86/include/asm/mtrr.h +0 -3
index 25d442659df2..3a5b4f5b6eec 100644
@@ -7,9 +7,6 @@
 #define MEMORY_NUM_TYPES     MTRR_NUM_TYPES
 #define NO_HARDCODE_MEM_TYPE MTRR_NUM_TYPES
 
-#define NORMAL_CACHE_MODE          0
-#define NO_FILL_CACHE_MODE         2
-
 #define INVALID_MEM_TYPE X86_NUM_MT
 
 /* In the Intel processor's MTRR interface, the MTRR type is always held in
xen/arch/x86/mm/shadow/multi.c +1 -1
index 7be9c180ec43..03be61e225c0 100644
@@ -559,7 +559,7 @@ _sh_propagate(struct vcpu *v,
         if ( !mmio_mfn &&
              (type = hvm_get_mem_pinned_cacheattr(d, target_gfn, 0)) >= 0 )
             sflags |= pat_type_2_pte_flags(type);
-        else if ( d->arch.hvm.is_in_uc_mode )
+        else if ( hvm_is_in_uc_mode(d) )
             sflags |= pat_type_2_pte_flags(X86_MT_UC);
         else
             if ( iomem_access_permitted(d, mfn_x(target_mfn), mfn_x(target_mfn)) )
Signature · 1 line
2.34.1
#11 lore raw

Re: [XEN][PATCH 3/3] x86/hvm: vmx: refactor cache disable mode data

Quoted 8 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The Cache Disable mode data is used only by VMX code, so move it from common HVM structures into VMX specific structures:
- move "uc_lock", "is_in_uc_mode" fields from struct hvm_domain to struct vmx_domain;
- move "cache_mode" field from struct hvm_vcpu to struct vmx_vcpu.

Quoted 5 lines

Hence, the "is_in_uc_mode" field is used directly in mm/shadow/multi.c _sh_propagate(), introduce the hvm_is_in_uc_mode() macro to avoid direct access to this field and account for INTEL_VMX configuration.

Requested-by: Andrew ?

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_domain_initialise(struct domain *d) · line 586

Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -583,6 +583,7 @@ static int cf_check vmx_domain_initialise(struct domain *d)
     int rc;
 
     d->arch.ctxt_switch = &csw;
+    spin_lock_init(&d->arch.hvm.vmx.uc_lock);

I don't think this is the best place; in any event it wants to be separated from adjacent code by a blank line. I'd prefer if it was put ...

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_domain_initialise(struct domain *d) · line 589

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
     /*
      * Work around CVE-2018-12207?  The hardware domain is already permitted

... below this CVE workaround.

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct ept_data { · line 54

Quoted 12 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
@@ -46,7 +46,9 @@ struct ept_data {
 
 #define _VMX_DOMAIN_PML_ENABLED    0
 #define VMX_DOMAIN_PML_ENABLED     (1ul << _VMX_DOMAIN_PML_ENABLED)
+
 struct vmx_domain {
+    spinlock_t uc_lock;
     mfn_t apic_access_mfn;
     /* VMX_DOMAIN_* */
     unsigned int status;

Any reason to make this the very first field of the struct? It might better live adjacent to the other field you move; there's going to be some padding anyway, afaict.

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct vmx_domain { · line 66

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +6 -0
@@ -56,6 +58,12 @@ struct vmx_domain {
      * around CVE-2018-12207 as appropriate.
      */
     bool exec_sp;
+    /*
+     * If one of vcpus of this domain is in no_fill_mode or
+     * mtrr/pat between vcpus is not the same, set is_in_uc_mode.
+     * Protected by uc_lock.
+     */
+    bool is_in_uc_mode;

Imo while moving, the is_ prefix could also be dropped. It doesn't convey any extra information on top of the in_, and I think we prefer is_*() also as typically function(-like) predicates. (I.e. in hvm_is_in_uc_mode() I'm fine with the name.)

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct pi_blocking_vcpu { · line 105

Quoted 6 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
@@ -93,6 +101,9 @@ struct pi_blocking_vcpu {
     spinlock_t           *lock;
 };
 
+#define NORMAL_CACHE_MODE          0
+#define NO_FILL_CACHE_MODE         2

As you necessarily touch all use sites, could we switch to the more common CACHE_MODE_* at this occasion? Also imo these want to live ...

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct vmx_vcpu { · line 171

Quoted 6 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
@@ -156,6 +167,9 @@ struct vmx_vcpu {
 
     uint8_t              lbr_flags;
 
+    /* Which cache mode is this VCPU in (CR0:CD/NW)? */
+    uint8_t              cache_mode;

... right next to the field they belong to.

Jan

#12 lore raw

Re: [XEN][PATCH 3/3] x86/hvm: vmx: refactor cache disable mode data

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 8 lines · Grygorii Strashko

From: Grygorii Strashko <[email protected]>

The Cache Disable mode data is used only by VMX code, so move it from common HVM structures into VMX specific structures:
- move "uc_lock", "is_in_uc_mode" fields from struct hvm_domain to struct vmx_domain;
- move "cache_mode" field from struct hvm_vcpu to struct vmx_vcpu.

Quoted 7 lines
Quoted 5 lines

Hence, the "is_in_uc_mode" field is used directly in mm/shadow/multi.c _sh_propagate(), introduce the hvm_is_in_uc_mode() macro to avoid direct access to this field and account for INTEL_VMX configuration.

Requested-by: Andrew ?

ok

Quoted 12 lines

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_domain_initialise(struct domain *d) · line 586

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -583,6 +583,7 @@ static int cf_check vmx_domain_initialise(struct domain *d)
      int rc;
 
      d->arch.ctxt_switch = &csw;
+    spin_lock_init(&d->arch.hvm.vmx.uc_lock);

I don't think this is the best place; in any event it wants to be separated from adjacent code by a blank line. I'd prefer if it was put ...

Quoted 4 lines

xen/arch/x86/hvm/vmx/vmx.c static int cf_check vmx_domain_initialise(struct domain *d) · line 589

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -0
      /*
       * Work around CVE-2018-12207?  The hardware domain is already permitted

... below this CVE workaround.

ok

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +0 -0
@@ -46,7 +46,9 @@ struct ept_data {
 
Quoted 12 lines

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct ept_data { · line 54

Quoted 8 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
  #define _VMX_DOMAIN_PML_ENABLED    0
  #define VMX_DOMAIN_PML_ENABLED     (1ul << _VMX_DOMAIN_PML_ENABLED)
+
  struct vmx_domain {
+    spinlock_t uc_lock;
      mfn_t apic_access_mfn;
      /* VMX_DOMAIN_* */
      unsigned int status;

Any reason to make this the very first field of the struct? It might better live adjacent to the other field you move; there's going to be some padding anyway, afaict.

I've tried to put fields in holes and checked with pahole.

With current change it is:
struct vmx_domain {

	spinlock_t                 uc_lock;              /*     0     8 */
	mfn_t                      apic_access_mfn;      /*     8     8 */
	unsigned int               status;               /*    16     4 */
	_Bool                      exec_sp;              /*    20     1 */
	_Bool                      is_in_uc_mode;        /*    21     1 */
	/* size: 24, cachelines: 1, members: 5 */
	/* padding: 2 */
	/* last cacheline: 24 bytes */
};

It seems can be grouped like below?:
struct vmx_domain {

	mfn_t                      apic_access_mfn;      /*     0     8 */
	unsigned int               status;               /*     8     4 */
	_Bool                      exec_sp;              /*    12     1 */
	_Bool                      is_in_uc_mode;        /*    13     1 */
	/* XXX 2 bytes hole, try to pack */

	spinlock_t                 uc_lock;              /*    16     8 */

	/* size: 24, cachelines: 1, members: 5 */
	/* sum members: 22, holes: 1, sum holes: 2 */
	/* last cacheline: 24 bytes */
};
Quoted 16 lines

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct vmx_domain { · line 66

Quoted 11 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +6 -0
@@ -56,6 +58,12 @@ struct vmx_domain {
       * around CVE-2018-12207 as appropriate.
       */
      bool exec_sp;
+    /*
+     * If one of vcpus of this domain is in no_fill_mode or
+     * mtrr/pat between vcpus is not the same, set is_in_uc_mode.
+     * Protected by uc_lock.
+     */
+    bool is_in_uc_mode;

Imo while moving, the is_ prefix could also be dropped. It doesn't convey any extra information on top of the in_, and I think we prefer is_*() also as typically function(-like) predicates. (I.e. in hvm_is_in_uc_mode() I'm fine with the name.)

ok

Quoted 11 lines

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct pi_blocking_vcpu { · line 105

Quoted 7 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
@@ -93,6 +101,9 @@ struct pi_blocking_vcpu {
      spinlock_t           *lock;
  };
 
+#define NORMAL_CACHE_MODE          0
+#define NO_FILL_CACHE_MODE         2

As you necessarily touch all use sites, could we switch to the more common CACHE_MODE_* at this occasion? Also imo these want to live ...

Quoted 8 lines

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct vmx_vcpu { · line 171

Quoted 6 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
@@ -156,6 +167,9 @@ struct vmx_vcpu {
 
      uint8_t              lbr_flags;
 
+    /* Which cache mode is this VCPU in (CR0:CD/NW)? */
+    uint8_t              cache_mode;

... right next to the field they belong to.

ok.

Signature · 2 lines
Best regards,
-grygorii
#13 lore raw

Re: [XEN][PATCH 3/3] x86/hvm: vmx: refactor cache disable mode data

Quoted 41 lines · Grygorii Strashko
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Grygorii Strashko
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +0 -0
@@ -46,7 +46,9 @@ struct ept_data {
 
Quoted 12 lines

xen/arch/x86/include/asm/hvm/vmx/vmcs.h struct ept_data { · line 54

Quoted 8 lines
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +2 -0
  #define _VMX_DOMAIN_PML_ENABLED    0
  #define VMX_DOMAIN_PML_ENABLED     (1ul << _VMX_DOMAIN_PML_ENABLED)
+
  struct vmx_domain {
+    spinlock_t uc_lock;
      mfn_t apic_access_mfn;
      /* VMX_DOMAIN_* */
      unsigned int status;

Any reason to make this the very first field of the struct? It might better live adjacent to the other field you move; there's going to be some padding anyway, afaict.

I've tried to put fields in holes and checked with pahole.

With current change it is:
struct vmx_domain {

	spinlock_t                 uc_lock;              /*     0     8 */
	mfn_t                      apic_access_mfn;      /*     8     8 */
	unsigned int               status;               /*    16     4 */
	_Bool                      exec_sp;              /*    20     1 */
	_Bool                      is_in_uc_mode;        /*    21     1 */
	/* size: 24, cachelines: 1, members: 5 */
	/* padding: 2 */
	/* last cacheline: 24 bytes */
};

It seems can be grouped like below?:
struct vmx_domain {

	mfn_t                      apic_access_mfn;      /*     0     8 */
	unsigned int               status;               /*     8     4 */
	_Bool                      exec_sp;              /*    12     1 */
	_Bool                      is_in_uc_mode;        /*    13     1 */
Quoted 8 lines
	/* XXX 2 bytes hole, try to pack */

	spinlock_t                 uc_lock;              /*    16     8 */

	/* size: 24, cachelines: 1, members: 5 */
	/* sum members: 22, holes: 1, sum holes: 2 */
	/* last cacheline: 24 bytes */
};

Yes, this is what I was thinking of.

Jan