From patchwork Fri Apr 24 07:53:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 47550 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f197.google.com (mail-lb0-f197.google.com [209.85.217.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 705E620553 for ; Fri, 24 Apr 2015 07:54:18 +0000 (UTC) Received: by lbbqq2 with SMTP id qq2sf9498406lbb.0 for ; Fri, 24 Apr 2015 00:54:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=viBxJUW2aBFX6aKXC/lJ+g3DKo8N2U+ob+Dkk2Ya58M=; b=eyCxe8XimfId6COseFveBYDGLLXuOM7o2rRHNaSyvDg8nziS9BSV5aXEGeZqBq8bef rELWDNEbnZfuNBp/oy0TRxorxfDCy1Eg1nzByuDWRMHLQ8ASzUyGQAtNPzg+rqFsU6K7 4MTNPU8WlIcqNS7mxZqlwknSYSlrVcOrzhELI1nv2N3kTbvbD4yy/1923ZeHvU8C7V8i SNrjr86KtyUyFdzgYzOBW91OopieK5mN7Shf6MMe5e43l6z5UWVtOFilyj9uSwpVV48Y ZFt4tjQIE9o8fiZtQD8GnsN6uzQhkLjt2cDnbbKmspaToyxI99vAT+emPrGheMIk62Pb 2ZOA== X-Gm-Message-State: ALoCoQkvhgb3QXM5VIBYRL6A2bRMCiOr3OO4cNw+jOuiOash3RbYrBm0OrSJSoSsgwD9exaA5rZE X-Received: by 10.180.88.226 with SMTP id bj2mr426074wib.7.1429862057068; Fri, 24 Apr 2015 00:54:17 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.6.35 with SMTP id cr3ls381412lad.47.gmail; Fri, 24 Apr 2015 00:54:16 -0700 (PDT) X-Received: by 10.152.1.4 with SMTP id 4mr5853167lai.25.1429862056874; Fri, 24 Apr 2015 00:54:16 -0700 (PDT) Received: from mail-la0-f49.google.com (mail-la0-f49.google.com. [209.85.215.49]) by mx.google.com with ESMTPS id e10si7697927lae.15.2015.04.24.00.54.16 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Apr 2015 00:54:16 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.49 as permitted sender) client-ip=209.85.215.49; Received: by lagv1 with SMTP id v1so29482678lag.3 for ; Fri, 24 Apr 2015 00:54:16 -0700 (PDT) X-Received: by 10.112.198.74 with SMTP id ja10mr4074806lbc.19.1429862056719; Fri, 24 Apr 2015 00:54:16 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.67.65 with SMTP id l1csp1032402lbt; Fri, 24 Apr 2015 00:54:15 -0700 (PDT) X-Received: by 10.66.123.73 with SMTP id ly9mr12728680pab.156.1429862054473; Fri, 24 Apr 2015 00:54:14 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qx4si16280099pbb.19.2015.04.24.00.54.13; Fri, 24 Apr 2015 00:54:14 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933674AbbDXHx7 (ORCPT + 27 others); Fri, 24 Apr 2015 03:53:59 -0400 Received: from mail-pd0-f170.google.com ([209.85.192.170]:34791 "EHLO mail-pd0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755344AbbDXHxz (ORCPT ); Fri, 24 Apr 2015 03:53:55 -0400 Received: by pdbqa5 with SMTP id qa5so41976293pdb.1 for ; Fri, 24 Apr 2015 00:53:55 -0700 (PDT) X-Received: by 10.70.42.104 with SMTP id n8mr3915023pdl.88.1429862035283; Fri, 24 Apr 2015 00:53:55 -0700 (PDT) Received: from localhost.localdomain (61-205-1-167m5.grp1.mineo.jp. [61.205.1.167]) by mx.google.com with ESMTPSA id ry2sm10154199pbb.83.2015.04.24.00.53.49 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Apr 2015 00:53:53 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, vgoyal@redhat.com, hbabus@us.ibm.com Cc: geoff@infradead.org, broonie@kernel.org, david.griego@linaro.org, kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linaro-kernel@lists.linaro.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [v2 1/5] arm64: kdump: reserve memory for crash dump kernel Date: Fri, 24 Apr 2015 16:53:04 +0900 Message-Id: <1429861989-8417-2-git-send-email-takahiro.akashi@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1429861989-8417-1-git-send-email-takahiro.akashi@linaro.org> References: <1429861989-8417-1-git-send-email-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: takahiro.akashi@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.49 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , On system kernel, the memory region used by crash dump kernel must be specified by "crashkernel=X@Y" boot parameter. reserve_crashkernel() will allocate the region in "System RAM" and reserve it for later use. On crash dump kernel, memory region information in system kernel is described in a specific region specified by "elfcorehdr=X@Y" boot parameter. reserve_elfcorehdr() will set aside the region to avoid data destruction by the kernel. Crash dump kernel will access memory regions in system kernel via copy_oldmem_page(), which reads a page by ioremap'ing it assuming that such pages are not part of main memory of crash dump kernel. This is true under non-UEFI environment because kexec-tools modifies a device tree adding "usablemem" attributes to memory sections. Under UEFI, however, this is not true because UEFI remove memory sections in a device tree and export all the memory regions, even though they belong to system kernel. So we should add "mem=X[MG]" boot parameter to limit the memory size and avoid hitting the following assertion in ioremap(): if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr)))) return NULL; Signed-off-by: AKASHI Takahiro --- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/crash_dump.c | 71 +++++++++++++++++++++++++++++++++ arch/arm64/kernel/setup.c | 8 +++- arch/arm64/mm/init.c | 84 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/crash_dump.c diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index ac3c2e2..6fcc602 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -36,6 +36,7 @@ arm64-obj-$(CONFIG_EFI) += efi.o efi-stub.o efi-entry.o arm64-obj-$(CONFIG_PCI) += pci.o arm64-obj-$(CONFIG_ARMV8_DEPRECATED) += armv8_deprecated.o arm64-obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o +arm64-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o obj-y += $(arm64-obj-y) vdso/ obj-m += $(arm64-obj-m) diff --git a/arch/arm64/kernel/crash_dump.c b/arch/arm64/kernel/crash_dump.c new file mode 100644 index 0000000..3d86c0a --- /dev/null +++ b/arch/arm64/kernel/crash_dump.c @@ -0,0 +1,71 @@ +/* + * Routines for doing kexec-based kdump + * + * Copyright (C) 2014 Linaro Limited + * Author: AKASHI Takahiro + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include + +/** + * copy_oldmem_page() - copy one page from old kernel memory + * @pfn: page frame number to be copied + * @buf: buffer where the copied page is placed + * @csize: number of bytes to copy + * @offset: offset in bytes into the page + * @userbuf: if set, @buf is in a user address space + * + * This function copies one page from old kernel memory into buffer pointed by + * @buf. If @buf is in userspace, set @userbuf to %1. Returns number of bytes + * copied or negative error in case of failure. + */ +ssize_t copy_oldmem_page(unsigned long pfn, char *buf, + size_t csize, unsigned long offset, + int userbuf) +{ + void *vaddr; + + if (!csize) + return 0; + + vaddr = ioremap_cache(pfn << PAGE_SHIFT, PAGE_SIZE); + if (!vaddr) + return -ENOMEM; + + if (userbuf) { + if (copy_to_user(buf, vaddr + offset, csize)) { + iounmap(vaddr); + return -EFAULT; + } + } else { + memcpy(buf, vaddr + offset, csize); + } + + iounmap(vaddr); + + return csize; +} + +/** + * elfcorehdr_read - read from ELF core header + * @buf: buffer where the data is placed + * @csize: number of bytes to read + * @ppos: address in the memory + * + * This function reads @count bytes from elf core header which exists + * on crash dump kernel's memory. + */ +ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos) +{ + memcpy(buf, phys_to_virt((phys_addr_t)*ppos), count); + return count; +} diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index 51ef972..7932bd0 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -364,6 +363,12 @@ static void __init request_standard_resources(void) kernel_data.end <= res->end) request_resource(res, &kernel_data); } + +#ifdef CONFIG_KEXEC + /* User space tools will detect the region with /proc/iomem */ + if (crashk_res.end) + insert_resource(&iomem_resource, &crashk_res); +#endif } u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID }; @@ -393,6 +398,7 @@ void __init setup_arch(char **cmdline_p) local_async_enable(); efi_init(); + arm64_memblock_init(); paging_init(); diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index ae85da6..ea70d41 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include #include @@ -66,6 +68,81 @@ static int __init early_initrd(char *p) early_param("initrd", early_initrd); #endif +#ifdef CONFIG_KEXEC +/* + * reserve_crashkernel() - reserves memory for crash kernel + * + * This function reserves memory area given in "crashkernel=" kernel command + * line parameter. The memory reserved is used by a dump capture kernel when + * primary kernel is crashing. + */ +static void __init reserve_crashkernel(phys_addr_t limit) +{ + unsigned long long crash_size = 0, crash_base = 0; + int ret; + + ret = parse_crashkernel(boot_command_line, limit, + &crash_size, &crash_base); + if (ret) + return; + + if (crash_base == 0) { + crash_base = memblock_alloc(crash_size, 1 << 20); + if (crash_base == 0) { + pr_warn("crashkernel allocation failed (size:%llx)\n", + crash_size); + return; + } + } else { + /* User specifies base address explicitly. Sanity check */ + if (!memblock_is_region_memory(crash_base, crash_size) || + memblock_is_region_reserved(crash_base, crash_size)) { + pr_warn("crashkernel= has wrong address or size\n"); + return; + } + + if (memblock_reserve(crash_base, crash_size)) { + pr_warn("crashkernel reservation failed - out of memory\n"); + return; + } + } + + pr_info("Reserving %lldMB of memory at %lldMB for crashkernel\n", + crash_size >> 20, crash_base >> 20); + + crashk_res.start = crash_base; + crashk_res.end = crash_base + crash_size - 1; +} +#endif /* CONFIG_KEXEC */ + +#ifdef CONFIG_CRASH_DUMP +/* + * reserve_elfcorehdr() - reserves memory for elf core header + * + * This function reserves memory area given in "elfcorehdr=" kernel command + * line parameter. The memory reserved is used by a dump capture kernel to + * identify the memory used by primary kernel. + */ +static void __init reserve_elfcorehdr(void) +{ + if (!elfcorehdr_size) + return; + + if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) { + pr_warn("elfcorehdr reservation failed - memory is in use (0x%llx)\n", + elfcorehdr_addr); + return; + } + + if (memblock_reserve(elfcorehdr_addr, elfcorehdr_size)) { + pr_warn("elfcorehdr reservation failed - out of memory\n"); + return; + } + + pr_info("Reserving %lldKB of memory at %lldMB for elfcorehdr\n", + elfcorehdr_size >> 10, elfcorehdr_addr >> 20); +} +#endif /* CONFIG_CRASH_DUMP */ /* * Return the maximum physical address for ZONE_DMA (DMA_BIT_MASK(32)). It * currently assumes that for memory starting above 4G, 32-bit devices will @@ -170,6 +247,13 @@ void __init arm64_memblock_init(void) memblock_reserve(__virt_to_phys(initrd_start), initrd_end - initrd_start); #endif +#ifdef CONFIG_KEXEC + reserve_crashkernel(memory_limit); +#endif +#ifdef CONFIG_CRASH_DUMP + reserve_elfcorehdr(); +#endif + early_init_fdt_scan_reserved_mem(); /* 4GB maximum for 32-bit only capable devices */