From patchwork Fri Jan 26 04:11: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: 13532058 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 214F4C47422 for ; Fri, 26 Jan 2024 04:45:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9C706B0089; Thu, 25 Jan 2024 23:45:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A73BF6B00A8; Thu, 25 Jan 2024 23:45:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 914E76B00A9; Thu, 25 Jan 2024 23:45:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7E5746B0089 for ; Thu, 25 Jan 2024 23:45:07 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 561D6403D2 for ; Fri, 26 Jan 2024 04:45:07 +0000 (UTC) X-FDA: 81720222654.01.394C261 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2043.outbound.protection.outlook.com [40.107.93.43]) by imf09.hostedemail.com (Postfix) with ESMTP id 6423314001F for ; Fri, 26 Jan 2024 04:45:04 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=TFrl5xVk; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.43 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244304; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZrwuA+myy/OhKqqUcE+jpu3j4mlNa0YdB31jV4lASGo=; b=sPROR2opKFqpsWVxQnfL4A9D15ucrEslOpM6Xb/NgDRGlM3isq4HKmkk8uypNXYZX9FFka IoaeQ0VpAmldAK1QsAZX8GMY5dUzvT8zOoE+cGJn1bkAClZEIBANvx4jWu/RfblncKG4AF 6BcdgU6ep6Jc1otG+0gIiEfvgkj4h5I= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=TFrl5xVk; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.43 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244304; a=rsa-sha256; cv=pass; b=x3FwaMwZeVkelaUHsP1rzsABygU+TqNkNZ6NrnUGxvphla6YiERGYdFnSKb1pTlz5eaJBM 2pd4UVoCWPBvQBjZhRV9AqubV/2FJYDpcFhBFH6TbREuVuF/drKbkb3UbJ584zlDVcorzy 2h+j3C3UjLmKSxq778PMq6jKhEq085U= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NORmJmUq3hewCRKzFyyxWUYGy181Yq8fpWCoEtHsennHv4epf324Y8riNOB3LU9ZNn45GTseD7Zryh3VxbUdWIERUBiy9qZ3cCGj5l0miNaR+Q/WnRhVq8Boc5/hRGimEv4Umrywio/krRR4gGcwhZtJHUbCQLi05JJsApBvKbYfUfbV8P6PFeNbfikxeFsvPdn1JOBtuBHqJ82jax3+aprGW2jw9CqQyo7WiN6S5cNEw83BWNJjHvCq5RjEZTS/eN2/rCTa0ZZ5q3VOdDqwewmiA8ynhQ6mGOVHHs77bBQ7ZRiDV4gizylkbt9So915UTrTBhLR5xGwLYI7B61WNg== 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=ZrwuA+myy/OhKqqUcE+jpu3j4mlNa0YdB31jV4lASGo=; b=K+rj3c7t03BUAfAcIcXSI0tCMtbVUS8c1MKFlBzs2YojsrU2VCbcMajD8Lv73Ze/EGO4GXaWFb06Kl1TlJmuhkHwmfcOv1zV9UEb4plTiDRTuMmyh7+jP6OJpy7I/etEy+EHEVXDNm8AkGbhaBsenQFtY9m4la3SEz4Dm9XxU657rCy6TSpsXmaskgMYpqJC0sdKnmmZ00m1nKFIeuSfla5Yr5j+lpSuUcGJUx2KV3nRwh+NgM6fV7ejXOdMBuDyZKquJaqFzyT5p8Kl+M9FS8t06Xe707jwZ48TPpH/QHv52PyYceglRRCLGdxKwufNyzSxADNB/p8qB21RNIWHrA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=ZrwuA+myy/OhKqqUcE+jpu3j4mlNa0YdB31jV4lASGo=; b=TFrl5xVk28GpKG7SBiKvyf5FUcIFy7LLlN+f7WhNBuZVPWXflzykkEKEBvtNs5GcvYP9k10Ia99BFYVQH8dMF6qVn723ULxjQdgc0TAGoIfp9ePRET3t2dJFPqqvF9HxvSHYVQH5QhdkyJeYhhslN2hTiTMGgFYwwQgkQU0DuhQ= Received: from MW4PR04CA0216.namprd04.prod.outlook.com (2603:10b6:303:87::11) by IA1PR12MB7517.namprd12.prod.outlook.com (2603:10b6:208:41a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:45:00 +0000 Received: from MWH0EPF000971E2.namprd02.prod.outlook.com (2603:10b6:303:87:cafe::42) by MW4PR04CA0216.outlook.office365.com (2603:10b6:303:87::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:45:00 +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 MWH0EPF000971E2.mail.protection.outlook.com (10.167.243.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:45: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.34; Thu, 25 Jan 2024 22:44:59 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen , Ashish Kalra Subject: [PATCH v2 01/25] x86/cpufeatures: Add SEV-SNP CPU feature Date: Thu, 25 Jan 2024 22:11:01 -0600 Message-ID: <20240126041126.1927228-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E2:EE_|IA1PR12MB7517:EE_ X-MS-Office365-Filtering-Correlation-Id: 927a4435-6866-4501-2d90-08dc1e298de7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z14I7UH0O9zJcJLtj9u6+VPOuiymI6TtN+U49b4OsPyNha/EU6UHwXGGrlmJmevM8A62piUIw/446E1+tuvDWMAxGN5E2Ji0jBFXnJwfbk9yf8zupv61R7N91MKi9IxLZBIS6k7EdgwpgoTcibGDVGcNavJzKWDcjpWIEXd8GAv+avX++pOY/7sW2EyPPWb/LFhIL4nh7728QEnX8UiKRthRCsVetIkIY+WFTQCShLiVytesSownful9bTRAPGbDABZJWct5l9SPh7kUts9UayjTt77SsQUCVQEH9oJfJcpgGg+d4fptjV9ByzixchVrlWUTeVO48YOe0ueizAJ5m4Lhi+Juwi3v76+3FhImY6+e9wkhKc4k5Q3D3LOY4QNs/21CJ+vWFQmeRt7TGJXLro34QJw6SAapcJY/YW44/dmbZrVWAalyzeAR7UOCFtCSy77Tlq+2gUaNjgPbI+o9RUTpkk1u+dNbzgvRdYcxNcMf0GpBNHVX4heEVK4mQ0wubXuXRxxBIcJnRH+He3uy0i6JaNIMbMPBa7sj1mJj5DsDsEsNnIXyWjsARf/g7yvWlEyLaEE2fOrsemXy+I00IolHCCgNlYejkqbhO0wkt7ZQ5MeznZk7nvaT2sBDDrQrhS0QGxP4xkGalbNR2TIMssJ7E13+j1ZoVzjpZag1fS//0MuMp4Q78ti5kq5aelL3mSA2IZL2UdJtATYvIs8gpMu+WCqViay5aQKcKivpP21ZIaErWJCqhPivdvrT6c4+v1riFbyRYbxPJUz3QEnEFA== 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)(4636009)(396003)(376002)(346002)(136003)(39860400002)(230922051799003)(1800799012)(186009)(82310400011)(64100799003)(451199024)(46966006)(36840700001)(40470700004)(40480700001)(40460700003)(5660300002)(426003)(336012)(26005)(16526019)(70206006)(70586007)(54906003)(6916009)(2906002)(7406005)(7416002)(83380400001)(6666004)(316002)(478600001)(8676002)(44832011)(4326008)(1076003)(2616005)(8936002)(82740400003)(81166007)(356005)(47076005)(86362001)(36860700001)(41300700001)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:45:00.1435 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 927a4435-6866-4501-2d90-08dc1e298de7 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: MWH0EPF000971E2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7517 X-Rspamd-Queue-Id: 6423314001F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 1181z7htppxm1u8cwb7bd7mdem3twg66 X-HE-Tag: 1706244304-388491 X-HE-Meta: U2FsdGVkX1/aCfqd/jvbJ9DjrqOstqBIAGjpttxxjrIidTqdbaBe6EeXBoOVhRkCmbun8VYa3kh5sdWz+uhdacTupSYprRQPvtLSh1BFDYabuy/OgcpHmmgcjZnR5MyYNik1/rxPdWKQr35CK8/++m+zfeS8flLT9gTffi+5JcZrhWDHDsqujPz2BVdFMgKR9goLnr+VH7YfkYOfak34sBuuKA/N/hvjJcmMXeGUJzYT4+FdA/UMjGj1EUJc4fYMdbOsmbRD3O58FWSgdNDABgmtxiB3qRPJNvKyZkOHZ9l6yXvIGzDWx/lNoLoetK3Jg7udLMY7auDDv6d9K5o74vXrv6vgXYVfZTKB/iOe0sS/7go69BJdeAB/Wp9KrzU70HV7NMCn5drNbcMFm0xjsRFXZtcNYUp/anD5nreCfUSpUWRolQbqACQqhrdR9w1xiIVEEji/T36xt1N8OepvAio2XJ96G1GUztCmqs1AGbCYOn8+L1VDtcCmWTyVIPPvvZP1zrbSxi/IGN1i79NzIy8828+yjBPXE68IpTYNn5BzFNbcTvnWYDwtLeUN1XmGAGslD3YNrlwpj+T/tiBYkgQekeKIVGPMAka0BRRrHT6Wxt7kix4JHp/eZx8ppfI6bRmVOAJP8mDFUJey8oNNSx6d1QhoHesfduoZbCcxzp3Nu9IptJoiNMvLNYYibvAhEPRDqTAcXeFEZ3iPWhFrw0uDJrmimVS8pfcKVSrohRIo64p68o0P/GC0sfFDpJ9y6rSCFh4NjfOsom0+X28FBpzhPTaY06dghHZ6GonGF5BI52lCtj0eveLNSaekkowZAOLcw7pUlgNQDJgZ1CNCx+QBOx+nEBZdGLx2Kuca5ARI7/XSGlwJfR4NodZkyChS9R0meuElv4rgvfqm7tyBK716eKSyPWVcuNRlC/U3F8d9hJXcbOI6hd0oT5Lw74R7sa//BIx/88f/L0qt3gl PrRIPwoD yXCttHIFAgp90VErZZNdtTkavNUlAirq8KAsq7uw7gONsQuZ4WOmZbr8PojnoNT5G1SVkxf26jMqyEC18Oe8ql2wWTt7QFJkwnc1EkkxE//ZoPhAEi9h4joteF5h2meXQVRy9gu8lapuWXIc8R6O0HiKYcnJKHDDZyg6CEs0TGGMTRMQZxGm+jdLQlSFjcicuUQiRK2/iIw+SuHazja1O8TxS6Mb7wDwOYEOeES5lnfRUhGTTNTaIuInt0NChOmevOB8qddE/36JVHuUBMOFRijIlnuEHbLZKTIXmamWBS0290tPaXFRAuQWqIxblCauXUhgnun5fZGiOaZaL9onywBdmeDuwoaf1AeInr1ke1kpINy/6ASrZyTjhFzyCkDVeSxzVhNaBsPvji+E= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more. Since enabling the SNP CPU feature imposes a number of additional requirements on host initialization and handling legacy firmware APIs for SEV/SEV-ES guests, only introduce the CPU feature bit so that the relevant handling can be added, but leave it disabled via a disabled-features mask. Once all the necessary changes needed to maintain legacy SEV/SEV-ES support are introduced in subsequent patches, the SNP feature bit will be unmasked/enabled. Signed-off-by: Brijesh Singh Signed-off-by: Jarkko Sakkinen Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 4 +++- arch/x86/kernel/cpu/amd.c | 5 +++-- tools/arch/x86/include/asm/cpufeatures.h | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index fdf723b6f6d0..0fa702673e73 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -440,6 +440,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ #define X86_FEATURE_DEBUG_SWAP (19*32+14) /* AMD SEV-ES full debug state swap support */ diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 36d0c1e05e60..1ea64d4e7021 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -117,6 +117,8 @@ #define DISABLE_IBT (1 << (X86_FEATURE_IBT & 31)) #endif +#define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) + /* * Make sure to add features to the correct mask */ @@ -141,7 +143,7 @@ DISABLE_ENQCMD) #define DISABLED_MASK17 0 #define DISABLED_MASK18 (DISABLE_IBT) -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP) #define DISABLED_MASK20 0 #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 21) diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 34e5c2cb8042..79153e9b92b5 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -602,8 +602,8 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) * SME feature (set in scattered.c). * If the kernel has not enabled SME via any means then * don't advertise the SME feature. - * For SEV: If BIOS has not enabled SEV then don't advertise the - * SEV and SEV_ES feature (set in scattered.c). + * For SEV: If BIOS has not enabled SEV then don't advertise SEV and + * any additional functionality based on it. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32. @@ -638,6 +638,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev: setup_clear_cpu_cap(X86_FEATURE_SEV); setup_clear_cpu_cap(X86_FEATURE_SEV_ES); + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); } } diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/include/asm/cpufeatures.h index f4542d2718f4..e58bd69356ee 100644 --- a/tools/arch/x86/include/asm/cpufeatures.h +++ b/tools/arch/x86/include/asm/cpufeatures.h @@ -437,6 +437,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+ 4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ #define X86_FEATURE_DEBUG_SWAP (19*32+14) /* AMD SEV-ES full debug state swap support */ From patchwork Fri Jan 26 04:11: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: 13532064 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA9D2C47422 for ; Fri, 26 Jan 2024 04:47:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F9696B00B7; Thu, 25 Jan 2024 23:47:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 481FD6B00B8; Thu, 25 Jan 2024 23:47:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D5286B00B9; Thu, 25 Jan 2024 23:47:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1682C6B00B7 for ; Thu, 25 Jan 2024 23:47:11 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id ECFA91402B1 for ; Fri, 26 Jan 2024 04:47:10 +0000 (UTC) X-FDA: 81720227820.14.6A41D90 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2089.outbound.protection.outlook.com [40.107.93.89]) by imf08.hostedemail.com (Postfix) with ESMTP id 3751616000D for ; Fri, 26 Jan 2024 04:47:08 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=g7M43fZ2; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244428; a=rsa-sha256; cv=pass; b=B5dnl98lug7YYojMxBSLN1UmM0089acJPR61qzrr9YGMZQWyhB2PrXDPcJPMBZo3Fn046I x4wfWXOuLFYHDpHC9sh8Pe6qrUcrZcfkkVp9yNIGABm57J4Zay2SV5ek8yBUEMDpzMU+Hr dPj+nubM3iLcb22lQQP/X6aqiPVDQKE= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=g7M43fZ2; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.89 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244428; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=42bfQ6FeSVGZCam3Ke+AMjdPhfDYAoqjZO55LNOmw0E=; b=om7ECb9WHixBQEl6jcmnYWgFd1n2m5pQpi3aGgVWycqJJJLD7bD0aWMMceNRKki8s8j2TW HbvF5wvKAZWpkmuKIzodb1k2+qjS0YUCsj2LGD8xSU1qUzJ/sS6pqD+1I8clbg8X9HQnDq MxRaiWmawq1NixLMTwvQADamZGlsiLM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S1DTrrrnFBhNV5pH5i1/g1svc2pOWwA6mbno5u1u6jQoIR6X0OEXQcIq4q8qHppLbdtP4qBVIDCkRf7pd+/6thOxS01jXqfIiseR9qWTRUuR/hBS46Lwgwjf1BjTBhAbLmbf2ZyMy+TX4Io/FNJEo0lQAitTEvnFytN88LDv9ezeLMvwOQ50FCQLreCHl16wT22YYGTAoJBmltEdqICt4pc00thAsF7rJDPQ7CSBhordYox9tsYc3S3bnjdWeRYDYm3y9OvVK2fF0VueFPlDFUCFO9SIpJPrG/f+fk/qtqSgdEQCFYhBfhypzcwvT7m8IquQYRlf9othIKxD+MxONw== 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=42bfQ6FeSVGZCam3Ke+AMjdPhfDYAoqjZO55LNOmw0E=; b=FHx2D6PZoZd8AXxyflBKlaH7SoO74ERApwJRBbmy9goF9e58cFXg2Ro1GEFeto1b6o4j7ibgiJU//CLQi6EwYImXRozUXK6Cx0YTVOkagw4UToeOMnYLVzbeI3TQVXjl/q614SqMTuR6cfq3dosla2rCBm7xHpcyoU0WAldsNE5UtjC49qikHUaKNrSdHsjZQzJQF19rf+K0LNbIYwgnqBNOoXiEqw+OkQwPdH7DmyFJj5IOHKv5yCHFhro0eM9lMqXUHfDE86H4kyljz5jcnrIv2p8QbvYJwmILa/szwTpbco9oz1I0EoCGfwvlHOcpV7Wdi11Mfo8qtV9sVhwPAQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=42bfQ6FeSVGZCam3Ke+AMjdPhfDYAoqjZO55LNOmw0E=; b=g7M43fZ2184zswG+joUmgvnGGK+5ZAgKnlNvpuK+a/RFJa3hn88deiXQ9UJ71uZt69heDVoc2Q7Np5FCKsG7XmfhEeX8KX/Vh5BZG71VgMAswvMI0L0rJ/2xGTSEKTsegaPrZburUjZwUvs6hyKzrdZd6Ll3j8WaS5gX36mme3I= Received: from CH0PR08CA0025.namprd08.prod.outlook.com (2603:10b6:610:33::30) by BL1PR12MB5995.namprd12.prod.outlook.com (2603:10b6:208:39b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:47:05 +0000 Received: from DS2PEPF0000343C.namprd02.prod.outlook.com (2603:10b6:610:33:cafe::93) by CH0PR08CA0025.outlook.office365.com (2603:10b6:610:33::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:47:05 +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 DS2PEPF0000343C.mail.protection.outlook.com (10.167.18.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:47:04 +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.34; Thu, 25 Jan 2024 22:47:04 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Kim Phillips , Dave Hansen Subject: [PATCH v2 02/25] x86/speculation: Do not enable Automatic IBRS if SEV SNP is enabled Date: Thu, 25 Jan 2024 22:11:02 -0600 Message-ID: <20240126041126.1927228-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343C:EE_|BL1PR12MB5995:EE_ X-MS-Office365-Filtering-Correlation-Id: 75451af7-cfca-47bc-91bb-08dc1e29d834 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PPYG51viIBgn3zzeikdi6PnXSoSYJYqYcUwGNM7bWLs/PMAfI/4RLnOjnw99COLAewhy6+XExTVpGi8jLR/fTeZyEVz05bKCIxAr56XQkzqWzk0NF9hWNYDOHxagxJNde3EBpy+SE991JzXL3qAlwJBtTgniP0NSwhhX5HJCT6F2PLCYnc7a3hzy9fn/BRuZihVqgt2fUVivFY5jM8SIOqR/LkmQyDDGXks/2VfUG9RNl4YpCOxDeLzff5+08VlmhfrON3/FV09Hl1SQLHliPVfnCrMq0BArPOjtrWS10EfG6MU/Fe8D/WPvRSHFNomxSedKlFNyS7qPEx7mecZmCkyzrZQE48Pc38kc+a2Mc+NHsT/tPKQgMcJYDnOh3+M28c7kBW4iVb7BraGGQKeVyV3uOyA11EjNdiuVYvfcfkNvA8/0kqc0dHX1YdvBNxNYfYoFZ1L//6vYY8VziGN9ebNOviIT08MaNG6mRdGvnIJHw6OyNIpi6rByr6K8PR5CWjIVZhkoNDBo67NnsAsED1iZTTqtfjyHeBK8QSxTfmaotsXXF5tum6c4LOrIc48dNLLzKIxWY+3Gsydq2pUl+ggB3su84F6UslVfJwuf30DX2E9rAnMXKONaXpN8gJidQvj0W/vdvtQJg/mXg1U947M6g/n2DrUMbOPhwET66tQ4qsA8b4eynvtca8OtndfBXrnS9XdvNrM9HvMyCwPRchDgDVAtdz5K0c5Tc1NfNMSPIVxOW5qGQR0bfh66CEFD3UIb4z+/A9RpV+F91kaBLw== 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)(4636009)(396003)(376002)(136003)(39860400002)(346002)(230922051799003)(186009)(64100799003)(451199024)(82310400011)(1800799012)(40470700004)(46966006)(36840700001)(8676002)(7406005)(7416002)(8936002)(4326008)(2906002)(316002)(70206006)(44832011)(5660300002)(86362001)(70586007)(6916009)(36860700001)(36756003)(47076005)(54906003)(356005)(82740400003)(478600001)(81166007)(2616005)(1076003)(426003)(336012)(41300700001)(40480700001)(26005)(40460700003)(16526019)(83380400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:47:04.8802 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 75451af7-cfca-47bc-91bb-08dc1e29d834 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: DS2PEPF0000343C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5995 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3751616000D X-Stat-Signature: mf8tmouh3broqu1wdx1nj3xex7mi74or X-HE-Tag: 1706244427-920386 X-HE-Meta: U2FsdGVkX18OyGfD/+lP1zn2RMa70sHTH79A2/sDoueNY4FRmlQIcVHWZd8gqV00DdMP6v691PL31qdJxE/Sv0e/VNY3WsZk6KrFlRRI6tGtH0IedWbJQ7fbYnt9rChBgKMOaDDlvqqbJOfR3hN1O+LNXZ10TGACmuVZL1SRCjfwhvMe2zGYV4VVXKFE+1ogw0JW8c4m3zmmyr0UdxIcaQgGHOOjWhvdxQiBLjitHrym9Duy/Fr2bZsXcwAEjAbX6tltXBc44Gqy81bWIj09XC6CTiQ2JRZY0YASsWUZ0NBEjstr2dYetCcatOJDwpJukQEKiS7S8f34QY1k7jP9THYqNVvinQoknzXwLjMUHS7NTCv8Je/jgZYTejG9mYxFtXjMkeKmyn4oEWJN5l95xW3akZmHqkMNm4U5ne8lpKiYiJs4kuIxRRti+ev+Y10OAJnHuGApKWhTSrrNiok/1V3UVM+pn6ZW+kxGoPxxNwel51X6yGLR5LKmQQ8zsYKqRPtwJsRshuLweogTO09ND+RE0C0ohooeJnrJi2p1jlZbHUPFNuHN/2FShCiMnsPI9JGi1cDdlCezH2XraGe41XAyvEb/8vNAY68WtboJqbIWzMHPc3JP/ee1akJUVE4JHu3OFY5NGLqrRecLRDC/ytOcOkYbRn+ftakco71L+2P+T7slAXmBzXCftP0joUmkt7hzOZUWXUK4B+x1XJSySFtHXLWfjBWLsdSWlLowu2ctHyWcz7vGZKeFV1nu4ZWE+OzVJdFM4Ns1dFqYMR4JiRhtHaoTGkRB3u8uwsk7Zi7SnaJzlzKrOExXaJGOIM38RmQDlAdC71fViQYtU5gsKoKPx2xQmHSqqn1/bvsZihQ8CGi5wuHzCTgOIBYm/No3AI2744XbXyhKPoO5FtLYBB6qziJf+jewxLp5eT52rXA7ww1cXcCtlwLhoK73uOzb/MCQjiCyoNNBSVcmWVI Vvw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kim Phillips Without SEV-SNP, Automatic IBRS protects only the kernel. But when SEV-SNP is enabled, the Automatic IBRS protection umbrella widens to all host-side code, including userspace. This protection comes at a cost: reduced userspace indirect branch performance. To avoid this performance loss, don't use Automatic IBRS on SEV-SNP hosts. Fall back to retpolines instead. Signed-off-by: Kim Phillips Acked-by: Dave Hansen [mdr: squash in changes from review discussion] Signed-off-by: Michael Roth --- arch/x86/kernel/cpu/common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 8f367d376520..6b253440ea72 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -1355,8 +1355,13 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c) /* * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature * flag and protect from vendor-specific bugs via the whitelist. + * + * Don't use AutoIBRS when SNP is enabled because it degrades host + * userspace indirect branch performance. */ - if ((ia32_cap & ARCH_CAP_IBRS_ALL) || cpu_has(c, X86_FEATURE_AUTOIBRS)) { + if ((ia32_cap & ARCH_CAP_IBRS_ALL) || + (cpu_has(c, X86_FEATURE_AUTOIBRS) && + !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) { setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED); if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) && !(ia32_cap & ARCH_CAP_PBRSB_NO)) From patchwork Fri Jan 26 04:11: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: 13532065 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1713C47422 for ; Fri, 26 Jan 2024 04:47:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60A678D0001; Thu, 25 Jan 2024 23:47:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 594056B00B9; Thu, 25 Jan 2024 23:47:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 398408D0001; Thu, 25 Jan 2024 23:47:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 214616B00B0 for ; Thu, 25 Jan 2024 23:47:31 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id F2BEEA23E1 for ; Fri, 26 Jan 2024 04:47:30 +0000 (UTC) X-FDA: 81720228660.18.4826C79 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) by imf19.hostedemail.com (Postfix) with ESMTP id 09BF61A0018 for ; Fri, 26 Jan 2024 04:47:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tT6aYyvF; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.75 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244448; a=rsa-sha256; cv=pass; b=GB2qCu3pYkQhQnDJU6Cnh8w3gv4L4CxPzNCnSryC073i9sFfAArOyDKkpCRy3i4aZ+KlZl KLIDTJBKUMxsncUNRvGg+N2WZJaNmgpYb+C86dwPDdj8+fak5wkX2taZAlga343KecFZqj jRsvsrD2f64iLnI0UuQcMXdGWxrBTsA= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tT6aYyvF; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.75 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244448; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NBek176QqoHkHFTnqUlqsPM+BztDxiraqcjGfbpN8I0=; b=8g4Oq+/WferUYhJO3DsLQkF71IS67MQ8HYck/aMPJgoceY2ro8nuVQR2rM47On6oxxBkMU ZU0tkJ8l2gPxIXD/pWjYieSPmXPd6bhP75x14Ix8X4H4Ui8NADOGUMR2W61CKxSg1JkZxG Po7uLir3+9WoR0LxR+aEBZ2uEbEbJoQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ceoRQrq+IgLVVDpLxXpGqbP2qWiAqQJZRjBXkcDuGHZIsbbOwTQe5oQ5lsgLYMUaiMhmC3zbalTmLmfXJ6p36kW5GGhC42LPOrXBmKDDP4SqHOWIuddiwBbGgjQY3UpIE0tiA84noJSpTA+a6wgXCR89zWEAnrH2YWbE9uxxIHJws097AIVCwIprjE0yc6ROTeLAvn7BPw5hMWp+AWMHx2uuYOKU1hUtYL7Dz9242SgsOKT+qhpvNyatQfSrA8YxB9gOxQ1xAX+IdP7kdqD8kQTnKQ3eyhiu6jTeV7xhVLGtVCOTg18nucC9CV3tZSSwT6U7k41b+BqrXsBaM7lzig== 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=NBek176QqoHkHFTnqUlqsPM+BztDxiraqcjGfbpN8I0=; b=e9FqhDffu3LV9vRMBDFdI+FCKQPPaHIrTSgfOQDiLEm8azQgTsSWUnR29rlnZ9KJNfvN3uTd3g2MjMg2obMjDMCy7aRF9TdRAuluKw28wwDdaCv1cXQzE5D0CWc9BfVJMxz11spbbFl8YMWRq5VInYAmah1VWah3dzVshIV+L4wNkh5XfGC6gvkh5ANJLMvpcRJReyKlFljRQ9CK9O//+xIuu0f0RLBqT6AEeTDECMzaVBfwM7tQZcTsQxArlK6Qiqz1gdy23huxz2ZabHkuXQggvUTn1hCkPJvnMOjzDR89ODEmk2S2CQ9fUAd02V/FUUYvkkM6OqaJfgUa2uH8ZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=NBek176QqoHkHFTnqUlqsPM+BztDxiraqcjGfbpN8I0=; b=tT6aYyvFZIPmWVEX7yoNV/udFFop7tAqdIBEj7/rDOQQ1GYu62/md7mXYdgxL5c0m8P5G549nA4AS1cOrfCrWm75ZQHFdjE1mOJN3ZkcdNbxKRBZjloqZD3Upk4w8B//zKo5we/TvsiRCrvj0ceODnv5k3SC5glC62qThXT7YM4= Received: from DM6PR02CA0145.namprd02.prod.outlook.com (2603:10b6:5:332::12) by CH3PR12MB8852.namprd12.prod.outlook.com (2603:10b6:610:17d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:47:25 +0000 Received: from DS2PEPF0000343E.namprd02.prod.outlook.com (2603:10b6:5:332:cafe::7a) by DM6PR02CA0145.outlook.office365.com (2603:10b6:5:332::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 04:47:25 +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 DS2PEPF0000343E.mail.protection.outlook.com (10.167.18.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:47:25 +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.34; Thu, 25 Jan 2024 22:47:24 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 03/25] iommu/amd: Don't rely on external callers to enable IOMMU SNP support Date: Thu, 25 Jan 2024 22:11:03 -0600 Message-ID: <20240126041126.1927228-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343E:EE_|CH3PR12MB8852:EE_ X-MS-Office365-Filtering-Correlation-Id: 07423e29-4b92-4799-7cb8-08dc1e29e47f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: l9wmNHlYa0mNiXk1c/w2oBNPwghxyOcOYuUY/vTjt4cDvVZtaDMtbHcbk8k+ZlAT+jb1fLkw20kMbY5/PWQz9YsqA40108eDRRficOq2Wh7TEi0H1fg5JvBThKAp+8KUzaPZh2Pd6mVl/FZQlHDXRxIr3/wRqgATuw4yMPvUew8w72PQowGjLyfnQjYq0D6F8aFrFl7eEsrGxrZS9qMYTqT7rsNT6w1FH9siXejLGhQee3qma/i0AdLPjm3bhu5p3PYFOG1VCwi0W0etmeQe/sPSYY54czCLtF8V1Q5+p9tg7sB97L1TWxSoubR8pyDBe89mZMALk9xz1OH9IVwloVSePkQdn+OvMLRoRakxqgunBbn3AjkHM0acxCAixoBzphUxeYjJ1Ce/BWc/gff4vtmcNAUlVELUhstAtzhAm4z7nQ0ms0QqDKajb9a9gf2VBfuZbfHE2Bg29k1hTruxpS3WPzJAI+UqvXKgwhlAPQBSp/aa28eyHUDPZzZ14FvCUwmdCZYJReknACfYY5gEg/62/YyYjuSFmFAmqWBEzADWCbBzzp6CxlIjd4hlw8qDiE0vfuLFwHu9T6M2FKNSYVh8OTtwWUbqb4TPz7tN8YX0von70WiF6wJprGV6G6Ja1vqnlhYjcS3PdQK3LqWi995vQLafJxfzMrI/wn2N+JFbw7LJYB6luY029n0VnNDgVApgxO6tF1OTXwSik30YV+xzBVC1d3xr1pJSlWT4qd1bn7lZcBvTOJQeCLMEdPiVQS3atMc5bSTjfufAfHUNjA== 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)(4636009)(346002)(396003)(376002)(136003)(39860400002)(230922051799003)(82310400011)(451199024)(186009)(1800799012)(64100799003)(36840700001)(46966006)(40470700004)(40460700003)(40480700001)(41300700001)(83380400001)(86362001)(36756003)(82740400003)(356005)(81166007)(36860700001)(44832011)(47076005)(1076003)(26005)(2616005)(16526019)(426003)(336012)(2906002)(478600001)(70206006)(6666004)(6916009)(70586007)(316002)(54906003)(4326008)(5660300002)(8676002)(7416002)(8936002)(7406005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:47:25.5039 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 07423e29-4b92-4799-7cb8-08dc1e29e47f 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: DS2PEPF0000343E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8852 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 09BF61A0018 X-Stat-Signature: r14w39aasuha3hhobszppi3m91uxasfn X-Rspam-User: X-HE-Tag: 1706244447-323991 X-HE-Meta: U2FsdGVkX1+n1tDjf5iGTtlDtTgu4nEgY9SEDHlawyQHGh+RnTmfRNdlDlQplQKxrPRuCXjBjnoUyoNQLBvkPjq6Ho6jqME2wVTIC7TmHNNx+rZntPtI5waJ7rhBrdSS63TV4CP4UTXoJTHA0Z16jWIXA650uzSKjowRAjAnTRQVtuWtmcGlNpbgE3YfpBErRMpzsnDeILbBP0hxSctjoEcvtNl1SLVY88AJfwfu1pKWKAdSV6ajSYCp9IF3hnrGO4dK9b7ScWpB8IUJYoLPz295oXjxG9SiMsPUGryzpKKAAFpoRDQ3qOxJzZAI1mrZ5jmL7sdrZBtdWKZFgiqjXN4KezLF0Oik+aTDwZKVKKreNVF07Z4imhxT6/nVVk1lrl0AkDjWB75dnRXxL7ff7+ZpjPkTfyYVBG3cYZVADuRD554RumZA0IzGUBpvmrEojP0q1S3OVLqiiduCV16nvOIuJzRykzd48ygZUO0MJf+HF1aADMCXMpGU4dM8bclttL1YtMsJfGb9A173+zTpFFEdOlXE/bC34jmkxzBgHljMGQqbSXId+Yfg37mwvy4boMxcTvv82RHg5OdDWzr8A/TvbSaUlJV5RqWB2jkjXmxAmedISlBBCNv11XlNe8qhay8CwCWfmSMZY6z6ZmldGyH3JHkP/eKmbLIAAYpVNCt3BLRQbstq/62UNHxuxx0+CnmATePx5sl9hBGG6NMPEofYrKyfBsuc+aNRKsPzatZee4sgRp608fxawXgZle4Ippk84n8hH++0zOymSCKqeycpzVMEzZQwaKuEUZ1e628kQryPO8gDXFUD5l3WFQWpoj/iMLUBYjReh6eT0+J8+88pm5DfPVhKmNwXxVs3SCZG+h11p+o8c34pRZyBGf6voA1hcsB79CYyoWKMkPDcaTLPTqpvBaq9xfo+mi0VAFaq+yPyYgEsXUq85FBbJIOa3nhCypAocQMUJHk14q9 FpYNyF3K HJonb84YloNp35J51tr50JibX2JfYRMQqMc8VgLUml5vtLlzhHcKREZReIXwQifJs/652bDfyCORDpbGiVUFvx4RxFw8ub4NfNpcnqY6BRQErUgQ8nvy27SVXd3O9eJYtQnJa/+zWI11utdHWcVr5egYJsaw4R4VdFIpV/euOLYujSZ6NbYc6e515yYgJbWxpEDFCiRc0ljbORA56IgFAWPDBEEcgrWgN49gaUztCGc4h0JMPGcP4pYlrWpFpFjgjmuKsOTxDwMyiSOqOTer5l3mnpl4A8SK4iLDm70P1srzSIBN1tguo8nJsjomOTSIKwu4fB97pCgaLjpk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Ashish Kalra Currently the expectation is that the kernel will call amd_iommu_snp_enable() to perform various checks and set the amd_iommu_snp_en flag that the IOMMU uses to adjust its setup routines to account for additional requirements on hosts where SNP is enabled. This is somewhat fragile as it relies on this call being done prior to IOMMU setup. It is more robust to just do this automatically as part of IOMMU initialization, so rework the code accordingly. There is still a need to export information about whether or not the IOMMU is configured in a manner compatible with SNP, so relocate the existing amd_iommu_snp_en flag so it can be used to convey that information in place of the return code that was previously provided by calls to amd_iommu_snp_enable(). While here, also adjust the kernel messages related to IOMMU SNP enablement for consistency/grammar/clarity. Suggested-by: Borislav Petkov (AMD) Signed-off-by: Ashish Kalra Acked-by: Joerg Roedel Co-developed-by: Michael Roth Signed-off-by: Michael Roth --- arch/x86/include/asm/iommu.h | 1 + drivers/iommu/amd/amd_iommu.h | 1 - drivers/iommu/amd/init.c | 69 ++++++++++++++++------------------- include/linux/amd-iommu.h | 4 -- 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/arch/x86/include/asm/iommu.h b/arch/x86/include/asm/iommu.h index 2fd52b65deac..3be2451e7bc8 100644 --- a/arch/x86/include/asm/iommu.h +++ b/arch/x86/include/asm/iommu.h @@ -10,6 +10,7 @@ extern int force_iommu, no_iommu; extern int iommu_detected; extern int iommu_merge; extern int panic_on_overflow; +extern bool amd_iommu_snp_en; #ifdef CONFIG_SWIOTLB extern bool x86_swiotlb_enable; diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 8b3601f285fd..c970eae2313d 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -164,5 +164,4 @@ void amd_iommu_domain_set_pgtable(struct protection_domain *domain, u64 *root, int mode); struct dev_table_entry *get_dev_table(struct amd_iommu *iommu); -extern bool amd_iommu_snp_en; #endif diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index c83bd0c2a1c9..3a4eeb26d515 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -3221,6 +3221,36 @@ static bool __init detect_ivrs(void) return true; } +static void iommu_snp_enable(void) +{ +#ifdef CONFIG_KVM_AMD_SEV + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return; + /* + * The SNP support requires that IOMMU must be enabled, and is + * not configured in the passthrough mode. + */ + if (no_iommu || iommu_default_passthrough()) { + pr_err("SNP: IOMMU disabled or configured in passthrough mode, SNP cannot be supported.\n"); + return; + } + + amd_iommu_snp_en = check_feature(FEATURE_SNP); + if (!amd_iommu_snp_en) { + pr_err("SNP: IOMMU SNP feature not enabled, SNP cannot be supported.\n"); + return; + } + + pr_info("IOMMU SNP support enabled.\n"); + + /* Enforce IOMMU v1 pagetable when SNP is enabled. */ + if (amd_iommu_pgtable != AMD_IOMMU_V1) { + pr_warn("Forcing use of AMD IOMMU v1 page table due to SNP.\n"); + amd_iommu_pgtable = AMD_IOMMU_V1; + } +#endif +} + /**************************************************************************** * * AMD IOMMU Initialization State Machine @@ -3256,6 +3286,7 @@ static int __init state_next(void) break; case IOMMU_ENABLED: register_syscore_ops(&amd_iommu_syscore_ops); + iommu_snp_enable(); ret = amd_iommu_init_pci(); init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT; break; @@ -3766,41 +3797,3 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true); } - -#ifdef CONFIG_AMD_MEM_ENCRYPT -int amd_iommu_snp_enable(void) -{ - /* - * The SNP support requires that IOMMU must be enabled, and is - * not configured in the passthrough mode. - */ - if (no_iommu || iommu_default_passthrough()) { - pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported"); - return -EINVAL; - } - - /* - * Prevent enabling SNP after IOMMU_ENABLED state because this process - * affect how IOMMU driver sets up data structures and configures - * IOMMU hardware. - */ - if (init_state > IOMMU_ENABLED) { - pr_err("SNP: Too late to enable SNP for IOMMU.\n"); - return -EINVAL; - } - - amd_iommu_snp_en = check_feature(FEATURE_SNP); - if (!amd_iommu_snp_en) - return -EINVAL; - - pr_info("SNP enabled\n"); - - /* Enforce IOMMU v1 pagetable when SNP is enabled. */ - if (amd_iommu_pgtable != AMD_IOMMU_V1) { - pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n"); - amd_iommu_pgtable = AMD_IOMMU_V1; - } - - return 0; -} -#endif diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index dc7ed2f46886..7365be00a795 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -85,8 +85,4 @@ int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value); struct amd_iommu *get_amd_iommu(unsigned int idx); -#ifdef CONFIG_AMD_MEM_ENCRYPT -int amd_iommu_snp_enable(void); -#endif - #endif /* _ASM_X86_AMD_IOMMU_H */ From patchwork Fri Jan 26 04:11: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: 13532066 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27B1FC47422 for ; Fri, 26 Jan 2024 04:47:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC7396B00AB; Thu, 25 Jan 2024 23:47:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A4F5D6B00BA; Thu, 25 Jan 2024 23:47:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A22C6B00B9; Thu, 25 Jan 2024 23:47:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 729206B00A2 for ; Thu, 25 Jan 2024 23:47:57 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B34FC1C1180 for ; Fri, 26 Jan 2024 04:47:56 +0000 (UTC) X-FDA: 81720229752.22.7B64BF7 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2081.outbound.protection.outlook.com [40.107.237.81]) by imf08.hostedemail.com (Postfix) with ESMTP id AE2F1160002 for ; Fri, 26 Jan 2024 04:47:53 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=VDIeUJNs; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.81 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244473; a=rsa-sha256; cv=pass; b=kz8Ls4pZhKEfT1CxgEZvXKva6gIhMyxADwHR2OxPxaVtnYKr8Y0HpmalCVtA2BHmcKTkcW Y670G0WS//IueL8RUAfnhTVJKPzQzOufbrdhE8/IqmQtA0LJzA/NChgeuCln5ONcwkecIv yy7dZIhKvnFiDK3KLEIBYInTxjmWlm4= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=VDIeUJNs; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.81 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244473; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GBxBpWLLh4SxtbEWcLpyIsbUIEYmQcgwhc3PL61stys=; b=HZw6mwlkB8fSxOnrhZfqMEzX8OEpBYoeVhVhTGOtq+JkK7wtgxSFsWH75kf4ZfBdsgTUcP +U0NQ4BkLiRJEGZMYCqpVwlWCwCoSTh6RcYpJo+hVC6NwQcHb+qb0dUFf1iPe/N5dGppm6 Z/W7U0XAwOG7VJFMn225c5YFxi0Tf1w= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RHMe/JkGzdHxP8twb6fChAw8gQvajWedazTVciiRwC7JoH+3kPUb0b/69oMRsc/Lo9DDapZioz3kkoHydWxXwl3VatvHZ+wOX3P/fG2QkGkod0DVhvtCcPBrdqcC//eFsX1JY0m1sHbHAaoBsQUdsWcea9rIktBrIDp3sGsIElVYQuJE2TjcAJ/JpHjzsiQzQDQxbvoc0ySoIELb9T8Owsq+B1SUIjeDFChJDX/Px1Q+nmXiWuZTqH24WeoDWLcha2MFoXOWrhxdnbyBvz1uXQ4JHJy6NUj8ZzSp9JMBqBh/HLvS5Xn0oxQ3fAybgc5pjpku8zE5Z0c3Ehp2ye965g== 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=GBxBpWLLh4SxtbEWcLpyIsbUIEYmQcgwhc3PL61stys=; b=M7lxUyruAbxOfEaRbm3wWqTdmKcjVUUKrXCBz1mriPp4SjhKcP3nvJs9C6t0IVKQ2kZOEsaMb66rdRJfeEwLAVPHjUMgG4SP+Lq94B7Lrf7enqbop20nUnrPakajh0oKUrbCIw1T4mZgGmPkjKT0MgTXC5FHPHUqoplLZesXXtVYLhyr9hyOnq/M6E6BhstAVhdy6rMfWjFCazCoK0HTZAFJnmGm+7eKDUFPQN2b2zmmXyZmQ/4GRf5MfwC/UARsREQU9MMi14bFvmP4Jx4T4+e1DBOa4M+szX+foemUTfI0G8hprKLlj1vYD+0fZ+xnVltkWc6fvba6FfGq7QEwJQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=GBxBpWLLh4SxtbEWcLpyIsbUIEYmQcgwhc3PL61stys=; b=VDIeUJNsV+jYIZ0DOYtyphKyRTSxGDpZtRiyrjA44hqdiketwB+3ZJ7fY0KxIwmXwaaMbZQb/W4Lw12+NKdgiT/5yjaTcRLeTRHMgCo1ElFCISGrkQu4xGiseZLBXdCB3Gn1QwZTJ3aRjHAf0ZzkTXDRuFX5vizBOGL7MOF1yTg= Received: from DM6PR08CA0035.namprd08.prod.outlook.com (2603:10b6:5:80::48) by PH7PR12MB6906.namprd12.prod.outlook.com (2603:10b6:510:1b8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.32; Fri, 26 Jan 2024 04:47:47 +0000 Received: from DS2PEPF00003439.namprd02.prod.outlook.com (2603:10b6:5:80:cafe::44) by DM6PR08CA0035.outlook.office365.com (2603:10b6:5:80::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22 via Frontend Transport; Fri, 26 Jan 2024 04:47:47 +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 DS2PEPF00003439.mail.protection.outlook.com (10.167.18.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:47:47 +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.34; Thu, 25 Jan 2024 22:47:45 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 04/25] x86/sev: Add the host SEV-SNP initialization support Date: Thu, 25 Jan 2024 22:11:04 -0600 Message-ID: <20240126041126.1927228-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003439:EE_|PH7PR12MB6906:EE_ X-MS-Office365-Filtering-Correlation-Id: 2eabc104-c699-4330-dead-08dc1e29f180 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M5cjxA2CEZ+fq5KJEv3P05gNOK2/idubEfRpL6we+PfGzsmg3gdlygs+7l3tBA85ObKR+VN20csw6tMEdIpoEJ4YhlXSESuUuFERtTfuG0S1y5OHo/lHJXh7z//nvaTyg6RuChctiIH98RrOPvez7gMV/LtxNenFp/fMQaSbORpbGPrZcXQOktYjAegaHt3zjXb4yPLtOJXGPdvNwdNkVdAeTDh9IfX4G03VvmFswx+SkVtQCV3Wgfa5I2HWVnXkIBVDcDqylZvRz/BAhcQT3kyu5E7pA9qNxgByf0m04XJSME+la6vlmmq3SSx4eTCK8Xn9RWWFi4Zbkn84Kjo6tlxrYPgE/P3HSj61jb7A2rT+OHHglrz0HxWMhC19/72u9JDxGxZQANG9Xq3yo7VNVo08G+l8xcYORuvaL7ENyF3Znr5MOOVv5zNqVJQVG0bBZFYNQeys3+1cj87Lxxu/fZ4afJYme/0NFZXmj9J/IFe6LBCFvP2pgh72S31nUaTGrxmNwtP/qVW+D07B+fikFBSuCXCvsAU0rnl0RNvZj2nMsuIgG176IeYP5wqI4MZHnq8s059d5Pprf/9m4g0Br4qwfgRobOPPMnSFzam9mJGD/b0dHEteT3RQ/51S9C6QnWJqIug99eNdDOg9IFlUbGDLusHhuSHn/oab0NjPfByV5bqVMveCVbIlaX6hpe5SVsKGDucHR2Vxo9GgeBU1CNGKbr2H6Rc5+7q5ceod5963H6YxNydPstlb0vshHLRaJOuGtL+cJ4rI82TYTDlFBg== 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)(4636009)(396003)(136003)(39860400002)(346002)(376002)(230922051799003)(1800799012)(64100799003)(82310400011)(451199024)(186009)(46966006)(36840700001)(40470700004)(30864003)(40480700001)(82740400003)(40460700003)(6666004)(36860700001)(478600001)(356005)(7416002)(2906002)(5660300002)(7406005)(81166007)(86362001)(1076003)(8676002)(2616005)(41300700001)(6916009)(316002)(44832011)(47076005)(54906003)(70586007)(26005)(16526019)(426003)(4326008)(36756003)(336012)(83380400001)(8936002)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:47:47.3233 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2eabc104-c699-4330-dead-08dc1e29f180 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: DS2PEPF00003439.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6906 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: AE2F1160002 X-Stat-Signature: uqdoqiubuqqmptgo5qw9z9jdx69tg1is X-Rspam-User: X-HE-Tag: 1706244473-27005 X-HE-Meta: U2FsdGVkX18hE0zMfNlkLg4skzjODMLpMOPv8ODPoVSuwHxM50Mhwc3PoQlaG4Z7nzok7YiyyUit6+S01FO+ztYxPf+oiypUooMo27lBXcm6qW+E5GMe6ma9EJXlkxmJ6oKIAHR2+9kxlWqs6R/nwjQiAFTAkdReOAQkj0kRoiu1Vjb+LN9WqWf+qKZsfVuIkZ+Ues2eHwbZPzwjZt1I2ud/ugaKp4/r8gVAISKS7LqOwUP1H0q2FJbb9lnt7ww4T8ilyrWkdZ6u9GcTSUNlSGqxIx+wHk6RlwCclHnN3xS+/Fn968FRC3uNMZ8sHViGZYdNxUxVvBd29P3EozNpWKTbT9ZA1cs0DbqLWrMtRmZ7JopU1//FbFEjX2pmhpwZO0FEoriQck9LnLR6Xfo6DznEFM0JyDz/3MBKrWMvMfXJfO+9sOU45w6WPiDc1IwLOuokBJ6yoXbPCcE6JvY16xwNgEq4eg8+mZMijFFP9yR90TXWgZReNwrXkeO5a5ndw9F7LMk0QPL6lT6iLNIHMPTh5rfp/9qfsPLopC6mnunqMmvLw+WMj5+QUiovDrJ/BzuY9fMPiPgQQy6yLZ43cptcBbuHiVtvIzJklhurJfNyDQ9otLV/lV9Lc/J0jVzNzO2AXgYYtXDFQ4I0SLK/hjQ9cymEOpQJVCI+eOB9e07aaciALYbVbxJCm+QxEv1n10i8k5pWJzn0gEllQLH5wCpfSHjCR0zn/k6n1eUs1RR93qDQmXn/tfeJctcAzAIzQdXktJrbPbiRATsZBlAvzlR/O64Qvg6V9Q8Lkq+Mbqi+WrCXx0xDqB5hhFsqW+k6QxfgS+h06mcIW2IuCQJL5sxTkSrdZLvUEe/HY/UaOQdUhlGlMObbM7l/O37rwF3jSzX1J6znwg9fqapG/GYGJCvd9pW+mtyRl1qBSONPOeN4z5Mk8zDsFwYpdHbIw2/K2D/897UQbV+IWYvKFDU ZSZJm7ll 9dVZvf9M92B+vqRecQy+8Nejiz6klAMZm3EXUQT3fCyzOkkTjqV9W5jHtUNIyQBQgnh8YqmYaPYldzxN5b/xkWJs85/GObE3IsCjTvA/CwYVg79/Qe6IWai6M1KQvUUKMSrhUoHReMfxB31U27IMjimVCKCfTsNwDkgfT0lFjvpzQ8rDxWQX2SosG/eT/Xf+mVrogE960KL9mdhz6/NaB9dmXNUfhrLBgDET/pt7LjlAZgmtXxFmmimytKay+jSQ4eM8IczAmrKazmJapQGyiLkuQdAZrfSiPxSZlgFZddXcjxTnDCWE7l+cVIGypVdgYUPqt7YGpZ0bjWO4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. The APM Volume 2 section on Secure Nested Paging (SEV-SNP) details a number of steps needed to detect/enable SEV-SNP and RMP table support on the host: - Detect SEV-SNP support based on CPUID bit - Initialize the RMP table memory reported by the RMP base/end MSR registers and configure IOMMU to be compatible with RMP access restrictions - Set the MtrrFixDramModEn bit in SYSCFG MSR - Set the SecureNestedPagingEn and VMPLEn bits in the SYSCFG MSR - Configure IOMMU RMP table entry format is non-architectural and it can vary by processor. It is defined by the PPR document for each respective CPU family. Restrict SNP support to CPU models/families which are compatible with the current RMP table entry format to guard against any undefined behavior when running on other system types. Future models/support will handle this through an architectural mechanism to allow for broader compatibility. SNP host code depends on CONFIG_KVM_AMD_SEV config flag which may be enabled even when CONFIG_AMD_MEM_ENCRYPT isn't set, so update the SNP-specific IOMMU helpers used here to rely on CONFIG_KVM_AMD_SEV instead of CONFIG_AMD_MEM_ENCRYPT. Signed-off-by: Brijesh Singh Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Co-developed-by: Tom Lendacky Signed-off-by: Tom Lendacky Co-developed-by: Borislav Petkov (AMD) Signed-off-by: Borislav Petkov (AMD) Co-developed-by: Michael Roth Signed-off-by: Michael Roth --- arch/x86/Kbuild | 2 + arch/x86/include/asm/msr-index.h | 11 +- arch/x86/include/asm/sev.h | 6 + arch/x86/kernel/cpu/amd.c | 16 +++ arch/x86/virt/svm/Makefile | 3 + arch/x86/virt/svm/sev.c | 216 +++++++++++++++++++++++++++++++ 6 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 arch/x86/virt/svm/Makefile create mode 100644 arch/x86/virt/svm/sev.c diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index 5a83da703e87..6a1f36df6a18 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -28,5 +28,7 @@ obj-y += net/ obj-$(CONFIG_KEXEC_FILE) += purgatory/ +obj-y += virt/svm/ + # for cleaning subdir- += boot tools diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index f1bd7b91b3c6..f482bc6a5ae7 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -599,6 +599,8 @@ #define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT) #define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT) #define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133 /* SNP feature bits enabled by the hypervisor */ #define MSR_AMD64_SNP_VTOM BIT_ULL(3) @@ -708,8 +710,15 @@ #define MSR_K8_TOP_MEM1 0xc001001a #define MSR_K8_TOP_MEM2 0xc001001d #define MSR_AMD64_SYSCFG 0xc0010010 -#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 +#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 #define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_BIT) + #define MSR_K8_INT_PENDING_MSG 0xc0010055 /* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000 diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 5b4a1ce3d368..1f59d8ba9776 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -243,4 +243,10 @@ static inline u64 snp_get_unsupported_features(u64 status) { return 0; } static inline u64 sev_get_status(void) { return 0; } #endif +#ifdef CONFIG_KVM_AMD_SEV +bool snp_probe_rmptable_info(void); +#else +static inline bool snp_probe_rmptable_info(void) { return false; } +#endif + #endif diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 79153e9b92b5..f48c51640c65 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -20,6 +20,7 @@ #include #include #include +#include #ifdef CONFIG_X86_64 # include @@ -584,6 +585,21 @@ static void bsp_init_amd(struct cpuinfo_x86 *c) break; } + if (cpu_has(c, X86_FEATURE_SEV_SNP)) { + /* + * RMP table entry format is not architectural and it can vary by processor + * and is defined by the per-processor PPR. Restrict SNP support on the + * known CPU model and family for which the RMP table entry format is + * currently defined for. + */ + if (!boot_cpu_has(X86_FEATURE_ZEN3) && + !boot_cpu_has(X86_FEATURE_ZEN4) && + !boot_cpu_has(X86_FEATURE_ZEN5)) + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + else if (!snp_probe_rmptable_info()) + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + } + return; warn: diff --git a/arch/x86/virt/svm/Makefile b/arch/x86/virt/svm/Makefile new file mode 100644 index 000000000000..ef2a31bdcc70 --- /dev/null +++ b/arch/x86/virt/svm/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_KVM_AMD_SEV) += sev.o diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c new file mode 100644 index 000000000000..575a9ff046cb --- /dev/null +++ b/arch/x86/virt/svm/sev.c @@ -0,0 +1,216 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD SVM-SEV Host Support. + * + * Copyright (C) 2023 Advanced Micro Devices, Inc. + * + * Author: Ashish Kalra + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The RMP entry format is not architectural. The format is defined in PPR + * Family 19h Model 01h, Rev B1 processor. + */ +struct rmpentry { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9, + gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + u64 rsvd3; +} __packed; + +/* + * The first 16KB from the RMP_BASE is used by the processor for the + * bookkeeping, the range needs to be added during the RMP entry lookup. + */ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 + +static u64 probed_rmp_base, probed_rmp_size; +static struct rmpentry *rmptable __ro_after_init; +static u64 rmptable_max_pfn __ro_after_init; + +#undef pr_fmt +#define pr_fmt(fmt) "SEV-SNP: " fmt + +static int __mfd_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_MFDM; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void mfd_enable(void *arg) +{ + __mfd_enable(smp_processor_id()); +} + +static int __snp_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_SNP_EN; + val |= MSR_AMD64_SYSCFG_SNP_VMPL_EN; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void snp_enable(void *arg) +{ + __snp_enable(smp_processor_id()); +} + +#define RMP_ADDR_MASK GENMASK_ULL(51, 13) + +bool snp_probe_rmptable_info(void) +{ + u64 max_rmp_pfn, calc_rmp_sz, rmp_sz, rmp_base, rmp_end; + + rdmsrl(MSR_AMD64_RMP_BASE, rmp_base); + rdmsrl(MSR_AMD64_RMP_END, rmp_end); + + if (!(rmp_base & RMP_ADDR_MASK) || !(rmp_end & RMP_ADDR_MASK)) { + pr_err("Memory for the RMP table has not been reserved by BIOS\n"); + return false; + } + + if (rmp_base > rmp_end) { + pr_err("RMP configuration not valid: base=%#llx, end=%#llx\n", rmp_base, rmp_end); + return false; + } + + rmp_sz = rmp_end - rmp_base + 1; + + /* + * Calculate the amount the memory that must be reserved by the BIOS to + * address the whole RAM, including the bookkeeping area. The RMP itself + * must also be covered. + */ + max_rmp_pfn = max_pfn; + if (PHYS_PFN(rmp_end) > max_pfn) + max_rmp_pfn = PHYS_PFN(rmp_end); + + calc_rmp_sz = (max_rmp_pfn << 4) + RMPTABLE_CPU_BOOKKEEPING_SZ; + + if (calc_rmp_sz > rmp_sz) { + pr_err("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n", + calc_rmp_sz, rmp_sz); + return false; + } + + probed_rmp_base = rmp_base; + probed_rmp_size = rmp_sz; + + pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n", + probed_rmp_base, probed_rmp_base + probed_rmp_size - 1); + + return true; +} + +/* + * Do the necessary preparations which are verified by the firmware as + * described in the SNP_INIT_EX firmware command description in the SNP + * firmware ABI spec. + */ +static int __init snp_rmptable_init(void) +{ + void *rmptable_start; + u64 rmptable_size; + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + if (!amd_iommu_snp_en) + return 0; + + if (!probed_rmp_size) + goto nosnp; + + rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB); + if (!rmptable_start) { + pr_err("Failed to map RMP table\n"); + return 1; + } + + /* + * Check if SEV-SNP is already enabled, this can happen in case of + * kexec boot. + */ + rdmsrl(MSR_AMD64_SYSCFG, val); + if (val & MSR_AMD64_SYSCFG_SNP_EN) + goto skip_enable; + + memset(rmptable_start, 0, probed_rmp_size); + + /* Flush the caches to ensure that data is written before SNP is enabled. */ + wbinvd_on_all_cpus(); + + /* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP. */ + on_each_cpu(mfd_enable, NULL, 1); + + on_each_cpu(snp_enable, NULL, 1); + +skip_enable: + rmptable_start += RMPTABLE_CPU_BOOKKEEPING_SZ; + rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ; + + rmptable = (struct rmpentry *)rmptable_start; + rmptable_max_pfn = rmptable_size / sizeof(struct rmpentry) - 1; + + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + + return 0; + +nosnp: + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + return -ENOSYS; +} + +/* + * This must be called after the IOMMU has been initialized. + */ +device_initcall(snp_rmptable_init); From patchwork Fri Jan 26 04:11: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: 13532067 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97EDCC47DDB for ; Fri, 26 Jan 2024 04:48:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 289D66B00BB; Thu, 25 Jan 2024 23:48:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 212566B00BC; Thu, 25 Jan 2024 23:48:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 064FD6B00BD; Thu, 25 Jan 2024 23:48:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E17136B00BB for ; Thu, 25 Jan 2024 23:48:14 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BBDA5C045F for ; Fri, 26 Jan 2024 04:48:14 +0000 (UTC) X-FDA: 81720230508.19.9669802 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2055.outbound.protection.outlook.com [40.107.101.55]) by imf21.hostedemail.com (Postfix) with ESMTP id BAFD51C000D for ; Fri, 26 Jan 2024 04:48:11 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GGH8dCyR; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.55 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244491; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tvsYf0n2GxElwqDzXGtfzBBHQRJyP8zKv4HrR1SkNBs=; b=tWcjZVovixJ20JnuKi+rHHvzXfHy3RpMDHA4Fyjj4f58ShUZ9ONBrsemUJ68nQb/FH/Arf hAYlxiT8FDbRFWU2+6M8wPak4EQlz8fhsI3mbu6x9KsZCKUKuuvh6ScYrbVF7LCl9WFK/G RwrHcAMf/14Zp/mJsQXpOML2oAiuWPM= ARC-Authentication-Results: i=2; imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GGH8dCyR; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.101.55 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244491; a=rsa-sha256; cv=pass; b=6wb7rDMNePfxrgDMlJ5Mg6x+EzgCgoLxyzjJuq1R0x9RwzNfO+XS7nApypqlYbwOLkoyB5 bjMFN5HsLCsMicyMjs8Y7qIzX7n8DMr5rB5xgUrognFn6iFBM8grd7ZVhIXBRPdZmlF1ur KNDYcP2PHuApalLdIFPW6uZYMHtVV5w= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G64ZnOrSQROppcr6lqYkGKhzr0wev46JbWZseHfCdyix2mJeMj48ppMvN3gQwpG2ySfhxteo5r3lWjkGBTVpb37FQPejRXF/lp0X3uWGYZL8db50lyOrJnLXM7lGuUSMmyQsyCCviIVUAv/PduReYq3BCWAIAjKpZnf6a6iM1t8kzX6LAqxkb3WPuz/KCtszWYB64lp8k4PPe84C/bejCUqY59fmum6CGHG+61esfd+MGgKXrIwJtFTJvaIWddbdbkGPK9kbjqXT92X2QKRii1kjrDIosQdw4l2l5go6mp0DnNZBw05jJEsokD6CxFyNmRO2ZVxd+v8jhdfkN6nGJA== 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=tvsYf0n2GxElwqDzXGtfzBBHQRJyP8zKv4HrR1SkNBs=; b=Ggf88ZlPHV2fsrYYhg61blL9Cq7WdJNZfAAbpe1Zigzdim7+t6CLekvRXuMVguxe3TtQIQlc688bLmGGw0R130GgGekz8vG7vczIuY1SXjuU8llc3zY+vO8WRUEP6yb9fQ16zCU6GmkO9JPIbt+cwYxgj2b5Gz8e/QvyOY5T5H7NfuhBrE1ZXd1QiiW3B2D8za7o0+PDbw31Vvrbom2ZLHfP6wDz9QbLnmqF87GnLeV99pvnMvMnS9Sdenx5WD6lzZpHtaiDMqsWpo71Oex4zIcdeul8xV9TlPjKVPCsMt4FHmseB2KErcsvDhIBywrhbakqxyjudabtPv7DEjkVOw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=tvsYf0n2GxElwqDzXGtfzBBHQRJyP8zKv4HrR1SkNBs=; b=GGH8dCyRDU6+gR1YN3f3N6MrlqPL3NNTs8JFYvJBRl3RcmBZi1QYjrOtbjx/8z7Fa+fKLfm8Rx8j9e3OXjqaULiXaabX77d3iajLbxcPkzh4S6swk2NBdHBHFDyeZAwnAT4QBKChrwhlxy1DemcYtQSC2ItdSHLMAV96fa0Yooo= Received: from DS7PR05CA0020.namprd05.prod.outlook.com (2603:10b6:5:3b9::25) by IA1PR12MB8517.namprd12.prod.outlook.com (2603:10b6:208:449::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:48:08 +0000 Received: from DS2PEPF0000343F.namprd02.prod.outlook.com (2603:10b6:5:3b9:cafe::c6) by DS7PR05CA0020.outlook.office365.com (2603:10b6:5:3b9::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.10 via Frontend Transport; Fri, 26 Jan 2024 04:48: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 DS2PEPF0000343F.mail.protection.outlook.com (10.167.18.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:48:07 +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.34; Thu, 25 Jan 2024 22:48:07 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Jeremi Piotrowski Subject: [PATCH v2 05/25] x86/mtrr: Don't print errors if MtrrFixDramModEn is set when SNP enabled Date: Thu, 25 Jan 2024 22:11:05 -0600 Message-ID: <20240126041126.1927228-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343F:EE_|IA1PR12MB8517:EE_ X-MS-Office365-Filtering-Correlation-Id: 5e26abfa-c292-41dc-b97e-08dc1e29fdd0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qENmzQTe6sSqX1jQlR889VffXysS4HfjmVtEL7fThyLTV3e+6vQiG2wkT2Z+YIe5Q25rIsklg+mB4Kr2w+V4BCwsg6QJFv9WVMzXTl1zjVUjqev9ixpV6Z5jbYXonv972TJ01QOrPO9vLdcSLeZIv5z2NxfnZRYGgy4LiElCCoG7PZc5f0svx8dTyUlm1zv5lwNOlWVkwJOeJRmLiq19XK5zUzVYOqKLG/ZqUrI+WZPEJE8sV54dcg5lArc9HE18VtFRVQz3XkDeUm37jga9PGMlH8YTT8nJP1aYX6G/HAyvDkI1xFaZbNyYH8ldhUbXK5s5A/nqjc2KTS4SFE0vPBac3KVUXGT9y6lYXWfAkX9thKDhRbnTn9WxhL72aT8uHcjQvCGl8YtgwL4HGeBIbaLBx7LE1+7ETshDZ4BBRAcH+696iEfwC9TXMMUWtkg+pFa0TgVjq6NqFROmLIXZJ1wY0CKBSNoQQgzeulde8rre1Ll4DBEKiHDD2ktdqgxdgt9fTlhRMeGZvWBiioeuVFeBlH7gdJ19xG8KuwUCrsiaI2gzcXKXt4jjPrlst8uEIOi5LWCy8U3OQjQl+sfMxW0/JqLU7bjX7XTx1A9RGDx8XRLqczsWF4uJhbUGKftWbRJ2BLqKdiwYkCJCYLQluh6VATdPhd8tr93zqT4pYPrtMV54v3dxc99iVsADB2yEARCGcdiFxq0F0FZfC5NjcfbOzlmzK14dRk5xXWiOivyityNFkSquf5V0ntvmIVwfzVpMClifTwwMg5ZwWnjeKg== 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)(4636009)(136003)(346002)(376002)(39860400002)(396003)(230922051799003)(1800799012)(186009)(64100799003)(451199024)(82310400011)(36840700001)(46966006)(40470700004)(41300700001)(83380400001)(47076005)(16526019)(2616005)(426003)(336012)(26005)(1076003)(36860700001)(81166007)(4326008)(5660300002)(8676002)(7406005)(7416002)(2906002)(478600001)(8936002)(966005)(356005)(82740400003)(54906003)(44832011)(6916009)(70586007)(316002)(70206006)(86362001)(36756003)(40480700001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:48:07.9793 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5e26abfa-c292-41dc-b97e-08dc1e29fdd0 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: DS2PEPF0000343F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8517 X-Rspamd-Queue-Id: BAFD51C000D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: whc1c9sn8bkn1yedocxm3fy166mmi3dt X-HE-Tag: 1706244491-937109 X-HE-Meta: U2FsdGVkX1+IDkJlY+65C7/bmXmauArP2cQLXafb72v8PFIg8In0znvy5W+Qs4qoTDIVg3MM2bIzveyeWP253+GyUL8GGuZ/PFs6EGxdvKwt0NxLTE1gf1yNtBBKMuTBEVsZDpkpRQ4MQHzPabOqvn7qI47MRfoRj0G0AJkP1/nESsb0VjtJIN8LdfRZ391EnvGcb+/UPQf6kc4WbngEcY9fhwVofIzcUZBT61d8SXJRh+Qv46ULLhegMAD31iE2CfpedlHc5FpAaLLbynsQ5CwiqZ4OoBfqBLjya7rC8Su27q9hp7j0NgQvELAIiV6uGykE9xgNktqtZts3q1FzORcYCLLG4inbVOr/rr6KzvkkgefceTUiF+htS870+SXEfekjOIUfgTHnknwrYwIphlS7V7mC7j7eWE8byBw7BWWE1RG9MnwLnRfdSJh6xtwUne+yP0tdSl284gV95TM1P4YX0LW0Ywm2PyxU4OS0G3xf0UwqU3pBPg9DR+F+PYevnu+AROueonAmR0PSTeIHQs2nx3y/6HqT9PjPex0MRl0IvTLng6oQ85FFBAeSTi/+ypyJtIOjp7gOkCrbIi7CEDfVSa0Czk9pn7ujVW3liaDtyD1h862AlnpAT9RotAJ7Cnj/XycTQQH+f2VhKEHPnIQMJTstbSsxSoJizaMJtgycDQ/iG12iWTPej1X1jEj7l6+/LPeCbe4cBaz7KswR5X4v1pNKzvhbz/xfpdWVURikuyJaU8Pu6ZrCsKLTTI2ZfhQmiT3/jOQKwvS7tHer1ix73Bc4jDwnxLrDyQvIMsQgE3aBnQ7scscLVJhIdmWBh8djNOC5ihIO7Vaj4YDh2zEXYSpwDVLxwNQ/8GFv4XY4r0MRnCHePlKbGfqjkOGhHFAcgZCo9QCKgxJf3VS8wjCPq+AlogcFHep584CcnZbKpod6QtC9rqudlPtfGplLLJuV7ZfQsX3jbovWiO6 NXhJV+R1 fRDY26QicR5RCdrph/3wW1oEBiKx/BUPExJZm+MiEReE98+JhBqd5xp8z0GMoMG+ykNqYE0XrwE7bo16bH/t3ZZoVt/eyY8VR+++kCsZhWGXRbDI664WTL0o/PPk2/0NSLkQSHyltzxbt7/vVtrehYQrakPhIaz3QvvB3XxGjwRNCKOeHaREfBho7IkHrYmMWjPlSVdkS+nzeVP88bift5yPCs+53elW4XRTyydKOQ8m9bbrIA8qPpE1938I0GdLkmeBSq8twB8vGR+ORF+b7nftIb3pNJ9i6QliLaUNj6fncjVx4aRIhDNA+hL2NtDoxNUABrL3rSIIj5nij7Fs9yS6v4sJUa2nDOPWWGhyWtNpmhRW6dpe3so4cwOjin8bpdw3iBLU9qczGNmiGduFEIDhm5JTO8pLEBLxF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Ashish Kalra SNP enabled platforms require the MtrrFixDramModeEn bit to be set across all CPUs when SNP is enabled. Therefore, don't print error messages when MtrrFixDramModeEn is set when bringing CPUs online. Reported-by: Jeremi Piotrowski Closes: https://lore.kernel.org/kvm/68b2d6bf-bce7-47f9-bebb-2652cc923ff9@linux.microsoft.com/ Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- arch/x86/kernel/cpu/mtrr/generic.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/generic.c index d3524778a545..422a4ddc2ab7 100644 --- a/arch/x86/kernel/cpu/mtrr/generic.c +++ b/arch/x86/kernel/cpu/mtrr/generic.c @@ -108,6 +108,9 @@ static inline void k8_check_syscfg_dram_mod_en(void) (boot_cpu_data.x86 >= 0x0f))) return; + if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return; + rdmsr(MSR_AMD64_SYSCFG, lo, hi); if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) { pr_err(FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]" From patchwork Fri Jan 26 04:11: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: 13532068 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A8A0C47422 for ; Fri, 26 Jan 2024 04:48:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B2176B00BD; Thu, 25 Jan 2024 23:48:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 23A306B00BE; Thu, 25 Jan 2024 23:48:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03E636B00BF; Thu, 25 Jan 2024 23:48:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E1EDD6B00BD for ; Thu, 25 Jan 2024 23:48:34 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AEBE0A0348 for ; Fri, 26 Jan 2024 04:48:34 +0000 (UTC) X-FDA: 81720231348.20.3F7AF92 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2069.outbound.protection.outlook.com [40.107.96.69]) by imf01.hostedemail.com (Postfix) with ESMTP id E02C440007 for ; Fri, 26 Jan 2024 04:48:31 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="F62Oz/Lm"; spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.69 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244512; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3YVWcXzm+jfjuUhzMCYYR4/BYYox6gPlIYPWdG/TcLU=; b=wp8YeKbmKN2ZoeVtbCWBqUV9GIOdlO+qbrLlGz+o+XxEtsWpnV7AMb1PmGw/NDYTy8lvMA dZC+vDT/F16nuU0Vlnbeqd+HU+KC3mSvYItpTJ4J72WFkAIuZVPuNawPG8BW8YHQcxeMHS GxOr59BHt3MhNn8AYPCB91pT5oYgh78= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244512; a=rsa-sha256; cv=pass; b=J0qgySePtjItjuUfMG3y6MeLY7A9Y8YetV+37vdPtQ6h9TnWuaWKTNkFSZRc2UHd9EBetm 1C6K05UiFGfqlfbvI5TQJfpy7g0i0eNapuUtA2lcGB4+T6dFpjiQdl8EVHshEtay1c7PXh uzRarbQfCoaUSX+dmjxbZF3cXs+j29U= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="F62Oz/Lm"; spf=pass (imf01.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.96.69 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KJoKw6rhuXmJuUwVqt5/5wDop+qAHhnejFnBwOAp75+7vK0ll7t1gtxrkFo2UrTpMgwPVOBk0FI3hNP4cCbSCJ/V1IBWgHjaGmS233Q7sNSLqYP3j3bDvnoncbKKvOKSlsLMeY+Iz0g7gpv/YE018Ndne44hd3I3ooXBdqKZjU/p3T1zP38THaLVwXyaBNP+g9mn1KUx8W34jFHeJwGC8W0+5VZlld14H8k/BFLnGQJrA8iZh0+TnYsoBvgf2xepNYmGEVQe8d535YpTdzOUUud1gjHcdE6QkJZFhmIv6obbJwRAbLiF3VsZN5V5ExD1Eg5uxRMNUFLDkQ4itYeT3w== 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=3YVWcXzm+jfjuUhzMCYYR4/BYYox6gPlIYPWdG/TcLU=; b=MpapJ8nr231R0e31PMYJyNuWsMCzoU2Pl7e8OMIWk3JT/QwwY23iQSxLxmF7AD9vablVQFW5gmeP6Ze/Oz1uCIqfp+UOCkZzhfitKW6/rL/SvRSmlHUeMGh2exokyFw0nl96noJ2l1PDhcuzlLgpXihMHRRpi1VMqsxE6omnPJ25j+BDNOWIvlaqnHDa5FIasJZstWI2jipgtm61DvNpAXqLgluj3cT09Q2CjAKUO4iLEjIcaR3LahW0KFgHOODwyh/ne/wtgKf87UMFD00RlgU35cSzCdE/KcJWh7fuGRGvnsOIp9W9eJ4tt3h84fZa4IZiMLOkacRvUHqnjOwwyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=3YVWcXzm+jfjuUhzMCYYR4/BYYox6gPlIYPWdG/TcLU=; b=F62Oz/LmNGvjnxumsssaQ3zEm10Vkm8eXwXuGEncMPDM9tsZXbwfiOYa6WRYwjhxiFXI/XC477ziE+Lcp2pNe81ica4rJNO/2LCw+gwDSoH+YfeLQFmf6Xv3+cGvInohGjERSLvVcdsoiHg4H//g/FQgCi0iF/AsoU6aIvf9jaE= Received: from CH0PR08CA0012.namprd08.prod.outlook.com (2603:10b6:610:33::17) by DM6PR12MB4925.namprd12.prod.outlook.com (2603:10b6:5:1b7::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:48:28 +0000 Received: from DS2PEPF0000343C.namprd02.prod.outlook.com (2603:10b6:610:33:cafe::e5) by CH0PR08CA0012.outlook.office365.com (2603:10b6:610:33::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:48:28 +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 DS2PEPF0000343C.mail.protection.outlook.com (10.167.18.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:48: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.34; Thu, 25 Jan 2024 22:48:28 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 06/25] x86/sev: Add RMP entry lookup helpers Date: Thu, 25 Jan 2024 22:11:06 -0600 Message-ID: <20240126041126.1927228-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343C:EE_|DM6PR12MB4925:EE_ X-MS-Office365-Filtering-Correlation-Id: a81e195e-16cf-4947-e7ae-08dc1e2a0a32 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OEtPTSsE7CU9mO1D/RCzrp+9NQFIwleRRdOKv+/shvDy4Rz7x6u0M4Lt598UE5fLL/7FB/aiTChVpZcLFvWBXsfukuPiRkN55//nOfhm3yCT5AABLwFkEWWc57u9qB2Yqmo7gYjRoMLJu/3a1gOfiBxomcZfRAUmI45+QQBU/SZ+8bXF/9T5jZclzYebXLyAmVrQlJzgqXjBK5mMu+EABoaomq8F4V4NJpYma81ONQ8DTTk1pYy/WsBtKIlyiqkkDWSIMOkBhUyfpl+/ZwEAa3nbXhlCMyqDTlSXssLzrVL3GM3wtJuWM3weysXIlei3zSDXeqVLJbixajkN9yhv7pnxyfSe2RKyyJ6uQIpfdjGLF9JciE/Rh5rd4u2roqLpPchE1LC80IZs0rRNsrighzwKm0h8WGuGEUA4m8Yutt9O024/20DT1bOF8vmae0nfOqgSqWi/ZUWyNn9FBda19j7FDD5uWjvGRoMdAPzJO3Sa/G1ZcAEhtQ+bB4MQisPFV1VA/o8mQBit+2Llbk/jBRGEtx/FYLy+7mLEPfYg18GGt+4XyMaeuWywoLBj8KD+Fh+lUUSh9TYU/JIiurtl13uck0Otef80Nsbgo9xOUJMAydF7T5D0VQFnNulnW/8FjX2TWJTg9lIcPlPmSNavU9ntLJIpyIo6eo5RWyUjN3Bj/uC4Z1M3w+N3R7WY74mdgZu0NYfK/3dI2XYT0/Oa9nwnr+DP4MYFROuHo6TKLA0xw+rB0MHUr2g4TSG4ho0eNDRQQuZVhdXtyKMXRMzU65n0OuH5yhlsTC2YpX71AtQ= 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)(4636009)(346002)(376002)(39860400002)(136003)(396003)(230922051799003)(1800799012)(186009)(451199024)(64100799003)(82310400011)(46966006)(36840700001)(40470700004)(83380400001)(47076005)(1076003)(2616005)(26005)(36860700001)(316002)(4326008)(82740400003)(426003)(336012)(70586007)(5660300002)(44832011)(8936002)(8676002)(41300700001)(70206006)(7406005)(7416002)(478600001)(6666004)(6916009)(2906002)(54906003)(36756003)(86362001)(356005)(81166007)(16526019)(40460700003)(40480700001)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:48:28.7554 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a81e195e-16cf-4947-e7ae-08dc1e2a0a32 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: DS2PEPF0000343C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4925 X-Rspamd-Queue-Id: E02C440007 X-Rspam-User: X-Stat-Signature: hq4ttup6zimo3xw14ripqxqfrqb7ywx4 X-Rspamd-Server: rspam03 X-HE-Tag: 1706244511-937945 X-HE-Meta: U2FsdGVkX19EQYGuLjBK4FKy5jaRwvgYSfWece0r7q8h5J9VN2X5WWeaVzmUfjUdsDPyOvwu1LPqIMmxwqNv6cJRJuqzWKpP3GhVSFSHROlrIV5maBerEzIz9mp+7Qp5wLIdifJLT8bFo7PS7+h7ggYJMyrUaFvrwtMaCKhh04QHXDrreW7YnO7G9eIMyRG0SAZ6eYQ8n7Ev7AgWVJtMMJ7Yz3+yZSWxn2cL2vYdipZXh5qslnMXsvZ7KMUIiwf5qmUjcOkIizUsgn2EDhfxYHGnKsLyKXnDBh9zW5o6sRpM1NftUM+2q5YpJNX6MWJ1wIAPYRCdAVKMDfkjTJyGe5puEECWoplrAKmDWV6D9fJaIccMwhNGh+QuiBYr2c8Rsi/ihCG5x2SDBpng1TI2tUrGn7zI/7zpCwoNL8uqOpGNsDKj8csDomLRuJiVgFaDNq144jyjpo4rHATPtVcqkboxuQQw+NIzo+weBM97L80wKUeIK8yZej2cYSps36ox6UcKH8uCCgCjIsTLjYRWRuryTxDfSCmBlJSS1Rpmo78e2YCYCcX9S1+zcPT/P3AA5yZf0wgOmqrv13NcfENvTt3SWLUNomeGouY9+QJ2LM3LHff1GFb/VKRszZluEFfJUW1BIvty3zPofcmK4N9GBSFhZvE8DCy9aapTQ5la6sYFzzNcYWAVoQK52mVsx66oJBuscb4y30SJT44a7BCBjOsLcRhum5SR4qVA6SSKZCiU4qz9Au1CRxP0IWkZ7cZsJd2A4gb7fq4GhPF6MGIXchTJy9STBYE4DyCUDG39SVLVKY/5g6V3hglKNhX3AFSQPyXou67IWKN7byegvJruDJ2kCeKli4U8tJJsb4iHi6UOWc5xyuIORIVLo48/dh9JTyVpdk5RevLmHVR4hDWGZstKciF8xeI5Q+mXSvRV4rHMeyltvGmJDYwF7qmQXNMQsCeWPLEv3G+CqQOgEGq +SQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Add a helper that can be used to access information contained in the RMP entry corresponding to a particular PFN. This will be needed to make decisions on how to handle setting up mappings in the NPT in response to guest page-faults and handling things like cleaning up pages and setting them back to the default hypervisor-owned state when they are no longer being used for private data. Signed-off-by: Brijesh Singh Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra [mdr: separate 'assigned' indicator from return code, and simplify function signatures for various helpers] Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 3 +++ arch/x86/virt/svm/sev.c | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 1f59d8ba9776..01ce61b283a3 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -90,6 +90,7 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* RMP page size */ #define RMP_PG_SIZE_4K 0 #define RMP_PG_SIZE_2M 1 +#define RMP_TO_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) #define RMPADJUST_VMSA_PAGE_BIT BIT(16) @@ -245,8 +246,10 @@ static inline u64 sev_get_status(void) { return 0; } #ifdef CONFIG_KVM_AMD_SEV bool snp_probe_rmptable_info(void); +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); #else static inline bool snp_probe_rmptable_info(void) { return false; } +static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 575a9ff046cb..7669b2ff0ec7 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -53,6 +53,9 @@ struct rmpentry { */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +/* Mask to apply to a PFN to get the first PFN of a 2MB page */ +#define PFN_PMD_MASK GENMASK_ULL(63, PMD_SHIFT - PAGE_SHIFT) + static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; @@ -214,3 +217,49 @@ static int __init snp_rmptable_init(void) * This must be called after the IOMMU has been initialized. */ device_initcall(snp_rmptable_init); + +static struct rmpentry *get_rmpentry(u64 pfn) +{ + if (WARN_ON_ONCE(pfn > rmptable_max_pfn)) + return ERR_PTR(-EFAULT); + + return &rmptable[pfn]; +} + +static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) +{ + struct rmpentry *large_entry, *entry; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return ERR_PTR(-ENODEV); + + entry = get_rmpentry(pfn); + if (IS_ERR(entry)) + return entry; + + /* + * Find the authoritative RMP entry for a PFN. This can be either a 4K + * RMP entry or a special large RMP entry that is authoritative for a + * whole 2M area. + */ + large_entry = get_rmpentry(pfn & PFN_PMD_MASK); + if (IS_ERR(large_entry)) + return large_entry; + + *level = RMP_TO_PG_LEVEL(large_entry->pagesize); + + return entry; +} + +int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) +{ + struct rmpentry *e; + + e = __snp_lookup_rmpentry(pfn, level); + if (IS_ERR(e)) + return PTR_ERR(e); + + *assigned = !!e->assigned; + return 0; +} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); From patchwork Fri Jan 26 04:11: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: 13532087 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF4D0C47422 for ; Fri, 26 Jan 2024 04:48:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F91E8D0006; Thu, 25 Jan 2024 23:48:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 680CD8D0005; Thu, 25 Jan 2024 23:48:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D3C18D0006; Thu, 25 Jan 2024 23:48:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 38FCB8D0005 for ; Thu, 25 Jan 2024 23:48:55 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 15D6AA0201 for ; Fri, 26 Jan 2024 04:48:55 +0000 (UTC) X-FDA: 81720232230.01.0A6D29A Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2084.outbound.protection.outlook.com [40.107.212.84]) by imf24.hostedemail.com (Postfix) with ESMTP id 35EC2180003 for ; Fri, 26 Jan 2024 04:48:52 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uNwkMi+D; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.84 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244532; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JBK8P9DZYAHFE8/v3KeAVGaPleccg4tTZLLxJ7l1gD4=; b=ayD4G2TKmTd5W1rrljhR+jzC8HDuiwQeshL68HCaNFyxRlPHxw3CCnNiNEgWtbbPBGoYJZ CgxiBVdB1b/KdpHAbd5cOuTIwDIPdPNj3Y+1yZYC3Q3Y+dkSoGJl5lpvmIu2mvhvO0pEKF dUGa5K3TyhpMjRCgdzUdmIFKjH78hg0= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uNwkMi+D; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf24.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.84 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244532; a=rsa-sha256; cv=pass; b=iyvQVI6jtBqxKmIt//u+xEQzqbLUf0IVpdTO1kZtii9ipazzfqWMjHsEHV3P4xWhpDb1eP 4ReLhxGo0lghyvwemHzhcEc3BQq/YM4K4JOaYp7ZBtTputiSUScme2U7KklXx6sEwFAGUc 7h/VPn44Oejwo2RAwa/pO2ohVmXxBQU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S1POwzsHhno8drn/FXjHLl/L1T8OGoe/kifWwnR0ukQW5glBGDYM0PI9wwMhsnIcq1DMg4gcL2oF1M3Lxvedmg+UMqOwelOBvnsN0DM6tf5AjnUO45y1vaIs/HGlDAYaGgXM1xbCvKr5yflAnQ4E9Zg2uHV/mwZoHYH/ly6VgMu/YI42RDGZBYb6+YcAJEPb3tnx2rs4AykLXjNVEECWVgJiH2LLxZZT1h9oVkdtkgyfCpcvaQu1WL0f/JmZ1F9NBwLx3Ej0CiRNOHFDCBfP+WWHMusn1HWj1Bb2vylaD8s/JVHdQZL/JoHpo4sMmUQxzlurBGSKNmYDiDQFDzPvwQ== 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=JBK8P9DZYAHFE8/v3KeAVGaPleccg4tTZLLxJ7l1gD4=; b=jXAnAzKGqVZJhRryPN9cIk/zpXjKnpVmjiW89M2gtl8zbOBGxVWtuDb9AbpnayrbLqWlw/9LXxbT1f9fBMFOTFBd235T58FhuL7vf//ZVHcG8ER7O1UVKa6jHuF979mJ5cslV5m6wgCdNy768/0lJ1MJlP4B47oJi2SA1HvAxUAskZDOg75XK+zwuRGL2KFmA3KMA4TXusWJBSIJ7TvUkbNITvxpXf924Xb4miGzNT+uKKywiijq4YHfS9MW3coPIRc1gm1UBEa7j+g43iyhA4yrzSsu0WaVo2LWoNc+k0a2DOIfUjpX74UCTNFmBmGoleklF+Jqhtj1cUOqfKieHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=JBK8P9DZYAHFE8/v3KeAVGaPleccg4tTZLLxJ7l1gD4=; b=uNwkMi+DNNYTdvwFhNah8LpkcYzH/hWieIOF+elUJ0Q43LNhVQZKwlb+VQst+iuYT6Kfkeg1lu6rqJcrQGID26ozzZM3got1VA1pP/38GGq/pKSimruSox2nU4tY/3uvTg3R1j42eeB1TDlNgvjMFwri82mWC3/qXnlsnllEcr4= Received: from DM6PR03CA0031.namprd03.prod.outlook.com (2603:10b6:5:40::44) by IA0PR12MB8422.namprd12.prod.outlook.com (2603:10b6:208:3de::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.24; Fri, 26 Jan 2024 04:48:49 +0000 Received: from DS2PEPF0000343B.namprd02.prod.outlook.com (2603:10b6:5:40:cafe::c) by DM6PR03CA0031.outlook.office365.com (2603:10b6:5:40::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.33 via Frontend Transport; Fri, 26 Jan 2024 04:48:49 +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 DS2PEPF0000343B.mail.protection.outlook.com (10.167.18.38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:48:49 +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.34; Thu, 25 Jan 2024 22:48:48 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 07/25] x86/fault: Add helper for dumping RMP entries Date: Thu, 25 Jan 2024 22:11:07 -0600 Message-ID: <20240126041126.1927228-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343B:EE_|IA0PR12MB8422:EE_ X-MS-Office365-Filtering-Correlation-Id: 6a74fbb3-cc10-4ac6-f082-08dc1e2a1683 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 77W5NKw1K0s0M5MPCewhZZDELukIR19/JflObok4Q5xcHZjZH1lkLShKSY0btuNA//uGplIpW0V6zuVNmFiPb0C6cfAHqlg525V4W1GkwMPX/xI5lV+Ylc9f0XNSdGxhSwEwxFnByHlMaJWxu+UoHkedcdmTACCJmICbTJJiu4EcdfnN7Lp5/z4OjRqSlFBzNK0QJtJIEJUz5+flKlX4neSS6UIsmm3QzCyYEF5l1dsv7isFvM/2DQngw+K/VIBeK7SBeD8Sk0zbHMOePmdayGxYxAeCaWKPduHijshvQ4b3QbLJJthYVr5TrTxmg4dq0HQlRHxu1/1naKYxldVaJqrstxbNv0b/89n54MiH0D+cgw+ntxwbneAdET14GvU5gUpJa+pLXimQgh47yuIVu39lBMTvYz+7TJa8/Cg5jh9kxeoT3UaAWwRRDcydNZebnE3Xss+9De4aj70EV8ayHLICHtbzbNbUrRINbW7j8FykNaWtFZSfyaZtR/W8fK0CVK9Aoy/bC+S6cZ/YTps7IBFE/wOTRahmcW+olAPVrflInuKiNOdRbVs37geQck/2Ry54UqMVtOT0fmA+nO9dzeOF0uK/Jx/jksl5HDExwnBnuJXRw01Nq2hxmmG8sBcCg8qqIY83TbFtpFGS98ZA2yhkuLx/XI8HoOrHdMAPlpa2+nR4ZQbeo8vl1qGLHhkJaj27NkOwds7a5cC/exwcSXTIm3mmHVDGhmBpl3zHfayMDE7uuQ38qK3tEXaNCLkGJ4YY+21CBc7grB+FfaPBtQ== 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)(4636009)(376002)(346002)(396003)(39860400002)(136003)(230922051799003)(451199024)(1800799012)(82310400011)(186009)(64100799003)(36840700001)(40470700004)(46966006)(41300700001)(83380400001)(336012)(426003)(26005)(1076003)(16526019)(8676002)(2616005)(82740400003)(36860700001)(81166007)(356005)(7406005)(4326008)(44832011)(8936002)(5660300002)(47076005)(316002)(478600001)(2906002)(6666004)(54906003)(7416002)(70206006)(6916009)(70586007)(36756003)(86362001)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:48:49.4191 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6a74fbb3-cc10-4ac6-f082-08dc1e2a1683 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: DS2PEPF0000343B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8422 X-Rspamd-Queue-Id: 35EC2180003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: e7idxbhc3fb3zgeusz9d8fib3hatyhj6 X-HE-Tag: 1706244532-405687 X-HE-Meta: U2FsdGVkX19Hv3I+sRWMRE9jw7PjYE8I0onKzmxKlCNtQi/WAdcXUaR17f6SS85jqJpqr+1oyfrgcr0r4i3qG/tuPxcsYJDLJcwZTCX5ghJPyjUO15fjzrPE/p9gpM/6gbT4c2qMXcABY8NVyX7ACNOERdKkx7cTHm2jDC+rhVoK57EnN+GwOUSt/VwgmSpelfO5/ykUnwVURX8KVTHDNY7JRwtJj3qDiVIMxMW03MmSc201F2fN/UBSyPr6+XrFRkZRuuzjKUVQK+yYGy8TDmsyJWDPN1OXYpu4kxn+UxcZixBRScg9O3+MNphyZ1QBV6/CXBS3rTfH5X9xDQw/RCPZzMWrImdYq7LxQ9RtiZcKNq6OShIARiqZY5fL67W5hhC5Lqp/tPVtbWVcvyP6GJmSMYCvP6WwKbBkcTT3MZRZ3KrdvSIfcmlLE02Avd7+3MFpscn6Luci0YFARhHojYT0E3hSU+4AVVyWbkd90pzDJn6pxQt0SWdKZzdUBLThWy5F4fdXoQpSJrK0ztUgYGhHdPCnkGGPvEiaEC7ySVUSEGXcTVzXU91TWsS+Ep73MAX2gBLQLmKM4r3GTMcMrWQDcPtmED+Li0EWncAgAxBthihlMi/sEuKu9YIBT1LTHEvfIGhOh1k+tLHijAZ8ICSBsfi376fFKXE8QlCPvvIRukcBgV6iXlkc4fiu2k/O50/YtJ0vvp1dUsXCi79ZhcLuIn51I4LC0XCWLn+vyri50AHgBAngjHzKbccWXZ002YhCzTwuWocPAa2DiMXgo0vuw1U0y50g1Wo3edh5rw/i8w/NE1O+BPg9NrOb1fiOLDpNiZ/xQKua88d4OMNR0Sy9uS+VQMBALYwK4uD6yyEDW2jngkp4ye0H4x9o52fjROFBS/msP6SzbY7/wO/1TtxIwgCcCykhpKuINzTmEKUqMTwsHKvgdy5U/E1e8PMc3hfpUXpP7CJSoVq1R8G J3v5n7U4 d/xUwBSIPzp/Rg+O2L1PNMaannpq1eXfwsFEaOqNlDscdvvl4U8TGxIq+1qypXFwdCgodDqNHrzHLp3W5p5sSBCtmnKVnJVRM2bxAB5MqW/HTXml0XI4dIsZFyD/wnF0VaoEJ5TV9CkaakmFUDA6ThCSlZRX95+z9VL/KJGd+5tAmlHtDiW8t2EpXIq6HiHsfpmjtooYvYCcgnd2cHJ1/cFeWaKX9iPzku1oSpzKCfq32cB7sqii6BhNwc+UDxibEVAPoW86geLEvHuExmN5PDrkZcW5n4xMo690lcLiy0eal1PwiNRcQfcnoDKV5AjDUwO7hfA5X0Dk1b1BZ7b7t1dYfi+yv1F/wE4ZDijVLDIvO/6YvorgK1Z2C0w== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh This information will be useful for debugging things like page faults due to RMP access violations and RMPUPDATE failures. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: move helper to standalone patch, rework dump logic as suggested by Boris ] Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 2 + arch/x86/virt/svm/sev.c | 99 ++++++++++++++++++++++++++++++++++---- 2 files changed, 91 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 01ce61b283a3..2c53e3de0b71 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -247,9 +247,11 @@ static inline u64 sev_get_status(void) { return 0; } #ifdef CONFIG_KVM_AMD_SEV bool snp_probe_rmptable_info(void); int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); +void snp_dump_hva_rmpentry(unsigned long address); #else static inline bool snp_probe_rmptable_info(void) { return false; } static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } +static inline void snp_dump_hva_rmpentry(unsigned long address) {} #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 7669b2ff0ec7..c74266e039b2 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -35,16 +35,21 @@ * Family 19h Model 01h, Rev B1 processor. */ struct rmpentry { - u64 assigned : 1, - pagesize : 1, - immutable : 1, - rsvd1 : 9, - gpa : 39, - asid : 10, - vmsa : 1, - validated : 1, - rsvd2 : 1; - u64 rsvd3; + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9, + gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + }; + u64 lo; + }; + u64 hi; } __packed; /* @@ -263,3 +268,77 @@ int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) return 0; } EXPORT_SYMBOL_GPL(snp_lookup_rmpentry); + +/* + * Dump the raw RMP entry for a particular PFN. These bits are documented in the + * PPR for a particular CPU model and provide useful information about how a + * particular PFN is being utilized by the kernel/firmware at the time certain + * unexpected events occur, such as RMP faults. + */ +static void dump_rmpentry(u64 pfn) +{ + u64 pfn_i, pfn_end; + struct rmpentry *e; + int level; + + e = __snp_lookup_rmpentry(pfn, &level); + if (IS_ERR(e)) { + pr_err("Failed to read RMP entry for PFN 0x%llx, error %ld\n", + pfn, PTR_ERR(e)); + return; + } + + if (e->assigned) { + pr_info("PFN 0x%llx, RMP entry: [0x%016llx - 0x%016llx]\n", + pfn, e->lo, e->hi); + return; + } + + /* + * If the RMP entry for a particular PFN is not in an assigned state, + * then it is sometimes useful to get an idea of whether or not any RMP + * entries for other PFNs within the same 2MB region are assigned, since + * those too can affect the ability to access a particular PFN in + * certain situations, such as when the PFN is being accessed via a 2MB + * mapping in the host page table. + */ + pfn_i = ALIGN_DOWN(pfn, PTRS_PER_PMD); + pfn_end = pfn_i + PTRS_PER_PMD; + + pr_info("PFN 0x%llx unassigned, dumping non-zero entries in 2M PFN region: [0x%llx - 0x%llx]\n", + pfn, pfn_i, pfn_end); + + while (pfn_i < pfn_end) { + e = __snp_lookup_rmpentry(pfn_i, &level); + if (IS_ERR(e)) { + pr_err("Error %ld reading RMP entry for PFN 0x%llx\n", + PTR_ERR(e), pfn_i); + pfn_i++; + continue; + } + + if (e->lo || e->hi) + pr_info("PFN: 0x%llx, [0x%016llx - 0x%016llx]\n", pfn_i, e->lo, e->hi); + pfn_i++; + } +} + +void snp_dump_hva_rmpentry(unsigned long hva) +{ + unsigned long paddr; + unsigned int level; + pgd_t *pgd; + pte_t *pte; + + pgd = __va(read_cr3_pa()); + pgd += pgd_index(hva); + pte = lookup_address_in_pgd(pgd, hva, &level); + + if (!pte) { + pr_err("Can't dump RMP entry for HVA %lx: no PTE/PFN found\n", hva); + return; + } + + paddr = PFN_PHYS(pte_pfn(*pte)) | (hva & ~page_level_mask(level)); + dump_rmpentry(PHYS_PFN(paddr)); +} From patchwork Fri Jan 26 04:11: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: 13532088 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0D25C47422 for ; Fri, 26 Jan 2024 04:49:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FD328D0007; Thu, 25 Jan 2024 23:49:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 680138D0005; Thu, 25 Jan 2024 23:49:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AC578D0007; Thu, 25 Jan 2024 23:49:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 35E258D0005 for ; Thu, 25 Jan 2024 23:49:27 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 01BBE120542 for ; Fri, 26 Jan 2024 04:49:26 +0000 (UTC) X-FDA: 81720233574.21.857CEF3 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2085.outbound.protection.outlook.com [40.107.244.85]) by imf08.hostedemail.com (Postfix) with ESMTP id 0F5A3160013 for ; Fri, 26 Jan 2024 04:49:23 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tkqtAq22; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244564; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3U3JEpITMWrdJUCfljL+TeerAiuU7ufrDQlHJ3pkGqI=; b=nEAEvM6AJfbgQQ3aKb+H4JJ0IysXJyrfyGr/f28lIsv2b19IiAWHD3javyHNlI48FAqyzP JdrQZADtQj4dJlE5bO371gBkmVGA+SvTmQZs0Fxgo2t9y2Rhdeo5V2ZBlU0k9I+SuCxfbG 1VNU2Q6J9DA0HPlqPdxaq6VFWDtB200= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tkqtAq22; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244564; a=rsa-sha256; cv=pass; b=ovComU1SwqweypL9wOfA0gZ3yapoAY0739okeNneHJx5kZZVFuSvOEeXy53hHQCQtNyAs9 qbHbQwKXYs+qYLtkvUFzeCKgt/fU0+WzEW+Nm5t2M8K16zCc5tEH6tNZayn+/RHThmdor0 PYUdH9HB93hJmxc4gV48EfFibIgsGos= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=as2DK2Bfajk95Lg0jcbXcWM8yXeMmXNwYy5xj9qcrO07OWxS9uHMcZdgboZHY/QiEMjnUUXSVMk3SsuxubPGXT4ikoWuhXNzL52WHLQCy+i+QAI6lNx6jJMK/+d1iLogM0h2YVTPxIjmC1TH2jpxLJ2lREvwLIhq59lU0Xiw1QvJvmltZwXsSrbSLOXFp98/Q6tv6QajGH2mN0Z13NEhPKHgp7RevsYTv6JsDupXqrtTQNotoSFQAE85yWXp9T2YWyWUjP5ogo9A6lMqdYuoMRQAUg1bB+KaNIo82KE2Bp2sqdWhaqei7rrSm2XKfH2bVVvoZ5CBE0IjsU9olsp57A== 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=3U3JEpITMWrdJUCfljL+TeerAiuU7ufrDQlHJ3pkGqI=; b=jlsDZ89QTE3Ca71M7pcDugcg3MOYVtUJELp3u687xd+XAqROX3J6V7B8juFc2ztutc/dcInvZiSbtk6Rbi6ymgDQvZud4+QynlOJ08m2Q0fNQ5odXnjo6q2edYvYH1DxyRVTUebdPGwmZZcAZX7mQk/FGZxIlQyS0/GsJU6jTxaR1zn5O9ca+31U3vaLig6vI8MohgYeJhAqmGWQhW9xZHsC/EMc2Vsw14iAghgJpqQiFLrUEftJ3vkonrHBhLSuyJGwbL7WoGyVl3nF4eSqUj4A1xURGSoFXaiLn5yVwONxnlP+s2v/HKVuY21T6OXtOngIiiYoHgpj0I6pVk03uw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=3U3JEpITMWrdJUCfljL+TeerAiuU7ufrDQlHJ3pkGqI=; b=tkqtAq22rRfxOOf6dhvzdxc4kK1pXOStAtMkFGUfvf7XU2rfJxvcPcJu0ldQKTeElET5IhHc0T9mzyoGmPvEXfzLovU+rZB6tjDwUwhcIXhtnLbuoP1or1YqVPR+K1mArVqFGNwFcc7oPrTk0ZRREHKHYhdyBjDTrqZqkXahaKs= Received: from CH5PR02CA0001.namprd02.prod.outlook.com (2603:10b6:610:1ed::17) by DM4PR12MB5152.namprd12.prod.outlook.com (2603:10b6:5:393::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:49:18 +0000 Received: from DS2PEPF00003440.namprd02.prod.outlook.com (2603:10b6:610:1ed:cafe::c9) by CH5PR02CA0001.outlook.office365.com (2603:10b6:610:1ed::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 04:49: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 DS2PEPF00003440.mail.protection.outlook.com (10.167.18.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:49: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.34; Thu, 25 Jan 2024 22:49:14 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Dave Hansen Subject: [PATCH v2 08/25] x86/traps: Define RMP violation #PF error code Date: Thu, 25 Jan 2024 22:11:08 -0600 Message-ID: <20240126041126.1927228-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003440:EE_|DM4PR12MB5152:EE_ X-MS-Office365-Filtering-Correlation-Id: 6473a4cb-ba7d-4781-7266-08dc1e2a27b6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EMlA1XpWP772hJ+SIi8tdaA8NpZT4Q/4+zomJ+PnNHgCfBRXcb5myQ/MfxYziLCilGju1KYixxBhtl1yBxFWwj50ToqUSDXXkVxlyPbk2pUU8zU5C23T8Uq5Xm8H++HHRJz8jw/7RkKpvH24KqYpFkPLttLM/bikHVm1v7x58CWJPy3JZtUTlyYRpagWlYuvahPqQnboHKpr62kCikhRx90BVZ5pl03TSYjku83faJOE9L0QJsJ5Zcmky/qA0h0M9JY28PPOOJcoQFLW73Hixo93nWPQDs5R+BFiygCBe0chwXIXH5BiBb63I8040HzusKahKKAudQZurGl8b6IiHzhWIY9Wvdr5+TrXNPFWk/zG0rxhRDlMmd+cc73TkBZd0dH1A46ISsB7/kvD3YWOk5zOW8FHpVe00jvAEUZzvSNTCm/urPKZ0BZNFdWbI6V/yIdYHmFrfBapcOTDbKd1kp8mkFXd8H1RzN5aGPdzZqANV+saMNwZ9JO5kqLfKxiOdErnUDL+pKRPUouLeEfTXCWwv0rylqGQo2YeoYqohfckfTI8ddSmSZQXnWg1WVbPGx0iKLZY49fg4QVGm8VeBZO6uFE8HQK63yLMtS+pbbfBF4N2I7/5HJPA3dgwrKKC2OAUVmp0+PVXgWTID3jDWk5H3QB3XLkR/m+KMkzzv62zkYpoWieCyI0Dci2a5yTX/0xl9OEtgyS1Yzwa54cQAPtMzlQ1ecFJYq45GZMyiSQ8PCUkuTQynuqFF+vrf5N081Nqo9qa12pUZ6PaMLt9tA== 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)(4636009)(376002)(346002)(136003)(39860400002)(396003)(230922051799003)(1800799012)(186009)(64100799003)(451199024)(82310400011)(36840700001)(40470700004)(46966006)(26005)(16526019)(2616005)(1076003)(6666004)(83380400001)(336012)(426003)(8936002)(8676002)(4326008)(44832011)(5660300002)(478600001)(6916009)(54906003)(316002)(70206006)(70586007)(86362001)(356005)(36860700001)(82740400003)(81166007)(47076005)(41300700001)(7416002)(40480700001)(40460700003)(36756003)(2906002)(7406005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:49:18.2714 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6473a4cb-ba7d-4781-7266-08dc1e2a27b6 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: DS2PEPF00003440.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5152 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0F5A3160013 X-Stat-Signature: j5di4k4w5h7zyyotnw3cmpyw5s7ny9c1 X-HE-Tag: 1706244563-62722 X-HE-Meta: U2FsdGVkX1/xKF/BJbMOd7RMeZCMHaPzQqJwHQUdor5fEmCkzXsM4WUMddI7ULtIkBbUjW4jtHdhjfhObG0f6E6LmslIoCSVwBPhvKF8OrB2r4Z+9JXiMpQHIuTTEN8lxdnpfXfhZSAw5mjyiX/Lv7PKUCScF099HW6W2hOUqyu4/LmwdxMTsGzZzZMh2Xy+O3BtTyZIwPwW35lrus7mlbrhYr+1qmpvDO2RCabuDNJFH6iZcrTBLrQmTZNNywo1m2nhe2K3jZS8rKVW6vkZr6GsM/dc+YzKroM1tIsnYbi1C680LixZHawcMb+YGtFSl8vANrvVQT6FF8vhD4TD9K6f3lwyL5mLKKTrgZs6vJGA0ry25GgY/z9PlqIBjRFVNTQiJf6BbkMnmY4e89ccyQs65AbnlELBg+8GnlS3pexOeTdDLafP//ToIetoTZLw5cL5VnB/u0rKhJf6UwGBfNIYVo9gXYpHUQNKpjX48KvEkITrpeHGwQKNqR3Yrah7tq289g8dlplpiV4R7/QJ7FxggEj9CgPCU6U3kiuuwWYd9KW6NRC8bgAOoXDXscZ0lw+lnZOQCfrSdSzRvksO+Ka4/z1BmZv+yWt0m2Wpuhr6Gb0vtxzOsoNSViSc7A/Ir5Ek9W/bih6rhUb+tm0aITYv8qFrmrLbT3wOxACJRphe7XhL7clzQUD3xGsbEtrdrP8QqOd34fzKGaq4qAWdpwk4bqkkqDaF1E4slz1LLgXtxsW1G3hVI+SN42NlX7YaZnwnfCMDlx8rloEfIw3KVQpdy+RIAZMok7+jsIKrDptJJY9h/Rmcnm6yA6H8hgnWV4xonT8T04eeVIWgsFLEd1OZZzt7y/VDZ1ZpMVFwVXpKVyvDJHGy214DDxLbuyGOYUq2F7XUOpVc4Jjkv6+bOdti/bYOXlaOeu6uereQjxBv4nYqTqxEL8nwAUmCGTfRp44ujMj9M+HPY9FXspU ZLlZqpHp WvFcLlMieQXuuVuoyTxSW2/iVuAq3Z1AjlV2zvY0Evey/s0HWyhDeZ+S20YEGyAW6jDGDW1QZteSv51tXl5doVwU8h1jFaMd+z9kyvp3q73Rb/IcisgnvWpaIu93AiLcQ2EAygO4r7tKJWP4d8tOWL31mWIgQuh194HkRo1lXsqsylBExCyTPphGJiBB9vVMn+tlgPACeGA3vd62jYUT1YPrlcPAOlEVuwUpyKSMBk31+zP218/chnqKxu2jmOieAR0OkDFDB5comyn48TB2hE9/iIPRiS9napaKY72pXMahSoVBn8dvM56y0W2Q3dkg8g+IcchrqosTCegIh5+02zQKsesUXqkXB5Nz+jPC+rVY0OSI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation. While at it, use the BIT() macro. Signed-off-by: Brijesh Singh Signed-off by: Ashish Kalra Acked-by: Dave Hansen Signed-off-by: Michael Roth --- arch/x86/include/asm/trap_pf.h | 20 ++++++++++++-------- arch/x86/mm/fault.c | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index afa524325e55..7fc4db774dd6 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -2,6 +2,8 @@ #ifndef _ASM_X86_TRAP_PF_H #define _ASM_X86_TRAP_PF_H +#include /* BIT() macro */ + /* * Page fault error code bits: * @@ -13,16 +15,18 @@ * bit 5 == 1: protection keys block access * bit 6 == 1: shadow stack access fault * bit 15 == 1: SGX MMU page-fault + * bit 31 == 1: fault was due to RMP violation */ enum x86_pf_error_code { - X86_PF_PROT = 1 << 0, - X86_PF_WRITE = 1 << 1, - X86_PF_USER = 1 << 2, - X86_PF_RSVD = 1 << 3, - X86_PF_INSTR = 1 << 4, - X86_PF_PK = 1 << 5, - X86_PF_SHSTK = 1 << 6, - X86_PF_SGX = 1 << 15, + X86_PF_PROT = BIT(0), + X86_PF_WRITE = BIT(1), + X86_PF_USER = BIT(2), + X86_PF_RSVD = BIT(3), + X86_PF_INSTR = BIT(4), + X86_PF_PK = BIT(5), + X86_PF_SHSTK = BIT(6), + X86_PF_SGX = BIT(15), + X86_PF_RMP = BIT(31), }; #endif /* _ASM_X86_TRAP_PF_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 679b09cfe241..8805e2e20df6 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -547,6 +547,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad !(error_code & X86_PF_PROT) ? "not-present page" : (error_code & X86_PF_RSVD) ? "reserved bit violation" : (error_code & X86_PF_PK) ? "protection keys violation" : + (error_code & X86_PF_RMP) ? "RMP violation" : "permissions violation"); if (!(error_code & X86_PF_USER) && user_mode(regs)) { From patchwork Fri Jan 26 04:11: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: 13532021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DC38C47422 for ; Fri, 26 Jan 2024 04:40:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0DAF6B0075; Thu, 25 Jan 2024 23:40:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CBA996B0080; Thu, 25 Jan 2024 23:40:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B81F56B0081; Thu, 25 Jan 2024 23:40:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A86F56B0075 for ; Thu, 25 Jan 2024 23:40:34 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 65136120564 for ; Fri, 26 Jan 2024 04:40:34 +0000 (UTC) X-FDA: 81720211188.06.5F33D1B Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2082.outbound.protection.outlook.com [40.107.244.82]) by imf22.hostedemail.com (Postfix) with ESMTP id 33C2DC0014 for ; Fri, 26 Jan 2024 04:40:30 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UnTD0ec+; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf22.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244031; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=K9Q0YgQlJrDtGCtegnCZIrYCnCzwv6QRWqtQLd+3r9M=; b=iNF6ODUaufyiYHfJO6M6AHqSqsJw8j4hRFkKRK4TrcM5z9hHqGYQmrxgF79T0xGKBdUbAh yND2CNLtGRys1rICtTgqY3dkTtmplj8KhAM4n0mR9J9mIDi68VYP2q/w7FOPv5PFCvIrbI 8OpHhQ2tO/gIs3lltzXJuG35U6sU33o= ARC-Authentication-Results: i=2; imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UnTD0ec+; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf22.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244031; a=rsa-sha256; cv=pass; b=f1I6iRFiKi9XLY+grod4zeW57DMxYY1I8x5t6dJuY8vj3Tdp32oxDhsVMdM19U0v6p3TBi eQQ62uksDnFcgVpZ2UHUFhwYwRd8t1sif9JHpOA4EiYaWIo9uh6t83I0XGY84IxfSP0olb +o//t9KKgLTHMfvrHI0zf/onfbfJf0Q= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oBqmHv6Evhm163A2xVGw51jnDKJk7oBcDZaIQl9U5YSYmuYfTF9L0O0wsxOuRA/IzhIWk3EbRbLoUWnJZazz79AaOvr94maFNm1IpHLz7u8PCfYieG0+hzIqXUvSJLU2o5FS3GYE4++RlWM1wWM3pNr0+ycomj/n3DDc+sMEpHJVsNlcS3EI2jBDqAzLbqZn1gD03H4WX9GlVJPCI+GVwnhANfxcV0dlL28pNCD9FWJiq+2U284vfQbNXmEjf8g8j7kL9mE+65BoIg/HcmnYRIehljjzlgKSyd3Do9c1VcOrdJekgdMcWYi4lGwrYmbT5HWGHaOqhJuhT2yQaXffcw== 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=K9Q0YgQlJrDtGCtegnCZIrYCnCzwv6QRWqtQLd+3r9M=; b=Z4EfU93VeA/O/Xi05eRTU98/IJ8dtcrDGNaG+E/vcMULC0SQbX6mPej5mDseJ/dckNH8rl1wn7dtBlMv4RYeeMyFUs8+QVanPfZpUWf4PMLS9pqq/lsgHDDDd+OPmBNYmeDyzCh5DeRBU5cqDkq9284MQEbX2K8hHBDrfEvy10LklQWGAC+/hGCxC9eGDAu2Mp+vekT6WwRZmJT0NPnLwGzBCklnLgki8TGdZFBbPjE9q4hPJ2iwM+a7wLZPLM2OrvTy1JMPxVlIK29dOOxgbk62Rsqga6TcnshE/4C43eszTAzZoPlS7OS2M9X9OsA5wI1NH1HtxuNYjOsP7odbaw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=K9Q0YgQlJrDtGCtegnCZIrYCnCzwv6QRWqtQLd+3r9M=; b=UnTD0ec+zrLSbweX3i1tEktgdOf7oPNPtl8fOyr3q13qqxe+k/ovVGTEMfI2HdMnU+pD7RjE2xcHqSigzGXvd4BjeTQMwOBkpR0oKcHzw9TMvOCbf52We7+9/MuvqTBVzqxiJ6PxshXelpv113QFDBZLQ2jvFsHDXd6n8YhcTmg= Received: from PH8PR07CA0033.namprd07.prod.outlook.com (2603:10b6:510:2cf::20) by DM4PR12MB5277.namprd12.prod.outlook.com (2603:10b6:5:390::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:40:26 +0000 Received: from SN1PEPF000252A0.namprd05.prod.outlook.com (2603:10b6:510:2cf:cafe::8a) by PH8PR07CA0033.outlook.office365.com (2603:10b6:510:2cf::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:40:26 +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 SN1PEPF000252A0.mail.protection.outlook.com (10.167.242.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:40:25 +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.34; Thu, 25 Jan 2024 22:40:23 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 09/25] x86/fault: Dump RMP table information when RMP page faults occur Date: Thu, 25 Jan 2024 22:11:09 -0600 Message-ID: <20240126041126.1927228-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000252A0:EE_|DM4PR12MB5277:EE_ X-MS-Office365-Filtering-Correlation-Id: 63cd7b46-ef6d-4ba9-bb9e-08dc1e28ea4d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5Nx1Cy9Ebf2u7ipr6PQVd3Hk4Vt9DgDqJ4l5zQAD7nViLYOd2ejLcTdz7T5HDw0WDjG7DGBvqPavv8CqB/VO5Hd2CAo24i3v8GIecOkcpRj3GTeprjV5l47B31OPaHzpRmiaS63vG9DF93icgPpYC/6Z8UOfiwgkCfthfrOQZ3y8f1P1xAVHggKQr/h8KXXxNXTcQCWL/cI9KD7SDFMkZjGh1+GV8041TVPC20RsPNGaSLS2yh1lFSp4XxexqFztWKgi0XefwUvRlI9YNoBfFCwvjzz5b9edIeXZytYcX0tM07FwVkp33r9b1DxDEAOa0rMjsFmb7nKhKtBn8h4HhE1N0nAJjYiAo61DZ2GL9zFMP6VxW6xYvWfVN/xVlvTlHSR7dNodWfZSGrKPSr9856jSVu+GtwPKXmESlZapTEdfzDOM8x49mTjfUhT4Jmw+p00ugezzMIPbFEnLJzf9Xc7T3ccMxFE4CRWxTbYeWt0W8ABJQhY2ddVGs0ymQ7celxIwCjLd477zftipvHs8vdledlYJ1fwcbKhqywqhzSmSIjT5TYov6mTr8HA9mALbAMLUSc6pUTYQVMpYnPxIroNM3wsT+l4QLexnqfTDlIrsmmpgmqsEzaOz9D06QHMTUxAAJRU4EVEYRlfsVV5R3Y3jmMwPokBvmfQuwYkEf7CQFO4ZXZ3LC4WdrA2du3wP9p6JSjJUKtruli2Sn0hzLcIAmdL3oJ9MkVNzHYMtX2f88uqbl66JoBnCKYl6+wYCPWX9lOKakRMWpAZq4KVduA== 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)(4636009)(396003)(346002)(376002)(136003)(39860400002)(230922051799003)(1800799012)(451199024)(82310400011)(64100799003)(186009)(40470700004)(36840700001)(46966006)(36756003)(426003)(1076003)(26005)(16526019)(2616005)(6666004)(336012)(5660300002)(7406005)(7416002)(47076005)(2906002)(44832011)(8676002)(316002)(41300700001)(478600001)(6916009)(54906003)(70206006)(36860700001)(8936002)(70586007)(4326008)(82740400003)(86362001)(81166007)(356005)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:40:25.7630 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 63cd7b46-ef6d-4ba9-bb9e-08dc1e28ea4d 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: SN1PEPF000252A0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5277 X-Rspamd-Queue-Id: 33C2DC0014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 9tapj7jha6wjfx6bo133ku5fzk1h7rk8 X-HE-Tag: 1706244030-520586 X-HE-Meta: U2FsdGVkX18F7ImoY7X5BCcyjnfVqhkqok/s9zNboffkcdn/HN5ceDr8YH0kG8wzEjiBH5ZqN4v3UwmLUaSgchOWhu9DrpS0hyR3oYvB8VHi0bjOd0DYwfsCFYas22BbuL/2OpT3pZc02F+3w7+T7c3IIOx0L3DNq1SA/YC7qWuQrFgrrPWglGJTDEizyEUd33bd24SsUiz/9pAHt7VTkQyoRo/OqCe3qjHTejMRL812ULrXsy3NjMBSpS1cOqUYwfP9WzEdjI3RnBq3fFbpADcAx/66IU89RYIoD1tdmXSfd+GpZCNbFd8ucTMU+RPOGtEi4lFLfrYCk0/2xg37KGPzvUmv9CQZVjFhyzFHig1eNhRTUwtL+2Szdm1SLy3LxS+uw3J7/8oBIYWDaQe8E1COJ/PIG5w4Ta6mK/kfL4M1N4aJq5iM6zAPqlotZyZE5hjWEJX5JJ1qDvcNsc2VhrasVAr8rsBRmdayA/8og/le8A99yknsNcVIrq6SGet50YTfqrEigULK98ex6qlYCDkQxN6o+/HZvs6hkgi1Jpv5tqK6gXcc2A0Z8JYqe3GcbGAr59GqIOl7dEzWSZqZamHVSsuDj+Yfg00NhB01E1JeW3pXBslDfBbAZq4iQ0G3ySjHTZk5gc6xOfwWtztnsw4SElzCwBdtvPSe9x5T9SW8omNcV64JKr/JYOZAMetMK7GvdqxITUQjxskuP6lTsMWdUujbYl1OdWhtLwE+O600Kixd+WyQ/Z/g3m34lBSI5foPCXJrcs2Cvu1WHcpyaMzRqYf2EUNLTev8cMHKuAscycezoBZUbqbAl0XspEAZnPHEPcoTvNVPAZ8A4k2GCK5wTr5sSx2wjo1zFyJD8OaO5Evb1rLDgBWEikAexlDYaw02Lx8BoOApwmRROg9cjlxz76Wbwe13jRSChI/MKr6oOuHXGj05nbj4SlTSTyvcvEekGnJSpsfx0jP+g/b tmGsqVBA M32C8Ze3du7BwhgSkKh61O2fnWJtnVOQuiM+sh4zB9dn1LurLHYopOEkL2wPkha+CPDC2Yu4QqCj+i3PhdP58Kg8Q94BgbafbujinNmDAUwK/RZgr7k+elwQlA6oo7OSAYYke6KOd58Rb/FbuzH1+qeqHb2oAtjlpW153Dzg8G4IyfI9aPwpHYfoOJ/3h36D+4NDcbaBTetjo8Ak0Uo+oFS7bagK49T/+RZUZR4UeTvXi3JQksxNvW7kNyiDSQkocBoY0I4KRsvEmLcDEjKY+ccILXvnVauc5XTqwtmZ5Mvx1mUxGRUXOBpl0BYjuMSDJVqLNKNI4fl0YsYiclD4YTtJ6ToziwDlDnPxl+xH7a9N5Z28UhvKnsYpunA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: RMP faults on kernel addresses are fatal and should never happen in practice. They indicate a bug in the host kernel somewhere. Userspace RMP faults shouldn't occur either, since even for VMs the memory used for private pages is handled by guest_memfd and by design is not mappable by userspace. Dump RMP table information about the PFN corresponding to the faulting HVA to help diagnose any issues of this sort when show_fault_oops() is triggered by an RMP fault. Signed-off-by: Michael Roth --- arch/x86/mm/fault.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 8805e2e20df6..859adcd123c9 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -34,6 +34,7 @@ #include /* kvm_handle_async_pf */ #include /* fixup_vdso_exception() */ #include +#include /* snp_dump_hva_rmpentry() */ #define CREATE_TRACE_POINTS #include @@ -580,6 +581,9 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad } dump_pagetable(address); + + if (error_code & X86_PF_RMP) + snp_dump_hva_rmpentry(address); } static noinline void From patchwork Fri Jan 26 04:11: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: 13532022 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06AD5C47DDB for ; Fri, 26 Jan 2024 04:40:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89EB86B0082; Thu, 25 Jan 2024 23:40:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 84E4F6B0087; Thu, 25 Jan 2024 23:40:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6EE556B0088; Thu, 25 Jan 2024 23:40:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 604456B0082 for ; Thu, 25 Jan 2024 23:40:52 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DCC13C047A for ; Fri, 26 Jan 2024 04:40:51 +0000 (UTC) X-FDA: 81720211902.02.DDEE54B Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2062.outbound.protection.outlook.com [40.107.237.62]) by imf03.hostedemail.com (Postfix) with ESMTP id E3A302000C for ; Fri, 26 Jan 2024 04:40:47 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=moVU9MZI; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.62 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244048; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5kiZwN/er7VREe8gy2nnlht7qc4XPTgKknA5h/Y9v8k=; b=LEprw9pBJiwhu58V502OW/OR+D/CaRnOrLY4l9jYyQhdiTqvrllq/k0MWgvtNMdcilopga qFvSx78odyJQewArIL4VGDhwyoZqDmzss/tNXFD8pYQCuThHIRIKeOqz514tXgWoAn1oq0 XXsYkDxepKbgKn0qhyz1U6zuxYjjYJc= ARC-Authentication-Results: i=2; imf03.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=moVU9MZI; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf03.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.62 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244048; a=rsa-sha256; cv=pass; b=f+mZ6qisfOv9Hz/UHis+tkdLH6Y68wl1lPPn57m5PMnALa+JnWE1LDBM2b6Sjb7Ik0J1qD UlyzVtZKwa8TA0089HjG8tbjVJmaIwUMap6w7ERk1uFlLak7/WZBDlwJki459c4FKrSuZS nHFHFCiFIG76aIKaYDjvPsip65cjpDA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VeZrnm2e1PW48zb3oGKYqTbeBzD2zCTHbKb8kXo/mxBgEFow+yQP2g47MZ/oIEyooLTIKtITkjLsBVc2TCfpa4nSOZK1VoIH4Ayc/GUksMD0IINfR1GQnnPHcghUrWBSK46cGABSAzZwixYy4ShKWbcPqpYiH6TdcWdDGpmqehs0cg/Eowe/zSJaExRbipgQTi8QJjx+5Wd82USyTnEquxhh6qP/LP/3i8Wkgged0mO+btFOvJSfSgS19ZlfTo2ykvdVnWJYQ9rv1VwmSc0hCsHmhf48fGxaPGKRVsVusFe0l9T300Xq7Hw6pwZsvYUqqClnsq1DHkZUytCqYS6J/g== 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=5kiZwN/er7VREe8gy2nnlht7qc4XPTgKknA5h/Y9v8k=; b=ZpHer7G8Ruaqy39EaHpBXUeXNcW3FDIdM3Dk3A2I9gyoL8n2KpOHdQ+Hcw5zN5qfV+AmJm9z5vqdNpZzKMFHcLnpW5WV8qE3Nd7o7Rs0artCeRBVIUEw6GR5LPnG8z+SkytTQW5opnggBOGBl3l8fFJ6lhEyzJAEJIiHSLbvVnnYPqMxYT8eMpnV01CKEBA4KNUTzZ2spASBhfpJkHETolci40Adk3cMUO/Zaq2p/ubyezgmsMf5DdUGrieiH5+3EPyk+oo29jRpzAWOuPv6FXeR83MILhgLFaT2bZLpsnG3axmryE0hyw3o2nKlwE4A/MzrKPs+S/x8VT5oEtM+lg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=5kiZwN/er7VREe8gy2nnlht7qc4XPTgKknA5h/Y9v8k=; b=moVU9MZIEPloTUJXqQONJrCUv+0Cw36CAG6mHvA8kOjihXtLloY+2jKWiNQfskwrcPbk70qMG0lfM5rCCJLmgPWHDeBqM0NTlOeJKBxFpHcp+2o9CBVCiu/9CVDiblkeXaYYjCooYxaqUequUqg0WgutiAOJQdwL6MBeqnG7tGU= Received: from BYAPR07CA0032.namprd07.prod.outlook.com (2603:10b6:a02:bc::45) by DM4PR12MB5344.namprd12.prod.outlook.com (2603:10b6:5:39a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:40:45 +0000 Received: from MWH0EPF000971E6.namprd02.prod.outlook.com (2603:10b6:a02:bc:cafe::46) by BYAPR07CA0032.outlook.office365.com (2603:10b6:a02:bc::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.37 via Frontend Transport; Fri, 26 Jan 2024 04:40: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 MWH0EPF000971E6.mail.protection.outlook.com (10.167.243.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:40: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.34; Thu, 25 Jan 2024 22:40:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 10/25] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Date: Thu, 25 Jan 2024 22:11:10 -0600 Message-ID: <20240126041126.1927228-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E6:EE_|DM4PR12MB5344:EE_ X-MS-Office365-Filtering-Correlation-Id: fa54b2f0-6fe3-4570-8bbf-08dc1e28f5ab X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WfJ1hAzRyglTd9I7ruhZxVOS5k4L25u9zbwuRLnd6NkdjdukB5XeeCayEROD8I3+6wLXMHRtP5MnM3lAZZoM58zVrVtg635HeR2k9flfOP005GgxjksLL3ObpbhJFYiPfMgmcpl3f93ooJ+F6B+tbtd70VjVvTW692/zaZdlAkmnqiVg5xl5KtUmZHpDmELpqeuHLsEUovauqfFakQsylFfmg9YLud8YV0XtwfW2l/sIsfTdDziQa1QbE7H7v40+398BOVAMTezVsVMJHDw4lEjcu4AF0wo9+/DvN38NEplgN9CDb/qyaPuVTx/E5Pifn5s+tESmKKyBMaAV0iT//WGvHALm0QVeuPwv9KWYubBmc2fM9nmdywMR4vUv+JrXl3vych13xqFBqi96rGZ0T9XRtf2CzcjPnzeLHYD1mB1NmX8rtc54m6rcsnYtrzMFOcCcZbNVTK8vyjF1KUYZKbgnXE2XCS/MgkbPPlo9rs+/iUe+h/w9zqVzmVGIOYREYziiKZ3fHxzYlpOZihVrRZaUsq5u6LOBCJix7NkRGr8fiflfsAbr5PbHsaA57AZI7yOUQN5+fqu9/DKBmdkwA0WUWBcztqpiTbVesQn526Dj+PnGYCkH8g0GQRBevDmkkmIBp6stCKSciDMco2al1f6GAYvtLtnjzTDQwiyZxPVTEGhnGHc9/jZXcBGEqCRhb6Nil0i+1ucQNtoP5fxp79Y9opwWiv4Gh7Yu2YjKT92FtUWhemGbGt8EAAJOdZPmVmVQneLWcpZGpDbGFJuxeQ== 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)(4636009)(39860400002)(396003)(376002)(136003)(346002)(230922051799003)(64100799003)(186009)(451199024)(82310400011)(1800799012)(40470700004)(36840700001)(46966006)(47076005)(36860700001)(16526019)(8936002)(336012)(426003)(41300700001)(5660300002)(36756003)(83380400001)(478600001)(8676002)(4326008)(26005)(86362001)(70586007)(70206006)(44832011)(6916009)(54906003)(316002)(1076003)(2616005)(7406005)(2906002)(7416002)(6666004)(40460700003)(40480700001)(356005)(81166007)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:40:44.7547 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fa54b2f0-6fe3-4570-8bbf-08dc1e28f5ab 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: MWH0EPF000971E6.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5344 X-Rspamd-Queue-Id: E3A302000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 8c493dgf64nq9nqb74xdhou3a3wy4mhi X-HE-Tag: 1706244047-143862 X-HE-Meta: U2FsdGVkX1/bEdUYT4hpT8XDhoQNpUmpPaVgEHoBZpQTiJQnZjU/kq0pHbPokrmFtceAadxuHz+VSiKtQ0n5Xvwm5ObeDI2n/kjO8IzVOIZ36/bVJFoIsERL5nRX2Oz/H0eO0yNkakIxxWwmyexjbMauelBgb5Fr7mONlsaa0VY/4LcZDmnxX4ULVVaTeJYf624BGi487j16WJVGnrf8wSgy5xbmSn3VaHoHPX+5DAFlICYuxQbJ4SAnnRMpnGjk/vD5++5MeDqruzdVy59X6VeYyWJbVGqs/vHf66j0cixRSNBJFUpQl8a31LgVJQ3NM+PUvwF5Om/mYBSiAHsHPrqBlPmi5cePDnQlnBqnfrInsv/p54Qsxcbv9j7aNs/0C1G0WrRdzfi4FGvZ1BAUGHRA7/uyzk+AnV1bspDqODuc9FpXchyNZRDLlq0Sy3RDmnOVVpQPiqPER2CAW8iEDXL5+vjODnlrdGAAP+guSXRCNZ5QXCpToUGWp2rce/mSJIAO/nvz1Y2cSIl/vVxTXQn0LPS32KcPF/FE+EX1Xi/0vFKfQpC+6M1C9RxhePvJAGoucZkxJFpgoHLpgM2T263Fs9vLzqRB22bjOZhUlrFTKQe1ciz+k3dJPdAkFeUEQTeEcL/17eNVlA6fEw2rRbdDfvBSFZUx0wD/HdmnssgFFSMA8UX18OGaAJzRIIsPdXydMYm7RzCjJ73FW8MQUOVnOFCA3hHmF7Oym8v5U26+BhweKwp9v/cpXIs2C5H9vTWwjbuL6OVcbZdC+LNiPOGJH724OT41coNH+cIASbQEV2W0tWbUszp+DXCFb6KMfdYvx79HhIGh+1gCfGt19zm958o34h5dJm4whwVvcA8ywgu5iUKyDiTdE7j7sX/tISan1XkgtB5L8cFO2qYCZ+OL2ayAFJlT4GkE5JZ1YHv1XpWcGPMKAborAXShj0Y+ceHb8tKJY6hKwuCg1+l Us2HYqUe bIBEFjQizwpyytFwxgRcj+TVrvvUZMQYaXPPyqr0ULOIY19+vCc/Aafsz/YxYRUO6fIjTeTGw56a86t3TOhC5HWlB/lDMLFHFkJCxR5LJK49JW6YIFSgVN+sFhcPtBQZ/gWeXFx+xsnf0CKqh246mSXej4zwZr7j8nh7HzljW2Ik/qs/VAVBWCQMt+gBTMQ1gyXE0E9xyYlVl6Cau7AgrB4WoSbbJbIjDQjyqgNHjaAz4+FgYJ3+lczhfobM03o5sObD8zkeLCBPgHkNnSXoQHW58ptdBfiCsrH5fTzHnUALvYxlPvNfHQDU6vmoWw/1rsk3c5/GgKBmC5MY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh The RMPUPDATE instruction updates the access restrictions for a page via its corresponding entry in the RMP Table. The hypervisor will use the instruction to enforce various access restrictions on pages used for confidential guests and other specialized functionality. See APM3 for details on the instruction operations. The PSMASH instruction expands a 2MB RMP entry in the RMP table into a corresponding set of contiguous 4KB RMP entries while retaining the state of the validated bit from the original 2MB RMP entry. The hypervisor will use this instruction in cases where it needs to re-map a page as 4K rather than 2MB in a guest's nested page table. Add helpers to make use of these instructions. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: add RMPUPDATE retry logic for transient FAIL_OVERLAP errors] Signed-off-by: Michael Roth Reviewed-by: Liam Merwick --- arch/x86/include/asm/sev.h | 23 ++++++++++ arch/x86/virt/svm/sev.c | 92 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 2c53e3de0b71..d3ccb7a0c7e9 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -87,10 +87,23 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 4 + /* RMP page size */ #define RMP_PG_SIZE_4K 0 #define RMP_PG_SIZE_2M 1 #define RMP_TO_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define PG_LEVEL_TO_RMP(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M) + +struct rmp_state { + u64 gpa; + u8 assigned; + u8 pagesize; + u8 immutable; + u8 rsvd; + u32 asid; +} __packed; #define RMPADJUST_VMSA_PAGE_BIT BIT(16) @@ -248,10 +261,20 @@ static inline u64 sev_get_status(void) { return 0; } bool snp_probe_rmptable_info(void); int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level); void snp_dump_hva_rmpentry(unsigned long address); +int psmash(u64 pfn); +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); +int rmp_make_shared(u64 pfn, enum pg_level level); #else static inline bool snp_probe_rmptable_info(void) { return false; } static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } static inline void snp_dump_hva_rmpentry(unsigned long address) {} +static inline int psmash(u64 pfn) { return -ENODEV; } +static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, + bool immutable) +{ + return -ENODEV; +} +static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index c74266e039b2..16b3d8139649 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -342,3 +342,95 @@ void snp_dump_hva_rmpentry(unsigned long hva) paddr = PFN_PHYS(pte_pfn(*pte)) | (hva & ~page_level_mask(level)); dump_rmpentry(PHYS_PFN(paddr)); } + +/* + * PSMASH a 2MB aligned page into 4K pages in the RMP table while preserving the + * Validated bit. + */ +int psmash(u64 pfn) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENODEV; + + if (!pfn_valid(pfn)) + return -EINVAL; + + /* Binutils version 2.36 supports the PSMASH mnemonic. */ + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" + : "=a" (ret) + : "a" (paddr) + : "memory", "cc"); + + return ret; +} +EXPORT_SYMBOL_GPL(psmash); + +/* + * It is expected that those operations are seldom enough so that no mutual + * exclusion of updaters is needed and thus the overlap error condition below + * should happen very seldomly and would get resolved relatively quickly by + * the firmware. + * + * If not, one could consider introducing a mutex or so here to sync concurrent + * RMP updates and thus diminish the amount of cases where firmware needs to + * lock 2M ranges to protect against concurrent updates. + * + * The optimal solution would be range locking to avoid locking disjoint + * regions unnecessarily but there's no support for that yet. + */ +static int rmpupdate(u64 pfn, struct rmp_state *state) +{ + unsigned long paddr = pfn << PAGE_SHIFT; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENODEV; + + do { + /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" + : "=a" (ret) + : "a" (paddr), "c" ((unsigned long)state) + : "memory", "cc"); + } while (ret == RMPUPDATE_FAIL_OVERLAP); + + if (ret) { + pr_err("RMPUPDATE failed for PFN %llx, ret: %d\n", pfn, ret); + dump_rmpentry(pfn); + dump_stack(); + return -EFAULT; + } + + return 0; +} + +/* Transition a page to guest-owned/private state in the RMP table. */ +int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable) +{ + struct rmp_state state; + + memset(&state, 0, sizeof(state)); + state.assigned = 1; + state.asid = asid; + state.immutable = immutable; + state.gpa = gpa; + state.pagesize = PG_LEVEL_TO_RMP(level); + + return rmpupdate(pfn, &state); +} +EXPORT_SYMBOL_GPL(rmp_make_private); + +/* Transition a page to hypervisor-owned/shared state in the RMP table. */ +int rmp_make_shared(u64 pfn, enum pg_level level) +{ + struct rmp_state state; + + memset(&state, 0, sizeof(state)); + state.pagesize = PG_LEVEL_TO_RMP(level); + + return rmpupdate(pfn, &state); +} +EXPORT_SYMBOL_GPL(rmp_make_shared); From patchwork Fri Jan 26 04:11:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7FFFC47422 for ; Fri, 26 Jan 2024 04:41:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73EC76B0092; Thu, 25 Jan 2024 23:41:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EEA06B0093; Thu, 25 Jan 2024 23:41:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B6B36B0095; Thu, 25 Jan 2024 23:41:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4BAA86B0092 for ; Thu, 25 Jan 2024 23:41:41 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1F48F1A063C for ; Fri, 26 Jan 2024 04:41:41 +0000 (UTC) X-FDA: 81720214002.11.453D695 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2068.outbound.protection.outlook.com [40.107.212.68]) by imf06.hostedemail.com (Postfix) with ESMTP id 58FC7180003 for ; Fri, 26 Jan 2024 04:41:38 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="S1OFy/N+"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.68 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244098; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ieXJvlkx0ARe4Dlb1EX+7YF5/vwoMHH96EazENT13Fs=; b=8Gv2kTnjlH06Tx4GQqKIFFA5hnRwRXdPLHsMNRS1dggZiJ/McslxXx5WD/gkGRUJ9dcjJn mA7Ks5ixi7IMu5tIXLAl2XDiUXEg+9L5DunFUH68kCFbmpktV6JOsK5Ch8wcwM08K+OCtF hZ3AgGn218/Yrh6CY1ZXBQhrLHAgk8M= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="S1OFy/N+"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.212.68 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244098; a=rsa-sha256; cv=pass; b=uy7yzGrMadIDPMQhPnemCV8Dk6/XAJAxmC0nm5hBCHVAWVC9Ud4+PVRBmWL849Q9ogNIJ0 4bS5DTP4/iOCgDE+i+spYYp84nAA0X9Z7788hxi7ztO7ggbUI2dwLpbbtxWEgjR9u+IX4Q LzR/36IUdn68lIVAtQw3ooLbhpW88Ko= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fO4d3+LsIpzF4A9aewJtDNAS5cz80Tm76FCsb7V1BeCa49AF+Aw1GXEVxAGWHVBXn27eRY7V+JwGddDd9L3TFqKT0WLQBuc8qzjQcjycCzbpzrB7ljaPuwTa1ZX1VkdRXQIiT3Cnohq5Ri4RcJum84zweRieqU/z4VBx/0VXqNFCHSG6GfB8Xa0JfBC53VTzNfB4fLPvHJ2eVAv4m65IcJjzW1FcARR7aEPAPrRPEmZmuL1yo21qubGeHodG+FAyH9s/nfXbvMcTMdMHfZw25COQrdhDtQ9sley+9eeg4iSvPrD4hhe/0hp8tl0K0vmi/q+/HjUfujWA5RJb/buWVA== 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=ieXJvlkx0ARe4Dlb1EX+7YF5/vwoMHH96EazENT13Fs=; b=P5YTGBuosp3dA87a1NT+FIDR5RfnSqRMfDivs543Ih7nktM7cx9vhekEnWAyI4cT2z7KDSXhmhr0j3yenQf/UOy/fo58nDdB9CYW3ygI26IsajKpcZDuhYLD0+HgpZuBUzG9kcN9e554Cvl9E4sGgHWQSpF6mrWAf6DiYg8ylZ75mW6WWFynZg/R2s6P3XwFy+pVoT+dbkGiNiHJu9ebfQiaqdnKfvcznaErTNL9WhuqtJRuywdxtM5rsOGvG9C9My6N4OKqp5LlHY0J1wsJx9dD3jut6eLzNnyXWAkSPQV7o/cu1YjZzTYstKAYiSUQxwurbyARb7eE/gQLhx/nJA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=ieXJvlkx0ARe4Dlb1EX+7YF5/vwoMHH96EazENT13Fs=; b=S1OFy/N+fIm6BHMIkkW3Npopu4kla6Oix4WQ7HWEgZDSlzhvUQYXCveRWUssMnQHlHGJlRWny6A+ahf0Aixk8z6O7VvJIBNDEophXv7xt1kGjH40ZR08wric2965EnVtVwcqzaiU2fzy7EpMjg/cjXnblq+eYHC7A9cqTyUMkm8= Received: from MW4PR03CA0279.namprd03.prod.outlook.com (2603:10b6:303:b5::14) by IA1PR12MB6233.namprd12.prod.outlook.com (2603:10b6:208:3e7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.24; Fri, 26 Jan 2024 04:41:35 +0000 Received: from MWH0EPF000971E4.namprd02.prod.outlook.com (2603:10b6:303:b5:cafe::10) by MW4PR03CA0279.outlook.office365.com (2603:10b6:303:b5::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:41:34 +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 MWH0EPF000971E4.mail.protection.outlook.com (10.167.243.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:41:34 +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.34; Thu, 25 Jan 2024 22:41:32 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 11/25] x86/sev: Adjust directmap to avoid inadvertant RMP faults Date: Thu, 25 Jan 2024 22:11:11 -0600 Message-ID: <20240126041126.1927228-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E4:EE_|IA1PR12MB6233:EE_ X-MS-Office365-Filtering-Correlation-Id: 443f62dd-9e6a-4b06-a1b7-08dc1e291371 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lqKkq3OIPj0YoZqJN8zaR/lu25NFzon3JReU10qoBWHzdU8K92JplpoQV2Z/f4FbpwraHSTbGQuPByj7elzh+QeitFwCwO4oXaR84MiO85vX5WZyfneIdnQOd3mpZWCVV/AG7UztY8TkV+ofmZcDGt3ETU5fyRFyHw8WMmP+M049LwEgG/gcOpLVLd/ZeA70WkSZ4xLDyF0HFux3LEdu+MhgEkEwhRDxqXtDWSSgeoqKdGG4338NjFn8gsAjBn4nc7zFZ2hBImfh73bAGfxKZllDBqRTFwLno4WJy1q4lGD+1EcYjHddGX4xei44/uqxjzksJ72ErRPe7iW/VQGV1yDBosaXyOYxY4m7H02hMGszex3cbb8v3dCU2QBvjMmuamJ1/vwtdSMUnWc0HTPDAqlQSma/pZwW6L378u9RxDKTYMnJSqGLAkbG0K0tJWNA1gzulDiuOx6fssgNLjiziOHVwtvtQvJg88ppm8lgeFBtirn2Uqcm6mJj1Y1A7gqGX30AOxMMFhrEcm4Mg2aYE94iMlm/HkjNFU5o9F7NZ+OkwzMrK19F8lj8tzSEWGFkOF57apaF5vBhosnKm025XLdkCwrHXkYXRX8SiRpj+JlxkVtfPSCj8LaWe2ulnYh14q9fnVzYaQBOs/KPANa0ZfpfMhB6oz8a09ZUIslfu4pLtSBLulnqi2n24J7d59LJjg3NqN4lvvIQKWKT7OlXijiE3CBqX0hu1Yxb6nUjqhvvNkxmPhG6fUtCQMsJwkVjmg13CZZKe+1MP9F1DT7Mmg== 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)(4636009)(346002)(376002)(396003)(39860400002)(136003)(230922051799003)(64100799003)(186009)(1800799012)(82310400011)(451199024)(40470700004)(46966006)(36840700001)(40460700003)(40480700001)(83380400001)(47076005)(41300700001)(356005)(86362001)(81166007)(36756003)(82740400003)(426003)(1076003)(5660300002)(36860700001)(2616005)(26005)(16526019)(336012)(70206006)(54906003)(70586007)(478600001)(6666004)(316002)(6916009)(2906002)(4326008)(44832011)(7406005)(7416002)(8676002)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:41:34.6911 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 443f62dd-9e6a-4b06-a1b7-08dc1e291371 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: MWH0EPF000971E4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6233 X-Rspamd-Queue-Id: 58FC7180003 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: h6routk8xtsi7xqmc133swqrsjhfiwf8 X-HE-Tag: 1706244098-857032 X-HE-Meta: U2FsdGVkX18uQGPP8M8itqf01FHkr6tPL7j3nd0qt+y4vAVh6y+4tpTfP1w8wkoZDfFNvA9aHBkvsTuOhQnI6xu6xaGQxsjqOyNArTRP0nlomNq77XwvfoeNrxYWBhsCYEzvstnv+wEpU9hyTFhbXa6bXvpZsmVNIQ+XKsb/nFkDh3/wmSOv6vSY4wAgs5Zh/OAkqsCcCE/XS4HFwkipTqsRIhKjftRH0540be9KepL3gCr4Sv2jEPlFnlV8BdhaNOr8d11v8ZWijWD3Zx4M4+npezJYGg0//blUmUJ70rc9BleabFlP9rPk19Gmjv0oIWKppG8IZ+UxufEDlqPy4npCNyVGaytNvKV0Ef1t1/FHPiHQyn5SK5HTMNuDHNI4UllECh5cvWrVyHqc82+80mPLx28StesMR6dHXWn8MyrNeO9R0RFXkEXWqY6gsaAtFOE76AiQXy6aEoaWoNaYN/YTltTxHBBe73D0fhAXwRvb1DaqGSMuCysvj091TcS3GA/3gczszHZuuGpLh9IHMZXxbpvbUlhLFijvpDlPcKiiBiM7EKR5IqC42XabdngTZN/zR6FfjQF8HTpkAsCpwiyfx0fArsj3RwXlJXEaoq10JEAkSriKsoIHqS7kSFgQxUDSZjUHBghvL5AkrI/YICeoEP98GVMVPkNNhDkKY5rYfzPiCGeHR937kngp9krNrjMLrwEXSP45cORbloQ16HCZgrBR9mHE5JSdW4+k52Frij+KWsIffxCnKkAEj/iPfEbrU8SPZ+6E3kzNOWe559y2mm1wBpO5lRcT+n9QEuwSoO/v59ofekEjjKFNaqCqGJvG3BGKIXUMLDq0WmRLD5sgOqSvsM1czrE7dC+gZDd9OYfquTIj4Hf+VdtyIVvschKK3VpW5EtmIAh0k1GudPZHDR/N3/uCg1M9qGhG740Yp5qVh83ww5yMGsW3TZpfgnUgA1ER9gMUQ2vYrea bYQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: If the kernel uses a 2MB or larger directmap mapping to write to an address, and that mapping contains any 4KB pages that are set to private in the RMP table, an RMP #PF will trigger and cause a host crash. SNP-aware code that owns the private PFNs will never attempt such a write, but other kernel tasks writing to other PFNs in the range may trigger these checks inadvertantly due to writing to those other PFNs via a large directmap mapping that happens to also map a private PFN. Prevent this by splitting any 2MB+ mappings that might end up containing a mix of private/shared PFNs as a result of a subsequent RMPUPDATE for the PFN/rmp_level passed in. Another way to handle this would be to limit the directmap to 4K mappings in the case of hosts that support SNP, but there is potential risk for performance regressions of certain host workloads. Handling it as-needed results in the directmap being slowly split over time, which lessens the risk of a performance regression since the more the directmap gets split as a result of running SNP guests, the more likely the host is being used primarily to run SNP guests, where a mostly-split directmap is actually beneficial since there is less chance of TLB flushing and cpa_lock contention being needed to perform these splits. Cases where a host knows in advance it wants to primarily run SNP guests and wishes to pre-split the directmap can be handled by adding a tuneable in the future, but preliminary testing has shown this to not provide a signficant benefit in the common case of guests that are backed primarily by 2MB THPs, so it does not seem to be warranted currently and can be added later if a need arises in the future. Signed-off-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Borislav Petkov (AMD) Acked-by: Vlastimil Babka --- arch/x86/virt/svm/sev.c | 75 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 16b3d8139649..1a13eff78c9d 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -368,6 +368,71 @@ int psmash(u64 pfn) } EXPORT_SYMBOL_GPL(psmash); +/* + * If the kernel uses a 2MB or larger directmap mapping to write to an address, + * and that mapping contains any 4KB pages that are set to private in the RMP + * table, an RMP #PF will trigger and cause a host crash. Hypervisor code that + * owns the PFNs being transitioned will never attempt such a write, but other + * kernel tasks writing to other PFNs in the range may trigger these checks + * inadvertantly due a large directmap mapping that happens to overlap such a + * PFN. + * + * Prevent this by splitting any 2MB+ mappings that might end up containing a + * mix of private/shared PFNs as a result of a subsequent RMPUPDATE for the + * PFN/rmp_level passed in. + * + * Note that there is no attempt here to scan all the RMP entries for the 2MB + * physical range, since it would only be worthwhile in determining if a + * subsequent RMPUPDATE for a 4KB PFN would result in all the entries being of + * the same shared/private state, thus avoiding the need to split the mapping. + * But that would mean the entries are currently in a mixed state, and so the + * mapping would have already been split as a result of prior transitions. + * And since the 4K split is only done if the mapping is 2MB+, and there isn't + * currently a mechanism in place to restore 2MB+ mappings, such a check would + * not provide any usable benefit. + * + * More specifics on how these checks are carried out can be found in APM + * Volume 2, "RMP and VMPL Access Checks". + */ +static int adjust_direct_map(u64 pfn, int rmp_level) +{ + unsigned long vaddr = (unsigned long)pfn_to_kaddr(pfn); + unsigned int level; + int npages, ret; + pte_t *pte; + + /* Only 4KB/2MB RMP entries are supported by current hardware. */ + if (WARN_ON_ONCE(rmp_level > PG_LEVEL_2M)) + return -EINVAL; + + if (WARN_ON_ONCE(rmp_level == PG_LEVEL_2M && !IS_ALIGNED(pfn, PTRS_PER_PMD))) + return -EINVAL; + + /* + * If an entire 2MB physical range is being transitioned, then there is + * no risk of RMP #PFs due to write accesses from overlapping mappings, + * since even accesses from 1GB mappings will be treated as 2MB accesses + * as far as RMP table checks are concerned. + */ + if (rmp_level == PG_LEVEL_2M) + return 0; + + pte = lookup_address(vaddr, &level); + if (!pte || pte_none(*pte)) + return 0; + + if (level == PG_LEVEL_4K) + return 0; + + npages = page_level_size(rmp_level) / PAGE_SIZE; + ret = set_memory_4k(vaddr, npages); + if (ret) + pr_warn("Failed to split direct map for PFN 0x%llx, ret: %d\n", + pfn, ret); + + return ret; +} + /* * It is expected that those operations are seldom enough so that no mutual * exclusion of updaters is needed and thus the overlap error condition below @@ -384,11 +449,16 @@ EXPORT_SYMBOL_GPL(psmash); static int rmpupdate(u64 pfn, struct rmp_state *state) { unsigned long paddr = pfn << PAGE_SHIFT; - int ret; + int ret, level; if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) return -ENODEV; + level = RMP_TO_PG_LEVEL(state->pagesize); + + if (adjust_direct_map(pfn, level)) + return -EFAULT; + do { /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" @@ -398,7 +468,8 @@ static int rmpupdate(u64 pfn, struct rmp_state *state) } while (ret == RMPUPDATE_FAIL_OVERLAP); if (ret) { - pr_err("RMPUPDATE failed for PFN %llx, ret: %d\n", pfn, ret); + pr_err("RMPUPDATE failed for PFN %llx, pg_level: %d, ret: %d\n", + pfn, level, ret); dump_rmpentry(pfn); dump_stack(); return -EFAULT; From patchwork Fri Jan 26 04:11:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AFABC47422 for ; Fri, 26 Jan 2024 04:42:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AEA1B6B0095; Thu, 25 Jan 2024 23:42:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A725A6B0096; Thu, 25 Jan 2024 23:42:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C4756B0098; Thu, 25 Jan 2024 23:42:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 786496B0095 for ; Thu, 25 Jan 2024 23:42:01 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4B9BF8055A for ; Fri, 26 Jan 2024 04:42:01 +0000 (UTC) X-FDA: 81720214842.02.B8CA0F3 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2044.outbound.protection.outlook.com [40.107.243.44]) by imf08.hostedemail.com (Postfix) with ESMTP id 517BA160002 for ; Fri, 26 Jan 2024 04:41:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UBqDzf6H; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.44 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244118; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JMQff3xMrtTAk9jHUzlJLddFasX2fCuSYJJ9cnssB5s=; b=q65kStSb5cQ00w4gNz3LJwKMShbO6gpWDsOwT+e+l+W3AqCDSRLScX/PSe5OXDInyKHOlO VuTzZ+NEMzmeTCpnSXv1BvocAedkND9SzlnrrMrltLVGROOO9onRyYDGJUcrD2Q69iAbIS emwqkD5wIYMgHaxXL+iutjIBrNGI98A= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244118; a=rsa-sha256; cv=pass; b=IdtuGkZv9ttHEIPxcFVH7lLXtIxY5Fou4e4AeK/z5+6zsMmnyoeeKyah/Eqx0bihUvRWod nlaJ1M7UL1tStitm0C2y6EfalpVWMgMLf98gDJeI1tJfxci/Z123Nkg/5LX6nImrtDnIo9 CoKeV89udeEgtKltBf4KicO7RlhTxPk= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UBqDzf6H; spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.44 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Iwgkqbrk6TJ7N+k5EExU4f6blUeAM3R4Q9tOU8I5s0zNGvYrUOyJM+VRQ+YQPKh4F50C1kLc4QmcLz7m2x6NI2pjYN/h6JK2fqynzpYfqdiVrM/nccVvJdo2/VLm+FGPk65yVqWYP2JqyHtExq8iIT4NaGpU7RKFBJFildkyZXNXo8nm39KYhYpxnAP9Gc1SAU2BLHf6gquce6dlrdfHTnXTieuXjsPu1luIQlVD7bY+F6TSJLf4b+EA0PxJGD96sUguTZ5yydU2un99SGNffzgT49xoXl6x89yfAiBsbilBqgfh4yOsWqGzqMF18JUMIeTCKCrNR1GdxEfdwJC1Ow== 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=JMQff3xMrtTAk9jHUzlJLddFasX2fCuSYJJ9cnssB5s=; b=Hsko0JEoJkmgV/NRFaOLekBgZ1V1TgWHh0EHMayVPGcTBPCnxGiESB9C+G/+LWux4ucSNlEQb04unoOg28HjlalP4LekRdnuirMIKagBU6/yFB3ZndWBAxQMqW8URTtBzJFEOak0sym+mftjSOkuFJNhNF8RKprke2cveoBvfFoYBxC89vO0yjXpovPxEYylVxTf4+aOi5eYVyRjLRpxeCPYtyNjiiXLlAYnrZp/d2BJyU/wH2cMxfgQmcjY2yzw9K4k2AbhrN4YhKd7tOKu5qT9I+a6vVHGlf5VenzJclq/jjjWZTPmy6XGQB4MidGzC4m58KdplqUSJfeFjEuy+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=JMQff3xMrtTAk9jHUzlJLddFasX2fCuSYJJ9cnssB5s=; b=UBqDzf6Hi29E3U8MWsqVYWhcQ2v2kUoyFukIbY4n8lsn1pxkH+OLeC71npOXmdyd6mYNjWBTpTBrh7lY6n2RHQXFiSt0rAfS1pE9dLdPXbAk45+Le4K7MkZ1aw3v0qmtJCgc08HAYOyKVcPtNaBkSfT7kCCwSAGD+Glwsh9UuNQ= Received: from MW4PR02CA0009.namprd02.prod.outlook.com (2603:10b6:303:16d::20) by DM4PR12MB7765.namprd12.prod.outlook.com (2603:10b6:8:113::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 04:41:54 +0000 Received: from MWH0EPF000971E8.namprd02.prod.outlook.com (2603:10b6:303:16d:cafe::69) by MW4PR02CA0009.outlook.office365.com (2603:10b6:303:16d::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 04:41:54 +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 MWH0EPF000971E8.mail.protection.outlook.com (10.167.243.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:41: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.34; Thu, 25 Jan 2024 22:41:52 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 12/25] crypto: ccp: Define the SEV-SNP commands Date: Thu, 25 Jan 2024 22:11:12 -0600 Message-ID: <20240126041126.1927228-13-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E8:EE_|DM4PR12MB7765:EE_ X-MS-Office365-Filtering-Correlation-Id: 7f09d903-e00e-4fd8-187b-08dc1e291edf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: stQ6lE2yg+RufQX99qT6q2Hl70WebvoleNAoZLUCmy45c9TP8k0s89T+BFImcyr3EvpmUJKGYhiYEgCToiOx52qevUx2F/d0XWkCFDyaxlT5dHdqeqRmCxztv8yUEb7e79K4T+CcSk3vCVHUqNt2KNMpghaOsr/v/sqgGbNwjfz85r72xWv0gLxSSBMR3nhNNjoxokTG/IoQsCy5AlcyPhnNXp8sh2bDEwG1O4Cz7kZTKZKLNBLZ+/V0EJEmfrn7esr2KExH7FOOtlF5aGflhTeRHlmiE8RVY64EXJZ9+VVqeLv8WsIFqrTaLTp3Gm2MY3HPOLQq8XN0ba4TCBWJ+t7fvi7M4H5dcFvGKgoM6YNqGEjnY8n/s6bV005S93zTShYDLGyZ1UU+0046jubhHVMuDPeI3G0a20S7oFxJd6vjxA7D8z1ASN8QYfi0zis6FdC+g/aUKIxRcAk4zAMfKEXDo3g3JrxJZCU3/JquR1NgmVugSe7S/UIPaCpgMiFfpBCUuzql7+Hiqc4VO/6pbLbrlMU8mSQxFC3RwMfXMpEWqQrMSVbMHq7JfVcwBYcmRt4opakcv9q6IYbxaH8uUZD4gtiF5asJCbuJuKkyGbBY/5NnowPJRtceFPi/AAQ3TyOsWJMlRQqkuSMEByo9+W9h8y+X7FDKFQ17bxszgYROdV8cGUCfQd1s02pgTN1nG2owuqpA6wOt5JvhiufF9LzfYtA0Bn3CXcVto+48tWsePXhC73ZOrXUdXAAJR7KV/5AOiBoPi5gBDaxkEm+buA== 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)(4636009)(39860400002)(346002)(376002)(396003)(136003)(230922051799003)(64100799003)(82310400011)(1800799012)(186009)(451199024)(46966006)(36840700001)(40470700004)(40480700001)(40460700003)(84970400001)(83380400001)(336012)(41300700001)(356005)(86362001)(36756003)(426003)(82740400003)(81166007)(2616005)(26005)(1076003)(30864003)(47076005)(36860700001)(8676002)(16526019)(70206006)(6666004)(6916009)(54906003)(7406005)(2906002)(316002)(478600001)(70586007)(44832011)(8936002)(5660300002)(7416002)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:41:53.8791 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7f09d903-e00e-4fd8-187b-08dc1e291edf 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: MWH0EPF000971E8.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7765 X-Rspamd-Queue-Id: 517BA160002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 3n48j779iumbzqfkbjs1aa8yu6cu9gxt X-HE-Tag: 1706244118-721958 X-HE-Meta: U2FsdGVkX18YjZXQl/XT0zs6Jl2SIzfxf6SrwW/jIAZf9OeJ4GwfCgY5ndyGsNqSd4qFmuKVVc0TQs5UmF+u1F7jJj1tTzz7lG+TElORz4icionlGXSH0njCnynHtDIlny6eleJSFWGESGWEVd+Fp6V9pVo/r/QHsJLImcDmVS+aPoQnCI3rS9fcp1o82mj9+5tk4xzqXvRSx75Sce5hNeFdPLSBlzn/yxlpRdyKq42HcFUaPa0tL27Px9zoJO8bonUbbfVEa+2pm76jw6A6U0EVYc/irg7alSbmcUx6wSOj/OYGXubZgGTz/rlGQluECOxe5fHLdGYS4pg0ROUs8NNnNgXl/I8csGj9X/HxtfLvqS643tzK8N20ksWSBnfc7S9H8bNHVoNMrPVVBHNuEx+EFQ9cx3Cv25dtH/XaDvakVcpgAr614CnGwYjeEzoPA6RYA1ncLS2Mbwlb/9iT+FQjWkDMi7ZEQCmiBXQl6M+Ub6Wky5n8c6+l2X1xN/5IX0jRkJh0dZnNGeJIDJ8cVY0rmC8TPgpyvE2I4eV9IpxDFWJQEQrOfKK6CZbvdsmqyiBHxIyy66lAwSUylonCvEUzICT9gBdevLN3gORaTcbX7bzN3YeNBQFA1GHPt8W5vDW0lgwbJgNS2J1ssTzTDoNpEbQqcf3w/TVyRBdOBf/GvaUn6ap7RUyvO03JXne3vIUCWyMXfoCQNlSVETDQ6I2XHWDHSkincDuJqksqibf+0He5liYdotlxl6/gecav5oCBz42rvD5A8iCirz735kwUuux4gb9QdAX/0Z11ujreCMiBFJivpWPc7noyLKyPoX5uyheeaB8tS62F7CaEvYcGZAjQYyhBv9grcKW/OzvF7l28/TyidnO9sUP1P2sw1achQGvZuGMkOXxBuB7JCx15Nj7nw77O13z15eppO/AShKTwWao3Fxay7EgX8Cv0J7gBd82voUBn4hUiLKQ JparKwrC g6LjBSYyDjM1RP9YP97pyxIL38GMVol0RHUwBLuFzFNHhaoeAlqGFAv5heBSWfKYFl0C76seWz+Ew9qSKb13G6QouHzde7/byEslCLhixU5WRkbM83EBAbPC6TQTJvmFATnGFxIKnGICv8N9uWM/IHWoZ0J7bgci/EYkl+1LUHYDUVLrBxSF5LFU8BWjshIqcRAOstx8uwQDA0jlCCGuun7GtebIaSVkQ6cnEVXaR2yleDsDgsFFcn+7aOycEzGQBKhrS99qOeYi/FRFqH65iz057ZFq27TXmM9pHI3hqhu59y4jUaQKggR1HF5MmBuJJk5WfICJdj4BKFozQ1md6qfLmKIJUT3FjB9BP0ewshl6q6yXMhRZkIAzaQQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection. Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev. Signed-off-by: Brijesh Singh Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra [mdr: update SNP command list and SNP status struct based on current spec, use C99 flexible arrays, fix kernel-doc issues] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 16 +++ include/linux/psp-sev.h | 265 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 56 ++++++++ 3 files changed, 337 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index e4d3f45242f6..e38986d39b63 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -130,6 +130,8 @@ static int sev_cmd_buffer_len(int cmd) switch (cmd) { case SEV_CMD_INIT: return sizeof(struct sev_data_init); case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex); + case SEV_CMD_SNP_SHUTDOWN_EX: return sizeof(struct sev_data_snp_shutdown_ex); + case SEV_CMD_SNP_INIT_EX: return sizeof(struct sev_data_snp_init_ex); case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); @@ -158,6 +160,20 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); + case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_addr); + case SEV_CMD_SNP_LAUNCH_START: return sizeof(struct sev_data_snp_launch_start); + case SEV_CMD_SNP_LAUNCH_UPDATE: return sizeof(struct sev_data_snp_launch_update); + case SEV_CMD_SNP_ACTIVATE: return sizeof(struct sev_data_snp_activate); + case SEV_CMD_SNP_DECOMMISSION: return sizeof(struct sev_data_snp_addr); + case SEV_CMD_SNP_PAGE_RECLAIM: return sizeof(struct sev_data_snp_page_reclaim); + case SEV_CMD_SNP_GUEST_STATUS: return sizeof(struct sev_data_snp_guest_status); + case SEV_CMD_SNP_LAUNCH_FINISH: return sizeof(struct sev_data_snp_launch_finish); + case SEV_CMD_SNP_DBG_DECRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_DBG_ENCRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_PAGE_UNSMASH: return sizeof(struct sev_data_snp_page_unsmash); + case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_addr); + case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); + case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); default: return 0; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 7fd17e82bab4..006e4cdbeb78 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -78,6 +78,36 @@ enum sev_cmd { SEV_CMD_DBG_DECRYPT = 0x060, SEV_CMD_DBG_ENCRYPT = 0x061, + /* SNP specific commands */ + SEV_CMD_SNP_INIT = 0x081, + SEV_CMD_SNP_SHUTDOWN = 0x082, + SEV_CMD_SNP_PLATFORM_STATUS = 0x083, + SEV_CMD_SNP_DF_FLUSH = 0x084, + SEV_CMD_SNP_INIT_EX = 0x085, + SEV_CMD_SNP_SHUTDOWN_EX = 0x086, + SEV_CMD_SNP_DECOMMISSION = 0x090, + SEV_CMD_SNP_ACTIVATE = 0x091, + SEV_CMD_SNP_GUEST_STATUS = 0x092, + SEV_CMD_SNP_GCTX_CREATE = 0x093, + SEV_CMD_SNP_GUEST_REQUEST = 0x094, + SEV_CMD_SNP_ACTIVATE_EX = 0x095, + SEV_CMD_SNP_LAUNCH_START = 0x0A0, + SEV_CMD_SNP_LAUNCH_UPDATE = 0x0A1, + SEV_CMD_SNP_LAUNCH_FINISH = 0x0A2, + SEV_CMD_SNP_DBG_DECRYPT = 0x0B0, + SEV_CMD_SNP_DBG_ENCRYPT = 0x0B1, + SEV_CMD_SNP_PAGE_SWAP_OUT = 0x0C0, + SEV_CMD_SNP_PAGE_SWAP_IN = 0x0C1, + SEV_CMD_SNP_PAGE_MOVE = 0x0C2, + SEV_CMD_SNP_PAGE_MD_INIT = 0x0C3, + SEV_CMD_SNP_PAGE_SET_STATE = 0x0C6, + SEV_CMD_SNP_PAGE_RECLAIM = 0x0C7, + SEV_CMD_SNP_PAGE_UNSMASH = 0x0C8, + SEV_CMD_SNP_CONFIG = 0x0C9, + SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX = 0x0CA, + SEV_CMD_SNP_COMMIT = 0x0CB, + SEV_CMD_SNP_VLEK_LOAD = 0x0CD, + SEV_CMD_MAX, }; @@ -523,6 +553,241 @@ struct sev_data_attestation_report { u32 len; /* In/Out */ } __packed; +/** + * struct sev_data_snp_download_firmware - SNP_DOWNLOAD_FIRMWARE command params + * + * @address: physical address of firmware image + * @len: length of the firmware image + */ +struct sev_data_snp_download_firmware { + u64 address; /* In */ + u32 len; /* In */ +} __packed; + +/** + * struct sev_data_snp_activate - SNP_ACTIVATE command params + * + * @gctx_paddr: system physical address guest context page + * @asid: ASID to bind to the guest + */ +struct sev_data_snp_activate { + u64 gctx_paddr; /* In */ + u32 asid; /* In */ +} __packed; + +/** + * struct sev_data_snp_addr - generic SNP command params + * + * @address: physical address of generic data param + */ +struct sev_data_snp_addr { + u64 address; /* In/Out */ +} __packed; + +/** + * struct sev_data_snp_launch_start - SNP_LAUNCH_START command params + * + * @gctx_paddr: system physical address of guest context page + * @policy: guest policy + * @ma_gctx_paddr: system physical address of migration agent + * @ma_en: the guest is associated with a migration agent + * @imi_en: launch flow is launching an IMI (Incoming Migration Image) for the + * purpose of guest-assisted migration. + * @rsvd: reserved + * @gosvw: guest OS-visible workarounds, as defined by hypervisor + */ +struct sev_data_snp_launch_start { + u64 gctx_paddr; /* In */ + u64 policy; /* In */ + u64 ma_gctx_paddr; /* In */ + u32 ma_en:1; /* In */ + u32 imi_en:1; /* In */ + u32 rsvd:30; + u8 gosvw[16]; /* In */ +} __packed; + +/* SNP support page type */ +enum { + SNP_PAGE_TYPE_NORMAL = 0x1, + SNP_PAGE_TYPE_VMSA = 0x2, + SNP_PAGE_TYPE_ZERO = 0x3, + SNP_PAGE_TYPE_UNMEASURED = 0x4, + SNP_PAGE_TYPE_SECRET = 0x5, + SNP_PAGE_TYPE_CPUID = 0x6, + + SNP_PAGE_TYPE_MAX +}; + +/** + * struct sev_data_snp_launch_update - SNP_LAUNCH_UPDATE command params + * + * @gctx_paddr: system physical address of guest context page + * @page_size: page size 0 indicates 4K and 1 indicates 2MB page + * @page_type: encoded page type + * @imi_page: indicates that this page is part of the IMI (Incoming Migration + * Image) of the guest + * @rsvd: reserved + * @rsvd2: reserved + * @address: system physical address of destination page to encrypt + * @rsvd3: reserved + * @vmpl1_perms: VMPL permission mask for VMPL1 + * @vmpl2_perms: VMPL permission mask for VMPL2 + * @vmpl3_perms: VMPL permission mask for VMPL3 + * @rsvd4: reserved + */ +struct sev_data_snp_launch_update { + u64 gctx_paddr; /* In */ + u32 page_size:1; /* In */ + u32 page_type:3; /* In */ + u32 imi_page:1; /* In */ + u32 rsvd:27; + u32 rsvd2; + u64 address; /* In */ + u32 rsvd3:8; + u32 vmpl1_perms:8; /* In */ + u32 vmpl2_perms:8; /* In */ + u32 vmpl3_perms:8; /* In */ + u32 rsvd4; +} __packed; + +/** + * struct sev_data_snp_launch_finish - SNP_LAUNCH_FINISH command params + * + * @gctx_paddr: system physical address of guest context page + * @id_block_paddr: system physical address of ID block + * @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 + * @rsvd: reserved + * @host_data: host-supplied data for guest, not interpreted by firmware + */ +struct sev_data_snp_launch_finish { + u64 gctx_paddr; + u64 id_block_paddr; + u64 id_auth_paddr; + u8 id_block_en:1; + u8 auth_key_en:1; + u64 rsvd:62; + u8 host_data[32]; +} __packed; + +/** + * struct sev_data_snp_guest_status - SNP_GUEST_STATUS command params + * + * @gctx_paddr: system physical address of guest context page + * @address: system physical address of guest status page + */ +struct sev_data_snp_guest_status { + u64 gctx_paddr; + u64 address; +} __packed; + +/** + * struct sev_data_snp_page_reclaim - SNP_PAGE_RECLAIM command params + * + * @paddr: system physical address of page to be claimed. The 0th bit in the + * address indicates the page size. 0h indicates 4KB and 1h indicates + * 2MB page. + */ +struct sev_data_snp_page_reclaim { + u64 paddr; +} __packed; + +/** + * struct sev_data_snp_page_unsmash - SNP_PAGE_UNSMASH command params + * + * @paddr: system physical address of page to be unsmashed. The 0th bit in the + * address indicates the page size. 0h indicates 4 KB and 1h indicates + * 2 MB page. + */ +struct sev_data_snp_page_unsmash { + u64 paddr; +} __packed; + +/** + * struct sev_data_snp_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters + * + * @gctx_paddr: system physical address of guest context page + * @src_addr: source address of data to operate on + * @dst_addr: destination address of data to operate on + */ +struct sev_data_snp_dbg { + u64 gctx_paddr; /* In */ + u64 src_addr; /* In */ + u64 dst_addr; /* In */ +} __packed; + +/** + * struct sev_data_snp_guest_request - SNP_GUEST_REQUEST command params + * + * @gctx_paddr: system physical address of guest context page + * @req_paddr: system physical address of request page + * @res_paddr: system physical address of response page + */ +struct sev_data_snp_guest_request { + u64 gctx_paddr; /* In */ + u64 req_paddr; /* In */ + u64 res_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_init_ex - SNP_INIT_EX structure + * + * @init_rmp: indicate that the RMP should be initialized. + * @list_paddr_en: indicate that list_paddr is valid + * @rsvd: reserved + * @rsvd1: reserved + * @list_paddr: system physical address of range list + * @rsvd2: reserved + */ +struct sev_data_snp_init_ex { + u32 init_rmp:1; + u32 list_paddr_en:1; + u32 rsvd:30; + u32 rsvd1; + u64 list_paddr; + u8 rsvd2[48]; +} __packed; + +/** + * struct sev_data_range - RANGE structure + * + * @base: system physical address of first byte of range + * @page_count: number of 4KB pages in this range + * @rsvd: reserved + */ +struct sev_data_range { + u64 base; + u32 page_count; + u32 rsvd; +} __packed; + +/** + * struct sev_data_range_list - RANGE_LIST structure + * + * @num_elements: number of elements in RANGE_ARRAY + * @rsvd: reserved + * @ranges: array of num_elements of type RANGE + */ +struct sev_data_range_list { + u32 num_elements; + u32 rsvd; + struct sev_data_range ranges[]; +} __packed; + +/** + * struct sev_data_snp_shutdown_ex - SNP_SHUTDOWN_EX structure + * + * @len: length of the command buffer read by the PSP + * @iommu_snp_shutdown: Disable enforcement of SNP in the IOMMU + * @rsvd1: reserved + */ +struct sev_data_snp_shutdown_ex { + u32 len; + u32 iommu_snp_shutdown:1; + u32 rsvd1:31; +} __packed; + #ifdef CONFIG_CRYPTO_DEV_SP_PSP /** diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index b44ba7dcdefc..207e34217528 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -69,6 +69,12 @@ typedef enum { SEV_RET_RESOURCE_LIMIT, SEV_RET_SECURE_DATA_INVALID, SEV_RET_INVALID_KEY = 0x27, + SEV_RET_INVALID_PAGE_SIZE, + SEV_RET_INVALID_PAGE_STATE, + SEV_RET_INVALID_MDATA_ENTRY, + SEV_RET_INVALID_PAGE_OWNER, + SEV_RET_INVALID_PAGE_AEAD_OFLOW, + SEV_RET_RMP_INIT_REQUIRED, SEV_RET_MAX, } sev_ret_code; @@ -155,6 +161,56 @@ struct sev_user_data_get_id2 { __u32 length; /* In/Out */ } __packed; +/** + * struct sev_user_data_snp_status - SNP status + * + * @api_major: API major version + * @api_minor: API minor version + * @state: current platform state + * @is_rmp_initialized: whether RMP is initialized or not + * @rsvd: reserved + * @build_id: firmware build id for the API version + * @mask_chip_id: whether chip id is present in attestation reports or not + * @mask_chip_key: whether attestation reports are signed or not + * @vlek_en: VLEK (Version Loaded Endorsement Key) hashstick is loaded + * @rsvd1: reserved + * @guest_count: the number of guest currently managed by the firmware + * @current_tcb_version: current TCB version + * @reported_tcb_version: reported TCB version + */ +struct sev_user_data_snp_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 is_rmp_initialized:1; /* Out */ + __u8 rsvd:7; + __u32 build_id; /* Out */ + __u32 mask_chip_id:1; /* Out */ + __u32 mask_chip_key:1; /* Out */ + __u32 vlek_en:1; /* Out */ + __u32 rsvd1:29; + __u32 guest_count; /* Out */ + __u64 current_tcb_version; /* Out */ + __u64 reported_tcb_version; /* Out */ +} __packed; + +/** + * struct sev_user_data_snp_config - system wide configuration value for SNP. + * + * @reported_tcb: the TCB version to report in the guest attestation report. + * @mask_chip_id: whether chip id is present in attestation reports or not + * @mask_chip_key: whether attestation reports are signed or not + * @rsvd: reserved + * @rsvd1: reserved + */ +struct sev_user_data_snp_config { + __u64 reported_tcb ; /* In */ + __u32 mask_chip_id:1; /* In */ + __u32 mask_chip_key:1; /* In */ + __u32 rsvd:30; /* In */ + __u8 rsvd1[52]; +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Fri Jan 26 04:11:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37616C47422 for ; Fri, 26 Jan 2024 04:42:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6FFF6B0098; Thu, 25 Jan 2024 23:42:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C20436B0099; Thu, 25 Jan 2024 23:42:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A99C66B009D; Thu, 25 Jan 2024 23:42:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9681B6B0098 for ; Thu, 25 Jan 2024 23:42:25 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7172DA237C for ; Fri, 26 Jan 2024 04:42:25 +0000 (UTC) X-FDA: 81720215850.03.82434B5 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2071.outbound.protection.outlook.com [40.107.92.71]) by imf23.hostedemail.com (Postfix) with ESMTP id 57179140011 for ; Fri, 26 Jan 2024 04:42:21 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=otDMFbTD; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.71 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244141; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=If6ijxmQzNWdB92SQLJNvgxfRipsuZht06xFAygzb5c=; b=AR2OE86EkXLrVnTFoIJ6+2sDh4QwTBvcS8laM/K8MvugeWAsN6CelO1UcCMEQuT3vE9LKN Cc2jaXVtGPHbqcGCk2PylJicTQBkS/D8uXJouptwHvejbNXEA5JPC/VpfJK+n9sp/RRFF1 bc/Vu7MJCRLJvvw2BEbXQfpuSOb88xI= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244141; a=rsa-sha256; cv=pass; b=76vUmcuXVECddUZW/xDU7GBfQ5Odd6dGcgvddjrzVznXNRZv1QwMsqeqOIes82uww/pu5W qB6Lv6ezKsMcn/MZWeCoZWcwflsQHANcyqWb0WZD+X4ICRwr2kE01l597Q3TkeohJyCNwk lYrO5eO0AbL0GRog5b5kHnE9zmR933w= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=otDMFbTD; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.71 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aBDMH+F79fJBm978/vc7QQBZpS8nPWIw3cNxQYtHtQ1uxWCjk4/BRJuDSOsG82mQF9MQAUPRNwoO4mytX2fGTNaChzd4uvXwRq29WDLNPy/skh1x1Py8hMCIiPIZ0SYgmyMhHeJWuXVdJgFYNsqRfkYZbYb68s3cJqb3rprjBEIFE8L7B+p/osUFMtNLSa/d+2Qjs4SlXihYp35Ja0O+g4rptEX1BRQivn7Ec0k15DvA2A4bDjVxgkD3cFlFRl0xKlvxxUhuPR8uC48ZB//jsxTVe8Tf40UNDXuoyh8pjzz3y2N3hSw5b6NNVfLTmGb+eWyM37uGv6odnzFpDMnT6w== 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=If6ijxmQzNWdB92SQLJNvgxfRipsuZht06xFAygzb5c=; b=EV8OdSZELE2Tf8oDHvSWsoyFeovssWJpeZzMhQ0iyvbBr9EUosVVOeAbFjht9tskkadehYnmpmlf6Xp87rF21qWab6wQHttfZMou0zKMYo98AKG9i5aB5Y2XLErWIo8c9/BF6QMwcVWMQYIVYm+L1HXlQIKTzJsT1wh204RoWRhNHFSsL7dFOxjLvvhAbnvjK66KCAJaTlXl78dSpeb9M3PSgBX1Ie9axEZ+GOAnd2MUDGOdnyRaY7bo0T5P6XMDdcbZ/xDHd3h6dkHT3zbDqw5C+KbJD8dLlU87uzcHQ5wt0sJYVlkpZ+Ds5iSa9UIpfVjMv5unrV8bZLweRSyvsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=If6ijxmQzNWdB92SQLJNvgxfRipsuZht06xFAygzb5c=; b=otDMFbTDaYv5zh0cB9L2TpeFJFkTzsGiHhG0stcuL43SaeowGn53vEjqKESgkGGPBST3ZMMuF8D3NAZu1pfvmqb1H/hdaQItDwDO8TVvaIMRHyzG/ObbThZqaoZ8vcPb/MbKszN+z/Svd4d5O/84arN1ApgXrBNimmaSA39LCX8= Received: from BY5PR16CA0023.namprd16.prod.outlook.com (2603:10b6:a03:1a0::36) by PH8PR12MB6892.namprd12.prod.outlook.com (2603:10b6:510:1bc::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 04:42:15 +0000 Received: from MWH0EPF000971E3.namprd02.prod.outlook.com (2603:10b6:a03:1a0:cafe::c7) by BY5PR16CA0023.outlook.office365.com (2603:10b6:a03:1a0::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 04:42: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 MWH0EPF000971E3.mail.protection.outlook.com (10.167.243.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:42: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.34; Thu, 25 Jan 2024 22:42:13 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Jarkko Sakkinen Subject: [PATCH v2 13/25] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Thu, 25 Jan 2024 22:11:13 -0600 Message-ID: <20240126041126.1927228-14-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E3:EE_|PH8PR12MB6892:EE_ X-MS-Office365-Filtering-Correlation-Id: f6043c41-90da-4fee-3ffe-08dc1e292b39 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DMJNtwqlUkw4SSC1gL1CdNPf9vmKI883sRTd3eCPiFCzhv9ATdCA0fznJZI4CN3kiPrQHeW/2g8eF+DjtBXAQbs/or3zs6uo98jqnKQRdVSIxD2W2r6ov0gtQD3soXJTBC4fAAvuZfkkhpjQs0K5vV7NgEgbjvWpfZZIPVYv2XifV1wkmNAzmybwigPKbOb354wH4ZFVrHunTYz2OrUAuYCRby10vfs5fEIVRSRru4uD83LKpLsxsW7zHJPacgKAC3KBle9VC5kFt+pbqBok1AgoX0PHl5diePGnzmkyOWBRpd5X2YjRMozpqv+KLjTRPBc41ToZD7QuavblxiM6ncxwNk14LQO4/mrPNMomMCrHaELqfqdcE+WwRqL84tYQC4d9tygDoVc3rTcTnd+PvG7yvmPmogjU4kqGNfZaH09iQSrqreMMDvRU2QXVRd8tPPoDQdFAPW/MNvbZ2h53MrBlqVmRknCwfWENMwpKOsOBtMF9debNfmEPhKX1o8prrtrHFsBZ/Mq7V7Q2vfLYCkE2G2BYZsQXdJ6S25v68lGL/LwvG1rBEvMgZLIrpzOkew7RgnrrzbBvwC6aFu8z5x50oWxSnc0QI8Rj8/janPD9Y9HQ75LW0GbFM8awFWuMxriT7coYok/LZt7UGzb9zDYEqGZxH8014h7fzbISLsbnswn/eGXH1+YPUovlL4/dZW9ymLMIg3qW6eSCYJYbyl4FTiHBqFb+GD7vTw37b04y1WGhi7EwSxJEDuCeP+0w/IQJWb/utuGx2b+FobSYCJsauxA/UaliDF+NL8i0UqOk44XFrp/5XMIoVMjfUsDyiPbqku5xFOZoI70gU9n12w== 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)(4636009)(396003)(376002)(346002)(136003)(39860400002)(230173577357003)(230922051799003)(230273577357003)(1800799012)(186009)(82310400011)(64100799003)(451199024)(36840700001)(46966006)(40470700004)(40480700001)(40460700003)(41300700001)(4326008)(36860700001)(426003)(81166007)(82740400003)(356005)(336012)(2616005)(6916009)(70586007)(54906003)(83380400001)(316002)(1076003)(70206006)(86362001)(2906002)(36756003)(30864003)(26005)(16526019)(44832011)(8676002)(8936002)(7416002)(7406005)(5660300002)(478600001)(47076005)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:42:14.5869 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f6043c41-90da-4fee-3ffe-08dc1e292b39 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: MWH0EPF000971E3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6892 X-Rspamd-Queue-Id: 57179140011 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ja9puym8d6xk3b5wf3rbzq5d9hmbzu6i X-HE-Tag: 1706244141-491542 X-HE-Meta: U2FsdGVkX19Yb3stnzljJXxh+mU/rLBXKl8p00U5CJaUVTV7S+XgpfdejaKCf8ZHDAV79A2x9S8/c6gymeeG9pSLJfOVTekDNG9D4w+A/UaW9bC7jbSXdiiCwF97bteIUrG7bQo6m9NmNTYzcbBormIiInzB6+gHc5KJi0wXFYDVTXhtA9t1RfX7aoY3HONgOda+TxZblI4wc3md1QddX+pMmqclcdfcsGuCyLqZ9aJS5AyhobSw49V/M5LIQ3I1+0nIg0wkmS+NyNn+jp+7qHH63CU6sw5jhFZOwEleGjjnqraK+iZ08lf81IH+eKEWc2fKIOe5LWW/e1cRgFl3gpNQCGHQvts7WXRQ8diTBuYD3yAUK9gFL89nt8kXSWsxUYGjARKVQClzEwKl8ujV0pgcgnAlXgMkRkRRdCadUZPd5gj255+Tm5fLtpgHlA8bDOgH9jcwI9iKW6tObcu8wHGiFdj9ZCDrnlhmrkQifPZ0fHAEGOKjUYtxQlL146Ms9oI6i9C/uVODuYA8HzXpJ3rclPYoH41GwR4SE8b+drVD4S8IGPee8E4zKDhM14Fd20ARCHESrPEGynXDuPFYrarQIl/O+OnOfa7g9aE58pqFkz9IhsG0jEpbC6rYw4mDc9ORFxaWwuhzW3QuSmkd3WZmssy4vhPW5qtSOUHNgvszYMiR+x3QtBtLySUaTkWu/+QWyxxO4aGI/X2h10Y6HKNrWqAD7YxYkpt50Q/xc6pxTZNaHcB9xeO4IFgrFeeJ+I61UcSSRkaKwP1Z0d55h3mikWPFvqU9pysF3XMlCID7OuMtlTKTvpE9HA8rjGHXUPvKYDdhC47/tyJCd4C0MyLg5CdXrPFNVXgtbxto2QR94LWxKA6l2kd/TT+UwVANuu+jEsRa7QwZbSkSv49CUkxVFI0CcqpTNspker9tqbSMllGLDl31QweZ0RevJlJ3vOPD/eywIphZ4EYHD1C 6GTzsM2Y a1AsvD4h5cwLRe+1oeZ4OIZEKUaZARf6Mb+7EfXl6xtSWtGk2klGsuglFaqPARQMLjnUmWOJzk7R3QcjtCJjFuG3zCzHLIkxfV1fJM3Ks70LbQWz+e30NDQnREj+Yjn5KdFCdAZW0szP0goTO42Y2fAHjembtRAop+mnlkDeO60XcmV96HEyqQe1OX7Ntf0upSqCYinIIyMqFLVo0tSTJJldh6W175SMISgBQiuJT3i6FRhFF3XI1emBkLf1MDG1lTKBf8b+nWNjkz0gDq0Pb4JiibodVgaclnQJqP9xdZ3Hd2RA4tsf9dDhDrGQ1kjEzYhrv+pMF/LIj32pjskcHivxIysS+Gv4VY+Hu+ZNCIxJg0vqYHP8ac8IsIOUAF2rZ7D6VKswlKg6HpyI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. During the execution of SNP_INIT command, the firmware configures and enables SNP security policy enforcement in many system components. Some system components write to regions of memory reserved by early x86 firmware (e.g. UEFI). Other system components write to regions provided by the operation system, hypervisor, or x86 firmware. Such system components can only write to HV-fixed pages or Default pages. They will error when attempting to write to pages in other page states after SNP_INIT enables their SNP enforcement. Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list of system physical address ranges to convert into the HV-fixed page states during the RMP initialization. If INIT_RMP is 1, hypervisors should provide all system physical address ranges that the hypervisor will never assign to a guest until the next RMP re-initialization. For instance, the memory that UEFI reserves should be included in the range list. This allows system components that occasionally write to memory (e.g. logging to UEFI reserved regions) to not fail due to RMP initialization and SNP enablement. Note that SNP_INIT(_EX) must not be executed while non-SEV guests are executing, otherwise it is possible that the system could reset or hang. The psp_init_on_probe module parameter was added for SEV/SEV-ES support and the init_ex_path module parameter to allow for time for the necessary file system to be mounted/available. SNP_INIT(_EX) does not use the file associated with init_ex_path. So, to avoid running into issues where SNP_INIT(_EX) is called while there are other running guests, issue it during module probe regardless of the psp_init_on_probe setting, but maintain the previous deferrable handling for SEV/SEV-ES initialization. Signed-off-by: Brijesh Singh Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Tom Lendacky [mdr: squash in psp_init_on_probe changes from Tom, reduce proliferation of 'probe' function parameter where possible] Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 5 +- drivers/crypto/ccp/sev-dev.c | 280 ++++++++++++++++++++++++++++++++--- drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 17 ++- 4 files changed, 281 insertions(+), 23 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f760106c31f8..564091f386f7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -246,6 +246,7 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_platform_init_args init_args = {0}; int asid, ret; if (kvm->created_vcpus) @@ -262,7 +263,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_no_asid; sev->asid = asid; - ret = sev_platform_init(&argp->error); + init_args.probe = false; + ret = sev_platform_init(&init_args); if (ret) goto e_free; @@ -274,6 +276,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) return 0; e_free: + argp->error = init_args.error; sev_asid_free(sev); sev->asid = 0; e_no_asid: diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index e38986d39b63..712964469612 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -29,6 +29,7 @@ #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -37,6 +38,10 @@ #define SEV_FW_FILE "amd/sev.fw" #define SEV_FW_NAME_SIZE 64 +/* Minimum firmware version required for the SEV-SNP support */ +#define SNP_MIN_API_MAJOR 1 +#define SNP_MIN_API_MINOR 51 + static DEFINE_MUTEX(sev_cmd_mutex); static struct sev_misc_dev *misc_dev; @@ -80,6 +85,13 @@ static void *sev_es_tmr; #define NV_LENGTH (32 * 1024) static void *sev_init_ex_buffer; +/* + * SEV_DATA_RANGE_LIST: + * Array containing range of pages that firmware transitions to HV-fixed + * page state. + */ +struct sev_data_range_list *snp_range_list; + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -480,20 +492,163 @@ static inline int __sev_do_init_locked(int *psp_ret) return __sev_init_locked(psp_ret); } -static int __sev_platform_init_locked(int *error) +static void snp_set_hsave_pa(void *arg) +{ + wrmsrl(MSR_VM_HSAVE_PA, 0); +} + +static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg) +{ + struct sev_data_range_list *range_list = arg; + struct sev_data_range *range = &range_list->ranges[range_list->num_elements]; + size_t size; + + /* + * Ensure the list of HV_FIXED pages that will be passed to firmware + * do not exceed the page-sized argument buffer. + */ + if ((range_list->num_elements * sizeof(struct sev_data_range) + + sizeof(struct sev_data_range_list)) > PAGE_SIZE) + return -E2BIG; + + switch (rs->desc) { + case E820_TYPE_RESERVED: + case E820_TYPE_PMEM: + case E820_TYPE_ACPI: + range->base = rs->start & PAGE_MASK; + size = PAGE_ALIGN((rs->end + 1) - rs->start); + range->page_count = size >> PAGE_SHIFT; + range_list->num_elements++; + break; + default: + break; + } + + return 0; +} + +static int __sev_snp_init_locked(int *error) { - int rc = 0, psp_ret = SEV_RET_NO_FW_CALL; struct psp_device *psp = psp_master; + struct sev_data_snp_init_ex data; struct sev_device *sev; + void *arg = &data; + int cmd, rc = 0; - if (!psp || !psp->sev_data) + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) return -ENODEV; sev = psp->sev_data; + if (sev->snp_initialized) + return 0; + + if (!sev_version_greater_or_equal(SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR)) { + dev_dbg(sev->dev, "SEV-SNP support requires firmware version >= %d:%d\n", + SNP_MIN_API_MAJOR, SNP_MIN_API_MINOR); + return 0; + } + + /* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ + on_each_cpu(snp_set_hsave_pa, NULL, 1); + + /* + * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list + * of system physical address ranges to convert into HV-fixed page + * states during the RMP initialization. For instance, the memory that + * UEFI reserves should be included in the that list. This allows system + * components that occasionally write to memory (e.g. logging to UEFI + * reserved regions) to not fail due to RMP initialization and SNP + * enablement. + * + */ + if (sev_version_greater_or_equal(SNP_MIN_API_MAJOR, 52)) { + /* + * Firmware checks that the pages containing the ranges enumerated + * in the RANGES structure are either in the default page state or in the + * firmware page state. + */ + snp_range_list = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!snp_range_list) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX range list memory allocation failed\n"); + return -ENOMEM; + } + + /* + * Retrieve all reserved memory regions from the e820 memory map + * to be setup as HV-fixed pages. + */ + rc = walk_iomem_res_desc(IORES_DESC_NONE, IORESOURCE_MEM, 0, ~0, + snp_range_list, snp_filter_reserved_mem_regions); + if (rc) { + dev_err(sev->dev, + "SEV: SNP_INIT_EX walk_iomem_res_desc failed rc = %d\n", rc); + return rc; + } + + memset(&data, 0, sizeof(data)); + data.init_rmp = 1; + data.list_paddr_en = 1; + data.list_paddr = __psp_pa(snp_range_list); + cmd = SEV_CMD_SNP_INIT_EX; + } else { + cmd = SEV_CMD_SNP_INIT; + arg = NULL; + } + + /* + * The following sequence must be issued before launching the first SNP + * guest to ensure all dirty cache lines are flushed, including from + * updates to the RMP table itself via the RMPUPDATE instruction: + * + * - WBINVD on all running CPUs + * - SEV_CMD_SNP_INIT[_EX] firmware command + * - WBINVD on all running CPUs + * - SEV_CMD_SNP_DF_FLUSH firmware command + */ + wbinvd_on_all_cpus(); + + rc = __sev_do_cmd_locked(cmd, arg, error); + if (rc) + return rc; + + /* Prepare for first SNP guest launch after INIT. */ + wbinvd_on_all_cpus(); + rc = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, error); + if (rc) + return rc; + + sev->snp_initialized = true; + dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + + return rc; +} + +static int __sev_platform_init_locked(int *error) +{ + int rc, psp_ret = SEV_RET_NO_FW_CALL; + struct sev_device *sev; + + if (!psp_master || !psp_master->sev_data) + return -ENODEV; + + sev = psp_master->sev_data; + if (sev->state == SEV_STATE_INIT) return 0; + if (!sev_es_tmr) { + /* Obtain the TMR memory area for SEV-ES use */ + sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); + if (sev_es_tmr) + /* Must flush the cache before giving it to the firmware */ + clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); + else + dev_warn(sev->dev, + "SEV: TMR allocation failed, SEV-ES support unavailable\n"); + } + if (sev_init_ex_buffer) { rc = sev_read_init_ex_file(); if (rc) @@ -536,12 +691,46 @@ static int __sev_platform_init_locked(int *error) return 0; } -int sev_platform_init(int *error) +static int _sev_platform_init_locked(struct sev_platform_init_args *args) +{ + struct sev_device *sev; + int rc; + + if (!psp_master || !psp_master->sev_data) + return -ENODEV; + + sev = psp_master->sev_data; + + if (sev->state == SEV_STATE_INIT) + return 0; + + /* + * Legacy guests cannot be running while SNP_INIT(_EX) is executing, + * so perform SEV-SNP initialization at probe time. + */ + rc = __sev_snp_init_locked(&args->error); + if (rc && rc != -ENODEV) { + /* + * Don't abort the probe if SNP INIT failed, + * continue to initialize the legacy SEV firmware. + */ + dev_err(sev->dev, "SEV-SNP: failed to INIT rc %d, error %#x\n", + rc, args->error); + } + + /* Defer legacy SEV/SEV-ES support if allowed by caller/module. */ + if (args->probe && !psp_init_on_probe) + return 0; + + return __sev_platform_init_locked(&args->error); +} + +int sev_platform_init(struct sev_platform_init_args *args) { int rc; mutex_lock(&sev_cmd_mutex); - rc = __sev_platform_init_locked(error); + rc = _sev_platform_init_locked(args); mutex_unlock(&sev_cmd_mutex); return rc; @@ -852,6 +1041,55 @@ static int sev_update_firmware(struct device *dev) return ret; } +static int __sev_snp_shutdown_locked(int *error) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_shutdown_ex data; + int ret; + + if (!sev->snp_initialized) + return 0; + + memset(&data, 0, sizeof(data)); + data.len = sizeof(data); + data.iommu_snp_shutdown = 1; + + wbinvd_on_all_cpus(); + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, error); + /* SHUTDOWN may require DF_FLUSH */ + if (*error == SEV_RET_DFFLUSH_REQUIRED) { + ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, NULL); + if (ret) { + dev_err(sev->dev, "SEV-SNP DF_FLUSH failed\n"); + return ret; + } + /* reissue the shutdown command */ + ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, + error); + } + if (ret) { + dev_err(sev->dev, "SEV-SNP firmware shutdown failed\n"); + return ret; + } + + sev->snp_initialized = false; + dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); + + return ret; +} + +static int sev_snp_shutdown(int *error) +{ + int rc; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_shutdown_locked(error); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} + static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) { struct sev_device *sev = psp_master->sev_data; @@ -1299,6 +1537,8 @@ int sev_dev_init(struct psp_device *psp) static void sev_firmware_shutdown(struct sev_device *sev) { + int error; + sev_platform_shutdown(NULL); if (sev_es_tmr) { @@ -1315,6 +1555,13 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer = NULL; } + + if (snp_range_list) { + kfree(snp_range_list); + snp_range_list = NULL; + } + + sev_snp_shutdown(&error); } void sev_dev_destroy(struct psp_device *psp) @@ -1345,7 +1592,8 @@ EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); void sev_pci_init(void) { struct sev_device *sev = psp_master->sev_data; - int error, rc; + struct sev_platform_init_args args = {0}; + int rc; if (!sev) return; @@ -1370,23 +1618,15 @@ void sev_pci_init(void) } } - /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); - if (sev_es_tmr) - /* Must flush the cache before giving it to the firmware */ - clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); - else - dev_warn(sev->dev, - "SEV: TMR allocation failed, SEV-ES support unavailable\n"); - - if (!psp_init_on_probe) - return; - /* Initialize the platform */ - rc = sev_platform_init(&error); + args.probe = true; + rc = sev_platform_init(&args); if (rc) dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n", - error, rc); + args.error, rc); + + dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_initialized ? + "-SNP" : "", sev->api_major, sev->api_minor, sev->build); return; diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 778c95155e74..85506325051a 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -52,6 +52,8 @@ struct sev_device { u8 build; void *cmd_buf; + + bool snp_initialized; }; int sev_dev_init(struct psp_device *psp); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 006e4cdbeb78..8128de17f0f4 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -790,10 +790,23 @@ struct sev_data_snp_shutdown_ex { #ifdef CONFIG_CRYPTO_DEV_SP_PSP +/** + * struct sev_platform_init_args + * + * @error: SEV firmware error code + * @probe: True if this is being called as part of CCP module probe, which + * will defer SEV_INIT/SEV_INIT_EX firmware initialization until needed + * unless psp_init_on_probe module param is set + */ +struct sev_platform_init_args { + int error; + bool probe; +}; + /** * sev_platform_init - perform SEV INIT command * - * @error: SEV command return code + * @args: struct sev_platform_init_args to pass in arguments * * Returns: * 0 if the SEV successfully processed the command @@ -802,7 +815,7 @@ struct sev_data_snp_shutdown_ex { * -%ETIMEDOUT if the SEV command timed out * -%EIO if the SEV returned a non-zero return code */ -int sev_platform_init(int *error); +int sev_platform_init(struct sev_platform_init_args *args); /** * sev_platform_status - perform SEV PLATFORM_STATUS command From patchwork Fri Jan 26 04:11:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2DE3C47DDB for ; Fri, 26 Jan 2024 04:42:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A4726B009D; Thu, 25 Jan 2024 23:42:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 653576B009F; Thu, 25 Jan 2024 23:42:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 51BAF6B00A0; Thu, 25 Jan 2024 23:42:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 41EFA6B009D for ; Thu, 25 Jan 2024 23:42:41 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E4C241C1180 for ; Fri, 26 Jan 2024 04:42:40 +0000 (UTC) X-FDA: 81720216480.19.E07D41E Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2080.outbound.protection.outlook.com [40.107.102.80]) by imf27.hostedemail.com (Postfix) with ESMTP id 1EDF940005 for ; Fri, 26 Jan 2024 04:42:37 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KhZVpj0s; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.80 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244158; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gIRT4X+BHmhFMwKgtoHtLtgItgyKgjXL4IM1rRR5oe0=; b=RPqo/j4mcCbHd+GQnNcY/dVJFd4y91qxPmIPH9m/B39lYYJFpCwt+vvzV2gXeKZsrxG59/ HbgctkF99Y4b9Pmd2N/qbWu++N0+4yxstV0fare4Go81UDOpb4Pzl4Zzs2hVR1MGZ3L71/ 9nELGc7A+7c95UcDziu2cfsjz6qSYo4= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KhZVpj0s; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.102.80 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244158; a=rsa-sha256; cv=pass; b=maYUbZkuYTx5v7AVsLcv3rRt3t42Ec1vpbKNzv359z4WSti6WI+C40mjcfgIEyYv+Vl/qc wWxGGpIZ5DSlOWVB4V3C9B0ZgTExcLtluOXjTxu3RG5DbdBG5PgkEsUUp85ZPMnQDQHX3+ HG81kURpXidkJYYK2D7I1h3gKmgmGBo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZWiyBsvkaStF8zJ0Kh+IHwLKdq7Gre2oAlu9F1h8h0d8FQNP5hF8M5qyDtO8sOZ5Mo+cxzZYyeWVBwASWx3qdu7sJYmxD3TK0d/xSKzNtXuEc9KSNw7Aj+qA1gQA+0Bn6mVzXBV1NhUB1kXC27PTI41kTgUfxgTsJ3CA4Tl8dijdxUOQ/taDC3m4Ri/ws8OAGQC4MLhsf8Ipw7ru3XKTzFord9vSc/O2E0wIkRJdAKdXAkRTodviF9pDWSr63JugDIiyslQMXyE4Vhtm/tQehNhAj5CDurH42S1sO3WmjTz2Mkb8OWSMtc3pAax1BvZHsjAgcdqVu80oquKf4JOaKQ== 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=gIRT4X+BHmhFMwKgtoHtLtgItgyKgjXL4IM1rRR5oe0=; b=CjG3dUY/lL8i21jrBBNVB9SaURWGdeCSEU/Lw13UFclVDuKZGtEFzDC+PXcmtyDt+UaDN9lRabC5ZY4HpPcr2Dt+zgio5ILjkRe+CuGl3wqkkwkDTGU1wF40HLqAxdUOItiiiiG5ibmIafjBIMO0C8L5Z8Z4ZA/T4J5usp1wnKymlAZpyhVykEksVFYy4I+Ybki5ePWbh01K5Hb9ZvowPFNXbZ4E1dTuzk4wz+R7EPNY50dvBcN+HWP8LCUCTcJ6F7/4chfPRIaROatug92My27NZoivN563/bXYAMwHPIehNHBjoPEcl51xluavLZOBjO+Ab0j5PVY8p2Qm8TzSFg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=gIRT4X+BHmhFMwKgtoHtLtgItgyKgjXL4IM1rRR5oe0=; b=KhZVpj0skMvlZJBljCeQCjzHpn3N/Pz81st6X4r8JOKFSaIZFtqbe6+z4zPxA2Rlw6PNk4XdlsuMs43xBe/3b7ZzhdvFE4JMxKaPSU+Cu6sMDQ0FqCnj4yGwv5bUxFwBRt3oruaOow4JC2CWvw/qU6o7VanLoopUdyHAtlQgg/8= Received: from MW4PR03CA0287.namprd03.prod.outlook.com (2603:10b6:303:b5::22) by SN7PR12MB7833.namprd12.prod.outlook.com (2603:10b6:806:344::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:42:35 +0000 Received: from MWH0EPF000971E4.namprd02.prod.outlook.com (2603:10b6:303:b5:cafe::dd) by MW4PR03CA0287.outlook.office365.com (2603:10b6:303:b5::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:42:35 +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 MWH0EPF000971E4.mail.protection.outlook.com (10.167.243.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:42: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.34; Thu, 25 Jan 2024 22:42:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 14/25] crypto: ccp: Provide API to issue SEV and SNP commands Date: Thu, 25 Jan 2024 22:11:14 -0600 Message-ID: <20240126041126.1927228-15-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E4:EE_|SN7PR12MB7833:EE_ X-MS-Office365-Filtering-Correlation-Id: 9ee5e0df-eb93-40b6-caff-08dc1e29378a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uZkSYZdwzoWmMd6bEqa0pFnR+a8nQeMRW79xJMbALVR61dmbwhviDXxuxduFv1Tsvpk8zELywWoonGZFoOu/9ixzsMVWEXKh6sI6xWXsmAsUSz4QN6tdOU5zByE3IV/ayV0L0/wBSuxIfFOj3eHEs/GeeKi8Sa2d6O24J/EEQSb+kE2RhbFdPJkOaKm+a4NFq0+3/pUr16Hc+l+e3oigLwkqAFNEHnE+wcDUEQC6JsSbb/CXXwLz+5jvv9F3kxV4bts7OWBjpHkMj2wUY6XKo0T7vN2MMuzxep2bp9KU2/eiQV4QhIGTrA0/7+Gk4kMguOGVw/2opJTSlo6HjaStDtS33SMPiKZVtEAIg5xcZgg8l28KAeGw8rCfaCgMe8dPTmWQHg+eGSDZG21jQiOTTYiRe6UztT90+2y44GQbkwTDmKNm9iZZUlvbFmVvQ3O6K39gYJxASnv3lqU3tDEMO9QsRPbDIqNg1Pp05ovthqCGvpm2f3QZyVd9gkvqvMxF/Gpz8LOsrCDLne9GreIbRrcCSANn1Lcla4mqSzama2f7ZCUc7kA6p/jBLmY7ZrJCLy7oQbXn/7kDQIrJIF7crwDIW956RB8J5e70E+2cz5CCt0JDj1//9awKfyGBSHz1QKT1KG5sZvCPz+/igA5hV6OHVleMYCOiul1BeZxsNfz4A9cGZmrxcnz7HHKKmN1hzM8JxTCqOu2bF2MCSZ+S7bGxarlnU1hQKCqKN30ICs/b4DQzEJ/Boxf5F0CsDXZXE2WlJwTDY/smrW6F4kwzUiP2mgYnjEq7xphM3yxG0VU= 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)(4636009)(136003)(376002)(39860400002)(346002)(396003)(230922051799003)(64100799003)(82310400011)(186009)(451199024)(1800799012)(40470700004)(46966006)(36840700001)(36860700001)(47076005)(86362001)(2906002)(36756003)(41300700001)(81166007)(82740400003)(356005)(54906003)(478600001)(6916009)(70206006)(2616005)(70586007)(5660300002)(426003)(7406005)(6666004)(336012)(44832011)(7416002)(8676002)(26005)(1076003)(8936002)(316002)(83380400001)(16526019)(4326008)(40460700003)(40480700001)(36900700001)(134885004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:42:35.2540 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9ee5e0df-eb93-40b6-caff-08dc1e29378a 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: MWH0EPF000971E4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7833 X-Rspam-User: X-Stat-Signature: n8zf36cx5yg5w75iss3hhfxo3td77n3i X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1EDF940005 X-HE-Tag: 1706244157-615044 X-HE-Meta: U2FsdGVkX1/cHEEq4b4KLqDDdNwOCiTJyW3j9TiZrN04hl4JNKpBCDosbXq2BybKicTz3duTQ+SekkA11/N5YKdfubCiNtyIFxk9EIWGBDiPsJGRYLWARNx0pXH74kWEPrXtTzVDFSSyMeMTb2fDfhyFilwpP0ZDqlpwYohJbD/MrAQ8KDhoTssU7mrRNHRoybVHz8amsVdD9MrywjRlLofEfoHb1EqOgNZIySv7W0VplOKehz/O4uocpfvpxMa2DYk4pzzZE3jksRV3EvtDZ41pTZ630YPetGn2RDoqubTmcw6uorKZbPlaAGTGwms8DUD3pnbFAYBT/eIHecV4M036i++1gLKBQAfcTSf11jh5ElCoa9CuIoaXdvox0X/sPc+om+KHLcZa/+o1+pQKn4rezPMNK1ycnQoBbvdcM1koZV5aPNkeA2aDX0i6CsnIbsDtYGlfiIgnBTYP7YmQPAx4u9inkSxWz9afUIc9o6b6DS/7cIz7tTlkxm14wHfZeMW1UTSUEIw1e2PGG9FUNXg7aCwEzOgy0C7LADrinsNeloBPQTCOmqKsY3b3ZIPoYc5BATMcH94d7Plg521P2dgyJdiR03rRYesP97YK38ggYyBz2de0KaM95uNus3WNs3nkUoiXTwF7cxcqZdLFGuqlHi87vGZPELpJAbWF5x1tLASwNepT37yDgLs0e4bd5wL45EqTieihN3HBlQUe4v6lLjwzqui7W9kzy8ppl3kb/OFP8NvknHOHk1V+r1O/gh9RDkwoxSWtfaznk8ef1AjwNtMksQPJDv5TfIf8prOJ8W7rPO+LnTDLJc0xqeVxir4YSKFFPKS74/aXT/5lCaJKQxlSs9OOiR/LUh9bxy1T6KGxcXCXMskgjXj7pQI7SPj/94E/OdOEJFa6C7Pw+sDo6CFaKD5NFbfrDOkWi0bbCjgav0iHgfXkW4od4/neOLRnuCbZN8XI6I+n2kt xMA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Export sev_do_cmd() as a generic API for the hypervisor to issue commands to manage an SEV and SNP guest. The commands for SEV and SNP are defined in the SEV and SEV-SNP firmware specifications. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra [mdr: kernel-doc fixups] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 3 ++- include/linux/psp-sev.h | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 712964469612..abee1a68d609 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -431,7 +431,7 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) return ret; } -static int sev_do_cmd(int cmd, void *data, int *psp_ret) +int sev_do_cmd(int cmd, void *data, int *psp_ret) { int rc; @@ -441,6 +441,7 @@ static int sev_do_cmd(int cmd, void *data, int *psp_ret) return rc; } +EXPORT_SYMBOL_GPL(sev_do_cmd); static int __sev_init_locked(int *error) { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 8128de17f0f4..c7dd6ff9f36b 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -915,6 +915,22 @@ int sev_guest_df_flush(int *error); */ int sev_guest_decommission(struct sev_data_decommission *data, int *error); +/** + * sev_do_cmd - issue an SEV or an SEV-SNP command + * + * @cmd: SEV or SEV-SNP firmware command to issue + * @data: arguments for firmware command + * @psp_ret: SEV command return code + * + * Returns: + * 0 if the SEV device successfully processed the command + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +int sev_do_cmd(int cmd, void *data, int *psp_ret); + void *psp_copy_user_blob(u64 uaddr, u32 len); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -930,6 +946,9 @@ sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENO static inline int sev_guest_decommission(struct sev_data_decommission *data, int *error) { return -ENODEV; } +static inline int +sev_do_cmd(int cmd, void *data, int *psp_ret) { return -ENODEV; } + static inline int sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; } From patchwork Fri Jan 26 04:11:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532028 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91D39C47422 for ; Fri, 26 Jan 2024 04:43:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2AD056B006E; Thu, 25 Jan 2024 23:43:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 25CD96B00A0; Thu, 25 Jan 2024 23:43:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 124A88D0001; Thu, 25 Jan 2024 23:43:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 049DB6B006E for ; Thu, 25 Jan 2024 23:43:02 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D6DE61604D1 for ; Fri, 26 Jan 2024 04:43:01 +0000 (UTC) X-FDA: 81720217362.23.E875231 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2085.outbound.protection.outlook.com [40.107.243.85]) by imf27.hostedemail.com (Postfix) with ESMTP id E195A40005 for ; Fri, 26 Jan 2024 04:42:58 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=v8hHeC2Z; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244179; a=rsa-sha256; cv=pass; b=R8RLS5sXfZzvhvcC7hdiicj8WcLvoyu88Z92hVDxHo0UinQF9eZVggLzQhDMzZDJHJNgZO xoMceC2XKKamlbTbKmNUg4pvBRp5W3AK60aM5h9VTqEEhB5nzCepwQiGPahk7XiUCRtshr RU2VuFFEbWJHssJg4fyz9bqF/MxWi6M= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=v8hHeC2Z; spf=pass (imf27.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.243.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244179; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=szaFyuq6iYA2lOSQBax06TCzQzV99C7hyp/FnL8i7Hc=; b=PZYZv2P4jqgwsARjerwzVQ0kgOZvIlT9kvJKcqBHRoBBWBKBvq8UO6OLe2q91Q3i9KseaN ZxaTdt8cNYLewBchoynhcifxKQR9+XIkY6SDgGq1A5s+sN45B/U2CWfDVxIgm5aYbHDhLY CNkn9dNXo/8TdZ/QICWnggITnzLow38= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K3KHWAjOLss0WZGMPcO93hZBx4wMrFIl5x6c8R4VZDaeZKvg7sM6WTI1Y1RhuGD4yx8W2U2gb6pm95jGsj4Ck0StcIj9nlNCcIGT3yh/KTToi21eJpgumttIvTG5sj4AuroS+2l9zufe8p2ArdGAi+rX2zFte1KWp7gQH7hvHFFIq4I0uLJL9M8CYALuA+TBxLAXPlztFfyd4QBOFeVkhvDsx+nmRNMfg9Y2rsN2U81Pnhn+2CeS33nnP36vfpNq5WNTyaidrrdxiNJ5WRtnKQq9R2t7HAABa9dT9/0AfZ3nCpGz3teXthMEJIV1HnV5ISXrIn5lRHLcC4h8Kq/fqg== 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=szaFyuq6iYA2lOSQBax06TCzQzV99C7hyp/FnL8i7Hc=; b=Syb6hp+01eXb9Kf8lTtpasYkhYeUv+BkC5eUJKc/KRODblm2lYxyoerlh6HEk1R2OAXQXN2qBKdtE6grnlMbdTplkT1jSJZGGLK4entwZ2Y4suxYOFJbOEtM2xVB5JbULKtZQ/MXk3Edsnc+7+6bWTwuzBgsDWhTSUYmJsbdnFAh1xCmYo/YDv8xEuCK39GxYh1nBz3k8DMpxjDdWpDVRAmP20J6bB4f+8dM1vjXqEP0IIeyr5ewTrCpk/FDHI1Qb5YEGoEKKryHh+f6QU17aMMq68s0Tc8ZXVVUtASsx/R7LTv0x0eQbr9/kaqPgRBNP/jtfOYS4hCGO6krVcjuZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=szaFyuq6iYA2lOSQBax06TCzQzV99C7hyp/FnL8i7Hc=; b=v8hHeC2ZdIIanohNyWdlhUZjGeSkSMI5qQZ7fV/BdYDTMknwJ0ZeTPUplf68QasTPI+cyEB9FpjCD4JYsw5Bftu+Xhl9xR+iJRpNKaKX6dzA6Z2ABvX8uANb32EUz/3st4lUNINQg/cu4Ks+dVwckXBUKWBtgCljiK2RNwNLkoQ= Received: from MW4PR02CA0028.namprd02.prod.outlook.com (2603:10b6:303:16d::7) by SA0PR12MB4543.namprd12.prod.outlook.com (2603:10b6:806:9d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.33; Fri, 26 Jan 2024 04:42:56 +0000 Received: from MWH0EPF000971E8.namprd02.prod.outlook.com (2603:10b6:303:16d:cafe::9) by MW4PR02CA0028.outlook.office365.com (2603:10b6:303:16d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:42:55 +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 MWH0EPF000971E8.mail.protection.outlook.com (10.167.243.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:42:55 +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.34; Thu, 25 Jan 2024 22:42:54 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 15/25] x86/sev: Introduce snp leaked pages list Date: Thu, 25 Jan 2024 22:11:15 -0600 Message-ID: <20240126041126.1927228-16-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E8:EE_|SA0PR12MB4543:EE_ X-MS-Office365-Filtering-Correlation-Id: b8048db2-57b5-4e12-66fd-08dc1e2943cc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hr7bMntL6Ht3VXhxEwbcLo8pEpeNGsPsvPPPpQJXzNGfYzxT0+SECARMwBUcFeGQ95bjUlnIGg4VQJo6GoAaG1qmQv+SKnrQ92zKzGAK7ND2qIutYu1D7dtgWTlBzfKbs+W+75EYumDz1vFrGntvjKTj/nkIcGf9+DDZEVkZELSUQ21Vqygh7Z2yxcXqKqHaQSs+O/zZmcRvnzP7j3/XBHt/HxX9WGDZzQYADL0zmQj4gLeDlPVVLgRnL9y66QPvyT6wrL37+IxoWcGShg0I3u7zqrO71xe1j2HUqzQ+U4aZBIuq4AP8Mv1qSKUO8lpfCWBMy0LO7fNFCCKTeno4hyKGX17YOnJI5DHsRA9Yn385qWXhdQn4O4trVT0/+MiTtGHMdt1zleltjwnSUYBJnKJRmnMRzfkIuJFOmbNDnDvnz6mupgFs8uw1Wb72+yncGfH54hB31TVQ49NI8N+CPPqy7vEic6wodPdjzl1ZmHxrU/Ns9bSCK78XNwJZtolMchgKt/7g3ug5kCN4gwxzt1PoBofW/UDS39B7cSY2j2KYnv8lK0ws2Qcwki5NS7OiZH+2yvcoDVjHBDC5hpHPH2STzWtI5zWw8QHSo/sf+vxHHrehp1EgEnphhYWldqE9UD9dO9qfN474SzAzyLbPvnLqtBUs8aQrzlg+80U7IYI6K0XxHQktZBUkexho/PzyhoUagOJeexZKciE9ITY0GEliXVrzsx8d7cUqO6l2QJHF6Rmt6VdiSA/0LWSIWMQT76BR2N40K/RlU573Pnv+4w== 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)(4636009)(376002)(136003)(39860400002)(346002)(396003)(230922051799003)(82310400011)(64100799003)(451199024)(1800799012)(186009)(46966006)(40470700004)(36840700001)(356005)(81166007)(82740400003)(1076003)(41300700001)(2906002)(36756003)(6666004)(4326008)(40480700001)(336012)(40460700003)(426003)(8936002)(8676002)(83380400001)(26005)(86362001)(2616005)(70586007)(70206006)(54906003)(316002)(7416002)(16526019)(6916009)(47076005)(5660300002)(7406005)(478600001)(44832011)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:42:55.8323 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b8048db2-57b5-4e12-66fd-08dc1e2943cc 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: MWH0EPF000971E8.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4543 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E195A40005 X-Stat-Signature: an6qg6t91j3n1nbo6mdsxnbjjcmn4ros X-Rspam-User: X-HE-Tag: 1706244178-116280 X-HE-Meta: U2FsdGVkX18AEOhGvH363j1zexTNe3k1PTiaieC5KxOA8cbRqLKeeCMmSEdeIMOIMtyB5+JuZ0sMTYAK2S6X6IeukqJ6gvrGdbIL9KxyuACtkCIonMupWvYT6BN0pq7HX3/bt6n15nxJVzMvaxLOw+zJhvzAg5Lg2D9kPw0+MoV0ZReD3qPO0D/Z6dv3fBFGyIxUW3prlu4ZC2QjDa1fSJU52u2Xjrn5OqW7IYpfp8Yp8Q/18mwlybyBv89EkULHmkR287txm/5sAzQCpaEzw9lBsVzSchtRt4eTailU46WIV2xTjJTqwsjmgqk/ZjGttif6Fo27vDnDrGoa8DoF+MZkhZa/zpMKgoPr7lfxl85u1iXh4p3zY0RQKBKJNC6vko8W3b2Ks1MbVNM63sbddZYKirXR7I14lIzupy1bkkFk+MclEah6cJjyGRb3BYbksU9sTFJ6hoggoA2JcqJtwrB050651If9MvKM5TiEiv+ve9B19Eb05O1RZ70XAp83BbZn7on8yypviR03xZQgwOv5K7yoLOyEXNZ/WEBQkxvr52XCGoEs95/UxRvDa/k1xuqWbdQT66Z5d+tzlNnv61nFOgGA+kX5OHM/T4WcNbw0QTcnzDHijO4SMgEY3jcZy3sVva0jHH1RBwZIEcD/Vk5G3jIJfkqQRn0A8bqDw4nFqUgOGQS+MV9LMKEVonRfHTV/Q+Mqhc1fLpspIotmjiAtbKxjjNM8zcajeBX/SqsyOlfpYFKTAkneqdHEzTH/tpBwYdErkBuv3YBJh1deX6m6deb3HyJeObf8YuiGkFKyuvr+8WQyq+pQ1a6f6O38UBA1eRm6zXfeNMEfQypWEIcpC4d/Q7K8Zt7eNxBw8AfYUR9CcfS1HyWQEOg/p3EXamyq39MCxXEAccb6auReHCcxK0eUcYkKOkYnBsQEMxuBFDwLVk3UfceC8SZFsHUhiS48vDT0flmoP/QWmvz 2ASK8EAW 1efXsveM5Aq5uI+Y2i1qyXzGKmyBu0GHLj+6ESeWPnb7Xmc9aWIlKG2TX1jF/2mGg7pPf6kJ/JKVyMevJ2TMgsa2Z3iEO3abB6JIYonSG3az6kskx4K8alaYORbhW/RrCX1tzZZr3SY9P/+jfgFcyEpG824RrhwGjQ0yLbTI6rfQknuLJX8SFNCi3/evfuiHZSnDpH6Fyva1l+/OTnJdmgnLqqh7FFK3gw/SI/W4HQhEjbu8cmMSz79phkK3CPzcWV5ZNuu2cTNLYKV5Y6lOYA6yLN7nEPOrY8vCxS1pyPR4ufwF8o/QQit1fnmdf2WCWAe0K1wTygy7QtwgZ6VCMw8wL16EOjQB+rtFVSsZ09AM2OzSjFoNvOQ350A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Ashish Kalra Pages are unsafe to be released back to the page-allocator, if they have been transitioned to firmware/guest state and can't be reclaimed or transitioned back to hypervisor/shared state. In this case add them to an internal leaked pages list to ensure that they are not freed or touched/accessed to cause fatal page faults. Suggested-by: Vlastimil Babka Signed-off-by: Ashish Kalra [mdr: relocate to arch/x86/virt/svm/sev.c] Signed-off-by: Michael Roth Reviewed-by: Vlastimil Babka --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index d3ccb7a0c7e9..435ba9bc4510 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -264,6 +264,7 @@ void snp_dump_hva_rmpentry(unsigned long address); int psmash(u64 pfn); int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); int rmp_make_shared(u64 pfn, enum pg_level level); +void snp_leak_pages(u64 pfn, unsigned int npages); #else static inline bool snp_probe_rmptable_info(void) { return false; } static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } @@ -275,6 +276,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int as return -ENODEV; } static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } +static inline void snp_leak_pages(u64 pfn, unsigned int npages) {} #endif #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 1a13eff78c9d..649ac1bb6b0e 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -65,6 +65,11 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static LIST_HEAD(snp_leaked_pages_list); +static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); + +static unsigned long snp_nr_leaked_pages; + #undef pr_fmt #define pr_fmt(fmt) "SEV-SNP: " fmt @@ -505,3 +510,32 @@ int rmp_make_shared(u64 pfn, enum pg_level level) return rmpupdate(pfn, &state); } EXPORT_SYMBOL_GPL(rmp_make_shared); + +void snp_leak_pages(u64 pfn, unsigned int npages) +{ + struct page *page = pfn_to_page(pfn); + + pr_warn("Leaking PFN range 0x%llx-0x%llx\n", pfn, pfn + npages); + + spin_lock(&snp_leaked_pages_list_lock); + while (npages--) { + /* + * Reuse the page's buddy list for chaining into the leaked + * pages list. This page should not be on a free list currently + * and is also unsafe to be added to a free list. + */ + if (likely(!PageCompound(page)) || + (PageHead(page) && compound_nr(page) <= npages)) + /* + * Skip inserting tail pages of compound page as + * page->buddy_list of tail pages is not usable. + */ + list_add_tail(&page->buddy_list, &snp_leaked_pages_list); + dump_rmpentry(pfn); + snp_nr_leaked_pages++; + pfn++; + page++; + } + spin_unlock(&snp_leaked_pages_list_lock); +} +EXPORT_SYMBOL_GPL(snp_leak_pages); From patchwork Fri Jan 26 04:11:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D117C47422 for ; Fri, 26 Jan 2024 04:43:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0154B8D0003; Thu, 25 Jan 2024 23:43:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F07858D0001; Thu, 25 Jan 2024 23:43:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D81748D0003; Thu, 25 Jan 2024 23:43:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C75078D0001 for ; Thu, 25 Jan 2024 23:43:22 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9A07E14055B for ; Fri, 26 Jan 2024 04:43:22 +0000 (UTC) X-FDA: 81720218244.24.AE7C9FE Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2043.outbound.protection.outlook.com [40.107.237.43]) by imf09.hostedemail.com (Postfix) with ESMTP id A569C14001A for ; Fri, 26 Jan 2024 04:43:19 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Q0ioCkGN; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.43 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244199; a=rsa-sha256; cv=pass; b=QEAOqCyrOzhWxGyZT/wg77/UmY4L/FUTHxg822NZI+SewfKsUVAIpVAe/OW7ihB2eAO1cp EbPd6oKhOOpCRnKvxgPuCHASNwXoZVHVqD5gPzVGNjfTLauZ0WjeHdDK0YTHXnNcKfxQ36 8T0IKQgmvBE7SgDrTfeSSpuniEIaMOM= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Q0ioCkGN; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf09.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.43 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244199; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iS9xr7kd5bHfaLb97IAWDyOpOz+WLSM7PA7ZyGIuU6I=; b=f1CSQ5fKF6RbnMwK6bWK+dYo1I59JDrijM7eJKPqX9D/waPL4fmV3CHSdVtslB1GYcSJWb 6ZmBaFATSU6tYL8HRI4R+laOMCSedogHRbr252qWxil0X39WrhRM9Tu+B+RLdNXK0m+ZnY H5ek0sWFBnLczNXrhFyKlMhHA1wpWo8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ktlalzZaaFMKPnrIDo2+4ycTg/ir+fPk2IKQmzOscq6tGCyrsZdubT9JxUtYlppFKVOfaZkLRx3KP4lITgUEsMaOkwDW+9sCZ+VQzeEMHSwDsvNh++BI4ATRXpzkMOtyiTE5gy/7c/EeWFeiuneB4rEDWgh2h351LpIuxUS03j+ATJuElF3WAdDSU1jWQ7FQofK31ONqLy/iMYGE/Ki1qerXoFDhbW7LHhikgRyNgXxaoJd3GnBBQOjsDKTCB6b0nOdVBjbXS/1drivMFL8NinE7W9YjhpoMX9696rYmcgFoiAneNDhVNlo8n7GnCOwlKqEg5bNXEfewTcHLFr5kEQ== 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=iS9xr7kd5bHfaLb97IAWDyOpOz+WLSM7PA7ZyGIuU6I=; b=fq1u/nIcDLnlRZ/NHZsecIARNMwakibvqcJ+udgeJxlRR8YbXDLAVyWc/ekctQqMDrGnPu1Z+/kRnN1yOGap2cVaS5dFDsR4JKefAU3S7EF7Vquf9Rixf/nISlUD2cebFS7+xkQsClwXLqex3Ist2FSd5bMKqO+S9vM/o2Yd7PICOexVd1TDb6OqXZVx4vncpHcLJS4s+go2/EPrYffR640YfMBWK07LzDsSdF6RUakOlf0GBklN2FHW29oDcPgW6X4svQStJv54q7TP8PpAhwtTl+T+r9zaq+/hd/mkHLEsnGd7szgy46UqOG+woP1DtZyZ7mrtuAAwub9tYaOUVQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=iS9xr7kd5bHfaLb97IAWDyOpOz+WLSM7PA7ZyGIuU6I=; b=Q0ioCkGNuh4lvAJYjIJveQtG4DL6S7RY4yygdo0o17ezlCEyYFQrf8w2sdP3nH310ORzLH2Ibq3fs9U/iXUXaUTqf1C4fVmKSGqUkqeIgXBoBqqeToaQLhsjh6S0O9qT7J1G+PYoEeaAsbVzpNSWdQ77plK/RA2y8gZFLMXUCgU= Received: from BY5PR16CA0005.namprd16.prod.outlook.com (2603:10b6:a03:1a0::18) by LV2PR12MB5966.namprd12.prod.outlook.com (2603:10b6:408:171::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:43:17 +0000 Received: from MWH0EPF000971E3.namprd02.prod.outlook.com (2603:10b6:a03:1a0:cafe::38) by BY5PR16CA0005.outlook.office365.com (2603:10b6:a03:1a0::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22 via Frontend Transport; Fri, 26 Jan 2024 04:43:16 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by MWH0EPF000971E3.mail.protection.outlook.com (10.167.243.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:43:16 +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.34; Thu, 25 Jan 2024 22:43:15 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 16/25] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled Date: Thu, 25 Jan 2024 22:11:16 -0600 Message-ID: <20240126041126.1927228-17-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E3:EE_|LV2PR12MB5966:EE_ X-MS-Office365-Filtering-Correlation-Id: aa9438fe-1114-4aad-8e0f-08dc1e295045 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EXiB764r70Q7yvKppidWUfIR//0CNm7vhe5JI4DrekcjAdcrj12HA4Qz+JDOLGmCuoCndRHtkSmonr5V41wpAGzvN5qW0u5U05nx1SV+9kBnvpT9Z04Dtam9nQOA6p/+FOhUcfzAf+Z9tzdeiKMlo7vZ8FnGa18mB7rNwPS7RE/qRFOEcCvuphssom/fpgcnOXbHus4b2ufgJEcD8l2XaJjHzYe7BntBqftjadNW0G9eJUI/7itECRFwc33bk7zuBEvaK7MDBsBqrelwanfcqtwyxf5jo6JFBzuEI5M68EwnnvQPrYc440+5rjaF6mMn/votdZaRdsi0hWltP/RjZ43AzXYEsGi1ldIVG+XPa1l6kwZyFXSJ/Jb02UyHgkbXpVsh5mkFblADVmpR5xWjnLJQv3Ol3iUCwY+nHD/io8lp9mh7/N/dg4yVqJp62CarXZE27A6ChIrRdxcnEeb3Oj3XRRuRT160/o9K38CND0SXA/lkjNHkW5bpcNPnrcJ6VaoJBDR3VypLk1Od8vezr+Nebv1M1nw/LzTJzBJxNQ83hp0AI1HWf27E1wlaKwaqhzdqviJAKrgYhg6pvz5hIyyeJjAuaoPdpBTwQjy0qLeTx0RDX2aTGif1q2pzUypu2QRfGP82RWI+n20RKkDQkalKIULfMLReCZZb1dtj/uPo5C6OXYDxoOq2g6Os37aoYTAxvWuUJEE2/22AJ5NJFxbna5T4niEGa2jMBlDDQPZZkp709KXTun6yznm2XXPDWary9Y2DNowcuJsMul7BrjJYSQWo632HTSJIoNgQL8pXKzmXOmX/tjN7WjSPG3Vc 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)(4636009)(346002)(396003)(136003)(39860400002)(376002)(230273577357003)(230173577357003)(230922051799003)(1800799012)(451199024)(64100799003)(82310400011)(186009)(36840700001)(46966006)(40470700004)(47076005)(36860700001)(83380400001)(8676002)(8936002)(478600001)(5660300002)(44832011)(4326008)(70586007)(54906003)(86362001)(6916009)(70206006)(316002)(26005)(426003)(7416002)(16526019)(36756003)(2616005)(1076003)(336012)(7406005)(2906002)(41300700001)(356005)(40460700003)(81166007)(40480700001)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:43:16.7589 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: aa9438fe-1114-4aad-8e0f-08dc1e295045 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: MWH0EPF000971E3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5966 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A569C14001A X-Stat-Signature: o8sq4woaer9i86xmdf71aembpbcbjdim X-HE-Tag: 1706244199-895195 X-HE-Meta: U2FsdGVkX1+CeNrCGSQIStpdh+zvUtHOx+xpTwr579JGMZqALkDOfJ8RTHyMIJ+gHp7fayKRbg3mFecIq2szBvgsC9bRnuCKSgixarebQb4ZkBtOrTgaYJGfIo6eUye9mxQF08bRw1O07HERjv2qQRdkxG90WzD5Z46a/Km83kUdEJR5cE44wDgGZDlWCy9PSWw0S93IcfGMvifWKaryPpr3041PWYL2dz3GZuqEtgZv3vZgFSbYjnYgW5s4xcN/LZ+mFeS4PuSmTHCXv4N/zxlprmoWuXfJs3n0g5R8FDOW3FI/CzT6+gagfc+0ECNvtkn6DIVVYnUeS73Iv/64eSjNSczkvvyq2UzuyayLBqy2Q1IJkijyXTgMiVp9DH466brlscLhmYjDtvuADM41EIybKJ1CT3YGADS3yjL/XuBxbExokAhfziwc/rCdSh7APU+pnkVuZ1ophUuyLeS9w2VtGd6O9lUbnX4r3esp9qbUgi1kuq4r44P+kQdgf+O2aIGGmjhZvJFleI9iCsq1St3uFt5nIfIND21jewjP5YirV5worBMUE6Ly4M+CCgGsJ8GpnPe/Awa4XGqFMSYRs+BkLTyLiRqtcE6+M5vqRn1toyx9N9CaHzY0hDw+eePxdNpeoSFVU4BrNxWwp9polfnkDqPsfG10NlmBaAmbOJ3m4+9ESN0bfyISfYHIMkDshHsWT48TyRh8Xj3+bpUiv9MOzdjRh5UD2q1qEs7x9lFaCTa6shomFuLdx1P2SIMIsAI+3qfSu+Pqr8EiM61o61wE2pyYYhoL4+TFSig3GCC1yk9IfGU6zf5Z2wule/chexwFT+PKFILRZT9gqxZ2RUTTh/WNWg3aEj/0fbhRqXb/7d/IWNz6uYx4FJvhnDBE7KaRSQdzpXHuGgSO5eeylw/R4B37Na1kWmNvfBmLmDTFjyN37A7WZ/qfWmIXZ9w8HDYZ7+qhDU/fDwx+Fe/ BdEXQkRS AEQDmF+nhbRd3AbpKUb73ppBVJseW3oYV145H2ItWHJ35UAt7swIsGitlpPE0GsEHz+ErNjR1zukt2WucH1g4XdD/6q5V1gf8z7/bBmazSpkpd6YywrTR4i+Te0ChvMLkmAQ8E4wQAnF0+1j+a8tefLQXIRnNbDZJR57dtoqBXT7kkFYWsNVzkf9JPcV2y0RUfkjjYgRFUeRwrMDKW/G8ciGTD7cfRVRr10kJbHcTGlB1WvQtCOIs2zepAdcLJetjQ4HdniThWxsybxsxm30DJuAa8dQLl7q5CRJr6gYhynw3G16jvUBP+y8mfvMfAbaoJcejLoWtlOfdXeE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware ABI specification for more details. Allocate the Trusted Memory Region (TMR) as a 2MB-sized/aligned region when SNP is enabled to satisfy new requirements for SNP. Continue allocating a 1MB-sized region for !SNP configuration. Signed-off-by: Brijesh Singh Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra [mdr: use struct sev_data_snp_page_reclaim instead of passing paddr directly to SEV_CMD_SNP_PAGE_RECLAIM] Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 176 +++++++++++++++++++++++++++++++---- include/linux/psp-sev.h | 9 ++ 2 files changed, 165 insertions(+), 20 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index abee1a68d609..fa992ce57ffe 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "psp-dev.h" #include "sev-dev.h" @@ -73,9 +74,14 @@ static int psp_timeout; * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator * to allocate the memory, which will return aligned memory for the specified * allocation order. + * + * When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB sized. */ -#define SEV_ES_TMR_SIZE (1024 * 1024) +#define SEV_TMR_SIZE (1024 * 1024) +#define SNP_TMR_SIZE (2 * 1024 * 1024) + static void *sev_es_tmr; +static size_t sev_es_tmr_size = SEV_TMR_SIZE; /* INIT_EX NV Storage: * The NV Storage is a 32Kb area and must be 4Kb page aligned. Use the page @@ -192,17 +198,6 @@ static int sev_cmd_buffer_len(int cmd) return 0; } -static void *sev_fw_alloc(unsigned long len) -{ - struct page *page; - - page = alloc_pages(GFP_KERNEL, get_order(len)); - if (!page) - return NULL; - - return page_address(page); -} - static struct file *open_file_as_root(const char *filename, int flags, umode_t mode) { struct file *fp; @@ -333,6 +328,142 @@ static int sev_write_init_ex_file_if_required(int cmd_id) return sev_write_init_ex_file(); } +/* + * snp_reclaim_pages() needs __sev_do_cmd_locked(), and __sev_do_cmd_locked() + * needs snp_reclaim_pages(), so a forward declaration is needed. + */ +static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret); + +static int snp_reclaim_pages(unsigned long paddr, unsigned int npages, bool locked) +{ + int ret, err, i; + + paddr = __sme_clr(ALIGN_DOWN(paddr, PAGE_SIZE)); + + for (i = 0; i < npages; i++, paddr += PAGE_SIZE) { + struct sev_data_snp_page_reclaim data = {0}; + + data.paddr = paddr; + + if (locked) + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + else + ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + + if (ret) + goto cleanup; + + ret = rmp_make_shared(__phys_to_pfn(paddr), PG_LEVEL_4K); + if (ret) + goto cleanup; + } + + return 0; + +cleanup: + /* + * If there was a failure reclaiming the page then it is no longer safe + * to release it back to the system; leak it instead. + */ + snp_leak_pages(__phys_to_pfn(paddr), npages - i); + return ret; +} + +static int rmp_mark_pages_firmware(unsigned long paddr, unsigned int npages, bool locked) +{ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + int rc, i; + + for (i = 0; i < npages; i++, pfn++) { + rc = rmp_make_private(pfn, 0, PG_LEVEL_4K, 0, true); + if (rc) + goto cleanup; + } + + return 0; + +cleanup: + /* + * Try unrolling the firmware state changes by + * reclaiming the pages which were already changed to the + * firmware state. + */ + snp_reclaim_pages(paddr, i, locked); + + return rc; +} + +static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order) +{ + unsigned long npages = 1ul << order, paddr; + struct sev_device *sev; + struct page *page; + + if (!psp_master || !psp_master->sev_data) + return NULL; + + page = alloc_pages(gfp_mask, order); + if (!page) + return NULL; + + /* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; + if (!sev->snp_initialized) + return page; + + paddr = __pa((unsigned long)page_address(page)); + if (rmp_mark_pages_firmware(paddr, npages, false)) + return NULL; + + return page; +} + +void *snp_alloc_firmware_page(gfp_t gfp_mask) +{ + struct page *page; + + page = __snp_alloc_firmware_pages(gfp_mask, 0); + + return page ? page_address(page) : NULL; +} +EXPORT_SYMBOL_GPL(snp_alloc_firmware_page); + +static void __snp_free_firmware_pages(struct page *page, int order, bool locked) +{ + struct sev_device *sev = psp_master->sev_data; + unsigned long paddr, npages = 1ul << order; + + if (!page) + return; + + paddr = __pa((unsigned long)page_address(page)); + if (sev->snp_initialized && + snp_reclaim_pages(paddr, npages, locked)) + return; + + __free_pages(page, order); +} + +void snp_free_firmware_page(void *addr) +{ + if (!addr) + return; + + __snp_free_firmware_pages(virt_to_page(addr), 0, false); +} +EXPORT_SYMBOL_GPL(snp_free_firmware_page); + +static void *sev_fw_alloc(unsigned long len) +{ + struct page *page; + + page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len)); + if (!page) + return NULL; + + return page_address(page); +} + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { struct psp_device *psp = psp_master; @@ -456,7 +587,7 @@ static int __sev_init_locked(int *error) data.tmr_address = __pa(sev_es_tmr); data.flags |= SEV_INIT_FLAGS_SEV_ES; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); @@ -479,7 +610,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address = __pa(sev_es_tmr); data.flags |= SEV_INIT_FLAGS_SEV_ES; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error); @@ -623,6 +754,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_initialized = true; dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + sev_es_tmr_size = SNP_TMR_SIZE; + return rc; } @@ -641,14 +774,16 @@ static int __sev_platform_init_locked(int *error) if (!sev_es_tmr) { /* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE); - if (sev_es_tmr) + sev_es_tmr = sev_fw_alloc(sev_es_tmr_size); + if (sev_es_tmr) { /* Must flush the cache before giving it to the firmware */ - clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE); - else + if (!sev->snp_initialized) + clflush_cache_range(sev_es_tmr, sev_es_tmr_size); + } else { dev_warn(sev->dev, "SEV: TMR allocation failed, SEV-ES support unavailable\n"); } + } if (sev_init_ex_buffer) { rc = sev_read_init_ex_file(); @@ -1546,8 +1681,9 @@ static void sev_firmware_shutdown(struct sev_device *sev) /* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus(); - free_pages((unsigned long)sev_es_tmr, - get_order(SEV_ES_TMR_SIZE)); + __snp_free_firmware_pages(virt_to_page(sev_es_tmr), + get_order(sev_es_tmr_size), + false); sev_es_tmr = NULL; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index c7dd6ff9f36b..7f9bc1979018 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -932,6 +932,8 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); int sev_do_cmd(int cmd, void *data, int *psp_ret); void *psp_copy_user_blob(u64 uaddr, u32 len); +void *snp_alloc_firmware_page(gfp_t mask); +void snp_free_firmware_page(void *addr); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -959,6 +961,13 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); } +static inline void *snp_alloc_firmware_page(gfp_t mask) +{ + return NULL; +} + +static inline void snp_free_firmware_page(void *addr) { } + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Fri Jan 26 04:11:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30EF6C47422 for ; Fri, 26 Jan 2024 04:43:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B38FB6B007E; Thu, 25 Jan 2024 23:43:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AE8876B0093; Thu, 25 Jan 2024 23:43:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 989256B0080; Thu, 25 Jan 2024 23:43:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 86BCE8D0001 for ; Thu, 25 Jan 2024 23:43:43 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 34E00403D2 for ; Fri, 26 Jan 2024 04:43:43 +0000 (UTC) X-FDA: 81720219126.08.3D70C8F Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2045.outbound.protection.outlook.com [40.107.100.45]) by imf23.hostedemail.com (Postfix) with ESMTP id 3F51E14000B for ; Fri, 26 Jan 2024 04:43:40 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=OKvSFvVn; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.45 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244220; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=12SpdQ0B6iDjFBVE/dIRbaDpCG3fJ02n914HbZOKUFs=; b=gVeSUTCZRfahWARzG0HKIATf6Dup8X3MHlrXcvEXvB2U1TNPyDNSgxmQxNpKFSv54h27Na XVko5KlzwEDMDDiTvDgMhIopaH1lcXKaBshKRBobcuzoJH6gqTBSWC3maSoxwiyXOaq+G9 bwV6oBia6ZhfM/7Iq4ngRvvH7BVW668= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=OKvSFvVn; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.100.45 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244220; a=rsa-sha256; cv=pass; b=uxrcgvs93oHRK3/kuPSFOA+Y90bl6yPBEkDACNeaJzf2nR49n9Xc20WX5KxXr+NTOL8XEn 3BI+dxps4Lar/XYVIlZIZDrHM4SngTvauY4MmF08EVF0fCsUbMTZqsd+UK0dVMPZv08t7N BQhX56Bu1JDlWIMaedHTZGHCwTTv7jE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bZju9EyRpQlWkrbNPzthYSmTDyDjjKPgkKhok7b3UV565n/SW6gCyBicC+dJZ79t6sBHF1r3+1qhvIwr2Lwvx9zc2rx5X9k6jGFPAv8jEhU4Y3PyW2SNDV5nY8A4oCZFqT162fBTXJ0NZED7PjYjb5SV0KI+sRjxxxwtFYZftA3LV9/n5cOQ2YVb5j8bI/FuxGOSrsKLDdoclxUkmGys63f5BT1lfHhH8jycXyu3hGDtPeS2aL/Sr5R8wlyAfqakmUIgT7qJCctNypS18LL+rId+60cj/1cSUWc8Mr1FV8TJ/ulSfkosXKBJNf5tU4HfeFITRFHbMOBcTZbA8UE/SQ== 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=12SpdQ0B6iDjFBVE/dIRbaDpCG3fJ02n914HbZOKUFs=; b=LejprowJvt7gKKRUb1VEfrSx9M7r4F6C0M1tAR3Awrjf+dw4ay3p3hNVH9pn7rFdfODZp0+sAQbXJ/XRd3SrJtiXOnYP6cMuv16cLopI+2w1d1Rro8oK0HD9afcqdNu90tvQ9Z5nhuMJYa2H132ihNZ7a3iTUC0w1RsFRY/sHPedWnt6o9Nd5Vrqx0PGQPYCnwqc9HwRUSPVYswpnWz5a/kq8qWKkEWvtJQOiU4f4HJr5qr2YlN4T2fenCb3r8i7tL9xVv08OK+VJ7ZSmeNdAuNxZFvCgo1n2f0P3Rh7tKukjRRGy5klnNARz4/LIEna57gsPSPuzuDxhylVzLz6SQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=12SpdQ0B6iDjFBVE/dIRbaDpCG3fJ02n914HbZOKUFs=; b=OKvSFvVnwjxw6+V3be4CFPOnxvkhKaQXYCw9nR2SOUVavQrbFNt7rPl6bhjLyW1ni4W149pRThOGPAXEMC9UXQ65WfU7ROQ0XQUpF8551vF2G2bWAl5jJW2ku+MHk88qbWzOwCTc1MP9nPA+al9h3/ZxR7QdEvX3X/ShPuof8WU= Received: from BY5PR16CA0005.namprd16.prod.outlook.com (2603:10b6:a03:1a0::18) by MW4PR12MB6850.namprd12.prod.outlook.com (2603:10b6:303:1ed::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.28; Fri, 26 Jan 2024 04:43:37 +0000 Received: from MWH0EPF000971E7.namprd02.prod.outlook.com (2603:10b6:a03:1a0:cafe::a5) by BY5PR16CA0005.outlook.office365.com (2603:10b6:a03:1a0::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22 via Frontend Transport; Fri, 26 Jan 2024 04:43:37 +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 MWH0EPF000971E7.mail.protection.outlook.com (10.167.243.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:43:37 +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.34; Thu, 25 Jan 2024 22:43:36 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 17/25] crypto: ccp: Handle non-volatile INIT_EX data when SNP is enabled Date: Thu, 25 Jan 2024 22:11:17 -0600 Message-ID: <20240126041126.1927228-18-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E7:EE_|MW4PR12MB6850:EE_ X-MS-Office365-Filtering-Correlation-Id: bee9c4d1-4669-46f6-7291-08dc1e295c76 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mppbisoawNh0OTtF/pO//rH5L6WWYGjlUeRh17RS7ZByZqvooUaRwMyl5dn8StYTadrOcX9s0F38DUuxrNzZ1Mt0u+L88qAjaVtBggll/xnZdoOmKZnCTkPdy6GphDpf85Fypj9vREM2OaIDtIPWJy0US976A2Bc4EXF3qYAJgE5aE/iSBjgNCHEMOvZQG9NH8s9xW51hL4zw63c9KXnVlA2I49yR2UshjtDXE41NMlRNhr/Je664CAPGRgxXvJGdqDI8b72SVxXVicvWOFH5tyHdWo6IrjOm1TqWm7qqQ51ijAfNjGk6wtMEkehBlR8p34QXL40Basj4qCaatcADu4IaQx+TH2GyFUCrZYe6x0GzRvpCcaVN7g3fIlXCPLEM4Q6uzJlPLxQG9j1j7VqH8IrK3x+oPubX7kosZoJlqDQ8rK2HnNsd/W08DZSWs64lof9VxB62svVFg+oSdXR2a84VXLqtJfLfW2EzOekFGmP6x0UlQT+mx4CwsTidvfvgB1sx6Mr46BbA4XLR5Bgiksqz2TVayD+13uXBREJ3FE17WCx+ayyfdeQa0JObUcaUeikPwsb5/PZ68puHl8X29kz1oJb7nHQDoCI1+TXR3ATctG9kn9N9an6YO5/A1U2zKg1A4Bn+nh1V6sbYd6/qY24A9y8pv0wnYnMxOrjh+Cr7rBAkgRkTHvsB0mDsOxfYIVFn3lao+nG6lAsdFGLPF7xapnrvvJnwa0gciUNROdTIvIilXNlnaQXkNC+qXHJDZYJq6lNVc11q7syvemHvw== 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)(4636009)(376002)(396003)(346002)(39860400002)(136003)(230922051799003)(64100799003)(1800799012)(82310400011)(451199024)(186009)(40470700004)(46966006)(36840700001)(336012)(426003)(41300700001)(40460700003)(40480700001)(16526019)(26005)(6916009)(1076003)(4326008)(36860700001)(36756003)(82740400003)(83380400001)(47076005)(6666004)(356005)(478600001)(81166007)(2616005)(2906002)(7416002)(8676002)(70586007)(70206006)(316002)(44832011)(54906003)(5660300002)(8936002)(86362001)(7406005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:43:37.2120 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bee9c4d1-4669-46f6-7291-08dc1e295c76 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: MWH0EPF000971E7.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB6850 X-Rspamd-Queue-Id: 3F51E14000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: jjjwefdfjz8obqqzibet717f1ab71eko X-HE-Tag: 1706244220-396507 X-HE-Meta: U2FsdGVkX1+xHvUpeJKry1sBKEa2/YacHtQln3GUJSBIyDEK/2ud+p9OMjRCNeaJNqKgnR8C264D0X6Z4xt1Zld1wCGsfZloO31ablEUZRuMfwoBTt2ycrkPT/KubM4c01AvMM98nFAcDmgYACJfdxJEMMtVUR7i1q9Je1sbHxD2ekep/d1twL6+w1rPI/xrk1ejrZuFGdwfU3IgkIGNfA+GbphbL9B7uDrbuGEWJTHMVrBHRw0fKohUIjnuURrX/WBcSDamqeMYo2sYduFC5mzBxq2WwZoSSTq9oDVHaKJitS05UVu0MeeRa/j9oqYqu5huqZ5OmMlUUOBcDrNqorv2tNOWB3jFyIWCI9hwnco3eVEznn4fkwRMDaOZe6orXuDaIJEneGfPHbWiljG3zTnHJY/OngnGYPs9crECf0MSwzn7hihOV46nBIbwK89/8ozUhWEqxw3qcNlbWsJaZI0KWwO9Rsbdzu2lOEmnYijW6xku00wh3DJrxkC6Gc/g465vUNtE7qiByX6EvQx4mxyFf8Fve1TVT5gZi+fvxk3Bff094+f1cb0k0/7sZDpOwvsbzCHfWiNesOgaN8BeWl/hAESJgvRJerBEhPZUQcuPCF3wtFToYS5EdUnTAGlxeuj0AW5zmgE/A5jQwkojtI7lYQPqzVOHI2RaleI1efOtGMKP1Hmw5qmcUl+QZGzAfROVSKL1v3DIAWZmufVGpHIH1R3wl7dA+V6gTHUX3L6tCJLkYIVWQjjmAR2cVmYXRb0oFXTpaBgo8xFaK2KSw5Upammoc+c9Kuiv3Ur8cJRLQ0ErUguMHW74Px2isEAhor57m0Fze254v5y7cciwN/Wwn0ITBepsvVXXqyX9+QCuT0ea8clool1ZgPxOo6ohsF/kSAl2AsnU1RnF4mLqaQwaGQ8Qr1Xg7A48OAM8KqAKYKLFby/+WvtD7UCUDkTUTtYgbN69mVm7wFv3OMI 0QrR7rVv D+RmMmJGqtBlo7f2klX/hKn07mBcHSkDn53oWyhbTpQqtxP0AG75MlS4nLuQOuTNMSL4cYZXEluLIOFw/voagnb/UNAJoIsVQxfXZsQT4MQK3MNqstATWIQS3rjyiAZIQHcVr4QejpW4TRuRfnkwmnzNmTqLZOyGNCX+a5LyZ22/K25ZRquK1wWAKW4JLBTpwN0Xp0Z35HuWUd0DjLZzbU6yfl6Qn6LLzjZ/qR2WgNMWm4zLZfmnENFLDa8tT8dl+Db4aKF6QobEOnl31xqyKWgi54pdp43ladqzvESNuq5XX/Ra5RiosgZtwWr+Yc6wxQoYMcbjBID8ftTrgpFeunKZcZyBxhqv7cKcTk/IldIGkr9ufgBqCqql4YYAWvpbzs8n2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Tom Lendacky For SEV/SEV-ES, a buffer can be used to access non-volatile data so it can be initialized from a file specified by the init_ex_path CCP module parameter instead of relying on the SPI bus for NV storage, and afterward the buffer can be read from to sync new data back to the file. When SNP is enabled, the pages comprising this buffer need to be set to firmware-owned in the RMP table before they can be accessed by firmware for subsequent updates to the initial contents. Implement that handling here. Signed-off-by: Tom Lendacky Co-developed-by: Michael Roth Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 47 ++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index fa992ce57ffe..97fdd98e958c 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -785,10 +785,38 @@ static int __sev_platform_init_locked(int *error) } } - if (sev_init_ex_buffer) { + /* + * If an init_ex_path is provided allocate a buffer for the file and + * read in the contents. Additionally, if SNP is initialized, convert + * the buffer pages to firmware pages. + */ + if (init_ex_path && !sev_init_ex_buffer) { + struct page *page; + + page = alloc_pages(GFP_KERNEL, get_order(NV_LENGTH)); + if (!page) { + dev_err(sev->dev, "SEV: INIT_EX NV memory allocation failed\n"); + return -ENOMEM; + } + + sev_init_ex_buffer = page_address(page); + rc = sev_read_init_ex_file(); if (rc) return rc; + + /* If SEV-SNP is initialized, transition to firmware page. */ + if (sev->snp_initialized) { + unsigned long npages; + + npages = 1UL << get_order(NV_LENGTH); + if (rmp_mark_pages_firmware(__pa(sev_init_ex_buffer), + npages, false)) { + dev_err(sev->dev, + "SEV: INIT_EX NV memory page state change failed.\n"); + return -ENOMEM; + } + } } rc = __sev_do_init_locked(&psp_ret); @@ -1688,8 +1716,9 @@ static void sev_firmware_shutdown(struct sev_device *sev) } if (sev_init_ex_buffer) { - free_pages((unsigned long)sev_init_ex_buffer, - get_order(NV_LENGTH)); + __snp_free_firmware_pages(virt_to_page(sev_init_ex_buffer), + get_order(NV_LENGTH), + true); sev_init_ex_buffer = NULL; } @@ -1743,18 +1772,6 @@ void sev_pci_init(void) if (sev_update_firmware(sev->dev) == 0) sev_get_api_version(); - /* If an init_ex_path is provided rely on INIT_EX for PSP initialization - * instead of INIT. - */ - if (init_ex_path) { - sev_init_ex_buffer = sev_fw_alloc(NV_LENGTH); - if (!sev_init_ex_buffer) { - dev_err(sev->dev, - "SEV: INIT_EX NV memory allocation failed\n"); - goto err; - } - } - /* Initialize the platform */ args.probe = true; rc = sev_platform_init(&args); From patchwork Fri Jan 26 04:11:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05288C47422 for ; Fri, 26 Jan 2024 04:44:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 879FD6B0099; Thu, 25 Jan 2024 23:44:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 829176B00A2; Thu, 25 Jan 2024 23:44:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CA9A6B00A3; Thu, 25 Jan 2024 23:44:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 495196B0099 for ; Thu, 25 Jan 2024 23:44:08 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E61BE40468 for ; Fri, 26 Jan 2024 04:44:07 +0000 (UTC) X-FDA: 81720220134.27.6A7E7F9 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2057.outbound.protection.outlook.com [40.107.244.57]) by imf07.hostedemail.com (Postfix) with ESMTP id CE44A4000B for ; Fri, 26 Jan 2024 04:44:04 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gJS8x26s; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.57 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244245; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NI0TXxna9Q4A+0oib7Nh9hyRMric/3e/+cYBycjs+J8=; b=XnHagjIH0IxUZmZ6mJAaiRkQSZUVHOy/JE1hmToT6uhfJMdRVjEkV54MsbTW3/EUzwcwnZ BM57V4NEM+96cvyjrn3XiJRMEAkf06bR8oF0T9ErBN3RxplzqsoiNk6Aay3f9kT1XK0TKr HpN1c0oXqpyDgM26c9rBsBEAQjm3aos= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=gJS8x26s; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf07.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.57 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244245; a=rsa-sha256; cv=pass; b=MOzTU/JcEGPpgFCFNnaag5fM2H7OEBfpJkSKhB5/MTsJgY0p8jHIoZH5mBxfw6axSBwtAb pxGJHolqGm5DREetUFdCeXjAYy6mKywm/5JiSbn2rG66aMjVDW+YyN9Wiu9BmVF2E8E8qI POPa+Wa12aSAZpx1klUDySf6+Sw5ydQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ILZaLjMchTI4RU+CNcquYEGomM1hzg8UDagr18HflWSdwQ8u3BU0CkosiZxJBp55G+pJZ8IGIFZ72i42/GgrCxQ4inKL6xgnBTlth9+3Rfjg0v94HP9vBumZ9zjqrSSyIHbBgrJ1QBJKwL+xoLcOjvneiNXhnNoybPvYMQ+9bIrMSMGPU+2XRzh2yt5Lf7rLYANHIjkhKg2QQYmWg1sqpX6vqPqOii+A1AUVWlHi1p+VGL2JA9hNWd9v1076waRwOoWyZ4o3vbxig3s4K0m+Au+EbxVsNHXdZlmOT40S0S747u4WBXc0DbeNC1hAd5rsm/8L8KGt4D6YM84wsdYvtA== 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=NI0TXxna9Q4A+0oib7Nh9hyRMric/3e/+cYBycjs+J8=; b=Lj4YyTJgLMbA3OLUsMzSMydsGRDg27NlvHFEM8ecKe6MzMz+2p2gL2qg8nUy78e6qvV/5mal5waTyC5wQX4PW2UMFAx7PcuuOa7gwBSskrLhFEmtU0iajGEJBJk8fHOPua7nx63nTT+keWFwH46iAhQCxK/IrCVWmKJm0mJV3AbuExmAqtieMUQhmoK58v1hvISXkNstgkD13XWgYZi9r+9gTM8m+3K0aRqAqGN8OHkuL6tlQNSlPRGl7yGkw9EsbkXUVYbG8AtDNhP5xxSuNsgmOpZk8laOZJb8ehp7Qq1vqnqkdT3Xfu9WnC4GEnORqMDlvwbaszxvTYRjMRKYMQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=NI0TXxna9Q4A+0oib7Nh9hyRMric/3e/+cYBycjs+J8=; b=gJS8x26s9J7pwrb6LmqOZs2eARcyToUMVmMTDhLCk4LrIiNtnNEOMjaDdBPUZX4LuF2dPtg9L07e6AmMJvQ3PETt43yvl1owzDxkWObcojee3Be3ClUCAMxndpLKxLcHrRNRtvHwXGEEgiCAXf1eKK05dP20fuyijulDU1yYFBA= Received: from MW4PR02CA0024.namprd02.prod.outlook.com (2603:10b6:303:16d::9) by BN9PR12MB5306.namprd12.prod.outlook.com (2603:10b6:408:103::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:43:58 +0000 Received: from MWH0EPF000971E8.namprd02.prod.outlook.com (2603:10b6:303:16d:cafe::cf) by MW4PR02CA0024.outlook.office365.com (2603:10b6:303:16d::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:43:58 +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 MWH0EPF000971E8.mail.protection.outlook.com (10.167.243.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:43:58 +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.34; Thu, 25 Jan 2024 22:43:57 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 18/25] crypto: ccp: Handle legacy SEV commands when SNP is enabled Date: Thu, 25 Jan 2024 22:11:18 -0600 Message-ID: <20240126041126.1927228-19-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E8:EE_|BN9PR12MB5306:EE_ X-MS-Office365-Filtering-Correlation-Id: 591a442d-f3bc-45b1-16ae-08dc1e2968db X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XOOHhdoKjm65vCjo8+hUgclOITHjxUG3Zaf4q796Jy2+fWKqEbtT9v/a3qoGt7GMaZGdCALyeOqEB0Zg1DKT5C/KEn4cgalsQMCUUg5SONT1hAo0vfvIfw9BO4kEwObNPR4PCHPpFBzfwmNlcA+H+AcurW6ziGfhqwDjA8AYKNBdg/w7ugtK1fsNC23ZjbDnuYdVxNw7QMfANnaiFCX4ZTygRs6+ZCNWIJLdtSxDZ9yMYouWKtmC3NKBEvu7aHfOykEQVQbepCzk1D0qVqxaUpZ2e8tnL80X7LRb+d1P4hJbPWy+P6rKY/7oOsaFATqWFf51o5gEIARnt1SLgYcTKOOfn4LJnfUkzlZ5qdTfuAGW69eWrSZJP1WvhLn1KS+WV3t7ut3xRQzf0rUvMQtYd5HoV976wuun4x9nPjTi963+XanHuO70vydnzraZej3tZgMsIfhuyUWgf8x42Vd5XcEEgvbEu/OyExYSqYw3Pvjv408lJNBeXwx083U2DxfKBpXoMK3mxCkIQH8Wz/SnC9DxYc9JqQYxmVAMNg/2ycpHtlGU+yLg5SDRhc9Lr5SDo0dgoav9333mwYe5IQLfbOS7TOJNMDBYqugLwuta8Cgaldgo768kL6uO1gjQnpgQ+U0QbpSDjcbDwkr1hAlL3qj3tH9sMQJZ/DnUAFOX+eBdPE2DV5+lBLurBYS4xAxs176yiwib5VOjbZto26jjsPEnnophGanEcjQ9+CuQKGoJiXHmxXHRBhBhxxHmDvV5SOeHuCFxvdEMe6A/LxzTag== 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)(4636009)(136003)(39860400002)(346002)(376002)(396003)(230922051799003)(64100799003)(451199024)(1800799012)(82310400011)(186009)(40470700004)(36840700001)(46966006)(82740400003)(7406005)(356005)(81166007)(30864003)(2906002)(7416002)(36860700001)(5660300002)(44832011)(41300700001)(36756003)(86362001)(336012)(426003)(26005)(16526019)(478600001)(1076003)(2616005)(6666004)(8936002)(8676002)(4326008)(47076005)(83380400001)(54906003)(70206006)(316002)(6916009)(70586007)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:43:58.0042 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 591a442d-f3bc-45b1-16ae-08dc1e2968db 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: MWH0EPF000971E8.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5306 X-Rspam-User: X-Stat-Signature: 5a396zwuhzyehy5sjsfscirrrmmmpwex X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: CE44A4000B X-HE-Tag: 1706244244-355429 X-HE-Meta: U2FsdGVkX19cZbHT6NxaU3ucuwcMJhclw57uPy7iMphozgaqvTr3Z5D+7pcGId9e3NknupUo1U6QeZg0D65aBi0L1qfr28SN6ZVMi+BH26gWWrocqBpEEguz3/gExgxDV9GU9q7tF2dm6TzCt1pHGscsiSPOm5tVzXDtsVY5io6kHNYLittuALiig/k+Ok6+YqpEsUeTN39B3AdSxWTTMQAQWN8SwfWAOP/DB4t7tzqwmK13WfQkYniTFMGYHBzeMUHgMUKYu7gHpZKMt2zvWgr0lqnEM79QNcaPCnRbMFyqLWr9+TeQuysWUecDlrgvyGSzfHdIPNNAw0lTezk7aUg7urRuj/SoWs/0JOkqvB5et1MufCBhg96goa+IFqBqQbo3WGYCycFse+6jukRdS2jInOWSNjEhdGdyf+398PVmFkQY5iJCYbmgMPu+Qa8cMIYSOw7zQsR8vZNKAg/QhOIsYREmgTlA5lKRCcmGmvzThNytUSjUMKr6cIhWD/EcBXL65UXY4cjpUy2rmkwLDB79QCbQWzF+mB4P6Wq44Zo7iXC+eNwxS2vrSan5AC4Dm7cGIlD2IcqCSTyz+lyGHSwIwuchFN+X6mEghc39WI9Ful+SrWBgYDB+8FgLKnf7VQq6sUDnsTjsZhH1WE0kKWWcdlg2gGrkjWr2qCPIeP2lKS8nFe65uQ6qxZzfiPnqmYuvgohhJNcvcwJFidPYXm8eZqmBfHcBr90rhc+kzcSfm2uyQdg59lfKpI6VsURciEqSKzbLVpWt2LdKh31revI0YGPPdVObybL2mJ3DroGb46vdAHJl7zRtYDsnIZ+fDTmHpXRFLlhFAmg+c61S4cOoY1b+RM+8VkWK8su9sJ6ASgWv0/FEO+70LiTJtgyNo6uEv3NLJ9ZOFL5CP/rBCpV5Du1ZbYlgF/mmh044H4Q3zZTtJvXWaDqMN1rQlbcbTY7xU9h3pHTcmkNN1qX DhptGAFN t4evCDE/0JKjiILnh8X6e0YuQPFd/we9j26TftLGHnNtyr1gn/iUnV9QptCYLKgawZdv73cOcBy8PfNaOWKl7uMnMh7dczaXpVGDii7CDKRb25UEVUA/uGemfFAgEpM5CukEGXCwQO8FBbOHMYaIo00+C4YpgoKY+8Ss7XafJYVdKtx0cC2fxz0mCSgvxjwAuIadkcOQhF5Z0VhXlOgb90huI6fO6gmMEJH4G0zC41aU1mPHAfb4NeJ9MTE0b+djcqlz5nfGAxI2M8AT+U3O4huiNzUrpsyxUPrgzEOw0iPnnLTshCKlmQPunwpUztBLMgmbGS2V3Z76t1DU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh The behavior of legacy SEV commands is altered when the firmware is initialized for SNP support. In that case, all command buffer memory that may get written to by legacy SEV commands must be marked as firmware-owned in the RMP table prior to issuing the command. Additionally, when a command buffer contains a system physical address that points to additional buffers that firmware may write to, special handling is needed depending on whether: 1) the system physical address points to guest memory 2) the system physical address points to host memory To handle case #1, the pages of these buffers are changed to firmware-owned in the RMP table before issuing the command, and restored to hypervisor-owned after the command completes. For case #2, a bounce buffer is used instead of the original address. Signed-off-by: Brijesh Singh Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Ashish Kalra --- drivers/crypto/ccp/sev-dev.c | 418 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 3 + 2 files changed, 411 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 97fdd98e958c..b2ad41ce5f77 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -43,6 +43,15 @@ #define SNP_MIN_API_MAJOR 1 #define SNP_MIN_API_MINOR 51 +/* + * Maximum number of firmware-writable buffers that might be specified + * in the parameters of a legacy SEV command buffer. + */ +#define CMD_BUF_FW_WRITABLE_MAX 2 + +/* Leave room in the descriptor array for an end-of-list indicator. */ +#define CMD_BUF_DESC_MAX (CMD_BUF_FW_WRITABLE_MAX + 1) + static DEFINE_MUTEX(sev_cmd_mutex); static struct sev_misc_dev *misc_dev; @@ -464,13 +473,344 @@ static void *sev_fw_alloc(unsigned long len) return page_address(page); } +/** + * struct cmd_buf_desc - descriptors for managing legacy SEV command address + * parameters corresponding to buffers that may be written to by firmware. + * + * @paddr_ptr: pointer the address parameter in the command buffer, which may + * need to be saved/restored depending on whether a bounce buffer is + * used. Must be NULL if this descriptor is only an end-of-list + * indicator. + * @paddr_orig: storage for the original address parameter, which can be used to + * restore the original value in @paddr_ptr in cases where it is + * replaced with the address of a bounce buffer. + * @len: length of buffer located at the address originally stored at @paddr_ptr + * @guest_owned: true if the address corresponds to guest-owned pages, in which + * case bounce buffers are not needed. + */ +struct cmd_buf_desc { + u64 *paddr_ptr; + u64 paddr_orig; + u32 len; + bool guest_owned; +}; + +/* + * If a legacy SEV command parameter is a memory address, those pages in + * turn need to be transitioned to/from firmware-owned before/after + * executing the firmware command. + * + * Additionally, in cases where those pages are not guest-owned, a bounce + * buffer is needed in place of the original memory address parameter. + * + * A set of descriptors are used to keep track of this handling, and + * initialized here based on the specific commands being executed. + */ +static void snp_populate_cmd_buf_desc_list(int cmd, void *cmd_buf, + struct cmd_buf_desc *desc_list) +{ + switch (cmd) { + case SEV_CMD_PDH_CERT_EXPORT: { + struct sev_data_pdh_cert_export *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->pdh_cert_address; + desc_list[0].len = data->pdh_cert_len; + desc_list[1].paddr_ptr = &data->cert_chain_address; + desc_list[1].len = data->cert_chain_len; + break; + } + case SEV_CMD_GET_ID: { + struct sev_data_get_id *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->address; + desc_list[0].len = data->len; + break; + } + case SEV_CMD_PEK_CSR: { + struct sev_data_pek_csr *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->address; + desc_list[0].len = data->len; + break; + } + case SEV_CMD_LAUNCH_UPDATE_DATA: { + struct sev_data_launch_update_data *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->address; + desc_list[0].len = data->len; + desc_list[0].guest_owned = true; + break; + } + case SEV_CMD_LAUNCH_UPDATE_VMSA: { + struct sev_data_launch_update_vmsa *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->address; + desc_list[0].len = data->len; + desc_list[0].guest_owned = true; + break; + } + case SEV_CMD_LAUNCH_MEASURE: { + struct sev_data_launch_measure *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->address; + desc_list[0].len = data->len; + break; + } + case SEV_CMD_LAUNCH_UPDATE_SECRET: { + struct sev_data_launch_secret *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->guest_address; + desc_list[0].len = data->guest_len; + desc_list[0].guest_owned = true; + break; + } + case SEV_CMD_DBG_DECRYPT: { + struct sev_data_dbg *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->dst_addr; + desc_list[0].len = data->len; + desc_list[0].guest_owned = true; + break; + } + case SEV_CMD_DBG_ENCRYPT: { + struct sev_data_dbg *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->dst_addr; + desc_list[0].len = data->len; + desc_list[0].guest_owned = true; + break; + } + case SEV_CMD_ATTESTATION_REPORT: { + struct sev_data_attestation_report *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->address; + desc_list[0].len = data->len; + break; + } + case SEV_CMD_SEND_START: { + struct sev_data_send_start *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->session_address; + desc_list[0].len = data->session_len; + break; + } + case SEV_CMD_SEND_UPDATE_DATA: { + struct sev_data_send_update_data *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->hdr_address; + desc_list[0].len = data->hdr_len; + desc_list[1].paddr_ptr = &data->trans_address; + desc_list[1].len = data->trans_len; + break; + } + case SEV_CMD_SEND_UPDATE_VMSA: { + struct sev_data_send_update_vmsa *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->hdr_address; + desc_list[0].len = data->hdr_len; + desc_list[1].paddr_ptr = &data->trans_address; + desc_list[1].len = data->trans_len; + break; + } + case SEV_CMD_RECEIVE_UPDATE_DATA: { + struct sev_data_receive_update_data *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->guest_address; + desc_list[0].len = data->guest_len; + desc_list[0].guest_owned = true; + break; + } + case SEV_CMD_RECEIVE_UPDATE_VMSA: { + struct sev_data_receive_update_vmsa *data = cmd_buf; + + desc_list[0].paddr_ptr = &data->guest_address; + desc_list[0].len = data->guest_len; + desc_list[0].guest_owned = true; + break; + } + default: + break; + } +} + +static int snp_map_cmd_buf_desc(struct cmd_buf_desc *desc) +{ + unsigned int npages; + + if (!desc->len) + return 0; + + /* Allocate a bounce buffer if this isn't a guest owned page. */ + if (!desc->guest_owned) { + struct page *page; + + page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(desc->len)); + if (!page) { + pr_warn("Failed to allocate bounce buffer for SEV legacy command.\n"); + return -ENOMEM; + } + + desc->paddr_orig = *desc->paddr_ptr; + *desc->paddr_ptr = __psp_pa(page_to_virt(page)); + } + + npages = PAGE_ALIGN(desc->len) >> PAGE_SHIFT; + + /* Transition the buffer to firmware-owned. */ + if (rmp_mark_pages_firmware(*desc->paddr_ptr, npages, true)) { + pr_warn("Error moving pages to firmware-owned state for SEV legacy command.\n"); + return -EFAULT; + } + + return 0; +} + +static int snp_unmap_cmd_buf_desc(struct cmd_buf_desc *desc) +{ + unsigned int npages; + + if (!desc->len) + return 0; + + npages = PAGE_ALIGN(desc->len) >> PAGE_SHIFT; + + /* Transition the buffers back to hypervisor-owned. */ + if (snp_reclaim_pages(*desc->paddr_ptr, npages, true)) { + pr_warn("Failed to reclaim firmware-owned pages while issuing SEV legacy command.\n"); + return -EFAULT; + } + + /* Copy data from bounce buffer and then free it. */ + if (!desc->guest_owned) { + void *bounce_buf = __va(__sme_clr(*desc->paddr_ptr)); + void *dst_buf = __va(__sme_clr(desc->paddr_orig)); + + memcpy(dst_buf, bounce_buf, desc->len); + __free_pages(virt_to_page(bounce_buf), get_order(desc->len)); + + /* Restore the original address in the command buffer. */ + *desc->paddr_ptr = desc->paddr_orig; + } + + return 0; +} + +static int snp_map_cmd_buf_desc_list(int cmd, void *cmd_buf, struct cmd_buf_desc *desc_list) +{ + int i; + + snp_populate_cmd_buf_desc_list(cmd, cmd_buf, desc_list); + + for (i = 0; i < CMD_BUF_DESC_MAX; i++) { + struct cmd_buf_desc *desc = &desc_list[i]; + + if (!desc->paddr_ptr) + break; + + if (snp_map_cmd_buf_desc(desc)) + goto err_unmap; + } + + return 0; + +err_unmap: + for (i--; i >= 0; i--) + snp_unmap_cmd_buf_desc(&desc_list[i]); + + return -EFAULT; +} + +static int snp_unmap_cmd_buf_desc_list(struct cmd_buf_desc *desc_list) +{ + int i, ret = 0; + + for (i = 0; i < CMD_BUF_DESC_MAX; i++) { + struct cmd_buf_desc *desc = &desc_list[i]; + + if (!desc->paddr_ptr) + break; + + if (snp_unmap_cmd_buf_desc(&desc_list[i])) + ret = -EFAULT; + } + + return ret; +} + +static bool sev_cmd_buf_writable(int cmd) +{ + switch (cmd) { + case SEV_CMD_PLATFORM_STATUS: + case SEV_CMD_GUEST_STATUS: + case SEV_CMD_LAUNCH_START: + case SEV_CMD_RECEIVE_START: + case SEV_CMD_LAUNCH_MEASURE: + case SEV_CMD_SEND_START: + case SEV_CMD_SEND_UPDATE_DATA: + case SEV_CMD_SEND_UPDATE_VMSA: + case SEV_CMD_PEK_CSR: + case SEV_CMD_PDH_CERT_EXPORT: + case SEV_CMD_GET_ID: + case SEV_CMD_ATTESTATION_REPORT: + return true; + default: + return false; + } +} + +/* After SNP is INIT'ed, the behavior of legacy SEV commands is changed. */ +static bool snp_legacy_handling_needed(int cmd) +{ + struct sev_device *sev = psp_master->sev_data; + + return cmd < SEV_CMD_SNP_INIT && sev->snp_initialized; +} + +static int snp_prep_cmd_buf(int cmd, void *cmd_buf, struct cmd_buf_desc *desc_list) +{ + if (!snp_legacy_handling_needed(cmd)) + return 0; + + if (snp_map_cmd_buf_desc_list(cmd, cmd_buf, desc_list)) + return -EFAULT; + + /* + * Before command execution, the command buffer needs to be put into + * the firmware-owned state. + */ + if (sev_cmd_buf_writable(cmd)) { + if (rmp_mark_pages_firmware(__pa(cmd_buf), 1, true)) + return -EFAULT; + } + + return 0; +} + +static int snp_reclaim_cmd_buf(int cmd, void *cmd_buf) +{ + if (!snp_legacy_handling_needed(cmd)) + return 0; + + /* + * After command completion, the command buffer needs to be put back + * into the hypervisor-owned state. + */ + if (sev_cmd_buf_writable(cmd)) + if (snp_reclaim_pages(__pa(cmd_buf), 1, true)) + return -EFAULT; + + return 0; +} + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { + struct cmd_buf_desc desc_list[CMD_BUF_DESC_MAX] = {0}; struct psp_device *psp = psp_master; struct sev_device *sev; unsigned int cmdbuff_hi, cmdbuff_lo; unsigned int phys_lsb, phys_msb; unsigned int reg, ret = 0; + void *cmd_buf; int buf_len; if (!psp || !psp->sev_data) @@ -490,12 +830,47 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) * work for some memory, e.g. vmalloc'd addresses, and @data may not be * physically contiguous. */ - if (data) - memcpy(sev->cmd_buf, data, buf_len); + if (data) { + /* + * Commands are generally issued one at a time and require the + * sev_cmd_mutex, but there could be recursive firmware requests + * due to SEV_CMD_SNP_PAGE_RECLAIM needing to be issued while + * preparing buffers for another command. This is the only known + * case of nesting in the current code, so exactly one + * additional command buffer is available for that purpose. + */ + if (!sev->cmd_buf_active) { + cmd_buf = sev->cmd_buf; + sev->cmd_buf_active = true; + } else if (!sev->cmd_buf_backup_active) { + cmd_buf = sev->cmd_buf_backup; + sev->cmd_buf_backup_active = true; + } else { + dev_err(sev->dev, + "SEV: too many firmware commands in progress, no command buffers available.\n"); + return -EBUSY; + } + + memcpy(cmd_buf, data, buf_len); + + /* + * The behavior of the SEV-legacy commands is altered when the + * SNP firmware is in the INIT state. + */ + ret = snp_prep_cmd_buf(cmd, cmd_buf, desc_list); + if (ret) { + dev_err(sev->dev, + "SEV: failed to prepare buffer for legacy command 0x%x. Error: %d\n", + cmd, ret); + return ret; + } + } else { + cmd_buf = sev->cmd_buf; + } /* Get the physical address of the command buffer */ - phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0; - phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0; + phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0; + phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0; dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", cmd, phys_msb, phys_lsb, psp_timeout); @@ -549,15 +924,36 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret = sev_write_init_ex_file_if_required(cmd); } - print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, - buf_len, false); - /* * Copy potential output from the PSP back to data. Do this even on * failure in case the caller wants to glean something from the error. */ - if (data) - memcpy(data, sev->cmd_buf, buf_len); + if (data) { + int ret_reclaim; + /* + * Restore the page state after the command completes. + */ + ret_reclaim = snp_reclaim_cmd_buf(cmd, cmd_buf); + if (ret_reclaim) { + dev_err(sev->dev, + "SEV: failed to reclaim buffer for legacy command %#x. Error: %d\n", + cmd, ret_reclaim); + return ret_reclaim; + } + + memcpy(data, cmd_buf, buf_len); + + if (sev->cmd_buf_backup_active) + sev->cmd_buf_backup_active = false; + else + sev->cmd_buf_active = false; + + if (snp_unmap_cmd_buf_desc_list(desc_list)) + return -EFAULT; + } + + print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, + buf_len, false); return ret; } @@ -1657,10 +2053,12 @@ int sev_dev_init(struct psp_device *psp) if (!sev) goto e_err; - sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1); if (!sev->cmd_buf) goto e_sev; + sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE; + psp->sev_data = sev; sev->dev = dev; diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 85506325051a..3e4e5574e88a 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -52,6 +52,9 @@ struct sev_device { u8 build; void *cmd_buf; + void *cmd_buf_backup; + bool cmd_buf_active; + bool cmd_buf_backup_active; bool snp_initialized; }; From patchwork Fri Jan 26 04:11:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532056 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 136B3C47DDB for ; Fri, 26 Jan 2024 04:44:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A00876B00A3; Thu, 25 Jan 2024 23:44:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B0916B00A4; Thu, 25 Jan 2024 23:44:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 878B96B00A5; Thu, 25 Jan 2024 23:44:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 78F746B00A3 for ; Thu, 25 Jan 2024 23:44:25 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3F6D9C04E2 for ; Fri, 26 Jan 2024 04:44:25 +0000 (UTC) X-FDA: 81720220890.17.49F2BD4 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2045.outbound.protection.outlook.com [40.107.237.45]) by imf13.hostedemail.com (Postfix) with ESMTP id 87B9320008 for ; Fri, 26 Jan 2024 04:44:22 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=iCYTuyQX; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.45 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244262; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+qG75r4WHkgWbMzwHKr0H1I3Mb70xWCu5pDmxLxzbm0=; b=P3St7wtf35Wg2lutY0uFFiUy55PZ2qE5Rjo6x1galPsrhkB7y3PX4xuQruhM32pUa8Lxkk YSejNvs8f3PleTYVl64tF0WK8O9wVUV4JMQP6p87oT+q32yVjWRN4o20MBt03wEAiRUiS9 A8LQf2YE+vqzLGe+axp6w82dvHM3wUQ= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=iCYTuyQX; spf=pass (imf13.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.45 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244262; a=rsa-sha256; cv=pass; b=00hXLvJl/qzlfLK222Y5yfwpQprxIhbvvDuJrTLsBRieY0gV1yD57n6j3jUqJjrU/D31n0 35d6SrCGFX30f5h5jTXvfnDtUB2cW1mz0+5Q5z1AJNi6F/XyBOf8ugHw4Otq4Mf6wpymvp 21Q1G8PJE2tHiOdWWNaWcyoqnbXzw2c= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=arIXtEDaW2ZrrZdfdRWg6FDXMREgjXCopdMb673d6b1hgWTbFIaygcXcjdQhCYkVPrvQg4K6c1DGFGIXIuI+HFYF2huxYmXpAgGmLcgn2hrwPDlXk7YoEaDAq9vSs8KDXd2wJYP0OwajB5zO2YtJSNxgMSug9LMj1vbtkiyxMNdYviRS2n9UP2jqGqWoXihINzN0CltLIqhTIAHZaDig9obn0LtqGOWfGN1j/aqhXrNKgviPJcHLvOYETZmtbRuNgjjxbWKDKJFR0kBuuRmNl+5RB30JSuHFijjlT4tQUsT4cq3QYsS1YoKMpYvtOvMKpYqv4qa0O6X/9iPiQ70sMg== 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=+qG75r4WHkgWbMzwHKr0H1I3Mb70xWCu5pDmxLxzbm0=; b=FCFdqCGWmi9ZHUZhSUNMEAniKR0J4+9hEWV5TIvnFfAEyw2ovdLPQlzGc1UGKHcRTBL8Gup3dGViUtSFQ3GHs68YLM9VrE2iSU8FccSrczg2zS/9lOdDYtzDxjpZQczclxX0xO2XEHg5wffx93rQ992u4adBo1c1yoiARtym1X1ehdyme7AKoLl5qgZAxu+Y8p66QkTYwq3R5VK4I6Xg1doW6GjOw/1KBXU0pNwoOD57VhQMaTbYUvAD3SwdNVTfv+Xhv0axY7z1ry0up1GAxwFYvrWwJdZaip1nxREQbatowbQLEOCpynRDY5Y8zrhk4SxMBSLuFdZ9SN4HmzohpQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=+qG75r4WHkgWbMzwHKr0H1I3Mb70xWCu5pDmxLxzbm0=; b=iCYTuyQXK9QqQXk+8+TeDi6mI7k8g7wYKx3vfgFNIrFc+qzXjbgYbkYDcBfVQYQ+WuNKK+vhigzeVCWu7tYAT27fmcf078+Cvej+O0HQNA9wj/l6of5CSXpVtAtkz7AoVEz13sdxk+qVKKoQ+I3TGC4uRgZLLQw3btH2z+XK5yg= Received: from BY5PR16CA0027.namprd16.prod.outlook.com (2603:10b6:a03:1a0::40) by IA1PR12MB6555.namprd12.prod.outlook.com (2603:10b6:208:3a1::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:44:19 +0000 Received: from MWH0EPF000971E7.namprd02.prod.outlook.com (2603:10b6:a03:1a0:cafe::a5) by BY5PR16CA0027.outlook.office365.com (2603:10b6:a03:1a0::40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:44: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 MWH0EPF000971E7.mail.protection.outlook.com (10.167.243.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:44: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.34; Thu, 25 Jan 2024 22:44:17 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 19/25] iommu/amd: Clean up RMP entries for IOMMU pages during SNP shutdown Date: Thu, 25 Jan 2024 22:11:19 -0600 Message-ID: <20240126041126.1927228-20-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E7:EE_|IA1PR12MB6555:EE_ X-MS-Office365-Filtering-Correlation-Id: ce8654d0-eb09-4f90-6a13-08dc1e29753c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: c1/N+NOdlDEYkL8nup6ZrqkbaTUwNTx4w1tb1kYLBF2ZsRUeoE/t4rCcWjHvSbOskC3cZ3B/D1Z4m4Dj9X31dfu/fKaKzwuoqfGO+iO0wqy6dTi/W1e2g3vLPSmO6K5ShdywZCLi+EPavjm3W8nicVnWvVQMUTNB0yiuYpdifelJd2jrgFT5O9X9JSzpK9ZcQbXmkabI92VThui4MTF0YR1TW8j97jWjVUVuLQwrLAMBk5zrHYbyVe0SUSbTF3qWMwisNZLJUEdVaI+7r+3qubvVtKWwK4bavN6LPixo4hEZ/T8LlYo8a4Y6KT1K7GDjAQHOTOhUvPnNukiY4oMVoyLOfXCSIKBsG6XyljAE7mPr+YlrAavs18G8K3LfmkCzRSaEUEMbXPifsR3F0mMHoOQ37KFWu8xj+diXpGq/iE0pQBKVwDxVVNsgJZ+YfpzkiyMbcT89heNQ8IAsmtgPYO6MMVnKDJbeM4lFmrtC5/08d2YmUjIMXLPk5x/H101OZRw4TAn6yc6Io4VucyT1P9kkFTsXKhgyCXufEA27X4R7F3P/9617yVnYS8Oaw/V3JMrRVUyyT5hI4MpNm0VncYs9RWAEnZo9q6xtE0TUh9ERq8HP6JPgdwLhUg2clR/l9BTwCnLQArCW7rQsp8Usgtq2ls+qV/Rk69kB6rk1HizZkvwx7pNbs3oUDudfPGW0rswQZ3XSu9kPTn56lnby1PisRRKjomyNU27UiemNwbazxDGQZxi4oPrxY5yn3JcBTToQ9fGqY9qtxXOX3oiSpA== 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)(4636009)(396003)(136003)(376002)(39860400002)(346002)(230922051799003)(186009)(1800799012)(82310400011)(64100799003)(451199024)(36840700001)(40470700004)(46966006)(8936002)(8676002)(4326008)(36860700001)(5660300002)(82740400003)(7406005)(7416002)(2616005)(1076003)(70586007)(70206006)(54906003)(83380400001)(44832011)(47076005)(6916009)(316002)(36756003)(356005)(81166007)(40460700003)(40480700001)(2906002)(336012)(426003)(478600001)(26005)(16526019)(86362001)(41300700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:44:18.7591 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ce8654d0-eb09-4f90-6a13-08dc1e29753c 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: MWH0EPF000971E7.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6555 X-Rspamd-Queue-Id: 87B9320008 X-Rspam-User: X-Stat-Signature: c3r46kni5mxhqpgynp9mm5ugn4jx5pij X-Rspamd-Server: rspam01 X-HE-Tag: 1706244262-553227 X-HE-Meta: U2FsdGVkX19vwycLVNUq0Hcl0KyHr2ZcL59LJycHclduoAgyFwsyFtEyWyJZcs61AwsEEdmLsLMlQcZks3JEq1dVC7Y7uApPb4efBVKGdeYTq5Hp2wi1uS6aVmEd/ZxeZVXD7BhS0bYprzpy7rs1wYmOaNRlhpjDBEYsga5YP9GTXGsQb5oXYW4qTLvxj6m3Xxo8jwt0f636NzkN7NhNbCY4udjMct5nDiKrWSkNM+x2XeyE+s9lxbCy5Qyh85EqhZQ6jYeozedNV0oLEAOR9oHzQA53+d04lDP5ECroikgnIeU12KUbHjb0tL7RLvnl6/68kb40uCcah6ApX5X+DWiBDRU63IM6dcNHljfXvx+1yfIGquL0orKaXPIC7Ggujk+3tri/7AkXNjELzZgSJ9XfPKYttjv4zXxE2e4TQOaPrM13+FRiPSGvAezp9EUNiZWZ1suj5QwNfXUwKaSjEm6Oxjd2cxyotnkxE05mvTUUE41UuzcR5dBHDAwGRRJB0Di6SH0+zJJVjHrLaMhPnyH4vYIAtKHHbPw9wOy39YIpWcF+UIYptfi3QCTnMPqPSR4yC1bCBKtluex06Py5P59Rmf01IUfX51RR96K51qHtikFv0NTn3Oo8oCH1wUUu7QdN/45wHJxBkNiswbk15X6myIZiKntt873IRww05ttCk/XfW8teQNvUnACA6CJTopqrcY0nt5kpSEx2j35v8N2Lbp/i23EtXCQ3al0vWbCFUbFRf8y25k7vyw45TuuxksuVuNiNbrptQh+qjhLKH96uNpjGYXsXTevXHIu3wRcns/+LXvHE4vqpA025HerGbOEdmk6ZasgWxyMKLxFpFoxmjDCCv/UI4GDiDFOkebmept1WJt3NqDfS7Hjgfn9AxXpFll34GAaJSphfWkBtV41Qae9XYlAu9fpSCcVWUGuvuSV1iT9LN9kugnkGeuVlDwbqFiQe3Ihi9QxWNDV e2A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Ashish Kalra Add a new IOMMU API interface amd_iommu_snp_disable() to transition IOMMU pages to Hypervisor state from Reclaim state after SNP_SHUTDOWN_EX command. Invoke this API from the CCP driver after SNP_SHUTDOWN_EX command. Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- drivers/crypto/ccp/sev-dev.c | 20 +++++++++ drivers/iommu/amd/init.c | 79 ++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 6 +++ 3 files changed, 105 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index b2ad41ce5f77..d26bff55ec93 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -1633,6 +1634,25 @@ static int __sev_snp_shutdown_locked(int *error) return ret; } + /* + * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP + * enforcement by the IOMMU and also transitions all pages + * associated with the IOMMU to the Reclaim state. + * Firmware was transitioning the IOMMU pages to Hypervisor state + * before version 1.53. But, accounting for the number of assigned + * 4kB pages in a 2M page was done incorrectly by not transitioning + * to the Reclaim state. This resulted in RMP #PF when later accessing + * the 2M page containing those pages during kexec boot. Hence, the + * firmware now transitions these pages to Reclaim state and hypervisor + * needs to transition these pages to shared state. SNP Firmware + * version 1.53 and above are needed for kexec boot. + */ + ret = amd_iommu_snp_disable(); + if (ret) { + dev_err(sev->dev, "SNP IOMMU shutdown failed\n"); + return ret; + } + sev->snp_initialized = false; dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 3a4eeb26d515..88bb08ae39b2 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -3797,3 +3798,81 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true); } + +#ifdef CONFIG_KVM_AMD_SEV +static int iommu_page_make_shared(void *page) +{ + unsigned long paddr, pfn; + + paddr = iommu_virt_to_phys(page); + /* Cbit maybe set in the paddr */ + pfn = __sme_clr(paddr) >> PAGE_SHIFT; + + if (!(pfn % PTRS_PER_PMD)) { + int ret, level; + bool assigned; + + ret = snp_lookup_rmpentry(pfn, &assigned, &level); + if (ret) + pr_warn("IOMMU PFN %lx RMP lookup failed, ret %d\n", + pfn, ret); + + if (!assigned) + pr_warn("IOMMU PFN %lx not assigned in RMP table\n", + pfn); + + if (level > PG_LEVEL_4K) { + ret = psmash(pfn); + if (ret) { + pr_warn("IOMMU PFN %lx had a huge RMP entry, but attempted psmash failed, ret: %d, level: %d\n", + pfn, ret, level); + } + } + } + + return rmp_make_shared(pfn, PG_LEVEL_4K); +} + +static int iommu_make_shared(void *va, size_t size) +{ + void *page; + int ret; + + if (!va) + return 0; + + for (page = va; page < (va + size); page += PAGE_SIZE) { + ret = iommu_page_make_shared(page); + if (ret) + return ret; + } + + return 0; +} + +int amd_iommu_snp_disable(void) +{ + struct amd_iommu *iommu; + int ret; + + if (!amd_iommu_snp_en) + return 0; + + for_each_iommu(iommu) { + ret = iommu_make_shared(iommu->evt_buf, EVT_BUFFER_SIZE); + if (ret) + return ret; + + ret = iommu_make_shared(iommu->ppr_log, PPR_LOG_SIZE); + if (ret) + return ret; + + ret = iommu_make_shared((void *)iommu->cmd_sem, PAGE_SIZE); + if (ret) + return ret; + } + + return 0; +} +EXPORT_SYMBOL_GPL(amd_iommu_snp_disable); +#endif diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 7365be00a795..2b90c48a6a87 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -85,4 +85,10 @@ int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value); struct amd_iommu *get_amd_iommu(unsigned int idx); +#ifdef CONFIG_KVM_AMD_SEV +int amd_iommu_snp_disable(void); +#else +static inline int amd_iommu_snp_disable(void) { return 0; } +#endif + #endif /* _ASM_X86_AMD_IOMMU_H */ From patchwork Fri Jan 26 04:11: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: 13532057 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D66B9C47422 for ; Fri, 26 Jan 2024 04:44:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6CCD56B00A6; Thu, 25 Jan 2024 23:44:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 67C396B00A7; Thu, 25 Jan 2024 23:44:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 502786B00A8; Thu, 25 Jan 2024 23:44:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 399CE6B00A6 for ; Thu, 25 Jan 2024 23:44:48 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0AA59160217 for ; Fri, 26 Jan 2024 04:44:48 +0000 (UTC) X-FDA: 81720221856.20.5D3D02D Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2085.outbound.protection.outlook.com [40.107.94.85]) by imf11.hostedemail.com (Postfix) with ESMTP id EF10A4000A for ; Fri, 26 Jan 2024 04:44:44 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JDR7d7tf; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244285; a=rsa-sha256; cv=pass; b=ca/E9KlYw3qg1ZoSQE1xqXBSOM3o/8JrQ2mwMltoEBbWE186EEi60wKEh9z52zIrHW4h3O 8lWWFNtjBSgggs/n+ioDW1t1ULlAakyo9Ha4XrBju+FADDCXkOVPTgga8ycBc6VAvv1Uww rh8pzhRUtR9tM6gsqZjFeV+zzRZUCm0= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JDR7d7tf; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.94.85 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244285; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=N0OvJ4iyrN4T9ToYeFsQcclgTHr6U+nK7L/b28IQX5s=; b=7BJcWdRWQMjUn40TEILEUL1nizkNEt6JTX5HiRH2ULQSJNGoSogaWF2oofkrKpfQnO1Hi+ jHX1Sqgah0NH+YYLMpLhvU1PjV4Qq/nPG2OQwRWe1vv2UXaD/WPvAdCKtX8ManHJMXZZ+k zvHTXq/eMYeHZA5SXDQoLdc5NYLUNWQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VNuLO09Y2Icd6tQwSLgqg2yFGdKF7CJMhHrKbvt5AgX6G1IzxKO0TiY9plBWBpgv1tmnD6Y++fFy+PjRt4TTE2EbbIfVG/EeQbVxcSIZvwKi1zHBVYKIJvod+rq+ssXJTCV8wp/wYYiRLOM4132Dg6Ryif5hX/LyNBGh1+q6KB34xK2Lxl2prPbCxIPVBtxzqrH86WRYaBFCTsX4Q6ASWZ4tl2pioHHzbthFNsYusbq5eEYm982mUvhgSAO2ALPXzAuAGO7UUZyGq2rK1s0nMDjjD88MfHlizgvRoofgfNsDTasD12fUrABlGfKCUha75ukK51775Nva/GoRoqwGdQ== 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=N0OvJ4iyrN4T9ToYeFsQcclgTHr6U+nK7L/b28IQX5s=; b=mJ9GgXBkyxLc4KN5Pv3qcNEfzTxk9oVM3N8PrDoED4bClgy9/PCOvNMCXbwu56Lw8wpLZ1G6SLgVjhFNAUZkHdIb8917TZRNbCgyHWn5Obdu0GMtahZu3+7wlnu4vL70j/AmJpo1jZ7rYDZ0B6FDg7J2slMtBPUzKYCYQmGkoPAhTV/nIH7BtdOOxeFNYbFJRPuiQMYUsnkVyMCafbx7042vUAUVnqdxO5WYWjEk06t9aJYRtzp09DFeAum8PCnctgvcZzEf4e1CvnkAUhgsqTHq9l+x9XQwnzoQs8ZVK9o5sWSYTqeAsyYY32GpBrvBhEAdJo87dgKGAQR8nOMuoQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=N0OvJ4iyrN4T9ToYeFsQcclgTHr6U+nK7L/b28IQX5s=; b=JDR7d7tf58xjzq7Adjtt5YxfCwGHfT3k5/oQpvPbrHeCL67VLXUD4NfsXcZXbiOW9VSLAGqY649IBS29AeN5IFjtyUJ8dEeE6O1CRC2BBV7BaCAThN+9pSRUk/Li7ZRYGxF7myWzJ9OyxIxc1FNtJHIenYeakTonvRw+OyOuzKs= Received: from MW4PR04CA0221.namprd04.prod.outlook.com (2603:10b6:303:87::16) by DS0PR12MB7533.namprd12.prod.outlook.com (2603:10b6:8:132::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:44:39 +0000 Received: from MWH0EPF000971E2.namprd02.prod.outlook.com (2603:10b6:303:87:cafe::bb) by MW4PR04CA0221.outlook.office365.com (2603:10b6:303:87::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 04:44: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 MWH0EPF000971E2.mail.protection.outlook.com (10.167.243.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:44: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.34; Thu, 25 Jan 2024 22:44:38 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 20/25] crypto: ccp: Add panic notifier for SEV/SNP firmware shutdown on kdump Date: Thu, 25 Jan 2024 22:11:20 -0600 Message-ID: <20240126041126.1927228-21-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E2:EE_|DS0PR12MB7533:EE_ X-MS-Office365-Filtering-Correlation-Id: 57beac15-2d24-4074-f700-08dc1e298188 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dll6qedUGuxpLhR2GpHWFEKBugV8wZtIQxRVVEXp/qatYdgTkkJBnLnXMARvEmvAaj2X0ehLQHBrGOuggTMgd+5iHkd9k1vIGPDDOMsjp+p6nPhm2hAQjg9WRVYCz9X2Wqe7k76j2iVObM6B9JKFjMZR/73ljfcbzvtQ4omXHF17+Ujcu0ddDUknmaTHBkUACJEs2FysbpE4XC8+oRVqY6XyhwYeJLnLLIrZ/kp1VSefudV4BNyblArsTBKwJAoSGrW7J7rQNRqc4xKW08HOrNB9WhJFvYqln5Rwv8zEh1lusdq6TCs/d5v1gekbqsfKXF9LOsdDSa/4rWWMcu2nFUggSkqjtMMl4TC+QOscYUfI0XZMvWXpRTFwOHHfAOGuVK+Gcs4mKzb3HjccWSnGQyBgPrPWSJSBkYFee8mKJJTvtj2fHhXVJLdd4cRMtZf89nK1HqZWfeXqzSnFmYTQtNQuyqrJQTDsSS3dJmuhTn/qM8bZqIFwc6c0bM5qMMYrRL2JSGZco4wPvaht72/8yIByD2B+JepIooNLATq18CxAXwNDgxcK8MtJFq6HQgsY55Og1pRy38/FoUiDKjHOG5D5bR52l810R7llMV60nNqm7YcZRrKeEG3QnOIzMc2K3lnBcbEG5k0rdyjEiQf1KApAHzePafLSZqD1SZJKb47HU1SalqfNmIc3+V+hHpzPu0+y3crTZkJQ0HRp24VbGs1D6byoLqFl9iI7rH31/F4Ybgp1A7fyOVDlsYAvaNiVubpHWPh/s1S8i43p8bRBnnGsc+D1FG06BWZsp0RRIVXWcsInQv+Z20g7ZjA+bYR1 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)(4636009)(376002)(39860400002)(136003)(346002)(396003)(230273577357003)(230922051799003)(230173577357003)(64100799003)(82310400011)(186009)(1800799012)(451199024)(40470700004)(46966006)(36840700001)(36860700001)(47076005)(36756003)(2906002)(86362001)(41300700001)(356005)(82740400003)(81166007)(6916009)(54906003)(316002)(70586007)(70206006)(478600001)(6666004)(336012)(426003)(5660300002)(4326008)(44832011)(2616005)(8676002)(7416002)(16526019)(1076003)(26005)(8936002)(7406005)(83380400001)(40480700001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:44:39.3778 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 57beac15-2d24-4074-f700-08dc1e298188 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: MWH0EPF000971E2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7533 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: EF10A4000A X-Stat-Signature: o9qpwxazk47ysag9356c8ccnexkpxn57 X-HE-Tag: 1706244284-553242 X-HE-Meta: U2FsdGVkX19vYMivuUe1T0usTKHhN8B5F1z2oJcSvLxBztACXMEZlwzd9wm+H8uJ0a7+bRvYRv0zYd08m3ZQDQNtgoWSfaHgGNG2eCnMxSDcLRv2Tcw8j0Mhjo7jxnTW/5TdEPybOpfhjh/kJIxrpYEmFwYUX5YuTgQ/NJNi3M5nthuedJ3+n43uzz2zPyW+wHnmrFJQ9AmdoVE9X38l2rrlx9QeqqWGust88ItHQ9JWzYxaw4ghnK0f7uGF0n88jyzags64iRfrzv7EdBOGtow5CT75dHB3KuJXtmVVcjQ3c6N8JqzppDg0Mq5FiFiwesxJtAXdqbNVJls6B0a0HQN88WwlaDDzgIR7jloM5nLI6Y6m3WWli72P+geDudJS5JHeJf6bhLb8bUkEbmleiFjsY3j3ORbBf6HuH4mgh6ut95G7mLUCVMmju+tInyhXwMxwgN+P91WD/4VlY7VXNx/fx7ithnn02Oa/AEclCIPT5T2ebKBGj+JhRh39FHtxXzpOHkVHPNpIsG3GTP7xqv7l/qu9vv3j23a2o3z2xjw7AwDZW4cEGQUtuup+sH3DmwR9ODCFjRPP64qD0J3jIriQPubJENBr/uF7W4b7NsfK5w8UkM+tgB3klA2JxNpWzy4b+VN38/I22JSeJpVPJ1UCy4g1zuL2oHt/LZJh3qiJpIZ/EQhbq9TX/LR3sshPqD92yYI4ZfOGMlzlqT9Fh4Azd2rUqC6b9hRaAQqxMjz+BBfTf52cHCRuryU9Naid1EE1EhneDILiP60t80D4Wbrb/AhWT0aKtLSV86Y0VSzNePbRM4spw5JcvQWnu3fr0riBHrOpQyeMGRx9VLY2BVam61bCHGjP+V0P0WwZYoI3lm7H/CYLLSFPTbabeCIXGOw0ztsxSRlP4JOMTyJG1dJHBfTdgXnfJdPW6Kz3XtkX1+hXQdZ7hCI8LAC8t+SJICMsyBzIQSufzzwr0K9 vkWDDOqV 228FQ1UyXg8to9CjpPFu5PGKHhpnv4AYM28miRKx051hqVFdBdqmB/EWiITtdF9wgnE1YiFVQoP0dcPwCfFwLrebyuF8K1lhhaywyOrS0YyXk0fxvsmdL1DbgImf/3tPRr+pfv7GD7deP0daV6y5YXS1vxiGTT42tPYMLSMzFsKZxhBRvASkDAycyZoLWfyq89rI+sdNJ16XEFMO+WH9vpIY9AO1atTyS5+YGO+LMrcjmDPbikHbn20VeeqKN/lLTS1blUamCY/tdm0sbh5xH6WRauBTGPkeuM9SS+H0K+6Omme/BTtTYY6lWYzQPDbzc1z/iNYPuZ/H83mY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Ashish Kalra Add a kdump safe version of sev_firmware_shutdown() and register it as a crash_kexec_post_notifier so it will be invoked during panic/crash to do SEV/SNP shutdown. This is required for transitioning all IOMMU pages to reclaim/hypervisor state, otherwise re-init of IOMMU pages during crashdump kernel boot fails and panics the crashdump kernel. This panic notifier runs in atomic context, hence it ensures not to acquire any locks/mutexes and polls for PSP command completion instead of depending on PSP command completion interrupt. Signed-off-by: Ashish Kalra [mdr: remove use of "we" in comments] Signed-off-by: Michael Roth --- arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/crash.c | 3 + arch/x86/kernel/sev.c | 10 ++++ arch/x86/virt/svm/sev.c | 6 ++ drivers/crypto/ccp/sev-dev.c | 111 +++++++++++++++++++++++++---------- 5 files changed, 102 insertions(+), 30 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 435ba9bc4510..b3ba32c6fc9f 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -227,6 +227,7 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct sn void snp_accept_memory(phys_addr_t start, phys_addr_t end); u64 snp_get_unsupported_features(u64 status); u64 sev_get_status(void); +void kdump_sev_callback(void); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -255,6 +256,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { } static inline u64 snp_get_unsupported_features(u64 status) { return 0; } static inline u64 sev_get_status(void) { return 0; } +static inline void kdump_sev_callback(void) { } #endif #ifdef CONFIG_KVM_AMD_SEV diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index b6b044356f1b..d184c29398db 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -40,6 +40,7 @@ #include #include #include +#include /* Used while preparing memory map entries for second kernel */ struct crash_memmap_data { @@ -59,6 +60,8 @@ static void kdump_nmi_callback(int cpu, struct pt_regs *regs) */ cpu_emergency_stop_pt(); + kdump_sev_callback(); + disable_local_APIC(); } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 1ec753331524..002af6c30601 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2265,3 +2265,13 @@ static int __init snp_init_platform_device(void) return 0; } device_initcall(snp_init_platform_device); + +void kdump_sev_callback(void) +{ + /* + * Do wbinvd() on remote CPUs when SNP is enabled in order to + * safely do SNP_SHUTDOWN on the local CPU. + */ + if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + wbinvd(); +} diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 649ac1bb6b0e..3d33b75b4606 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -216,6 +216,12 @@ static int __init snp_rmptable_init(void) cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + /* + * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic + * notifier is invoked to do SNP IOMMU shutdown before kdump. + */ + crash_kexec_post_notifiers = true; + return 0; nosnp: diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d26bff55ec93..9a395f0f9b10 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -143,6 +144,25 @@ static int sev_wait_cmd_ioc(struct sev_device *sev, { int ret; + /* + * If invoked during panic handling, local interrupts are disabled, + * so the PSP command completion interrupt can't be used. Poll for + * PSP command completion instead. + */ + if (irqs_disabled()) { + unsigned long timeout_usecs = (timeout * USEC_PER_SEC) / 10; + + /* Poll for SEV command completion: */ + while (timeout_usecs--) { + *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); + if (*reg & PSP_CMDRESP_RESP) + return 0; + + udelay(10); + } + return -ETIMEDOUT; + } + ret = wait_event_timeout(sev->int_queue, sev->int_rcvd, timeout * HZ); if (!ret) @@ -1316,17 +1336,6 @@ static int __sev_platform_shutdown_locked(int *error) return ret; } -static int sev_platform_shutdown(int *error) -{ - int rc; - - mutex_lock(&sev_cmd_mutex); - rc = __sev_platform_shutdown_locked(NULL); - mutex_unlock(&sev_cmd_mutex); - - return rc; -} - static int sev_get_platform_state(int *state, int *error) { struct sev_user_data_status data; @@ -1602,7 +1611,7 @@ static int sev_update_firmware(struct device *dev) return ret; } -static int __sev_snp_shutdown_locked(int *error) +static int __sev_snp_shutdown_locked(int *error, bool panic) { struct sev_device *sev = psp_master->sev_data; struct sev_data_snp_shutdown_ex data; @@ -1615,7 +1624,16 @@ static int __sev_snp_shutdown_locked(int *error) data.len = sizeof(data); data.iommu_snp_shutdown = 1; - wbinvd_on_all_cpus(); + /* + * If invoked during panic handling, local interrupts are disabled + * and all CPUs are stopped, so wbinvd_on_all_cpus() can't be called. + * In that case, a wbinvd() is done on remote CPUs via the NMI + * callback, so only a local wbinvd() is needed here. + */ + if (!panic) + wbinvd_on_all_cpus(); + else + wbinvd(); ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN_EX, &data, error); /* SHUTDOWN may require DF_FLUSH */ @@ -1659,17 +1677,6 @@ static int __sev_snp_shutdown_locked(int *error) return ret; } -static int sev_snp_shutdown(int *error) -{ - int rc; - - mutex_lock(&sev_cmd_mutex); - rc = __sev_snp_shutdown_locked(error); - mutex_unlock(&sev_cmd_mutex); - - return rc; -} - static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) { struct sev_device *sev = psp_master->sev_data; @@ -2117,19 +2124,28 @@ int sev_dev_init(struct psp_device *psp) return ret; } -static void sev_firmware_shutdown(struct sev_device *sev) +static void __sev_firmware_shutdown(struct sev_device *sev, bool panic) { int error; - sev_platform_shutdown(NULL); + __sev_platform_shutdown_locked(NULL); if (sev_es_tmr) { - /* The TMR area was encrypted, flush it from the cache */ - wbinvd_on_all_cpus(); + /* + * The TMR area was encrypted, flush it from the cache + * + * If invoked during panic handling, local interrupts are + * disabled and all CPUs are stopped, so wbinvd_on_all_cpus() + * can't be used. In that case, wbinvd() is done on remote CPUs + * via the NMI callback, and done for this CPU later during + * SNP shutdown, so wbinvd_on_all_cpus() can be skipped. + */ + if (!panic) + wbinvd_on_all_cpus(); __snp_free_firmware_pages(virt_to_page(sev_es_tmr), get_order(sev_es_tmr_size), - false); + true); sev_es_tmr = NULL; } @@ -2145,7 +2161,14 @@ static void sev_firmware_shutdown(struct sev_device *sev) snp_range_list = NULL; } - sev_snp_shutdown(&error); + __sev_snp_shutdown_locked(&error, panic); +} + +static void sev_firmware_shutdown(struct sev_device *sev) +{ + mutex_lock(&sev_cmd_mutex); + __sev_firmware_shutdown(sev, false); + mutex_unlock(&sev_cmd_mutex); } void sev_dev_destroy(struct psp_device *psp) @@ -2163,6 +2186,29 @@ void sev_dev_destroy(struct psp_device *psp) psp_clear_sev_irq_handler(psp); } +static int snp_shutdown_on_panic(struct notifier_block *nb, + unsigned long reason, void *arg) +{ + struct sev_device *sev = psp_master->sev_data; + + /* + * If sev_cmd_mutex is already acquired, then it's likely + * another PSP command is in flight and issuing a shutdown + * would fail in unexpected ways. Rather than create even + * more confusion during a panic, just bail out here. + */ + if (mutex_is_locked(&sev_cmd_mutex)) + return NOTIFY_DONE; + + __sev_firmware_shutdown(sev, true); + + return NOTIFY_DONE; +} + +static struct notifier_block snp_panic_notifier = { + .notifier_call = snp_shutdown_on_panic, +}; + int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error) { @@ -2200,6 +2246,8 @@ void sev_pci_init(void) dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_initialized ? "-SNP" : "", sev->api_major, sev->api_minor, sev->build); + atomic_notifier_chain_register(&panic_notifier_list, + &snp_panic_notifier); return; err: @@ -2214,4 +2262,7 @@ void sev_pci_exit(void) return; sev_firmware_shutdown(sev); + + atomic_notifier_chain_unregister(&panic_notifier_list, + &snp_panic_notifier); } From patchwork Fri Jan 26 04:11: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: 13532059 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45767C47422 for ; Fri, 26 Jan 2024 04:45:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C28B96B00AD; Thu, 25 Jan 2024 23:45:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAD596B00AE; Thu, 25 Jan 2024 23:45:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9DC086B00AF; Thu, 25 Jan 2024 23:45:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 831576B00AD for ; Thu, 25 Jan 2024 23:45:48 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 641221A05A6 for ; Fri, 26 Jan 2024 04:45:48 +0000 (UTC) X-FDA: 81720224376.02.AD13EE7 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf14.hostedemail.com (Postfix) with ESMTP id 7DC1E100009 for ; Fri, 26 Jan 2024 04:45:45 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=26LcK3hw; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244345; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vOqbJt4AWqrFUm6TwvKdJ4cOhGB1Q4xIXK8repYmn8Q=; b=0MoPoAkV9TCBERxNWEziVz1xglx1Px0486tHR3uPWcQKTF00W+KYMrR67N6hiBfkF6iyHk AhJxW3Eb5crU9addxYGZgcClYuwS9gP1rUSgbulLONqkjzKG3dVzAOuWwmxbAGQLB8mYFP O12OWc7x1nJ3OLrIAR3/677MndhGrbY= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=26LcK3hw; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244345; a=rsa-sha256; cv=pass; b=6xEUPhl/zCrmyvFmtukhp2f1ED/XuJp/PE9nmptLzj+Z/1x5vlGlycKZAO19dNJwSwS0bd WaIDKNMrWg/LLXJUBOc3ZnrE0I8T9h215y1efKi1/GdL9TrbKOT5lF3UU1v5Ec5bVsp/23 6z3krevmfYi7PWYurhmKBs+LYbJAud0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VDPAFUKtRofZIhx2Rnecs1yuorc8mimYqsMxgyLxMtXWty3g/bNWhjJoOhMqhsb+qEnXu+h0EsBQ1oOW3QAnfZkIlSDA/mn+Bd44r4ouMwMlZzGIOaf7vIy0evxx04fxZUnYbALeEEO6qeQcFt6m6/WKcCZSRYiaqxAHYTA06ByemZEWju8UKW3kyGpJiSuR60EakiAq1d+2sYJghJRA3FHJLx+xzhtzozZfVS6gRhCysv/FPArfWgxxqd+Oe2CA/a2mIc0mzbTALfCTLPA3QHf+cbUm1BCE+Jl3NDCulFkqSDnSWyEibg7b5AIjaN5qBRpH0PEh5VN+/P2/OBsq8Q== 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=vOqbJt4AWqrFUm6TwvKdJ4cOhGB1Q4xIXK8repYmn8Q=; b=duE3Y6hLNbmsayEXZuq0V+NFG5ij4QtNM2L4oGyWL4wfd+tAxmI1BT67d+lWGe8SUoaynCfLpS4jNqm1NIWsMRnoPXUjMGKDbw/7JB5pgsfuSoazFnJPOHyWmEzpjKDYKteWPLo60OMfIquXNI7eZl06G8NvDokrpSPJg3kckVpfKTWNUWthJp/SW3BT0M0XGmmc7QEiG/2kULzjUSFqWYu0fx30dU6ohT51+ZwmgxcLIRfESu2NEVE49GwB86yYCZgR+G7gzqA0h0SoZnoxAIRgbrAKLf5SosKmN9FDPFDyQ+IMN8nP6PxbHIoxsF+q8TN97p+7zyAa4FzHADdLvQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=vOqbJt4AWqrFUm6TwvKdJ4cOhGB1Q4xIXK8repYmn8Q=; b=26LcK3hwjochdB05JbxrZNmz/4y9l6UEwizxNuY/Rfk6BtoT2pjl+LE2Tl9VPZkxEttXN5iBABoWnyyxrnIRiiv+Jgm3zyCjgD7z+G17DdSYPcrIh7WifnCBrlKx93wbrUVqivWfCgOo+6QXlzbgVtd6NGbYyNetfVpkNbTM/ew= Received: from DM6PR08CA0011.namprd08.prod.outlook.com (2603:10b6:5:80::24) by DM4PR12MB8521.namprd12.prod.outlook.com (2603:10b6:8:17e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 04:45:43 +0000 Received: from DS2PEPF00003439.namprd02.prod.outlook.com (2603:10b6:5:80:cafe::86) by DM6PR08CA0011.outlook.office365.com (2603:10b6:5:80::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.38 via Frontend Transport; Fri, 26 Jan 2024 04:45:43 +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 DS2PEPF00003439.mail.protection.outlook.com (10.167.18.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:45:42 +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.34; Thu, 25 Jan 2024 22:45:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Marc Orr Subject: [PATCH v2 21/25] KVM: SEV: Make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Thu, 25 Jan 2024 22:11:21 -0600 Message-ID: <20240126041126.1927228-22-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003439:EE_|DM4PR12MB8521:EE_ X-MS-Office365-Filtering-Correlation-Id: ce326697-6c1f-45c3-fa13-08dc1e29a752 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TRaHYen1j3hWp/i4+RwYnPQSVdKEkvEzZlJDTP+R5EIPr+SvHcQJWNvx6S4G7rX9rDx32AwyEAWWDFWAGmBEwKrA1kVfibCh660wxDiuKf96l7DNSDQiRekbLAJXbmf1Ak0ALxpj+GAtYe04Dxp6/yYhHxUOS5PqNNtsy2ZyoIevk+JjWBYv0C0I4epG26dQzhdIZ8DKMOKAubaeUj85tgfnhlCqY56RhpOrTQLDvR6KXQ63f+f/LBj5mQ5mMAq5sG6TzETh3MUkexqyYwXrkOfLJi2as1AqtXDqCHVTqsptp5ab3JBNDHdp4zFmYWjTDXhOeCyHAHEReR6aYGTGHZqCUe1K+duN9bJMTf9WOQ1KEidRCbrY+ZJ2za8O2iuwCClM8SIm2wQEpg1m/ieHwtEG27GGB6oDVMwEDZYqXe0r8OwFZOPapqysAf/86P1QXKqvItAoMo3NJoIomTchKORj1W+fcwao5sn+wYbHvBVbJvHWlfzsy9MwkI5llv+iSHrN18GTrpKDw4iJZ42fM6qdEHh/9s9XW6PR3hNUAs7a+cj83g+xCulH8z4JEbK5EM5H1vzSzhLUgLvXxPIv+b0xfkBcfeFiB8fI+OuOHzt2UAx07NDLD0sAjvqTRX4IP5nzRf39cXVhhEaata2tLE3//SgaquzI0tMr0iMu/CA7VLNfkz4sddz4CMexnCAnwXG9cLMRKxdCry3eDExeYOnhbto0JYB6E/gr1XsFcO7ghTiacp5wtt+AlViTjNMN9qDiy7NmahyTdUlCkJEeXg== 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)(4636009)(376002)(346002)(136003)(396003)(39860400002)(230922051799003)(64100799003)(451199024)(1800799012)(186009)(82310400011)(36840700001)(40470700004)(46966006)(336012)(426003)(1076003)(26005)(16526019)(36860700001)(2616005)(356005)(6666004)(7406005)(5660300002)(83380400001)(7416002)(47076005)(2906002)(44832011)(41300700001)(478600001)(316002)(8676002)(70206006)(70586007)(6916009)(54906003)(8936002)(4326008)(36756003)(81166007)(86362001)(82740400003)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:45:42.8555 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ce326697-6c1f-45c3-fa13-08dc1e29a752 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: DS2PEPF00003439.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB8521 X-Rspam-User: X-Stat-Signature: puo7wnk8xtq8c4d5ar9ky7c4jz5dj51q X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7DC1E100009 X-HE-Tag: 1706244345-140479 X-HE-Meta: U2FsdGVkX18il0/XPY9tKO7efQitpCDAdYcCWpNAxcD9NuASr9XucuY6khqhMG/ujdTPJrRGoVmq3cAWWztkvyv1cYfPGGl6Zan66sTicocArp8Xsb0+oxs5wi9u882xQDF43gkR6TpK6NVf3QI4lFbI86QkdmFtpc3fD3s/tS8YSK1cvCYzVclaIPnO2aVaIwP4olBkwiitQPN6mhWIwuQVpPD9WAT2wcRpAFusdobZBSGZXi9fuOTM4Wgl2ITzL2xWfDpR8B+BtvkKRLin7lABeHXBwcL9+me2Qug2lAMyZpRLE+f6x99X3Ng4sW6EUaV6l/JWxm8+0e5m8VQvgtn19xRyfKrl5g4mKS2T/pMLWwvaU6m3HPD5U3ppmt/vr1AZmYF5zjM9nrcn0R6cSR2PrbdG1UkAII2l8UWn0r/XUfrSL083lYBLwyDrNHcUb4rxTTfZeiQrPfMtVbAGywl6SKkYouu3ddSXwNs/Jb2DjYVICeWOTrDV0FUY+00feZkJCwAoqE2Grcig9equslzhgpEChjDkIQ68PtH/8z7Jk8Tbl0qkLRjsAHy8Ubex9FcvskwX06IRCmO7NaijPMrPbHe5C2YXZ8DV0IhQtkUSuUA1gTPfiazoO+vwaN94eNUPg8VhTSKS9fkMLp1SfhcnjKvSsdnPxk1/Rj7iECQ1UcV3hGChhmEVMKDzRXpz2T2npCs/d9lcWn+23LCYCHpVctn0TYY61Izi6zRvCachidLJpNYAc9sxEVdITgghM7zPXSkUD3ocKq/T5Q2GFCRiXFOd1LXNHqbqyfF3Qp6u9pqPoHPypvp9ZAMBuHf2xmtzsevoGU3exrnvv/ffYCJZ/nUmL0DpRlioDCUUMtcD6C62JASuCTPtqYfwtBxtwmOikTWmvwQcAWQFCvoWiYOjTmG8XPSt1nD9ra5/c1Gb7IRbnowhAy6vS1maj75FJuBUXwxGJXF7wkZ0pid n1drflzz IAybVnD9PSB4K6TZXJjF0HUWD+UqNX7kouoXZu8+PHOAobHt+mCZGa9E77l7ToQyiJfyLCv0UZjyk5YYPjXYVau3nSSrKApKR8oMe3XGDDx2iVUmmd/A6J1UybK4FXcUxKNHK0YCif9JiMTECeGvLYG4GJdTsRC95hVbiB+qHIC9RVow2bizxnrB5uRCDTRK5OOg8fgJkTGXT5ohrBmGroEWf7AjOPrmy4AcqI6arU/j+m6NCFWj6lEW07/VsBlexxuJAaEgD6u9u3T2SvJZoT+eKpMlGVg6+ccn66RoSNQLlAxKc0FmGUTzjFGvDJiBq2bHUqkEgn3QZ1o4TlQvsIdz9SxwHEySjr3eJ7lBsv6Rxzt4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2MB or 1GB) collides with the RMP entry of a VMCB, VMSA or AVIC backing page. When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC backing pages as "in-use" via a reserved bit in the corresponding RMP entry after a successful VMRUN. This is done for _all_ VMs, not just SNP-Active VMs. If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2MB-aligned and software accesses any part of the associated 2MB region with a hugepage, the CPU will incorrectly treat the entire 2MB region as in-use and signal a an RMP violation #PF. To avoid this, the recommendation is to not use a 2MB-aligned page for the VMCB, VMSA or AVIC pages. Add a generic allocator that will ensure that the page returned is not 2MB-aligned and is safe to be used when SEV-SNP is enabled. Also implement similar handling for the VMCB/VMSA pages of nested guests. Signed-off-by: Brijesh Singh Co-developed-by: Marc Orr Signed-off-by: Marc Orr Reported-by: Alper Gun # for nested VMSA case Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Acked-by: Vlastimil Babka [mdr: squash in nested guest handling from Ashish, commit msg fixups] Signed-off-by: Michael Roth Acked-by: Paolo Bonzini --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 17 +++++++++++++--- arch/x86/kvm/svm/svm.h | 1 + 7 files changed, 54 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 378ed944b849..ab24ce207988 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -138,6 +138,7 @@ KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) 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) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index b5b2d0fde579..5c12af29fd9b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1794,6 +1794,7 @@ struct kvm_x86_ops { unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags); + void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 3242f3da2457..1edf93ee3395 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2815,7 +2815,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs = static_call(kvm_x86_alloc_apic_backing_page)(vcpu); + else + apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); if (!apic->regs) { printk(KERN_ERR "malloc apic regs error for vcpu %x\n", vcpu->vcpu_id); diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index dee62362a360..55b9a6d96bcf 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1181,7 +1181,7 @@ int svm_allocate_nested(struct vcpu_svm *svm) if (svm->nested.initialized) return 0; - vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb02_page = snp_safe_alloc_page(&svm->vcpu); if (!vmcb02_page) return -ENOMEM; svm->nested.vmcb02.ptr = page_address(vmcb02_page); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 564091f386f7..f99435b6648f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3163,3 +3163,35 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, 1); } + +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) +{ + unsigned long pfn; + struct page *p; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + + /* + * Allocate an SNP-safe page to workaround the SNP erratum where + * the CPU will incorrectly signal an RMP violation #PF if a + * hugepage (2MB or 1GB) collides with the RMP entry of a + * 2MB-aligned VMCB, VMSA, or AVIC backing page. + * + * Allocate one extra page, choose a page which is not + * 2MB-aligned, and free the other. + */ + p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1); + if (!p) + return NULL; + + split_page(p, 1); + + pfn = page_to_pfn(p); + if (IS_ALIGNED(pfn, PTRS_PER_PMD)) + __free_page(p++); + else + __free_page(p + 1); + + return p; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 61f2bdc9f4f8..272d5ed37ce7 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -703,7 +703,7 @@ static int svm_cpu_init(int cpu) int ret = -ENOMEM; memset(sd, 0, sizeof(struct svm_cpu_data)); - sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO); + sd->save_area = snp_safe_alloc_page(NULL); if (!sd->save_area) return ret; @@ -1421,7 +1421,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); err = -ENOMEM; - vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page = snp_safe_alloc_page(vcpu); if (!vmcb01_page) goto out; @@ -1430,7 +1430,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) * SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page = snp_safe_alloc_page(vcpu); if (!vmsa_page) goto error_free_vmcb_page; @@ -4900,6 +4900,16 @@ static int svm_vm_init(struct kvm *kvm) return 0; } +static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) +{ + struct page *page = snp_safe_alloc_page(vcpu); + + if (!page) + return NULL; + + return page_address(page); +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .name = KBUILD_MODNAME, @@ -5031,6 +5041,7 @@ 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, }; /* diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 8ef95139cd24..7f1fbd874c45 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -694,6 +694,7 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); /* vmenter.S */ From patchwork Fri Jan 26 04:11: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: 13532060 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0535C48260 for ; Fri, 26 Jan 2024 04:45:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5970C6B00A4; Thu, 25 Jan 2024 23:45:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 51FB96B00AE; Thu, 25 Jan 2024 23:45:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 371146B00AF; Thu, 25 Jan 2024 23:45:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1D3656B00A4 for ; Thu, 25 Jan 2024 23:45:50 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E6603C0236 for ; Fri, 26 Jan 2024 04:45:49 +0000 (UTC) X-FDA: 81720224418.25.8A47485 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2074.outbound.protection.outlook.com [40.107.92.74]) by imf20.hostedemail.com (Postfix) with ESMTP id 142C51C001F for ; Fri, 26 Jan 2024 04:45:46 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=VRpfg1eR; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244347; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9AMGVMMWRfpMi4iyhUnQz528Hz/O4DZ5uaxI+sp5psM=; b=tgA+gnaHqUX9Y92AXfSwNaIdJfYG4twAYDnjhjwpwUzGwk81pTLUFIsgbZHU0qh9rBFSFb MZ55iO7zpriQSggk1aIipDYY//62lJRe4/LcvT0lzOO1jeQEIA6apEOTsZJCbP5lVgpTfx x/pM10Y2YxmifdOHswTuV+nQghptpi8= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=VRpfg1eR; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf20.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.74 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244347; a=rsa-sha256; cv=pass; b=JW18Tg4liQZLVDnse0+ykqkFoEnNAMbzd8gSfwDVozxrMNRmGz8frSBYcrDYoPCh2FWXE3 pgvLsMwEBGdWTz9uqK9+uvGGl2k1nR4x1wQslQVBRWH8BUP2WNItHkxwdyoEpGj3P2N6lY UO1N77UQ6ylUlTW+hU5AqKv85cRFpIg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J6wjTveJHPX6U+uUvmKwgGtIa8NA12O44etpuwPmlmOPIHinAZ3BP3+PK/SF7N5pXWzz2EHx8HwsGguJWrOpg7V5+o0CTVTBiI2YH8mX1KKG2VIfN4WmQN7RTknrY8f8YimYopZaRHx/JTfjDSju4lSKG29GSPoSjAx+m/+uqBVRx188JYaBKVWERrZN88ap8lKe2OT+5iM9/rQlSN7LfD57Hfn9Ia4Cgse56CBQ7kts34FAZVBOillzS0l/DfVuGozqQKXVJOtcsNIaW8chQkb9O1tAHqNcDKw+hICra6+AQrMZ+hVEHEXdE9Sa4AXBlRaWxOGi+DE5W9l39pAKqA== 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=9AMGVMMWRfpMi4iyhUnQz528Hz/O4DZ5uaxI+sp5psM=; b=oPkgfGwNW1mjAOS64BqG8Lro1NE76vwvXzlIz6HTtyhtMmDzLe4L+ktAdcY1TFKTi068upOlsV4evEDTLIu8/xoNx43fe1GUQ4ASVQ8I9KzN64R9Zo4cffJnBZb8W3eS5G9Z7/pRz7Io9msWgce0rNCAQN0bxbEA58EHykYQT7yZImlWd2GKq0IIZPA7K1Ub+N+7aUDsjDvk2DdziF0uhnFETvpdqrmclpnDA2zUho4tunYsKB8mvJsBPXBg3s4/j1qUq5itNk56wIK040teyXgIFPZOXOvUEw4ShwAJP0PK7k8F0OY5esQqDOyolP6tBUqX+X+2FjOkFwBNR1kq5g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=9AMGVMMWRfpMi4iyhUnQz528Hz/O4DZ5uaxI+sp5psM=; b=VRpfg1eRJBgaFp0qlb4QLS86yagyUuSMiUh32zHu8+WMev2gdIuma23xhcZ5cj1jf/CNZ1E8RFefCyRxhTmiINmzIkICwuOP0FeNaUvDqRH/jgP01QhxHJk+utzUOM9J9sMh3Be059ZhIIYR+lvZR3z6/Atk8gm0zA+xm1NOVik= Received: from DM6PR08CA0011.namprd08.prod.outlook.com (2603:10b6:5:80::24) by BL0PR12MB4915.namprd12.prod.outlook.com (2603:10b6:208:1c9::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:45:44 +0000 Received: from DS2PEPF00003439.namprd02.prod.outlook.com (2603:10b6:5:80:cafe::69) by DM6PR08CA0011.outlook.office365.com (2603:10b6:5:80::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.38 via Frontend Transport; Fri, 26 Jan 2024 04:45: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 DS2PEPF00003439.mail.protection.outlook.com (10.167.18.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:45: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.34; Thu, 25 Jan 2024 22:45:41 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 22/25] x86/cpufeatures: Enable/unmask SEV-SNP CPU feature Date: Thu, 25 Jan 2024 22:11:22 -0600 Message-ID: <20240126041126.1927228-23-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003439:EE_|BL0PR12MB4915:EE_ X-MS-Office365-Filtering-Correlation-Id: 100c4706-a2ee-4b33-17f6-08dc1e29a814 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rHP180opXMHn+HCrZQ0/PpXNBbJB3JxvKE7k+OpmZnHQ+LzBKxp7gRHVQQlmPosLwWL++N8oxZ0cEbPepgnvyeoN65rhJD3kV+P+3McVQ4Yy8lKrgyy+pUKyCyqayu91rZ0/pmwN2PsWSDg06ZeGVpMPKNYA0fi2RGHDGJJhP4WL/CqictxAXG2PVWqTsE78+i7Uv8SUzxm4VM7v+X4Yg43X5nckjA2+mlYNa1w0jY892ofL/LsvoLMNfKsvBXVePYzbjoNKUGWLYmhxoJAjbqA8pxrYNIDpnE8GQN6h9/IFecUHkDdOu5efRSN/dbWIgaEnQNrF/QPl5nY/O94f5qXiuBBPtA4tdSZxcS6/PLIg/1fAoWt3YrofpWrZtIyV6CtgQOOjiYD1Yj5Cn9QMrtcgVk8hB7fiRHZ++yQI4ivVp9GckmuMjiE8L0eAFk87Md0MBkJHcvxE/NrI6Rx4LM60WOSoWVO5MHTZycc/9+BiMSPOGX2SCt1CvBzp9GgpcPr92XiJGCOwj/ntiYOy2LbAq5qYWGEiW3iaNuBYggEgcD2/bF9CZrBzsKssQFq85WuPGcZJeEiMZPMYtpjwyojzaa7w36Cop09OA/LK05/h5toLF24mK3gCUSH9K3ZB19nu6qb8m2Aqa40AVgkHI1lx/9Qz/Sqpv2h4xj5NouAHa96PCFhvZ746MtgSIOa/oz69mjxpYVH1ArG9kb6JniBLxmVEd1G/KzKBQkwK0VRoSKJWda3bpMNZOEFHg4KxmrolhMZUlsEJvVbzpQ/PuA== 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)(4636009)(396003)(136003)(376002)(39860400002)(346002)(230922051799003)(64100799003)(186009)(82310400011)(451199024)(1800799012)(36840700001)(40470700004)(46966006)(40480700001)(40460700003)(83380400001)(47076005)(36756003)(356005)(81166007)(86362001)(36860700001)(44832011)(8936002)(8676002)(4326008)(5660300002)(426003)(336012)(26005)(16526019)(2616005)(1076003)(82740400003)(70206006)(316002)(54906003)(6916009)(70586007)(7416002)(7406005)(4744005)(2906002)(41300700001)(6666004)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:45:44.1211 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 100c4706-a2ee-4b33-17f6-08dc1e29a814 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: DS2PEPF00003439.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4915 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 142C51C001F X-Stat-Signature: kyh7wunqubokh7gqm96iuduq1roh7pmi X-HE-Tag: 1706244346-665791 X-HE-Meta: U2FsdGVkX1+4REnOuiztPMxcXuahOSBAOd2e2I/Ea9kUAl9GqGHM4drNkrHEQhNbgQqan8SNhvRu6f6amAKNFv5YW89YZHONPWxVQQAGAZB+i+oMSU0QVa31iMN98QFnxYBQlcbNyAwwqPPNUdvIlqZKYWyByL61xpQczwGT7ybJcd0uoV9cfGrtvOCAi4ajglV/0Yk2xrak9gybN+jTE31CQ4hXW2+okh5YwIpqtEfwpgqFicyFDC6Qql3nBN2cIdOBebX+1sjX9pze7sf1B5A/vR49qZjDzhgAVtIcPloAWpeYjVYq4QwL8TG12W3XEtwkr5lQw4dSI+hSBnskpcReF2cKkoVIaAMP1J6uVn66BGbH3AnXJbq2mmvcwXQvSUHG01rrEEyhGxnZfyzTPG1tHf1vq3nif+aAzmkjrsLGfl5uhEBEe990h5+t2Y9xmcYlKOJkzAK7jQwmGGcyI4GrakZ0I4d7G1D121o0rzpGWVPNM4iOVAfSkT6SN42snrEUhi1SnVHGlqTeIeJA6ezCj51l4/llyEd8+aGIzeGK5su1Fq/0JLVJb7yNCvLVJghwg9TUYsOx/8oraAHIFFLJ5P+u2M6BSCaToGePAbf6lCeaNFVFV172utfCaOctSuUsFzQ4CwKhcsCKCibnwcVlt4MQeH8o6sHqTFVtedJ7ld+YXEEVU83aJE1PCvk+aKHJsBsFjWgOnwvNVxKhQQk9/0apHWpe8zhUMR6s563ThcwweAeDulc4ots+r0yFrW9+yzn3IOmeUI1WlTxMzfGP8rx9TvmbC5Z19OAfZfWTacAhn4b+v/0Na2ug5+HvDKVcAJEcK24nj8VMXhyBLKD/tIiuQXPFSpoAcwuha6DKFMicxIS3/z0zBFkiCqYdi3qL9Q5n1niKihvkCT4WbBI9b/XeL379W7Bv5B9uZDYJqL8y/VwGZ24JXXADcHrJo9fAHnSMYyrSU7+5BqM UqJZMCol mHrxwYI71BdOGf/AHybaSYgAdX24XoPY1EYUQCld9K8KA9eAYMAt4r9rJAZhicos1sueciF50Oaw7fB5kv84aOaHuEIYjHgl9gKpyfAzcXB4tjXwJVjPWouoEzOTVnU79PEmSC6vLp3pf1xTbqj1wNdK6LVmJvKwu7EYfxq6tPBakDnJv8kWO3UYjZS3Ewn21tflvaW5eMnKWdSBlFQrczHFYYVL4mESFRyO2rYFNJkwyybH9c9rt0W7T+eG60kKma6s0rRUzlFNUpJDf03opw7yw8R1TlFmsWvhXJlG9MnKrFqHVRfbqYwKm2XCI8hzqShrIvuxpdXRHDsQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000027, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With all the required host changes in place, it should now be possible to initialize SNP-related MSR bits, set up RMP table enforcement, and initialize SNP support in firmware while maintaining legacy support for SEV/SEV-ES guests. Go ahead and enable the SNP feature now. Signed-off-by: Michael Roth --- arch/x86/include/asm/disabled-features.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 1ea64d4e7021..85a7b5ce96c9 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -117,7 +117,11 @@ #define DISABLE_IBT (1 << (X86_FEATURE_IBT & 31)) #endif +#ifdef CONFIG_KVM_AMD_SEV +#define DISABLE_SEV_SNP 0 +#else #define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif /* * Make sure to add features to the correct mask From patchwork Fri Jan 26 04:11:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532061 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C920DC47422 for ; Fri, 26 Jan 2024 04:46:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 595F36B0081; Thu, 25 Jan 2024 23:46:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 51DEF6B00B1; Thu, 25 Jan 2024 23:46:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 371176B00B2; Thu, 25 Jan 2024 23:46:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2230A6B0081 for ; Thu, 25 Jan 2024 23:46:10 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EFE49A0348 for ; Fri, 26 Jan 2024 04:46:09 +0000 (UTC) X-FDA: 81720225258.25.3CF5E99 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf06.hostedemail.com (Postfix) with ESMTP id 131D0180003 for ; Fri, 26 Jan 2024 04:46:05 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="P3us/8Oa"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244366; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=P24ey68QgkCCklEFwnPZMKuFWRudOfuqK8Rb9REQ1Dc=; b=xHO/rzPgNCGaF8LgTGp493Qvd8k2BRKcOPiNYF59zRom2NUpLRcKc+AlGebjLBisZxV7VT uf9LvgIitVsyADVc46H64K4cCuFpr8TXcLKYc7K4nErrPpo45hGE1sftpA3af1V8PpvQrH Q2fssTaBi2Xp/naBgnabHSriya74NDY= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="P3us/8Oa"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244366; a=rsa-sha256; cv=pass; b=oC6nU7XDVJPIIoiAn7sYhBjaoyibzE9M/Wu2U0ORhUh7ycVUBMJTGZqu/WIDiHorVdbu6l KK3JbsO0ClqnqN7qwqiLfy9gBT+U5CHnKXJuTDkEjyNgwMmvSbIsqkRqkyZ+qcdAew+87s qAOq4+nCWNR7HcPYeYTzihwj1rSrAac= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KSB9w4dLGJxZ7b4ItNCX7ZA2t/8x7/Xk9xCQk/azdaj5ghsNBqNDwITrZUvGSCguEiqfEizxE3taY76BnSAgnanj11EwlXVXRsvPzTRVv3V2vHLopkKIvR8MtkPNsykYj8EElxXgmtbWL0sQHAWv3KdV/iSQdPDps9ily8usYQSJQ8p3JYt5IOk6fiudmRJ/lD/HlKkbXTNthMmKzPSqeGwAfxMzBQe2xkzf3/RZvBx+7CZTpYyqi92GMTD85y/mp4DB6JFDxQgi5idlsHDZQa2GV32+NVbf8QpEVkdgPcUjnDlCBF5o0Jeu9mfQQZhDA+pqhIjBhAUu1DpDo/g7wg== 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=P24ey68QgkCCklEFwnPZMKuFWRudOfuqK8Rb9REQ1Dc=; b=Huy7oOmrkC79hDZeFQWEOyXXGJ07zdYp44xxqFdq36hZyQLiPe83S5mdQ9cy4RqXrHo83oHugMCAdLu9PB7JEVtBbUf9ao0w+zYS97ecTupgZkG/oDYp3qZLwxjJO9XUzeBjfFGCqtb0BHd52CV1VfqKKDs+/1tPTaliPddVS2UL9rWfPvEPrlB5nLDMc6wfwlWDiWoM2V/9sO5ZhvWuPAz4fDmUz6ZLI/xM7nMIwchyNid+EYojK+8j1ctnAap69LWmhejZccQE8JBgtmjahab7UciAzUgxDAfZNKTgSrAKr28O7LoNFI3nXVYNAuNSJNq63khMGmH8GUplofcGRQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=P24ey68QgkCCklEFwnPZMKuFWRudOfuqK8Rb9REQ1Dc=; b=P3us/8OakS9/enVf+ULHDxHhuWks7ZCBbZuZZhMtuRaGtkR5OBklfWb7ipcRu/sxo+Wgi/ts3mputIy67fBfpxa+hrF55SyzjGimd5jcx7yCzR3TqMhJDakjYw/DO2RhARyfCz7k6+5VrvH4kfKDIy07BCrKrm4Bb4VERaaPP8E= Received: from DM6PR03CA0031.namprd03.prod.outlook.com (2603:10b6:5:40::44) by DM4PR12MB5987.namprd12.prod.outlook.com (2603:10b6:8:6a::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.24; Fri, 26 Jan 2024 04:46:03 +0000 Received: from DS2PEPF0000343B.namprd02.prod.outlook.com (2603:10b6:5:40:cafe::fc) by DM6PR03CA0031.outlook.office365.com (2603:10b6:5:40::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.33 via Frontend Transport; Fri, 26 Jan 2024 04:46:03 +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 DS2PEPF0000343B.mail.protection.outlook.com (10.167.18.38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:46:03 +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.34; Thu, 25 Jan 2024 22:46:02 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v2 23/25] crypto: ccp: Add the SNP_PLATFORM_STATUS command Date: Thu, 25 Jan 2024 22:11:23 -0600 Message-ID: <20240126041126.1927228-24-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343B:EE_|DM4PR12MB5987:EE_ X-MS-Office365-Filtering-Correlation-Id: 2faef846-fc4b-4963-0a82-08dc1e29b35e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8P4HI7ieC8Fz2YbYt0FRTzbArB2pJb/HO7caRJZ+Vt5dYOqIAeBTap/ThKzFTa5Idr77OhCfWk1Eixza4rp3RQOL69RsPDrpALV8L6YH4vRNOYS5j5yOSglEP+cB7WNlRW40q37yqSFeyy8a91+BEbHozTST/CVFdLncAUGOR+MR9cxeJenxkVmit+qi+SJ5GEFBFF8KvUHH5yxvx9HFLSQtINJIVOYWO4f0HpyRATLnDaInWMT/Du8sS1G73XGRjJREABMdqNln/GHAkX7eFZn1D8tHSaXP/0Jd5v7AVYnzhLXy9zATRzircYlf3SQnBUF2IUIpSYIsoppaBWObSxQGx2KA+odrmmoErT+8wTa3f54pYWcAPEXUKFGV7X6i8ydeVg5PIMHOGMZDEkSkkdvdB4acAq1WlGZ/tD56VNQ8YeWRfJupUD25LANy3zAZmuhFVTZImbx1E6FlCIX43GmRqAd3bYMx7Vm4I49ggx3DaeS0PX8jdXIsslTBHfuhP0TX+5eSVB57iZVyv8lYiFWhfqH1GjqRTng/3pio3ZEagMbnkMDoo6wb2VvysBHyXz5eVsym+k4RH9kBbohJqd6ZONKiYZ02j8QK4Vki9gIhnhD4uCOiUNYiV//wFKsnWmgq8botFq+UoT9D7gyvkNVJxJO4M8UYALXrP6r9swgLE6+iorGF4fo1ZApn11HuoBpzgyOqaTo0Kn60Ou7P+rAGryagQhuSgUaNa0tCPIRLwh+Tr49rSJlBX7PTx3cty6CuShjyolsmPVrr6Yji2A== 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)(4636009)(346002)(376002)(39860400002)(136003)(396003)(230922051799003)(451199024)(64100799003)(82310400011)(1800799012)(186009)(40470700004)(36840700001)(46966006)(40460700003)(40480700001)(70206006)(26005)(70586007)(5660300002)(36860700001)(54906003)(7416002)(316002)(4326008)(16526019)(336012)(8676002)(8936002)(426003)(6666004)(7406005)(1076003)(2906002)(47076005)(6916009)(83380400001)(44832011)(81166007)(356005)(86362001)(478600001)(36756003)(41300700001)(2616005)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:46:03.0763 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2faef846-fc4b-4963-0a82-08dc1e29b35e 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: DS2PEPF0000343B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5987 X-Rspamd-Queue-Id: 131D0180003 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: xp4o5bn1dnf9cehof1n3y5n7k8gmb4ru X-HE-Tag: 1706244365-992828 X-HE-Meta: U2FsdGVkX1/hykjK0H9a22cliqe2MurOpCxosB3MOgVPiXDHzl05tA0rk7BEzwz/dD/E0Aw+o548kHkdSy0Afv76abR0EGP1FetEBtVGJBGyUjErhF9Ql3vHHmzZeV90ISSaVYnYfHJHF4t6otiJVCsq9aHat+jVNIct7uwg8GEzy/KCQzYsSWaWAYsmUI52XgRX1R3ppB1SLSHIF0jI5EEuasvpVwXc5vBdDr9Nl0z3OU1gNZn6BSr/4AX21tvFlZnliNZZT51xOufJRPOvNmkmuSxDCCzsi9MpMB/sfiVGv87bBVurIQ+8t2fiiNIIB6JUVubpGrJDkFFWVY8WvXTsWeHLC3MDGceWWRHrXmZWoB5ju9IzQ5Z3XieTabYZJVvZ6qbwurVhGRWqYiGbPnFU4nw5fPbrt07Yru/+gTBSkZVw/cog1rQM6aWPa0DLAgLekMpd7gqdlpnCSfkwxMyhwcEFhaLtNn4ivmzaddcPOpXydpRsSfX2Dz4Ouu+fjzTeWv90ILMChO+EMQUzHhf6/YTN8aSOuRckPwGGoZygWFATCGgRTG902zpR8k4SdjqeUtYT3x7HOvpGRXTEb65STihMhgRPbvI+46XEPHXkRL+obClzt9tJ9TeSzXnvNCW5NM4Qrw5vu4bKDFIQ0qgnwjCK7nsmmYJOFala5wi1COxMBdlZrJ+GGQ1sn3HsIesGQ53XOdnlMSMxV3jT9IknxpOBv3sYioygzOJs2BKeBfoM8StD4VVLGzpI2EjdBJCZLnoKgk1B+4Gvaq7y2MY/ZTJvhOt7mEhx2lchKyVtlN+fAMreIfvBB6bZU3pec/gBIh7iG5LQJgEdAJ56KMuDhHSXFSMZse+Tt7cTc/YexT0m7qaTK7ef9l3TgR5j/j+Y9TZdyc6Bjz3GNCDfJgmyKCk5wukZ/gRwRpx9dta0TDkDpe7KQ4RR0/sZkJtlT8zI8ImCXRxsgOamo5A oBxMLfJp D2NhjIbfB/ltm3rcu2IonNRdhdFXYdYkqiyOtLBqksZ8EGv/7+8Gducut1c00OtuvAKK+5/PpXoRIbKkx73KAQoD2AM5SdjP1il480CZ9a9PQg2dEvZn71xsJxWgXgBD06CDFhb7HbluSgBXUcvwN0SCSfRxiqAEuHyexz7gcNRccqpy8ILI0AqCMKp2MHFve2GI6PQsb7zPGwqGu4ofZwbgBB0cMvMoZFK6w3tQnq62jH7MWu/BaxTxgjMI2W7J1ITVVpQHxi4t0huxFD7YGxabLrLcc/OwY+CwcwmzVv7WKItsIhP1DdczPtlA3ERoTexOoncxMDJ950f+1qQmuNHBnjhi3KOMPTxIrMkliecOlFDMvwLc8kPe/ejQ2k03TpGqP X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh This command is used to query the SNP platform status. See the SEV-SNP spec for more details. Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 27 ++++++++++++++ drivers/crypto/ccp/sev-dev.c | 52 +++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 80 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index 68b0d2363af8..6d3d5d336e5f 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -67,6 +67,22 @@ counter (e.g. counter overflow), then -EIO will be returned. }; }; +The host ioctls are issued to a file descriptor of the /dev/sev device. +The ioctl accepts the command ID/input structure documented below. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* Firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------ @@ -124,6 +140,17 @@ be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. +2.4 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (out): struct sev_user_data_snp_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 9a395f0f9b10..9f6ee0d24781 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1919,6 +1919,55 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) return ret; } +static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_addr buf; + struct page *status_page; + void *data; + int ret; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + status_page = alloc_page(GFP_KERNEL_ACCOUNT); + if (!status_page) + return -ENOMEM; + + data = page_address(status_page); + + /* + * Firmware expects status page to be in firmware-owned state, otherwise + * it will report firmware error code INVALID_PAGE_STATE (0x1A). + */ + if (rmp_mark_pages_firmware(__pa(data), 1, true)) { + ret = -EFAULT; + goto cleanup; + } + + buf.address = __psp_pa(data); + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PLATFORM_STATUS, &buf, &argp->error); + + /* + * Status page will be transitioned to Reclaim state upon success, or + * left in Firmware state in failure. Use snp_reclaim_pages() to + * transition either case back to Hypervisor-owned state. + */ + if (snp_reclaim_pages(__pa(data), 1, true)) + return -EFAULT; + + if (ret) + goto cleanup; + + if (copy_to_user((void __user *)argp->data, data, + sizeof(struct sev_user_data_snp_status))) + ret = -EFAULT; + +cleanup: + __free_pages(status_page, 0); + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -1970,6 +2019,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SEV_GET_ID2: ret = sev_ioctl_do_get_id2(&input); break; + case SNP_PLATFORM_STATUS: + ret = sev_ioctl_do_snp_platform_status(&input); + break; default: ret = -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 207e34217528..f1e2c55a92b4 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -28,6 +28,7 @@ enum { SEV_PEK_CERT_IMPORT, SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, + SNP_PLATFORM_STATUS, SEV_MAX, }; From patchwork Fri Jan 26 04:11:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532062 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 908D9C47422 for ; Fri, 26 Jan 2024 04:46:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EE366B00A1; Thu, 25 Jan 2024 23:46:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 14E296B00A5; Thu, 25 Jan 2024 23:46:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE2736B00B3; Thu, 25 Jan 2024 23:46:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D91536B00A1 for ; Thu, 25 Jan 2024 23:46:31 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BB97F40468 for ; Fri, 26 Jan 2024 04:46:31 +0000 (UTC) X-FDA: 81720226182.26.5530EF4 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2061.outbound.protection.outlook.com [40.107.237.61]) by imf06.hostedemail.com (Postfix) with ESMTP id BCAEB180007 for ; Fri, 26 Jan 2024 04:46:28 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GytQ8apd; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.61 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244388; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UU3BHFRlkhMspS3Gn0J+D66WgUTsZEWbGuYOYJ2bfmM=; b=pSfA6MDpyEXUtA5uf5+Dkk5m3XRrMrxSosEGbWeCSBIvLO976BucAwobvWmaN0rMuYaZ5r iregQKZXf2EfVhNP5RA8I0ELxRJOzH9eMbEMinoi43z+TYGwE7sE8vMbjhC2R/lg64IkiK ccCdPV+3VN8BvdvI2wZgb3AWTT1F5Zk= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244388; a=rsa-sha256; cv=pass; b=aOnm2Kkrz3jRQivAqKMlL62ueRDIDLZweeSrImRw1eLNEq71A20TG6q1Esimq1n9JAgoL8 LMv0w9CcSV8BRWkuvnKptNEiERem2YV1o+3VMgVrWXikcqR0WdPzGzwiJQgupic9/S0UP7 h+i85BXHfgXUkNXXQ+n9GnISlTDbuE8= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GytQ8apd; spf=pass (imf06.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.237.61 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kxfMNnaM7qw8N1rfbjsJ5zhXRMD57BGXeNmaMHAdFAhgQ1VSGMZ+VZsJ5D7NBGAu+O6z9fSzGAon6VkbYQeEsP8AHjsmeXn9e7nsAkGP7htkVE8C/UK8O4bUDNe3K/8Z/vWSRcRLMuylaj0asRmKM622ktOlR/7oUHnUEJpGz/lhEHi8/Nndrixj2QLCbSgqa/e/JzuHM3qxZL0EiZeJhdFLOYVENfatRSJZPwoPo0i4z5X4JQDwpRvFBhSleb3CxMDeGN3Gvmjy3Fv3UBhu6mc9tKnIyo9mCGxhbcVlVjPyi3Cecc5o7/cqDph5P/pzKzDWOvkVGykrm02EP+sFjA== 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=UU3BHFRlkhMspS3Gn0J+D66WgUTsZEWbGuYOYJ2bfmM=; b=kPlzX1veup/YkDDfqKcNObGXy+sXg6K8Whx7jF4jitMhKo6yUT0+sjjaA4ufwO23+ilTZddyGtTXqZo7yK0vLA0tJsLhagUL25WXMYeldZrd7x9lNAZzNQDyeA79xt5w2vsAF1mhzgXdrvXCvCsV92TtX/lbViAtoi/W3l5JQqQD+LJrcI/CjBAq707Dq5gNumkPt5nsGfEYiQCL/3tnh9ntKrkqkNFfOd3axZlV2PinTWeSRcAIILsymtQ1fTf9nH/IWc+wbz3vaE8ReevfRIqkRCPAXn3xRfwp4lcRBCzreeTz3TKGrRixnEUtCbhqZjsjOEtPj2gxkpjnf0shBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=UU3BHFRlkhMspS3Gn0J+D66WgUTsZEWbGuYOYJ2bfmM=; b=GytQ8apduQsxhzzccJeUkUh25wh9JHkyGp46O8LjRZID+yMbuYVx6VDg8u6wyKpHNbkUxn5hilYR9veCbVWC0ANH/Vfb2SDvjA1lcx9BQm3KCFpXunZZauSSO7OLSi1PqLaNzJzLyaYULYhs+rzllqd7LuIs3nit/31DnmErFz0= Received: from DM6PR02CA0166.namprd02.prod.outlook.com (2603:10b6:5:332::33) by MW4PR12MB7467.namprd12.prod.outlook.com (2603:10b6:303:212::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:46:24 +0000 Received: from DS2PEPF0000343E.namprd02.prod.outlook.com (2603:10b6:5:332:cafe::fd) by DM6PR02CA0166.outlook.office365.com (2603:10b6:5:332::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 04:46:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS2PEPF0000343E.mail.protection.outlook.com (10.167.18.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:46: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.34; Thu, 25 Jan 2024 22:46:22 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 24/25] crypto: ccp: Add the SNP_COMMIT command Date: Thu, 25 Jan 2024 22:11:24 -0600 Message-ID: <20240126041126.1927228-25-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343E:EE_|MW4PR12MB7467:EE_ X-MS-Office365-Filtering-Correlation-Id: 7864d932-96d5-41e2-b8bd-08dc1e29bf8f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VKQb21dAilhTE1lfdCFY/CEwt2l2RaTvjiYFvU+Hj0/IPAEgG/9axsuY3xxe8uvvJ5j/b8sRw1T7O7qcyAupl30W4iGL2QNf7iPoVSKqnz72rznp1TPjT/a3g8rGppnpsi6MejcwPq/tsLy10RrRjaCla9m1AVNb2zbb9aNOftUWefRlLUMwo9nzD3M9P+OjWlnKcgSqJA5ulk8N7Dkko0Vs0xTsmZbxgg0LU9FsZKSR1jThC91TpYRqPzea2kY1TZo69aEM4f+YSYZtEt1DEYaZ4soAqhW8d39PL4/aIBrpx3rzx5JOzxsbidmeRsOuQYoHSa8b00IqKRyDncRs79vkMGimF2mcTvZ5DGr1DTfPsIiMS1W890LKtPxLNrNiHZECO8XnxsMP2AbswZFXXvv/kx0iujKJbKg2Jby1rX9+ODZ5ELNoRKACNqPsN/f3WZPBw7pb0NMoFpkxm5T2lSp6SSHICfkYpAG+4+GTDOTmoZWJ+fhUHb8MzgNE5vYuHM2odGFw26AkM38nt94HDa/O+aGmuHfolpzFhxbgBilVzfAobolLnt/EEx+ad38/uXt7op0BmS9+CtsPXhwBnJXtrXVpGk+BnQPQOL3x90NG6F8CMk864j6Nx0bhwep8JQRi4lwXiSC9b1qEOQFu4JgpTKCyHU01780l+UviB2qYTFgpUkKd7oJjg2dkiROhjQx7+5EgNH3rAS3ZhYOXAVhNxIWAl9rFXstE9rMAHHv66BL0EsbhWY2wRkP29gMcMkQZHUQJN3RhDHhQ9b/J2g== 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)(4636009)(136003)(396003)(346002)(376002)(39860400002)(230922051799003)(451199024)(186009)(82310400011)(64100799003)(1800799012)(46966006)(36840700001)(40470700004)(47076005)(83380400001)(1076003)(2616005)(16526019)(426003)(336012)(26005)(82740400003)(36860700001)(4326008)(5660300002)(8676002)(8936002)(44832011)(41300700001)(2906002)(7406005)(7416002)(478600001)(54906003)(6916009)(316002)(70206006)(70586007)(36756003)(81166007)(356005)(86362001)(40480700001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:46:23.5346 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7864d932-96d5-41e2-b8bd-08dc1e29bf8f 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: DS2PEPF0000343E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7467 X-Rspamd-Queue-Id: BCAEB180007 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: hb7eusnsu7kfypk1nzxafu5gxmgicfkz X-HE-Tag: 1706244388-559271 X-HE-Meta: U2FsdGVkX18jldgoyHz2Lzkvggiks7gweoi8RQ1FR9n+2IMERbYjKu4TDorV+I2e4aBO+SKCo4FhEX352cdypffhbnjvqJqfhal2FmmZucSwvCVdEyPJttjuipB1FQWw5K5MP0cgw6BdWkNDFF161k29muo+9mTVo56knH+mWvjOpQEA9OQOC/N1T2hlQ/M779C9zLkEu1FUUcPiHlMNTF1qMh8ZliwOKbikQZ1NU77WHA3+/3F+zIIny333ZMZGZisz6WJl1b174UJsnPfpFl9/QoopfiSfXgsx/C/Rqqplmdk2WHGuFFTCSgLX8QXEMUcCVmTmVF4PSkF71rhoIPbYCGtOljXDBYTGBnAWSCSMiORiqiFhPP15igzQy4sO2diEYP7h0cDEEOA/keNL0LW8Uet10M+AtxD8q6NSmwiTuNkmXR2U4tbKXHKN04T9tr3YI2N9cRkH8ilB5WSMEOQSG7weUNY1Cse6nsXyNKi7TyQeVKR02MXEfAA5beSAdM6x61AwWyziKqIH6n3TfVJBzlVs/dty1de+kBsQEKe+nmduYhP9OAU3GpEwPjlUxmFkGAK8eBXFAjS87+rqvIvES03/D04nqF7HCwcQyPpnYRnL+b8zihpdSNlZih6O7NkOMP3MUgp5JNSa+6rUV95kyZRWr1Y2XhmZF47vpT2cOXVurCERcNuwE84LUc52zV+j6fswZwiYIzLXzvx47tasK0NVIFoZGCYDCdwFwgxkpzJEMVk7rdIyYyhGQd82378AGyYf9sB2bhXWOkh2Q+blv670P9UDu/qeynUCKt6zLpcoE8QuequDUsf74DvM3rq70D1kbx7RNgxKdogYUNojZmpArmSu/XrM1vy0wsRMpMb5T6BanrRvcq3qNSFLcrMGHrAnLqiPbsuGfKHy3oixM6K/lyWNkycewbkXgTpcIm8KA73dGPCaOork0DDZ+tM8yCsIRJoDr389ARf 4vREQH0k SDQF9CRnkR/+2sdPpCInZFd8YRA7CtWJZBLAmL0kJ0dwIONO5ibhkJ4M2M0z/iMLu3qr0PPQu54naVwkocc9Ms1LVF5W+E7DjhqfmKcbRbhfuGK4GWv0cEIe2mgrNy14lCgbTuaQP5ZTSUZXp/wEi8dzLvCRYdwI39abJk9kgVIDQSqRPKW3XPmPUNokwkKG6VCjBB6ITIrzurULuJDA45+CN/WaCJRHIWnMvufM4hAHg+GjehEGHxcaL32m+SL5flnbiXYrC2hau/YoBd+kMOYqJXSNw5h9fvqrsj8qba7KM642+XyPVKb+LCoULJIFF1oP2ix7xSoL/ZbX4Ka93govUldqwU7enAEUFhFD11cLEcg2B3+mwU0wu8QLZPGDelrwD X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Tom Lendacky The SNP_COMMIT command is used to commit the currently installed version of the SEV firmware. Once committed, the firmware cannot be replaced with a previous firmware version (cannot be rolled back). This command will also update the reported TCB to match that of the currently installed firmware. Signed-off-by: Tom Lendacky [mdr: note the reported TCB update in the documentation/commit] Signed-off-by: Michael Roth --- Documentation/virt/coco/sev-guest.rst | 11 +++++++++++ drivers/crypto/ccp/sev-dev.c | 17 +++++++++++++++++ include/linux/psp-sev.h | 9 +++++++++ include/uapi/linux/psp-sev.h | 1 + 4 files changed, 38 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index 6d3d5d336e5f..007ae828aa2a 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -151,6 +151,17 @@ The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. +2.5 SNP_COMMIT +-------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Returns (out): 0 on success, -negative on error + +SNP_COMMIT is used to commit the currently installed firmware using the +SEV-SNP firmware SNP_COMMIT command. This prevents roll-back to a previously +committed firmware version. This will also update the reported TCB to match +that of the currently installed firmware. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 9f6ee0d24781..73ace4064e5a 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -222,6 +222,7 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_addr); case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); + case SEV_CMD_SNP_COMMIT: return sizeof(struct sev_data_snp_commit); default: return 0; } @@ -1968,6 +1969,19 @@ static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp) return ret; } +static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_data_snp_commit buf; + + if (!sev->snp_initialized) + return -EINVAL; + + buf.len = sizeof(buf); + + return __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error); +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -2022,6 +2036,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SNP_PLATFORM_STATUS: ret = sev_ioctl_do_snp_platform_status(&input); break; + case SNP_COMMIT: + ret = sev_ioctl_do_snp_commit(&input); + break; default: ret = -EINVAL; goto out; diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 7f9bc1979018..beba10d6b39c 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -788,6 +788,15 @@ struct sev_data_snp_shutdown_ex { u32 rsvd1:31; } __packed; +/** + * struct sev_data_snp_commit - SNP_COMMIT structure + * + * @len: length of the command buffer read by the PSP + */ +struct sev_data_snp_commit { + u32 len; +} __packed; + #ifdef CONFIG_CRYPTO_DEV_SP_PSP /** diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index f1e2c55a92b4..35c207664e95 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -29,6 +29,7 @@ enum { SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, SNP_PLATFORM_STATUS, + SNP_COMMIT, SEV_MAX, }; From patchwork Fri Jan 26 04:11:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13532063 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3A91C47DDB for ; Fri, 26 Jan 2024 04:46:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 680796B0074; Thu, 25 Jan 2024 23:46:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6087E6B0087; Thu, 25 Jan 2024 23:46:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 482566B00B5; Thu, 25 Jan 2024 23:46:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2C03D6B0074 for ; Thu, 25 Jan 2024 23:46:53 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 077921204E9 for ; Fri, 26 Jan 2024 04:46:53 +0000 (UTC) X-FDA: 81720227106.24.B9CC8B4 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2086.outbound.protection.outlook.com [40.107.93.86]) by imf19.hostedemail.com (Postfix) with ESMTP id 169F01A0007 for ; Fri, 26 Jan 2024 04:46:49 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eWPMk9It; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.86 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1706244410; a=rsa-sha256; cv=pass; b=v2AIf8rdyOYbwelCg/5YLtnnpIbNRxhxt83GUtqysg1GuPzsxsoZAL0Zz4ROuNPfm2NUEn wTCsKhVCH6Ji7FmLqzKZqkAQSZJ/PxkoQBp79EXjwg0q1VROSNC+S5ZPKA9P1iG595iZt+ FXRUKWww9G8mSla/c3WFzAdXP1x5d6Q= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=eWPMk9It; spf=pass (imf19.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.93.86 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706244410; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aDeqv0Rd+NQEOVYZuoLg7AnInMZUEU9WR2h7GI/0rZw=; b=GP7oWXB/IsNFtR831EdAZzamL81m7ei9MLM8L9rS6iLZYrjNlxFRWHPAZL6KW16EhUFHgD yGAQrdnw4AoUdIR8Gq9aHS8Q1VYM3Dy9odzQbLOx3Plz97R0y/c6Koj4zf3vvQcNeEds0J OZ22oB1kNGDwKp1X5eSQ0XrgcTuiMxs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C7TaOS+T6xv4IAh7SPObQbVcd9elXaUW7IQFoDXlEe+v9TBQARTepxRKpbsc+pndrcp3vvZEZtiT/9EykVbto/E8wamWzFmgZ/585snUMIXQ6iFgiBP0QUNT6B/8PcqpT0MU02fpuD5NI5PzqHKaFprqph8u5miqoMoa8GQBGjnMXT7dKRyN8dzSXX3tCP82eE+SPWtenB5IDY0Mjq5kDfG1htL3xLEVpUaqhTGxFtDoVF5ezrqmUOfcuMS+YXMdUxIyOcT8ywyngEzVt0QeNfXR3uDJcfDqR/aTukSk3XIRvEsPHMIr+VgsOWZmMPcgEzKRXYm0vNa2wExtm5SCLA== 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=aDeqv0Rd+NQEOVYZuoLg7AnInMZUEU9WR2h7GI/0rZw=; b=eLkiNoctUIsw/hJPkI2fmS6mYF+G2UFx/O9oG4FBZOSeyAj2Re8pem/U1kn0bL9aHmGHiYTaQuI64b4G+JczHoC5RQcgMT/RItsPyTi3SOot2kBmb+7QHFJlUoytR9Lif3am10Cbeb+2IPlgsTqfyHjvGMUSsL/s2BNg8laxsbZw5Vql8BqMFjt1P3EnhaVxqeqyjT517516URug6g0WL+C0swA5AeAfq1OXuczDiClB8cfZjD3DvP4ehj+ZaWp7N4IF0AfPOBdLS+vkZ+3kT8/+wBh5hpGPqQ4+6r/qtst9BdSJOQWvro8lQ3Nq87VQnmnmQ3DdvpmOjUhSwRbUxw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=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=aDeqv0Rd+NQEOVYZuoLg7AnInMZUEU9WR2h7GI/0rZw=; b=eWPMk9ItwDqFSwJb3jUb6+LgwVjQw3GJBqSzw7C37mQzf5cFfhT0BEOmVJTKxLFgRQQa3VF+ylzbSbThlrqUkTFZrdir1tIuyCB6Aim9/npxvF494ip/+yWsdHjFz3XiGzjej5V3/q87J2QvsTAO+rOXGM0YQ7h0jtVQfSsYQdg= Received: from CH5PR02CA0017.namprd02.prod.outlook.com (2603:10b6:610:1ed::19) by BY5PR12MB4869.namprd12.prod.outlook.com (2603:10b6:a03:1d9::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 04:46:44 +0000 Received: from DS2PEPF00003440.namprd02.prod.outlook.com (2603:10b6:610:1ed:cafe::7a) by CH5PR02CA0017.outlook.office365.com (2603:10b6:610:1ed::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 04:46: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 DS2PEPF00003440.mail.protection.outlook.com (10.167.18.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 04:46: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.34; Thu, 25 Jan 2024 22:46:43 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh , Alexey Kardashevskiy , Dionna Glaze Subject: [PATCH v2 25/25] crypto: ccp: Add the SNP_SET_CONFIG command Date: Thu, 25 Jan 2024 22:11:25 -0600 Message-ID: <20240126041126.1927228-26-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240126041126.1927228-1-michael.roth@amd.com> References: <20240126041126.1927228-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003440:EE_|BY5PR12MB4869:EE_ X-MS-Office365-Filtering-Correlation-Id: 6f7e2305-fbb1-427c-3cff-08dc1e29cbe7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aqDsBcSb6NdQLynCq/UCIeCeSzRx6k0iglwucgxYu3q5U+ZjM/2IC9biXlr2W4l2hoxIn1+6GoNzOud2aritAeAmA9A/ZIp8Nf1oI7LKwlss3YvIkqls/+suAnc6nry4/9nc6NteXOcfMJRLFBVN9t/IlNPHZueu8UAOUdA2TpOy1Lll3ibu5rx3UdTZozYRrqJbr9t83Q6GSyRGNEIGdmx/M3qJB+TzNvE/Ff732vMkkdGb5WkwTK3gt10bIfKdiID7OpBSNFLWxqjw8lNrfdHo7aVuPsTCgc45BhBIAYrEIvSNpJyUxKv9yVjeoiJ6faOovaw0tjzO/B2XwAhyHXDY5KtxxnfKfhOlXp40NVcYI7SI0v6Q0nVuLOFtKWlxBQBJpWwPRQDg5f+KHLxoTmxDCmgam49qCzwuRWRJMK40guZgpmQ89aFPHGaETiCG6F9i1aQchJAi6tzOy05WfqznMha/a1XHVB5aahYF5Hitw9qj4vxAyA3Vzkm5vRqZ5EW6wVpQYJJaGIJBrWuqqdOCnmyTgwCUYqlbM3fA5TsJVshoj+1O9aOVrxt2OGhs72HcZP7j6/OmutAlUWQI7Q1o79vWghBOjpxbXB2EbxmgOt6/thTATIA93R4pUzmWNWr5iZv40h+vMDWWf0rzAbLofTAmLCs+4AGq1xNknEwxdJWI/X0yaWA8lPThLg6IvUaByyOWq2b644x69SO0QmXSwadGKO9t2nEy9kiHX8BqrMUE15lkerf5wGmYFbfoxsnFt0iH1Exs0wiUqUfg+A== 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)(4636009)(376002)(136003)(39860400002)(396003)(346002)(230922051799003)(1800799012)(82310400011)(64100799003)(186009)(451199024)(46966006)(36840700001)(40470700004)(82740400003)(81166007)(2906002)(36860700001)(5660300002)(44832011)(7406005)(36756003)(7416002)(356005)(86362001)(426003)(4326008)(2616005)(336012)(26005)(16526019)(478600001)(1076003)(6666004)(41300700001)(47076005)(8676002)(8936002)(70586007)(54906003)(83380400001)(70206006)(316002)(6916009)(40480700001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 04:46:44.2252 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6f7e2305-fbb1-427c-3cff-08dc1e29cbe7 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: DS2PEPF00003440.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4869 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 169F01A0007 X-Stat-Signature: phdkdw4kh44r83si9ucfqd1yfgmnhr9i X-Rspam-User: X-HE-Tag: 1706244409-500858 X-HE-Meta: U2FsdGVkX1+chmgthqFkT+pTingRIZlE38Kj6xo5BMqWZtIVuRpE5iXPz9NMMGgKqwQExElN70CbugQCWP+jNgKvMp00yiGWTvLQr/4u1lw1+7hY2JpMNjl4P6wpCCbnWruoKQd/rdfMTxe3cyQRhx28AQnPoIZHK7fvCIZPcxUfVkEvjv6aAOrNtyYRu0yz/brOM1xpWXv85BzWbHGw23f3XQGzq5gOKh5FPCILBWjTc/y/PdKfK4dp1FrbejZc4dPe6+mFyRkMOImy0HNDoki8c/eJBL4OeZ3iUuv37GigRZz/0K+kA+qp1Pw9SI5oKiySOJbTRtRtOJfezupgCfMxnF0PonNVWhjl/+uokQtH1kyZnJnm/M1XPxfMjRCpV+e4sXKKqzXxO8pSsj2A9v07UKog0/1eRGtJEBH4UqYwCigU9MWtF+S9CA5RioTi2JhqMV6BdTUhQFj/DLHbfdgiMTd75bpeX0rkor2dw83rzsgycxFZ3h53HxO772E2kOcTHwVkQZGK1b4KWmpbe39Ds+BgUrLZxSTxdApZi9PtPV1Vg+9sZxoIiQAGnfg8vEnnCk7iAcwZgZavC/5dcurFZgQtTLBvN2qAdvYW2NPHSj0+fvMaFf7BT2bvwmQnWQ0degC016Cste7t6R0S4wmC1LP0risFmJOIVH1i91i3ZbawK0DbkNkD7e+jUU6TofZOAZ4nZrN0iJrQwrKQ1IFvVOK3ULn6i/w4TKRFwfbwLvP6+iXjlgiSB1DCstLMXToUjDjkTCCyz7+O/CPmgsxAdT6MxjK4QbSeS/zkg1eTcRN2Hnp/OUJ+kJmmOLGQqAxxiMGpdRjRGMgIHRpjr0fVOfxQpPl5OkC6OPsXLvUiIcM8Wb+c9xSkrgOPcQbm8PcMMV2Qq98pJroANflGj3b1lNQRRShLxz77Gj1r/Af/ND8HOBSgJIeva6qNHJ1dxN7IR6zdOqVvi0Mj711 znjlSpW6 RhPav6j9rMTI6F1rNfwk3fzhSNQd6duqOhuMPJH80B4TfNQUltPiD8xhAQkvW8dRtwtwfddICZqWe60mn815t44QOkgYobQEr6PgBy/UC2/YLuVo6CMwwyNYcTLY/2RcgD3vH5yeMIsOwxnbR6X2vxledgIgs08RyfWKTAYGWSKoIsCjCELI2ce3W8fZL4msW3rn9pCbBoAiqya5xRuJ2tWCVRkUt9S5l6GqYMRCUIPPSD0MRXxg/TRVGAd0hop7e/Nk2T2uKtFkYF0se7bxeT2UTkcsZxFNKP+yVyl7Y30SCU7uHVJjK/r24yKq7Kgq45q5qKGUeGdSv9JzTbN2/6Q0L+8AzYWxR0CdCGSnJaILNHs6lUcF1mZttT01xNgMGDwHd42LmJXm3m1+8+jFFEhzbcg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Brijesh Singh The SEV-SNP firmware provides the SNP_CONFIG command used to set various system-wide configuration values for SNP guests, such as the reported TCB version used when signing guest attestation reports. Add an interface to set this via userspace. Signed-off-by: Brijesh Singh Co-developed-by: Alexey Kardashevskiy Signed-off-by: Alexey Kardashevskiy Co-developed-by: Dionna Glaze Signed-off-by: Dionna Glaze Signed-off-by: Ashish Kalra [mdr: squash in doc patch from Dionna, drop extended request/certificate handling and simplify this to a simple wrapper around SNP_CONFIG fw cmd] Signed-off-by: Michael Roth Reviewed-by: Liam Merwick --- Documentation/virt/coco/sev-guest.rst | 13 +++++++++++++ drivers/crypto/ccp/sev-dev.c | 20 ++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 34 insertions(+) diff --git a/Documentation/virt/coco/sev-guest.rst b/Documentation/virt/coco/sev-guest.rst index 007ae828aa2a..14c9de997b7d 100644 --- a/Documentation/virt/coco/sev-guest.rst +++ b/Documentation/virt/coco/sev-guest.rst @@ -162,6 +162,19 @@ SEV-SNP firmware SNP_COMMIT command. This prevents roll-back to a previously committed firmware version. This will also update the reported TCB to match that of the currently installed firmware. +2.6 SNP_SET_CONFIG +------------------ +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_user_data_snp_config +:Returns (out): 0 on success, -negative on error + +SNP_SET_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar +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. + 3. SEV-SNP CPUID Enforcement ============================ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 73ace4064e5a..398ae932aa0b 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1982,6 +1982,23 @@ static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp) return __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error); } +static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable) +{ + struct sev_device *sev = psp_master->sev_data; + struct sev_user_data_snp_config config; + + if (!sev->snp_initialized || !argp->data) + return -EINVAL; + + if (!writable) + return -EPERM; + + if (copy_from_user(&config, (void __user *)argp->data, sizeof(config))) + return -EFAULT; + + return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error); +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -2039,6 +2056,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SNP_COMMIT: ret = sev_ioctl_do_snp_commit(&input); break; + case SNP_SET_CONFIG: + ret = sev_ioctl_do_snp_set_config(&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 35c207664e95..b7a2c2ee35b7 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -30,6 +30,7 @@ enum { SEV_GET_ID2, SNP_PLATFORM_STATUS, SNP_COMMIT, + SNP_SET_CONFIG, SEV_MAX, };