Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1172342rbb; Mon, 26 Feb 2024 00:31:47 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWysSyct1MTA3Ph1VMiomrnKcRHoi+n2CeR9MeebYhY/OqansYTBbz+/EkHmiW7cJSzJ7yoMFOTGJITiDkAQQ1GbJFKmmIvQ6Z8UA+/sg== X-Google-Smtp-Source: AGHT+IFy2Y7QH1wdjgiNW+/MgwUlqarvYC8eWZrepyRbuv5f4QsZzdH/D8V746Ej36PhOE6/eHut X-Received: by 2002:a05:6359:458d:b0:17b:602f:24c6 with SMTP id no13-20020a056359458d00b0017b602f24c6mr7782838rwb.24.1708936306843; Mon, 26 Feb 2024 00:31:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708936306; cv=pass; d=google.com; s=arc-20160816; b=Dzy1fCjQNbXBCNZqDkAd1W1qtHvDJSP7IR8iPtLOw09+Hgh5hLJDoTU3CqY74dMgE6 0MLNRPvzNfdxOa+STur1RQ/d9QDhMMqhjcqwnDIXr5VNws0JVPgFTZ1idNYOFCqZoE1C EWKnGnAeOaBkO+SVKks1eqHPtO7wiDyLtztfFkT+m6lWGfCItG1L0y3nd3mVdk9Snrtb o4j5nOFiMGaSapGPxp9vAwsV0XYy/tKLyW3Mc9zJSkdyLrnaxPWyqAqOZgxEkF4gHOao vQuGSSQjrvDM009xxm2znyGn6kZRgu8k1iFolj2H2/4iaRkeJbIQ9RhrV3t88Na60kc/ 0MOg== 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=qmBP1gE/ZdkgN99GsseGMnfI4Nqb2JTQs2tZjVywtT8=; fh=i2Ly18AIEfvpNZ6elzO3BWrYKB5IKhsO07/Ad+Lw4G4=; b=qWo5oNQOrpWrSfvM2ucuQIilLpYIIbyT+W3zAA9dlLgFtnfci7QSjqn9ixHESwstIV WjVSIhfeXJMe+c40X6HST0dW8TV5U8l96BTHeSUq6Vb5T6BzL1FnS1ObyIK7DmJbFd9L yduETMdz8iLGx/0yUksVrpRq8Ha7/KSiz8+Ga8A2NcS9iGbdUErhUjpolL7akT9FiaJh IkOzRADLv7VAfPzyQ8lFQfQx4K8uor2duNnj2HWm/9RGWpRN9oG0ZOnTZhWqX3F8kBJg oaLR6LFipt5C3BPyebbOoandDX9h+4WBWhMapJYKj79UL+50HfgUJ0isRXmhBs4Uag7j Y6LA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BNhxpvNl; 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-80770-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80770-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u26-20020a63471a000000b005dc9bea60fdsi104659pga.415.2024.02.26.00.31.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:31:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80770-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BNhxpvNl; 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-80770-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80770-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 23364283C3F for ; Mon, 26 Feb 2024 08:31:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC6874EB3A; Mon, 26 Feb 2024 08:27:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BNhxpvNl" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 4B2833399F; Mon, 26 Feb 2024 08:27:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936064; cv=none; b=LFqCZsoEDvOEruwIaPm7Q7+tbZXTjJdT97+0oVBGdyiDmOzg18Y2yvd0uTlW1Otl+6Naxm28HPCO45Filkbv93wBgZ3CMvr67SmWIkjS4i2HOZbLl0x1HCmPvdf393Yyw33a+QJfo2Yl0edqdaRguaestetLk5IooeQRhQW60u8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936064; c=relaxed/simple; bh=eTShvvspjk0PvYC7qMm7VTWj6uInBYa2cEf1cy02sbw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pqsRv15flTfQjO+II747ujtr2QSQaNh/5sZjXOylDgJEKbW2FXgi9zO0uWZkRI8S4lv1HodaI61+H4YpCdhaOGx5wMpE2HFyUR1cwH0OPOc6GFRcHxZes7bKgHCNIO+iVf4JQ68GMAZJyfRTiQTm0lGyIoDN/HRWqA6FqfiW+rc= 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=BNhxpvNl; arc=none smtp.client-ip=192.198.163.7 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=1708936062; x=1740472062; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eTShvvspjk0PvYC7qMm7VTWj6uInBYa2cEf1cy02sbw=; b=BNhxpvNlISeVeAN8JOQRIlxodbDlIwBSPD+7CKQ5T2FMbOn0+u2SVKrx Ew1372MycZrT3Ax2cVSPDel1OYtCv6TVD954/A+nmuZEZqg1TQ3ZiPQm6 Gf202aKtVHkPscp3jNI7lIO1k4vIS1E3VaxRddiAmgHqGWzsD+yMKYV4l a09w/zeZ401Sq8weILT/ulNXcic796C1uaRF++vzCmn/RwozkeRdQ7Rx9 dmxuaCNMXueM7hi3qWGeKmemuFJm0XyZJ3Hv92UKt9kOnAJQAXaCtIKC3 Jbn+H32qMeOWNoiWlycfe8cUnDJCx3YVQ1U1P+YCe86r5alhMzl8YTJ8x Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="28631460" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="28631460" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6474316" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmviesa007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27: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, Michael Roth Subject: [PATCH v19 009/130] KVM: x86: Add gmem hook for determining max NPT mapping level Date: Mon, 26 Feb 2024 00:25:11 -0800 Message-Id: <7ef616293093a94809625432f7855aa7da958492.1708933498.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: Michael Roth In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance: - gmem allocates 2MB page - guest issues PVALIDATE on 2MB page - guest later converts a subpage to shared - SNP host code issues PSMASH to split 2MB RMP mapping to 4K - KVM MMU splits NPT mapping to 4K At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this. Add a hook to determine the max NPT mapping size in situations like this. Signed-off-by: Michael Roth Link: https://lore.kernel.org/r/20231230172351.574091-31-michael.roth@amd.com Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 12 ++++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 378ed944b849..156832f01ebe 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -138,6 +138,7 @@ KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) +KVM_X86_OP_OPTIONAL_RET0(gmem_max_level) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d271ba20a0b2..d15f5b4b1656 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1796,6 +1796,8 @@ struct kvm_x86_ops { unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags); + + int (*gmem_max_level)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2d6cdeab1f8a..1e5e12d2707d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4308,6 +4308,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { int max_order, r; + u8 max_level; if (!kvm_slot_can_be_private(fault->slot)) { kvm_mmu_prepare_memory_fault_exit(vcpu, fault); @@ -4321,8 +4322,15 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, return r; } - fault->max_level = min(kvm_max_level_for_order(max_order), - fault->max_level); + max_level = kvm_max_level_for_order(max_order); + r = static_call(kvm_x86_gmem_max_level)(vcpu->kvm, fault->pfn, + fault->gfn, &max_level); + if (r) { + kvm_release_pfn_clean(fault->pfn); + return r; + } + + fault->max_level = min(max_level, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); return RET_PF_CONTINUE; -- 2.25.1