All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>,
	x86@kernel.org, Andy Lutomirski <luto@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Alexander Potapenko <glider@google.com>
Subject: [RFC patch 18/41] stacktrace: Provide helpers for common stack trace operations
Date: Wed, 10 Apr 2019 12:28:12 +0200	[thread overview]
Message-ID: <20190410103645.226477945@linutronix.de> (raw)
In-Reply-To: 20190410102754.387743324@linutronix.de

All operations with stack traces are based on struct stack_trace. That's a
horrible construct as the struct is a kitchen sink for input and
output. Quite some usage sites embed it into their own data structures
which creates weird indirections.

There is absolutely no point in doing so. For all use cases a storage array
and the number of valid stack trace entries in the array is sufficient.

Provide helper functions which avoid the struct stack_trace indirection so
the usage sites can be cleaned up.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/stacktrace.h |   18 +++++
 kernel/stacktrace.c        |  137 ++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 140 insertions(+), 15 deletions(-)

--- a/include/linux/stacktrace.h
+++ b/include/linux/stacktrace.h
@@ -26,6 +26,24 @@ extern void print_stack_trace(struct sta
 extern int snprint_stack_trace(char *buf, size_t size,
 			struct stack_trace *trace, int spaces);
 
+extern void stack_trace_print(unsigned long *trace, unsigned int nr_entries,
+			      int spaces);
+extern int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
+			       unsigned int nr_entries, int spaces);
+extern unsigned int stack_trace_save(unsigned long *store, unsigned int size,
+				     unsigned int skipnr);
+extern unsigned int stack_trace_save_tsk(struct task_struct *task,
+					 unsigned long *store,
+					 unsigned int size,
+					 unsigned int skipnr);
+extern unsigned int stack_trace_save_regs(struct pt_regs *regs,
+					  unsigned long *store,
+					  unsigned int size,
+					  unsigned int skipnr);
+extern unsigned int stack_trace_save_user(unsigned long *store,
+					  unsigned int size,
+					  unsigned int skipnr);
+
 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
 extern void save_stack_trace_user(struct stack_trace *trace);
 #else
--- a/kernel/stacktrace.c
+++ b/kernel/stacktrace.c
@@ -11,35 +11,52 @@
 #include <linux/kallsyms.h>
 #include <linux/stacktrace.h>
 
-void print_stack_trace(struct stack_trace *trace, int spaces)
+/**
+ * stack_trace_print - Print the entries in the stack trace
+ * @entries:	Pointer to storage array
+ * @nr_entries:	Number of entries in the storage array
+ * @spaces:	Number of leading spaces to print
+ */
+void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
+		       int spaces)
 {
-	int i;
+	unsigned int i;
 
-	if (WARN_ON(!trace->entries))
+	if (WARN_ON(!entries))
 		return;
 
-	for (i = 0; i < trace->nr_entries; i++)
-		printk("%*c%pS\n", 1 + spaces, ' ', (void *)trace->entries[i]);
+	for (i = 0; i < nr_entries; i++)
+		printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
+}
+EXPORT_SYMBOL_GPL(stack_trace_print);
+
+void print_stack_trace(struct stack_trace *trace, int spaces)
+{
+	stack_trace_print(trace->entries, trace->nr_entries, spaces);
 }
 EXPORT_SYMBOL_GPL(print_stack_trace);
 
-int snprint_stack_trace(char *buf, size_t size,
-			struct stack_trace *trace, int spaces)
+/**
+ * stack_trace_snprint - Print the entries in the stack trace into a buffer
+ * @buf:	Pointer to the print buffer
+ * @size:	Size of the print buffer
+ * @entries:	Pointer to storage array
+ * @nr_entries:	Number of entries in the storage array
+ * @spaces:	Number of leading spaces to print
+ */
+int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
+			unsigned int nr_entries, int spaces)
 {
-	int i;
-	int generated;
-	int total = 0;
+	unsigned int generated, i, total = 0;
 
-	if (WARN_ON(!trace->entries))
+	if (WARN_ON(!entries))
 		return 0;
 
-	for (i = 0; i < trace->nr_entries; i++) {
+	for (i = 0; i < nr_entries; i++) {
 		generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
-				     (void *)trace->entries[i]);
+				     (void *)entries[i]);
 
 		total += generated;
-
-		/* Assume that generated isn't a negative number */
 		if (generated >= size) {
 			buf += size;
 			size = 0;
@@ -51,6 +68,14 @@ int snprint_stack_trace(char *buf, size_
 
 	return total;
 }
+EXPORT_SYMBOL_GPL(stack_trace_snprint);
+
+int snprint_stack_trace(char *buf, size_t size,
+			struct stack_trace *trace, int spaces)
+{
+	return stack_trace_snprint(buf, size, trace->entries,
+				   trace->nr_entries, spaces);
+}
 EXPORT_SYMBOL_GPL(snprint_stack_trace);
 
 /*
@@ -77,3 +102,85 @@ save_stack_trace_tsk_reliable(struct tas
 	WARN_ONCE(1, KERN_INFO "save_stack_tsk_reliable() not implemented yet.\n");
 	return -ENOSYS;
 }
+
+/**
+ * stack_trace_save - Save a stack trace into a storage array
+ * @store:	Pointer to storage array
+ * @size:	Size of the storage array
+ * @skipnr:	Number of entries to skip at the start of the stack trace
+ */
+unsigned int stack_trace_save(unsigned long *store, unsigned int size,
+			      unsigned int skipnr)
+{
+	struct stack_trace trace = {
+		.entries	= store,
+		.max_entries	= size,
+		.skip		= skipnr + 1,
+	};
+
+	save_stack_trace(&trace);
+	return trace.nr_entries;
+}
+EXPORT_SYMBOL_GPL(stack_trace_save);
+
+/**
+ * stack_trace_save_tsk - Save a task stack trace into a storage array
+ * @task:	The task to examine
+ * @store:	Pointer to storage array
+ * @size:	Size of the storage array
+ * @skipnr:	Number of entries to skip at the start of the stack trace
+ */
+unsigned int stack_trace_save_tsk(struct task_struct *task,
+				  unsigned long *store, unsigned int size,
+				  unsigned int skipnr)
+{
+	struct stack_trace trace = {
+		.entries	= store,
+		.max_entries	= size,
+		.skip		= skipnr + 1,
+	};
+
+	save_stack_trace_tsk(task, &trace);
+	return trace.nr_entries;
+}
+
+/**
+ * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
+ * @regs:	Pointer to pt_regs to examine
+ * @store:	Pointer to storage array
+ * @size:	Size of the storage array
+ * @skipnr:	Number of entries to skip at the start of the stack trace
+ */
+unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
+				   unsigned int size, unsigned int skipnr)
+{
+	struct stack_trace trace = {
+		.entries	= store,
+		.max_entries	= size,
+		.skip		= skipnr + 1,
+	};
+
+	save_stack_trace_regs(regs, &trace);
+	return trace.nr_entries;
+}
+
+#ifdef CONFIG_USER_STACKTRACE_SUPPORT
+/**
+ * stack_trace_save_user - Save a user space stack trace into a storage array
+ * @store:	Pointer to storage array
+ * @size:	Size of the storage array
+ * @skipnr:	Number of entries to skip at the start of the stack trace
+ */
+unsigned int stack_trace_save_user(unsigned long *store, unsigned int size,
+				   unsigned int skipnr)
+{
+	struct stack_trace trace = {
+		.entries	= store,
+		.max_entries	= size,
+		.skip		= skipnr + 1,
+	};
+
+	save_stack_trace_user(&trace);
+	return trace.nr_entries;
+}
+#endif



  parent reply	other threads:[~2019-04-10 11:08 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-10 10:27 [RFC patch 00/41] stacktrace: Avoid the pointless redirection through struct stack_trace Thomas Gleixner
2019-04-10 10:27 ` [RFC patch 01/41] um/stacktrace: Remove the pointless ULONG_MAX marker Thomas Gleixner
2019-04-10 10:27   ` Thomas Gleixner
2019-04-14 20:34   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:27 ` [RFC patch 02/41] x86/stacktrace: " Thomas Gleixner
2019-04-14 20:34   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:27 ` [RFC patch 03/41] arm/stacktrace: " Thomas Gleixner
2019-04-10 10:27   ` Thomas Gleixner
2019-04-14 20:35   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:27 ` [RFC patch 04/41] sh/stacktrace: " Thomas Gleixner
2019-04-10 10:27   ` Thomas Gleixner
2019-04-14 20:36   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:27 ` [RFC patch 05/41] unicore32/stacktrace: " Thomas Gleixner
2019-04-14 20:36   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 06/41] riscv/stacktrace: " Thomas Gleixner
2019-04-10 10:28   ` Thomas Gleixner
2019-04-14 20:37   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 07/41] arm64/stacktrace: " Thomas Gleixner
2019-04-10 10:28   ` Thomas Gleixner
2019-04-14 20:38   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 08/41] parisc/stacktrace: " Thomas Gleixner
2019-04-14 20:38   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 09/41] s390/stacktrace: " Thomas Gleixner
2019-04-14 20:39   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 10/41] lockdep: Remove the ULONG_MAX stack trace hackery Thomas Gleixner
2019-04-14 20:40   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 11/41] mm/slub: " Thomas Gleixner
2019-04-14 20:40   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 12/41] mm/page_owner: " Thomas Gleixner
2019-04-14 20:41   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 13/41] mm/kasan: " Thomas Gleixner
2019-04-10 11:31   ` Dmitry Vyukov
2019-04-10 11:31     ` Dmitry Vyukov
2019-04-14 20:42   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 14/41] latency_top: " Thomas Gleixner
2019-04-14 20:42   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 15/41] drm: " Thomas Gleixner
2019-04-14 20:43   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 16/41] tracing: " Thomas Gleixner
2019-04-11  2:34   ` Josh Poimboeuf
2019-04-11  3:07     ` Steven Rostedt
2019-04-14 20:44   ` [tip:core/stacktrace] " tip-bot for Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 17/41] tracing: Make stack_trace_print() static and rename it Thomas Gleixner
2019-04-10 12:47   ` Steven Rostedt
2019-04-11  0:19     ` AKASHI Takahiro
2019-04-10 10:28 ` Thomas Gleixner [this message]
2019-04-10 10:28 ` [RFC patch 19/41] lib/stackdepot: Provide functions which operate on plain storage arrays Thomas Gleixner
2019-04-10 13:39   ` Alexander Potapenko
2019-04-10 10:28 ` [RFC patch 20/41] backtrace-test: Simplify stack trace handling Thomas Gleixner
2019-04-11  2:47   ` Josh Poimboeuf
2019-04-10 10:28 ` [RFC patch 21/41] proc: Simplify task stack retrieval Thomas Gleixner
2019-04-14 14:49   ` Alexey Dobriyan
2019-04-10 10:28 ` [RFC patch 22/41] latency_top: Simplify stack trace handling Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 23/41] mm/slub: Simplify stack trace retrieval Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 24/41] mm/kmemleak: Simplify stacktrace handling Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 25/41] mm/kasan: " Thomas Gleixner
2019-04-10 11:33   ` Dmitry Vyukov
2019-04-10 11:33     ` Dmitry Vyukov
2019-04-11  2:55   ` Josh Poimboeuf
2019-04-14 16:54     ` Thomas Gleixner
2019-04-14 16:54       ` Thomas Gleixner
2019-04-14 17:00       ` Thomas Gleixner
2019-04-14 17:00         ` Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 26/41] mm/page_owner: Simplify stack trace handling Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 27/41] fault-inject: Simplify stacktrace retrieval Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 28/41] dma/debug: Simplify stracktrace retrieval Thomas Gleixner
2019-04-10 10:28   ` Thomas Gleixner
2019-04-10 11:08   ` Christoph Hellwig
2019-04-10 11:08     ` Christoph Hellwig
2019-04-10 12:08     ` Thomas Gleixner
2019-04-10 12:08       ` Thomas Gleixner
2019-04-10 12:25       ` Steven Rostedt
2019-04-10 12:25         ` Steven Rostedt
2019-04-11 17:21       ` Christoph Hellwig
2019-04-11 17:21         ` Christoph Hellwig
2019-04-11 17:36         ` Steven Rostedt
2019-04-11 17:36           ` Steven Rostedt
2019-04-11 17:44           ` Christoph Hellwig
2019-04-11 17:44             ` Christoph Hellwig
2019-04-11  3:02   ` Josh Poimboeuf
2019-04-11  3:02     ` Josh Poimboeuf
2019-04-11  3:09     ` Steven Rostedt
2019-04-11  3:09       ` Steven Rostedt
2019-04-10 10:28 ` [RFC patch 29/41] btrfs: ref-verify: Simplify stack trace retrieval Thomas Gleixner
2019-04-10 11:31   ` Johannes Thumshirn
2019-04-10 12:05     ` Thomas Gleixner
2019-04-10 12:38       ` Johannes Thumshirn
2019-04-10 12:50   ` David Sterba
2019-04-10 13:47   ` Alexander Potapenko
2019-04-10 10:28 ` [RFC patch 30/41] dm bufio: " Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 31/41] dm persistent data: Simplify stack trace handling Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 32/41] drm: Simplify stacktrace handling Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 33/41] lockdep: Remove unused trace argument from print_circular_bug() Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 34/41] lockdep: Move stack trace logic into check_prev_add() Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 35/41] lockdep: Simplify stack trace handling Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 36/41] tracing: Simplify stacktrace retrieval in histograms Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 37/41] tracing: Use percpu stack trace buffer more intelligently Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 38/41] tracing: Make ftrace_trace_userstack() static and conditional Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 39/41] tracing: Simplify stack trace retrieval Thomas Gleixner
2019-04-10 10:28 ` [RFC patch 40/41] stacktrace: Remove obsolete functions Thomas Gleixner
2019-04-11  3:33   ` Josh Poimboeuf
2019-04-11  9:13     ` Peter Zijlstra
2019-04-11 13:00     ` Josh Poimboeuf
2019-04-10 10:28 ` [RFC patch 41/41] lib/stackdepot: " Thomas Gleixner
2019-04-10 13:49   ` Alexander Potapenko
2019-04-10 11:49 ` [RFC patch 00/41] stacktrace: Avoid the pointless redirection through struct stack_trace Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190410103645.226477945@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=glider@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.