From patchwork Wed Oct 10 08:41:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Fan X-Patchwork-Id: 10634187 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 084AA69B1 for ; Wed, 10 Oct 2018 08:41:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2336429596 for ; Wed, 10 Oct 2018 08:41:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 169D12959A; Wed, 10 Oct 2018 08:41:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0521529597 for ; Wed, 10 Oct 2018 08:41:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726722AbeJJQCy (ORCPT ); Wed, 10 Oct 2018 12:02:54 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:4245 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726636AbeJJQCx (ORCPT ); Wed, 10 Oct 2018 12:02:53 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="45870134" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 10 Oct 2018 16:41:44 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 571D94B6ED4F; Wed, 10 Oct 2018 16:41:43 +0800 (CST) Received: from localhost.localdomain (10.167.225.56) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 10 Oct 2018 16:41:49 +0800 From: Chao Fan To: , , , , , , , , CC: , , , , Subject: [PATCH v8 1/3] x86/boot: Add acpitb.c to parse acpi tables Date: Wed, 10 Oct 2018 16:41:17 +0800 Message-ID: <20181010084119.17539-2-fanc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181010084119.17539-1-fanc.fnst@cn.fujitsu.com> References: <20181010084119.17539-1-fanc.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.225.56] X-yoursite-MailScanner-ID: 571D94B6ED4F.AE74B X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: fanc.fnst@cn.fujitsu.com Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There is a bug that kaslr may randomly chooses some positions which are located in movable memory regions. This will break memory hotplug feature and make the movable memory chosen by KASLR can't be removed. So dig SRAT table from ACPI tables to get memory information. Imitate the ACPI code of parsing ACPI tables to dig and read ACPI tables. Since some operations are not needed here, functions are simplified. Functions will be used to dig only SRAT tables to get information of memory, so that KASLR can the memory in immovable node. And also, these functions won't influence the initialization of ACPI after start_kernel(). Since use physical address directely, so acpi_os_map_memory() and acpi_os_unmap_memory() are not needed. Signed-off-by: Chao Fan --- arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/acpitb.c | 405 ++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 8 + 3 files changed, 415 insertions(+) create mode 100644 arch/x86/boot/compressed/acpitb.c diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 28764dacf018..1609e4efcaed 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -83,6 +83,8 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/pgtable_64.o endif +vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/acpitb.o + $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \ diff --git a/arch/x86/boot/compressed/acpitb.c b/arch/x86/boot/compressed/acpitb.c new file mode 100644 index 000000000000..6b869e3f9780 --- /dev/null +++ b/arch/x86/boot/compressed/acpitb.c @@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0 +#define BOOT_CTYPE_H +#include "misc.h" +#include "error.h" + +#include +#include +#include +#include + +extern unsigned long get_cmd_line_ptr(void); + +#define STATIC +#include + +#ifdef CONFIG_MEMORY_HOTREMOVE +struct mem_vector { + unsigned long long start; + unsigned long long size; +}; +/* Store the immovable memory regions */ +struct mem_vector immovable_mem[MAX_NUMNODES*2]; +#endif + +#ifdef CONFIG_EFI +/* Search EFI table for rsdp table. */ +static bool efi_get_rsdp_addr(acpi_physical_address *rsdp_addr) +{ + efi_system_table_t *systab; + bool find_rsdp = false; + bool efi_64 = false; + void *config_tables; + struct efi_info *e; + char *sig; + int size; + int i; + + e = &boot_params->efi_info; + sig = (char *)&e->efi_loader_signature; + + if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) + efi_64 = true; + else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) + efi_64 = false; + else { + debug_putstr("Wrong EFI loader signature.\n"); + return false; + } + + /* Get systab from boot params. Based on efi_init(). */ +#ifdef CONFIG_X86_32 + if (e->efi_systab_hi || e->efi_memmap_hi) { + debug_putstr("Table located above 4GB, disabling EFI.\n"); + return false; + } + systab = (efi_system_table_t *)e->efi_systab; +#else + systab = (efi_system_table_t *)( + e->efi_systab | ((__u64)e->efi_systab_hi<<32)); +#endif + + if (systab == NULL) + return false; + + /* + * Get EFI tables from systab. Based on efi_config_init() and + * efi_config_parse_tables(). Only dig the config_table. + */ + size = efi_64 ? sizeof(efi_config_table_64_t) : + sizeof(efi_config_table_32_t); + + for (i = 0; i < systab->nr_tables; i++) { + efi_guid_t guid; + unsigned long table; + + config_tables = (void *)(systab->tables + size * i); + if (efi_64) { + efi_config_table_64_t *tmp_table; + + tmp_table = (efi_config_table_64_t *)config_tables; + guid = tmp_table->guid; + table = tmp_table->table; +#ifndef CONFIG_64BIT + if (table >> 32) { + debug_putstr("Table located above 4G, disabling EFI.\n"); + return false; + } +#endif + } else { + efi_config_table_32_t *tmp_table; + + tmp_table = (efi_config_table_32_t *)config_tables; + guid = tmp_table->guid; + table = tmp_table->table; + } + + /* + * Get rsdp from EFI tables. + * If ACPI20 table found, use it and return true. + * If ACPI20 table not found, but ACPI table found, + * use the ACPI table and return true. + * If neither ACPI table nor ACPI20 table found, + * return false. + */ + if (!(efi_guidcmp(guid, ACPI_TABLE_GUID))) { + *rsdp_addr = (acpi_physical_address)table; + find_rsdp = true; + } else if (!(efi_guidcmp(guid, ACPI_20_TABLE_GUID))) { + *rsdp_addr = (acpi_physical_address)table; + return true; + } + } + return find_rsdp; +} +#else +static bool efi_get_rsdp_addr(acpi_physical_address *rsdp_addr) +{ + return false; +} +#endif + +static u8 checksum(u8 *buffer, u32 length) +{ + u8 sum = 0; + u8 *end = buffer + length; + + while (buffer < end) + sum = (u8)(sum + *(buffer++)); + + return sum; +} + +/* + * Used to search a block of memory for the RSDP signature. + * Return Pointer to the RSDP if found, otherwise NULL. + * Based on acpi_tb_scan_memory_for_rsdp(). + */ +static u8 *scan_mem_for_rsdp(u8 *start_address, u32 length) +{ + struct acpi_table_rsdp *rsdp; + u8 *end_address; + u8 *mem_rover; + + end_address = start_address + length; + + /* Search from given start address for the requested length */ + for (mem_rover = start_address; mem_rover < end_address; + mem_rover += ACPI_RSDP_SCAN_STEP) { + /* + * The RSDP signature and checksum must both be correct + * Note: Sometimes there exists more than one RSDP in memory; + * the valid RSDP has a valid checksum, all others have an + * invalid checksum. + */ + rsdp = (struct acpi_table_rsdp *)mem_rover; + + /* Nope, BAD Signature */ + if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature)) + continue; + + /* Check the standard checksum */ + if (checksum((u8 *) rsdp, ACPI_RSDP_CHECKSUM_LENGTH) != 0) + continue; + + /* Check extended checksum if table version >= 2 */ + if ((rsdp->revision >= 2) && + (checksum((u8 *) rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)) + continue; + + /* Sig and checksum valid, we have found a real RSDP */ + return mem_rover; + } + return NULL; +} + +/* + * Used to search RSDP physical address. + * Based on acpi_find_root_pointer(). Since only use physical address + * in this period, so there is no need to do the memory map jobs. + */ +static void bios_get_rsdp_addr(acpi_physical_address *rsdp_addr) +{ + struct acpi_table_rsdp *rsdp; + u8 *table_ptr; + u8 *mem_rover; + u32 address; + + /* + * Get the location of the Extended BIOS Data Area (EBDA) + * Since we use physical address directely, so + * acpi_os_map_memory() and acpi_os_unmap_memory() are + * not needed here. + */ + table_ptr = (u8 *)ACPI_EBDA_PTR_LOCATION; + *(u32 *)(void *)&address = *(u16 *)(void *)table_ptr; + address <<= 4; + table_ptr = (u8 *)address; + + /* + * Search EBDA paragraphs (EBDA is required to be a minimum of + * 1K length) + */ + if (address > 0x400) { + mem_rover = scan_mem_for_rsdp(table_ptr, ACPI_EBDA_WINDOW_SIZE); + + if (mem_rover) { + address += (u32)ACPI_PTR_DIFF(mem_rover, table_ptr); + *rsdp_addr = (acpi_physical_address)address; + return; + } + } + + table_ptr = (u8 *)ACPI_HI_RSDP_WINDOW_BASE; + mem_rover = scan_mem_for_rsdp(table_ptr, ACPI_HI_RSDP_WINDOW_SIZE); + + /* + * Search upper memory: 16-byte boundaries in E0000h-FFFFFh + * Since we use physical address directely, so + * acpi_os_map_memory() and acpi_os_unmap_memory() are + * not needed here. + */ + if (mem_rover) { + address = (u32)(ACPI_HI_RSDP_WINDOW_BASE + + ACPI_PTR_DIFF(mem_rover, table_ptr)); + *rsdp_addr = (acpi_physical_address)address; + return; + } +} + +#ifdef CONFIG_KEXEC +static bool get_acpi_rsdp(acpi_physical_address *rsdp_addr) +{ + char *args = (char *)get_cmd_line_ptr(); + size_t len = strlen((char *)args); + char *tmp_cmdline, *param, *val; + unsigned long long addr = 0; + char *endptr; + + if (!strstr(args, "acpi_rsdp=")) + return false; + + tmp_cmdline = malloc(len+1); + if (!tmp_cmdline) + error("Failed to allocate space for tmp_cmdline"); + + memcpy(tmp_cmdline, args, len); + tmp_cmdline[len] = 0; + args = tmp_cmdline; + + args = skip_spaces(args); + + while (*args) { + args = next_arg(args, ¶m, &val); + + if (!val && strcmp(param, "--") == 0) { + warn("Only '--' specified in cmdline"); + free(tmp_cmdline); + return false; + } + + if (!strcmp(param, "acpi_rsdp")) { + addr = simple_strtoull(val, &endptr, 0); + + if (addr == 0) + return false; + + *rsdp_addr = (acpi_physical_address)addr; + return true; + } + } + return false; +} +#else +static bool get_acpi_rsdp(acpi_physical_address *rsdp_addr) +{ + return false; +} +#endif + +/* + * Used to dig rsdp table from EFI table or BIOS. + * If rsdp table found in EFI table, use it. Or search BIOS. + * Based on acpi_os_get_root_pointer(). + */ +static acpi_physical_address get_rsdp_addr(void) +{ + acpi_physical_address pa = 0; + bool status = false; + + status = get_acpi_rsdp(&pa); + + if (!status || pa == 0) + status = efi_get_rsdp_addr(&pa); + + if (!status || pa == 0) + bios_get_rsdp_addr(&pa); + + return pa; +} + +static struct acpi_table_header *get_acpi_srat_table(void) +{ + char *args = (char *)get_cmd_line_ptr(); + acpi_physical_address acpi_table; + acpi_physical_address root_table; + struct acpi_table_header *header; + struct acpi_table_rsdp *rsdp; + char *signature; + u8 *entry; + u32 count; + u32 size; + int i, j; + u32 len; + + rsdp = (struct acpi_table_rsdp *)get_rsdp_addr(); + if (!rsdp) + return NULL; + + /* Get rsdt or xsdt from rsdp. */ + if (!strstr(args, "acpi=rsdt") && + rsdp->xsdt_physical_address && rsdp->revision > 1) { + root_table = rsdp->xsdt_physical_address; + size = ACPI_XSDT_ENTRY_SIZE; + } else { + root_table = rsdp->rsdt_physical_address; + size = ACPI_RSDT_ENTRY_SIZE; + } + + /* Get ACPI root table from rsdt or xsdt.*/ + header = (struct acpi_table_header *)root_table; + len = header->length; + count = (u32)((len - sizeof(struct acpi_table_header)) / size); + entry = ACPI_ADD_PTR(u8, header, sizeof(struct acpi_table_header)); + + for (i = 0; i < count; i++) { + u64 address64; + + if (size == ACPI_RSDT_ENTRY_SIZE) + acpi_table = ((acpi_physical_address) + (*ACPI_CAST_PTR(u32, entry))); + else { + *(u64 *)(void *)&address64 = *(u64 *)(void *)entry; + acpi_table = (acpi_physical_address) address64; + } + + if (acpi_table) { + header = (struct acpi_table_header *)acpi_table; + signature = header->signature; + + if (!strncmp(signature, "SRAT", 4)) + return header; + } + entry += size; + } + return NULL; +} + +#ifdef CONFIG_MEMORY_HOTREMOVE +/* + * According to ACPI table, filter the immvoable memory regions + * and store them in immovable_mem[]. + */ +void get_immovable_mem(void) +{ + char *args = (char *)get_cmd_line_ptr(); + struct acpi_table_header *table_header; + struct acpi_subtable_header *table; + struct acpi_srat_mem_affinity *ma; + unsigned long table_end; + int i = 0; + + if (!strstr(args, "movable_node") || strstr(args, "acpi=off")) + return; + + table_header = get_acpi_srat_table(); + if (!table_header) + return; + + table_end = (unsigned long)table_header + table_header->length; + + table = (struct acpi_subtable_header *) + ((unsigned long)table_header + sizeof(struct acpi_table_srat)); + + while (((unsigned long)table) + table->length < table_end) { + if (table->type == 1) { + ma = (struct acpi_srat_mem_affinity *)table; + if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)) { + immovable_mem[i].start = ma->base_address; + immovable_mem[i].size = ma->length; + i++; + } + + if (i >= MAX_NUMNODES*2) + break; + } + table = (struct acpi_subtable_header *) + ((unsigned long)table + table->length); + } + num_immovable_mem = i; +} +#else +void get_immovable_mem(void) +{ +} +#endif diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index a1d5918765f3..60337355d113 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -116,3 +116,11 @@ static inline void console_init(void) void set_sev_encryption_mask(void); #endif + +/* acpitb.c */ +/* Store the amount of immovable memory regions */ +int num_immovable_mem; +void get_immovable_mem(void); +#ifdef CONFIG_MEMORY_HOTREMOVE +#define ACPI_MAX_TABLES 128 +#endif From patchwork Wed Oct 10 08:41:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Fan X-Patchwork-Id: 10634193 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C274A46E4 for ; Wed, 10 Oct 2018 08:42:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 65D0829596 for ; Wed, 10 Oct 2018 08:42:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 59E752959A; Wed, 10 Oct 2018 08:42:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0815529596 for ; Wed, 10 Oct 2018 08:42:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727277AbeJJQC4 (ORCPT ); Wed, 10 Oct 2018 12:02:56 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:4245 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727261AbeJJQCz (ORCPT ); Wed, 10 Oct 2018 12:02:55 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="45870137" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 10 Oct 2018 16:41:44 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 02B534B6ED69; Wed, 10 Oct 2018 16:41:46 +0800 (CST) Received: from localhost.localdomain (10.167.225.56) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 10 Oct 2018 16:41:51 +0800 From: Chao Fan To: , , , , , , , , CC: , , , , Subject: [PATCH v8 2/3] x86/boot/KASLR: Walk srat tables to filter immovable memory Date: Wed, 10 Oct 2018 16:41:18 +0800 Message-ID: <20181010084119.17539-3-fanc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181010084119.17539-1-fanc.fnst@cn.fujitsu.com> References: <20181010084119.17539-1-fanc.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.225.56] X-yoursite-MailScanner-ID: 02B534B6ED69.ABF36 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: fanc.fnst@cn.fujitsu.com Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If CONFIG_MEMORY_HOTREMOVE enabled, walk through the acpi srat memory tables and store those immovable memory regions so that kaslr can get where to choose for randomization. Signed-off-by: Chao Fan --- arch/x86/boot/compressed/kaslr.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 9ed9709d9947..573e582e8709 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -417,6 +417,9 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, /* Mark the memmap regions we need to avoid */ handle_mem_options(); + /* Mark the immovable regions we need to choose */ + get_immovable_mem(); + #ifdef CONFIG_X86_VERBOSE_BOOTUP /* Make sure video RAM can be used. */ add_identity_map(0, PMD_SIZE); From patchwork Wed Oct 10 08:41:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Fan X-Patchwork-Id: 10634189 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DC4E33CF1 for ; Wed, 10 Oct 2018 08:42:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4FEE29597 for ; Wed, 10 Oct 2018 08:42:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C90832959C; Wed, 10 Oct 2018 08:42:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 56A7329597 for ; Wed, 10 Oct 2018 08:42:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727033AbeJJQDA (ORCPT ); Wed, 10 Oct 2018 12:03:00 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:50528 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726636AbeJJQC7 (ORCPT ); Wed, 10 Oct 2018 12:02:59 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="45870141" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 10 Oct 2018 16:41:50 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 399E84B6ED4B; Wed, 10 Oct 2018 16:41:49 +0800 (CST) Received: from localhost.localdomain (10.167.225.56) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 10 Oct 2018 16:41:54 +0800 From: Chao Fan To: , , , , , , , , CC: , , , , Subject: [PATCH v8 3/3] x86/boot/KASLR: Limit kaslr to choosing the immovable memory Date: Wed, 10 Oct 2018 16:41:19 +0800 Message-ID: <20181010084119.17539-4-fanc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181010084119.17539-1-fanc.fnst@cn.fujitsu.com> References: <20181010084119.17539-1-fanc.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.225.56] X-yoursite-MailScanner-ID: 399E84B6ED4B.ADD86 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: fanc.fnst@cn.fujitsu.com Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If CONFIG_MEMORY_HOTREMOVE enabled and the amount of immovable memory regions is not zero. Calculate the intersection between memory regions from e820/efi memory table and immovable memory regions. Signed-off-by: Chao Fan --- arch/x86/boot/compressed/kaslr.c | 72 +++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 573e582e8709..61486aad39b6 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -101,6 +101,11 @@ static bool memmap_too_large; /* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */ static unsigned long long mem_limit = ULLONG_MAX; +#ifdef CONFIG_MEMORY_HOTREMOVE +/* Store the immovable memory regions */ +extern struct mem_vector immovable_mem[MAX_NUMNODES*2]; +#endif + enum mem_avoid_index { MEM_AVOID_ZO_RANGE = 0, @@ -575,9 +580,9 @@ static unsigned long slots_fetch_random(void) return 0; } -static void process_mem_region(struct mem_vector *entry, - unsigned long minimum, - unsigned long image_size) +static void slots_count(struct mem_vector *entry, + unsigned long minimum, + unsigned long image_size) { struct mem_vector region, overlap; unsigned long start_orig, end; @@ -653,6 +658,57 @@ static void process_mem_region(struct mem_vector *entry, } } +static bool process_mem_region(struct mem_vector *region, + unsigned long long minimum, + unsigned long long image_size) +{ + int i; + /* + * If no immovable memory found, or MEMORY_HOTREMOVE disabled, + * walk all the regions, so use region directely. + */ + if (num_immovable_mem == 0) { + slots_count(region, minimum, image_size); + + if (slot_area_index == MAX_SLOT_AREA) { + debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n"); + return 1; + } + return 0; + } + +#ifdef CONFIG_MEMORY_HOTREMOVE + /* + * If immovable memory found, filter the intersection between + * immovable memory and region to slots_count. + * Otherwise, go on old code. + */ + for (i = 0; i < num_immovable_mem; i++) { + struct mem_vector entry; + unsigned long long start, end, entry_end, region_end; + + if (!mem_overlaps(region, &immovable_mem[i])) + continue; + + start = immovable_mem[i].start; + end = start + immovable_mem[i].size; + region_end = region->start + region->size; + + entry.start = clamp(region->start, start, end); + entry_end = clamp(region_end, start, end); + entry.size = entry_end - entry.start; + + slots_count(&entry, minimum, image_size); + + if (slot_area_index == MAX_SLOT_AREA) { + debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n"); + return 1; + } + } + return 0; +#endif +} + #ifdef CONFIG_EFI /* * Returns true if mirror region found (and must have been processed @@ -718,11 +774,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size) region.start = md->phys_addr; region.size = md->num_pages << EFI_PAGE_SHIFT; - process_mem_region(®ion, minimum, image_size); - if (slot_area_index == MAX_SLOT_AREA) { - debug_putstr("Aborted EFI scan (slot_areas full)!\n"); + if (process_mem_region(®ion, minimum, image_size)) break; - } } return true; } @@ -749,11 +802,8 @@ static void process_e820_entries(unsigned long minimum, continue; region.start = entry->addr; region.size = entry->size; - process_mem_region(®ion, minimum, image_size); - if (slot_area_index == MAX_SLOT_AREA) { - debug_putstr("Aborted e820 scan (slot_areas full)!\n"); + if (process_mem_region(®ion, minimum, image_size)) break; - } } }