All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Add interface to trigger backtrace on specified CPUs
@ 2024-11-12 12:56 Liu Chao
  2024-11-13 12:40 ` kernel test robot
  2024-11-13 14:03 ` kernel test robot
  0 siblings, 2 replies; 3+ messages in thread
From: Liu Chao @ 2024-11-12 12:56 UTC (permalink / raw)
  To: linux-kernel; +Cc: lixiaokeng, caihe

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@huawei.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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-11-13 14:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-12 12:56 [PATCH] Add interface to trigger backtrace on specified CPUs Liu Chao
2024-11-13 12:40 ` kernel test robot
2024-11-13 14:03 ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.