From patchwork Thu Nov 24 09:12:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zelin Deng X-Patchwork-Id: 25394 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3282104wrr; Thu, 24 Nov 2022 01:19:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf5g6QJOH4aQDL0wMHI2fInTqy8W3Eeh8atUU2KvMzbfuIX8BmcQvO0RA6AB6gtD+G/u950H X-Received: by 2002:a05:6402:4507:b0:467:205b:723d with SMTP id ez7-20020a056402450700b00467205b723dmr9361177edb.69.1669281555920; Thu, 24 Nov 2022 01:19:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669281555; cv=none; d=google.com; s=arc-20160816; b=nuuw17amaPLmgr+/ew2a+pR1ui9hGRjiR/0IbkWplXv+xWBI8BX4+oqIQCRFedAZ3h hG9t2ZEXB5vJrGaKWTJ1URQqND3EGSW6jmEQdKvcgyKkXNmmgdwXmHwyckzRp4IQBloO jux19cS+gRc/MbMk2KFFrH9PMHEs4Rh2XNUj7YyU3b7w4GQhwgl3Ely9M+6utdAqVne6 e92IzOsE5AbB8pJbZoa5ys+W09s3sIIGKgXR2obO3Gm4z6T+l2TzuP3V75ZcPlAG/LTq tDMFs4eMKMoIWDyDvJ1Lu7NsmMiwxvVmRkwW3oEVOs57Ep+JPvWMd4jLlXkPcI63Ju/U KcdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=4BpTzz3AtH9NXcfx+6PJqvadOfKbReQbnT0hUtRmvxQ=; b=ngFpeIeowzyTS6XAzESmrQmm1esBbN0cT1RVS4zH/M1m6Wvy9mmWFRfW4Zky2EHQ8j 6WqUsgm637i3pLFixAOK204HI7RFp0m+ticwPljLPvugqJMGTzILvK/a4+u+0P0qcTd5 I1Fqagayg+387avEpVcG0I69PpYBd0Q3VNVVLjwauFItrMMiWXYaRHB/KxUDLm6ZnsyJ 38u2QN6l84JvbVrdOvtYa2XFsfDKD8N80KNrSC9S6cf75G9ME5SwwVMVYqfc2+E5B6Z4 3MdB/YixKW+edqD+R5oz8uiSfqZUxw2P0J2iw6RMN7H3H2v5EsR+hVnODdMtAZZeZ8X3 PxuQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id he44-20020a1709073dac00b00741688254f4si500691ejc.163.2022.11.24.01.18.52; Thu, 24 Nov 2022 01:19:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229874AbiKXJNR (ORCPT + 99 others); Thu, 24 Nov 2022 04:13:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbiKXJNN (ORCPT ); Thu, 24 Nov 2022 04:13:13 -0500 Received: from out30-43.freemail.mail.aliyun.com (out30-43.freemail.mail.aliyun.com [115.124.30.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D65E5B7E95 for ; Thu, 24 Nov 2022 01:13:09 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=zelin.deng@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VVaTOzC_1669281186; Received: from alinux-host.hz.ali.com(mailfrom:zelin.deng@linux.alibaba.com fp:SMTPD_---0VVaTOzC_1669281186) by smtp.aliyun-inc.com; Thu, 24 Nov 2022 17:13:07 +0800 From: Zelin Deng To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Tom Lendacky , Thomas Gleixner , Borislav Petkov , Zelin Deng Subject: [PATCH 1/2] mm/early_ioremap.c: Always build early_memremap_prot() in x86 Date: Thu, 24 Nov 2022 17:12:45 +0800 Message-Id: <20221124091246.4957-2-zelin.deng@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221124091246.4957-1-zelin.deng@linux.alibaba.com> References: <20221124091246.4957-1-zelin.deng@linux.alibaba.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750368576581188897?= X-GMAIL-MSGID: =?utf-8?q?1750368576581188897?= In some scenarios, we'd want to specify protection attributes when we are doing early memory map. As early_memremap_prot() is also defined in loongarch it's better to select ARCH_USE_MEMREMAP_PROT on X86 config to avoid redefined error. Signed-off-by: Zelin Deng --- arch/x86/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 48d98a5fe06b..133d7d660b2e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -97,6 +97,7 @@ config X86 select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_SYSCALL_WRAPPER select ARCH_HAS_UBSAN_SANITIZE_ALL + select ARCH_USE_MEMREMAP_PROT select ARCH_HAS_DEBUG_WX select ARCH_HAS_ZONE_DMA_SET if EXPERT select ARCH_HAVE_NMI_SAFE_CMPXCHG From patchwork Thu Nov 24 09:12:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zelin Deng X-Patchwork-Id: 25395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3282288wrr; Thu, 24 Nov 2022 01:19:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf7AhMn8/rABSvSNL3neGlsvQr9jyiTarZKp5a2Hq0w3IAhKdW2/gZgWtGpelQFXwXi8+NQJ X-Received: by 2002:a17:902:6943:b0:188:ab16:2393 with SMTP id k3-20020a170902694300b00188ab162393mr15856454plt.87.1669281590171; Thu, 24 Nov 2022 01:19:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669281590; cv=none; d=google.com; s=arc-20160816; b=ovIInGYhkQdsT1PDxnOw+4CzSeGDcaK6bBN9EbXnye/iqlyU3+rYiiybzQT79aYMMH 1x8AFnkSvikz4hedVs4szEHZUeav77mXFNpiXfNOu++piaYjgP8d58NlD0mUaE7M0W6O VWj/PGkkk1aHmML//2L9tRzEtL9hk5aPFeLyGck0yc0wCMRySaG3i4IG39RlvVPosmtT YEOALYyMzrqYEGC9XFdlQxLHVlPFqIVGoxBxyrNz4g5Cx2MES5Bd2iuqMBbFk1xPi87O 24xR0hcVP9kHDuGLPgsR/i9dqNl4sqjE/MRkPBmJ6ZGa/Ayz6mr4eSC7YP8d2MP7y0/d XQKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=BARpFNBA3EKFLY40puLAbRxnSC2wfnc9DjWNAEVWkNA=; b=WAyHg32WG9QSeWxO5UBDdM8MKwY5uJfiMbx9qtFMpcR0DTsk551MZkH8B9ucyRY1JN NrUXKMFOBEFsDlkN0RTvSCrv+83i625p0LMBpZYf+6PaF+8NpxHROH9onp1el0GZEQtZ jlLH5om3gRtIHbRbrOh5cGzF/LZT9lTi5zOQOZbh7fhusJ29sykHcmuOXD8d57BzoQr2 eDemaR1c5Jz9T76c4zZ5sdvM+puVk4/5IvFCMsPNWIJPpE8Fi2gPGk20kpG8lyP11ugL xrftvdlVuPeoXcg5cRI0EnXjFZt01ViT0xqPgw57iPToTeid1lTGUHTuPtgnBkMgKBez WLzA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j4-20020a170903028400b001727963f929si569658plr.130.2022.11.24.01.19.37; Thu, 24 Nov 2022 01:19:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229922AbiKXJNx (ORCPT + 99 others); Thu, 24 Nov 2022 04:13:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229914AbiKXJNv (ORCPT ); Thu, 24 Nov 2022 04:13:51 -0500 Received: from out30-54.freemail.mail.aliyun.com (out30-54.freemail.mail.aliyun.com [115.124.30.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E199D1122E3 for ; Thu, 24 Nov 2022 01:13:49 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R251e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045170;MF=zelin.deng@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VVaTOzS_1669281187; Received: from alinux-host.hz.ali.com(mailfrom:zelin.deng@linux.alibaba.com fp:SMTPD_---0VVaTOzS_1669281187) by smtp.aliyun-inc.com; Thu, 24 Nov 2022 17:13:07 +0800 From: Zelin Deng To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Tom Lendacky , Thomas Gleixner , Borislav Petkov , Zelin Deng Subject: [PATCH 2/2] x86/setup: Preserve _ENC flag when initrd is being relocated Date: Thu, 24 Nov 2022 17:12:46 +0800 Message-Id: <20221124091246.4957-3-zelin.deng@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221124091246.4957-1-zelin.deng@linux.alibaba.com> References: <20221124091246.4957-1-zelin.deng@linux.alibaba.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750368612674084633?= X-GMAIL-MSGID: =?utf-8?q?1750368612674084633?= Commit 107cd2532181 ("Encrypt the initrd earlier for BSP microcode update") when SME is enabled, initrd will be encrypted at earlier stage. If initrd is located at e820 reserved area the initrd will be copied to direct mapping area in relocate_initrd(). In this case source address of initrd should be mapped as encrypted while copy_from_early_mem() will clear encrypted attribute as the source address is not in kernel usable area, therefore relocated initrd is encrypted data and is not able to be unpacked later. Add new function copy_early_initrd() to preserve _ENC flag in setup.c and remove copy_from_early_mem() as it's only used once here by x86. Signed-off-by: Zelin Deng --- arch/x86/kernel/setup.c | 30 ++++++++++++++++++++++++++++- include/asm-generic/early_ioremap.h | 6 ------ mm/early_ioremap.c | 21 -------------------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 56deaf37e508..f9996982f026 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -260,6 +260,34 @@ static u64 __init get_ramdisk_size(void) return ramdisk_size; } +#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT) + +static void __init copy_early_initrd(void *dest, phys_addr_t src, + unsigned long size) +{ + unsigned long slop, clen; + char *p; + + while (size) { + slop = offset_in_page(src); + clen = size; + if (clen > MAX_MAP_CHUNK - slop) + clen = MAX_MAP_CHUNK - slop; + /* + * _ENC flag should be preserved so that when SME is enabled initrd + * can be mapped as encrypted, as it had been encrypted earlier. + * This flag won't impact on other platforms like TDX/SEV enabled. + */ + p = early_memremap_prot(src & PAGE_MASK, clen + slop, + pgprot_val(FIXMAP_PAGE_NORMAL)); + memcpy(dest, p + slop, clen); + early_memunmap(p, clen + slop); + dest += clen; + src += clen; + size -= clen; + } +} + static void __init relocate_initrd(void) { /* Assume only end is not page aligned */ @@ -279,7 +307,7 @@ static void __init relocate_initrd(void) printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n", relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1); - copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size); + copy_early_initrd((void *)initrd_start, ramdisk_image, ramdisk_size); printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to" " [mem %#010llx-%#010llx]\n", diff --git a/include/asm-generic/early_ioremap.h b/include/asm-generic/early_ioremap.h index 9d0479f50f97..be1ce406f481 100644 --- a/include/asm-generic/early_ioremap.h +++ b/include/asm-generic/early_ioremap.h @@ -32,12 +32,6 @@ extern void early_ioremap_setup(void); */ extern void early_ioremap_reset(void); -/* - * Early copy from unmapped memory to kernel mapped memory. - */ -extern void copy_from_early_mem(void *dest, phys_addr_t src, - unsigned long size); - #else static inline void early_ioremap_init(void) { } static inline void early_ioremap_setup(void) { } diff --git a/mm/early_ioremap.c b/mm/early_ioremap.c index 9bc12e526ed0..86b68d63ad35 100644 --- a/mm/early_ioremap.c +++ b/mm/early_ioremap.c @@ -245,27 +245,6 @@ early_memremap_prot(resource_size_t phys_addr, unsigned long size, } #endif -#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT) - -void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size) -{ - unsigned long slop, clen; - char *p; - - while (size) { - slop = offset_in_page(src); - clen = size; - if (clen > MAX_MAP_CHUNK - slop) - clen = MAX_MAP_CHUNK - slop; - p = early_memremap(src & PAGE_MASK, clen + slop); - memcpy(dest, p + slop, clen); - early_memunmap(p, clen + slop); - dest += clen; - src += clen; - size -= clen; - } -} - #else /* CONFIG_MMU */ void __init __iomem *