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 contiguos 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/percpu.h>
27 #include <linux/printk.h>
28 #include <linux/slab.h>
29 #include <linux/stacktrace.h>
30 #include <linux/stackdepot.h>
31 #include <linux/string.h>
32 #include <linux/types.h>
34 #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
36 #define STACK_ALLOC_NULL_PROTECTION_BITS 1
37 #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
38 #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
39 #define STACK_ALLOC_ALIGN 4
40 #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
42 #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
43 STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
44 #define STACK_ALLOC_SLABS_CAP 8192
45 #define STACK_ALLOC_MAX_SLABS \
46 (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
47 (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
49 /* The compact structure to store the reference to stacks. */
51 depot_stack_handle_t handle
;
53 u32 slabindex
: STACK_ALLOC_INDEX_BITS
;
54 u32 offset
: STACK_ALLOC_OFFSET_BITS
;
55 u32 valid
: STACK_ALLOC_NULL_PROTECTION_BITS
;
60 struct stack_record
*next
; /* Link in the hashtable */
61 u32 hash
; /* Hash in the hastable */
62 u32 size
; /* Number of frames in the stack */
63 union handle_parts handle
;
64 unsigned long entries
[1]; /* Variable-sized array of entries. */
67 static void *stack_slabs
[STACK_ALLOC_MAX_SLABS
];
69 static int depot_index
;
70 static int next_slab_inited
;
71 static size_t depot_offset
;
72 static DEFINE_SPINLOCK(depot_lock
);
74 static bool init_stack_slab(void **prealloc
)
79 * This smp_load_acquire() pairs with smp_store_release() to
80 * |next_slab_inited| below and in depot_alloc_stack().
82 if (smp_load_acquire(&next_slab_inited
))
84 if (stack_slabs
[depot_index
] == NULL
) {
85 stack_slabs
[depot_index
] = *prealloc
;
88 /* If this is the last depot slab, do not touch the next one. */
89 if (depot_index
+ 1 < STACK_ALLOC_MAX_SLABS
) {
90 stack_slabs
[depot_index
+ 1] = *prealloc
;
94 * This smp_store_release pairs with smp_load_acquire() from
95 * |next_slab_inited| above and in stack_depot_save().
97 smp_store_release(&next_slab_inited
, 1);
102 /* Allocation of a new stack in raw storage */
103 static struct stack_record
*depot_alloc_stack(unsigned long *entries
, int size
,
104 u32 hash
, void **prealloc
, gfp_t alloc_flags
)
106 int required_size
= offsetof(struct stack_record
, entries
) +
107 sizeof(unsigned long) * size
;
108 struct stack_record
*stack
;
110 required_size
= ALIGN(required_size
, 1 << STACK_ALLOC_ALIGN
);
112 if (unlikely(depot_offset
+ required_size
> STACK_ALLOC_SIZE
)) {
113 if (unlikely(depot_index
+ 1 >= STACK_ALLOC_MAX_SLABS
)) {
114 WARN_ONCE(1, "Stack depot reached limit capacity");
120 * smp_store_release() here pairs with smp_load_acquire() from
121 * |next_slab_inited| in stack_depot_save() and
124 if (depot_index
+ 1 < STACK_ALLOC_MAX_SLABS
)
125 smp_store_release(&next_slab_inited
, 0);
127 init_stack_slab(prealloc
);
128 if (stack_slabs
[depot_index
] == NULL
)
131 stack
= stack_slabs
[depot_index
] + depot_offset
;
135 stack
->handle
.slabindex
= depot_index
;
136 stack
->handle
.offset
= depot_offset
>> STACK_ALLOC_ALIGN
;
137 stack
->handle
.valid
= 1;
138 memcpy(stack
->entries
, entries
, size
* sizeof(unsigned long));
139 depot_offset
+= required_size
;
144 #define STACK_HASH_ORDER 20
145 #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
146 #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
147 #define STACK_HASH_SEED 0x9747b28c
149 static struct stack_record
*stack_table
[STACK_HASH_SIZE
] = {
150 [0 ... STACK_HASH_SIZE
- 1] = NULL
153 /* Calculate hash for a stack */
154 static inline u32
hash_stack(unsigned long *entries
, unsigned int size
)
156 return jhash2((u32
*)entries
,
157 size
* sizeof(unsigned long) / sizeof(u32
),
161 /* Use our own, non-instrumented version of memcmp().
163 * We actually don't care about the order, just the equality.
166 int stackdepot_memcmp(const unsigned long *u1
, const unsigned long *u2
,
169 for ( ; n
-- ; u1
++, u2
++) {
176 /* Find a stack that is equal to the one stored in entries in the hash */
177 static inline struct stack_record
*find_stack(struct stack_record
*bucket
,
178 unsigned long *entries
, int size
,
181 struct stack_record
*found
;
183 for (found
= bucket
; found
; found
= found
->next
) {
184 if (found
->hash
== hash
&&
185 found
->size
== size
&&
186 !stackdepot_memcmp(entries
, found
->entries
, size
))
193 * stack_depot_fetch - Fetch stack entries from a depot
195 * @handle: Stack depot handle which was returned from
196 * stack_depot_save().
197 * @entries: Pointer to store the entries address
199 * Return: The number of trace entries for this depot.
201 unsigned int stack_depot_fetch(depot_stack_handle_t handle
,
202 unsigned long **entries
)
204 union handle_parts parts
= { .handle
= handle
};
205 void *slab
= stack_slabs
[parts
.slabindex
];
206 size_t offset
= parts
.offset
<< STACK_ALLOC_ALIGN
;
207 struct stack_record
*stack
= slab
+ offset
;
209 *entries
= stack
->entries
;
212 EXPORT_SYMBOL_GPL(stack_depot_fetch
);
215 * stack_depot_save - Save a stack trace from an array
217 * @entries: Pointer to storage array
218 * @nr_entries: Size of the storage array
219 * @alloc_flags: Allocation gfp flags
221 * Return: The handle of the stack struct stored in depot
223 depot_stack_handle_t
stack_depot_save(unsigned long *entries
,
224 unsigned int nr_entries
,
227 struct stack_record
*found
= NULL
, **bucket
;
228 depot_stack_handle_t retval
= 0;
229 struct page
*page
= NULL
;
230 void *prealloc
= NULL
;
234 if (unlikely(nr_entries
== 0))
237 hash
= hash_stack(entries
, nr_entries
);
238 bucket
= &stack_table
[hash
& STACK_HASH_MASK
];
241 * Fast path: look the stack trace up without locking.
242 * The smp_load_acquire() here pairs with smp_store_release() to
245 found
= find_stack(smp_load_acquire(bucket
), entries
,
251 * Check if the current or the next stack slab need to be initialized.
252 * If so, allocate the memory - we won't be able to do that under the
255 * The smp_load_acquire() here pairs with smp_store_release() to
256 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
258 if (unlikely(!smp_load_acquire(&next_slab_inited
))) {
260 * Zero out zone modifiers, as we don't have specific zone
261 * requirements. Keep the flags related to allocation in atomic
264 alloc_flags
&= ~GFP_ZONEMASK
;
265 alloc_flags
&= (GFP_ATOMIC
| GFP_KERNEL
);
266 alloc_flags
|= __GFP_NOWARN
;
267 page
= alloc_pages(alloc_flags
, STACK_ALLOC_ORDER
);
269 prealloc
= page_address(page
);
272 spin_lock_irqsave(&depot_lock
, flags
);
274 found
= find_stack(*bucket
, entries
, nr_entries
, hash
);
276 struct stack_record
*new =
277 depot_alloc_stack(entries
, nr_entries
,
278 hash
, &prealloc
, alloc_flags
);
282 * This smp_store_release() pairs with
283 * smp_load_acquire() from |bucket| above.
285 smp_store_release(bucket
, new);
288 } else if (prealloc
) {
290 * We didn't need to store this stack trace, but let's keep
291 * the preallocated memory for the future.
293 WARN_ON(!init_stack_slab(&prealloc
));
296 spin_unlock_irqrestore(&depot_lock
, flags
);
299 /* Nobody used this memory, ok to free it. */
300 free_pages((unsigned long)prealloc
, STACK_ALLOC_ORDER
);
303 retval
= found
->handle
.handle
;
307 EXPORT_SYMBOL_GPL(stack_depot_save
);