From: Maxim Levitsky <mlevitsk@redhat.com>
To: Marc Orr <marcorr@google.com>, Tom Lendacky <Thomas.Lendacky@amd.com>
Cc: pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com,
wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org,
tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] KVM: x86: Always set kvm_run->if_flag
Date: Tue, 07 Dec 2021 17:33:51 +0200 [thread overview]
Message-ID: <0a6933a1679668e1da81f0bfbb850636d4a5ebb5.camel@redhat.com> (raw)
In-Reply-To: <CAA03e5E7-ns7w9B9Tu7pSWzCo0Nh7Ba5jwQXcn_XYPf_reRq9Q@mail.gmail.com>
On Tue, 2021-12-07 at 07:14 -0800, Marc Orr wrote:
> On Tue, Dec 7, 2021 at 6:43 AM Tom Lendacky <thomas.lendacky@amd.com> wrote:
> > On 12/6/21 10:31 PM, Marc Orr wrote:
> > > The kvm_run struct's if_flag is apart of the userspace/kernel API. The
> > > SEV-ES patches failed to set this flag because it's no longer needed by
> > > QEMU (according to the comment in the source code). However, other
> > > hypervisors may make use of this flag. Therefore, set the flag for
> > > guests with encrypted regiesters (i.e., with guest_state_protected set).
> > >
> > > Fixes: f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
> > > Signed-off-by: Marc Orr <marcorr@google.com>
> > > ---
> > > arch/x86/include/asm/kvm-x86-ops.h | 1 +
> > > arch/x86/include/asm/kvm_host.h | 1 +
> > > arch/x86/kvm/svm/svm.c | 8 ++++++++
> > > arch/x86/kvm/vmx/vmx.c | 6 ++++++
> > > arch/x86/kvm/x86.c | 9 +--------
> > > 5 files changed, 17 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
> > > index cefe1d81e2e8..9e50da3ed01a 100644
> > > --- a/arch/x86/include/asm/kvm-x86-ops.h
> > > +++ b/arch/x86/include/asm/kvm-x86-ops.h
> > > @@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
> > > KVM_X86_OP(cache_reg)
> > > KVM_X86_OP(get_rflags)
> > > KVM_X86_OP(set_rflags)
> > > +KVM_X86_OP(get_if_flag)
> > > KVM_X86_OP(tlb_flush_all)
> > > KVM_X86_OP(tlb_flush_current)
> > > KVM_X86_OP_NULL(tlb_remote_flush)
> > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> > > index 860ed500580c..a7f868ff23e7 100644
> > > --- a/arch/x86/include/asm/kvm_host.h
> > > +++ b/arch/x86/include/asm/kvm_host.h
> > > @@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
> > > void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
> > > unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
> > > void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
> > > + bool (*get_if_flag)(struct kvm_vcpu *vcpu);
> > >
> > > void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
> > > void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
> > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> > > index d0f68d11ec70..91608f8c0cde 100644
> > > --- a/arch/x86/kvm/svm/svm.c
> > > +++ b/arch/x86/kvm/svm/svm.c
> > > @@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
> > > to_svm(vcpu)->vmcb->save.rflags = rflags;
> > > }
> > >
> > > +static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
> > > +{
> > > + struct vmcb *vmcb = to_svm(vcpu)->vmcb;
> > > +
> > > + return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);
> >
> > I'm not sure if this is always valid to use for non SEV-ES guests. Maybe
> > the better thing would be:
I also noticed long ago that SVM_GUEST_INTERRUPT_MASK seems to duplicate the EFLAGS.IF value,
it seems to have no other purpose.
Best regards,
Maxim Levitsky
> >
> > return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
> > : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;
> >
> > (Since this function returns a bool, I don't think you need the !!)
>
> I had the same reservations when writing the patch. (Why fix what's
> not broken.) The reason I wrote the patch this way is based on what I
> read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK -
> Value of the RFLAGS.IF bit for the guest."
>
> Also, I had _thought_ that `svm_interrupt_allowed()` -- the
> AMD-specific function used to populate `ready_for_interrupt_injection`
> -- was relying on `GUEST_INTERRUPT_MASK`. But now I'm reading the code
> again, and I realized I was overly focused on the SEV-ES handling.
> That code is actually extracting the IF bit from the RFLAGS register
> in the same way you've proposed here.
>
> Changing the patch as you've suggested SGTM. I can send out a v2. I'll
> wait a day or two to see if there are any other comments first. I
> guess the alternative would be to change `svm_interrupt_blocked()` to
> solely use the `SVM_GUEST_INTERRUPT_MASK`. If we were confident that
> it was sufficient, it would be a nice little cleanup. But regardless,
> I think we should keep the code introduced by this patch consistent
> with `svm_interrupt_blocked()`.
>
> Also, noted on the `!!` not being needed when returning from a bool
> function. I'll keep this in mind in the future. Thanks!
>
next prev parent reply other threads:[~2021-12-07 15:34 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-07 4:31 Marc Orr
2021-12-07 4:49 ` Jim Mattson
2021-12-07 14:43 ` Tom Lendacky
2021-12-07 15:14 ` Marc Orr
2021-12-07 15:33 ` Maxim Levitsky [this message]
2021-12-07 16:00 ` Tom Lendacky
2021-12-07 16:34 ` Sean Christopherson
2021-12-07 17:28 ` Marc Orr
2021-12-07 17:28 ` Marc Orr
2021-12-09 17:52 ` Paolo Bonzini
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=0a6933a1679668e1da81f0bfbb850636d4a5ebb5.camel@redhat.com \
--to=mlevitsk@redhat.com \
--cc=Thomas.Lendacky@amd.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcorr@google.com \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=x86@kernel.org \
/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
all inboxes | Powered by JetHome®