Skip to contentSkip to conversation

lib: stacklog_debug: Introduce helper tool for collecting and displaying stacktraces

PATCH Superseded John Stultz 2 participants 3 messages Nov 16, 2024 – Nov 17, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 lib: stacklog_debug: Introduce helper tool for collecting and displaying stacktraces

When debugging, its often useful to understand how a function is called and the different paths taken to get there. Usually dump_stack() can be used for this purpose. However there are a number of cases where a function is called very frequently, making dump_stack far too noisy to be useful.

This is a little debug tool that utilizes stackdepot to capture unique stack traces and store them in a circular buffer.

In the code, the developer adds: stacklog_debug_save() calls at points of interest (as they might with stack_dump()).

Then after running the kernel, the developer can dump the unique stack traces from the buffer via:
cat /sys/kernel/debug/stacklog_debug

This is pretty trivial, but I've had this hanging around for awhile and recently hit another case where it was helpful, so I figured it would be worth sending it out for feedback as to if others thought it would be useful enough to merge upstream or to possibly rework into stackdepot itself?

Cc Steven Rostedt <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andrew Morton <[email protected]>

---
v2:
* Add dependency on STACKTRACE_SUPPORT as reported by kernel test robot <[email protected]>
* Expand the Kconfig help to include basic usage

---
 include/linux/stacklog_debug.h |  13 ++++
 lib/Kconfig                    |  16 +++++
 lib/Makefile                   |   1 +
 lib/stacklog_debug.c           | 110 +++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+)
 create mode 100644 include/linux/stacklog_debug.h
 create mode 100644 lib/stacklog_debug.c
include/linux/stacklog_debug.h new file +13 -0
new file mode 100644
index 000000000000..d88f05d7000a
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _LINUX_STACKLOG_DEBUG_H
+#define _LINUX_STACKLOG_DEBUG_H
+
+#ifdef CONFIG_STACKLOG_DEBUG
+void stacklog_debug_save(void);
+#else
+static inline void stacklog_debug_save(void)
+{
+}
+#endif
+#endif
lib/Kconfig +16 -0
index b38849af6f13..17eb0f8d30db 100644
@@ -725,6 +725,22 @@ config REF_TRACKER
 	depends on STACKTRACE_SUPPORT
 	select STACKDEPOT
 
+config STACKLOG_DEBUG
+	bool "Debug tool for logging and later displaying stacktraces"
+	depends on STACKTRACE_SUPPORT
+	select STACKDEPOT
+	select STACKDEPOT_ALWAYS_INIT
+	help
+	  Enables debug infrastructure for logging unique stack traces at
+	  a specific point, which can be later displayed from userland.
+
+	  The developer adds: stacklog_debug_save() calls at points of
+	  interest (as they might with stack_dump()).
+
+	  Then after running the kernel, the developer can dump the unique
+	  stack traces from the buffer via:
+	    cat /sys/kernel/debug/stacklog_debug
+
 config SBITMAP
 	bool
 
lib/Makefile +1 -0
index 773adf88af41..5a07573be73c 100644
@@ -264,6 +264,7 @@ obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
 obj-$(CONFIG_POLYNOMIAL) += polynomial.o
 
+obj-$(CONFIG_STACKLOG_DEBUG) += stacklog_debug.o
 # stackdepot.c should not be instrumented or call instrumented functions.
 # Prevent the compiler from calling builtins like memcmp() or bcmp() from this
 # file.
lib/stacklog_debug.c new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.47.0.338.g60cca15819-goog
#1 lore raw

PATCH v2 2/2 lib: stackdepot: Avoid null pointer if stack_depot_save is called too early

If stack_depot_save is called before stackdepot has initialized a null reference to the stack_table might be used, crashing the system.

Instead, check stack_table has been initialized before going further.

Cc Steven Rostedt <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 lib/stackdepot.c | 3 +++
 1 file changed, 3 insertions(+)
lib/stackdepot.c +3 -0
index 5ed34cc963fc..09780c835362 100644
@@ -611,6 +611,9 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries,
 	if (unlikely(nr_entries == 0) || stack_depot_disabled)
 		return 0;
 
+	if (!stack_table)
+		return 0;
+
 	hash = hash_stack(entries, nr_entries);
 	bucket = &stack_table[hash & stack_hash_mask];
 
Signature · 1 line
2.47.0.338.g60cca15819-goog
#2 lore raw
In reply to John Stultz
kernel test robot noticed the following build warnings:

Hi John,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-nonmm-unstable]
[also build test WARNING on linus/master v6.12-rc7 next-20241115] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/John-Stultz/lib-stackdepot-Avoid-null-pointer-if-stack_depot_save-is-called-too-early/20241116-230825
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable
patch link:    https://lore.kernel.org/r/20241116002322.1035417-1-jstultz%40google.com
patch subject: [PATCH v2 1/2] lib: stacklog_debug: Introduce helper tool for collecting and displaying stacktraces
config: arm-randconfig-r132-20241117 (https://download.01.org/0day-ci/archive/20241117/[email protected]/config)
compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
reproduce: (https://download.01.org/0day-ci/archive/20241117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> lib/stacklog_debug.c:19:1: sparse: sparse: symbol 'stack_lock' was not declared. Should it be static?

lib/stacklog_debug.c:21:5: sparse: sparse: symbol 'head' was not declared. Should it be static?

vim +/stack_lock +19 lib/stacklog_debug.c

    17	
    18	#define LIST_ENTRIES 512
  > 19	DEFINE_SPINLOCK(stack_lock);
    20	depot_stack_handle_t stack_list[LIST_ENTRIES];
    21	int head, tail;
    22	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki