linux/kernel/crash_core.c
<<
>>
Prefs
   1/*
   2 * crash.c - kernel crash support code.
   3 * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com>
   4 *
   5 * This source code is licensed under the GNU General Public License,
   6 * Version 2.  See the file COPYING for more details.
   7 */
   8
   9#include <linux/crash_core.h>
  10#include <linux/utsname.h>
  11#include <linux/vmalloc.h>
  12#include <linux/sizes.h>
  13
  14#include <asm/page.h>
  15#include <asm/sections.h>
  16
  17/* vmcoreinfo stuff */
  18unsigned char *vmcoreinfo_data;
  19size_t vmcoreinfo_size;
  20u32 *vmcoreinfo_note;
  21
  22/* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
  23static unsigned char *vmcoreinfo_data_safecopy;
  24
  25/*
  26 * parsing the "crashkernel" commandline
  27 *
  28 * this code is intended to be called from architecture specific code
  29 */
  30
  31
  32/*
  33 * This function parses command lines in the format
  34 *
  35 *   crashkernel=ramsize-range:size[,...][@offset]
  36 *
  37 * The function returns 0 on success and -EINVAL on failure.
  38 */
  39static int __init parse_crashkernel_mem(char *cmdline,
  40                                        unsigned long long system_ram,
  41                                        unsigned long long *crash_size,
  42                                        unsigned long long *crash_base)
  43{
  44        char *cur = cmdline, *tmp;
  45        unsigned long long total_mem = system_ram;
  46
  47        /*
  48         * Firmware sometimes reserves some memory regions for it's own use.
  49         * so we get less than actual system memory size.
  50         * Workaround this by round up the total size to 128M which is
  51         * enough for most test cases.
  52         */
  53        total_mem = roundup(total_mem, SZ_128M);
  54
  55        /* for each entry of the comma-separated list */
  56        do {
  57                unsigned long long start, end = ULLONG_MAX, size;
  58
  59                /* get the start of the range */
  60                start = memparse(cur, &tmp);
  61                if (cur == tmp) {
  62                        pr_warn("crashkernel: Memory value expected\n");
  63                        return -EINVAL;
  64                }
  65                cur = tmp;
  66                if (*cur != '-') {
  67                        pr_warn("crashkernel: '-' expected\n");
  68                        return -EINVAL;
  69                }
  70                cur++;
  71
  72                /* if no ':' is here, than we read the end */
  73                if (*cur != ':') {
  74                        end = memparse(cur, &tmp);
  75                        if (cur == tmp) {
  76                                pr_warn("crashkernel: Memory value expected\n");
  77                                return -EINVAL;
  78                        }
  79                        cur = tmp;
  80                        if (end <= start) {
  81                                pr_warn("crashkernel: end <= start\n");
  82                                return -EINVAL;
  83                        }
  84                }
  85
  86                if (*cur != ':') {
  87                        pr_warn("crashkernel: ':' expected\n");
  88                        return -EINVAL;
  89                }
  90                cur++;
  91
  92                size = memparse(cur, &tmp);
  93                if (cur == tmp) {
  94                        pr_warn("Memory value expected\n");
  95                        return -EINVAL;
  96                }
  97                cur = tmp;
  98                if (size >= total_mem) {
  99                        pr_warn("crashkernel: invalid size\n");
 100                        return -EINVAL;
 101                }
 102
 103                /* match ? */
 104                if (total_mem >= start && total_mem < end) {
 105                        *crash_size = size;
 106                        break;
 107                }
 108        } while (*cur++ == ',');
 109
 110        if (*crash_size > 0) {
 111                while (*cur && *cur != ' ' && *cur != '@')
 112                        cur++;
 113                if (*cur == '@') {
 114                        cur++;
 115                        *crash_base = memparse(cur, &tmp);
 116                        if (cur == tmp) {
 117                                pr_warn("Memory value expected after '@'\n");
 118                                return -EINVAL;
 119                        }
 120                }
 121        } else
 122                pr_info("crashkernel size resulted in zero bytes\n");
 123
 124        return 0;
 125}
 126
 127/*
 128 * That function parses "simple" (old) crashkernel command lines like
 129 *
 130 *      crashkernel=size[@offset]
 131 *
 132 * It returns 0 on success and -EINVAL on failure.
 133 */
 134static int __init parse_crashkernel_simple(char *cmdline,
 135                                           unsigned long long *crash_size,
 136                                           unsigned long long *crash_base)
 137{
 138        char *cur = cmdline;
 139
 140        *crash_size = memparse(cmdline, &cur);
 141        if (cmdline == cur) {
 142                pr_warn("crashkernel: memory value expected\n");
 143                return -EINVAL;
 144        }
 145
 146        if (*cur == '@')
 147                *crash_base = memparse(cur+1, &cur);
 148        else if (*cur != ' ' && *cur != '\0') {
 149                pr_warn("crashkernel: unrecognized char: %c\n", *cur);
 150                return -EINVAL;
 151        }
 152
 153        return 0;
 154}
 155
 156#define SUFFIX_HIGH 0
 157#define SUFFIX_LOW  1
 158#define SUFFIX_NULL 2
 159static __initdata char *suffix_tbl[] = {
 160        [SUFFIX_HIGH] = ",high",
 161        [SUFFIX_LOW]  = ",low",
 162        [SUFFIX_NULL] = NULL,
 163};
 164
 165/*
 166 * That function parses "suffix"  crashkernel command lines like
 167 *
 168 *      crashkernel=size,[high|low]
 169 *
 170 * It returns 0 on success and -EINVAL on failure.
 171 */
 172static int __init parse_crashkernel_suffix(char *cmdline,
 173                                           unsigned long long   *crash_size,
 174                                           const char *suffix)
 175{
 176        char *cur = cmdline;
 177
 178        *crash_size = memparse(cmdline, &cur);
 179        if (cmdline == cur) {
 180                pr_warn("crashkernel: memory value expected\n");
 181                return -EINVAL;
 182        }
 183
 184        /* check with suffix */
 185        if (strncmp(cur, suffix, strlen(suffix))) {
 186                pr_warn("crashkernel: unrecognized char: %c\n", *cur);
 187                return -EINVAL;
 188        }
 189        cur += strlen(suffix);
 190        if (*cur != ' ' && *cur != '\0') {
 191                pr_warn("crashkernel: unrecognized char: %c\n", *cur);
 192                return -EINVAL;
 193        }
 194
 195        return 0;
 196}
 197
 198static __init char *get_last_crashkernel(char *cmdline,
 199                             const char *name,
 200                             const char *suffix)
 201{
 202        char *p = cmdline, *ck_cmdline = NULL;
 203
 204        /* find crashkernel and use the last one if there are more */
 205        p = strstr(p, name);
 206        while (p) {
 207                char *end_p = strchr(p, ' ');
 208                char *q;
 209
 210                if (!end_p)
 211                        end_p = p + strlen(p);
 212
 213                if (!suffix) {
 214                        int i;
 215
 216                        /* skip the one with any known suffix */
 217                        for (i = 0; suffix_tbl[i]; i++) {
 218                                q = end_p - strlen(suffix_tbl[i]);
 219                                if (!strncmp(q, suffix_tbl[i],
 220                                             strlen(suffix_tbl[i])))
 221                                        goto next;
 222                        }
 223                        ck_cmdline = p;
 224                } else {
 225                        q = end_p - strlen(suffix);
 226                        if (!strncmp(q, suffix, strlen(suffix)))
 227                                ck_cmdline = p;
 228                }
 229next:
 230                p = strstr(p+1, name);
 231        }
 232
 233        if (!ck_cmdline)
 234                return NULL;
 235
 236        return ck_cmdline;
 237}
 238
 239static int __init __parse_crashkernel(char *cmdline,
 240                             unsigned long long system_ram,
 241                             unsigned long long *crash_size,
 242                             unsigned long long *crash_base,
 243                             const char *name,
 244                             const char *suffix)
 245{
 246        char    *first_colon, *first_space;
 247        char    *ck_cmdline;
 248
 249        BUG_ON(!crash_size || !crash_base);
 250        *crash_size = 0;
 251        *crash_base = 0;
 252
 253        ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
 254
 255        if (!ck_cmdline)
 256                return -EINVAL;
 257
 258        ck_cmdline += strlen(name);
 259
 260        if (suffix)
 261                return parse_crashkernel_suffix(ck_cmdline, crash_size,
 262                                suffix);
 263
 264        if (strncmp(ck_cmdline, "auto", 4) == 0) {
 265#if defined(CONFIG_X86_64) || defined(CONFIG_S390)
 266                ck_cmdline = "1G-4G:160M,4G-64G:192M,64G-1T:256M,1T-:512M";
 267#elif defined(CONFIG_ARM64)
 268                ck_cmdline = "2G-:448M";
 269#elif defined(CONFIG_PPC64)
 270                char *fadump_cmdline;
 271
 272                fadump_cmdline = get_last_crashkernel(cmdline, "fadump=", NULL);
 273                fadump_cmdline = fadump_cmdline ?
 274                                fadump_cmdline + strlen("fadump=") : NULL;
 275                if (!fadump_cmdline || (strncmp(fadump_cmdline, "off", 3) == 0))
 276                        ck_cmdline = "2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G";
 277                else
 278                        ck_cmdline = "4G-16G:768M,16G-64G:1G,64G-128G:2G,128G-1T:4G,1T-2T:6G,2T-4T:12G,4T-8T:20G,8T-16T:36G,16T-32T:64G,32T-64T:128G,64T-:180G";
 279#endif
 280                pr_info("Using crashkernel=auto, the size chosen is a best effort estimation.\n");
 281        }
 282
 283        /*
 284         * if the commandline contains a ':', then that's the extended
 285         * syntax -- if not, it must be the classic syntax
 286         */
 287        first_colon = strchr(ck_cmdline, ':');
 288        first_space = strchr(ck_cmdline, ' ');
 289        if (first_colon && (!first_space || first_colon < first_space))
 290                return parse_crashkernel_mem(ck_cmdline, system_ram,
 291                                crash_size, crash_base);
 292
 293        return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
 294}
 295
 296/*
 297 * That function is the entry point for command line parsing and should be
 298 * called from the arch-specific code.
 299 */
 300int __init parse_crashkernel(char *cmdline,
 301                             unsigned long long system_ram,
 302                             unsigned long long *crash_size,
 303                             unsigned long long *crash_base)
 304{
 305        return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
 306                                        "crashkernel=", NULL);
 307}
 308
 309int __init parse_crashkernel_high(char *cmdline,
 310                             unsigned long long system_ram,
 311                             unsigned long long *crash_size,
 312                             unsigned long long *crash_base)
 313{
 314        return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
 315                                "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
 316}
 317
 318int __init parse_crashkernel_low(char *cmdline,
 319                             unsigned long long system_ram,
 320                             unsigned long long *crash_size,
 321                             unsigned long long *crash_base)
 322{
 323        return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
 324                                "crashkernel=", suffix_tbl[SUFFIX_LOW]);
 325}
 326
 327Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
 328                          void *data, size_t data_len)
 329{
 330        struct elf_note *note = (struct elf_note *)buf;
 331
 332        note->n_namesz = strlen(name) + 1;
 333        note->n_descsz = data_len;
 334        note->n_type   = type;
 335        buf += DIV_ROUND_UP(sizeof(*note), sizeof(Elf_Word));
 336        memcpy(buf, name, note->n_namesz);
 337        buf += DIV_ROUND_UP(note->n_namesz, sizeof(Elf_Word));
 338        memcpy(buf, data, data_len);
 339        buf += DIV_ROUND_UP(data_len, sizeof(Elf_Word));
 340
 341        return buf;
 342}
 343
 344void final_note(Elf_Word *buf)
 345{
 346        memset(buf, 0, sizeof(struct elf_note));
 347}
 348
 349static void update_vmcoreinfo_note(void)
 350{
 351        u32 *buf = vmcoreinfo_note;
 352
 353        if (!vmcoreinfo_size)
 354                return;
 355        buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
 356                              vmcoreinfo_size);
 357        final_note(buf);
 358}
 359
 360void crash_update_vmcoreinfo_safecopy(void *ptr)
 361{
 362        if (ptr)
 363                memcpy(ptr, vmcoreinfo_data, vmcoreinfo_size);
 364
 365        vmcoreinfo_data_safecopy = ptr;
 366}
 367
 368void crash_save_vmcoreinfo(void)
 369{
 370        if (!vmcoreinfo_note)
 371                return;
 372
 373        /* Use the safe copy to generate vmcoreinfo note if have */
 374        if (vmcoreinfo_data_safecopy)
 375                vmcoreinfo_data = vmcoreinfo_data_safecopy;
 376
 377        vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
 378        update_vmcoreinfo_note();
 379}
 380
 381void vmcoreinfo_append_str(const char *fmt, ...)
 382{
 383        va_list args;
 384        char buf[0x50];
 385        size_t r;
 386
 387        va_start(args, fmt);
 388        r = vscnprintf(buf, sizeof(buf), fmt, args);
 389        va_end(args);
 390
 391        r = min(r, (size_t)VMCOREINFO_BYTES - vmcoreinfo_size);
 392
 393        memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
 394
 395        vmcoreinfo_size += r;
 396}
 397
 398/*
 399 * provide an empty default implementation here -- architecture
 400 * code may override this
 401 */
 402void __weak arch_crash_save_vmcoreinfo(void)
 403{}
 404
 405phys_addr_t __weak paddr_vmcoreinfo_note(void)
 406{
 407        return __pa(vmcoreinfo_note);
 408}
 409EXPORT_SYMBOL(paddr_vmcoreinfo_note);
 410
 411static int __init crash_save_vmcoreinfo_init(void)
 412{
 413        vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL);
 414        if (!vmcoreinfo_data) {
 415                pr_warn("Memory allocation for vmcoreinfo_data failed\n");
 416                return -ENOMEM;
 417        }
 418
 419        vmcoreinfo_note = alloc_pages_exact(VMCOREINFO_NOTE_SIZE,
 420                                                GFP_KERNEL | __GFP_ZERO);
 421        if (!vmcoreinfo_note) {
 422                free_page((unsigned long)vmcoreinfo_data);
 423                vmcoreinfo_data = NULL;
 424                pr_warn("Memory allocation for vmcoreinfo_note failed\n");
 425                return -ENOMEM;
 426        }
 427
 428        VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
 429        VMCOREINFO_PAGESIZE(PAGE_SIZE);
 430
 431        VMCOREINFO_SYMBOL(init_uts_ns);
 432        VMCOREINFO_SYMBOL(node_online_map);
 433#ifdef CONFIG_MMU
 434        VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);
 435#endif
 436        VMCOREINFO_SYMBOL(_stext);
 437        VMCOREINFO_SYMBOL(vmap_area_list);
 438
 439#ifndef CONFIG_NEED_MULTIPLE_NODES
 440        VMCOREINFO_SYMBOL(mem_map);
 441        VMCOREINFO_SYMBOL(contig_page_data);
 442#endif
 443#ifdef CONFIG_SPARSEMEM
 444        VMCOREINFO_SYMBOL_ARRAY(mem_section);
 445        VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
 446        VMCOREINFO_STRUCT_SIZE(mem_section);
 447        VMCOREINFO_OFFSET(mem_section, section_mem_map);
 448#endif
 449        VMCOREINFO_STRUCT_SIZE(page);
 450        VMCOREINFO_STRUCT_SIZE(pglist_data);
 451        VMCOREINFO_STRUCT_SIZE(zone);
 452        VMCOREINFO_STRUCT_SIZE(free_area);
 453        VMCOREINFO_STRUCT_SIZE(list_head);
 454        VMCOREINFO_SIZE(nodemask_t);
 455        VMCOREINFO_OFFSET(page, flags);
 456        VMCOREINFO_OFFSET(page, _refcount);
 457        VMCOREINFO_OFFSET(page, mapping);
 458        VMCOREINFO_OFFSET(page, lru);
 459        VMCOREINFO_OFFSET(page, _mapcount);
 460        VMCOREINFO_OFFSET(page, private);
 461        VMCOREINFO_OFFSET(page, compound_dtor);
 462        VMCOREINFO_OFFSET(page, compound_order);
 463        VMCOREINFO_OFFSET(page, compound_head);
 464        VMCOREINFO_OFFSET(pglist_data, node_zones);
 465        VMCOREINFO_OFFSET(pglist_data, nr_zones);
 466#ifdef CONFIG_FLAT_NODE_MEM_MAP
 467        VMCOREINFO_OFFSET(pglist_data, node_mem_map);
 468#endif
 469        VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
 470        VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
 471        VMCOREINFO_OFFSET(pglist_data, node_id);
 472        VMCOREINFO_OFFSET(zone, free_area);
 473        VMCOREINFO_OFFSET(zone, vm_stat);
 474        VMCOREINFO_OFFSET(zone, spanned_pages);
 475        VMCOREINFO_OFFSET(free_area, free_list);
 476        VMCOREINFO_OFFSET(list_head, next);
 477        VMCOREINFO_OFFSET(list_head, prev);
 478        VMCOREINFO_OFFSET(vmap_area, va_start);
 479        VMCOREINFO_OFFSET(vmap_area, list);
 480        VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
 481        log_buf_vmcoreinfo_setup();
 482        VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
 483        VMCOREINFO_NUMBER(NR_FREE_PAGES);
 484        VMCOREINFO_NUMBER(PG_lru);
 485        VMCOREINFO_NUMBER(PG_private);
 486        VMCOREINFO_NUMBER(PG_swapcache);
 487        VMCOREINFO_NUMBER(PG_swapbacked);
 488        VMCOREINFO_NUMBER(PG_slab);
 489#ifdef CONFIG_MEMORY_FAILURE
 490        VMCOREINFO_NUMBER(PG_hwpoison);
 491#endif
 492        VMCOREINFO_NUMBER(PG_head_mask);
 493#define PAGE_BUDDY_MAPCOUNT_VALUE       (~PG_buddy)
 494        VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
 495#ifdef CONFIG_HUGETLB_PAGE
 496        VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
 497#define PAGE_OFFLINE_MAPCOUNT_VALUE     (~PG_offline)
 498        VMCOREINFO_NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE);
 499#endif
 500
 501        arch_crash_save_vmcoreinfo();
 502        update_vmcoreinfo_note();
 503
 504        return 0;
 505}
 506
 507subsys_initcall(crash_save_vmcoreinfo_init);
 508