]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
module: add printk formats to add module build ID to stacktraces
authorStephen Boyd <swboyd@chromium.org>
Wed, 2 Jun 2021 03:53:28 +0000 (13:53 +1000)
committerStephen Rothwell <sfr@canb.auug.org.au>
Thu, 3 Jun 2021 08:17:06 +0000 (18:17 +1000)
Let's make kernel stacktraces easier to identify by including the build
ID[1] of a module if the stacktrace is printing a symbol from a module.
This makes it simpler for developers to locate a kernel module's full
debuginfo for a particular stacktrace.  Combined with
scripts/decode_stracktrace.sh, a developer can download the matching
debuginfo from a debuginfod[2] server and find the exact file and line
number for the functions plus offsets in a stacktrace that match the
module.  This is especially useful for pstore crash debugging where the
kernel crashes are recorded in something like console-ramoops and the
recovery kernel/modules are different or the debuginfo doesn't exist on
the device due to space concerns (the debuginfo can be too large for space
limited devices).

Originally, I put this on the %pS format, but that was quickly rejected
given that %pS is used in other places such as ftrace where build IDs
aren't meaningful.  There was some discussions on the list to put every
module build ID into the "Modules linked in:" section of the stacktrace
message but that quickly becomes very hard to read once you have more than
three or four modules linked in.  It also provides too much information
when we don't expect each module to be traversed in a stacktrace.  Having
the build ID for modules that aren't important just makes things messy.
Splitting it to multiple lines for each module quickly explodes the number
of lines printed in an oops too, possibly wrapping the warning off the
console.  And finally, trying to stash away each module used in a
callstack to provide the ID of each symbol printed is cumbersome and would
require changes to each architecture to stash away modules and return
their build IDs once unwinding has completed.

Instead, we opt for the simpler approach of introducing new printk formats
'%pS[R]b' for "pointer symbolic backtrace with module build ID" and '%pBb'
for "pointer backtrace with module build ID" and then updating the few
places in the architecture layer where the stacktrace is printed to use
this new format.

Before:

 Call trace:
  lkdtm_WARNING+0x28/0x30 [lkdtm]
  direct_entry+0x16c/0x1b4 [lkdtm]
  full_proxy_write+0x74/0xa4
  vfs_write+0xec/0x2e8

After:

 Call trace:
  lkdtm_WARNING+0x28/0x30 [lkdtm 6c2215028606bda50de823490723dc4bc5bf46f9]
  direct_entry+0x16c/0x1b4 [lkdtm 6c2215028606bda50de823490723dc4bc5bf46f9]
  full_proxy_write+0x74/0xa4
  vfs_write+0xec/0x2e8

Link: https://lkml.kernel.org/r/20210511003845.2429846-6-swboyd@chromium.org
Link: https://fedoraproject.org/wiki/Releases/FeatureBuildId
Link: https://sourceware.org/elfutils/Debuginfod.html
Signed-off-by: Stephen Boyd <swboyd@chromium.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Jessica Yu <jeyu@kernel.org>
Cc: Evan Green <evgreen@chromium.org>
Cc: Hsin-Yi Wang <hsinyi@chromium.org>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Cc: Sasha Levin <sashal@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Documentation/core-api/printk-formats.rst
include/linux/kallsyms.h
include/linux/module.h
kernel/kallsyms.c
kernel/module.c
lib/vsprintf.c

index 1dd4ebf0be799264eddd54a82bd04a64fe41bce3..a68730e7cfac5477fe6704745ab4f1bdc470f3fa 100644 (file)
@@ -126,6 +126,17 @@ used when printing stack backtraces. The specifier takes into
 consideration the effect of compiler optimisations which may occur
 when tail-calls are used and marked with the noreturn GCC attribute.
 
+If the pointer is within a module, the module name and optionally build ID is
+printed after the symbol name with an extra ``b`` appended to the end of the
+specifier.
+
+::
+       %pS     versatile_init+0x0/0x110 [module_name]
+       %pSb    versatile_init+0x0/0x110 [module_name ed5019fdf5e53be37cb1ba7899292d7e143b259e]
+       %pSRb   versatile_init+0x9/0x110 [module_name ed5019fdf5e53be37cb1ba7899292d7e143b259e]
+               (with __builtin_extract_return_addr() translation)
+       %pBb    prev_fn_of_versatile_init+0x88/0x88 [module_name ed5019fdf5e53be37cb1ba7899292d7e143b259e]
+
 Probed Pointers from BPF / tracing
 ----------------------------------
 
index 5cdc6903abca3581474977c8de4965f86598bb01..1a7968cf7679f0471c91eb8ff21ad889b5e3c3b3 100644 (file)
@@ -7,6 +7,7 @@
 #define _LINUX_KALLSYMS_H
 
 #include <linux/errno.h>
+#include <linux/buildid.h>
 #include <linux/kernel.h>
 #include <linux/stddef.h>
 #include <linux/mm.h>
@@ -15,8 +16,9 @@
 #include <asm/sections.h>
 
 #define KSYM_NAME_LEN 512
-#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s]") + (KSYM_NAME_LEN - 1) + \
-                        2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + 1)
+#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + (KSYM_NAME_LEN - 1) + \
+                        2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \
+                        (BUILD_ID_SIZE_MAX * 2) + 1)
 
 struct cred;
 struct module;
@@ -91,8 +93,10 @@ const char *kallsyms_lookup(unsigned long addr,
 
 /* Look up a kernel symbol and return it in a text buffer. */
 extern int sprint_symbol(char *buffer, unsigned long address);
+extern int sprint_symbol_build_id(char *buffer, unsigned long address);
 extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
 extern int sprint_backtrace(char *buffer, unsigned long address);
+extern int sprint_backtrace_build_id(char *buffer, unsigned long address);
 
 int lookup_symbol_name(unsigned long addr, char *symname);
 int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
@@ -128,6 +132,12 @@ static inline int sprint_symbol(char *buffer, unsigned long addr)
        return 0;
 }
 
+static inline int sprint_symbol_build_id(char *buffer, unsigned long address)
+{
+       *buffer = '\0';
+       return 0;
+}
+
 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
 {
        *buffer = '\0';
@@ -140,6 +150,12 @@ static inline int sprint_backtrace(char *buffer, unsigned long addr)
        return 0;
 }
 
+static inline int sprint_backtrace_build_id(char *buffer, unsigned long addr)
+{
+       *buffer = '\0';
+       return 0;
+}
+
 static inline int lookup_symbol_name(unsigned long addr, char *symname)
 {
        return -ERANGE;
index 8100bb477d86575e2ae8549f4e454da903d683e4..29d81547837d240fcea937ae1599b7b640647e89 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <linux/list.h>
 #include <linux/stat.h>
+#include <linux/buildid.h>
 #include <linux/compiler.h>
 #include <linux/cache.h>
 #include <linux/kmod.h>
@@ -369,6 +370,11 @@ struct module {
        /* Unique handle for this module */
        char name[MODULE_NAME_LEN];
 
+#ifdef CONFIG_STACKTRACE_BUILD_ID
+       /* Module build ID */
+       unsigned char build_id[BUILD_ID_SIZE_MAX];
+#endif
+
        /* Sysfs stuff. */
        struct module_kobject mkobj;
        struct module_attribute *modinfo_attrs;
@@ -636,7 +642,7 @@ void *dereference_module_function_descriptor(struct module *mod, void *ptr);
 const char *module_address_lookup(unsigned long addr,
                            unsigned long *symbolsize,
                            unsigned long *offset,
-                           char **modname,
+                           char **modname, const unsigned char **modbuildid,
                            char *namebuf);
 int lookup_module_symbol_name(unsigned long addr, char *symname);
 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
index 9d0c23e1993c13965bbadc352b0db1c4043281f0..92411224a4dacf8b464b369a6bf44864fee358fe 100644 (file)
 #include <linux/filter.h>
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
+#include <linux/build_bug.h>
 #include <linux/compiler.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
 
 /*
  * These will be re-linked against their real values
@@ -304,21 +307,13 @@ int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
                get_symbol_pos(addr, symbolsize, offset);
                return 1;
        }
-       return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
+       return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) ||
               !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
 }
 
-/*
- * Lookup an address
- * - modname is set to NULL if it's in the kernel.
- * - We guarantee that the returned name is valid until we reschedule even if.
- *   It resides in a module.
- * - We also guarantee that modname will be valid until rescheduled.
- */
-const char *kallsyms_lookup(unsigned long addr,
-                           unsigned long *symbolsize,
-                           unsigned long *offset,
-                           char **modname, char *namebuf)
+const char *kallsyms_lookup_buildid(unsigned long addr, unsigned long *symbolsize,
+                                   unsigned long *offset, char **modname,
+                                   const unsigned char **modbuildid, char *namebuf)
 {
        const char *ret;
 
@@ -334,6 +329,8 @@ const char *kallsyms_lookup(unsigned long addr,
                                       namebuf, KSYM_NAME_LEN);
                if (modname)
                        *modname = NULL;
+               if (modbuildid)
+                       *modbuildid = NULL;
 
                ret = namebuf;
                goto found;
@@ -341,7 +338,7 @@ const char *kallsyms_lookup(unsigned long addr,
 
        /* See if it's in a module or a BPF JITed image. */
        ret = module_address_lookup(addr, symbolsize, offset,
-                                   modname, namebuf);
+                                   modname, modbuildid, namebuf);
        if (!ret)
                ret = bpf_address_lookup(addr, symbolsize,
                                         offset, modname, namebuf);
@@ -355,6 +352,22 @@ found:
        return ret;
 }
 
+/*
+ * Lookup an address
+ * - modname is set to NULL if it's in the kernel.
+ * - We guarantee that the returned name is valid until we reschedule even if.
+ *   It resides in a module.
+ * - We also guarantee that modname will be valid until rescheduled.
+ */
+const char *kallsyms_lookup(unsigned long addr,
+                           unsigned long *symbolsize,
+                           unsigned long *offset,
+                           char **modname, char *namebuf)
+{
+       return kallsyms_lookup_buildid(addr, symbolsize, offset, modname,
+                                      NULL, namebuf);
+}
+
 int lookup_symbol_name(unsigned long addr, char *symname)
 {
        int res;
@@ -411,15 +424,17 @@ found:
 
 /* Look up a kernel symbol and return it in a text buffer. */
 static int __sprint_symbol(char *buffer, unsigned long address,
-                          int symbol_offset, int add_offset)
+                          int symbol_offset, int add_offset, int add_buildid)
 {
        char *modname;
+       const unsigned char *buildid;
        const char *name;
        unsigned long offset, size;
        int len;
 
        address += symbol_offset;
-       name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
+       name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid,
+                                      buffer);
        if (!name)
                return sprintf(buffer, "0x%lx", address - symbol_offset);
 
@@ -431,8 +446,17 @@ static int __sprint_symbol(char *buffer, unsigned long address,
        if (add_offset)
                len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
 
-       if (modname)
-               len += sprintf(buffer + len, " [%s]", modname);
+       if (modname) {
+               len += sprintf(buffer + len, " [%s", modname);
+#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+               if (add_buildid && buildid) {
+                       /* build ID should match length of sprintf */
+                       static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
+                       len += sprintf(buffer + len, " %20phN", buildid);
+               }
+#endif
+               len += sprintf(buffer + len, "]");
+       }
 
        return len;
 }
@@ -450,10 +474,27 @@ static int __sprint_symbol(char *buffer, unsigned long address,
  */
 int sprint_symbol(char *buffer, unsigned long address)
 {
-       return __sprint_symbol(buffer, address, 0, 1);
+       return __sprint_symbol(buffer, address, 0, 1, 0);
 }
 EXPORT_SYMBOL_GPL(sprint_symbol);
 
+/**
+ * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer
+ * @buffer: buffer to be stored
+ * @address: address to lookup
+ *
+ * This function looks up a kernel symbol with @address and stores its name,
+ * offset, size, module name and module build ID to @buffer if possible. If no
+ * symbol was found, just saves its @address as is.
+ *
+ * This function returns the number of bytes stored in @buffer.
+ */
+int sprint_symbol_build_id(char *buffer, unsigned long address)
+{
+       return __sprint_symbol(buffer, address, 0, 1, 1);
+}
+EXPORT_SYMBOL_GPL(sprint_symbol_build_id);
+
 /**
  * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
  * @buffer: buffer to be stored
@@ -467,7 +508,7 @@ EXPORT_SYMBOL_GPL(sprint_symbol);
  */
 int sprint_symbol_no_offset(char *buffer, unsigned long address)
 {
-       return __sprint_symbol(buffer, address, 0, 0);
+       return __sprint_symbol(buffer, address, 0, 0, 0);
 }
 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
 
@@ -487,7 +528,27 @@ EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
  */
 int sprint_backtrace(char *buffer, unsigned long address)
 {
-       return __sprint_symbol(buffer, address, -1, 1);
+       return __sprint_symbol(buffer, address, -1, 1, 0);
+}
+
+/**
+ * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
+ * @buffer: buffer to be stored
+ * @address: address to lookup
+ *
+ * This function is for stack backtrace and does the same thing as
+ * sprint_symbol() but with modified/decreased @address. If there is a
+ * tail-call to the function marked "noreturn", gcc optimized out code after
+ * the call so that the stack-saved return address could point outside of the
+ * caller. This function ensures that kallsyms will find the original caller
+ * by decreasing @address. This function also appends the module build ID to
+ * the @buffer if @address is within a kernel module.
+ *
+ * This function returns the number of bytes stored in @buffer.
+ */
+int sprint_backtrace_build_id(char *buffer, unsigned long address)
+{
+       return __sprint_symbol(buffer, address, -1, 1, 1);
 }
 
 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
index a1a83c13c1b5f20050d14f24d50d9d285ac723fc..7f73b3152943e0bc40219075e992332d0bf6cca1 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/trace_events.h>
 #include <linux/init.h>
 #include <linux/kallsyms.h>
+#include <linux/buildid.h>
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/sysfs.h>
@@ -2793,6 +2794,26 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
 }
 #endif /* CONFIG_KALLSYMS */
 
+#if IS_ENABLED(CONFIG_KALLSYMS) && IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+static void init_build_id(struct module *mod, const struct load_info *info)
+{
+       const Elf_Shdr *sechdr;
+       unsigned int i;
+
+       for (i = 0; i < info->hdr->e_shnum; i++) {
+               sechdr = &info->sechdrs[i];
+               if (!sect_empty(sechdr) && sechdr->sh_type == SHT_NOTE &&
+                   !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id,
+                                       sechdr->sh_size))
+                       break;
+       }
+}
+#else
+static void init_build_id(struct module *mod, const struct load_info *info)
+{
+}
+#endif
+
 static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
 {
        if (!debug)
@@ -4017,6 +4038,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
                goto free_arch_cleanup;
        }
 
+       init_build_id(mod, info);
        dynamic_debug_setup(mod, info->debug, info->num_debug);
 
        /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
@@ -4249,7 +4271,7 @@ void * __weak dereference_module_function_descriptor(struct module *mod,
 const char *module_address_lookup(unsigned long addr,
                            unsigned long *size,
                            unsigned long *offset,
-                           char **modname,
+                           char **modname, const unsigned char **modbuildid,
                            char *namebuf)
 {
        const char *ret = NULL;
@@ -4260,6 +4282,13 @@ const char *module_address_lookup(unsigned long addr,
        if (mod) {
                if (modname)
                        *modname = mod->name;
+               if (modbuildid) {
+#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+                       *modbuildid = mod->build_id;
+#else
+                       *modbuildid = NULL;
+#endif
+               }
 
                ret = find_kallsyms_symbol(mod, addr, size, offset);
        }
index e5c7afbf7405fcf5064f8b232c6e5943fd63757f..1d6bca047690d26bb9a76172638bfd0ac710019c 100644 (file)
@@ -992,8 +992,12 @@ char *symbol_string(char *buf, char *end, void *ptr,
        value = (unsigned long)ptr;
 
 #ifdef CONFIG_KALLSYMS
-       if (*fmt == 'B')
+       if (*fmt == 'B' && fmt[1] == 'b')
+               sprint_backtrace_build_id(sym, value);
+       else if (*fmt == 'B')
                sprint_backtrace(sym, value);
+       else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b')))
+               sprint_symbol_build_id(sym, value);
        else if (*fmt != 's')
                sprint_symbol(sym, value);
        else
@@ -2262,9 +2266,11 @@ early_param("no_hash_pointers", no_hash_pointers_enable);
  * - 'S' For symbolic direct pointers (or function descriptors) with offset
  * - 's' For symbolic direct pointers (or function descriptors) without offset
  * - '[Ss]R' as above with __builtin_extract_return_addr() translation
+ * - 'S[R]b' as above with module build ID (for use in backtraces)
  * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
  *         %ps and %pS. Be careful when re-using these specifiers.
  * - 'B' For backtraced symbolic direct pointers with offset
+ * - 'Bb' as above with module build ID (for use in backtraces)
  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  * - 'b[l]' For a bitmap, the number of bits is determined by the field