Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3260510pxj; Tue, 1 Jun 2021 00:55:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYFOdQKreOvJR/Poe60QdXHOW3irSsiWzCnmfx4SpI3uT2VseMwIMVDolN9byHdhJcW312 X-Received: by 2002:a05:6638:34ac:: with SMTP id t44mr23360484jal.97.1622534119313; Tue, 01 Jun 2021 00:55:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622534119; cv=none; d=google.com; s=arc-20160816; b=me9D53f5JsPlWSybTrfdN3Cq8sXYI1ecCogFqVSzswgs65DEVXR5PQHgMH/In+PSE2 R3N5RVU8i+hgK+uHtGYHP5iWOPLgSQGOg3yp8XvI0bgCclndpfPzbNVAKlfvjWujND7V jipJmdydOWQCVvQd6IokSbbSsNoFDAqHtPilIIXDsR3RbqtRTS2tRv9u10eDqFg2oOAd ZTw63BiupVO7yeNOoySM2Xyw3e6FJ1sKi2IPTKqtxQ+BGP1jLfx706+9x2sgF/q2gKfB sdv+jsUae5L/ngqMfahxaSgp56WL4ZDtja0EWo/7B92i69FNVLGvJffqcDV+Pn4Pi+Ga Do7w== 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 :dkim-signature; bh=KEXUaG3POqQkAlKE8S4toO8OK4t3qKX5CUgq+FZVAmI=; b=cTPaQFk4Wm4kHDgd4S2MJVxwlahCCqBkiOLxoeYIUEDvAQnm/KDJ8CDsX8PZ+o3GbV IVVSpCZh7cak8r2xloP6tACchOQIG8oYqFarwgieeAPo4+LOscFDO8a2XarG9wlQSSTq HT5mnj/uOzigtbF/HGocQ4KWKjC36d4nfhKeZRdYicohPSbFFNkctgNNK/gdsMXDnENb crFwtqQQajknyDDiwG354Ccaphld2Eocn2tLDsgchFQUVk72pSEXOwGxCoNaV3IDfhk8 16YqkylW4RTZe8puLByrtUWmbfa8T/QJKEsPRAzEwHrrLUsaX5366lfYv9Yko9BkgJyN aobg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kbSnnlSc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f12si10567837ils.63.2021.06.01.00.55.06; Tue, 01 Jun 2021 00:55:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kbSnnlSc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233300AbhFAH4E (ORCPT + 99 others); Tue, 1 Jun 2021 03:56:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:34754 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233265AbhFAH4B (ORCPT ); Tue, 1 Jun 2021 03:56:01 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A08E961378; Tue, 1 Jun 2021 07:54:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1622534060; bh=3D+bj5o5A5tcEPmr9Tb4qDYrRfzOIR7HmG3BtQDywkw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kbSnnlSctb6taPdQLppGzt3Mh6GYeKS/HKwxxhaOiIssZdPxWwLsOpwG+Z+12pnfG mJhJQdF08CH/ab9fC94kJM1KqI+XzUsqwjHkt5QbMm9ua3IFsG2Um15hhiNNJbJMjh TVKWRoWQd8D8BK6/QR7GpgpxReivElXECLBXtfqCzUiScTU5Ady9CkozetXlU/ra++ H86i00og9/6sMybL8tSNtFnl1dQpdOLgYO07jYbu15DJOq6D0AiMB7bDLPeMUPwNm7 QDn5dBzL08mGPV8mXRuHdCXXdwtIGGHce/NEx9ydaenXjgYP0psbMFeQXAVbHYenJt auqoATZ0lthnA== From: Mike Rapoport To: x86@kernel.org Cc: "H. Peter Anvin" , Andy Lutomirski , Andy Shevchenko , Ard Biesheuvel , Baoquan He , Borislav Petkov , Darren Hart , Dave Young , Hugh Dickins , Ingo Molnar , Jonathan Corbet , Lianbo Jiang , Mike Rapoport , Mike Rapoport , Randy Dunlap , Thomas Gleixner , linux-doc@vger.kernel.org, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org Subject: [PATCH 1/3] x86/setup: always reserve the first 1M of RAM Date: Tue, 1 Jun 2021 10:53:52 +0300 Message-Id: <20210601075354.5149-2-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210601075354.5149-1-rppt@kernel.org> References: <20210601075354.5149-1-rppt@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Rapoport There are BIOSes that are known to corrupt the memory under 1M, or more precisely under 640K because the memory above 640K is anyway reserved for the EGA/VGA frame buffer and BIOS. To prevent usage of the memory that will be potentially clobbered by the kernel, the beginning of the memory is always reserved. The exact size of the reserved area is determined by CONFIG_X86_RESERVE_LOW build time and reservelow command line option. The reserved range may be from 4K to 640K with the default of 64K. There are also configurations that reserve the entire 1M range, like machines with SandyBridge graphic devices or systems that enable crash kernel. In addition to the potentially clobbered memory, EBDA of unknown size may be as low as 128K and the memory above that EBDA start is also reserved early. It would have been possible to reserve the entire range under 1M unless for the real mode trampoline that must reside in that area. To accommodate placement of the real mode trampoline and keep the memory safe from being clobbered by BIOS reserve the first 64K of RAM before memory allocations are possible and then, after the real mode trampoline is allocated, reserve the entire range from 0 to 1M. Update trim_snb_memory() and reserve_real_mode() to avoid redundant reservations of the same memory range. Also make sure the memory under 1M is not getting freed by efi_free_boot_services(). Fixes: a799c2bd29d1 ("x86/setup: Consolidate early memory reservations") Signed-off-by: Mike Rapoport --- arch/x86/kernel/setup.c | 35 ++++++++++++++++++++-------------- arch/x86/platform/efi/quirks.c | 12 ++++++++++++ arch/x86/realmode/init.c | 14 ++++++++------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 72920af0b3c0..22e9a17d6ac3 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -637,11 +637,11 @@ static void __init trim_snb_memory(void) * them from accessing certain memory ranges, namely anything below * 1M and in the pages listed in bad_pages[] above. * - * To avoid these pages being ever accessed by SNB gfx devices - * reserve all memory below the 1 MB mark and bad_pages that have - * not already been reserved at boot time. + * To avoid these pages being ever accessed by SNB gfx devices reserve + * bad_pages that have not already been reserved at boot time. + * All memory below the 1 MB mark is anyway reserved later during + * setup_arch(), so there is no need to reserve it here. */ - memblock_reserve(0, 1<<20); for (i = 0; i < ARRAY_SIZE(bad_pages); i++) { if (memblock_reserve(bad_pages[i], PAGE_SIZE)) @@ -733,14 +733,14 @@ static void __init early_reserve_memory(void) * 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. + * Reserve the first 64K of memory since some BIOSes are known to + * corrupt low memory. After the real mode trampoline is allocated the + * rest of the memory below 640k is reserved. * * 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, ALIGN(reserve_low, PAGE_SIZE)); + memblock_reserve(0, SZ_64K); early_reserve_initrd(); @@ -751,6 +751,7 @@ static void __init early_reserve_memory(void) reserve_ibft_region(); reserve_bios_regions(); + trim_snb_memory(); } /* @@ -1081,14 +1082,20 @@ void __init setup_arch(char **cmdline_p) (max_pfn_mapped<