Loading...
1/*
2 * Generic stack depot for storing stack traces.
3 *
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).
9 *
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.
14 *
15 * Author: Alexander Potapenko <glider@google.com>
16 * Copyright (C) 2016 Google, Inc.
17 *
18 * Based on code by Dmitry Chernenkov.
19 *
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.
23 *
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.
28 *
29 */
30
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>
42
43#define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
44
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)
57
58/* The compact structure to store the reference to stacks. */
59union 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;
65 };
66};
67
68struct 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. */
74};
75
76static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
77
78static int depot_index;
79static int next_slab_inited;
80static size_t depot_offset;
81static DEFINE_SPINLOCK(depot_lock);
82
83static bool init_stack_slab(void **prealloc)
84{
85 if (!*prealloc)
86 return false;
87 /*
88 * This smp_load_acquire() pairs with smp_store_release() to
89 * |next_slab_inited| below and in depot_alloc_stack().
90 */
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 } else {
96 stack_slabs[depot_index + 1] = *prealloc;
97 /*
98 * This smp_store_release pairs with smp_load_acquire() from
99 * |next_slab_inited| above and in depot_save_stack().
100 */
101 smp_store_release(&next_slab_inited, 1);
102 }
103 *prealloc = NULL;
104 return true;
105}
106
107/* Allocation of a new stack in raw storage */
108static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
109 u32 hash, void **prealloc, gfp_t alloc_flags)
110{
111 int required_size = offsetof(struct stack_record, entries) +
112 sizeof(unsigned long) * size;
113 struct stack_record *stack;
114
115 required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
116
117 if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
118 if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
119 WARN_ONCE(1, "Stack depot reached limit capacity");
120 return NULL;
121 }
122 depot_index++;
123 depot_offset = 0;
124 /*
125 * smp_store_release() here pairs with smp_load_acquire() from
126 * |next_slab_inited| in depot_save_stack() and
127 * init_stack_slab().
128 */
129 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
130 smp_store_release(&next_slab_inited, 0);
131 }
132 init_stack_slab(prealloc);
133 if (stack_slabs[depot_index] == NULL)
134 return NULL;
135
136 stack = stack_slabs[depot_index] + depot_offset;
137
138 stack->hash = hash;
139 stack->size = size;
140 stack->handle.slabindex = depot_index;
141 stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
142 stack->handle.valid = 1;
143 memcpy(stack->entries, entries, size * sizeof(unsigned long));
144 depot_offset += required_size;
145
146 return stack;
147}
148
149#define STACK_HASH_ORDER 20
150#define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
151#define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
152#define STACK_HASH_SEED 0x9747b28c
153
154static struct stack_record *stack_table[STACK_HASH_SIZE] = {
155 [0 ... STACK_HASH_SIZE - 1] = NULL
156};
157
158/* Calculate hash for a stack */
159static inline u32 hash_stack(unsigned long *entries, unsigned int size)
160{
161 return jhash2((u32 *)entries,
162 size * sizeof(unsigned long) / sizeof(u32),
163 STACK_HASH_SEED);
164}
165
166/* Find a stack that is equal to the one stored in entries in the hash */
167static inline struct stack_record *find_stack(struct stack_record *bucket,
168 unsigned long *entries, int size,
169 u32 hash)
170{
171 struct stack_record *found;
172
173 for (found = bucket; found; found = found->next) {
174 if (found->hash == hash &&
175 found->size == size &&
176 !memcmp(entries, found->entries,
177 size * sizeof(unsigned long))) {
178 return found;
179 }
180 }
181 return NULL;
182}
183
184void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
185{
186 union handle_parts parts = { .handle = handle };
187 void *slab = stack_slabs[parts.slabindex];
188 size_t offset = parts.offset << STACK_ALLOC_ALIGN;
189 struct stack_record *stack = slab + offset;
190
191 trace->nr_entries = trace->max_entries = stack->size;
192 trace->entries = stack->entries;
193 trace->skip = 0;
194}
195EXPORT_SYMBOL_GPL(depot_fetch_stack);
196
197/**
198 * depot_save_stack - save stack in a stack depot.
199 * @trace - the stacktrace to save.
200 * @alloc_flags - flags for allocating additional memory if required.
201 *
202 * Returns the handle of the stack struct stored in depot.
203 */
204depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
205 gfp_t alloc_flags)
206{
207 u32 hash;
208 depot_stack_handle_t retval = 0;
209 struct stack_record *found = NULL, **bucket;
210 unsigned long flags;
211 struct page *page = NULL;
212 void *prealloc = NULL;
213
214 if (unlikely(trace->nr_entries == 0))
215 goto fast_exit;
216
217 hash = hash_stack(trace->entries, trace->nr_entries);
218 bucket = &stack_table[hash & STACK_HASH_MASK];
219
220 /*
221 * Fast path: look the stack trace up without locking.
222 * The smp_load_acquire() here pairs with smp_store_release() to
223 * |bucket| below.
224 */
225 found = find_stack(smp_load_acquire(bucket), trace->entries,
226 trace->nr_entries, hash);
227 if (found)
228 goto exit;
229
230 /*
231 * Check if the current or the next stack slab need to be initialized.
232 * If so, allocate the memory - we won't be able to do that under the
233 * lock.
234 *
235 * The smp_load_acquire() here pairs with smp_store_release() to
236 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
237 */
238 if (unlikely(!smp_load_acquire(&next_slab_inited))) {
239 /*
240 * Zero out zone modifiers, as we don't have specific zone
241 * requirements. Keep the flags related to allocation in atomic
242 * contexts and I/O.
243 */
244 alloc_flags &= ~GFP_ZONEMASK;
245 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
246 alloc_flags |= __GFP_NOWARN;
247 page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
248 if (page)
249 prealloc = page_address(page);
250 }
251
252 spin_lock_irqsave(&depot_lock, flags);
253
254 found = find_stack(*bucket, trace->entries, trace->nr_entries, hash);
255 if (!found) {
256 struct stack_record *new =
257 depot_alloc_stack(trace->entries, trace->nr_entries,
258 hash, &prealloc, alloc_flags);
259 if (new) {
260 new->next = *bucket;
261 /*
262 * This smp_store_release() pairs with
263 * smp_load_acquire() from |bucket| above.
264 */
265 smp_store_release(bucket, new);
266 found = new;
267 }
268 } else if (prealloc) {
269 /*
270 * We didn't need to store this stack trace, but let's keep
271 * the preallocated memory for the future.
272 */
273 WARN_ON(!init_stack_slab(&prealloc));
274 }
275
276 spin_unlock_irqrestore(&depot_lock, flags);
277exit:
278 if (prealloc) {
279 /* Nobody used this memory, ok to free it. */
280 free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
281 }
282 if (found)
283 retval = found->handle.handle;
284fast_exit:
285 return retval;
286}
287EXPORT_SYMBOL_GPL(depot_save_stack);
1// SPDX-License-Identifier: GPL-2.0-only
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
14 * another in a contiguous memory allocation.
15 *
16 * Author: Alexander Potapenko <glider@google.com>
17 * Copyright (C) 2016 Google, Inc.
18 *
19 * Based on code by Dmitry Chernenkov.
20 */
21
22#include <linux/gfp.h>
23#include <linux/jhash.h>
24#include <linux/kernel.h>
25#include <linux/mm.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>
35#include <linux/kasan-enabled.h>
36
37#define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
38
39#define STACK_ALLOC_NULL_PROTECTION_BITS 1
40#define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
41#define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
42#define STACK_ALLOC_ALIGN 4
43#define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
44 STACK_ALLOC_ALIGN)
45#define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
46 STACK_ALLOC_NULL_PROTECTION_BITS - \
47 STACK_ALLOC_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS)
48#define STACK_ALLOC_SLABS_CAP 8192
49#define STACK_ALLOC_MAX_SLABS \
50 (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
51 (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
52
53/* The compact structure to store the reference to stacks. */
54union handle_parts {
55 depot_stack_handle_t handle;
56 struct {
57 u32 slabindex : STACK_ALLOC_INDEX_BITS;
58 u32 offset : STACK_ALLOC_OFFSET_BITS;
59 u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
60 u32 extra : STACK_DEPOT_EXTRA_BITS;
61 };
62};
63
64struct 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;
69 unsigned long entries[]; /* Variable-sized array of entries. */
70};
71
72static bool __stack_depot_want_early_init __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT);
73static bool __stack_depot_early_init_passed __initdata;
74
75static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
76
77static int depot_index;
78static int next_slab_inited;
79static size_t depot_offset;
80static DEFINE_RAW_SPINLOCK(depot_lock);
81
82unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle)
83{
84 union handle_parts parts = { .handle = handle };
85
86 return parts.extra;
87}
88EXPORT_SYMBOL(stack_depot_get_extra_bits);
89
90static bool init_stack_slab(void **prealloc)
91{
92 if (!*prealloc)
93 return false;
94 /*
95 * This smp_load_acquire() pairs with smp_store_release() to
96 * |next_slab_inited| below and in depot_alloc_stack().
97 */
98 if (smp_load_acquire(&next_slab_inited))
99 return true;
100 if (stack_slabs[depot_index] == NULL) {
101 stack_slabs[depot_index] = *prealloc;
102 *prealloc = NULL;
103 } else {
104 /* If this is the last depot slab, do not touch the next one. */
105 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
106 stack_slabs[depot_index + 1] = *prealloc;
107 *prealloc = NULL;
108 }
109 /*
110 * This smp_store_release pairs with smp_load_acquire() from
111 * |next_slab_inited| above and in stack_depot_save().
112 */
113 smp_store_release(&next_slab_inited, 1);
114 }
115 return true;
116}
117
118/* Allocation of a new stack in raw storage */
119static struct stack_record *
120depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
121{
122 struct stack_record *stack;
123 size_t required_size = struct_size(stack, entries, size);
124
125 required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
126
127 if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
128 if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
129 WARN_ONCE(1, "Stack depot reached limit capacity");
130 return NULL;
131 }
132 depot_index++;
133 depot_offset = 0;
134 /*
135 * smp_store_release() here pairs with smp_load_acquire() from
136 * |next_slab_inited| in stack_depot_save() and
137 * init_stack_slab().
138 */
139 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
140 smp_store_release(&next_slab_inited, 0);
141 }
142 init_stack_slab(prealloc);
143 if (stack_slabs[depot_index] == NULL)
144 return NULL;
145
146 stack = stack_slabs[depot_index] + depot_offset;
147
148 stack->hash = hash;
149 stack->size = size;
150 stack->handle.slabindex = depot_index;
151 stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
152 stack->handle.valid = 1;
153 stack->handle.extra = 0;
154 memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
155 depot_offset += required_size;
156
157 return stack;
158}
159
160/* one hash table bucket entry per 16kB of memory */
161#define STACK_HASH_SCALE 14
162/* limited between 4k and 1M buckets */
163#define STACK_HASH_ORDER_MIN 12
164#define STACK_HASH_ORDER_MAX 20
165#define STACK_HASH_SEED 0x9747b28c
166
167static unsigned int stack_hash_order;
168static unsigned int stack_hash_mask;
169
170static bool stack_depot_disable;
171static struct stack_record **stack_table;
172
173static int __init is_stack_depot_disabled(char *str)
174{
175 int ret;
176
177 ret = kstrtobool(str, &stack_depot_disable);
178 if (!ret && stack_depot_disable) {
179 pr_info("Stack Depot is disabled\n");
180 stack_table = NULL;
181 }
182 return 0;
183}
184early_param("stack_depot_disable", is_stack_depot_disabled);
185
186void __init stack_depot_want_early_init(void)
187{
188 /* Too late to request early init now */
189 WARN_ON(__stack_depot_early_init_passed);
190
191 __stack_depot_want_early_init = true;
192}
193
194int __init stack_depot_early_init(void)
195{
196 unsigned long entries = 0;
197
198 /* This is supposed to be called only once, from mm_init() */
199 if (WARN_ON(__stack_depot_early_init_passed))
200 return 0;
201
202 __stack_depot_early_init_passed = true;
203
204 if (kasan_enabled() && !stack_hash_order)
205 stack_hash_order = STACK_HASH_ORDER_MAX;
206
207 if (!__stack_depot_want_early_init || stack_depot_disable)
208 return 0;
209
210 if (stack_hash_order)
211 entries = 1UL << stack_hash_order;
212 stack_table = alloc_large_system_hash("stackdepot",
213 sizeof(struct stack_record *),
214 entries,
215 STACK_HASH_SCALE,
216 HASH_EARLY | HASH_ZERO,
217 NULL,
218 &stack_hash_mask,
219 1UL << STACK_HASH_ORDER_MIN,
220 1UL << STACK_HASH_ORDER_MAX);
221
222 if (!stack_table) {
223 pr_err("Stack Depot hash table allocation failed, disabling\n");
224 stack_depot_disable = true;
225 return -ENOMEM;
226 }
227
228 return 0;
229}
230
231int stack_depot_init(void)
232{
233 static DEFINE_MUTEX(stack_depot_init_mutex);
234 int ret = 0;
235
236 mutex_lock(&stack_depot_init_mutex);
237 if (!stack_depot_disable && !stack_table) {
238 unsigned long entries;
239 int scale = STACK_HASH_SCALE;
240
241 if (stack_hash_order) {
242 entries = 1UL << stack_hash_order;
243 } else {
244 entries = nr_free_buffer_pages();
245 entries = roundup_pow_of_two(entries);
246
247 if (scale > PAGE_SHIFT)
248 entries >>= (scale - PAGE_SHIFT);
249 else
250 entries <<= (PAGE_SHIFT - scale);
251 }
252
253 if (entries < 1UL << STACK_HASH_ORDER_MIN)
254 entries = 1UL << STACK_HASH_ORDER_MIN;
255 if (entries > 1UL << STACK_HASH_ORDER_MAX)
256 entries = 1UL << STACK_HASH_ORDER_MAX;
257
258 pr_info("Stack Depot allocating hash table of %lu entries with kvcalloc\n",
259 entries);
260 stack_table = kvcalloc(entries, sizeof(struct stack_record *), GFP_KERNEL);
261 if (!stack_table) {
262 pr_err("Stack Depot hash table allocation failed, disabling\n");
263 stack_depot_disable = true;
264 ret = -ENOMEM;
265 }
266 stack_hash_mask = entries - 1;
267 }
268 mutex_unlock(&stack_depot_init_mutex);
269 return ret;
270}
271EXPORT_SYMBOL_GPL(stack_depot_init);
272
273/* Calculate hash for a stack */
274static inline u32 hash_stack(unsigned long *entries, unsigned int size)
275{
276 return jhash2((u32 *)entries,
277 array_size(size, sizeof(*entries)) / sizeof(u32),
278 STACK_HASH_SEED);
279}
280
281/* Use our own, non-instrumented version of memcmp().
282 *
283 * We actually don't care about the order, just the equality.
284 */
285static inline
286int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
287 unsigned int n)
288{
289 for ( ; n-- ; u1++, u2++) {
290 if (*u1 != *u2)
291 return 1;
292 }
293 return 0;
294}
295
296/* Find a stack that is equal to the one stored in entries in the hash */
297static inline struct stack_record *find_stack(struct stack_record *bucket,
298 unsigned long *entries, int size,
299 u32 hash)
300{
301 struct stack_record *found;
302
303 for (found = bucket; found; found = found->next) {
304 if (found->hash == hash &&
305 found->size == size &&
306 !stackdepot_memcmp(entries, found->entries, size))
307 return found;
308 }
309 return NULL;
310}
311
312/**
313 * stack_depot_snprint - print stack entries from a depot into a buffer
314 *
315 * @handle: Stack depot handle which was returned from
316 * stack_depot_save().
317 * @buf: Pointer to the print buffer
318 *
319 * @size: Size of the print buffer
320 *
321 * @spaces: Number of leading spaces to print
322 *
323 * Return: Number of bytes printed.
324 */
325int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
326 int spaces)
327{
328 unsigned long *entries;
329 unsigned int nr_entries;
330
331 nr_entries = stack_depot_fetch(handle, &entries);
332 return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries,
333 spaces) : 0;
334}
335EXPORT_SYMBOL_GPL(stack_depot_snprint);
336
337/**
338 * stack_depot_print - print stack entries from a depot
339 *
340 * @stack: Stack depot handle which was returned from
341 * stack_depot_save().
342 *
343 */
344void stack_depot_print(depot_stack_handle_t stack)
345{
346 unsigned long *entries;
347 unsigned int nr_entries;
348
349 nr_entries = stack_depot_fetch(stack, &entries);
350 if (nr_entries > 0)
351 stack_trace_print(entries, nr_entries, 0);
352}
353EXPORT_SYMBOL_GPL(stack_depot_print);
354
355/**
356 * stack_depot_fetch - Fetch stack entries from a depot
357 *
358 * @handle: Stack depot handle which was returned from
359 * stack_depot_save().
360 * @entries: Pointer to store the entries address
361 *
362 * Return: The number of trace entries for this depot.
363 */
364unsigned int stack_depot_fetch(depot_stack_handle_t handle,
365 unsigned long **entries)
366{
367 union handle_parts parts = { .handle = handle };
368 void *slab;
369 size_t offset = parts.offset << STACK_ALLOC_ALIGN;
370 struct stack_record *stack;
371
372 *entries = NULL;
373 if (!handle)
374 return 0;
375
376 if (parts.slabindex > depot_index) {
377 WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
378 parts.slabindex, depot_index, handle);
379 return 0;
380 }
381 slab = stack_slabs[parts.slabindex];
382 if (!slab)
383 return 0;
384 stack = slab + offset;
385
386 *entries = stack->entries;
387 return stack->size;
388}
389EXPORT_SYMBOL_GPL(stack_depot_fetch);
390
391/**
392 * __stack_depot_save - Save a stack trace from an array
393 *
394 * @entries: Pointer to storage array
395 * @nr_entries: Size of the storage array
396 * @extra_bits: Flags to store in unused bits of depot_stack_handle_t
397 * @alloc_flags: Allocation gfp flags
398 * @can_alloc: Allocate stack slabs (increased chance of failure if false)
399 *
400 * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is
401 * %true, is allowed to replenish the stack slab pool in case no space is left
402 * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids
403 * any allocations and will fail if no space is left to store the stack trace.
404 *
405 * If the stack trace in @entries is from an interrupt, only the portion up to
406 * interrupt entry is saved.
407 *
408 * Additional opaque flags can be passed in @extra_bits, stored in the unused
409 * bits of the stack handle, and retrieved using stack_depot_get_extra_bits()
410 * without calling stack_depot_fetch().
411 *
412 * Context: Any context, but setting @can_alloc to %false is required if
413 * alloc_pages() cannot be used from the current context. Currently
414 * this is the case from contexts where neither %GFP_ATOMIC nor
415 * %GFP_NOWAIT can be used (NMI, raw_spin_lock).
416 *
417 * Return: The handle of the stack struct stored in depot, 0 on failure.
418 */
419depot_stack_handle_t __stack_depot_save(unsigned long *entries,
420 unsigned int nr_entries,
421 unsigned int extra_bits,
422 gfp_t alloc_flags, bool can_alloc)
423{
424 struct stack_record *found = NULL, **bucket;
425 union handle_parts retval = { .handle = 0 };
426 struct page *page = NULL;
427 void *prealloc = NULL;
428 unsigned long flags;
429 u32 hash;
430
431 /*
432 * If this stack trace is from an interrupt, including anything before
433 * interrupt entry usually leads to unbounded stackdepot growth.
434 *
435 * Because use of filter_irq_stacks() is a requirement to ensure
436 * stackdepot can efficiently deduplicate interrupt stacks, always
437 * filter_irq_stacks() to simplify all callers' use of stackdepot.
438 */
439 nr_entries = filter_irq_stacks(entries, nr_entries);
440
441 if (unlikely(nr_entries == 0) || stack_depot_disable)
442 goto fast_exit;
443
444 hash = hash_stack(entries, nr_entries);
445 bucket = &stack_table[hash & stack_hash_mask];
446
447 /*
448 * Fast path: look the stack trace up without locking.
449 * The smp_load_acquire() here pairs with smp_store_release() to
450 * |bucket| below.
451 */
452 found = find_stack(smp_load_acquire(bucket), entries,
453 nr_entries, hash);
454 if (found)
455 goto exit;
456
457 /*
458 * Check if the current or the next stack slab need to be initialized.
459 * If so, allocate the memory - we won't be able to do that under the
460 * lock.
461 *
462 * The smp_load_acquire() here pairs with smp_store_release() to
463 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
464 */
465 if (unlikely(can_alloc && !smp_load_acquire(&next_slab_inited))) {
466 /*
467 * Zero out zone modifiers, as we don't have specific zone
468 * requirements. Keep the flags related to allocation in atomic
469 * contexts and I/O.
470 */
471 alloc_flags &= ~GFP_ZONEMASK;
472 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
473 alloc_flags |= __GFP_NOWARN;
474 page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
475 if (page)
476 prealloc = page_address(page);
477 }
478
479 raw_spin_lock_irqsave(&depot_lock, flags);
480
481 found = find_stack(*bucket, entries, nr_entries, hash);
482 if (!found) {
483 struct stack_record *new = depot_alloc_stack(entries, nr_entries, hash, &prealloc);
484
485 if (new) {
486 new->next = *bucket;
487 /*
488 * This smp_store_release() pairs with
489 * smp_load_acquire() from |bucket| above.
490 */
491 smp_store_release(bucket, new);
492 found = new;
493 }
494 } else if (prealloc) {
495 /*
496 * We didn't need to store this stack trace, but let's keep
497 * the preallocated memory for the future.
498 */
499 WARN_ON(!init_stack_slab(&prealloc));
500 }
501
502 raw_spin_unlock_irqrestore(&depot_lock, flags);
503exit:
504 if (prealloc) {
505 /* Nobody used this memory, ok to free it. */
506 free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
507 }
508 if (found)
509 retval.handle = found->handle.handle;
510fast_exit:
511 retval.extra = extra_bits;
512
513 return retval.handle;
514}
515EXPORT_SYMBOL_GPL(__stack_depot_save);
516
517/**
518 * stack_depot_save - Save a stack trace from an array
519 *
520 * @entries: Pointer to storage array
521 * @nr_entries: Size of the storage array
522 * @alloc_flags: Allocation gfp flags
523 *
524 * Context: Contexts where allocations via alloc_pages() are allowed.
525 * See __stack_depot_save() for more details.
526 *
527 * Return: The handle of the stack struct stored in depot, 0 on failure.
528 */
529depot_stack_handle_t stack_depot_save(unsigned long *entries,
530 unsigned int nr_entries,
531 gfp_t alloc_flags)
532{
533 return __stack_depot_save(entries, nr_entries, 0, alloc_flags, true);
534}
535EXPORT_SYMBOL_GPL(stack_depot_save);