[PATCH] x86/svm: Don't clear clean bits on every context switch

Ross Lagerwall posted 1 patch 2 weeks, 3 days ago
xen/arch/x86/hvm/svm/svm.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
[PATCH] x86/svm: Don't clear clean bits on every context switch
Posted by Ross Lagerwall 2 weeks, 3 days ago
Clear the cleanbits only when the VMCB is used on a different pCPU
rather than on every context switch to the vCPU. This follows the APM
guideline for when to clear the cleanbits:

"The hypervisor executes the guest on a different CPU core than one used
the last time that guest was executed."

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
---
 xen/arch/x86/hvm/svm/svm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
index 71e48351f22c..4e22322f4b42 100644
--- a/xen/arch/x86/hvm/svm/svm.c
+++ b/xen/arch/x86/hvm/svm/svm.c
@@ -890,7 +890,6 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *v)
 
 static void cf_check svm_ctxt_switch_to(struct vcpu *v)
 {
-    struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
     int cpu = smp_processor_id();
 
     /*
@@ -908,7 +907,6 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v)
 
     svm_restore_dr(v);
 
-    vmcb->cleanbits.raw = 0;
     svm_tsc_ratio_load(v);
 
     if ( cpu_has_msr_tsc_aux )
@@ -953,6 +951,7 @@ static void noreturn cf_check svm_do_resume(void)
         hvm_migrate_pirqs(v);
         /* Migrating to another ASID domain.  Request a new ASID. */
         hvm_asid_flush_vcpu(v);
+        vmcb->cleanbits.raw = 0;
     }
 
     if ( !vcpu_guestmode && !vlapic_hw_disabled(vlapic) )
-- 
2.55.0
Re: [PATCH] x86/svm: Don't clear clean bits on every context switch
Posted by Andrew Cooper 2 weeks, 2 days ago
On 06/10/2026 3:48 pm, Ross Lagerwall wrote:
> Clear the cleanbits only when the VMCB is used on a different pCPU
> rather than on every context switch to the vCPU. This follows the APM
> guideline for when to clear the cleanbits:
>
> "The hypervisor executes the guest on a different CPU core than one used
> the last time that guest was executed."

I think this wants the whole section from the APM, not just the one
sentence in isolation.  It doesn't quite form a stand-alone sentence.

I can fix up on commit.

>
> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>

Code-wise, Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

Re: [PATCH] x86/svm: Don't clear clean bits on every context switch
Posted by Jason Andryuk 2 weeks, 2 days ago
On 2026-10-06 10:48, Ross Lagerwall wrote:
> Clear the cleanbits only when the VMCB is used on a different pCPU
> rather than on every context switch to the vCPU. This follows the APM
> guideline for when to clear the cleanbits:
> 
> "The hypervisor executes the guest on a different CPU core than one used
> the last time that guest was executed."
> 
> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Reviewed-by: Jason Andryuk <jason.andryuk@amd.com>

The other times are:
"This is the first time a particular guest is run."

The vmcb page is zeroed in alloc_vmcb().

"The hypervisor has moved the guest's VMCB to a different physical page 
since the last time that guest was executed."

VMCBs aren't moved.

Regards,
Jason