Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6637798rwd; Mon, 5 Jun 2023 23:03:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6xxSXOw4AVLyAWVF4iKmMIKmq552tmGSeDNyOu0h3L3g2GaYm1FjDl/wcaD+foJy3QjoBj X-Received: by 2002:a05:6a00:812:b0:643:8496:e41c with SMTP id m18-20020a056a00081200b006438496e41cmr1255170pfk.20.1686031434144; Mon, 05 Jun 2023 23:03:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686031434; cv=none; d=google.com; s=arc-20160816; b=akm2Bhgc7hXO2qfpSquxEPy7wn/Tr0S0pV5HUHPmYK8N0Gz18qGSMhSJ5Vg6jatCWL t7EBLZXY5dKvINzdK04SdeZTIA0Twe8y1H+NsoWR/eDrzspA/8IJ61BoSZGMKbFp1x5c jewsMQ4P5GbDTWDwtYqRPf6u5Pb3assClaYc10eKQSyZ61sCqmXzpBqwoILk6S7WLTfP wlSKWG96ivUtY1nwy7BMqU1eOPdEZQouTNcGbNF1kWCTdZcH05QxPqmkjGGktJ/ZP72E i1YE7RcydM/PfsKfgM/F+OhWwr+PQtqRzFYOg020syAzTH/wB5oJ79qUUpZ2fP4JN123 xBSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=Ovlfsl9JHO9gWavx7qoKLkvqP2nknS1b7FwhiU1p3kQ=; b=rJGD/UwQBmtZxmjwyt1daxlK42KRpewXiPqoihC/MwpMoxFznWggEsQas24KOcjScC o+9kSLtDfQu5Ca386sIeZiu8/fzcgnQxR6R0Ju8pOiQQZxh7qN0hByohrY7MlLus/FQQ 4NFaJRrsoFy+hKHXKuCFeSvrZRwroTdShYVyRp+0gUuTdmHLPismxaiN6GjH1sefap0b SZF3MgNGv7x/ahnlU1S2svkrAwfwZ9ZPqfGsKyFc/IGvuP3beJ4ylynTxlAax30BYunb 2Onp2R9npEl3GqsP4Dq+OlnHsS6dgO7DPu9bPmC6f21DRTZ4dWFKV3GUxRF7M0clSnw8 Dx/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Y1RFvSLW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id az9-20020a056a02004900b00542d69c5153si4955632pgb.405.2023.06.05.23.03.40; Mon, 05 Jun 2023 23:03:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Y1RFvSLW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231983AbjFFFbT (ORCPT + 99 others); Tue, 6 Jun 2023 01:31:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230121AbjFFFbQ (ORCPT ); Tue, 6 Jun 2023 01:31:16 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2373D1B1; Mon, 5 Jun 2023 22:31:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686029475; x=1717565475; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=6XLjfDvI/7YAdozf3uM1i/r0+n9p2mhEWmLgE3TeBTE=; b=Y1RFvSLW7KU5W4uSutDP/m3JIbucumr4fdzRCnVmUOIxkrAh8tufKG5U xsTuD7wzeKlZPmv2CJyWAuZxw8y9c+a5aO9b0Bp3d3yaQrevw/X/ublf4 R8AD/+EwfC7aSrgC2QbRZGieq3KY1yZMqA+Fp+Xp1p3MYfN+dNZxvbqbT JIXZWA2lGzbKQoWE1R40L8Yo3tLNQ3im+fP0aSRCo50MOIFYhWBDCW9vJ +eEY+/cKCyjzjDHuObzNIGoUoU1SB9eJDgkDU/Iotn7RHZfTigEGUijo+ Bh0Mk8VflrTxquF4/6pl+VaFOrTg3G/5SCLFElSoVDy89o029Ary4wWpz w==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="420115709" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="420115709" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2023 22:31:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="798687338" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="798687338" Received: from danwu1-mobl.ccr.corp.intel.com (HELO [10.238.6.21]) ([10.238.6.21]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2023 22:31:11 -0700 Message-ID: <2710f9c8-cddc-1441-f2b6-7d0616880f1d@intel.com> Date: Tue, 6 Jun 2023 13:31:04 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.11.2 Subject: Re: [PATCH v14 031/113] KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE Content-Language: en-US To: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, Sean Christopherson References: <8b4f21e2fada944d041ffee0f27d527e0e447cbb.1685333727.git.isaku.yamahata@intel.com> From: "Wu, Dan1" In-Reply-To: <8b4f21e2fada944d041ffee0f27d527e0e447cbb.1685333727.git.isaku.yamahata@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham 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 On 5/29/2023 12:19 PM, isaku.yamahata@intel.com wrote: > From: Isaku Yamahata > > The TDX support will need the "suppress #VE" bit (bit 63) set as the > initial value for SPTE. To reduce code change size, introduce a new macro > SHADOW_NONPRESENT_VALUE for the initial value for the shadow page table > entry (SPTE) and replace hard-coded value 0 for it. Initialize shadow page > tables with their value. > > The plan is to unconditionally set the "suppress #VE" bit for both AMD and > Intel as: 1) AMD hardware uses the bit 63 as NX for present SPTE and > ignored for non-present SPTE; 2) for conventional VMX guests, KVM never > enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is > ignored by hardware. > > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/mmu/mmu.c | 20 +++++++++++++++----- > arch/x86/kvm/mmu/paging_tmpl.h | 2 +- > arch/x86/kvm/mmu/spte.h | 2 ++ > arch/x86/kvm/mmu/tdp_mmu.c | 14 +++++++------- > 4 files changed, 25 insertions(+), 13 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index dc2b9a2f717c..1b6fd4434e96 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -576,9 +576,9 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) > > if (!is_shadow_present_pte(old_spte) || > !spte_has_volatile_bits(old_spte)) > - __update_clear_spte_fast(sptep, 0ull); > + __update_clear_spte_fast(sptep, SHADOW_NONPRESENT_VALUE); > else > - old_spte = __update_clear_spte_slow(sptep, 0ull); > + old_spte = __update_clear_spte_slow(sptep, SHADOW_NONPRESENT_VALUE); > > if (!is_shadow_present_pte(old_spte)) > return old_spte; > @@ -612,7 +612,7 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) > */ > static void mmu_spte_clear_no_track(u64 *sptep) > { > - __update_clear_spte_fast(sptep, 0ull); > + __update_clear_spte_fast(sptep, SHADOW_NONPRESENT_VALUE); > } > > static u64 mmu_spte_get_lockless(u64 *sptep) > @@ -1969,7 +1969,8 @@ static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) > > static int kvm_sync_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int i) > { > - if (!sp->spt[i]) > + /* sp->spt[i] has initial value of shadow page table allocation */ > + if (sp->spt[i] != SHADOW_NONPRESENT_VALUE) this condition should be "sp->spt[i] == SHADOW_NONPRESENT_VALUE"? I found this cause the bug of  "guest launch failure with EPT disable". > return 0; > > return vcpu->arch.mmu->sync_spte(vcpu, sp, i); > @@ -6120,7 +6121,16 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) > vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; > vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; > > - vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; > + /* > + * When X86_64, initial SEPT entries are initialized with > + * SHADOW_NONPRESENT_VALUE. Otherwise zeroed. See > + * mmu_memory_cache_alloc_obj(). > + */ > + if (IS_ENABLED(CONFIG_X86_64)) > + vcpu->arch.mmu_shadow_page_cache.init_value = > + SHADOW_NONPRESENT_VALUE; > + if (!vcpu->arch.mmu_shadow_page_cache.init_value) > + vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; > > vcpu->arch.mmu = &vcpu->arch.root_mmu; > vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; > diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h > index 0662e0278e70..ef8124bd2f11 100644 > --- a/arch/x86/kvm/mmu/paging_tmpl.h > +++ b/arch/x86/kvm/mmu/paging_tmpl.h > @@ -892,7 +892,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int > gpa_t pte_gpa; > gfn_t gfn; > > - if (WARN_ON_ONCE(!sp->spt[i])) > + if (WARN_ON_ONCE(sp->spt[i] == SHADOW_NONPRESENT_VALUE)) > return 0; > > first_pte_gpa = FNAME(get_level1_sp_gpa)(sp); > diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h > index 1279db2eab44..a99eb7d4ae5d 100644 > --- a/arch/x86/kvm/mmu/spte.h > +++ b/arch/x86/kvm/mmu/spte.h > @@ -148,6 +148,8 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11); > > #define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) > > +#define SHADOW_NONPRESENT_VALUE 0ULL > + > extern u64 __read_mostly shadow_host_writable_mask; > extern u64 __read_mostly shadow_mmu_writable_mask; > extern u64 __read_mostly shadow_nx_mask; > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 3000ef6d79ea..ddd995885dd3 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -627,7 +627,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm, > * here since the SPTE is going from non-present to non-present. Use > * the raw write helper to avoid an unnecessary check on volatile bits. > */ > - __kvm_tdp_mmu_write_spte(iter->sptep, 0); > + __kvm_tdp_mmu_write_spte(iter->sptep, SHADOW_NONPRESENT_VALUE); > > return 0; > } > @@ -764,8 +764,8 @@ static void __tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root, > continue; > > if (!shared) > - tdp_mmu_iter_set_spte(kvm, &iter, 0); > - else if (tdp_mmu_set_spte_atomic(kvm, &iter, 0)) > + tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); > + else if (tdp_mmu_set_spte_atomic(kvm, &iter, SHADOW_NONPRESENT_VALUE)) > goto retry; > } > } > @@ -821,8 +821,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp) > if (WARN_ON_ONCE(!is_shadow_present_pte(old_spte))) > return false; > > - tdp_mmu_set_spte(kvm, kvm_mmu_page_as_id(sp), sp->ptep, old_spte, 0, > - sp->gfn, sp->role.level + 1); > + tdp_mmu_set_spte(kvm, kvm_mmu_page_as_id(sp), sp->ptep, old_spte, > + SHADOW_NONPRESENT_VALUE, sp->gfn, sp->role.level + 1); > > return true; > } > @@ -856,7 +856,7 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, > !is_last_spte(iter.old_spte, iter.level)) > continue; > > - tdp_mmu_iter_set_spte(kvm, &iter, 0); > + tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); > flush = true; > } > > @@ -1250,7 +1250,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_iter_set_spte(kvm, iter, 0); > + tdp_mmu_iter_set_spte(kvm, iter, SHADOW_NONPRESENT_VALUE); > > if (!pte_write(range->pte)) { > new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,