1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic stack depot for storing stack traces.
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
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 contiguous memory allocation.
16 * Author: Alexander Potapenko <glider@google.com>
17 * Copyright (C) 2016 Google, Inc.
19 * Based on code by Dmitry Chernenkov.
22 #include <linux/gfp.h>
23 #include <linux/jhash.h>
24 #include <linux/kernel.h>
26 #include <linux/mutex.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>
34 #include <linux/memblock.h>
36 #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
38 #define STACK_ALLOC_NULL_PROTECTION_BITS 1
39 #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 - \
44 #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
45 STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
46 #define STACK_ALLOC_SLABS_CAP 8192
47 #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)
51 /* The compact structure to store the reference to stacks. */
53 depot_stack_handle_t handle;
55 u32 slabindex : STACK_ALLOC_INDEX_BITS;
56 u32 offset : STACK_ALLOC_OFFSET_BITS;
57 u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
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;
66 unsigned long entries[]; /* Variable-sized array of entries. */
69 static bool __stack_depot_want_early_init __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT);
70 static bool __stack_depot_early_init_passed __initdata;
72 static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
74 static int depot_index;
75 static int next_slab_inited;
76 static size_t depot_offset;
77 static DEFINE_RAW_SPINLOCK(depot_lock);
79 static bool init_stack_slab(void **prealloc)
84 * This smp_load_acquire() pairs with smp_store_release() to
85 * |next_slab_inited| below and in depot_alloc_stack().
87 if (smp_load_acquire(&next_slab_inited))
89 if (stack_slabs[depot_index] == NULL) {
90 stack_slabs[depot_index] = *prealloc;
93 /* If this is the last depot slab, do not touch the next one. */
94 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
95 stack_slabs[depot_index + 1] = *prealloc;
99 * This smp_store_release pairs with smp_load_acquire() from
100 * |next_slab_inited| above and in stack_depot_save().
102 smp_store_release(&next_slab_inited, 1);
107 /* Allocation of a new stack in raw storage */
108 static struct stack_record *
109 depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
111 struct stack_record *stack;
112 size_t required_size = struct_size(stack, entries, size);
114 required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
116 if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
117 if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
118 WARN_ONCE(1, "Stack depot reached limit capacity");
124 * smp_store_release() here pairs with smp_load_acquire() from
125 * |next_slab_inited| in stack_depot_save() and
128 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
129 smp_store_release(&next_slab_inited, 0);
131 init_stack_slab(prealloc);
132 if (stack_slabs[depot_index] == NULL)
135 stack = stack_slabs[depot_index] + depot_offset;
139 stack->handle.slabindex = depot_index;
140 stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
141 stack->handle.valid = 1;
142 memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
143 depot_offset += required_size;
148 #define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER)
149 #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
150 #define STACK_HASH_SEED 0x9747b28c
152 static bool stack_depot_disable;
153 static struct stack_record **stack_table;
155 static int __init is_stack_depot_disabled(char *str)
159 ret = kstrtobool(str, &stack_depot_disable);
160 if (!ret && stack_depot_disable) {
161 pr_info("Stack Depot is disabled\n");
166 early_param("stack_depot_disable", is_stack_depot_disabled);
168 void __init stack_depot_want_early_init(void)
170 /* Too late to request early init now */
171 WARN_ON(__stack_depot_early_init_passed);
173 __stack_depot_want_early_init = true;
176 int __init stack_depot_early_init(void)
180 /* This is supposed to be called only once, from mm_init() */
181 if (WARN_ON(__stack_depot_early_init_passed))
184 __stack_depot_early_init_passed = true;
186 if (!__stack_depot_want_early_init || stack_depot_disable)
189 size = (STACK_HASH_SIZE * sizeof(struct stack_record *));
190 pr_info("Stack Depot early init allocating hash table with memblock_alloc, %zu bytes\n",
192 stack_table = memblock_alloc(size, SMP_CACHE_BYTES);
195 pr_err("Stack Depot hash table allocation failed, disabling\n");
196 stack_depot_disable = true;
203 int stack_depot_init(void)
205 static DEFINE_MUTEX(stack_depot_init_mutex);
208 mutex_lock(&stack_depot_init_mutex);
209 if (!stack_depot_disable && !stack_table) {
210 pr_info("Stack Depot allocating hash table with kvcalloc\n");
211 stack_table = kvcalloc(STACK_HASH_SIZE, sizeof(struct stack_record *), GFP_KERNEL);
213 pr_err("Stack Depot hash table allocation failed, disabling\n");
214 stack_depot_disable = true;
218 mutex_unlock(&stack_depot_init_mutex);
221 EXPORT_SYMBOL_GPL(stack_depot_init);
223 /* Calculate hash for a stack */
224 static inline u32 hash_stack(unsigned long *entries, unsigned int size)
226 return jhash2((u32 *)entries,
227 array_size(size, sizeof(*entries)) / sizeof(u32),
231 /* Use our own, non-instrumented version of memcmp().
233 * We actually don't care about the order, just the equality.
236 int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
239 for ( ; n-- ; u1++, u2++) {
246 /* Find a stack that is equal to the one stored in entries in the hash */
247 static inline struct stack_record *find_stack(struct stack_record *bucket,
248 unsigned long *entries, int size,
251 struct stack_record *found;
253 for (found = bucket; found; found = found->next) {
254 if (found->hash == hash &&
255 found->size == size &&
256 !stackdepot_memcmp(entries, found->entries, size))
263 * stack_depot_snprint - print stack entries from a depot into a buffer
265 * @handle: Stack depot handle which was returned from
266 * stack_depot_save().
267 * @buf: Pointer to the print buffer
269 * @size: Size of the print buffer
271 * @spaces: Number of leading spaces to print
273 * Return: Number of bytes printed.
275 int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
278 unsigned long *entries;
279 unsigned int nr_entries;
281 nr_entries = stack_depot_fetch(handle, &entries);
282 return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries,
285 EXPORT_SYMBOL_GPL(stack_depot_snprint);
288 * stack_depot_print - print stack entries from a depot
290 * @stack: Stack depot handle which was returned from
291 * stack_depot_save().
294 void stack_depot_print(depot_stack_handle_t stack)
296 unsigned long *entries;
297 unsigned int nr_entries;
299 nr_entries = stack_depot_fetch(stack, &entries);
301 stack_trace_print(entries, nr_entries, 0);
303 EXPORT_SYMBOL_GPL(stack_depot_print);
306 * stack_depot_fetch - Fetch stack entries from a depot
308 * @handle: Stack depot handle which was returned from
309 * stack_depot_save().
310 * @entries: Pointer to store the entries address
312 * Return: The number of trace entries for this depot.
314 unsigned int stack_depot_fetch(depot_stack_handle_t handle,
315 unsigned long **entries)
317 union handle_parts parts = { .handle = handle };
319 size_t offset = parts.offset << STACK_ALLOC_ALIGN;
320 struct stack_record *stack;
326 if (parts.slabindex > depot_index) {
327 WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
328 parts.slabindex, depot_index, handle);
331 slab = stack_slabs[parts.slabindex];
334 stack = slab + offset;
336 *entries = stack->entries;
339 EXPORT_SYMBOL_GPL(stack_depot_fetch);
342 * __stack_depot_save - Save a stack trace from an array
344 * @entries: Pointer to storage array
345 * @nr_entries: Size of the storage array
346 * @alloc_flags: Allocation gfp flags
347 * @can_alloc: Allocate stack slabs (increased chance of failure if false)
349 * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is
350 * %true, is allowed to replenish the stack slab pool in case no space is left
351 * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids
352 * any allocations and will fail if no space is left to store the stack trace.
354 * If the stack trace in @entries is from an interrupt, only the portion up to
355 * interrupt entry is saved.
357 * Context: Any context, but setting @can_alloc to %false is required if
358 * alloc_pages() cannot be used from the current context. Currently
359 * this is the case from contexts where neither %GFP_ATOMIC nor
360 * %GFP_NOWAIT can be used (NMI, raw_spin_lock).
362 * Return: The handle of the stack struct stored in depot, 0 on failure.
364 depot_stack_handle_t __stack_depot_save(unsigned long *entries,
365 unsigned int nr_entries,
366 gfp_t alloc_flags, bool can_alloc)
368 struct stack_record *found = NULL, **bucket;
369 depot_stack_handle_t retval = 0;
370 struct page *page = NULL;
371 void *prealloc = NULL;
376 * If this stack trace is from an interrupt, including anything before
377 * interrupt entry usually leads to unbounded stackdepot growth.
379 * Because use of filter_irq_stacks() is a requirement to ensure
380 * stackdepot can efficiently deduplicate interrupt stacks, always
381 * filter_irq_stacks() to simplify all callers' use of stackdepot.
383 nr_entries = filter_irq_stacks(entries, nr_entries);
385 if (unlikely(nr_entries == 0) || stack_depot_disable)
388 hash = hash_stack(entries, nr_entries);
389 bucket = &stack_table[hash & STACK_HASH_MASK];
392 * Fast path: look the stack trace up without locking.
393 * The smp_load_acquire() here pairs with smp_store_release() to
396 found = find_stack(smp_load_acquire(bucket), entries,
402 * Check if the current or the next stack slab need to be initialized.
403 * If so, allocate the memory - we won't be able to do that under the
406 * The smp_load_acquire() here pairs with smp_store_release() to
407 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
409 if (unlikely(can_alloc && !smp_load_acquire(&next_slab_inited))) {
411 * Zero out zone modifiers, as we don't have specific zone
412 * requirements. Keep the flags related to allocation in atomic
415 alloc_flags &= ~GFP_ZONEMASK;
416 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
417 alloc_flags |= __GFP_NOWARN;
418 page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
420 prealloc = page_address(page);
423 raw_spin_lock_irqsave(&depot_lock, flags);
425 found = find_stack(*bucket, entries, nr_entries, hash);
427 struct stack_record *new = depot_alloc_stack(entries, nr_entries, hash, &prealloc);
432 * This smp_store_release() pairs with
433 * smp_load_acquire() from |bucket| above.
435 smp_store_release(bucket, new);
438 } else if (prealloc) {
440 * We didn't need to store this stack trace, but let's keep
441 * the preallocated memory for the future.
443 WARN_ON(!init_stack_slab(&prealloc));
446 raw_spin_unlock_irqrestore(&depot_lock, flags);
449 /* Nobody used this memory, ok to free it. */
450 free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
453 retval = found->handle.handle;
457 EXPORT_SYMBOL_GPL(__stack_depot_save);
460 * stack_depot_save - Save a stack trace from an array
462 * @entries: Pointer to storage array
463 * @nr_entries: Size of the storage array
464 * @alloc_flags: Allocation gfp flags
466 * Context: Contexts where allocations via alloc_pages() are allowed.
467 * See __stack_depot_save() for more details.
469 * Return: The handle of the stack struct stored in depot, 0 on failure.
471 depot_stack_handle_t stack_depot_save(unsigned long *entries,
472 unsigned int nr_entries,
475 return __stack_depot_save(entries, nr_entries, alloc_flags, true);
477 EXPORT_SYMBOL_GPL(stack_depot_save);