[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen master] x86/vm_event: introduce vm_event_is_enabled()



commit 0ea539000c1d2fa93318c715e8f24c829f79f055
Author:     Penny Zheng <Penny.Zheng@xxxxxxx>
AuthorDate: Thu Jan 15 17:28:37 2026 +0800
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Mon Jan 19 09:15:35 2026 +0100

    x86/vm_event: introduce vm_event_is_enabled()
    
    Function vm_event_is_enabled() is introduced to check if vm event is 
enabled,
    and also make the checking conditional upon CONFIG_VM_EVENT, which could 
help
    DCE a lot unreachable calls/codes, such as hvm_monitor_io(), etc,
    when having VM_EVENT=n.
    
    Signed-off-by: Penny Zheng <Penny.Zheng@xxxxxxx>
    Acked-by: Jan Beulich <jbeulich@xxxxxxxx>
    Reviewed-by: Jason Andryuk <jason.andryuk@xxxxxxx>
    Acked-by: Tamas K Lengyel <tamas@xxxxxxxxxxxxx>
---
 xen/arch/x86/hvm/emulate.c          |  9 +++---
 xen/arch/x86/hvm/hvm.c              | 23 ++++++++++----
 xen/arch/x86/hvm/svm/intr.c         |  2 +-
 xen/arch/x86/hvm/svm/svm.c          | 46 +++++++++++++++------------
 xen/arch/x86/hvm/vmx/intr.c         |  2 +-
 xen/arch/x86/hvm/vmx/vmx.c          | 63 ++++++++++++++++++++++---------------
 xen/arch/x86/include/asm/vm_event.h |  5 +++
 xen/arch/x86/mm/mem_sharing.c       |  3 ++
 8 files changed, 96 insertions(+), 57 deletions(-)

diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c
index 2af4f30359..fe75b0516d 100644
--- a/xen/arch/x86/hvm/emulate.c
+++ b/xen/arch/x86/hvm/emulate.c
@@ -105,7 +105,7 @@ static int set_context_data(void *buffer, unsigned int size)
 {
     struct vcpu *curr = current;
 
-    if ( curr->arch.vm_event )
+    if ( vm_event_is_enabled(curr) )
     {
         unsigned int safe_size =
             min(size, curr->arch.vm_event->emul.read.size);
@@ -771,7 +771,7 @@ static void *hvmemul_map_linear_addr(
             ASSERT(p2mt == p2m_ram_logdirty || !p2m_is_readonly(p2mt));
         }
 
-        if ( unlikely(curr->arch.vm_event) &&
+        if ( unlikely(vm_event_is_enabled(curr)) &&
              curr->arch.vm_event->send_event &&
              hvm_monitor_check_p2m(addr, gfn, pfec, npfec_kind_with_gla) )
         {
@@ -1863,16 +1863,17 @@ static int hvmemul_rep_outs_set_context(
     unsigned int bytes_per_rep,
     unsigned long *reps)
 {
-    const struct arch_vm_event *ev = current->arch.vm_event;
+    const struct arch_vm_event *ev;
     const uint8_t *ptr;
     unsigned int avail;
     unsigned long done;
     int rc = X86EMUL_OKAY;
 
     ASSERT(bytes_per_rep <= 4);
-    if ( !ev )
+    if ( !vm_event_is_enabled(current) )
         return X86EMUL_UNHANDLEABLE;
 
+    ev = current->arch.vm_event;
     ptr = ev->emul.read.data;
     avail = ev->emul.read.size;
 
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index 0eafa515bb..07e54890d9 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -532,7 +532,7 @@ void hvm_do_resume(struct vcpu *v)
     if ( !vcpu_ioreq_handle_completion(v) )
         return;
 
-    if ( unlikely(v->arch.vm_event) )
+    if ( unlikely(vm_event_is_enabled(v)) )
         hvm_vm_event_do_resume(v);
 
     /* Inject pending hw/sw event */
@@ -546,7 +546,8 @@ void hvm_do_resume(struct vcpu *v)
         v->arch.hvm.inject_event.vector = HVM_EVENT_VECTOR_UNSET;
     }
 
-    if ( unlikely(v->arch.vm_event) && v->arch.monitor.next_interrupt_enabled )
+    if ( unlikely(vm_event_is_enabled(v)) &&
+         v->arch.monitor.next_interrupt_enabled )
     {
         struct x86_event info;
 
@@ -2095,7 +2096,7 @@ int hvm_handle_xsetbv(u32 index, u64 new_bv)
 {
     int rc;
 
-    if ( index == 0 )
+    if ( index == 0 && vm_event_is_enabled(current) )
         hvm_monitor_crX(XCR0, new_bv, current->arch.xcr0);
 
     rc = x86emul_write_xcr(index, new_bv, NULL);
@@ -2266,6 +2267,8 @@ int hvm_set_cr0(unsigned long value, bool may_defer)
 
     HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR0 value = %lx", value);
 
+    may_defer &= vm_event_is_enabled(v);
+
     if ( (u32)value != value )
     {
         HVM_DBG_LOG(DBG_LEVEL_1,
@@ -2401,6 +2404,8 @@ int hvm_set_cr3(unsigned long value, bool noflush, bool 
may_defer)
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
 
+    may_defer &= vm_event_is_enabled(curr);
+
     if ( value >> currd->arch.cpuid->extd.maxphysaddr )
     {
         HVM_DBG_LOG(DBG_LEVEL_1,
@@ -2456,6 +2461,8 @@ int hvm_set_cr4(unsigned long value, bool may_defer)
     struct vcpu *v = current;
     unsigned long old_cr, valid = hvm_cr4_guest_valid_bits(v->domain);
 
+    may_defer &= vm_event_is_enabled(v);
+
     if ( value & ~valid )
     {
         HVM_DBG_LOG(DBG_LEVEL_1, "Bad CR4 %#lx (valid %#lx, rejected %#lx)",
@@ -3356,7 +3363,7 @@ static enum hvm_translation_result __hvm_copy(
             return HVMTRANS_bad_gfn_to_mfn;
         }
 
-        if ( unlikely(v->arch.vm_event) &&
+        if ( unlikely(vm_event_is_enabled(v)) &&
              (flags & HVMCOPY_linear) &&
              v->arch.vm_event->send_event &&
              hvm_monitor_check_p2m(addr, gfn, pfec, npfec_kind_with_gla) )
@@ -3503,7 +3510,9 @@ int hvm_vmexit_cpuid(struct cpu_user_regs *regs, unsigned 
int inst_len)
     regs->rcx = res.c;
     regs->rdx = res.d;
 
-    return hvm_monitor_cpuid(inst_len, leaf, subleaf);
+    return vm_event_is_enabled(curr)
+           ? hvm_monitor_cpuid(inst_len, leaf, subleaf)
+           : 0;
 }
 
 void hvm_rdtsc_intercept(struct cpu_user_regs *regs)
@@ -3635,6 +3644,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t 
msr_content,
 
     TRACE(TRC_HVM_MSR_WRITE, msr, msr_content, msr_content >> 32);
 
+    may_defer &= vm_event_is_enabled(v);
     if ( may_defer && unlikely(monitored_msr(v->domain, msr)) )
     {
         uint64_t msr_old_content;
@@ -3803,7 +3813,8 @@ int hvm_descriptor_access_intercept(uint64_t exit_info,
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
 
-    if ( currd->arch.monitor.descriptor_access_enabled )
+    if ( vm_event_is_enabled(curr) &&
+         currd->arch.monitor.descriptor_access_enabled )
     {
         ASSERT(curr->arch.vm_event);
         hvm_monitor_descriptor_access(exit_info, vmx_exit_qualification,
diff --git a/xen/arch/x86/hvm/svm/intr.c b/xen/arch/x86/hvm/svm/intr.c
index 702c071e89..6453a46b85 100644
--- a/xen/arch/x86/hvm/svm/intr.c
+++ b/xen/arch/x86/hvm/svm/intr.c
@@ -130,7 +130,7 @@ void asmlinkage svm_intr_assist(void)
     enum hvm_intblk intblk;
 
     /* Block event injection while handling a sync vm_event. */
-    if ( unlikely(v->arch.vm_event) && v->arch.vm_event->sync_event )
+    if ( unlikely(vm_event_is_enabled(v)) && v->arch.vm_event->sync_event )
         return;
 
     /* Crank the handle on interrupt state. */
diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
index 564337bb4e..21f355a657 100644
--- a/xen/arch/x86/hvm/svm/svm.c
+++ b/xen/arch/x86/hvm/svm/svm.c
@@ -35,6 +35,7 @@
 #include <asm/p2m.h>
 #include <asm/paging.h>
 #include <asm/processor.h>
+#include <asm/vm_event.h>
 #include <asm/x86_emulate.h>
 
 #include <public/sched.h>
@@ -2702,7 +2703,7 @@ void asmlinkage svm_vmexit_handler(void)
         if ( !v->domain->debugger_attached )
         {
             unsigned int trap_type;
-            int rc;
+            int rc = 0;
 
             if ( likely(exit_reason != VMEXIT_ICEBP) )
             {
@@ -2718,11 +2719,14 @@ void asmlinkage svm_vmexit_handler(void)
                     break;
             }
 
-            rc = hvm_monitor_debug(regs->rip,
-                                   HVM_MONITOR_DEBUG_EXCEPTION,
-                                   trap_type, insn_len, 0);
-            if ( rc < 0 )
-                goto unexpected_exit_type;
+            if ( vm_event_is_enabled(v) )
+            {
+                rc = hvm_monitor_debug(regs->rip,
+                                       HVM_MONITOR_DEBUG_EXCEPTION,
+                                       trap_type, insn_len, 0);
+                if ( rc < 0 )
+                    goto unexpected_exit_type;
+            }
             if ( !rc )
                 hvm_inject_exception(X86_EXC_DB,
                                      trap_type, insn_len, X86_EVENT_NO_EC);
@@ -2746,9 +2750,10 @@ void asmlinkage svm_vmexit_handler(void)
         }
         else
         {
-            int rc = hvm_monitor_debug(regs->rip,
-                                       HVM_MONITOR_SOFTWARE_BREAKPOINT,
-                                       X86_ET_SW_EXC, insn_len, 0);
+            int rc = vm_event_is_enabled(v) ?
+                        hvm_monitor_debug(regs->rip,
+                                          HVM_MONITOR_SOFTWARE_BREAKPOINT,
+                                          X86_ET_SW_EXC, insn_len, 0) : 0;
             if ( rc < 0 )
                 goto unexpected_exit_type;
             if ( !rc )
@@ -2899,17 +2904,19 @@ void asmlinkage svm_vmexit_handler(void)
         break;
 
     case VMEXIT_IOIO:
-    {
-        int rc;
+        if ( vm_event_is_enabled(v) )
+        {
+            int rc;
 
-        rc = hvm_monitor_io(vmcb->ei.io.port,
-                            vmcb->ei.io.bytes,
-                            vmcb->ei.io.in,
-                            vmcb->ei.io.str);
-        if ( rc < 0 )
-            goto unexpected_exit_type;
-        if ( rc )
-            break;
+            rc = hvm_monitor_io(vmcb->ei.io.port,
+                                vmcb->ei.io.bytes,
+                                vmcb->ei.io.in,
+                                vmcb->ei.io.str);
+            if ( rc < 0 )
+                goto unexpected_exit_type;
+            if ( rc )
+                break;
+        }
 
         if ( !vmcb->ei.io.str )
         {
@@ -2921,7 +2928,6 @@ void asmlinkage svm_vmexit_handler(void)
         else if ( !hvm_emulate_one_insn(x86_insn_is_portio, "port I/O") )
             hvm_inject_hw_exception(X86_EXC_GP, 0);
         break;
-    }
 
     case VMEXIT_CR0_READ ... VMEXIT_CR15_READ:
     case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE:
diff --git a/xen/arch/x86/hvm/vmx/intr.c b/xen/arch/x86/hvm/vmx/intr.c
index b35dc8c586..a8ced95871 100644
--- a/xen/arch/x86/hvm/vmx/intr.c
+++ b/xen/arch/x86/hvm/vmx/intr.c
@@ -239,7 +239,7 @@ void asmlinkage vmx_intr_assist(void)
     }
 
     /* Block event injection while handling a sync vm_event. */
-    if ( unlikely(v->arch.vm_event) && v->arch.vm_event->sync_event )
+    if ( unlikely(vm_event_is_enabled(v)) && v->arch.vm_event->sync_event )
         return;
 
 #ifdef CONFIG_MEM_SHARING
diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
index 524bce8307..89f9d9c7f6 100644
--- a/xen/arch/x86/hvm/vmx/vmx.c
+++ b/xen/arch/x86/hvm/vmx/vmx.c
@@ -47,6 +47,7 @@
 #include <asm/shadow.h>
 #include <asm/spec_ctrl.h>
 #include <asm/stubs.h>
+#include <asm/vm_event.h>
 #include <asm/x86_emulate.h>
 
 #include <public/arch-x86/cpuid.h>
@@ -3335,7 +3336,8 @@ static int vmx_cr_access(cr_access_qual_t qual)
          * VM_EVENT_FLAG_DENY-capable application, so the hvm_monitor_crX()
          * return value is ignored for now.
          */
-        hvm_monitor_crX(CR0, value, old);
+        if ( vm_event_is_enabled(curr) )
+            hvm_monitor_crX(CR0, value, old);
         curr->arch.hvm.guest_cr[0] = value;
         vmx_update_guest_cr(curr, 0, 0);
         TRACE(TRC_HVM_CLTS);
@@ -4322,7 +4324,8 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs 
*regs)
         p2m_set_altp2m(v, idx);
     }
 
-    if ( unlikely(currd->arch.monitor.vmexit_enabled) )
+    if ( vm_event_is_enabled(v) &&
+         unlikely(currd->arch.monitor.vmexit_enabled) )
     {
         int rc;
 
@@ -4470,19 +4473,21 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs 
*regs)
             if ( !v->domain->debugger_attached )
             {
                 unsigned long insn_len = 0;
-                int rc;
+                int rc = 0;
                 unsigned long trap_type = MASK_EXTR(intr_info,
                                                     INTR_INFO_INTR_TYPE_MASK);
 
                 if ( trap_type >= X86_ET_SW_INT )
                     __vmread(VM_EXIT_INSTRUCTION_LEN, &insn_len);
 
-                rc = hvm_monitor_debug(regs->rip,
-                                       HVM_MONITOR_DEBUG_EXCEPTION,
-                                       trap_type, insn_len, 0);
-
-                if ( rc < 0 )
-                    goto exit_and_crash;
+                if ( vm_event_is_enabled(v) )
+                {
+                    rc = hvm_monitor_debug(regs->rip,
+                                           HVM_MONITOR_DEBUG_EXCEPTION,
+                                           trap_type, insn_len, 0);
+                    if ( rc < 0 )
+                        goto exit_and_crash;
+                }
                 if ( !rc )
                     vmx_propagate_intr(intr_info);
             }
@@ -4494,16 +4499,19 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs 
*regs)
             if ( !v->domain->debugger_attached )
             {
                 unsigned long insn_len;
-                int rc;
+                int rc = 0;
 
                 __vmread(VM_EXIT_INSTRUCTION_LEN, &insn_len);
-                rc = hvm_monitor_debug(regs->rip,
-                                       HVM_MONITOR_SOFTWARE_BREAKPOINT,
-                                       X86_ET_SW_EXC,
-                                       insn_len, 0);
+                if ( vm_event_is_enabled(v) )
+                {
+                    rc = hvm_monitor_debug(regs->rip,
+                                           HVM_MONITOR_SOFTWARE_BREAKPOINT,
+                                           X86_ET_SW_EXC,
+                                           insn_len, 0);
 
-                if ( rc < 0 )
-                    goto exit_and_crash;
+                    if ( rc < 0 )
+                        goto exit_and_crash;
+                }
                 if ( !rc )
                     vmx_propagate_intr(intr_info);
             }
@@ -4758,16 +4766,20 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs 
*regs)
             };
         } io_qual;
         unsigned int bytes;
-        int rc;
 
         __vmread(EXIT_QUALIFICATION, &io_qual.raw);
         bytes = io_qual.size + 1;
 
-        rc = hvm_monitor_io(io_qual.port, bytes, io_qual.in, io_qual.str);
-        if ( rc < 0 )
-            goto exit_and_crash;
-        if ( rc )
-            break;
+        if ( vm_event_is_enabled(v) )
+        {
+            int rc;
+
+            rc = hvm_monitor_io(io_qual.port, bytes, io_qual.in, io_qual.str);
+            if ( rc < 0 )
+                goto exit_and_crash;
+            if ( rc )
+                break;
+        }
 
         if ( io_qual.str )
         {
@@ -4821,9 +4833,10 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs 
*regs)
         vmx_update_cpu_exec_control(v);
         if ( v->arch.hvm.single_step )
         {
-            hvm_monitor_debug(regs->rip,
-                              HVM_MONITOR_SINGLESTEP_BREAKPOINT,
-                              0, 0, 0);
+            if ( vm_event_is_enabled(v) )
+                hvm_monitor_debug(regs->rip,
+                                  HVM_MONITOR_SINGLESTEP_BREAKPOINT,
+                                  0, 0, 0);
 
             if ( v->domain->debugger_attached )
                 domain_pause_for_debugger();
diff --git a/xen/arch/x86/include/asm/vm_event.h 
b/xen/arch/x86/include/asm/vm_event.h
index 46e77ed6d9..0310f30c18 100644
--- a/xen/arch/x86/include/asm/vm_event.h
+++ b/xen/arch/x86/include/asm/vm_event.h
@@ -45,4 +45,9 @@ void vm_event_sync_event(struct vcpu *v, bool value);
 
 void vm_event_reset_vmtrace(struct vcpu *v);
 
+static inline bool vm_event_is_enabled(const struct vcpu *v)
+{
+    return IS_ENABLED(CONFIG_VM_EVENT) && v->arch.vm_event != NULL;
+}
+
 #endif /* __ASM_X86_VM_EVENT_H__ */
diff --git a/xen/arch/x86/mm/mem_sharing.c b/xen/arch/x86/mm/mem_sharing.c
index 4787b27964..b5a259af1d 100644
--- a/xen/arch/x86/mm/mem_sharing.c
+++ b/xen/arch/x86/mm/mem_sharing.c
@@ -648,6 +648,9 @@ int mem_sharing_notify_enomem(struct domain *d, unsigned 
long gfn,
         .u.mem_sharing.p2mt = p2m_ram_shared,
     };
 
+    if ( !vm_event_is_enabled(current) )
+        return -EOPNOTSUPP;
+
     if ( (rc = __vm_event_claim_slot(
               d, d->vm_event_share, allow_sleep)) < 0 )
         return rc;
--
generated by git-patchbot for /home/xen/git/xen.git#master



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.