From patchwork Wed May 1 08:51:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650346 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74A01C4345F for ; Wed, 1 May 2024 09:07:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 05FC46B00B1; Wed, 1 May 2024 05:07:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00FB16B00B3; Wed, 1 May 2024 05:07:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA42A6B00B4; Wed, 1 May 2024 05:07:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BD60C6B00B1 for ; Wed, 1 May 2024 05:07:22 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7BB01A07FF for ; Wed, 1 May 2024 09:07:22 +0000 (UTC) X-FDA: 82069248324.06.52D434C Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by imf27.hostedemail.com (Postfix) with ESMTP id 840FC40013 for ; Wed, 1 May 2024 09:07:19 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=iYoQL1dk; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.63 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714554439; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1BWH3GNLeL9sQ4PkWWThCO7b5Qbxt9LKELlPiXbR6D4=; b=TbOhRcvyddO5z1KU3Kb+7DfBYu2ylBjrHMC7W0XfL5sP/NE9KL6X/6hmAGedkvoi/vX3A2 dOypOim3U7Eo3hUtEHrvIuobf3+meYfB+evt0P06upvzrvzd4ujYg6SIJ0bMUiNexpOF3r oG3OsVlxn0V0G91MbO+Xvgl3qc4YofE= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=iYoQL1dk; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.63 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1714554439; a=rsa-sha256; cv=pass; b=Q9+OBL5UI5qEZmRV3wAyjln+FQ3ZJ/Q3fJ0wX5B7gDT/9cRE/ugIAGbuO43vOK6J15Tbhe 4agOtWdL9V30T+1g6Q2nACu/7bDTTWLRMvAtemGPiJBcWAdxmkRb23Nx0kV8CIrlCY7Z3m Pulv8+iGNR26bNghdxYDj3rqXTjudK0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ek/qvCRv1fC573m7DQMXBBoCxj6a2ILYX8R9eviL+VCoUeFOCAkr0ui2iUkdUzq0IQK+yE5YwLFkamHzkuzSPlx30rGZIK5pPclP56ymuqD9dNmbU7BkNIvn12hEJWV/ZjYeQjt0jr3LDaAdR4augLz0swJTZKRXoLOczi292YT43B5njMYkvCX07ydh10YyQqCchr1hzWCepZwgAAgGZJ0izNKEbDk7uyKkpW6uXrWrzl31Lt+xGHqMuvsdAiirg9AVWgQSfaJlFnyuW9DgIIz7cif38IPQMwK1QNsfgFPMC4PM+enZn3J27yfeAA1Fqc3sQFLN1EcFwP4lf3zsyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1BWH3GNLeL9sQ4PkWWThCO7b5Qbxt9LKELlPiXbR6D4=; b=JdwSyBEDoZRWiJzDekPxnP4FkNiNf8Sot4SB2gNgPakjrzf6zxdf3X1kUQ9JMBAVoZ41Dq4adnIi2vYwT/TQxvxIXAo19gV9lAr7SVgyfTIvpSA960fZtHTOHT0MyqSs1LhmG7qsWgqeGgHtBSW7W/VmaKWyVDno3k1rxIa8Ob3Pa8xmj/oBvXSUlGH5PB+7DnYmjNBUmLwBlBp6OO2q78CBUUjUo/qyPxmF34InimErxaMW8h6TMYvoigd8hZPW9YkroJ9pkQTQuVFgRNlh8SzhGtPi3DIkFF6nyWVW32xbmlYjBmwClP/8hBbCEblWRs+kw6JT7cvvlJfYbIfb2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1BWH3GNLeL9sQ4PkWWThCO7b5Qbxt9LKELlPiXbR6D4=; b=iYoQL1dkcc5O3ws0NWLc2Mgo2uquc86L5ZTMuhklXEdC98op5JkTW1SGWHjMxQ7BESrVWlLJNHdd2trBWOnffetbsHIXAM5jVX99Ika/9Upf8/euubeXrf2X9DwidrZSiStXP88wcCfba0sqHrfHOqESATN18Jwk6gJ7Vben46s= Received: from BN9PR03CA0636.namprd03.prod.outlook.com (2603:10b6:408:13b::11) by MN0PR12MB6176.namprd12.prod.outlook.com (2603:10b6:208:3c3::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.28; Wed, 1 May 2024 09:07:15 +0000 Received: from BN2PEPF000044A0.namprd02.prod.outlook.com (2603:10b6:408:13b:cafe::98) by BN9PR03CA0636.outlook.office365.com (2603:10b6:408:13b::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.28 via Frontend Transport; Wed, 1 May 2024 09:07:14 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN2PEPF000044A0.mail.protection.outlook.com (10.167.243.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Wed, 1 May 2024 09:07:14 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 1 May 2024 04:07:13 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 02/20] KVM: x86: Add hook for determining max NPT mapping level Date: Wed, 1 May 2024 03:51:52 -0500 Message-ID: <20240501085210.2213060-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240501085210.2213060-1-michael.roth@amd.com> References: <20240501085210.2213060-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF000044A0:EE_|MN0PR12MB6176:EE_ X-MS-Office365-Filtering-Correlation-Id: da130d67-db94-405a-4e2d-08dc69be17ea X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|7416005|376005|82310400014|1800799015|36860700004; X-Microsoft-Antispam-Message-Info: ePWBXUnH8RlTULBIIfF1iE8Zn2Nl0L9mv7qB9E/fJlpnI+JZncU0Vn2TKYF5AOpZDJ1V04l918NGkfmaUpIAMNiaIhKQtbnyFkgGJilaOP1O0a3Gg78GckYv0RzADUz2QlOyS5zfyYxOTSO2EouCllZ4w7SLmh7sK6ZPkpdJ71/PgGhSoGbfzp8DbuzUGJGDlBBvKvwz0Tb2/e26sp2HvRAXdPDAd+mLPSbmu0MlhfUQ/eIJ8U0htJEWvMjacdKXkalaM9B+1QBpcwRm+Qb0ahokD8CjKgkIq4ghqQYhEKh9uyOYFPEd8ESq79n5Yi72PTqYeFseyBJJz62l8/rN1VrEtY0EEsqq1cFE21POxvMQ4G0mKeRm65E8XmYSqINQYksRqpWWJks24M848fJOQIA9fOfD/wnHlqayIhZHpm67o7tsqQIerQTgBNZbA7UnxpAT5dsYbu5HfwdUOeKp2fV/XnEd60BctPCUJ5nkbS06Fd5Vo45Dk+V1JAiKBaHrpsG5tDK/K9M28CuWm+8tdv7eGIZbsyHHBIzCWZmizqNMG2xqK1wc2BQswRwI/gKmtsE6IZF6IjJ0oT91kORxxoB0PSlHVvCciBTRKntofDg/6oYp+tt+6foYEamBFZKjd2YMe9utiaDYv048M3QX7vCXsN/TnqZl4zse/aaBfNWrB57cgKZLaZiwfDXxCamx3xEhhNYOdK72b9+G7Al7KEyqwARU9LYra0BKBT0boOVq+vKDtmfTo1j5V/XK0Mt2vzxd/qpq65JB7tXRBfUZEE4Mf3hNC5hDFdQ5SHA69P9AwOuSa1TEEktdyM/CVO/oPkr3rFP+S0o3H2OpKNJ+JvMev6GlIC5DcKe4Q7VS+Bo4W9YgEdU/1g7sK6+uXyaDli9xUr5KokC4Fp5i02rdMErBPIhCNqvjL/gz3Q4rLln0J0vG2Q5uyE7HD8vYxmKAF8q1hEFWXJS4xd6eNHFH6R9pkUpFORW3qIG7aboLbFMIWEHkiL67UoHb840ha+qOzKoOiXWYv2H2htuthRlA0YmahbPBe9AclBIZWdvxvyocy2bow4FTF35T+YAqua0O6SgUhd6aIgzqJ66/1N0jCBSwqfflZGXXGfd2zCXsuAJsdW+PGQJZS/3cOKctTb6mIawbJYDcq0xgTos3c9eGBpbtVPoboBzi889joA5Awkk20TuCQorqYkPhJKh7taL8VJcVYtyLGcNrq2PvQjDLj+WzDp4YYJ25m0R/ga6ALRSEkOuzF+itD6nfEtzqCyPzQDzqXYSZfRuVuAGIQ5x2kj8aDDHijqXs65YFGIRDejzpXfOXQk5tM4ERo1pu/fqTKcvKFLHf8JflBNgo574IpwZDKDuPp1K+UubE1g9DqfY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(7416005)(376005)(82310400014)(1800799015)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:07:14.4920 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: da130d67-db94-405a-4e2d-08dc69be17ea X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF000044A0.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6176 X-Rspamd-Queue-Id: 840FC40013 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: pk8mzo741ykgbeekm5wsswz6ph97mgak X-HE-Tag: 1714554439-851522 X-HE-Meta: U2FsdGVkX1/Bfr1WQq4qQQxUzTqDdiaB8d5QuMFhaA3zrHdctbHKhuSrNbMwsiTRkdn2uMqLYPrngOBo3aa7lmlAe/bJvmZ42eeLVEjutQwEmf3qtdzNPh2KOKUIJWai93RYcIXqHxvxIO18JVYXBVx0gA97BdCptRPddcQ4GbEuRgnipXwnU/hnHnqJ5qu/Qx4Xbh88WxetCVLwS5pR8ZNwfgG9+rip1rjEspW2uAeUamNMEpYvJksPz8x0P/o6YT0AbfwRzgNsvO//FeG2q1Aj806Jb3Zxrox+/PcJqoo1FpsuT9xNnNrl0ifRQ1t1DUVAS6lHuulb+VSxY4PWoS+d952DUe6CXRutTL45TuXo1hs3XKen0HwKVVyHdjoaBjaccJSRuqG8uYqsheb4s15Th2fq2o5IyT/x3r7tqu/UZtzDqRNc72eDzWmLCyCGiafq6LpI7C281VT4bVtdE/M2Ax/d2dgNZwml1YjZvevtL36e4RpW/QWsbv3MpRFTAYCENCE3V3slPz09mA7BHGhJsqQ2swO2cvC7O8QpQnouqlRmilfEwEp87mgOoaemfzSzvtans17lEwa6F99FVjnaInBjHlcq6m5lYUeGK7MLbTSLyICAloV13B37UMOtjICgVByXfML6SWPAEXebQpv29t+FpA39HgTWEzZQBC56HywbOCDA6zWQejnthDDiLbxJRYD+KkEUZa3DmLtBK0cEJ1WbzLYViK14PmJCODfPUqPCfvIhIbQiFRqxKXW8wM0Z080IqaJwpEM+j/H3uBUBVb0wxshjSjAZHGtxQ9gXx0JH2ADq4SWWsKkZhcbQcPGh0kfiWfpLfnBQondGrxA4r14zQF1WTf84cwkRsFyOKvrwuwC6JNTtDGU5dWDx/s/wEjYdBJZ+RMPuLOSpDSTj2ObpImP9MNskLduXvX49o6K+hpIJkqdOCIzlCaeuQP6ETZA+3yXa6QyS3OE LfiRAbqH r4wcBJnT5tMnOy6aCuCkTJStH0dy6BLgvLi/dSjeUuWD7x17Ab6DMIjwVYflSYseEjjJTo98JWl7/v7254p2SlikAnE+X5R6pT4OuEMdS7D3deeq/EmNXk1a4gUwzZZGCA3C43hZjx5n9gi+xR3tRcK0jRJWn88VuUbzrS7j7Dfx/FzdWBHAinsB5IYEAyJDcXAFYZH7ZAlD5piP+l9+R0A9tgY6Ht3DpTMPnW/GKlPzuHz/VqdNzX39h4djEXLa8+AbJnnAFjnulNiNYsH1g4cmQvLvD7Jrug/s7xzIZ/GjcxTRWtVbcgR1GLQOj48G8Mdwxi29ck75l/bQvKPoDWBaElm8HfFhEogznDqL57VeqLPI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 - guest later converts that shared page back to private 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. Suggested-by: Sean Christopherson Signed-off-by: Michael Roth Reviewed-by: Isaku Yamahata --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index c81990937ab4..566d19b02483 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -140,6 +140,7 @@ KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level) KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c6c5018376be..87265b73906a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1816,6 +1816,7 @@ struct kvm_x86_ops { void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end); + int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 510eb1117012..0d556da052f6 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4271,6 +4271,20 @@ static inline u8 kvm_max_level_for_order(int order) return PG_LEVEL_4K; } +static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, + u8 max_level, int gmem_order) +{ + if (max_level == PG_LEVEL_4K) + return PG_LEVEL_4K; + + max_level = min(kvm_max_level_for_order(gmem_order), max_level); + if (max_level == PG_LEVEL_4K) + return PG_LEVEL_4K; + + return min(max_level, + static_call(kvm_x86_private_max_mapping_level)(kvm, pfn)); +} + static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { @@ -4288,9 +4302,9 @@ 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); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); + fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->pfn, + fault->max_level, max_order); return RET_PF_CONTINUE; }