Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2279139pxm; Fri, 4 Mar 2022 13:00:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJxEY0AaPPyzbIqcVoYVvry0YTcOYQSdRdL5sjhlEPCL3shhdDytf+N5mXZ2ork7N+/yp+p/ X-Received: by 2002:a17:903:110d:b0:14e:ea6c:7086 with SMTP id n13-20020a170903110d00b0014eea6c7086mr217849plh.0.1646427601920; Fri, 04 Mar 2022 13:00:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646427601; cv=none; d=google.com; s=arc-20160816; b=HDMF+z7fqK7xoGYX0W63S/I+xmPV7Z8YkQfhNhjrvQ4738WQKYbYRoL5dUxkI1nXgE aq/wRhf8NVy2dKTTVDQe2Ekk0+hOhJ1mZrbupvA7jjT4N8u1WxLwQhWk8O0+yDdVUXuq mRMEbg+QTE7n5zuC6Z682yp+TH6EZRjOe1xziuN+V3/EBGjlg7WcxO92NPvEfYJ8xJxK wccEBHQc8vNYT9zxuD3ocWmY0VDQ1lgkhCSB9kqn+zJEk/aKm+3+TSLWld8bvRUso4Xz QYrXqDEJ+jR/3Xn2ru8wBg6ETE1pKvm+ASWH82mEkMYtPvGGW6PcMGn17jYEbsLI/mfo n11Q== 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=wFabsFvfoYi4KCayeimDFJgGd7OHBdgpFnuV6VY8BaU=; b=Yy18dH1DX2ZVpoEdFD3FWumMAB3L+tFw/0FQJZDvsCr40Vl+rFXi91FwEExpBzFI36 aJTYf3yW964qpc4uRl4jO/R9KiIsr2xjCZNNfxVSagu2McXmcsxqq44NsQQlXjvU/9Em JbnF3VydK6/r8Bmh7Dwtd7VsqBUrAJuZjVQ0Op/8ITKdswriKuda7jlnm54uW3m+F0Nu WaZGmwoEVrdL4FpmCuhe03XzbE2DQ7JPA3DXOnxPM+F+JT5gezV8zIOd9YF+DhmtfVWC 0N8r+NpaqpfG36iNEIpVeUNMxy2FkCdN/ue8oopC6ixHCkTvf4/9uHD6wVhjhevc/FPP TNnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=H9UKrRxb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d6-20020a170903230600b0014f56989222si6032939plh.121.2022.03.04.13.00.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 13:00:01 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=H9UKrRxb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3D7C72CFBAE; Fri, 4 Mar 2022 12:10:39 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229769AbiCDUKj (ORCPT + 99 others); Fri, 4 Mar 2022 15:10:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231210AbiCDUHz (ORCPT ); Fri, 4 Mar 2022 15:07:55 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2D171C9B57; Fri, 4 Mar 2022 12:02:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646424142; x=1677960142; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gRL60Eh24dbvczWyyL0w5AiCqy8X0iQtaAcE9wJT7Ow=; b=H9UKrRxbji5m1e6WhyjJpKIrodHIVicIgUHgMm6Y1aI4TBl51gEbw60E s2qz+XN7VxT+zMbc1lNpbeS1bM3lVCKCxoH16QKudaIQqwNGg7vaqJpbI yB5E6/PQwZ4dWF8UnwKvCyjowTDRgadcz9gzrKI3yICnAVpjDnYCwUF7b 3ZmxMX5B8ehadY+V9ED/MJKPJX04DIesb3+UDozaTv7LyZGzxZeSH5ql3 rhLgLTaf4z+bAle7Oo4eqQb0RHNiTMA6rlYoXEIAPoNOTMO/sZvOc1Bc9 8wBxcmEM8VZJgwiYvI4Hrknu06JgAUTIXyX+4b4kQEXWZsx40LnSjh4Nx w==; X-IronPort-AV: E=McAfee;i="6200,9189,10276"; a="253983499" X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="253983499" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:23 -0800 X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="552344348" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:23 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson Subject: [RFC PATCH v5 045/104] KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value Date: Fri, 4 Mar 2022 11:49:01 -0800 Message-Id: <6614d2a2bc34441ed598830392b425fdf8e5ca52.1646422845.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Isaku Yamahata TDP MMU uses REMOVED_SPTE = 0x5a0ULL as special constant to indicate the intermediate value to indicate one thread is operating on it and the value should be semi-arbitrary value. For TDX (more correctly to use #VE), the value should include suppress #VE value which is shadow_init_value. Define SHADOW_REMOVED_SPTE as shadow_init_value | REMOVED_SPTE, and replace REMOVED_SPTE with SHADOW_REMOVED_SPTE to use suppress #VE bit properly for TDX. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/spte.h | 14 ++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 23 ++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index bde843bce878..e88f796724b4 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -194,7 +194,9 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask; * If a thread running without exclusive control of the MMU lock must perform a * multi-part operation on an SPTE, it can set the SPTE to REMOVED_SPTE as a * non-present intermediate value. Other threads which encounter this value - * should not modify the SPTE. + * should not modify the SPTE. When TDX is enabled, shadow_init_value, which + * is "suppress #VE" bit set, is also set to removed SPTE, because TDX module + * always enables "EPT violation #VE". * * Use a semi-arbitrary value that doesn't set RWX bits, i.e. is not-present on * bot AMD and Intel CPUs, and doesn't set PFN bits, i.e. doesn't create a L1TF @@ -207,9 +209,17 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask; /* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert(!(REMOVED_SPTE & SPTE_MMU_PRESENT_MASK)); +/* + * See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual + * intermediate value set to the removed SPET. When TDX is enabled, it sets + * the "suppress #VE" bit, otherwise it's REMOVED_SPTE. + */ +extern u64 __read_mostly shadow_init_value; +#define SHADOW_REMOVED_SPTE (shadow_init_value | REMOVED_SPTE) + static inline bool is_removed_spte(u64 spte) { - return spte == REMOVED_SPTE; + return spte == SHADOW_REMOVED_SPTE; } /* diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index ebd0a02620e8..b6ec2f112c26 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -338,7 +338,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt, * value to the removed SPTE value. */ for (;;) { - old_child_spte = xchg(sptep, REMOVED_SPTE); + old_child_spte = xchg(sptep, SHADOW_REMOVED_SPTE); if (!is_removed_spte(old_child_spte)) break; cpu_relax(); @@ -365,10 +365,10 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt, * the two branches consistent and simplifies * the function. */ - WRITE_ONCE(*sptep, REMOVED_SPTE); + WRITE_ONCE(*sptep, SHADOW_REMOVED_SPTE); } handle_changed_spte(kvm, kvm_mmu_page_as_id(sp), gfn, - old_child_spte, REMOVED_SPTE, level, + old_child_spte, SHADOW_REMOVED_SPTE, level, shared); } @@ -537,7 +537,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm, * immediately installing a present entry in its place * before the TLBs are flushed. */ - if (!tdp_mmu_set_spte_atomic(kvm, iter, REMOVED_SPTE)) + if (!tdp_mmu_set_spte_atomic(kvm, iter, SHADOW_REMOVED_SPTE)) return false; kvm_flush_remote_tlbs_with_address(kvm, iter->gfn, @@ -550,8 +550,16 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm, * special removed SPTE value. No bookkeeping is needed * here since the SPTE is going from non-present * to non-present. + * + * Set non-present value to shadow_init_value, rather than 0. + * It is because when TDX is enabled, TDX module always + * enables "EPT-violation #VE", so KVM needs to set + * "suppress #VE" bit in EPT table entries, in order to get + * real EPT violation, rather than TDVMCALL. KVM sets + * shadow_init_value (which sets "suppress #VE" bit) so it + * can be set when EPT table entries are zapped. */ - WRITE_ONCE(*rcu_dereference(iter->sptep), 0); + WRITE_ONCE(*rcu_dereference(iter->sptep), shadow_init_value); return true; } @@ -748,7 +756,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, continue; if (!shared) { - tdp_mmu_set_spte(kvm, &iter, 0); + /* see comments in tdp_mmu_zap_spte_atomic() */ + tdp_mmu_set_spte(kvm, &iter, shadow_init_value); flush = true; } else if (!tdp_mmu_zap_spte_atomic(kvm, &iter)) { /* @@ -1135,7 +1144,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter, * invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ - tdp_mmu_set_spte(kvm, iter, 0); + tdp_mmu_set_spte(kvm, iter, shadow_init_value); if (!pte_write(range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte, -- 2.25.1