From patchwork Mon Oct 2 11:57:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 147471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1691903vqb; Mon, 2 Oct 2023 14:08:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYmNlFz2guHT02znoPDevU5J/MNj0gVigRcX9r8pLBpof3aZeCtkVHCUFs/u0lERsmJHD1 X-Received: by 2002:a05:6a20:748d:b0:158:7fdf:66df with SMTP id p13-20020a056a20748d00b001587fdf66dfmr15256135pzd.18.1696280920444; Mon, 02 Oct 2023 14:08:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696280920; cv=none; d=google.com; s=arc-20160816; b=CAFc57TVQfvbsd5Z71aH7pYMa57P2e3tZJMBqlpjE5NLe3zRSPdiih6hMfebnbkulF qa9RfTa5cVoQhFiY39l+QBT8kOJ9ssKZNYbnb6NRON6XVJP+CGkdxr6wU0bxYWeNGa4h MYwcmBfjbQsD4bJmM0CPBn26lmapkDa+giaslOrbrKGDqw6EH2HemRtSbC/o90xsnSjG BUfZeXhlN7z8TkdQKP+BRsOpSUJRMA+w3Vwx+7pYi+5ajJxY15WU2GW2PfpW4Co7Srlf Nw07pXlbHHhOfKpIPp7YH0f8L5ynOBUfy9pkFRqmPrNSMGLPDzNT9WStHvvGtzH+sSjc WgbA== 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=k2JA8Z38D8eUi/xmH+rBm9nb9Bwfoc30nugRqNPb0OY=; fh=OogiIvQ2zUR209vET+yuT7qDV8D5we5HX6IGkxNqqHY=; b=Sk00o7O0DKrLo++/0tku9usi4Hq18dtAfsSxh1whcIpAqmxdY9BQWgH0sfXmaHGAv/ tZ3unY9j/shhQKmDG9FSQ1OJG+Pwdi/JMzu/enBTm8OiNBSs0jJV6F+QOckrZ7m4K3zU Nha1hE9lilTX1diMNkWQYlL9i3nLKrFNxjU3KrVc5x3h/k+r6WEecXLOgdJHv0de+l1P nX62k2F6xfFC/Hla0vAs3rydNlex+fwx7w4azb/FwkU73eDFfGVT60qunh4G3oFPQyzC wiBQxJYS17uhoO2LSraWmE973LrrV09y25x6UxATSeTkzLSmiaYJ2XRHlv2c3pvBNNnG xSAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d4fMeZt5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id w14-20020a056a0014ce00b0068a6eb3b548si30347952pfu.401.2023.10.02.14.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 14:08:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d4fMeZt5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 6225B80965BD; Mon, 2 Oct 2023 04:59:00 -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 S236835AbjJBL62 (ORCPT + 18 others); Mon, 2 Oct 2023 07:58:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236604AbjJBL6Z (ORCPT ); Mon, 2 Oct 2023 07:58:25 -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 65F0ED8 for ; Mon, 2 Oct 2023 04:57:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696247860; 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=k2JA8Z38D8eUi/xmH+rBm9nb9Bwfoc30nugRqNPb0OY=; b=d4fMeZt522x/Yg7vVF3sug+c1n8kyw1YcJ95IBZ7hLG+B4pyPnmqfc1sR8a2P7NJyv1M+v 9v6/RfRAtWakLpBOLwDBdPup3CUajvyw6hMUBmbh2uoN16tSmVqQG62UAFnh4rL0l+szfM FvTUtG/jcn+QPdYXRUdOS/tVbco2Llk= 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-249-CWvtSVzZOEiXnzY8rUs16w-1; Mon, 02 Oct 2023 07:57:37 -0400 X-MC-Unique: CWvtSVzZOEiXnzY8rUs16w-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 E4A5F8007A4; Mon, 2 Oct 2023 11:57:35 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id A5CB6140E950; Mon, 2 Oct 2023 11:57:32 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Will Deacon , linux-kernel@vger.kernel.org, Borislav Petkov , Dave Hansen , x86@kernel.org, Ingo Molnar , "H. Peter Anvin" , Thomas Gleixner , Joerg Roedel , Suravee Suthikulpanit , Sean Christopherson , Maxim Levitsky , Robin Murphy , iommu@lists.linux.dev, Paolo Bonzini Subject: [PATCH v3 2/4] x86: KVM: AVIC: stop using 'is_running' bit in avic_vcpu_put() Date: Mon, 2 Oct 2023 14:57:21 +0300 Message-Id: <20231002115723.175344-3-mlevitsk@redhat.com> In-Reply-To: <20231002115723.175344-1-mlevitsk@redhat.com> References: <20231002115723.175344-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 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]); Mon, 02 Oct 2023 04:59:00 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778679462188419358 X-GMAIL-MSGID: 1778679462188419358 An optimization was added to avic_vcpu_load() in commit 782f64558de7 ("KVM: SVM: Skip AVIC and IRTE updates when loading blocking vCPU") to avoid re-enabling AVIC if the vCPU is about to block. Such situation arises when a vCPU thread is preempted in between the call to kvm_arch_vcpu_blocking() and before the matching call to kvm_arch_vcpu_unblocking() which in case of AVIC disables/enables the AVIC on this vCPU. The same optimization was done in avic_vcpu_put() however the code was based on physical id table's 'is_running' bit, building upon assumption that if avic_vcpu_load() didn't set it, then kvm doesn't need to disable avic (since it wasn't really enabled). However, once AVIC's IPI virtualization is made optional, this bit might be always false regardless if a vCPU is running or not. To fix this, instead of checking this bit, check the same 'kvm_vcpu_is_blocking()' condition. Also, as a bonus, re-enable the warning for already set 'is_running' bit, if it was found set, during avic_vcpu_put() execution and the vCPU was not blocking a condition which indicates a bug. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 2092db892d7d052..4c75ca15999fcd4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -1075,16 +1075,10 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) lockdep_assert_preemption_disabled(); /* - * Note, reading the Physical ID entry outside of ir_list_lock is safe - * as only the pCPU that has loaded (or is loading) the vCPU is allowed - * to modify the entry, and preemption is disabled. I.e. the vCPU - * can't be scheduled out and thus avic_vcpu_{put,load}() can't run - * recursively. + * If the vcpu is blocking, there is no need to do anything. + * See the comment in avic_vcpu_load(). */ - 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 (kvm_vcpu_is_blocking(vcpu)) return; /* @@ -1099,6 +1093,9 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) avic_update_iommu_vcpu_affinity(vcpu, -1, 0); + entry = READ_ONCE(*(svm->avic_physical_id_cache)); + WARN_ON_ONCE(!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)); + entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; WRITE_ONCE(*(svm->avic_physical_id_cache), entry);