Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp795031pxa; Wed, 5 Aug 2020 12:58:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTMnSc/D9Itc95I3dMyJaybjKpc7OxrzFSae97svkALv9VXSmZuBli7JJZhE+bHYX4Ui9R X-Received: by 2002:aa7:cb15:: with SMTP id s21mr910263edt.175.1596657539125; Wed, 05 Aug 2020 12:58:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596657539; cv=none; d=google.com; s=arc-20160816; b=RWQ2p7wmG4U6INQUvcOGcqFmSd+7UMlM5lQzrTGlIw+p21HXnZ6R4hZMQIELgBQo2Z 5rzDumkBAnBYaY1wTV+Znt2lCY+oi2Ykeq5CXPrVlTpeFksRWyEcSbgjUZadN2PHOuo8 sumh37JynMxRqmB3yb8eEnog58BFUCC0bju6tkrZBavEtWmC08LlMK5TWfbUg8GcH+Tj nTcNuXkVlhgbw2Yy0EMGpB/Db85qS1c8S/wqFvX2WzK7L8pK7IA2EcBhM5qHcNmdX2zU bnHYglLXLyYFUG9Co4sbp44TbFBbZ2i0+SlEP1HqE+2n67jfPFKAIN7uRN7b8P6wii8p z9Hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=Z1dq08rcyRAJKP6YZL/SMfPNJkj3TPNkHN01IYY4yWs=; b=ZhpypQR0fT2rKFDApqXgx7k3h8TX0jgTtoe8PiHx2qRWGg7dblBMO4dzAwIHN0d9Jf dGNUnxa3egSkp8hfpReVleDDzbC8CyLt8NxcfdWwTUOSgJSu7zj+JvcQ73s5YhnGRP5a QEydUnxMrzwNv759k8rsvi2GK6AD28MhU4kyTaAeeir7IgAkQvF22yc1oWhzqCWDxg5L 1cuCk+TNiCRjIwp2xvThRKccPvX7JPdaufpFbgJWRyadcmap+G6KleO/dWR3H0cyMrN0 37N20zM74YJKgXH3mw8Jd6Q+gWlzBsFU4G603qWh0rBUyFPEzdoh+Gk34jQ8Gek7ipgp 9OCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XroRQzFh; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y1si1258964edm.489.2020.08.05.12.58.36; Wed, 05 Aug 2020 12:58:59 -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=@gmail.com header.s=20161025 header.b=XroRQzFh; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728984AbgHET5Y (ORCPT + 99 others); Wed, 5 Aug 2020 15:57:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727977AbgHEQny (ORCPT ); Wed, 5 Aug 2020 12:43:54 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D210C0A893C; Wed, 5 Aug 2020 07:12:56 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id u185so22789067pfu.1; Wed, 05 Aug 2020 07:12:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Z1dq08rcyRAJKP6YZL/SMfPNJkj3TPNkHN01IYY4yWs=; b=XroRQzFhX23GOwnvJ+TMAohKF4jLXddbL2pj4Vw71Fv6+fTlsOQL75TwEX/IfhoZXu MbkZ02tnwmXDdaJDdqD4378pi9AHy0egPbJGYx/AQ/Qb8zdVOVi5tJAfYkNmTmqyS3k8 6pmcLeJx6DSR8ghJB/OopLM6Z5YioxqNN1KVrsvbvD69FeKQ7yMg1V4ifrnk4rhuG5I5 ATi3LOotF/G5kthGiBTZOa+vWCsr97uArQGw/gBUCBzvp18H5F9IWL9fTy6LrnfGFG7u LxwozEMlqFzTIzquuHwh3hHdW+HKviD7rvsng85PoalKziVwuQ9ZIXskHGS1Xj5/LQYz /0Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Z1dq08rcyRAJKP6YZL/SMfPNJkj3TPNkHN01IYY4yWs=; b=sBQ6KALId60/sBtnDB+n9oMqT+Cka/o3NGrCBxhNyKBV4DZre85LgMeyTASQXBxrEB o58TGZ2/2M1PwcaPqW0K468SB8yI+T11zdg2eJ1gcR0/1YvnKruk+pXZXB3AdR9L1rBy GnPw6RT3t1DT+TfrvhXazQRpU0W9YDi4pQ6QhkUnargnYiRZ2/WO8a900gM7ixjt/Q8O bak957wByQLI23MWUKTwHE33Ff9zEpreynf6vwDcsx3xqmSj7pHM66GZOA4G0ozDJTnx Yt7nRygquMci4cyTMO5vai5VVU/qTC3jLMYGMpvQ/479KUhqRYbsEDQriAmCgssG9Uiy XxLw== X-Gm-Message-State: AOAM533oHVRqiytEHj6DzgYMar1wXQ9NDOR+DZHDs+f1P7yveATNE0Wp g/XchM9OvZ7EczcCIMg2PNg= X-Received: by 2002:a65:5502:: with SMTP id f2mr3017228pgr.375.1596636775668; Wed, 05 Aug 2020 07:12:55 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.56]) by smtp.gmail.com with ESMTPSA id b185sm3726551pfg.71.2020.08.05.07.12.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Aug 2020 07:12:55 -0700 (PDT) From: Yulei Zhang To: pbonzini@redhat.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, sean.j.christopherson@intel.com, jmattson@google.com, vkuznets@redhat.com, xiaoguangrong.eric@gmail.com, kernellwp@gmail.com, lihaiwei.kernel@gmail.com, Yulei Zhang Subject: [RFC 4/9] Add release function for direct build ept when guest VM exit Date: Wed, 5 Aug 2020 22:13:43 +0800 Message-Id: <20200805141344.9038-1-yulei.kernel@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yulei Zhang Release the pre-pinned memory in direct build ept when guest VM exit. Signed-off-by: Yulei Zhang --- arch/x86/kvm/mmu/mmu.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 539974183653..df703deac928 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4360,8 +4360,11 @@ static void __kvm_mmu_new_cr3(struct kvm_vcpu *vcpu, gpa_t new_cr3, void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu, gpa_t new_cr3, bool skip_tlb_flush) { - __kvm_mmu_new_cr3(vcpu, new_cr3, kvm_mmu_calc_root_page_role(vcpu), - skip_tlb_flush); + if (!vcpu->arch.direct_build_tdp) + __kvm_mmu_new_cr3(vcpu, new_cr3, kvm_mmu_calc_root_page_role(vcpu), + skip_tlb_flush); + else + vcpu->arch.mmu->root_hpa = INVALID_PAGE; } EXPORT_SYMBOL_GPL(kvm_mmu_new_cr3); @@ -5204,10 +5207,16 @@ EXPORT_SYMBOL_GPL(kvm_mmu_load); void kvm_mmu_unload(struct kvm_vcpu *vcpu) { - kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL); - WARN_ON(VALID_PAGE(vcpu->arch.root_mmu.root_hpa)); - kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); - WARN_ON(VALID_PAGE(vcpu->arch.guest_mmu.root_hpa)); + + if (!vcpu->arch.direct_build_tdp) { + kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, KVM_MMU_ROOTS_ALL); + WARN_ON(VALID_PAGE(vcpu->arch.root_mmu.root_hpa)); + kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); + WARN_ON(VALID_PAGE(vcpu->arch.guest_mmu.root_hpa)); + } + + vcpu->arch.direct_build_tdp = false; + vcpu->arch.mmu->root_hpa = INVALID_PAGE; } EXPORT_SYMBOL_GPL(kvm_mmu_unload); @@ -6510,6 +6519,14 @@ void kvm_direct_tdp_remove_page_table(struct kvm *kvm, struct kvm_memory_slot *s kvm_flush_remote_tlbs(kvm); } +void kvm_direct_tdp_release_global_root(struct kvm *kvm) +{ + if (kvm->arch.global_root_hpa) + __kvm_walk_global_page(kvm, kvm->arch.global_root_hpa, kvm_x86_ops.get_tdp_level(NULL)); + + return; +} + /* * Calculate mmu pages needed for kvm. */ @@ -6536,9 +6553,13 @@ unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm) void kvm_mmu_destroy(struct kvm_vcpu *vcpu) { - kvm_mmu_unload(vcpu); - free_mmu_pages(&vcpu->arch.root_mmu); - free_mmu_pages(&vcpu->arch.guest_mmu); + if (vcpu->arch.direct_build_tdp) { + vcpu->arch.mmu->root_hpa = INVALID_PAGE; + } else { + kvm_mmu_unload(vcpu); + free_mmu_pages(&vcpu->arch.root_mmu); + free_mmu_pages(&vcpu->arch.guest_mmu); + } mmu_free_memory_caches(vcpu); } -- 2.17.1