From patchwork Thu Apr 18 19:41:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13635349 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 7D606C4345F for ; Thu, 18 Apr 2024 19:46:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19E6E6B00AE; Thu, 18 Apr 2024 15:46:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14DA06B00AF; Thu, 18 Apr 2024 15:46:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0151F6B00B0; Thu, 18 Apr 2024 15:46:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D73FA6B00AE for ; Thu, 18 Apr 2024 15:46:29 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 86EC8120BE1 for ; Thu, 18 Apr 2024 19:46:29 +0000 (UTC) X-FDA: 82023684498.24.7927D96 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2070.outbound.protection.outlook.com [40.107.96.70]) by imf19.hostedemail.com (Postfix) with ESMTP id 9F91D1A0013 for ; Thu, 18 Apr 2024 19:46:26 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1VCrZiVo; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.70 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; 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=1713469586; 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=01TAlTG+4iBkE8EHH54F2/lq8N43H8eYojPkXkwF0WY=; b=MiIBkQ3oL/xZFDuP6h7J2++V7uP1kbRhAzbJwagD/pZUiz6QUGhjuDdcp7EbxYMzEXvE1L Y3kCy8m2Ad4lwbiBKSrG7sbKKgOzuVrKoidJRUB7QkmnjcUkQj5xfZPef9XPAnG9Dl5SLb wOcqyP7hcnvcMzgVCftLqCjsxKKRYa8= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1VCrZiVo; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.70 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1713469586; a=rsa-sha256; cv=pass; b=kpF5IsEJRdEeFMn3quVRobSCU7rzaWmODG+buEchJyA8us7BjzT2nxNyQgb6PaURAcAdbY ajrIEEqdpkbDKZNWW90IuhLgFnat7EK233oOqia+KSH+yWL5FkJY6EWEvJHyGSW8qbPeHB v2FJc0+vqTHQizGvR8gMOPTxCuoP0fY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IGGB1lupOv9/AedCouE+OjkytGKCdC1PquBQ2yDmF6SeyiHRcq92ZpMGXRk/uksF6VxezsPgXQQDGj3JXVZXedKwPbZxIDH41ZqUESzicGiU5QPblndTITrpmpt490iJ2zmKslW71cWrfj+b74h+wP6qZRjJpxKWC/fZ+lFZNHH5WFPWaBG5Zn1TbllhHPp0QzJGzIdFPRCO+sVA5CUSxq8ogueJMZhA9tc6Ffgkw7CWh3XFVVvKKGYtF1LRMyNHd+dELc38T2uNqye57+MSOY0NWMOhMQb9wcfEqm3n4tT9exSrSHqjvgEpwNdnFq8T5D2caJYHVv7b6izIg8UxgQ== 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=01TAlTG+4iBkE8EHH54F2/lq8N43H8eYojPkXkwF0WY=; b=iYSNVk1uw+YTvC4FjhBluyKxyEBChnm/a5QbIrdyonkafuAa/ug1vF7EtQAhfh+U0DIpyp9pD9rlc8Z+2K5TvLe932QihrSgacis2UhOGT1BbzKE07tQxybtRsoS735/uxEtKjIlN7N7Iv5ul/KHSMGt9JLrR0OONGFxRlgq9MHVMlw9wn0WwnFzvPaxK8hwp+xDpMCWYmnI8tl2lBE1hm2y2ofqySylt6CKgVbUwWszbl9BzgexwfKvD8/QEmYstFVBVHhBmiBro4Zn4G23CEhG7Q2sYQSRRwvWNQhk1C1TAcqwb1kLc9D0tfMdKMtx7NDO+xwbSXYTWOmowLplKw== 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=01TAlTG+4iBkE8EHH54F2/lq8N43H8eYojPkXkwF0WY=; b=1VCrZiVo70BaKuGnaQUUrJ3xw5I040RZ6wQoukqwGURj7/vhnGb63hxn7xiRxBRFlQIzr+wYJYaHOQ8iH0BtGHu+1nOvet7hBUVtQSJEjePvNtYRNBJa0l2+ZdZjS8s8iw11butqCkWr/8iJsR5u7R85n2fG9Nybwa72tnKUVBg= Received: from PH7PR17CA0040.namprd17.prod.outlook.com (2603:10b6:510:323::26) by IA0PR12MB8713.namprd12.prod.outlook.com (2603:10b6:208:48e::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.40; Thu, 18 Apr 2024 19:46:23 +0000 Received: from SN1PEPF0002BA4F.namprd03.prod.outlook.com (2603:10b6:510:323:cafe::c0) by PH7PR17CA0040.outlook.office365.com (2603:10b6:510:323::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.28 via Frontend Transport; Thu, 18 Apr 2024 19:46:23 +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 SN1PEPF0002BA4F.mail.protection.outlook.com (10.167.242.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7452.22 via Frontend Transport; Thu, 18 Apr 2024 19:46:22 +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; Thu, 18 Apr 2024 14:46:21 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v13 20/26] KVM: x86: Implement gmem hook for determining max NPT mapping level Date: Thu, 18 Apr 2024 14:41:27 -0500 Message-ID: <20240418194133.1452059-21-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240418194133.1452059-1-michael.roth@amd.com> References: <20240418194133.1452059-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: SN1PEPF0002BA4F:EE_|IA0PR12MB8713:EE_ X-MS-Office365-Filtering-Correlation-Id: 01120960-070d-4c0d-fb2c-08dc5fe039f5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eupQh/X5952KQoryRR/dikatdPGzgRlikufwCdcY7VB1sYHkWTIJM21g+jzHDDBUFejDvZtchywP6+krDjEdardhStcQTyElMplGB7xw8OPaeMjMm+HI8d7GSRptaHkxw6sdyC2uEQjXVoNn/+Lk+cISZEoR63zNY46uMvFMYsaAjG144UotQr/j45TwbSsoSBv4+pE7l2Ds5Ja7ChbWsXnRymR3+OQUqcajBZtQ/j3ifYxpOxmvi8sHLbF4H1cmQOLmuRKWSg1JvTbTM25K0sdmQP2Y5ZLENDzeIZmVRYORuLX8u/rugyR4N4R+K1oXMCbuqQeth4rADTmcFMDCDhLbfktTahCMtFFJJpDARcgrmga78gAArs1d8aE21ZmhuaegIT3ddjye4U3ds3eCkoKtt8CZRjxijUkkF9GSK0SqaM6rkotH3pRBzldz35qwga4Jvx7UwPPTe9BqAEBVxC7l0ltkc06Cs6alNkqaw57J8058+r4dM8dRmCVGDoojQ42ik5hf8D9TFC0n4poEyVcmbD1eMecWInV4SPy1ndINW3cS3mAtDqJl3asFtAbKmRA0SiwlIZ4SWIAYG1tx82CL5qjKDo8dbNrQxAgSPHcoltMt2lfJuGKSnk3diSuOmujsRu04xFNjIL6h+2gaKIaU/JXgGISCMa4v2VUxpkbqgohSai8x071U5qII8CPigq7t4TwN6++M9u7EF3eLs2hpVw9cWTJBTZGBjQwYCaSp0wM9ZVDyba5RcQlpddLX 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)(1800799015)(7416005)(36860700004)(82310400014)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2024 19:46:22.8721 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 01120960-070d-4c0d-fb2c-08dc5fe039f5 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: SN1PEPF0002BA4F.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8713 X-Rspamd-Queue-Id: 9F91D1A0013 X-Stat-Signature: k39mjm37b6qg4up9t4eo8n6sommxrmhe X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1713469586-265453 X-HE-Meta: U2FsdGVkX1+9Iw5wvzZduMLSTt63nbMeswQdCtdyA4cCmGAEDs6DlnJKDdb4LBT2RjgQYRxoB3XMautEwu2mG09xoWsFJOpBxCgsi3HlJv1ONvAfmEjPQJYFhvDWLom6enVyQm0ks7tFeFIKV65h9OxYM9Df/LqF2X9+U7MoEJfxD96kkrc/RFlpqLDy4AJzjD45tw+Y8Okz2guin0jD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mRSktr8AZT7wM+SNym+GQ4n3asNOoWGNQVt5WFteKX6lJXiBWL5SfTrJviwwKlo= 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. Implement a kvm_x86_ops.gmem_validate_fault() hook for SEV that checks for this condition and adjusts the mapping level accordingly. Reviewed-by: Paolo Bonzini Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f60bb8291494..3fabd1ee718f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4519,3 +4519,35 @@ void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) cond_resched(); } } + +/* + * Re-check whether an #NPF for a private/gmem page can still be serviced, and + * adjust maximum mapping level if needed. + */ +int sev_gmem_validate_fault(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, bool is_private, + u8 *max_level) +{ + int level, rc; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc) { + pr_err_ratelimited("SEV: RMP entry not found: GFN %llx PFN %llx level %d error %d\n", + gfn, pfn, level, rc); + return -ENOENT; + } + + if (!assigned) { + pr_err_ratelimited("SEV: RMP entry is not assigned: GFN %llx PFN %llx level %d\n", + gfn, pfn, level); + return -EINVAL; + } + + if (level < *max_level) + *max_level = level; + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 60d121250b0d..4b330b5ba4c5 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5083,6 +5083,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .gmem_prepare = sev_gmem_prepare, .gmem_invalidate = sev_gmem_invalidate, + .gmem_validate_fault = sev_gmem_validate_fault, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6721e5c6cf73..8a8ee475ad86 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -732,6 +732,8 @@ void sev_vcpu_unblocking(struct kvm_vcpu *vcpu); void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu); int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); +int sev_gmem_validate_fault(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, bool is_private, + u8 *max_level); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -753,6 +755,11 @@ static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, in return 0; } static inline void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) {} +static inline int sev_gmem_validate_fault(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, + bool is_private, u8 *max_level) +{ + return 0; +} #endif