kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Manali Shukla <manali.shukla@amd.com>
Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
	pbonzini@redhat.com,  nikunj@amd.com, thomas.lendacky@amd.com,
	bp@alien8.de
Subject: Re: [PATCH v4 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs
Date: Wed, 23 Apr 2025 08:44:12 -0700	[thread overview]
Message-ID: <aAkKzEpNXDgC9_Vh@google.com> (raw)
In-Reply-To: <20250324130248.126036-5-manali.shukla@amd.com>

On Mon, Mar 24, 2025, Manali Shukla wrote:
> +	if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip)) {
> +		vmcb02->control.bus_lock_counter = 1;
> +		svm->bus_lock_rip = svm->nested.ctl.bus_lock_rip;
> +	} else {
> +		vmcb02->control.bus_lock_counter = 0;
> +	}
> +	svm->nested.ctl.bus_lock_rip = INVALID_GPA;
> +
>  	/* Done at vmrun: asid.  */
>  
>  	/* Also overwritten later if necessary.  */
> @@ -1039,6 +1069,18 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
>  
>  	}
>  
> +	/*
> +	 * If bus_lock_counter is nonzero and the guest has not moved past the
> +	 * guilty instruction, save bus_lock_rip in svm_nested_state. This will
> +	 * help determine at nested VMRUN whether to stash vmcb02's counter or
> +	 * reset it to '0'.
> +	 */
> +	if (vmcb02->control.bus_lock_counter &&
> +	    svm->bus_lock_rip == vmcb02->save.rip)
> +		svm->nested.ctl.bus_lock_rip = svm->bus_lock_rip;
> +	else
> +		svm->nested.ctl.bus_lock_rip = INVALID_GPA;
> +
>  	nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
>  
>  	svm_switch_vmcb(svm, &svm->vmcb01);

...

> +static int bus_lock_exit(struct kvm_vcpu *vcpu)
> +{
> +	struct vcpu_svm *svm = to_svm(vcpu);
> +
> +	vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
> +	vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
> +
> +	vcpu->arch.cui_linear_rip = kvm_get_linear_rip(vcpu);
> +	svm->bus_lock_rip = vcpu->arch.cui_linear_rip;
> +	vcpu->arch.complete_userspace_io = complete_userspace_buslock;
> +
> +	return 0;
> +}

> @@ -327,6 +328,7 @@ struct vcpu_svm {
>  
>  	/* Guest GIF value, used when vGIF is not enabled */
>  	bool guest_gif;
> +	u64 bus_lock_rip;

I don't think this field is necessary.  Rather than unconditionally invalidate
on nested VMRUN and then conditionally restore on nested #VMEXIT, just leave
svm->nested.ctl.bus_lock_rip set on VMRUN and conditionally invalidate on #VMEXIT.
And then in bus_lock_exit(), update the field if the exit occurred while L2 is
active.

Completely untested:

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index a42ef7dd9143..98e065a93516 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -700,13 +700,10 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
         * L1 re-enters L2, the same instruction will trigger a VM-Exit and the
         * entire cycle start over.
         */
-       if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip)) {
+       if (vmcb02->save.rip && (svm->nested.ctl.bus_lock_rip == vmcb02->save.rip))
                vmcb02->control.bus_lock_counter = 1;
-               svm->bus_lock_rip = svm->nested.ctl.bus_lock_rip;
-       } else {
+       else
                vmcb02->control.bus_lock_counter = 0;
-       }
-       svm->nested.ctl.bus_lock_rip = INVALID_GPA;
 
        /* Done at vmrun: asid.  */
 
@@ -1070,15 +1067,10 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
        }
 
        /*
-        * If bus_lock_counter is nonzero and the guest has not moved past the
-        * guilty instruction, save bus_lock_rip in svm_nested_state. This will
-        * help determine at nested VMRUN whether to stash vmcb02's counter or
-        * reset it to '0'.
+        * Invalidate bus_lock_rip unless kVM is still waiting for the guest
+        * to make forward progress before re-enabling bus lock detection.
         */
-       if (vmcb02->control.bus_lock_counter &&
-           svm->bus_lock_rip == vmcb02->save.rip)
-               svm->nested.ctl.bus_lock_rip = svm->bus_lock_rip;
-       else
+       if (!vmcb02->control.bus_lock_counter)
                svm->nested.ctl.bus_lock_rip = INVALID_GPA;
 
        nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index ea12e93ae983..11ce031323fd 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -3333,9 +3333,10 @@ static int bus_lock_exit(struct kvm_vcpu *vcpu)
        vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
 
        vcpu->arch.cui_linear_rip = kvm_get_linear_rip(vcpu);
-       svm->bus_lock_rip = vcpu->arch.cui_linear_rip;
        vcpu->arch.complete_userspace_io = complete_userspace_buslock;
 
+       if (is_guest_mode(vcpu))
+               svm->nested.ctl.bus_lock_rip = vcpu->arch.cui_linear_rip;
        return 0;
 }
 
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 7a4c5848c952..8667faccaedc 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -328,7 +328,6 @@ struct vcpu_svm {
 
        /* Guest GIF value, used when vGIF is not enabled */
        bool guest_gif;
-       u64 bus_lock_rip;
 };
 
 struct svm_cpu_data {


  parent reply	other threads:[~2025-04-23 15:44 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-24 13:02 [PATCH v4 0/5] Add support for the Bus Lock Threshold Manali Shukla
2025-03-24 13:02 ` [PATCH v4 1/5] KVM: x86: Preparatory patch to move linear_rip out of kvm_pio_request Manali Shukla
2025-04-23 15:22   ` Sean Christopherson
2025-03-24 13:02 ` [PATCH v4 2/5] x86/cpufeatures: Add CPUID feature bit for the Bus Lock Threshold Manali Shukla
2025-03-24 21:56   ` Borislav Petkov
2025-04-09  6:00     ` Manali Shukla
2025-04-09  9:21       ` Borislav Petkov
2025-04-10 23:25         ` Sean Christopherson
2025-04-23  5:58           ` Manali Shukla
2025-03-24 13:02 ` [PATCH v4 3/5] KVM: SVM: Enable Bus lock threshold exit Manali Shukla
2025-04-16  6:00   ` Xiaoyao Li
2025-04-23  6:15     ` Manali Shukla
2025-04-23 15:29       ` Sean Christopherson
2025-04-30 11:15         ` Manali Shukla
2025-03-24 13:02 ` [PATCH v4 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs Manali Shukla
2025-04-16  6:14   ` Xiaoyao Li
2025-04-23 11:26     ` Manali Shukla
2025-04-23 15:30     ` Sean Christopherson
2025-04-30 11:18       ` Manali Shukla
2025-04-23 15:44   ` Sean Christopherson [this message]
2025-04-30 11:30     ` Manali Shukla
2025-03-24 13:02 ` [PATCH v4 5/5] KVM: selftests: Add bus lock exit test Manali Shukla

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aAkKzEpNXDgC9_Vh@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=manali.shukla@amd.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=thomas.lendacky@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).