Linux 4.19.133
[linux/fpc-iii.git] / lib / stackdepot.c
blob3376a3291186497628418bf2538caf8c5c9ebbae
1 /*
2 * Generic stack depot for storing stack traces.
4 * Some debugging tools need to save stack traces of certain events which can
5 * be later presented to the user. For example, KASAN needs to safe alloc and
6 * free stacks for each object, but storing two stack traces per object
7 * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
8 * that).
10 * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
11 * and free stacks repeat a lot, we save about 100x space.
12 * Stacks are never removed from depot, so we store them contiguously one after
13 * another in a contiguos memory allocation.
15 * Author: Alexander Potapenko <glider@google.com>
16 * Copyright (C) 2016 Google, Inc.
18 * Based on code by Dmitry Chernenkov.
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * version 2 as published by the Free Software Foundation.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
31 #include <linux/gfp.h>
32 #include <linux/jhash.h>
33 #include <linux/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/percpu.h>
36 #include <linux/printk.h>
37 #include <linux/slab.h>
38 #include <linux/stacktrace.h>
39 #include <linux/stackdepot.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
43 #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
45 #define STACK_ALLOC_NULL_PROTECTION_BITS 1
46 #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
47 #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
48 #define STACK_ALLOC_ALIGN 4
49 #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
50 STACK_ALLOC_ALIGN)
51 #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
52 STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
53 #define STACK_ALLOC_SLABS_CAP 8192
54 #define STACK_ALLOC_MAX_SLABS \
55 (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
56 (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
58 /* The compact structure to store the reference to stacks. */
59 union handle_parts {
60 depot_stack_handle_t handle;
61 struct {
62 u32 slabindex : STACK_ALLOC_INDEX_BITS;
63 u32 offset : STACK_ALLOC_OFFSET_BITS;
64 u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
68 struct stack_record {
69 struct stack_record *next; /* Link in the hashtable */
70 u32 hash; /* Hash in the hastable */
71 u32 size; /* Number of frames in the stack */
72 union handle_parts handle;
73 unsigned long entries[1]; /* Variable-sized array of entries. */
76 static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
78 static int depot_index;
79 static int next_slab_inited;
80 static size_t depot_offset;
81 static DEFINE_SPINLOCK(depot_lock);
83 static bool init_stack_slab(void **prealloc)
85 if (!*prealloc)
86 return false;
88 * This smp_load_acquire() pairs with smp_store_release() to
89 * |next_slab_inited| below and in depot_alloc_stack().
91 if (smp_load_acquire(&next_slab_inited))
92 return true;
93 if (stack_slabs[depot_index] == NULL) {
94 stack_slabs[depot_index] = *prealloc;
95 *prealloc = NULL;
96 } else {
97 /* If this is the last depot slab, do not touch the next one. */
98 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
99 stack_slabs[depot_index + 1] = *prealloc;
100 *prealloc = NULL;
103 * This smp_store_release pairs with smp_load_acquire() from
104 * |next_slab_inited| above and in depot_save_stack().
106 smp_store_release(&next_slab_inited, 1);
108 return true;
111 /* Allocation of a new stack in raw storage */
112 static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
113 u32 hash, void **prealloc, gfp_t alloc_flags)
115 int required_size = offsetof(struct stack_record, entries) +
116 sizeof(unsigned long) * size;
117 struct stack_record *stack;
119 required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
121 if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
122 if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
123 WARN_ONCE(1, "Stack depot reached limit capacity");
124 return NULL;
126 depot_index++;
127 depot_offset = 0;
129 * smp_store_release() here pairs with smp_load_acquire() from
130 * |next_slab_inited| in depot_save_stack() and
131 * init_stack_slab().
133 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
134 smp_store_release(&next_slab_inited, 0);
136 init_stack_slab(prealloc);
137 if (stack_slabs[depot_index] == NULL)
138 return NULL;
140 stack = stack_slabs[depot_index] + depot_offset;
142 stack->hash = hash;
143 stack->size = size;
144 stack->handle.slabindex = depot_index;
145 stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
146 stack->handle.valid = 1;
147 memcpy(stack->entries, entries, size * sizeof(unsigned long));
148 depot_offset += required_size;
150 return stack;
153 #define STACK_HASH_ORDER 20
154 #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
155 #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
156 #define STACK_HASH_SEED 0x9747b28c
158 static struct stack_record *stack_table[STACK_HASH_SIZE] = {
159 [0 ... STACK_HASH_SIZE - 1] = NULL
162 /* Calculate hash for a stack */
163 static inline u32 hash_stack(unsigned long *entries, unsigned int size)
165 return jhash2((u32 *)entries,
166 size * sizeof(unsigned long) / sizeof(u32),
167 STACK_HASH_SEED);
170 /* Use our own, non-instrumented version of memcmp().
172 * We actually don't care about the order, just the equality.
174 static inline
175 int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
176 unsigned int n)
178 for ( ; n-- ; u1++, u2++) {
179 if (*u1 != *u2)
180 return 1;
182 return 0;
185 /* Find a stack that is equal to the one stored in entries in the hash */
186 static inline struct stack_record *find_stack(struct stack_record *bucket,
187 unsigned long *entries, int size,
188 u32 hash)
190 struct stack_record *found;
192 for (found = bucket; found; found = found->next) {
193 if (found->hash == hash &&
194 found->size == size &&
195 !stackdepot_memcmp(entries, found->entries, size))
196 return found;
198 return NULL;
201 void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
203 union handle_parts parts = { .handle = handle };
204 void *slab = stack_slabs[parts.slabindex];
205 size_t offset = parts.offset << STACK_ALLOC_ALIGN;
206 struct stack_record *stack = slab + offset;
208 trace->nr_entries = trace->max_entries = stack->size;
209 trace->entries = stack->entries;
210 trace->skip = 0;
212 EXPORT_SYMBOL_GPL(depot_fetch_stack);
215 * depot_save_stack - save stack in a stack depot.
216 * @trace - the stacktrace to save.
217 * @alloc_flags - flags for allocating additional memory if required.
219 * Returns the handle of the stack struct stored in depot.
221 depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
222 gfp_t alloc_flags)
224 u32 hash;
225 depot_stack_handle_t retval = 0;
226 struct stack_record *found = NULL, **bucket;
227 unsigned long flags;
228 struct page *page = NULL;
229 void *prealloc = NULL;
231 if (unlikely(trace->nr_entries == 0))
232 goto fast_exit;
234 hash = hash_stack(trace->entries, trace->nr_entries);
235 bucket = &stack_table[hash & STACK_HASH_MASK];
238 * Fast path: look the stack trace up without locking.
239 * The smp_load_acquire() here pairs with smp_store_release() to
240 * |bucket| below.
242 found = find_stack(smp_load_acquire(bucket), trace->entries,
243 trace->nr_entries, hash);
244 if (found)
245 goto exit;
248 * Check if the current or the next stack slab need to be initialized.
249 * If so, allocate the memory - we won't be able to do that under the
250 * lock.
252 * The smp_load_acquire() here pairs with smp_store_release() to
253 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
255 if (unlikely(!smp_load_acquire(&next_slab_inited))) {
257 * Zero out zone modifiers, as we don't have specific zone
258 * requirements. Keep the flags related to allocation in atomic
259 * contexts and I/O.
261 alloc_flags &= ~GFP_ZONEMASK;
262 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
263 alloc_flags |= __GFP_NOWARN;
264 page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
265 if (page)
266 prealloc = page_address(page);
269 spin_lock_irqsave(&depot_lock, flags);
271 found = find_stack(*bucket, trace->entries, trace->nr_entries, hash);
272 if (!found) {
273 struct stack_record *new =
274 depot_alloc_stack(trace->entries, trace->nr_entries,
275 hash, &prealloc, alloc_flags);
276 if (new) {
277 new->next = *bucket;
279 * This smp_store_release() pairs with
280 * smp_load_acquire() from |bucket| above.
282 smp_store_release(bucket, new);
283 found = new;
285 } else if (prealloc) {
287 * We didn't need to store this stack trace, but let's keep
288 * the preallocated memory for the future.
290 WARN_ON(!init_stack_slab(&prealloc));
293 spin_unlock_irqrestore(&depot_lock, flags);
294 exit:
295 if (prealloc) {
296 /* Nobody used this memory, ok to free it. */
297 free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
299 if (found)
300 retval = found->handle.handle;
301 fast_exit:
302 return retval;
304 EXPORT_SYMBOL_GPL(depot_save_stack);