Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
cd11016e AP |
2 | /* |
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 | |
9dbbc3b9 | 14 | * another in a contiguous memory allocation. |
cd11016e AP |
15 | * |
16 | * Author: Alexander Potapenko <glider@google.com> | |
17 | * Copyright (C) 2016 Google, Inc. | |
18 | * | |
19 | * Based on code by Dmitry Chernenkov. | |
cd11016e AP |
20 | */ |
21 | ||
4a6b5314 AK |
22 | #define pr_fmt(fmt) "stackdepot: " fmt |
23 | ||
cd11016e AP |
24 | #include <linux/gfp.h> |
25 | #include <linux/jhash.h> | |
26 | #include <linux/kernel.h> | |
27 | #include <linux/mm.h> | |
2dba5eb1 | 28 | #include <linux/mutex.h> |
cd11016e AP |
29 | #include <linux/percpu.h> |
30 | #include <linux/printk.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/stacktrace.h> | |
33 | #include <linux/stackdepot.h> | |
34 | #include <linux/string.h> | |
35 | #include <linux/types.h> | |
e1fdc403 | 36 | #include <linux/memblock.h> |
f9987921 | 37 | #include <linux/kasan-enabled.h> |
cd11016e | 38 | |
424cafee AK |
39 | #define DEPOT_HANDLE_BITS (sizeof(depot_stack_handle_t) * 8) |
40 | ||
41 | #define DEPOT_VALID_BITS 1 | |
42 | #define DEPOT_POOL_ORDER 2 /* Pool size order, 4 pages */ | |
43 | #define DEPOT_POOL_SIZE (1LL << (PAGE_SHIFT + DEPOT_POOL_ORDER)) | |
44 | #define DEPOT_STACK_ALIGN 4 | |
45 | #define DEPOT_OFFSET_BITS (DEPOT_POOL_ORDER + PAGE_SHIFT - DEPOT_STACK_ALIGN) | |
46 | #define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_VALID_BITS - \ | |
47 | DEPOT_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS) | |
48 | #define DEPOT_POOLS_CAP 8192 | |
49 | #define DEPOT_MAX_POOLS \ | |
50 | (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \ | |
51 | (1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP) | |
cd11016e AP |
52 | |
53 | /* The compact structure to store the reference to stacks. */ | |
54 | union handle_parts { | |
55 | depot_stack_handle_t handle; | |
56 | struct { | |
424cafee AK |
57 | u32 pool_index : DEPOT_POOL_INDEX_BITS; |
58 | u32 offset : DEPOT_OFFSET_BITS; | |
59 | u32 valid : DEPOT_VALID_BITS; | |
60 | u32 extra : STACK_DEPOT_EXTRA_BITS; | |
cd11016e AP |
61 | }; |
62 | }; | |
63 | ||
64 | struct stack_record { | |
65 | struct stack_record *next; /* Link in the hashtable */ | |
66 | u32 hash; /* Hash in the hastable */ | |
67 | u32 size; /* Number of frames in the stack */ | |
68 | union handle_parts handle; | |
3a2b67e6 | 69 | unsigned long entries[]; /* Variable-sized array of entries. */ |
cd11016e AP |
70 | }; |
71 | ||
735df3c3 | 72 | static bool stack_depot_disabled; |
1c0310ad | 73 | static bool __stack_depot_early_init_requested __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT); |
a5f1783b VB |
74 | static bool __stack_depot_early_init_passed __initdata; |
75 | ||
0d249ac0 | 76 | /* Use one hash table bucket per 16 KB of memory. */ |
4c2e9a67 | 77 | #define STACK_HASH_TABLE_SCALE 14 |
0d249ac0 | 78 | /* Limit the number of buckets between 4K and 1M. */ |
4c2e9a67 AK |
79 | #define STACK_BUCKET_NUMBER_ORDER_MIN 12 |
80 | #define STACK_BUCKET_NUMBER_ORDER_MAX 20 | |
0d249ac0 | 81 | /* Initial seed for jhash2. */ |
cd11016e AP |
82 | #define STACK_HASH_SEED 0x9747b28c |
83 | ||
0d249ac0 AK |
84 | /* Hash table of pointers to stored stack traces. */ |
85 | static struct stack_record **stack_table; | |
86 | /* Fixed order of the number of table buckets. Used when KASAN is enabled. */ | |
4c2e9a67 | 87 | static unsigned int stack_bucket_number_order; |
0d249ac0 | 88 | /* Hash mask for indexing the table. */ |
f9987921 VB |
89 | static unsigned int stack_hash_mask; |
90 | ||
0d249ac0 | 91 | /* Array of memory regions that store stack traces. */ |
424cafee | 92 | static void *stack_pools[DEPOT_MAX_POOLS]; |
961c949b AK |
93 | /* Currently used pool in stack_pools. */ |
94 | static int pool_index; | |
95 | /* Offset to the unused space in the currently used pool. */ | |
96 | static size_t pool_offset; | |
0d249ac0 | 97 | /* Lock that protects the variables above. */ |
961c949b | 98 | static DEFINE_RAW_SPINLOCK(pool_lock); |
d11a5621 AK |
99 | /* |
100 | * Stack depot tries to keep an extra pool allocated even before it runs out | |
101 | * of space in the currently used pool. | |
102 | * This flag marks that this next extra pool needs to be allocated and | |
103 | * initialized. It has the value 0 when either the next pool is not yet | |
104 | * initialized or the limit on the number of pools is reached. | |
105 | */ | |
106 | static int next_pool_required = 1; | |
e1fdc403 | 107 | |
735df3c3 | 108 | static int __init disable_stack_depot(char *str) |
e1fdc403 | 109 | { |
64427985 VJ |
110 | int ret; |
111 | ||
735df3c3 AK |
112 | ret = kstrtobool(str, &stack_depot_disabled); |
113 | if (!ret && stack_depot_disabled) { | |
4a6b5314 | 114 | pr_info("disabled\n"); |
e1fdc403 VJ |
115 | stack_table = NULL; |
116 | } | |
117 | return 0; | |
118 | } | |
735df3c3 | 119 | early_param("stack_depot_disable", disable_stack_depot); |
e1fdc403 | 120 | |
1c0310ad | 121 | void __init stack_depot_request_early_init(void) |
a5f1783b | 122 | { |
1c0310ad | 123 | /* Too late to request early init now. */ |
a5f1783b VB |
124 | WARN_ON(__stack_depot_early_init_passed); |
125 | ||
1c0310ad | 126 | __stack_depot_early_init_requested = true; |
a5f1783b VB |
127 | } |
128 | ||
df225c87 | 129 | /* Allocates a hash table via memblock. Can only be used during early boot. */ |
a5f1783b VB |
130 | int __init stack_depot_early_init(void) |
131 | { | |
f9987921 | 132 | unsigned long entries = 0; |
a5f1783b | 133 | |
df225c87 | 134 | /* This function must be called only once, from mm_init(). */ |
a5f1783b VB |
135 | if (WARN_ON(__stack_depot_early_init_passed)) |
136 | return 0; | |
a5f1783b VB |
137 | __stack_depot_early_init_passed = true; |
138 | ||
df225c87 AK |
139 | /* |
140 | * If KASAN is enabled, use the maximum order: KASAN is frequently used | |
141 | * in fuzzing scenarios, which leads to a large number of different | |
142 | * stack traces being stored in stack depot. | |
143 | */ | |
4c2e9a67 AK |
144 | if (kasan_enabled() && !stack_bucket_number_order) |
145 | stack_bucket_number_order = STACK_BUCKET_NUMBER_ORDER_MAX; | |
f9987921 | 146 | |
735df3c3 | 147 | if (!__stack_depot_early_init_requested || stack_depot_disabled) |
a5f1783b VB |
148 | return 0; |
149 | ||
df225c87 | 150 | /* |
4c2e9a67 AK |
151 | * If stack_bucket_number_order is not set, leave entries as 0 to rely |
152 | * on the automatic calculations performed by alloc_large_system_hash. | |
df225c87 | 153 | */ |
4c2e9a67 AK |
154 | if (stack_bucket_number_order) |
155 | entries = 1UL << stack_bucket_number_order; | |
df225c87 | 156 | pr_info("allocating hash table via alloc_large_system_hash\n"); |
f9987921 VB |
157 | stack_table = alloc_large_system_hash("stackdepot", |
158 | sizeof(struct stack_record *), | |
159 | entries, | |
4c2e9a67 | 160 | STACK_HASH_TABLE_SCALE, |
f9987921 VB |
161 | HASH_EARLY | HASH_ZERO, |
162 | NULL, | |
163 | &stack_hash_mask, | |
4c2e9a67 AK |
164 | 1UL << STACK_BUCKET_NUMBER_ORDER_MIN, |
165 | 1UL << STACK_BUCKET_NUMBER_ORDER_MAX); | |
a5f1783b | 166 | if (!stack_table) { |
4a6b5314 | 167 | pr_err("hash table allocation failed, disabling\n"); |
735df3c3 | 168 | stack_depot_disabled = true; |
a5f1783b VB |
169 | return -ENOMEM; |
170 | } | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
df225c87 | 175 | /* Allocates a hash table via kvcalloc. Can be used after boot. */ |
a5f1783b | 176 | int stack_depot_init(void) |
e1fdc403 | 177 | { |
2dba5eb1 | 178 | static DEFINE_MUTEX(stack_depot_init_mutex); |
c60324fb | 179 | unsigned long entries; |
a5f1783b | 180 | int ret = 0; |
2dba5eb1 VB |
181 | |
182 | mutex_lock(&stack_depot_init_mutex); | |
f9987921 | 183 | |
c60324fb AK |
184 | if (stack_depot_disabled || stack_table) |
185 | goto out_unlock; | |
f9987921 | 186 | |
c60324fb | 187 | /* |
4c2e9a67 | 188 | * Similarly to stack_depot_early_init, use stack_bucket_number_order |
c60324fb AK |
189 | * if assigned, and rely on automatic scaling otherwise. |
190 | */ | |
4c2e9a67 AK |
191 | if (stack_bucket_number_order) { |
192 | entries = 1UL << stack_bucket_number_order; | |
c60324fb | 193 | } else { |
4c2e9a67 | 194 | int scale = STACK_HASH_TABLE_SCALE; |
c60324fb AK |
195 | |
196 | entries = nr_free_buffer_pages(); | |
197 | entries = roundup_pow_of_two(entries); | |
198 | ||
199 | if (scale > PAGE_SHIFT) | |
200 | entries >>= (scale - PAGE_SHIFT); | |
201 | else | |
202 | entries <<= (PAGE_SHIFT - scale); | |
e1fdc403 | 203 | } |
c60324fb | 204 | |
4c2e9a67 AK |
205 | if (entries < 1UL << STACK_BUCKET_NUMBER_ORDER_MIN) |
206 | entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MIN; | |
207 | if (entries > 1UL << STACK_BUCKET_NUMBER_ORDER_MAX) | |
208 | entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MAX; | |
c60324fb AK |
209 | |
210 | pr_info("allocating hash table of %lu entries via kvcalloc\n", entries); | |
211 | stack_table = kvcalloc(entries, sizeof(struct stack_record *), GFP_KERNEL); | |
212 | if (!stack_table) { | |
213 | pr_err("hash table allocation failed, disabling\n"); | |
214 | stack_depot_disabled = true; | |
215 | ret = -ENOMEM; | |
216 | goto out_unlock; | |
217 | } | |
218 | stack_hash_mask = entries - 1; | |
219 | ||
220 | out_unlock: | |
2dba5eb1 | 221 | mutex_unlock(&stack_depot_init_mutex); |
c60324fb | 222 | |
a5f1783b | 223 | return ret; |
e1fdc403 | 224 | } |
2dba5eb1 | 225 | EXPORT_SYMBOL_GPL(stack_depot_init); |
cd11016e | 226 | |
cd0fc64e | 227 | /* Uses preallocated memory to initialize a new stack depot pool. */ |
514d5c55 | 228 | static void depot_init_pool(void **prealloc) |
15ef6a98 | 229 | { |
15ef6a98 | 230 | /* |
d11a5621 AK |
231 | * If the next pool is already initialized or the maximum number of |
232 | * pools is reached, do not use the preallocated memory. | |
cd0fc64e AK |
233 | * smp_load_acquire() here pairs with smp_store_release() below and |
234 | * in depot_alloc_stack(). | |
15ef6a98 | 235 | */ |
d11a5621 | 236 | if (!smp_load_acquire(&next_pool_required)) |
514d5c55 | 237 | return; |
cd0fc64e AK |
238 | |
239 | /* Check if the current pool is not yet allocated. */ | |
961c949b | 240 | if (stack_pools[pool_index] == NULL) { |
cd0fc64e | 241 | /* Use the preallocated memory for the current pool. */ |
961c949b | 242 | stack_pools[pool_index] = *prealloc; |
15ef6a98 AK |
243 | *prealloc = NULL; |
244 | } else { | |
cd0fc64e AK |
245 | /* |
246 | * Otherwise, use the preallocated memory for the next pool | |
247 | * as long as we do not exceed the maximum number of pools. | |
248 | */ | |
424cafee | 249 | if (pool_index + 1 < DEPOT_MAX_POOLS) { |
961c949b | 250 | stack_pools[pool_index + 1] = *prealloc; |
15ef6a98 AK |
251 | *prealloc = NULL; |
252 | } | |
253 | /* | |
d11a5621 AK |
254 | * At this point, either the next pool is initialized or the |
255 | * maximum number of pools is reached. In either case, take | |
256 | * note that initializing another pool is not required. | |
cd0fc64e AK |
257 | * This smp_store_release pairs with smp_load_acquire() above |
258 | * and in stack_depot_save(). | |
15ef6a98 | 259 | */ |
d11a5621 | 260 | smp_store_release(&next_pool_required, 0); |
15ef6a98 | 261 | } |
15ef6a98 AK |
262 | } |
263 | ||
cd0fc64e | 264 | /* Allocates a new stack in a stack depot pool. */ |
15ef6a98 AK |
265 | static struct stack_record * |
266 | depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) | |
267 | { | |
268 | struct stack_record *stack; | |
269 | size_t required_size = struct_size(stack, entries, size); | |
270 | ||
424cafee | 271 | required_size = ALIGN(required_size, 1 << DEPOT_STACK_ALIGN); |
15ef6a98 | 272 | |
cd0fc64e | 273 | /* Check if there is not enough space in the current pool. */ |
424cafee | 274 | if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) { |
cd0fc64e | 275 | /* Bail out if we reached the pool limit. */ |
424cafee | 276 | if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) { |
15ef6a98 AK |
277 | WARN_ONCE(1, "Stack depot reached limit capacity"); |
278 | return NULL; | |
279 | } | |
cd0fc64e AK |
280 | |
281 | /* Move on to the next pool. */ | |
961c949b AK |
282 | pool_index++; |
283 | pool_offset = 0; | |
15ef6a98 | 284 | /* |
d11a5621 AK |
285 | * If the maximum number of pools is not reached, take note |
286 | * that the next pool needs to initialized. | |
cd0fc64e AK |
287 | * smp_store_release() here pairs with smp_load_acquire() in |
288 | * stack_depot_save() and depot_init_pool(). | |
15ef6a98 | 289 | */ |
424cafee | 290 | if (pool_index + 1 < DEPOT_MAX_POOLS) |
d11a5621 | 291 | smp_store_release(&next_pool_required, 1); |
15ef6a98 | 292 | } |
cd0fc64e AK |
293 | |
294 | /* Assign the preallocated memory to a pool if required. */ | |
514d5c55 AK |
295 | if (*prealloc) |
296 | depot_init_pool(prealloc); | |
cd0fc64e AK |
297 | |
298 | /* Check if we have a pool to save the stack trace. */ | |
961c949b | 299 | if (stack_pools[pool_index] == NULL) |
15ef6a98 AK |
300 | return NULL; |
301 | ||
cd0fc64e | 302 | /* Save the stack trace. */ |
961c949b | 303 | stack = stack_pools[pool_index] + pool_offset; |
15ef6a98 AK |
304 | stack->hash = hash; |
305 | stack->size = size; | |
961c949b | 306 | stack->handle.pool_index = pool_index; |
424cafee | 307 | stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN; |
15ef6a98 AK |
308 | stack->handle.valid = 1; |
309 | stack->handle.extra = 0; | |
310 | memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); | |
961c949b | 311 | pool_offset += required_size; |
15ef6a98 AK |
312 | |
313 | return stack; | |
314 | } | |
315 | ||
cd11016e AP |
316 | /* Calculate hash for a stack */ |
317 | static inline u32 hash_stack(unsigned long *entries, unsigned int size) | |
318 | { | |
319 | return jhash2((u32 *)entries, | |
180644f8 GS |
320 | array_size(size, sizeof(*entries)) / sizeof(u32), |
321 | STACK_HASH_SEED); | |
cd11016e AP |
322 | } |
323 | ||
a571b272 AP |
324 | /* Use our own, non-instrumented version of memcmp(). |
325 | * | |
326 | * We actually don't care about the order, just the equality. | |
327 | */ | |
328 | static inline | |
329 | int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2, | |
330 | unsigned int n) | |
331 | { | |
332 | for ( ; n-- ; u1++, u2++) { | |
333 | if (*u1 != *u2) | |
334 | return 1; | |
335 | } | |
336 | return 0; | |
337 | } | |
338 | ||
cd11016e AP |
339 | /* Find a stack that is equal to the one stored in entries in the hash */ |
340 | static inline struct stack_record *find_stack(struct stack_record *bucket, | |
341 | unsigned long *entries, int size, | |
342 | u32 hash) | |
343 | { | |
344 | struct stack_record *found; | |
345 | ||
346 | for (found = bucket; found; found = found->next) { | |
347 | if (found->hash == hash && | |
348 | found->size == size && | |
a571b272 | 349 | !stackdepot_memcmp(entries, found->entries, size)) |
cd11016e | 350 | return found; |
cd11016e AP |
351 | } |
352 | return NULL; | |
353 | } | |
354 | ||
cd11016e | 355 | /** |
11ac25c6 | 356 | * __stack_depot_save - Save a stack trace from an array |
c0cfc337 TG |
357 | * |
358 | * @entries: Pointer to storage array | |
359 | * @nr_entries: Size of the storage array | |
360 | * @alloc_flags: Allocation gfp flags | |
961c949b | 361 | * @can_alloc: Allocate stack pools (increased chance of failure if false) |
11ac25c6 ME |
362 | * |
363 | * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is | |
961c949b | 364 | * %true, is allowed to replenish the stack pool in case no space is left |
11ac25c6 ME |
365 | * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids |
366 | * any allocations and will fail if no space is left to store the stack trace. | |
367 | * | |
e9400660 ME |
368 | * If the stack trace in @entries is from an interrupt, only the portion up to |
369 | * interrupt entry is saved. | |
370 | * | |
11ac25c6 ME |
371 | * Context: Any context, but setting @can_alloc to %false is required if |
372 | * alloc_pages() cannot be used from the current context. Currently | |
373 | * this is the case from contexts where neither %GFP_ATOMIC nor | |
374 | * %GFP_NOWAIT can be used (NMI, raw_spin_lock). | |
cd11016e | 375 | * |
11ac25c6 | 376 | * Return: The handle of the stack struct stored in depot, 0 on failure. |
cd11016e | 377 | */ |
11ac25c6 ME |
378 | depot_stack_handle_t __stack_depot_save(unsigned long *entries, |
379 | unsigned int nr_entries, | |
380 | gfp_t alloc_flags, bool can_alloc) | |
cd11016e | 381 | { |
cd11016e | 382 | struct stack_record *found = NULL, **bucket; |
83a4f1ef | 383 | union handle_parts retval = { .handle = 0 }; |
cd11016e AP |
384 | struct page *page = NULL; |
385 | void *prealloc = NULL; | |
c0cfc337 TG |
386 | unsigned long flags; |
387 | u32 hash; | |
cd11016e | 388 | |
e9400660 ME |
389 | /* |
390 | * If this stack trace is from an interrupt, including anything before | |
391 | * interrupt entry usually leads to unbounded stackdepot growth. | |
392 | * | |
393 | * Because use of filter_irq_stacks() is a requirement to ensure | |
394 | * stackdepot can efficiently deduplicate interrupt stacks, always | |
395 | * filter_irq_stacks() to simplify all callers' use of stackdepot. | |
396 | */ | |
397 | nr_entries = filter_irq_stacks(entries, nr_entries); | |
398 | ||
735df3c3 | 399 | if (unlikely(nr_entries == 0) || stack_depot_disabled) |
cd11016e AP |
400 | goto fast_exit; |
401 | ||
c0cfc337 | 402 | hash = hash_stack(entries, nr_entries); |
f9987921 | 403 | bucket = &stack_table[hash & stack_hash_mask]; |
cd11016e AP |
404 | |
405 | /* | |
406 | * Fast path: look the stack trace up without locking. | |
407 | * The smp_load_acquire() here pairs with smp_store_release() to | |
408 | * |bucket| below. | |
409 | */ | |
c0cfc337 TG |
410 | found = find_stack(smp_load_acquire(bucket), entries, |
411 | nr_entries, hash); | |
cd11016e AP |
412 | if (found) |
413 | goto exit; | |
414 | ||
415 | /* | |
d11a5621 AK |
416 | * Check if another stack pool needs to be initialized. If so, allocate |
417 | * the memory now - we won't be able to do that under the lock. | |
cd11016e AP |
418 | * |
419 | * The smp_load_acquire() here pairs with smp_store_release() to | |
cb788e84 | 420 | * |next_pool_inited| in depot_alloc_stack() and depot_init_pool(). |
cd11016e | 421 | */ |
d11a5621 | 422 | if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { |
cd11016e AP |
423 | /* |
424 | * Zero out zone modifiers, as we don't have specific zone | |
425 | * requirements. Keep the flags related to allocation in atomic | |
426 | * contexts and I/O. | |
427 | */ | |
428 | alloc_flags &= ~GFP_ZONEMASK; | |
429 | alloc_flags &= (GFP_ATOMIC | GFP_KERNEL); | |
87cc271d | 430 | alloc_flags |= __GFP_NOWARN; |
424cafee | 431 | page = alloc_pages(alloc_flags, DEPOT_POOL_ORDER); |
cd11016e AP |
432 | if (page) |
433 | prealloc = page_address(page); | |
434 | } | |
435 | ||
961c949b | 436 | raw_spin_lock_irqsave(&pool_lock, flags); |
cd11016e | 437 | |
c0cfc337 | 438 | found = find_stack(*bucket, entries, nr_entries, hash); |
cd11016e | 439 | if (!found) { |
7f2b8818 ME |
440 | struct stack_record *new = depot_alloc_stack(entries, nr_entries, hash, &prealloc); |
441 | ||
cd11016e AP |
442 | if (new) { |
443 | new->next = *bucket; | |
444 | /* | |
445 | * This smp_store_release() pairs with | |
446 | * smp_load_acquire() from |bucket| above. | |
447 | */ | |
448 | smp_store_release(bucket, new); | |
449 | found = new; | |
450 | } | |
451 | } else if (prealloc) { | |
452 | /* | |
453 | * We didn't need to store this stack trace, but let's keep | |
454 | * the preallocated memory for the future. | |
455 | */ | |
514d5c55 | 456 | depot_init_pool(&prealloc); |
cd11016e AP |
457 | } |
458 | ||
961c949b | 459 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
cd11016e AP |
460 | exit: |
461 | if (prealloc) { | |
462 | /* Nobody used this memory, ok to free it. */ | |
424cafee | 463 | free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER); |
cd11016e AP |
464 | } |
465 | if (found) | |
83a4f1ef | 466 | retval.handle = found->handle.handle; |
cd11016e | 467 | fast_exit: |
83a4f1ef | 468 | return retval.handle; |
cd11016e | 469 | } |
11ac25c6 ME |
470 | EXPORT_SYMBOL_GPL(__stack_depot_save); |
471 | ||
472 | /** | |
473 | * stack_depot_save - Save a stack trace from an array | |
474 | * | |
475 | * @entries: Pointer to storage array | |
476 | * @nr_entries: Size of the storage array | |
477 | * @alloc_flags: Allocation gfp flags | |
478 | * | |
479 | * Context: Contexts where allocations via alloc_pages() are allowed. | |
480 | * See __stack_depot_save() for more details. | |
481 | * | |
482 | * Return: The handle of the stack struct stored in depot, 0 on failure. | |
483 | */ | |
484 | depot_stack_handle_t stack_depot_save(unsigned long *entries, | |
485 | unsigned int nr_entries, | |
486 | gfp_t alloc_flags) | |
487 | { | |
36aa1e67 | 488 | return __stack_depot_save(entries, nr_entries, alloc_flags, true); |
11ac25c6 | 489 | } |
c0cfc337 | 490 | EXPORT_SYMBOL_GPL(stack_depot_save); |
15ef6a98 AK |
491 | |
492 | /** | |
493 | * stack_depot_fetch - Fetch stack entries from a depot | |
494 | * | |
495 | * @handle: Stack depot handle which was returned from | |
496 | * stack_depot_save(). | |
497 | * @entries: Pointer to store the entries address | |
498 | * | |
499 | * Return: The number of trace entries for this depot. | |
500 | */ | |
501 | unsigned int stack_depot_fetch(depot_stack_handle_t handle, | |
502 | unsigned long **entries) | |
503 | { | |
504 | union handle_parts parts = { .handle = handle }; | |
961c949b | 505 | void *pool; |
424cafee | 506 | size_t offset = parts.offset << DEPOT_STACK_ALIGN; |
15ef6a98 AK |
507 | struct stack_record *stack; |
508 | ||
509 | *entries = NULL; | |
510 | if (!handle) | |
511 | return 0; | |
512 | ||
961c949b AK |
513 | if (parts.pool_index > pool_index) { |
514 | WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", | |
515 | parts.pool_index, pool_index, handle); | |
15ef6a98 AK |
516 | return 0; |
517 | } | |
961c949b AK |
518 | pool = stack_pools[parts.pool_index]; |
519 | if (!pool) | |
15ef6a98 | 520 | return 0; |
961c949b | 521 | stack = pool + offset; |
15ef6a98 AK |
522 | |
523 | *entries = stack->entries; | |
524 | return stack->size; | |
525 | } | |
526 | EXPORT_SYMBOL_GPL(stack_depot_fetch); | |
527 | ||
528 | /** | |
529 | * stack_depot_print - print stack entries from a depot | |
530 | * | |
531 | * @stack: Stack depot handle which was returned from | |
532 | * stack_depot_save(). | |
533 | * | |
534 | */ | |
535 | void stack_depot_print(depot_stack_handle_t stack) | |
536 | { | |
537 | unsigned long *entries; | |
538 | unsigned int nr_entries; | |
539 | ||
540 | nr_entries = stack_depot_fetch(stack, &entries); | |
541 | if (nr_entries > 0) | |
542 | stack_trace_print(entries, nr_entries, 0); | |
543 | } | |
544 | EXPORT_SYMBOL_GPL(stack_depot_print); | |
545 | ||
546 | /** | |
547 | * stack_depot_snprint - print stack entries from a depot into a buffer | |
548 | * | |
549 | * @handle: Stack depot handle which was returned from | |
550 | * stack_depot_save(). | |
551 | * @buf: Pointer to the print buffer | |
552 | * | |
553 | * @size: Size of the print buffer | |
554 | * | |
555 | * @spaces: Number of leading spaces to print | |
556 | * | |
557 | * Return: Number of bytes printed. | |
558 | */ | |
559 | int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, | |
560 | int spaces) | |
561 | { | |
562 | unsigned long *entries; | |
563 | unsigned int nr_entries; | |
564 | ||
565 | nr_entries = stack_depot_fetch(handle, &entries); | |
566 | return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries, | |
567 | spaces) : 0; | |
568 | } | |
569 | EXPORT_SYMBOL_GPL(stack_depot_snprint); | |
570 | ||
36aa1e67 AK |
571 | /** |
572 | * stack_depot_set_extra_bits - Set extra bits in a stack depot handle | |
573 | * | |
574 | * @handle: Stack depot handle returned from stack_depot_save() | |
575 | * @extra_bits: Value to set the extra bits | |
576 | * | |
577 | * Return: Stack depot handle with extra bits set | |
578 | * | |
579 | * Stack depot handles have a few unused bits, which can be used for storing | |
580 | * user-specific information. These bits are transparent to the stack depot. | |
581 | */ | |
582 | depot_stack_handle_t __must_check stack_depot_set_extra_bits( | |
583 | depot_stack_handle_t handle, unsigned int extra_bits) | |
584 | { | |
585 | union handle_parts parts = { .handle = handle }; | |
586 | ||
587 | /* Don't set extra bits on empty handles. */ | |
588 | if (!handle) | |
589 | return 0; | |
590 | ||
591 | parts.extra = extra_bits; | |
592 | return parts.handle; | |
593 | } | |
594 | EXPORT_SYMBOL(stack_depot_set_extra_bits); | |
595 | ||
596 | /** | |
597 | * stack_depot_get_extra_bits - Retrieve extra bits from a stack depot handle | |
598 | * | |
599 | * @handle: Stack depot handle with extra bits saved | |
600 | * | |
601 | * Return: Extra bits retrieved from the stack depot handle | |
602 | */ | |
15ef6a98 AK |
603 | unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) |
604 | { | |
605 | union handle_parts parts = { .handle = handle }; | |
606 | ||
607 | return parts.extra; | |
608 | } | |
609 | EXPORT_SYMBOL(stack_depot_get_extra_bits); |