Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp311840rwb; Tue, 25 Jul 2023 16:30:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlGofTQ2Peci8ybCV0AKw45Ukzd8CoPo+EmwZwdrqc6e36A4L54BqKvb4aiHLiwZ6bKVwyIe X-Received: by 2002:a17:903:2287:b0:1bb:9f07:5e0 with SMTP id b7-20020a170903228700b001bb9f0705e0mr767367plh.60.1690327844751; Tue, 25 Jul 2023 16:30:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690327844; cv=none; d=google.com; s=arc-20160816; b=LpWYSr60i6owE2ONdkNzmeNicz5F/itw1D5QXj6p4Otw3VFwLFVbZ3WK6Lh2bbP+Dx jaTi0A/1+JEbvquITpstJfGPkfg/A6INd5LjR+Q153LFXGKbPb5/DvqsGKioxV2oTrqD nSrDs48VwaWWPThGfnDS3ldC+53HPDks90CmQO/JPTZtSwS6b0ZTQwtNrs6wUV/XTryu oEwiAz/j/18jw0BoZSTh1zWAM1u0EbOVYrsoEQqQeQJg3hr/cib6Np2Chnl6muziYrKo 2Ldc/uYlvKNjYCMvBc/Pe8g3OLLftINu2LSLgy0d/BF31REDfZ26riiVEQry9O6x9RMg KbOw== 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=mVW6h1pOVZtP3As8LaHMjlGC+IRypZccve03Yt46djM=; fh=juxwNcA6iKwLISFgUCNwdoIYC0NKLUseq3xZdq25RR4=; b=Y77VNQuzAIBlZCQgOWe28VvjSoE9lGth6G9vd7QCdVT0DkSJxMop5A22t3DpC8aulE ZKUorcfjFQpGf82SWnCYV6ENXO3UHdXcpWdfkWbtVNddvQgOMqdB22kG53ox9tlJKUvP 8wPqkiDZe7CAkzSGaSuA/pBFInjHTIfBKpsvZJjZPe4aiG+c8O+zGl3FXhbModVSW8MB vbshDq58SKQCwpbbV2BvRQMDlOGpRRMclgU1KIXsUtaZMv08bBix+S3P7MJPi6dsk88e 9nUfq7zdiWIC+kJh2TeM9OF9Tr7U+3+QWQ9fASeJ+MvrHxbkgD+hCUfZuL3itXUgB58N 9EXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DYwqxd6f; 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 f5-20020a170902860500b001b8b2865f38si11668263plo.170.2023.07.25.16.30.32; Tue, 25 Jul 2023 16:30:44 -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=DYwqxd6f; 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 S231831AbjGYWg4 (ORCPT + 99 others); Tue, 25 Jul 2023 18:36:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231820AbjGYWgA (ORCPT ); Tue, 25 Jul 2023 18:36:00 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FB1E83EC; Tue, 25 Jul 2023 15:29:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690324168; x=1721860168; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r+E6pH58+NMI4eOdU+xrarS94XBq53qfcCPtFd0hZa8=; b=DYwqxd6fQiJF8ARnfjZahUC3oCyQ2HRyIW4Bb11ougVKJssknlb19Q+y bA6oEYEIPqmqorClXNBrtYNyek3C6MBQ07Sj88kM7s1Yd7fS+koz0gUOm E/5YbfO62KIwazEzzylgh/CALsbMCx6rh+Crpc3Ml4T1+ePyIXuYDBLpx 1FzDIF5BmxGUZrg1i36TpU9q9bHIt7gXFCiJmH0hZiqra3tNMe+VVqY+F ScJalDqykUg3vWjWr3LryIBaKHVBQKxkjoqAtg7e12UF8BfHnetBvWSqf YnQYlcJoFztiNwdCb9fUniwTCHwaYNQHPJLm+t+Kw+mzF+i9vl2VMYU2S g==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="371467169" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="371467169" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:24:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="972855844" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="972855844" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:24:14 -0700 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 , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [RFC PATCH v4 15/16] KVM: x86/mmu: Make kvm fault handler aware of large page of private memslot Date: Tue, 25 Jul 2023 15:24:01 -0700 Message-Id: <765736cf093e2948b67845c6263a3bcb74c990df.1690323516.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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 From: Isaku Yamahata struct kvm_page_fault.req_level is the page level which takes care of the faulted-in page size. For now its calculation is only for the conventional kvm memslot by host_pfn_mapping_level() that traverses page table. However, host_pfn_mapping_level() cannot be used for private kvm memslot because pages of private kvm memlost aren't mapped into user virtual address space. Instead page order is given when getting pfn. Remember it in struct kvm_page_fault and use it. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++-------------- arch/x86/kvm/mmu/mmu_internal.h | 12 +++++++++++- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 949ef2fa8264..bb828eb2b1e3 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3190,10 +3190,10 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, static int __kvm_mmu_max_mapping_level(struct kvm *kvm, const struct kvm_memory_slot *slot, - gfn_t gfn, int max_level, bool is_private) + gfn_t gfn, int max_level, int host_level, + bool is_private) { struct kvm_lpage_info *linfo; - int host_level; max_level = min(max_level, max_huge_page_level); for ( ; max_level > PG_LEVEL_4K; max_level--) { @@ -3202,24 +3202,23 @@ static int __kvm_mmu_max_mapping_level(struct kvm *kvm, break; } - if (is_private) - return max_level; - if (max_level == PG_LEVEL_4K) return PG_LEVEL_4K; - host_level = host_pfn_mapping_level(kvm, gfn, slot); + if (!is_private) { + WARN_ON_ONCE(host_level != PG_LEVEL_NONE); + host_level = host_pfn_mapping_level(kvm, gfn, slot); + } + WARN_ON_ONCE(host_level == PG_LEVEL_NONE); return min(host_level, max_level); } int kvm_mmu_max_mapping_level(struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn, - int max_level) + int max_level, bool faultin_private) { - bool is_private = kvm_slot_can_be_private(slot) && - kvm_mem_is_private(kvm, gfn); - - return __kvm_mmu_max_mapping_level(kvm, slot, gfn, max_level, is_private); + return __kvm_mmu_max_mapping_level(kvm, slot, gfn, max_level, + PG_LEVEL_NONE, faultin_private); } void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) @@ -3244,7 +3243,8 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault */ fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot, fault->gfn, fault->max_level, - fault->is_private); + fault->host_level, + kvm_is_faultin_private(fault)); if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed) return; @@ -4391,6 +4391,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, return r; } + fault->host_level = max_level; fault->max_level = min(max_level, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); return RET_PF_CONTINUE; @@ -4440,7 +4441,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault return kvm_do_memory_fault_exit(vcpu, fault); } - if (fault->is_private && kvm_slot_can_be_private(slot)) + if (kvm_is_faultin_private(fault)) return kvm_faultin_pfn_private(vcpu, fault); if (fault->is_private && !kvm_slot_can_be_private(fault->slot)) @@ -6922,7 +6923,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, */ if (sp->role.direct && sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn, - PG_LEVEL_NUM)) { + PG_LEVEL_NUM, false)) { kvm_zap_one_rmap_spte(kvm, rmap_head, sptep); if (kvm_available_flush_remote_tlbs_range()) diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index bc3d38762ace..556c6ceec15f 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -359,6 +359,9 @@ struct kvm_page_fault { * is changing its own translation in the guest page tables. */ bool write_fault_to_shadow_pgtable; + + /* valid only for private memslot && private gfn */ + enum pg_level host_level; }; int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); @@ -455,7 +458,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, int kvm_mmu_max_mapping_level(struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn, - int max_level); + int max_level, bool faultin_private); void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_level); @@ -473,4 +476,11 @@ static inline bool kvm_hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t g } #endif +static inline bool kvm_is_faultin_private(const struct kvm_page_fault *fault) +{ + if (IS_ENABLED(CONFIG_KVM_GENERIC_PRIVATE_MEM)) + return fault->is_private && kvm_slot_can_be_private(fault->slot); + return false; +} + #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 612fcaac600d..6f22e38e3973 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -2176,7 +2176,7 @@ static void zap_collapsible_spte_range(struct kvm *kvm, continue; max_mapping_level = kvm_mmu_max_mapping_level(kvm, slot, - iter.gfn, PG_LEVEL_NUM); + iter.gfn, PG_LEVEL_NUM, false); if (max_mapping_level < iter.level) continue; -- 2.25.1