blob: df9179f4f441487332eb1fc090771d79044219a7 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Alexander Potapenkocd110162016-03-25 14:22:08 -07002/*
3 * Generic stack depot for storing stack traces.
4 *
5 * Some debugging tools need to save stack traces of certain events which can
6 * be later presented to the user. For example, KASAN needs to safe alloc and
7 * free stacks for each object, but storing two stack traces per object
8 * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
9 * that).
10 *
11 * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
12 * and free stacks repeat a lot, we save about 100x space.
13 * Stacks are never removed from depot, so we store them contiguously one after
14 * another in a contiguos memory allocation.
15 *
16 * Author: Alexander Potapenko <glider@google.com>
17 * Copyright (C) 2016 Google, Inc.
18 *
19 * Based on code by Dmitry Chernenkov.
Alexander Potapenkocd110162016-03-25 14:22:08 -070020 */
21
22#include <linux/gfp.h>
Alexander Potapenko505a0ef2020-04-06 20:10:22 -070023#include <linux/interrupt.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070024#include <linux/jhash.h>
25#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/percpu.h>
28#include <linux/printk.h>
29#include <linux/slab.h>
30#include <linux/stacktrace.h>
31#include <linux/stackdepot.h>
32#include <linux/string.h>
33#include <linux/types.h>
Vijayanand Jittae1fdc402021-02-25 17:21:27 -080034#include <linux/memblock.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070035
36#define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
37
Joonsoo Kim7c311902016-05-05 16:22:35 -070038#define STACK_ALLOC_NULL_PROTECTION_BITS 1
Alexander Potapenkocd110162016-03-25 14:22:08 -070039#define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
40#define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
41#define STACK_ALLOC_ALIGN 4
42#define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
43 STACK_ALLOC_ALIGN)
Joonsoo Kim7c311902016-05-05 16:22:35 -070044#define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
45 STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
Dmitry Vyukov02754e02016-10-27 17:46:44 -070046#define STACK_ALLOC_SLABS_CAP 8192
Alexander Potapenkocd110162016-03-25 14:22:08 -070047#define STACK_ALLOC_MAX_SLABS \
48 (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
49 (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
50
51/* The compact structure to store the reference to stacks. */
52union handle_parts {
53 depot_stack_handle_t handle;
54 struct {
55 u32 slabindex : STACK_ALLOC_INDEX_BITS;
56 u32 offset : STACK_ALLOC_OFFSET_BITS;
Joonsoo Kim7c311902016-05-05 16:22:35 -070057 u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
Alexander Potapenkocd110162016-03-25 14:22:08 -070058 };
59};
60
61struct stack_record {
62 struct stack_record *next; /* Link in the hashtable */
63 u32 hash; /* Hash in the hastable */
64 u32 size; /* Number of frames in the stack */
65 union handle_parts handle;
Gustavo A. R. Silva3a2b67e2020-12-15 20:43:04 -080066 unsigned long entries[]; /* Variable-sized array of entries. */
Alexander Potapenkocd110162016-03-25 14:22:08 -070067};
68
69static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
70
71static int depot_index;
72static int next_slab_inited;
73static size_t depot_offset;
Zqiang78564b92021-05-06 18:03:40 -070074static DEFINE_RAW_SPINLOCK(depot_lock);
Alexander Potapenkocd110162016-03-25 14:22:08 -070075
76static bool init_stack_slab(void **prealloc)
77{
78 if (!*prealloc)
79 return false;
80 /*
81 * This smp_load_acquire() pairs with smp_store_release() to
82 * |next_slab_inited| below and in depot_alloc_stack().
83 */
84 if (smp_load_acquire(&next_slab_inited))
85 return true;
86 if (stack_slabs[depot_index] == NULL) {
87 stack_slabs[depot_index] = *prealloc;
Alexander Potapenko305e5192020-02-20 20:04:30 -080088 *prealloc = NULL;
Alexander Potapenkocd110162016-03-25 14:22:08 -070089 } else {
Alexander Potapenko305e5192020-02-20 20:04:30 -080090 /* If this is the last depot slab, do not touch the next one. */
91 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
92 stack_slabs[depot_index + 1] = *prealloc;
93 *prealloc = NULL;
94 }
Alexander Potapenkocd110162016-03-25 14:22:08 -070095 /*
96 * This smp_store_release pairs with smp_load_acquire() from
Miles Chenee050dc2019-08-15 19:32:46 +080097 * |next_slab_inited| above and in stack_depot_save().
Alexander Potapenkocd110162016-03-25 14:22:08 -070098 */
99 smp_store_release(&next_slab_inited, 1);
100 }
Alexander Potapenkocd110162016-03-25 14:22:08 -0700101 return true;
102}
103
104/* Allocation of a new stack in raw storage */
105static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
106 u32 hash, void **prealloc, gfp_t alloc_flags)
107{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700108 struct stack_record *stack;
Gustavo A. R. Silva3a2b67e2020-12-15 20:43:04 -0800109 size_t required_size = struct_size(stack, entries, size);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700110
111 required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
112
113 if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
114 if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
115 WARN_ONCE(1, "Stack depot reached limit capacity");
116 return NULL;
117 }
118 depot_index++;
119 depot_offset = 0;
120 /*
121 * smp_store_release() here pairs with smp_load_acquire() from
Miles Chenee050dc2019-08-15 19:32:46 +0800122 * |next_slab_inited| in stack_depot_save() and
Alexander Potapenkocd110162016-03-25 14:22:08 -0700123 * init_stack_slab().
124 */
125 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
126 smp_store_release(&next_slab_inited, 0);
127 }
128 init_stack_slab(prealloc);
129 if (stack_slabs[depot_index] == NULL)
130 return NULL;
131
132 stack = stack_slabs[depot_index] + depot_offset;
133
134 stack->hash = hash;
135 stack->size = size;
136 stack->handle.slabindex = depot_index;
137 stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
Joonsoo Kim7c311902016-05-05 16:22:35 -0700138 stack->handle.valid = 1;
Gustavo A. R. Silva47e684a2020-12-15 20:43:07 -0800139 memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
Alexander Potapenkocd110162016-03-25 14:22:08 -0700140 depot_offset += required_size;
141
142 return stack;
143}
144
Yogesh Lald2620932021-02-25 17:21:24 -0800145#define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER)
Alexander Potapenkocd110162016-03-25 14:22:08 -0700146#define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
147#define STACK_HASH_SEED 0x9747b28c
148
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800149static bool stack_depot_disable;
150static struct stack_record **stack_table;
151
152static int __init is_stack_depot_disabled(char *str)
153{
Vijayanand Jitta64427982021-02-25 17:21:31 -0800154 int ret;
155
156 ret = kstrtobool(str, &stack_depot_disable);
157 if (!ret && stack_depot_disable) {
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800158 pr_info("Stack Depot is disabled\n");
159 stack_table = NULL;
160 }
161 return 0;
162}
163early_param("stack_depot_disable", is_stack_depot_disabled);
164
165int __init stack_depot_init(void)
166{
167 if (!stack_depot_disable) {
168 size_t size = (STACK_HASH_SIZE * sizeof(struct stack_record *));
169 int i;
170
171 stack_table = memblock_alloc(size, size);
172 for (i = 0; i < STACK_HASH_SIZE; i++)
173 stack_table[i] = NULL;
174 }
175 return 0;
176}
Alexander Potapenkocd110162016-03-25 14:22:08 -0700177
178/* Calculate hash for a stack */
179static inline u32 hash_stack(unsigned long *entries, unsigned int size)
180{
181 return jhash2((u32 *)entries,
Gustavo A. R. Silva180644f2020-12-15 20:43:10 -0800182 array_size(size, sizeof(*entries)) / sizeof(u32),
183 STACK_HASH_SEED);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700184}
185
Alexander Potapenkoa571b272018-02-06 15:38:24 -0800186/* Use our own, non-instrumented version of memcmp().
187 *
188 * We actually don't care about the order, just the equality.
189 */
190static inline
191int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
192 unsigned int n)
193{
194 for ( ; n-- ; u1++, u2++) {
195 if (*u1 != *u2)
196 return 1;
197 }
198 return 0;
199}
200
Alexander Potapenkocd110162016-03-25 14:22:08 -0700201/* Find a stack that is equal to the one stored in entries in the hash */
202static inline struct stack_record *find_stack(struct stack_record *bucket,
203 unsigned long *entries, int size,
204 u32 hash)
205{
206 struct stack_record *found;
207
208 for (found = bucket; found; found = found->next) {
209 if (found->hash == hash &&
210 found->size == size &&
Alexander Potapenkoa571b272018-02-06 15:38:24 -0800211 !stackdepot_memcmp(entries, found->entries, size))
Alexander Potapenkocd110162016-03-25 14:22:08 -0700212 return found;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700213 }
214 return NULL;
215}
216
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200217/**
218 * stack_depot_fetch - Fetch stack entries from a depot
219 *
220 * @handle: Stack depot handle which was returned from
221 * stack_depot_save().
222 * @entries: Pointer to store the entries address
223 *
224 * Return: The number of trace entries for this depot.
225 */
226unsigned int stack_depot_fetch(depot_stack_handle_t handle,
227 unsigned long **entries)
Alexander Potapenkocd110162016-03-25 14:22:08 -0700228{
229 union handle_parts parts = { .handle = handle };
Alexander Potapenko69866e12020-04-06 20:10:15 -0700230 void *slab;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700231 size_t offset = parts.offset << STACK_ALLOC_ALIGN;
Alexander Potapenko69866e12020-04-06 20:10:15 -0700232 struct stack_record *stack;
233
234 *entries = NULL;
235 if (parts.slabindex > depot_index) {
236 WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
237 parts.slabindex, depot_index, handle);
238 return 0;
239 }
240 slab = stack_slabs[parts.slabindex];
241 if (!slab)
242 return 0;
243 stack = slab + offset;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700244
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200245 *entries = stack->entries;
246 return stack->size;
247}
248EXPORT_SYMBOL_GPL(stack_depot_fetch);
249
Alexander Potapenkocd110162016-03-25 14:22:08 -0700250/**
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200251 * stack_depot_save - Save a stack trace from an array
Alexander Potapenkocd110162016-03-25 14:22:08 -0700252 *
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200253 * @entries: Pointer to storage array
254 * @nr_entries: Size of the storage array
255 * @alloc_flags: Allocation gfp flags
256 *
257 * Return: The handle of the stack struct stored in depot
Alexander Potapenkocd110162016-03-25 14:22:08 -0700258 */
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200259depot_stack_handle_t stack_depot_save(unsigned long *entries,
260 unsigned int nr_entries,
261 gfp_t alloc_flags)
Alexander Potapenkocd110162016-03-25 14:22:08 -0700262{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700263 struct stack_record *found = NULL, **bucket;
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200264 depot_stack_handle_t retval = 0;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700265 struct page *page = NULL;
266 void *prealloc = NULL;
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200267 unsigned long flags;
268 u32 hash;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700269
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800270 if (unlikely(nr_entries == 0) || stack_depot_disable)
Alexander Potapenkocd110162016-03-25 14:22:08 -0700271 goto fast_exit;
272
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200273 hash = hash_stack(entries, nr_entries);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700274 bucket = &stack_table[hash & STACK_HASH_MASK];
275
276 /*
277 * Fast path: look the stack trace up without locking.
278 * The smp_load_acquire() here pairs with smp_store_release() to
279 * |bucket| below.
280 */
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200281 found = find_stack(smp_load_acquire(bucket), entries,
282 nr_entries, hash);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700283 if (found)
284 goto exit;
285
286 /*
287 * Check if the current or the next stack slab need to be initialized.
288 * If so, allocate the memory - we won't be able to do that under the
289 * lock.
290 *
291 * The smp_load_acquire() here pairs with smp_store_release() to
292 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
293 */
294 if (unlikely(!smp_load_acquire(&next_slab_inited))) {
295 /*
296 * Zero out zone modifiers, as we don't have specific zone
297 * requirements. Keep the flags related to allocation in atomic
298 * contexts and I/O.
299 */
300 alloc_flags &= ~GFP_ZONEMASK;
301 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
Kirill A. Shutemov87cc2712016-07-28 15:49:10 -0700302 alloc_flags |= __GFP_NOWARN;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700303 page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
304 if (page)
305 prealloc = page_address(page);
306 }
307
Zqiang78564b92021-05-06 18:03:40 -0700308 raw_spin_lock_irqsave(&depot_lock, flags);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700309
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200310 found = find_stack(*bucket, entries, nr_entries, hash);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700311 if (!found) {
312 struct stack_record *new =
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200313 depot_alloc_stack(entries, nr_entries,
Alexander Potapenkocd110162016-03-25 14:22:08 -0700314 hash, &prealloc, alloc_flags);
315 if (new) {
316 new->next = *bucket;
317 /*
318 * This smp_store_release() pairs with
319 * smp_load_acquire() from |bucket| above.
320 */
321 smp_store_release(bucket, new);
322 found = new;
323 }
324 } else if (prealloc) {
325 /*
326 * We didn't need to store this stack trace, but let's keep
327 * the preallocated memory for the future.
328 */
329 WARN_ON(!init_stack_slab(&prealloc));
330 }
331
Zqiang78564b92021-05-06 18:03:40 -0700332 raw_spin_unlock_irqrestore(&depot_lock, flags);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700333exit:
334 if (prealloc) {
335 /* Nobody used this memory, ok to free it. */
336 free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
337 }
338 if (found)
339 retval = found->handle.handle;
340fast_exit:
341 return retval;
342}
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200343EXPORT_SYMBOL_GPL(stack_depot_save);
Alexander Potapenko505a0ef2020-04-06 20:10:22 -0700344
345static inline int in_irqentry_text(unsigned long ptr)
346{
347 return (ptr >= (unsigned long)&__irqentry_text_start &&
348 ptr < (unsigned long)&__irqentry_text_end) ||
349 (ptr >= (unsigned long)&__softirqentry_text_start &&
350 ptr < (unsigned long)&__softirqentry_text_end);
351}
352
353unsigned int filter_irq_stacks(unsigned long *entries,
354 unsigned int nr_entries)
355{
356 unsigned int i;
357
358 for (i = 0; i < nr_entries; i++) {
359 if (in_irqentry_text(entries[i])) {
360 /* Include the irqentry function into the stack. */
361 return i + 1;
362 }
363 }
364 return nr_entries;
365}
366EXPORT_SYMBOL_GPL(filter_irq_stacks);