Skip to contentSkip to conversation

x86/vmx: Update __vmread() signature

PATCH Superseded [email protected] 2 participants 4 messages Apr 22, 2025 – Apr 23, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/vmx: Update __vmread() signature

From: Denis Mukhin <[email protected]>

Current implementation of __vmread() returns the result via pointer argument which leads to having excess code in some places.

Update the signature of __vmread() to return unsigned long and drop the pointer argument as per suggestion in [1].

Update all call sites everywhere in the VT-x code.

[1] https://lore.kernel.org/xen-devel/[email protected]/

---
bloat-o-meter reports:
add/remove: 0/0 grow/shrink: 2/3 up/down: 8/-28 (-20)
Function old new delta

vmx_vmexit_handler                          7000    7005      +5
nvmx_hap_walk_L1_p2m                         258     261      +3
vmx_do_resume                                485     484      -1
shadow_to_vvmcs_bulk                         304     294     -10
vvmcs_to_shadow_bulk                         296     279     -17

Total: Before=3540972, After=3540952, chg -0.00%

Changes since v1:
- Limited the scope of the change to __vmread() signature change only
- Link to v1: https://lore.kernel.org/xen-devel/[email protected]/
- Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1778785297

---
 xen/arch/x86/cpu/vpmu_intel.c          |   3 +-
 xen/arch/x86/hvm/vmx/intr.c            |  26 +---
 xen/arch/x86/hvm/vmx/realmode.c        |   2 +-
 xen/arch/x86/hvm/vmx/vmcs.c            |  11 +-
 xen/arch/x86/hvm/vmx/vmx.c             | 208 +++++++++++--------------
 xen/arch/x86/hvm/vmx/vvmx.c            |  43 +++--
 xen/arch/x86/include/asm/hvm/vmx/vmx.h |  19 +--
 7 files changed, 132 insertions(+), 180 deletions(-)
xen/arch/x86/cpu/vpmu_intel.c +1 -2
index 7ce98ee42e..3c08448ba4 100644
@@ -796,8 +796,7 @@ static int cf_check core2_vpmu_do_interrupt(void)
     else
     {
         /* No PMC overflow but perhaps a Trace Message interrupt. */
-        __vmread(GUEST_IA32_DEBUGCTL, &msr_content);
-        if ( !(msr_content & IA32_DEBUGCTLMSR_TR) )
+        if ( !(__vmread(GUEST_IA32_DEBUGCTL) & IA32_DEBUGCTLMSR_TR) )
             return 0;
     }
 
xen/arch/x86/hvm/vmx/intr.c +7 -19
index 91b407e6bc..9e9217e8b6 100644
@@ -63,9 +63,8 @@ static void vmx_enable_intr_window(struct vcpu *v, struct hvm_intack intack)
 
     if ( unlikely(tb_init_done) )
     {
-        unsigned long intr;
+        unsigned long intr = __vmread(VM_ENTRY_INTR_INFO);
 
-        __vmread(VM_ENTRY_INTR_INFO, &intr);
         TRACE(TRC_HVM_INTR_WINDOW, intack.vector, intack.source,
               (intr & INTR_INFO_VALID_MASK) ? intr & 0xff : -1);
     }
@@ -81,9 +80,8 @@ static void vmx_enable_intr_window(struct vcpu *v, struct hvm_intack intack)
          * we may immediately vmexit and hance make no progress!
          * (see SDM 3B 21.3, "Other Causes of VM Exits").
          */
-        unsigned long intr_shadow;
+        unsigned long intr_shadow = __vmread(GUEST_INTERRUPTIBILITY_INFO);
 
-        __vmread(GUEST_INTERRUPTIBILITY_INFO, &intr_shadow);
         if ( intr_shadow & VMX_INTR_SHADOW_STI )
         {
             /* Having both STI-blocking and MOV-SS-blocking fails vmentry. */
@@ -144,14 +142,8 @@ enum hvm_intblk cf_check nvmx_intr_blocked(struct vcpu *v)
         if ( nvcpu->nv_vmexit_pending ||
              nvcpu->nv_vmswitch_in_progress )
             r = hvm_intblk_rflags_ie;
-        else
-        {
-            unsigned long intr_info;
-
-            __vmread(VM_ENTRY_INTR_INFO, &intr_info);
-            if ( intr_info & INTR_INFO_VALID_MASK )
-                r = hvm_intblk_rflags_ie;
-        }
+        else if ( __vmread(VM_ENTRY_INTR_INFO) & INTR_INFO_VALID_MASK )
+            r = hvm_intblk_rflags_ie;
     }
     else if ( nvcpu->nv_vmentry_pending )
         r = hvm_intblk_rflags_ie;
@@ -253,8 +245,6 @@ void asmlinkage vmx_intr_assist(void)
     pt_vector = pt_update_irq(v);
 
     do {
-        unsigned long intr_info;
-
         intack = hvm_vcpu_has_pending_irq(v);
         if ( likely(intack.source == hvm_intsrc_none) )
             goto out;
@@ -275,8 +265,7 @@ void asmlinkage vmx_intr_assist(void)
                 goto out;
             }
 
-            __vmread(VM_ENTRY_INTR_INFO, &intr_info);
-            if ( intr_info & INTR_INFO_VALID_MASK )
+            if ( __vmread(VM_ENTRY_INTR_INFO) & INTR_INFO_VALID_MASK )
             {
                 if ( (intack.source == hvm_intsrc_pic) ||
                      (intack.source == hvm_intsrc_nmi) ||
@@ -299,8 +288,7 @@ void asmlinkage vmx_intr_assist(void)
         }
         else
         {
-            __vmread(VM_ENTRY_INTR_INFO, &intr_info);
-            if ( intr_info & INTR_INFO_VALID_MASK )
+            if ( __vmread(VM_ENTRY_INTR_INFO) & INTR_INFO_VALID_MASK )
             {
                 vmx_enable_intr_window(v, intack);
                 goto out;
@@ -377,7 +365,7 @@ void asmlinkage vmx_intr_assist(void)
         }
 
         /* we need update the RVI field */
-        __vmread(GUEST_INTR_STATUS, &status);
+        status = __vmread(GUEST_INTR_STATUS);
         status &= ~VMX_GUEST_INTR_STATUS_SUBFIELD_BITMASK;
         status |= VMX_GUEST_INTR_STATUS_SUBFIELD_BITMASK &
                     intack.vector;
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index ff44ddcfa6..ee7a3570ec 100644
@@ -159,7 +159,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     unsigned int emulations = 0;
 
     /* Get-and-clear VM_ENTRY_INTR_INFO. */
-    __vmread(VM_ENTRY_INTR_INFO, &intr_info);
+    intr_info = __vmread(VM_ENTRY_INTR_INFO);
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, 0);
 
xen/arch/x86/hvm/vmx/vmcs.c +4 -7
index a44475ae15..c2e4410f89 100644
@@ -1031,7 +1031,7 @@ u64 virtual_vmcs_vmread(const struct vcpu *v, u32 vmcs_encoding)
     u64 res;
 
     virtual_vmcs_enter(v);
-    __vmread(vmcs_encoding, &res);
+    res = __vmread(vmcs_encoding);
     virtual_vmcs_exit(v);
 
     return res;
@@ -1691,7 +1691,7 @@ void vmx_vcpu_flush_pml_buffer(struct vcpu *v)
 
     vmx_vmcs_enter(v);
 
-    __vmread(GUEST_PML_INDEX, &pml_idx);
+    pml_idx = __vmread(GUEST_PML_INDEX);
 
     /* Do nothing if PML buffer is empty. */
     if ( pml_idx == (NR_PML_ENTRIES - 1) )
@@ -1874,9 +1874,8 @@ void vmx_destroy_vmcs(struct vcpu *v)
 void vmx_vmentry_failure(void)
 {
     struct vcpu *curr = current;
-    unsigned long error;
+    unsigned long error = __vmread(VM_INSTRUCTION_ERROR);
 
-    __vmread(VM_INSTRUCTION_ERROR, &error);
     gprintk(XENLOG_ERR, "VM%s error: %#lx\n",
             curr->arch.hvm.vmx.launched ? "RESUME" : "LAUNCH", error);
 
@@ -1905,7 +1904,6 @@ void cf_check vmx_do_resume(void)
 {
     struct vcpu *v = current;
     bool debug_state;
-    unsigned long host_cr4;
 
     if ( v->arch.hvm.vmx.active_cpu == smp_processor_id() )
         vmx_vmcs_reload(v);
@@ -1957,8 +1955,7 @@ void cf_check vmx_do_resume(void)
     hvm_do_resume(v);
 
     /* Sync host CR4 in case its value has changed. */
-    __vmread(HOST_CR4, &host_cr4);
-    if ( host_cr4 != read_cr4() )
+    if ( __vmread(HOST_CR4) != read_cr4() )
         __vmwrite(HOST_CR4, read_cr4());
 
     reset_stack_and_jump(vmx_asm_do_vmentry);
xen/arch/x86/hvm/vmx/vmx.c +90 -118

590 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +19 -24

169 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/vmx/vmx.h +10 -9
index 33d3d43a38..fbbf9acce8 100644
@@ -320,16 +320,20 @@ static always_inline void __vmpclear(u64 addr)
     BUG();
 }
 
-static always_inline void __vmread(unsigned long field, unsigned long *value)
+static always_inline unsigned long __vmread(unsigned long field)
 {
-    asm volatile ( "vmread %1, %0\n\t"
+    unsigned long value;
+
+    asm volatile ( "vmread %[field], %[value]\n\t"
                    /* CF==1 or ZF==1 --> BUG() */
                    UNLIKELY_START(be, vmread)
                    _ASM_BUGFRAME_TEXT(0)
                    UNLIKELY_END_SECTION
-                   : "=rm" (*value)
-                   : "r" (field),
+                   : [value] "=rm" (value)
+                   : [field] "r" (field),
                      _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) );
+
+    return value;
 }
 
 static always_inline void __vmwrite(unsigned long field, unsigned long value)
@@ -363,7 +367,7 @@ static inline enum vmx_insn_errno vmread_safe(unsigned long field,
     if ( unlikely(fail_invalid) )
         ret = VMX_INSN_FAIL_INVALID;
     else if ( unlikely(fail_valid) )
-        __vmread(VM_INSTRUCTION_ERROR, &ret);
+        ret = __vmread(VM_INSTRUCTION_ERROR);
 
     return ret;
 }
@@ -371,8 +375,6 @@ static inline enum vmx_insn_errno vmread_safe(unsigned long field,
 static inline enum vmx_insn_errno vmwrite_safe(unsigned long field,
                                                unsigned long value)
 {
-    unsigned long ret;
-
     asm goto ( "vmwrite %[value], %[field]\n\t"
                "jc %l[vmfail_invalid]\n\t"
                "jz %l[vmfail_error]"
@@ -386,8 +388,7 @@ static inline enum vmx_insn_errno vmwrite_safe(unsigned long field,
     return VMX_INSN_FAIL_INVALID;
 
  vmfail_error:
-    __vmread(VM_INSTRUCTION_ERROR, &ret);
-    return ret;
+    return __vmread(VM_INSTRUCTION_ERROR);
 }
 
 static always_inline void __invept(unsigned long type, uint64_t eptp)
Signature · 1 line
2.34.1
#1 lore raw
Quoted 9 lines · On 22.04.2025 03:15, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Current implementation of __vmread() returns the result via pointer argument which leads to having excess code in some places.

Update the signature of __vmread() to return unsigned long and drop the pointer argument as per suggestion in [1].

Update all call sites everywhere in the VT-x code.

You did read Andrew's reply to v1 before posting v2, didn't you?

Jan

#2 lore raw
Quoted 6 lines · Jan Beulich
Quoted 5 lines · On 22.04.2025 03:15, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Current implementation of __vmread() returns the result via pointer argument which leads to having excess code in some places.

Quoted 6 lines
Quoted 4 lines

Update the signature of __vmread() to return unsigned long and drop the pointer argument as per suggestion in [1].

Update all call sites everywhere in the VT-x code.

You did read Andrew's reply to v1 before posting v2, didn't you?

I missed to address the function name change request, posted v3:
https://lore.kernel.org/xen-devel/[email protected]/

Quoted 2 lines

Jan

#3 lore raw
Quoted 10 lines · On 23.04.2025 00:11, [email protected] wrote:
Quoted 9 lines · Jan Beulich
Quoted 8 lines · On 22.04.2025 03:15, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Current implementation of __vmread() returns the result via pointer argument which leads to having excess code in some places.

Update the signature of __vmread() to return unsigned long and drop the pointer argument as per suggestion in [1].

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Update all call sites everywhere in the VT-x code.

You did read Andrew's reply to v1 before posting v2, didn't you?

I missed to address the function name change request, posted v3:
https://lore.kernel.org/xen-devel/[email protected]/

That was only part of what was asked for. Andrew also gave a pretty clear outline of how to avoid the need to touch all use sites in one single patch. Plus he explained why it's better to do this in smaller steps.

Jan