Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp189927rdb; Mon, 22 Jan 2024 17:04:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeXTqWKHt0p9uVmnqCPYc7MgIYfUlWr4WTgiJcftRjVjc4hJ+Do40lpGpCAONYa1xl70UM X-Received: by 2002:a05:6808:284:b0:3bb:c6a0:a4fc with SMTP id z4-20020a056808028400b003bbc6a0a4fcmr5219601oic.62.1705971871954; Mon, 22 Jan 2024 17:04:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705971871; cv=pass; d=google.com; s=arc-20160816; b=nlUTvklk1f+fBRtufzTSSgKJwzjhTZBkNIp3cJ0pAEO1hvwLYPexpVLtJPc+aK8h/4 bbUF81T/osj/5P3pgz0bPfqDdw03vHAjl35NoRzU68Clv457CKYS5K7CSz5tJhbYW1tO 0mQijdVMOv5mBztf1EUT0dZIUXyipUVBuNcvHr1D1KlwBmLU7njjmkd95YlmeOsJmVpu AKioDJTju6aUlvWnekW3ZG4oD2Bbk93F5oTOMBe7S/d8Zg7y5UTFSgS17cGzmxYXTPae ubpXqVIVEyo2/7nAD7n1hUYEbTtHdRKTkgFiDNfy8OT+t9PROF7MpTPCOZ+RrzXcenCQ IVGw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=BVpcl7HA3yMbaPaFFAvcdqcVQJFz78L3N7cJP+h/VHg=; fh=sMdP/xP2j0mAr7mmqXKi/DmIqVqVXObmf3aqGFg9BJU=; b=Y3vGY7jLd0mpecD2eZ1Fdy2Gx4rDmuJVWF33z526V4O+U6M4ldhKRdG6YNtgzDsyp7 ERBl/ccC7iqie/9/0NuAFr9PxsUCsCKdvug5qk4IVQhH7Bz3bWEj6cKkKEEYA64jauS7 Cz9bJmEXNSsSmVAsh7BzCScskzX4Pj/SR5AldDavogTRhq7+vjdVdW6lBYU9WCaAkXgr tWn3+jGH0AnikUp6wZ7eyA03XSvmJo7v3Rn29Gg7S/V4ZCqwinyKDz4btwPzlIjlba+r ujWdPJVYYio1S2x0XkWrgn6cvgmTC45/dTm9GPDbmaMUuuxqUjPPo/ss+rbQxRIUYe9C tl5w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Yi1w5Rix; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34455-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34455-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id cz54-20020a05620a36f600b007759f94727asi6527269qkb.271.2024.01.22.17.04.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 17:04:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34455-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Yi1w5Rix; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34455-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34455-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B12A51C27B7B for ; Tue, 23 Jan 2024 01:04:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A57E21509BF; Tue, 23 Jan 2024 00:22:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Yi1w5Rix" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D237114F538; Tue, 23 Jan 2024 00:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=134.134.136.65 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705969364; cv=none; b=L7VqMbZBR9nwbWOFaXv1N3tS1epo5z9ikCBYl82Rhwh6ghIP1hXkb7UdYY6kbgdvcK/0KIRG/s4SAb6I7Ar1bBDZL46sFAcYc5jvlTo07gjLoJY4eOW/nuyK+8wuXXc6QPtQDrizBWwEKWco4MxiVAmbCmnDrPqmGqbPpk0SLcI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705969364; c=relaxed/simple; bh=KX7PJQwd4bEJKGoSjmNKx7nJ7E6cYVc8W8UDuuUN2zY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CDwfIUsAoQ0F8gJ6BTEBOGCy2sCbtmnKSP9Pf4gC6dt+bpFpACd0TdpXNVq6WlrSQGZjF2DRJAXVxeQKnXAuQg1tqGrat52ru9CR2+G1/QFfh3nFQnCAavqNSq3qSvMxHyUndWPCuWHVcjpaLshbyIXu5NvdPfscCAutNWh6XUU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Yi1w5Rix; arc=none smtp.client-ip=134.134.136.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705969362; x=1737505362; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KX7PJQwd4bEJKGoSjmNKx7nJ7E6cYVc8W8UDuuUN2zY=; b=Yi1w5Rixo2RgUZVXHQAPoKv//Bh9FTgkQugbcwJZtAcdDwNjLltqB8gY bmcklW7DYl6kl9A8ON9BAX1VlP07onsilZneZxQJ30i2VgJNcAu1FQqxX 8JIu5a6EHRlYxsEzsZrxqBSJgIUPHkcBLpUxUbx8GfTnqadEC0+7XvbnA MYPTr2v7OjGZZBqKwDjXdAB+q4R0E5A1A7DrO59o6ERIJ2dJpfpGvIMIf dSmrgdhXHYLb/jZZVAtE3zgbBS1znZ+//qCJkR+8zkSJREegzbiAswWF8 dTS7pxcRa9KjKGJYMeVul7eBetBNYYEA0HRUQJcAIXeiO9iQ8/bwnXQ8q A==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="405125681" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="405125681" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 16:22:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="27825653" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 16:22:39 -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 , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Xiaoyao Li Subject: [PATCH v7 08/13] KVM: x86/tdp_mmu: Split the large page when zap leaf Date: Mon, 22 Jan 2024 16:22:23 -0800 Message-Id: <3391bf2cf96df8744e0abc023d8af9ec677ee4e8.1705965958.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- v7: - remote unnecessary tlb shoot down in tdp_mmu_zap_leafs() to free unused split_sp. --- 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 | 60 ++++++++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index fa00abb9ab39..1aa37d494ae9 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -89,6 +89,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 971dbd9c95cc..a9e7a3d2d362 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7461,8 +7461,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; } @@ -7489,7 +7489,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 9888ea0046ea..cc0a95e554b5 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -461,4 +461,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 7991934b3f37..98de2c093815 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -953,6 +953,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 @@ -964,14 +972,16 @@ 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)); - if (!zap_private && is_private_sp(root)) + WARN_ON_ONCE(zap_private && !is_private); + if (!zap_private && is_private) return false; /* @@ -995,12 +1005,56 @@ 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; + } + 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)) { + /* force retry on this gfn. */ + iter.yielded = true; + split_sp = sp; + } else + flush = true; + continue; + } + } + tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); flush = true; } rcu_read_unlock(); + if (split_sp) + tdp_mmu_free_sp(split_sp); + /* * 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. @@ -1617,8 +1671,6 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, kvm_lockdep_assert_mmu_lock_held(kvm, shared); 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 -- 2.25.1