[PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0

Yosry Ahmed posted 4 patches 2 months, 2 weeks ago
[PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Posted by Yosry Ahmed 2 months, 2 weeks ago
From: Jim Mattson <jmattson@google.com>

GIF==0 together with EFER.SVME==0 is a valid architectural
state. Don't return -EINVAL for KVM_SET_NESTED_STATE when this
combination is specified.

Fixes: cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")
Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Yosry Ahmed <yosry.ahmed@linux.dev>
Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
---
 arch/x86/kvm/svm/nested.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index c81005b24522..3e4bd8d69788 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
 	 */
 	if (!(vcpu->arch.efer & EFER_SVME)) {
-		/* GIF=1 and no guest mode are required if SVME=0.  */
-		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
+		/* GUEST_MODE must be clear when SVME==0 */
+		if (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)
 			return -EINVAL;
 	}
 
-- 
2.52.0.rc2.455.g230fcf2819-goog
Re: [PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Posted by Sean Christopherson 1 month ago
On Fri, Nov 21, 2025, Yosry Ahmed wrote:
> From: Jim Mattson <jmattson@google.com>
> 
> GIF==0 together with EFER.SVME==0 is a valid architectural
> state. Don't return -EINVAL for KVM_SET_NESTED_STATE when this
> combination is specified.
> 
> Fixes: cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")
> Signed-off-by: Jim Mattson <jmattson@google.com>
> Reviewed-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> ---
>  arch/x86/kvm/svm/nested.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> index c81005b24522..3e4bd8d69788 100644
> --- a/arch/x86/kvm/svm/nested.c
> +++ b/arch/x86/kvm/svm/nested.c
> @@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
>  	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
>  	 */
>  	if (!(vcpu->arch.efer & EFER_SVME)) {
> -		/* GIF=1 and no guest mode are required if SVME=0.  */
> -		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
> +		/* GUEST_MODE must be clear when SVME==0 */
> +		if (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)

Hmm, this is technically wrong, as it will allow KVM_STATE_NESTED_RUN_PENDING.
Now, arguably KVM already has a flaw there as KVM allows KVM_STATE_NESTED_RUN_PENDING
without KVM_STATE_NESTED_GUEST_MODE for SVME=1, but I'd prefer not to make the
hole bigger.

The nested if-statement is also unnecessary.

How about this instead?  (not yet tested)

	/*
	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
	 * If SVME is disabled, the only valid states are "none" and GIF=1
	 * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed).
	 */
	if (!(vcpu->arch.efer & EFER_SVME) && kvm_state->flags &&
	    kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
		return -EINVAL;
Re: [PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Posted by Yosry Ahmed 1 month ago
On Thu, Jan 08, 2026 at 10:58:40AM -0800, Sean Christopherson wrote:
> On Fri, Nov 21, 2025, Yosry Ahmed wrote:
> > From: Jim Mattson <jmattson@google.com>
> > 
> > GIF==0 together with EFER.SVME==0 is a valid architectural
> > state. Don't return -EINVAL for KVM_SET_NESTED_STATE when this
> > combination is specified.
> > 
> > Fixes: cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")
> > Signed-off-by: Jim Mattson <jmattson@google.com>
> > Reviewed-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> > Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> > ---
> >  arch/x86/kvm/svm/nested.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> > index c81005b24522..3e4bd8d69788 100644
> > --- a/arch/x86/kvm/svm/nested.c
> > +++ b/arch/x86/kvm/svm/nested.c
> > @@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
> >  	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
> >  	 */
> >  	if (!(vcpu->arch.efer & EFER_SVME)) {
> > -		/* GIF=1 and no guest mode are required if SVME=0.  */
> > -		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
> > +		/* GUEST_MODE must be clear when SVME==0 */
> > +		if (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)
> 
> Hmm, this is technically wrong, as it will allow KVM_STATE_NESTED_RUN_PENDING.
> Now, arguably KVM already has a flaw there as KVM allows KVM_STATE_NESTED_RUN_PENDING
> without KVM_STATE_NESTED_GUEST_MODE for SVME=1, but I'd prefer not to make the
> hole bigger.
> 
> The nested if-statement is also unnecessary.
> 
> How about this instead?  (not yet tested)
> 
> 	/*
> 	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
> 	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
> 	 * If SVME is disabled, the only valid states are "none" and GIF=1
> 	 * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed).
> 	 */
> 	if (!(vcpu->arch.efer & EFER_SVME) && kvm_state->flags &&
> 	    kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
> 		return -EINVAL;

Looks good to me, with the tiny exception that at this point clearing
SVME does set GIF. Maybe re-order the patches?

Let me know if you want me to send a new version or if you'll fix it up
while applying.
Re: [PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Posted by Sean Christopherson 1 month ago
On Thu, Jan 08, 2026, Yosry Ahmed wrote:
> On Thu, Jan 08, 2026 at 10:58:40AM -0800, Sean Christopherson wrote:
> > On Fri, Nov 21, 2025, Yosry Ahmed wrote:
> > > From: Jim Mattson <jmattson@google.com>
> > > 
> > > GIF==0 together with EFER.SVME==0 is a valid architectural
> > > state. Don't return -EINVAL for KVM_SET_NESTED_STATE when this
> > > combination is specified.
> > > 
> > > Fixes: cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")
> > > Signed-off-by: Jim Mattson <jmattson@google.com>
> > > Reviewed-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> > > Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> > > ---
> > >  arch/x86/kvm/svm/nested.c | 4 ++--
> > >  1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> > > index c81005b24522..3e4bd8d69788 100644
> > > --- a/arch/x86/kvm/svm/nested.c
> > > +++ b/arch/x86/kvm/svm/nested.c
> > > @@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
> > >  	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
> > >  	 */
> > >  	if (!(vcpu->arch.efer & EFER_SVME)) {
> > > -		/* GIF=1 and no guest mode are required if SVME=0.  */
> > > -		if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
> > > +		/* GUEST_MODE must be clear when SVME==0 */
> > > +		if (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)
> > 
> > Hmm, this is technically wrong, as it will allow KVM_STATE_NESTED_RUN_PENDING.
> > Now, arguably KVM already has a flaw there as KVM allows KVM_STATE_NESTED_RUN_PENDING
> > without KVM_STATE_NESTED_GUEST_MODE for SVME=1, but I'd prefer not to make the
> > hole bigger.
> > 
> > The nested if-statement is also unnecessary.
> > 
> > How about this instead?  (not yet tested)
> > 
> > 	/*
> > 	 * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
> > 	 * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
> > 	 * If SVME is disabled, the only valid states are "none" and GIF=1
> > 	 * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed).
> > 	 */
> > 	if (!(vcpu->arch.efer & EFER_SVME) && kvm_state->flags &&
> > 	    kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
> > 		return -EINVAL;
> 
> Looks good to me, with the tiny exception that at this point clearing
> SVME does set GIF. Maybe re-order the patches?
> 
> Let me know if you want me to send a new version or if you'll fix it up
> while applying.

No need for a new version.