From patchwork Thu Mar 30 12:35:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 77168 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1093809vqo; Thu, 30 Mar 2023 05:39:19 -0700 (PDT) X-Google-Smtp-Source: AKy350YZiNXYc8ZPbuawuhYe9Z31oZ3PgePpJWRBVZ3pUkQbLwa+l8wFMw97IfHSFfo1Cjfgel/B X-Received: by 2002:a17:902:cad1:b0:1a0:50bd:31a8 with SMTP id y17-20020a170902cad100b001a050bd31a8mr20427787pld.26.1680179959762; Thu, 30 Mar 2023 05:39:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680179959; cv=none; d=google.com; s=arc-20160816; b=Hf9O5WsqOO90mLvorKHNd0HJd2wS+KRmdsdFH466fKgotQHD/XNIKfcf1vOjSME5Ob d1YkfW6icKeNM09iIeUQjQfwNesl52r2wRw/NIqDmeqkGwQHDm01Yp0DwPRVCix47mo+ Bnk72nLa/0mLjzu75b2LWqh7Dddu84A8k24K0D+LT5rZ/7JcpaaWZD/8kLb8o2QYjdoI c/cjINL4Qz0x2HpnuNi09Ut1NdCFat2wLqXSyxqT3B+mqerm1Cb3vgtQXnWhOv/oF5P4 ZmBJLsT27ukz3QsBzOgt2QcINWpAziIoeXX/1j0D3B/9eROpxAgZfHKXRTVfIp4+jByF Yzhg== 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; bh=eZOVP0mdcy4B6h5bcYIePRaHeeOZqEdFKEnclWiMD1c=; b=ZTeQv4juH6teM/SeysUGHqsj5as9ZyuCubaet/n/S4nnUZGn7KgbfGxx/3GoXxtQWk BzQdIAwl4VPPbSu/4oVTJgdL8zbp/Tv2OUx/CEPkVsm7gcwqlccRiJtPYhfDCFCs6LZI dDB6BnhcMlpygVk+O7EcgpNllAztx0lDMG92Yd7rEt0IWqP8KtyJSZjIoGEflhJcu4O4 6+mLtoCb8ME3a0cnH7afHPpmHBN2eFnBYG9uT1RMKqVWjlYcDHcsExpYmfl++ubkWee6 UL8JWFm2Vg0wPauJoYZNRG1BGED8/bK/Uy5zDKtwbK4B5DDuCrP0N5fMSjqnMvua4mg0 7QGg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 9-20020a170902c14900b00177568a0e53si33435132plj.252.2023.03.30.05.39.06; Thu, 30 Mar 2023 05:39:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231406AbjC3MgI (ORCPT + 99 others); Thu, 30 Mar 2023 08:36:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231263AbjC3MgF (ORCPT ); Thu, 30 Mar 2023 08:36:05 -0400 Received: from out0-196.mail.aliyun.com (out0-196.mail.aliyun.com [140.205.0.196]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3867D7EC0; Thu, 30 Mar 2023 05:36:04 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R221e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047199;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---.S3-w4rS_1680179757; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.S3-w4rS_1680179757) by smtp.aliyun-inc.com; Thu, 30 Mar 2023 20:35:58 +0800 From: "Hou Wenlong" To: kvm@vger.kernel.org Cc: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , linux-kernel@vger.kernel.org Subject: [PATCH 3/3] KVM: x86: Detect KVM_CPUID_FEATURES base before KVM PV feature CPUID updating Date: Thu, 30 Mar 2023 20:35:54 +0800 Message-Id: <712ea6c3db34d5b488888648c1eb2db655eaa5b9.1680179693.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <9227068821b275ac547eb2ede09ec65d2281fe07.1680179693.git.houwenlong.hwl@antgroup.com> References: <9227068821b275ac547eb2ede09ec65d2281fe07.1680179693.git.houwenlong.hwl@antgroup.com> MIME-Version: 1.0 X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761796381362782261?= X-GMAIL-MSGID: =?utf-8?q?1761796381362782261?= KVM_CPUID_FEATURES is detected after CPUID updating in kvm_set_cpuid(), then 'vcpu->arch.kvm_cpuid_base' is NULL and KVM PV feature CPUID updating wouldn't work. So detect it early before CPUID updating in kvm_set_cpuid(). Fixes: ee3a5f9e3d9b ("KVM: x86: Do runtime CPUID update before updating vcpu->arch.cpuid_entries") Signed-off-by: Hou Wenlong --- arch/x86/kvm/cpuid.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index af92d3422c79..0c8658fdb6c1 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -176,15 +176,15 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 return 0; } -static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu, - const char *sig) +static struct kvm_hypervisor_cpuid __kvm_get_hypervisor_cpuid(struct kvm_cpuid_entry2 *entries, + int nent, const char *sig) { struct kvm_hypervisor_cpuid cpuid = {}; struct kvm_cpuid_entry2 *entry; u32 base; for_each_possible_hypervisor_cpuid_base(base) { - entry = kvm_find_cpuid_entry(vcpu, base); + entry = cpuid_entry2_find(entries, nent, base, KVM_CPUID_INDEX_NOT_SIGNIFICANT); if (entry) { u32 signature[3]; @@ -202,6 +202,14 @@ static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcp } return cpuid; + +} + +static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu, + const char *sig) +{ + return __kvm_get_hypervisor_cpuid(vcpu->arch.cpuid_entries, + vcpu->arch.cpuid_nent, sig); } static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu, @@ -406,9 +414,12 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, int nent) { int r; + struct kvm_hypervisor_cpuid kvm_cpuid; __kvm_update_cpuid_runtime(vcpu, e2, nent); - kvm_update_pv_cpuid(vcpu, e2, nent); + kvm_cpuid = __kvm_get_hypervisor_cpuid(e2, nent, KVM_SIGNATURE); + if (kvm_cpuid.base) + kvm_update_pv_cpuid(vcpu, e2, nent); /* * KVM does not correctly handle changing guest CPUID after KVM_RUN, as @@ -444,7 +455,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, vcpu->arch.cpuid_entries = e2; vcpu->arch.cpuid_nent = nent; - vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); + vcpu->arch.kvm_cpuid = kvm_cpuid; vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE); kvm_vcpu_after_set_cpuid(vcpu);