Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9076DC433EF for ; Tue, 7 Dec 2021 17:28:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240005AbhLGRcX (ORCPT ); Tue, 7 Dec 2021 12:32:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239791AbhLGRcX (ORCPT ); Tue, 7 Dec 2021 12:32:23 -0500 Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D2EFC061746 for ; Tue, 7 Dec 2021 09:28:52 -0800 (PST) Received: by mail-oi1-x231.google.com with SMTP id bj13so28985878oib.4 for ; Tue, 07 Dec 2021 09:28:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/AvOf7ER8v5H0MjABhsd0BcEX4X+zqr8vh7bP9dwVAA=; b=ZvcljMy7ztZ2M2RzI/cYNwQh+ld65A/KWrGMi0BI/g1hKirUUjFuJERVQBNwc9G/nb vViR2AjLmz3v4X3QEpZPDgCcEHG6OSHgAzYlCaDw4qY58CmleIVOWJF+Eruvq7Y+5sfo BL6/Piynk8knxTaad4opvmEUSA1e17+Mr0tX1Fvsd4WnZxFgLAwrri+GnHagzKyPFlkw y4nwcZmjzGsVT55Uc8hwTGDBtnRTNhvQI2yHGZu+7Jfv4OEl3OilEAANz0AuOc9P14Zb oOYzyrJ1F2/F7TE3QqVnML7eGobICS8ZpdrN7Nzf0uCd+F+FWFmYkfFLegeyblPmonRt kdPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/AvOf7ER8v5H0MjABhsd0BcEX4X+zqr8vh7bP9dwVAA=; b=xlQH0hsjLXI0TrlV8dSfSdARmVf4ynbCcsK2ZIYKIuGDSWZGbYo19zzLDvU1U89nDr tElWEHm0TiWLQpUX4IiYDNOWo55tqYe86iaMyzy81CZJkODEbf2KKn4Hu2KT5SlOD980 JLSTz3rvjC26JbDpnPK/F12fnuF6v9kL6J/EnkllPDe0NPixkFEFAMLuIPaORMiDIY3q t41MXd+bGtwgLQRyWQDWCRO+fkQn7onWjrvEPKrcmOjAVIQubaJdBzHi3lu3JDNuctZ/ Y0XGDsrj8CVjuTW2KnlxcGqam94EN8sw3JL/XohS+BvB8wm/1FLjcp+w4psNj+CgqDab 45dw== X-Gm-Message-State: AOAM531QOpXivTMNrUSeG3rbE+TNEv0F2j8iWGIYWf6MGEYr+TrMNugS 42iXhnMMoUKEjgrr24JX6ueOyyDOhaczDIRzTiW1Icfcaj87Hg== X-Google-Smtp-Source: ABdhPJzKZi3NPnBCVYoeIOunfFC4LiThbaSzLqhOKTlwi9M5FmOEA5dK5aAeDwO4w+3RZwxMjvr3esT4tI4hPHO6ip4= X-Received: by 2002:a54:4515:: with SMTP id l21mr6394061oil.15.1638898120973; Tue, 07 Dec 2021 09:28:40 -0800 (PST) MIME-Version: 1.0 References: <20211207043100.3357474-1-marcorr@google.com> <5e69c0ca-389c-3ace-7559-edd901a0ab3c@amd.com> In-Reply-To: From: Marc Orr Date: Tue, 7 Dec 2021 09:28:29 -0800 Message-ID: Subject: Re: [PATCH] KVM: x86: Always set kvm_run->if_flag To: Sean Christopherson Cc: Tom Lendacky , pbonzini@redhat.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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 7, 2021 at 8:34 AM Sean Christopherson wrote: > > On Tue, Dec 07, 2021, Tom Lendacky wrote: > > On 12/7/21 9:14 AM, Marc Orr wrote: > > > On Tue, Dec 7, 2021 at 6:43 AM Tom Lendacky wrote: > > > > > +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: > > > > > > > > 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." > > > > I just verified with the hardware team that this flag is indeed only set for > > a guest with protected state (SEV-ES / SEV-SNP). An update to the APM will > > be made. > > svm_interrupt_blocked() should be modified to use the new svm_get_if_flag() > helper so that the SEV-{ES,SN} behavior is contained in a single location, e.g. > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 208566f63bce..fef04e9fa9c9 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -3583,14 +3583,10 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) > if (!gif_set(svm)) > return true; > > - if (sev_es_guest(vcpu->kvm)) { > - /* > - * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask > - * bit to determine the state of the IF flag. > - */ > - if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK)) > + if (!is_guest_mode(vcpu)) { > + if (!svm_get_if_flag(vcpu)) > return true; > - } else if (is_guest_mode(vcpu)) { > + } else { > /* As long as interrupts are being delivered... */ > if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) > ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF) > @@ -3600,9 +3596,6 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) > /* ... vmexits aren't blocked by the interrupt shadow */ > if (nested_exit_on_intr(svm)) > return false; > - } else { > - if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF)) > - return true; > } > > return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK); Agreed. This is a nice change. I'll incorporate it into v2. Thanks!