From patchwork Thu Jan 28 10:57:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 12053365 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47842C433E0 for ; Thu, 28 Jan 2021 10:57:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B76B964DDD for ; Thu, 28 Jan 2021 10:57:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B76B964DDD Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 483E26B0006; Thu, 28 Jan 2021 05:57:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 434066B006C; Thu, 28 Jan 2021 05:57:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 372006B0070; Thu, 28 Jan 2021 05:57:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0165.hostedemail.com [216.40.44.165]) by kanga.kvack.org (Postfix) with ESMTP id 22A866B0006 for ; Thu, 28 Jan 2021 05:57:40 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id D01DA3631 for ; Thu, 28 Jan 2021 10:57:39 +0000 (UTC) X-FDA: 77754883038.02.spot04_150c41a2759f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id A4AFE10097AA0 for ; Thu, 28 Jan 2021 10:57:39 +0000 (UTC) X-HE-Tag: spot04_150c41a2759f X-Filterd-Recvd-Size: 7397 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Thu, 28 Jan 2021 10:57:38 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id CA21164DDC; Thu, 28 Jan 2021 10:57:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1611831457; bh=70iHhMxvmraB34yqcAAJU007XgcD9AGfPnYDMHhju08=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O4FAFSSXFkb0EsMN6SDLmMung7rmRn1ozygUmvcrjlH4MtnI410hMaNtzN43nguBv bLQPVHTZc+te4WxGWwvC/BAYjr6mFCVQZy1fMxSktiiXL+zwTNM4SVtGiTwY9qW+Nz VsX+BJOQuMLoL6UbG7idcTBev8Ms9MPLq8e2/ajHFyGn9y0Dcsadk4XDF9Yn7mdZ8i PZyqTGjMDLqPXkrHkhNCk8n6fhyTHB8/xTJeHgAg/RbvJHC4CoqMiMQFjYSPGEOTjh jtyUypoQ5K+AaRFNSN1KkY0QxuzBO1Hx9/aVgWQSEawu6Ff3jWREZmslsJoXWEV3+C AE4UidmbYl8MQ== From: Mike Rapoport To: Andrew Morton Cc: Andrea Arcangeli , Baoquan He , Borislav Petkov , David Hildenbrand , "H. Peter Anvin" , Ingo Molnar , Mel Gorman , Michal Hocko , Mike Rapoport , Mike Rapoport , Qian Cai , Thomas Gleixner , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org, Borislav Petkov Subject: [PATCH v2 1/2] x86/setup: consolidate early memory reservations Date: Thu, 28 Jan 2021 12:57:10 +0200 Message-Id: <20210128105711.10428-2-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210128105711.10428-1-rppt@kernel.org> References: <20210128105711.10428-1-rppt@kernel.org> MIME-Version: 1.0 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: From: Mike Rapoport The early reservations of memory areas used by the firmware, bootloader, kernel text and data are spread over setup_arch(). Moreover, some of them happen *after* memblock allocations, e.g trim_platform_memory_ranges() and trim_low_memory_range() are called after reserve_real_mode() that allocates memory. There was no corruption of these memory regions because memblock always allocates memory either from the end of memory (in top-down mode) or above the kernel image (in bottom-up mode). However, the bottom up mode is going to be updated to span the entire memory [1] to avoid limitations caused by KASLR. Consolidate early memory reservations in a dedicated function to improve robustness against future changes. Having the early reservations in one place also makes it clearer what memory must be reserved before we allow memblock allocations. [1] https://lore.kernel.org/lkml/20201217201214.3414100-2-guro@fb.com Signed-off-by: Mike Rapoport Acked-by: Borislav Petkov --- arch/x86/kernel/setup.c | 92 ++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 3412c4595efd..3b582406363a 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -646,18 +646,6 @@ static void __init trim_snb_memory(void) } } -/* - * Here we put platform-specific memory range workarounds, i.e. - * memory known to be corrupt or otherwise in need to be reserved on - * specific platforms. - * - * If this gets used more widely it could use a real dispatch mechanism. - */ -static void __init trim_platform_memory_ranges(void) -{ - trim_snb_memory(); -} - static void __init trim_bios_range(void) { /* @@ -728,7 +716,38 @@ static void __init trim_low_memory_range(void) */ memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE)); } - + +static void __init early_reserve_memory(void) +{ + /* + * Reserve the memory occupied by the kernel between _text and + * __end_of_kernel_reserve symbols. Any kernel sections after the + * __end_of_kernel_reserve symbol must be explicitly reserved with a + * separate memblock_reserve() or they will be discarded. + */ + memblock_reserve(__pa_symbol(_text), + (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); + + /* + * Make sure page 0 is always reserved because on systems with + * L1TF its contents can be leaked to user processes. + */ + memblock_reserve(0, PAGE_SIZE); + + early_reserve_initrd(); + + if (efi_enabled(EFI_BOOT)) + efi_memblock_x86_reserve_range(); + + memblock_x86_reserve_range_setup_data(); + + reserve_ibft_region(); + reserve_bios_regions(); + + trim_snb_memory(); + trim_low_memory_range(); +} + /* * Dump out kernel offset information on panic. */ @@ -763,29 +782,6 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p) void __init setup_arch(char **cmdline_p) { - /* - * Reserve the memory occupied by the kernel between _text and - * __end_of_kernel_reserve symbols. Any kernel sections after the - * __end_of_kernel_reserve symbol must be explicitly reserved with a - * separate memblock_reserve() or they will be discarded. - */ - memblock_reserve(__pa_symbol(_text), - (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); - - /* - * Make sure page 0 is always reserved because on systems with - * L1TF its contents can be leaked to user processes. - */ - memblock_reserve(0, PAGE_SIZE); - - early_reserve_initrd(); - - /* - * At this point everything still needed from the boot loader - * or BIOS or kernel text should be early reserved or marked not - * RAM in e820. All other memory is free game. - */ - #ifdef CONFIG_X86_32 memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data)); @@ -909,8 +905,18 @@ void __init setup_arch(char **cmdline_p) parse_early_param(); - if (efi_enabled(EFI_BOOT)) - efi_memblock_x86_reserve_range(); + /* + * Do some memory reservations *before* memory is added to + * memblock, so memblock allocations won't overwrite it. + * Do it after early param, so we could get (unlikely) panic from + * serial. + * + * After this point everything still needed from the boot loader or + * firmware or kernel text should be early reserved or marked not + * RAM in e820. All other memory is free game. + */ + early_reserve_memory(); + #ifdef CONFIG_MEMORY_HOTPLUG /* * Memory used by the kernel cannot be hot-removed because Linux @@ -937,9 +943,6 @@ void __init setup_arch(char **cmdline_p) x86_report_nx(); - /* after early param, so could get panic from serial */ - memblock_x86_reserve_range_setup_data(); - if (acpi_mps_check()) { #ifdef CONFIG_X86_LOCAL_APIC disable_apic = 1; @@ -1031,8 +1034,6 @@ void __init setup_arch(char **cmdline_p) */ find_smp_config(); - reserve_ibft_region(); - early_alloc_pgt_buf(); /* @@ -1053,8 +1054,6 @@ void __init setup_arch(char **cmdline_p) */ sev_setup_arch(); - reserve_bios_regions(); - efi_fake_memmap(); efi_find_mirror(); efi_esrt_init(); @@ -1080,9 +1079,6 @@ void __init setup_arch(char **cmdline_p) reserve_real_mode(); - trim_platform_memory_ranges(); - trim_low_memory_range(); - init_mem_mapping(); idt_setup_early_pf(); From patchwork Thu Jan 28 10:57:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 12053367 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35B45C433DB for ; Thu, 28 Jan 2021 10:57:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B77C064DCC for ; Thu, 28 Jan 2021 10:57:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B77C064DCC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3C0B96B006C; Thu, 28 Jan 2021 05:57:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 370AF6B0070; Thu, 28 Jan 2021 05:57:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 287266B0071; Thu, 28 Jan 2021 05:57:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0197.hostedemail.com [216.40.44.197]) by kanga.kvack.org (Postfix) with ESMTP id 128736B006C for ; Thu, 28 Jan 2021 05:57:45 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id D0A003631 for ; Thu, 28 Jan 2021 10:57:44 +0000 (UTC) X-FDA: 77754883248.27.brick20_5b125b32759f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id CE0A23D66D for ; Thu, 28 Jan 2021 10:57:43 +0000 (UTC) X-HE-Tag: brick20_5b125b32759f X-Filterd-Recvd-Size: 4230 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Thu, 28 Jan 2021 10:57:43 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id D006364DDE; Thu, 28 Jan 2021 10:57:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1611831462; bh=5Sw4HTwjI2sdnT2rdgUGy47J/eSb+BY4hM6CzexHw0k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CUrpTULtXjISGGuNLkaWh0nyyzZTs9ivBnwvmEUajVOVy7MPcr4Q/xpz7PolcSOe3 jqvC7zsMVzYIpbYOD+jVkZzBawXWFJzSMeWk3AllXmiBbp6ouGo+sEkeBhy7QZ2Oie WshwKwijExbs5ruO/r+CwGxVC9g7roLLAdP1Ddy8vBM2I7BhCa/S1QGpLyimd8o294 tf4QED1f0++AjJxWmganQ0ultc8Rt6YUcmfqx6N0pXOXplv/yI4raNAjABC8RL8v0s iufvLT80AvYfodt5kYUFKRef3EhEF/zmcIea3wZABPlKWPk+XPq93X0tT5VjeNzEbT g1DZaRo+kBUAA== From: Mike Rapoport To: Andrew Morton Cc: Andrea Arcangeli , Baoquan He , Borislav Petkov , David Hildenbrand , "H. Peter Anvin" , Ingo Molnar , Mel Gorman , Michal Hocko , Mike Rapoport , Mike Rapoport , Qian Cai , Thomas Gleixner , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org, Borislav Petkov Subject: [PATCH v2 2/2] x86/setup: merge several reservations of start of the memory Date: Thu, 28 Jan 2021 12:57:11 +0200 Message-Id: <20210128105711.10428-3-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210128105711.10428-1-rppt@kernel.org> References: <20210128105711.10428-1-rppt@kernel.org> MIME-Version: 1.0 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: From: Mike Rapoport Currently the first several pages are reserved both to avoid leaking their contents on systems with L1TF and to avoid corrupting BIOS memory. Merge the two memory reservations. Signed-off-by: Mike Rapoport Reviewed-by: David Hildenbrand Acked-by: Borislav Petkov --- arch/x86/kernel/setup.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 3b582406363a..b36624e3dc9e 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -703,20 +703,6 @@ static int __init parse_reservelow(char *p) early_param("reservelow", parse_reservelow); -static void __init trim_low_memory_range(void) -{ - /* - * A special case is the first 4Kb of memory; - * This is a BIOS owned area, not kernel ram, but generally - * not listed as such in the E820 table. - * - * This typically reserves additional memory (64KiB by default) - * since some BIOSes are known to corrupt low memory. See the - * Kconfig help text for X86_RESERVE_LOW. - */ - memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE)); -} - static void __init early_reserve_memory(void) { /* @@ -729,10 +715,17 @@ static void __init early_reserve_memory(void) (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); /* - * Make sure page 0 is always reserved because on systems with - * L1TF its contents can be leaked to user processes. + * The first 4Kb of memory is a BIOS owned area, but generally it is + * not listed as such in the E820 table. + * + * Reserve the first memory page and typically some additional + * memory (64KiB by default) since some BIOSes are known to corrupt + * low memory. See the Kconfig help text for X86_RESERVE_LOW. + * + * In addition, make sure page 0 is always reserved because on + * systems with L1TF its contents can be leaked to user processes. */ - memblock_reserve(0, PAGE_SIZE); + memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE)); early_reserve_initrd(); @@ -745,7 +738,6 @@ static void __init early_reserve_memory(void) reserve_bios_regions(); trim_snb_memory(); - trim_low_memory_range(); } /*