From patchwork Fri Mar 29 22:58:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13611217 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 2E3BBC6FD1F for ; Fri, 29 Mar 2024 23:04:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C187C6B00A1; Fri, 29 Mar 2024 19:04:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BA1156B00AA; Fri, 29 Mar 2024 19:04:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A19A76B00AB; Fri, 29 Mar 2024 19:04:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7FF786B00A1 for ; Fri, 29 Mar 2024 19:04:23 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4645C140141 for ; Fri, 29 Mar 2024 23:04:23 +0000 (UTC) X-FDA: 81951607206.25.4853126 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2080.outbound.protection.outlook.com [40.107.94.80]) by imf14.hostedemail.com (Postfix) with ESMTP id 3C38F100005 for ; Fri, 29 Mar 2024 23:04:19 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=cc0uyUYN; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.80 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711753460; 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=aC7aySI043+XZrY5VGR6Yx8968W+cYuttAiNqJpHqGA=; b=F8WpM/gBpQCKnbPWmiQxgV4C/C5Nxw+bTxjlknnkqd5/vtsIIGDjJHwipQl6V47+X+IMMn uVCtqiRDoKrTM7gbh0KoOhFcfn7FTaqcs4V3hypmIGMRX5+ACshzOX4pFC5X6u2tAAG0BG dXuD8sBtUKhLeYReykyQR16MhWGWi8s= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1711753460; a=rsa-sha256; cv=pass; b=FVE711i1UWnY1JD4lHmJMXDTNrQ2qnvXH/qccJbEqD8KAMYIj1pPZycCcWaB8Uy/GpwABW 31WYDaf5crNedsI0UYGq53n6SEbWPKR6xaP0Zg1pxKPMYh4wA2iAwZFbRI9wVLaRPjeJXt m7diQz2TDsnKbYBzskjG+38jTmo+518= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=cc0uyUYN; spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.80 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Egwx+Yj+O9GqycHKkixz6q6WqQl2025+we8G7RWNBTOQwjXQrBQqzMlyS+v1FZCUop1DmJ9GWptxdse59prYS3Cjkt209sM8gcipEcuvecX6w4Z20PkW9h5jiVDY3wiHc4ZCqCK3v0LFI4SvKzMNx7NAphkvnpxLBDozJ0sfxTCdqhkpHBboS0pNDzZLpIlSQhRJ5mALAlkSdkvipum0XSjmehyE1WqgMacyz9sCitAzrBQBG2/+DZa+0oOODguJ9zQKWZtor1atdnq6nenllic/7avtJkFRREXbDMoXGECUaAAzwm/sz2urFNmoqPUIIAVEi91G5WDL71V5QPx1mA== 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=aC7aySI043+XZrY5VGR6Yx8968W+cYuttAiNqJpHqGA=; b=cTUTSp2GpeLZpZGwUPRSO/T8EXWC74ipA2NnD+VzU9tW/SqQYNV0bxZgsBaTYcHTQLNm+hGNsyquRQlMvda//nXQ5GmUAGEnV0cqN49172en3KaNUovSESj87XEfPACp3b24PoQYXaIfICZ+N4muvvu0dbQaRGUo7jGEH2mtj5iddh273Fipx4Tk2ETKfdS1rnvtzmwX5IaZOouhJvANNBAiwou//elVD3+oHT0/zgqSHSMkiHXkrOZS1MP4KCHWZ+u9ktEFjc2IolaVwkoiNMSHB9dlRTRBdhLJgpqxB3oIjlNQIBME3DXYwp7Ba26e9hdt3NkXwKOPfYfY2oIowA== 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=aC7aySI043+XZrY5VGR6Yx8968W+cYuttAiNqJpHqGA=; b=cc0uyUYNMyeAQ5K8eq2/q/ZPuUjv9DGzNEi//Z9zc0EC0ANF6fCUIWdrv+bm+cAiEfgIeXRCeHAAqvX1/vTx2aft6GpjnWr6vZ4H744mfOHLmYVqfaYmRaAr5pm/FVtNs0d56wu2NB9ee6PhfEokC9e7br1hkCDym/i0c+Xqut8= Received: from BYAPR07CA0068.namprd07.prod.outlook.com (2603:10b6:a03:60::45) by CY5PR12MB6574.namprd12.prod.outlook.com (2603:10b6:930:42::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.38; Fri, 29 Mar 2024 23:04:16 +0000 Received: from SJ1PEPF00001CDD.namprd05.prod.outlook.com (2603:10b6:a03:60:cafe::c) by BYAPR07CA0068.outlook.office365.com (2603:10b6:a03:60::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.40 via Frontend Transport; Fri, 29 Mar 2024 23:04:16 +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 SJ1PEPF00001CDD.mail.protection.outlook.com (10.167.242.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7409.10 via Frontend Transport; Fri, 29 Mar 2024 23:04:15 +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; Fri, 29 Mar 2024 18:04:15 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v12 22/29] KVM: SEV: Implement gmem hook for invalidating private pages Date: Fri, 29 Mar 2024 17:58:28 -0500 Message-ID: <20240329225835.400662-23-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240329225835.400662-1-michael.roth@amd.com> References: <20240329225835.400662-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CDD:EE_|CY5PR12MB6574:EE_ X-MS-Office365-Filtering-Correlation-Id: 42a3b550-3de1-448e-bc04-08dc50448eae X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PgmESFyaAzP5udG4PZdmIr/pO1stMQNxvv7F1a0Km6GDitf+qO+H4IhrR7V57XHkeTfeeMvszrda85nNEQ9MNqktCq8ek5e7U60wxWG1t6xIGz9UaLWojzFwE35ePid2KvjHglVbkFBTm7KCkoyHuHxMcCSimyzWAuLz8Y3PTHIF2V2jhmRy5QjNTr9DKz5kP4B8i3ysj3iT4+dV8W68wtYbyrum1Byc0B+S3Yj2TiKhZxtbUzfitCyHZ2rKpvuAW23sL1ajklH+GAujLxR7dDgoxN7iUEcCgsMvO5YhDo/iG2fn0SZJuSfIslZcyxCgNFaaIoDy7bcFfDw+m+vTNasy8OJS8a8JFPs0J0aqKu8L2miJn8KbhevGTLOIFsrE/dtHfMzUTh6YX/+cXuItNVDfxBlMo8ydcz1JspIJi2CgWwhGjbhhv33yiy5xH3CP8rqqwBlPrfp6qQb4MOx0RzJXzlTlFFwcfCKVUjCq0zVqIg/I7wTP532CnhooxFtUfGG1KLwTGH2NLu0iTyUvNl82wJlKDWjaRTCWosDL5EG/cmFNMiDG7vbpaQDa4LDJhAnh6xKnnTlrsXirNBtyHha4GulEaldPg/2lSf2oXgB48YqI1mJpKam90pW3Z8H6pqCj0JM4L3Srwfcb6ANNEe5pyf8KCKx0QKcLnC6i41VJ62nN8CaEbK0hpPCLf+yNrXaCSSlqrwSqIFUXR0ZNZXmOLEVWtA1rzVeSwf9wsUrMC0Mr+so4hGqUl4l/T10A 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)(82310400014)(376005)(36860700004)(7416005)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 23:04:15.9550 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 42a3b550-3de1-448e-bc04-08dc50448eae 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: SJ1PEPF00001CDD.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6574 X-Rspamd-Queue-Id: 3C38F100005 X-Rspam-User: X-Stat-Signature: oxtjpq8ga69ezi8mpr7hsabjbtgiobm1 X-Rspamd-Server: rspam03 X-HE-Tag: 1711753459-373306 X-HE-Meta: U2FsdGVkX1/Haw9McMLV0mOiM7PHlKbQ6TjHVXQPrP3j5M13x5EMxHUiplnW8857vPEb5jpKCJPsu4Cf5FuqP1f0nj+Tsp/BLOw5/Q77YC/eP+5hxhvWrnG6g9VR24XESe3Gv6YuQkY3h8oLKXCQre5NH+bSvfm60/Bf+BdkPTe82sVSfjw8MzbUcAsGUqLb2Sf/HMQR/FyO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h79AiLVGJaeQHqcOxevYcdYTTibHLOr23ZCHIsMKdzA= 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: Implement a platform hook to do the work of restoring the direct map entries of gmem-managed pages and transitioning the corresponding RMP table entries back to the default shared/hypervisor-owned state. Signed-off-by: Michael Roth --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/svm/sev.c | 63 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 ++ 4 files changed, 67 insertions(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 286b40d0b07c..32a5c37cbf88 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -125,6 +125,7 @@ config KVM_AMD_SEV select ARCH_HAS_CC_PLATFORM select KVM_GENERIC_PRIVATE_MEM select HAVE_KVM_GMEM_PREPARE + select HAVE_KVM_GMEM_INVALIDATE help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e1f8be1df219..87d621d013a4 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4380,3 +4380,66 @@ int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order) return 0; } + +void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn; + + pr_debug("%s: PFN start 0x%llx PFN end 0x%llx\n", __func__, start, end); + + for (pfn = start; pfn < end;) { + bool use_2m_update = false; + int rc, rmp_level; + bool assigned; + + rc = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (rc) { + pr_debug_ratelimited("SEV: Failed to retrieve RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + goto next_pfn; + } + + if (!assigned) + goto next_pfn; + + use_2m_update = IS_ALIGNED(pfn, PTRS_PER_PMD) && + end >= (pfn + PTRS_PER_PMD) && + rmp_level > PG_LEVEL_4K; + + /* + * If an unaligned PFN corresponds to a 2M region assigned as a + * large page in he RMP table, PSMASH the region into individual + * 4K RMP entries before attempting to convert a 4K sub-page. + */ + if (!use_2m_update && rmp_level > PG_LEVEL_4K) { + rc = snp_rmptable_psmash(pfn); + if (rc) + pr_err_ratelimited("SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + } + + rc = rmp_make_shared(pfn, use_2m_update ? PG_LEVEL_2M : PG_LEVEL_4K); + if (WARN_ON_ONCE(rc)) { + pr_err_ratelimited("SEV: Failed to update RMP entry for PFN 0x%llx error %d\n", + pfn, rc); + goto next_pfn; + } + + /* + * SEV-ES avoids host/guest cache coherency issues through + * WBINVD hooks issued via MMU notifiers during run-time, and + * KVM's VM destroy path at shutdown. Those MMU notifier events + * don't cover gmem since there is no requirement to map pages + * to a HVA in order to use them for a running guest. While the + * shutdown path would still likely cover things for SNP guests, + * userspace may also free gmem pages during run-time via + * hole-punching operations on the guest_memfd, so flush the + * cache entries for these pages before free'ing them back to + * the host. + */ + clflush_cache_range(__va(pfn_to_hpa(pfn)), + use_2m_update ? PMD_SIZE : PAGE_SIZE); +next_pfn: + pfn += use_2m_update ? PTRS_PER_PMD : 1; + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c099154e326a..b456906f2670 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5080,6 +5080,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .gmem_prepare = sev_gmem_prepare, + .gmem_invalidate = sev_gmem_invalidate, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 53618cfc2b89..3f1f6d3d3ade 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -731,6 +731,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); 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); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -751,6 +752,7 @@ 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) {} #endif