2021-03-31 03:07:11

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 00/12] Add build ID to stacktraces

This series adds the kernel's build ID[1] to the stacktrace header printed
in oops messages, warnings, etc. and the build ID for any module that
appears in the stacktrace after the module name. The goal is to make the
stacktrace more self-contained and descriptive by including the relevant
build IDs in the kernel logs when something goes wrong. This can be used
by post processing tools like script/decode_stacktrace.sh and kernel
developers to easily locate the debug info associated with a kernel
crash and line up what line and file things started falling apart at.

To show how this can be used I've included a patch to
decode_stacktrace.sh that downloads the debuginfo from a debuginfod
server.

This also includes some patches to make the buildid.c file use more
const arguments and consolidate logic into buildid.c from kdump. These
are left to the end as they were mostly cleanup patches. I don't know
who exactly maintains this so I guess Andrew is the best option to merge
all this code.

Here's an example lkdtm stacktrace on arm64.

WARNING: CPU: 4 PID: 3255 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE
CPU: 4 PID: 3255 Comm: bash Not tainted 5.11 #3 aa23f7a1231c229de205662d5a9e0d4c580f19a1
Hardware name: Google Lazor (rev3+) with KB Backlight (DT)
pstate: 00400009 (nzcv daif +PAN -UAO -TCO BTYPE=--)
pc : lkdtm_WARNING+0x28/0x30 [lkdtm]
lr : lkdtm_do_action+0x24/0x40 [lkdtm]
sp : ffffffc0134fbca0
x29: ffffffc0134fbca0 x28: ffffff92d53ba240
x27: 0000000000000000 x26: 0000000000000000
x25: 0000000000000000 x24: ffffffe3622352c0
x23: 0000000000000020 x22: ffffffe362233366
x21: ffffffe3622352e0 x20: ffffffc0134fbde0
x19: 0000000000000008 x18: 0000000000000000
x17: ffffff929b6536fc x16: 0000000000000000
x15: 0000000000000000 x14: 0000000000000012
x13: ffffffe380ed892c x12: ffffffe381d05068
x11: 0000000000000000 x10: 0000000000000000
x9 : 0000000000000001 x8 : ffffffe362237000
x7 : aaaaaaaaaaaaaaaa x6 : 0000000000000000
x5 : 0000000000000000 x4 : 0000000000000001
x3 : 0000000000000008 x2 : ffffff93fef25a70
x1 : ffffff93fef15788 x0 : ffffffe3622352e0
Call trace:
lkdtm_WARNING+0x28/0x30 [lkdtm ed5019fdf5e53be37cb1ba7899292d7e143b259e]
direct_entry+0x16c/0x1b4 [lkdtm ed5019fdf5e53be37cb1ba7899292d7e143b259e]
full_proxy_write+0x74/0xa4
vfs_write+0xec/0x2e8
ksys_write+0x84/0xf0
__arm64_sys_write+0x24/0x30
el0_svc_common+0xf4/0x1c0
do_el0_svc_compat+0x28/0x3c
el0_svc_compat+0x10/0x1c
el0_sync_compat_handler+0xa8/0xcc
el0_sync_compat+0x178/0x180
---[ end trace 3d95032303e59e68 ]---

Changes from v2 (https://lore.kernel.org/r/[email protected]):
* Renamed symbol printing function to indicate build IDness
* Put build ID information behind Kconfig knob
* Build ID for vmlinux is calculated in early init instead of on demand
* printk format is %pS[R]b

Changes from v1 (https://lore.kernel.org/r/[email protected]):
* New printk format %pSb and %pSr
* Return binary format instead of hex format string from build ID APIs
* Some new patches to cleanup buildid/decode_stacktrace.sh
* A new patch to decode_stacktrace.sh to parse output

[1] https://fedoraproject.org/wiki/Releases/FeatureBuildId

Cc: Alexei Starovoitov <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Dave Young <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: <[email protected]>
Cc: Konstantin Khlebnikov <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Sasha Levin <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Vivek Goyal <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: peter enderborg <[email protected]>

Stephen Boyd (12):
buildid: Add API to parse build ID out of buffer
buildid: Stash away kernels build ID on init
dump_stack: Add vmlinux build ID to stack traces
module: Add printk format to add module build ID to stacktraces
arm64: stacktrace: Use %pSb for backtrace printing
x86/dumpstack: Use %pSb for backtrace printing
scripts/decode_stacktrace.sh: Support debuginfod
scripts/decode_stacktrace.sh: Silence stderr messages from
addr2line/nm
scripts/decode_stacktrace.sh: Indicate 'auto' can be used for base
path
buildid: Mark some arguments const
buildid: Fix kernel-doc notation
kdump: Use vmlinux_build_id to simplify

Documentation/core-api/printk-formats.rst | 9 +++
arch/arm64/kernel/stacktrace.c | 2 +-
arch/x86/kernel/dumpstack.c | 4 +-
include/linux/buildid.h | 4 +
include/linux/crash_core.h | 6 +-
include/linux/kallsyms.h | 13 +++-
include/linux/module.h | 6 +-
init/main.c | 1 +
kernel/crash_core.c | 41 +----------
kernel/kallsyms.c | 73 ++++++++++++++-----
kernel/module.c | 24 +++++-
lib/Kconfig.debug | 11 +++
lib/buildid.c | 73 +++++++++++++++----
lib/dump_stack.c | 12 ++-
lib/vsprintf.c | 3 +
scripts/decode_stacktrace.sh | 89 +++++++++++++++++++----
16 files changed, 267 insertions(+), 104 deletions(-)


base-commit: a38fd8748464831584a19438cbb3082b5a2dab15
--
https://chromeos.dev


2021-03-31 03:07:29

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 01/12] buildid: Add API to parse build ID out of buffer

Add an API that can parse the build ID out of a buffer, instead of a
vma, to support printing a kernel module's build ID for stack traces.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
include/linux/buildid.h | 1 +
lib/buildid.c | 50 ++++++++++++++++++++++++++++++-----------
2 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/include/linux/buildid.h b/include/linux/buildid.h
index 40232f90db6e..ebce93f26d06 100644
--- a/include/linux/buildid.h
+++ b/include/linux/buildid.h
@@ -8,5 +8,6 @@

int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
__u32 *size);
+int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size);

#endif
diff --git a/lib/buildid.c b/lib/buildid.c
index 6156997c3895..010ab0674cb9 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -2,30 +2,23 @@

#include <linux/buildid.h>
#include <linux/elf.h>
+#include <linux/kernel.h>
#include <linux/pagemap.h>

#define BUILD_ID 3
+
/*
* Parse build id from the note segment. This logic can be shared between
* 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
* identical.
*/
-static inline int parse_build_id(void *page_addr,
- unsigned char *build_id,
- __u32 *size,
- void *note_start,
- Elf32_Word note_size)
+static int parse_build_id_buf(unsigned char *build_id,
+ __u32 *size,
+ const void *note_start,
+ Elf32_Word note_size)
{
Elf32_Word note_offs = 0, new_offs;

- /* check for overflow */
- if (note_start < page_addr || note_start + note_size < note_start)
- return -EINVAL;
-
- /* only supports note that fits in the first page */
- if (note_start + note_size > page_addr + PAGE_SIZE)
- return -EINVAL;
-
while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);

@@ -49,9 +42,27 @@ static inline int parse_build_id(void *page_addr,
break;
note_offs = new_offs;
}
+
return -EINVAL;
}

+static inline int parse_build_id(void *page_addr,
+ unsigned char *build_id,
+ __u32 *size,
+ void *note_start,
+ Elf32_Word note_size)
+{
+ /* check for overflow */
+ if (note_start < page_addr || note_start + note_size < note_start)
+ return -EINVAL;
+
+ /* only supports note that fits in the first page */
+ if (note_start + note_size > page_addr + PAGE_SIZE)
+ return -EINVAL;
+
+ return parse_build_id_buf(build_id, size, note_start, note_size);
+}
+
/* Parse build ID from 32-bit ELF */
static int get_build_id_32(void *page_addr, unsigned char *build_id,
__u32 *size)
@@ -147,3 +158,16 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
put_page(page);
return ret;
}
+
+/**
+ * build_id_parse_buf - Get build ID from a buffer
+ * @buf: Elf note section(s) to parse
+ * @buf_size: Size of @buf in bytes
+ * @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long
+ *
+ * Return: 0 on success, -EINVAL otherwise
+ */
+int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
+{
+ return parse_build_id_buf(build_id, NULL, buf, buf_size);
+}
--
https://chromeos.dev

2021-03-31 03:09:51

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 02/12] buildid: Stash away kernels build ID on init

Parse the kernel's build ID at initialization so that other code can
print a hex format string representation of the running kernel's build
ID. This will be used in the kdump and dump_stack code so that
developers can easily locate the vmlinux debug symbols for a
crash/stacktrace.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Vivek Goyal <[email protected]>
Cc: <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
include/linux/buildid.h | 3 +++
init/main.c | 1 +
lib/buildid.c | 17 +++++++++++++++++
3 files changed, 21 insertions(+)

diff --git a/include/linux/buildid.h b/include/linux/buildid.h
index ebce93f26d06..0312660089c3 100644
--- a/include/linux/buildid.h
+++ b/include/linux/buildid.h
@@ -10,4 +10,7 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
__u32 *size);
int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size);

+extern unsigned char vmlinux_build_id[];
+void init_vmlinux_build_id(void);
+
#endif
diff --git a/init/main.c b/init/main.c
index 53b278845b88..eaede2f41327 100644
--- a/init/main.c
+++ b/init/main.c
@@ -857,6 +857,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)
set_task_stack_end_magic(&init_task);
smp_setup_processor_id();
debug_objects_early_init();
+ init_vmlinux_build_id();

cgroup_init_early();

diff --git a/lib/buildid.c b/lib/buildid.c
index 010ab0674cb9..b939bbc59233 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0

#include <linux/buildid.h>
+#include <linux/cache.h>
#include <linux/elf.h>
#include <linux/kernel.h>
#include <linux/pagemap.h>
@@ -171,3 +172,19 @@ int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
{
return parse_build_id_buf(build_id, NULL, buf, buf_size);
}
+
+unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init;
+
+/**
+ * init_vmlinux_build_id - Get the running kernel's build ID
+ *
+ * Return: Running kernel's build ID
+ */
+void __init init_vmlinux_build_id(void)
+{
+ extern const void __start_notes __weak;
+ extern const void __stop_notes __weak;
+ unsigned int size = &__stop_notes - &__start_notes;
+
+ build_id_parse_buf(&__start_notes, vmlinux_build_id, size);
+}
--
https://chromeos.dev

2021-03-31 03:10:42

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

Add the running kernel's build ID[1] to the stacktrace information
header. This makes it simpler for developers to locate the vmlinux with
full debuginfo for a particular kernel stacktrace. Combined with
scripts/decode_stracktrace.sh, a developer can download the correct
vmlinux from a debuginfod[2] server and find the exact file and line
number for the functions plus offsets in a stacktrace.

This is especially useful for pstore crash debugging where the kernel
crashes are recorded in the pstore logs and the recovery kernel is
different or the debuginfo doesn't exist on the device due to space
concerns (the data can be large and a security concern). The stacktrace
can be analyzed after the crash by using the build ID to find the
matching vmlinux and understand where in the function something went
wrong.

Example stacktrace from lkdtm:

WARNING: CPU: 4 PID: 3255 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE
CPU: 4 PID: 3255 Comm: bash Not tainted 5.11 #3 aa23f7a1231c229de205662d5a9e0d4c580f19a1
Hardware name: Google Lazor (rev3+) with KB Backlight (DT)
pstate: 00400009 (nzcv daif +PAN -UAO -TCO BTYPE=--)
pc : lkdtm_WARNING+0x28/0x30 [lkdtm]

The hex string aa23f7a1231c229de205662d5a9e0d4c580f19a1 is the build ID,
following the kernel version number. Put it all behind a config option,
STACKTRACE_BUILD_ID, so that kernel developers can remove this
information if they decide it is too much.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Link: https://fedoraproject.org/wiki/Releases/FeatureBuildId [1]
Link: https://sourceware.org/elfutils/Debuginfod.html [2]
Signed-off-by: Stephen Boyd <[email protected]>
---
lib/Kconfig.debug | 11 +++++++++++
lib/dump_stack.c | 12 ++++++++++--
2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2779c29d9981..5f883e50f406 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -35,6 +35,17 @@ config PRINTK_CALLER
no option to enable/disable at the kernel command line parameter or
sysfs interface.

+config STACKTRACE_BUILD_ID
+ bool "Show build ID information in stacktraces"
+ depends on PRINTK
+ help
+ Selecting this option adds build ID information for symbols in
+ stacktraces printed with the printk format '%p[SR]b'.
+
+ This option is intended for distros where debuginfo is not easily
+ accessible but can be downloaded given the build ID of the vmlinux or
+ kernel module where the function is located.
+
config CONSOLE_LOGLEVEL_DEFAULT
int "Default console loglevel (1-15)"
range 1 15
diff --git a/lib/dump_stack.c b/lib/dump_stack.c
index f5a33b6f773f..07e0214c2d4d 100644
--- a/lib/dump_stack.c
+++ b/lib/dump_stack.c
@@ -5,6 +5,7 @@
*/

#include <linux/kernel.h>
+#include <linux/buildid.h>
#include <linux/export.h>
#include <linux/sched.h>
#include <linux/sched/debug.h>
@@ -36,6 +37,12 @@ void __init dump_stack_set_arch_desc(const char *fmt, ...)
va_end(args);
}

+#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+#define BUILD_ID_STR " %20phN"
+#else
+#define BUILD_ID_STR "%s"
+#endif
+
/**
* dump_stack_print_info - print generic debug info for dump_stack()
* @log_lvl: log level
@@ -45,13 +52,14 @@ void __init dump_stack_set_arch_desc(const char *fmt, ...)
*/
void dump_stack_print_info(const char *log_lvl)
{
- printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s\n",
+ printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s" BUILD_ID_STR "\n",
log_lvl, raw_smp_processor_id(), current->pid, current->comm,
kexec_crash_loaded() ? "Kdump: loaded " : "",
print_tainted(),
init_utsname()->release,
(int)strcspn(init_utsname()->version, " "),
- init_utsname()->version);
+ init_utsname()->version,
+ IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) ? vmlinux_build_id : "");

if (dump_stack_arch_desc_str[0] != '\0')
printk("%sHardware name: %s\n",
--
https://chromeos.dev

2021-03-31 03:11:40

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

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 a new printk
format '%pS[R]b' for "pointer symbolic 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.

Example:

WARNING: CPU: 3 PID: 3373 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE hci_uart <modules trimmed>
CPU: 3 PID: 3373 Comm: bash Not tainted 5.11 #12 a8c0d47f7051f3e6670ceaea724af66a39c6cec8
Hardware name: Google Lazor (rev3+) with KB Backlight (DT)
pstate: 00400009 (nzcv daif +PAN -UAO -TCO BTYPE=--)
pc : lkdtm_WARNING+0x28/0x30 [lkdtm]
lr : lkdtm_do_action+0x24/0x40 [lkdtm]
sp : ffffffc013febca0
x29: ffffffc013febca0 x28: ffffff88d9438040
x27: 0000000000000000 x26: 0000000000000000
x25: 0000000000000000 x24: ffffffdd0e9772c0
x23: 0000000000000020 x22: ffffffdd0e975366
x21: ffffffdd0e9772e0 x20: ffffffc013febde0
x19: 0000000000000008 x18: 0000000000000000
x17: 0000000000000000 x16: 0000000000000037
x15: ffffffdd102ab174 x14: 0000000000000003
x13: 0000000000000004 x12: 0000000000000000
x11: 0000000000000000 x10: 0000000000000000
x9 : 0000000000000001 x8 : ffffffdd0e979000
x7 : 0000000000000000 x6 : ffffffdd10ff6b54
x5 : 0000000000000000 x4 : 0000000000000000
x3 : ffffffc013feb938 x2 : ffffff89fef05a70
x1 : ffffff89feef5788 x0 : ffffffdd0e9772e0
Call trace:
lkdtm_WARNING+0x28/0x30 [lkdtm 6c2215028606bda50de823490723dc4bc5bf46f9]
direct_entry+0x16c/0x1b4 [lkdtm 6c2215028606bda50de823490723dc4bc5bf46f9]
full_proxy_write+0x74/0xa4
vfs_write+0xec/0x2e8
ksys_write+0x84/0xf0
__arm64_sys_write+0x24/0x30
el0_svc_common+0xf4/0x1c0
do_el0_svc_compat+0x28/0x3c
el0_svc_compat+0x10/0x1c
el0_sync_compat_handler+0xa8/0xcc
el0_sync_compat+0x178/0x180
---[ end trace f89bc7f5417cbcc6 ]---

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Link: https://fedoraproject.org/wiki/Releases/FeatureBuildId [1]
Link: https://sourceware.org/elfutils/Debuginfod.html [2]
Signed-off-by: Stephen Boyd <[email protected]>
---
Documentation/core-api/printk-formats.rst | 9 +++
include/linux/kallsyms.h | 13 +++-
include/linux/module.h | 6 +-
kernel/kallsyms.c | 73 ++++++++++++++++-------
kernel/module.c | 24 +++++++-
lib/vsprintf.c | 3 +
6 files changed, 104 insertions(+), 24 deletions(-)

diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index 160e710d992f..9145b91f8503 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -114,6 +114,15 @@ 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.
+
+::
+ %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)
+
Probed Pointers from BPF / tracing
----------------------------------

diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index 465060acc981..2569a4792480 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -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 128
-#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,6 +93,7 @@ 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);

@@ -128,6 +131,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';
diff --git a/include/linux/module.h b/include/linux/module.h
index 59f094fa6f74..4bf869f6c944 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -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>
@@ -367,6 +368,9 @@ struct module {
/* Unique handle for this module */
char name[MODULE_NAME_LEN];

+ /* Module build ID */
+ unsigned char build_id[BUILD_ID_SIZE_MAX];
+
/* Sysfs stuff. */
struct module_kobject mkobj;
struct module_attribute *modinfo_attrs;
@@ -630,7 +634,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);
diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index 8043a90aa50e..5dfbffe46667 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -273,21 +273,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;

@@ -303,12 +295,14 @@ const char *kallsyms_lookup(unsigned long addr,
namebuf, KSYM_NAME_LEN);
if (modname)
*modname = NULL;
+ if (modbuildid)
+ *modbuildid = NULL;
return namebuf;
}

/* 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);
@@ -319,6 +313,22 @@ const char *kallsyms_lookup(unsigned long addr,
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)
{
symname[0] = '\0';
@@ -359,15 +369,17 @@ int lookup_symbol_attrs(unsigned long addr, unsigned long *size,

/* 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);

@@ -379,8 +391,12 @@ 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) && add_buildid && buildid)
+ len += sprintf(buffer + len, " %20phN", buildid);
+ len += sprintf(buffer + len, "]");
+ }

return len;
}
@@ -398,10 +414,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
@@ -415,7 +448,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);

@@ -435,7 +468,7 @@ 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);
}

/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
diff --git a/kernel/module.c b/kernel/module.c
index 30479355ab85..6f5bc1b046a5 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -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>
@@ -2770,6 +2771,20 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
}
mod->core_kallsyms.num_symtab = ndst;
}
+
+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 inline void layout_symtab(struct module *mod, struct load_info *info)
{
@@ -2778,6 +2793,10 @@ static inline void layout_symtab(struct module *mod, struct load_info *info)
static void add_kallsyms(struct module *mod, const struct load_info *info)
{
}
+
+static void init_build_id(struct module *mod, const struct load_info *info)
+{
+}
#endif /* CONFIG_KALLSYMS */

static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
@@ -4004,6 +4023,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 */
@@ -4235,7 +4255,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;
@@ -4246,6 +4266,8 @@ const char *module_address_lookup(unsigned long addr,
if (mod) {
if (modname)
*modname = mod->name;
+ if (modbuildid)
+ *modbuildid = mod->build_id;

ret = find_kallsyms_symbol(mod, addr, size, offset);
}
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 41ddc353ebb8..91a70125148c 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -968,6 +968,8 @@ char *symbol_string(char *buf, char *end, void *ptr,
#ifdef CONFIG_KALLSYMS
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
@@ -2129,6 +2131,7 @@ 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
--
https://chromeos.dev

2021-03-31 03:12:46

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 05/12] arm64: stacktrace: Use %pSb for backtrace printing

Let's use the new printk format to print the stacktrace entry when
printing a backtrace to the kernel logs. This will include any module's
build ID[1] in it so that offline/crash debugging can easily locate the
debuginfo for a module via something like debuginfod[2].

Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Link: https://fedoraproject.org/wiki/Releases/FeatureBuildId [1]
Link: https://sourceware.org/elfutils/Debuginfod.html [2]
Signed-off-by: Stephen Boyd <[email protected]>
---
arch/arm64/kernel/stacktrace.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c
index ad20981dfda4..9d38da01ff98 100644
--- a/arch/arm64/kernel/stacktrace.c
+++ b/arch/arm64/kernel/stacktrace.c
@@ -129,7 +129,7 @@ NOKPROBE_SYMBOL(walk_stackframe);

static void dump_backtrace_entry(unsigned long where, const char *loglvl)
{
- printk("%s %pS\n", loglvl, (void *)where);
+ printk("%s %pSb\n", loglvl, (void *)where);
}

void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
--
https://chromeos.dev

2021-03-31 03:12:56

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 06/12] x86/dumpstack: Use %pSb for backtrace printing

Let's use the new printk format to print the stacktrace entry when
printing a backtrace to the kernel logs. This will include any module's
build ID[1] in it so that offline/crash debugging can easily locate the
debuginfo for a module via something like debuginfod[2].

Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Link: https://fedoraproject.org/wiki/Releases/FeatureBuildId [1]
Link: https://sourceware.org/elfutils/Debuginfod.html [2]
Signed-off-by: Stephen Boyd <[email protected]>
---
arch/x86/kernel/dumpstack.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c
index 299c20f0a38b..7ad5eea99b2b 100644
--- a/arch/x86/kernel/dumpstack.c
+++ b/arch/x86/kernel/dumpstack.c
@@ -143,9 +143,9 @@ void show_opcodes(struct pt_regs *regs, const char *loglvl)
void show_ip(struct pt_regs *regs, const char *loglvl)
{
#ifdef CONFIG_X86_32
- printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
+ printk("%sEIP: %pSb\n", loglvl, (void *)regs->ip);
#else
- printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
+ printk("%sRIP: %04x:%pSb\n", loglvl, (int)regs->cs, (void *)regs->ip);
#endif
show_opcodes(regs, loglvl);
}
--
https://chromeos.dev

2021-03-31 03:13:40

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 07/12] scripts/decode_stacktrace.sh: Support debuginfod

Now that stacktraces contain the build ID information we can update this
script to use debuginfod-find to locate the debuginfo for the vmlinux
and modules automatically. This can replace the existing code that
requires specifying a path to vmlinux or tries to find the vmlinux and
modules automatically by using the release number. Work it into the
script as a fallback option if the vmlinux isn't specified on the
commandline.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Konstantin Khlebnikov <[email protected]>
Cc: Sasha Levin <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
scripts/decode_stacktrace.sh | 81 +++++++++++++++++++++++++++++++-----
1 file changed, 70 insertions(+), 11 deletions(-)

diff --git a/scripts/decode_stacktrace.sh b/scripts/decode_stacktrace.sh
index 90398347e366..ca21f8bdf5f2 100755
--- a/scripts/decode_stacktrace.sh
+++ b/scripts/decode_stacktrace.sh
@@ -3,11 +3,10 @@
# (c) 2014, Sasha Levin <[email protected]>
#set -x

-if [[ $# < 1 ]]; then
+usage() {
echo "Usage:"
echo " $0 -r <release> | <vmlinux> [base path] [modules path]"
- exit 1
-fi
+}

if [[ $1 == "-r" ]] ; then
vmlinux=""
@@ -24,6 +23,7 @@ if [[ $1 == "-r" ]] ; then

if [[ $vmlinux == "" ]] ; then
echo "ERROR! vmlinux image for release $release is not found" >&2
+ usage
exit 2
fi
else
@@ -31,12 +31,35 @@ else
basepath=${2-auto}
modpath=$3
release=""
+ debuginfod=
+
+ # Can we use debuginfod-find?
+ if type debuginfod-find >/dev/null 2>&1 ; then
+ debuginfod=${1-only}
+ fi
+
+ if [[ $vmlinux == "" && -z $debuginfod ]] ; then
+ echo "ERROR! vmlinux image must be specified" >&2
+ usage
+ exit 1
+ fi
fi

declare -A cache
declare -A modcache

find_module() {
+ if [[ -n $debuginfod ]] ; then
+ if [[ -n $modbuildid ]] ; then
+ debuginfod-find debuginfo $modbuildid && return
+ fi
+
+ # Only using debuginfod so don't try to find vmlinux module path
+ if [[ $debuginfod == "only" ]] ; then
+ return
+ fi
+ fi
+
if [[ "$modpath" != "" ]] ; then
for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
if readelf -WS "$fn" | grep -qwF .debug_line ; then
@@ -150,6 +173,27 @@ parse_symbol() {
symbol="$segment$name ($code)"
}

+debuginfod_get_vmlinux() {
+ local vmlinux_buildid=${1##* }
+
+ if [[ $vmlinux != "" ]]; then
+ return
+ fi
+
+ if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
+ vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
+ if [[ $? -ne 0 ]] ; then
+ echo "ERROR! vmlinux image not found via debuginfod-find" >&2
+ usage
+ exit 2
+ fi
+ return
+ fi
+ echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
+ usage
+ exit 2
+}
+
decode_code() {
local scripts=`dirname "${BASH_SOURCE[0]}"`

@@ -157,6 +201,14 @@ decode_code() {
}

handle_line() {
+ if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
+ module=""
+ symbol="kernel_init+0x0/0x0"
+ parse_symbol
+ basepath=${symbol#kernel_init (}
+ basepath=${basepath%/init/main.c:*)}
+ fi
+
local words

# Tokenize
@@ -182,16 +234,28 @@ handle_line() {
fi
done

+ if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
+ words[$last-1]="${words[$last-1]} ${words[$last]}"
+ unset words[$last]
+ last=$(( $last - 1 ))
+ fi
+
if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
module=${words[$last]}
module=${module#\[}
module=${module%\]}
+ modbuildid=${module#* }
+ module=${module% *}
+ if [[ $modbuildid == $module ]]; then
+ modbuildid=
+ fi
symbol=${words[$last-1]}
unset words[$last-1]
else
# The symbol is the last element, process it
symbol=${words[$last]}
module=
+ modbuildid=
fi

unset words[$last]
@@ -201,14 +265,6 @@ handle_line() {
echo "${words[@]}" "$symbol $module"
}

-if [[ $basepath == "auto" ]] ; then
- module=""
- symbol="kernel_init+0x0/0x0"
- parse_symbol
- basepath=${symbol#kernel_init (}
- basepath=${basepath%/init/main.c:*)}
-fi
-
while read line; do
# Let's see if we have an address in the line
if [[ $line =~ \[\<([^]]+)\>\] ]] ||
@@ -218,6 +274,9 @@ while read line; do
# Is it a code line?
elif [[ $line == *Code:* ]]; then
decode_code "$line"
+ # Is it a version line?
+ elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
+ debuginfod_get_vmlinux "$line"
else
# Nothing special in this line, show it as is
echo "$line"
--
https://chromeos.dev

2021-03-31 03:14:44

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 09/12] scripts/decode_stacktrace.sh: Indicate 'auto' can be used for base path

Add "auto" to the usage message so that it's a little clearer that you
can pass "auto" as the second argument. When passing "auto" the script
tries to find the base path automatically instead of requiring it be
passed on the commandline. Also use [<variable>] to indicate the
variable argument and that it is optional so that we can differentiate
from the literal "auto" that should be passed.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Konstantin Khlebnikov <[email protected]>
Cc: Sasha Levin <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
scripts/decode_stacktrace.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/decode_stacktrace.sh b/scripts/decode_stacktrace.sh
index 20b5af1ebe5e..5fbad61fe490 100755
--- a/scripts/decode_stacktrace.sh
+++ b/scripts/decode_stacktrace.sh
@@ -5,7 +5,7 @@

usage() {
echo "Usage:"
- echo " $0 -r <release> | <vmlinux> [base path] [modules path]"
+ echo " $0 -r <release> | <vmlinux> [<base path>|auto] [<modules path>]"
}

if [[ $1 == "-r" ]] ; then
--
https://chromeos.dev

2021-03-31 03:15:49

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 11/12] buildid: Fix kernel-doc notation

Kernel doc should use "Return:" instead of "Returns" to properly reflect
the return values.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
lib/buildid.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/buildid.c b/lib/buildid.c
index e8a473f2b023..483abf71eeec 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -120,7 +120,7 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id,
* @build_id: buffer to store build id, at least BUILD_ID_SIZE long
* @size: returns actual build id size in case of success
*
- * Returns 0 on success, otherwise error (< 0).
+ * Return: 0 on success, -EINVAL otherwise
*/
int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
__u32 *size)
--
https://chromeos.dev

2021-03-31 03:16:03

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 08/12] scripts/decode_stacktrace.sh: Silence stderr messages from addr2line/nm

Sometimes if you're using tools that have linked things improperly or
have new features/sections that older tools don't expect you'll see
warnings printed to stderr. We don't really care about these warnings,
so let's just silence these messages to cleanup output of this script.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Konstantin Khlebnikov <[email protected]>
Cc: Sasha Levin <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
scripts/decode_stacktrace.sh | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/scripts/decode_stacktrace.sh b/scripts/decode_stacktrace.sh
index ca21f8bdf5f2..20b5af1ebe5e 100755
--- a/scripts/decode_stacktrace.sh
+++ b/scripts/decode_stacktrace.sh
@@ -74,7 +74,7 @@ find_module() {
find_module && return

if [[ $release == "" ]] ; then
- release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" | sed -n 's/\$1 = "\(.*\)".*/\1/p')
+ release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
fi

for dn in {/usr/lib/debug,}/lib/modules/$release ; do
@@ -128,7 +128,7 @@ parse_symbol() {
if [[ "${cache[$module,$name]+isset}" == "isset" ]]; then
local base_addr=${cache[$module,$name]}
else
- local base_addr=$(nm "$objfile" | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
+ local base_addr=$(nm "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
if [[ $base_addr == "" ]] ; then
# address not found
return
@@ -152,7 +152,7 @@ parse_symbol() {
if [[ "${cache[$module,$address]+isset}" == "isset" ]]; then
local code=${cache[$module,$address]}
else
- local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address")
+ local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address" 2>/dev/null)
cache[$module,$address]=$code
fi

--
https://chromeos.dev

2021-03-31 03:16:35

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 12/12] kdump: Use vmlinux_build_id to simplify

We can use the vmlinux_build_id array here now instead of open coding
it. This mostly consolidates code.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Cc: Dave Young <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Vivek Goyal <[email protected]>
Cc: <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
include/linux/crash_core.h | 6 +-----
kernel/crash_core.c | 41 ++------------------------------------
2 files changed, 3 insertions(+), 44 deletions(-)

diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
index 206bde8308b2..fb8ab99bb2ee 100644
--- a/include/linux/crash_core.h
+++ b/include/linux/crash_core.h
@@ -39,7 +39,7 @@ phys_addr_t paddr_vmcoreinfo_note(void);
#define VMCOREINFO_OSRELEASE(value) \
vmcoreinfo_append_str("OSRELEASE=%s\n", value)
#define VMCOREINFO_BUILD_ID(value) \
- vmcoreinfo_append_str("BUILD-ID=%s\n", value)
+ vmcoreinfo_append_str("BUILD-ID=%20phN\n", value)
#define VMCOREINFO_PAGESIZE(value) \
vmcoreinfo_append_str("PAGESIZE=%ld\n", value)
#define VMCOREINFO_SYMBOL(name) \
@@ -69,10 +69,6 @@ extern unsigned char *vmcoreinfo_data;
extern size_t vmcoreinfo_size;
extern u32 *vmcoreinfo_note;

-/* raw contents of kernel .notes section */
-extern const void __start_notes __weak;
-extern const void __stop_notes __weak;
-
Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
void *data, size_t data_len);
void final_note(Elf_Word *buf);
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 825284baaf46..6b560cf9f374 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -4,6 +4,7 @@
* Copyright (C) 2002-2004 Eric Biederman <[email protected]>
*/

+#include <linux/buildid.h>
#include <linux/crash_core.h>
#include <linux/utsname.h>
#include <linux/vmalloc.h>
@@ -378,51 +379,13 @@ phys_addr_t __weak paddr_vmcoreinfo_note(void)
}
EXPORT_SYMBOL(paddr_vmcoreinfo_note);

-#define NOTES_SIZE (&__stop_notes - &__start_notes)
-#define BUILD_ID_MAX SHA1_DIGEST_SIZE
-#define NT_GNU_BUILD_ID 3
-
-struct elf_note_section {
- struct elf_note n_hdr;
- u8 n_data[];
-};
-
/*
* Add build ID from .notes section as generated by the GNU ld(1)
* or LLVM lld(1) --build-id option.
*/
static void add_build_id_vmcoreinfo(void)
{
- char build_id[BUILD_ID_MAX * 2 + 1];
- int n_remain = NOTES_SIZE;
-
- while (n_remain >= sizeof(struct elf_note)) {
- const struct elf_note_section *note_sec =
- &__start_notes + NOTES_SIZE - n_remain;
- const u32 n_namesz = note_sec->n_hdr.n_namesz;
-
- if (note_sec->n_hdr.n_type == NT_GNU_BUILD_ID &&
- n_namesz != 0 &&
- !strcmp((char *)&note_sec->n_data[0], "GNU")) {
- if (note_sec->n_hdr.n_descsz <= BUILD_ID_MAX) {
- const u32 n_descsz = note_sec->n_hdr.n_descsz;
- const u8 *s = &note_sec->n_data[n_namesz];
-
- s = PTR_ALIGN(s, 4);
- bin2hex(build_id, s, n_descsz);
- build_id[2 * n_descsz] = '\0';
- VMCOREINFO_BUILD_ID(build_id);
- return;
- }
- pr_warn("Build ID is too large to include in vmcoreinfo: %u > %u\n",
- note_sec->n_hdr.n_descsz,
- BUILD_ID_MAX);
- return;
- }
- n_remain -= sizeof(struct elf_note) +
- ALIGN(note_sec->n_hdr.n_namesz, 4) +
- ALIGN(note_sec->n_hdr.n_descsz, 4);
- }
+ VMCOREINFO_BUILD_ID(vmlinux_build_id);
}

static int __init crash_save_vmcoreinfo_init(void)
--
https://chromeos.dev

2021-03-31 03:17:14

by Stephen Boyd

[permalink] [raw]
Subject: [PATCH v3 10/12] buildid: Mark some arguments const

These arguments are never modified so they can be marked const to
indicate as such.

Cc: Jiri Olsa <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jessica Yu <[email protected]>
Cc: Evan Green <[email protected]>
Cc: Hsin-Yi Wang <[email protected]>
Signed-off-by: Stephen Boyd <[email protected]>
---
lib/buildid.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/buildid.c b/lib/buildid.c
index b939bbc59233..e8a473f2b023 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -47,10 +47,10 @@ static int parse_build_id_buf(unsigned char *build_id,
return -EINVAL;
}

-static inline int parse_build_id(void *page_addr,
+static inline int parse_build_id(const void *page_addr,
unsigned char *build_id,
__u32 *size,
- void *note_start,
+ const void *note_start,
Elf32_Word note_size)
{
/* check for overflow */
@@ -65,7 +65,7 @@ static inline int parse_build_id(void *page_addr,
}

/* Parse build ID from 32-bit ELF */
-static int get_build_id_32(void *page_addr, unsigned char *build_id,
+static int get_build_id_32(const void *page_addr, unsigned char *build_id,
__u32 *size)
{
Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
@@ -90,7 +90,7 @@ static int get_build_id_32(void *page_addr, unsigned char *build_id,
}

/* Parse build ID from 64-bit ELF */
-static int get_build_id_64(void *page_addr, unsigned char *build_id,
+static int get_build_id_64(const void *page_addr, unsigned char *build_id,
__u32 *size)
{
Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
--
https://chromeos.dev

2021-04-07 21:01:38

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

On Tue 2021-03-30 20:05:11, Stephen Boyd wrote:
> Add the running kernel's build ID[1] to the stacktrace information
> header. This makes it simpler for developers to locate the vmlinux with
> full debuginfo for a particular kernel stacktrace. Combined with
> scripts/decode_stracktrace.sh, a developer can download the correct
> vmlinux from a debuginfod[2] server and find the exact file and line
> number for the functions plus offsets in a stacktrace.
>
> This is especially useful for pstore crash debugging where the kernel
> crashes are recorded in the pstore logs and the recovery kernel is
> different or the debuginfo doesn't exist on the device due to space
> concerns (the data can be large and a security concern). The stacktrace
> can be analyzed after the crash by using the build ID to find the
> matching vmlinux and understand where in the function something went
> wrong.
>
> Example stacktrace from lkdtm:
>
> WARNING: CPU: 4 PID: 3255 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
> Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE
> CPU: 4 PID: 3255 Comm: bash Not tainted 5.11 #3 aa23f7a1231c229de205662d5a9e0d4c580f19a1
> Hardware name: Google Lazor (rev3+) with KB Backlight (DT)
> pstate: 00400009 (nzcv daif +PAN -UAO -TCO BTYPE=--)
> pc : lkdtm_WARNING+0x28/0x30 [lkdtm]
>
> The hex string aa23f7a1231c229de205662d5a9e0d4c580f19a1 is the build ID,
> following the kernel version number. Put it all behind a config option,
> STACKTRACE_BUILD_ID, so that kernel developers can remove this
> information if they decide it is too much.
>
> diff --git a/lib/dump_stack.c b/lib/dump_stack.c
> index f5a33b6f773f..07e0214c2d4d 100644
> --- a/lib/dump_stack.c
> +++ b/lib/dump_stack.c
> @@ -45,13 +52,14 @@ void __init dump_stack_set_arch_desc(const char *fmt, ...)
> */
> void dump_stack_print_info(const char *log_lvl)
> {
> - printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s\n",
> + printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s" BUILD_ID_STR "\n",
> log_lvl, raw_smp_processor_id(), current->pid, current->comm,
> kexec_crash_loaded() ? "Kdump: loaded " : "",
> print_tainted(),
> init_utsname()->release,
> (int)strcspn(init_utsname()->version, " "),
> - init_utsname()->version);
> + init_utsname()->version,
> + IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) ? vmlinux_build_id : "");

I get the following warning when CONFIG_STACKTRACE_BUILD_ID is not
enabled:

CC lib/dump_stack.o
lib/dump_stack.c: In function ‘dump_stack_print_info’:
lib/dump_stack.c:62:67: warning: pointer type mismatch in conditional expression
IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) ? vmlinux_build_id : "");
^
lib/dump_stack.c:55:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 11 has type ‘void *’ [-Wformat=]
printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s" BUILD_ID_STR "\n",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/dump_stack.c:43:24: note: format string is defined here
#define BUILD_ID_STR "%s"
~^
%p

and this when the options is enabled:

CC lib/dump_stack.o
lib/dump_stack.c: In function ‘dump_stack_print_info’:
lib/dump_stack.c:62:67: warning: pointer type mismatch in conditional expression
IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) ? vmlinux_build_id : "");


I think that you need to use something like:

#ifdef CONFIG_STACKTRACE_BUILD_ID
#define BUILD_ID_FTM " %20phN"
#define BUILD_ID_VAL vmlinux_build_id
#else
#define BUILD_ID_FTM "%s"
#define BUILD_ID_VAL ""
#endif

printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s" BUILD_ID_FTM "\n",
log_lvl, raw_smp_processor_id(), current->pid, current->comm,
kexec_crash_loaded() ? "Kdump: loaded " : "",
print_tainted(),
init_utsname()->release,
(int)strcspn(init_utsname()->version, " "),
init_utsname()->version,
BUILD_ID_VAL);


Best Regards,
Petr

2021-04-07 21:04:28

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

On Tue 2021-03-30 20:05:11, Stephen Boyd wrote:
> Add the running kernel's build ID[1] to the stacktrace information
> header. This makes it simpler for developers to locate the vmlinux with
> full debuginfo for a particular kernel stacktrace. Combined with
> scripts/decode_stracktrace.sh, a developer can download the correct
> vmlinux from a debuginfod[2] server and find the exact file and line
> number for the functions plus offsets in a stacktrace.
>
> This is especially useful for pstore crash debugging where the kernel
> crashes are recorded in the pstore logs and the recovery kernel is
> different or the debuginfo doesn't exist on the device due to space
> concerns (the data can be large and a security concern). The stacktrace
> can be analyzed after the crash by using the build ID to find the
> matching vmlinux and understand where in the function something went
> wrong.
>
> Example stacktrace from lkdtm:
>
> WARNING: CPU: 4 PID: 3255 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
> Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE
> CPU: 4 PID: 3255 Comm: bash Not tainted 5.11 #3 aa23f7a1231c229de205662d5a9e0d4c580f19a1

I tried "echo l >/proc/sysrq-trigger" and get:

[ 75.123014] CPU: 1 PID: 5079 Comm: bash Kdump: loaded Not tainted 5.12.0-rc6-default+ #169 00000080ffffffff0000000000000000
00000000

It does not look like an unique ID. I have already reported this for
v2. But you sent v3 just 8 hours later before I was able to provide
more details.

Anyway, here are the requested details:

# hex /sys/kernel/notes
0000 04 00 00 00 06 00 00 00 06 00 00 00 58 65 6e 00 ........ ....Xen.
0010 6c 69 6e 75 78 00 00 00 04 00 00 00 04 00 00 00 linux... ........
0020 07 00 00 00 58 65 6e 00 32 2e 36 00 04 00 00 00 ....Xen. 2.6.....
0030 08 00 00 00 05 00 00 00 58 65 6e 00 78 65 6e 2d ........ Xen.xen-
0040 33 2e 30 00 04 00 00 00 08 00 00 00 03 00 00 00 3.0..... ........
0050 58 65 6e 00 00 00 00 80 ff ff ff ff 04 00 00 00 Xen..... ........
0060 08 00 00 00 0f 00 00 00 58 65 6e 00 00 00 00 00 ........ Xen.....
0070 80 00 00 00 04 00 00 00 08 00 00 00 01 00 00 00 ........ ........
0080 58 65 6e 00 c0 11 34 93 ff ff ff ff 04 00 00 00 Xen...4. ........
0090 08 00 00 00 02 00 00 00 58 65 6e 00 00 20 00 91 ........ Xen.. ..
00a0 ff ff ff ff 04 00 00 00 29 00 00 00 0a 00 00 00 ........ ).......
00b0 58 65 6e 00 21 77 72 69 74 61 62 6c 65 5f 70 61 Xen.!wri table_pa
00c0 67 65 5f 74 61 62 6c 65 73 7c 70 61 65 5f 70 67 ge_table s|pae_pg
00d0 64 69 72 5f 61 62 6f 76 65 5f 34 67 62 00 00 00 dir_abov e_4gb...
00e0 04 00 00 00 04 00 00 00 11 00 00 00 58 65 6e 00 ........ ....Xen.
00f0 01 88 00 00 04 00 00 00 04 00 00 00 09 00 00 00 ........ ........
0100 58 65 6e 00 79 65 73 00 04 00 00 00 08 00 00 00 Xen.yes. ........
0110 08 00 00 00 58 65 6e 00 67 65 6e 65 72 69 63 00 ....Xen. generic.
0120 04 00 00 00 10 00 00 00 0d 00 00 00 58 65 6e 00 ........ ....Xen.
0130 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ........ ........
0140 04 00 00 00 04 00 00 00 0e 00 00 00 58 65 6e 00 ........ ....Xen.
0150 01 00 00 00 04 00 00 00 04 00 00 00 10 00 00 00 ........ ........
0160 58 65 6e 00 01 00 00 00 04 00 00 00 08 00 00 00 Xen..... ........
0170 0c 00 00 00 58 65 6e 00 00 00 00 00 00 80 ff ff ....Xen. ........
0180 04 00 00 00 08 00 00 00 04 00 00 00 58 65 6e 00 ........ ....Xen.
0190 00 00 00 00 00 00 00 00 04 00 00 00 14 00 00 00 ........ ........
01a0 03 00 00 00 47 4e 55 00 6c ed 77 1e 56 f7 47 d0 ....GNU. l.w.V.G.
01b0 1c 17 cd 28 25 8e 48 61 60 ee 3d e2 06 00 00 00 ...(%.Ha `.=.....
01c0 01 00 00 00 00 01 00 00 4c 69 6e 75 78 00 00 00 ........ Linux...
01d0 00 00 00 00 04 00 00 00 08 00 00 00 12 00 00 00 ........ ........
01e0 58 65 6e 00 a0 06 00 11 00 00 00 00 Xen..... ....

and

# file vmlinux-5.12.0-rc6-default+
vmlinux-5.12.0-rc6-default+: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=6ced771e56f747d01c17cd28258e486160ee3de2, not stripped

and also

# readelf -Wn vmlinux-5.12.0-rc6-default+

Displaying notes found in: .notes
Owner Data size Description
Xen 0x00000006 Unknown note type: (0x00000006) description data: 6c 69 6e 75 78 00
Xen 0x00000004 Unknown note type: (0x00000007) description data: 32 2e 36 00
Xen 0x00000008 Unknown note type: (0x00000005) description data: 78 65 6e 2d 33 2e 30 00
Xen 0x00000008 Unknown note type: (0x00000003) description data: 00 00 00 ffffff80 ffffffff ffffffff ffffffff ffffffff
Xen 0x00000008 Unknown note type: (0x0000000f) description data: 00 00 00 00 ffffff80 00 00 00
Xen 0x00000008 NT_VERSION (version) description data: ffffffc0 11 34 ffffff83 ffffffff ffffffff ffffffff ffffffff
Xen 0x00000008 NT_ARCH (architecture) description data: 00 20 00 ffffff81 ffffffff ffffffff ffffffff ffffffff
Xen 0x00000029 Unknown note type: (0x0000000a) description data: 21 77 72 69 74 61 62 6c 65 5f 70 61 67 65 5f 74 61 62 6c 65 73 7c 70 61 65 5f 70 67 64 69 72 5f 61 62 6f 76 65 5f 34 67 62
Xen 0x00000004 Unknown note type: (0x00000011) description data: 01 ffffff88 00 00
Xen 0x00000004 Unknown note type: (0x00000009) description data: 79 65 73 00
Xen 0x00000008 Unknown note type: (0x00000008) description data: 67 65 6e 65 72 69 63 00
Xen 0x00000010 Unknown note type: (0x0000000d) description data: 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
Xen 0x00000004 Unknown note type: (0x0000000e) description data: 01 00 00 00
Xen 0x00000004 Unknown note type: (0x00000010) description data: 01 00 00 00
Xen 0x00000008 Unknown note type: (0x0000000c) description data: 00 00 00 00 00 ffffff80 ffffffff ffffffff
Xen 0x00000008 Unknown note type: (0x00000004) description data: 00 00 00 00 00 00 00 00
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: 6ced771e56f747d01c17cd28258e486160ee3de2
Linux 0x00000001 OPEN description data: 00
Xen 0x00000008 Unknown note type: (0x00000012) description data: ffffffa0 06 00 01 00 00 00 00


Best Regards,
Petr

2021-04-07 21:11:22

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

On Tue 2021-03-30 20:05:12, Stephen Boyd wrote:
> 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).
>
> @@ -359,15 +369,17 @@ int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
>
> /* 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);
>
> @@ -379,8 +391,12 @@ static int __sprint_symbol(char *buffer, unsigned long address,
> if (add_offset)
> len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);

Please add something like:

/* Keep BUILD_ID_SIZE_MAX in sync with the below used %20phN */
BUILD_BUG_ON(BUILD_ID_SIZE_MAX != 20)

to make sure the the hard

>
> - if (modname)
> - len += sprintf(buffer + len, " [%s]", modname);
> + if (modname) {
> + len += sprintf(buffer + len, " [%s", modname);
> + if (IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) && add_buildid && buildid)
> + len += sprintf(buffer + len, " %20phN", buildid);
> + len += sprintf(buffer + len, "]");
> + }
>
> return len;
> }

Otherwise the approach looks fine to me. The main problem is that it
does not work for me as described in the other reply ;-)

Best Regards,
Petr

2021-04-07 21:11:47

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

On Wed, Apr 07, 2021 at 04:54:33PM +0200, Petr Mladek wrote:
> On Tue 2021-03-30 20:05:12, Stephen Boyd wrote:
> > 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).

...

> It shows wrong build id for vmlinux.
> And it does not show the build if for the module at all.

From your previous messages and this seems like virtualization breaks the
access / use of build ID :-(

--
With Best Regards,
Andy Shevchenko


2021-04-07 21:41:56

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 12/12] kdump: Use vmlinux_build_id to simplify

On Tue 2021-03-30 20:05:20, Stephen Boyd wrote:
> We can use the vmlinux_build_id array here now instead of open coding
> it. This mostly consolidates code.
>
> Cc: Jiri Olsa <[email protected]>
> Cc: Alexei Starovoitov <[email protected]>
> Cc: Jessica Yu <[email protected]>
> Cc: Evan Green <[email protected]>
> Cc: Hsin-Yi Wang <[email protected]>
> Cc: Dave Young <[email protected]>
> Cc: Baoquan He <[email protected]>
> Cc: Vivek Goyal <[email protected]>
> Cc: <[email protected]>
> Signed-off-by: Stephen Boyd <[email protected]>
> ---
> include/linux/crash_core.h | 6 +-----
> kernel/crash_core.c | 41 ++------------------------------------
> 2 files changed, 3 insertions(+), 44 deletions(-)
>
> diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
> index 206bde8308b2..fb8ab99bb2ee 100644
> --- a/include/linux/crash_core.h
> +++ b/include/linux/crash_core.h
> @@ -39,7 +39,7 @@ phys_addr_t paddr_vmcoreinfo_note(void);
> #define VMCOREINFO_OSRELEASE(value) \
> vmcoreinfo_append_str("OSRELEASE=%s\n", value)
> #define VMCOREINFO_BUILD_ID(value) \
> - vmcoreinfo_append_str("BUILD-ID=%s\n", value)
> + vmcoreinfo_append_str("BUILD-ID=%20phN\n", value)

Please, add also build check that BUILD_ID_MAX == 20.


> #define VMCOREINFO_PAGESIZE(value) \
> vmcoreinfo_append_str("PAGESIZE=%ld\n", value)
> #define VMCOREINFO_SYMBOL(name) \
> @@ -69,10 +69,6 @@ extern unsigned char *vmcoreinfo_data;
> extern size_t vmcoreinfo_size;
> extern u32 *vmcoreinfo_note;
>
> -/* raw contents of kernel .notes section */
> -extern const void __start_notes __weak;
> -extern const void __stop_notes __weak;
> -
> Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
> void *data, size_t data_len);
> void final_note(Elf_Word *buf);
> diff --git a/kernel/crash_core.c b/kernel/crash_core.c
> index 825284baaf46..6b560cf9f374 100644
> --- a/kernel/crash_core.c
> +++ b/kernel/crash_core.c
> @@ -4,6 +4,7 @@
> * Copyright (C) 2002-2004 Eric Biederman <[email protected]>
> */
>
> +#include <linux/buildid.h>
> #include <linux/crash_core.h>
> #include <linux/utsname.h>
> #include <linux/vmalloc.h>
> @@ -378,51 +379,13 @@ phys_addr_t __weak paddr_vmcoreinfo_note(void)
> }
> EXPORT_SYMBOL(paddr_vmcoreinfo_note);
>
> -#define NOTES_SIZE (&__stop_notes - &__start_notes)
> -#define BUILD_ID_MAX SHA1_DIGEST_SIZE
> -#define NT_GNU_BUILD_ID 3
> -
> -struct elf_note_section {
> - struct elf_note n_hdr;
> - u8 n_data[];
> -};
> -
> /*
> * Add build ID from .notes section as generated by the GNU ld(1)
> * or LLVM lld(1) --build-id option.
> */
> static void add_build_id_vmcoreinfo(void)
> {
> - char build_id[BUILD_ID_MAX * 2 + 1];
> - int n_remain = NOTES_SIZE;
> -
> - while (n_remain >= sizeof(struct elf_note)) {
> - const struct elf_note_section *note_sec =
> - &__start_notes + NOTES_SIZE - n_remain;
> - const u32 n_namesz = note_sec->n_hdr.n_namesz;
> -
> - if (note_sec->n_hdr.n_type == NT_GNU_BUILD_ID &&
> - n_namesz != 0 &&
> - !strcmp((char *)&note_sec->n_data[0], "GNU")) {
> - if (note_sec->n_hdr.n_descsz <= BUILD_ID_MAX) {
> - const u32 n_descsz = note_sec->n_hdr.n_descsz;
> - const u8 *s = &note_sec->n_data[n_namesz];
> -
> - s = PTR_ALIGN(s, 4);
> - bin2hex(build_id, s, n_descsz);
> - build_id[2 * n_descsz] = '\0';
> - VMCOREINFO_BUILD_ID(build_id);
> - return;
> - }
> - pr_warn("Build ID is too large to include in vmcoreinfo: %u > %u\n",
> - note_sec->n_hdr.n_descsz,
> - BUILD_ID_MAX);
> - return;
> - }
> - n_remain -= sizeof(struct elf_note) +
> - ALIGN(note_sec->n_hdr.n_namesz, 4) +
> - ALIGN(note_sec->n_hdr.n_descsz, 4);
> - }
> + VMCOREINFO_BUILD_ID(vmlinux_build_id);
> }

The function add_build_id_vmcoreinfo() is used in
crash_save_vmcoreinfo_init() in this context:


VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
add_build_id_vmcoreinfo();
VMCOREINFO_PAGESIZE(PAGE_SIZE);

VMCOREINFO_SYMBOL(init_uts_ns);
VMCOREINFO_OFFSET(uts_namespace, name);
VMCOREINFO_SYMBOL(node_online_map);

The function is not longer need. VMCOREINFO_BUILD_ID()
can be used directly:

VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
VMCOREINFO_BUILD_ID(vmlinux_build_id);
VMCOREINFO_PAGESIZE(PAGE_SIZE);

VMCOREINFO_SYMBOL(init_uts_ns);
VMCOREINFO_OFFSET(uts_namespace, name);
VMCOREINFO_SYMBOL(node_online_map);


Best Regards,
Petr


>
> static int __init crash_save_vmcoreinfo_init(void)
> --
> https://chromeos.dev

2021-04-07 22:37:35

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

On Tue 2021-03-30 20:05:12, Stephen Boyd wrote:
> 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 a new printk
> format '%pS[R]b' for "pointer symbolic 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.
>
> Example:
>
> WARNING: CPU: 3 PID: 3373 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
> Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE hci_uart <modules trimmed>
> CPU: 3 PID: 3373 Comm: bash Not tainted 5.11 #12 a8c0d47f7051f3e6670ceaea724af66a39c6cec8
> Hardware name: Google Lazor (rev3+) with KB Backlight (DT)
> pstate: 00400009 (nzcv daif +PAN -UAO -TCO BTYPE=--)
> pc : lkdtm_WARNING+0x28/0x30 [lkdtm]
> lr : lkdtm_do_action+0x24/0x40 [lkdtm]
> sp : ffffffc013febca0
> x29: ffffffc013febca0 x28: ffffff88d9438040
> x27: 0000000000000000 x26: 0000000000000000
> x25: 0000000000000000 x24: ffffffdd0e9772c0
> x23: 0000000000000020 x22: ffffffdd0e975366
> x21: ffffffdd0e9772e0 x20: ffffffc013febde0
> x19: 0000000000000008 x18: 0000000000000000
> x17: 0000000000000000 x16: 0000000000000037
> x15: ffffffdd102ab174 x14: 0000000000000003
> x13: 0000000000000004 x12: 0000000000000000
> x11: 0000000000000000 x10: 0000000000000000
> x9 : 0000000000000001 x8 : ffffffdd0e979000
> x7 : 0000000000000000 x6 : ffffffdd10ff6b54
> x5 : 0000000000000000 x4 : 0000000000000000
> x3 : ffffffc013feb938 x2 : ffffff89fef05a70
> x1 : ffffff89feef5788 x0 : ffffffdd0e9772e0
> Call trace:
> lkdtm_WARNING+0x28/0x30 [lkdtm 6c2215028606bda50de823490723dc4bc5bf46f9]
> direct_entry+0x16c/0x1b4 [lkdtm 6c2215028606bda50de823490723dc4bc5bf46f9]
> full_proxy_write+0x74/0xa4
> vfs_write+0xec/0x2e8
> ksys_write+0x84/0xf0
> __arm64_sys_write+0x24/0x30
> el0_svc_common+0xf4/0x1c0
> do_el0_svc_compat+0x28/0x3c
> el0_svc_compat+0x10/0x1c
> el0_sync_compat_handler+0xa8/0xcc
> el0_sync_compat+0x178/0x180
> ---[ end trace f89bc7f5417cbcc6 ]---

I used this test patch:

diff --git a/lib/test_printf.c b/lib/test_printf.c
index 95a2f82427c7..06716948de3c 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -686,6 +686,7 @@ test_pointer(void)
kernel_ptr();
struct_resource();
addr();
+ BUG();
escaped_str();
hex_string();
mac();

Then I did

# modprobe test_printf

and got the following:

[ 125.707281][ T1837] test_printf: loaded.
[ 125.707707][ T1837] ------------[ cut here ]------------
[ 125.708227][ T1837] kernel BUG at lib/test_printf.c:689!
[ 125.709102][ T1837] invalid opcode: 0000 [#1] SMP NOPTI
[ 125.709111][ T1837] CPU: 3 PID: 1837 Comm: modprobe Kdump: loaded Tainted: G E 5.12.0-rc6-default+ #171 00000080ffffffff000000000000000000000000
[ 125.712102][ T1837] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.12.0-59-gc9ba527-rebuilt.opensuse.org 04/01/2014
[ 125.712191][ T1837] RIP: 0010:test_printf_init+0x561/0xc99 [test_printf c2388ff0552611501b4d2ad58d8e5ca441d9a350]
[ 125.712233][ T1837] Code: 00 48 c7 c7 b8 96 0f c0 e8 19 f9 ff ff b9 ab 00 00 00 48 c7 c2 93 96 0f c0 be 08 00 00 00 48 c7 c7 af 96 0f c0 e8 fc f8 ff ff <0f> 0b 8b 05 44 07 00 00 8b 35 3a 07 00 00 8b 1d 3c 07 00 00 85 c0
[ 125.712235][ T1837] RSP: 0018:ffffb060004abc78 EFLAGS: 00010282
[ 125.712240][ T1837] RAX: 0000000000000000 RBX: ffffb060004abc80 RCX: ffffd05fffc00b70
[ 125.712241][ T1837] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffffad352fd5
[ 125.712242][ T1837] RBP: ffffffffc00fd367 R08: 0000000000000001 R09: 0000000000000001
[ 125.712243][ T1837] R10: 0000000000000000 R11: 0000000000000001 R12: ffff9b6cc49ffc00
[ 125.712244][ T1837] R13: 0000000000000001 R14: ffff9b6cc2ee0000 R15: ffffb060004abe90
[ 125.712247][ T1837] FS: 00007f70c4466b80(0000) GS:ffff9b6d3fc00000(0000) knlGS:0000000000000000
[ 125.712248][ T1837] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 125.712249][ T1837] CR2: 00007ffdb02e8b38 CR3: 000000010b6f2002 CR4: 0000000000370ee0
[ 125.712257][ T1837] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 125.712257][ T1837] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 125.712258][ T1837] Call Trace:
[ 125.712275][ T1837] ? __test+0x13c/0x149 [test_printf]
[ 125.723404][ T1837] ? rcu_read_lock_sched_held+0x52/0x80
[ 125.723425][ T1837] do_one_initcall+0x5b/0x2d0
[ 125.724367][ T1837] do_init_module+0x5b/0x21c
[ 125.724377][ T1837] load_module+0x1eaa/0x23c0
[ 125.725381][ T1837] ? show_modinfo_version+0x30/0x30
[ 125.725422][ T1837] ? __do_sys_finit_module+0xad/0x110
[ 125.725425][ T1837] __do_sys_finit_module+0xad/0x110
[ 125.725431][ T1837] do_syscall_64+0x33/0x40
[ 125.725464][ T1837] entry_SYSCALL_64_after_hwframe+0x44/0xae

It shows wrong build id for vmlinux.
And it does not show the build if for the module at all.


readelf shows the following:

# readelf -Wn /lib/modules/5.12.0-rc6-default+/kernel/lib/test_printf.ko

Displaying notes found in: .note.gnu.build-id
Owner Data size Description
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: c2388ff0552611501b4d2ad58d8e5ca441d9a350


Best Regards,
Petr

2021-04-07 23:18:21

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

On Wed, Apr 07, 2021 at 05:03:47PM +0200, Petr Mladek wrote:
> On Tue 2021-03-30 20:05:12, Stephen Boyd wrote:

> Please add something like:
>
> /* Keep BUILD_ID_SIZE_MAX in sync with the below used %20phN */
> BUILD_BUG_ON(BUILD_ID_SIZE_MAX != 20)
>
> to make sure the the hard

Why not static assert?

--
With Best Regards,
Andy Shevchenko


2021-04-08 01:19:02

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

Quoting Petr Mladek (2021-04-07 07:03:19)
> On Tue 2021-03-30 20:05:11, Stephen Boyd wrote:
> > Add the running kernel's build ID[1] to the stacktrace information
> > header. This makes it simpler for developers to locate the vmlinux with
> > full debuginfo for a particular kernel stacktrace. Combined with
> > scripts/decode_stracktrace.sh, a developer can download the correct
> > vmlinux from a debuginfod[2] server and find the exact file and line
> > number for the functions plus offsets in a stacktrace.
> >
> > This is especially useful for pstore crash debugging where the kernel
> > crashes are recorded in the pstore logs and the recovery kernel is
> > different or the debuginfo doesn't exist on the device due to space
> > concerns (the data can be large and a security concern). The stacktrace
> > can be analyzed after the crash by using the build ID to find the
> > matching vmlinux and understand where in the function something went
> > wrong.
> >
> > Example stacktrace from lkdtm:
> >
> > WARNING: CPU: 4 PID: 3255 at drivers/misc/lkdtm/bugs.c:83 lkdtm_WARNING+0x28/0x30 [lkdtm]
> > Modules linked in: lkdtm rfcomm algif_hash algif_skcipher af_alg xt_cgroup uinput xt_MASQUERADE
> > CPU: 4 PID: 3255 Comm: bash Not tainted 5.11 #3 aa23f7a1231c229de205662d5a9e0d4c580f19a1
>
> I tried "echo l >/proc/sysrq-trigger" and get:
>
> [ 75.123014] CPU: 1 PID: 5079 Comm: bash Kdump: loaded Not tainted 5.12.0-rc6-default+ #169 00000080ffffffff0000000000000000
> 00000000
>
> It does not look like an unique ID. I have already reported this for
> v2. But you sent v3 just 8 hours later before I was able to provide
> more details.

Cool thanks! I'll look into it. Does kdump get the build ID properly
without these patches applied?

2021-04-08 05:39:48

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 12/12] kdump: Use vmlinux_build_id to simplify

Quoting Petr Mladek (2021-04-07 10:03:28)
> On Tue 2021-03-30 20:05:20, Stephen Boyd wrote:
> > We can use the vmlinux_build_id array here now instead of open coding
> > it. This mostly consolidates code.
> >
> > Cc: Jiri Olsa <[email protected]>
> > Cc: Alexei Starovoitov <[email protected]>
> > Cc: Jessica Yu <[email protected]>
> > Cc: Evan Green <[email protected]>
> > Cc: Hsin-Yi Wang <[email protected]>
> > Cc: Dave Young <[email protected]>
> > Cc: Baoquan He <[email protected]>
> > Cc: Vivek Goyal <[email protected]>
> > Cc: <[email protected]>
> > Signed-off-by: Stephen Boyd <[email protected]>
> > ---
> > include/linux/crash_core.h | 6 +-----
> > kernel/crash_core.c | 41 ++------------------------------------
> > 2 files changed, 3 insertions(+), 44 deletions(-)
> >
> > diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
> > index 206bde8308b2..fb8ab99bb2ee 100644
> > --- a/include/linux/crash_core.h
> > +++ b/include/linux/crash_core.h
> > @@ -39,7 +39,7 @@ phys_addr_t paddr_vmcoreinfo_note(void);
> > #define VMCOREINFO_OSRELEASE(value) \
> > vmcoreinfo_append_str("OSRELEASE=%s\n", value)
> > #define VMCOREINFO_BUILD_ID(value) \
> > - vmcoreinfo_append_str("BUILD-ID=%s\n", value)
> > + vmcoreinfo_append_str("BUILD-ID=%20phN\n", value)
>
> Please, add also build check that BUILD_ID_MAX == 20.
>

I added a BUILD_BUG_ON() in kernel/crash_core.c. I tried static_assert()
here but got mixed ISO errors from gcc-10, although it feels like it
should work.

In file included from ./arch/arm64/include/asm/cmpxchg.h:10,
from ./arch/arm64/include/asm/atomic.h:16,
from ./include/linux/atomic.h:7,
from ./include/linux/mm_types_task.h:13,
from ./include/linux/mm_types.h:5,
from ./include/linux/buildid.h:5,
from kernel/crash_core.c:7:
kernel/crash_core.c: In function 'crash_save_vmcoreinfo_init':
./include/linux/build_bug.h:78:41: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
| ^~~~~~~~~~~~~~
./include/linux/build_bug.h:77:34: note: in expansion of macro '__static_assert'
77 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
| ^~~~~~~~~~~~~~~
./include/linux/crash_core.h:42:2: note: in expansion of macro 'static_assert'
42 | static_assert(ARRAY_SIZE(value) == BUILD_ID_SIZE_MAX); \
| ^~~~~~~~~~~~~
kernel/crash_core.c:401:2: note: in expansion of macro 'VMCOREINFO_BUILD_ID'
401 | VMCOREINFO_BUILD_ID(vmlinux_build_id);

>
> The function add_build_id_vmcoreinfo() is used in
> crash_save_vmcoreinfo_init() in this context:
>
>
> VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
> add_build_id_vmcoreinfo();
> VMCOREINFO_PAGESIZE(PAGE_SIZE);
>
> VMCOREINFO_SYMBOL(init_uts_ns);
> VMCOREINFO_OFFSET(uts_namespace, name);
> VMCOREINFO_SYMBOL(node_online_map);
>
> The function is not longer need. VMCOREINFO_BUILD_ID()
> can be used directly:
>
> VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
> VMCOREINFO_BUILD_ID(vmlinux_build_id);
> VMCOREINFO_PAGESIZE(PAGE_SIZE);
>
> VMCOREINFO_SYMBOL(init_uts_ns);
> VMCOREINFO_OFFSET(uts_namespace, name);
> VMCOREINFO_SYMBOL(node_online_map);
>
>

Thanks. Makes sense. I've rolled that in.

2021-04-08 05:47:44

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

Quoting Petr Mladek (2021-04-07 06:42:38)
>
> I think that you need to use something like:
>
> #ifdef CONFIG_STACKTRACE_BUILD_ID
> #define BUILD_ID_FTM " %20phN"
> #define BUILD_ID_VAL vmlinux_build_id
> #else
> #define BUILD_ID_FTM "%s"
> #define BUILD_ID_VAL ""
> #endif
>
> printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s" BUILD_ID_FTM "\n",
> log_lvl, raw_smp_processor_id(), current->pid, current->comm,
> kexec_crash_loaded() ? "Kdump: loaded " : "",
> print_tainted(),
> init_utsname()->release,
> (int)strcspn(init_utsname()->version, " "),
> init_utsname()->version,
> BUILD_ID_VAL);
>

Thanks. I didn't see this warning but I see it now after compiling
again. Not sure how I missed this one. I've rolled in this fix as well.

2021-04-08 06:23:30

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

Quoting Petr Mladek (2021-04-07 07:03:19)
> # readelf -Wn vmlinux-5.12.0-rc6-default+
>
> Displaying notes found in: .notes
> Owner Data size Description
> Xen 0x00000006 Unknown note type: (0x00000006) description data: 6c 69 6e 75 78 00
> Xen 0x00000004 Unknown note type: (0x00000007) description data: 32 2e 36 00
> Xen 0x00000008 Unknown note type: (0x00000005) description data: 78 65 6e 2d 33 2e 30 00
> Xen 0x00000008 Unknown note type: (0x00000003) description data: 00 00 00 ffffff80 ffffffff ffffffff ffffffff ffffffff
>

Thanks for the readelf output. That was most helpful to see what's going
on. The buildid code isn't prepared for this it seems. We'll need to
check the note data to see if it starts with "GNU\0" and if it does then
we use it as the buildid. I'm not sure what this Xen note is that has a
type of 3 but I think that's OK, it won't have "GNU" in there so we
should ignore it. Does this patch work?

----8<----
diff --git a/lib/buildid.c b/lib/buildid.c
index 483abf71eeec..f281dbe6fb83 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -25,6 +25,7 @@ static int parse_build_id_buf(unsigned char *build_id,

if (nhdr->n_type == BUILD_ID &&
nhdr->n_namesz == sizeof("GNU") &&
+ !strcmp((char *)(nhdr + 1), "GNU") &&
nhdr->n_descsz > 0 &&
nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
memcpy(build_id,

2021-04-08 06:59:49

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

Quoting Petr Mladek (2021-04-07 08:03:47)
> On Tue 2021-03-30 20:05:12, Stephen Boyd wrote:
> > 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).
> >
> > @@ -359,15 +369,17 @@ int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
> >
> > /* 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);
> >
> > @@ -379,8 +391,12 @@ static int __sprint_symbol(char *buffer, unsigned long address,
> > if (add_offset)
> > len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
>
> Please add something like:
>
> /* Keep BUILD_ID_SIZE_MAX in sync with the below used %20phN */
> BUILD_BUG_ON(BUILD_ID_SIZE_MAX != 20)
>

Done. Hopefully the "GNU" string check also fixes this module problem
you're seeing.

2021-04-08 10:14:43

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

On Wed 2021-04-07 23:20:32, Stephen Boyd wrote:
> Quoting Petr Mladek (2021-04-07 07:03:19)
> > # readelf -Wn vmlinux-5.12.0-rc6-default+
> >
> > Displaying notes found in: .notes
> > Owner Data size Description
> > Xen 0x00000006 Unknown note type: (0x00000006) description data: 6c 69 6e 75 78 00
> > Xen 0x00000004 Unknown note type: (0x00000007) description data: 32 2e 36 00
> > Xen 0x00000008 Unknown note type: (0x00000005) description data: 78 65 6e 2d 33 2e 30 00
> > Xen 0x00000008 Unknown note type: (0x00000003) description data: 00 00 00 ffffff80 ffffffff ffffffff ffffffff ffffffff
> >
>
> Thanks for the readelf output. That was most helpful to see what's going
> on. The buildid code isn't prepared for this it seems. We'll need to
> check the note data to see if it starts with "GNU\0" and if it does then
> we use it as the buildid. I'm not sure what this Xen note is that has a
> type of 3 but I think that's OK, it won't have "GNU" in there so we
> should ignore it. Does this patch work?
>
> ----8<----
> diff --git a/lib/buildid.c b/lib/buildid.c
> index 483abf71eeec..f281dbe6fb83 100644
> --- a/lib/buildid.c
> +++ b/lib/buildid.c
> @@ -25,6 +25,7 @@ static int parse_build_id_buf(unsigned char *build_id,
>
> if (nhdr->n_type == BUILD_ID &&
> nhdr->n_namesz == sizeof("GNU") &&
> + !strcmp((char *)(nhdr + 1), "GNU") &&
> nhdr->n_descsz > 0 &&
> nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
> memcpy(build_id,

It helped with the vmlinux buildid. I see the following:

[ 551.435942][ T1803] test_printf: loaded.
[ 551.436667][ T1803] ------------[ cut here ]------------
[ 551.437561][ T1803] kernel BUG at lib/test_printf.c:689!
[ 551.438352][ T1803] invalid opcode: 0000 [#1] SMP NOPTI
[ 551.438359][ T1803] CPU: 3 PID: 1803 Comm: modprobe Kdump: loaded Tainted: G E 5.12.0-rc6-default+ #176 e51781e52aaf4d6dfea7a18574c104c8bfd7c37f
[ 551.438363][ T1803] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.12.0-59-gc9ba527-rebuilt.opensuse.org 04/01/2014
[ 551.438365][ T1803] RIP: 0010:test_printf_init+0x561/0xc99 [test_printf c2388ff0552611501b4d2ad58d8e5ca441d9a350]
[ 551.443090][ T1803] Code: 00 48 c7 c7 b8 36 1b c0 e8 19 f9 ff ff b9 ab 00 00 00 48 c7 c2 93 36 1b c0 be 08 00 00 00 48 c7 c7 af 36 1b c0 e8 fc f8 ff ff <0f> 0b 8b 05 44 07 00 00 8b 35 3a 07 00 00 8b 1d 3c 07 00 00 85 c0
[ 551.443094][ T1803] RSP: 0018:ffffb62c0039bc78 EFLAGS: 00010282
[ 551.443096][ T1803] RAX: 0000000000000000 RBX: ffffb62c0039bc80 RCX: ffffd62bffc00b70
[ 551.443098][ T1803] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffffa0352fd5
[ 551.443099][ T1803] RBP: ffffffffc01b7367 R08: 0000000000000001 R09: 0000000000000001
[ 551.443100][ T1803] R10: 0000000000000000 R11: 0000000000000001 R12: ffff9bc08c87c820
[ 551.443101][ T1803] R13: 0000000000000001 R14: ffff9bc0d2798480 R15: ffffb62c0039be90
[ 551.443102][ T1803] FS: 00007f5767485b80(0000) GS:ffff9bc0ffc00000(0000) knlGS:0000000000000000
[ 551.443103][ T1803] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 551.443105][ T1803] CR2: 00007f5766b36ef0 CR3: 0000000100368004 CR4: 0000000000370ee0
[ 551.443108][ T1803] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 551.443108][ T1803] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 551.443109][ T1803] Call Trace:
[ 551.443113][ T1803] ? __test+0x13c/0x149 [test_printf]
[ 551.443116][ T1803] ? rcu_read_lock_sched_held+0x52/0x80
[ 551.443120][ T1803] do_one_initcall+0x5b/0x2d0
[ 551.443125][ T1803] do_init_module+0x5b/0x21c
[ 551.443127][ T1803] load_module+0x1eaa/0x23c0
[ 551.443130][ T1803] ? show_modinfo_version+0x30/0x30
[ 551.443134][ T1803] ? __do_sys_finit_module+0xad/0x110
[ 551.443135][ T1803] __do_sys_finit_module+0xad/0x110
[ 551.443138][ T1803] do_syscall_64+0x33/0x40
[ 551.443139][ T1803] entry_SYSCALL_64_after_hwframe+0x44/0xae
[ 551.443143][ T1803] RIP: 0033:0x7f5766b5b2a9
[

Note that it still does not show the build id for the module. It fails
in the module init call and the build id should be already initialized
at this stage.

One more thing. I am not familiar with the elf-related code.
Is it safe to access (nhdr + 1)? Do we need a check that
it is still withing the given section?

Best Regards,
Petr

2021-04-08 10:19:11

by Baoquan He

[permalink] [raw]
Subject: Re: [PATCH v3 12/12] kdump: Use vmlinux_build_id to simplify

On 04/07/21 at 07:03pm, Petr Mladek wrote:
> On Tue 2021-03-30 20:05:20, Stephen Boyd wrote:
> > We can use the vmlinux_build_id array here now instead of open coding
> > it. This mostly consolidates code.
> >
> > Cc: Jiri Olsa <[email protected]>
> > Cc: Alexei Starovoitov <[email protected]>
> > Cc: Jessica Yu <[email protected]>
> > Cc: Evan Green <[email protected]>
> > Cc: Hsin-Yi Wang <[email protected]>
> > Cc: Dave Young <[email protected]>
> > Cc: Baoquan He <[email protected]>
> > Cc: Vivek Goyal <[email protected]>
> > Cc: <[email protected]>
> > Signed-off-by: Stephen Boyd <[email protected]>
> > ---
> > include/linux/crash_core.h | 6 +-----
> > kernel/crash_core.c | 41 ++------------------------------------
> > 2 files changed, 3 insertions(+), 44 deletions(-)
> >
> > diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
> > index 206bde8308b2..fb8ab99bb2ee 100644
> > --- a/include/linux/crash_core.h
> > +++ b/include/linux/crash_core.h
> > @@ -39,7 +39,7 @@ phys_addr_t paddr_vmcoreinfo_note(void);
> > #define VMCOREINFO_OSRELEASE(value) \
> > vmcoreinfo_append_str("OSRELEASE=%s\n", value)
> > #define VMCOREINFO_BUILD_ID(value) \
> > - vmcoreinfo_append_str("BUILD-ID=%s\n", value)
> > + vmcoreinfo_append_str("BUILD-ID=%20phN\n", value)

I may miss something, wondering why we need add '20' here.

>
> Please, add also build check that BUILD_ID_MAX == 20.
>
>
> > #define VMCOREINFO_PAGESIZE(value) \
> > vmcoreinfo_append_str("PAGESIZE=%ld\n", value)
> > #define VMCOREINFO_SYMBOL(name) \
> > @@ -69,10 +69,6 @@ extern unsigned char *vmcoreinfo_data;
> > extern size_t vmcoreinfo_size;
> > extern u32 *vmcoreinfo_note;
> >
> > -/* raw contents of kernel .notes section */
> > -extern const void __start_notes __weak;
> > -extern const void __stop_notes __weak;
> > -
> > Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
> > void *data, size_t data_len);
> > void final_note(Elf_Word *buf);
> > diff --git a/kernel/crash_core.c b/kernel/crash_core.c
> > index 825284baaf46..6b560cf9f374 100644
> > --- a/kernel/crash_core.c
> > +++ b/kernel/crash_core.c
> > @@ -4,6 +4,7 @@
> > * Copyright (C) 2002-2004 Eric Biederman <[email protected]>
> > */
> >
> > +#include <linux/buildid.h>
> > #include <linux/crash_core.h>
> > #include <linux/utsname.h>
> > #include <linux/vmalloc.h>
> > @@ -378,51 +379,13 @@ phys_addr_t __weak paddr_vmcoreinfo_note(void)
> > }
> > EXPORT_SYMBOL(paddr_vmcoreinfo_note);
> >
> > -#define NOTES_SIZE (&__stop_notes - &__start_notes)
> > -#define BUILD_ID_MAX SHA1_DIGEST_SIZE
> > -#define NT_GNU_BUILD_ID 3
> > -
> > -struct elf_note_section {
> > - struct elf_note n_hdr;
> > - u8 n_data[];
> > -};
> > -
> > /*
> > * Add build ID from .notes section as generated by the GNU ld(1)
> > * or LLVM lld(1) --build-id option.
> > */
> > static void add_build_id_vmcoreinfo(void)
> > {
> > - char build_id[BUILD_ID_MAX * 2 + 1];
> > - int n_remain = NOTES_SIZE;
> > -
> > - while (n_remain >= sizeof(struct elf_note)) {
> > - const struct elf_note_section *note_sec =
> > - &__start_notes + NOTES_SIZE - n_remain;
> > - const u32 n_namesz = note_sec->n_hdr.n_namesz;
> > -
> > - if (note_sec->n_hdr.n_type == NT_GNU_BUILD_ID &&
> > - n_namesz != 0 &&
> > - !strcmp((char *)&note_sec->n_data[0], "GNU")) {
> > - if (note_sec->n_hdr.n_descsz <= BUILD_ID_MAX) {
> > - const u32 n_descsz = note_sec->n_hdr.n_descsz;
> > - const u8 *s = &note_sec->n_data[n_namesz];
> > -
> > - s = PTR_ALIGN(s, 4);
> > - bin2hex(build_id, s, n_descsz);
> > - build_id[2 * n_descsz] = '\0';
> > - VMCOREINFO_BUILD_ID(build_id);
> > - return;
> > - }
> > - pr_warn("Build ID is too large to include in vmcoreinfo: %u > %u\n",
> > - note_sec->n_hdr.n_descsz,
> > - BUILD_ID_MAX);
> > - return;
> > - }
> > - n_remain -= sizeof(struct elf_note) +
> > - ALIGN(note_sec->n_hdr.n_namesz, 4) +
> > - ALIGN(note_sec->n_hdr.n_descsz, 4);
> > - }
> > + VMCOREINFO_BUILD_ID(vmlinux_build_id);
> > }
>
> The function add_build_id_vmcoreinfo() is used in
> crash_save_vmcoreinfo_init() in this context:
>
>
> VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
> add_build_id_vmcoreinfo();
> VMCOREINFO_PAGESIZE(PAGE_SIZE);
>
> VMCOREINFO_SYMBOL(init_uts_ns);
> VMCOREINFO_OFFSET(uts_namespace, name);
> VMCOREINFO_SYMBOL(node_online_map);
>
> The function is not longer need. VMCOREINFO_BUILD_ID()
> can be used directly:
>
> VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
> VMCOREINFO_BUILD_ID(vmlinux_build_id);
> VMCOREINFO_PAGESIZE(PAGE_SIZE);
>
> VMCOREINFO_SYMBOL(init_uts_ns);
> VMCOREINFO_OFFSET(uts_namespace, name);
> VMCOREINFO_SYMBOL(node_online_map);
>
>
> Best Regards,
> Petr
>
>
> >
> > static int __init crash_save_vmcoreinfo_init(void)
> > --
> > https://chromeos.dev
>

2021-04-08 12:08:07

by Jessica Yu

[permalink] [raw]
Subject: Re: [PATCH v3 02/12] buildid: Stash away kernels build ID on init

+++ Stephen Boyd [30/03/21 20:05 -0700]:
[snipped]
>diff --git a/lib/buildid.c b/lib/buildid.c
>index 010ab0674cb9..b939bbc59233 100644
>--- a/lib/buildid.c
>+++ b/lib/buildid.c
>@@ -1,6 +1,7 @@
> // SPDX-License-Identifier: GPL-2.0
>
> #include <linux/buildid.h>
>+#include <linux/cache.h>
> #include <linux/elf.h>
> #include <linux/kernel.h>
> #include <linux/pagemap.h>
>@@ -171,3 +172,19 @@ int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
> {
> return parse_build_id_buf(build_id, NULL, buf, buf_size);
> }
>+
>+unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init;
>+
>+/**
>+ * init_vmlinux_build_id - Get the running kernel's build ID
>+ *
>+ * Return: Running kernel's build ID
>+ */

Hm, init_vmlinux_build_id() doesn't return anything, so this comment is
not accurate - maybe "Get the running kernel's build ID and store it in
vmlinux_build_id"?

>+void __init init_vmlinux_build_id(void)
>+{
>+ extern const void __start_notes __weak;
>+ extern const void __stop_notes __weak;
>+ unsigned int size = &__stop_notes - &__start_notes;
>+
>+ build_id_parse_buf(&__start_notes, vmlinux_build_id, size);
>+}

2021-04-08 13:47:11

by Jessica Yu

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

+++ Stephen Boyd [30/03/21 20:05 -0700]:
[snipped]
>diff --git a/kernel/module.c b/kernel/module.c
>index 30479355ab85..6f5bc1b046a5 100644
>--- a/kernel/module.c
>+++ b/kernel/module.c
>@@ -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>
>@@ -2770,6 +2771,20 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
> }
> mod->core_kallsyms.num_symtab = ndst;
> }
>+
>+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;
>+ }
>+}

Why not just look for the .note.gnu.build-id section instead of trying
to parse each note section? Doesn't it always contain the build id? At
least the ld man page seems to suggest this section name should be
consistent.

Jessica

2021-04-08 14:08:09

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

On Thu, Apr 08, 2021 at 03:44:57PM +0200, Jessica Yu wrote:
> +++ Stephen Boyd [30/03/21 20:05 -0700]:

...

> > +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;
> > + }
> > +}
>
> Why not just look for the .note.gnu.build-id section instead of trying
> to parse each note section? Doesn't it always contain the build id? At
> least the ld man page seems to suggest this section name should be
> consistent.

Interesting idea (in positive way!), I'm wondering what Clang does in such
case.

--
With Best Regards,
Andy Shevchenko


2021-04-08 18:53:52

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 02/12] buildid: Stash away kernels build ID on init

Quoting Jessica Yu (2021-04-08 05:05:33)
> +++ Stephen Boyd [30/03/21 20:05 -0700]:
> >+/**
> >+ * init_vmlinux_build_id - Get the running kernel's build ID
> >+ *
> >+ * Return: Running kernel's build ID
> >+ */
>
> Hm, init_vmlinux_build_id() doesn't return anything, so this comment is
> not accurate - maybe "Get the running kernel's build ID and store it in
> vmlinux_build_id"?
>
> >+void __init init_vmlinux_build_id(void)

Thanks! I've fixed it for the next round.

2021-04-08 19:54:15

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

Quoting Petr Mladek (2021-04-08 03:13:20)
> It helped with the vmlinux buildid. I see the following:
>
> [ 551.435942][ T1803] test_printf: loaded.
> [ 551.436667][ T1803] ------------[ cut here ]------------
> [ 551.437561][ T1803] kernel BUG at lib/test_printf.c:689!
> [ 551.438352][ T1803] invalid opcode: 0000 [#1] SMP NOPTI
> [ 551.438359][ T1803] CPU: 3 PID: 1803 Comm: modprobe Kdump: loaded Tainted: G E 5.12.0-rc6-default+ #176 e51781e52aaf4d6dfea7a18574c104c8bfd7c37f
> [ 551.438363][ T1803] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.12.0-59-gc9ba527-rebuilt.opensuse.org 04/01/2014
> [ 551.438365][ T1803] RIP: 0010:test_printf_init+0x561/0xc99 [test_printf c2388ff0552611501b4d2ad58d8e5ca441d9a350]

It shows it for the test module here.

> [ 551.443090][ T1803] Code: 00 48 c7 c7 b8 36 1b c0 e8 19 f9 ff ff b9 ab 00 00 00 48 c7 c2 93 36 1b c0 be 08 00 00 00 48 c7 c7 af 36 1b c0 e8 fc f8 ff ff <0f> 0b 8b 05 44 07 00 00 8b 35 3a 07 00 00 8b 1d 3c 07 00 00 85 c0
> [ 551.443094][ T1803] RSP: 0018:ffffb62c0039bc78 EFLAGS: 00010282
> [ 551.443096][ T1803] RAX: 0000000000000000 RBX: ffffb62c0039bc80 RCX: ffffd62bffc00b70
> [ 551.443098][ T1803] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffffa0352fd5
> [ 551.443099][ T1803] RBP: ffffffffc01b7367 R08: 0000000000000001 R09: 0000000000000001
> [ 551.443100][ T1803] R10: 0000000000000000 R11: 0000000000000001 R12: ffff9bc08c87c820
> [ 551.443101][ T1803] R13: 0000000000000001 R14: ffff9bc0d2798480 R15: ffffb62c0039be90
> [ 551.443102][ T1803] FS: 00007f5767485b80(0000) GS:ffff9bc0ffc00000(0000) knlGS:0000000000000000
> [ 551.443103][ T1803] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> [ 551.443105][ T1803] CR2: 00007f5766b36ef0 CR3: 0000000100368004 CR4: 0000000000370ee0
> [ 551.443108][ T1803] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
> [ 551.443108][ T1803] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
> [ 551.443109][ T1803] Call Trace:
> [ 551.443113][ T1803] ? __test+0x13c/0x149 [test_printf]

But not here. I missed a place in the x86 code, printk_stack_address()
uses %pB, so I'll need to introduce %pBb to indicate that we're printing
a backtrace with a build ID, oof!

It must be obvious by now but I didn't test on x86. Let me go scrounge
for some hardware...

> [ 551.443116][ T1803] ? rcu_read_lock_sched_held+0x52/0x80
> [ 551.443120][ T1803] do_one_initcall+0x5b/0x2d0
> [ 551.443125][ T1803] do_init_module+0x5b/0x21c
> [ 551.443127][ T1803] load_module+0x1eaa/0x23c0
> [ 551.443130][ T1803] ? show_modinfo_version+0x30/0x30
> [ 551.443134][ T1803] ? __do_sys_finit_module+0xad/0x110
> [ 551.443135][ T1803] __do_sys_finit_module+0xad/0x110
> [ 551.443138][ T1803] do_syscall_64+0x33/0x40
> [ 551.443139][ T1803] entry_SYSCALL_64_after_hwframe+0x44/0xae
> [ 551.443143][ T1803] RIP: 0033:0x7f5766b5b2a9
> [
>
> Note that it still does not show the build id for the module. It fails
> in the module init call and the build id should be already initialized
> at this stage.
>
> One more thing. I am not familiar with the elf-related code.
> Is it safe to access (nhdr + 1)? Do we need a check that
> it is still withing the given section?

Should be safe given that the elf note header is prepended to the name buffer
and the descriptor buffer. The 'n_namesz' member of the header tells us how
many bytes after the header is reserved for the name and the 'n_descsz' member
of the header tells us how many bytes after the name is reserved for the
description (where the build ID data is). I did the nhdr + 1 thing to make the
pointer point to the name directly after the header. The name is NUL terminated
per the elf spec. See the man page[1] for elf and the section about notes:

"""
Notes (Nhdr)
ELF notes allow for appending arbitrary information for the
system to use. They are largely used by core files (e_type of
ET_CORE), but many projects define their own set of extensions.
For example, the GNU tool chain uses ELF notes to pass
information from the linker to the C library.

Note sections contain a series of notes (see the struct
definitions below). Each note is followed by the name field
(whose length is defined in n_namesz) and then by the descriptor
field (whose length is defined in n_descsz) and whose starting
address has a 4 byte alignment. Neither field is defined in the
note struct due to their arbitrary lengths.
"""

[1] https://man7.org/linux/man-pages/man5/elf.5.html


Can you try this patch for x86? I'll dig up some hardware in the meantime.

-----8<----
diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c
index 7ad5eea99b2b..be2de39bf16f 100644
--- a/arch/x86/kernel/dumpstack.c
+++ b/arch/x86/kernel/dumpstack.c
@@ -69,7 +69,7 @@ static void printk_stack_address(unsigned long address, int reliable,
const char *log_lvl)
{
touch_nmi_watchdog();
- printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address);
+ printk("%s %s%pBb\n", log_lvl, reliable ? "" : "? ", (void *)address);
}

static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src,
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index 2569a4792480..f760cb839775 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -96,6 +96,7 @@ 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);
@@ -149,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;
diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index 74e792e0f7b8..b835992e76c2 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -473,6 +473,26 @@ int sprint_backtrace(char *buffer, unsigned long address)
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. */
struct kallsym_iter {
loff_t pos;
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 91a70125148c..571f9aa74b89 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -966,7 +966,9 @@ 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);

2021-04-08 20:00:09

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 04/12] module: Add printk format to add module build ID to stacktraces

Quoting Andy Shevchenko (2021-04-08 07:05:54)
> On Thu, Apr 08, 2021 at 03:44:57PM +0200, Jessica Yu wrote:
> > +++ Stephen Boyd [30/03/21 20:05 -0700]:
>
> ...
>
> > > +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;
> > > + }
> > > +}
> >
> > Why not just look for the .note.gnu.build-id section instead of trying
> > to parse each note section? Doesn't it always contain the build id? At
> > least the ld man page seems to suggest this section name should be
> > consistent.

That's basically what this code is doing. We're looking through all the
section headers and finding the ones that are notes and then
build_id_parse_buf() is checking to see if that note is a GNU type note
(name == "GNU") and is of the type NT_GNU_BUILD_ID (type == 3). We don't
need to check for a section name of ".note.gnu.build-id", we can use the
existing code in build_id_parse_buf() that looks for the name and type.

>
> Interesting idea (in positive way!), I'm wondering what Clang does in such
> case.
>

Clang also inserts a GNU build ID and it works with these patches.

2021-04-08 20:00:13

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 12/12] kdump: Use vmlinux_build_id to simplify

Quoting Baoquan He (2021-04-08 03:17:43)
> On 04/07/21 at 07:03pm, Petr Mladek wrote:
> > On Tue 2021-03-30 20:05:20, Stephen Boyd wrote:
> > > We can use the vmlinux_build_id array here now instead of open coding
> > > it. This mostly consolidates code.
> > >
> > > Cc: Jiri Olsa <[email protected]>
> > > Cc: Alexei Starovoitov <[email protected]>
> > > Cc: Jessica Yu <[email protected]>
> > > Cc: Evan Green <[email protected]>
> > > Cc: Hsin-Yi Wang <[email protected]>
> > > Cc: Dave Young <[email protected]>
> > > Cc: Baoquan He <[email protected]>
> > > Cc: Vivek Goyal <[email protected]>
> > > Cc: <[email protected]>
> > > Signed-off-by: Stephen Boyd <[email protected]>
> > > ---
> > > include/linux/crash_core.h | 6 +-----
> > > kernel/crash_core.c | 41 ++------------------------------------
> > > 2 files changed, 3 insertions(+), 44 deletions(-)
> > >
> > > diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
> > > index 206bde8308b2..fb8ab99bb2ee 100644
> > > --- a/include/linux/crash_core.h
> > > +++ b/include/linux/crash_core.h
> > > @@ -39,7 +39,7 @@ phys_addr_t paddr_vmcoreinfo_note(void);
> > > #define VMCOREINFO_OSRELEASE(value) \
> > > vmcoreinfo_append_str("OSRELEASE=%s\n", value)
> > > #define VMCOREINFO_BUILD_ID(value) \
> > > - vmcoreinfo_append_str("BUILD-ID=%s\n", value)
> > > + vmcoreinfo_append_str("BUILD-ID=%20phN\n", value)
>
> I may miss something, wondering why we need add '20' here.

The build ID is an array of 20 bytes and this format is to print 20
bytes in hex.

2021-04-08 21:09:14

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

Quoting Stephen Boyd (2021-04-08 12:52:27)
>
> Can you try this patch for x86? I'll dig up some hardware in the meantime.
>

It works for me backported to this 5.10 kernel running on this x86
device I found. I'll wait for you to confirm and then send the next
round.

CPU: 0 PID: 2719 Comm: bash Not tainted 5.10.27 #1 301c09d6713f73cc8761e0ec7c4a50f354819757
Hardware name: Google Grunt/Grunt, BIOS Google_Grunt.11031.115.0 11/15/2019
RIP: 0010:lkdtm_WARNING+0x13/0x17 [lkdtm bf9f9188770187b40e576292363651839854180c]
Code: c7 15 ae 71 c0 31 c0 e8 b1 f9 f6 c9 0f 1f 44 00 00 55 48 89 e5 0f 0b 0f 1f 44 00 00 55 48 89 e5 ff 05 f3 7d 00 00 75 02 5d c3 <0f> 0b eb fa 0f 1f 44 00 00 55 48 89 e5 8b 35 dc 7d 00 00 ff c6 89
RSP: 0018:ffffb296c1757dc0 EFLAGS: 00010202
RAX: 0000000000000026 RBX: ffffffffc071a060 RCX: 5d63534cda001100
RDX: ffff8e5faac22cf8 RSI: ffff8e5faac12e08 RDI: ffffffffc071a060
RBP: ffffb296c1757dc0 R08: 0000000000000000 R09: ffffb296c1757ba0
R10: 00000000ffffdfff R11: ffffffffc071722e R12: 0000000000000008
R13: ffff8e5fa615f000 R14: ffffffffffffffea R15: ffffffffc071a819
FS: 00007ee3373f2740(0000) GS:ffff8e5faac00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000058d943914918 CR3: 000000000f0a0000 CR4: 00000000001506f0
Call Trace:
direct_entry+0x107/0x113 [lkdtm bf9f9188770187b40e576292363651839854180c]
full_proxy_write+0x5b/0x78
vfs_write+0x145/0x40d
ksys_write+0x7e/0xde
do_syscall_64+0x43/0x55
entry_SYSCALL_64_after_hwframe+0x44/0xa9
RIP: 0033:0x7ee3372cb7f4
Code: 89 02 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 8d 05 31 40 0c 00 8b 00 85 c0 75 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 c3 0f 1f 00 55 48 89 e5 48 83 ec 20 48 89
RSP: 002b:00007ffe1c6db648 EFLAGS: 00000246 ORIG_RAX: 0000000000000001
RAX: ffffffffffffffda RBX: 0000000000000008 RCX: 00007ee3372cb7f4
RDX: 0000000000000008 RSI: 000058d944343c60 RDI: 0000000000000001
RBP: 00007ffe1c6db670 R08: 000000000000000a R09: 00007ee3373f2740
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000008
R13: 000058d944343c60 R14: 00007ee33738b5c0 R15: 00007ee3373865c0

2021-04-09 09:21:17

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v3 03/12] dump_stack: Add vmlinux build ID to stack traces

On Thu 2021-04-08 12:52:27, Stephen Boyd wrote:
> Quoting Petr Mladek (2021-04-08 03:13:20)
> > It helped with the vmlinux buildid. I see the following:
> >
> > [ 551.435942][ T1803] test_printf: loaded.
> > [ 551.436667][ T1803] ------------[ cut here ]------------
> > [ 551.437561][ T1803] kernel BUG at lib/test_printf.c:689!
> > [ 551.438352][ T1803] invalid opcode: 0000 [#1] SMP NOPTI
> > [ 551.438359][ T1803] CPU: 3 PID: 1803 Comm: modprobe Kdump: loaded Tainted: G E 5.12.0-rc6-default+ #176 e51781e52aaf4d6dfea7a18574c104c8bfd7c37f
> > [ 551.438363][ T1803] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.12.0-59-gc9ba527-rebuilt.opensuse.org 04/01/2014
> > [ 551.438365][ T1803] RIP: 0010:test_printf_init+0x561/0xc99 [test_printf c2388ff0552611501b4d2ad58d8e5ca441d9a350]
>
> It shows it for the test module here.

I see.

> > [ 551.443090][ T1803] Code: 00 48 c7 c7 b8 36 1b c0 e8 19 f9 ff ff b9 ab 00 00 00 48 c7 c2 93 36 1b c0 be 08 00 00 00 48 c7 c7 af 36 1b c0 e8 fc f8 ff ff <0f> 0b 8b 05 44 07 00 00 8b 35 3a 07 00 00 8b 1d 3c 07 00 00 85 c0
> > [ 551.443094][ T1803] RSP: 0018:ffffb62c0039bc78 EFLAGS: 00010282
> > [ 551.443096][ T1803] RAX: 0000000000000000 RBX: ffffb62c0039bc80 RCX: ffffd62bffc00b70
> > [ 551.443098][ T1803] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffffa0352fd5
> > [ 551.443099][ T1803] RBP: ffffffffc01b7367 R08: 0000000000000001 R09: 0000000000000001
> > [ 551.443100][ T1803] R10: 0000000000000000 R11: 0000000000000001 R12: ffff9bc08c87c820
> > [ 551.443101][ T1803] R13: 0000000000000001 R14: ffff9bc0d2798480 R15: ffffb62c0039be90
> > [ 551.443102][ T1803] FS: 00007f5767485b80(0000) GS:ffff9bc0ffc00000(0000) knlGS:0000000000000000
> > [ 551.443103][ T1803] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> > [ 551.443105][ T1803] CR2: 00007f5766b36ef0 CR3: 0000000100368004 CR4: 0000000000370ee0
> > [ 551.443108][ T1803] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
> > [ 551.443108][ T1803] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
> > [ 551.443109][ T1803] Call Trace:
> > [ 551.443113][ T1803] ? __test+0x13c/0x149 [test_printf]
>
> But not here. I missed a place in the x86 code, printk_stack_address()
> uses %pB, so I'll need to introduce %pBb to indicate that we're printing
> a backtrace with a build ID, oof!
>
> Can you try this patch for x86? I'll dig up some hardware in the meantime.

The patch helped. Now, I see:

55.324174][ T1843] test_printf: loaded.
[ 55.326875][ T1843] ------------[ cut here ]------------
[ 55.329644][ T1843] kernel BUG at lib/test_printf.c:689!
[ 55.331642][ T1843] invalid opcode: 0000 [#1] SMP NOPTI
[ 55.331651][ T1843] CPU: 3 PID: 1843 Comm: modprobe Kdump: loaded Tainted: G E 5.12.0-rc6-default+ #177 4a028b3004ded4cbaa81d717e520bff895755225
[ 55.331658][ T1843] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.12.0-59-gc9ba527-rebuilt.opensuse.org 04/01/2014
[ 55.331660][ T1843] RIP: 0010:test_printf_init+0x561/0xc99 [test_printf c2388ff0552611501b4d2ad58d8e5ca441d9a350]
[ 55.331677][ T1843] Code: 00 48 c7 c7 b8 b6 42 c0 e8 19 f9 ff ff b9 ab 00 00 00 48 c7 c2 93 b6 42 c0 be 08 00 00 00 48 c7 c7 af b6 42 c0 e8 fc f8 ff ff <0f> 0b 8b 05 44 07 00 00 8b 35 3a 07 00 00 8b 1d 3c 07 00 00 85 c0
[ 55.331680][ T1843] RSP: 0018:ffffa1d800c1fc78 EFLAGS: 00010282
[ 55.331685][ T1843] RAX: 0000000000000000 RBX: ffffa1d800c1fc80 RCX: ffffc1d7ffc00b70
[ 55.331688][ T1843] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffffa5352ff5
[ 55.331690][ T1843] RBP: ffffffffc042f367 R08: 0000000000000001 R09: 0000000000000001
[ 55.331693][ T1843] R10: 0000000000000000 R11: 0000000000000001 R12: ffff940290b29480
[ 55.331695][ T1843] R13: 0000000000000001 R14: ffff940286a83f80 R15: ffffa1d800c1fe90
[ 55.331697][ T1843] FS: 00007f870ba13b80(0000) GS:ffff9402ffc00000(0000) knlGS:0000000000000000
[ 55.331701][ T1843] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 55.331704][ T1843] CR2: 00007f870b0c4ef0 CR3: 000000010ac8a001 CR4: 0000000000370ee0
[ 55.331712][ T1843] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 55.331715][ T1843] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[ 55.359095][ T1843] Call Trace:
[ 55.359100][ T1843] ? __test+0x13c/0x149 [test_printf c2388ff0552611501b4d2ad58d8e5ca441d9a350]
g[ 55.359107][ T1843] ? rcu_read_lock_sched_held+0x52/0x80
[ 55.359112][ T1843] do_one_initcall+0x5b/0x2d0

Best Regards,
Petr