From patchwork Wed May 1 08:51:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650355 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2048.outbound.protection.outlook.com [40.107.102.48]) (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 26E274E1CE; Wed, 1 May 2024 09:06:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.48 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554417; cv=fail; b=nf8J8n4rYsOQe7yq/bpYup7OTZJdzmdRDltmYM2Jry+9v4Yt1GJTxxaEi9XQbYjb+nD4tx3huhjoKi77UiytA2GUgn/gIYFuKyoeBoPglnTlIMY+Oep9yF0dwnq9RIWQMM1ytc6KkExE3w9i+YX0lZfd19/INT73XL6hDoKigKA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554417; c=relaxed/simple; bh=6+Xu3VEmTUE7ESBzNS/FjAKGK9BVXpheciBLCAa3t+g=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KksnVKkXZ/QCofn/LJlQ3wNKg1YcdyHL9zXbqBDxlY610DU+lU4sEHBi/X7hOMdfPR/Oz1qCGDiXnGDGeQSZfUV94NLQoHtoUcRTISK5qy9b82YoQly6T4tOUSi1RKZ4E7KWucdIoB5g8XgQMHroTQCUDbg2XWiiXqhafKWtbKU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=1RniRHzq; arc=fail smtp.client-ip=40.107.102.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="1RniRHzq" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hs4Zpl/KDiYMOwiM6MGWXmc3tZVzsz0qzZvRlm9MYn0uN+RJjhcM/Q7rMHiOASNxm/musDM7CXY+u7o2qQdVwLEWIERNzFCsZIEjNy3zIzqweQgm9XlDS9DnqE4eGwMQhWyThGCtWd38x20lklXzRxWFBmwW4Hr0xtqbLMz2BHmnaNKtbMfKpD22JgdocuBldhf9kzRAtL7asLhIY+aUzNoW9X2lAxUzWFYubRalIxlZ+lkuAnIaKXQeWKiU3l/Zz4h/kE5nRMKb3RvNyLLoStpyJPPbN4vvqPQMWsYRLsfL2ocesYDKO8SkD9FTIkMlodjkIEpVeT/b0oUSEpv0Pw== 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=alTvwCC+QqKAYNVMqng3/wZwTqjTdFBEpHqD12DsSYs=; b=jAKrzLfBE+8cAr+TTxs+YEV35l9dzR5aGvP8nNLWq5bnYtqm6dLnAbm2SAwgcN7Fp8TkqwUePv3Ne81tsY84uynn9aef0zuVRSEeo+f7XIIKq1TN6I14HOvjy4JaokYR/cexbAHk7QkR5n3tAa2ingWQrx0fb7Jc34pVGaqoRMTdPjvuS1Hg0l5KHq0/bNi7LZa8tBH6/ayGOXEfJlcc4ga0Uqi8X4lRs7JxbObQkfaQtkXt9HlCbxLxYOzOJgiQoFeoPKQPhzt6Cpnc5LmZI/I+0FprGgNElcDsmZQ0O3zArTyRmGlfyoBtQt2fmIUh2ofUAm5nETZqIv4NMUIKlQ== 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=alTvwCC+QqKAYNVMqng3/wZwTqjTdFBEpHqD12DsSYs=; b=1RniRHzq59ira0ee4CYBmOLd0r1cGkiWOJAQkr2v7DDe+iY6yDpWVgQuAZDl2hSqxh3cOUUgNDYv3jMpm0guwmimVgEiR/+bP6Ilmdlmv0YlmFlwIMX4k5lGBw4vTQbXtxQt/udWmmtYMEc6xgGMuXW8Z6XSJrsqPa1kMY5QYsI= Received: from BN8PR12CA0012.namprd12.prod.outlook.com (2603:10b6:408:60::25) by PH7PR12MB5855.namprd12.prod.outlook.com (2603:10b6:510:1d6::11) 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:06:53 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:408:60:cafe::b5) by BN8PR12CA0012.outlook.office365.com (2603:10b6:408:60::25) 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:06:53 +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 BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) 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:06:53 +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:06:52 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 01/20] Revert "KVM: x86: Add gmem hook for determining max NPT mapping level" Date: Wed, 1 May 2024 03:51:51 -0500 Message-ID: <20240501085210.2213060-2-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A3:EE_|PH7PR12MB5855:EE_ X-MS-Office365-Filtering-Correlation-Id: d13c5f51-a792-44ee-c5e7-08dc69be0b5e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|36860700004|1800799015|7416005|376005; X-Microsoft-Antispam-Message-Info: pE+Dw05hoQsBzaunELHWHwft+hszd+4S4vLIt5eeMUk8Aj8aIeKeC9SYRErikfcv9BaesLFsCoqzI/R7rjMj+3i6gbOj8MrRD4qRsvfDS9MFgkP58OIMUd3kzJqfdlJ+cy+3h1O9Eb9OQMPfFcPdJ/URTiyI+YAUwrj6Ul0+VfI5qfsctB6SyK/L1md0OWQPV/Cef9Q/iol1rE8txlwcOgtogp76CP5nKmUTJskeJcgRP3TS4LpQW85wuEV4Ti2EYU2tmqqVMhTaPnPHNfC6WeyYkdX9rLFOd/AFdTkr5FPXb/X3/k1KEsOyAC8IG3lpjKN/JGRHrZvN775Mun3PDYlJ8QbCfVJaoty/dkgoW4SFTnOHxqapTeJBkyayZbmltgdTejO1+YSDjVFmizds96I5A+rLNvvbbH+UJKf4PWA53/AXodQJESOvoDjSAE4S5LwIvSEePMVe3G3VK1Jx9DYKuNOYxkPlnTEfouviaOQbNJ3xK8++XdeDPaMx5bwoMVbEH4v/4Mh9jXmm4MhrLUPHExF63nmvAvFZs99Dpn/T7Eum1LGzMfxPuMsP/R8rTE+OSBZ0sFx2iL9VQb/2nD6SIfk+rbX1EC5d7ex3aZN73xmKNxdd86mmCjKUdSPHmburFljdd7paTVNuMXci0LfqsbP36E18wXPbrWKQ+Ky9lkI8+/DUYVJsN+14ub2W9GgsIRlPX1LN++cjXR9/hzBHVlO1Y7pjByE+t69L7IEI9KJEnP2iW3Kg36kDhoQ0T7C6/R4oudLnmFw/6a6EXS8/qMKfgd25+7GBKff59o0SCbiC74DSwVIiss1HihXVHU6a4MYEnl1jW5sCknRJh49QtE2jUA/lah6Ap4rq8XA/1SG9z/+J/5kxXxcBrYkr8+DrVaJ23t2lwvtVt0exiRIuSLXzl54cxzv0IFb9cOodS93ZfNrfd6yq8KRU1++pKiigXhy1TKaYjyZGF9w6hvUrsNZYNPOiegsoN1WHMxHMzjiTbcgnWGeQQTCz0mtvBk1fQu3qKHzz6nM1CteKYIH8kSEB8DQzo6ILa4GON2RlQkrd4S9hT387eN12lUzGMV+CdksC63OEVEUs0366bLPvn3hUTmwdkQYTXSimB9SDXZIvKNVA1bLG/RY0l2l1Un0EJKzTO6WdRx3oQAzMMoJHN25HNTTMbwsJPd5W7hnzq8Pem4CebrCwERGP3i151CNmXvYmD+Cqh0ftUQT/U3UkppBxNXCcCAWSeNM+lRYpgYf1Jj69r/8FSqQvMIaeH32G2sCsek11GR38Nv6u0fcNH0Xm4p0b6hhbI767THbj4qPGWtrGvF0m9v97rjNw 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)(36860700004)(1800799015)(7416005)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:06:53.4762 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d13c5f51-a792-44ee-c5e7-08dc69be0b5e 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: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5855 This reverts commit 20cc50a0410f338657e23e77fcc21fee2bc291e6. As pointed out here[1], this patch has a few issues: - the error response could theoretically kill a guest in cases where retrying based on mmu_invalidate_seq might have been sufficient and so it should purely be a means to find the max mapping level that never returns error - the gpa/private arguments are not currently needed for anything - it's not really a "gmem" hook but uses the same naming convention as actual gmem hooks Revert it so can replaced with a fully-intact replacement patch that addresses the above. [1] https://lore.kernel.org/kvm/ZimnngU7hn7sKoSc@google.com/ Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/mmu/mmu.c | 8 -------- 3 files changed, 11 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 2db87a6fd52a..c81990937ab4 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -140,7 +140,6 @@ 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(gmem_validate_fault) 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 4c9d8a22840a..c6c5018376be 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1816,8 +1816,6 @@ 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 (*gmem_validate_fault)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, bool is_private, - 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 eebb1562c5bc..510eb1117012 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4292,14 +4292,6 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); - r = static_call(kvm_x86_gmem_validate_fault)(vcpu->kvm, fault->pfn, - fault->gfn, fault->is_private, - &fault->max_level); - if (r) { - kvm_release_pfn_clean(fault->pfn); - return r; - } - return RET_PF_CONTINUE; } 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: 13650356 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2041.outbound.protection.outlook.com [40.107.244.41]) (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 6B1702C853; Wed, 1 May 2024 09:07:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554439; cv=fail; b=NoBGoCK4LZ/b+mCgMt3m2Jx3rUtHiczHMKmTPjEuhX6HrrNTTnRil5NAQGxqRNdbTX8drQWwC8rskFh2HhVbHNl6x0oQIlUQy0FR4HKou++1jxEaIBi8brmgQUsnMR/QKjDwl06MQ9DNVrGrn8u+7q0t9Yh0fKNBgyOBD5zjK68= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554439; c=relaxed/simple; bh=KLaNgRFrzo2Pm9t7NGXZe5L0zu2jf5hHRtAFOk/iNWE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oIHUaQU7cYWK0lvi64tKoD0YPOYAdAXsUR0I3B4wC+awyYyt+o95y8noI+BnMuTwHqV1WyedWKVrxEtPTne0QLfJjOgE4ZuHaJUhxwvc7+necfHLi5DlL8PaDkdoNWCoJONIY5PHygy3D5+1/BhWRGdN+P4Rl/aDWi3zphaaxYI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=iYoQL1dk; arc=fail smtp.client-ip=40.107.244.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="iYoQL1dk" 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 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; } From patchwork Wed May 1 08:51:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650357 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2059.outbound.protection.outlook.com [40.107.237.59]) (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 8D8C44E1A2; Wed, 1 May 2024 09:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.59 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554464; cv=fail; b=rCRKfNIYJw0uR6ruY/EDnb/Fc5ZFn95hVmahBIfsy4gHjbsJ0M6FZgFseuy4gkkgudfuIkGSuFEryFtXgtrFhNf/rkTNKGMnuMhC51v1X2tGdPV/Anha3tqk5uBQCCRjkX3fisMNMIaQDl+MTwHbeADVZVsgQM1dz0GCZIOqzz0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554464; c=relaxed/simple; bh=FJCMQvRyfp5Z1IhuXUvGBSE2rK2rAfv2nhEopFQTfmA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=d6or9r96i3V1wfOeIAQJk9nBBKX2pL1HfrvelHg1aGsQpcbMVfGyXGA3BFwkaeW6lLCJAmFiIvSBxftVh7vMuiQ2NUuFjztSrrmEaC7lIom90lIwddI02m2mBllYO2lyUMrugSbPmAvOoKh/r5QceQXGy6xZ5glKqP6gwzInnbU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=2aDYeyq8; arc=fail smtp.client-ip=40.107.237.59 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="2aDYeyq8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NrfnL/4mjbd9/o6FyIQYIt59G6MEDH//FXRWi/ebt0WLlKQyXkweaGi/OgcYb1JdAtcf7/JtbZyjYYCJ2A5aqCA1DAg0Don5suQ4N7VWdFJH5H2PI51rh8e87FMwNOYNX4RwehNt8cco7B+rVjnV0hzLFLCJG63aexodig7MRmEMIf0oZlCI6mEZce/TspMgdZ6o48VDHHqFJOvpNpXtoUF3iO05C/4SFQzaN98O6tCEfetWa9ThaNpc1b0KJHQCJy/fg1fPN5pp7qZ5CiAeYSpoUzitmWeFWr588cqYUiP9WFZPK52gM4n5NH/rd3HBtAWsC3zRtK/6KrFUkKWjnA== 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=cXRnpadbMHv81jR+cczjNT88FJu0zOLgWChtKNLc+po=; b=hDytNGQBKQLpX74Yx0NqUpPby0ciIfrKqJMPKLwPIYmv03TuStafmc9pzid9NkkNDtyV+CjeFGolVQe6qUlo0SQ9il1yj4zZJ7+5IMRU5ZQHcVbJcR015vTuxUnEMg2aZQS+TYMPsBv+k7YdaBs6xI1Qzm9V04swBKJ+bpG3QQ3zlNm1VQeg80eQwm/9P/LdHM5X5ljR/89DDhWgczGXACwldYVyiO9MTTSzEiFh/5PMYwMxWYMsGzgq+gTGhmH4RCK243LU84aBRFyjNUX07uxBG5Z7AZn0n39s43beoy31LZ3IYSiSX0iPOh2ZxjEZkUj1/QloEnSpM9rSwJdcDQ== 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=cXRnpadbMHv81jR+cczjNT88FJu0zOLgWChtKNLc+po=; b=2aDYeyq8y+2NRsrSvzgw7WwhHSbXZVJxrpwTiasorZgmr6XGN5P8I/PDiS6t2JBE/YpTcNVdlH0v4g8xganb23UKDSi7g/XXxzE5joVGeeCWR6pagZschyAEpQRHzdxIH6Fwz/0hRmls7oIjz5i+NdMUoUA1sdFLghTjNZTDDfw= Received: from BN8PR12CA0003.namprd12.prod.outlook.com (2603:10b6:408:60::16) by CY5PR12MB6431.namprd12.prod.outlook.com (2603:10b6:930:39::8) 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:07:38 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:408:60:cafe::30) by BN8PR12CA0003.outlook.office365.com (2603:10b6:408:60::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.27 via Frontend Transport; Wed, 1 May 2024 09:07:38 +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 BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) 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:38 +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:37 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 03/20] KVM: SEV: Select KVM_GENERIC_PRIVATE_MEM when CONFIG_KVM_AMD_SEV=y Date: Wed, 1 May 2024 03:51:53 -0500 Message-ID: <20240501085210.2213060-4-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A3:EE_|CY5PR12MB6431:EE_ X-MS-Office365-Filtering-Correlation-Id: 18d077b5-226f-4716-be0a-08dc69be261b 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: j0k5CPwcjjPjbOfcDghqoJIAlYyvZpwET3TvhL0PR2BdbAe2fSYfY3j1CKM9aLZ33xbyMVuA2XdsECLQU18FJaRBNVnU+5GkoWyqj5VCsmhYrOfzbes6y6I/7dixQ5dnGu3p1Z7zeYl3zJCPK7eZS/DtOmxImfkz9NaFyazU5aEemlQNIq6EFol1UhZsMwuCQvObYZr+LVgJIMks5MfSbK9/KmC81mkZRIMy8jj0OCing6PFX80Vd11gKhxEwAkb4QqfvaLQ2ou5jyMXy4lyKtnXFOmb+B8oZPpuXim0HbfGCSWAA+Aq4Xx35vL7fW8Pa1SMRkvNVLsKNlEHv03eB0fjRizztSYT+m0Q2PCky/0v6suVGWaZa904JKdSgMpISD0Brw204qBsD/a3B0HLzF+ExX0qJTYvkcwWCWVBaE/npterivJzlsJhQjkdUql53iUzgazgnAeQec//a48Z6GzFJbncDwPaC7GL4hisGH4bY+CKVb/2sljXD/Cmgur8cZ/O8x/Xvf277IZ2VCjxKZ1krkLjP102oioiovg87x28YzONG5NRi7LM5zLNFh+v6mywfP1jaYPP45EyFG711l4/pnOCehk7MHbmT+FOOO3jygM7A6BN85cXAwOGA/QfXoHMFSsPGC9b13Aw9LyPz5WPEQQd5Fg/3BV0LTTsamOqsdyIy3mdDjGmQ3ZPpPdDNk3AyjjGf95Ym68Q7kqCp/AavYObhGMKLZrzdR0MjJoyH5GuBS2L4P0k0Fpqkd20uzt5qLbOMhL7m4MOEXx3ffSxHlpJwCLaC5l/+zvnhXm01Dpf9eVJ4eUtyZZ7I//3U2K0KEggJL2dZ1/JtBIH976xhjUl4Z7F0awzdLGBBHed7sD66JTyV97o/sRsCZqhDgz0dyR4fE60Lg/moAszGelkFEnys8XeQC1DD7/BxDzBsCpusYl+VyQGcRBhq12SL4VUc//wfaBAjpSnTl45cPWarHkG//GGLn8K0u4IK2GCf4AqlPBo65BeuiPTdZrjoTv16P0qmFCbq0YkW3gQ30H+RarF5HiMQIsTKDGXGkLf0CJXEw1mM8j61PD4/zvbEywHXha3tbyoYvXD2J2Gg+9yz9z0ST97oJ4hsfP/Ej9OOPG9cAsaeIsjBVh2TekM6k98y58smQ873MiBpgb1wgE1f86yunCwXk6SmaRvYt3RohCfAkOqAOhPs+0Cuwbn/fFw/9sgl/YkgZSSw2N+epke3rIT29+IK3pSQEBU3gu6Xf8ZpeEX0AHicAwUIb0pFZna5PELQB7kyCE6zrFuSLNBbXoGPBg2F6mA3te8d03Qk5ttrj4SHBUppJFo12lO7cF0CRX6YXYxxKGzjVLSMg7pPstyP9h/I1honuuyUI/b1gRN/vkfxK8mGqFk9wHN 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:07:38.3197 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 18d077b5-226f-4716-be0a-08dc69be261b 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: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6431 SEV-SNP relies on private memory support to run guests, so make sure to enable that support via the CONFIG_KVM_GENERIC_PRIVATE_MEM config option. Signed-off-by: Michael Roth --- arch/x86/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index d64fb2b3eb69..5e72faca4e8f 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -136,6 +136,7 @@ config KVM_AMD_SEV depends on KVM_AMD && X86_64 depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) select ARCH_HAS_CC_PLATFORM + select KVM_GENERIC_PRIVATE_MEM help Provides support for launching Encrypted VMs (SEV) and Encrypted VMs with Encrypted State (SEV-ES) on AMD processors. From patchwork Wed May 1 08:51:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650358 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2084.outbound.protection.outlook.com [40.107.220.84]) (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 D2EDD4E1A2; Wed, 1 May 2024 09:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554486; cv=fail; b=IbGrGRkhwv+tQMpacLxV83vabE7qFOeHjRfxxe9xGLCnsE7iZVWAiWCmhhr+ZnoQ43sidMdNQoeXnT8zimcmfyP4GDY9rf+WKHGC3Q+Pb1gYomrQpxKchAGTYQ9Ll3GEkyNZj6SDRvGXi53D5recYtUOG7PLz9j7sCqaikc12Pw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554486; c=relaxed/simple; bh=Hp471QranugjUypt6cnWLkko5TpXESrkGYW44SWbfjs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sJHFsfqR3n6z4VG/CnBCS5pATtxoMiSdEgr9FoeEROKQVgfxkXRjeXllh1ORtuSdxZ0gX6HuGaJ51/gBBM74r4mO7qBCuYoAxRjD8/grW2wrD099pBOr9+MQSoJwCY0Ntz8iyoDnCWR2HueWV2BDTCAsw58vBoTzuDW5IfHyxmg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=lg3SOjLc; arc=fail smtp.client-ip=40.107.220.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="lg3SOjLc" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZiC5Tm8kFPvajn6mGnrcvyRYSagm42VsJqJZwwZQEAbWa1kFKbQ8aRWrxlDiHYyt0F/f1afQYXEa2Eg50iIFMruyBgjf71fMMaFrg/kCvBKldpXyNjF8w4fVng5h8Wne6lw2Xmp/bRzIsdR53ZuAqbW8Pt/NyVa0v+7DNEgbnH2KEatYbUdPNwBQmwB80yATRamoEGs9Ki4earNwNqsmyBrky6ToTmTw3vABsMmp85rZkKuseHIP0SiOxTX7fmXRD6aJjqiGmnDDCuSxjhjFk3QDOlJ9CY6aquU3O1oXKsyNsjrUtxpmVx/9hMoBnfqxjAw8hgxH6ufsvbQasX/jBw== 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=JoBb4Rvwn2Q5aMHC/+7SFu3Ry/fPDAnvq/GxIbd4pvU=; b=IKKMngiGJtjlFIa72Zxim5hcC44HcgbWcIVYlvGycXfnQepBeywuGazyZbEz3JgKs2f8vn/e8RH3ZLxjadWkX7GGquciRih11a4VNQNmBS9O6+9AnVewanSyoGKCgdbj3Z+2D61hmFkBRIvwHVPATfk5v26Hnmu4mHOBlNFQ5Bg2264r9UCZb43bVl48YLqucRAz3Iuxe3AWGSSQJjliIoXP1vBY530nl3gEIx+YgmEmrcEqto9ZBeUQtwnVnm4a8TEDmG0Ok+5m4Qj/wkXzxKV10AgVxX12gj1hZiri9G46TGndkHf0B+1/sk55wiz+4tSpuX8ej93Y/S5GBCGZng== 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=JoBb4Rvwn2Q5aMHC/+7SFu3Ry/fPDAnvq/GxIbd4pvU=; b=lg3SOjLcWCB6T8m9VFOQCDaqYzg4Jk5RoKC9DMn8ctQ60A8XJ6D2g+Zm8TdLJlMVBhpcQEyWXKpdfCFYs21Y0ofH+3B7jJsOoMN31XJaWe8PFLfzZA4FyCVsY+g92CChSw4GcQohE+wgC0/DyUIF6CHtWj2BixmesUpuauTypOg= Received: from BN9PR03CA0650.namprd03.prod.outlook.com (2603:10b6:408:13b::25) by DM4PR12MB7768.namprd12.prod.outlook.com (2603:10b6:8:102::21) 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:07:59 +0000 Received: from BN2PEPF000044A0.namprd02.prod.outlook.com (2603:10b6:408:13b:cafe::e0) by BN9PR03CA0650.outlook.office365.com (2603:10b6:408:13b::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.27 via Frontend Transport; Wed, 1 May 2024 09:07:59 +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:59 +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:58 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 04/20] KVM: SEV: Add initial SEV-SNP support Date: Wed, 1 May 2024 03:51:54 -0500 Message-ID: <20240501085210.2213060-5-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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_|DM4PR12MB7768:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e49441f-61cd-4171-1ec6-08dc69be32a0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|376005|82310400014|36860700004; X-Microsoft-Antispam-Message-Info: TVgMncpKV5z4L41/HScfFsZNk/okUuRy1J17QvGZRbgk36mh97fBpxLhS1pnZheiSe0z5eAQZTxZTfw17vEvBPsWecoFAJYliUMBYpkk7qOh5SAyufH93uXiG0LXQc06WFF6QDM+BFiSDPiMEdH6+U0cLZILOTUvuvPnLLrEGS7qnBRiGMzbKPlO2quOa19J9+LSO0y1SbX9ZEPq6A15I1U89PWQMXRJiVGbjJkehqGi1sXoEELaKsiYriXo0nxSJ2QdX3K9/2snHOmOhepNVdtRjoGXn7pBY0N/MsuRt3JstoE58KmhgPzUOF19fiN8XFX36gzkTMHarDTWE505bNNUNYRAOjacXvg8A1tDR4b9+IAl/xuQAJdkPjwzMYOpclV9t2CGgL1WkjqzWedZ7oZMe6aBDrVL3UTfwsGvva52jEirCIgxw181BuYFLddjqb55+h4jieTGEsAaPvDBKVDD6BP5Aq/cys14QtefPByjWoXMrK8DfM3QnVcy7CaSlAcx3ua0wiWdC2BIXu31cJ3OEYHawCPmo3pCAsuBqnUc0AlU1lxbNwDvc2ZaQ3D6cYasrJh+4mUEH2iUjrKdSv7SCdFZhMOXafVK2/J7oJ6mdWHG/E33O85untbH/iQM2PXrfEsGIYZZ5mlctxM0pEHi1KW+n2XFIkFeabg0nYhq0/XPmXhuIlmf9XcZ+NGC0gy1y0Ye9YXuSXZheiLBj/ZCDc6fROhFdCK3Bcyb3kZo4CgwOG3WD58beZ2zBHV1qdfOBWe0OCgYgeH4HxDVRpr2iK05pFyvNkn07gCaUrM1lkchIeBvBe+LFoEhDqZ7/iaIkd/oV0Hc6/dOEsLDL3F6PMupYcFOz+YkFIww87GBVLd1kuglLrKsqVwMtrFwVyMLEyHewdETCvAAzVwTbgqXJorsgwPbtsANlCv0Y9Xpcz4PA7Lq3mymK5XTr44tA4nP3PFhCL1iOss9iPQqvN9eSoBKEqsp2WlTz9ojJpKbKfWlKsX3YXhlcUSg1iz4a5/gQ/Cr1hlL08gC9xAjJHnxTVPRPT4DtJZmWuOADoqjJrzrxRgKCmIYSpE16w8jKi38axEu0dclSD6PGbFd/xlKGz72Djg3ABVrjbeEt50GnifVI2rPHgR/8Kf4TZ8fFfL45O9dP+LDj6ni5m1jOMIolciTCaRhHxSafUO0SlnBahaB6agHo+0U8nAAPF7VdaKXE7zhKV1KGDPYi5I3SzyPpJFriOA1g34O3bLQeUQZwB8aRyIZG40rmdFzlOQddQ6FuFX9x7GHQXbbGwrUiDKslS0+yuEK6zcmN5q6CoUGKD8cnS/V938zGcQqYbI49SCklzPnuJaPilY8pNnPUre5MP/EjOiukrKLs9hBFYP248lUHli6bzbtphknisDJ 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)(376005)(82310400014)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:07:59.3043 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e49441f-61cd-4171-1ec6-08dc69be32a0 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: DM4PR12MB7768 From: Brijesh Singh SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware-based security protection. SEV-SNP adds strong memory encryption and integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment. Define a new KVM_X86_SNP_VM type which makes use of these capabilities and extend the KVM_SEV_INIT2 ioctl to support it. Also add a basic helper to check whether SNP is enabled and set PFERR_PRIVATE_ACCESS for private #NPFs so they are handled appropriately by KVM MMU. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra Reviewed-by: Paolo Bonzini --- arch/x86/include/asm/svm.h | 3 ++- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 21 ++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 8 +++++++- arch/x86/kvm/svm/svm.h | 12 ++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 728c98175b9c..544a43c1cf11 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -285,7 +285,8 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) struct vmcb_seg { u16 selector; diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 9fae1b73b529..d2ae5fcc0275 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -874,5 +874,6 @@ struct kvm_hyperv_eventfd { #define KVM_X86_SW_PROTECTED_VM 1 #define KVM_X86_SEV_VM 2 #define KVM_X86_SEV_ES_VM 3 +#define KVM_X86_SNP_VM 4 #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a4bde1193b92..be831e2c06eb 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -47,6 +47,9 @@ module_param_named(sev, sev_enabled, bool, 0444); static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled; + /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true; module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444); @@ -288,6 +291,9 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, if (sev->es_active && !sev->ghcb_version) sev->ghcb_version = GHCB_VERSION_DEFAULT; + if (vm_type == KVM_X86_SNP_VM) + sev->vmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE; + ret = sev_asid_new(sev); if (ret) goto e_no_asid; @@ -348,7 +354,8 @@ static int sev_guest_init2(struct kvm *kvm, struct kvm_sev_cmd *argp) return -EINVAL; if (kvm->arch.vm_type != KVM_X86_SEV_VM && - kvm->arch.vm_type != KVM_X86_SEV_ES_VM) + kvm->arch.vm_type != KVM_X86_SEV_ES_VM && + kvm->arch.vm_type != KVM_X86_SNP_VM) return -EINVAL; if (copy_from_user(&data, u64_to_user_ptr(argp->data), sizeof(data))) @@ -2328,11 +2335,16 @@ void __init sev_set_cpu_caps(void) kvm_cpu_cap_set(X86_FEATURE_SEV_ES); kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_ES_VM); } + if (sev_snp_enabled) { + kvm_cpu_cap_set(X86_FEATURE_SEV_SNP); + kvm_caps.supported_vm_types |= BIT(KVM_X86_SNP_VM); + } } void __init sev_hardware_setup(void) { unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; + bool sev_snp_supported = false; bool sev_es_supported = false; bool sev_supported = false; @@ -2413,6 +2425,7 @@ void __init sev_hardware_setup(void) sev_es_asid_count = min_sev_asid - 1; WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count)); sev_es_supported = true; + sev_snp_supported = sev_snp_enabled && cc_platform_has(CC_ATTR_HOST_SEV_SNP); out: if (boot_cpu_has(X86_FEATURE_SEV)) @@ -2425,9 +2438,15 @@ void __init sev_hardware_setup(void) pr_info("SEV-ES %s (ASIDs %u - %u)\n", sev_es_supported ? "enabled" : "disabled", min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1); + if (boot_cpu_has(X86_FEATURE_SEV_SNP)) + pr_info("SEV-SNP %s (ASIDs %u - %u)\n", + sev_snp_supported ? "enabled" : "disabled", + min_sev_asid > 1 ? 1 : 0, min_sev_asid - 1); sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported; + if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) || !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP)) sev_es_debug_swap_enabled = false; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 535018f152a3..422b452fbc3b 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2056,6 +2056,9 @@ static int npf_interception(struct kvm_vcpu *vcpu) if (WARN_ON_ONCE(error_code & PFERR_SYNTHETIC_MASK)) error_code &= ~PFERR_SYNTHETIC_MASK; + if (sev_snp_guest(vcpu->kvm) && (error_code & PFERR_GUEST_ENC_MASK)) + error_code |= PFERR_PRIVATE_ACCESS; + trace_kvm_page_fault(vcpu, fault_address, error_code); return kvm_mmu_page_fault(vcpu, fault_address, error_code, static_cpu_has(X86_FEATURE_DECODEASSISTS) ? @@ -4899,8 +4902,11 @@ static int svm_vm_init(struct kvm *kvm) if (type != KVM_X86_DEFAULT_VM && type != KVM_X86_SW_PROTECTED_VM) { - kvm->arch.has_protected_state = (type == KVM_X86_SEV_ES_VM); + kvm->arch.has_protected_state = + (type == KVM_X86_SEV_ES_VM || type == KVM_X86_SNP_VM); to_kvm_sev_info(kvm)->need_init = true; + + kvm->arch.has_private_mem = (type == KVM_X86_SNP_VM); } if (!pause_filter_count || !pause_filter_thresh) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 9ae0c57c7d20..1407acf45a23 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -349,6 +349,18 @@ static __always_inline bool sev_es_guest(struct kvm *kvm) #endif } +static __always_inline bool sev_snp_guest(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_AMD_SEV + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return (sev->vmsa_features & SVM_SEV_FEAT_SNP_ACTIVE) && + !WARN_ON_ONCE(!sev_es_guest(kvm)); +#else + return false; +#endif +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Wed May 1 08:51:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650359 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2076.outbound.protection.outlook.com [40.107.102.76]) (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 2A63E1DFDE; Wed, 1 May 2024 09:08:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554508; cv=fail; b=E2pbFbRUksbukmN2dZbq7DgnT2l0jJ2o+KliIrmXEvFRpmwm0b37ah104Ps6ujh8jhnEzuML3fhMG4guPS/14cMZUtQTxREOTFdEW+ZV00dU/3cSZajpRVqVpGoL9o2H8yUENjdcEad6u07YXiHDfP54lRfT6SSLnZHOTyb/fLg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554508; c=relaxed/simple; bh=LrGlfh8N3J0VcVVcqYTLVIvmWQUZwnQsbxcYYH4gncA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=E/WuTAkDkJHtDjJSfOmQNdKc+HJ4ykyt1ie+CVGDvJI1TUccm18IyFEVPGdbdSKGaU6ekLU9tnjrbATpQrKprsRaa2D16X4lWgG8IxGYShb4fN2sFtF3CM9i1EpbNtS42AMuYSTgtbVktZni/7SYz0lz11yDDy3bPuichf0p+FI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=Diw++JkF; arc=fail smtp.client-ip=40.107.102.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="Diw++JkF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d1QOa8ndclrRVM5qVR+mmfH52EGUZV3RK/S2c8HZs62D36z/StNL7ct5ycboUUs0q4FuWfFarLALKF22CGB2bKp5ZuSkjBHEIWzedC7sgM/VckKU7jN8kjSf7hBeW+B5646dKUC0y16nUXQZ0HI/PgYVOPLbEERKXFrMEj1ptIP/Bi9T5HbR5oan6FQJ288pff9gR5dMnoPFN8BWjCPRPMTUUkVLZQb+082u2tNZHRs1iLeG+DSB0N2TyiCASrDq+MgQshSN3L2X4dsK9JGiKTOf572o4PKAMSKKfNTZG4dXpVCHu+ctMUYBEBYWVd/6m1h8Fd4tj4DnQP7nLlySgQ== 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=adchZnC4GHdiw/o4G0b7b23QArDQM7ZH8lxbcI2Pby8=; b=GB+NKVe7573fwrZPyBtqY4RXC29iBriq4G2D497uVNYsCGsKBgJDGNhwTIu4I8MnBysrbyfYeQuqXNAZ/h7oFYdqvU1AQzXW4FQ9vt1mY+SFSB929mAvoA284EP2nIiNlqHGPhjzJljskDS5JPvK3Yi11xMpM6CYK49jnPKerRTEzBd6ysaCmHywcV0uE4IRx375JIRXIksDpS72k5hKxYxoAmLOkm4bh5vXuVdyjxAz8GPMrq6YrzDTrmTRo60sg4GqBzbOpyNWQ3DZ4Rb/pjg2ezAhyGSi6vBINfaUWtY7FUJVgKpoaoNjNdWmiopok1FHjozCdqAaYIEFM35QyA== 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=adchZnC4GHdiw/o4G0b7b23QArDQM7ZH8lxbcI2Pby8=; b=Diw++JkFxMkkazWqrYpZAQk282Y7mkwXpde9NAeqSBgR5Kz0Lq61wpINtH9ssag+CRmVkelr1Rqpo8ZUmiDcZraGIuslUNtXpWxgLoMitVCw6s/vSb8EPfLUsvRz9R9WE6dSVQpjttfu6/3kJI7NeWicAgCiDxurvrZI1YmnTBQ= Received: from BN9PR03CA0776.namprd03.prod.outlook.com (2603:10b6:408:13a::31) by CH3PR12MB8972.namprd12.prod.outlook.com (2603:10b6:610:169::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35; Wed, 1 May 2024 09:08:23 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:408:13a:cafe::5b) by BN9PR03CA0776.outlook.office365.com (2603:10b6:408:13a::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35 via Frontend Transport; Wed, 1 May 2024 09:08: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 BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) 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:08:23 +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:08:22 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 05/20] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_START command Date: Wed, 1 May 2024 03:51:55 -0500 Message-ID: <20240501085210.2213060-6-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A3:EE_|CH3PR12MB8972:EE_ X-MS-Office365-Filtering-Correlation-Id: bd1a55a5-bec6-44ad-18d3-08dc69be40db X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|82310400014|36860700004|376005; X-Microsoft-Antispam-Message-Info: ANF41sZRnBIUdunTLSNN6C5+J/qB/uUjysDAGI5v0gVUq+fAgO7+XhJ134H5bf9acDkxuwCoBcfadufi/zvTfBvqrqhDV4bpJAmIgSJ7NdPwR0reEgDQCCtwJllVC55ijMMNHw3pdgCG7CyTAfaQVRMt8dhe+Zyj/nRsj54jxTGIuDmfny6OicsU2zfC8O0wWmOVULjMd5beEF8kQT4Oinypn6vBOnvKBB4Z0f+5MwaiLmGBSnHsgwyUBuJgshvQkvfHbgNXbFOKGy13m9v6XeR5ZCi4od2ab4l+zuCVHlHhLbuBt+cUW+f2cnoITLbTX/5U8HRCyzjLCs9eS9gIKooWfN45UFkl7R16LvrxpP+o1rzIOdZrDzsrzedHX82yoLw6tWuLDSDxI09p37kugFj96vTkevvq3Q98aaSF6L+G1Ob7b360F2zCwZna+rvXkmfPljZf2zta0enSNuN2d2eX7a9hhGtWRoaF+a4flheJJ9sVF5DWjT7jM90ZXk9YSy4FXo+7h5K34dqPEoVfpkU0PrcjGjylI/p7OpOm+kuQu7XlNdnLM7HNjOk3FWxpjHRYYYtARjS9qYuYZGEbAmzVy5Dx7c3SV0dqDiDa/gv0SksFfsTjZyVNXC86ootT3RH76gd2FofZiTtK9gNhVr2tFFXz0qXA5GiS1orvm6buytoIoiSNoz9I4Q5QXYMEqknV7XrGemLYKpbIQb1Jgy4dR67QoWmbzEm0DK1fpMxOZ6atdjz/cvco/H0ZiZGjCijsbrc5mTKD46stNOOBXrTOp08Gf9snf+Dc5Rs3R2r38LMSRF8z4AkLyzlAbFkvo+nKz4iRlTUNQdLkLrYl/yZstHY3vndpIlDj8ZKJS86GKFWQdBa+bff30j7Q5fg7HP4FvuucQHW8vTdW7pbv0EDndojJ3CmMxo7AoaiKqvcbfQhihQekwRf1CAYJFPgY3pkzmvFlm9CTMmtp+ZaCIIuIwXHarPSrw86T+M51zok+sa/0WJTC1sNYpu2scK/7/d21dNxDF9vEQNY/9OBKod5RhU1KOqFoBTRbW4a1qgxIbEvB5kKKnyoaog+BbFOX8JQxEHe/igF8YxYpsWsZipnxrkqefgTQEKIMm4esN/GDGIjH6tLydFg2HYTt8FlV1N06fhjvvEmiiBsja/InFeCgK91XjN7VqI1b1cvzfx21lhUJ7I74vXkiKhD5sFYH4KfAWKnzij4ppFr4JuFhqm5uDLyopBC0Kyvp2udTCi+ppWkcKEiV9jr9BU1SSPmpEHXwNbmZTjnxP/KNRuHTgeaDvE/lcQE2dqHnitIz0tk5p9BrIhgF4I7YeXtQgUm/ 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)(82310400014)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:08:23.1946 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bd1a55a5-bec6-44ad-18d3-08dc69be40db 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: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8972 From: Brijesh Singh KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. Other commands can then at that point be used to load/encrypt data into the guest's initial launch image. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra --- .../virt/kvm/x86/amd-memory-encryption.rst | 28 ++- arch/x86/include/uapi/asm/kvm.h | 11 ++ arch/x86/kvm/svm/sev.c | 176 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 212 insertions(+), 4 deletions(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index 9677a0714a39..dd179e162a87 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -466,6 +466,30 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SEV_SNP_LAUNCH_START +---------------------------- + +The KVM_SNP_LAUNCH_START command is used for creating the memory encryption +context for the SEV-SNP guest. It must be called prior to issuing +KVM_SEV_SNP_LAUNCH_UPDATE or KVM_SEV_SNP_LAUNCH_FINISH; + +Parameters (in): struct kvm_sev_snp_launch_start + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_start { + __u64 policy; /* Guest policy to use. */ + __u8 gosvw[16]; /* Guest OS visible workarounds. */ + __u16 flags; /* Must be zero. */ + __u8 pad0[6]; + __u64 pad1[4]; + }; + +See SNP_LAUNCH_START in the SEV-SNP specification [snp-fw-abi]_ for further +details on the input parameters in ``struct kvm_sev_snp_launch_start``. + Device attribute API ==================== @@ -497,9 +521,11 @@ References ========== -See [white-paper]_, [api-spec]_, [amd-apm]_ and [kvm-forum]_ for more info. +See [white-paper]_, [api-spec]_, [amd-apm]_, [kvm-forum]_, and [snp-fw-abi]_ +for more info. .. [white-paper] https://developer.amd.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf .. [api-spec] https://support.amd.com/TechDocs/55766_SEV-KM_API_Specification.pdf .. [amd-apm] https://support.amd.com/TechDocs/24593.pdf (section 15.34) .. [kvm-forum] https://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf +.. [snp-fw-abi] https://www.amd.com/system/files/TechDocs/56860.pdf diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index d2ae5fcc0275..693a80ffe40a 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -697,6 +697,9 @@ enum sev_cmd_id { /* Second time is the charm; improved versions of the above ioctls. */ KVM_SEV_INIT2, + /* SNP-specific commands */ + KVM_SEV_SNP_LAUNCH_START = 100, + KVM_SEV_NR_MAX, }; @@ -824,6 +827,14 @@ struct kvm_sev_receive_update_data { __u32 pad2; }; +struct kvm_sev_snp_launch_start { + __u64 policy; + __u8 gosvw[16]; + __u16 flags; + __u8 pad0[6]; + __u64 pad1[4]; +}; + #define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) #define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index be831e2c06eb..4676ce171aaa 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "mmu.h" #include "x86.h" @@ -59,6 +60,21 @@ static u64 sev_supported_vmsa_features; #define AP_RESET_HOLD_NAE_EVENT 1 #define AP_RESET_HOLD_MSR_PROTO 2 +/* As defined by SEV-SNP Firmware ABI, under "Guest Policy". */ +#define SNP_POLICY_MASK_API_MINOR GENMASK_ULL(7, 0) +#define SNP_POLICY_MASK_API_MAJOR GENMASK_ULL(15, 8) +#define SNP_POLICY_MASK_SMT BIT_ULL(16) +#define SNP_POLICY_MASK_RSVD_MBO BIT_ULL(17) +#define SNP_POLICY_MASK_DEBUG BIT_ULL(19) +#define SNP_POLICY_MASK_SINGLE_SOCKET BIT_ULL(20) + +#define SNP_POLICY_MASK_VALID (SNP_POLICY_MASK_API_MINOR | \ + SNP_POLICY_MASK_API_MAJOR | \ + SNP_POLICY_MASK_SMT | \ + SNP_POLICY_MASK_RSVD_MBO | \ + SNP_POLICY_MASK_DEBUG | \ + SNP_POLICY_MASK_SINGLE_SOCKET) + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -69,6 +85,8 @@ static unsigned int nr_asids; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context(struct kvm *kvm); + struct enc_region { struct list_head list; unsigned long npages; @@ -95,12 +113,17 @@ static int sev_flush_asids(unsigned int min_asid, unsigned int max_asid) down_write(&sev_deactivate_lock); wbinvd_on_all_cpus(); - ret = sev_guest_df_flush(&error); + + if (sev_snp_enabled) + ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, &error); + else + ret = sev_guest_df_flush(&error); up_write(&sev_deactivate_lock); if (ret) - pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); + pr_err("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n", + sev_snp_enabled ? "-SNP" : "", ret, error); return ret; } @@ -1998,6 +2021,106 @@ int sev_dev_get_attr(u32 group, u64 attr, u64 *val) } } +/* + * The guest context contains all the information, keys and metadata + * associated with the guest that the firmware tracks to implement SEV + * and SNP features. The firmware stores the guest context in hypervisor + * provide page via the SNP_GCTX_CREATE command. + */ +static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct sev_data_snp_addr data = {}; + void *context; + int rc; + + /* Allocate memory for context page */ + context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return NULL; + + data.address = __psp_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); + if (rc) { + pr_warn("Failed to create SEV-SNP context, rc %d fw_error %d", + rc, argp->error); + snp_free_firmware_page(context); + return NULL; + } + + return context; +} + +static int snp_bind_asid(struct kvm *kvm, int *error) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_activate data = {0}; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.asid = sev_get_asid(kvm); + return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error); +} + +static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_start start = {0}; + struct kvm_sev_snp_launch_start params; + int rc; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + /* Don't allow userspace to allocate memory for more than 1 SNP context. */ + if (sev->snp_context) + return -EINVAL; + + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + + if (params.flags) + return -EINVAL; + + if (params.policy & ~SNP_POLICY_MASK_VALID) + return -EINVAL; + + /* Check for policy bits that must be set */ + if (!(params.policy & SNP_POLICY_MASK_RSVD_MBO) || + !(params.policy & SNP_POLICY_MASK_SMT)) + return -EINVAL; + + if (params.policy & SNP_POLICY_MASK_SINGLE_SOCKET) + return -EINVAL; + + start.gctx_paddr = __psp_pa(sev->snp_context); + start.policy = params.policy; + memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw)); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error); + if (rc) { + pr_debug("%s: SEV_CMD_SNP_LAUNCH_START firmware command failed, rc %d\n", + __func__, rc); + goto e_free_context; + } + + sev->fd = argp->sev_fd; + rc = snp_bind_asid(kvm, &argp->error); + if (rc) { + pr_debug("%s: Failed to bind ASID to SEV-SNP context, rc %d\n", + __func__, rc); + goto e_free_context; + } + + return 0; + +e_free_context: + snp_decommission_context(kvm); + + return rc; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2021,6 +2144,15 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) goto out; } + /* + * Once KVM_SEV_INIT2 initializes a KVM instance as an SNP guest, only + * allow the use of SNP-specific commands. + */ + if (sev_snp_guest(kvm) && sev_cmd.id < KVM_SEV_SNP_LAUNCH_START) { + r = -EPERM; + goto out; + } + switch (sev_cmd.id) { case KVM_SEV_ES_INIT: if (!sev_es_enabled) { @@ -2085,6 +2217,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_FINISH: r = sev_receive_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_START: + r = snp_launch_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2280,6 +2415,31 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) return ret; } +static int snp_decommission_context(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_addr data = {}; + int ret; + + /* If context is not created then do nothing */ + if (!sev->snp_context) + return 0; + + /* Do the decommision, which will unbind the ASID from the SNP context */ + data.address = __sme_pa(sev->snp_context); + down_write(&sev_deactivate_lock); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL); + up_write(&sev_deactivate_lock); + + if (WARN_ONCE(ret, "Failed to release guest context, ret %d", ret)) + return ret; + + snp_free_firmware_page(sev->snp_context); + sev->snp_context = NULL; + + return 0; +} + void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -2321,7 +2481,17 @@ void sev_vm_destroy(struct kvm *kvm) } } - sev_unbind_asid(kvm, sev->handle); + if (sev_snp_guest(kvm)) { + /* + * Decomission handles unbinding of the ASID. If it fails for + * some unexpected reason, just leak the ASID. + */ + if (snp_decommission_context(kvm)) + return; + } else { + sev_unbind_asid(kvm, sev->handle); + } + sev_asid_free(sev); } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 1407acf45a23..d175059fa7c8 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -93,6 +93,7 @@ struct kvm_sev_info { struct list_head mirror_entry; /* Use as a list entry of mirrors */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; + void *snp_context; /* SNP guest context page */ }; struct kvm_svm { From patchwork Wed May 1 08:51:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650360 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2057.outbound.protection.outlook.com [40.107.94.57]) (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 07B2D1DFDE; Wed, 1 May 2024 09:08:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554531; cv=fail; b=jcW13NP+rVtVs/fp9VsfDPI/JMYGnZnaFcO0sHnoYgKY5YMebkN5JdcmM8Zt/BaOggrQhIGGiqPs1gOzz8bSuwW4fzdm9h+LLgFNoFnue5S+W9RjYyFLvRpcjY2Yyvvv28KUXVZuI3UqOe42zEkfEYv1mtcgF0d51f6/KaD0ra8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554531; c=relaxed/simple; bh=HhUZiTPLKI4dfVpuq0XBgFOOUfHPDQtyi4lDJRp77c0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=P1QMJDm+vjaJswRXzxRw2kQfvZLXDcsmW01WxysypKb1uDQbyK7CapAO+NpPeFV0n86FEkdBemsvPhwClQMi8pR9lwpQAscA9x+4p5+GYPl5Xr+h+kfZQzVErI52Fj6scQySx748Xlyf/A5OmKIJMokQLJGewO1KYnY174/LFS0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=4Yrt7Qos; arc=fail smtp.client-ip=40.107.94.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="4Yrt7Qos" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f8QeFKvXVCPtsJC/i7aFhXqdtY1jyEO2WpmlNvWbBgzvQpph/L7CTbiXu25B2//0BNImztXPiQySwS7LIhAIq3DWzfMfIM4axk0QmZshn3xKR0WJAmfSAqjIswTAo1nQQaORSi8gQvA5TiFskFjujOx7FV408NgpKDktMOcmeVryHKSx0Pxuxzvxto0WD1L1HGYuPdtBhWzZ90AXMx2kOwh/6xWFnbVO4o29D0meY3crLfHrgDAZi3fTwq2g25qnL64Y50/x02Hf+bUqa26qOwXTIbf9fgCjmNO0uzlyxf8SVykFY6exDHg3tc35qZzS9TOoGH75//E/n9QGuqlnaQ== 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=TMtwTcljvfpmFGi8ikmGc+WUjG3kns5eqcMGLb6+ovM=; b=NFvasFS7Vl9haVoNllAoM/kQlRPhPe8RHzsWr3AX0U9ZeAT6kZQ6XhVB5BYcZ8GkYDORlqe1w7q5QB2M7kjgfxTCO+YMkB1nnOb6wTAvViw6D5MyL8mFhKcJ4qpxh3R05vrdjtLbiSmzFohqO46w6AsUqcG5IH6HIcCnf4cdlpDdc/e0D0gsZT0FNwMDuMnFp90gRbQuKF969IiODDQMfK2RO+ThLT6JnK4K9Sqbd/TvrFa0QTtrXDX/wZ7PqR+hYWxWKqGHotfiLLVxksqUJvfGAHP5BbWpW72B/syQDfww60hFKuAwfn+HTRwsE6Vb3Dren0oYrzFx2KxoQtxYHw== 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=TMtwTcljvfpmFGi8ikmGc+WUjG3kns5eqcMGLb6+ovM=; b=4Yrt7Qosq9ZNy45QRReZp1feV3ndLJbUFysYvr5krjWkbxk98Q/bL900zAIOKe0jTMwYyljaF6l/Qz4AAO7aC9wPVkQNulrOJxAbH/OqKWB/NBQEKLg1TzuyQmR9BC8+Ny34R4qXywoy/knRMCSKT01nMZo7H1NlagCTS9eNTpI= Received: from BN9PR03CA0586.namprd03.prod.outlook.com (2603:10b6:408:10d::21) by PH0PR12MB7010.namprd12.prod.outlook.com (2603:10b6:510:21c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.29; Wed, 1 May 2024 09:08:45 +0000 Received: from BN2PEPF000044A2.namprd02.prod.outlook.com (2603:10b6:408:10d:cafe::c4) by BN9PR03CA0586.outlook.office365.com (2603:10b6:408:10d::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34 via Frontend Transport; Wed, 1 May 2024 09:08:44 +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 BN2PEPF000044A2.mail.protection.outlook.com (10.167.243.153) 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:08:44 +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:08:43 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 06/20] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Wed, 1 May 2024 03:51:56 -0500 Message-ID: <20240501085210.2213060-7-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A2:EE_|PH0PR12MB7010:EE_ X-MS-Office365-Filtering-Correlation-Id: a2579a33-0673-4ea6-7644-08dc69be4d5e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|36860700004|376005|1800799015|7416005; X-Microsoft-Antispam-Message-Info: YYuspEeVpV3Pn2EC5/RaN8YeyD38zjyCDjxwg8kK8R8wyxDNEQb42teo1p6dM/J2Y/aMusw5eEkKVNrVMZOHVYwYTnaUOW5tlAO+RFgoH6LpGD6sKDieJzx36knCpkjAshkbYZ9eOT1Nsk3+/p3T2IduNs+KcK/msyK/M5+/XmdUzNpa2SSHk62ZGzd+bprV4ramDMTKc9QK74wTMVYhLknahnNPgyiVBEcQ+mPC9uhXom9C8KJNGUg71X2ijfcjAVP7ela611fBJHjILjzbiz/wCv79mJE9R3+nnU+t4TvlmUiQiivQ+Rpx8viWgYLHM5DyI3jImOLE0o2Vt/bRZJOhs5BCCpRckjz7VHncJGgFtGT413+Vehp0yvSJwQAdAYUg8vfYF7frLn6ZvDmgiPMD75aDwGiw+yurx5aoQHZVymO/K+ICskBLfq8Dxq0zGFP1bWtVteOmEb1wmTgBTCLJLJHzCVCa9aMPxfB7Mfa49C7ja9PwGzf1f7/X9sslh17VdC9zTjJ6pNQwH+6oSfnUcOzoIN71Ndyh5UGTIR7QFZm36K4d7rFEAEpqtDvH9nPCfPcA4k3N0wl1iDY0vjs7ovMt3ST1uAQkXLMg2X1rRvF9wuuD2LetaSkNbeCXzD8/M0mKlptZe5k7RDe+fTBC/UOaaIHdsD6t34vBQH2geuUOTW8Az4G1h4E9R1IomoozsXxCdv50gAOp9vgtCQ9f+IkIxBcZ25igN9Npt8Lo3PUFK6ZP5dZ5CFTi++RvyKzXlF3n8M1V49RpChHMyuMxFh2ZWXW+rF+UsR91o7DBS42QOIbbFCsZ7vhd2CShUzM5uTf4w0nMtUZjC6bnucZnXEOSlXYNQQRwmU94vNGU/PtH9JXBRi39a9z/nOeimyDhajpCLWIZWcy6Zt39HikLkPBQmMoJslQjVYpevX1WKo/Hvo0Pnnwd/R6AT04+bhTzZENv0D2hbZfTyA8BK0DRdecnWcCzxoTNovgc+JcoNb5EHjA1YFH7SjAJ0lZoRpnSzxmX0utam/f7eIwi9MO108dUGmia541ilmIocxGgy2Ghm3uP85JzRHdeInrSyuUseAQY5ErWmoIjzduPmUFCpUzdvqsHkwdxws0MrjicvrRVajsokRqtvg6JAG++dAYds087UeS5s+JiKtik7Zlq+Ipy27L9QU5fWuEEzH363QJwfUWLIDG6xcLxBaO2nv/U5qMebS74EFGMxIgkI+Tt2d3Uze/UqDDXVh9St0Iox4hB/d5VFwD0L1uM3LK0DvV+vg932PkC1mGAkt2ydmcXjxE8JPCdlseliDupa49qlaiuda7GO/CDK+2r0ET9+XlYQTDm7RlMDUj1PF0bKij/juCFUl2UGHjpnjZkkLuzVyDMfntDh+6tAz/oupA+ 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)(36860700004)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:08:44.1881 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a2579a33-0673-4ea6-7644-08dc69be4d5e 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: BN2PEPF000044A2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7010 From: Brijesh Singh A key aspect of a launching an SNP guest is initializing it with a known/measured payload which is then encrypted into guest memory as pre-validated private pages and then measured into the cryptographic launch context created with KVM_SEV_SNP_LAUNCH_START so that the guest can attest itself after booting. Since all private pages are provided by guest_memfd, make use of the kvm_gmem_populate() interface to handle this. The general flow is that guest_memfd will handle allocating the pages associated with the GPA ranges being initialized by each particular call of KVM_SEV_SNP_LAUNCH_UPDATE, copying data from userspace into those pages, and then the post_populate callback will do the work of setting the RMP entries for these pages to private and issuing the SNP firmware calls to encrypt/measure them. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra --- .../virt/kvm/x86/amd-memory-encryption.rst | 54 ++++ arch/x86/include/uapi/asm/kvm.h | 19 ++ arch/x86/kvm/svm/sev.c | 230 ++++++++++++++++++ 3 files changed, 303 insertions(+) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index dd179e162a87..cc16a7426d18 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -490,6 +490,60 @@ Returns: 0 on success, -negative on error See SNP_LAUNCH_START in the SEV-SNP specification [snp-fw-abi]_ for further details on the input parameters in ``struct kvm_sev_snp_launch_start``. +19. KVM_SEV_SNP_LAUNCH_UPDATE +----------------------------- + +The KVM_SEV_SNP_LAUNCH_UPDATE command is used for loading userspace-provided +data into a guest GPA range, measuring the contents into the SNP guest context +created by KVM_SEV_SNP_LAUNCH_START, and then encrypting/validating that GPA +range so that it will be immediately readable using the encryption key +associated with the guest context once it is booted, after which point it can +attest the measurement associated with its context before unlocking any +secrets. + +It is required that the GPA ranges initialized by this command have had the +KVM_MEMORY_ATTRIBUTE_PRIVATE attribute set in advance. See the documentation +for KVM_SET_MEMORY_ATTRIBUTES for more details on this aspect. + +Upon success, this command is not guaranteed to have processed the entire +range requested. Instead, the ``gfn_start``, ``uaddr``, and ``len`` fields of +``struct kvm_sev_snp_launch_update`` will be updated to correspond to the +remaining range that has yet to be processed. The caller should continue +calling this command until those fields indicate the entire range has been +processed, e.g. ``len`` is 0, ``gfn_start`` is equal to the last GFN in the +range plus 1, and ``uaddr`` is the last byte of the userspace-provided source +buffer address plus 1. In the case where ``type`` is KVM_SEV_SNP_PAGE_TYPE_ZERO, +``uaddr`` will be ignored completely. + +Parameters (in): struct kvm_sev_snp_launch_update + +Returns: 0 on success, < 0 on error, -EAGAIN if caller should retry + +:: + + struct kvm_sev_snp_launch_update { + __u64 gfn_start; /* Guest page number to load/encrypt data into. */ + __u64 uaddr; /* Userspace address of data to be loaded/encrypted. */ + __u64 len; /* 4k-aligned length in bytes to copy into guest memory.*/ + __u8 type; /* The type of the guest pages being initialized. */ + __u8 pad0; + __u16 flags; /* Must be zero. */ + __u32 pad1; + __u64 pad2[4]; + + }; + +where the allowed values for page_type are #define'd as:: + + KVM_SEV_SNP_PAGE_TYPE_NORMAL + KVM_SEV_SNP_PAGE_TYPE_ZERO + KVM_SEV_SNP_PAGE_TYPE_UNMEASURED + KVM_SEV_SNP_PAGE_TYPE_SECRETS + KVM_SEV_SNP_PAGE_TYPE_CPUID + +See the SEV-SNP spec [snp-fw-abi]_ for further details on how each page type is +used/measured. + Device attribute API ==================== diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 693a80ffe40a..5935dc8a7e02 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -699,6 +699,7 @@ enum sev_cmd_id { /* SNP-specific commands */ KVM_SEV_SNP_LAUNCH_START = 100, + KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_NR_MAX, }; @@ -835,6 +836,24 @@ struct kvm_sev_snp_launch_start { __u64 pad1[4]; }; +/* Kept in sync with firmware values for simplicity. */ +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { + __u64 gfn_start; + __u64 uaddr; + __u64 len; + __u8 type; + __u8 pad0; + __u16 flags; + __u32 pad1; + __u64 pad2[4]; +}; + #define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) #define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 4676ce171aaa..f31f87655a67 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -259,6 +259,45 @@ static void sev_decommission(unsigned int handle) sev_guest_decommission(&decommission, NULL); } +/* + * Certain page-states, such as Pre-Guest and Firmware pages (as documented + * in Chapter 5 of the SEV-SNP Firmware ABI under "Page States") cannot be + * directly transitioned back to normal/hypervisor-owned state via RMPUPDATE + * unless they are reclaimed first. + * + * Until they are reclaimed and subsequently transitioned via RMPUPDATE, they + * might not be usable by the host due to being set as immutable or still + * being associated with a guest ASID. + */ +static int snp_page_reclaim(u64 pfn) +{ + struct sev_data_snp_page_reclaim data = {0}; + int err, rc; + + data.paddr = __sme_set(pfn << PAGE_SHIFT); + rc = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + if (WARN_ONCE(rc, "Failed to reclaim PFN %llx", pfn)) + snp_leak_pages(pfn, 1); + + return rc; +} + +/* + * Transition a page to hypervisor-owned/shared state in the RMP table. This + * should not fail under normal conditions, but leak the page should that + * happen since it will no longer be usable by the host due to RMP protections. + */ +static int host_rmp_make_shared(u64 pfn, enum pg_level level) +{ + int rc; + + rc = rmp_make_shared(pfn, level); + if (WARN_ON_ONCE(rc)) + snp_leak_pages(pfn, page_level_size(level) >> PAGE_SHIFT); + + return rc; +} + static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) { struct sev_data_deactivate deactivate; @@ -2121,6 +2160,194 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +struct sev_gmem_populate_args { + __u8 type; + int sev_fd; + int fw_error; +}; + +static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn_start, kvm_pfn_t pfn, + void __user *src, int order, void *opaque) +{ + struct sev_gmem_populate_args *sev_populate_args = opaque; + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + int n_private = 0, ret, i; + int npages = (1 << order); + gfn_t gfn; + + if (WARN_ON_ONCE(sev_populate_args->type != KVM_SEV_SNP_PAGE_TYPE_ZERO && !src)) + return -EINVAL; + + for (gfn = gfn_start, i = 0; gfn < gfn_start + npages; gfn++, i++) { + struct sev_data_snp_launch_update fw_args = {0}; + bool assigned; + int level; + + if (!kvm_mem_is_private(kvm, gfn)) { + pr_debug("%s: Failed to ensure GFN 0x%llx has private memory attribute set\n", + __func__, gfn); + ret = -EINVAL; + goto err; + } + + ret = snp_lookup_rmpentry((u64)pfn + i, &assigned, &level); + if (ret || assigned) { + pr_debug("%s: Failed to ensure GFN 0x%llx RMP entry is initial shared state, ret: %d assigned: %d\n", + __func__, gfn, ret, assigned); + ret = -EINVAL; + goto err; + } + + if (src) { + void *vaddr = kmap_local_pfn(pfn + i); + + ret = copy_from_user(vaddr, src + i * PAGE_SIZE, PAGE_SIZE); + if (ret) + goto err; + kunmap_local(vaddr); + } + + ret = rmp_make_private(pfn + i, gfn << PAGE_SHIFT, PG_LEVEL_4K, + sev_get_asid(kvm), true); + if (ret) + goto err; + + n_private++; + + fw_args.gctx_paddr = __psp_pa(sev->snp_context); + fw_args.address = __sme_set(pfn_to_hpa(pfn + i)); + fw_args.page_size = PG_LEVEL_TO_RMP(PG_LEVEL_4K); + fw_args.page_type = sev_populate_args->type; + + ret = __sev_issue_cmd(sev_populate_args->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &fw_args, &sev_populate_args->fw_error); + if (ret) + goto fw_err; + } + + return 0; + +fw_err: + /* + * If the firmware command failed handle the reclaim and cleanup of that + * PFN specially vs. prior pages which can be cleaned up below without + * needing to reclaim in advance. + * + * Additionally, when invalid CPUID function entries are detected, + * firmware writes the expected values into the page and leaves it + * unencrypted so it can be used for debugging and error-reporting. + * + * Copy this page back into the source buffer so userspace can use this + * information to provide information on which CPUID leaves/fields + * failed CPUID validation. + */ + if (!snp_page_reclaim(pfn + i) && !host_rmp_make_shared(pfn + i, PG_LEVEL_4K) && + sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_CPUID && + sev_populate_args->fw_error == SEV_RET_INVALID_PARAM) { + void *vaddr = kmap_local_pfn(pfn + i); + + if (copy_to_user(src + i * PAGE_SIZE, vaddr, PAGE_SIZE)) + pr_debug("Failed to write CPUID page back to userspace\n"); + + kunmap_local(vaddr); + } + + /* pfn + i is hypervisor-owned now, so skip below cleanup for it. */ + n_private--; + +err: + pr_debug("%s: exiting with error ret %d (fw_error %d), restoring %d gmem PFNs to shared.\n", + __func__, ret, sev_populate_args->fw_error, n_private); + for (i = 0; i < n_private; i++) + host_rmp_make_shared(pfn + i, PG_LEVEL_4K); + + return ret; +} + +static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_gmem_populate_args sev_populate_args = {0}; + struct kvm_sev_snp_launch_update params; + struct kvm_memory_slot *memslot; + long npages, count; + void __user *src; + int ret = 0; + + if (!sev_snp_guest(kvm) || !sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + pr_debug("%s: GFN start 0x%llx length 0x%llx type %d flags %d\n", __func__, + params.gfn_start, params.len, params.type, params.flags); + + if (!PAGE_ALIGNED(params.len) || params.flags || + (params.type != KVM_SEV_SNP_PAGE_TYPE_NORMAL && + params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO && + params.type != KVM_SEV_SNP_PAGE_TYPE_UNMEASURED && + params.type != KVM_SEV_SNP_PAGE_TYPE_SECRETS && + params.type != KVM_SEV_SNP_PAGE_TYPE_CPUID)) + return -EINVAL; + + npages = params.len / PAGE_SIZE; + + /* + * For each GFN that's being prepared as part of the initial guest + * state, the following pre-conditions are verified: + * + * 1) The backing memslot is a valid private memslot. + * 2) The GFN has been set to private via KVM_SET_MEMORY_ATTRIBUTES + * beforehand. + * 3) The PFN of the guest_memfd has not already been set to private + * in the RMP table. + * + * The KVM MMU relies on kvm->mmu_invalidate_seq to retry nested page + * faults if there's a race between a fault and an attribute update via + * KVM_SET_MEMORY_ATTRIBUTES, and a similar approach could be utilized + * here. However, kvm->slots_lock guards against both this as well as + * concurrent memslot updates occurring while these checks are being + * performed, so use that here to make it easier to reason about the + * initial expected state and better guard against unexpected + * situations. + */ + mutex_lock(&kvm->slots_lock); + + memslot = gfn_to_memslot(kvm, params.gfn_start); + if (!kvm_slot_can_be_private(memslot)) { + ret = -EINVAL; + goto out; + } + + sev_populate_args.sev_fd = argp->sev_fd; + sev_populate_args.type = params.type; + src = params.type == KVM_SEV_SNP_PAGE_TYPE_ZERO ? NULL : u64_to_user_ptr(params.uaddr); + + count = kvm_gmem_populate(kvm, params.gfn_start, src, npages, + sev_gmem_post_populate, &sev_populate_args); + if (count < 0) { + argp->error = sev_populate_args.fw_error; + pr_debug("%s: kvm_gmem_populate failed, ret %ld (fw_error %d)\n", + __func__, count, argp->error); + ret = -EIO; + } else { + params.gfn_start += count; + params.len -= count * PAGE_SIZE; + if (params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO) + params.uaddr += count * PAGE_SIZE; + + ret = 0; + if (copy_to_user(u64_to_user_ptr(argp->data), ¶ms, sizeof(params))) + ret = -EFAULT; + } + +out: + mutex_unlock(&kvm->slots_lock); + + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2220,6 +2447,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_START: r = snp_launch_start(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_UPDATE: + r = snp_launch_update(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; From patchwork Wed May 1 08:51:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650361 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2085.outbound.protection.outlook.com [40.107.93.85]) (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 ADAEF2C853; Wed, 1 May 2024 09:09:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554553; cv=fail; b=SXgM445q+Km7NTgUPx0IdTe2QMzWc+XFCKmIMiuAjgsfDvgflM/y7bD11TuxCJRRPMrN6UfwOUFXQyhFgYCtJZTbukYTMY2xfcCNmCQUeLDkqALd2WC5e8U6OFypyhZ5/sxPVyF1iaCig2Cwl5QBqvpbrPeMeFYaM2/mJuWoi9k= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554553; c=relaxed/simple; bh=5SXTk4ZwXeEZDw7TUL8qxCNH2s/qKRSP1Wben3jPzqo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TtkLTYSrJEEIcdtGVX8RLAKUcE2oBTQUTNA3Z2nTW4nyGbGPZ8ekH1Iy48JlJbS7cCXqFa2qxDrRQw9Q6pIQyxvdpnuOnqbOUNHPjRO29SsoU0YttC+dOQNhuPs72StLwLz0Gmr7FIYiczV/EoGktPrfSJDq45DdBRzi9zX4v3s= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=dtwkBlX+; arc=fail smtp.client-ip=40.107.93.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="dtwkBlX+" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SAl0Q8JY0GCH40JK+81sA+6jxeK3gsIJoUi4qed+Ee0lz1c65tF+VbDNbUNHtHjL7ijD0SbH3w54opLxXF+pisYC7u2eSZpAdgRG20+mRmT58LztSfwMncLJSUdqWvuTV6pSa3M7cplbOIoBGhehEKOjneKMDjwBXHeteasSg1LsMUaJrOd9AM0esbe2FegUmbM+DyUbekjRuJkg/Cw8y2f9Q132kkgIEuTBtx8jkoDR+AuLDg5lyyU/yzCKmFqS083Ds/MQUF5gTWYmplII0EVSEjm0Hc4JnR+/balXzZfcmBtqCTPAqg8IT+rvkldgBFWLYzeVWe2/ebzcEYh7Yg== 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=dYQYofULTsvq+RGAK48Bsi2yQW2HzXb5HOdHe6aXetE=; b=J8L/pyVOK9vKZGde06f3kT1ftJhwPTyA2f27OhMxB1p5/pEgACbegtmDa9po8RrB4Cgf3taA6y58xQ5ar1ZIEroFuPCsEACAdHMmUd7A7YQCxYYFVJYrClTvZy2qMn4jhD81I400K11Xj60HoATOXZYsUFTBmZKQh4Hhys2Q5baAWxg0xBY8QOFk8V7GyvmT1KdETmGRTpdlPbNjZnWhppk3Ok8NMIeBD/Do/tFHLtU0RZSKZ6bkTV7QilMN4f+/TJLS+bwCcmhu3NpmSRwBaoRrdRwP+xBqO8j6aYk+Dhduf9yps2Tep5tXeoiXb5KK3Xbqc7EG6iwG0/ra5g8Gng== 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=dYQYofULTsvq+RGAK48Bsi2yQW2HzXb5HOdHe6aXetE=; b=dtwkBlX+5bkBElTr6Wj+jvmJyoNUiRLK89bfKXhM2BwnzaYpzjqSpuY0j4/znIPrakRd0AgkAdegUTNbi4AXzh2HG1eU8cPiWH+oNu3mvsefgHZtsbBTYxxTOP4CoX70RzokWWlEJRzlAityLiKrFADB7rmHFF2IPzA+focIMig= Received: from BN9PR03CA0601.namprd03.prod.outlook.com (2603:10b6:408:106::6) by IA1PR12MB7733.namprd12.prod.outlook.com (2603:10b6:208:423::17) 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:09:08 +0000 Received: from BN2PEPF0000449E.namprd02.prod.outlook.com (2603:10b6:408:106:cafe::8c) by BN9PR03CA0601.outlook.office365.com (2603:10b6:408:106::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.29 via Frontend Transport; Wed, 1 May 2024 09:09:08 +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 BN2PEPF0000449E.mail.protection.outlook.com (10.167.243.149) 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:09:08 +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:09:07 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Harald Hoyer Subject: [PATCH v15 07/20] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Wed, 1 May 2024 03:51:57 -0500 Message-ID: <20240501085210.2213060-8-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF0000449E:EE_|IA1PR12MB7733:EE_ X-MS-Office365-Filtering-Correlation-Id: 794f380b-3a05-4235-0915-08dc69be5bc1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|36860700004|82310400014|376005|7416005; X-Microsoft-Antispam-Message-Info: sWCowbIWfbJmJGNSiizYeGpGUwCk0iftfK0FREu4oVMlqcd6nYhes/xdE4XF+dduVXbj8oiLce1A83aG1MMufiQFoM3//CxkhkjrzbIcxPuze2qRgSGC2d5NWbaPCnhOLtWetburlDY8OGuygESDcABKmu7nIGQvgI32b8vkIUyPXeBEUJWgrfQpb3h1r8iH7WevHzM+YhWgriCei8ajLXFQo5q0R4ELG6Hd93Yhxkxo2DwbqzDNabPx6bYdEDSSrj7VpcQu7YwBCYvUSClNb60J1ae2hOowaW6kYmvK9UA+F5a1YwVEWBofwStwtW7cCf1a/MqGDPTaNPoSlHnPQ6k6Vr5EtA6+gCoPDpDqN5tdVs8YcnH3/N342bDJDhV81NvbaC5yWmJV0Cj1zmdmVY+RjGPbh7uLyjUDv/WBtoI4e5tqq26XJgpP+su5kweo5LDEYZZJsIrZcNNTPjzuf8t6u4JQgkVyn0rLUH4yVE3dF7hqMpf4tF+PfgppZuWlBV2D/afKvaDwKaMgbwCYPWUk/AF3soyV395E11N8G5HQeylkKSqm6KLj9q/ZytSxCOBrVNxJIu6/HZF9RgX14EuaRIir7r4vVrBNQySJbMWjIUsMMHzZxPr78w08y5FeAgGt4hPvPfLk/7RovsKLgGpxPcGKcCjgV1lcVfkpyez7jyS1s0bM0ul9NwzCsSFQ0abW/iqh4zMelS3FpwgGx9B705zmuTCRsr5sW+RlHPIFLfD1fe4ZptzqTA1SGSPw1nJURL7+GK9l1Li9B1oVQTMteHW2ufTjaPTySduepKIZS9v91ZPPAQKHCi96iWo0iLWv5b6USgzgZpVeJbYflBY5MtaxZ2wdxmIrigyX4+kpiagt8NvnJ288biRONW2SUtk80CxjXJUaexPzMP55pCs0y08XstQ6WJmLPCZw6GCr2uYHADdXOj26SkLQeWEhbS2oIG5BC5/udDmDgpz+PRdIrYRdM9+X9zy4JtT6jHubaWTS0DhrYYQXbnmjHWVmYKKphRKoj2+bL4/MVmmPnHuz3ujgG4PEK3td33d44JrAQYP0WVJdXozFwY9RXdTQNxw5uj72lxP+b6cQX5R1kKwoNPISn0NyUNKliUMZAZNjE0n0cpcE30+uAOT80Ioeo6jwBNS0n+B9Ph0ZcKxn56nKfHRlfF2OAwdeuPZTPrh6y2+zVM0D+pmJOoImO9y4SFE9Gmay6GrnH6JOZrc3hqoULPNTH0ehVPC26RLguKknnpmBG4hF2zeaWQsdfbvMXRrLw5T9ofh9SDFiCtiMlQQebufowdzfw8iPcYrLuI7wrLj85GUJ2/6eei1VxDv1zR0QJX8n4IKtJypK2houSs8QbDcGxoL963LcQK6CIBWgCA28s4cXHevzu9Oib/WG 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)(36860700004)(82310400014)(376005)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:09:08.3066 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 794f380b-3a05-4235-0915-08dc69be5bc1 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: BN2PEPF0000449E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7733 From: Brijesh Singh Add a KVM_SEV_SNP_LAUNCH_FINISH command to finalize the cryptographic launch digest which stores the measurement of the guest at launch time. Also extend the existing SNP firmware data structures to support disabling the use of Versioned Chip Endorsement Keys (VCEK) by guests as part of this command. While finalizing the launch flow, the code also issues the LAUNCH_UPDATE SNP firmware commands to encrypt/measure the initial VMSA pages for each configured vCPU, which requires setting the RMP entries for those pages to private, so also add handling to clean up the RMP entries for these pages whening freeing vCPUs during shutdown. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Harald Hoyer Signed-off-by: Ashish Kalra --- .../virt/kvm/x86/amd-memory-encryption.rst | 28 ++++ arch/x86/include/uapi/asm/kvm.h | 17 +++ arch/x86/kvm/svm/sev.c | 127 ++++++++++++++++++ include/linux/psp-sev.h | 4 +- 4 files changed, 175 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/x86/amd-memory-encryption.rst b/Documentation/virt/kvm/x86/amd-memory-encryption.rst index cc16a7426d18..1ddb6a86ce7f 100644 --- a/Documentation/virt/kvm/x86/amd-memory-encryption.rst +++ b/Documentation/virt/kvm/x86/amd-memory-encryption.rst @@ -544,6 +544,34 @@ where the allowed values for page_type are #define'd as:: See the SEV-SNP spec [snp-fw-abi]_ for further details on how each page type is used/measured. +20. KVM_SEV_SNP_LAUNCH_FINISH +----------------------------- + +After completion of the SNP guest launch flow, the KVM_SEV_SNP_LAUNCH_FINISH +command can be issued to make the guest ready for execution. + +Parameters (in): struct kvm_sev_snp_launch_finish + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 vcek_disabled; + __u8 host_data[32]; + __u8 pad0[3]; + __u16 flags; /* Must be zero */ + __u64 pad1[4]; + }; + + +See SNP_LAUNCH_FINISH in the SEV-SNP specification [snp-fw-abi]_ for further +details on the input parameters in ``struct kvm_sev_snp_launch_finish``. + Device attribute API ==================== diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 5935dc8a7e02..988b5204d636 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -700,6 +700,7 @@ enum sev_cmd_id { /* SNP-specific commands */ KVM_SEV_SNP_LAUNCH_START = 100, KVM_SEV_SNP_LAUNCH_UPDATE, + KVM_SEV_SNP_LAUNCH_FINISH, KVM_SEV_NR_MAX, }; @@ -854,6 +855,22 @@ struct kvm_sev_snp_launch_update { __u64 pad2[4]; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 vcek_disabled; + __u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE]; + __u8 pad0[3]; + __u16 flags; + __u64 pad1[4]; +}; + #define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) #define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f31f87655a67..797230f810f8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -75,6 +75,8 @@ static u64 sev_supported_vmsa_features; SNP_POLICY_MASK_DEBUG | \ SNP_POLICY_MASK_SINGLE_SOCKET) +#define INITIAL_VMSA_GPA 0xFFFFFFFFF000 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -2348,6 +2350,115 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update data = {}; + struct kvm_vcpu *vcpu; + unsigned long i; + int ret; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.page_type = SNP_PAGE_TYPE_VMSA; + + kvm_for_each_vcpu(i, vcpu, kvm) { + struct vcpu_svm *svm = to_svm(vcpu); + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + ret = sev_es_sync_vmsa(svm); + if (ret) + return ret; + + /* Transition the VMSA page to a firmware state. */ + ret = rmp_make_private(pfn, INITIAL_VMSA_GPA, PG_LEVEL_4K, sev->asid, true); + if (ret) + return ret; + + /* Issue the SNP command to encrypt the VMSA */ + data.address = __sme_pa(svm->sev_es.vmsa); + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, &argp->error); + if (ret) { + if (!snp_page_reclaim(pfn)) + host_rmp_make_shared(pfn, PG_LEVEL_4K); + + return ret; + } + + svm->vcpu.arch.guest_state_protected = true; + } + + return 0; +} + +static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct kvm_sev_snp_launch_finish params; + struct sev_data_snp_launch_finish *data; + void *id_block = NULL, *id_auth = NULL; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, u64_to_user_ptr(argp->data), sizeof(params))) + return -EFAULT; + + if (params.flags) + return -EINVAL; + + /* Measure all vCPUs using LAUNCH_UPDATE before finalizing the launch flow. */ + ret = snp_launch_update_vmsa(kvm, argp); + if (ret) + return ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + if (params.id_block_en) { + id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE); + if (IS_ERR(id_block)) { + ret = PTR_ERR(id_block); + goto e_free; + } + + data->id_block_en = 1; + data->id_block_paddr = __sme_pa(id_block); + + id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE); + if (IS_ERR(id_auth)) { + ret = PTR_ERR(id_auth); + goto e_free_id_block; + } + + data->id_auth_paddr = __sme_pa(id_auth); + + if (params.auth_key_en) + data->auth_key_en = 1; + } + + data->vcek_disabled = params.vcek_disabled; + + memcpy(data->host_data, params.host_data, KVM_SEV_SNP_FINISH_DATA_SIZE); + data->gctx_paddr = __psp_pa(sev->snp_context); + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error); + + kfree(id_auth); + +e_free_id_block: + kfree(id_block); + +e_free: + kfree(data); + + return ret; +} + int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -2450,6 +2561,9 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_UPDATE: r = snp_launch_update(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_FINISH: + r = snp_launch_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2940,11 +3054,24 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); + /* + * If it's an SNP guest, then the VMSA was marked in the RMP table as + * a guest-owned page. Transition the page to hypervisor state before + * releasing it back to the system. + */ + if (sev_snp_guest(vcpu->kvm)) { + u64 pfn = __pa(svm->sev_es.vmsa) >> PAGE_SHIFT; + + if (host_rmp_make_shared(pfn, PG_LEVEL_4K)) + goto skip_vmsa_free; + } + if (vcpu->arch.guest_state_protected) sev_flush_encrypted_page(vcpu, svm->sev_es.vmsa); __free_page(virt_to_page(svm->sev_es.vmsa)); +skip_vmsa_free: if (svm->sev_es.ghcb_sa_free) kvfree(svm->sev_es.ghcb_sa); } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 3705c2044fc0..903ddfea8585 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -658,6 +658,7 @@ struct sev_data_snp_launch_update { * @id_auth_paddr: system physical address of ID block authentication structure * @id_block_en: indicates whether ID block is present * @auth_key_en: indicates whether author key is present in authentication structure + * @vcek_disabled: indicates whether use of VCEK is allowed for attestation reports * @rsvd: reserved * @host_data: host-supplied data for guest, not interpreted by firmware */ @@ -667,7 +668,8 @@ struct sev_data_snp_launch_finish { u64 id_auth_paddr; u8 id_block_en:1; u8 auth_key_en:1; - u64 rsvd:62; + u8 vcek_disabled:1; + u64 rsvd:61; u8 host_data[32]; } __packed; From patchwork Wed May 1 08:51:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650362 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2065.outbound.protection.outlook.com [40.107.220.65]) (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 27E4A4E1CE; Wed, 1 May 2024 09:09:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.65 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554575; cv=fail; b=H18O+gLb9CxDDnANlmnHWEmiZ0/EhfAhFfHgXYptIE+ogDOCojvB6lJzySsLEMycS8RvoNAh5c5Si8QwizOgrwQScAhfO9mgPAfaPSjVIZ/FZNrpbjWk8y6JpHs3qbpS4gXNzRKWheUPUvWzNRpm1Qjbp4yU7khFXXYSfeFUA4Y= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554575; c=relaxed/simple; bh=836ytDsdBQ7gXrWqPodCXtxsPop0IKrnMf73+Ey6uGA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VuMn7BEyAYUeHmZ3eWePhxrwebSaKS0F21li0CUk6sqctTQJoFIdfZav/C5AozwoUEKdtztCnk84hIrcS8CuQl/cWazd3XUQ562FA8gpKyTtQ/FLcgILn7Ep1UlGUeoVCDe16Xy3NLbHvDXycgRSMDcVrfWbzthnmzr3qpwvnGQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=2eyGDdfs; arc=fail smtp.client-ip=40.107.220.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="2eyGDdfs" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iehqxKffHJWHxchRrLta++7MmWW93auhdKgF1Qk7lDSjqMAiEYWADCpchLowcfzUbUukIn8y7X2TB5QC+AJrzaEZSuIvTTBNxfGsNiLZbi/w2ZEKngfQMbGqECs50x5aG+r7ESkGlCVxxrXShk6EK5X7I5+J128mg09FLnHD5nDiWztGgIy+tVZZazgrC/hHmY6+z4Hs+PyGlaMk/J0kcbWUVdznJDApTiA16IHAZCiobsQuyZUglDLdSauxMTOf5O5Bu6SyNVHayD/Rpzg5eJ3TPv2iJlI/gPoBJFVs1oVwjH00ZTd61QLMsdeKFdP07yRo1pvKrSnLhmTSXADimw== 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=jmmI4Fmr+tDaQB0oEsfFicY+rShpIyOPOkhjlKpDm2M=; b=OIrI9mFe84ej1Mtetqr6M+FkaSv4HPVva7jR8kur3o6yxrcMbou5CudPL6z/qz0UUX5bzzRo3bBSzHJ9OlmgfibhxTuktpPsa8BWwWtg+5CiKamjEFYncM59QtYJXWaPKQjGpLp1H9lmpPDgEqtEqr0TsjhKAGbs2pEQrDiJL3PQTvZKDdObv+OSivC2KPbJqXazV6WjrgziAqdfMH5pAoYy3l8GsP05GjINTIpNAWrx+FAh9eL1c04eoa5/7FHwJ2X2Zm2jxw7QX96xhodKeOMY99rTlGKJSLC8xFieGDyTiArg0PkMGA+kr5MGs0anfOryUKywjFAQ1HFBDFcM4A== 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=jmmI4Fmr+tDaQB0oEsfFicY+rShpIyOPOkhjlKpDm2M=; b=2eyGDdfsaCRJdX684JzWgEIOKRi1IbDI9qITc1PnHgBhxZAYkFtd57yF1vvoqtE9BdCLKa0YZrmeM3V/IAvv9jzrbDji4UGSg1JxtJ60wThUnRrdMd5RDbHvEgYLz4OBQqhVv8DzY/FItNc4FxR9HpZdk6+YfDeBUqsdo7yDHMM= Received: from BN9PR03CA0588.namprd03.prod.outlook.com (2603:10b6:408:10d::23) by DS0PR12MB8069.namprd12.prod.outlook.com (2603:10b6:8:f0::7) 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:09:29 +0000 Received: from BN2PEPF000044A2.namprd02.prod.outlook.com (2603:10b6:408:10d:cafe::ae) by BN9PR03CA0588.outlook.office365.com (2603:10b6:408:10d::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.34 via Frontend Transport; Wed, 1 May 2024 09:09:29 +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 BN2PEPF000044A2.mail.protection.outlook.com (10.167.243.153) 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:09:28 +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:09:28 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 08/20] KVM: SEV: Add support to handle GHCB GPA register VMGEXIT Date: Wed, 1 May 2024 03:51:58 -0500 Message-ID: <20240501085210.2213060-9-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A2:EE_|DS0PR12MB8069:EE_ X-MS-Office365-Filtering-Correlation-Id: cf70d100-2436-4e2a-96da-08dc69be680f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400014|7416005|36860700004|376005; X-Microsoft-Antispam-Message-Info: KjOOh29vRC6UQhfcvYqHmaK2jwfQIFkqvW2fYq9f9UYbNn7NpN0MsdBz6TExiRZMX+ZApTODie6TuexahIhaQkZn2uql+AYWLG+MR3JQfgBS4UHVM/VGz3QpPT/Wy8mTdapZ127MsELvryob37ffCddL0ciloFk/lYslsa4PfBvh5n/2pGVz8UPNUcoOleq6nPRahlodbGSx1cUw7GAUKXDX3H230ag3L1CdxoWpA0il0nDTT9sKvIMmeAtEkL9tQvHKzaDUW6SQXAsTGMGdbu0Br/qXhJH5QPewCL9ec9hCBacsSl0j8650azeUNc/O4AlMNd07U72e0nIrHYUCvbefGtQTm59yvSiiM7VPhePO6eTg0/YlD2kEeSSEJlQ9rsvxvnoDD9Um0+DZuG8smQDr3wVhzvO6JmDyz40mEjCKJkiOZBM5JRPJekgHcXdhKhIT/HagiwB91782xufKMOFT3EgtR1oFeSrMx0uG4+ToxsCoA3bA8ZZmV1npoWOX8T69JUuBggt8AT1n2StcYd7fzrCAHGyidTjL/Xyw7bNjd6JwmvKjMf0s3CCzeD1f/Z0jMUCUsLQZ/jG6hVukUMDfHOg56C+n/1rCvBVCaMsznVEkiCH1xPZ9VbMNaiMf5op3PkBP0T2/7s9LTYksUHdQrLByykPL1k8GyKtIzq2DCjtUoBpDTgI6HGnpMKG+40EcR1WA1d7LGT2pTsELV7fQ3PTy5ucWPr6kjCpI1TRXREcGhIHKjkFtR0hVqfLXlyuQTPuvYwTLcx4RaGjJra2y2jxxqiKMrPfrym9Y13mxqVsHmJB10z2mddxYWVQ+XLrDbK3IRSdHyo8LNR/KMIoYXFPsfxHEAcDcT4x2zVzFq08Ig+mI3Y0hWQniCioTrL6GrEOTkSIwRft/rkkfFRTgVzcpFMrFW/C5N3UqeqE8O3cuALxsPb/TRe64Iuymxr1qXQgBjR4I/QFyAKkHusfM77fODSRUcXS3P1i/ewfj1i5kjfxBOOj52N2llgdFxDKMgvdx645XwPXXNjnVG8q1Jii72PyGNax7fq73TctNsJOUKjOX2rPpt0ZUMEPeWmo6ppE9IFD3BrHcOKuVv/mMYlHsJsTGAYw4C7dLndeXEY8Cjq/sqIDWVetJAYk+5IoN2v9U+2Pkf/Qv8mMpp9Pa8JgWcJULy8teLX1aloN/ljmpOj0RU5GS8JrubOf61nbkHtH94W97sq4yex7eI2ly+xD4JA1TPERa6a8H3QqqsD0RqWywdBYEZtiBPDTOSMsIo5xLV9ppFFlyDluWEXLPihXw/NwGmzDkaUOUwEzaY5XhrpNApfuNDmzY0Q0vDFClr+FSYjnCvK6KkZvPG9wjty4bfPZK8OA09ybwuE2AXrwv2di4ufaUm3qytcZb 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)(82310400014)(7416005)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:09:28.9697 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cf70d100-2436-4e2a-96da-08dc69be680f 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: BN2PEPF000044A2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8069 From: Brijesh Singh SEV-SNP guests are required to perform a GHCB GPA registration. Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1. On VMEXIT, verify that the GHCB GPA matches with the registered value. If a mismatch is detected, then abort the guest. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/sev-common.h | 8 ++++++ arch/x86/kvm/svm/sev.c | 48 +++++++++++++++++++++++++++---- arch/x86/kvm/svm/svm.h | 7 +++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 5a8246dd532f..1006bfffe07a 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -59,6 +59,14 @@ #define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 #define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) +/* Preferred GHCB GPA Request */ +#define GHCB_MSR_PREF_GPA_REQ 0x010 +#define GHCB_MSR_GPA_VALUE_POS 12 +#define GHCB_MSR_GPA_VALUE_MASK GENMASK_ULL(51, 0) + +#define GHCB_MSR_PREF_GPA_RESP 0x011 +#define GHCB_MSR_PREF_GPA_NONE 0xfffffffffffff + /* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012 #define GHCB_MSR_REG_GPA_REQ_VAL(v) \ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 797230f810f8..e1ac5af4cb74 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3540,6 +3540,32 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; + case GHCB_MSR_PREF_GPA_REQ: + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_NONE, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PREF_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + case GHCB_MSR_REG_GPA_REQ: { + u64 gfn; + + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + + svm->sev_es.ghcb_registered_gpa = gfn_to_gpa(gfn); + + set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_VALUE_MASK, + GHCB_MSR_GPA_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_REG_GPA_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -3552,12 +3578,7 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) pr_info("SEV-ES guest requested termination: %#llx:%#llx\n", reason_set, reason_code); - vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; - vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM; - vcpu->run->system_event.ndata = 1; - vcpu->run->system_event.data[0] = control->ghcb_gpa; - - return 0; + goto out_terminate; } default: /* Error, keep GHCB MSR value as-is */ @@ -3568,6 +3589,14 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) control->ghcb_gpa, ret); return ret; + +out_terminate: + vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; + vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM; + vcpu->run->system_event.ndata = 1; + vcpu->run->system_event.data[0] = control->ghcb_gpa; + + return 0; } int sev_handle_vmgexit(struct kvm_vcpu *vcpu) @@ -3603,6 +3632,13 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) trace_kvm_vmgexit_enter(vcpu->vcpu_id, svm->sev_es.ghcb); sev_es_sync_from_ghcb(svm); + + /* SEV-SNP guest requires that the GHCB GPA must be registered */ + if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) { + vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa); + return -EINVAL; + } + ret = sev_es_validate_vmgexit(svm); if (ret) return ret; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index d175059fa7c8..bbfbeed4c676 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -209,6 +209,8 @@ struct vcpu_sev_es_state { u32 ghcb_sa_len; bool ghcb_sa_sync; bool ghcb_sa_free; + + u64 ghcb_registered_gpa; }; struct vcpu_svm { @@ -362,6 +364,11 @@ static __always_inline bool sev_snp_guest(struct kvm *kvm) #endif } +static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) +{ + return svm->sev_es.ghcb_registered_gpa == val; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Wed May 1 08:51:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650321 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2067.outbound.protection.outlook.com [40.107.93.67]) (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 EB7A950288; Wed, 1 May 2024 09:02:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554147; cv=fail; b=oAFWFNeBV3hnyYIAUHx6d7vlGHz7OKXmx4nmSIz43bO/S2TS+qeNojAIc7USiwYkBb3wciAowJNmj63E1sIObGLnHblSyqHkARc0QU8nbLGaAHuGnIpiOt8aMF5CjrkZx/uhjSSTrA+0mx8P+HUF92syb0qmGyx+QeAnrKW3JXs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554147; c=relaxed/simple; bh=vDgJ6yzjZQxXkKig1glRTC6baWdSj5iWeNpILl2GWcs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mr6T1r0YP4o3W3uzNWVoVJUKiU99j4Av79m96Mq4TamhV3VYiYvgyTi5GmYNWfGdFx/L2caoGx2321GhX4uDt1s+ZrT6ss8I+3BuThs7lZRIdGTf2HvKlJhQLK6d+PqVYY4E1oUpjxW7pov3AhNIm6iaAdSvN6s8xYTYblYNWjM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=uxy8Olep; arc=fail smtp.client-ip=40.107.93.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="uxy8Olep" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G4ryxOPEzp3+sS5EGNwVMD5wDnXIz1Rm3MsBAEM/7gj/MqeybA9fT+EgCl/6+2quVjNQEh739Wx0aGEM6xchcTgJorzo6bVY/oOZswXngVOPkBYdPkcbYJoKZJ+rI7L7xwhOP944VQ91RGSTkD1EDNEEVIXvYhxHaucXjx/DL5Q4r1Ly3WiOydnyxQccVqdVZmZ/sPDqzIfHnyggeexkPFoFt8FzmSqQgEX8NdHBcsFy+Fo7peg3xMqtVA4ylTyNohrSexVPr1DnedGCFXUgvkDjJrQvWQqH+6KUaVn3WrZQMkn/WISrLJTo3WPCbeRgIJiYB+jLbpavzNTkmskh3Q== 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=bK20vcWucIreIaSDh3sNBwKjRQE7GxXQ728dKk/YFZA=; b=D2bz4TxCz0J2PktU/C7cWimAfVsdH8Mcdl0HFVO5qxQ7BFC63x7DT5APGRqE8FYPdh8i7RQd0QNi0LlVND6/Kf2xw5pwyexAgf6e9uEq+XbrFoaYjXhQ+V/+59ZazgTdWveOwIOV7VMIdAWqu88YS38LkzHzptUMBn6qa1tIUvaufzUs/KIPztENL/S6cug93lJITtg69mv/+ivGGoeqDvxdG0+GI/FZCjfvdSLTS2daux4BkCeuGSJNBGUVyQUgGeDaH/2Vcz/GE4fIQKyNjA/QTSmzQiTb00IyFyOTjdxs3zt9+HcvBZzq1dAxnl7jJDlJK0GMbjcH4ncObq3OQA== 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=bK20vcWucIreIaSDh3sNBwKjRQE7GxXQ728dKk/YFZA=; b=uxy8OleptnA5XhKkoZXWbEBz+BDGiDOt0sME/G37M2DsU/vKG4/R93IoNMt0GBssh7rkAF3W23dsp6fErLwPRNNINoGBTKnZLYrYZJSFkHx2v0yJJm3qJ+vlctLWHHaXadVVBkTp1PgX89kjdgu7DArvBPqELHLlYJejdMpztLE= Received: from BY3PR10CA0021.namprd10.prod.outlook.com (2603:10b6:a03:255::26) by PH7PR12MB7794.namprd12.prod.outlook.com (2603:10b6:510:276::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:02:15 +0000 Received: from CO1PEPF000044F3.namprd05.prod.outlook.com (2603:10b6:a03:255:cafe::a1) by BY3PR10CA0021.outlook.office365.com (2603:10b6:a03:255::26) 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:02:15 +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 CO1PEPF000044F3.mail.protection.outlook.com (10.167.241.73) 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:02: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; Wed, 1 May 2024 04:02:14 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 09/20] KVM: SEV: Add support to handle MSR based Page State Change VMGEXIT Date: Wed, 1 May 2024 03:51:59 -0500 Message-ID: <20240501085210.2213060-10-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CO1PEPF000044F3:EE_|PH7PR12MB7794:EE_ X-MS-Office365-Filtering-Correlation-Id: 65ed4a01-1b45-4bfc-24f2-08dc69bd65af X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|1800799015|376005|7416005|36860700004; X-Microsoft-Antispam-Message-Info: ls4uveIgbbjXe+xVN9H8hYDP49MkXr8mngw4l+xatvlPXX0Hgg3W3bfbVhNQ/kA5uo62F6gk/55N6bHbw3VnUwO0e0KyNtvRJXyN3mvi4jqdixMMwX/Yopf1tk9ZhTKokWSXsDH4tuYtylRLbFzgSqM3oOr8PFYSDVUOo3bO0IP67aaNrKFnT7BujQOaZAywsRcFdOEyixzqyw5SfQP30dtke0+prINgOi9xHRZqFy5oTSNmrTfoVtX+OenaIz6vy7rI3x0SRdCT6JKWIpbNOobAiiUU38LhdcFTYN3i9o5uB3OtJNinTYHnXtKRAbvJWVkiJPoVusMgiqsLWc4Y9clLjMF98FSvdyhq+wjrwT6UzBNBrbbO9mQMt3C2i6Np2tpv6Dv02wL4qD6zXXejxe5QI/25dUaxKesl9aizP9cIhu8cGvD9HbrpfAKVWW5Kar7OA2NB4swOouC6ToRjh5PFEX1bfE6OFa54aSJsIH1HWvIqZd41et0+Ftq1tO+w4AT5FxlBffENg3QpUjmBf6HjC12BvbQlIWy+biTn1ILKdNG/dAVpwqCaBN9Di7bH16fMACiZxqES0U3QF0sElVAFvl6rfhVuV8CyaC6/nOxMDjlpU7G4wvfN6FH0pq7Vp+SquxKNFA6yR16KeC0tavUTNv+dxDsZPPJ0IyYMI4tLQr8heAyLL26q6MqYQtyqNMqEk2+LAKycFomeOTGfFUJyCGZ7pFL9OEnoOFsHhkI59x25ATQNGFVILbsQPpMjPZR8Zw+X1oO7oWvjYrwJbFTaO8sWZqTtaI20iehkB7hAYMv9+nY5eOa2xRs0ZkLpP3Dqrv+HFs9JTQIGCBXBcHFTfNkxzB/QfcasfwjqoHylWWx0oRKq+8sawG/9hc8vejvMaZqk3nDvC3OyucJA8BjKa0BPnBw7CyXrQqAsJC7+E4DjHvxMOO0Gn8w34m2xSg+CY6kyHphOdcgDzAFxisjxLlB5XZzXGWn+PGyY5Lc15Ude2pn5jEqWs/dN41v8I+fwc6IYFSk3s9LTY6bA0QBBWJRhWdLy/t/7lJSozQQ/iJTN0DjmlIxUeYOCCxvW3Rn6hvBgv0dO1u6ZSxLkczHrofkvmqs9OnMeATKnrzrtIDgouA0B0URiSDfarcgN7d8goIslL53REvpc9bhnWWs4sed1Ay0SpKtSIlup2iZalHptBIY6YgCzvchm3Io/cXNDFLYYWJnZmo4xxDSz0DAo0DFYKK8KwkFmv+vYpHu+M32Y8RW/vsvZjGZwOW0+sDURmcumULYSs1R+4T4WAMa5s8qinu/qF9uiPs9VdPzRVyhkrxRwPCFClU2UaJKM19YIwHN28c2sVfRoQNQH5Z37xzYfjM9tOxuGPW1r6ubgg+lMyybeN3kWGE7WBTaG 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)(1800799015)(376005)(7416005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:02:15.3761 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 65ed4a01-1b45-4bfc-24f2-08dc69bd65af 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: CO1PEPF000044F3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7794 SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change MSR protocol as defined in the GHCB specification. When using gmem, private/shared memory is allocated through separate pools, and KVM relies on userspace issuing a KVM_SET_MEMORY_ATTRIBUTES KVM ioctl to tell the KVM MMU whether or not a particular GFN should be backed by private memory or not. Forward these page state change requests to userspace so that it can issue the expected KVM ioctls. The KVM MMU will handle updating the RMP entries when it is ready to map a private page into a guest. Use the existing KVM_HC_MAP_GPA_RANGE hypercall format to deliver these requests to userspace via KVM_EXIT_HYPERCALL. Signed-off-by: Michael Roth Co-developed-by: Brijesh Singh Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/sev-common.h | 6 ++++ arch/x86/kvm/svm/sev.c | 48 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 1006bfffe07a..6d68db812de1 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -101,11 +101,17 @@ enum psc_op { /* GHCBData[11:0] */ \ GHCB_MSR_PSC_REQ) +#define GHCB_MSR_PSC_REQ_TO_GFN(msr) (((msr) & GENMASK_ULL(51, 12)) >> 12) +#define GHCB_MSR_PSC_REQ_TO_OP(msr) (((msr) & GENMASK_ULL(55, 52)) >> 52) + #define GHCB_MSR_PSC_RESP 0x015 #define GHCB_MSR_PSC_RESP_VAL(val) \ /* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) +/* Set highest bit as a generic error response */ +#define GHCB_MSR_PSC_RESP_ERROR (BIT_ULL(63) | GHCB_MSR_PSC_RESP) + /* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e1ac5af4cb74..720775c9d0b8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3461,6 +3461,48 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_complete_psc_msr(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (vcpu->run->hypercall.ret) + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + else + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP); + + return 1; /* resume guest */ +} + +static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr) +{ + u64 gpa = gfn_to_gpa(GHCB_MSR_PSC_REQ_TO_GFN(ghcb_msr)); + u8 op = GHCB_MSR_PSC_REQ_TO_OP(ghcb_msr); + struct kvm_vcpu *vcpu = &svm->vcpu; + + if (op != SNP_PAGE_STATE_PRIVATE && op != SNP_PAGE_STATE_SHARED) { + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + return 1; /* resume guest */ + } + + if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); + return 1; /* resume guest */ + } + + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; + vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; + vcpu->run->hypercall.args[0] = gpa; + vcpu->run->hypercall.args[1] = 1; + vcpu->run->hypercall.args[2] = (op == SNP_PAGE_STATE_PRIVATE) + ? KVM_MAP_GPA_RANGE_ENCRYPTED + : KVM_MAP_GPA_RANGE_DECRYPTED; + vcpu->run->hypercall.args[2] |= KVM_MAP_GPA_RANGE_PAGE_SZ_4K; + + vcpu->arch.complete_userspace_io = snp_complete_psc_msr; + + return 0; /* forward request to userspace */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3566,6 +3608,12 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: + if (!sev_snp_guest(vcpu->kvm)) + goto out_terminate; + + ret = snp_begin_psc_msr(svm, control->ghcb_gpa); + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; From patchwork Wed May 1 08:52:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650322 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2077.outbound.protection.outlook.com [40.107.223.77]) (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 3A29351005; Wed, 1 May 2024 09:02:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554162; cv=fail; b=gwTXc7v/rnjduZdgOgbx6ShldXT6dhz3sBT/Jwru3T4ghAqpXfpGLPkAI6z/RGePWDB2s1s8Fiw+d96UNkok91DNDUQ9DcLSXM/lX6tPymvvyJsVhM40DSnnQAWakGmc2FgOtIssI7aY+7TLADgV2IkkRZh+yJ3h0dxasBnHzF8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554162; c=relaxed/simple; bh=GLoCMJ2rRmvXLV+BJdDBF+Wy3eNsOQgzDw7g2sb7xS0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ilr4DV3Z4CdZUAx4jvZyW1HPipA9Zoeiu+zlq9oZCZxARQdY6cyKm2KjIbvVt/SmGjidLz6MW+2IQY3+BNa7ARLcvjoOQ9l0FsHRfFqfwuiJ5pfHe9OZBcCdjWoysIWrzDh3WRWPXgcLZF2lmfbYSGBhYdF+gA33BQXdFWf4QBk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=HSVTE4z8; arc=fail smtp.client-ip=40.107.223.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="HSVTE4z8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nsjMTqLzqljwKTS7+PmG3KpcLO0j2VbcNtBL3LBS3iTipx3/dynHOzvbrWsurVyTRI3O3i0q0bZZyMk24eHNkKVoeAAklAe61aQz03KgjkmtjTNUAVS7UsWm1NtlLFanGxvqGNr0Cumzgz7ZswOesq4MqagpIsgyxMkaGsEXX3Iqwi69hG3wHqZRiPwRLoqIgtmnhlRPS9RYdlXTErW0HO+3l3JkghTxSez9eDXGYsUeazVZWPq6XiIDmFKcd4EJZ+77mVFAdVG+kq+XZnV8J0r/5rUeuaT5E5dWKWGJ07G+wj3DNcNAXPU4Soo8kME0CXg5qn0xIApEY5CUmqTSqg== 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=K95Qu3jRNV8CCBZ1X77fAEHfdK/7g4/JrIihX0urDA0=; b=n+PXtz8xsXRqEww8Iaq7kv4cf2n0+IsJL/wTFsLBi5XGXbm2SphPmp9O48c+RsGUHeUWepP/w/XD4K61xwo4rNSKtVtdeq5Z11L+A4twfxz+ckzfa9sWg9LuVosE70cAb7N59ZCWED4W6ugMs6ZZfsuQdcA+qYBSu2MS4RZTikaPONL13sfkeoDXs8YMO+g1UPItPWu9zlQtxJBULshlK8qsjC3Pa0vpwhV5KK7yHM84N8qtRTf3EOEPIBxK2HBJ4ADUx5/9i3Gwhn1R249z3EoPn/ZesRzE+8eEMrL1yupD2+SjsWkyjuRZ0Ba5QLexDpWp+iBGXAGNdsNQgoQyyQ== 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=K95Qu3jRNV8CCBZ1X77fAEHfdK/7g4/JrIihX0urDA0=; b=HSVTE4z8znmqY9/TeTIDNK6NfqMV5zpunGfXqEbNH6IcTr4kkPJJpZxiNrsy21I2u9tLJq7d3EjpV7hoOW/SYUKEjewjyxoUgtJgpPJdm7TYSMbfhyoUmvMOPASPtO4F//JvPmjOMPzd1BAH3qpXrkCtfVEDxor9c8SeoQv8svY= Received: from SJ0PR13CA0023.namprd13.prod.outlook.com (2603:10b6:a03:2c0::28) by MN6PR12MB8568.namprd12.prod.outlook.com (2603:10b6:208:471::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.36; Wed, 1 May 2024 09:02:37 +0000 Received: from CO1PEPF000044F2.namprd05.prod.outlook.com (2603:10b6:a03:2c0:cafe::d) by SJ0PR13CA0023.outlook.office365.com (2603:10b6:a03:2c0::28) 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:02:36 +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 CO1PEPF000044F2.mail.protection.outlook.com (10.167.241.72) 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:02:36 +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:02:35 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 10/20] KVM: SEV: Add support to handle Page State Change VMGEXIT Date: Wed, 1 May 2024 03:52:00 -0500 Message-ID: <20240501085210.2213060-11-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CO1PEPF000044F2:EE_|MN6PR12MB8568:EE_ X-MS-Office365-Filtering-Correlation-Id: 9f8a0130-3da8-4886-3204-08dc69bd7220 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|7416005|1800799015|36860700004|376005; X-Microsoft-Antispam-Message-Info: jHeYFUHX9Q5LWq4VI0747vy3KNHPf2V001po1lcU9Z7gQhv6JdHAkfoSz6ryUv/u9Q4ShQQwhYvSjAWCIZm2rtm/DIEqkykJAKTZFQDqJj/J0Wfmf1U/GSYfJx2DwHYrD5K7oDvIACaeIUTLObR3bhvTdzx+oC1ifwneZSv05Iyf4vlxX6S8/69sAfEvl9FLh19sX/nHS1Fx9OCUXtvT/lVx2/uwAnQq4Yayp/aA/+whKy8ot5yH/frBaGbS+Wg8NWp0lbe8thCNYncFW1dXsoR9/d99csaGvm1RbJ65t11AuRYQEsbGhwtvtLaG9UcVThHZbuF97Eee0b48D0vV6IP0VY8JOngDyOWxF40ntMxW0NcBnAsJ2eh1z1sRGdyPgdvP+yjb8BP9FOsLGqTNOq4JcwdwE3deBvoQcSzyKh8W9ToCJsP9s7HeJcBS5yx11gfbp//RlB8xY5JgSmaH/pz1JgGG0+q19WTSIhCi56RqONwXiyBZ9d2UuOPvN7lelsZqGAXR4hYhmBfWxKFffPsSC1RCdxE2JsjGc/XTUvVKJh+y82ZoQaDHlJGEFWKqTRSHecCsXHAhRh2tOP7GFiQ9Xv7pKZTZJk6Lj8IOtkJkNogX+7BJPGlB196IMKstoL5sLt8KBCumPSre1gzJOGWAnjoIAYjYTlZLtzz6M0H4YZM8bcsGW7YX32nV5mENpeYZVGSVeLYZrzhi80tZKQsuo/u2IWxliad8gI7KPbprLixhqmuhxOwWdztRK6HiRJ9RmDlExdQMEEDYVvDuQUWhfXV3bb1IwD7fxqThf2W3wfCsbPDsvM794Si/gnnGM4QXllL6klFGSHIwE9BvJoDxrXF/MfAgEmSugHFimgljydhVX4j2dkHv9+5Szk5ji31gX1gOGpgBTjAdPv0CiAjQfvEqN08ZTjViMM1XaQhLdTYevR+RTocT4yfS3k1v5Uxz2BQXYcvVdlpWYxKNpVKjqrIpnXKhE/yAujTGbvkyBD1e4VySXPblc8ujFoA1H4x8zyFBAfZc8ovGeaUzPT0dl3jn0DYXlpTEsrj+ln6n9FDLEAV8KdYy/eOjyQ82RUm2HOBCwNRtH5Yxj61gBl39b8P6bQAR3InI3SqZvuoefN1iwMT5hahmfCIEaLnD8neipgSWb10FOA/2cXAPQD7TV5SyxqlKAFGf86NdR0V4tSrFcofVznk++zZ1C23E7fhHRORmxeZGAHg9NjN9A+uc07+25aTzX8wxW0mpWMu9bLZ3mQLP2mTikFQmhIYtOMhxILf4YbNdR734aMQm+4sxwBfO4qwNBZj9CRWlDOnr+0+MtJd85BSZhFHvPgyE4qqVjrYeU/s8dO2eohhnkpZWQuXZGMmqtPts+n2KMtA= 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)(7416005)(1800799015)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:02:36.2497 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9f8a0130-3da8-4886-3204-08dc69bd7220 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: CO1PEPF000044F2.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN6PR12MB8568 SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification version 2. Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page. As with the MSR-based page-state changes, use the existing KVM_HC_MAP_GPA_RANGE hypercall format to deliver these requests to userspace via KVM_EXIT_HYPERCALL. Signed-off-by: Michael Roth Co-developed-by: Brijesh Singh Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/sev-common.h | 11 ++ arch/x86/kvm/svm/sev.c | 180 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 5 + 3 files changed, 196 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 6d68db812de1..8647cc05e2f4 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -129,8 +129,19 @@ enum psc_op { * The VMGEXIT_PSC_MAX_ENTRY determines the size of the PSC structure, which * is a local stack variable in set_pages_state(). Do not increase this value * without evaluating the impact to stack usage. + * + * Use VMGEXIT_PSC_MAX_COUNT in cases where the actual GHCB-defined max value + * is needed, such as when processing GHCB requests on the hypervisor side. */ #define VMGEXIT_PSC_MAX_ENTRY 64 +#define VMGEXIT_PSC_MAX_COUNT 253 + +#define VMGEXIT_PSC_ERROR_GENERIC (0x100UL << 32) +#define VMGEXIT_PSC_ERROR_INVALID_HDR ((1UL << 32) | 1) +#define VMGEXIT_PSC_ERROR_INVALID_ENTRY ((1UL << 32) | 2) + +#define VMGEXIT_PSC_OP_PRIVATE 1 +#define VMGEXIT_PSC_OP_SHARED 2 struct psc_hdr { u16 cur_entry; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 720775c9d0b8..70b8f4cd1b03 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3274,6 +3274,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_HV_FEATURES: case SVM_VMGEXIT_TERM_REQUEST: break; + case SVM_VMGEXIT_PSC: + if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_sw_scratch_is_valid(svm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -3503,6 +3507,175 @@ static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr) return 0; /* forward request to userspace */ } +struct psc_buffer { + struct psc_hdr hdr; + struct psc_entry entries[]; +} __packed; + +static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc); + +static int snp_complete_psc(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct psc_buffer *psc = svm->sev_es.ghcb_sa; + struct psc_entry *entries = psc->entries; + struct psc_hdr *hdr = &psc->hdr; + __u64 psc_ret; + __u16 idx; + + if (vcpu->run->hypercall.ret) { + psc_ret = VMGEXIT_PSC_ERROR_GENERIC; + goto out_resume; + } + + /* + * Everything in-flight has been processed successfully. Update the + * corresponding entries in the guest's PSC buffer. + */ + for (idx = svm->sev_es.psc_idx; svm->sev_es.psc_inflight; + svm->sev_es.psc_inflight--, idx++) { + struct psc_entry *entry = &entries[idx]; + + entry->cur_page = svm->sev_es.psc_2m ? 512 : 1; + } + + hdr->cur_entry = idx; + + /* Handle the next range (if any). */ + return snp_begin_psc(svm, psc); + +out_resume: + svm->sev_es.psc_idx = 0; + svm->sev_es.psc_inflight = 0; + svm->sev_es.psc_2m = false; + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, psc_ret); + + return 1; /* resume guest */ +} + +static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) +{ + struct psc_entry *entries = psc->entries; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct psc_hdr *hdr = &psc->hdr; + struct psc_entry entry_start; + u16 idx, idx_start, idx_end; + __u64 psc_ret, gpa; + int npages; + + /* There should be no other PSCs in-flight at this point. */ + if (WARN_ON_ONCE(svm->sev_es.psc_inflight)) { + psc_ret = VMGEXIT_PSC_ERROR_GENERIC; + goto out_resume; + } + + if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + psc_ret = VMGEXIT_PSC_ERROR_GENERIC; + goto out_resume; + } + + /* + * The PSC descriptor buffer can be modified by a misbehaved guest after + * validation, so take care to only use validated copies of values used + * for things like array indexing. + */ + idx_start = hdr->cur_entry; + idx_end = hdr->end_entry; + + if (idx_end >= VMGEXIT_PSC_MAX_COUNT) { + psc_ret = VMGEXIT_PSC_ERROR_INVALID_HDR; + goto out_resume; + } + + /* Nothing more to process. */ + if (idx_start > idx_end) { + psc_ret = 0; + goto out_resume; + } + + /* Find the start of the next range which needs processing. */ + for (idx = idx_start; idx <= idx_end; idx++, hdr->cur_entry++) { + __u16 cur_page; + gfn_t gfn; + bool huge; + + entry_start = entries[idx]; + + /* Only private/shared conversions are currently supported. */ + if (entry_start.operation != VMGEXIT_PSC_OP_PRIVATE && + entry_start.operation != VMGEXIT_PSC_OP_SHARED) + continue; + + gfn = entry_start.gfn; + cur_page = entry_start.cur_page; + huge = entry_start.pagesize; + + if ((huge && (cur_page > 512 || !IS_ALIGNED(gfn, 512))) || + (!huge && cur_page > 1)) { + psc_ret = VMGEXIT_PSC_ERROR_INVALID_ENTRY; + goto out_resume; + } + + /* All sub-pages already processed. */ + if ((huge && cur_page == 512) || (!huge && cur_page == 1)) + continue; + + /* + * If this is a partially-completed 2M range, force 4K handling + * for the remaining pages since they're effectively split at + * this point. Subsequent code should ensure this doesn't get + * combined with adjacent PSC entries where 2M handling is still + * possible. + */ + svm->sev_es.psc_2m = cur_page ? false : huge; + svm->sev_es.psc_idx = idx; + svm->sev_es.psc_inflight = 1; + + gpa = gfn_to_gpa(gfn + cur_page); + npages = huge ? 512 - cur_page : 1; + break; + } + + /* + * Find all subsequent PSC entries that contain adjacent GPA + * ranges/operations and can be combined into a single + * KVM_HC_MAP_GPA_RANGE exit. + */ + for (idx = svm->sev_es.psc_idx + 1; idx <= idx_end; idx++) { + struct psc_entry entry = entries[idx]; + + if (entry.operation != entry_start.operation || + entry.gfn != entry_start.gfn + npages || + !!entry.pagesize != svm->sev_es.psc_2m) + break; + + svm->sev_es.psc_inflight++; + npages += entry_start.pagesize ? 512 : 1; + } + + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; + vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; + vcpu->run->hypercall.args[0] = gpa; + vcpu->run->hypercall.args[1] = npages; + vcpu->run->hypercall.args[2] = entry_start.operation == VMGEXIT_PSC_OP_PRIVATE + ? KVM_MAP_GPA_RANGE_ENCRYPTED + : KVM_MAP_GPA_RANGE_DECRYPTED; + vcpu->run->hypercall.args[2] |= entry_start.pagesize + ? KVM_MAP_GPA_RANGE_PAGE_SZ_2M + : KVM_MAP_GPA_RANGE_PAGE_SZ_4K; + vcpu->arch.complete_userspace_io = snp_complete_psc; + + return 0; /* forward request to userspace */ + +out_resume: + svm->sev_es.psc_idx = 0; + svm->sev_es.psc_inflight = 0; + svm->sev_es.psc_2m = false; + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, psc_ret); + + return 1; /* resume guest */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3761,6 +3934,13 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) vcpu->run->system_event.ndata = 1; vcpu->run->system_event.data[0] = control->ghcb_gpa; break; + case SVM_VMGEXIT_PSC: + ret = setup_vmgexit_scratch(svm, true, control->exit_info_2); + if (ret) + break; + + ret = snp_begin_psc(svm, svm->sev_es.ghcb_sa); + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index bbfbeed4c676..438cad6c9421 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -210,6 +210,11 @@ struct vcpu_sev_es_state { bool ghcb_sa_sync; bool ghcb_sa_free; + /* SNP Page-State-Change buffer entries currently being processed */ + u16 psc_idx; + u16 psc_inflight; + bool psc_2m; + u64 ghcb_registered_gpa; }; From patchwork Wed May 1 08:52:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650324 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2048.outbound.protection.outlook.com [40.107.93.48]) (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 9298A50283; Wed, 1 May 2024 09:03:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.48 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554204; cv=fail; b=XO/+8/gnMiuq31Vsjy/2pQQfcIjVInwgizf3vhfZ7yIN0TyXH7Mr4IaKz7KIw9TaKs7yA5r2FEe2O9fR7y/jlpdSDiPMUZTjkKFIZMbdrWzmx658QDsaVwej9V1yHEFEbdBC+bgs0JEQseW572aJ0KQUtp+L34Ueb/UXLLpzSQk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554204; c=relaxed/simple; bh=oRt5/vq2HqmrnDgfQVSe7aV5QUgv4TH3pr5DOjGh0gU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aJxKGr6h66r4jBQfuAUMincscZuCOW84G9iwIRYjjFLAP7p4DciWUtAQqoFmKs3nPFu6CpUbqXfmvVUFRh6EHlryG8TitRUFwZd/QXNyw79zPLfY+xcwOO5RzeyzA3vnw1g+VPRCmfTCudRvksWgX2sSSID+RglfcIr1L+G8UD8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=gqNAG9Nq; arc=fail smtp.client-ip=40.107.93.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="gqNAG9Nq" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nQgAEAUZ20oX7W740BuVVzol63s3BgawoNeMDsID6bJo66naMjXV/k9j1UjxB93fiepE/1RWwPpF21vn1USBNcE++PZ/VQ6sljD+bJVclZjHBPdCqnWg5AniS81aZ8QZRVB3l1lsM84xYoYgwkV/zpIhQn2Qm/aCT1KcTrCSp4ntpOYGsXZCn8nJQwALqmFkMV6dZDaAXLjgQQvmsVA6JhLcwIXwM/79eT9oyd+l6xkUjQmD0vlKbbIM7tXtGOjtkobFl5dinL9+3yHaWf/p28GrPFzrxo6HteSXlWz7I/ZORnLkFxQ+D1SGuCX7WlkgOk2mFXxYGKUZ9Ms0SrD1WA== 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=DRXF3LwoRr9qGzHrPNzmydQVLR8NfYSaqVwmpG6rTOM=; b=iUnKrBXX8/HJd98rB8w7EQlhL2hxGU+IPluTjDkmH/hdxESlgmIU894go2eEkOaTeq7rK5qo0C7Nl2piK4bD956SpGZ1uN0NqEnXCEjj+77NYQQtIjf90MB2289cFEROgod5hPkLFfq81KLmvNIh9ATTB5NO3jX+GTGzCLeYRUTqHJgH4Sm+DcDeUFti6Liom+3p2GUOC+tPSYvI6LH+ZLfyiKGtFba0T6HkbgjE+PoZKQ9Os2Fi/Z1+TmBemqRKwEPFYFXYHvbhdcBYfQZmVKpgdkhKJRM5/fAzB7vOnG82X3NE3qYHjCkAWRU0+cQ9D/gyiX2g/y+iczluBpd6iA== 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=DRXF3LwoRr9qGzHrPNzmydQVLR8NfYSaqVwmpG6rTOM=; b=gqNAG9NqfxiGc5xbRPon/gydbv5spJMUAczJk2KQlZsctbvJlz6h2T/Dbnr0CYy8n5Ub6uWhEPMFtauSBDHPJI+XoOssJ9e7GzLk4rDwDmuQ0q4XrfYigF77ljDDZ5wLsZzPSEjQNgNB5+xRr57uK+z21im4iQ8gQ/fcOzaMbLY= Received: from SJ0PR13CA0005.namprd13.prod.outlook.com (2603:10b6:a03:2c0::10) by IA0PR12MB8647.namprd12.prod.outlook.com (2603:10b6:208:480::8) 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:03:19 +0000 Received: from CO1PEPF000044F2.namprd05.prod.outlook.com (2603:10b6:a03:2c0:cafe::d6) by SJ0PR13CA0005.outlook.office365.com (2603:10b6:a03:2c0::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.24 via Frontend Transport; Wed, 1 May 2024 09:03:18 +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 CO1PEPF000044F2.mail.protection.outlook.com (10.167.241.72) 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:03:18 +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:17 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 11/20] KVM: SEV: Add support to handle RMP nested page faults Date: Wed, 1 May 2024 03:52:01 -0500 Message-ID: <20240501085210.2213060-12-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CO1PEPF000044F2:EE_|IA0PR12MB8647:EE_ X-MS-Office365-Filtering-Correlation-Id: 2cdc6fe1-01e7-4c59-3d96-08dc69bd8b24 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: Z1xwkU0IOvVwKrrjbKgU46ywhDzWjwIF4eDUJn5uh5fgZEzBv2j1tOY/zu5jHN7WuxSjQxRj+I1SQUq1mP5J3q1gGiiJ192+keTErtglNQkW82bkThE15afWrsChwhBW9nWf3GR7VFknS1TIFIKvO2qb2/h9MVQ4IVaRKnDZOPUvcGbU4C9jGs6rjjD9drzTtRRJrWnKzrr/1p8LBxcvkGCsthe96zUoA/sMQL28LKXXFN8jL4ce91jrvTmmr8Fz8GH4nGZO2ud8TTwzkAWLyYr4H7YjXJGAqcR1s3sPoPU9fEBXgXmDkXdM92OHImuNHubsKT+9O9bP0Q+ngvLyTErE8Jg4Qj47EO0vClJBGq305GzWIkcbS7mtra2HmH/PGxuS1IK8fTnrUDQDMB7xOEcJv25/VTZMr+4wVEjmzsvakz2V/rnxDRX0ICFJJaI25kBd0KEHIf55ICqoYSaFDvAPjprTYxE0GtoXzCxNoRFit4lwCvCU1mpIDLnoPLBdQi2+pnwHHnwKSQBuaVD13s4oejvdUWBaZkHWadxcMHdx6PK1XfvyFIrpzGQlKto5Zq6qcUstlaHdbivJgjOrm0DeqZzTBlx7f4YTkBzTp+4qMjsriIPXGh7F40twaN8dlUrKHb+PHtDibFkL+0wdRysqRJ4LRqij8ZQB9jThVt7uaMnOcAVvcROrP7+Y7ro7eH4cA+TVp1iBgaOCaUFAoNA59AKSkZysaTmiKnd8HA7TC1dgr+bXSLvbER2SiFAj40VjTuPQL2RZPjHDFQAwJJBqL6ctVXpUEzGy6eZU5RCweXOkH63v2Sw5jcw3ETkQHb9TRMCKPCcq/SoW75p1ZlX/ojyuLbsgCMRLKu1oK3unW03vymeW1y0Snso/ynyUD1oe6vMG16D6kRqc7Myi4K9NHLDwoySK20NbFNtrA3o6uKcEGts85acjC8dVnvEMBIBRQKSmAFohgiWJiwPLVzyC+Nx/fre/n8wb99fPapMtSwEOdJsHSOqnM0FxC7v98/B1hhkncMwzsp8ELBqPV1M6KLPhPBaqm6ALFPLYCLxb0XcshU6anuTuDBahNaTxF3fW9EfA4c+NHM+AhdLi0dAP2G0xC41B0+eOR/YZsGHPz3BD8vmncWIEhTAC2T7FzunDjYECYOtJQhfePwszBDhlrqD5ZLLwveIZ4ckiHZL4TEzyY9x7jq0hPvddxa/0XUz2ZLrGpefdCW77MH1/GP7K9kfF24C2vaU2EQv19yo6mlUqUV96xFLhTiGlydPppuh+8ugtvEnVmRxISByobpXqHygDJM2l4yTgOtDFqPJ0O+iYIOVGWwmlfAApMKf0bC/It40wgjP0u2mnlTfXo5/SDkPo+yGaxvWh/XU9vyNk28UDC4ztRWr31lsGZk2H 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:03:18.2192 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2cdc6fe1-01e7-4c59-3d96-08dc69bd8b24 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: CO1PEPF000044F2.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8647 From: Brijesh Singh When SEV-SNP is enabled in the guest, the hardware places restrictions on all memory accesses based on the contents of the RMP table. When hardware encounters RMP check failure caused by the guest memory access it raises the #NPF. The error code contains additional information on the access type. See the APM volume 2 for additional information. When using gmem, RMP faults resulting from mismatches between the state in the RMP table vs. what the guest expects via its page table result in KVM_EXIT_MEMORY_FAULTs being forwarded to userspace to handle. This means the only expected case that needs to be handled in the kernel is when the page size of the entry in the RMP table is larger than the mapping in the nested page table, in which case a PSMASH instruction needs to be issued to split the large RMP entry into individual 4K entries so that subsequent accesses can succeed. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/sev.h | 3 + arch/x86/kvm/mmu.h | 2 - arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/svm/sev.c | 109 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 21 ++++-- arch/x86/kvm/svm/svm.h | 3 + arch/x86/kvm/trace.h | 31 +++++++++ arch/x86/kvm/x86.c | 1 + 9 files changed, 166 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 87265b73906a..8a414fc972f8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1944,6 +1944,7 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, const struct kvm_memory_slot *memslot); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages); +void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3); diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 7f57382afee4..3a06f06b847a 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -91,6 +91,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* RMUPDATE detected 4K page and 2MB page overlap. */ #define RMPUPDATE_FAIL_OVERLAP 4 +/* PSMASH failed due to concurrent access by another CPU */ +#define PSMASH_FAIL_INUSE 3 + /* RMP page size */ #define RMP_PG_SIZE_4K 0 #define RMP_PG_SIZE_2M 1 diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 2343c9f00e31..e3cb35b9396d 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -251,8 +251,6 @@ static inline bool kvm_mmu_honors_guest_mtrrs(struct kvm *kvm) return __kvm_mmu_honors_guest_mtrrs(kvm_arch_has_noncoherent_dma(kvm)); } -void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); - int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); int kvm_mmu_post_init_vm(struct kvm *kvm); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0d556da052f6..de35dee25bf6 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6758,6 +6758,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, return need_tlb_flush; } +EXPORT_SYMBOL_GPL(kvm_zap_gfn_range); static void kvm_rmap_zap_collapsible_sptes(struct kvm *kvm, const struct kvm_memory_slot *slot) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 70b8f4cd1b03..7f5ddd92113e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3465,6 +3465,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash(kvm_pfn_t pfn) +{ + int ret; + + pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + + /* + * PSMASH_FAIL_INUSE indicates another processor is modifying the + * entry, so retry until that's no longer the case. + */ + do { + ret = psmash(pfn); + } while (ret == PSMASH_FAIL_INUSE); + + return ret; +} + static int snp_complete_psc_msr(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -4221,3 +4238,95 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } + +void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + struct kvm_memory_slot *slot; + struct kvm *kvm = vcpu->kvm; + int order, rmp_level, ret; + bool assigned; + kvm_pfn_t pfn; + gfn_t gfn; + + gfn = gpa >> PAGE_SHIFT; + + /* + * The only time RMP faults occur for shared pages is when the guest is + * triggering an RMP fault for an implicit page-state change from + * shared->private. Implicit page-state changes are forwarded to + * userspace via KVM_EXIT_MEMORY_FAULT events, however, so RMP faults + * for shared pages should not end up here. + */ + if (!kvm_mem_is_private(kvm, gfn)) { + pr_warn_ratelimited("SEV: Unexpected RMP fault for non-private GPA 0x%llx\n", + gpa); + return; + } + + slot = gfn_to_memslot(kvm, gfn); + if (!kvm_slot_can_be_private(slot)) { + pr_warn_ratelimited("SEV: Unexpected RMP fault, non-private slot for GPA 0x%llx\n", + gpa); + return; + } + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order); + if (ret) { + pr_warn_ratelimited("SEV: Unexpected RMP fault, no backing page for private GPA 0x%llx\n", + gpa); + return; + } + + ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level); + if (ret || !assigned) { + pr_warn_ratelimited("SEV: Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n", + gpa, pfn, ret); + goto out_no_trace; + } + + /* + * There are 2 cases where a PSMASH may be needed to resolve an #NPF + * with PFERR_GUEST_RMP_BIT set: + * + * 1) RMPADJUST/PVALIDATE can trigger an #NPF with PFERR_GUEST_SIZEM + * bit set if the guest issues them with a smaller granularity than + * what is indicated by the page-size bit in the 2MB RMP entry for + * the PFN that backs the GPA. + * + * 2) Guest access via NPT can trigger an #NPF if the NPT mapping is + * smaller than what is indicated by the 2MB RMP entry for the PFN + * that backs the GPA. + * + * In both these cases, the corresponding 2M RMP entry needs to + * be PSMASH'd to 512 4K RMP entries. If the RMP entry is already + * split into 4K RMP entries, then this is likely a spurious case which + * can occur when there are concurrent accesses by the guest to a 2MB + * GPA range that is backed by a 2MB-aligned PFN who's RMP entry is in + * the process of being PMASH'd into 4K entries. These cases should + * resolve automatically on subsequent accesses, so just ignore them + * here. + */ + if (rmp_level == PG_LEVEL_4K) + goto out; + + ret = snp_rmptable_psmash(pfn); + if (ret) { + /* + * Look it up again. If it's 4K now then the PSMASH may have + * raced with another process and the issue has already resolved + * itself. + */ + if (!snp_lookup_rmpentry(pfn, &assigned, &rmp_level) && + assigned && rmp_level == PG_LEVEL_4K) + goto out; + + pr_warn_ratelimited("SEV: Unable to split RMP entry for GPA 0x%llx PFN 0x%llx ret %d\n", + gpa, pfn, ret); + } + + kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD); +out: + trace_kvm_rmp_fault(vcpu, gpa, pfn, error_code, rmp_level, ret); +out_no_trace: + put_page(pfn_to_page(pfn)); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 422b452fbc3b..7c9807fdafc3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2043,6 +2043,7 @@ static int pf_interception(struct kvm_vcpu *vcpu) static int npf_interception(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); + int rc; u64 fault_address = svm->vmcb->control.exit_info_2; u64 error_code = svm->vmcb->control.exit_info_1; @@ -2060,10 +2061,22 @@ static int npf_interception(struct kvm_vcpu *vcpu) error_code |= PFERR_PRIVATE_ACCESS; trace_kvm_page_fault(vcpu, fault_address, error_code); - return kvm_mmu_page_fault(vcpu, fault_address, error_code, - static_cpu_has(X86_FEATURE_DECODEASSISTS) ? - svm->vmcb->control.insn_bytes : NULL, - svm->vmcb->control.insn_len); + rc = kvm_mmu_page_fault(vcpu, fault_address, error_code, + static_cpu_has(X86_FEATURE_DECODEASSISTS) ? + svm->vmcb->control.insn_bytes : NULL, + svm->vmcb->control.insn_len); + + /* + * rc == 0 indicates a userspace exit is needed to handle page + * transitions, so do that first before updating the RMP table. + */ + if (error_code & PFERR_GUEST_RMP_MASK) { + if (rc == 0) + return rc; + sev_handle_rmp_fault(vcpu, fault_address, error_code); + } + + return rc; } static int db_interception(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 438cad6c9421..d779f1f431af 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -728,6 +728,7 @@ void sev_hardware_unsetup(void); int sev_cpu_init(struct svm_cpu_data *sd); int sev_dev_get_attr(u32 group, u64 attr, u64 *val); extern unsigned int max_sev_asid; +void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -741,6 +742,8 @@ static inline void sev_hardware_unsetup(void) {} static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; } static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; } #define max_sev_asid 0 +static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {} + #endif /* vmenter.S */ diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index c6b4b1728006..3531a187d5d9 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1834,6 +1834,37 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit, __entry->vcpu_id, __entry->ghcb_gpa, __entry->result) ); +/* + * Tracepoint for #NPFs due to RMP faults. + */ +TRACE_EVENT(kvm_rmp_fault, + TP_PROTO(struct kvm_vcpu *vcpu, u64 gpa, u64 pfn, u64 error_code, + int rmp_level, int psmash_ret), + TP_ARGS(vcpu, gpa, pfn, error_code, rmp_level, psmash_ret), + + TP_STRUCT__entry( + __field(unsigned int, vcpu_id) + __field(u64, gpa) + __field(u64, pfn) + __field(u64, error_code) + __field(int, rmp_level) + __field(int, psmash_ret) + ), + + TP_fast_assign( + __entry->vcpu_id = vcpu->vcpu_id; + __entry->gpa = gpa; + __entry->pfn = pfn; + __entry->error_code = error_code; + __entry->rmp_level = rmp_level; + __entry->psmash_ret = psmash_ret; + ), + + TP_printk("vcpu %u gpa %016llx pfn 0x%llx error_code 0x%llx rmp_level %d psmash_ret %d", + __entry->vcpu_id, __entry->gpa, __entry->pfn, + __entry->error_code, __entry->rmp_level, __entry->psmash_ret) +); + #endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 83b8260443a3..14693effec6b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13996,6 +13996,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_rmp_fault); static int __init kvm_x86_init(void) { From patchwork Wed May 1 08:52:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650325 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2066.outbound.protection.outlook.com [40.107.244.66]) (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 ED6F750298; Wed, 1 May 2024 09:03:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.66 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554224; cv=fail; b=g6RRyUWeJ96kZiAADfYO/+Sb/nS64nJKMWo0KaRmd7jWhQqJboXZQ4r+RaXj0HfDJDJLLX27RPtQfb2Wx8LVO8FIk7pAhoxYLZS+OSErHho8FaNe+uKDS8MSrkd7y+wRp+mRaHaSu6pJbb+PjmdaJV5RawoDmExr4DfY5vgNw9A= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554224; c=relaxed/simple; bh=dOTaswYI57u15PKqcKRohV/wBsRQafuCXu5A6G7ZDnc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OJ8eLqQbC3QpRBObxLgdb8p1eJWwaiY5+We6lfiwewnyTtv5StwLjEcQ31fBaTU+Rqa/4sKJVtnUEBNtT49SYrokHjvn4KZyZZ7iJnlfaL3nZLKAzz6j5wg6wTH0uo85VKNqySzwexOT4Z8WomBCNrx5Uuz2g73YgrOrE5Gx5lM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=HrcAlvMI; arc=fail smtp.client-ip=40.107.244.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="HrcAlvMI" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hagtb1E5PufxIsCMNj+5tXOpBN+fGI/uWA0hQkFIiCOA6/rNaQzJvc3PVZE+KSuA3wDdR9U0uiR7CHTTa6TInRimhRir+/AFQlzzKXDdEGskj2caMS0/0ZWrn3QQ1VgslujIb3pNBFA06vuqiIrSkyve8Q41i9G8xbJXIVjQawr3svIHpHz/W2N4Sa+U3ejy58iMYpIO1RfyRWCmQD03KqxOQh09EfjNHkHkGab/lnYNhCH0Li/qMF0eoIvzVBOvd6bTLBasqW48dyBk0CJqWVx/LUPlx5QCqTeSA6O0wN9+Dg8LjFafg6esKDeQioB1I8A60Jv1/A7zp73GiAPczA== 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=2cQRxjWkX0ns1Fxh0GJaedpqgPFyIsP2jvvzy9GSh+E=; b=gywnxsjhBG1MsZIxof7m+MVBGqLiZoL7VAtkbCtqtH7CoeYaHJZvOMgqPB7g/BXNjUllCxdQoIQTDOV/cXpBFUbnOuCr1GkzVM91zq6HDdxC6WPLSEEzbr9coxZ+Q3SJ13wqcfQEYBD2fPfqdqDm4Z/6F2bLm9Fw7EvqOEQ1bKzRpHy1aT3VKB+jcJ9xzLNIpkK60JS0eewGHL7vk8B4MF7pNuLtWsSKz7IfjYAw5IUmy9qFtNgYPbt2qYkUTpD+79T9hhKr7v90dA1aXcI5TiniQyfbVglW6yDyd2/g5BtWoxB6lEr0eLK8PiH7eUs9irsOoCHKxzCTf5Yz1eRXVg== 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=2cQRxjWkX0ns1Fxh0GJaedpqgPFyIsP2jvvzy9GSh+E=; b=HrcAlvMI/5zptcjXzR2eYdBAj7mt1oDsHaWhZwwGNJPHlm78WSspFUcfUTP9lIqZehbyH2u5jh00Xef+tygen5k08fmV2Mv2wHQEojg1v7VAyxoDrxWyuI9qV6JrpPz8GzjuE4kSvPItLAE+gEpsE19KQK1rHn+OhtUwRfWD4Eg= Received: from SJ0PR05CA0166.namprd05.prod.outlook.com (2603:10b6:a03:339::21) by SJ0PR12MB6734.namprd12.prod.outlook.com (2603:10b6:a03:478::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35; Wed, 1 May 2024 09:03:39 +0000 Received: from CO1PEPF000044F1.namprd05.prod.outlook.com (2603:10b6:a03:339:cafe::ff) by SJ0PR05CA0166.outlook.office365.com (2603:10b6:a03:339::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.25 via Frontend Transport; Wed, 1 May 2024 09:03:39 +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 CO1PEPF000044F1.mail.protection.outlook.com (10.167.241.71) 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:03:39 +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:38 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 12/20] KVM: SEV: Support SEV-SNP AP Creation NAE event Date: Wed, 1 May 2024 03:52:02 -0500 Message-ID: <20240501085210.2213060-13-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CO1PEPF000044F1:EE_|SJ0PR12MB6734:EE_ X-MS-Office365-Filtering-Correlation-Id: 9f99f4e3-02c3-4b0f-2161-08dc69bd97b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|7416005|82310400014|36860700004|1800799015|376005; X-Microsoft-Antispam-Message-Info: ARAnaOL1+wRHK+YhFmwKaKVd6DAyAfn8eQ/8TDdC7q/Mtzx6WLahIzEuWS0ckpSfIDtwihHjxGnY5RpsLIPsBOh44Udk3T850r3UooRQj4lkMEz7mvH9u81YnCeRKmyMx5X9CQfPch1Wdm/nldhTtskqbI2oLfmBCYIM6sRqxrdcK0aSnNpDDGHvS6UNvR3uSBAbu+b1o6aeo7S688HyQRuqpwLBjbjOqhjc3F5OcTzRg/i1MuE+VHU1YPo4F/XHjZboIxTD/nv1QxFN1kc1xAwdNydR8heBrArAmenACUTRZxemJ7ZbCLMDf3HJdeD1yXIosmYNsYLxzy2xJPOxORQszo7rsBPzTwXlwd4mXCipKmlwv4MbFzXFoVOk11756lNGMA6eubwTWhUJ3ky6JbcGF1/SHK3I/9YBZnohYiFZ1lRE8rzLK366lrt1TwlNSHImzb99OHTbOt6RHpP1Pf67sSeEuold+siGp/pA9WVtijIH8xz49pWlRMOxyCt4fJcy1PMGtjPb/MNl1ER+ELyy41WAqVogZIo2RQ2QjZz3Hir8aie4QXin4Pt8RmT90zYHabKVftJbjD+ViRMg5m3YMfBql2ZhmlJ1WRBViT3Fl16442nORbmmRLq2sspQftHywSiqHIcCBckVG5UnQHyV4e1HFU373Xo15jYtgEiUXE+AsmBg8TkTjYKIfik6LIF36A7eAJM75C4ePLc/CCmQD2w2sKCMSm6ZCwXyZsAEqhjw2+RBFv7BLeDxWshJY/iaLAjGtY9rbUuyGMeYylQXbbMmgzsVs4y0QIgBiUkOPb6dofUnBfGFYXOWWX9nrYUGrKQUrFGFPDxaNWxhltcvFhAba69GeqjWLNnwYXvMPHJvRkE/RQbZIfhCnwme6UTHWli43kbwx85paTJiVW+P4E9HVRSlVlY1v/8V4yeApVgr/mSNz8vbgzTRe4K41XMOzGNix6n0qjtDP4EOrAGoUp3VBgEnyw6ATrra5NDT90pDdKbCU9ibJNinaKTTpS/QfWijI8Rk8V9cwOVAxjr3BYt6oNJ5zF9B4LU/yxHkeiJawicLJXxBoy2mDvCB6Lq6nyycA+hUTvVkGEyr5DEV0R6p9DBnxbkwazhBMSDFB/GuDYZmu1ObAOKKaagUKi6W1j2uqwbCI1GB/cN0bcoJssECRD+noQSS48eIdIzEKTi1bqX16dzoXj9zVuTYQvCk5OC3Iv4iJVqibXIYPfLQBVfBxnI/EKYKPlnU1XzC11kJWxc5oi+1r0yPULTgPyNALbF1nycUMor9VtAUGDx/Wi9P33WFKni0uYUEBpnipQqIXHrTDTuo3cV+t7rbNXRDXGS/IuDgkoW0Kr4UG1v6o5cT0N3nwQGRYZDUZJA= 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)(82310400014)(36860700004)(1800799015)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:03:39.2974 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9f99f4e3-02c3-4b0f-2161-08dc69bd97b2 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: CO1PEPF000044F1.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6734 From: Tom Lendacky Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI. A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running. For CREATE The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked. For CREATE_ON_INIT: The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU. For DESTROY: The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked. The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. If a new VMSA is to be installed, the VMSA guest page is set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_HALTED to prevent it from being run. Signed-off-by: Tom Lendacky Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/svm.h | 6 + arch/x86/kvm/svm/sev.c | 231 +++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.c | 11 +- arch/x86/kvm/svm/svm.h | 9 ++ arch/x86/kvm/x86.c | 11 ++ 6 files changed, 266 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 8a414fc972f8..cd5f99a8891e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -121,6 +121,7 @@ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(34) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 544a43c1cf11..f0dea3750ca9 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -286,8 +286,14 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_ #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) #define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEAT_INT_INJ_MODES \ + (SVM_SEV_FEAT_RESTRICTED_INJECTION | \ + SVM_SEV_FEAT_ALTERNATE_INJECTION) + struct vmcb_seg { u16 selector; u16 attrib; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7f5ddd92113e..69ec8f577763 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -38,7 +38,7 @@ #define GHCB_VERSION_DEFAULT 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) /* enable/disable SEV support */ static bool sev_enabled = true; @@ -3267,6 +3267,13 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!kvm_ghcb_sw_scratch_is_valid(svm)) goto vmgexit_err; break; + case SVM_VMGEXIT_AP_CREATION: + if (!sev_snp_guest(vcpu->kvm)) + goto vmgexit_err; + if (lower_32_bits(control->exit_info_1) != SVM_VMGEXIT_AP_DESTROY) + if (!kvm_ghcb_rax_is_valid(svm)) + goto vmgexit_err; + break; case SVM_VMGEXIT_NMI_COMPLETE: case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: @@ -3693,6 +3700,205 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) return 1; /* resume guest */ } +static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + WARN_ON(!mutex_is_locked(&svm->sev_es.snp_vmsa_mutex)); + + /* Mark the vCPU as offline and not runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_HALTED; + + /* Clear use of the VMSA */ + svm->vmcb->control.vmsa_pa = INVALID_PAGE; + + if (VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) { + gfn_t gfn = gpa_to_gfn(svm->sev_es.snp_vmsa_gpa); + struct kvm_memory_slot *slot; + kvm_pfn_t pfn; + + slot = gfn_to_memslot(vcpu->kvm, gfn); + if (!slot) + return -EINVAL; + + /* + * The new VMSA will be private memory guest memory, so + * retrieve the PFN from the gmem backend. + */ + if (kvm_gmem_get_pfn(vcpu->kvm, slot, gfn, &pfn, NULL)) + return -EINVAL; + + /* + * From this point forward, the VMSA will always be a + * guest-mapped page rather than the initial one allocated + * by KVM in svm->sev_es.vmsa. In theory, svm->sev_es.vmsa + * could be free'd and cleaned up here, but that involves + * cleanups like wbinvd_on_all_cpus() which would ideally + * be handled during teardown rather than guest boot. + * Deferring that also allows the existing logic for SEV-ES + * VMSAs to be re-used with minimal SNP-specific changes. + */ + svm->sev_es.snp_has_guest_vmsa = true; + + /* Use the new VMSA */ + svm->vmcb->control.vmsa_pa = pfn_to_hpa(pfn); + + /* Mark the vCPU as runnable */ + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + + svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + + /* + * gmem pages aren't currently migratable, but if this ever + * changes then care should be taken to ensure + * svm->sev_es.vmsa is pinned through some other means. + */ + kvm_release_pfn_clean(pfn); + } + + /* + * When replacing the VMSA during SEV-SNP AP creation, + * mark the VMCB dirty so that full state is always reloaded. + */ + vmcb_mark_all_dirty(svm->vmcb); + + return 0; +} + +/* + * Invoked as part of svm_vcpu_reset() processing of an init event. + */ +void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + int ret; + + if (!sev_snp_guest(vcpu->kvm)) + return; + + mutex_lock(&svm->sev_es.snp_vmsa_mutex); + + if (!svm->sev_es.snp_ap_waiting_for_reset) + goto unlock; + + svm->sev_es.snp_ap_waiting_for_reset = false; + + ret = __sev_snp_update_protected_guest_state(vcpu); + if (ret) + vcpu_unimpl(vcpu, "snp: AP state update on init failed\n"); + +unlock: + mutex_unlock(&svm->sev_es.snp_vmsa_mutex); +} + +static int sev_snp_ap_creation(struct vcpu_svm *svm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(svm->vcpu.kvm)->sev_info; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_vcpu *target_vcpu; + struct vcpu_svm *target_svm; + unsigned int request; + unsigned int apic_id; + bool kick; + int ret; + + request = lower_32_bits(svm->vmcb->control.exit_info_1); + apic_id = upper_32_bits(svm->vmcb->control.exit_info_1); + + /* Validate the APIC ID */ + target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, apic_id); + if (!target_vcpu) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP APIC ID [%#x] from guest\n", + apic_id); + return -EINVAL; + } + + ret = 0; + + target_svm = to_svm(target_vcpu); + + /* + * The target vCPU is valid, so the vCPU will be kicked unless the + * request is for CREATE_ON_INIT. For any errors at this stage, the + * kick will place the vCPU in an non-runnable state. + */ + kick = true; + + mutex_lock(&target_svm->sev_es.snp_vmsa_mutex); + + target_svm->sev_es.snp_vmsa_gpa = INVALID_PAGE; + target_svm->sev_es.snp_ap_waiting_for_reset = true; + + /* Interrupt injection mode shouldn't change for AP creation */ + if (request < SVM_VMGEXIT_AP_DESTROY) { + u64 sev_features; + + sev_features = vcpu->arch.regs[VCPU_REGS_RAX]; + sev_features ^= sev->vmsa_features; + + if (sev_features & SVM_SEV_FEAT_INT_INJ_MODES) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP injection mode [%#lx] from guest\n", + vcpu->arch.regs[VCPU_REGS_RAX]); + ret = -EINVAL; + goto out; + } + } + + switch (request) { + case SVM_VMGEXIT_AP_CREATE_ON_INIT: + kick = false; + fallthrough; + case SVM_VMGEXIT_AP_CREATE: + if (!page_address_valid(vcpu, svm->vmcb->control.exit_info_2)) { + vcpu_unimpl(vcpu, "vmgexit: invalid AP VMSA address [%#llx] from guest\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + /* + * Malicious guest can RMPADJUST a large page into VMSA which + * will hit the SNP erratum where the CPU will incorrectly signal + * an RMP violation #PF if a hugepage collides with the RMP entry + * of VMSA page, reject the AP CREATE request if VMSA address from + * guest is 2M aligned. + */ + if (IS_ALIGNED(svm->vmcb->control.exit_info_2, PMD_SIZE)) { + vcpu_unimpl(vcpu, + "vmgexit: AP VMSA address [%llx] from guest is unsafe as it is 2M aligned\n", + svm->vmcb->control.exit_info_2); + ret = -EINVAL; + goto out; + } + + target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + break; + case SVM_VMGEXIT_AP_DESTROY: + break; + default: + vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n", + request); + ret = -EINVAL; + break; + } + +out: + if (kick) { + kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu); + + if (target_vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) + kvm_make_request(KVM_REQ_UNBLOCK, target_vcpu); + + kvm_vcpu_kick(target_vcpu); + } + + mutex_unlock(&target_svm->sev_es.snp_vmsa_mutex); + + return ret; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3958,6 +4164,15 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = snp_begin_psc(svm, svm->sev_es.ghcb_sa); break; + case SVM_VMGEXIT_AP_CREATION: + ret = sev_snp_ap_creation(svm); + if (ret) { + ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 2); + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); + } + + ret = 1; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", @@ -4052,7 +4267,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm) * the VMSA will be NULL if this vCPU is the destination for intrahost * migration, and will be copied later. */ - if (svm->sev_es.vmsa) + if (svm->sev_es.vmsa && !svm->sev_es.snp_has_guest_vmsa) svm->vmcb->control.vmsa_pa = __pa(svm->sev_es.vmsa); /* Can't intercept CR register access, HV can't modify CR registers */ @@ -4128,6 +4343,8 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) set_ghcb_msr(svm, GHCB_MSR_SEV_INFO((__u64)sev->ghcb_version, GHCB_VERSION_MIN, sev_enc_bit)); + + mutex_init(&svm->sev_es.snp_vmsa_mutex); } void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa) @@ -4239,6 +4456,16 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return p; } +void sev_vcpu_unblocking(struct kvm_vcpu *vcpu) +{ + if (!sev_snp_guest(vcpu->kvm)) + return; + + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu) && + vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; +} + void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) { struct kvm_memory_slot *slot; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7c9807fdafc3..b70556608e8d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1398,6 +1398,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; + if (init_event) + sev_snp_init_protected_guest_state(vcpu); + init_vmcb(vcpu); if (!init_event) @@ -4944,6 +4947,12 @@ static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) return page_address(page); } +static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) +{ + sev_vcpu_unblocking(vcpu); + avic_vcpu_unblocking(vcpu); +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .name = KBUILD_MODNAME, @@ -4966,7 +4975,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_load = svm_vcpu_load, .vcpu_put = svm_vcpu_put, .vcpu_blocking = avic_vcpu_blocking, - .vcpu_unblocking = avic_vcpu_unblocking, + .vcpu_unblocking = svm_vcpu_unblocking, .update_exception_bitmap = svm_update_exception_bitmap, .get_msr_feature = svm_get_msr_feature, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index d779f1f431af..858e74a26fab 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -216,6 +216,11 @@ struct vcpu_sev_es_state { bool psc_2m; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */ + gpa_t snp_vmsa_gpa; + bool snp_ap_waiting_for_reset; + bool snp_has_guest_vmsa; }; struct vcpu_svm { @@ -729,6 +734,8 @@ int sev_cpu_init(struct svm_cpu_data *sd); int sev_dev_get_attr(u32 group, u64 attr, u64 *val); 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); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -743,6 +750,8 @@ static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; } static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; } #define max_sev_asid 0 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) {} #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 14693effec6b..b20f6c1b8214 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10938,6 +10938,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) { + kvm_vcpu_reset(vcpu, true); + if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { + r = 1; + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || @@ -13145,6 +13153,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) if (kvm_test_request(KVM_REQ_PMI, vcpu)) return true; + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) + return true; + if (kvm_arch_interrupt_allowed(vcpu) && (kvm_cpu_has_interrupt(vcpu) || kvm_guest_apic_has_interrupt(vcpu))) 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: 13650326 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2067.outbound.protection.outlook.com [40.107.102.67]) (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 4045950299; Wed, 1 May 2024 09:04:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554246; cv=fail; b=jVCt28Nby11h7RxKEfjttDeMqtawHwKuXChR8MT2KL2atOchJtro/3DXaPQJ4l+K/ZHCcr4uKIFdYM/cRC4zj9n87deOwDJktNiHiFcRkgofJMeWi94mHyhYmpPtMW02YIGGa4QK6LdqVoficnRck4uuHNa9VpATnqcgOwWBYdw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554246; c=relaxed/simple; bh=QGZqGIeWFDQXCI7RnwyVxOJ4YF9XhrWsIuXcoWkSd5Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rTEH6jc25xsdwrZxUdQ02RFHt3+V7FgMcJIHbfnG2zmsnq3HiZvG/i4EyjObK0CJwos6CUJQTk75MFzPlcfDHZad69fqUWYcPg4eyBwEUO/ptOCW/iRz6ZOlt7UojvFLJwYb5/EWL319CmzHDaif+S9Ir4PzIe9PCeIowK6FiEE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=CKVD1T99; arc=fail smtp.client-ip=40.107.102.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="CKVD1T99" 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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 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; } } From patchwork Wed May 1 08:52:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650327 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2074.outbound.protection.outlook.com [40.107.92.74]) (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 C98A650288; Wed, 1 May 2024 09:04:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554270; cv=fail; b=HCEO3KIksIUWTDnizYMfM+ahmEyUvqdk2hZ8wSyKY92xv/IZSmMAYgrnM8Ye890HPSGF7g/iQncFh9yzv82pWeqmaffwqCbSSCbBt/h0Vl2L2ycm6mgz0ghnF4NPzDv+Ni8/aftwymJIs0PZ+J17qJUkoZOFPnqt9RDx2jLsS0M= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554270; c=relaxed/simple; bh=uW7L+NNzdAwdplqdooe55yjkQb0b/2k/0HRM//9PGxQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C25J6SOvkFMQIJiWzMIgNKjC4cAsj9jc+KZSpJwXABuEuPajvpOU1SThlDq1o350PyyuJr08c+ZhCjD/TKFGRebvqaxnBfO5LDZ7mZxZ7V7c07KWNVWm7TA8fWx17HqSEA3iTcr1Gn31/xK2WSfggbb+VIEcpC/HehkciGpqJLY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=hYeQDNOg; arc=fail smtp.client-ip=40.107.92.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="hYeQDNOg" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iEz9jp5vB6xZOETZ2Xojf5O2gRaNs33FkrX0ST/6tQo0mEbFus0I1k1D1usIoX9ATgPtJHOeMM/CHakaf23MDP+b7ew8yOuSJDR2HRQK/bTu7yx3HQvulOaEddfzusRQG5RX1/Mx0i8p6ydRhzFnQHOFfyj9LopSPr2ZMJQK2z3JjXBELMX1yZ30e84CXVQQ9WM8SegWrMZZp/7C3u0faHCZll08W4hh20ibPylNfTGtUhUQ47zuJuFVbEhRG/FL04UzfScJMkebq2e9ESGUBWrMfQ33gr80YQRrQC+i7eF+4WqB0HWwuQD8+5TDNQmxuLOyRlSm9meBxScQH6ll1Q== 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=FS7iOw8OSCDAeMDBOiBD6sqTzTf6l4cypzmEfY1yRZ4=; b=KyskQ+R2GKK516LpEefGcyRKg2ykjmWfncJ6expBBwmNdc+tJcep7lk+79xH5OxOTzVkFUa6AVJ0QA+o+TWpEEoiach8cfArzzJPEMWQEr6S6izJtY7QJJiHLzeCqgHUYpo5xXJ+E/118QEiDGN9Vkg3wkm0dzMgknHwb1WGemv53FWCe7EpZ0qWh0gBsSx60dhJ9emOVggwFC/Su/FNDiLsBVdrJTnWAL+IeFVrVxzfraZVYUjWv1pY4eNq54AQehe++4RfZ8ph9n/GxN8avrmPcHH1vPr3exBfEbCNO9sKrbEe2c2iizBYOIiTF5sYeIzmRD95p8re3yE/MXNpuA== 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=FS7iOw8OSCDAeMDBOiBD6sqTzTf6l4cypzmEfY1yRZ4=; b=hYeQDNOggsnRTMo8jKpQerq8ZqUlHfrbLJbK6l9HL5VV5/95cbYFOfIr6whFo+BiLSL+Qg3SdekQyq0hG2ReMRdwZGrIzDC4dT2bdAka9eiqWPzwpWBhxs0B/TwJx4A6u84ZnoJQgOok2MBppw3E882/ZF79c8ecGBJhfiFkeZI= Received: from SJ0PR03CA0044.namprd03.prod.outlook.com (2603:10b6:a03:33e::19) by SA1PR12MB8917.namprd12.prod.outlook.com (2603:10b6:806:386::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.24; Wed, 1 May 2024 09:04:24 +0000 Received: from CO1PEPF000044F4.namprd05.prod.outlook.com (2603:10b6:a03:33e:cafe::ba) by SJ0PR03CA0044.outlook.office365.com (2603:10b6:a03:33e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.32 via Frontend Transport; Wed, 1 May 2024 09:04:24 +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 CO1PEPF000044F4.mail.protection.outlook.com (10.167.241.74) 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:24 +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:04:23 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 14/20] KVM: SEV: Implement gmem hook for invalidating private pages Date: Wed, 1 May 2024 03:52:04 -0500 Message-ID: <20240501085210.2213060-15-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CO1PEPF000044F4:EE_|SA1PR12MB8917:EE_ X-MS-Office365-Filtering-Correlation-Id: a1cdd03a-7e00-4720-5c3d-08dc69bdb257 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: OzRVf3177MAe6xGmBnSo6cgr9lN2xYfJf7xzTVeABku0Bk+3+L/cWkaDxeMtYRUqkxKrNhjcDP6bnmYyDE9WHhOh2d55QPIipIhHCO6Of23ykpU8gQVxw5xrNNJ5ANiW7DTX+rNg8HDcYV8/M/uEEN86sZsXLE8IO6Ul39EQMzrZFmS5c9vrjKAR1xFKDmkL8vsm84BhR3ahUiBN6H0Pa1Qi4J/qSeOI5l0W9gveQ3VzJDD0bC0CuqgUBC26/JLuwSsxfxWVkGkQmSnRyk3VuCh5/x6d1hl1nWhcuChGOoTpDt/MYe/dfEtkbM8p0qU2bprTw2uLp79MGPOyjOoojKHQobpzGoo641njrNRoSuVqYwP282+pvp7OjmRKcOJJjhT4w/sqbCFWa+RLZn1N4rXlSVPbSjUn19ZF5g75LzZcxAUCpVojh1X3N/hJt8/2LxhTinuiA58/Xu8s0xocy+KcTOHpn1AFEdR7C1icAV4znsQ/yjyaBwG7UfLNof6R8CrJaQ9CXuwMnc667tjIFQgqoHGjd/BozHjCtYyGslIvdmQ4h9m8XGG5pW2mxqRqncbq9Wyy65gogbs6CTiM4RjqLEjKF0+XDyRSjJB9NlGaHxst1tjGbv4rF2ExZvY6EnkDmxKd3Ycnn35BL+dC0ohkDtpyQispdFUD8SQwpRap3hjFvHn1YyJMbcOJ7UwO1kSIJDQYgeGBzj8ixzi64mNNVYvXVtgkpf8lEyj+jo3SriTnVBab9Mv1V3MmXn3RndEVQqj8aIMf2/TdrNiFjKuTHfYVPP/0hXy8uutUjn9LZZYWjMJKnuHdtFpKZXJz5Odd6YzZlBcImLenlytxwGX9m/QGhWg7I5Ehs2lPk+9Rf2B44PZvDHWi8btriGfoPh1fXpzNinalutRbQlfBp4psUC97M1reZbmTuwdI0aOmti4LroAkuLiIBLKeMOTTXlO3aORMV9/g1QToubtTaG8eeofJy0OGHPTYQ56HKZf8JYqrkecGZqM+6irh9GWYfw88ZJWyvhCx3cZvwUMaTxccpkE3mqp+QjRi85Q6z6D/9CPBWzpsJVewy/00bDDEVO56jG+RokACQX6X66HoAj1MLuMWfKwvEQd5dQ9FUidULxNin4b8mFAwvZCjZEFPkUikediIP1u45QMtlth6foAvItlD5bbtRfuHtKdJGIF0WV2crSg8OulDNpFykAEA0fS5JNlvZQAaM3vqroHrSC3yve2hDPSM6nQA+B1Culpo4rMBJJp9cGkamZwc4XRLDZFa9kdIK8QJ30rAQlvOSz8Dl5/r8GzNEgFMxUJr5lz2WInQv98WU9tKRvZD2fXm6lYLLQocY1Ck7iiEUcQxbLWYjgdW5nJ8rVrfWHpU9OM= 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:04:24.0166 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a1cdd03a-7e00-4720-5c3d-08dc69bdb257 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: CO1PEPF000044F4.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8917 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 | 64 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 ++ 4 files changed, 68 insertions(+) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 10768f13b240..2a7f69abcac3 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -138,6 +138,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 0439ec12fa90..cb89f6eba6ea 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4655,3 +4655,67 @@ 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 (WARN_ONCE(rc, "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 the 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) { + /* + * This shouldn't fail, but if it does, report it, but + * still try to update RMP entry to shared and pray this + * was a spurious error that can be addressed later. + */ + rc = snp_rmptable_psmash(pfn); + WARN_ONCE(rc, "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_ONCE(rc, "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; + cond_resched(); + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 60783e9f2ae8..29dc5fa28d97 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5087,6 +5087,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 ff1aca7e10e9..f91096722e29 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -737,6 +737,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); @@ -757,6 +758,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 From patchwork Wed May 1 08:52:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650328 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2067.outbound.protection.outlook.com [40.107.223.67]) (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 456A750A6E; Wed, 1 May 2024 09:04:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554289; cv=fail; b=rJEBGzAwakPQ8zfIhBnu9YVFylFmZMzdw9CE8TPsvbU3IfmY0dGeKuAGcBBsa0EOhUHVzI7o6gIztOEOO1MnC2DvNG4jRny2CyQFWJ4XL4z1XrbDMH5bQCQgwwk9G3UJfDIxuSN9xXuwttDqzPZKY7i+zVg1kOf1awol1ddFrQU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554289; c=relaxed/simple; bh=R6RwPmHIuzdiwx1hWrxmaknsya2FXoxHCu8cQuC+zbI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VTl5Jn8+bXf6d39FtvdR5ATeIBZccHvPijsLT6Nrl5//PGMN9D0W8RCWv8U85KEtgv4ctXMp33rarewCYuRRG5Zu+x3Nejw67/23w4rbPcMcUtEwSfwV0IwmM76EO19ZjN7j1ccc/4kvq0eN8g4qWPSI3xZFl3lfEQTfiQtbNsc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=0Yq8QSvT; arc=fail smtp.client-ip=40.107.223.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="0Yq8QSvT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aYYRQeMh8BruveL91eD6uUtvSZElMhrTkMtBDXWWl+bsnto0FSf5M31X+UPALT8XzgkBKm9YCQRLTSoMR1XLOiylJ8BEVv3g1SVVxQoMfrPrzMH20arWHW3XUqhrzvJ/EpIThTfdAXrUPomWuYXLKmwC7k9A2WBioABpNHlfxWCDIa6sJaVfmAnoAVia9WTiqS434KHhDrvEhHrUVC5uvd9L9NOInKB46SyvEVlzyUlgUnK+hlVg8UETKaL12gm8KA48O2zGKNPXCdC3lC4hVLA/0JB7pXzi4ilDffiJAd+qcTfN8YcZC4BckOCEHSyO6EHuJwmmHNKYlt4GrEDP0Q== 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=odLsKy2BVTXhQ/IUMjT5RUgh9M/qTLi8/RJo4yE97cs=; b=YSYXIZh3IP5WyH0XgJniflOIkSLI83Bhe8kWR8NSqxabcCGKw9C7E5nJIGcvUXg7uTmZYk+9HC79TV1bfSBKgDV9mJXlnJr/Fkkw28cN9qbvcvMN47c+fTAw5nQpQ4PhcEwqCgPTtP8JM3ABCJfGTgKvEmOXZxBwenx09/ta1SRPOaDyPlSA2T98lkOlcWuengA3PESjkHJ81GTEnqUWXwEzUR+fzf7HCOapRgkNT650n2BM+e3MfO6vlrLu5xQloL29LCfSvH8o3KWTHGDiuDmr0G0ONppgVQOI1i9iY/M1Jjx6Fy5Ey4IngJKKgOzn/FnMX+nAIdT+4bv7irWGEg== 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=odLsKy2BVTXhQ/IUMjT5RUgh9M/qTLi8/RJo4yE97cs=; b=0Yq8QSvTEfxtED6bFmyKy1glMVQu8W0+9MY6yhTjKOE4hGpn5jl3YSZscvZvFgdI3VJpqxg+79YPS7A5mRrztzfn3DzojZbTCg24QQBSleLlrhst769Q/TbSREInho4qEzvfYwcUSjl0ygKKT6KOMZHNnNn4hvGpirnORFJD8QE= Received: from SJ0PR03CA0049.namprd03.prod.outlook.com (2603:10b6:a03:33e::24) by CY8PR12MB7338.namprd12.prod.outlook.com (2603:10b6:930:52::8) 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:04:45 +0000 Received: from CO1PEPF000044F4.namprd05.prod.outlook.com (2603:10b6:a03:33e:cafe::6d) by SJ0PR03CA0049.outlook.office365.com (2603:10b6:a03:33e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.27 via Frontend Transport; Wed, 1 May 2024 09:04:45 +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 CO1PEPF000044F4.mail.protection.outlook.com (10.167.241.74) 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:45 +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:04:44 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 15/20] KVM: x86: Implement hook for determining max NPT mapping level Date: Wed, 1 May 2024 03:52:05 -0500 Message-ID: <20240501085210.2213060-16-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: CO1PEPF000044F4:EE_|CY8PR12MB7338:EE_ X-MS-Office365-Filtering-Correlation-Id: 80eb790a-11ff-4364-035c-08dc69bdbeea 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: K32I9VwWuvDmAbJpOGAbhyOULboQhvTw3ra379GBwIpEl77ornxOrmjh+ty+3hnHFbM2lwIlJA6dHtS1FHPU8lUyrKWjIo2UyVKe/Jei9ifMppCiL594bCZIMwP9K+ZL9QMsv4+6x04TDW0AMYsabcYeVwHIywawBsU7hQ5hgYDIWs6hvQV9b3MHEjZau0S1KM5KuHHJkOWt2i++TYIIWAEcN1OHCsf7T1pjJCWjE06wGtAmuZ1zmv0N7PdKldEWSGSGlfOO+f/8Al+sW7JoDGi2D8DQQHtR0RXmKKnFM1ti8uhkkv3gT/TQJlbyHgWwXrZFpA2t2+ZEGtO6YGMyPNU2W0GjDEPOoXMMdzcxNwuCBXzl8soAPIGJXzK0rL53Rbcfrahx5lfErRz878YM/RA+szhWzgsUbrKkRXdsCkwqgEYaEyoO80oWFA0pjSPN4dVSDP0eDdbPesxHG3ysMtxqBFFxPyRI/W2TR1e6lSTmARtfHO2g3orbAOBaQO44x+jcc1rWDtB7dS3/rcF4n5XWNULf2yDqJvRUPmeRh906hiHYKnYtID9DdZlQM1Jqk5X2gHXqDQqA2H4szVk3SrYt6rKWReHx0WedDvonBpOEAjho8cXMzK9QHrea9da/277TkvNwn3pj3Gh75Wv8goCI5yWyrupipnWI1vnf7AhpDUxyA0etk60fiDUF7Nx1E2ui5qFEheZL3mzFFS/05wA1RAWsBE3yy2ek/keNdSwDzEwwBBqsGWBdE7J83/diIh6fPWthKjhc1pYUTandgESLE7PfPGSKwlR7kofBj7LFXq3h1QMBkRrRHQPawU3dp6mzgWhWhvofhJ2GjV3hFRamdUqvuwpK+UO2gJT9OKXirkNxwpKsLKv9fq82xEtljP8dLkg/s1zBLex9MmJFAjX9oSfpFup5fWkGPJkuLE9QT8j16SJSFdceLRlBZ+eC9tT34zlCMw9wyfARdQJv9lzytLSHL/WmAov//xXdZKxBw46xoEfbIbABguEr1Jsgyb2IffetX4L4XRV5DcURyVM8vPNTrTqnIP2cKkatdsr9ewB8tnMH45L0t+wl5NAyqfke/gQExWx+lmWo+cg+UC/7EyVTIANjXePB+yuwMzkPEUSnnGhtiAwg6mN4nhCgoDxfbwkwGC9yC1SAtQj2iydFz6YjXawoOYDcRrBCJOK/VGvm7krIcGDIjvyC893q4RrBBLDuds+pnLQnqUOgGc44fgV8VkRN+Yj+FnCAaeicS0fFkoEGseGpZzQLi0xlQTzFwqt9XZTr4z4k2pygWXBdR/270qC3lXWj1DESkhfxhsN3RFZKMin3xKlCRJDfnBRvFSwgBVt34qQkEIlBt8Frj931G7uX20VtjKqjg+DpJuKGN/9cGBXv7GZ4LIRy 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:04:45.0479 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 80eb790a-11ff-4364-035c-08dc69bdbeea 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: CO1PEPF000044F4.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7338 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.private_max_mapping_level() 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 | 15 +++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index cb89f6eba6ea..224fdab32950 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4719,3 +4719,18 @@ void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) cond_resched(); } } + +int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) +{ + int level, rc; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc || !assigned) + return PG_LEVEL_4K; + + return level; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 29dc5fa28d97..426ad49325d7 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5088,6 +5088,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .gmem_prepare = sev_gmem_prepare, .gmem_invalidate = sev_gmem_invalidate, + .private_max_mapping_level = sev_private_max_mapping_level, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f91096722e29..e325ede0f463 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -738,6 +738,7 @@ 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_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn); #else static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) { return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); @@ -759,6 +760,10 @@ 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_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn) +{ + return 0; +} #endif From patchwork Wed May 1 08:52:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650329 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2054.outbound.protection.outlook.com [40.107.236.54]) (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 DBE0E50283; Wed, 1 May 2024 09:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.54 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554315; cv=fail; b=Q9+ln2p9m2IOaJ3y/Qke7xTRRMCi1PREsOsKia4MaRHXyLu4ghP9YUyZLARxrUakVNmrv1OnC0K/R1ihzMkWSabwWAK7fytmTeg7tk7kAczL1g11XI9GjIznh8nDhN5pX0foaJYwfIl1IcOJLN8liwTGgz+2BXD1++z4aQ5KNHA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554315; c=relaxed/simple; bh=SlGqphJ5aFaJcRjcdzvzVwD8OjnQRzyqkLhLSvgNfIw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uRWlKjsb+htiUON/Hm7sFgmAlba0FilH9XK8kC6rN7ac+pe4uZpMFTEFGlsCUdy8j13Al2hH0mgva9pH+rR/F/FwhwqqSrLAsWKcFawnaXiKDsftmspnRwZyKo2Cua2a5CQMEbNHxUJLhfIj0wB/YI0hkBX4u0gOE3vloBOYnHk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=lhc4i/Iy; arc=fail smtp.client-ip=40.107.236.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="lhc4i/Iy" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dGqTL+/a1ei8TmW9zsnae+oZbxhPQcuZvK54/zYj2IXBs5JVU+0C93l0mNyghvF48LiT9dN2DSW+7g1gP7WPSW1n6toAZylUgbDuwENiUYydOOO5eaUo3eCedNYj8dVI8iJMmbuD1qY+xQdhyEUYRMp7zeVRYFl70tnLfFfVkJwr5pdY5XHB2LoEu+4rO0BCxA0V30vFxLRIwFA4NFgid2z5Za4JJbdeFRSzeI0FwhvOotJR/5BswAkWyD99ymJTS1AHDwAqXKWE1ENUOiPs6ReWfnxivufUpW6PMYoB67n56lkCNZbZya/MSAaN0rxOyu7RvPTp7qW0qMQJEpFUZw== 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=RY+enspJL5gaYp4jenQZ4TzSk3E82OmppXhPjsVB6o4=; b=H1viPb2TbQf9vDgtH6KOQ472zmC0rcVYqHJHlefsfDsKX0d0vpziwU7TD2+3OoK4IDafWolVjwuu3f7bGZV4QyKVLkug/c1CdvLvKIAf7w5JW/6/nE8ECPQiOd6Ru+g4xDwqLsaG57YShZor6foAxSrSzpVTjr/MUMWODIYaRiYdNu4rGjw9czxPlNgMoOGMbaaNcdGsRjZaXj2MQVVUPArOzuaCnMqOmaBDrbXJ5nO0H88tmyNyYAJnCD/f0Q2M8D65kfQWRSAKAjmgUS9g9pgQvT6QRn3tGd2WgFtcadHV/wlhqENxlfEAREISfQPmyYnm+eXeg0icM6RmKTG80Q== 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=RY+enspJL5gaYp4jenQZ4TzSk3E82OmppXhPjsVB6o4=; b=lhc4i/Iy930I+1DDrpTX2BWCM2fWLehn2UkBVWL1/+rRetesGWacU9xyGa7PF8IZSrxstyN/miMXc13fqcnZf0ZoFKaWvc+UMK+vqSgRlZLP6vA3aEaTB4KJgyHKyDhMbHSgH2z7e8roEg72+7vkTgi2H4J0iH7SSgqtIuszg3E= Received: from BN8PR12CA0015.namprd12.prod.outlook.com (2603:10b6:408:60::28) by SN7PR12MB8101.namprd12.prod.outlook.com (2603:10b6:806:321::7) 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:05:11 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:408:60:cafe::90) by BN8PR12CA0015.outlook.office365.com (2603:10b6:408:60::28) 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:05:11 +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 BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) 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:05:10 +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:05:07 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 16/20] KVM: SEV: Avoid WBINVD for HVA-based MMU notifications for SNP Date: Wed, 1 May 2024 03:52:06 -0500 Message-ID: <20240501085210.2213060-17-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A3:EE_|SN7PR12MB8101:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e60ccb4-a7a6-4f96-35cc-08dc69bdcde7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|36860700004|1800799015|376005|7416005; X-Microsoft-Antispam-Message-Info: 7EXutAV9z67jxAWXY+EEe0xFLLyq3DxCCTrXd2Gim0fkdxNG0DkiyHoI30K0XCenP1WOnlZsL9C9OjbZcbmvFGjnrKXCv4bg5tZpXFLukuLFiYx7FEzdfFHleFg3uhfB+ZEZow5mmTOD5QPVFavrzHgrQccpzsm4MU+Ytwv2XiTNeUO1fg59/ZDs/FVFO5KCwj/O/GsrLLGJhdUIvbKxNLg0oKFcSi+ZIyo0evUw9RVUnipaq75/MwM5wLwKIJriVGRhFfcxhhArV4TboRHLpQMa3gJd4uKvcNlXrQ+S5g9lMOCqic1eKsq/NFZVOn/hxaL9XG15mVbpLYIDWCbkJIw2QL38dt4T+9VTLCUHVrUyAa3XL9PwZUBs4SfDHua6yIFwQVljDdjbBdXZqiJQjYHfyMEvtwTTxylMol2xWxdeFPHHh2AFz7InoRxrUzQyjj+HMJzd9B+lK56yF7PVwW03RGjv2iUXIwroXcyZSDupKdzdLeXIOrq8usfYE8o0GQx+pvjU0TYeAQ2qCs8xsp4AvRvWkEVwPGEetIKKXhvdsuaVBSEaKMh8b9azw8ji2qEhVK98WaTkrayTWvK2C0ivf0s7YsGaPAL2ohhZQmxLrPuN6Nt/kPYYwmf7Y010nk5Hy8P64E58TfEljGXWS/eb4yvm3Ol3eaxjYI73wIOPEYKCemOWmXlmLvNcLrKkSWWDy8ZTtDqNuWdqmcbo/Ws381xLQFSmX43dcBFqWDwvvvz8KL/E3yvKjMhBKZoRuoRFEclYOun83xDFDHjIjODGbCYzMBqKw0Is+a0vfTGAdvPsUd1l3GSXXbm83PBEZfpvjsY/hdYE7k25CXExVOt4kep7Nzdszp9AWThZbu2zG6qX8gQth7JUh/psFUfrDSPk9xHorGqco73XF0KH62+0r19dRYhqKAFYlMjocD+Cp2AFi12RkNiGNMtLldAS/N1c6/Oqiq86lgE5N+A92WwYBO0hoph70mS22EDmskMBqWzQpTFJufRrxWfMVBfnd+taxz3HDlMSwT0PgIfIzgn5GF4HeatyOjb3abekS+rYdwprZWaj1btHNm3PKl/6KA3so+Rqdu9tldYjF30SdjJJ8/4m2ng2S3qx5Z/N9TQqg11ioijlC9AgskkM1SAmUFCvd3zNFzc893lnEoob6pQuPtaCpmEyoVXW6GvJkUgiKoWEGKiymObP5z/bkjdLsVTAnbv5PvnrhTrliV5UmtYhTmQddK28r9fmGKsm2AlHsLd5jkGaU3Z8UwyWzPb1j4sSlMEvC7pPezP6Cg9T7gcQXGTPkzJC9Ts/qxCYowsEQ0SeSz/eKG5072C4i9Mc6SFsRXncH+QLN/9YHLKgMa1wih9F68n6tOrcI0GGT9k= 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)(36860700004)(1800799015)(376005)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:05:10.3203 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e60ccb4-a7a6-4f96-35cc-08dc69bdcde7 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: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8101 From: Ashish Kalra With SNP/guest_memfd, private/encrypted memory should not be mappable, and MMU notifications for HVA-mapped memory will only be relevant to unencrypted guest memory. Therefore, the rationale behind issuing a wbinvd_on_all_cpus() in sev_guest_memory_reclaimed() should not apply for SNP guests and can be ignored. Signed-off-by: Ashish Kalra Reviewed-by: Paolo Bonzini [mdr: Add some clarifications in commit] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 224fdab32950..e94e3aa4d932 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3039,7 +3039,13 @@ static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va) void sev_guest_memory_reclaimed(struct kvm *kvm) { - if (!sev_guest(kvm)) + /* + * With SNP+gmem, private/encrypted memory is unreachable via the + * hva-based mmu notifiers, so these events are only actually + * pertaining to shared pages where there is no need to perform + * the WBINVD to flush associated caches. + */ + if (!sev_guest(kvm) || sev_snp_guest(kvm)) return; wbinvd_on_all_cpus(); From patchwork Wed May 1 08:52:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650330 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) (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 B4A364F881; Wed, 1 May 2024 09:05:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554335; cv=fail; b=CXvixME4tGwmRn1lvSLM+nGD+7FvznZXX/XLPYou70lEYIboHpSwP5/5TBuOnHVwjlwe/AD3zeKYsh2Ipcy3Mv89HvAKBA91OrKpx3L/au/dKYeEV67sZdokfQCDYZ+DGqdMXE5RJwS3PnSS9jaBYVafUkDEr0/+RJyjVRj8bzo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554335; c=relaxed/simple; bh=eiwFA/WtOymC834NZHngmA22ERsSPKHLWV5Kcn8NcTE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ghPHcW1gc3qOx2bRLYkrd1whjTeXSt1G8W6O2ers7Bf1xeD3bGuA6AVYy9Jws2hkt7rWB4/BJbAA3p7zDJdAKcVr7k9Lp8S0hk2AETP+BeLwpR2O6KeSya2WJ6XuOOmw5rbCizkwTFZwUs9c9ESUAvEWAmh7f8hIusYjkzRmEFQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=l9jrihWB; arc=fail smtp.client-ip=40.107.92.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="l9jrihWB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QalWgB1Zn+TsHYgm70ySjNhuKkqgoZLB71HIH75C0fUh1/jlkwSIYdin6goVokzmmTxnWGUG9akMG/FwBAlP4hIsEcvaxz0BLy3fMTwFM6RzUeGkvn6jtCzXOJEiBxsMjSUVJsvrgG7CROupKeq/cGl+dKhzjnvY7TV2Z3uRgCjM1fe+TX0xLqTPE6X3KwjGmUWWSwz0XBNUJ+Xg927BYpskvFhK8lq/kqFgAB9mdyUItPNitqt5hByd8c00cGA4yUpIQJJpHAWEcnsiU/rFvn9p+fHs/SQbHYPgyfORafvdEoBTvSmI5ipjJxDvHEVdKcBXkIEd3iuSmVbKURoRdg== 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=Y+FNVfD9fNFkqi60nkH0uAzq1nrTCqNbeox2T52AD2c=; b=aqw1meTMsIoxPMwOo2ubzyzR+4BAnBHGGg+dCxYlucPQ09jyDI9c55im/xnM/EQlBaQ5F3anjWslz6qFq4dWIHAOFiQAi+hvFzJPgQ0TIoA1NkcU1+vSx1JzTDmihm16rM+64nPrzFiS3HI7pM+22FEbIAPK7IH9aZwcL083mYLfUziX1X+rSijX/1UdLZFLfv+zMX0i1fhflFc5jaPCtOr3pZLr8U6fz6vHVl/rnHfTfsajcdYMsBq6OFPPCNAt9n7OzKHrKBWq45DiiuYR8iUCtB7VXOER68Qa/uDhY69haBqxZynfMjbxi1RmFuH7X910fZdQ/+uTpDyrjeAIjg== 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=Y+FNVfD9fNFkqi60nkH0uAzq1nrTCqNbeox2T52AD2c=; b=l9jrihWB4tY98VUlHjoLDc5Mb60StL1LeHv0v9bvd59uksr1FVD59CnFMVUy9fySEq11f1F9mvff7/k1VQAnk5W8Gokw15C2ttLxmQgaeEZffDFS63mxvO9HLW3lY+LCBV0cJI6EzuxmLrqwMPq1QT++duvSQmvu82yVtxtzTF0= Received: from BN9PR03CA0892.namprd03.prod.outlook.com (2603:10b6:408:13c::27) by SJ1PR12MB6220.namprd12.prod.outlook.com (2603:10b6:a03:455::11) 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:05:30 +0000 Received: from BN2PEPF000044A4.namprd02.prod.outlook.com (2603:10b6:408:13c:cafe::4e) by BN9PR03CA0892.outlook.office365.com (2603:10b6:408:13c::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.34 via Frontend Transport; Wed, 1 May 2024 09:05:29 +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 BN2PEPF000044A4.mail.protection.outlook.com (10.167.243.155) 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:05:29 +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:05:28 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v15 17/20] KVM: SVM: Add module parameter to enable SEV-SNP Date: Wed, 1 May 2024 03:52:07 -0500 Message-ID: <20240501085210.2213060-18-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A4:EE_|SJ1PR12MB6220:EE_ X-MS-Office365-Filtering-Correlation-Id: 7242241f-b64d-43b0-4242-08dc69bdd949 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400014|7416005|36860700004|376005; X-Microsoft-Antispam-Message-Info: Owcem0LFO1Xy5hTdrGqTXe9bK7gK1cF8+k57St/25NcVPKfM5ecyP8QtmWqP4Wjas0dcrFeoSQnlAjacq8/3NjInnnLUh8WQXoq2RZyMtCnz1iyRzAFHdPbjXeYulwRQjYtl+WQmPbH/yRqTk4lpkVlPues09UAodv6PphzHk0qjmBXC3Bu3AzvNKY+C8o5nFIVZs2SBOolcvXwi/lwmfP45iqoH8vkPJkUVRjZ5Y/tVoriN45MDX8kAtenQql62TWuRpphDgvFkyuNnPJyynlfsaueFgiSIPObzqqQnhGtHJzTPJFFgm+sPpOZLfszaytQ8e1XctkPKu/9p5YC1SRM57nI6OLEubml4yV062HXsdIKwtawvyldyeqRUMpjeh7+o3u++WxBMFU4ChNt7uuwwFjWRAluYWcTyxhRqrVaQVHAhvDkaQfWCrb3P9nrwBn+qnnqdUDtj1ILuVhfcrQCD2Ymql5cZjYNoSfHyN7j66divwIO9YefJJ0JBLcvYmY4sxsRmcjFfAphdM6XLz8xSZly+tpVnVIEx5LqLawyGwy0zYsDsAtIz9lvDqyoqSWvPGj1i6rw8+rP5+UUMPtX0tWe0I8JJF7dMZp3FUQtbpNPYYC9Bwr8axpGtueGVH+294HVTXw6vGR1in9oQzNLVSfYVauTaxvspi3bqN3KXbJlpVb3kDaZ0kWlmPqY60WfXJ3ydWJmTZ043BdcQ6fA7jaW0tuwmXFSQyWJqqrX0r2GhpI7XMfAZRHW4r4+alLQUgxPVoT4J6I1RAawTa+1TA4H2u3/rwV5UYTiaGd2DFHOln/TR71rUDwoWF4ccJExP/toGKdX+7FbrVnQlvrJ15IWkaKFvsPM45tXZNZuOZfUPIZOAV0AFDESI5DGeLWaOpXCQYNIcs+X1yLlyn4ClZioLO1wzFJaFMXAPTaUb+hpGLbc21njUR+UDly2IMJLSqI1uVNtnTyG6/+y7RmMvmdqgPRs5drEaOhKEkat5RnsayLqfIQ5qBUjVyTe9YWG8tQJO9uyFbYQYeDj7ijKO5/AekFRFkGya9Wv754N88HZnSby0RAR9a43F2W+yWv/1aXlio6ZWNoLvSq7nkYbX7B7kn0hKi9P1FoMNgx2/ORwS/W/Rp9/X7WIXdfXgpba03legx/HwIjHvudh5v55VGTxZVyMLiI8c271jQBoPvA32fshJmEZY0CdEHBO+1i9rua7hhWpO3TGLcM7N8qhJnMSbIDa0Zs0IaaPzwQpEKaEBfJnH9Q3Kz/+STEIhFr90WMfQL8HqD6Pvt0x7ySCXSnob+umjN2jmDv3kEo/ndgpR8aZiCAyKq8NEHouHJr3kdUUTFgs39mD/r70El4xIexskJgnlfe3pEy/qeqw= 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)(82310400014)(7416005)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:05:29.4335 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7242241f-b64d-43b0-4242-08dc69bdd949 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: BN2PEPF000044A4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6220 From: Brijesh Singh Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported. Signed-off-by: Brijesh Singh Reviewed-by: Paolo Bonzini Signed-off-by: Ashish Kalra --- arch/x86/kvm/svm/sev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e94e3aa4d932..112041ee55e9 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -49,7 +49,8 @@ static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); /* enable/disable SEV-SNP support */ -static bool sev_snp_enabled; +static bool sev_snp_enabled = true; +module_param_named(sev_snp, sev_snp_enabled, bool, 0444); /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true; From patchwork Wed May 1 08:52:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650331 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2063.outbound.protection.outlook.com [40.107.100.63]) (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 7888A4F881; Wed, 1 May 2024 09:05:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.100.63 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554356; cv=fail; b=QqI8Rtg3IexSBf+dcdXhj4gQnSf116J8UxO667mn9A6QlsbfXMJ+GMo9pp+dnNgXQWt17j23cpNxo5x4AJWK1mMin4JxIGRxJyjFlNL3XFI/ZyZ4lURJFtTgJrbUcdcV+IomAqkMOXgh5Mu/2rsKEpsCC6Mk5Q69Oudnhgqwnz4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554356; c=relaxed/simple; bh=sy2cOt+9o3XO29YvToEzahURb57X50jViXf/puyBieo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eWUKzh97um6y5OWQ/fR5RzQOSwnhvRnlWfM90xjOCa0JnE2PPuk+3UMxZwa/fIBbrEGEdLJcW4HQk0TbY9EGsowSCezSB8y9HGdUzmLgODLqBO4q0cgcN4uhDJ6Til2EZoqmZjx3gOOE4Ni4Ns4Qqpj5dpyiS3XEjBId5rkWUHc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=SZ1fMWAK; arc=fail smtp.client-ip=40.107.100.63 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="SZ1fMWAK" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XfYrVl9/goC4brzg/QQOXl518Yd0Wj7Um13k3hXNXRBFVw8rf7oWuSTpmbil7S2gV+CBWQuZ+xdu+1ny8wwNuHEz01RjTm2OtA3uWfdB2XI2bGXuKrGE7PtKqByVq/TvkmfzwqpCde23U+wB7c+Fl1CWhEJNsKUC6h0JjmNTC28UWW/NgFE5bAnfHOAk2Np80f3Jw5UvllAOla3kITtU06YAnRdALEJZTQHQn9y+1Xpi4Dkasc64Vqp+RuUXG3hBrW4hJEnkIVumILamLEf3amqhlEP4xbEv4rt56U5hwJ6B0uZVMn92bQIjlWc/XIB+shL4ybZN7apkpce/EXGoSg== 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=sQbyuPzaOb+gGVc90x/XMaLFc7sXGq7Y/GylR/tiuKQ=; b=UwZiqTgEgUjZj7Zg3EVWAj4GcS6yEZP/kvFUKgLrWb1rqS+OL2wadXKCLBPZEDtNowLAU4UAdItCa6lnLLE5Qt0qtGFyZ3Ptht1q3+XAiTXaKxhXRs+o/jShfdt9TqElp+SRlK9f4ZfBKXYahA01rtgo30l/+mMDQghieYwVi64mGQTRCcBIu2r+CgXc5lfckLdxCAAtbPZQd7DKKCSaqtAdaaCZyEL09V/g0gMgeuEVsg3Xa7nm3f7pzC10uqJslKFAzVAUNNp2lkyAElLeXXoLTuiKObq2rwp5QKylleda/H600HHME2wAN5iHxgfzB4QMgE2pW0aPUYIxa/ZWcA== 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=sQbyuPzaOb+gGVc90x/XMaLFc7sXGq7Y/GylR/tiuKQ=; b=SZ1fMWAKh8MSElpF4nZOf0aUZXby/B8TftVqawT40zBqyKXGiVq7Jc2Io/d60jDhpBkdKjGbVUaYQkCqLl3RIdDTZxjRrLSx2jt6NNxzhEYZbDudN9LdgC3YYze7M25MFLZkjcqCG8Bf4PeaKwJ6ZfhCNYr/OT42wbHpRN2NO/M= Received: from BN8PR12CA0016.namprd12.prod.outlook.com (2603:10b6:408:60::29) by MW6PR12MB9019.namprd12.prod.outlook.com (2603:10b6:303:23f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35; Wed, 1 May 2024 09:05:51 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:408:60:cafe::a6) by BN8PR12CA0016.outlook.office365.com (2603:10b6:408:60::29) 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:05:50 +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 BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) 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:05:50 +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:05:49 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy Subject: [PATCH v15 18/20] KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event Date: Wed, 1 May 2024 03:52:08 -0500 Message-ID: <20240501085210.2213060-19-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF000044A3:EE_|MW6PR12MB9019:EE_ X-MS-Office365-Filtering-Correlation-Id: 63416b37-e41f-4ce1-001d-08dc69bde5ec X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|7416005|1800799015|82310400014|36860700004|376005; X-Microsoft-Antispam-Message-Info: aAZEMNnskWWxV4GkMLp/qWPUuSVQ5DYSGpHj/GDIRiw4KuIUZogB2uB0OLdboEpX/qGOEQ7qGcPHIOAzuFLpkgqDsRaImvkM9Rc9X1XKzfwmVkCSE3z2/snuarzu58ynAKFG8j6jlTjSP0rZjs5bNQ8QUeSMS+5cbSTIBMvIqZf8yHChg2DvPWWwbQYRIAyC+gUMxTWXAPNEayb7b5wRO5Mvg1Y/RWYSEfL88KoUYiZvbNW8cHY1nXIrHLawvrNNXEOLCb22MdTHDi6WnTB76AEMPWwzl6FeFEsXzZFQ9/HgsfyIc1nZ8HSoZGLNpXm3VtKvhMEN4NQs2G5dsJAoN3XqfvQGINPPsUXir4Tw6P6JhD1WZbaRPXTvVGBs9q92MA5FVgV7h5c/nTGI84tiF4a4kMVSu9RpgIYgaP/CERPadN2eDXoiPiUDZTHVwPHrH2Ci7PJ8O/bJbmAs8is7ss+XWOUu8ED3PGNY9atx9sB9u06MmUFVxJZdqeHfL9LGxLiP1NpOaYZNhENK4CiSuIWfJaEB6YI9djfyqNf7WmxYQRNvaP5SmkfcaNsSZ2igcF0s0rmU0uPv5xLZv2UkmRXtQsgzigV4P51fHY4azGHBG2TRHQzQEF5A2/rQWRtYOMLnYJXE1rAC44hKbDt8kJ04ixvwwCt+7k1dZ464DjndFvTwriA8Clri7VcQ25IGW1IonD0ZXI+W8Alfv6ccLb+9pTRYshC0Qk1Z81WYIBc3co+7zi1+Zxb37eUuqnY1u2EQCyLBrHqrfIsWfJiTqRlsUh+box+XIj2ugPgGDY6f05BVL/VZCC3aH0K6VMs1nNV/2b/ipti1Q21ZjDGlfssry3crtfaKePdOiDSVRegEtZsc7ZAaLZe1QtW+4N9X3LW5sJOz+Vwb6/AfKM1D5uOoZap5wK9X877e7ull0+5QC2OfemNtbkJXsSlKlhBCk3A4cp6kGWMWQSRuJtj1FzmUFVtC08tQC08Qn4Rx8vLEPD9/MGmPe9ZU9MpumVItJIerXkGiC5gNixVf5m7NUN3BCyp9NJozMjiX1ftacDVq/opPkdaCyOKY5X6jfEnA9g+VmFvwsJHopH/2HfaqJ2OASIAAqzoy1zHK/FmHKfuQZtPj+3c7pE6aSuRg5ZyTsmKpioy48NeMMlBiAkVmI6mCnBTZl+NRMh9sPQhcMPeuh1pbz6N+AVaywFzTJyyltLsaMzflDrjLYdMtzj0oHl70upZEASr3n4GGKYbkvQb+p3D8QpLI/v+qx7/R8b866kR59WpjtlP4QR2V5EDAmZjWQbnBPzlj0VQy20g6ZvW9Sr1DFZkuKJOAh4njI90HJ4xEv/ESH+CJxDYtzlmmwCY3olX86G7EOSZzF/tJm6LGNG3AfHuYw4py1adtZqYL 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)(1800799015)(82310400014)(36860700004)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:05:50.6327 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 63416b37-e41f-4ce1-001d-08dc69bde5ec 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: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB9019 From: Brijesh Singh Version 2 of GHCB specification added support for the SNP Guest Request Message NAE event. The event allows for an SEV-SNP guest to make requests to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API defined in the SEV-SNP firmware specification. This is used by guests primarily to request attestation reports from firmware. There are other request types are available as well, but the specifics of what guest requests are being made are opaque to the hypervisor, which only serves as a proxy for the guest requests and firmware responses. Implement handling for these events. Signed-off-by: Brijesh Singh Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Reviewed-by: Tom Lendacky [mdr: ensure FW command failures are indicated to guest, drop extended request handling to be re-written as separate patch, massage commit] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 86 ++++++++++++++++++++++++++++++++++ include/uapi/linux/sev-guest.h | 9 ++++ 2 files changed, 95 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 112041ee55e9..5c6262f3232f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -3292,6 +3293,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_sw_scratch_is_valid(svm)) goto vmgexit_err; break; + case SVM_VMGEXIT_GUEST_REQUEST: + if (!sev_snp_guest(vcpu->kvm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -3906,6 +3911,83 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm) return ret; } +static int snp_setup_guest_buf(struct kvm *kvm, struct sev_data_snp_guest_request *data, + gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + kvm_pfn_t req_pfn, resp_pfn; + + if (!PAGE_ALIGNED(req_gpa) || !PAGE_ALIGNED(resp_gpa)) + return -EINVAL; + + req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa)); + if (is_error_noslot_pfn(req_pfn)) + return -EINVAL; + + resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa)); + if (is_error_noslot_pfn(resp_pfn)) + return -EINVAL; + + if (rmp_make_private(resp_pfn, 0, PG_LEVEL_4K, 0, true)) + return -EINVAL; + + data->gctx_paddr = __psp_pa(sev->snp_context); + data->req_paddr = __sme_set(req_pfn << PAGE_SHIFT); + data->res_paddr = __sme_set(resp_pfn << PAGE_SHIFT); + + return 0; +} + +static int snp_cleanup_guest_buf(struct sev_data_snp_guest_request *data) +{ + u64 pfn = __sme_clr(data->res_paddr) >> PAGE_SHIFT; + + if (snp_page_reclaim(pfn) || rmp_make_shared(pfn, PG_LEVEL_4K)) + return -EINVAL; + + return 0; +} + +static int __snp_handle_guest_req(struct kvm *kvm, gpa_t req_gpa, gpa_t resp_gpa, + sev_ret_code *fw_err) +{ + struct sev_data_snp_guest_request data = {0}; + struct kvm_sev_info *sev; + int ret; + + if (!sev_snp_guest(kvm)) + return -EINVAL; + + sev = &to_kvm_svm(kvm)->sev_info; + + ret = snp_setup_guest_buf(kvm, &data, req_gpa, resp_gpa); + if (ret) + return ret; + + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, fw_err); + if (ret) + return ret; + + ret = snp_cleanup_guest_buf(&data); + if (ret) + return ret; + + return 0; +} + +static void snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + sev_ret_code fw_err = 0; + int vmm_ret = 0; + + if (__snp_handle_guest_req(kvm, req_gpa, resp_gpa, &fw_err)) + vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -4178,6 +4260,10 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, GHCB_ERR_INVALID_INPUT); } + ret = 1; + break; + case SVM_VMGEXIT_GUEST_REQUEST: + snp_handle_guest_req(svm, control->exit_info_1, control->exit_info_2); ret = 1; break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index 154a87a1eca9..7bd78e258569 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -89,8 +89,17 @@ struct snp_ext_report_req { #define SNP_GUEST_FW_ERR_MASK GENMASK_ULL(31, 0) #define SNP_GUEST_VMM_ERR_SHIFT 32 #define SNP_GUEST_VMM_ERR(x) (((u64)x) << SNP_GUEST_VMM_ERR_SHIFT) +#define SNP_GUEST_FW_ERR(x) ((x) & SNP_GUEST_FW_ERR_MASK) +#define SNP_GUEST_ERR(vmm_err, fw_err) (SNP_GUEST_VMM_ERR(vmm_err) | \ + SNP_GUEST_FW_ERR(fw_err)) +/* + * The GHCB spec only formally defines INVALID_LEN/BUSY VMM errors, but define + * a GENERIC error code such that it won't ever conflict with GHCB-defined + * errors if any get added in the future. + */ #define SNP_GUEST_VMM_ERR_INVALID_LEN 1 #define SNP_GUEST_VMM_ERR_BUSY 2 +#define SNP_GUEST_VMM_ERR_GENERIC BIT(31) #endif /* __UAPI_LINUX_SEV_GUEST_H_ */ From patchwork Wed May 1 08:52:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650353 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2066.outbound.protection.outlook.com [40.107.223.66]) (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 0A7CF1DFDE; Wed, 1 May 2024 09:06:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.66 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554378; cv=fail; b=ITtMrfGZaKFRNpGwllPdLvcIck/ztLLZDwNhcJtNOqWUAMTJJXhSSOL6EXRtwSQ+OOneP+UxmQwNrRp7uNPEoKt+p7gPmE5mhBx9r+Ccky7VRaNgiYs+uVIRCOivDK1KiUBP9YG54YwGtZAr40Bvo5+y1LB6k8TMpzG81VD2ZEw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554378; c=relaxed/simple; bh=gmL4udGgV2x6xxYceh5KDXcZ8ilHLlyEcPMu/isz0Lo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CBS4Oi8r/Q5V04NLRiglWHoFJTqrFrmGkLcrISGkOTSPNzNMgRuqWsM3PFqftG4VFyxk3wFDSMQW1quMFtMGd3bLSGscVhjLpujhGmhp5XPEqIOoUjxOvbAvI8DGhlxn7wC5Ws7isqfZ4LaeUf+RWv8x9n9qsbsDIYBIcyvBDgg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=hhdFfRMp; arc=fail smtp.client-ip=40.107.223.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="hhdFfRMp" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=euz/LBgcQHSnCYvWGLTaXNiVQAPteE2Zn+NdyIeiXEGH0y3Ocg7tYlguLjhKiWoLTQOaO50CrvcRvIiOgc7S2O+sqJTF5OIT1epkZQU/qBatD4Gb+KElmPDo1I475UPa2RZz0fvHLc4m3H/QHhI6xAjYmW5hICovs0jdUXjxcEqw98FhjMg5NNgvmREKiQdtO0kUwCdb+1lkSESBbbA6ZIJHi5ZCFEuZt8bu1sBMV6UFA8NlPYDob9U9bz5KrDj3wfLbumJ8f+eWXzLrCP/By0U+54oRiXocfC/TRBzK+eFn9ZfUlhAfBdpoYZfdFa82qxGy5kmZkrZyqwBP0VD+2A== 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=bpKnivgAVH2H/wCIQAMeWVTFrkr7yKIT0Qi9SSKf8jk=; b=au2DfYocEIGFA83z2eMYqy6TknYOXMwKGa5rcGrMTX+6JjBDWlk491bDr8CM/fdATMa/r0uVlLdvZpENKA+5gtl2LqyqBswiPgR/Ahvcji/lvy0fIMJp1YFSPM6XZwunAbuvx9OC45v/DqQ1KciYUfQZz3TPrj2U2nMDymDiUi5JFGlCgtWxA2BDPwbK4/WMKW+c0mWKmENNmEnE8sIqv7h79ziRKsZ1jN9fLAaGMv0Z5jDfYY7etbs8d6r+RNZMRA/hVL25W7rwz4o8ekSrh4fBVbeIsjKpfSV27aCkdr0nN0fpKuIEplmdCpj8A+St4GVvHmGTB/cimjrD4P+URA== 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=bpKnivgAVH2H/wCIQAMeWVTFrkr7yKIT0Qi9SSKf8jk=; b=hhdFfRMp3SwneAGvgueb/kBipUwfChQDdukYjL2PPWFqhxoTpQDrSlLW0ABiif0Fbt0xCOTk04eOBDC8mak5bohF9yNkE0UsNzokZSIAjwNbfNXK9nV/hGaeGggE4Cov8xP0cY7IKo7elTs4P9J5I4RcgJlT4tFywr+dN23c7mM= Received: from BN9PR03CA0523.namprd03.prod.outlook.com (2603:10b6:408:131::18) by SN7PR12MB7154.namprd12.prod.outlook.com (2603:10b6:806:2a5::16) 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:06:12 +0000 Received: from BN2PEPF0000449F.namprd02.prod.outlook.com (2603:10b6:408:131:cafe::f7) by BN9PR03CA0523.outlook.office365.com (2603:10b6:408:131::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.36 via Frontend Transport; Wed, 1 May 2024 09:06:12 +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 BN2PEPF0000449F.mail.protection.outlook.com (10.167.243.150) 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:06:12 +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:06:10 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 19/20] KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event Date: Wed, 1 May 2024 03:52:09 -0500 Message-ID: <20240501085210.2213060-20-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF0000449F:EE_|SN7PR12MB7154:EE_ X-MS-Office365-Filtering-Correlation-Id: 445e63d8-c90d-4659-70d0-08dc69bdf2be X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|36860700004|1800799015|82310400014|376005|7416005; X-Microsoft-Antispam-Message-Info: XHWBHG4RyxSyohYa7PC8PGj6bbR4/2e4nSNfl4gjBCRIpyb1FtiT2u2yYuroVM5KQm5pKQn7N4+R6xswO4oU6guC5pwebB4JDID0VlVlB/msUjKK8BW4AZj/iX1w9t0773870SSDHpOw/IWMgq0WlJ40TPZpH9ThQUBtJtA9Qgge/7QyM+SWdcEkqXHCMUluHhp63dYdf14vzKVe+77X4dZYKFbk5LRhPc2PmbKknyGre4AqEaJtfXiKvQVUzngcK3HQOmMPWHcPcUHIpU4gYOjkfDUTcb/lMc4ZXiJjCQF7zshr/puC6cOCBV3E87lfq1qhG8hLqcAScUA85JnGEc98bNGWydTmblgDcKOXHe2X9oc0S+88X0vB0D/rir+rKVO11HEL0c3NqJ07yL4QQX9HkjUSmMv8BkbDcfi4ETdHSc9rdufOu9ctYj+fGZuoTJYRNzciu1iRgCm9XumFdwpOZ3PCVLfHewzh9Ptwb/duzqo0OnwVXgDjy+NXIpHk9j3X0/IR4ss1JtZgWA3MBr9xqZ/00piCiSAFS22hecrO99t67JrU3CxFaVleaUhtdFr0OAZmxnMO1RCPaXtdB22kfdM1pjVMrq6/XsmFVEiPJNKSIzhyfF/6r8FcHFutawlBuGeqP/3NId4dQyi+aiQobzNTNzvww62Q5ktAR1bjh4KbNVfeqzVwaSs//YLbz3ypf5duT6KjDDeWFSCfu9QeDkTaTOjcB9TPaoNRkaXBmI5S/g5icspAA90VZcPZIDibMtmfJzU6b0akTMs+xuidicz3Yb0Dy5UlGTaUVStCaFMTRsy+n3bDTAnn09JrNFnw9LTjX62r0YMhX47m0Ix99go5Fgu+5C+4lZiAkeORzZUQs1LmJEuD0ErSxGL8pvLsSffdSh+Y1g6xTXhAeqS21d6AReJVATVe6TAoWCOJfppIT+sYsnnYxHd5SNvE5yokwJjewzcWFM7s/N+lSNO1EKO3MZuNRV2nsdG8s6nmi9ArMtVRmLvc9no3nSWwBgMnxukCvqmNEpHgyVvDnB3dTsN6Em6j6kilMwRRlJHnVkQDBiwsFZr5Wj36kPxXwQzeXp1lDFy9o9Ag7A2HCBXlH3ocmwgDTPNGynjJPEIxtPQRPcBWGxNaIiCHafz2Yf3sG28qq8IPN5WhNHwfEj9iEnv57wS8t290RgDuQuYa7dIuhQjS12WTNl93zxEDbeMIdueDRO5PtpPziLhng+Hqi4se4/WtOXsq9dMYJEeW8qX9ZQ81OfFHflh33APt87a7sWyoq1b3gou5GG+e7HkxOku7Cp/zHHekNbDHRVpKAtOom5vGx8gBowqQKOj3OGOijDETWRW9YJN0xZOnEYyXCxeWGedRPbJB6zMPfrJjaBrOSPvnKbI7P4QyeWls 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)(1800799015)(82310400014)(376005)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:06:12.1445 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 445e63d8-c90d-4659-70d0-08dc69bdf2be 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: BN2PEPF0000449F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7154 Version 2 of GHCB specification added support for the SNP Extended Guest Request Message NAE event. This event serves a nearly identical purpose to the previously-added SNP_GUEST_REQUEST event, but allows for additional certificate data to be supplied via an additional guest-supplied buffer to be used mainly for verifying the signature of an attestation report as returned by firmware. This certificate data is supplied by userspace, so unlike with SNP_GUEST_REQUEST events, SNP_EXTENDED_GUEST_REQUEST events are first forwarded to userspace via a KVM_EXIT_VMGEXIT exit structure, and then the firmware request is made after the certificate data has been fetched from userspace. Since there is a potential for race conditions where the userspace-supplied certificate data may be out-of-sync relative to the reported TCB or VLEK that firmware will use when signing attestation reports, a hook is also provided so that userspace can be informed once the attestation request is actually completed. See the updates to Documentation/ for more details on these aspects. Signed-off-by: Michael Roth --- Documentation/virt/kvm/api.rst | 87 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/sev.c | 86 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++ include/uapi/linux/kvm.h | 23 +++++++++ 4 files changed, 199 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index f0b76ff5030d..f3780ac98d56 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7060,6 +7060,93 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* KVM_EXIT_VMGEXIT */ + struct kvm_user_vmgexit { + #define KVM_USER_VMGEXIT_REQ_CERTS 1 + __u32 type; /* KVM_USER_VMGEXIT_* type */ + union { + struct { + __u64 data_gpa; + __u64 data_npages; + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_INVALID_LEN 1 + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_BUSY 2 + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_GENERIC (1 << 31) + __u32 ret; + #define KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE BIT(0) + __u8 flags; + #define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING 0 + #define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE 1 + __u8 status; + } req_certs; + }; + }; + + +If exit reason is KVM_EXIT_VMGEXIT then it indicates that an SEV-SNP guest +has issued a VMGEXIT instruction (as documented by the AMD Architecture +Programmer's Manual (APM)) to the hypervisor that needs to be serviced by +userspace. These are generally handled by the host kernel, but in some +cases some aspects of handling a VMGEXIT are done in userspace. + +A kvm_user_vmgexit structure is defined to encapsulate the data to be +sent to or returned by userspace. The type field defines the specific type +of exit that needs to be serviced, and that type is used as a discriminator +to determine which union type should be used for input/output. + +KVM_USER_VMGEXIT_REQ_CERTS +-------------------------- + +When an SEV-SNP issues a guest request for an attestation report, it has the +option of issuing it in the form an *extended* guest request when a +certificate blob is returned alongside the attestation report so the guest +can validate the endorsement key used by SNP firmware to sign the report. +These certificates are managed by userspace and are requested via +KVM_EXIT_VMGEXITs using the KVM_USER_VMGEXIT_REQ_CERTS type. + +For the KVM_USER_VMGEXIT_REQ_CERTS type, the req_certs union type +is used. The kernel will supply in 'data_gpa' the value the guest supplies +via the RAX field of the GHCB when issuing extended guest requests. +'data_npages' will similarly contain the value the guest supplies in RBX +denoting the number of shared pages available to write the certificate +data into. + + - If the supplied number of pages is sufficient, userspace should write + the certificate data blob (in the format defined by the GHCB spec) in + the address indicated by 'data_gpa' and set 'ret' to 0. + + - If the number of pages supplied is not sufficient, userspace must write + the required number of pages in 'data_npages' and then set 'ret' to 1. + + - If userspace is temporarily unable to handle the request, 'ret' should + be set to 2 to inform the guest to retry later. + + - If some other error occurred, userspace should set 'ret' to a non-zero + value that is distinct from the specific return values mentioned above. + +Generally some care needs be taken to keep the returned certificate data in +sync with the actual endorsement key in use by firmware at the time the +attestation request is sent to SNP firmware. The recommended scheme to do +this is for the VMM to obtain a shared or exclusive lock on the path the +certificate blob file resides at before reading it and returning it to KVM, +and that it continues to hold the lock until the attestation request is +actually sent to firmware. To facilitate this, the VMM can set the +KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE flag before returning the +certificate blob, in which case another KVM_EXIT_VMGEXIT of type +KVM_USER_VMGEXIT_REQ_CERTS will be sent to userspace with +KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE being set in the status field to +indicate the request is fully-completed and that any associated locks can be +released. + +Tools/libraries that perform updates to SNP firmware TCB values or endorsement +keys (e.g. firmware interfaces such as SNP_COMMIT, SNP_SET_CONFIG, or +SNP_VLEK_LOAD, see Documentation/virt/coco/sev-guest.rst for more details) in +such a way that the certificate blob needs to be updated, should similarly +take an exclusive lock on the certificate blob for the duration of any updates +to firmware or the certificate blob contents to ensure that VMMs using the +above scheme will not return certificate blob data that is out of sync with +firmware. 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5c6262f3232f..35f0bd91f92e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3297,6 +3297,11 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!sev_snp_guest(vcpu->kvm)) goto vmgexit_err; break; + case SVM_VMGEXIT_EXT_GUEST_REQUEST: + if (!sev_snp_guest(vcpu->kvm) || !kvm_ghcb_rax_is_valid(svm) || + !kvm_ghcb_rbx_is_valid(svm)) + goto vmgexit_err; + break; default: reason = GHCB_ERR_INVALID_EVENT; goto vmgexit_err; @@ -3988,6 +3993,84 @@ static void snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); } +static int snp_complete_ext_guest_req(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct vmcb_control_area *control; + struct kvm *kvm = vcpu->kvm; + sev_ret_code fw_err = 0; + int vmm_ret; + + vmm_ret = vcpu->run->vmgexit.req_certs.ret; + if (vmm_ret) { + if (vmm_ret == SNP_GUEST_VMM_ERR_INVALID_LEN) + vcpu->arch.regs[VCPU_REGS_RBX] = + vcpu->run->vmgexit.req_certs.data_npages; + goto out; + } + + /* + * The request was completed on the previous completion callback and + * this completion is only for the STATUS_DONE userspace notification. + */ + if (vcpu->run->vmgexit.req_certs.status == KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE) + goto out_resume; + + control = &svm->vmcb->control; + + if (__snp_handle_guest_req(kvm, control->exit_info_1, + control->exit_info_2, &fw_err)) + vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + +out: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); + + if (vcpu->run->vmgexit.req_certs.flags & KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE) { + vcpu->run->vmgexit.req_certs.status = KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE; + vcpu->run->vmgexit.req_certs.flags = 0; + return 0; /* notify userspace of completion */ + } + +out_resume: + return 1; /* resume guest */ +} + +static int snp_begin_ext_guest_req(struct kvm_vcpu *vcpu) +{ + int vmm_ret = SNP_GUEST_VMM_ERR_GENERIC; + struct vcpu_svm *svm = to_svm(vcpu); + unsigned long data_npages; + sev_ret_code fw_err; + gpa_t data_gpa; + + if (!sev_snp_guest(vcpu->kvm)) + goto abort_request; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) + goto abort_request; + + /* + * Grab the certificates from userspace so that can be bundled with + * attestation/guest requests. + */ + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.type = KVM_USER_VMGEXIT_REQ_CERTS; + vcpu->run->vmgexit.req_certs.data_gpa = data_gpa; + vcpu->run->vmgexit.req_certs.data_npages = data_npages; + vcpu->run->vmgexit.req_certs.flags = 0; + vcpu->run->vmgexit.req_certs.status = KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING; + vcpu->arch.complete_userspace_io = snp_complete_ext_guest_req; + + return 0; /* forward request to userspace */ + +abort_request: + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, SNP_GUEST_ERR(vmm_ret, fw_err)); + return 1; /* resume guest */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -4266,6 +4349,9 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) snp_handle_guest_req(svm, control->exit_info_1, control->exit_info_2); ret = 1; break; + case SVM_VMGEXIT_EXT_GUEST_REQUEST: + ret = snp_begin_ext_guest_req(vcpu); + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e325ede0f463..83c562b4712a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -309,6 +309,9 @@ struct vcpu_svm { /* Guest GIF value, used when vGIF is not enabled */ bool guest_gif; + + /* Transaction ID associated with SNP config updates */ + u64 snp_transaction_id; }; struct svm_cpu_data { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 2190adbe3002..106367d87189 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -135,6 +135,26 @@ struct kvm_xen_exit { } u; }; +struct kvm_user_vmgexit { +#define KVM_USER_VMGEXIT_REQ_CERTS 1 + __u32 type; /* KVM_USER_VMGEXIT_* type */ + union { + struct { + __u64 data_gpa; + __u64 data_npages; +#define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_INVALID_LEN 1 +#define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_BUSY 2 +#define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_GENERIC (1 << 31) + __u32 ret; +#define KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE BIT(0) + __u8 flags; +#define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING 0 +#define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE 1 + __u8 status; + } req_certs; + }; +}; + #define KVM_S390_GET_SKEYS_NONE 1 #define KVM_S390_SKEYS_MAX 1048576 @@ -178,6 +198,7 @@ struct kvm_xen_exit { #define KVM_EXIT_NOTIFY 37 #define KVM_EXIT_LOONGARCH_IOCSR 38 #define KVM_EXIT_MEMORY_FAULT 39 +#define KVM_EXIT_VMGEXIT 40 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -433,6 +454,8 @@ struct kvm_run { __u64 gpa; __u64 size; } memory_fault; + /* KVM_EXIT_VMGEXIT */ + struct kvm_user_vmgexit vmgexit; /* Fix the size of the union. */ char padding[256]; }; From patchwork Wed May 1 08:52:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13650354 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2052.outbound.protection.outlook.com [40.107.94.52]) (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 AB94A1DFDE; Wed, 1 May 2024 09:06:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554399; cv=fail; b=Vzd6MBvs5pQ2TtVQ1Ge1mL2IwQ+sD/wpF8Xt5Am6ETXkQgPZBI7PLMrBRqVsLtWAVvuFOGDia0f+11RZ5CMURKN1LtqA+8Fyp1s79gDDBkfAq3Sv4qBvcgQ3GzTd2aC/mVsRmk/U9H++JaQ3j8zSAMHb/mmbocLXNw0uGGX9j7c= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714554399; c=relaxed/simple; bh=T6pGJVRHlHSyXnXGrojJ2riNnafIVnvK+50POJ+c5gE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cRReEE0Ngtp+b03RML76AObeTy1CSi5k1P3ITPQSgBcbLZKtJpVZIygtDRbYZpKt1jZn0leSDlO0YTleUaQTWKF4gpDAG4rU56XukQZQtLYqVxgDIOXqcidfMTgrlVT/8WJY066BygaszX3aRXdLHdolwqVmLkO+gWDj7JlOvoQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=ZWfrqQot; arc=fail smtp.client-ip=40.107.94.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="ZWfrqQot" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BoWItKzLSyXRbwSn9BAiAEdWY8VnHj8eR8uQo7ooP8lSWu8mGSi5ELVCvem00+n70P4yZgP00crHTmzmSZrjAtpXbZbR6X2ch0iX20pMQ+q6PxNeaO74CMy0kwwuvsqC+Jq1OfrjiL+BZMjXrzLik73l1PNf3Eirud7QpmNRbzM9dAmodpfW9b8ji3/ZgFDuhKGKwRGE3+Gqox4qu3axqepEUdKw7Pfycd0nbI/dCIa0lqCMHyrE/gOv73IxYmfzy/P31Hglat81mG3aHDmwjNk8EOJ5dcHSuSlbcrsgEDkxWe7xT+J1tQyzqtvbDimpz/7sPwDgyl9TtX5CGIajcg== 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=BrLx/Gtif4PESh/Cb7f+ZeKvu4HjLI75t/BpaHSyZ/E=; b=QKvhXVUuWykmPTBrORprW6uUWNpuOHQrXqxHq3FshKWIMvaqHDihwREPAqM+HQVAjDdc7hQqqZek8/+t7HKiHvdv1dI/N9z2vlM8HRmOqyGrjbxn/rtojh/2GgP9ECSx7chGtVedvJ7xvgDcA5ro3A2qY/C/XdjwnL0zpfpscFSYY3398k+GmKbPkPOZjaDE2Khf93ATNkprMWGE7M0K5PQy5ISONCbTB0jM4yV3TxU6nC3C3YjO0ScgDgMO4WkzYtZXkSWD5VGWZahuNXUK/aCT4fRWYSYt7ufPeESc3TAGrYMn159bcsvJwmDOlVwP034uQ/GBxLuMRO6rScF7ZA== 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=BrLx/Gtif4PESh/Cb7f+ZeKvu4HjLI75t/BpaHSyZ/E=; b=ZWfrqQotR8K7QKALU4IPm20pAJNXmSBYY+PZZJrAFA5JgG1ISmG+ms/QxeyxI5617sV2Jw4z13bah2LldhmdlgGyVdmVlhTfBY3zhvQZRALCIoPQtni2KAZGXbwcBuiq8JJyhYuQgNoPFo+xaVrySNW2XTPqGSn6yXXq+IfkeEo= Received: from BN9PR03CA0663.namprd03.prod.outlook.com (2603:10b6:408:10e::8) by PH7PR12MB6419.namprd12.prod.outlook.com (2603:10b6:510:1fd::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35; Wed, 1 May 2024 09:06:32 +0000 Received: from BN2PEPF0000449D.namprd02.prod.outlook.com (2603:10b6:408:10e:cafe::e8) by BN9PR03CA0663.outlook.office365.com (2603:10b6:408:10e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.27 via Frontend Transport; Wed, 1 May 2024 09:06:32 +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 BN2PEPF0000449D.mail.protection.outlook.com (10.167.243.148) 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:06:32 +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:06:31 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 20/20] crypto: ccp: Add the SNP_VLEK_LOAD command Date: Wed, 1 May 2024 03:52:10 -0500 Message-ID: <20240501085210.2213060-21-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: BN2PEPF0000449D:EE_|PH7PR12MB6419:EE_ X-MS-Office365-Filtering-Correlation-Id: 27418ea7-e4c0-4af4-7100-08dc69bdfee3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400014|376005|1800799015|7416005|36860700004; X-Microsoft-Antispam-Message-Info: AhLvACFJpGWAOMl2/I3fX8Pl48bF2v3L7Vb2lGg+0Qex0iMDTRgWOjPaXmiLZXRtBlKx5NVYpYaDpWpfjt2AuggQoUWlijxjoih5R6YDZmpcf5DOXmF6qhuOhDm1IQsm3QagDUbPUoxInX3ubvaqT972vWj5rZmmsSZQtTph0w30m8slbI4wekK9YSsVqH/MhWnbBWwA2ZZJMB0XxeKV17gjegh0IvLVdequM5Mv6W0fk2PsKe+FBGZlvA0uLheg1PYwDH41pSkA0oaQdN/cdH/+87cDFyxBiDQ0vYxRy6PIQRs9Z2Cwv5e9qHUOCRFnDQkbpPOxhVbekhzQKDhaCbS49fOerh6j+2sOdDIFy1crpPL4f1WP9TolA0P06B4FwftD1A1LdudJItYqMG4jph6CWsGf5mOb5T1fCQzcwpJK3HiQ1I2alCL/6uvVKLuWIdoNfrQ4aYXY+bQfKzGs/NCuAiSJ82tPjJrFHtoRRjJ4kUhfX8dh3Y5NqUElALhdxpaiWAsaNfZgePWFB1eWIMuwveiaNAppf2XRozjw4KsoIcW4S7LV6oEgiEibiyIeGVI3gGelusEs1dxY1+n9xWxHSgLcalOcaXgLigXhCiUjeZJQHEqsYKJHF5ENYLz4tUkikoqjUTYuhNlO9JcuFyrNBMOLKgGEICuQI76LzOXPggG3ffWPC4vltEhJZtanbn8BTmdLspsYY9A3Ei7p6R7loKbESbJW0a2LqbC7pynhlXTJmqUPk/o8ARgXIpFe4kqy+7f1GgCVxu/CD9E4+bkVRb9Lf2/5kwDkAEnHyLy+ilLJi4++QiDVKKJ53ffOxd9/97aPdfj9S3eNabvckgQEyMIEOAC/vwHDiuD2pEoGxU67coxi/CvZAu8QDVBzbLsJAa0SRjchSoxaVAeZXdBddRDypEvIiM6YY6LeB5bBxGWwkLpyPom746LvSauDw7Y4/1t6k80khbwHyBn9mZtvlXWHpac9P3Z/8lulbAwK34stayryaNppqX8ym53+hUxi1jrL0tH5h6N+Nf9vUKqKjk825M7eyw5FakagEowBnrwgztAI6hMMiPUivtKynZe8v/wnLbqTa0XvaTm/dGtVUk+AyDS/0B2tIs9hkJDw4gZkyDYNMPLxf5GoptoA6vkJyOdrJR+mtqaqHak8rl1KC/oGRCEGN4uYivAqGpEIikTGfZ5GbNKdWFWwy9rzeFAkMQUr+3gJXWXmJZBeOXsTtt2SzJQwPVElNUswSMURI9gmUqKvmMFdAU0a8Swm316mUZg9dPlzgDYyrrR4jhuBGVLoQL+tqZiZhk56TbOQvzs6fGTZQAFmWQ2zyu7Y6akOkPEpdIxyL5jsEMO4Ozhu/g4u2kBRTD9ewg8F76Jeg5yzIwpyjrm+ySnD5jgy 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)(1800799015)(7416005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 May 2024 09:06:32.5196 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 27418ea7-e4c0-4af4-7100-08dc69bdfee3 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: BN2PEPF0000449D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6419 When requesting an attestation report a guest is able to specify whether it wants SNP firmware to sign the report using either a Versioned Chip Endorsement Key (VCEK), which is derived from chip-unique secrets, or a Versioned Loaded Endorsement Key (VLEK) which is obtained from an AMD Key Derivation Service (KDS) and derived from seeds allocated to enrolled cloud service providers (CSPs). For VLEK keys, an SNP_VLEK_LOAD SNP firmware command is used to load them into the system after obtaining them from the KDS. Add a corresponding userspace interface so to allow the loading of VLEK keys into the system. See SEV-SNP Firmware ABI 1.54, SNP_VLEK_LOAD for more details. Reviewed-by: Tom Lendacky Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 19 ++++++++++++++ drivers/crypto/ccp/sev-dev.c | 36 +++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 27 ++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index e1eaf6a830ce..de68d3a4b540 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -176,6 +176,25 @@ to SNP_CONFIG command defined in the SEV-SNP spec. The current values of the firmware parameters affected by this command can be queried via SNP_PLATFORM_STATUS. +2.7 SNP_VLEK_LOAD +----------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_user_data_snp_vlek_load +:Returns (out): 0 on success, -negative on error + +When requesting an attestation report a guest is able to specify whether +it wants SNP firmware to sign the report using either a Versioned Chip +Endorsement Key (VCEK), which is derived from chip-unique secrets, or a +Versioned Loaded Endorsement Key (VLEK) which is obtained from an AMD +Key Derivation Service (KDS) and derived from seeds allocated to +enrolled cloud service providers. + +In the case of VLEK keys, the SNP_VLEK_LOAD SNP command is used to load +them into the system after obtaining them from the KDS, and corresponds +closely to the SNP_VLEK_LOAD firmware command specified in the SEV-SNP +spec. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 2102377f727b..97a7959406ee 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2027,6 +2027,39 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); } +static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_snp_vlek_load input; + void *blob; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + if (!writable) + return -EPERM; + + if (copy_from_user(&input, u64_to_user_ptr(argp->data), sizeof(input))) + return -EFAULT; + + if (input.len != sizeof(input) || input.vlek_wrapped_version != 0) + return -EINVAL; + + blob = psp_copy_user_blob(input.vlek_wrapped_address, + sizeof(struct sev_user_data_snp_wrapped_vlek_hashstick)); + if (IS_ERR(blob)) + return PTR_ERR(blob); + + input.vlek_wrapped_address = __psp_pa(blob); + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error); + + kfree(blob); + + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -2087,6 +2120,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SNP_SET_CONFIG: ret = sev_ioctl_do_snp_set_config(&input, writable); break; + case SNP_VLEK_LOAD: + ret = sev_ioctl_do_snp_vlek_load(&input, writable); + break; default: ret = -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index b7a2c2ee35b7..2289b7c76c59 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -31,6 +31,7 @@ enum { SNP_PLATFORM_STATUS, SNP_COMMIT, SNP_SET_CONFIG, + SNP_VLEK_LOAD, SEV_MAX, }; @@ -214,6 +215,32 @@ struct sev_user_data_snp_config { __u8 rsvd1[52]; } __packed; +/** + * struct sev_data_snp_vlek_load - SNP_VLEK_LOAD structure + * + * @len: length of the command buffer read by the PSP + * @vlek_wrapped_version: version of wrapped VLEK hashstick (Must be 0h) + * @rsvd: reserved + * @vlek_wrapped_address: address of a wrapped VLEK hashstick + * (struct sev_user_data_snp_wrapped_vlek_hashstick) + */ +struct sev_user_data_snp_vlek_load { + __u32 len; /* In */ + __u8 vlek_wrapped_version; /* In */ + __u8 rsvd[3]; /* In */ + __u64 vlek_wrapped_address; /* In */ +} __packed; + +/** + * struct sev_user_data_snp_vlek_wrapped_vlek_hashstick - Wrapped VLEK data + * + * @data: Opaque data provided by AMD KDS (as described in SEV-SNP Firmware ABI + * 1.54, SNP_VLEK_LOAD) + */ +struct sev_user_data_snp_wrapped_vlek_hashstick { + __u8 data[432]; /* In */ +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Fri May 10 01:58:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13660828 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) (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 B5DB1148FEB; Fri, 10 May 2024 02:36:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.75 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715308602; cv=fail; b=LUwTY/y7nwdBQNBDBgXvs+mjiQSTd9Db9JpGv1VQ72V9SNdIC22Q2p185xUVVYtYR4y3lnu2wFTNWJRHObfOur/RCAyrkXRPgdp4UJuXiTAHBJyGPwFKmrLyVruxGrgePFDcJbe1f+YjiCkVV2Tr+WHoz2jszcLIj5pkDgVO2/4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715308602; c=relaxed/simple; bh=55rOGSOSrcRZM3UtM+oxDWtePaOWhJq7An03Nsz/miU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rR5wgk/lwtQHGGusljRawIYa7P3HDzZ/QQIBsunh4S8o3bAXNFZ2gynEw35ESPgNaPTgQsbv71xa0hdlGRcO9OP8gwDrvS7SxLm1YWeSH1Uz2SFq5M83vZUJVyGsEqU/OqFCy3YeS4c1EWSCL8RdZuCJAmw6apMlcPMNY7F0tW8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=wpApFz8i; arc=fail smtp.client-ip=40.107.237.75 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="wpApFz8i" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jvUJ2uQLt5PVmBLnGtBqYs6HYo4SbN01xFxHcMlfRpMHmVlSyvlhwR5HJvfHnGLgu5PejxDGEgL6EUmdGCrn0cLF+MZbLr+HY95j3C5Xtobq9wxpC6V3eXgr23X/F7cykl2cPDfWYx5dX/RwpVrFM5Uqf+o6IpHFqbgfts1yiSSJjvtYoIjfPlW862o7AUT2mcFo7HDmpiVAZ5tp+rBgUbCnQNN/iDnP41u/wl2mK2gXDmCXL9ZLzhpkSmwlbXAZmLF+fCmhRz+ylDeXTa6dQ2T/tPFc9Eel0kMcolEmEyG6vog4BBEEu4HG8HD/69ME9WfyFwcqrr93MOoWwUJ4MQ== 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=zvSZolCBykKdQtTZaILW7FQXukMYbhMEQkcghT+tDNw=; b=gqnSKT629gQzl3bOWgWoA6NQ8Kk8nNcc9QaenPmK3em5Qx1v2aEVKQqveewzcI5k24jVcyJUjGopvYAHruM5iOm83fdHdF3uw1Yl1QczLUtNWu3vPNtu1zzkqJVX3dZNUIX3wcQL0jsnuIaAoVBl91s4fkJrpoEGryUY1JGhu2YBPVqTNO1B5R+RiVwJMPwD/VVD4yWFq/YFUq49QNQBKmiktYrQscrke1esN0OXkzNfcUcLTZGverbowNPQCafD4BKKdCQOAcUWbCo8Ssu3zr6e69M23CWRG7ZYh9WWVIpkWFqj7I7RBbgSoyQlmjNQJ1zCMgkBX/WhaxwD/DbbVQ== 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=zvSZolCBykKdQtTZaILW7FQXukMYbhMEQkcghT+tDNw=; b=wpApFz8iNOpST/ptOBC9jf9MLZ3qXtQowrhszTUfU7ORQRopZHPywvNrq3R34WRFMqHD2zyB52yMBWY176+a0qsF2hCFHJF4dg+HK056ZIy64gJKsNPNVNgaGQE3dod1V6YW8MExwjf8ZvOOwwQ77qkFubmLvupGTCTDT36ir9M= Received: from MN2PR11CA0023.namprd11.prod.outlook.com (2603:10b6:208:23b::28) by CH2PR12MB4149.namprd12.prod.outlook.com (2603:10b6:610:7c::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.48; Fri, 10 May 2024 02:36:32 +0000 Received: from MN1PEPF0000ECD7.namprd02.prod.outlook.com (2603:10b6:208:23b:cafe::bd) by MN2PR11CA0023.outlook.office365.com (2603:10b6:208:23b::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.49 via Frontend Transport; Fri, 10 May 2024 02:36:32 +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 MN1PEPF0000ECD7.mail.protection.outlook.com (10.167.242.136) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 02:36:32 +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, 9 May 2024 21:36:30 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "Isaku Yamahata" Subject: [PATCH v15 21/23] KVM: MMU: Disable fast path for private memslots Date: Thu, 9 May 2024 20:58:20 -0500 Message-ID: <20240510015822.503071-1-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> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: MN1PEPF0000ECD7:EE_|CH2PR12MB4149:EE_ X-MS-Office365-Filtering-Correlation-Id: 097b6ffb-7c53-4761-2923-08dc709a00d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|7416005|36860700004|376005|82310400017; X-Microsoft-Antispam-Message-Info: Jcc013T/TnDlncSxcKTAre8nd9XjAKVnDK5/eAD2XzDjCUJY3vEZP4Yb+39OQKek7B7rYKlQPfXOZAbBfWQieYUOIxNsxt77KDQsGQlyepMOZJzuPKvgycRn8bcQvDACipdY3J+AxyXxYFyNkUUQthz8ERUW54uh/pXNxW2cB1tl8zb82Li1TAuzQ6fdwF0sYFNiS03Z7cTpMNAZ+YTTYXJBYWIC1IamzR7r3/ElN0iHmK9HhpP0xay7f9WbfueybKuA10IzERtAGikLYjpVhMpq6fOAJc83Z+pJLmfGD8/+lFC1aZhndbzS4hVsaGAI1GHqRrLgQNG2FZ4PWsOvGb8BLRPx86yQ80F6w29I7dS5+MZ8+HpoXBjf4zcqSMtJfKQF8sDyt8YB7E5GT/uxTqkPgMfGRzJceLafzFUu827Oa7P8KQ4uYtD1+csWTKgupWB5jAV2gVjbwH90YLGlmlHmW1C99Mrrf81IKDLWtXFfwsWdFkV0YphLH6Tlqqf7Q4AkPJfTPdGhkJcgN7BjrIO4yOtd5PMjmjZTRqAXfnXWfST5r3oDCqRVg0rQktG2XxnL8Cl+pKMaoCYbw/rdvIXeq6AB9NQ29h2wndTl+RCY+FqGpcyTyFnub61ZhZlgri3uV8AaUrNEBlshvHsVZbKrZTG/KWY42rWtSrkttqwvoKNybqevOq117EbG2PkAnM/5mYChfuYTJI6a6psqv7PgtVVHfPPUslNfUGNpFHwqHZRiBEz1iUHuk0I/vDnNdlzh4TkJCLbteqA+q4D7JHQenHhMr1KYbR/PNhDp6igSpw/x7EpKFQgHU7sE3ao7vel7whjOUjY/ndHzg3NDDQKIShfGJ5sQwJX72nDf/jf9exh+Tyc2iGzHTVXgmbLjjdmGuapnyskwWqe+Mq6vycId2gLu3PPK4yBwaT1KYzuhBh4lIUk6GZtwnNGyNLUwRDvHIHCzn4hOKIpAH5Y5Qx4S2M/2y8tWdS0gfM7sPD5XYBTDHIy8lZzxmmVPt0p7vcIGCNlIknxkjLYWuVqRCiMYa3UJ/rhDmv9gwFuQwybUyJR9DPHTCGZf+zxtTsySV5sre8+DP6sypPjmueACiexuDLt+n7QO20XD1lF+LpdmZqTecHGyofULqIJUXeKe/lee+PnJVVoxcamdHIeJo9/vPRZEPaA6MVvaHm6FTj1aJ4TkBFEB7R80fFLmymevZFrQjmA6QjsqdVCABoFNPwkHwADQ6ZrYpcDF5R4DS0BwwPLO9J6bQfkshOmvtPD0LoPNz3y+q3yZIP7sjqMMrn0Mp7lm++fHhVnphP88rysocejpT+vfRdVzXL68uL/QlTy/tLkV67n6yPXL4iUrUncUgrVWfwgwEoGNolojh8t1cX3d+ZZYrQnn+Usn1mSW 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)(376005)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 02:36:32.0255 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 097b6ffb-7c53-4761-2923-08dc709a00d3 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: MN1PEPF0000ECD7.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4149 For hardware-protected VMs like SEV-SNP guests, certain conditions like attempting to perform a write to a page which is not in the state that the guest expects it to be in can result in a nested/extended #PF which can only be satisfied by the host performing an implicit page state change to transition the page into the expected shared/private state. This is generally handled by generating a KVM_EXIT_MEMORY_FAULT event that gets forwarded to userspace to handle via KVM_SET_MEMORY_ATTRIBUTES. However, the fast_page_fault() code might misconstrue this situation as being the result of a write-protected access, and treat it as a spurious case when it sees that writes are already allowed for the sPTE. This results in the KVM MMU trying to resume the guest rather than taking any action to satisfy the real source of the #PF such as generating a KVM_EXIT_MEMORY_FAULT, resulting in the guest spinning on nested #PFs. For now, just skip the fast path for hardware-protected VMs since they don't currently utilize any of this access-tracking machinery anyway. In the future, these considerations will need to be taken into account if there's any need/desire to re-enable the fast path for hardware-protected VMs. Since software-protected VMs don't have a notion of a shared vs. private that's separate from what KVM is tracking, the above KVM_EXIT_MEMORY_FAULT condition wouldn't occur, so avoid the special handling for that case for now. Cc: Isaku Yamahata Signed-off-by: Michael Roth Reviewed-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 62ad38b2a8c9..cecd8360378f 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3296,7 +3296,7 @@ static int kvm_handle_noslot_fault(struct kvm_vcpu *vcpu, return RET_PF_CONTINUE; } -static bool page_fault_can_be_fast(struct kvm_page_fault *fault) +static bool page_fault_can_be_fast(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { /* * Page faults with reserved bits set, i.e. faults on MMIO SPTEs, only @@ -3307,6 +3307,32 @@ static bool page_fault_can_be_fast(struct kvm_page_fault *fault) if (fault->rsvd) return false; + /* + * For hardware-protected VMs, certain conditions like attempting to + * perform a write to a page which is not in the state that the guest + * expects it to be in can result in a nested/extended #PF. In this + * case, the below code might misconstrue this situation as being the + * result of a write-protected access, and treat it as a spurious case + * rather than taking any action to satisfy the real source of the #PF + * such as generating a KVM_EXIT_MEMORY_FAULT. This can lead to the + * guest spinning on a #PF indefinitely. + * + * For now, just skip the fast path for hardware-protected VMs since + * they don't currently utilize any of this machinery anyway. In the + * future, these considerations will need to be taken into account if + * there's any need/desire to re-enable the fast path for + * hardware-protected VMs. + * + * Since software-protected VMs don't have a notion of a shared vs. + * private that's separate from what KVM is tracking, the above + * KVM_EXIT_MEMORY_FAULT condition wouldn't occur, so avoid the + * special handling for that case for now. + */ + if (kvm_slot_can_be_private(fault->slot) && + !(IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && + vcpu->kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM)) + return false; + /* * #PF can be fast if: * @@ -3407,7 +3433,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) u64 *sptep; uint retry_count = 0; - if (!page_fault_can_be_fast(fault)) + if (!page_fault_can_be_fast(vcpu, fault)) return ret; walk_shadow_page_lockless_begin(vcpu); From patchwork Fri May 10 01:58:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13660829 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2040.outbound.protection.outlook.com [40.107.94.40]) (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 1E97D13AA59; Fri, 10 May 2024 02:36:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.40 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715308616; cv=fail; b=imBTpJafssT/pU//3TnGPHxMGJyJor8Vd0zVuRJKS86ymX0c3TAM7GggQ2PQN/emlCTHqGj7Q+mDHFiJWLJfHQetDTzRLEeRjgp6ew61vfLfT/rKNVKw5TPXpNeA4zPnSLi4+aW5B9PO39n4Pu3vrTGhuiqaHCW5LVm2HiK0RNc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715308616; c=relaxed/simple; bh=qyC92NCTcb/drIn+9pUWVVSjmECl++xQMMQhQPc7LWI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aG2HC8UtSK7l3prZPaBmhhQ3CauRLvTs4+Gxp68KalrgIh3Rm8ukdMax++zgS5f2oI54kl/Cyh6ZN4nWoy82Wwn2YkEBBXUxjBuEBlCd++mSr+NwQaY4JmYo4A5OTNZVoYbs/HENIMcqjF71Al9kbgv2BqgqjL0tMX3sR4/2pdU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=ROoxQmT0; arc=fail smtp.client-ip=40.107.94.40 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="ROoxQmT0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T+UadS+/M2eRRByO2OUQVTfpMF5Fb/cE7te72mmU7fSSvid/CoBIbR8Q+CXSfc7/Iv39LFpE6qdmlulRU/LOQC7JqyMsnFeHRaNqxrgf63ZbWot1aIQ24CIjkFvulFUrk10ZbYpyiyHIE72vkw/uTYgtt6sedpZ+3WLYTYWf8qtedYkMyVHxh/GgRzRcX2iRhMW7xji2x5cAzNbHHoTjEz8qo7JxELjQTJHi2wndx36Qy75SJ7L94JaJ8e2oDxIrZghxdZwpg7k3Qje2o96c47HsUqF4zpYUKTlWUDiQ7HgkBLdISgO36NpkC8KUTLmp3f6WaiziFeor48lDJYxMFw== 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=BGhmcL2OHBdWCaoWb6sS59iIQkpm0AUCFdadplIl+MQ=; b=n3FouW/fLxN02CoFU7L8uW3fCgBY4nVYPMmdwSB3OvpaRsaS8qVrvem/R/4Pa7JtdaWimcaoL/5xIYlc9iDsuci+Kw67sKoUHuKm3SgsICEPJXpCyFnun1p/iYcntMvUuRl5vUSmNULFEBhvDdJp0G7RUhKYn802/Jm627SBXsI1HPw1G8x2dX2HmDxV29h7pvZoXSBbNqe/kygg9y0Ba+gw5X+maHeEqvM2FK6As9R0oVndzuOGsLKwgjrR8BlZGKZT9MzYfnkSNvRg6QdOOQx0DjKFN5lNpHYby436JCKWE3AGD+IbALBxEYRPfubPz9uazYWhMr75NRmCVFYYjQ== 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=BGhmcL2OHBdWCaoWb6sS59iIQkpm0AUCFdadplIl+MQ=; b=ROoxQmT0RQxlklLagkVC3KBCjgGKp1TIOO2IuLkfOGZ91ZFJjhyf3JjwgwGcyBtOsOMowjF/S1lLk5P2DpPsh0bBxR1h96edawekySFwdPlORA4Gn9HNi97meX3GNBLTiZU8NFZ31e1/JGPbThrWMl7z/6VlYmDfqPVRptoRRSA= Received: from MN2PR15CA0021.namprd15.prod.outlook.com (2603:10b6:208:1b4::34) by DM6PR12MB4316.namprd12.prod.outlook.com (2603:10b6:5:21a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.46; Fri, 10 May 2024 02:36:51 +0000 Received: from MN1PEPF0000ECD5.namprd02.prod.outlook.com (2603:10b6:208:1b4:cafe::65) by MN2PR15CA0021.outlook.office365.com (2603:10b6:208:1b4::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.43 via Frontend Transport; Fri, 10 May 2024 02:36:51 +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 MN1PEPF0000ECD5.mail.protection.outlook.com (10.167.242.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 02:36:51 +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, 9 May 2024 21:36:51 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 22/23] KVM: SEV: Fix return code interpretation for RMP nested page faults Date: Thu, 9 May 2024 20:58:21 -0500 Message-ID: <20240510015822.503071-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510015822.503071-1-michael.roth@amd.com> References: <20240501085210.2213060-1-michael.roth@amd.com> <20240510015822.503071-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: MN1PEPF0000ECD5:EE_|DM6PR12MB4316:EE_ X-MS-Office365-Filtering-Correlation-Id: 8a9c8d35-b30f-42e9-9162-08dc709a0c80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|7416005|376005|1800799015|82310400017|36860700004; X-Microsoft-Antispam-Message-Info: EgVtHvT1lXFKVIEJsVvgCk1Xmagu/sbxizEvRBk63+ce2YPkqcym+ULPL/NnGxRbqHR8bzqcNQpHv7TOm2URqJEkJbw2NB2hEBVkAQEwjYNGmh3sdyB+5+ITih0PcX/OwPMtU37u4UoTVZr84KS++zaL9EZYmMZgSIMQmtJ+sMe4TLdC1UuHptsc5EsmVJe7IvSKJfxlzv7wx85TszTXszhXMeSUH2xY7Z4cAJXVrxNt+5khZIh3wZ9tkLEkUiwlKM2sEhBOyLO7JokZJSVv0dVGF4io8Brh6E2kE43BZPZ8z9Z/ueGRyroSML7N/oXPB/w/fFVt/Yz0bVFvQhuMUvL752xAa5x+7QMm/QGDbeVS3xtafufX5Hp0Cpc1wOxbNentSAe3dBzlSfqIlK2gu8A5AT9kVHsYvWHVAByU1PU9JOu8zQ8e5syMaPVs8srbElkJLue6v4lm8vp+TGj9TS3rw2g1FMs7XusgosKp13tMix7MJOgLthJUPc/mkcvgkkeARJqXAciuHr79lE5nT6qS7uoEDtnTrasngD3XhgAl4MQJ2KMnqzgfHCxgPH15wcgmxrYCX8VvIaJM0047j96/u6GZk1hIX7FSIOisM9LG4asv4HP0OSNB/usrpLSjcAHI14UrLedlfxMZv9uvaUTuYpbCjilkkjdvrfeZvQuMnIaGex9GNK1p/XQ/Ok1VbsGJs912ISIXTJRxLCsvd4ngsVGNERiVQzSGjrDC18ikFd9r0XBkaPGT2wTOyx3ZelipAVjbQ/3aq3R57f+12EV+tQcLxLCOaGGSHfYgX9kVwFEWan62dCfjMsd+T3bmN4W+ckOC+K7iowdGG0he580JEe1aK86myLl/R8D2lDuFgeQRexMZVht/wCyHJCFZovtLXqjyQGzsGBJ4Qz5kbjqn6Gw/OZjBdCXBSgqfQS2I2eHj5zN959NO3NXZHNbhJrrUfb4MlpL7xOd6gogJ4xnRxHYw8MYrjoiDER2JH/DDcXGFI/7MRxV6XMeXgZ+evK6BLYWNZUJBd74EETEj4r5jIJKhZU+Hh8kbFbU/kvQygKybp9deaL9Wn4EsumOfA6tkacowYw6IVuhpBt50MBxZSyrm/fwKgJCisWUvO0576cw0vPs0oUYU1yreJlxN5X+Jw1BJeKZLXkVzGK1+OBCyXR3E54jLjf1jAbLXf5C2/eJfmw1x86vopym+Q7dMjceH+TOyKrTeiHzg6BBbyD2ni4QSDgUs3OAGiJODvMIzKKOcgWqi2yejzTa9vGuWYSzseiOZneVLqmRzIoZl8GEhzgdLEDQGz1ah37hZ/ZeFHYSefCLRPC5GJdDdtwoEM8GiFbWVdcdO4BFqaTbWzB3wFS/7s/hwL0DjmHErZ873By/PUUiXmXKDdnBNi0ZY 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)(1800799015)(82310400017)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 02:36:51.6169 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8a9c8d35-b30f-42e9-9162-08dc709a0c80 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: MN1PEPF0000ECD5.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4316 The intended logic when handling #NPFs with the RMP bit set (31) is to first check to see if the #NPF requires a shared<->private transition and, if so, to go ahead and let the corresponding KVM_EXIT_MEMORY_FAULT get forwarded on to userspace before proceeding with any handling of other potential RMP fault conditions like needing to PSMASH the RMP entry/etc (which will be done later if the guest still re-faults after the KVM_EXIT_MEMORY_FAULT is processed by userspace). The determination of whether any userspace handling of KVM_EXIT_MEMORY_FAULT is needed is done by interpreting the return code of kvm_mmu_page_fault(). However, the current code misinterprets the return code, expecting 0 to indicate a userspace exit rather than less than 0 (-EFAULT). This leads to the following unexpected behavior: - for KVM_EXIT_MEMORY_FAULTs resulting for implicit shared->private conversions, warnings get printed from sev_handle_rmp_fault() because it does not expect to be called for GPAs where KVM_MEMORY_ATTRIBUTE_PRIVATE is not set. Standard linux guests don't generally do this, but it is allowed and should be handled similarly to private->shared conversions rather than triggering any sort of warnings - if gmem support for 2MB folios is enabled (via currently out-of-tree code), implicit shared<->private conversions will always result in a PSMASH being attempted, even if it's not actually needed to resolve the RMP fault. This doesn't cause any harm, but results in a needless PSMASH and zapping of the sPTE Resolve these issues by calling sev_handle_rmp_fault() only when kvm_mmu_page_fault()'s return code is greater than or equal to 0, indicating a KVM_MEMORY_EXIT_FAULT/-EFAULT isn't needed. While here, simplify the code slightly and fix up the associated comments for better clarity. Fixes: ccc9d836c5c3 ("KVM: SEV: Add support to handle RMP nested page faults") Signed-off-by: Michael Roth --- arch/x86/kvm/svm/svm.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 426ad49325d7..9431ce74c7d4 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2070,14 +2070,12 @@ static int npf_interception(struct kvm_vcpu *vcpu) svm->vmcb->control.insn_len); /* - * rc == 0 indicates a userspace exit is needed to handle page - * transitions, so do that first before updating the RMP table. + * rc < 0 indicates a userspace exit may be needed to handle page + * attribute updates, so deal with that first before handling other + * potential RMP fault conditions. */ - if (error_code & PFERR_GUEST_RMP_MASK) { - if (rc == 0) - return rc; + if (rc >= 0 && error_code & PFERR_GUEST_RMP_MASK) sev_handle_rmp_fault(vcpu, fault_address, error_code); - } return rc; } From patchwork Fri May 10 01:58:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13660830 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2088.outbound.protection.outlook.com [40.107.93.88]) (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 89F5C10E9; Fri, 10 May 2024 02:37:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.88 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715308662; cv=fail; b=RJ+WRIWD+LhuucPzMjO1wUC0S58BiVZdmH2mWPJ6rLlSMk8E7ppjyWSU6maBso0M40637Gxsz6Nq3S1cTroloud8MsBryM0KrjP3AIDEOdGlUQwc8hj+ZHyE3eg8sFbTs/99wKREVzltCuA+PtahgJKLi437KPt2/JNz+X7eBt8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715308662; c=relaxed/simple; bh=SVhEphBm9HatbJVzmiJyOppxZm7UffmKJEf3IfqFicI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GrZ7G+COBX+IspyGWeqaxwCXP9jiGnJJ3tEtXhaOICCPK+qYuCStv0TgwUIAd5ZsGamYyoGjVb6y1s0ETNtIMo2B18ailoEugQ5mo4EmBe/ne+X0caCRAGOVHKNXbS+6CMgmH+YFfxs7H26rTH3NXaoCJDK+ikDlLM0yqPUMGLg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=EvvGTigk; arc=fail smtp.client-ip=40.107.93.88 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="EvvGTigk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DOmi7GdckC9/5tCpeFqf1WOzdbQp6UrMMaSH/Az9SeOMb1KKoL0burWOIiHp5qIDTjHPZFcIv8v96v6Z6UAJf7TGi5LkHl+E2uqq7xEKl3dXctV8Wcai8+JjEvXYBfW6vf5rO5+kiQA/lCfVqjvBoz1daCtHrnN1QmkUXKGGO9OJubZJw9J/ltxfnAmnzir7hjDvRhb9/ulj5Dv0+yYEvN7XaS+i5MjB3hdVPUsciLoyYoPT7q6WJk622QbVtj+wk33KXhdUXGRVvf+SfB3gS+CCLUbGEr+QsChli+CuDixVC/6b9mwiyG+RxLNQRXSGnY9hwr+a2Dn8eQWD0dV+Cg== 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=iWgS+DI5R4F/sRW6PXtLiv7vQo8y0dnmJd23rQLEfgE=; b=VU1OxlKYRjP8x+W8XO4K7lsrofPYAB4t6bTFma+FbVaE1XgVI54/a6Cy6W3ZTwOD8p21g9CC0ZwWAWnEP6agOmImoNZh01CZ0UysTcct3+uNH3flc7ozVfPLOwzW4/5EOQ4TBIwMvh1uKOzWfOowEJIEADZkGU9V4p3baklfLclx8kPXzDyKaHMk5/IonQF6NMCi/49djlWi8hNV6mO9Qqrfgw9VbjGUyJ1nZj65WoQ+kTtEs6TfmrxgHTzGQr4CHOdkWk4x0UjXE08MZ/C68Wv8s8umA8Pu9RYklpfIgDgTKPByvKiJyP7TyLw2DqeoBmLGjxIDHovQeKv8xMTbeQ== 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=iWgS+DI5R4F/sRW6PXtLiv7vQo8y0dnmJd23rQLEfgE=; b=EvvGTigkA8t/4ou5BN8idWHGcDHK8XaPO3AOT0tb7FWeQ+f0nunBRayzXHZcLvlwOdOp3W5Y1OyoqHdoc6KzBJJRMbQpMb7WhTdzTZPUMc8S8yJrEI2w+cAeKp+DXFKRAUNcX/75mrG0FzXCN//ska02D6vb6ATAxMlEYisSA+o= Received: from BL1PR13CA0267.namprd13.prod.outlook.com (2603:10b6:208:2ba::32) by DS7PR12MB6093.namprd12.prod.outlook.com (2603:10b6:8:9e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.47; Fri, 10 May 2024 02:37:36 +0000 Received: from MN1PEPF0000ECDB.namprd02.prod.outlook.com (2603:10b6:208:2ba:cafe::e7) by BL1PR13CA0267.outlook.office365.com (2603:10b6:208:2ba::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.45 via Frontend Transport; Fri, 10 May 2024 02:37:36 +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 MN1PEPF0000ECDB.mail.protection.outlook.com (10.167.242.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7544.18 via Frontend Transport; Fri, 10 May 2024 02:37:35 +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, 9 May 2024 21:37:33 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v15 23/23] KVM: SEV: Fix PSC handling for SMASH/UNSMASH and partial update ops Date: Thu, 9 May 2024 20:58:22 -0500 Message-ID: <20240510015822.503071-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240510015822.503071-1-michael.roth@amd.com> References: <20240501085210.2213060-1-michael.roth@amd.com> <20240510015822.503071-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: MN1PEPF0000ECDB:EE_|DS7PR12MB6093:EE_ X-MS-Office365-Filtering-Correlation-Id: 335743a1-5c28-4db4-c211-08dc709a26e8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|1800799015|7416005|376005|36860700004; X-Microsoft-Antispam-Message-Info: ji9kjP+Eq1LF9eb9G3BxQsLbHLCm2T30G8yPBcHserdQdY3gJ9hTBVl1iIftSdh87C42c7PFnlV+hfPPSJBxZgTPihWFG2VorkeaDKnrNMb6GiX9mRv475geS1JpDopmbRxzcZzdiNCrNCsw9OGncHlQlk+1LwYpXZRLy+pKoDnrlnPTKp5pA9H7BGNdyzyWjAl0S1ktH5uBFWZv+oOnHqzcxEUjQn9/t0JmEJoz63IEb6YfGfvEvuhaWWiZ5J0Oz9zKF086VApkDN+TmB3iNvdilMFD8DRNxAr1M1UKk9AeYGaYRl56ynT+OoKw1LdcdvzeglTprkJBGHVasysDdXpkwE8/t7SiWq+FpBd2dwfipnE84UvZrrHQirVHALgpWS+95Ie3arpezZlpd6vPlQ36vprp799H9Q711lqhPM0iTze5tBobcfAcIF0mxDZDxk/KctE5x4ZIc4JisrPltlQCcbMV8ugPeZZbfU27UnZp1gsg6zs9DwV5m8oAKLCU8KojPXsZrP0R77Ql0heuNwUa+CjeFHSomFONRLZ2vpkwucQY2JfqO0T1XNNdn2w2hpmMCxgnKgoZvoRkB07Ww0aQtX0aRDzRo9LTtCaxJMkXO7JDd8WBO7exLz38jOFUR0jw76dbYuYv21XjaCJtnKLIZ3smTXxDDp1DZLMSkLRMzntOqE28w6mP3DawNQa+Lw6nrcp+jY9MuSRVkZEQk0SL3skXNRzAeWaxTdAve3dW+EPQqrEPd601ddeTb2/wlL4LICWOwoKCQzt8wliWG2dhAPs18qMNZ1tRC0Ol0/8v2JH0B+H01ycnV7RIQJLwG4NdnR98A9riidzb8swfSfsaxATw8onvmCwgVD9tk+Sq/GNkKknvyd7iYg/URDLo2dYlzhjJa4rzltcfR8Bl7FBQBjz3UeYUv5NJZppahfJYbkYL9/399qL+4CGuL3Uk3gNZ/yrgQ/RG+ddAvqBszumPjeTEdCQHj+ztOyhTqwD3O/Fmn++Eltd72c7btHJc59WcQLeQGiqnRSE6dn2+J2CNxTNk0tNFBRyJWZUCeBqomVCFBlEHz+Keyf0gE0BNryeaUYdxl6M5qhorHYeoyvzFyA4ugd9BhKC3XuqnlmykaxEL1Fg40lUzlLoK8nWWSv4gO7xFop+OhIAzCDP9iI539inaLXoG3+35gRTBDZtlT31LlC3hnuAE5PQFG9CqdoHQOXg2ewahDA6j0FqGz2TZkUrW5F84VkoQPogz/1XxtMZX/5xA8SAt2FIC6kmMitzmZLu9smM+tMuONfjE1eaExIxeNW7Fhv5obV8OCy53HjWVZO297V1RWUosdGWdMdCoN6AvWvOrQN3OSIvTAyRrdRtG0RPO//eTWlX0rCOxnJAlokjv4lveescM8e1J 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)(82310400017)(1800799015)(7416005)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2024 02:37:35.9205 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 335743a1-5c28-4db4-c211-08dc709a26e8 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: MN1PEPF0000ECDB.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6093 There are a few edge-cases that the current processing for GHCB PSC requests doesn't handle properly: - KVM properly ignores SMASH/UNSMASH ops when they are embedded in a PSC request buffer which contains other PSC operations, but inadvertantly forwards them to userspace as private->shared PSC requests if they appear at the end of the buffer. Make sure these are ignored instead, just like cases where they are not at the end of the request buffer. - Current code handles non-zero 'cur_page' fields when they are at the beginning of a new GPA range, but it is not handling properly when iterating through subsequent entries which are otherwise part of a contiguous range. Fix up the handling so that these entries are not combined into a larger contiguous range that include unintended GPA ranges and are instead processed later as the start of a new contiguous range. - The page size variable used to track 2M entries in KVM for inflight PSCs might be artifically set to a different value, which can lead to unexpected values in the entry's final 'cur_page' update. Use the entry's 'pagesize' field instead to determine what the value of 'cur_page' should be upon completion of processing. While here, also add a small helper for clearing in-flight PSCs variables and fix up comments for better readability. Fixes: 266205d810d2 ("KVM: SEV: Add support to handle Page State Change VMGEXIT") Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 73 +++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 35f0bd91f92e..ab23329e2bd0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3555,43 +3555,50 @@ struct psc_buffer { static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc); -static int snp_complete_psc(struct kvm_vcpu *vcpu) +static void snp_reset_inflight_psc(struct vcpu_svm *svm) +{ + svm->sev_es.psc_idx = 0; + svm->sev_es.psc_inflight = 0; + svm->sev_es.psc_2m = false; +} + +static void __snp_complete_psc(struct vcpu_svm *svm) { - struct vcpu_svm *svm = to_svm(vcpu); struct psc_buffer *psc = svm->sev_es.ghcb_sa; struct psc_entry *entries = psc->entries; struct psc_hdr *hdr = &psc->hdr; - __u64 psc_ret; __u16 idx; - if (vcpu->run->hypercall.ret) { - psc_ret = VMGEXIT_PSC_ERROR_GENERIC; - goto out_resume; - } - /* * Everything in-flight has been processed successfully. Update the - * corresponding entries in the guest's PSC buffer. + * corresponding entries in the guest's PSC buffer and zero out the + * count of in-flight PSC entries. */ for (idx = svm->sev_es.psc_idx; svm->sev_es.psc_inflight; svm->sev_es.psc_inflight--, idx++) { struct psc_entry *entry = &entries[idx]; - entry->cur_page = svm->sev_es.psc_2m ? 512 : 1; + entry->cur_page = entry->pagesize ? 512 : 1; } hdr->cur_entry = idx; +} - /* Handle the next range (if any). */ - return snp_begin_psc(svm, psc); +static int snp_complete_psc(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + struct psc_buffer *psc = svm->sev_es.ghcb_sa; -out_resume: - svm->sev_es.psc_idx = 0; - svm->sev_es.psc_inflight = 0; - svm->sev_es.psc_2m = false; - ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, psc_ret); + if (vcpu->run->hypercall.ret) { + snp_reset_inflight_psc(svm); + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, VMGEXIT_PSC_ERROR_GENERIC); + return 1; /* resume guest */ + } - return 1; /* resume guest */ + __snp_complete_psc(svm); + + /* Handle the next range (if any). */ + return snp_begin_psc(svm, psc); } static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) @@ -3634,6 +3641,7 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) goto out_resume; } +next_range: /* Find the start of the next range which needs processing. */ for (idx = idx_start; idx <= idx_end; idx++, hdr->cur_entry++) { __u16 cur_page; @@ -3642,11 +3650,6 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) entry_start = entries[idx]; - /* Only private/shared conversions are currently supported. */ - if (entry_start.operation != VMGEXIT_PSC_OP_PRIVATE && - entry_start.operation != VMGEXIT_PSC_OP_SHARED) - continue; - gfn = entry_start.gfn; cur_page = entry_start.cur_page; huge = entry_start.pagesize; @@ -3687,6 +3690,7 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) if (entry.operation != entry_start.operation || entry.gfn != entry_start.gfn + npages || + entry.cur_page != 0 || !!entry.pagesize != svm->sev_es.psc_2m) break; @@ -3694,6 +3698,25 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) npages += entry_start.pagesize ? 512 : 1; } + /* + * Only shared/private PSC operations are currently supported, so if the + * entire range consists of unsupported operations (e.g. SMASH/UNSMASH), + * then consider the entire range completed and avoid exiting to + * userspace. In theory snp_complete_psc() can always be called directly + * at this point to complete the current range and start the next one, + * but that could lead to unexpected levels of recursion, so only do + * that if there are no more entries to process and the entire request + * has been completed. + */ + if (entry_start.operation != VMGEXIT_PSC_OP_PRIVATE && + entry_start.operation != VMGEXIT_PSC_OP_SHARED) { + if (idx > idx_end) + return snp_complete_psc(vcpu); + + __snp_complete_psc(svm); + goto next_range; + } + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; vcpu->run->hypercall.args[0] = gpa; @@ -3709,9 +3732,7 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) return 0; /* forward request to userspace */ out_resume: - svm->sev_es.psc_idx = 0; - svm->sev_es.psc_inflight = 0; - svm->sev_es.psc_2m = false; + snp_reset_inflight_psc(svm); ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, psc_ret); return 1; /* resume guest */