140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2692f66f2SHari Bathini /* 3692f66f2SHari Bathini * crash.c - kernel crash support code. 4692f66f2SHari Bathini * Copyright (C) 2002-2004 Eric Biederman <[email protected]> 5692f66f2SHari Bathini */ 6692f66f2SHari Bathini 744e8a5e9SStephen Boyd #include <linux/buildid.h> 871d2bcecSPhilipp Rudo #include <linux/init.h> 9692f66f2SHari Bathini #include <linux/utsname.h> 10692f66f2SHari Bathini #include <linux/vmalloc.h> 1146d36b1bSTao Liu #include <linux/sizes.h> 126f991cc3SEric DeVolder #include <linux/kexec.h> 1324726275SEric DeVolder #include <linux/memory.h> 1424726275SEric DeVolder #include <linux/cpuhotplug.h> 150ab97169SBaoquan He #include <linux/memblock.h> 160ab97169SBaoquan He #include <linux/kmemleak.h> 17692f66f2SHari Bathini 18692f66f2SHari Bathini #include <asm/page.h> 19692f66f2SHari Bathini #include <asm/sections.h> 20692f66f2SHari Bathini 21a24d22b2SEric Biggers #include <crypto/sha1.h> 220935288cSVijay Balakrishna 235fd8fea9SStephen Brennan #include "kallsyms_internal.h" 2424726275SEric DeVolder #include "kexec_internal.h" 255fd8fea9SStephen Brennan 266f991cc3SEric DeVolder /* Per cpu memory for storing cpu states in case of system crash. */ 276f991cc3SEric DeVolder note_buf_t __percpu *crash_notes; 28692f66f2SHari Bathini 29692f66f2SHari Bathini /* vmcoreinfo stuff */ 3023c85094SOmar Sandoval unsigned char *vmcoreinfo_data; 3123c85094SOmar Sandoval size_t vmcoreinfo_size; 32203e9e41SXunlei Pang u32 *vmcoreinfo_note; 33692f66f2SHari Bathini 341229384fSXunlei Pang /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */ 351229384fSXunlei Pang static unsigned char *vmcoreinfo_data_safecopy; 361229384fSXunlei Pang 37b631b95dSBaoquan He /* Location of the reserved area for the crash kernel */ 38b631b95dSBaoquan He struct resource crashk_res = { 39b631b95dSBaoquan He .name = "Crash kernel", 40b631b95dSBaoquan He .start = 0, 41b631b95dSBaoquan He .end = 0, 42b631b95dSBaoquan He .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 43b631b95dSBaoquan He .desc = IORES_DESC_CRASH_KERNEL 44b631b95dSBaoquan He }; 45b631b95dSBaoquan He struct resource crashk_low_res = { 46b631b95dSBaoquan He .name = "Crash kernel", 47b631b95dSBaoquan He .start = 0, 48b631b95dSBaoquan He .end = 0, 49b631b95dSBaoquan He .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM, 50b631b95dSBaoquan He .desc = IORES_DESC_CRASH_KERNEL 51b631b95dSBaoquan He }; 52b631b95dSBaoquan He 53692f66f2SHari Bathini /* 54692f66f2SHari Bathini * parsing the "crashkernel" commandline 55692f66f2SHari Bathini * 56692f66f2SHari Bathini * this code is intended to be called from architecture specific code 57692f66f2SHari Bathini */ 58692f66f2SHari Bathini 59692f66f2SHari Bathini 60692f66f2SHari Bathini /* 61692f66f2SHari Bathini * This function parses command lines in the format 62692f66f2SHari Bathini * 63692f66f2SHari Bathini * crashkernel=ramsize-range:size[,...][@offset] 64692f66f2SHari Bathini * 65692f66f2SHari Bathini * The function returns 0 on success and -EINVAL on failure. 66692f66f2SHari Bathini */ 67692f66f2SHari Bathini static int __init parse_crashkernel_mem(char *cmdline, 68692f66f2SHari Bathini unsigned long long system_ram, 69692f66f2SHari Bathini unsigned long long *crash_size, 70692f66f2SHari Bathini unsigned long long *crash_base) 71692f66f2SHari Bathini { 72692f66f2SHari Bathini char *cur = cmdline, *tmp; 7346d36b1bSTao Liu unsigned long long total_mem = system_ram; 7446d36b1bSTao Liu 7546d36b1bSTao Liu /* 7646d36b1bSTao Liu * Firmware sometimes reserves some memory regions for its own use, 7746d36b1bSTao Liu * so the system memory size is less than the actual physical memory 7846d36b1bSTao Liu * size. Work around this by rounding up the total size to 128M, 7946d36b1bSTao Liu * which is enough for most test cases. 8046d36b1bSTao Liu */ 8146d36b1bSTao Liu total_mem = roundup(total_mem, SZ_128M); 82692f66f2SHari Bathini 83692f66f2SHari Bathini /* for each entry of the comma-separated list */ 84692f66f2SHari Bathini do { 85692f66f2SHari Bathini unsigned long long start, end = ULLONG_MAX, size; 86692f66f2SHari Bathini 87692f66f2SHari Bathini /* get the start of the range */ 88692f66f2SHari Bathini start = memparse(cur, &tmp); 89692f66f2SHari Bathini if (cur == tmp) { 90692f66f2SHari Bathini pr_warn("crashkernel: Memory value expected\n"); 91692f66f2SHari Bathini return -EINVAL; 92692f66f2SHari Bathini } 93692f66f2SHari Bathini cur = tmp; 94692f66f2SHari Bathini if (*cur != '-') { 95692f66f2SHari Bathini pr_warn("crashkernel: '-' expected\n"); 96692f66f2SHari Bathini return -EINVAL; 97692f66f2SHari Bathini } 98692f66f2SHari Bathini cur++; 99692f66f2SHari Bathini 100692f66f2SHari Bathini /* if no ':' is here, than we read the end */ 101692f66f2SHari Bathini if (*cur != ':') { 102692f66f2SHari Bathini end = memparse(cur, &tmp); 103692f66f2SHari Bathini if (cur == tmp) { 104692f66f2SHari Bathini pr_warn("crashkernel: Memory value expected\n"); 105692f66f2SHari Bathini return -EINVAL; 106692f66f2SHari Bathini } 107692f66f2SHari Bathini cur = tmp; 108692f66f2SHari Bathini if (end <= start) { 109692f66f2SHari Bathini pr_warn("crashkernel: end <= start\n"); 110692f66f2SHari Bathini return -EINVAL; 111692f66f2SHari Bathini } 112692f66f2SHari Bathini } 113692f66f2SHari Bathini 114692f66f2SHari Bathini if (*cur != ':') { 115692f66f2SHari Bathini pr_warn("crashkernel: ':' expected\n"); 116692f66f2SHari Bathini return -EINVAL; 117692f66f2SHari Bathini } 118692f66f2SHari Bathini cur++; 119692f66f2SHari Bathini 120692f66f2SHari Bathini size = memparse(cur, &tmp); 121692f66f2SHari Bathini if (cur == tmp) { 122692f66f2SHari Bathini pr_warn("Memory value expected\n"); 123692f66f2SHari Bathini return -EINVAL; 124692f66f2SHari Bathini } 125692f66f2SHari Bathini cur = tmp; 12646d36b1bSTao Liu if (size >= total_mem) { 127692f66f2SHari Bathini pr_warn("crashkernel: invalid size\n"); 128692f66f2SHari Bathini return -EINVAL; 129692f66f2SHari Bathini } 130692f66f2SHari Bathini 131692f66f2SHari Bathini /* match ? */ 13246d36b1bSTao Liu if (total_mem >= start && total_mem < end) { 133692f66f2SHari Bathini *crash_size = size; 134692f66f2SHari Bathini break; 135692f66f2SHari Bathini } 136692f66f2SHari Bathini } while (*cur++ == ','); 137692f66f2SHari Bathini 138692f66f2SHari Bathini if (*crash_size > 0) { 139692f66f2SHari Bathini while (*cur && *cur != ' ' && *cur != '@') 140692f66f2SHari Bathini cur++; 141692f66f2SHari Bathini if (*cur == '@') { 142692f66f2SHari Bathini cur++; 143692f66f2SHari Bathini *crash_base = memparse(cur, &tmp); 144692f66f2SHari Bathini if (cur == tmp) { 145692f66f2SHari Bathini pr_warn("Memory value expected after '@'\n"); 146692f66f2SHari Bathini return -EINVAL; 147692f66f2SHari Bathini } 148692f66f2SHari Bathini } 149de40ccefSDave Young } else 150de40ccefSDave Young pr_info("crashkernel size resulted in zero bytes\n"); 151692f66f2SHari Bathini 152692f66f2SHari Bathini return 0; 153692f66f2SHari Bathini } 154692f66f2SHari Bathini 155692f66f2SHari Bathini /* 156692f66f2SHari Bathini * That function parses "simple" (old) crashkernel command lines like 157692f66f2SHari Bathini * 158692f66f2SHari Bathini * crashkernel=size[@offset] 159692f66f2SHari Bathini * 160692f66f2SHari Bathini * It returns 0 on success and -EINVAL on failure. 161692f66f2SHari Bathini */ 162692f66f2SHari Bathini static int __init parse_crashkernel_simple(char *cmdline, 163692f66f2SHari Bathini unsigned long long *crash_size, 164692f66f2SHari Bathini unsigned long long *crash_base) 165692f66f2SHari Bathini { 166692f66f2SHari Bathini char *cur = cmdline; 167692f66f2SHari Bathini 168692f66f2SHari Bathini *crash_size = memparse(cmdline, &cur); 169692f66f2SHari Bathini if (cmdline == cur) { 170692f66f2SHari Bathini pr_warn("crashkernel: memory value expected\n"); 171692f66f2SHari Bathini return -EINVAL; 172692f66f2SHari Bathini } 173692f66f2SHari Bathini 174692f66f2SHari Bathini if (*cur == '@') 175692f66f2SHari Bathini *crash_base = memparse(cur+1, &cur); 176692f66f2SHari Bathini else if (*cur != ' ' && *cur != '\0') { 177692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 178692f66f2SHari Bathini return -EINVAL; 179692f66f2SHari Bathini } 180692f66f2SHari Bathini 181692f66f2SHari Bathini return 0; 182692f66f2SHari Bathini } 183692f66f2SHari Bathini 184692f66f2SHari Bathini #define SUFFIX_HIGH 0 185692f66f2SHari Bathini #define SUFFIX_LOW 1 186692f66f2SHari Bathini #define SUFFIX_NULL 2 187692f66f2SHari Bathini static __initdata char *suffix_tbl[] = { 188692f66f2SHari Bathini [SUFFIX_HIGH] = ",high", 189692f66f2SHari Bathini [SUFFIX_LOW] = ",low", 190692f66f2SHari Bathini [SUFFIX_NULL] = NULL, 191692f66f2SHari Bathini }; 192692f66f2SHari Bathini 193692f66f2SHari Bathini /* 194692f66f2SHari Bathini * That function parses "suffix" crashkernel command lines like 195692f66f2SHari Bathini * 196692f66f2SHari Bathini * crashkernel=size,[high|low] 197692f66f2SHari Bathini * 198692f66f2SHari Bathini * It returns 0 on success and -EINVAL on failure. 199692f66f2SHari Bathini */ 200692f66f2SHari Bathini static int __init parse_crashkernel_suffix(char *cmdline, 201692f66f2SHari Bathini unsigned long long *crash_size, 202692f66f2SHari Bathini const char *suffix) 203692f66f2SHari Bathini { 204692f66f2SHari Bathini char *cur = cmdline; 205692f66f2SHari Bathini 206692f66f2SHari Bathini *crash_size = memparse(cmdline, &cur); 207692f66f2SHari Bathini if (cmdline == cur) { 208692f66f2SHari Bathini pr_warn("crashkernel: memory value expected\n"); 209692f66f2SHari Bathini return -EINVAL; 210692f66f2SHari Bathini } 211692f66f2SHari Bathini 212692f66f2SHari Bathini /* check with suffix */ 213692f66f2SHari Bathini if (strncmp(cur, suffix, strlen(suffix))) { 214692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 215692f66f2SHari Bathini return -EINVAL; 216692f66f2SHari Bathini } 217692f66f2SHari Bathini cur += strlen(suffix); 218692f66f2SHari Bathini if (*cur != ' ' && *cur != '\0') { 219692f66f2SHari Bathini pr_warn("crashkernel: unrecognized char: %c\n", *cur); 220692f66f2SHari Bathini return -EINVAL; 221692f66f2SHari Bathini } 222692f66f2SHari Bathini 223692f66f2SHari Bathini return 0; 224692f66f2SHari Bathini } 225692f66f2SHari Bathini 226692f66f2SHari Bathini static __init char *get_last_crashkernel(char *cmdline, 227692f66f2SHari Bathini const char *name, 228692f66f2SHari Bathini const char *suffix) 229692f66f2SHari Bathini { 230692f66f2SHari Bathini char *p = cmdline, *ck_cmdline = NULL; 231692f66f2SHari Bathini 232692f66f2SHari Bathini /* find crashkernel and use the last one if there are more */ 233692f66f2SHari Bathini p = strstr(p, name); 234692f66f2SHari Bathini while (p) { 235692f66f2SHari Bathini char *end_p = strchr(p, ' '); 236692f66f2SHari Bathini char *q; 237692f66f2SHari Bathini 238692f66f2SHari Bathini if (!end_p) 239692f66f2SHari Bathini end_p = p + strlen(p); 240692f66f2SHari Bathini 241692f66f2SHari Bathini if (!suffix) { 242692f66f2SHari Bathini int i; 243692f66f2SHari Bathini 244692f66f2SHari Bathini /* skip the one with any known suffix */ 245692f66f2SHari Bathini for (i = 0; suffix_tbl[i]; i++) { 246692f66f2SHari Bathini q = end_p - strlen(suffix_tbl[i]); 247692f66f2SHari Bathini if (!strncmp(q, suffix_tbl[i], 248692f66f2SHari Bathini strlen(suffix_tbl[i]))) 249692f66f2SHari Bathini goto next; 250692f66f2SHari Bathini } 251692f66f2SHari Bathini ck_cmdline = p; 252692f66f2SHari Bathini } else { 253692f66f2SHari Bathini q = end_p - strlen(suffix); 254692f66f2SHari Bathini if (!strncmp(q, suffix, strlen(suffix))) 255692f66f2SHari Bathini ck_cmdline = p; 256692f66f2SHari Bathini } 257692f66f2SHari Bathini next: 258692f66f2SHari Bathini p = strstr(p+1, name); 259692f66f2SHari Bathini } 260692f66f2SHari Bathini 261692f66f2SHari Bathini return ck_cmdline; 262692f66f2SHari Bathini } 263692f66f2SHari Bathini 264692f66f2SHari Bathini static int __init __parse_crashkernel(char *cmdline, 265692f66f2SHari Bathini unsigned long long system_ram, 266692f66f2SHari Bathini unsigned long long *crash_size, 267692f66f2SHari Bathini unsigned long long *crash_base, 268692f66f2SHari Bathini const char *suffix) 269692f66f2SHari Bathini { 270692f66f2SHari Bathini char *first_colon, *first_space; 271692f66f2SHari Bathini char *ck_cmdline; 272a6304272SBaoquan He char *name = "crashkernel="; 273692f66f2SHari Bathini 274692f66f2SHari Bathini BUG_ON(!crash_size || !crash_base); 275692f66f2SHari Bathini *crash_size = 0; 276692f66f2SHari Bathini *crash_base = 0; 277692f66f2SHari Bathini 278692f66f2SHari Bathini ck_cmdline = get_last_crashkernel(cmdline, name, suffix); 279692f66f2SHari Bathini if (!ck_cmdline) 2802e5920bbSZhen Lei return -ENOENT; 281692f66f2SHari Bathini 282692f66f2SHari Bathini ck_cmdline += strlen(name); 283692f66f2SHari Bathini 284692f66f2SHari Bathini if (suffix) 285692f66f2SHari Bathini return parse_crashkernel_suffix(ck_cmdline, crash_size, 286692f66f2SHari Bathini suffix); 287692f66f2SHari Bathini /* 288692f66f2SHari Bathini * if the commandline contains a ':', then that's the extended 289692f66f2SHari Bathini * syntax -- if not, it must be the classic syntax 290692f66f2SHari Bathini */ 291692f66f2SHari Bathini first_colon = strchr(ck_cmdline, ':'); 292692f66f2SHari Bathini first_space = strchr(ck_cmdline, ' '); 293692f66f2SHari Bathini if (first_colon && (!first_space || first_colon < first_space)) 294692f66f2SHari Bathini return parse_crashkernel_mem(ck_cmdline, system_ram, 295692f66f2SHari Bathini crash_size, crash_base); 296692f66f2SHari Bathini 297692f66f2SHari Bathini return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base); 298692f66f2SHari Bathini } 299692f66f2SHari Bathini 300692f66f2SHari Bathini /* 301692f66f2SHari Bathini * That function is the entry point for command line parsing and should be 302692f66f2SHari Bathini * called from the arch-specific code. 30370916e9cSBaoquan He * 30470916e9cSBaoquan He * If crashkernel=,high|low is supported on architecture, non-NULL values 30570916e9cSBaoquan He * should be passed to parameters 'low_size' and 'high'. 306692f66f2SHari Bathini */ 307692f66f2SHari Bathini int __init parse_crashkernel(char *cmdline, 308692f66f2SHari Bathini unsigned long long system_ram, 309692f66f2SHari Bathini unsigned long long *crash_size, 310a9e1a3d8SBaoquan He unsigned long long *crash_base, 311a9e1a3d8SBaoquan He unsigned long long *low_size, 312a9e1a3d8SBaoquan He bool *high) 313692f66f2SHari Bathini { 314a9e1a3d8SBaoquan He int ret; 315a9e1a3d8SBaoquan He 316a9e1a3d8SBaoquan He /* crashkernel=X[@offset] */ 317a9e1a3d8SBaoquan He ret = __parse_crashkernel(cmdline, system_ram, crash_size, 318a9e1a3d8SBaoquan He crash_base, NULL); 31970916e9cSBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 32070916e9cSBaoquan He /* 32170916e9cSBaoquan He * If non-NULL 'high' passed in and no normal crashkernel 32270916e9cSBaoquan He * setting detected, try parsing crashkernel=,high|low. 32370916e9cSBaoquan He */ 32470916e9cSBaoquan He if (high && ret == -ENOENT) { 32570916e9cSBaoquan He ret = __parse_crashkernel(cmdline, 0, crash_size, 32670916e9cSBaoquan He crash_base, suffix_tbl[SUFFIX_HIGH]); 32770916e9cSBaoquan He if (ret || !*crash_size) 32870916e9cSBaoquan He return -EINVAL; 329a9e1a3d8SBaoquan He 33070916e9cSBaoquan He /* 33170916e9cSBaoquan He * crashkernel=Y,low can be specified or not, but invalid value 33270916e9cSBaoquan He * is not allowed. 33370916e9cSBaoquan He */ 33470916e9cSBaoquan He ret = __parse_crashkernel(cmdline, 0, low_size, 33570916e9cSBaoquan He crash_base, suffix_tbl[SUFFIX_LOW]); 33670916e9cSBaoquan He if (ret == -ENOENT) { 33770916e9cSBaoquan He *low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 33870916e9cSBaoquan He ret = 0; 33970916e9cSBaoquan He } else if (ret) { 34070916e9cSBaoquan He return ret; 34170916e9cSBaoquan He } 34270916e9cSBaoquan He 34370916e9cSBaoquan He *high = true; 34470916e9cSBaoquan He } 34570916e9cSBaoquan He #endif 34670916e9cSBaoquan He if (!*crash_size) 34770916e9cSBaoquan He ret = -EINVAL; 34870916e9cSBaoquan He 34970916e9cSBaoquan He return ret; 350692f66f2SHari Bathini } 351692f66f2SHari Bathini 35271d2bcecSPhilipp Rudo /* 35371d2bcecSPhilipp Rudo * Add a dummy early_param handler to mark crashkernel= as a known command line 35471d2bcecSPhilipp Rudo * parameter and suppress incorrect warnings in init/main.c. 35571d2bcecSPhilipp Rudo */ 35671d2bcecSPhilipp Rudo static int __init parse_crashkernel_dummy(char *arg) 35771d2bcecSPhilipp Rudo { 35871d2bcecSPhilipp Rudo return 0; 35971d2bcecSPhilipp Rudo } 36071d2bcecSPhilipp Rudo early_param("crashkernel", parse_crashkernel_dummy); 36171d2bcecSPhilipp Rudo 3620ab97169SBaoquan He #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION 3630ab97169SBaoquan He static int __init reserve_crashkernel_low(unsigned long long low_size) 3640ab97169SBaoquan He { 3650ab97169SBaoquan He #ifdef CONFIG_64BIT 3660ab97169SBaoquan He unsigned long long low_base; 3670ab97169SBaoquan He 3680ab97169SBaoquan He low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX); 3690ab97169SBaoquan He if (!low_base) { 3700ab97169SBaoquan He pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size); 3710ab97169SBaoquan He return -ENOMEM; 3720ab97169SBaoquan He } 3730ab97169SBaoquan He 3740ab97169SBaoquan He pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n", 3750ab97169SBaoquan He low_base, low_base + low_size, low_size >> 20); 3760ab97169SBaoquan He 3770ab97169SBaoquan He crashk_low_res.start = low_base; 3780ab97169SBaoquan He crashk_low_res.end = low_base + low_size - 1; 3790ab97169SBaoquan He insert_resource(&iomem_resource, &crashk_low_res); 3800ab97169SBaoquan He #endif 3810ab97169SBaoquan He return 0; 3820ab97169SBaoquan He } 3830ab97169SBaoquan He 3840ab97169SBaoquan He void __init reserve_crashkernel_generic(char *cmdline, 3850ab97169SBaoquan He unsigned long long crash_size, 3860ab97169SBaoquan He unsigned long long crash_base, 3870ab97169SBaoquan He unsigned long long crash_low_size, 3880ab97169SBaoquan He bool high) 3890ab97169SBaoquan He { 3900ab97169SBaoquan He unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0; 3910ab97169SBaoquan He bool fixed_base = false; 3920ab97169SBaoquan He 3930ab97169SBaoquan He /* User specifies base address explicitly. */ 3940ab97169SBaoquan He if (crash_base) { 3950ab97169SBaoquan He fixed_base = true; 3960ab97169SBaoquan He search_base = crash_base; 3970ab97169SBaoquan He search_end = crash_base + crash_size; 3980ab97169SBaoquan He } else if (high) { 3990ab97169SBaoquan He search_base = CRASH_ADDR_LOW_MAX; 4000ab97169SBaoquan He search_end = CRASH_ADDR_HIGH_MAX; 4010ab97169SBaoquan He } 4020ab97169SBaoquan He 4030ab97169SBaoquan He retry: 4040ab97169SBaoquan He crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, 4050ab97169SBaoquan He search_base, search_end); 4060ab97169SBaoquan He if (!crash_base) { 4070ab97169SBaoquan He /* 4080ab97169SBaoquan He * For crashkernel=size[KMG]@offset[KMG], print out failure 4090ab97169SBaoquan He * message if can't reserve the specified region. 4100ab97169SBaoquan He */ 4110ab97169SBaoquan He if (fixed_base) { 4120ab97169SBaoquan He pr_warn("crashkernel reservation failed - memory is in use.\n"); 4130ab97169SBaoquan He return; 4140ab97169SBaoquan He } 4150ab97169SBaoquan He 4160ab97169SBaoquan He /* 4170ab97169SBaoquan He * For crashkernel=size[KMG], if the first attempt was for 4180ab97169SBaoquan He * low memory, fall back to high memory, the minimum required 4190ab97169SBaoquan He * low memory will be reserved later. 4200ab97169SBaoquan He */ 4210ab97169SBaoquan He if (!high && search_end == CRASH_ADDR_LOW_MAX) { 4220ab97169SBaoquan He search_end = CRASH_ADDR_HIGH_MAX; 4230ab97169SBaoquan He search_base = CRASH_ADDR_LOW_MAX; 4240ab97169SBaoquan He crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE; 4250ab97169SBaoquan He goto retry; 4260ab97169SBaoquan He } 4270ab97169SBaoquan He 4280ab97169SBaoquan He /* 4290ab97169SBaoquan He * For crashkernel=size[KMG],high, if the first attempt was 4300ab97169SBaoquan He * for high memory, fall back to low memory. 4310ab97169SBaoquan He */ 4320ab97169SBaoquan He if (high && search_end == CRASH_ADDR_HIGH_MAX) { 4330ab97169SBaoquan He search_end = CRASH_ADDR_LOW_MAX; 4340ab97169SBaoquan He search_base = 0; 4350ab97169SBaoquan He goto retry; 4360ab97169SBaoquan He } 4370ab97169SBaoquan He pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 4380ab97169SBaoquan He crash_size); 4390ab97169SBaoquan He return; 4400ab97169SBaoquan He } 4410ab97169SBaoquan He 4421dd11e97SYuntao Wang if ((crash_base >= CRASH_ADDR_LOW_MAX) && 4430ab97169SBaoquan He crash_low_size && reserve_crashkernel_low(crash_low_size)) { 4440ab97169SBaoquan He memblock_phys_free(crash_base, crash_size); 4450ab97169SBaoquan He return; 4460ab97169SBaoquan He } 4470ab97169SBaoquan He 4480ab97169SBaoquan He pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 4490ab97169SBaoquan He crash_base, crash_base + crash_size, crash_size >> 20); 4500ab97169SBaoquan He 4510ab97169SBaoquan He /* 4520ab97169SBaoquan He * The crashkernel memory will be removed from the kernel linear 4530ab97169SBaoquan He * map. Inform kmemleak so that it won't try to access it. 4540ab97169SBaoquan He */ 4550ab97169SBaoquan He kmemleak_ignore_phys(crash_base); 4560ab97169SBaoquan He if (crashk_low_res.end) 4570ab97169SBaoquan He kmemleak_ignore_phys(crashk_low_res.start); 4580ab97169SBaoquan He 4590ab97169SBaoquan He crashk_res.start = crash_base; 4600ab97169SBaoquan He crashk_res.end = crash_base + crash_size - 1; 4610ab97169SBaoquan He insert_resource(&iomem_resource, &crashk_res); 4620ab97169SBaoquan He } 4630ab97169SBaoquan He #endif 4640ab97169SBaoquan He 4656f991cc3SEric DeVolder int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map, 4666f991cc3SEric DeVolder void **addr, unsigned long *sz) 4676f991cc3SEric DeVolder { 4686f991cc3SEric DeVolder Elf64_Ehdr *ehdr; 4696f991cc3SEric DeVolder Elf64_Phdr *phdr; 4706f991cc3SEric DeVolder unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 4716f991cc3SEric DeVolder unsigned char *buf; 4726f991cc3SEric DeVolder unsigned int cpu, i; 4736f991cc3SEric DeVolder unsigned long long notes_addr; 4746f991cc3SEric DeVolder unsigned long mstart, mend; 4756f991cc3SEric DeVolder 4766f991cc3SEric DeVolder /* extra phdr for vmcoreinfo ELF note */ 4776f991cc3SEric DeVolder nr_phdr = nr_cpus + 1; 4786f991cc3SEric DeVolder nr_phdr += mem->nr_ranges; 4796f991cc3SEric DeVolder 4806f991cc3SEric DeVolder /* 4816f991cc3SEric DeVolder * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 4826f991cc3SEric DeVolder * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). 4836f991cc3SEric DeVolder * I think this is required by tools like gdb. So same physical 4846f991cc3SEric DeVolder * memory will be mapped in two ELF headers. One will contain kernel 4856f991cc3SEric DeVolder * text virtual addresses and other will have __va(physical) addresses. 4866f991cc3SEric DeVolder */ 4876f991cc3SEric DeVolder 4886f991cc3SEric DeVolder nr_phdr++; 4896f991cc3SEric DeVolder elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 4906f991cc3SEric DeVolder elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 4916f991cc3SEric DeVolder 4926f991cc3SEric DeVolder buf = vzalloc(elf_sz); 4936f991cc3SEric DeVolder if (!buf) 4946f991cc3SEric DeVolder return -ENOMEM; 4956f991cc3SEric DeVolder 4966f991cc3SEric DeVolder ehdr = (Elf64_Ehdr *)buf; 4976f991cc3SEric DeVolder phdr = (Elf64_Phdr *)(ehdr + 1); 4986f991cc3SEric DeVolder memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 4996f991cc3SEric DeVolder ehdr->e_ident[EI_CLASS] = ELFCLASS64; 5006f991cc3SEric DeVolder ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 5016f991cc3SEric DeVolder ehdr->e_ident[EI_VERSION] = EV_CURRENT; 5026f991cc3SEric DeVolder ehdr->e_ident[EI_OSABI] = ELF_OSABI; 5036f991cc3SEric DeVolder memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 5046f991cc3SEric DeVolder ehdr->e_type = ET_CORE; 5056f991cc3SEric DeVolder ehdr->e_machine = ELF_ARCH; 5066f991cc3SEric DeVolder ehdr->e_version = EV_CURRENT; 5076f991cc3SEric DeVolder ehdr->e_phoff = sizeof(Elf64_Ehdr); 5086f991cc3SEric DeVolder ehdr->e_ehsize = sizeof(Elf64_Ehdr); 5096f991cc3SEric DeVolder ehdr->e_phentsize = sizeof(Elf64_Phdr); 5106f991cc3SEric DeVolder 511a396d0f8SEric DeVolder /* Prepare one phdr of type PT_NOTE for each possible CPU */ 512a396d0f8SEric DeVolder for_each_possible_cpu(cpu) { 5136f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 5146f991cc3SEric DeVolder notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 5156f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = notes_addr; 5166f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 5176f991cc3SEric DeVolder (ehdr->e_phnum)++; 5186f991cc3SEric DeVolder phdr++; 5196f991cc3SEric DeVolder } 5206f991cc3SEric DeVolder 5216f991cc3SEric DeVolder /* Prepare one PT_NOTE header for vmcoreinfo */ 5226f991cc3SEric DeVolder phdr->p_type = PT_NOTE; 5236f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 5246f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 5256f991cc3SEric DeVolder (ehdr->e_phnum)++; 5266f991cc3SEric DeVolder phdr++; 5276f991cc3SEric DeVolder 5286f991cc3SEric DeVolder /* Prepare PT_LOAD type program header for kernel text region */ 5296f991cc3SEric DeVolder if (need_kernel_map) { 5306f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 5316f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 5326f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) _text; 5336f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = _end - _text; 5346f991cc3SEric DeVolder phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 5356f991cc3SEric DeVolder ehdr->e_phnum++; 5366f991cc3SEric DeVolder phdr++; 5376f991cc3SEric DeVolder } 5386f991cc3SEric DeVolder 5396f991cc3SEric DeVolder /* Go through all the ranges in mem->ranges[] and prepare phdr */ 5406f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 5416f991cc3SEric DeVolder mstart = mem->ranges[i].start; 5426f991cc3SEric DeVolder mend = mem->ranges[i].end; 5436f991cc3SEric DeVolder 5446f991cc3SEric DeVolder phdr->p_type = PT_LOAD; 5456f991cc3SEric DeVolder phdr->p_flags = PF_R|PF_W|PF_X; 5466f991cc3SEric DeVolder phdr->p_offset = mstart; 5476f991cc3SEric DeVolder 5486f991cc3SEric DeVolder phdr->p_paddr = mstart; 5496f991cc3SEric DeVolder phdr->p_vaddr = (unsigned long) __va(mstart); 5506f991cc3SEric DeVolder phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 5516f991cc3SEric DeVolder phdr->p_align = 0; 5526f991cc3SEric DeVolder ehdr->e_phnum++; 553a85ee18cSBaoquan He #ifdef CONFIG_KEXEC_FILE 554a85ee18cSBaoquan He kexec_dprintk("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", 5556f991cc3SEric DeVolder phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 5566f991cc3SEric DeVolder ehdr->e_phnum, phdr->p_offset); 557a85ee18cSBaoquan He #endif 5586f991cc3SEric DeVolder phdr++; 5596f991cc3SEric DeVolder } 5606f991cc3SEric DeVolder 5616f991cc3SEric DeVolder *addr = buf; 5626f991cc3SEric DeVolder *sz = elf_sz; 5636f991cc3SEric DeVolder return 0; 5646f991cc3SEric DeVolder } 5656f991cc3SEric DeVolder 5666f991cc3SEric DeVolder int crash_exclude_mem_range(struct crash_mem *mem, 5676f991cc3SEric DeVolder unsigned long long mstart, unsigned long long mend) 5686f991cc3SEric DeVolder { 569*6dff3159SYuntao Wang int i; 5706f991cc3SEric DeVolder unsigned long long start, end, p_start, p_end; 5716f991cc3SEric DeVolder 5726f991cc3SEric DeVolder for (i = 0; i < mem->nr_ranges; i++) { 5736f991cc3SEric DeVolder start = mem->ranges[i].start; 5746f991cc3SEric DeVolder end = mem->ranges[i].end; 5756f991cc3SEric DeVolder p_start = mstart; 5766f991cc3SEric DeVolder p_end = mend; 5776f991cc3SEric DeVolder 578*6dff3159SYuntao Wang if (p_start > end) 5796f991cc3SEric DeVolder continue; 5806f991cc3SEric DeVolder 581*6dff3159SYuntao Wang /* 582*6dff3159SYuntao Wang * Because the memory ranges in mem->ranges are stored in 583*6dff3159SYuntao Wang * ascending order, when we detect `p_end < start`, we can 584*6dff3159SYuntao Wang * immediately exit the for loop, as the subsequent memory 585*6dff3159SYuntao Wang * ranges will definitely be outside the range we are looking 586*6dff3159SYuntao Wang * for. 587*6dff3159SYuntao Wang */ 588*6dff3159SYuntao Wang if (p_end < start) 589*6dff3159SYuntao Wang break; 590*6dff3159SYuntao Wang 5916f991cc3SEric DeVolder /* Truncate any area outside of range */ 592*6dff3159SYuntao Wang if (p_start < start) 5936f991cc3SEric DeVolder p_start = start; 594*6dff3159SYuntao Wang if (p_end > end) 5956f991cc3SEric DeVolder p_end = end; 5966f991cc3SEric DeVolder 5976f991cc3SEric DeVolder /* Found completely overlapping range */ 5986f991cc3SEric DeVolder if (p_start == start && p_end == end) { 599*6dff3159SYuntao Wang memmove(&mem->ranges[i], &mem->ranges[i + 1], 600*6dff3159SYuntao Wang (mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i])); 6016f991cc3SEric DeVolder i--; 6026f991cc3SEric DeVolder mem->nr_ranges--; 603*6dff3159SYuntao Wang } else if (p_start > start && p_end < end) { 6046f991cc3SEric DeVolder /* Split original range */ 605*6dff3159SYuntao Wang if (mem->nr_ranges >= mem->max_nr_ranges) 606*6dff3159SYuntao Wang return -ENOMEM; 607*6dff3159SYuntao Wang 608*6dff3159SYuntao Wang memmove(&mem->ranges[i + 2], &mem->ranges[i + 1], 609*6dff3159SYuntao Wang (mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i])); 610*6dff3159SYuntao Wang 6116f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 612*6dff3159SYuntao Wang mem->ranges[i + 1].start = p_end + 1; 613*6dff3159SYuntao Wang mem->ranges[i + 1].end = end; 614*6dff3159SYuntao Wang 615*6dff3159SYuntao Wang i++; 616*6dff3159SYuntao Wang mem->nr_ranges++; 6176f991cc3SEric DeVolder } else if (p_start != start) 6186f991cc3SEric DeVolder mem->ranges[i].end = p_start - 1; 6196f991cc3SEric DeVolder else 6206f991cc3SEric DeVolder mem->ranges[i].start = p_end + 1; 6216f991cc3SEric DeVolder } 6226f991cc3SEric DeVolder 6236f991cc3SEric DeVolder return 0; 6246f991cc3SEric DeVolder } 6256f991cc3SEric DeVolder 62651dbd925SHari Bathini Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type, 627692f66f2SHari Bathini void *data, size_t data_len) 628692f66f2SHari Bathini { 62951dbd925SHari Bathini struct elf_note *note = (struct elf_note *)buf; 630692f66f2SHari Bathini 63151dbd925SHari Bathini note->n_namesz = strlen(name) + 1; 63251dbd925SHari Bathini note->n_descsz = data_len; 63351dbd925SHari Bathini note->n_type = type; 63451dbd925SHari Bathini buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word)); 63551dbd925SHari Bathini memcpy(buf, name, note->n_namesz); 63651dbd925SHari Bathini buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word)); 63751dbd925SHari Bathini memcpy(buf, data, data_len); 63851dbd925SHari Bathini buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word)); 639692f66f2SHari Bathini 640692f66f2SHari Bathini return buf; 641692f66f2SHari Bathini } 642692f66f2SHari Bathini 64351dbd925SHari Bathini void final_note(Elf_Word *buf) 644692f66f2SHari Bathini { 64551dbd925SHari Bathini memset(buf, 0, sizeof(struct elf_note)); 646692f66f2SHari Bathini } 647692f66f2SHari Bathini 648692f66f2SHari Bathini static void update_vmcoreinfo_note(void) 649692f66f2SHari Bathini { 650692f66f2SHari Bathini u32 *buf = vmcoreinfo_note; 651692f66f2SHari Bathini 652692f66f2SHari Bathini if (!vmcoreinfo_size) 653692f66f2SHari Bathini return; 654692f66f2SHari Bathini buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data, 655692f66f2SHari Bathini vmcoreinfo_size); 656692f66f2SHari Bathini final_note(buf); 657692f66f2SHari Bathini } 658692f66f2SHari Bathini 6591229384fSXunlei Pang void crash_update_vmcoreinfo_safecopy(void *ptr) 6601229384fSXunlei Pang { 6611229384fSXunlei Pang if (ptr) 6621229384fSXunlei Pang memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size); 6631229384fSXunlei Pang 6641229384fSXunlei Pang vmcoreinfo_data_safecopy = ptr; 6651229384fSXunlei Pang } 6661229384fSXunlei Pang 667692f66f2SHari Bathini void crash_save_vmcoreinfo(void) 668692f66f2SHari Bathini { 669203e9e41SXunlei Pang if (!vmcoreinfo_note) 670203e9e41SXunlei Pang return; 671203e9e41SXunlei Pang 6721229384fSXunlei Pang /* Use the safe copy to generate vmcoreinfo note if have */ 6731229384fSXunlei Pang if (vmcoreinfo_data_safecopy) 6741229384fSXunlei Pang vmcoreinfo_data = vmcoreinfo_data_safecopy; 6751229384fSXunlei Pang 67691bc9aafSArnd Bergmann vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds()); 677692f66f2SHari Bathini update_vmcoreinfo_note(); 678692f66f2SHari Bathini } 679692f66f2SHari Bathini 680692f66f2SHari Bathini void vmcoreinfo_append_str(const char *fmt, ...) 681692f66f2SHari Bathini { 682692f66f2SHari Bathini va_list args; 683692f66f2SHari Bathini char buf[0x50]; 684692f66f2SHari Bathini size_t r; 685692f66f2SHari Bathini 686692f66f2SHari Bathini va_start(args, fmt); 687692f66f2SHari Bathini r = vscnprintf(buf, sizeof(buf), fmt, args); 688692f66f2SHari Bathini va_end(args); 689692f66f2SHari Bathini 6905203f499SXunlei Pang r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size); 691692f66f2SHari Bathini 692692f66f2SHari Bathini memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r); 693692f66f2SHari Bathini 694692f66f2SHari Bathini vmcoreinfo_size += r; 69508fc35f3SStephen Brennan 69608fc35f3SStephen Brennan WARN_ONCE(vmcoreinfo_size == VMCOREINFO_BYTES, 69708fc35f3SStephen Brennan "vmcoreinfo data exceeds allocated size, truncating"); 698692f66f2SHari Bathini } 699692f66f2SHari Bathini 700692f66f2SHari Bathini /* 701692f66f2SHari Bathini * provide an empty default implementation here -- architecture 702692f66f2SHari Bathini * code may override this 703692f66f2SHari Bathini */ 704692f66f2SHari Bathini void __weak arch_crash_save_vmcoreinfo(void) 705692f66f2SHari Bathini {} 706692f66f2SHari Bathini 707692f66f2SHari Bathini phys_addr_t __weak paddr_vmcoreinfo_note(void) 708692f66f2SHari Bathini { 709203e9e41SXunlei Pang return __pa(vmcoreinfo_note); 710692f66f2SHari Bathini } 71143d4cb47SMarc-André Lureau EXPORT_SYMBOL(paddr_vmcoreinfo_note); 712692f66f2SHari Bathini 713692f66f2SHari Bathini static int __init crash_save_vmcoreinfo_init(void) 714692f66f2SHari Bathini { 715203e9e41SXunlei Pang vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL); 716203e9e41SXunlei Pang if (!vmcoreinfo_data) { 717203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_data failed\n"); 718203e9e41SXunlei Pang return -ENOMEM; 719203e9e41SXunlei Pang } 720203e9e41SXunlei Pang 721203e9e41SXunlei Pang vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE, 722203e9e41SXunlei Pang GFP_KERNEL | __GFP_ZERO); 723203e9e41SXunlei Pang if (!vmcoreinfo_note) { 724203e9e41SXunlei Pang free_page((unsigned long)vmcoreinfo_data); 725203e9e41SXunlei Pang vmcoreinfo_data = NULL; 726203e9e41SXunlei Pang pr_warn("Memory allocation for vmcoreinfo_note failed\n"); 727203e9e41SXunlei Pang return -ENOMEM; 728203e9e41SXunlei Pang } 729203e9e41SXunlei Pang 730692f66f2SHari Bathini VMCOREINFO_OSRELEASE(init_uts_ns.name.release); 73144e8a5e9SStephen Boyd VMCOREINFO_BUILD_ID(); 732692f66f2SHari Bathini VMCOREINFO_PAGESIZE(PAGE_SIZE); 733692f66f2SHari Bathini 734692f66f2SHari Bathini VMCOREINFO_SYMBOL(init_uts_ns); 735ca4a9241SAlexander Egorenkov VMCOREINFO_OFFSET(uts_namespace, name); 736692f66f2SHari Bathini VMCOREINFO_SYMBOL(node_online_map); 737692f66f2SHari Bathini #ifdef CONFIG_MMU 738eff4345eSOmar Sandoval VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir); 739692f66f2SHari Bathini #endif 740692f66f2SHari Bathini VMCOREINFO_SYMBOL(_stext); 741692f66f2SHari Bathini VMCOREINFO_SYMBOL(vmap_area_list); 742692f66f2SHari Bathini 743a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA 744692f66f2SHari Bathini VMCOREINFO_SYMBOL(mem_map); 745692f66f2SHari Bathini VMCOREINFO_SYMBOL(contig_page_data); 746692f66f2SHari Bathini #endif 747692f66f2SHari Bathini #ifdef CONFIG_SPARSEMEM 748a0b12803SKirill A. Shutemov VMCOREINFO_SYMBOL_ARRAY(mem_section); 749692f66f2SHari Bathini VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS); 750692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(mem_section); 751692f66f2SHari Bathini VMCOREINFO_OFFSET(mem_section, section_mem_map); 7524f5aecdfSPingfan Liu VMCOREINFO_NUMBER(SECTION_SIZE_BITS); 7531d50e5d0SBhupesh Sharma VMCOREINFO_NUMBER(MAX_PHYSMEM_BITS); 754692f66f2SHari Bathini #endif 755692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(page); 756692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(pglist_data); 757692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(zone); 758692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(free_area); 759692f66f2SHari Bathini VMCOREINFO_STRUCT_SIZE(list_head); 760692f66f2SHari Bathini VMCOREINFO_SIZE(nodemask_t); 761692f66f2SHari Bathini VMCOREINFO_OFFSET(page, flags); 762692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _refcount); 763692f66f2SHari Bathini VMCOREINFO_OFFSET(page, mapping); 764692f66f2SHari Bathini VMCOREINFO_OFFSET(page, lru); 765692f66f2SHari Bathini VMCOREINFO_OFFSET(page, _mapcount); 766692f66f2SHari Bathini VMCOREINFO_OFFSET(page, private); 767692f66f2SHari Bathini VMCOREINFO_OFFSET(page, compound_head); 768692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_zones); 769692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, nr_zones); 77043b02ba9SMike Rapoport #ifdef CONFIG_FLATMEM 771692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_mem_map); 772692f66f2SHari Bathini #endif 773692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_start_pfn); 774692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_spanned_pages); 775692f66f2SHari Bathini VMCOREINFO_OFFSET(pglist_data, node_id); 776692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, free_area); 777692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, vm_stat); 778692f66f2SHari Bathini VMCOREINFO_OFFSET(zone, spanned_pages); 779692f66f2SHari Bathini VMCOREINFO_OFFSET(free_area, free_list); 780692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, next); 781692f66f2SHari Bathini VMCOREINFO_OFFSET(list_head, prev); 782692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, va_start); 783692f66f2SHari Bathini VMCOREINFO_OFFSET(vmap_area, list); 78423baf831SKirill A. Shutemov VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER + 1); 785692f66f2SHari Bathini log_buf_vmcoreinfo_setup(); 786692f66f2SHari Bathini VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES); 787692f66f2SHari Bathini VMCOREINFO_NUMBER(NR_FREE_PAGES); 788692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_lru); 789692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_private); 790692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_swapcache); 7911cbf29daSPetr Tesarik VMCOREINFO_NUMBER(PG_swapbacked); 792692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_slab); 793692f66f2SHari Bathini #ifdef CONFIG_MEMORY_FAILURE 794692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_hwpoison); 795692f66f2SHari Bathini #endif 796692f66f2SHari Bathini VMCOREINFO_NUMBER(PG_head_mask); 7976e292b9bSMatthew Wilcox #define PAGE_BUDDY_MAPCOUNT_VALUE (~PG_buddy) 798692f66f2SHari Bathini VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE); 799692f66f2SHari Bathini #ifdef CONFIG_HUGETLB_PAGE 8009c5ccf2dSMatthew Wilcox (Oracle) VMCOREINFO_NUMBER(PG_hugetlb); 801e04b742fSDavid Hildenbrand #define PAGE_OFFLINE_MAPCOUNT_VALUE (~PG_offline) 802e04b742fSDavid Hildenbrand VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE); 803692f66f2SHari Bathini #endif 804692f66f2SHari Bathini 8055fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS 8065fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_names); 807f09bddbdSStephen Brennan VMCOREINFO_SYMBOL(kallsyms_num_syms); 8085fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_table); 8095fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_token_index); 8105fd8fea9SStephen Brennan #ifdef CONFIG_KALLSYMS_BASE_RELATIVE 8115fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_offsets); 8125fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_relative_base); 8135fd8fea9SStephen Brennan #else 8145fd8fea9SStephen Brennan VMCOREINFO_SYMBOL(kallsyms_addresses); 8155fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS_BASE_RELATIVE */ 8165fd8fea9SStephen Brennan #endif /* CONFIG_KALLSYMS */ 8175fd8fea9SStephen Brennan 818692f66f2SHari Bathini arch_crash_save_vmcoreinfo(); 819692f66f2SHari Bathini update_vmcoreinfo_note(); 820692f66f2SHari Bathini 821692f66f2SHari Bathini return 0; 822692f66f2SHari Bathini } 823692f66f2SHari Bathini 824692f66f2SHari Bathini subsys_initcall(crash_save_vmcoreinfo_init); 8256f991cc3SEric DeVolder 8266f991cc3SEric DeVolder static int __init crash_notes_memory_init(void) 8276f991cc3SEric DeVolder { 8286f991cc3SEric DeVolder /* Allocate memory for saving cpu registers. */ 8296f991cc3SEric DeVolder size_t size, align; 8306f991cc3SEric DeVolder 8316f991cc3SEric DeVolder /* 8326f991cc3SEric DeVolder * crash_notes could be allocated across 2 vmalloc pages when percpu 8336f991cc3SEric DeVolder * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc 8346f991cc3SEric DeVolder * pages are also on 2 continuous physical pages. In this case the 8356f991cc3SEric DeVolder * 2nd part of crash_notes in 2nd page could be lost since only the 8366f991cc3SEric DeVolder * starting address and size of crash_notes are exported through sysfs. 8376f991cc3SEric DeVolder * Here round up the size of crash_notes to the nearest power of two 8386f991cc3SEric DeVolder * and pass it to __alloc_percpu as align value. This can make sure 8396f991cc3SEric DeVolder * crash_notes is allocated inside one physical page. 8406f991cc3SEric DeVolder */ 8416f991cc3SEric DeVolder size = sizeof(note_buf_t); 8426f991cc3SEric DeVolder align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE); 8436f991cc3SEric DeVolder 8446f991cc3SEric DeVolder /* 8456f991cc3SEric DeVolder * Break compile if size is bigger than PAGE_SIZE since crash_notes 8466f991cc3SEric DeVolder * definitely will be in 2 pages with that. 8476f991cc3SEric DeVolder */ 8486f991cc3SEric DeVolder BUILD_BUG_ON(size > PAGE_SIZE); 8496f991cc3SEric DeVolder 8506f991cc3SEric DeVolder crash_notes = __alloc_percpu(size, align); 8516f991cc3SEric DeVolder if (!crash_notes) { 8526f991cc3SEric DeVolder pr_warn("Memory allocation for saving cpu register states failed\n"); 8536f991cc3SEric DeVolder return -ENOMEM; 8546f991cc3SEric DeVolder } 8556f991cc3SEric DeVolder return 0; 8566f991cc3SEric DeVolder } 8576f991cc3SEric DeVolder subsys_initcall(crash_notes_memory_init); 85824726275SEric DeVolder 85924726275SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG 86024726275SEric DeVolder #undef pr_fmt 86124726275SEric DeVolder #define pr_fmt(fmt) "crash hp: " fmt 862a72bbec7SEric DeVolder 863a72bbec7SEric DeVolder /* 864e2a8f20dSBaoquan He * Different than kexec/kdump loading/unloading/jumping/shrinking which 865e2a8f20dSBaoquan He * usually rarely happen, there will be many crash hotplug events notified 866e2a8f20dSBaoquan He * during one short period, e.g one memory board is hot added and memory 867e2a8f20dSBaoquan He * regions are online. So mutex lock __crash_hotplug_lock is used to 868e2a8f20dSBaoquan He * serialize the crash hotplug handling specifically. 869e2a8f20dSBaoquan He */ 870e2a8f20dSBaoquan He DEFINE_MUTEX(__crash_hotplug_lock); 871e2a8f20dSBaoquan He #define crash_hotplug_lock() mutex_lock(&__crash_hotplug_lock) 872e2a8f20dSBaoquan He #define crash_hotplug_unlock() mutex_unlock(&__crash_hotplug_lock) 873e2a8f20dSBaoquan He 874e2a8f20dSBaoquan He /* 875a72bbec7SEric DeVolder * This routine utilized when the crash_hotplug sysfs node is read. 876a72bbec7SEric DeVolder * It reflects the kernel's ability/permission to update the crash 877a72bbec7SEric DeVolder * elfcorehdr directly. 878a72bbec7SEric DeVolder */ 879a72bbec7SEric DeVolder int crash_check_update_elfcorehdr(void) 880a72bbec7SEric DeVolder { 881a72bbec7SEric DeVolder int rc = 0; 882a72bbec7SEric DeVolder 883e2a8f20dSBaoquan He crash_hotplug_lock(); 884a72bbec7SEric DeVolder /* Obtain lock while reading crash information */ 885a72bbec7SEric DeVolder if (!kexec_trylock()) { 886a72bbec7SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 887e2a8f20dSBaoquan He crash_hotplug_unlock(); 888a72bbec7SEric DeVolder return 0; 889a72bbec7SEric DeVolder } 890a72bbec7SEric DeVolder if (kexec_crash_image) { 891a72bbec7SEric DeVolder if (kexec_crash_image->file_mode) 892a72bbec7SEric DeVolder rc = 1; 893a72bbec7SEric DeVolder else 894a72bbec7SEric DeVolder rc = kexec_crash_image->update_elfcorehdr; 895a72bbec7SEric DeVolder } 896a72bbec7SEric DeVolder /* Release lock now that update complete */ 897a72bbec7SEric DeVolder kexec_unlock(); 898e2a8f20dSBaoquan He crash_hotplug_unlock(); 899a72bbec7SEric DeVolder 900a72bbec7SEric DeVolder return rc; 901a72bbec7SEric DeVolder } 902a72bbec7SEric DeVolder 90324726275SEric DeVolder /* 90424726275SEric DeVolder * To accurately reflect hot un/plug changes of cpu and memory resources 90524726275SEric DeVolder * (including onling and offlining of those resources), the elfcorehdr 90624726275SEric DeVolder * (which is passed to the crash kernel via the elfcorehdr= parameter) 90724726275SEric DeVolder * must be updated with the new list of CPUs and memories. 90824726275SEric DeVolder * 90924726275SEric DeVolder * In order to make changes to elfcorehdr, two conditions are needed: 91024726275SEric DeVolder * First, the segment containing the elfcorehdr must be large enough 91124726275SEric DeVolder * to permit a growing number of resources; the elfcorehdr memory size 91224726275SEric DeVolder * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES. 91324726275SEric DeVolder * Second, purgatory must explicitly exclude the elfcorehdr from the 91424726275SEric DeVolder * list of segments it checks (since the elfcorehdr changes and thus 91524726275SEric DeVolder * would require an update to purgatory itself to update the digest). 91624726275SEric DeVolder */ 91724726275SEric DeVolder static void crash_handle_hotplug_event(unsigned int hp_action, unsigned int cpu) 91824726275SEric DeVolder { 91924726275SEric DeVolder struct kimage *image; 92024726275SEric DeVolder 921e2a8f20dSBaoquan He crash_hotplug_lock(); 92224726275SEric DeVolder /* Obtain lock while changing crash information */ 92324726275SEric DeVolder if (!kexec_trylock()) { 92424726275SEric DeVolder pr_info("kexec_trylock() failed, elfcorehdr may be inaccurate\n"); 925e2a8f20dSBaoquan He crash_hotplug_unlock(); 92624726275SEric DeVolder return; 92724726275SEric DeVolder } 92824726275SEric DeVolder 92924726275SEric DeVolder /* Check kdump is not loaded */ 93024726275SEric DeVolder if (!kexec_crash_image) 93124726275SEric DeVolder goto out; 93224726275SEric DeVolder 93324726275SEric DeVolder image = kexec_crash_image; 93424726275SEric DeVolder 935a72bbec7SEric DeVolder /* Check that updating elfcorehdr is permitted */ 936a72bbec7SEric DeVolder if (!(image->file_mode || image->update_elfcorehdr)) 937a72bbec7SEric DeVolder goto out; 938a72bbec7SEric DeVolder 93924726275SEric DeVolder if (hp_action == KEXEC_CRASH_HP_ADD_CPU || 94024726275SEric DeVolder hp_action == KEXEC_CRASH_HP_REMOVE_CPU) 94124726275SEric DeVolder pr_debug("hp_action %u, cpu %u\n", hp_action, cpu); 94224726275SEric DeVolder else 94324726275SEric DeVolder pr_debug("hp_action %u\n", hp_action); 94424726275SEric DeVolder 94524726275SEric DeVolder /* 94624726275SEric DeVolder * The elfcorehdr_index is set to -1 when the struct kimage 94724726275SEric DeVolder * is allocated. Find the segment containing the elfcorehdr, 94824726275SEric DeVolder * if not already found. 94924726275SEric DeVolder */ 95024726275SEric DeVolder if (image->elfcorehdr_index < 0) { 95124726275SEric DeVolder unsigned long mem; 95224726275SEric DeVolder unsigned char *ptr; 95324726275SEric DeVolder unsigned int n; 95424726275SEric DeVolder 95524726275SEric DeVolder for (n = 0; n < image->nr_segments; n++) { 95624726275SEric DeVolder mem = image->segment[n].mem; 95724726275SEric DeVolder ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); 95824726275SEric DeVolder if (ptr) { 95924726275SEric DeVolder /* The segment containing elfcorehdr */ 96024726275SEric DeVolder if (memcmp(ptr, ELFMAG, SELFMAG) == 0) 96124726275SEric DeVolder image->elfcorehdr_index = (int)n; 96224726275SEric DeVolder kunmap_local(ptr); 96324726275SEric DeVolder } 96424726275SEric DeVolder } 96524726275SEric DeVolder } 96624726275SEric DeVolder 96724726275SEric DeVolder if (image->elfcorehdr_index < 0) { 96824726275SEric DeVolder pr_err("unable to locate elfcorehdr segment"); 96924726275SEric DeVolder goto out; 97024726275SEric DeVolder } 97124726275SEric DeVolder 97224726275SEric DeVolder /* Needed in order for the segments to be updated */ 97324726275SEric DeVolder arch_kexec_unprotect_crashkres(); 97424726275SEric DeVolder 97524726275SEric DeVolder /* Differentiate between normal load and hotplug update */ 97624726275SEric DeVolder image->hp_action = hp_action; 97724726275SEric DeVolder 97824726275SEric DeVolder /* Now invoke arch-specific update handler */ 97924726275SEric DeVolder arch_crash_handle_hotplug_event(image); 98024726275SEric DeVolder 98124726275SEric DeVolder /* No longer handling a hotplug event */ 98224726275SEric DeVolder image->hp_action = KEXEC_CRASH_HP_NONE; 98324726275SEric DeVolder image->elfcorehdr_updated = true; 98424726275SEric DeVolder 98524726275SEric DeVolder /* Change back to read-only */ 98624726275SEric DeVolder arch_kexec_protect_crashkres(); 98724726275SEric DeVolder 98824726275SEric DeVolder /* Errors in the callback is not a reason to rollback state */ 98924726275SEric DeVolder out: 99024726275SEric DeVolder /* Release lock now that update complete */ 99124726275SEric DeVolder kexec_unlock(); 992e2a8f20dSBaoquan He crash_hotplug_unlock(); 99324726275SEric DeVolder } 99424726275SEric DeVolder 99524726275SEric DeVolder static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v) 99624726275SEric DeVolder { 99724726275SEric DeVolder switch (val) { 99824726275SEric DeVolder case MEM_ONLINE: 99924726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY, 100024726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 100124726275SEric DeVolder break; 100224726275SEric DeVolder 100324726275SEric DeVolder case MEM_OFFLINE: 100424726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY, 100524726275SEric DeVolder KEXEC_CRASH_HP_INVALID_CPU); 100624726275SEric DeVolder break; 100724726275SEric DeVolder } 100824726275SEric DeVolder return NOTIFY_OK; 100924726275SEric DeVolder } 101024726275SEric DeVolder 101124726275SEric DeVolder static struct notifier_block crash_memhp_nb = { 101224726275SEric DeVolder .notifier_call = crash_memhp_notifier, 101324726275SEric DeVolder .priority = 0 101424726275SEric DeVolder }; 101524726275SEric DeVolder 101624726275SEric DeVolder static int crash_cpuhp_online(unsigned int cpu) 101724726275SEric DeVolder { 101824726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu); 101924726275SEric DeVolder return 0; 102024726275SEric DeVolder } 102124726275SEric DeVolder 102224726275SEric DeVolder static int crash_cpuhp_offline(unsigned int cpu) 102324726275SEric DeVolder { 102424726275SEric DeVolder crash_handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu); 102524726275SEric DeVolder return 0; 102624726275SEric DeVolder } 102724726275SEric DeVolder 102824726275SEric DeVolder static int __init crash_hotplug_init(void) 102924726275SEric DeVolder { 103024726275SEric DeVolder int result = 0; 103124726275SEric DeVolder 103224726275SEric DeVolder if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) 103324726275SEric DeVolder register_memory_notifier(&crash_memhp_nb); 103424726275SEric DeVolder 103524726275SEric DeVolder if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) { 103624726275SEric DeVolder result = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN, 103724726275SEric DeVolder "crash/cpuhp", crash_cpuhp_online, crash_cpuhp_offline); 103824726275SEric DeVolder } 103924726275SEric DeVolder 104024726275SEric DeVolder return result; 104124726275SEric DeVolder } 104224726275SEric DeVolder 104324726275SEric DeVolder subsys_initcall(crash_hotplug_init); 104424726275SEric DeVolder #endif 1045