1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stack depot - a stack trace storage that avoids duplication.
5 * Internally, stack depot maintains a hash table of unique stacktraces. The
6 * stack traces themselves are stored contiguously one after another in a set
7 * of separate page allocations.
9 * Author: Alexander Potapenko <glider@google.com>
10 * Copyright (C) 2016 Google, Inc.
12 * Based on the code by Dmitry Chernenkov.
15 #define pr_fmt(fmt) "stackdepot: " fmt
17 #include <linux/gfp.h>
18 #include <linux/jhash.h>
19 #include <linux/kernel.h>
21 #include <linux/mutex.h>
22 #include <linux/percpu.h>
23 #include <linux/printk.h>
24 #include <linux/slab.h>
25 #include <linux/stacktrace.h>
26 #include <linux/stackdepot.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/memblock.h>
30 #include <linux/kasan-enabled.h>
32 #define DEPOT_HANDLE_BITS (sizeof(depot_stack_handle_t) * 8)
34 #define DEPOT_VALID_BITS 1
35 #define DEPOT_POOL_ORDER 2 /* Pool size order, 4 pages */
36 #define DEPOT_POOL_SIZE (1LL << (PAGE_SHIFT + DEPOT_POOL_ORDER))
37 #define DEPOT_STACK_ALIGN 4
38 #define DEPOT_OFFSET_BITS (DEPOT_POOL_ORDER + PAGE_SHIFT - DEPOT_STACK_ALIGN)
39 #define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_VALID_BITS - \
40 DEPOT_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS)
41 #define DEPOT_POOLS_CAP 8192
42 #define DEPOT_MAX_POOLS \
43 (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \
44 (1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP)
46 /* Compact structure that stores a reference to a stack. */
48 depot_stack_handle_t handle;
50 u32 pool_index : DEPOT_POOL_INDEX_BITS;
51 u32 offset : DEPOT_OFFSET_BITS;
52 u32 valid : DEPOT_VALID_BITS;
53 u32 extra : STACK_DEPOT_EXTRA_BITS;
58 struct stack_record *next; /* Link in the hash table */
59 u32 hash; /* Hash in the hash table */
60 u32 size; /* Number of stored frames */
61 union handle_parts handle;
62 unsigned long entries[]; /* Variable-sized array of frames */
65 static bool stack_depot_disabled;
66 static bool __stack_depot_early_init_requested __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT);
67 static bool __stack_depot_early_init_passed __initdata;
69 /* Use one hash table bucket per 16 KB of memory. */
70 #define STACK_HASH_TABLE_SCALE 14
71 /* Limit the number of buckets between 4K and 1M. */
72 #define STACK_BUCKET_NUMBER_ORDER_MIN 12
73 #define STACK_BUCKET_NUMBER_ORDER_MAX 20
74 /* Initial seed for jhash2. */
75 #define STACK_HASH_SEED 0x9747b28c
77 /* Hash table of pointers to stored stack traces. */
78 static struct stack_record **stack_table;
79 /* Fixed order of the number of table buckets. Used when KASAN is enabled. */
80 static unsigned int stack_bucket_number_order;
81 /* Hash mask for indexing the table. */
82 static unsigned int stack_hash_mask;
84 /* Array of memory regions that store stack traces. */
85 static void *stack_pools[DEPOT_MAX_POOLS];
86 /* Currently used pool in stack_pools. */
87 static int pool_index;
88 /* Offset to the unused space in the currently used pool. */
89 static size_t pool_offset;
90 /* Lock that protects the variables above. */
91 static DEFINE_RAW_SPINLOCK(pool_lock);
93 * Stack depot tries to keep an extra pool allocated even before it runs out
94 * of space in the currently used pool.
95 * This flag marks that this next extra pool needs to be allocated and
96 * initialized. It has the value 0 when either the next pool is not yet
97 * initialized or the limit on the number of pools is reached.
99 static int next_pool_required = 1;
101 static int __init disable_stack_depot(char *str)
105 ret = kstrtobool(str, &stack_depot_disabled);
106 if (!ret && stack_depot_disabled) {
107 pr_info("disabled\n");
112 early_param("stack_depot_disable", disable_stack_depot);
114 void __init stack_depot_request_early_init(void)
116 /* Too late to request early init now. */
117 WARN_ON(__stack_depot_early_init_passed);
119 __stack_depot_early_init_requested = true;
122 /* Allocates a hash table via memblock. Can only be used during early boot. */
123 int __init stack_depot_early_init(void)
125 unsigned long entries = 0;
127 /* This function must be called only once, from mm_init(). */
128 if (WARN_ON(__stack_depot_early_init_passed))
130 __stack_depot_early_init_passed = true;
133 * If KASAN is enabled, use the maximum order: KASAN is frequently used
134 * in fuzzing scenarios, which leads to a large number of different
135 * stack traces being stored in stack depot.
137 if (kasan_enabled() && !stack_bucket_number_order)
138 stack_bucket_number_order = STACK_BUCKET_NUMBER_ORDER_MAX;
140 if (!__stack_depot_early_init_requested || stack_depot_disabled)
144 * If stack_bucket_number_order is not set, leave entries as 0 to rely
145 * on the automatic calculations performed by alloc_large_system_hash.
147 if (stack_bucket_number_order)
148 entries = 1UL << stack_bucket_number_order;
149 pr_info("allocating hash table via alloc_large_system_hash\n");
150 stack_table = alloc_large_system_hash("stackdepot",
151 sizeof(struct stack_record *),
153 STACK_HASH_TABLE_SCALE,
154 HASH_EARLY | HASH_ZERO,
157 1UL << STACK_BUCKET_NUMBER_ORDER_MIN,
158 1UL << STACK_BUCKET_NUMBER_ORDER_MAX);
160 pr_err("hash table allocation failed, disabling\n");
161 stack_depot_disabled = true;
168 /* Allocates a hash table via kvcalloc. Can be used after boot. */
169 int stack_depot_init(void)
171 static DEFINE_MUTEX(stack_depot_init_mutex);
172 unsigned long entries;
175 mutex_lock(&stack_depot_init_mutex);
177 if (stack_depot_disabled || stack_table)
181 * Similarly to stack_depot_early_init, use stack_bucket_number_order
182 * if assigned, and rely on automatic scaling otherwise.
184 if (stack_bucket_number_order) {
185 entries = 1UL << stack_bucket_number_order;
187 int scale = STACK_HASH_TABLE_SCALE;
189 entries = nr_free_buffer_pages();
190 entries = roundup_pow_of_two(entries);
192 if (scale > PAGE_SHIFT)
193 entries >>= (scale - PAGE_SHIFT);
195 entries <<= (PAGE_SHIFT - scale);
198 if (entries < 1UL << STACK_BUCKET_NUMBER_ORDER_MIN)
199 entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MIN;
200 if (entries > 1UL << STACK_BUCKET_NUMBER_ORDER_MAX)
201 entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MAX;
203 pr_info("allocating hash table of %lu entries via kvcalloc\n", entries);
204 stack_table = kvcalloc(entries, sizeof(struct stack_record *), GFP_KERNEL);
206 pr_err("hash table allocation failed, disabling\n");
207 stack_depot_disabled = true;
211 stack_hash_mask = entries - 1;
214 mutex_unlock(&stack_depot_init_mutex);
218 EXPORT_SYMBOL_GPL(stack_depot_init);
220 /* Uses preallocated memory to initialize a new stack depot pool. */
221 static void depot_init_pool(void **prealloc)
224 * If the next pool is already initialized or the maximum number of
225 * pools is reached, do not use the preallocated memory.
226 * smp_load_acquire() here pairs with smp_store_release() below and
227 * in depot_alloc_stack().
229 if (!smp_load_acquire(&next_pool_required))
232 /* Check if the current pool is not yet allocated. */
233 if (stack_pools[pool_index] == NULL) {
234 /* Use the preallocated memory for the current pool. */
235 stack_pools[pool_index] = *prealloc;
239 * Otherwise, use the preallocated memory for the next pool
240 * as long as we do not exceed the maximum number of pools.
242 if (pool_index + 1 < DEPOT_MAX_POOLS) {
243 stack_pools[pool_index + 1] = *prealloc;
247 * At this point, either the next pool is initialized or the
248 * maximum number of pools is reached. In either case, take
249 * note that initializing another pool is not required.
250 * This smp_store_release pairs with smp_load_acquire() above
251 * and in stack_depot_save().
253 smp_store_release(&next_pool_required, 0);
257 /* Allocates a new stack in a stack depot pool. */
258 static struct stack_record *
259 depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
261 struct stack_record *stack;
262 size_t required_size = struct_size(stack, entries, size);
264 required_size = ALIGN(required_size, 1 << DEPOT_STACK_ALIGN);
266 /* Check if there is not enough space in the current pool. */
267 if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) {
268 /* Bail out if we reached the pool limit. */
269 if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) {
270 WARN_ONCE(1, "Stack depot reached limit capacity");
275 * Move on to the next pool.
276 * WRITE_ONCE pairs with potential concurrent read in
277 * stack_depot_fetch().
279 WRITE_ONCE(pool_index, pool_index + 1);
282 * If the maximum number of pools is not reached, take note
283 * that the next pool needs to initialized.
284 * smp_store_release() here pairs with smp_load_acquire() in
285 * stack_depot_save() and depot_init_pool().
287 if (pool_index + 1 < DEPOT_MAX_POOLS)
288 smp_store_release(&next_pool_required, 1);
291 /* Assign the preallocated memory to a pool if required. */
293 depot_init_pool(prealloc);
295 /* Check if we have a pool to save the stack trace. */
296 if (stack_pools[pool_index] == NULL)
299 /* Save the stack trace. */
300 stack = stack_pools[pool_index] + pool_offset;
303 stack->handle.pool_index = pool_index;
304 stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN;
305 stack->handle.valid = 1;
306 stack->handle.extra = 0;
307 memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
308 pool_offset += required_size;
313 /* Calculates the hash for a stack. */
314 static inline u32 hash_stack(unsigned long *entries, unsigned int size)
316 return jhash2((u32 *)entries,
317 array_size(size, sizeof(*entries)) / sizeof(u32),
322 * Non-instrumented version of memcmp().
323 * Does not check the lexicographical order, only the equality.
326 int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
329 for ( ; n-- ; u1++, u2++) {
336 /* Finds a stack in a bucket of the hash table. */
337 static inline struct stack_record *find_stack(struct stack_record *bucket,
338 unsigned long *entries, int size,
341 struct stack_record *found;
343 for (found = bucket; found; found = found->next) {
344 if (found->hash == hash &&
345 found->size == size &&
346 !stackdepot_memcmp(entries, found->entries, size))
352 depot_stack_handle_t __stack_depot_save(unsigned long *entries,
353 unsigned int nr_entries,
354 gfp_t alloc_flags, bool can_alloc)
356 struct stack_record *found = NULL, **bucket;
357 union handle_parts retval = { .handle = 0 };
358 struct page *page = NULL;
359 void *prealloc = NULL;
364 * If this stack trace is from an interrupt, including anything before
365 * interrupt entry usually leads to unbounded stack depot growth.
367 * Since use of filter_irq_stacks() is a requirement to ensure stack
368 * depot can efficiently deduplicate interrupt stacks, always
369 * filter_irq_stacks() to simplify all callers' use of stack depot.
371 nr_entries = filter_irq_stacks(entries, nr_entries);
373 if (unlikely(nr_entries == 0) || stack_depot_disabled)
376 hash = hash_stack(entries, nr_entries);
377 bucket = &stack_table[hash & stack_hash_mask];
380 * Fast path: look the stack trace up without locking.
381 * The smp_load_acquire() here pairs with smp_store_release() to
384 found = find_stack(smp_load_acquire(bucket), entries, nr_entries, hash);
389 * Check if another stack pool needs to be initialized. If so, allocate
390 * the memory now - we won't be able to do that under the lock.
392 * The smp_load_acquire() here pairs with smp_store_release() to
393 * |next_pool_inited| in depot_alloc_stack() and depot_init_pool().
395 if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) {
397 * Zero out zone modifiers, as we don't have specific zone
398 * requirements. Keep the flags related to allocation in atomic
401 alloc_flags &= ~GFP_ZONEMASK;
402 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
403 alloc_flags |= __GFP_NOWARN;
404 page = alloc_pages(alloc_flags, DEPOT_POOL_ORDER);
406 prealloc = page_address(page);
409 raw_spin_lock_irqsave(&pool_lock, flags);
411 found = find_stack(*bucket, entries, nr_entries, hash);
413 struct stack_record *new =
414 depot_alloc_stack(entries, nr_entries, hash, &prealloc);
419 * This smp_store_release() pairs with
420 * smp_load_acquire() from |bucket| above.
422 smp_store_release(bucket, new);
425 } else if (prealloc) {
427 * Stack depot already contains this stack trace, but let's
428 * keep the preallocated memory for the future.
430 depot_init_pool(&prealloc);
433 raw_spin_unlock_irqrestore(&pool_lock, flags);
436 /* Stack depot didn't use this memory, free it. */
437 free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER);
440 retval.handle = found->handle.handle;
442 return retval.handle;
444 EXPORT_SYMBOL_GPL(__stack_depot_save);
446 depot_stack_handle_t stack_depot_save(unsigned long *entries,
447 unsigned int nr_entries,
450 return __stack_depot_save(entries, nr_entries, alloc_flags, true);
452 EXPORT_SYMBOL_GPL(stack_depot_save);
454 unsigned int stack_depot_fetch(depot_stack_handle_t handle,
455 unsigned long **entries)
457 union handle_parts parts = { .handle = handle };
459 * READ_ONCE pairs with potential concurrent write in
462 int pool_index_cached = READ_ONCE(pool_index);
464 size_t offset = parts.offset << DEPOT_STACK_ALIGN;
465 struct stack_record *stack;
471 if (parts.pool_index > pool_index_cached) {
472 WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
473 parts.pool_index, pool_index_cached, handle);
476 pool = stack_pools[parts.pool_index];
479 stack = pool + offset;
481 *entries = stack->entries;
484 EXPORT_SYMBOL_GPL(stack_depot_fetch);
486 void stack_depot_print(depot_stack_handle_t stack)
488 unsigned long *entries;
489 unsigned int nr_entries;
491 nr_entries = stack_depot_fetch(stack, &entries);
493 stack_trace_print(entries, nr_entries, 0);
495 EXPORT_SYMBOL_GPL(stack_depot_print);
497 int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
500 unsigned long *entries;
501 unsigned int nr_entries;
503 nr_entries = stack_depot_fetch(handle, &entries);
504 return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries,
507 EXPORT_SYMBOL_GPL(stack_depot_snprint);
509 depot_stack_handle_t __must_check stack_depot_set_extra_bits(
510 depot_stack_handle_t handle, unsigned int extra_bits)
512 union handle_parts parts = { .handle = handle };
514 /* Don't set extra bits on empty handles. */
518 parts.extra = extra_bits;
521 EXPORT_SYMBOL(stack_depot_set_extra_bits);
523 unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle)
525 union handle_parts parts = { .handle = handle };
529 EXPORT_SYMBOL(stack_depot_get_extra_bits);