From patchwork Thu Mar 30 12:35:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 77167 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1093696vqo; Thu, 30 Mar 2023 05:39:08 -0700 (PDT) X-Google-Smtp-Source: AKy350Yt9QJD9dkaKwR6ABsSlA8PmNdfEXZgxFBOc7u8NIYrcnWYAPQdo1IHiCHoSGHa9VVk3cQr X-Received: by 2002:a17:902:ea08:b0:1a1:9666:7a30 with SMTP id s8-20020a170902ea0800b001a196667a30mr25761350plg.54.1680179948440; Thu, 30 Mar 2023 05:39:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680179948; cv=none; d=google.com; s=arc-20160816; b=oA0+sg3JD8Ro7jSlRG1TJAssZgwtNg9VnybkJnWJLFU0GX1p4PUdp3q5eDteylp6WH HBLwBoyCgFDssqTky8pzAnIyhOBOa+c3jEOx+irJIyq5jXVR2Y6Uzu55fKnydG3Xm81p L3mgMpetbEQwUKkmjnD5CY6gKbxxkMFXxtnZdncrMZV6PseGASCLloKtwQBtajrY7kp8 n1TGQCeOsTg8VxpukSPubcPxYCdAhtMMJdpQwm2LaHM3SN4VL9cr3Ltjm7HmrOjKkqsG cKf+Hw1I5PmLlxyGNjeS3v5SWqP/7cr+MSWFhlhnhKmLEVfIaidXo+I0wSR2nwgqz9gg IoHA== 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 :message-id:date:subject:cc:to:from; bh=QMaNrk4nUZN8Td+Q0OBo+AQYIfG6i5UJw2JsG9rYKgw=; b=g9rHFQl9U5b+mBjes5y+MV/MMnzRrkZu9Oh5g/MOVexos/iLsAlp6wsoHk1CphP3Aw 29P5094ZZRMwjls3u5WJXJ9/sE+DIOhDM7E7dKGq70RvJ1ep1hngNHtU4ktPAi2Qd182 vcCVRDS8c2p9Y7XxE8xar+YGD7sLsQGY01e0G8pX2D3JnyXdeQSjXyumtRbJH2vOWgth IhBqqelERRgh6nJfjSpT2dcYJ9dUSX+t0kQox40L6EptdaFBQ0RJtRQx26xIHCakBGQq HD7mEtfCoGhruqPopDbPTNbxhVCtg/rPWowILVOGsqa28azsVOMOLTMxHJcMLRG0z3aT VSwA== 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 u11-20020a170903124b00b0019b090e497esi12430968plh.298.2023.03.30.05.38.55; Thu, 30 Mar 2023 05:39:08 -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 S230480AbjC3MgC (ORCPT + 99 others); Thu, 30 Mar 2023 08:36:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229661AbjC3MgB (ORCPT ); Thu, 30 Mar 2023 08:36:01 -0400 Received: from out0-209.mail.aliyun.com (out0-209.mail.aliyun.com [140.205.0.209]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F49A7681; Thu, 30 Mar 2023 05:35:59 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R961e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047203;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=12;SR=0;TI=SMTPD_---.S2zl1PU_1680179755; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.S2zl1PU_1680179755) by smtp.aliyun-inc.com; Thu, 30 Mar 2023 20:35:55 +0800 From: "Hou Wenlong" To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] KVM: x86: Disallow enable KVM_CAP_X86_DISABLE_EXITS capability after vCPUs have been created Date: Thu, 30 Mar 2023 20:35:52 +0800 Message-Id: <9227068821b275ac547eb2ede09ec65d2281fe07.1680179693.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 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?1761796369322758330?= X-GMAIL-MSGID: =?utf-8?q?1761796369322758330?= Disable PAUSE/MWAIT/HLT exits after vCPUs have been created is useless, because PAUSE/MWAIT/HLT intercepts configuration is not changed after vCPU created. And two vCPUs may have inconsistent configuration if disable PAUSE/MWAIT/HLT exits between those two vCPUs creation. Since it's a per-VM capability, all vCPUs should keep same configuration, so disallow enable KVM_CAP_X86_DISABLE_EXITS capability after vCPUs have been created. Signed-off-by: Hou Wenlong --- Documentation/virt/kvm/api.rst | 3 ++- arch/x86/kvm/x86.c | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) base-commit: 27d6845d258b67f4eb3debe062b7dacc67e0c393 diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index a69e91088d76..95a683a27cf2 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7179,7 +7179,8 @@ branch to guests' 0x200 interrupt vector. :Architectures: x86 :Parameters: args[0] defines which exits are disabled -:Returns: 0 on success, -EINVAL when args[0] contains invalid exits +:Returns: 0 on success, -EINVAL when args[0] contains invalid exits or + any vCPUs have been created. Valid bits in args[0] are:: diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2c0ff40e5345..7e97595465fc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6275,6 +6275,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) break; + mutex_lock(&kvm->lock); + if (kvm->created_vcpus) + goto disable_exits_unlock; if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) kvm->arch.pause_in_guest = true; @@ -6296,6 +6299,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, } r = 0; +disable_exits_unlock: + mutex_unlock(&kvm->lock); break; case KVM_CAP_MSR_PLATFORM_INFO: kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; From patchwork Thu Mar 30 12:35:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Wenlong X-Patchwork-Id: 77170 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1095665vqo; Thu, 30 Mar 2023 05:42:49 -0700 (PDT) X-Google-Smtp-Source: AKy350aS0w1gs2CoNJ4QLV3IiXkhYbB8bBdEWzSscdSkcZTqVZy9dDr5qRELDHJ58BXJSYMGNHEW X-Received: by 2002:a17:903:245:b0:1a1:f0cb:1055 with SMTP id j5-20020a170903024500b001a1f0cb1055mr26629622plh.28.1680180168912; Thu, 30 Mar 2023 05:42:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680180168; cv=none; d=google.com; s=arc-20160816; b=C2NBZmRDI8F9kuhbD/+wVPhvLe5A1gqgK5x99za7O890nNcIc/4NPvoVho8Ob7K6Dr PVx4D1C+tRhOkrLe4CQIhwE7cTb+NTWOir+pH0xttPihOJu6BNjP0dJscU14zx/8jLCU 56RYk5np6nJrQKVrsx3MQ+1thqeSD7OlkAf4E4mPuTKcC2p+oFaX3Sz36MaxqYU8ElVa INVGxyxZJ3zFnu5ToQUNQ8RGUBRsBC5x8Lg8p6q29PhxjeVcOZ3qlR2bL6lp/N2ddm0u AKI5TuCKh7dUN+Rq1JUzuDncCy8vCMO6tZVQBZzicrZDtJB9kITGVAGpWZ58YG0J3WLj hSVA== 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=zpwR9K2zZDVpSuys4DAijpsFUtQvXSVJvjA6omlf7Rk=; b=IQiXHMKAq75lQ4JiHz/ZJRg5RK8IDnfpVX0GbJaBC+4RyvQ5VK+N24faRi3aTyzkco LgYHFW7JDFZgyQ1Gl7adRMPhbasG/TxmdXQJYCYyoDPGaPbZqaT8IerhYiRkcSBMJ0gv JyJm/sZurxfLq+UiM2XedMIiz1mn9gx8AxRPhZsjUFVNMpwcGzJ1fpJFpfu3oxrV4/FL QIbevss77FvLaVZqE1Mr0e0oYtcUhwHNjkdHvpX3w/scOhZ0BUZ4RM+wbvAZwEJxElf9 9VDbRBF7gixetusonnmbVJEiV0tCmSY+wGlVSnRdsuetCy7viC/rRbqU1NpWhBnRm/cI H2JQ== 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 l10-20020a170903244a00b001a1dc236bdbsi25565693pls.539.2023.03.30.05.42.34; Thu, 30 Mar 2023 05:42:48 -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 S230482AbjC3MgE (ORCPT + 99 others); Thu, 30 Mar 2023 08:36:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230014AbjC3MgB (ORCPT ); Thu, 30 Mar 2023 08:36:01 -0400 Received: from out0-193.mail.aliyun.com (out0-193.mail.aliyun.com [140.205.0.193]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70C65768A; Thu, 30 Mar 2023 05:36:00 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047212;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---.S30ISig_1680179756; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.S30ISig_1680179756) by smtp.aliyun-inc.com; Thu, 30 Mar 2023 20:35:57 +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" , linux-kernel@vger.kernel.org Subject: [PATCH 2/3] KVM: x86: Don't update KVM PV feature CPUID during vCPU running Date: Thu, 30 Mar 2023 20:35:53 +0800 Message-Id: <9fbf5b4022d67157d6305bc1811f36d9096c26fc.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?1761796600996971951?= X-GMAIL-MSGID: =?utf-8?q?1761796600996971951?= __kvm_update_cpuid_runtime() may be called during vCPU running and KVM PV feature CPUID is updated too. But the cached KVM PV feature bitmap is not updated. Actually, KVM PV feature CPUID shouldn't be updated, otherwise, KVM PV feature would be broken in guest. Currently, only KVM_FEATURE_PV_UNHALT is updated, and it's impossible after disallow disable HLT exits. However, KVM PV feature CPUID should be updated only in KVM_SET_CPUID{,2} ioctl. Signed-off-by: Hou Wenlong --- arch/x86/kvm/cpuid.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 6972e0be60fa..af92d3422c79 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -222,6 +222,17 @@ static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcp vcpu->arch.cpuid_nent); } +static void kvm_update_pv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries, + int nent) +{ + struct kvm_cpuid_entry2 *best; + + best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent); + if (kvm_hlt_in_guest(vcpu->kvm) && best && + (best->eax & (1 << KVM_FEATURE_PV_UNHALT))) + best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); +} + void kvm_update_pv_runtime(struct kvm_vcpu *vcpu) { struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu); @@ -280,11 +291,6 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e cpuid_entry_has(best, X86_FEATURE_XSAVEC))) best->ebx = xstate_required_size(vcpu->arch.xcr0, true); - best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent); - if (kvm_hlt_in_guest(vcpu->kvm) && best && - (best->eax & (1 << KVM_FEATURE_PV_UNHALT))) - best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); - if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) { best = cpuid_entry2_find(entries, nent, 0x1, KVM_CPUID_INDEX_NOT_SIGNIFICANT); if (best) @@ -402,6 +408,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, int r; __kvm_update_cpuid_runtime(vcpu, e2, nent); + kvm_update_pv_cpuid(vcpu, e2, nent); /* * KVM does not correctly handle changing guest CPUID after KVM_RUN, as 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);