From patchwork Tue Mar 6 10:23:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 130761 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3762211lja; Tue, 6 Mar 2018 02:23:42 -0800 (PST) X-Google-Smtp-Source: AG47ELvuolabMYAOVqRYOIJfFNhtPaz7jRnMo6PAS7Bqkf/KUsvuP+Ga2OxG6P6yFFbl+H8OsNtj X-Received: by 10.99.143.3 with SMTP id n3mr14711598pgd.159.1520331821902; Tue, 06 Mar 2018 02:23:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520331821; cv=none; d=google.com; s=arc-20160816; b=XJvBz5q4o407n3iR5yfY7h+OS2l6rjdeo30Pmt55MOTG46F+qVFb8zmS35JI5yVIIg X1Al5twEMD9UinKZ/BzxoeVUix8I62XIix5RZFF7vsNxJn54nRlPV2oAGSfXGg9rhin9 exTIfbihX4zTPfE7Xgjp4MTQ2nWZL4hCK9Qt4eB0BxxXYfuRctJvckw4CEhSkYA5WRXr OmhZCqckrPtBprnbrBN0lCll7l1NUSdHOyJLUk1AhLc280GAjzAu3rAB9Fu0TQj9i9Nc O5crkR2tQcOOOMxt4AECjHjHyxttK4HiUUJFZ+Wu1V28NkPFKScy/vZJPMpKiJTUqcoo bBag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=/VY+FFJrNPaxjaCzFTNjK1ZYe2uky/NRRaXjMwBDnfI=; b=Tp4EHtZYtF3HdRgbYe3M2qqdnEApSaR4FhijDGVmnTlXGrfPpl0wxqC+aLHTtSxPnG kt27apRTo28rYUhsoGqcogz0KDUnJv5vZ1oR5NXhemE+KG3UXoG7nzTexEimEQlGZzwx o9oM6mSmGtoDfzGIdsSgeVIw7SYFWc6MaAl7gAHc8dBrA/snnLgfRxY2y08YoVcTWrnm W0JvMrtN6xJYu1yG5Cvs+LeQarcrAkfRe1P97nl/5yzn2bnIyOPEF6s3alSrrvp7I8QV 3BVRAhdgolI7mHo5XyB3zacuvG29Y/F4ul+eGO3xRYU+8cd18l98tG2HqJP9MpjqPTKC Fa6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Kjyelv8v; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x4-v6si10789697plw.297.2018.03.06.02.23.41; Tue, 06 Mar 2018 02:23:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Kjyelv8v; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753516AbeCFKXi (ORCPT + 28 others); Tue, 6 Mar 2018 05:23:38 -0500 Received: from mail-io0-f194.google.com ([209.85.223.194]:35338 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753305AbeCFKXe (ORCPT ); Tue, 6 Mar 2018 05:23:34 -0500 Received: by mail-io0-f194.google.com with SMTP id 30so21493665iog.2 for ; Tue, 06 Mar 2018 02:23:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/VY+FFJrNPaxjaCzFTNjK1ZYe2uky/NRRaXjMwBDnfI=; b=Kjyelv8v2b0h2S8Kw5mmmH3wOPXAwdE4Zm6txuifKbeXMwxpmvKcTdFaS2F5CgIFhs vqSoxRTFhr9xjC6MBb0XH/gceGyvHJ8xRNY+eqy7LUyWiIWdwNVeKuzx/akVMWskZweY 4EdvX1T0owU+FNMBHsOxhD4cYT8LCqQFyYpBE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/VY+FFJrNPaxjaCzFTNjK1ZYe2uky/NRRaXjMwBDnfI=; b=HGwgXTmrzVNem4jneFtanSZg5/VGRjj3uR8oh2Pk93Ld+KTiYgSzukQlB23sH7YXg9 A1M1/KaHZGFE6hFvTgYkNtND0T4mgNsk12dcv4QfF8X5HFkk6K5QdwDGSlKs0rUu29fp WURJ3K/Lc40yeBU+BKwo5uJGMIiTkf1d5LZy3JHIHNj/WLLNLkv3A7Z4k0kVU8o6XmYi 44SpDpg46qTAVs6qFnL7d/O5NXI43v03bsmUR4yemQqohalzbBoXbtNvLpdF/jS7Eg45 +J427y2DZkmcauWmj2mqcJxcZ5N0mNp/T254fT66JBXbnkhLmN7Dhxlt6yVDVeexxwkh VfDg== X-Gm-Message-State: APf1xPBjQnhBW4tJ5XNdF4wgU4s2cc6NHT0evM60tZux4G9Uhnq/Euvy 7JHxsrer0+mobE2ySzxYzz4ZNA== X-Received: by 10.107.59.130 with SMTP id i124mr20744173ioa.129.1520331814189; Tue, 06 Mar 2018 02:23:34 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id k198sm6494478itk.5.2018.03.06.02.23.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Mar 2018 02:23:33 -0800 (PST) From: AKASHI Takahiro To: dyoung@redhat.com, vgoyal@redhat.com, bhe@redhat.com, mpe@ellerman.id.au, bauerman@linux.vnet.ibm.com, prudo@linux.vnet.ibm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v2 5/7] x86: kexec_file: lift CRASH_MAX_RANGES limit on crash_mem buffer Date: Tue, 6 Mar 2018 19:23:01 +0900 Message-Id: <20180306102303.9063-6-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180306102303.9063-1-takahiro.akashi@linaro.org> References: <20180306102303.9063-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While CRASH_MAX_RANGES (== 16) seems to be good enough, fixed-number array is not a good idea in general. In this patch, size of crash_mem buffer is calculated as before and the buffer is now dynamically allocated. This change also allows removing crash_elf_data structure. Signed-off-by: AKASHI Takahiro Cc: Dave Young Cc: Vivek Goyal Cc: Baoquan He --- arch/x86/kernel/crash.c | 82 +++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 51 deletions(-) -- 2.16.2 diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index b328153cee65..0f20a6330bd6 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -41,32 +41,14 @@ /* Alignment required for elf header segment */ #define ELF_CORE_HEADER_ALIGN 4096 -/* This primarily represents number of split ranges due to exclusion */ -#define CRASH_MAX_RANGES 16 - struct crash_mem_range { u64 start, end; }; struct crash_mem { - unsigned int nr_ranges; - struct crash_mem_range ranges[CRASH_MAX_RANGES]; -}; - -/* Misc data about ram ranges needed to prepare elf headers */ -struct crash_elf_data { - struct kimage *image; - /* - * Total number of ram ranges we have after various adjustments for - * crash reserved region, etc. - */ unsigned int max_nr_ranges; - - /* Pointer to elf header */ - void *ehdr; - /* Pointer to next phdr */ - void *bufp; - struct crash_mem mem; + unsigned int nr_ranges; + struct crash_mem_range ranges[0]; }; /* Used while preparing memory map entries for second kernel */ @@ -217,26 +199,31 @@ static int get_nr_ram_ranges_callback(struct resource *res, void *arg) return 0; } - /* Gather all the required information to prepare elf headers for ram regions */ -static void fill_up_crash_elf_data(struct crash_elf_data *ced, - struct kimage *image) +static struct crash_mem *fill_up_crash_elf_data(void) { unsigned int nr_ranges = 0; - - ced->image = image; + struct crash_mem *cmem; walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback); + if (!nr_ranges) + return NULL; - ced->max_nr_ranges = nr_ranges; + /* + * Exclusion of crash region and/or crashk_low_res may cause + * another range split. So add extra two slots here. + */ + nr_ranges += 2; + cmem = vzalloc(sizeof(struct crash_mem) + + sizeof(struct crash_mem_range) * nr_ranges); + if (!cmem) + return NULL; - /* Exclusion of crash region could split memory ranges */ - ced->max_nr_ranges++; + cmem->max_nr_ranges = nr_ranges; + cmem->nr_ranges = 0; - /* If crashk_low_res is not 0, another range split possible */ - if (crashk_low_res.end) - ced->max_nr_ranges++; + return cmem; } static int exclude_mem_range(struct crash_mem *mem, @@ -293,10 +280,8 @@ static int exclude_mem_range(struct crash_mem *mem, return 0; /* Split happened */ - if (i == CRASH_MAX_RANGES - 1) { - pr_err("Too many crash ranges after split\n"); + if (i == mem->max_nr_ranges - 1) return -ENOMEM; - } /* Location where new range should go */ j = i + 1; @@ -314,11 +299,10 @@ static int exclude_mem_range(struct crash_mem *mem, /* * Look for any unwanted ranges between mstart, mend and remove them. This - * might lead to split and split ranges are put in ced->mem.ranges[] array + * might lead to split and split ranges are put in cmem->ranges[] array */ -static int elf_header_exclude_ranges(struct crash_elf_data *ced) +static int elf_header_exclude_ranges(struct crash_mem *cmem) { - struct crash_mem *cmem = &ced->mem; int ret = 0; /* Exclude crashkernel region */ @@ -337,8 +321,7 @@ static int elf_header_exclude_ranges(struct crash_elf_data *ced) static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) { - struct crash_elf_data *ced = arg; - struct crash_mem *cmem = &ced->mem; + struct crash_mem *cmem = arg; cmem->ranges[cmem->nr_ranges].start = res->start; cmem->ranges[cmem->nr_ranges].end = res->end; @@ -347,7 +330,7 @@ static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) return 0; } -static int prepare_elf64_headers(struct crash_elf_data *ced, bool kernel_map, +static int prepare_elf64_headers(struct crash_mem *cmem, bool kernel_map, void **addr, unsigned long *sz) { Elf64_Ehdr *ehdr; @@ -356,12 +339,11 @@ static int prepare_elf64_headers(struct crash_elf_data *ced, bool kernel_map, unsigned char *buf, *bufp; unsigned int cpu, i; unsigned long long notes_addr; - struct crash_mem *cmem = &ced->mem; unsigned long mstart, mend; /* extra phdr for vmcoreinfo elf note */ nr_phdr = nr_cpus + 1; - nr_phdr += ced->max_nr_ranges; + nr_phdr += cmem->nr_ranges; /* * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping @@ -455,29 +437,27 @@ static int prepare_elf64_headers(struct crash_elf_data *ced, bool kernel_map, static int prepare_elf_headers(struct kimage *image, void **addr, unsigned long *sz) { - struct crash_elf_data *ced; + struct crash_mem *cmem; Elf64_Ehdr *ehdr; Elf64_Phdr *phdr; int ret, i; - ced = kzalloc(sizeof(*ced), GFP_KERNEL); - if (!ced) + cmem = fill_up_crash_elf_data(); + if (!cmem) return -ENOMEM; - fill_up_crash_elf_data(ced, image); - - ret = walk_system_ram_res(0, -1, ced, + ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback); if (ret) goto out; /* Exclude unwanted mem ranges */ - ret = elf_header_exclude_ranges(ced); + ret = elf_header_exclude_ranges(cmem); if (ret) goto out; /* By default prepare 64bit headers */ - ret = prepare_elf64_headers(ced, IS_ENABLED(CONFIG_X86_64), addr, sz); + ret = prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), addr, sz); if (ret) goto out; @@ -495,7 +475,7 @@ static int prepare_elf_headers(struct kimage *image, void **addr, break; } out: - kfree(ced); + vfree(cmem); return ret; }