Skip to content

Commit 214d8ca

Browse files
committed
stacktrace: Provide common infrastructure
All architectures which support stacktrace carry duplicated code and do the stack storage and filtering at the architecture side. Provide a consolidated interface with a callback function for consuming the stack entries provided by the architecture specific stack walker. This removes lots of duplicated code and allows to implement better filtering than 'skip number of entries' in the future without touching any architecture specific code. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: linux-arch@vger.kernel.org Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Alexander Potapenko <glider@google.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Christoph Lameter <cl@linux.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: linux-mm@kvack.org Cc: David Rientjes <rientjes@google.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: kasan-dev@googlegroups.com Cc: Mike Rapoport <rppt@linux.vnet.ibm.com> Cc: Akinobu Mita <akinobu.mita@gmail.com> Cc: Christoph Hellwig <hch@lst.de> Cc: iommu@lists.linux-foundation.org Cc: Robin Murphy <robin.murphy@arm.com> Cc: Marek Szyprowski <m.szyprowski@samsung.com> Cc: Johannes Thumshirn <jthumshirn@suse.de> Cc: David Sterba <dsterba@suse.com> Cc: Chris Mason <clm@fb.com> Cc: Josef Bacik <josef@toxicpanda.com> Cc: linux-btrfs@vger.kernel.org Cc: dm-devel@redhat.com Cc: Mike Snitzer <snitzer@redhat.com> Cc: Alasdair Kergon <agk@redhat.com> Cc: Daniel Vetter <daniel@ffwll.ch> Cc: intel-gfx@lists.freedesktop.org Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Cc: dri-devel@lists.freedesktop.org Cc: David Airlie <airlied@linux.ie> Cc: Jani Nikula <jani.nikula@linux.intel.com> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com> Cc: Tom Zanussi <tom.zanussi@linux.intel.com> Cc: Miroslav Benes <mbenes@suse.cz> Link: https://lkml.kernel.org/r/20190425094803.713568606@linutronix.de
1 parent 56d8f07 commit 214d8ca

3 files changed

Lines changed: 216 additions & 0 deletions

File tree

‎include/linux/stacktrace.h‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@ unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
2323
unsigned int stack_trace_save_user(unsigned long *store, unsigned int size);
2424

2525
/* Internal interfaces. Do not use in generic code */
26+
#ifdef CONFIG_ARCH_STACKWALK
27+
28+
/**
29+
* stack_trace_consume_fn - Callback for arch_stack_walk()
30+
* @cookie: Caller supplied pointer handed back by arch_stack_walk()
31+
* @addr: The stack entry address to consume
32+
* @reliable: True when the stack entry is reliable. Required by
33+
* some printk based consumers.
34+
*
35+
* Return: True, if the entry was consumed or skipped
36+
* False, if there is no space left to store
37+
*/
38+
typedef bool (*stack_trace_consume_fn)(void *cookie, unsigned long addr,
39+
bool reliable);
40+
/**
41+
* arch_stack_walk - Architecture specific function to walk the stack
42+
* @consume_entry: Callback which is invoked by the architecture code for
43+
* each entry.
44+
* @cookie: Caller supplied pointer which is handed back to
45+
* @consume_entry
46+
* @task: Pointer to a task struct, can be NULL
47+
* @regs: Pointer to registers, can be NULL
48+
*
49+
* ============ ======= ============================================
50+
* task regs
51+
* ============ ======= ============================================
52+
* task NULL Stack trace from task (can be current)
53+
* current regs Stack trace starting on regs->stackpointer
54+
* ============ ======= ============================================
55+
*/
56+
void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
57+
struct task_struct *task, struct pt_regs *regs);
58+
int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, void *cookie,
59+
struct task_struct *task);
60+
void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
61+
const struct pt_regs *regs);
62+
63+
#else /* CONFIG_ARCH_STACKWALK */
2664
struct stack_trace {
2765
unsigned int nr_entries, max_entries;
2866
unsigned long *entries;
@@ -37,6 +75,7 @@ extern void save_stack_trace_tsk(struct task_struct *tsk,
3775
extern int save_stack_trace_tsk_reliable(struct task_struct *tsk,
3876
struct stack_trace *trace);
3977
extern void save_stack_trace_user(struct stack_trace *trace);
78+
#endif /* !CONFIG_ARCH_STACKWALK */
4079
#endif /* CONFIG_STACKTRACE */
4180

4281
#if defined(CONFIG_STACKTRACE) && defined(CONFIG_HAVE_RELIABLE_STACKTRACE)

‎kernel/stacktrace.c‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*
66
* Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
77
*/
8+
#include <linux/sched/task_stack.h>
9+
#include <linux/sched/debug.h>
810
#include <linux/sched.h>
911
#include <linux/kernel.h>
1012
#include <linux/export.h>
@@ -66,6 +68,175 @@ int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
6668
}
6769
EXPORT_SYMBOL_GPL(stack_trace_snprint);
6870

71+
#ifdef CONFIG_ARCH_STACKWALK
72+
73+
struct stacktrace_cookie {
74+
unsigned long *store;
75+
unsigned int size;
76+
unsigned int skip;
77+
unsigned int len;
78+
};
79+
80+
static bool stack_trace_consume_entry(void *cookie, unsigned long addr,
81+
bool reliable)
82+
{
83+
struct stacktrace_cookie *c = cookie;
84+
85+
if (c->len >= c->size)
86+
return false;
87+
88+
if (c->skip > 0) {
89+
c->skip--;
90+
return true;
91+
}
92+
c->store[c->len++] = addr;
93+
return c->len < c->size;
94+
}
95+
96+
static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr,
97+
bool reliable)
98+
{
99+
if (in_sched_functions(addr))
100+
return true;
101+
return stack_trace_consume_entry(cookie, addr, reliable);
102+
}
103+
104+
/**
105+
* stack_trace_save - Save a stack trace into a storage array
106+
* @store: Pointer to storage array
107+
* @size: Size of the storage array
108+
* @skipnr: Number of entries to skip at the start of the stack trace
109+
*
110+
* Return: Number of trace entries stored.
111+
*/
112+
unsigned int stack_trace_save(unsigned long *store, unsigned int size,
113+
unsigned int skipnr)
114+
{
115+
stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
116+
struct stacktrace_cookie c = {
117+
.store = store,
118+
.size = size,
119+
.skip = skipnr + 1,
120+
};
121+
122+
arch_stack_walk(consume_entry, &c, current, NULL);
123+
return c.len;
124+
}
125+
EXPORT_SYMBOL_GPL(stack_trace_save);
126+
127+
/**
128+
* stack_trace_save_tsk - Save a task stack trace into a storage array
129+
* @task: The task to examine
130+
* @store: Pointer to storage array
131+
* @size: Size of the storage array
132+
* @skipnr: Number of entries to skip at the start of the stack trace
133+
*
134+
* Return: Number of trace entries stored.
135+
*/
136+
unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
137+
unsigned int size, unsigned int skipnr)
138+
{
139+
stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
140+
struct stacktrace_cookie c = {
141+
.store = store,
142+
.size = size,
143+
.skip = skipnr + 1,
144+
};
145+
146+
if (!try_get_task_stack(tsk))
147+
return 0;
148+
149+
arch_stack_walk(consume_entry, &c, tsk, NULL);
150+
put_task_stack(tsk);
151+
return c.len;
152+
}
153+
154+
/**
155+
* stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
156+
* @regs: Pointer to pt_regs to examine
157+
* @store: Pointer to storage array
158+
* @size: Size of the storage array
159+
* @skipnr: Number of entries to skip at the start of the stack trace
160+
*
161+
* Return: Number of trace entries stored.
162+
*/
163+
unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
164+
unsigned int size, unsigned int skipnr)
165+
{
166+
stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
167+
struct stacktrace_cookie c = {
168+
.store = store,
169+
.size = size,
170+
.skip = skipnr,
171+
};
172+
173+
arch_stack_walk(consume_entry, &c, current, regs);
174+
return c.len;
175+
}
176+
177+
#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
178+
/**
179+
* stack_trace_save_tsk_reliable - Save task stack with verification
180+
* @tsk: Pointer to the task to examine
181+
* @store: Pointer to storage array
182+
* @size: Size of the storage array
183+
*
184+
* Return: An error if it detects any unreliable features of the
185+
* stack. Otherwise it guarantees that the stack trace is
186+
* reliable and returns the number of entries stored.
187+
*
188+
* If the task is not 'current', the caller *must* ensure the task is inactive.
189+
*/
190+
int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
191+
unsigned int size)
192+
{
193+
stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
194+
struct stacktrace_cookie c = {
195+
.store = store,
196+
.size = size,
197+
};
198+
int ret;
199+
200+
/*
201+
* If the task doesn't have a stack (e.g., a zombie), the stack is
202+
* "reliably" empty.
203+
*/
204+
if (!try_get_task_stack(tsk))
205+
return 0;
206+
207+
ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
208+
put_task_stack(tsk);
209+
return ret;
210+
}
211+
#endif
212+
213+
#ifdef CONFIG_USER_STACKTRACE_SUPPORT
214+
/**
215+
* stack_trace_save_user - Save a user space stack trace into a storage array
216+
* @store: Pointer to storage array
217+
* @size: Size of the storage array
218+
*
219+
* Return: Number of trace entries stored.
220+
*/
221+
unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
222+
{
223+
stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
224+
struct stacktrace_cookie c = {
225+
.store = store,
226+
.size = size,
227+
};
228+
229+
/* Trace user stack if not a kernel thread */
230+
if (!current->mm)
231+
return 0;
232+
233+
arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
234+
return c.len;
235+
}
236+
#endif
237+
238+
#else /* CONFIG_ARCH_STACKWALK */
239+
69240
/*
70241
* Architectures that do not implement save_stack_trace_*()
71242
* get these weak aliases and once-per-bootup warnings
@@ -203,3 +374,5 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
203374
return trace.nr_entries;
204375
}
205376
#endif /* CONFIG_USER_STACKTRACE_SUPPORT */
377+
378+
#endif /* !CONFIG_ARCH_STACKWALK */

‎lib/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,10 @@ config ARCH_HAS_UACCESS_FLUSHCACHE
597597
config ARCH_HAS_UACCESS_MCSAFE
598598
bool
599599

600+
# Temporary. Goes away when all archs are cleaned up
601+
config ARCH_STACKWALK
602+
bool
603+
600604
config STACKDEPOT
601605
bool
602606
select STACKTRACE

0 commit comments

Comments
 (0)