From patchwork Mon Nov 8 12:43:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608357 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A2BEC433EF for ; Mon, 8 Nov 2021 12:44:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE7C5610F8 for ; Mon, 8 Nov 2021 12:44:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239615AbhKHMq6 (ORCPT ); Mon, 8 Nov 2021 07:46:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239608AbhKHMq5 (ORCPT ); Mon, 8 Nov 2021 07:46:57 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAED4C061714; Mon, 8 Nov 2021 04:44:13 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id gx15-20020a17090b124f00b001a695f3734aso8724538pjb.0; Mon, 08 Nov 2021 04:44:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Mxz8tU8qCanqhr03iDJonw9Th0/vdIsQXvtH3Lb3WMg=; b=UaEoyOKG8I8rKvakfVaqp1eZJev17NelYQcBytqBieIkAQ9viW38oHk6Gp3WrdVqSa miNJ/2ZaNRGzGtuBWzISIJ9qx7i462B1vDFJFSI1CJANze5PCgrCmgxVpATnEZZfQ9cA ZiM/Mlxb7H0LvuuBCku540zhBSYmfRebanMQog+hOpbyeP0zYGdLaIGl//3AJ5X+gDD6 7OjhT2fWZgU4krf535+0AQm9FTRDRnHFLY0by4Tp0tLknfAdp2CfQ72AEKZ9Oe9mFP1+ ck2ZxOxS3T+EPYacE5yaLT3rXjg5bFIaGweLUECbQla+a+nuLOKhvb2VJc1lgcuRHWic JDHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Mxz8tU8qCanqhr03iDJonw9Th0/vdIsQXvtH3Lb3WMg=; b=LGc/AqMGJP4D09I2xuHdll9Q8Lu1erEAXNGEDvSjIQR6QJ8MUO0bjdr+j+vVyAYjmJ MPwYXjUACUP6S5w9F/oRxGlo9eLktBAGudZ+29vzcmT5xplxt0dANg++1I48YotDRwLM GwGxD56+B6iOozJKIs2MQu8imoX2vy3UmMztuS3TMK5ws8nKKEqjYafRKD81JJxYNe9M zz/HqwIG3rG55KpqxJwDzpWbI86FALNQ/8Xik2KmkA0mrfO+ihtUcCvx/On8dlihlmPA ylmrHasrpLtbs+tLdVpDzDkzh6rNf1HfQQ5i9fanxnByt9XGDv8cfy0Y0+oJeHxvo/EL qtwA== X-Gm-Message-State: AOAM530cvNq42YXIysiVjszgwJL7dgYch51n0NGSXc4FE1mNH430DbGZ o9zF6P2X7RAEKyo1fcVKi41o9j2DqwM= X-Google-Smtp-Source: ABdhPJzvnGKIejSHtGzdnoXxS/QQJchyfKdKgafUeAPUHAKE+xzHrlStonjr4WLE+5Gqp+KRi7jEzg== X-Received: by 2002:a17:90a:6b0a:: with SMTP id v10mr51372402pjj.130.1636375452979; Mon, 08 Nov 2021 04:44:12 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id e7sm12425773pgk.90.2021.11.08.04.44.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:12 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Xiao Guangrong Subject: [PATCH 01/15] KVM: X86: Ensure the dirty PDPTEs to be loaded Date: Mon, 8 Nov 2021 20:43:53 +0800 Message-Id: <20211108124407.12187-2-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan For VMX, the dirty PDPTEs needs to be loaded before the coming VMENTER via vmx_load_mmu_pgd() if EPT is enabled. But not all paths that call load_pdptrs() will cause vmx_load_mmu_pgd() to be invoked. Normally, kvm_mmu_reset_context() and KVM_REQ_LOAD_MMU_PGD are used to launch later vmx_load_mmu_pgd(). The commit d81135a57aa6("KVM: x86: do not reset mmu if CR0.CD and CR0.NW are changed") skips kvm_mmu_reset_context() after load_pdptrs() when changing CR0.CD and CR0.NW. The commit 21823fbda552("KVM: x86: Invalidate all PGDs for the current PCID on MOV CR3 w/ flush") skips KVM_REQ_LOAD_MMU_PGD after load_pdptrs() when rewriting the CR3 with the same value. The commit a91a7c709600("KVM: X86: Don't reset mmu context when toggling X86_CR4_PGE") skips kvm_mmu_reset_context() after load_pdptrs() when changing CR4.PGE. Fixes: d81135a57aa6("KVM: x86: do not reset mmu if CR0.CD and CR0.NW are changed") Fixes: 21823fbda552("KVM: x86: Invalidate all PGDs for the current PCID on MOV CR3 w/ flush") Fixes: a91a7c709600("KVM: X86: Don't reset mmu context when toggling X86_CR4_PGE") Signed-off-by: Lai Jiangshan --- arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ac83d873d65b..034c233ea5a1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -830,6 +830,8 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); + /* Ensure the dirty PDPTEs to be loaded. */ + kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu); vcpu->arch.pdptrs_from_userspace = false; return 1; From patchwork Mon Nov 8 12:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608359 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 885C2C433F5 for ; Mon, 8 Nov 2021 12:44:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 72D7261355 for ; Mon, 8 Nov 2021 12:44:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239622AbhKHMrF (ORCPT ); Mon, 8 Nov 2021 07:47:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239606AbhKHMrE (ORCPT ); Mon, 8 Nov 2021 07:47:04 -0500 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24936C061570; Mon, 8 Nov 2021 04:44:20 -0800 (PST) Received: by mail-pg1-x52d.google.com with SMTP id g184so15050983pgc.6; Mon, 08 Nov 2021 04:44:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AWnQnZTcd6KUjYhg83N5anaGWnmVBccSmNsieVINo74=; b=l+rRI9cFGm9PUxjnxTpddwYEhrSqXixq6evuxem9IcribVyDgmEESyHEppavveYwHG qV/EJSUuC9j+Jc97LSCdT0b8goqUjUwbYoRO1GaIQ4JOELmyadomu8jKqnk48kXCRqzU IIxpN9AGHAsmIZmnYlY9SaBVx1KiW/jAAw40ncRX2GMOwfWFZQ6YP5EDKzXaZpSJQJrc 7X17TPwegOpFAooos1VuxO/hqHzNxdNPmLP0HKjnUfkxSvdDU//D4l+yq1TLnAaK3V1Z ZnZQcMXaYcQxuUlxTb/V0WlMeooyAMfHXPoMVfgFcBaY3eyyAx6lN61p+7Dd6ppqBwSo Z7wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AWnQnZTcd6KUjYhg83N5anaGWnmVBccSmNsieVINo74=; b=BmImN0/4b8+BTfeem8pf9LvyF8w8k4Ikr+oW9UuvCTj9ZEOYE3ixUfIlIbZ9lVkzon 0fkv1rdPhKiq8b3Sct9qr1RHTnSidvMz80lBnrlTKBRTKq+s6JUq2pMfChnHcgBgBGo2 N68R+/Su0CqEod2jW9HiYeu2h4+28Bm/kMqpY1UNqM6A6N7GDoUF8bxMN7PWaQpjmXiA HqMaFQ6G1pKeh1psXlCTpnLrXnLy/xjtsc5pGbBKCuGSG9z2MiWHGyATTCwukF3XKs68 GBs1NIfMbijTs9HNo4CeVzfw0I8IXVngs1bh3TC/3IicE/3Ym1C7vixKDtFvHvXR7cQD j2xA== X-Gm-Message-State: AOAM530TIKB16/dNfoq01sERHQKLpuqENAYKHS0Pb0gSfxKn0yiHihJs VAGdGhdx15CarB5sb4Fc2c+E+6lEGvY= X-Google-Smtp-Source: ABdhPJy486BVET8NcxrEGHISk5duKnZbe0kDNeK/ZvleavDOCHXCrIxBIxuL/N9EVTjikteRIOgPsA== X-Received: by 2002:a05:6a00:140c:b0:44b:e251:fe13 with SMTP id l12-20020a056a00140c00b0044be251fe13mr81521000pfu.10.1636375459387; Mon, 08 Nov 2021 04:44:19 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id hk18sm11764497pjb.20.2021.11.08.04.44.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:19 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 02/15] KVM: VMX: Mark VCPU_EXREG_PDPTR available in ept_save_pdptrs() Date: Mon, 8 Nov 2021 20:43:54 +0800 Message-Id: <20211108124407.12187-3-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan mmu->pdptrs[] and vmcs.GUEST_PDPTR[0-3] are synced, so mmu->pdptrs is available and GUEST_PDPTR[0-3] is not dirty. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/vmx/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 39db4f56bffd..79e5df5fbb32 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3001,7 +3001,7 @@ void ept_save_pdptrs(struct kvm_vcpu *vcpu) mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); - kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); + kvm_register_mark_available(vcpu, VCPU_EXREG_PDPTR); } #define CR3_EXITING_BITS (CPU_BASED_CR3_LOAD_EXITING | \ From patchwork Mon Nov 8 12:43:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608361 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D729C433EF for ; Mon, 8 Nov 2021 12:44:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E05561288 for ; Mon, 8 Nov 2021 12:44:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239639AbhKHMrN (ORCPT ); Mon, 8 Nov 2021 07:47:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239601AbhKHMrK (ORCPT ); Mon, 8 Nov 2021 07:47:10 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2F72C061714; Mon, 8 Nov 2021 04:44:25 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id gb13-20020a17090b060d00b001a674e2c4a8so8723256pjb.4; Mon, 08 Nov 2021 04:44:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XGkAh2kPauX2NV9OTG6gnpHhdpc8cmhMr1z1fLSTluI=; b=C64QvIVVeIVHJ1BqQ5tmKNgFBhfQ6o1SaYKTErmZEfms5zQZVbcRv7dfy3puE9UdUf 2/OS5Ke9+IfunQb9omi9HxnkL6GVKcBXqmtpnD9X3M5iqNt3NVkV5v/c6vAmxkSuDVM9 QaItnHkFTnLlVDAqH6A5STHFf1azokcEdBH7DTVnzyL7ifjk+Riz1bQQKtDnom11MS9T n8NarqpKIe9r2Cr5DVyODDJIDHAb7WlRAi/9sZfEhmE0eurmsEdvIj1Vhtc5f0OaqjgI W1cbIwOBhg/Y8STgbPL3ZxZA12GxM4Cf58lTFei7cidIqzB9AP1bk+yj9RFtzjBRtXu2 iwSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XGkAh2kPauX2NV9OTG6gnpHhdpc8cmhMr1z1fLSTluI=; b=uVEeSZQP+essxY40A3Vab8mG5mojHB+7BjpADbDVEQoHVkZbWBhSuxbpYWkqMGQd39 Zk2AntEVaYWZGmEGvHcsBAyinV4nYQ8iYtfe8xiYgJfElAsE2JqyIJ06O8nv+RnBr3o/ Unz6bGsIU6sIo1JMCcC3kd2Np7zIOX8xQRhSXszGhSNlJ5fAG79+qQOO7Y6aMqxwFypA gZT/Ra+aE3xXbUg5huRjK4zbQSxhSyTIGMBrD5GSSmDPPINIgh93RI02Pd2lPTusbZMx Yh7z2+R/lMWG2q3ZiHcgLcQfSWnu5bKTA2Y+pYVka5hI91U2XrHqAZ03O1J5z7vD9ebY xTLw== X-Gm-Message-State: AOAM532YuuN3k09kHQg4Hl4lNX2bWEsivwtIOxRwizVCCwGJZ/7oRoEn AU8dDCmhzYCLibjuQO1ArcDeOIrev4E= X-Google-Smtp-Source: ABdhPJwCt5dIDMSBbFz2LdiyHbg9sJVzpKI+qrobDbHWbyRg77cDNe4+BeWJGiMDs24z9wnh8QtqLQ== X-Received: by 2002:a17:902:b08a:b0:142:51be:57e2 with SMTP id p10-20020a170902b08a00b0014251be57e2mr20076724plr.53.1636375465226; Mon, 08 Nov 2021 04:44:25 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id p14sm13201801pjb.9.2021.11.08.04.44.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:24 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 03/15] KVM: SVM: Always clear available of VCPU_EXREG_PDPTR in svm_vcpu_run() Date: Mon, 8 Nov 2021 20:43:55 +0800 Message-Id: <20211108124407.12187-4-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan Make it the same logic to handle the availability of VCPU_EXREG_PDPTR as VMX and also remove a branch in svm_vcpu_run(). Signed-off-by: Lai Jiangshan --- arch/x86/kvm/svm/svm.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 88a730ad47a1..3e7043173668 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1583,10 +1583,16 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) { + kvm_register_mark_available(vcpu, reg); + switch (reg) { case VCPU_EXREG_PDPTR: - BUG_ON(!npt_enabled); - load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); + /* + * When !npt_enabled, mmu->pdptrs[] is already available since + * it is always updated per SDM when moving to CRs. + */ + if (npt_enabled) + load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); break; default: KVM_BUG_ON(1, vcpu->kvm); @@ -3964,8 +3970,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; vmcb_mark_all_clean(svm->vmcb); - if (npt_enabled) - kvm_register_clear_available(vcpu, VCPU_EXREG_PDPTR); + kvm_register_clear_available(vcpu, VCPU_EXREG_PDPTR); /* * We need to handle MC intercepts here before the vcpu has a chance to From patchwork Mon Nov 8 12:43:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608363 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CFF5C433F5 for ; Mon, 8 Nov 2021 12:44:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7517B610F8 for ; Mon, 8 Nov 2021 12:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239631AbhKHMrR (ORCPT ); Mon, 8 Nov 2021 07:47:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239636AbhKHMrQ (ORCPT ); Mon, 8 Nov 2021 07:47:16 -0500 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAB7DC061714; Mon, 8 Nov 2021 04:44:31 -0800 (PST) Received: by mail-pg1-x531.google.com with SMTP id p8so13711429pgh.11; Mon, 08 Nov 2021 04:44:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tufklCvQLXjLmL93FLlua3ov52AUTNVIPedxDPFEtnU=; b=oGARxUBotZQ2W3nb/efsf/1by+ygeBAnVm0MouFdlFYtFOXPHEzMguUIyoeDa43zFH KA4nwD8IhhVbXn7wV5SyXs5aMthCf485iLegmcp15LMK4N+FnBlbt4e2r+gHkoPJ0pdI bJh9pINGfWzV4hXsLTKwACySLa9UBoTwuPkue1wRrKBcca9KBpgWNWhk2LSRYBQM0RYl S7E0z4qOjfM+LMNB9lNTYMCav8hMviPFQCUhZ5HqtE9NveeKw9BxcR6zJ6AepcfaLO3J cyCBvAIq1aEHzcMfraOc7J6/nui8i5h/qH+OzRHCqxhUpCZRTSfmaPy6PdiIniRZAti2 ouFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tufklCvQLXjLmL93FLlua3ov52AUTNVIPedxDPFEtnU=; b=S/tMunwISV1K6e+meViWz9gobdSwoD8ICHpPcK8dGg1ZhI2kLoEofzBuuOpsgfSv9N 6yLivJtdE9Evg9ohHfZGeTynBOiYS0GORANJqsNbXQQ54TSaNd68mRKpabHbMtX4OxKH yaLK50Ns5cxyt6AHKKYk87SAZGsZbhcwJ6yqZxdHAuFv3KX0CLGJXKZX5LX79R5L1+yU vlAyJG82H9fWKfGW+iC7d2NJWQ/kgo259bFj+ot5Q0L8m3v6xMFzotx3hc8as56Rh3CZ 0gZm8GKTyJQkEQVTQ23VRi4AJKH6dQmyv4MQ7AqiMsSHjWZZ7Cs79A4YNqA38OrDVGSY 6cwQ== X-Gm-Message-State: AOAM533nrdsCIgtb8FRDO4Au8Ev+XZbZTWT/kH0v4VhlRqfi8fQ5Qz6h ZzgjNAV7/t6q2gWdRBgr7mJn8fa2dj0= X-Google-Smtp-Source: ABdhPJxVQMSkAAyqB29vEoilI7bdxZpenTzrsPHDEuLs3THJ+ZglltxW/jDrq9+Qko0r1KZMU3PfnA== X-Received: by 2002:a63:854a:: with SMTP id u71mr46731407pgd.174.1636375471310; Mon, 08 Nov 2021 04:44:31 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id u11sm2587076pfk.152.2021.11.08.04.44.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:30 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 04/15] KVM: VMX: Add and use X86_CR4_TLB_BITS when !enable_ept Date: Mon, 8 Nov 2021 20:43:56 +0800 Message-Id: <20211108124407.12187-5-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan In set_cr4_guest_host_mask(), X86_CR4_PGE is set to be intercepted when !enable_ept just because X86_CR4_PGE is the only bit that is responsible for flushing TLB but listed in KVM_POSSIBLE_CR4_GUEST_BITS. It is clearer and self-documented to use X86_CR4_TLB_BITS instead. No functionality changed. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/kvm_cache_regs.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 90e1ffdc05b7..8fe036efa654 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -9,6 +9,8 @@ (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | X86_CR4_OSXMMEXCPT | X86_CR4_PGE | X86_CR4_TSD | X86_CR4_FSGSBASE) +#define X86_CR4_TLB_BITS (X86_CR4_PGE | X86_CR4_PCIDE | X86_CR4_PAE | X86_CR4_SMEP) + #define BUILD_KVM_GPR_ACCESSORS(lname, uname) \ static __always_inline unsigned long kvm_##lname##_read(struct kvm_vcpu *vcpu)\ { \ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 79e5df5fbb32..1795702dc6de 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4107,7 +4107,7 @@ void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS & ~vcpu->arch.cr4_guest_rsvd_bits; if (!enable_ept) - vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PGE; + vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_TLB_BITS; if (is_guest_mode(&vmx->vcpu)) vcpu->arch.cr4_guest_owned_bits &= ~get_vmcs12(vcpu)->cr4_guest_host_mask; From patchwork Mon Nov 8 12:43:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608365 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E84EC433EF for ; Mon, 8 Nov 2021 12:44:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F68F610F8 for ; Mon, 8 Nov 2021 12:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239666AbhKHMrb (ORCPT ); Mon, 8 Nov 2021 07:47:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239644AbhKHMrW (ORCPT ); Mon, 8 Nov 2021 07:47:22 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A92E3C061714; Mon, 8 Nov 2021 04:44:38 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id n8so15719385plf.4; Mon, 08 Nov 2021 04:44:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zRUfb/3+n4txROifqZnPMWD7jD3Jwrb0pGPJoTs4U1E=; b=R/LjmUf7ZtvWkjtPcKo0z8b1EfUP1fC0HAlDh45bUwoIaZTKbUwROxcrhOPrFroyxF CRO797xKq2PavRMmkMIqbc99SHvO/TiaLfqF9hm0X6yNEFTEA5hYSDux/bNKdYI45K2+ YTk5pTzD/rLxBC8+QlhDPC5QB4W1eTfo2MYqKts74T9HJ7SUoN2hnmSE0fgYkvKqigu5 8KqcsCLg9tVqfUl/gdaDA4IQEF7b7QOu5/+W4khUbzOfKa2/lWc63r3+NlJPCfLiz8/z ew+C1cPHoQJDlvsyfGvW0ByEHWvKLS9nFqOEfpc64Ngo6/gPXaUU6tBNIRnBLIyCqtda jL8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zRUfb/3+n4txROifqZnPMWD7jD3Jwrb0pGPJoTs4U1E=; b=yI2cRO8ohqrNC4kn/2lKtNf/t5IRpamGX32KQJBciDFkL09Mmgz9rZN7q3ZfpWGqi0 lmNWlNBYEd6hf7Ot4SnLsNOs2+95obO3Fp305jUzfnoHesyC3a07TNzfvlK4JdOlWVKz PIYywbExIozxbW26T2dmnE/pp5nJmJnHs0Xt31gZ/8tUj187KKUbv/iYmyiT4Wl+yGPu wP7338KG06y/uUVaXhdGOCJLY7pSlzJ6MK9gYvf1fiO3kzYgzakW4WyVgPKvBndV40c2 +rddqoCgUHVuHjfcXop29cUOn5X9m9fvlHGekIz6yP7DjpeowmiOuZlNSHcmMV00gp7c J/nw== X-Gm-Message-State: AOAM531ziRiQRw5tZ++Td9sBLWAjDcc88ZhP3OWzKHAfk+7i+baHaG2/ kbHH4GNu6Urz5hg8WKRONCXcrb56RiA= X-Google-Smtp-Source: ABdhPJwdRk4VpIam22mSI0kPqvhTvJ3rEsCCtAsbqOrakl1pRJEHDeA4ulKNu6yJzZ0t9P58vRs9OA== X-Received: by 2002:a17:902:b209:b0:141:a755:79de with SMTP id t9-20020a170902b20900b00141a75579demr64101844plr.7.1636375478008; Mon, 08 Nov 2021 04:44:38 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id i2sm7742696pfe.70.2021.11.08.04.44.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:37 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 05/15] KVM: VMX: Add and use X86_CR4_PDPTR_BITS when !enable_ept Date: Mon, 8 Nov 2021 20:43:57 +0800 Message-Id: <20211108124407.12187-6-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan In set_cr4_guest_host_mask(), all cr4 pdptr bits are already set to be intercepted in an unclear way. Add X86_CR4_PDPTR_BITS to make it clear and self-documented. No functionality changed. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/kvm_cache_regs.h | 1 + arch/x86/kvm/vmx/vmx.c | 4 +++- arch/x86/kvm/x86.c | 4 +--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 8fe036efa654..592f9eb9753b 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -10,6 +10,7 @@ | X86_CR4_OSXMMEXCPT | X86_CR4_PGE | X86_CR4_TSD | X86_CR4_FSGSBASE) #define X86_CR4_TLB_BITS (X86_CR4_PGE | X86_CR4_PCIDE | X86_CR4_PAE | X86_CR4_SMEP) +#define X86_CR4_PDPTR_BITS (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_SMEP) #define BUILD_KVM_GPR_ACCESSORS(lname, uname) \ static __always_inline unsigned long kvm_##lname##_read(struct kvm_vcpu *vcpu)\ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 1795702dc6de..d94e51e9c08f 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4106,8 +4106,10 @@ void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS & ~vcpu->arch.cr4_guest_rsvd_bits; - if (!enable_ept) + if (!enable_ept) { vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_TLB_BITS; + vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PDPTR_BITS; + } if (is_guest_mode(&vmx->vcpu)) vcpu->arch.cr4_guest_owned_bits &= ~get_vmcs12(vcpu)->cr4_guest_host_mask; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 034c233ea5a1..b92d4241b4d9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1052,8 +1052,6 @@ EXPORT_SYMBOL_GPL(kvm_post_set_cr4); int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) { unsigned long old_cr4 = kvm_read_cr4(vcpu); - unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | - X86_CR4_SMEP; if (!kvm_is_valid_cr4(vcpu, cr4)) return 1; @@ -1064,7 +1062,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) if ((cr4 ^ old_cr4) & X86_CR4_LA57) return 1; } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) - && ((cr4 ^ old_cr4) & pdptr_bits) + && ((cr4 ^ old_cr4) & X86_CR4_PDPTR_BITS) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu))) return 1; From patchwork Mon Nov 8 12:43:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608367 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC267C433EF for ; Mon, 8 Nov 2021 12:44:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C3E4D610F8 for ; Mon, 8 Nov 2021 12:44:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239657AbhKHMre (ORCPT ); Mon, 8 Nov 2021 07:47:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239647AbhKHMr2 (ORCPT ); Mon, 8 Nov 2021 07:47:28 -0500 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53FA4C061570; Mon, 8 Nov 2021 04:44:44 -0800 (PST) Received: by mail-pg1-x529.google.com with SMTP id j9so15057599pgh.1; Mon, 08 Nov 2021 04:44:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o2tXLnqLipCcf0m6Gd15LojCTNxV/qmLP6IND4hBwMk=; b=n89igtCe8rKdP4DlzWpC/GwRUOo4rOAudhUb1xRyBnCHqeZlNZnpPffyuvfIuwiNbE Q+LqEEd3IKK749c3/7Gs4RpTtujWXIs4Q8G9tamHGuRHhJ9h9L6FBjTAXnY70P2i7cv7 ZK6KEVErdku8GfnNJzaKC6wR7bJpiWrdEwrmDD0TMWMhzDZ7aNBA5cwrAzEsyFv/w940 +TWlbvygQQDD7W0jtKQXzf7pzXtp1kYJv7u+lNFdP74Wc3wLaZuLISTiUiNL/oVh+fdH kyRhH2sc9SaD8UsNEQEaz6TJ4eGc0fudF+ZPVwJvTDU74QaqhPXYHb3zHPm4QjCcy9pK 3IoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o2tXLnqLipCcf0m6Gd15LojCTNxV/qmLP6IND4hBwMk=; b=eKvLGlb4EccwftIMT+ZNo+yJmbQmAWZNmF6M1D3fFs32Xpto+/7W3ysd1ck8sZefd5 Dk6S5/+j7GROp3xVlzDjMtIug1oIN9UKz+J0BV2kJ+JUgvaCynQNrkWcTrCd0WdV0Pwo mT7//ZCbLfo6wK/kYAjPQDwE42X1lOkoPeec24XkwjALWrpfOnftTtlTwPJiUULOPqFv GG3fc/3j1qJzu3hMVNgicqhWv7x9hrdRzx6NNy8cm33FhLKHwvNHB67aLI2NAaf7VXu3 cqNZaBN5TYQ2J8gePUHoIIkYvzFe710ejnGB7DSnwuy2wPRkWDKxKcrSzu1wnQkq2LEc THAA== X-Gm-Message-State: AOAM533Nf8o2qqi1Ymjm5vhglFEVACW47dXgaal6KYjcfOUSPQZlQ1m7 DPsTSvbyk11XmQ4tiW3lVJVHaPGnOOo= X-Google-Smtp-Source: ABdhPJy7Z/wzb4pgRW9UhzhdAeUyDr7OnVliaRjbQhQ0oYHUodD+Du7YWHR6EK+4GE2NbX6mKOB+sw== X-Received: by 2002:a63:cf48:: with SMTP id b8mr39346848pgj.434.1636375483771; Mon, 08 Nov 2021 04:44:43 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id f20sm12461464pfj.219.2021.11.08.04.44.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:43 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 06/15] KVM: X86: Move CR0 pdptr_bits into header file as X86_CR0_PDPTR_BITS Date: Mon, 8 Nov 2021 20:43:58 +0800 Message-Id: <20211108124407.12187-7-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan Not functionality changed. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/kvm_cache_regs.h | 3 +++ arch/x86/kvm/x86.c | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 592f9eb9753b..54a996adb18d 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -9,9 +9,12 @@ (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | X86_CR4_OSXMMEXCPT | X86_CR4_PGE | X86_CR4_TSD | X86_CR4_FSGSBASE) +#define X86_CR0_PDPTR_BITS (X86_CR0_CD | X86_CR0_NW | X86_CR0_PG) #define X86_CR4_TLB_BITS (X86_CR4_PGE | X86_CR4_PCIDE | X86_CR4_PAE | X86_CR4_SMEP) #define X86_CR4_PDPTR_BITS (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_SMEP) +static_assert(!(KVM_POSSIBLE_CR0_GUEST_BITS & X86_CR0_PDPTR_BITS)); + #define BUILD_KVM_GPR_ACCESSORS(lname, uname) \ static __always_inline unsigned long kvm_##lname##_read(struct kvm_vcpu *vcpu)\ { \ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b92d4241b4d9..e5f5042d4842 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -858,7 +858,6 @@ EXPORT_SYMBOL_GPL(kvm_post_set_cr0); int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) { unsigned long old_cr0 = kvm_read_cr0(vcpu); - unsigned long pdptr_bits = X86_CR0_CD | X86_CR0_NW | X86_CR0_PG; cr0 |= X86_CR0_ET; @@ -888,7 +887,7 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) } #endif if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && - is_pae(vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) && + is_pae(vcpu) && ((cr0 ^ old_cr0) & X86_CR0_PDPTR_BITS) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu))) return 1; From patchwork Mon Nov 8 12:43:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608369 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3ED17C433EF for ; Mon, 8 Nov 2021 12:44:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 295D261288 for ; Mon, 8 Nov 2021 12:44:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239659AbhKHMrh (ORCPT ); Mon, 8 Nov 2021 07:47:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239649AbhKHMre (ORCPT ); Mon, 8 Nov 2021 07:47:34 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1E34C061570; Mon, 8 Nov 2021 04:44:49 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id y1so15683738plk.10; Mon, 08 Nov 2021 04:44:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rzbFgj/HRvczCJnHB2uTBb/82oB/oP3QBt5uIwR1NNQ=; b=m/yz/THQIg/FNZPGwFAFly/HWsCKic+H8ebbtHg/icizaBRs02d4AhjUBEfAYzC3mU gzb8sSAmR2jAEnBsK4gKsujrhLz18U3NBtg66tymiNcmrinqsnfQ3CyYGjpWecln9P37 D1xeB7M8oNnzzBWFV2f1VZ62cl3vYXFZFN2AcybGCs9o70fmDy5p75KConQU72HyB40L 8IpbDLtazTYZn6HcZit/ykQDuaHMaGgg+6MAEhKY8jkliB7YR5hCCx+T6T0cHNc2nFvs Flfr7fH62d9oXA3xAT1+dNT3J+m8JCe+eL5cQqTOqTjHu/+7mFTUIkkZTimUzdxYrvyH zI7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rzbFgj/HRvczCJnHB2uTBb/82oB/oP3QBt5uIwR1NNQ=; b=sjf7CGyM0vDBmf5nLDrgS399dW1TF3Gsr/Vk7qw5pt7wOq7inDvXCIgiI1LUQNKqWp nZntkG2u2b6eMFGYrHq2u5KC7cUxg7woX9WLT44STVa/vYmyVbeZr6O006gW20fMhm7+ CHFUcSdgvc5HCxt1fkNCfIhnPPk4XgJw/+CvVowmIWoi6QiRKDM3XJZMYK4UGlXEZCA5 vIbIur6J3gwvG4nSNxi9M9zzEFJ499vKd8U7zG48ygoneaV46zGMdvu6d7hMl0EGmDlL JSLpABbfxcn4qz5FJB9kYWVDq4uPVIU/eLwS+3gjZpLmkuv2HEgOPKaq287Ey8IUfWsb H1dg== X-Gm-Message-State: AOAM533BU9cM20woVQ+zleMunZKny9Vh4z2rQiAsQVpNSPPmecczBh/1 l3qp/E2V7QfBAa77LfVy+W3ntYofFWQ= X-Google-Smtp-Source: ABdhPJzM3mjB6Xu7NtfZSVEtMjm8UBU8Thzfp/r1ZWZavHWyTAGXSTs8f9g4gzKr0wFKLIGJrx4YDA== X-Received: by 2002:a17:90a:5303:: with SMTP id x3mr51365345pjh.226.1636375489385; Mon, 08 Nov 2021 04:44:49 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id y184sm9367160pfg.175.2021.11.08.04.44.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:49 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 07/15] KVM: SVM: Remove outdated comment in svm_load_mmu_pgd() Date: Mon, 8 Nov 2021 20:43:59 +0800 Message-Id: <20211108124407.12187-8-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan The comment had been added in the commit 689f3bf21628 ("KVM: x86: unify callbacks to load paging root") and its related code was removed later, and it has nothing to do with the next line of code. So the comment should be removed too. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/svm/svm.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 3e7043173668..e3607fa025d3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4000,7 +4000,6 @@ static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, hv_track_root_tdp(vcpu, root_hpa); - /* Loading L2's CR3 is handled by enter_svm_guest_mode. */ if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) return; cr3 = vcpu->arch.cr3; From patchwork Mon Nov 8 12:44:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608371 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0FE2C433EF for ; Mon, 8 Nov 2021 12:45:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E3D061352 for ; Mon, 8 Nov 2021 12:45:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239720AbhKHMrp (ORCPT ); Mon, 8 Nov 2021 07:47:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239697AbhKHMrk (ORCPT ); Mon, 8 Nov 2021 07:47:40 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF720C06120C; Mon, 8 Nov 2021 04:44:55 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id p18so15648208plf.13; Mon, 08 Nov 2021 04:44:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lkQZLAs32xKdFX6mNMiLM8Wp8LK6aRx8X001XWtnJyg=; b=eStkVXn/kJAz1DHFPIMEuGV6xz1pzvO3JWZssAK3N0Qh/VIY19Nlq5NyDcBEqIHSqF O8Q4lD72bhObDYaGsUkEyOHAX0RO0VVG0v9u+VsoccwwLOUsGm0TN1UdRNrnJ+l/8gfn s72JU4qdJrYp9pfVUrlNM/0YRlyZj/k/MnwJ8Tb7c03BroGmt6K0Gzk3uu9A+hDT+UU6 CYeuFztetegREKpskig8nRfFLQW4T8TklrK2cqJajFUtNqKC+LIeR/sacq84HY8JBUVO fPYyggf2LheT7X4R7ASg/pbN89EaTDRFKDF+O29QQNK28+gsf98wtIK3pbzFFK//IW68 s5tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lkQZLAs32xKdFX6mNMiLM8Wp8LK6aRx8X001XWtnJyg=; b=yDGnMmLNp2wQkd/QBGXLWfbzEeDvTVjLNDkyoqjGqRUWQTMEWKjPn67+81apRZelck Y4xro3z9zA8oCVq/QpOkRvR2ZF8UcHxohLvDp6PUHE7cSAo/0g/eFAZ1PX25ex0ia+hT noCyv+ixTPAwoCjrQNAa+b1tIpDoVZadGT4d3ImAivALl7Q2UqAwI5M5FFfC+eaD9gb5 thXlzE0IHG1Aza8Cr1L6InuOPlxlR1muBxJ41tkqr3YX3xhcA0dZeUYbBvkPQYesiFEr 2JvkIQeOwR4miNwC2GK6ajVB6DVSs4IG9PUMxnu+W9Qhy5YKq4XKAQHvZ8YpyEFr3N68 ZODA== X-Gm-Message-State: AOAM530iCAsh6bgxi0vqKKWOCMQRcfi0M/REe08sA+1DUOWWflYxIsiX KLOGKRF+HyjRkuol4NERwgd21x1moAk= X-Google-Smtp-Source: ABdhPJxBRa3SKK37ZOZ3ZoioODMghksE5bZSuADmFvGtc0nk/cTT5rh8TDDqEZalvbAjtUOoNxmnpw== X-Received: by 2002:a17:90a:49:: with SMTP id 9mr51975839pjb.80.1636375495279; Mon, 08 Nov 2021 04:44:55 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id y4sm16701285pfi.178.2021.11.08.04.44.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:44:54 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 08/15] KVM: SVM: Remove useless check in svm_load_mmu_pgd() Date: Mon, 8 Nov 2021 20:44:00 +0800 Message-Id: <20211108124407.12187-9-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan VCPU_EXREG_CR3 is never cleared from vcpu->arch.regs_avail in SVM so the if-branch is always false and useless, just remove it. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/svm/svm.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e3607fa025d3..b7da66935e72 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4000,8 +4000,6 @@ static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, hv_track_root_tdp(vcpu, root_hpa); - if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) - return; cr3 = vcpu->arch.cr3; } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) { cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu); From patchwork Mon Nov 8 12:44:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608373 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 307F6C433F5 for ; Mon, 8 Nov 2021 12:45:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B1C4610F8 for ; Mon, 8 Nov 2021 12:45:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239706AbhKHMry (ORCPT ); Mon, 8 Nov 2021 07:47:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239723AbhKHMrq (ORCPT ); Mon, 8 Nov 2021 07:47:46 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46C7AC06120B; Mon, 8 Nov 2021 04:45:01 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id u11so15730210plf.3; Mon, 08 Nov 2021 04:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=su/igV5uUfDsLAamJ/jUapW9IEC/znYL2Yl/thFDQYY=; b=AwpkEwlCHjvUNJyxjkHCj5gYTo8dZFeOMM4QuKatYtFU+KctX0PYuwKvCyoDdehB1u hvkeTCZPkMp5kpArc8EYeOczf4D3vYWVbwdMsln9Ira4tTQAHXA+inHRKTyQsnOKegyu U8pUcFkobAS/jS50k5jO73jAn4Zp4xy3+4r0a5dcVGawT4YKSksi4o39hQRlxdKrf6QG iVffIR/u0pWg2GW0VOP4J3BJ0Q4LtVy/kcY9cC2oPrXza/8kkb7u4uR4CKrYuv1lgkzf U7E1l+Ya03YIsYQxVZtpESzBjEaIgkdfaH+cS9B7U6eW7i2nNds7W8rAmxq0V/9Jdyva qHlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=su/igV5uUfDsLAamJ/jUapW9IEC/znYL2Yl/thFDQYY=; b=qdthaZ0B8ONOaYuF8AD1Gwtt4WGgocAYso3FYuIMMIf5hOdC0lTIQXYxwmmvO+E8l/ FJ18wS1rkQx6YsuyapjlxVKdTZFDYF5Ei39v+Itr02T/ZDnpQ91z6tC6+kcRA61MAsUk O4jhyyvdzlVH6OI5KSX+gz0ZLDWmqojw9JGY8+BjE425dsr4i8WeQHow8LUFAyjy7ZmQ kiDa7blYl9Wslv9t4CLZx8nn5tmQOsZY5/9pg1tTAfSliiaLBkQgD4/0j0BmAjj/xnzJ nBrcXORBhGGDwVWAt+/LGrnX+8Xar/Rzir9UNREvmrjYlmCViy7k3dxaMlGkREBK4Bua GNtA== X-Gm-Message-State: AOAM532qy5/fYgpq8ptA3F1Qe8ibmlgkg8c3jjerYo86961wC18rdQ6X xkbbFxuJnINd1R0HtJQXUgwaiYdi51w= X-Google-Smtp-Source: ABdhPJwEJrGtdd7MwUO8/jqF7X5w5JTleM1Efb6C5Vx1BOVCVCTJ9SqKeQvLkOyofFoKZyVX72fMxw== X-Received: by 2002:a17:90a:ce02:: with SMTP id f2mr51648501pju.77.1636375500683; Mon, 08 Nov 2021 04:45:00 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id g16sm16383815pfj.5.2021.11.08.04.44.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:00 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 09/15] KVM: SVM: Remove the unneeded code to mark available for CR3 Date: Mon, 8 Nov 2021 20:44:01 +0800 Message-Id: <20211108124407.12187-10-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan VCPU_EXREG_CR3 is never cleared from vcpu->arch.regs_avail in SVM so marking available for CR3 is mere an NOP, just remove it. And it is not required to mark it dirty since VCPU_EXREG_CR3 is neither never cleared from vcpu->arch.regs_dirty and SVM doesn't use the dirty information of VCPU_EXREG_CR3. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/svm/nested.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 13a58722e097..2d88ff584d61 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -444,7 +444,6 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, kvm_mmu_new_pgd(vcpu, cr3); vcpu->arch.cr3 = cr3; - kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */ kvm_init_mmu(vcpu); From patchwork Mon Nov 8 12:44:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608375 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 769D2C433EF for ; Mon, 8 Nov 2021 12:45:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6094D61352 for ; Mon, 8 Nov 2021 12:45:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239759AbhKHMsB (ORCPT ); Mon, 8 Nov 2021 07:48:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239680AbhKHMrv (ORCPT ); Mon, 8 Nov 2021 07:47:51 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09C9AC061570; Mon, 8 Nov 2021 04:45:07 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id m26so15895652pff.3; Mon, 08 Nov 2021 04:45:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SwT7uyweM/6H8d06K/3A14sKRVp4dNL6BHRY2Yy/FP4=; b=Nv5XZq7JeTOHeffsZV/2wKloiGtmx0O40yZLK2N0R1m0TTEcD9+WElywK4Dq9R6OFp O0m7IJieV1kWqJyLmLTi5GwypbS09sBPeRRDaPxePGRYFQqenfsF8xz/46iVlUsbHRV3 8YiZu6pRVGYIJNjWRbp3jRpP2A27PHiaiBzFebNMPewnoYqvBkDU2zoIv3W6U9umArHx KrfUo8je9N+B4g0pTwB+nkOcLaLDTAE/scpOS8++vuof8x8Yzaj3s/z/KL4p5nUB2Q+g T21Nve8jt4zlKP904IYyv6fzuPZlksZB1tZq1nSu78bXdmfjmnNvKCY8FVWGYiJChpJ2 QN9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SwT7uyweM/6H8d06K/3A14sKRVp4dNL6BHRY2Yy/FP4=; b=Yqscm4vA17W4LNsHIWiHVs/o7mY27B3hjcGhlN4dYh6olpl4B7vcGYFnDJY2pUQVic fGCUmBkAUuGNexEZEhIcnZl2sUmubE2XiCVHEY/Rx3hGqSdMKWm1uqwh76kgcWTix+JX mPBdncCKNIeq7puW7XjXm1evr7BAGRPDehscmx3UBgFo7RcE/GwRVl+T6iiSy6sQZY0t cPevBQ3m7Ki//ToWJ+5lbm6BLJ403siso9ZSHqMLM1uRCmpxWCPPMtGFSNO+vFq1EowT B+gHKaKdwjYwwvaMlac+8DNIaM79NcdcPeQc9xS+kqX/UjKBqN+zDisbxLtnXSt6a1S1 vLbw== X-Gm-Message-State: AOAM530KznUwWbEKj7uPkBMjARNw9fVv+MfkzameQVyiu584GBXk4Knb zF/AAKtIjxrSKcVYxP5zu+cdJBrTv2E= X-Google-Smtp-Source: ABdhPJwI0hM8F6x840FJ7BWuQEoQ0aC1YyGV0LGxeJf7wt6kVJiy33evxlFOD+dVtYrq7lX/rG1IMw== X-Received: by 2002:a63:2aca:: with SMTP id q193mr60804765pgq.211.1636375506400; Mon, 08 Nov 2021 04:45:06 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id i6sm4905360pfu.173.2021.11.08.04.45.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:06 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 10/15] KVM: X86: Mark CR3 dirty when vcpu->arch.cr3 is changed Date: Mon, 8 Nov 2021 20:44:02 +0800 Message-Id: <20211108124407.12187-11-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan When vcpu->arch.cr3 is changed, it should be marked dirty unless it is being updated to the value of the architecture guest CR3 (i.e. VMX.GUEST_CR3 or vmcb->save.cr3 when tdp is enabled). This patch has no functionality changed because kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3) is superset of kvm_register_mark_available(vcpu, VCPU_EXREG_CR3) with additional change to vcpu->arch.regs_dirty, but no code uses regs_dirty for VCPU_EXREG_CR3. (vmx_load_mmu_pgd() uses vcpu->arch.regs_avail instead to test if VCPU_EXREG_CR3 dirty which means current code (ab)uses regs_avail for VCPU_EXREG_CR3 dirty information.) Signed-off-by: Lai Jiangshan --- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index dc0e5f80715d..ee5a68c2ea3a 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -1134,7 +1134,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, kvm_mmu_new_pgd(vcpu, cr3); vcpu->arch.cr3 = cr3; - kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); + kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */ kvm_init_mmu(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e5f5042d4842..6ca19cac4aff 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1159,7 +1159,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) kvm_mmu_new_pgd(vcpu, cr3); vcpu->arch.cr3 = cr3; - kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); + kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); handle_tlb_flush: /* @@ -10591,7 +10591,7 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, vcpu->arch.cr2 = sregs->cr2; *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; vcpu->arch.cr3 = sregs->cr3; - kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); + kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); kvm_set_cr8(vcpu, sregs->cr8); From patchwork Mon Nov 8 12:44:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608377 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42F7FC433F5 for ; Mon, 8 Nov 2021 12:45:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1FA0861288 for ; Mon, 8 Nov 2021 12:45:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238412AbhKHMsX (ORCPT ); Mon, 8 Nov 2021 07:48:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239728AbhKHMr4 (ORCPT ); Mon, 8 Nov 2021 07:47:56 -0500 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA390C061746; Mon, 8 Nov 2021 04:45:12 -0800 (PST) Received: by mail-pf1-x433.google.com with SMTP id m14so15870604pfc.9; Mon, 08 Nov 2021 04:45:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=453AXFvM7ls/vHkcEqDha9O8TvC50FNFpYyfW5EvhIs=; b=NbGzmf+mbIipAHgHj2T8JRa9A2PbUU0NAn5LQvviB078PItSyUmE5SijqcqplGA3XS alAT1wxn8EqferEnrXUoOHDQdYvPT7dQyh8b1dvkTQb8yPa+GGt2RdbtfAt36SFvWa/s FZbH8V6n1FrGIU4zgw94Pj08trtree/1ZleRFgiSuhdQV21mN2Q6QHdw2/9TRRgixiSB 8LwiO/C+IrLCuUWen6S1b1MYXp7rWIg0LC9aa+knSY/GDNMk9AIgNN6aw9Yqu2XGMFY/ JTGDI+54nxbaDMBid11Fu/D2Ta4eaVbFpTRzWGxZLNOxRk4NjKT6rb5YwatyNCBt1wz6 DGCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=453AXFvM7ls/vHkcEqDha9O8TvC50FNFpYyfW5EvhIs=; b=OyrdR3jgwhWLEUuqFamfa41QuiUo5rdrck5y5GBBF+S3TkzF8uRzMgMO3AP2/161rF ebrMsrbH84szDav4QMIWGnvd3RmOTDvx3RopJ9oJfE79irPay2LNUqNjhou80NTx7mh2 73ku7qRN8uz0oG9ntIgCow0oeANuQUYlsc9+IGGCvjXCzhI2YukOjMFEexsOHDNN7Ave Y150UGnNNsEMPk7S1YcEK5HOTuGhXedlDKwPnk9YvqZ0f5kfGG6AZLRfCnbdb+a5oXCc 10kxnQD1JUyx0EH4efw3EAVOi14fpXPXxdSL94FrlKNJ1Sp2BLg2gVuF2DTI6YEUZiFR 34qg== X-Gm-Message-State: AOAM532ioWW8Q3BJDPhjng77oD3LTCgdqGna/YLzdgSkBQcD1ndhy1fL JP6Xw/r9Qubi31H9YrbhAj7jhK5ziuc= X-Google-Smtp-Source: ABdhPJyc4vIsxwlCNzoB79MjjatwWka2mvuy2+/SXLBTZv1nW4SvOI6z9p2qOiXQnxKgUVfd93qnGw== X-Received: by 2002:a63:2317:: with SMTP id j23mr21090873pgj.41.1636375512099; Mon, 08 Nov 2021 04:45:12 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id j6sm12280076pgf.60.2021.11.08.04.45.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:11 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 11/15] KVM: VMX: Update vmcs.GUEST_CR3 only when the guest CR3 is dirty Date: Mon, 8 Nov 2021 20:44:03 +0800 Message-Id: <20211108124407.12187-12-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan When vcpu->arch.cr3 is changed, it is marked dirty, so vmcs.GUEST_CR3 can be updated only when kvm_register_is_dirty(vcpu, VCPU_EXREG_CR3). Signed-off-by: Lai Jiangshan --- arch/x86/kvm/vmx/vmx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d94e51e9c08f..38b65b97fb7b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3126,9 +3126,9 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, if (!enable_unrestricted_guest && !is_paging(vcpu)) guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; - else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) + else if (kvm_register_is_dirty(vcpu, VCPU_EXREG_CR3)) guest_cr3 = vcpu->arch.cr3; - else /* vmcs01.GUEST_CR3 is already up-to-date. */ + else /* vmcs.GUEST_CR3 is already up-to-date. */ update_guest_cr3 = false; vmx_ept_load_pdptrs(vcpu); } else { From patchwork Mon Nov 8 12:44:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608379 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CAB35C433EF for ; Mon, 8 Nov 2021 12:45:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B40C1610F8 for ; Mon, 8 Nov 2021 12:45:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239688AbhKHMsZ (ORCPT ); Mon, 8 Nov 2021 07:48:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239766AbhKHMsD (ORCPT ); Mon, 8 Nov 2021 07:48:03 -0500 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC77EC061205; Mon, 8 Nov 2021 04:45:18 -0800 (PST) Received: by mail-pf1-x42e.google.com with SMTP id o4so1818104pfp.13; Mon, 08 Nov 2021 04:45:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=A67VzkmpsIFHrMc7f+Wcqjp1h4M3PhJUyuzd6es3l8k=; b=ior0MqkaAdyC8qtt4GdN3bBd5naygoiB1I5aUn42nUjZf+//ZMNgD4wkZjz3LkIX5t Em5k2wmYSIeBFaBawG5yyh3K+ttB1S3+Tu4lMrybruJdbMLVS0i4wVR0A++h5LVD/x/q g1NEZXGjrw9e974izCJYxAsW3+F9uEEQlnP0NxIwRsajB3MHDs1BiricEaCkVTDWbGwA HdpgqPVUJW97Gab3DDMd2eOaaQYJ622BfOakQ5HW5Cj6gQK1YM2ZqPis+Nr80suZkRI1 6UU+NMovtTEJ3UKYNGlwKBSOEPFa3Fk2dHMQec2d96H3tiAWQD1tNsfBRjNIo6lM2v5H USGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=A67VzkmpsIFHrMc7f+Wcqjp1h4M3PhJUyuzd6es3l8k=; b=kGBT6TKzJkaAnzz5qVCYT901HNzyrd6Ed/qrQlDHzBA2Mkz0PA8lXckcpw9FEAT5ad YeS3e9v0Xo4fK5honVtXOVlQs9oq/g+wey7eQ4A7kgMDKCJ7y831OmrXtnOaFo0vhHCz gjT13X9SOjboe2xpG7uIBfGrzBmB33jyVIFdhu7eTVMKLbytpar2HaqOjTVxDWuxFoNt ufai7sSpKICY5yle5mHvvtB4fpa0jDhrtZq2Wu+SH/HUnHkphZ1xmNCRy3nARFFGPwHp ln9F1fPlShtbV5sB5b/7AzfcKxbUThsTjkPsAnrzKWct+U9yO8fx468sKQB91QllRQq3 uC0A== X-Gm-Message-State: AOAM533DemuEaqcez63mQPfaTTA7gUTtwZDg4wIzLzoGtttEeFxzPj5U 3wVMM/GPsyo3iUGS32b8dY1LkSkx2xk= X-Google-Smtp-Source: ABdhPJyrCaHCoYJa/M4qW3UJPuMmHCWK9221dY7LSGNAk5BYlEYMpTA+Uuyd/R1XdMbfTKqgH7ZpXg== X-Received: by 2002:a63:6a43:: with SMTP id f64mr88271pgc.393.1636375518181; Mon, 08 Nov 2021 04:45:18 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id z30sm5607212pfg.30.2021.11.08.04.45.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:17 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 12/15] KVM: VMX: Reset the bits that are meaningful to be reset in vmx_register_cache_reset() Date: Mon, 8 Nov 2021 20:44:04 +0800 Message-Id: <20211108124407.12187-13-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan Add meaningful bits as VMX_REGS_AVAIL_SET and VMX_REGS_DIRTY_SET. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/vmx/vmx.h | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index e7db42e3b0ce..465aa415c3cb 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -437,16 +437,33 @@ BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL) static inline void vmx_register_cache_reset(struct kvm_vcpu *vcpu) { - vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) - | (1 << VCPU_EXREG_RFLAGS) - | (1 << VCPU_EXREG_PDPTR) - | (1 << VCPU_EXREG_SEGMENTS) - | (1 << VCPU_EXREG_CR0) - | (1 << VCPU_EXREG_CR3) - | (1 << VCPU_EXREG_CR4) - | (1 << VCPU_EXREG_EXIT_INFO_1) - | (1 << VCPU_EXREG_EXIT_INFO_2)); - vcpu->arch.regs_dirty = 0; +/* + * VMX_REGS_AVAIL_SET - The set of registers that will be updated in cache on + * demand. Other registers not listed here are synced to + * the cache immediately after VM-Exit. + * + * VMX_REGS_DIRTY_SET - The set of registers that might be outdated in + * architecture. Other registers not listed here are synced + * to the architecture immediately when modifying. + */ +#define VMX_REGS_AVAIL_SET ((1 << VCPU_REGS_RIP) |\ + (1 << VCPU_REGS_RSP) |\ + (1 << VCPU_EXREG_RFLAGS) |\ + (1 << VCPU_EXREG_PDPTR) |\ + (1 << VCPU_EXREG_SEGMENTS) |\ + (1 << VCPU_EXREG_CR0) |\ + (1 << VCPU_EXREG_CR3) |\ + (1 << VCPU_EXREG_CR4) |\ + (1 << VCPU_EXREG_EXIT_INFO_1) |\ + (1 << VCPU_EXREG_EXIT_INFO_2)) + +#define VMX_REGS_DIRTY_SET ((1 << VCPU_REGS_RIP) |\ + (1 << VCPU_REGS_RSP) |\ + (1 << VCPU_EXREG_PDPTR) |\ + (1 << VCPU_EXREG_CR3)) + + vcpu->arch.regs_avail &= ~VMX_REGS_AVAIL_SET; + vcpu->arch.regs_dirty &= ~VMX_REGS_DIRTY_SET; } static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) From patchwork Mon Nov 8 12:44:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608381 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C470C433EF for ; Mon, 8 Nov 2021 12:45:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9E18610F8 for ; Mon, 8 Nov 2021 12:45:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239672AbhKHMsc (ORCPT ); Mon, 8 Nov 2021 07:48:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239780AbhKHMsI (ORCPT ); Mon, 8 Nov 2021 07:48:08 -0500 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A66DC061225; Mon, 8 Nov 2021 04:45:24 -0800 (PST) Received: by mail-pf1-x435.google.com with SMTP id x131so10689058pfc.12; Mon, 08 Nov 2021 04:45:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TmKbYqg9m5arRUZ1yoqUsfhIjoLn2jM25Bi3woDZ7Y0=; b=TS5T3BOEfcFbwXedPw5SQlHGYER9dzbE2MGMUzpOVp61wtiNOlSDGyfFphYYW7e8vv kvwIcTAWC5SS0AZWQVjfYHvRF8OYfkoT6pkY4Fq625e4xYQY0kEFjFHPfNR4ewKPGuXG es6pdEQ0d1BsjUlTvoV45pyunHhU5rwgOrZHrZo+Bdd4XrHU++n+Op+mCINNvW0cHkhz AzPUlIk9uL3JOkCqcuBhH/hpIkq2TCRqd3n4EMjDbLre75QbAU+5gr8lMvw/4LOd971P hWmjGAf+mVnzOXTtDLUUwPBMkCM/bnKRreL0cLfBhq7VvDTZdKy9VgqgAWUP6WjvLsVz ydlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TmKbYqg9m5arRUZ1yoqUsfhIjoLn2jM25Bi3woDZ7Y0=; b=2HXzObYLNQdSX07lYDqAsjS9/9MZbkkFIINFU5maENJPiaJRT9LpZpq9Mk6YLNmNWH 1r9zVgMmgcEMcu0W55Oown/eECV2avgoqZw9Rb+nzyKGr28x8lWkpRO2sTZtNyCJvMjx wdq9bxWEm7L9Upe0R9nMX7p+O2v1Rm8vDbwHtU7GkQWQey7W128TkX++G0o10f3Jy8+K iQtD0/9K5INgN5WFFqRB8Q2KYZoGFh7MFyUd5L00nd2FN6jqKpHrEwPlxt9hduTLyu3G mLVwGRQ+rXnsSpxDRFqmB787BW2Yl8A6vnroTXIcnTf6QtlC/VgPL33KunQlFKqLXgc0 KJWA== X-Gm-Message-State: AOAM532bjTOHGx7Xrc6QOoMsA8mCfhADxn9Dm1EC9MasFR0+UDpt3TKS kxlpkmMelkEwCZmvsRk0Igmhf3hai6g= X-Google-Smtp-Source: ABdhPJyFsaYpfmszc9hhnb+Dy6cSfwkjtylHDvxvBq5PdoJUz0xbxZV8tvxJsYgWyHuDu0vlA/nMzQ== X-Received: by 2002:a63:6881:: with SMTP id d123mr43841319pgc.68.1636375523761; Mon, 08 Nov 2021 04:45:23 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id k14sm12709952pga.65.2021.11.08.04.45.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:23 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 13/15] KVM: SVM: Add and use svm_register_cache_reset() Date: Mon, 8 Nov 2021 20:44:05 +0800 Message-Id: <20211108124407.12187-14-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan It resets all the appropriate bits like vmx. Signed-off-by: Lai Jiangshan Reviewed-by: Lai Jiangshan --- arch/x86/kvm/svm/svm.c | 3 +-- arch/x86/kvm/svm/svm.h | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b7da66935e72..ba9cfddd2875 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3969,8 +3969,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; vmcb_mark_all_clean(svm->vmcb); - - kvm_register_clear_available(vcpu, VCPU_EXREG_PDPTR); + svm_register_cache_reset(vcpu); /* * We need to handle MC intercepts here before the vcpu has a chance to diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0d7bbe548ac3..1cf5d5e2d0cd 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -274,6 +274,32 @@ static inline bool vmcb_is_dirty(struct vmcb *vmcb, int bit) return !test_bit(bit, (unsigned long *)&vmcb->control.clean); } +static inline void svm_register_cache_reset(struct kvm_vcpu *vcpu) +{ +/* + * SVM_REGS_AVAIL_SET - The set of registers that will be updated in cache on + * demand. Other registers not listed here are synced to + * the cache immediately after VM-Exit. + * + * SVM_REGS_DIRTY_SET - The set of registers that might be outdated in + * architecture. Other registers not listed here are synced + * to the architecture immediately when modifying. + * + * Special case: VCPU_EXREG_CR3 should be in this set due + * to the fact. But KVM_REQ_LOAD_MMU_PGD is always + * requested when the cache vcpu->arch.cr3 is changed and + * svm_load_mmu_pgd() always syncs the new CR3 value into + * the architecture. So the dirty information of + * VCPU_EXREG_CR3 is not used which means VCPU_EXREG_CR3 + * isn't required to be put in this set. + */ +#define SVM_REGS_AVAIL_SET (1 << VCPU_EXREG_PDPTR) +#define SVM_REGS_DIRTY_SET (0) + + vcpu->arch.regs_avail &= ~SVM_REGS_AVAIL_SET; + vcpu->arch.regs_dirty &= ~SVM_REGS_DIRTY_SET; +} + static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_svm, vcpu); From patchwork Mon Nov 8 12:44:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608383 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94C1CC433FE for ; Mon, 8 Nov 2021 12:45:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7B97A61378 for ; Mon, 8 Nov 2021 12:45:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239781AbhKHMse (ORCPT ); Mon, 8 Nov 2021 07:48:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239799AbhKHMsS (ORCPT ); Mon, 8 Nov 2021 07:48:18 -0500 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6482BC06122A; Mon, 8 Nov 2021 04:45:30 -0800 (PST) Received: by mail-pg1-x52d.google.com with SMTP id g28so6396913pgg.3; Mon, 08 Nov 2021 04:45:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=T8hpLUlYpKoolycknjDctgk2NLH51FU7az3iu++Ol2c=; b=opwHVayWIMLEZh2njFuTc1B6vQWxBv5AWm068Jk9QebU6DuhSPrt0JM8dixguMk6Ee c/HFPgNI8sMxaSOKu9gD2yLxmjBr3g0u2EhENHvv1tLVQR7TY7ybvGAeeeRY1X0bK2TZ duIjILbMtd+9JvwBcDXIK4v5PhEjMPhdSbMN3Lj4XSv8WsOzyt7HnGYlA84av6WkFWKs P0t+zpTkRTs0/9896NTJ+FRyOAnZeAJf+GXYR9xOJDe1EoZxKH5w6trl7hLM+Wd/3jCe cbnx5maqxI1qyRFvhvNbbHi1e9QP2IXg1cKIZeZRtgf9LzacNW8o9CrIS89s4iwaZujl NKzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=T8hpLUlYpKoolycknjDctgk2NLH51FU7az3iu++Ol2c=; b=QQ5FJ++EidgklcOiW1jey9w1Z23CDZw9Fm1XXOeMa8HcfxUS+Sh5QALRI091SivTKW cQvhdzwEotWqfZfbTQbOgSiEnXEb/+YNMfmYlmPq5VHThVdDcJK3EY7c2oAaHQ7iX9GF v/KuvFF2o/U2OsRP2dvqAek0YvBN60Qk2lJ9yhz+IM9BgNS3zWTTcPgcjSjqPOgO6dHt SrywiSeUEpH8QHLnkP9kIVjmi7OddI+daQJNn3XMP1t+88BaFp+5x1QsTUsTE6/nUXTR z78KRp+u6bPrEo7axrmFF6xODF6J9eZXShaU4JIsBTLuopVlJKbQsA9GHGWlA8RcWMMy 0zOA== X-Gm-Message-State: AOAM533QlwzHS4fCFHa0NsOjSDl6phIPeW2YOZOXpOiFivcqpvpUsR3l xEaVI4zKlIlVK5wdTfKRiq91zy/VTLQ= X-Google-Smtp-Source: ABdhPJwj/V9g53yD5iuSNQkjP9LLhkRiLwwybwqUa7yRLuboP1PQkLi6OMqhEJYQZARZtD+WIxYq7Q== X-Received: by 2002:a05:6a00:1741:b0:49f:99e6:1d1d with SMTP id j1-20020a056a00174100b0049f99e61d1dmr27440579pfc.34.1636375529742; Mon, 08 Nov 2021 04:45:29 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id h22sm4457675pgh.80.2021.11.08.04.45.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:29 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 14/15] KVM: X86: Remove kvm_register_clear_available() Date: Mon, 8 Nov 2021 20:44:06 +0800 Message-Id: <20211108124407.12187-15-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan It has no user. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/kvm_cache_regs.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 54a996adb18d..0f8847b981e5 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -61,13 +61,6 @@ static inline void kvm_register_mark_available(struct kvm_vcpu *vcpu, __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); } -static inline void kvm_register_clear_available(struct kvm_vcpu *vcpu, - enum kvm_reg reg) -{ - __clear_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); - __clear_bit(reg, (unsigned long *)&vcpu->arch.regs_dirty); -} - static inline void kvm_register_mark_dirty(struct kvm_vcpu *vcpu, enum kvm_reg reg) { From patchwork Mon Nov 8 12:44:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12608385 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E199C433EF for ; Mon, 8 Nov 2021 12:45:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 461CF610F8 for ; Mon, 8 Nov 2021 12:45:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239801AbhKHMsi (ORCPT ); Mon, 8 Nov 2021 07:48:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239825AbhKHMsV (ORCPT ); Mon, 8 Nov 2021 07:48:21 -0500 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B015DC061746; Mon, 8 Nov 2021 04:45:36 -0800 (PST) Received: by mail-pg1-x529.google.com with SMTP id r28so15053976pga.0; Mon, 08 Nov 2021 04:45:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ds5oAZuKgLL1xXWixp9LhgaW7VT7IJj7unEHWE7U3AA=; b=lyKdXi1LAjXqUBVHalP53qi4+ETLcq5PAJ4GFZ1LzlOPGPpqLC5O3NART0Ot2AZf9J tPoYgIWjfjsGZ+V4Kej4yrY0cUUdgClfcEsWrqlMdmrfPHMxyNOzQSQsd+lWG9z79vjU v8PZWeQmmGfTo6AiZtOzNHgGPNKivghpOfW+PNp0uJ6TKOzdnLdIdxg5qNL5707wAgc+ Pu7/7FwETwHzFlJXFsCc5j6zK/KEFHTOB4jkrfDjgVwRT5TUaf1BZZJOF/jkYH7ZAliW 2No7GURKjYhcGSjNrdnaObknT/IqGwR7sMf8I2o13ck+TSPcI/g+zFWRVEiHHs9zkzig NRdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ds5oAZuKgLL1xXWixp9LhgaW7VT7IJj7unEHWE7U3AA=; b=SSMQnLXjkmYQPimnaIkTS5swpL4IMe20E354VvP81J+fO5gi6r8HWJC8AoAjqm9EgM TUYa5t/Utv3+fW2VFaNKIPjQ9rc82PMXLHP5IlzD7QeWlbrM5A9sRitP1iONOJxRO/qH YHiHNJr9D5Vd/9o3X67JfCcWahbz0LXFNF6DSLuA5R91U8PKb222h/1jMM3bCDLmi3PN lziPDYs9/HHe54ymbmZhFS41YzRGha/LR2BzpmIvPoE69fEOoqvUzR3MSFUXKsbTcMhJ bnS3ThxdgDyiIIYYe9JfklKlsmHq+KtG00yE3bb9xRlsEyTb3/lnKe6Z0Tljpp+a/CrR /o8g== X-Gm-Message-State: AOAM532VSPRPZB/UsGvESVw2mDTZPRhDX0IEVvk534CWGQHMiKoDUB7o RYM+LUTn9iCWDrlpj5GqVTWWUvlF+Lo= X-Google-Smtp-Source: ABdhPJy9KW5BcL4gCTCvaGKr1La4X3SLYlC6REo6VDucO+mKTFycxjA50D3cfXvv13u6RDHa+nrOdg== X-Received: by 2002:a63:3543:: with SMTP id c64mr28825166pga.443.1636375536060; Mon, 08 Nov 2021 04:45:36 -0800 (PST) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id b8sm15424651pfi.103.2021.11.08.04.45.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Nov 2021 04:45:35 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini Cc: Lai Jiangshan , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 15/15] KVM: nVMX: Always write vmcs.GUEST_CR3 during nested VM-Exit Date: Mon, 8 Nov 2021 20:44:07 +0800 Message-Id: <20211108124407.12187-16-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan For VM-Enter, vmcs.GUEST_CR3 and vcpu->arch.cr3 are synced and it is better to mark VCPU_EXREG_CR3 available rather than dirty to reduce a redundant vmwrite(GUEST_CR3) in vmx_load_mmu_pgd(). But nested_vmx_load_cr3() is also served for VM-Exit which doesn't set vmcs.GUEST_CR3. This patch moves writing to vmcs.GUEST_CR3 into nested_vmx_load_cr3() for both nested VM-Eneter/Exit and use kvm_register_mark_available(). This patch doesn't cause any extra writing to vmcs.GUEST_CR3 and if userspace is modifying CR3 with KVM_SET_SREGS later, the dirty info for VCPU_EXREG_CR3 would be set for next writing to vmcs.GUEST_CR3 and no update will be lost. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/vmx/nested.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index ee5a68c2ea3a..4ddd4b1b0503 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -1133,8 +1133,28 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, if (!nested_ept) kvm_mmu_new_pgd(vcpu, cr3); + /* + * Immediately write vmcs.GUEST_CR3 when changing vcpu->arch.cr3. + * + * VCPU_EXREG_CR3 is marked available rather than dirty because + * vcpu->arch.cr3 and vmcs.GUEST_CR3 are synced when enable_ept and + * vmcs.GUEST_CR3 is irrelevant to vcpu->arch.cr3 when !enable_ept. + * + * For VM-Enter case, it will be propagated to vmcs12 on nested + * VM-Exit, which can occur without actually running L2 and thus + * without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with + * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept + * the transition to HLT instead of running L2. + * + * For VM-Exit case, it is likely that vmcs.GUEST_CR3 == cr3 here, but + * L1 may set HOST_CR3 to a value other than its CR3 before VM-Entry, + * so we just update it unconditionally. + */ + if (enable_ept) + vmcs_writel(GUEST_CR3, cr3); + vcpu->arch.cr3 = cr3; - kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3); + kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */ kvm_init_mmu(vcpu); @@ -2600,16 +2620,6 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, from_vmentry, entry_failure_code)) return -EINVAL; - /* - * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 - * on nested VM-Exit, which can occur without actually running L2 and - * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with - * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the - * transition to HLT instead of running L2. - */ - if (enable_ept) - vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); - /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { From patchwork Thu Nov 11 14:45:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12614967 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F517C433EF for ; Thu, 11 Nov 2021 14:45:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2FBF461212 for ; Thu, 11 Nov 2021 14:45:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233751AbhKKOsN (ORCPT ); Thu, 11 Nov 2021 09:48:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233287AbhKKOsM (ORCPT ); Thu, 11 Nov 2021 09:48:12 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02EBEC061766; Thu, 11 Nov 2021 06:45:23 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id y14-20020a17090a2b4e00b001a5824f4918so4776691pjc.4; Thu, 11 Nov 2021 06:45:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Dn20pTIFu4HHwXs9x/rHm6WXV5R7u62HLta5pjb6tC0=; b=ANTb3xKSQAjwywKq8UHZQzAS5iqonIlBAAexPyNnUwbY8KA/pmekqLy3qy2gIBffKl pbe+K8S7xbSkFL7KorIdut2gR52/t3Xfs81h8vHqYTGjPuMCIMWFtaR3DHskymrVUq8m MOUne3SkkJGChx6GODQZkkKzqfo+8/zd4CXfcY3MY/qhCFFHhswmO2l0WBHJiUbEE6TC gERDvQ5V243OIAIwL/WGksbBHjacWO+9tW1Iaa0d4filtz+ipqd9FcdLPzGb7tvg6eKo 205DNx+vN1fWi5ImltXrUEnWAGobtSHzyN+TR+Pov/nJXNoOFbgvVpwKX6z3JcixKxDo ce/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Dn20pTIFu4HHwXs9x/rHm6WXV5R7u62HLta5pjb6tC0=; b=3yygbe7JGBpXXRJdxKQ06Ypq4aAvpNfH44f2pYzPm2hx41u6BgcCu+tLo80VcRhFkA uGIEmrRjQUMIHup77cT4zm/UY8di96OsZBKSrAN05Neb8ZkKBHww/Up8mYsVlty49vF5 ljOU8NuoPwrMeOg0WKiuZGDeTyd06I/yaOMR1xiAXu97KCV1s/CtJa9WwJ9Ym7ExLq6q Nq2CYN7caQx0WdqalNVZG7N9C57MR1qV2AJelHaQX9Rilmoyiect054HwnbDug6UE/Hr pT0CDRPNQuhHV9Vz7BKGe6+ff48Jav++ybW91YYukB0jRdJ9vPAfHLIoCqM37XMT5gEO CQUA== X-Gm-Message-State: AOAM530Q+5+4RC3XbkU510/BhFajQ6E6+3CAWpM0V+cGBWVKwngYJqlI BEb+mUNlg8lILSv+tIoHGInyIg5yIww= X-Google-Smtp-Source: ABdhPJwuU77mjaqDv9EnZe9RcQ379DtJYH3n1Dzslu6+/oS8afhvwexoiOXJXgsPzxWgD6WikuOwNA== X-Received: by 2002:a17:902:7fc5:b0:143:6d84:88eb with SMTP id t5-20020a1709027fc500b001436d8488ebmr8191238plb.61.1636641922348; Thu, 11 Nov 2021 06:45:22 -0800 (PST) Received: from localhost ([47.251.4.198]) by smtp.gmail.com with ESMTPSA id ns21sm8329927pjb.37.2021.11.11.06.45.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Nov 2021 06:45:22 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Lai Jiangshan , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 16/15] KVM: X86: Update mmu->pdptrs only when it is changed Date: Thu, 11 Nov 2021 22:45:26 +0800 Message-Id: <20211111144527.88852-1-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan It is unchanged in most cases. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/x86.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6ca19cac4aff..0176eaa86a35 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -828,10 +828,13 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) } } - memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); - kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); - /* Ensure the dirty PDPTEs to be loaded. */ - kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu); + kvm_register_mark_available(vcpu, VCPU_EXREG_PDPTR); + if (memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) { + memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); + kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); + /* Ensure the dirty PDPTEs to be loaded. */ + kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu); + } vcpu->arch.pdptrs_from_userspace = false; return 1; From patchwork Thu Nov 11 14:46:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 12614969 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB945C433F5 for ; Thu, 11 Nov 2021 14:46:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92C5161213 for ; Thu, 11 Nov 2021 14:46:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233437AbhKKOtR (ORCPT ); Thu, 11 Nov 2021 09:49:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230177AbhKKOtQ (ORCPT ); Thu, 11 Nov 2021 09:49:16 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CACE4C061766; Thu, 11 Nov 2021 06:46:27 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id w33-20020a17090a6ba400b001a722a06212so4190124pjj.0; Thu, 11 Nov 2021 06:46:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aRIZhEJSC5CLQX6HTzAaA6f+WmYf2hQfxbDd38/dBig=; b=iVj5d/aqJVXFZjT5mEWOEg0AVSZ86JeMFrApiyvai8PrimFgwfAAT4Wxc3jdG/0vnA +Z3n6PvyKqjP6/nDu70n3dEhLNnerbO0/M5A65IKgLRjKr0Vxdpiif5aRCRh5sRpFrC5 dZqEcvHFE5GPbMFGpWq77VvEMzNDDCvF0JcLSoobOW13DoXW81sdaQ794Xft+3d177EY URkAIZ7BCeLa3YeNa1Q0x3hTkrZr1oQ/k9B/Nqv5xNGTnqEp4PwtX7uSY5DolHQnj5MQ up96Vp8C1QxMQMBWYHeOiWkOx4q6Rh91sgDrPxx7oa28VILvLqYAjMYbi7bYGSDCXqiS TeFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aRIZhEJSC5CLQX6HTzAaA6f+WmYf2hQfxbDd38/dBig=; b=c1cbMDokld12F1xmC9CAuj9H2QRrP1GPFfRYawWnPKDh5N4oV6KX7FNzTZuQUmXtzL gh+/ixV6A3qRb/UPa1Y/8P8vkb0AuQ1gXt4HOH9aM+VRVWax2FzzUmb2/FANyYG3vaRw BcMytIyF+UtbkULs8V6Cz/xw9uzqLiMpRDQWcBKpIPQDiHUb1Sq2E2j/ZCFA6t+945yA DCs+Ba5+jVT0fktRYgSvTzhTD3/Eji7BSHIVA7DXUp3xNsgVmuJnT6U3+H1gfUEu6aq9 GzvxROQk6nn8cEepf4jXSGd9d556ZtNzp4N0Psz3FxR9Z874Hz54pIv9trM4vxbvsnUx a34w== X-Gm-Message-State: AOAM530N7wMuBL2POBpFSap4Lg5zTZ+6XAsJwxgK3rJ9XnQQoC2Q3gZC ridi7DPwTW9PwEhRXJAlNW6TQ1j8RTA= X-Google-Smtp-Source: ABdhPJyYRCFRSloNwuNIXl3Hp9JTYXsSB6oRo4rEr2iZWQYuRPVtIJhPDTdLSoxqi4D9AZQojGoD4Q== X-Received: by 2002:a17:90b:4b90:: with SMTP id lr16mr27177912pjb.57.1636641987180; Thu, 11 Nov 2021 06:46:27 -0800 (PST) Received: from localhost ([47.251.4.198]) by smtp.gmail.com with ESMTPSA id a8sm2570103pgh.84.2021.11.11.06.46.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Nov 2021 06:46:26 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Lai Jiangshan , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Xiao Guangrong Subject: [PATCH 17/15] KVM: X86: Ensure pae_root to be reconstructed for shadow paging if the guest PDPTEs is changed Date: Thu, 11 Nov 2021 22:46:34 +0800 Message-Id: <20211111144634.88972-1-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211108124407.12187-1-jiangshanlai@gmail.com> References: <20211108124407.12187-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Lai Jiangshan For shadow paging, the pae_root needs to be reconstructed before the coming VMENTER if the guest PDPTEs is changed. But not all paths that call load_pdptrs() will cause the pae_root to be reconstructed. Normally, kvm_mmu_reset_context() and kvm_mmu_free_roots() are used to launch later reconstruction. The commit d81135a57aa6("KVM: x86: do not reset mmu if CR0.CD and CR0.NW are changed") skips kvm_mmu_reset_context() after load_pdptrs() when changing CR0.CD and CR0.NW. The commit 21823fbda552("KVM: x86: Invalidate all PGDs for the current PCID on MOV CR3 w/ flush") skips kvm_mmu_free_roots() after load_pdptrs() when rewriting the CR3 with the same value. The commit a91a7c709600("KVM: X86: Don't reset mmu context when toggling X86_CR4_PGE") skips kvm_mmu_reset_context() after load_pdptrs() when changing CR4.PGE. Normally, the guest doesn't change the PDPTEs before doing only the above operation without touching other bits that can force pae_root to be reconstructed. Guests like linux would keep the PDPTEs unchaged for every instance of pagetable. Fixes: d81135a57aa6("KVM: x86: do not reset mmu if CR0.CD and CR0.NW are changed") Fixes: 21823fbda552("KVM: x86: Invalidate all PGDs for the current PCID on MOV CR3 w/ flush") Fixes: a91a7c709600("KVM: X86: Don't reset mmu context when toggling X86_CR4_PGE") Signed-off-by: Lai Jiangshan --- arch/x86/kvm/x86.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0176eaa86a35..cfba337e46ab 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -832,8 +832,14 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) if (memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) { memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); - /* Ensure the dirty PDPTEs to be loaded. */ - kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu); + /* + * Ensure the dirty PDPTEs to be loaded for VMX with EPT + * enabled or pae_root to be reconstructed for shadow paging. + */ + if (tdp_enabled) + kvm_make_request(KVM_REQ_LOAD_MMU_PGD, vcpu); + else + kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, KVM_MMU_ROOT_CURRENT); } vcpu->arch.pdptrs_from_userspace = false;