From patchwork Thu Sep 28 17:33:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 146592 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp4093295vqu; Fri, 29 Sep 2023 07:55:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmkGOlen77K7GhrBZN0M8bvCcwnUFR4n3c/kCB+WCXoiCFYHrZdsdx6gOlXmAsZEQEs9B9 X-Received: by 2002:a9d:73cd:0:b0:6be:e3d8:b9d1 with SMTP id m13-20020a9d73cd000000b006bee3d8b9d1mr3998357otk.38.1695999340934; Fri, 29 Sep 2023 07:55:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695999340; cv=none; d=google.com; s=arc-20160816; b=wFacKXQoD05YhnuQgE8Bud9TVwdHXjaoBn0gienPzkSzILmhZWE1tZvvi07NE3JaWQ Mi6DzOM2zTfCHydq39qa1BYFnN+Bcfvby2NSgcOWbMfw5j5GdgurIRYF8qOXvY2geY5Y uckXmePJDdsx2jGXkp/nY2KNTmlwC168lKojxzu7sATEgBWcrTDoJdHsrVQHHDd6BeEu RPMH1Yq5iC+/prEWCtKHYHGpmJf217mIWFIpPBq8q0vRELTKskhFPhP2wd4OSgYnMi3Y Z5T+iAhedTjTPTDtkJfCubi80sWNzRqe7ntGwfdON08jDm2mTHoXQkJ9qQF/9ZWGyWaT kYKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oOqP2ppbORxupNPRbUHGH6nf4SvE0P2uUesTtFRc8Ik=; fh=7KlpA4Bjwb9QBszesWchOFreRBW5Vq50Y+lUlai2k4M=; b=UyzFZIel0bwEQ3EHmNBNaAKUzdwWL7XbcMQ0gsbd0l799vK6tTiDu2iN9VSkarOIQy sMp0p5tDTw6gNlfY+UIuXCrtblfgUVSnq3zCsS5QoxTVHUErEMeoS00dL81A0gL4nvp5 MpJgJpHsyVALt5MAJxOt9sn58tVPyJMasWgENwfhiMJwJ+ivI+CR1BIXFPLB9WnL5qOb +jy6hQslvUoccARyfaSlWEJ8WcqSiWCUy6vh3rm9ISoheVy/QuY+NNrNjtBhGYS6D4JW gEtV0L8cHtkd2pDOuw9Fll8my1rA6XDViQ5CPngXoGA/n5oNAHRHtLa9EUEuF9ymiOd+ drbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FRlxGiDp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id r136-20020a632b8e000000b00573f7b6999csi20034881pgr.440.2023.09.29.07.55.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 07:55:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FRlxGiDp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 6362D83271D2; Thu, 28 Sep 2023 10:35:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231952AbjI1RfD (ORCPT + 21 others); Thu, 28 Sep 2023 13:35:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231676AbjI1RfA (ORCPT ); Thu, 28 Sep 2023 13:35:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D798DD for ; Thu, 28 Sep 2023 10:34:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695922449; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oOqP2ppbORxupNPRbUHGH6nf4SvE0P2uUesTtFRc8Ik=; b=FRlxGiDpbtnvgCnp7ZehAaJE/ca/4sonYHYpkojVTRNOPVFX3bZsyqtjHbaAI6Le9Gem15 a6c5b5AydeIgle8Kku1aSQx5IGLdaWTGs1O2tlcMIrdQp4L6cFS07Ztf9C9fkZZmVDQQpY Qy6d+1l37Vyg05BE5IvCuy7+XU0Bytg= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-436-Df1GUGMiNMW4qntqZxmpqQ-1; Thu, 28 Sep 2023 13:34:04 -0400 X-MC-Unique: Df1GUGMiNMW4qntqZxmpqQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6EBE92810D42; Thu, 28 Sep 2023 17:34:03 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.226.141]) by smtp.corp.redhat.com (Postfix) with ESMTP id 723E314171B6; Thu, 28 Sep 2023 17:33:59 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: iommu@lists.linux.dev, "H. Peter Anvin" , Sean Christopherson , Maxim Levitsky , Paolo Bonzini , Thomas Gleixner , Borislav Petkov , Joerg Roedel , x86@kernel.org, Suravee Suthikulpanit , linux-kernel@vger.kernel.org, Dave Hansen , Will Deacon , Ingo Molnar , Robin Murphy , stable@vger.kernel.org Subject: [PATCH v2 1/4] x86: KVM: SVM: always update the x2avic msr interception Date: Thu, 28 Sep 2023 20:33:51 +0300 Message-Id: <20230928173354.217464-2-mlevitsk@redhat.com> In-Reply-To: <20230928173354.217464-1-mlevitsk@redhat.com> References: <20230928173354.217464-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 28 Sep 2023 10:35:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778329509484869717 X-GMAIL-MSGID: 1778384204582565563 The following problem exists since x2avic was enabled in the KVM: svm_set_x2apic_msr_interception is called to enable the interception of the x2apic msrs. In particular it is called at the moment the guest resets its apic. Assuming that the guest's apic was in x2apic mode, the reset will bring it back to the xapic mode. The svm_set_x2apic_msr_interception however has an erroneous check for '!apic_x2apic_mode()' which prevents it from doing anything in this case. As a result of this, all x2apic msrs are left unintercepted, and that exposes the bare metal x2apic (if enabled) to the guest. Oops. Remove the erroneous '!apic_x2apic_mode()' check to fix that. This fixes CVE-2023-5090 Fixes: 4d1d7942e36a ("KVM: SVM: Introduce logic to (de)activate x2AVIC mode") Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky Reviewed-by: Suravee Suthikulpanit Tested-by: Suravee Suthikulpanit --- arch/x86/kvm/svm/svm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 9507df93f410a63..acdd0b89e4715a3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -913,8 +913,7 @@ void svm_set_x2apic_msr_interception(struct vcpu_svm *svm, bool intercept) if (intercept == svm->x2avic_msrs_intercepted) return; - if (!x2avic_enabled || - !apic_x2apic_mode(svm->vcpu.arch.apic)) + if (!x2avic_enabled) return; for (i = 0; i < MAX_DIRECT_ACCESS_MSRS; i++) { From patchwork Thu Sep 28 17:33:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 146326 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3633416vqu; Thu, 28 Sep 2023 15:15:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJ+c53PMqs7BWOSHBnIYdrWRTKGb8Sq9dDokpOTNBaA+dxXB4SDJ17pGl2TxXZet6cz118 X-Received: by 2002:a17:90b:ec9:b0:26b:374f:97c2 with SMTP id gz9-20020a17090b0ec900b0026b374f97c2mr4062066pjb.6.1695939348478; Thu, 28 Sep 2023 15:15:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695939348; cv=none; d=google.com; s=arc-20160816; b=rZ5MawuHtiJ4iOkyDd1PksTEOYLEe6qECwcgkBd0fH66JLKbbPLt9siUXTipeEDfD1 EQXU2kDbeeQWYrKLdLrnA0boU83jHymXL01GH2B+XkMCkQQjc49cGTTAXegdlRJghQQD qz+9PP8MZXRHfhW/1B/jT4rc9SKvCym7pimoKaL2jVyBo2Xpgt6lUKkqjsZThWcsdun5 oYcfFotR5KhrVqVRwHM247b9rAroM+6at9thk79OljslMvrTDmBBDMvvKKuymnWeXV8h IJPpsxhrJuXrSlEL9ZjW95CObCvueUyPN/Ak/sCfbxuqBVDD9P7HBvP9iGCX+yeQ2iyj OuJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Y+xDOUzJP3Y3bm7S22bgnNGRpZTJ9N7w9x7x0py/CUU=; fh=7KlpA4Bjwb9QBszesWchOFreRBW5Vq50Y+lUlai2k4M=; b=kWIXFmhM1uaXgxsp/On/sMkPgMkycWYwHrZqgNAX0jtTfdmWiT5UBcH0pkyRvrMbRh OWJrZ2pkp55GDC3yjrXMn5IVH4G0zAQppwcG0h4H+od5LvxmvaY49bwOBDjQa5trQFIq xf2rFomupat27XPaQGAWBLxER2EEqia9XbMn2C1lnmJ00IP1ocZmRhm3GBzzP0VK6XkG wAbzqOuAi9vuWe1UOKRo3fYn52BROpcAEXGpnVSCMbDr2yd1lCYGXkyrPHA9D9ZJJmUG 5cTXlZmic0fWemdbCPOIe47DQsIS+jkqBuyFSn9KhHTrisD4YETMiOnrRpvBHNbOV+hS Ndiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jVpJtlYc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id j15-20020a170902da8f00b001c5ecff1bd4si16943639plx.603.2023.09.28.15.15.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 15:15:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jVpJtlYc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3880883FC3A8; Thu, 28 Sep 2023 10:35:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229870AbjI1Ret (ORCPT + 21 others); Thu, 28 Sep 2023 13:34:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjI1Res (ORCPT ); Thu, 28 Sep 2023 13:34:48 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A82351A2 for ; Thu, 28 Sep 2023 10:34:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695922452; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y+xDOUzJP3Y3bm7S22bgnNGRpZTJ9N7w9x7x0py/CUU=; b=jVpJtlYcgT8tMQ+DRLhKFceALJeYt8IUAHMZ7jnLeQ4C+kQ+j2mg17nsWuaJebiDfxaYsl EMP2is3P/RGgX4U4DBUf6kFM82eqkcN+0Ctv5Q456HTjwClz3b636xUDMTb3m3+8bXqcrH Yei6fGsSVMuSdEnzonLYWrN45ytec10= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-HV-dXwYNNUe7OoOnepqTxw-1; Thu, 28 Sep 2023 13:34:10 -0400 X-MC-Unique: HV-dXwYNNUe7OoOnepqTxw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 460F429AA386; Thu, 28 Sep 2023 17:34:07 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.226.141]) by smtp.corp.redhat.com (Postfix) with ESMTP id C356214171CA; Thu, 28 Sep 2023 17:34:03 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: iommu@lists.linux.dev, "H. Peter Anvin" , Sean Christopherson , Maxim Levitsky , Paolo Bonzini , Thomas Gleixner , Borislav Petkov , Joerg Roedel , x86@kernel.org, Suravee Suthikulpanit , linux-kernel@vger.kernel.org, Dave Hansen , Will Deacon , Ingo Molnar , Robin Murphy , stable@vger.kernel.org Subject: [PATCH v2 2/4] x86: KVM: SVM: add support for Invalid IPI Vector interception Date: Thu, 28 Sep 2023 20:33:52 +0300 Message-Id: <20230928173354.217464-3-mlevitsk@redhat.com> In-Reply-To: <20230928173354.217464-1-mlevitsk@redhat.com> References: <20230928173354.217464-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 28 Sep 2023 10:35:07 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778321297895500943 X-GMAIL-MSGID: 1778321297895500943 In later revisions of AMD's APM, there is a new 'incomplete IPI' exit code: "Invalid IPI Vector - The vector for the specified IPI was set to an illegal value (VEC < 16)" Note that tests on Zen2 machine show that this VM exit doesn't happen and instead AVIC just does nothing. Add support for this exit code by doing nothing, instead of filling the kernel log with errors. Also replace an unthrottled 'pr_err()' if another unknown incomplete IPI exit happens with vcpu_unimpl() (e.g in case AMD adds yet another 'Invalid IPI' exit reason) Cc: Signed-off-by: Maxim Levitsky Reviewed-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/avic.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 19bf955b67e0da0..3ac0ffc4f3e202b 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -268,6 +268,7 @@ enum avic_ipi_failure_cause { AVIC_IPI_FAILURE_TARGET_NOT_RUNNING, AVIC_IPI_FAILURE_INVALID_TARGET, AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, + AVIC_IPI_FAILURE_INVALID_IPI_VECTOR, }; #define AVIC_PHYSICAL_MAX_INDEX_MASK GENMASK_ULL(8, 0) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 2092db892d7d052..4b74ea91f4e6bb6 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -529,8 +529,11 @@ int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu) case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE: WARN_ONCE(1, "Invalid backing page\n"); break; + case AVIC_IPI_FAILURE_INVALID_IPI_VECTOR: + /* Invalid IPI with vector < 16 */ + break; default: - pr_err("Unknown IPI interception\n"); + vcpu_unimpl(vcpu, "Unknown avic incomplete IPI interception\n"); } return 1; From patchwork Thu Sep 28 17:33:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 146330 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3639255vqu; Thu, 28 Sep 2023 15:28:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEeSFMpbWtuKQfEwwbqDG6n5f9TS9IwimU9rPVKkvw7mlcSXkv27Z/EuRD+0ot+00Ocq2Gl X-Received: by 2002:a05:6808:92:b0:3a8:74bf:1541 with SMTP id s18-20020a056808009200b003a874bf1541mr2573077oic.50.1695940113130; Thu, 28 Sep 2023 15:28:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695940113; cv=none; d=google.com; s=arc-20160816; b=pR7XdT1hLxDoQA12eotMtwVylR8NOfbrW7KOho3++4Sh92BXhGBQRges0LI5C/M2RJ efU1/OpvLiVxd+k/0M0SCcIfC2CJ+XQ3Ha7DDqJO0IUaqdJ9O59IGVGCNtVWZYefRtjp DUqXpXmn35LoFg1yfwQ8RVIepaMLOZEV33akmglc0RbxuM2fcgCyooxcXzystsVmlkkf jkkotM0vBEMxTrmy8ZuZ3VSxxKDYKHntg03nqhyxH59ba9+RMP9+Kgr813WgFF36SsI5 MHUqBMxWZazWBQckO/XbE3dvK9m5/WhoMWgqGV0c3lB3EWexCa10OnwjvKBFFHkj7X6c gUlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jEwA8CyX78/GkMVoiAVbpRnGMlV0gBkCqcXscfLzkHs=; fh=7KlpA4Bjwb9QBszesWchOFreRBW5Vq50Y+lUlai2k4M=; b=mFJDxpmwKgKKf9nx8lYURKLYX/HT1bTLo+dRa5ZoZV1lNBVyeEE6URYpZpOUB9D6P4 N0irdqLda6Sz3lJqc30oXmQU6yE9taD8Xq/eteDVJYU77LUUYvGDsExUhhYhcfIZUyxf xLgAZxOQEDxcIIY1jPQvI+zHF9YRbypRuQwBeYDQ3Iy72yf82Rz/giqpEnLQHicwYdvz lVyTy6Wl4WWvMZk96DWoW7cOHdeD5hjoet2DkLaswMO2/IrGxzeC+5pxqlmbC3RlPfQO Dl4m+r5VIWQxm5UVs/4IsO+SnzuMpMISbdKZEvm5W23s6q04zdtlOGdEYzL7UD7ioa0o xleQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S4rYQnMi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id x7-20020a634847000000b005775e17f560si18994561pgk.82.2023.09.28.15.28.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 15:28:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S4rYQnMi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id E273883FC3AB; Thu, 28 Sep 2023 10:35:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232033AbjI1RfF (ORCPT + 21 others); Thu, 28 Sep 2023 13:35:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231954AbjI1RfE (ORCPT ); Thu, 28 Sep 2023 13:35:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44DEFC0 for ; Thu, 28 Sep 2023 10:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695922454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jEwA8CyX78/GkMVoiAVbpRnGMlV0gBkCqcXscfLzkHs=; b=S4rYQnMiIKh30TVOkRSTq3/TAykdShsSTM6paTA87i/SJFxgzuYpMDc1JSl66zQDU5B837 Na5cM4ASSadGM48+fEgLVJXJzfELXFUBomCexcRW2mLD90TnmDCmk9R4fo8NscbKaC6oYv fDCHPgDIgUdcrq1xE2VF1CfLmKIohUY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-8q4-2IX4M9GzgZ9LF6aO2A-1; Thu, 28 Sep 2023 13:34:12 -0400 X-MC-Unique: 8q4-2IX4M9GzgZ9LF6aO2A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1C8F9185A790; Thu, 28 Sep 2023 17:34:11 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.226.141]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B43D14171CB; Thu, 28 Sep 2023 17:34:07 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: iommu@lists.linux.dev, "H. Peter Anvin" , Sean Christopherson , Maxim Levitsky , Paolo Bonzini , Thomas Gleixner , Borislav Petkov , Joerg Roedel , x86@kernel.org, Suravee Suthikulpanit , linux-kernel@vger.kernel.org, Dave Hansen , Will Deacon , Ingo Molnar , Robin Murphy , stable@vger.kernel.org Subject: [PATCH v2 3/4] x86: KVM: SVM: refresh AVIC inhibition in svm_leave_nested() Date: Thu, 28 Sep 2023 20:33:53 +0300 Message-Id: <20230928173354.217464-4-mlevitsk@redhat.com> In-Reply-To: <20230928173354.217464-1-mlevitsk@redhat.com> References: <20230928173354.217464-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 28 Sep 2023 10:35:15 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778322100145133197 X-GMAIL-MSGID: 1778322100145133197 svm_leave_nested() similar to a nested VM exit, get the vCPU out of nested mode and thus should end the local inhibition of AVIC on this vCPU. Failure to do so, can lead to hangs on guest reboot. Raise the KVM_REQ_APICV_UPDATE request to refresh the AVIC state of the current vCPU in this case. Fixes: f44509f849fe ("KVM: x86: SVM: allow AVIC to co-exist with a nested guest running") Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky Reviewed-by: Sean Christopherson --- arch/x86/kvm/svm/nested.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index dd496c9e5f91f28..3fea8c47679e689 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1253,6 +1253,9 @@ void svm_leave_nested(struct kvm_vcpu *vcpu) nested_svm_uninit_mmu_context(vcpu); vmcb_mark_all_dirty(svm->vmcb); + + if (kvm_apicv_activated(vcpu->kvm)) + kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu); } kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); From patchwork Thu Sep 28 17:33:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 146364 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3681742vqu; Thu, 28 Sep 2023 17:06:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIsL3TuVkbLETgsxy89umk3QgoGaVRoVecRk2qxZfaESsZWiPNPfgDnlxtITgv7ihaBaVu X-Received: by 2002:a05:6a21:3d8d:b0:157:eb32:e775 with SMTP id bj13-20020a056a213d8d00b00157eb32e775mr2699072pzc.62.1695946002128; Thu, 28 Sep 2023 17:06:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695946002; cv=none; d=google.com; s=arc-20160816; b=wFxqnQijh4RpBB8JbmdtCSHqtKI6bRqvo/2KjVp7WviCKyTg+QxUBaJcGJHb45Lisc /MJbzCapGBqva+Wd7VG+OsSIeeojsNVa468sLHNxINPb5uGvrUtpdMS6ut6yA25rsUdj a+cZ92v51spHznQ3h8rPYDbN/ZhA3aMRFsblo/jUX3zr/M9Tq+/ljjCrH5TAZhhj7xDZ P6oZ3e3rNxw3lHiJdw2b5DNeXz1s3kX8hnmOkqYup8OJGAbe8IUYgQqa+IHLG+U1of6/ nvW1PEmhpMu8EIZYqM1NIKqMqJfUrVD7jr1HCWYHscS+X7PtHgLJ3KAueozEwF2wRTid XugQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bXvR8P30J13RoiFY6oQnabnLblTIM5XWqXhOKK4pnys=; fh=oNJvlPmIFpwfIkAABcMx9rUySvQYt47L4yNhdOcwda8=; b=nBxSCnymo+pKQoztK3WVg3G0lemSPOObN3vwVmCijFs4wDIMy9n6A4oaU1HGuT9VRQ M34nk6+7/GQw4Dm0fx7pqj097NCsoO2F7eMUK6mgFRMcPpQgcugjlC5JjnYFng/lqaLN Oqr9V6PbhL3Fp8JlhK6Aafp1kFLiNSbcu/PiIg+Paudamu3Pp9FiBi3yXBVr2s001Ice E063Y9Kwch8CTMcE+xUONtVL9nrQhRQUcurYRZYxqoHUjdNAfRClqFpIAcFd30wgqSWt eDx1aHAo0ypY7AZOMyr9n+pxi6CyWL8YhHuB+5pfAR054D3QnpvsBi48fEwDqDvDIxpE vq6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fbx2zvWk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id c9-20020a170903234900b001c737e1db94si3076070plh.110.2023.09.28.17.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 17:06:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fbx2zvWk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 7C254802DBA4; Thu, 28 Sep 2023 10:35:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232086AbjI1Rfk (ORCPT + 21 others); Thu, 28 Sep 2023 13:35:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231945AbjI1Rfi (ORCPT ); Thu, 28 Sep 2023 13:35:38 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 597921AA for ; Thu, 28 Sep 2023 10:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695922459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bXvR8P30J13RoiFY6oQnabnLblTIM5XWqXhOKK4pnys=; b=fbx2zvWkjYg0AOSjYpulhMrCWZX4Q3pA+YhVb5to5xz4fyrVaTgWlWIH/8EIDGTc7DQmw8 ZMNi9zLBHul96yavYJiV8p3F3VT5UTFE+jSaCVGintoC9/9PMbGf/B18a0djFjkRFwM6wT MB7UDZcphOI7U7vXZI8FOl7vWAFxJyc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-527-dV2krgcqMdaXISgjC_sr8Q-1; Thu, 28 Sep 2023 13:34:15 -0400 X-MC-Unique: dV2krgcqMdaXISgjC_sr8Q-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B8A308015AB; Thu, 28 Sep 2023 17:34:14 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.226.141]) by smtp.corp.redhat.com (Postfix) with ESMTP id 710B814171B6; Thu, 28 Sep 2023 17:34:11 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: iommu@lists.linux.dev, "H. Peter Anvin" , Sean Christopherson , Maxim Levitsky , Paolo Bonzini , Thomas Gleixner , Borislav Petkov , Joerg Roedel , x86@kernel.org, Suravee Suthikulpanit , linux-kernel@vger.kernel.org, Dave Hansen , Will Deacon , Ingo Molnar , Robin Murphy Subject: [PATCH v2 4/4] x86: KVM: SVM: workaround for AVIC's errata #1235 Date: Thu, 28 Sep 2023 20:33:54 +0300 Message-Id: <20230928173354.217464-5-mlevitsk@redhat.com> In-Reply-To: <20230928173354.217464-1-mlevitsk@redhat.com> References: <20230928173354.217464-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 28 Sep 2023 10:35:57 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778328275188466902 X-GMAIL-MSGID: 1778328275188466902 On Zen2 (and likely on Zen1 as well), AVIC doesn't reliably detect a change in the 'is_running' bit during ICR write emulation and might skip a VM exit, if that bit was recently cleared. The absence of the VM exit, leads to the KVM not waking up / triggering nested vm exit on the target(s) of the IPI which can, in some cases, lead to an unbounded delays in the guest execution. As I recently discovered, a reasonable workaround exists: make the KVM never set the is_running bit. This workaround ensures that (*) all ICR writes always cause a VM exit and therefore correctly emulated, in expense of never enjoying VM exit-less ICR emulation. This workaround does carry a performance penalty but according to my benchmarks is still much better than not using AVIC at all, because AVIC is still used for the receiving end of the IPIs, and for the posted interrupts. If the user is aware of the errata and it doesn't affect his workload, the user can disable the workaround with 'avic_zen2_errata_workaround=0' (*) More correctly all ICR writes except when 'Self' shorthand is used: In this case AVIC skips reading physid table and just sets bits in IRR of local APIC. Thankfully in this case, the errata is not possible, therefore an extra workaround for this case is not needed. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 63 +++++++++++++++++++++++++++++------------ arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 4b74ea91f4e6bb6..28bb0e6b321660d 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -62,6 +62,9 @@ static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) == -1u); static bool force_avic; module_param_unsafe(force_avic, bool, 0444); +static int avic_zen2_errata_workaround = -1; +module_param(avic_zen2_errata_workaround, int, 0444); + /* Note: * This hash table is used to map VM_ID to a struct kvm_svm, * when handling AMD IOMMU GALOG notification to schedule in @@ -276,7 +279,7 @@ static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, static int avic_init_backing_page(struct kvm_vcpu *vcpu) { - u64 *entry, new_entry; + u64 *entry; int id = vcpu->vcpu_id; struct vcpu_svm *svm = to_svm(vcpu); @@ -308,10 +311,10 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) if (!entry) return -EINVAL; - new_entry = __sme_set((page_to_phys(svm->avic_backing_page) & - AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | - AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); - WRITE_ONCE(*entry, new_entry); + svm->avic_physical_id_entry = __sme_set((page_to_phys(svm->avic_backing_page) & + AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | + AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); + WRITE_ONCE(*entry, svm->avic_physical_id_entry); svm->avic_physical_id_cache = entry; @@ -835,7 +838,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) * will update the pCPU info when the vCPU awkened and/or scheduled in. * See also avic_vcpu_load(). */ - entry = READ_ONCE(*(svm->avic_physical_id_cache)); + entry = READ_ONCE(svm->avic_physical_id_entry); if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK, true, pi->ir_data); @@ -1027,7 +1030,6 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { - u64 entry; int h_physical_id = kvm_cpu_get_apicid(cpu); struct vcpu_svm *svm = to_svm(vcpu); unsigned long flags; @@ -1056,14 +1058,23 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); - entry = READ_ONCE(*(svm->avic_physical_id_cache)); - WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); - entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; - entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); - entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + WARN_ON_ONCE(svm->avic_physical_id_entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); + + svm->avic_physical_id_entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; + svm->avic_physical_id_entry |= + (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); + + svm->avic_physical_id_entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + + /* + * Do not update the actual physical id table entry if workaround + * for #1235 - the physical ID entry is_running is never set when + * the workaround is activated + */ + if (!avic_zen2_errata_workaround) + WRITE_ONCE(*(svm->avic_physical_id_cache), svm->avic_physical_id_entry); - WRITE_ONCE(*(svm->avic_physical_id_cache), entry); avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); spin_unlock_irqrestore(&svm->ir_list_lock, flags); @@ -1071,7 +1082,6 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) void avic_vcpu_put(struct kvm_vcpu *vcpu) { - u64 entry; struct vcpu_svm *svm = to_svm(vcpu); unsigned long flags; @@ -1084,10 +1094,9 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) * can't be scheduled out and thus avic_vcpu_{put,load}() can't run * recursively. */ - entry = READ_ONCE(*(svm->avic_physical_id_cache)); /* Nothing to do if IsRunning == '0' due to vCPU blocking. */ - if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) + if (!(svm->avic_physical_id_entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) return; /* @@ -1102,8 +1111,14 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) avic_update_iommu_vcpu_affinity(vcpu, -1, 0); - entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; - WRITE_ONCE(*(svm->avic_physical_id_cache), entry); + svm->avic_physical_id_entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + + /* + * Do not update the actual physical id table entry + * See explanation in avic_vcpu_load + */ + if (!avic_zen2_errata_workaround) + WRITE_ONCE(*(svm->avic_physical_id_cache), svm->avic_physical_id_entry); spin_unlock_irqrestore(&svm->ir_list_lock, flags); @@ -1217,5 +1232,17 @@ bool avic_hardware_setup(void) amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); + if (avic_zen2_errata_workaround == -1) { + + /* Assume that Zen1 and Zen2 have errata #1235 */ + if (boot_cpu_data.x86 == 0x17) + avic_zen2_errata_workaround = 1; + else + avic_zen2_errata_workaround = 0; + } + + if (avic_zen2_errata_workaround) + pr_info("Workaround for AVIC errata #1235 is enabled\n"); + return true; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index be67ab7fdd104e3..98dc45b9c194d2e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -265,6 +265,7 @@ struct vcpu_svm { u32 ldr_reg; u32 dfr_reg; struct page *avic_backing_page; + u64 avic_physical_id_entry; u64 *avic_physical_id_cache; /*