From nobody Fri Dec 19 00:01:08 2025 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2058.outbound.protection.outlook.com [40.107.236.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D30A3176184 for ; Thu, 30 May 2024 23:37:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.58 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717112233; cv=fail; b=uO67ojM1VkriZGn3Asf/Le/Ia0Kns1fiXwRtw4E3epJQkPyz2klKmSMEmYiC+ZZrng4GbSe9UA0Wp1UCeQRbG69qjFbk3frjmlsB9mPyKUiLDKWP/wVK3yKURuyaLqjjP9DJ4z9tKCFGVAD/5sFx/RRyfZi8mBeOYmVkN0fShRU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717112233; c=relaxed/simple; bh=Z3VIjVK6Bw01toB3mGxu2a0JjdK+11wEvWbLs9xsW0w=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hD4v+1pVNH9+c0YZy+PE4ZvejGcyQ6OcBDGxKkiMFL0OfM0T3U96/OiDysiFPekWFwav03ixfL3r72v5igH4D4s81pweiFrBz/o/JtoPdyvRz7retjdaRxf7qRcezOKQn2Ib/S4tTKrd/kiSRD6En7XoNYy3qIRHsIvd7y1jHHA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=NUfQs9t5; arc=fail smtp.client-ip=40.107.236.58 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="NUfQs9t5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C8gA6gurBdlQGikON/X4vmg0HoVLNLGvoJDKn7oVF7/uodN4UJpiLf0osQ8sTaZSWu029l+igvbxICK25l2Ar91z8wfqjd6WVfGAZmOr5aAMGml0y2s245HzOjOM/JmoyiYyytta6vEZuJ8e0ppQSacKlhVPJlhDt1brzL8tHgK8dfWJjKl8QgFA+B8dJ4zA8v/YfOYW7l1+uK7CPAhFw4R5kDbC/hvuAOvXC65j8FBQMRqoa1aMa2Ck1Lq/iQ8Pcc0eEh+hapEn+uvGwW8qOXvnTd9HasbLo2T8dLfNCp/5Ahy5kK7Xpo5qiQoHjF0O8CPBjL/4XHgRw0khvmaZaw== 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=ieEefwgUhncIg313683Ilof1zAWY1rVRNtzilGjtXiY=; b=jNCsaxJELRZOoELjKo1ZntuevQc5BnxDTaMaFILgqcfUZVsogf3RKvFQPRqvwd5zu390IjcZmHwPFkfS6gabutLpIhYqX+ow0e3QCR8m9kI6OmVU9pBcYkQPsKVkwPo4fWlKFpIhTrTVqYoSE+lP2obFxl68l4Tpd1ig8S74Noxd2ZeSvLWhFKzuMVSkHjYtM31BVycHYoh+OmF7wsTBadFl8WS9r+CyqeymsI2nLR/kdPNSV8zRUsceD19YVQDb0V3BpmTirqGQoblrdAwQ+/SPV4aHrjkNRDJYs7YxfN4Y7yDFeG7Rbm0qR6DeytkhOPjwgwNaF+tH19P3hW12qg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linutronix.de 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=ieEefwgUhncIg313683Ilof1zAWY1rVRNtzilGjtXiY=; b=NUfQs9t5CJd7y+HmoVZ7/5ftH+O8zXriUuYsh/+B6dyOmu21VpfFeKzRb73oUU+9+my8r4h0kRoGwUFAtmDiyfgdWuGqhaUfh33YtiMrXm41bxWk5rKJiRNkY00Rn2h73c58MmzaBlxnpp/DDr0G1ng0rkGX4ZqZhw5Ji6e4Oco= Received: from BL0PR0102CA0016.prod.exchangelabs.com (2603:10b6:207:18::29) by CH3PR12MB8582.namprd12.prod.outlook.com (2603:10b6:610:163::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.36; Thu, 30 May 2024 23:37:09 +0000 Received: from BL02EPF00021F69.namprd02.prod.outlook.com (2603:10b6:207:18:cafe::7a) by BL0PR0102CA0016.outlook.office365.com (2603:10b6:207:18::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.19 via Frontend Transport; Thu, 30 May 2024 23:37: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 BL02EPF00021F69.mail.protection.outlook.com (10.167.249.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7633.15 via Frontend Transport; Thu, 30 May 2024 23:37:08 +0000 Received: from ethanolx7e2ehost.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 30 May 2024 18:37:07 -0500 From: Ashish Kalra To: , , , , CC: , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v7 1/3] efi/x86: Fix EFI memory map corruption with kexec Date: Thu, 30 May 2024 23:36:55 +0000 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: BL02EPF00021F69:EE_|CH3PR12MB8582:EE_ X-MS-Office365-Filtering-Correlation-Id: d3a5d2d1-ebb4-4492-a9fa-08dc81016c13 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|82310400017|7416005|376005|36860700004; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?8VP1BIqNrD/5kpbS3/An4a3Zg274n08fY1zsJKCyJVFI96Jol7uF9KtI/0Uy?= =?us-ascii?Q?MiYTseMfMT6OFXjt61znD9AHBK9Ntt+GR0WkOyqA6ElDrBQLmpR+bLkjyJQL?= =?us-ascii?Q?lE5F4zmamvhiojPzPc/UsA8dnWjVTxtoqr55+ZmBoIqszboBrkh8iY1kydYT?= =?us-ascii?Q?D8tuTTpQyopp8v1E7xILDPn5Ykb9MEBwkVHgdd7V6f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omAqocAo1Ots5/TvlWwIivlCYa8jiMdCtH8sclZu8cSv?= =?us-ascii?Q?cbXhqXAb7UeVefbruS/Enwn75RYrc2vNDFZrHBbVgN+QeSgRL1Evg9clwlxR?= =?us-ascii?Q?oDVs0ehBTh+wfNXoKLsLi9MgKVgoZQFOjjq0Jlj4X1WuLT3mC570KzOKXNOk?= =?us-ascii?Q?P6oJ93s7AbvhSas1jckRt8gH8K0AZ9+uJehJnB1o2ziifvaP339CP5ZVmQrs?= =?us-ascii?Q?YeBRCuxpEdclCEAdXx1jQI9Wl+T3IV9eov7B1dSSjmVfPdj0Z4j1Jr/DW0Z5?= =?us-ascii?Q?2aNHJfXlhTN5nTcR7avscaiknQEQ?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(1800799015)(82310400017)(7416005)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2024 23:37:08.7477 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3a5d2d1-ebb4-4492-a9fa-08dc81016c13 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: BL02EPF00021F69.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8582 Content-Type: text/plain; charset="utf-8" From: Ashish Kalra With SNP guest kexec observe the following efi memmap corruption : [ 0.000000] efi: EFI v2.7 by EDK II [ 0.000000] efi: SMBIOS=3D0x7e33f000 SMBIOS 3.0=3D0x7e33d000 ACPI=3D0x7e= 57e000 ACPI 2.0=3D0x7e57e014 MEMATTR=3D0x7cc3c018 Unaccepted=3D0x7c09e018 [ 0.000000] efi: [Firmware Bug]: Invalid EFI memory map entries: [ 0.000000] efi: mem03: [type=3D269370880|attr=3D0x0e42100e42180e41] ran= ge=3D[0x0486200e41038c18-0x200e898a0eee713ac17] (invalid) [ 0.000000] efi: mem04: [type=3D12336|attr=3D0x0e410686300e4105] range= =3D[0x100e420000000176-0x8c290f26248d200e175] (invalid) [ 0.000000] efi: mem06: [type=3D1124304408|attr=3D0x000030b400000028] ra= nge=3D[0x0e51300e45280e77-0xb44ed2142f460c1e76] (invalid) [ 0.000000] efi: mem08: [type=3D68|attr=3D0x300e540583280e41] range=3D[0= x0000011affff3cd8-0x486200e54b38c0bcd7] (invalid) [ 0.000000] efi: mem10: [type=3D1107529240|attr=3D0x0e42280e41300e41] ra= nge=3D[0x300e41058c280e42-0x38010ae54c5c328ee41] (invalid) [ 0.000000] efi: mem11: [type=3D189335566|attr=3D0x048d200e42038e18] ran= ge=3D[0x0000318c00000048-0xe42029228ce4200047] (invalid) [ 0.000000] efi: mem12: [type=3D239142534|attr=3D0x0000002400000b4b] ran= ge=3D[0x0e41380e0a7d700e-0x80f26238f22bfe500d] (invalid) [ 0.000000] efi: mem14: [type=3D239207055|attr=3D0x0e41300e43380e0a] ran= ge=3D[0x8c280e42048d200e-0xc70b028f2f27cc0a00d] (invalid) [ 0.000000] efi: mem15: [type=3D239210510|attr=3D0x00080e660b47080e] ran= ge=3D[0x0000324c0000001c-0xa78028634ce490001b] (invalid) [ 0.000000] efi: mem16: [type=3D4294848528|attr=3D0x0000329400000014] ra= nge=3D[0x0e410286100e4100-0x80f252036a218f20ff] (invalid) [ 0.000000] efi: mem19: [type=3D2250772033|attr=3D0x42180e42200e4328] ra= nge=3D[0x41280e0ab9020683-0xe0e538c28b39e62682] (invalid) [ 0.000000] efi: mem20: [type=3D16| | | | | | | | | | |WB| = |WC| ] range=3D[0x00000008ffff4438-0xffff44340090333c437] (invalid) [ 0.000000] efi: mem22: [Reserved |attr=3D0x000000c1ffff4420] range= =3D[0xffff442400003398-0x1033a04240003f397] (invalid) [ 0.000000] efi: mem23: [type=3D1141080856|attr=3D0x080e41100e43180e] ra= nge=3D[0x280e66300e4b280e-0x440dc5ee7141f4c080d] (invalid) [ 0.000000] efi: mem25: [Reserved |attr=3D0x0000000affff44a0] range= =3D[0xffff44a400003428-0x1034304a400013427] (invalid) [ 0.000000] efi: mem28: [type=3D16| | | | | | | | | | |WB| = |WC| ] range=3D[0x0000000affff4488-0xffff448400b034bc487] (invalid) [ 0.000000] efi: mem30: [Reserved |attr=3D0x0000000affff4470] range= =3D[0xffff447400003518-0x10352047400013517] (invalid) [ 0.000000] efi: mem33: [type=3D16| | | | | | | | | | |WB| = |WC| ] range=3D[0x0000000affff4458-0xffff445400b035ac457] (invalid) [ 0.000000] efi: mem35: [type=3D269372416|attr=3D0x0e42100e42180e41] ran= ge=3D[0x0486200e44038c18-0x200e8b8a0eee823ac17] (invalid) [ 0.000000] efi: mem37: [type=3D2351435330|attr=3D0x0e42100e42180e42] ra= nge=3D[0x470783380e410686-0x2002b2a041c2141e685] (invalid) [ 0.000000] efi: mem38: [type=3D1093668417|attr=3D0x100e420000000270] ra= nge=3D[0x42100e42180e4220-0xfff366a4e421b78c21f] (invalid) [ 0.000000] efi: mem39: [type=3D76357646|attr=3D0x180e42200e42280e] rang= e=3D[0x0e410686300e4105-0x4130f251a0710ae5104] (invalid) [ 0.000000] efi: mem40: [type=3D940444268|attr=3D0x0e42200e42280e41] ran= ge=3D[0x180e42200e42280e-0x300fc71c300b4f2480d] (invalid) [ 0.000000] efi: mem41: [MMIO |attr=3D0x8c280e42048d200e] range= =3D[0xffff479400003728-0x42138e0c87820292727] (invalid) [ 0.000000] efi: mem42: [type=3D1191674680|attr=3D0x0000004c0000000b] ra= nge=3D[0x300e41380e0a0246-0x470b0f26238f22b8245] (invalid) [ 0.000000] efi: mem43: [type=3D2010|attr=3D0x0301f00e4d078338] range=3D= [0x45038e180e42028f-0xe4556bf118f282528e] (invalid) [ 0.000000] efi: mem44: [type=3D1109921345|attr=3D0x300e44000000006c] ra= nge=3D[0x44080e42100e4218-0xfff39254e42138ac217] (invalid) ... This EFI memap corruption is happening with efi_arch_mem_reserve() invocati= on in case of kexec boot. ( efi_arch_mem_reserve() is invoked with the following call-stack: ) [ 0.310010] efi_arch_mem_reserve+0xb1/0x220 [ 0.311382] efi_mem_reserve+0x36/0x60 [ 0.311973] efi_bgrt_init+0x17d/0x1a0 [ 0.313265] acpi_parse_bgrt+0x12/0x20 [ 0.313858] acpi_table_parse+0x77/0xd0 [ 0.314463] acpi_boot_init+0x362/0x630 [ 0.315069] setup_arch+0xa88/0xf80 [ 0.315629] start_kernel+0x68/0xa90 [ 0.316194] x86_64_start_reservations+0x1c/0x30 [ 0.316921] x86_64_start_kernel+0xbf/0x110 [ 0.317582] common_startup_64+0x13e/0x141 efi_arch_mem_reserve() calls efi_memmap_alloc() to allocate memory for EFI memory map and due to early allocation it uses memblock allocation. Later during boot, efi_enter_virtual_mode() calls kexec_enter_virtual_mode() in case of a kexec-ed kernel boot. This function kexec_enter_virtual_mode() installs the new EFI memory map by calling efi_memmap_init_late() which remaps the efi_memmap physically alloc= ated in efi_arch_mem_reserve(), but this remapping is still using memblock alloc= ation. Subsequently, when memblock is freed later in boot flow, this remapped efi_memmap will have random corruption (similar to a use-after-free scenari= o). The corrupted EFI memory map is then passed to the next kexec-ed kernel which causes a panic when trying to use the corrupted EFI memory map. Fix this EFI memory map corruption by skipping efi_arch_mem_reserve() for k= exec. Additionally, efi_mem_reserve() is used to reserve boot service memory eg. bgrt, but it is not necessary for kexec boot, as there are no boot services in kexec reboot at all after the first kernel ExitBootService= s(). The UEFI memmap passed to kexec kernel includes not only the runtime service memory map but also the boot service memory ranges which were reserved by the first kernel with efi_mem_reserve, and those boot service memory ranges have already been marked "EFI_MEMORY_RUNTIME" attribute. This is the additional reason why efi_mem_reserve can be skipped for kexec booting and by checking the set EFI_MEMORY_RUNTIME attribute. Suggested-by: Dave Young [Dave Young: checking the md attribute instead of checking the efi_setup] Signed-off-by: Ashish Kalra --- arch/x86/platform/efi/quirks.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index f0cc00032751..6f398c59278a 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -255,15 +255,39 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u6= 4 size) struct efi_memory_map_data data =3D { 0 }; struct efi_mem_range mr; efi_memory_desc_t md; - int num_entries; + int num_entries, ret; void *new; =20 - if (efi_mem_desc_lookup(addr, &md) || - md.type !=3D EFI_BOOT_SERVICES_DATA) { + /* + * efi_mem_reserve() is used to reserve boot service memory, eg. bgrt, + * but it is not neccasery for kexec, as there are no boot services in + * kexec reboot at all after the first kernel's ExitBootServices(). + * + * Additionally kexec_enter_virtual_mode() during late init will remap + * the efi_memmap physical pages allocated here via memblock & then + * subsequently cause random EFI memmap corruption once memblock is freed. + * + * Therefore, skip efi_mem_reserve for kexec booting by checking the + * EFI_MEMORY_RUNTIME attribute which indicates boot service memory + * ranges reserved by the first kernel using efi_mem_reserve and marked + * with EFI_MEMORY_RUNTIME attribute. + */ + + ret =3D efi_mem_desc_lookup(addr, &md); + if (ret) { pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr); return; } =20 + if (md.type !=3D EFI_BOOT_SERVICES_DATA) { + pr_err("Skip reserving non EFI Boot Service Data memory for %pa\n", &add= r); + return; + } + + /* Kexec copied the efi memmap from the first kernel, thus skip the case = */ + if (md.attribute & EFI_MEMORY_RUNTIME) + return; + if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) { pr_err("Region spans EFI memory descriptors, %pa\n", &addr); return; --=20 2.34.1 From nobody Fri Dec 19 00:01:08 2025 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2084.outbound.protection.outlook.com [40.107.92.84]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91B7117C7B0 for ; Thu, 30 May 2024 23:37:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717112254; cv=fail; b=IVHG+UOL5ZWMfmCf4s3jjZNIQSQnx1cTyxgnAe+tZcRqgjY48pQY0sMhj07K4rA5XEuhdDRzTAYFnOML2Q+TK2rOeZgUZijdkzhUU+uLmausp/5NFo2HSPEa4r/htUmw6xgzsMYG9fagOWmd7a1Cy1yRF+XJcmyyFxmeQcQr0W0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717112254; c=relaxed/simple; bh=oeN3YWhguWxjQNeclQa+IfSOz+Df0i08/eFpNzUjq9o=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sQcVKDyDCT/3Zc+nuAfignRLJBYKjTdCUPX4MNsAPs3pkAB1Q7+uLoa8JmurUEX1P752nh0Fkd/DFah5nT1UOSjDcknVY+haiWVOwF1Glfh6ieJ/WJoqvt4H9nfN1C4g2/SlSl0ZxNua8JvnyYsxK0811lE7GWgF3WsefvOu4mQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=J66/BjmU; arc=fail smtp.client-ip=40.107.92.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="J66/BjmU" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CXInuiGoP6VMpfyO2PVAWX5DGUfwU9/HBTMAbKtkDIQYgAxIUE09uFfxRo793EC3zPCafP4vtyYh0iq878mKwPDaex+98s14UVmVMsM3hw7TgOBOQmfmkt42X5XHfFsHk4jis5iQkqLwVDzvKvz+NBPbBjGX3/CyIWOKeLfHipk0/hRHWG/3wGZ7hjKtE546UV5x/J57o/us+vmSzBtU82TMWmdQ63T4Pb9qf51OVqY+LNbz9yqUnPWNpvpv7noA4AJVSEA/MEF4OYRkTDDEEKZtFyogM/8VW93MHSimGCzZpR4bHlEwrokgziJtkfnkc4Ip4CcOmRnG0bhB5pf6+A== 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=8In1wj4zpxqXng+/MvpIStRWhMMfirb2tsJpUKGbnWE=; b=nkcF3OmxsvsV6Ks+jVLP5n/LsYEgTvxgabQj+DNofBKHVGN8W0yr5sqKur/S4QXQ4TaWpq/8cdocGt9FSxOekFK+v3glQSQv18K8b+RieApkyYp1xCTPT4EXQQ5t0L5OeaPQgWZorTQQiAmxsanQ8ofS72TWYy23Cx8qAQTCdDyfog1ypeZtIW7CPxQksGs4C/hl8Rt+w2SZg2C2gG53Uh1QyuLhawQvSllnpy3Z9t5e6DiL5so7meBqipHHoa2k9ow4YZFnXihuibRIFYkEmMGghlGTuDiIIVCeIdW/7BJtijSpuT2E3UHqfzcWqpUIsJbMf0FOUBlKHlRIDK+jEA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linutronix.de 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=8In1wj4zpxqXng+/MvpIStRWhMMfirb2tsJpUKGbnWE=; b=J66/BjmUJ6tSH+6Tr4Iyob7Kf1gMHsh9rMEYnxSQfwa+SQo1zXnoiw5x3b5+gEaGPgxIfxbH/ueLzuDgL5vvTgsgeEEoFGhexSgaFyQE4Gao8N/X0VZxrAPVPu35XAmiQoHL5Sv0zdn4HeZ8gDe7g2ESieMmNbmIhp3Bf6ezh5U= Received: from BL0PR0102CA0005.prod.exchangelabs.com (2603:10b6:207:18::18) by LV3PR12MB9332.namprd12.prod.outlook.com (2603:10b6:408:20f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.38; Thu, 30 May 2024 23:37:27 +0000 Received: from BL02EPF00021F69.namprd02.prod.outlook.com (2603:10b6:207:18:cafe::65) by BL0PR0102CA0005.outlook.office365.com (2603:10b6:207:18::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7611.30 via Frontend Transport; Thu, 30 May 2024 23:37:27 +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 BL02EPF00021F69.mail.protection.outlook.com (10.167.249.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7633.15 via Frontend Transport; Thu, 30 May 2024 23:37:27 +0000 Received: from ethanolx7e2ehost.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 30 May 2024 18:37:26 -0500 From: Ashish Kalra To: , , , , CC: , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v7 2/3] x86/boot/compressed: Skip Video Memory access in Decompressor for SEV-ES/SNP. Date: Thu, 30 May 2024 23:37:14 +0000 Message-ID: <75a313a44bfaf3f97027393665d625be2f881e66.1717111180.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: BL02EPF00021F69:EE_|LV3PR12MB9332:EE_ X-MS-Office365-Filtering-Correlation-Id: 4a7a83fc-105f-447c-6044-08dc81017770 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|7416005|36860700004|376005|1800799015|82310400017; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?AUdZ1Xezcu9v+OEx3/vAii+JZUNuDZie1z3cy5dEAGSwQl1Pndm3hS3N+feo?= =?us-ascii?Q?3mMe3NUshV14nAILOVKLHCD8J/BCzohWiNPqy/rwznSflBPhXJnbigB59ACM?= =?us-ascii?Q?j7r0mydfiQawQnLzdRM7aL0HTQXF1OqqVtl26CukDR05noHi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fSX5/l/OcHmhV+AOx358du3E+S6A2u5O97cT+0wFSAw9Uw5wakDgh?= =?us-ascii?Q?Bsebh+NEbLlxVGlAlSy2azLnynZnsS1KuiqkNP46kYTvdrq6PrMR7B4TuQZu?= =?us-ascii?Q?DE/wDmFYesgKA5IHj8K/UWJDXvFw/5737iA9MUfzkayHH005iZQ11jNDedgt?= =?us-ascii?Q?2aEfJBs2V/8k7Qqt9oTKVcWUGHYoNDsQsKwJY4i2nYjbGaAEER1luD6NvIx5?= =?us-ascii?Q?vV6W23prP4PW8PtsqqMtGrl1YzWSOENlSAVoZk5qrQd2mLJvQvIr3MYDabry?= =?us-ascii?Q?NWAemVnH6uYRALavghh4h4Ht8OShF1f1QuwfEFBAu3UMy+DoNlgKdObki08r?= =?us-ascii?Q?eGqlBZwOfC+7IZP7IuKwEd+SNTGX?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(7416005)(36860700004)(376005)(1800799015)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2024 23:37:27.8102 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4a7a83fc-105f-447c-6044-08dc81017770 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: BL02EPF00021F69.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR12MB9332 Content-Type: text/plain; charset="utf-8" From: Ashish Kalra Accessing guest video memory/RAM during kernel decompressor causes guest termination as boot stage2 #VC handler for SEV-ES/SNP systems does not support MMIO handling. This issue is observed with SEV-ES/SNP guest kexec as kexec -c adds screen_info to the boot parameters passed to the kexec kernel, which causes console output to be dumped to both video and serial. As the decompressor output gets cleared really fast, it is preferable to get the console output only on serial, hence, skip accessing video RAM during decompressor stage to prevent guest termination. Serial console output during decompressor stage works as boot stage2 #VC handler already supports handling port I/O. Suggested-by: Thomas Lendacy Signed-off-by: Ashish Kalra Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/boot/compressed/misc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/mis= c.c index b70e4a21c15f..3b9f96b3dbcc 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -427,8 +427,10 @@ asmlinkage __visible void *extract_kernel(void *rmode,= unsigned char *output) vidport =3D 0x3d4; } =20 - lines =3D boot_params_ptr->screen_info.orig_video_lines; - cols =3D boot_params_ptr->screen_info.orig_video_cols; + if (!(sev_status & MSR_AMD64_SEV_ES_ENABLED)) { + lines =3D boot_params_ptr->screen_info.orig_video_lines; + cols =3D boot_params_ptr->screen_info.orig_video_cols; + } =20 init_default_io_ops(); =20 --=20 2.34.1 From nobody Fri Dec 19 00:01:08 2025 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2064.outbound.protection.outlook.com [40.107.92.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C7671761AF for ; Thu, 30 May 2024 23:37:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717112277; cv=fail; b=UjeIHoxtrLDP3mA7jRyNvTj4IFjHX2ktmULESbYzpEBRp9hmpqDD1yoA6SVU5ipZdnpn5QoLHBKH79UgeDivm0diP2Y5S9EVJ80kl2mw0EjYzw3pKH6ZxjQipshy7tXp+6Rnci6Cpi8IvfA0h4kEB2HTH43izElqmQaJBtSggMc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717112277; c=relaxed/simple; bh=Glq3U4+VhK6UxVG78jJZ620JMHAa7DubHY4fpq7ULtI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fiF5mEP/NpbvONgRZR6uDpkBQh4Qfhh/4PbH1WG2VF5NR4wedCDLZAQlG/NbTXwSYH6Otgx07/9tljzdx74kzRg6kAV54//bqra+GZa9g7TdWqg15GhBdSXPMCBu6IApj86sgrdeBsdb8oO913NK0KaHMZ2JO73j1e2F2+u4i6Y= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=SA6vO5Sv; arc=fail smtp.client-ip=40.107.92.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="SA6vO5Sv" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gJ+OhhZprwa7Mq0gDfq7ntgMYUBoEnyD5aBarRqz+++KiVUun9FEknTgpw4C/1L7vLQF58gUQLF0u7a49GRWtd/hJnX0pSmLVkPOK7AzsD6Dpzm/VCTQS7lP45S1ZFOqxAN0nla3A1hxj6U8rDU6yPR8C67IXQmXopp4xZLfFGfF25Xz/MrEq924123f+MLwzjgkvOKs8tIhmXiorj3v6v+LPaGPsSphbSzPme7p/O0pmTfqcE2r/wrWeYRin5xO3stVp65zYfkqjOAZL1gVhtopdG7c3ucPi6jthxMs8wtppzCxYRS/Nxsi1k+zuS5EuIcJAtv3SQQir1ggAlyO4w== 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=su8iHGvdi2wP63JHxc+soe1j+7vmL6Et7j0y2xaV5xc=; b=a6FikYjfkw3AegPX6QQ2N23hXTgZDMOwmMjEwAgNvFMkoegfCJ1yE7ngR7Kxr71R5LZYQM8WSWIt4VtARR6uPJQfaqoHFsXm0VTHXd02FU+ItwxTFFx50CezZ3/CQVjaF5XGKQP1AVdBu33wgPrX5xkXMITlEqvrGdUH0/HuCtgjca09GSITh8jWtH4nUrQoHgWbMYhmiztSPwwubd7Ge65vx9p9nZcaia3fTUDGnFH+M7AOV4v7ifBqc8mPeVLD26FUTur4Y2AvUEIVs/VE7w95HHPnX9CC/gXeTAvSnYsRPvtfTWynzYMGW2vZxhhB6hcJz/HSwXjiq687609T0g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linutronix.de 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=su8iHGvdi2wP63JHxc+soe1j+7vmL6Et7j0y2xaV5xc=; b=SA6vO5SvOxeZd8SAi3yrR7v4z0iGTDIVnh8z0VhJo65ZLWlMOlQczujpQ9JPmHQJJTM+b75ip7V1I34T7dkynzHR+T6EmjI4e0mWdAAfu9D0ML8GEf8VkbjGO7qCFPM7JJ3rc+9hCUbUBthqnLf+o73zjyrlBX+8lvH+xIyOsO8= Received: from DS0PR17CA0013.namprd17.prod.outlook.com (2603:10b6:8:191::11) by IA1PR12MB6185.namprd12.prod.outlook.com (2603:10b6:208:3e7::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Thu, 30 May 2024 23:37:52 +0000 Received: from CY4PEPF0000EE3E.namprd03.prod.outlook.com (2603:10b6:8:191:cafe::16) by DS0PR17CA0013.outlook.office365.com (2603:10b6:8:191::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.36 via Frontend Transport; Thu, 30 May 2024 23:37:52 +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 CY4PEPF0000EE3E.mail.protection.outlook.com (10.167.242.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7633.15 via Frontend Transport; Thu, 30 May 2024 23:37:51 +0000 Received: from ethanolx7e2ehost.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 30 May 2024 18:37:46 -0500 From: Ashish Kalra To: , , , , CC: , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v7 3/3] x86/snp: Convert shared memory back to private on kexec Date: Thu, 30 May 2024 23:37:34 +0000 Message-ID: <14385051d096edc153dc2b7016c75296ebfde7ed.1717111180.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: CY4PEPF0000EE3E:EE_|IA1PR12MB6185:EE_ X-MS-Office365-Filtering-Correlation-Id: 24dd8334-0fa2-4d8c-cdc1-08dc810185ad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|82310400017|36860700004|376005|1800799015|7416005; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?gQ5/G1aA2Dv9Ipml2hFeeQQAQ62aCecEwK5a8oABQYVt1mwGbX/3mPk1l/q4?= =?us-ascii?Q?xo7dWSrcDX4WKzcml8LzWKeg9h/OAwtM0V9knkFBgcQgzfPu3fW2+JHPYXxy?= =?us-ascii?Q?1oELs3GrnKQI2fcW6/dniBd7TqHACqByAuos2fb/wrlYodZP+EcDJx2UA6m0?= =?us-ascii?Q?vdJTC1Aw9RNEvtdZKJUObBHQ47cqpxvIllpOol6ZDnLKirrIy0FMc3bkQlrV?= =?us-ascii?Q?SsSPFMCWGO9vjVjNXX8vfMRf+6t8RaebF/iJuAWEVECjWNKuwrSsoPXLukYc?= =?us-ascii?Q?gfPZfrDbQYfuxvGCS5Kfy9JCxMZcJhdWflVlOArjfAuriCDcQwyUbU11mQqM?= =?us-ascii?Q?+oUsTmgPVvKjILg3YeqTcqrBWPzJsRaW3lw4LheQgg76vNU+Xq32mTqvMH0R?= =?us-ascii?Q?rqXVc6XqZLsfppruisOQdok8poOC3AU3luP6B9Z2M5Q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isytymJ39iK6POAHPRb8ptkyBG5pfR+n9kYpK+LCKf+r9WIhcFb?= =?us-ascii?Q?a9AnfQM=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(82310400017)(36860700004)(376005)(1800799015)(7416005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2024 23:37:51.6384 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 24dd8334-0fa2-4d8c-cdc1-08dc810185ad 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: CY4PEPF0000EE3E.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6185 Content-Type: text/plain; charset="utf-8" From: Ashish Kalra SNP guests allocate shared buffers to perform I/O. It is done by allocating pages normally from the buddy allocator and converting them to shared with set_memory_decrypted(). The second kernel has no idea what memory is converted this way. It only sees E820_TYPE_RAM. Accessing shared memory via private mapping will cause unrecoverable RMP page-faults. On kexec walk direct mapping and convert all shared memory back to private. It makes all RAM private again and second kernel may use it normally. Additionally for SNP guests convert all bss decrypted section pages back to private. The conversion occurs in two steps: stopping new conversions and unsharing all memory. In the case of normal kexec, the stopping of conversions takes place while scheduling is still functioning. This allows for waiting until any ongoing conversions are finished. The second step is carried out when all CPUs except one are inactive and interrupts are disabled. This prevents any conflicts with code that may access shared memory. Signed-off-by: Ashish Kalra --- arch/x86/include/asm/sev.h | 4 + arch/x86/kernel/sev.c | 162 ++++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_amd.c | 3 + 3 files changed, 169 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ca20cc4e5826..f9b0a4eb1980 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -229,6 +229,8 @@ void snp_accept_memory(phys_addr_t start, phys_addr_t e= nd); u64 snp_get_unsupported_features(u64 status); u64 sev_get_status(void); void sev_show_status(void); +void snp_kexec_finish(void); +void snp_kexec_begin(bool crash); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -258,6 +260,8 @@ 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 sev_show_status(void) { } +static inline void snp_kexec_finish(void) { } +static inline void snp_kexec_begin(bool crash) { } #endif =20 #ifdef CONFIG_KVM_AMD_SEV diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 3342ed58e168..941f3996a9b6 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -42,6 +42,8 @@ #include #include #include +#include +#include =20 #define DR7_RESET_VALUE 0x400 =20 @@ -92,6 +94,9 @@ static struct ghcb *boot_ghcb __section(".data"); /* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features __ro_after_init; =20 +/* Last address to be switched to private during kexec */ +static unsigned long kexec_last_addr_to_make_private; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -913,6 +918,163 @@ void snp_accept_memory(phys_addr_t start, phys_addr_t= end) set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); } =20 +static bool set_pte_enc(pte_t *kpte, int level, void *va) +{ + pte_t new_pte; + + if (pte_none(*kpte)) + return false; + + /* + * Change the physical page attribute from C=3D0 to C=3D1. Flush the + * caches to ensure that data gets accessed with the correct C-bit. + */ + if (pte_present(*kpte)) + clflush_cache_range(va, page_level_size(level)); + + new_pte =3D __pte(cc_mkenc(pte_val(*kpte))); + set_pte_atomic(kpte, new_pte); + + return true; +} + +static bool make_pte_private(pte_t *pte, unsigned long addr, int pages, in= t level) +{ + struct sev_es_runtime_data *data; + struct ghcb *ghcb; + + data =3D this_cpu_read(runtime_data); + ghcb =3D &data->ghcb_page; + + /* Check for GHCB for being part of a PMD range. */ + if ((unsigned long)ghcb >=3D addr && + (unsigned long)ghcb <=3D (addr + (pages * PAGE_SIZE))) { + /* + * Ensure that the current cpu's GHCB is made private + * at the end of unshared loop so that we continue to use the + * optimized GHCB protocol and not force the switch to + * MSR protocol till the very end. + */ + pr_debug("setting boot_ghcb to NULL for this cpu ghcb\n"); + kexec_last_addr_to_make_private =3D addr; + return true; + } + + if (!set_pte_enc(pte, level, (void *)addr)) + return false; + + snp_set_memory_private(addr, pages); + + return true; +} + +static void unshare_all_memory(void) +{ + unsigned long addr, end; + + /* + * Walk direct mapping and convert all shared memory back to private, + */ + + addr =3D PAGE_OFFSET; + end =3D PAGE_OFFSET + get_max_mapped(); + + while (addr < end) { + unsigned long size; + unsigned int level; + pte_t *pte; + + pte =3D lookup_address(addr, &level); + size =3D page_level_size(level); + + /* + * pte_none() check is required to skip physical memory holes in direct = mapped. + */ + if (pte && pte_decrypted(*pte) && !pte_none(*pte)) { + int pages =3D size / PAGE_SIZE; + + if (!make_pte_private(pte, addr, pages, level)) { + pr_err("Failed to unshare range %#lx-%#lx\n", + addr, addr + size); + } + + } + + addr +=3D size; + } + __flush_tlb_all(); + +} + +static void unshare_all_bss_decrypted_memory(void) +{ + unsigned long vaddr, vaddr_end; + unsigned int level; + unsigned int npages; + pte_t *pte; + + vaddr =3D (unsigned long)__start_bss_decrypted; + vaddr_end =3D (unsigned long)__start_bss_decrypted_unused; + npages =3D (vaddr_end - vaddr) >> PAGE_SHIFT; + for (; vaddr < vaddr_end; vaddr +=3D PAGE_SIZE) { + pte =3D lookup_address(vaddr, &level); + if (!pte || !pte_decrypted(*pte) || pte_none(*pte)) + continue; + + set_pte_enc(pte, level, (void *)vaddr); + } + vaddr =3D (unsigned long)__start_bss_decrypted; + snp_set_memory_private(vaddr, npages); +} + +/* Stop new private<->shared conversions */ +void snp_kexec_begin(bool crash) +{ + /* + * Crash kernel reaches here with interrupts disabled: can't wait for + * conversions to finish. + * + * If race happened, just report and proceed. + */ + bool wait_for_lock =3D !crash; + + if (!set_memory_enc_stop_conversion(wait_for_lock)) + pr_warn("Failed to stop shared<->private conversions\n"); +} + +/* Walk direct mapping and convert all shared memory back to private */ +void snp_kexec_finish(void) +{ + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return; + + unshare_all_memory(); + + unshare_all_bss_decrypted_memory(); + + if (kexec_last_addr_to_make_private) { + unsigned long size; + unsigned int level; + pte_t *pte; + + /* + * Switch to using the MSR protocol to change this cpu's + * GHCB to private. + * All the per-cpu GHCBs have been switched back to private, + * so can't do any more GHCB calls to the hypervisor beyond + * this point till the kexec kernel starts running. + */ + boot_ghcb =3D NULL; + sev_cfg.ghcbs_initialized =3D false; + + pr_debug("boot ghcb 0x%lx\n", kexec_last_addr_to_make_private); + pte =3D lookup_address(kexec_last_addr_to_make_private, &level); + size =3D page_level_size(level); + set_pte_enc(pte, level, (void *)kexec_last_addr_to_make_private); + snp_set_memory_private(kexec_last_addr_to_make_private, (size / PAGE_SIZ= E)); + } +} + static int snp_set_vmsa(void *va, bool vmsa) { u64 attrs; diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index e7b67519ddb5..3ba792cd28ef 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -468,6 +468,9 @@ void __init sme_early_init(void) x86_platform.guest.enc_tlb_flush_required =3D amd_enc_tlb_flush_requir= ed; x86_platform.guest.enc_cache_flush_required =3D amd_enc_cache_flush_requ= ired; =20 + x86_platform.guest.enc_kexec_begin =3D snp_kexec_begin; + x86_platform.guest.enc_kexec_finish =3D snp_kexec_finish; + /* * AMD-SEV-ES intercepts the RDMSR to read the X2APIC ID in the * parallel bringup low level code. That raises #VC which cannot be --=20 2.34.1