Blame SOURCES/0006-bt-x86_64-filter-out-idle-task-stack.patch

ae0abc
From 6833262bf87177d8affe4f91b2e7d2c76ecdf636 Mon Sep 17 00:00:00 2001
ae0abc
From: Qi Zheng <zhengqi.arch@bytedance.com>
ae0abc
Date: Tue, 24 May 2022 20:25:53 +0800
ae0abc
Subject: [PATCH 06/15] bt: x86_64: filter out idle task stack
ae0abc
ae0abc
When we use crash to troubleshoot softlockup and other problems,
ae0abc
we often use the 'bt -a' command to print the stacks of running
ae0abc
processes on all CPUs. But now some servers have hundreds of CPUs
ae0abc
(such as AMD machines), which causes the 'bt -a' command to output
ae0abc
a lot of process stacks. And many of these stacks are the stacks
ae0abc
of the idle process, which are not needed by us.
ae0abc
ae0abc
Therefore, in order to reduce this part of the interference information,
ae0abc
this patch adds the -n option to the bt command. When we specify
ae0abc
'-n idle' (meaning no idle), the stack of the idle process will be
ae0abc
filtered out, thus speeding up our troubleshooting.
ae0abc
ae0abc
And the option works only for crash dumps captured by kdump.
ae0abc
ae0abc
The command output is as follows:
ae0abc
crash> bt -a -n idle
ae0abc
[...]
ae0abc
PID: 0      TASK: ffff889ff8c34380  CPU: 8   COMMAND: "swapper/8"
ae0abc
ae0abc
PID: 0      TASK: ffff889ff8c32d00  CPU: 9   COMMAND: "swapper/9"
ae0abc
ae0abc
PID: 0      TASK: ffff889ff8c31680  CPU: 10  COMMAND: "swapper/10"
ae0abc
ae0abc
PID: 0      TASK: ffff889ff8c35a00  CPU: 11  COMMAND: "swapper/11"
ae0abc
ae0abc
PID: 0      TASK: ffff889ff8c3c380  CPU: 12  COMMAND: "swapper/12"
ae0abc
ae0abc
PID: 150773  TASK: ffff889fe85a1680  CPU: 13  COMMAND: "bash"
ae0abc
 #0 [ffffc9000d35bcd0] machine_kexec at ffffffff8105a407
ae0abc
 #1 [ffffc9000d35bd28] __crash_kexec at ffffffff8113033d
ae0abc
 #2 [ffffc9000d35bdf0] panic at ffffffff81081930
ae0abc
 #3 [ffffc9000d35be70] sysrq_handle_crash at ffffffff814e38d1
ae0abc
 #4 [ffffc9000d35be78] __handle_sysrq.cold.12 at ffffffff814e4175
ae0abc
 #5 [ffffc9000d35bea8] write_sysrq_trigger at ffffffff814e404b
ae0abc
 #6 [ffffc9000d35beb8] proc_reg_write at ffffffff81330d86
ae0abc
 #7 [ffffc9000d35bed0] vfs_write at ffffffff812a72d5
ae0abc
 #8 [ffffc9000d35bf00] ksys_write at ffffffff812a7579
ae0abc
 #9 [ffffc9000d35bf38] do_syscall_64 at ffffffff81004259
ae0abc
    RIP: 00007fa7abcdc274  RSP: 00007fffa731f678  RFLAGS: 00000246
ae0abc
    RAX: ffffffffffffffda  RBX: 0000000000000002  RCX: 00007fa7abcdc274
ae0abc
    RDX: 0000000000000002  RSI: 0000563ca51ee6d0  RDI: 0000000000000001
ae0abc
    RBP: 0000563ca51ee6d0   R8: 000000000000000a   R9: 00007fa7abd6be80
ae0abc
    R10: 000000000000000a  R11: 0000000000000246  R12: 00007fa7abdad760
ae0abc
    R13: 0000000000000002  R14: 00007fa7abda8760  R15: 0000000000000002
ae0abc
    ORIG_RAX: 0000000000000001  CS: 0033  SS: 002b
ae0abc
[...]
ae0abc
ae0abc
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
ae0abc
Acked-by: Kazuhito Hagio <k-hagio-ab@nec.com>
ae0abc
Acked-by: Lianbo Jiang <lijiang@redhat.com>
ae0abc
---
ae0abc
 defs.h   |  1 +
ae0abc
 help.c   | 33 ++++++++++++++++++++++++++++++++-
ae0abc
 kernel.c | 13 ++++++++++++-
ae0abc
 x86_64.c |  8 ++++++++
ae0abc
 4 files changed, 53 insertions(+), 2 deletions(-)
ae0abc
ae0abc
diff --git a/defs.h b/defs.h
ae0abc
index ecbced24d2e3..c8444b4e54eb 100644
ae0abc
--- a/defs.h
ae0abc
+++ b/defs.h
ae0abc
@@ -5832,6 +5832,7 @@ ulong cpu_map_addr(const char *type);
ae0abc
 #define BT_SHOW_ALL_REGS  (0x2000000000000ULL)
ae0abc
 #define BT_REGS_NOT_FOUND (0x4000000000000ULL)
ae0abc
 #define BT_OVERFLOW_STACK (0x8000000000000ULL)
ae0abc
+#define BT_SKIP_IDLE     (0x10000000000000ULL)
ae0abc
 #define BT_SYMBOL_OFFSET   (BT_SYMBOLIC_ARGS)
ae0abc
 
ae0abc
 #define BT_REF_HEXVAL         (0x1)
ae0abc
diff --git a/help.c b/help.c
ae0abc
index 51a0fe3d687c..e1bbc5abe029 100644
ae0abc
--- a/help.c
ae0abc
+++ b/help.c
ae0abc
@@ -1909,12 +1909,14 @@ char *help_bt[] = {
ae0abc
 "bt",
ae0abc
 "backtrace",
ae0abc
 "[-a|-c cpu(s)|-g|-r|-t|-T|-l|-e|-E|-f|-F|-o|-O|-v|-p] [-R ref] [-s [-x|d]]"
ae0abc
-"\n     [-I ip] [-S sp] [pid | task]",
ae0abc
+"\n     [-I ip] [-S sp] [-n idle] [pid | task]",
ae0abc
 "  Display a kernel stack backtrace.  If no arguments are given, the stack",
ae0abc
 "  trace of the current context will be displayed.\n",
ae0abc
 "       -a  displays the stack traces of the active task on each CPU.",
ae0abc
 "           (only applicable to crash dumps)",
ae0abc
 "       -A  same as -a, but also displays vector registers (S390X only).",
ae0abc
+"  -n idle  filter the stack of idle tasks (x86_64).",
ae0abc
+"           (only applicable to crash dumps)",
ae0abc
 "       -p  display the stack trace of the panic task only.",
ae0abc
 "           (only applicable to crash dumps)",
ae0abc
 "   -c cpu  display the stack trace of the active task on one or more CPUs,",
ae0abc
@@ -2004,6 +2006,35 @@ char *help_bt[] = {
ae0abc
 "       DS:  002b      ESI: bfffc8a0  ES:  002b      EDI: 00000000 ",
ae0abc
 "       SS:  002b      ESP: bfffc82c  EBP: bfffd224 ",
ae0abc
 "       CS:  0023      EIP: 400d032e  ERR: 0000008e  EFLAGS: 00000246  ",
ae0abc
+" ",
ae0abc
+"  Display the stack trace of the active task(s) when the kernel panicked,",
ae0abc
+"  and filter out the stack of the idle tasks:",
ae0abc
+" ",
ae0abc
+"    %s> bt -a -n idle",
ae0abc
+"    ...",
ae0abc
+"    PID: 0      TASK: ffff889ff8c35a00  CPU: 11  COMMAND: \"swapper/11\"",
ae0abc
+" ",
ae0abc
+"    PID: 0      TASK: ffff889ff8c3c380  CPU: 12  COMMAND: \"swapper/12\"",
ae0abc
+" ",
ae0abc
+"    PID: 150773  TASK: ffff889fe85a1680  CPU: 13  COMMAND: \"bash\"",
ae0abc
+"    #0 [ffffc9000d35bcd0] machine_kexec at ffffffff8105a407",
ae0abc
+"    #1 [ffffc9000d35bd28] __crash_kexec at ffffffff8113033d",
ae0abc
+"    #2 [ffffc9000d35bdf0] panic at ffffffff81081930",
ae0abc
+"    #3 [ffffc9000d35be70] sysrq_handle_crash at ffffffff814e38d1",
ae0abc
+"    #4 [ffffc9000d35be78] __handle_sysrq.cold.12 at ffffffff814e4175",
ae0abc
+"    #5 [ffffc9000d35bea8] write_sysrq_trigger at ffffffff814e404b",
ae0abc
+"    #6 [ffffc9000d35beb8] proc_reg_write at ffffffff81330d86",
ae0abc
+"    #7 [ffffc9000d35bed0] vfs_write at ffffffff812a72d5",
ae0abc
+"    #8 [ffffc9000d35bf00] ksys_write at ffffffff812a7579",
ae0abc
+"    #9 [ffffc9000d35bf38] do_syscall_64 at ffffffff81004259",
ae0abc
+"       RIP: 00007fa7abcdc274  RSP: 00007fffa731f678  RFLAGS: 00000246",
ae0abc
+"       RAX: ffffffffffffffda  RBX: 0000000000000002  RCX: 00007fa7abcdc274",
ae0abc
+"       RDX: 0000000000000002  RSI: 0000563ca51ee6d0  RDI: 0000000000000001",
ae0abc
+"       RBP: 0000563ca51ee6d0   R8: 000000000000000a   R9: 00007fa7abd6be80",
ae0abc
+"       R10: 000000000000000a  R11: 0000000000000246  R12: 00007fa7abdad760",
ae0abc
+"       R13: 0000000000000002  R14: 00007fa7abda8760  R15: 0000000000000002",
ae0abc
+"       ORIG_RAX: 0000000000000001  CS: 0033  SS: 002b",
ae0abc
+"    ...",
ae0abc
 "\n  Display the stack trace of the active task on CPU 0 and 1:\n",
ae0abc
 "    %s> bt -c 0,1",
ae0abc
 "    PID: 0      TASK: ffffffff81a8d020  CPU: 0   COMMAND: \"swapper\"",
ae0abc
diff --git a/kernel.c b/kernel.c
ae0abc
index d0921cf567d9..411e9da1e54f 100644
ae0abc
--- a/kernel.c
ae0abc
+++ b/kernel.c
ae0abc
@@ -2503,7 +2503,7 @@ cmd_bt(void)
ae0abc
 	if (kt->flags & USE_OPT_BT)
ae0abc
 		bt->flags |= BT_OPT_BACK_TRACE;
ae0abc
 
ae0abc
-	while ((c = getopt(argcnt, args, "D:fFI:S:c:aAloreEgstTdxR:Ovp")) != EOF) {
ae0abc
+	while ((c = getopt(argcnt, args, "D:fFI:S:c:n:aAloreEgstTdxR:Ovp")) != EOF) {
ae0abc
                 switch (c)
ae0abc
 		{
ae0abc
 		case 'f':
ae0abc
@@ -2672,6 +2672,13 @@ cmd_bt(void)
ae0abc
 			active++;
ae0abc
 			break;
ae0abc
 
ae0abc
+		case 'n':
ae0abc
+			if (machine_type("X86_64") && STREQ(optarg, "idle"))
ae0abc
+				bt->flags |= BT_SKIP_IDLE;
ae0abc
+			else
ae0abc
+				option_not_supported(c);
ae0abc
+			break;
ae0abc
+
ae0abc
 		case 'r':
ae0abc
 			bt->flags |= BT_RAW;
ae0abc
 			break;
ae0abc
@@ -3092,6 +3099,10 @@ back_trace(struct bt_info *bt)
ae0abc
 	} else
ae0abc
                 machdep->get_stack_frame(bt, &eip, &esp;;
ae0abc
 
ae0abc
+	/* skip idle task stack */
ae0abc
+	if (bt->flags & BT_SKIP_IDLE)
ae0abc
+		return;
ae0abc
+
ae0abc
 	if (bt->flags & BT_KSTACKP) {
ae0abc
 		bt->stkptr = esp;
ae0abc
 		return;
ae0abc
diff --git a/x86_64.c b/x86_64.c
ae0abc
index ecaefd2f46a8..cfafbcc4dabe 100644
ae0abc
--- a/x86_64.c
ae0abc
+++ b/x86_64.c
ae0abc
@@ -4918,6 +4918,9 @@ x86_64_get_stack_frame(struct bt_info *bt, ulong *pcp, ulong *spp)
ae0abc
 	if (bt->flags & BT_DUMPFILE_SEARCH)
ae0abc
 		return x86_64_get_dumpfile_stack_frame(bt, pcp, spp);
ae0abc
 
ae0abc
+	if (bt->flags & BT_SKIP_IDLE)
ae0abc
+		bt->flags &= ~BT_SKIP_IDLE;
ae0abc
+
ae0abc
         if (pcp)
ae0abc
                 *pcp = x86_64_get_pc(bt);
ae0abc
         if (spp)
ae0abc
@@ -4960,6 +4963,9 @@ x86_64_get_dumpfile_stack_frame(struct bt_info *bt_in, ulong *rip, ulong *rsp)
ae0abc
 	estack = -1;
ae0abc
 	panic = FALSE;
ae0abc
 
ae0abc
+	if (bt_in->flags & BT_SKIP_IDLE)
ae0abc
+		bt_in->flags &= ~BT_SKIP_IDLE;
ae0abc
+
ae0abc
 	panic_task = tt->panic_task == bt->task ? TRUE : FALSE;
ae0abc
 
ae0abc
 	if (panic_task && bt->machdep) {
ae0abc
@@ -5098,6 +5104,8 @@ next_sysrq:
ae0abc
                 if (!panic_task && STREQ(sym, "crash_nmi_callback")) {
ae0abc
                         *rip = *up;
ae0abc
                         *rsp = bt->stackbase + ((char *)(up) - bt->stackbuf);
ae0abc
+			if ((bt->flags & BT_SKIP_IDLE) && is_idle_thread(bt->task))
ae0abc
+				bt_in->flags |= BT_SKIP_IDLE;
ae0abc
                         return;
ae0abc
                 }
ae0abc
 
ae0abc
-- 
ae0abc
2.30.2
ae0abc