[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241112125609.1406586-1-liuchao173@huawei.com>
Date: Tue, 12 Nov 2024 12:56:09 +0000
From: Liu Chao <liuchao173@...wei.com>
To: <linux-kernel@...r.kernel.org>
CC: <lixiaokeng@...wei.com>, <caihe@...wei.com>
Subject: [PATCH] Add interface to trigger backtrace on specified CPUs
When the number of CPUs is large, the echo l > /proc/sysrq-trigger
prints a large number of logs. Users can use this interface to
trigger backtrace on specified CPUs.
Signed-off-by: Liu Chao <liuchao173@...wei.com>
---
kernel/Makefile | 1 +
kernel/backtrace.c | 39 +++++++++++++++++++++++++++++++++++++++
lib/Kconfig.debug | 9 +++++++++
3 files changed, 49 insertions(+)
create mode 100644 kernel/backtrace.c
diff --git a/kernel/Makefile b/kernel/Makefile
index 87866b037fbe..330647518ae3 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -116,6 +116,7 @@ obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o
obj-$(CONFIG_HAVE_STATIC_CALL) += static_call.o
obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call_inline.o
obj-$(CONFIG_CFI_CLANG) += cfi.o
+obj-$(CONFIG_BACKTRACE) += backtrace.o
obj-$(CONFIG_PERF_EVENTS) += events/
diff --git a/kernel/backtrace.c b/kernel/backtrace.c
new file mode 100644
index 000000000000..767ad46d1add
--- /dev/null
+++ b/kernel/backtrace.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/debugfs.h>
+#include <linux/nmi.h>
+
+static ssize_t backtrace_write(struct file *file, const char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct cpumask mask;
+ int err;
+
+ err = cpumask_parselist_user(buf, count, &mask);
+ if (err < 0 || cpumask_last(&mask) >= nr_cpu_ids) {
+ pr_err("backtrace: incorrect CPU range.\n");
+ return -EINVAL;
+ }
+
+ if (!trigger_cpumask_backtrace(&mask)) {
+ pr_err("backtrace: backtrace printing fails.\n");
+ return -EINVAL;
+ }
+
+ return count;
+}
+
+static const struct file_operations backtrace_fops = {
+ .owner = THIS_MODULE,
+ .write = backtrace_write,
+ .llseek = no_llseek,
+};
+
+static int __init backtrace_init(void)
+{
+ debugfs_create_file("backtrace", 0200, NULL, NULL,
+ &backtrace_fops);
+
+ return 0;
+}
+device_initcall(backtrace_init);
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 7312ae7c3cc5..326ea9e6eba7 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -698,6 +698,15 @@ source "lib/Kconfig.kgdb"
source "lib/Kconfig.ubsan"
source "lib/Kconfig.kcsan"
+config BACKTRACE
+ bool "Support trigger backtrace according cpulist"
+ depends on SMP && DEBUG_FS
+ help
+ When the number of CPUs is large, the echo l > /proc/sysrq-trigger
+ prints a large number of logs. Users can echo cpulist >
+ /sys/kernel/debug/backtrace to trigger backtrace on specified
+ CPUs.
+
endmenu
menu "Networking Debugging"
--
2.33.0
Powered by blists - more mailing lists