Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kdump: append kernel build-id string to VMCOREINFO

Make kernel GNU build-id available in VMCOREINFO. Having build-id in
VMCOREINFO facilitates presenting appropriate kernel namelist image with
debug information file to kernel crash dump analysis tools. Currently
VMCOREINFO lacks uniquely identifiable key for crash analysis automation.

Regarding if this patch is necessary or matching of linux_banner and
OSRELEASE in VMCOREINFO employed by crash(8) meets the need -- IMO,
build-id approach more foolproof, in most instances it is a cryptographic
hash generated using internal code/ELF bits unlike kernel version string
upon which linux_banner is based that is external to the code. I feel
each is intended for a different purpose. Also OSRELEASE is not suitable
when two different kernel builds from same version with different features
enabled.

Currently for most linux (and non-linux) systems build-id can be extracted
using standard methods for file types such as user mode crash dumps,
shared libraries, loadable kernel modules etc., This is an exception for
linux kernel dump. Having build-id in VMCOREINFO brings some uniformity
for automation tools.

Tyler said:

: I think this is a nice improvement over today's linux_banner approach for
: correlating vmlinux to a kernel dump.
:
: The elf notes parsing in this patch lines up with what is described in in
: the "Notes (Nhdr)" section of the elf(5) man page.
:
: BUILD_ID_MAX is sufficient to hold a sha1 build-id, which is the default
: build-id type today in GNU ld(2). It is also sufficient to hold the
: "fast" build-id, which is the default build-id type today in LLVM lld(2).

Signed-off-by: Vijay Balakrishna <vijayb@linux.microsoft.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Tyler Hicks <tyhicks@linux.microsoft.com>
Acked-by: Baoquan He <bhe@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Link: http://lkml.kernel.org/r/1591849672-34104-1-git-send-email-vijayb@linux.microsoft.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Vijay Balakrishna and committed by
Linus Torvalds
0935288c 0fd338b2

+56
+6
include/linux/crash_core.h
··· 38 38 39 39 #define VMCOREINFO_OSRELEASE(value) \ 40 40 vmcoreinfo_append_str("OSRELEASE=%s\n", value) 41 + #define VMCOREINFO_BUILD_ID(value) \ 42 + vmcoreinfo_append_str("BUILD-ID=%s\n", value) 41 43 #define VMCOREINFO_PAGESIZE(value) \ 42 44 vmcoreinfo_append_str("PAGESIZE=%ld\n", value) 43 45 #define VMCOREINFO_SYMBOL(name) \ ··· 65 63 extern unsigned char *vmcoreinfo_data; 66 64 extern size_t vmcoreinfo_size; 67 65 extern u32 *vmcoreinfo_note; 66 + 67 + /* raw contents of kernel .notes section */ 68 + extern const void __start_notes __weak; 69 + extern const void __stop_notes __weak; 68 70 69 71 Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type, 70 72 void *data, size_t data_len);
+50
kernel/crash_core.c
··· 11 11 #include <asm/page.h> 12 12 #include <asm/sections.h> 13 13 14 + #include <crypto/sha.h> 15 + 14 16 /* vmcoreinfo stuff */ 15 17 unsigned char *vmcoreinfo_data; 16 18 size_t vmcoreinfo_size; ··· 378 376 } 379 377 EXPORT_SYMBOL(paddr_vmcoreinfo_note); 380 378 379 + #define NOTES_SIZE (&__stop_notes - &__start_notes) 380 + #define BUILD_ID_MAX SHA1_DIGEST_SIZE 381 + #define NT_GNU_BUILD_ID 3 382 + 383 + struct elf_note_section { 384 + struct elf_note n_hdr; 385 + u8 n_data[]; 386 + }; 387 + 388 + /* 389 + * Add build ID from .notes section as generated by the GNU ld(1) 390 + * or LLVM lld(1) --build-id option. 391 + */ 392 + static void add_build_id_vmcoreinfo(void) 393 + { 394 + char build_id[BUILD_ID_MAX * 2 + 1]; 395 + int n_remain = NOTES_SIZE; 396 + 397 + while (n_remain >= sizeof(struct elf_note)) { 398 + const struct elf_note_section *note_sec = 399 + &__start_notes + NOTES_SIZE - n_remain; 400 + const u32 n_namesz = note_sec->n_hdr.n_namesz; 401 + 402 + if (note_sec->n_hdr.n_type == NT_GNU_BUILD_ID && 403 + n_namesz != 0 && 404 + !strcmp((char *)&note_sec->n_data[0], "GNU")) { 405 + if (note_sec->n_hdr.n_descsz <= BUILD_ID_MAX) { 406 + const u32 n_descsz = note_sec->n_hdr.n_descsz; 407 + const u8 *s = &note_sec->n_data[n_namesz]; 408 + 409 + s = PTR_ALIGN(s, 4); 410 + bin2hex(build_id, s, n_descsz); 411 + build_id[2 * n_descsz] = '\0'; 412 + VMCOREINFO_BUILD_ID(build_id); 413 + return; 414 + } 415 + pr_warn("Build ID is too large to include in vmcoreinfo: %u > %u\n", 416 + note_sec->n_hdr.n_descsz, 417 + BUILD_ID_MAX); 418 + return; 419 + } 420 + n_remain -= sizeof(struct elf_note) + 421 + ALIGN(note_sec->n_hdr.n_namesz, 4) + 422 + ALIGN(note_sec->n_hdr.n_descsz, 4); 423 + } 424 + } 425 + 381 426 static int __init crash_save_vmcoreinfo_init(void) 382 427 { 383 428 vmcoreinfo_data = (unsigned char *)get_zeroed_page(GFP_KERNEL); ··· 443 394 } 444 395 445 396 VMCOREINFO_OSRELEASE(init_uts_ns.name.release); 397 + add_build_id_vmcoreinfo(); 446 398 VMCOREINFO_PAGESIZE(PAGE_SIZE); 447 399 448 400 VMCOREINFO_SYMBOL(init_uts_ns);