Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3096328pxj; Mon, 7 Jun 2021 02:04:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQmH+GeWzA2D/XHjMEhulk/fuBaUVfQa2mO27d3vx5dePnQoEohIQgtsDChgxfGLWn7lA4 X-Received: by 2002:a17:907:3e1a:: with SMTP id hp26mr16968914ejc.77.1623056697581; Mon, 07 Jun 2021 02:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623056697; cv=none; d=google.com; s=arc-20160816; b=LiLwbdBF4venVW94JSYbSHKgL9gZcIAmuE/dulVibbwCIbkDW4buDpjQ+naESuD6c0 BYG9qIOJqx48qW1gR159bxWcNJyNvCcYAOweSoaJO0TOJwpF6/s3Q/SwQsd+y7PN5I5E f8BWyQ9jrtosexjg0CgGn+qydJpL1NIUIm06vhEnvBNyapsvdaiK8Xss4D92/9HDpUAe 5iOPMuP2AO3dwk6NW4luow5/PUGtMpB2g/lzOIIztuQpMNk0l5pBy9TyuuIXpM/bg9BO uf5FrWsaak/QasHUYoGmSLrRpNh6VwSbWepQOnJfELXb4v5ZdmiLMW4IqvJRyFT8wuNn OLqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Cs0csa7t65m5XXMi4gqzOayl+MyH3sAO73AqdRmrWhQ=; b=OoLXHPu+/BwFZrTj7Vsf62JfoLarppM9HASCg7m0Lynz+Z7ZbXH0ROduiyKfmc7Our MPfLZ0bGT2z5+hsOCzKw1ky65jjObcoSwwSpZHYXPbXetW2ee3LZx/J0uktFhsI8eQoG 148GLbFizXf2+Dfyuhgb+J0+JfMNO+DFk6Q951TYLSxSN820F3rnvufbDBRJjXF4aO3Y 2O0a84ATKVsVOUe/01v7jMQ9Yj78CL9d4LfipYQ+pmWmaVBn3lchsGUfNkMZ1dBfsiBK 5nyEcrlD9xJfW/VpmGNG0QF1JgpwXHmsvUFXeIzP+w7EBH59kdZDkrB1TA933R0Dr/xR T9YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Pwcl0Ihn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p17si13243506edq.63.2021.06.07.02.04.34; Mon, 07 Jun 2021 02:04:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Pwcl0Ihn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230458AbhFGJEd (ORCPT + 99 others); Mon, 7 Jun 2021 05:04:33 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:21777 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230404AbhFGJEc (ORCPT ); Mon, 7 Jun 2021 05:04:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623056561; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Cs0csa7t65m5XXMi4gqzOayl+MyH3sAO73AqdRmrWhQ=; b=Pwcl0Ihn5qU+lh/E+P20aqZ/+1yyp/5ZnowayUWWWg0t6JU1VqBgITWz16w35MR2i39N4g 64HJG16CWO8vYuptCXUfMo87aLkIVjZvzxBJ9QKNprB4LuFKJjE5/15cyZozpPAd/DqkHd i+PrBkkwFUBgGLkgstwD4rYzdWIXreI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-560-z8JH8As9OJyCx-ZpXSoI3A-1; Mon, 07 Jun 2021 05:02:40 -0400 X-MC-Unique: z8JH8As9OJyCx-ZpXSoI3A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BF4A480EF80; Mon, 7 Jun 2021 09:02:38 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.194.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4C24100EB3D; Mon, 7 Jun 2021 09:02:32 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Paolo Bonzini , linux-kernel@vger.kernel.org (open list), linux-doc@vger.kernel.org (open list:DOCUMENTATION), Wanpeng Li , Ingo Molnar , Vitaly Kuznetsov , Thomas Gleixner , Sean Christopherson , Borislav Petkov , Joerg Roedel , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), Jim Mattson , Jonathan Corbet , "H. Peter Anvin" , Maxim Levitsky Subject: [PATCH v3 4/8] KVM: nSVM: refactor the CR3 reload on migration Date: Mon, 7 Jun 2021 12:01:59 +0300 Message-Id: <20210607090203.133058-5-mlevitsk@redhat.com> In-Reply-To: <20210607090203.133058-1-mlevitsk@redhat.com> References: <20210607090203.133058-1-mlevitsk@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Document the actual reason why we need to do it on migration and move the call to svm_set_nested_state to be closer to VMX code. To avoid loading the PDPTRs from possibly not up to date memory map, in nested_svm_load_cr3 after the move, move this code to .get_nested_state_pages. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/nested.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 8f5dbc80f57f..e3e5775b8f1c 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -385,12 +385,12 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm) * if we are emulating VM-Entry into a guest with NPT enabled. */ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, - bool nested_npt) + bool nested_npt, bool reload_pdptrs) { if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) return -EINVAL; - if (!nested_npt && is_pae_paging(vcpu) && + if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) && CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) return -EINVAL; @@ -574,7 +574,7 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, nested_vmcb02_prepare_save(svm, vmcb12); ret = nested_svm_load_cr3(&svm->vcpu, vmcb12->save.cr3, - nested_npt_enabled(svm)); + nested_npt_enabled(svm), true); if (ret) return ret; @@ -803,7 +803,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) nested_svm_uninit_mmu_context(vcpu); - rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false); + rc = nested_svm_load_cr3(vcpu, svm->vmcb->save.cr3, false, true); if (rc) return 1; @@ -1299,6 +1299,19 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, !nested_vmcb_valid_sregs(vcpu, save)) goto out_free; + /* + * While the nested guest CR3 is already checked and set by + * KVM_SET_SREGS, it was set when nested state was yet loaded, + * thus MMU might not be initialized correctly. + * Set it again to fix this. + */ + + ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3, + nested_npt_enabled(svm), false); + if (WARN_ON_ONCE(ret)) + goto out_free; + + /* * All checks done, we can enter guest mode. Userspace provides * vmcb12.control, which will be combined with L1 and stored into @@ -1356,9 +1369,14 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) if (WARN_ON(!is_guest_mode(vcpu))) return true; - if (nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3, - nested_npt_enabled(svm))) - return false; + if (!nested_npt_enabled(svm) && is_pae_paging(vcpu)) + /* + * Reload the guest's PDPTRs since after a migration + * the guest CR3 might be restored prior to setting the nested + * state which can lead to a load of wrong PDPTRs. + */ + if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) + return false; if (!nested_svm_vmrun_msrpm(svm)) { vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; -- 2.26.3