Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp265470rdf; Tue, 21 Nov 2023 01:58:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGtXKtF6DsaCCI8GsGN20ngGQCv35yULgg60jOWS1q1dD/aT9GKPbEw91I6amO540vfVbe1 X-Received: by 2002:a05:6a00:26c7:b0:68f:cbd3:5b01 with SMTP id p7-20020a056a0026c700b0068fcbd35b01mr2846522pfw.13.1700560690347; Tue, 21 Nov 2023 01:58:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700560690; cv=none; d=google.com; s=arc-20160816; b=WTsZ72EdFL/XTFnxGRpfIYcuuSd+MJyd7Zp1Q092Bv7tSNnyr5qc8eSdoJDhlDLhej mvDMhPC0DOBeoppIaPbtNIK4JZW+Z/t0NIreoKRLgbNfIuRawolL7gEzLcuNQo2YXTs0 BI1t7vqu/o1Fes325rhKCTZzawPRK8mZly63VrTf335+HaD8aA/ZAsYEPDmvqslHkIPK jF+3PUcIGjnZtcFxD3lDLYeVvwzpAKGc+DfjIFFPVllEXQ7sByEsL+z2Snnj8iLjpuJS 9b1m021KXkQQaGsN1aTedevsFvhzm/iq6aZ5WZIw0lcZODWH9LiXNVXJZDSW3rFCucUC U9Qw== 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:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=0rWn8NkUovO3lS6OmbN8VF8qOYE27Rg1yMjSEGHwwLw=; fh=SSl64FBK0Lhy77ytVxLm3gRT6oITPT9jTW5tmAf+4Dk=; b=cMJ2fXc5y/Gwpuk5r/EeYhmZy9Jdcx9+zpDzYoUm4C0QP6mfB4mpmOfFFjPZB+wCvm DxMqpdTH2WdpA1Zxvvo2ptG5MFQqDLJy8Zj23IuLidFDZeCCcN5IWxTfBhTuh7/PnNij 7amfEfnwoiSPNVUnjfRWXR5sJBiOGpin+L7WRjqGn2nzo2kfkOb1wbvzeNVHVh4TdqRG NL9n5gB7BgmFqpvJBiK9WvO41LzgPWxR0C99FdSbVF65Lw2v1xsUulgCY5oWG4W9+Vd/ XOT0SxvF4hT1aUwJb2wbn25oANlOH7SPVbKHYklD1j6w9OMrreyGKDaXnk6mgRROMslo b2+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="PhXR/yPz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id i10-20020a63d44a000000b005c21fcf96a2si7632967pgj.825.2023.11.21.01.58.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 01:58:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="PhXR/yPz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 229E38033DDF; Tue, 21 Nov 2023 01:58:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234108AbjKUJ5o (ORCPT + 99 others); Tue, 21 Nov 2023 04:57:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234176AbjKUJ5m (ORCPT ); Tue, 21 Nov 2023 04:57:42 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE899112; Tue, 21 Nov 2023 01:57:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700560654; x=1732096654; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=qFch2I4wb3I5lzRPVl9RrDhRU6eftq6hKyzQM7XrNCw=; b=PhXR/yPzzYYtVRB4z0ZIw6dBPviBXyq+hN7yPL0dbXgiCWLXYHAwanIL UvDJsbdJQbTmB3hd9/pFJfn7AEWMNhDLTqIeWMDv/MX0dqWD1WOC+s9CU PcH3dsKlKh6WF12udZlic8LMrlmF/WrbP6BgdEU6dD3MDgtC2q3889VJT M1eLzJDE5xu1ryV0poS4eYaZitj134RRE2TWz3L2Zcz5Q2CIO1byPqYLZ qgp8Y/l81SFFZCk1Sy58ZCahXq2ANONWM62z3ziA9YZOmM2fzyi9E3z6v 3H0otck6KSSk3TaXFJedAJHRHHltJQxmkLfS2klQD23pxi/+ZhzRceaT6 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10900"; a="13349006" X-IronPort-AV: E=Sophos;i="6.04,215,1695711600"; d="scan'208";a="13349006" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 01:57:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,215,1695711600"; d="scan'208";a="14867587" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.238.10.126]) ([10.238.10.126]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 01:57:30 -0800 Message-ID: <5d9aadbd-975b-4c4d-ba18-ac6e0fb07ba1@linux.intel.com> Date: Tue, 21 Nov 2023 17:57:28 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 11/16] KVM: x86/tdp_mmu: Split the large page when zap leaf To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Xiaoyao Li References: <8b43a9203c34b5330c4ea5901da5dac3458ac98d.1699368363.git.isaku.yamahata@intel.com> From: Binbin Wu In-Reply-To: <8b43a9203c34b5330c4ea5901da5dac3458ac98d.1699368363.git.isaku.yamahata@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,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 agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 21 Nov 2023 01:58:07 -0800 (PST) On 11/7/2023 11:00 PM, isaku.yamahata@intel.com wrote: > From: Xiaoyao Li > > When TDX enabled, a large page cannot be zapped if it contains mixed > pages. In this case, it has to split the large page. > > Signed-off-by: Xiaoyao Li > --- > arch/x86/kvm/Kconfig | 1 + > arch/x86/kvm/mmu/mmu.c | 6 +-- > arch/x86/kvm/mmu/mmu_internal.h | 9 +++++ > arch/x86/kvm/mmu/tdp_mmu.c | 68 +++++++++++++++++++++++++++++++-- > 4 files changed, 78 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig > index b0f103641547..557479737962 100644 > --- a/arch/x86/kvm/Kconfig > +++ b/arch/x86/kvm/Kconfig > @@ -93,6 +93,7 @@ config KVM_INTEL > tristate "KVM for Intel (and compatible) processors support" > depends on KVM && IA32_FEAT_CTL > select KVM_SW_PROTECTED_VM if INTEL_TDX_HOST > + select KVM_GENERIC_MEMORY_ATTRIBUTES if INTEL_TDX_HOST > select KVM_PRIVATE_MEM if INTEL_TDX_HOST > help > Provides support for KVM on processors equipped with Intel's VT > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 265177cedf37..0bf043812644 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -7463,8 +7463,8 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, > return kvm_unmap_gfn_range(kvm, range); > } > > -static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, > - int level) > +bool kvm_hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, > + int level) > { > return lpage_info_slot(gfn, slot, level)->disallow_lpage & KVM_LPAGE_MIXED_FLAG; > } > @@ -7491,7 +7491,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot, > return kvm_range_has_memory_attributes(kvm, start, end, attrs); > > for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { > - if (hugepage_test_mixed(slot, gfn, level - 1) || > + if (kvm_hugepage_test_mixed(slot, gfn, level - 1) || > attrs != kvm_get_memory_attributes(kvm, gfn)) > return false; > } > diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h > index 1da98be74ad2..653e96769956 100644 > --- a/arch/x86/kvm/mmu/mmu_internal.h > +++ b/arch/x86/kvm/mmu/mmu_internal.h > @@ -460,4 +460,13 @@ void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc); > void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp); > void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp); > > +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES > +bool kvm_hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, int level); > +#else > +static inline bool kvm_hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, int level) > +{ > + return false; > +} > +#endif > + > #endif /* __KVM_X86_MMU_INTERNAL_H */ > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 7873e9ee82ad..a209a67decae 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -964,6 +964,14 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp) > return true; > } > > + > +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, > + struct tdp_iter *iter, > + bool shared); > + > +static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter, > + struct kvm_mmu_page *sp, bool shared); > + > /* > * If can_yield is true, will release the MMU lock and reschedule if the > * scheduler needs the CPU or there is contention on the MMU lock. If this > @@ -975,13 +983,15 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, > gfn_t start, gfn_t end, bool can_yield, bool flush, > bool zap_private) > { > + bool is_private = is_private_sp(root); > + struct kvm_mmu_page *split_sp = NULL; > struct tdp_iter iter; > > end = min(end, tdp_mmu_max_gfn_exclusive()); > > lockdep_assert_held_write(&kvm->mmu_lock); > > - WARN_ON_ONCE(zap_private && !is_private_sp(root)); > + WARN_ON_ONCE(zap_private && !is_private); > if (!zap_private && is_private_sp(root)) Can use is_private instead of is_private_sp(root) here as well. > return false; > > @@ -1006,12 +1016,66 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, > !is_last_spte(iter.old_spte, iter.level)) > continue; > > + if (is_private && kvm_gfn_shared_mask(kvm) && > + is_large_pte(iter.old_spte)) { > + gfn_t gfn = iter.gfn & ~kvm_gfn_shared_mask(kvm); > + gfn_t mask = KVM_PAGES_PER_HPAGE(iter.level) - 1; > + struct kvm_memory_slot *slot; > + struct kvm_mmu_page *sp; > + > + slot = gfn_to_memslot(kvm, gfn); > + if (kvm_hugepage_test_mixed(slot, gfn, iter.level) || > + (gfn & mask) < start || > + end < (gfn & mask) + KVM_PAGES_PER_HPAGE(iter.level)) { > + WARN_ON_ONCE(!can_yield); > + if (split_sp) { > + sp = split_sp; > + split_sp = NULL; > + sp->role = tdp_iter_child_role(&iter); > + } else { > + WARN_ON(iter.yielded); > + if (flush && can_yield) { > + kvm_flush_remote_tlbs(kvm); > + flush = false; > + } Is it necessary to do the flush here? > + sp = tdp_mmu_alloc_sp_for_split(kvm, &iter, false); > + if (iter.yielded) { > + split_sp = sp; > + continue; > + } > + } > + KVM_BUG_ON(!sp, kvm); > + > + tdp_mmu_init_sp(sp, iter.sptep, iter.gfn); > + if (tdp_mmu_split_huge_page(kvm, &iter, sp, false)) { > + kvm_flush_remote_tlbs(kvm); > + flush = false; Why it needs to flush TLB immediately if tdp_mmu_split_huge_page() fails? Also, when KVM MMU write lock is held, it seems tdp_mmu_split_huge_page() will not fail. But let's assume this condition can be triggered, since sp is local variable, it will lost its value after continue, and split_sp is also NULL, it will try to allocate a new sp, memory leakage here? > + /* force retry on this gfn. */ > + iter.yielded = true; > + } else > + flush = true; > + continue; > + } > + } > + > tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); > flush = true; > } > > rcu_read_unlock(); > > + if (split_sp) { > + WARN_ON(!can_yield); > + if (flush) { > + kvm_flush_remote_tlbs(kvm); > + flush = false; > + } Same here, why we need to do the flush here? Can we delay it till the caller do the flush? > + > + write_unlock(&kvm->mmu_lock); > + tdp_mmu_free_sp(split_sp); > + write_lock(&kvm->mmu_lock); > + } > + > /* > * Because this flow zaps _only_ leaf SPTEs, the caller doesn't need > * to provide RCU protection as no 'struct kvm_mmu_page' will be freed. > @@ -1606,8 +1670,6 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, > > KVM_BUG_ON(kvm_mmu_page_role_is_private(role) != > is_private_sptep(iter->sptep), kvm); > - /* TODO: Large page isn't supported for private SPTE yet. */ > - KVM_BUG_ON(kvm_mmu_page_role_is_private(role), kvm); > > /* > * Since we are allocating while under the MMU lock we have to be