From patchwork Wed May 1 08:52:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650315 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 45020C10F1A for ; Wed, 1 May 2024 09:04:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D07936B00A0; Wed, 1 May 2024 05:04:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB9026B00A1; Wed, 1 May 2024 05:04:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0AB66B00A2; Wed, 1 May 2024 05:04:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9054F6B00A0 for ; Wed, 1 May 2024 05:04:10 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4A8A114079D for ; Wed, 1 May 2024 09:04:10 +0000 (UTC) X-FDA: 82069240260.15.C1EC1C9 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2049.outbound.protection.outlook.com [40.107.93.49]) by imf20.hostedemail.com (Postfix) with ESMTP id 48F921C0018 for ; Wed, 1 May 2024 09:04:04 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CKVD1T99; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.49 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=1714554244; 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=mOUWBfJkRb3XEGVJSGFqVsBJr2cxraupp2c9EXMGQME=; b=1/VVa2fCDrtodn+7PxFKraf+eiUElKHLAo1NteFMHzvlAmSEvYJt4bQFbsSiHd1OJu1BVF lERtXwAA/7eC+AgcivwV8JkcBz6U+5VvEGTPT27b4i37MCg5dO7Gl/z2ewE0v6S3g1GOBf wIRTvDKBrAa80JaSDu1NqjRmt3d1rAc= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=CKVD1T99; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.49 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=1714554244; a=rsa-sha256; cv=pass; b=iQo3pmUQFIph/wxBxewkCwQ7orxVXavHkYxKnNzdMdGcsGTp/UxrW5/prAC8UQZ2Se9tT/ zMtOx4nCC6cSrmIfDE92no3FMkpMicP50w6Jd7sLcvhkZ5AVNNGcSoRhQTFAhMlViHfwqK YleFfF670lUhDSQhqxfKtbQpjzrjSEo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Go3DDqk+xjPVac2IpbaR+B3V7USPwZCBYQs8FADUgyWOF2d7eGFlH150x5OaJIO4NzFxO32IXl8UiG1HbeYNSvp86SMTQY5YAlZfa9r3phxV3rS/Z9lJYHZTQ9IkH6jnH8tcTCIUeQpD2XRvuHwzQrHxJkcNmp36aufncRQzfWtwIqjONC05ZMkv3F9429+AyTgJ7mBMCFX9nq63jVoz51FqV7F9j+p/A53yJ2aHg6w52uqVk8IQmzyasBwAjXfdMz4BHEpHgO/8uIbc1OmFuuvw7yF5uL5BG6JJVxl2AwvU0HGMRXRImgHZ4taNFV5qmL4m11M2psBhWxt8a8R5Fg== 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=mOUWBfJkRb3XEGVJSGFqVsBJr2cxraupp2c9EXMGQME=; b=dynUGmLZkfM7l4gxdA7XNar6CHHkk4c4BTvCnH9KWn+8Kt5YZAddPtXULlJqWjnlqkSXs17y8re37Q4nn9LeHlS2o2oCmvwK7LG/C+8TD1QOI5ssTZuY4I/tz2d1bBFR48oKmenSwIvZ/ptfXYvQql+VmHzUq1KBzY1U/mX5xxGp13bPnGYZl1RQpM6fyA6GD1PFHFXgDEi3agNh1DPp7iCKOsdsXlau5Q1ZGw25wsqDibdk6Lk9RlDgcxIxJmGae7UGO+gjNUBX6Xe3DsjnLQofQGkBbNT18tbFPGbCpa3WjfJnJIvHv54mYM3jbtDQKbXEw0exq2KbaCLVwzgPHA== 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=mOUWBfJkRb3XEGVJSGFqVsBJr2cxraupp2c9EXMGQME=; b=CKVD1T99Qv4L2kGrM+bSKR0l3M2kqsd9xpVl/zhI45Oh87/HIq+k2VZZtisYlde20mzuby3n+zZK2uAFxfskg1vst1uJEwVshjN1yB/7nav5Iwr5kBQdjPj1UEIVCyEvRxvtVApFO736escnTyGdIHpBhz4X/h1py3TaYwzk0gc= Received: from MW4PR04CA0305.namprd04.prod.outlook.com (2603:10b6:303:82::10) by DM4PR12MB8476.namprd12.prod.outlook.com (2603:10b6:8:17e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34; Wed, 1 May 2024 09:04:01 +0000 Received: from CO1PEPF000044EE.namprd05.prod.outlook.com (2603:10b6:303:82:cafe::33) by MW4PR04CA0305.outlook.office365.com (2603:10b6:303:82::10) 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:04:01 +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 CO1PEPF000044EE.mail.protection.outlook.com (10.167.241.68) 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:04:00 +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:03:59 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 13/20] KVM: SEV: Implement gmem hook for initializing private pages Date: Wed, 1 May 2024 03:52:03 -0500 Message-ID: <20240501085210.2213060-14-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: CO1PEPF000044EE:EE_|DM4PR12MB8476:EE_ X-MS-Office365-Filtering-Correlation-Id: 86e91425-1926-488c-178e-08dc69bda46d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|36860700004|376005|82310400014|1800799015|7416005; X-Microsoft-Antispam-Message-Info: SW/JOBv3MzAhkUQaMpFRm2ZXEpT+AEIPr4ENcaOZDPwZyX/cSJK/vQWyUUZJmxUSlz1zd0JskBHzP6MNS18miJAKZkqZBAtOrQl2ttxZAT/7A5bi3QDP8Ju0EWl7fI8wEnJgtKEYA1e+yagOoQ5iOTOG1vI+PK3BAff9xywBu0oCu9WPRzei/DnsSKAYzvkiOxTc+9lBGif6y6X3NIgtMU7QD3ZUDs4OBzEewy5rFRsSOxICVhTJiq9TLRbI4yEWwa+ian9yE1w7Lp7AIWNPHmiNSaVJ70D/Cwm5Eff4p4mRLHLkS2iNmACdJzvGZsxLdwp/td5Nw6uIinY3H2C22Zj3jssMGZcwXwm/PZNwtfbwXOtI6VtVKI+56vWA2KF5A77rA7zK1kwOGi3nO2SGWKvpJbkc02iicAOd5r4NvdAYVDmNzE9fMmhPDikk9qvIoMcPXwef693vEXtoTMtGH1srr3Q0xDFfd4FimfiOgUYcZ1isaSSBcxJp7QkAgNVGPc2ua7/cWRe0kdDHjpadA/9qa1mDrpx71oRGTiYUwpRa4YVao/fW2Xz6H3Lp+D53QTieV0Ebfm0g9mmEIMJWxunWYTXp+nSmApfAXjxdQDWX4ElAPIKdoSz0/+L0xRSqAQo9TFVR0t5ddPLDo7U42BREha73CaByqTavaHI0AFx93NAqT0dbukD6gDAfNuHBnAwg+nQCz8wuWLNsWyzsRrHLZ9EzqoGS7zo3dMkOZeMHibQeiOfQVcG5qY1DsKlY7Twoa3MUDqCvUphQB+QYLCajoHJvk9oSpvGfJychQUF2UVNKQ5DRG/5zBDUiqFxWsNhXyoEs7EfN7LYtdosxYr4jNHrE4nuLSd61lx1Rbw8oBY6R23shrjBowPe/osLmLIDqC1SIrmpShfJDaVXFoodNkMljJJlhqxU98n2BGUAL+b4fiybFLOR9Ya5XdjeuHk6rLuAVIq31Qi2XNgCj65MzlZzZ7NDzg/c96iiG5KcTMYEOEj5PbXNYY3X2hVLc/389X6C+rWdJnEb84Y0plR+Tm89/OL0GvBd84LVf09vpCfnOpeORuJcI3y6UXMtmPYY2NaqmC8S5ALrRgx3zM0Q9h/1QYYj+6FUwdiXlSB7fB/UgPAoKbZVkvlw563bPiaY1JGrpFk2txTd0xQ+W3MphUb3IaZXty3VpC2aTIdI1XklAq0AH5gkIGQbolp0quOv4rFjQRBkN+isdymuv3DeGBg12AAPU5nRrLS/D6VGVUVeDoE3F947fd77lf5nCs44vDPN4Hz04128IWYgMLSZsOJvLVjkxqnqyK5FT0uu1ow3xNE6Qy0ZoyNFcvpdcG7f4wJOnKF6a/dVd09dhjwYQH/eL899eCWuA9hJSU4Yy9A9wmZL/M+tCCN1cps6W 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)(36860700004)(376005)(82310400014)(1800799015)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:04:00.6084 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 86e91425-1926-488c-178e-08dc69bda46d 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: CO1PEPF000044EE.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB8476 X-Stat-Signature: hne43wi9godhn6b7esjajcutuu1xwfur X-Rspamd-Queue-Id: 48F921C0018 X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1714554244-704101 X-HE-Meta: U2FsdGVkX1/VNdYdx21T9eTpFVw3ecjWGQYqS+lAM8qj5TVgTWonptz82KLeMkLaAM+OGhV9zZDckowTOoI32YZDLZByMCx2OJz9DmfJvN+43OfpeEwhrD8CsjYgeqloP57o0//1E/+6CoFd34O6ZaBiCRLSsshAH+pNZRcfr9H0IngARaVt3Ic0nbb619nzBPjmYhksm0jkejYo/W7IkB/oX1OTMDjHLpi36UfrRnVZ7ceuTgqVtkPOCTw19tB5+2K0f9Rk8bc8aDCGyjZ4unBM0eKvVOrOh5FbuwWYSXimsvsbl3M/63YwtNABtYAG42bANL6JD555ClV3afwwabCS9dCSckQKm9YyAOf2KTgrWqWLzH3KS1jnUmrd1BgOaMrZHWRaZbsGz/GlVWeS1vgP9k1iV3VJj0MTeuk6yDdkHVoW0pUMYqzVN8SNaq4sMVF3MhFA5DLu90G6kPOa2Aldj/VEtt2IrE9lidWKSGfEYrgLC1oFAMzXP1veNoBpxV3d1kIHjPmzRme07vw3kwYrZeeZQFp9np0pPWNGdaTboN0aLbHTUEfDN9/uSAjeDNx4nf6jllpNvXvLoObR6sVRyLo/V9E+0WS42LHLze3Ef4mNBKwr/SRWcvrJSKvr0G9OUGwCNiKfucz9R/OoEKGkFTsOe9RMW7kGdXD1tXqwpm/SI6dzhLaEUTh8HKt+s7NcY3SCTIHRYCV8FJPUWTDEkn8w4fDv4eLBJGppDMW/52fyFFf82p92WFP2WOwXR777z04PwERdgun98XtdevYdbnfQBCkvYT0lWmqMOKUvA4wNzYN36EEwvQUB1DwXh2FO6hoed16R7jDqBmciYpm+acRjh9o2eSIcf2DMSy3BpxzXN405dWrtPNeMUXajoujp2FM2mgBz6vplzRlAGBFDNnivR5hqhkUoeRcoOVyTb5BciTR85xOBekMTCgU6Ojtzi8fRuKTqKDzEuxU TYNRR+AL DroBtzxpDQ6lt5nXh1AR838cl7ozpDXKvClDAiOah+XsOJHTRxnHvUqkttRnlzFjLQjQR5AUR/GS/UDQuq2WyjVmWqdddd/Z44UjzVy7qmhDeImQ5TV3GY9z9BdOGN6Y3DspA1uzd+hrrJTyQPbIRkuNUM/jq5Cm5E0rrslvF7WthRNo6u6xpnUxPsy0qU0JWwGYQ9D75Sn4yunppUMhDfMKnLvz8WsnUPCYp1+K6bsGH315efhPjtlIqdCAVmiqIxC56YdijUWnUtmwBTAAfk/XFlJG52Nw9KNGwxsfKfowvWCWSyAPSwc8irJoVVUngPqW51BPBbscl7KRz8RqKs+eRl4n1alxuoEoPe+himlqwFxw= 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: This will handle the RMP table updates needed to put a page into a private state before mapping it into an SEV-SNP guest. Reviewed-by: Paolo Bonzini Signed-off-by: Michael Roth --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/svm/sev.c | 98 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 5 +++ arch/x86/kvm/x86.c | 5 +++ virt/kvm/guest_memfd.c | 4 +- 6 files changed, 113 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 5e72faca4e8f..10768f13b240 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -137,6 +137,7 @@ config KVM_AMD_SEV depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) select ARCH_HAS_CC_PLATFORM select KVM_GENERIC_PRIVATE_MEM + select HAVE_KVM_GMEM_PREPARE 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 69ec8f577763..0439ec12fa90 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4557,3 +4557,101 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) out_no_trace: put_page(pfn_to_page(pfn)); } + +static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end) +{ + kvm_pfn_t pfn = start; + + while (pfn < end) { + int ret, rmp_level; + bool assigned; + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret) { + pr_warn_ratelimited("SEV: Failed to retrieve RMP entry: PFN 0x%llx GFN start 0x%llx GFN end 0x%llx RMP level %d error %d\n", + pfn, start, end, rmp_level, ret); + return false; + } + + if (assigned) { + pr_debug("%s: overlap detected, PFN 0x%llx start 0x%llx end 0x%llx RMP level %d\n", + __func__, pfn, start, end, rmp_level); + return false; + } + + pfn++; + } + + return true; +} + +static u8 max_level_for_order(int order) +{ + if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M)) + return PG_LEVEL_2M; + + return PG_LEVEL_4K; +} + +static bool is_large_rmp_possible(struct kvm *kvm, kvm_pfn_t pfn, int order) +{ + kvm_pfn_t pfn_aligned = ALIGN_DOWN(pfn, PTRS_PER_PMD); + + /* + * If this is a large folio, and the entire 2M range containing the + * PFN is currently shared, then the entire 2M-aligned range can be + * set to private via a single 2M RMP entry. + */ + if (max_level_for_order(order) > PG_LEVEL_4K && + is_pfn_range_shared(pfn_aligned, pfn_aligned + PTRS_PER_PMD)) + return true; + + return false; +} + +int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + kvm_pfn_t pfn_aligned; + gfn_t gfn_aligned; + 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: Failed to look up RMP entry: GFN %llx PFN %llx error %d\n", + gfn, pfn, rc); + return -ENOENT; + } + + if (assigned) { + pr_debug("%s: already assigned: gfn %llx pfn %llx max_order %d level %d\n", + __func__, gfn, pfn, max_order, level); + return 0; + } + + if (is_large_rmp_possible(kvm, pfn, max_order)) { + level = PG_LEVEL_2M; + pfn_aligned = ALIGN_DOWN(pfn, PTRS_PER_PMD); + gfn_aligned = ALIGN_DOWN(gfn, PTRS_PER_PMD); + } else { + level = PG_LEVEL_4K; + pfn_aligned = pfn; + gfn_aligned = gfn; + } + + rc = rmp_make_private(pfn_aligned, gfn_to_gpa(gfn_aligned), level, sev->asid, false); + if (rc) { + pr_err_ratelimited("SEV: Failed to update RMP entry: GFN %llx PFN %llx level %d error %d\n", + gfn, pfn, level, rc); + return -EINVAL; + } + + pr_debug("%s: updated: gfn %llx pfn %llx pfn_aligned %llx max_order %d level %d\n", + __func__, gfn, pfn, pfn_aligned, max_order, level); + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index b70556608e8d..60783e9f2ae8 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5085,6 +5085,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + + .gmem_prepare = sev_gmem_prepare, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 858e74a26fab..ff1aca7e10e9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -736,6 +736,7 @@ extern unsigned int max_sev_asid; 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); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -752,6 +753,10 @@ static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXI static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} static inline void sev_vcpu_unblocking(struct kvm_vcpu *vcpu) {} static inline void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) {} +static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order) +{ + return 0; +} #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b20f6c1b8214..0fb76ef9b7e9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13610,6 +13610,11 @@ bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) EXPORT_SYMBOL_GPL(kvm_arch_no_poll); #ifdef CONFIG_HAVE_KVM_GMEM_PREPARE +bool kvm_arch_gmem_prepare_needed(struct kvm *kvm) +{ + return kvm->arch.vm_type == KVM_X86_SNP_VM; +} + int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_order) { return static_call(kvm_x86_gmem_prepare)(kvm, pfn, gfn, max_order); diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index a44f983eb673..7d3932e5a689 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -46,8 +46,8 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct fol gfn = slot->base_gfn + index - slot->gmem.pgoff; rc = kvm_arch_gmem_prepare(kvm, gfn, pfn, compound_order(compound_head(page))); if (rc) { - pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx, error %d.\n", - index, rc); + pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx GFN %llx PFN %llx error %d.\n", + index, gfn, pfn, rc); return rc; } }