Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 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 |
Zhen Lei | 9dbbc3b | 2021-07-07 18:07:31 -0700 | [diff] [blame] | 14 | * another in a contiguous memory allocation. |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 15 | * |
| 16 | * Author: Alexander Potapenko <glider@google.com> |
| 17 | * Copyright (C) 2016 Google, Inc. |
| 18 | * |
| 19 | * Based on code by Dmitry Chernenkov. |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 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/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> |
Vijayanand Jitta | e1fdc40 | 2021-02-25 17:21:27 -0800 | [diff] [blame] | 33 | #include <linux/memblock.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 34 | |
| 35 | #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8) |
| 36 | |
Joonsoo Kim | 7c31190 | 2016-05-05 16:22:35 -0700 | [diff] [blame] | 37 | #define STACK_ALLOC_NULL_PROTECTION_BITS 1 |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 38 | #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */ |
| 39 | #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER)) |
| 40 | #define STACK_ALLOC_ALIGN 4 |
| 41 | #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \ |
| 42 | STACK_ALLOC_ALIGN) |
Joonsoo Kim | 7c31190 | 2016-05-05 16:22:35 -0700 | [diff] [blame] | 43 | #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \ |
| 44 | STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS) |
Dmitry Vyukov | 02754e0 | 2016-10-27 17:46:44 -0700 | [diff] [blame] | 45 | #define STACK_ALLOC_SLABS_CAP 8192 |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 46 | #define STACK_ALLOC_MAX_SLABS \ |
| 47 | (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \ |
| 48 | (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP) |
| 49 | |
| 50 | /* The compact structure to store the reference to stacks. */ |
| 51 | union handle_parts { |
| 52 | depot_stack_handle_t handle; |
| 53 | struct { |
| 54 | u32 slabindex : STACK_ALLOC_INDEX_BITS; |
| 55 | u32 offset : STACK_ALLOC_OFFSET_BITS; |
Joonsoo Kim | 7c31190 | 2016-05-05 16:22:35 -0700 | [diff] [blame] | 56 | u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 57 | }; |
| 58 | }; |
| 59 | |
| 60 | struct stack_record { |
| 61 | struct stack_record *next; /* Link in the hashtable */ |
| 62 | u32 hash; /* Hash in the hastable */ |
| 63 | u32 size; /* Number of frames in the stack */ |
| 64 | union handle_parts handle; |
Gustavo A. R. Silva | 3a2b67e | 2020-12-15 20:43:04 -0800 | [diff] [blame] | 65 | unsigned long entries[]; /* Variable-sized array of entries. */ |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static void *stack_slabs[STACK_ALLOC_MAX_SLABS]; |
| 69 | |
| 70 | static int depot_index; |
| 71 | static int next_slab_inited; |
| 72 | static size_t depot_offset; |
Zqiang | 78564b9 | 2021-05-06 18:03:40 -0700 | [diff] [blame] | 73 | static DEFINE_RAW_SPINLOCK(depot_lock); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 74 | |
| 75 | static bool init_stack_slab(void **prealloc) |
| 76 | { |
| 77 | if (!*prealloc) |
| 78 | return false; |
| 79 | /* |
| 80 | * This smp_load_acquire() pairs with smp_store_release() to |
| 81 | * |next_slab_inited| below and in depot_alloc_stack(). |
| 82 | */ |
| 83 | if (smp_load_acquire(&next_slab_inited)) |
| 84 | return true; |
| 85 | if (stack_slabs[depot_index] == NULL) { |
| 86 | stack_slabs[depot_index] = *prealloc; |
Alexander Potapenko | 305e519 | 2020-02-20 20:04:30 -0800 | [diff] [blame] | 87 | *prealloc = NULL; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 88 | } else { |
Alexander Potapenko | 305e519 | 2020-02-20 20:04:30 -0800 | [diff] [blame] | 89 | /* If this is the last depot slab, do not touch the next one. */ |
| 90 | if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) { |
| 91 | stack_slabs[depot_index + 1] = *prealloc; |
| 92 | *prealloc = NULL; |
| 93 | } |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 94 | /* |
| 95 | * This smp_store_release pairs with smp_load_acquire() from |
Miles Chen | ee050dc | 2019-08-15 19:32:46 +0800 | [diff] [blame] | 96 | * |next_slab_inited| above and in stack_depot_save(). |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 97 | */ |
| 98 | smp_store_release(&next_slab_inited, 1); |
| 99 | } |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /* Allocation of a new stack in raw storage */ |
Marco Elver | 7f2b881 | 2021-11-05 13:35:36 -0700 | [diff] [blame] | 104 | static struct stack_record * |
| 105 | depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 106 | { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 107 | struct stack_record *stack; |
Gustavo A. R. Silva | 3a2b67e | 2020-12-15 20:43:04 -0800 | [diff] [blame] | 108 | size_t required_size = struct_size(stack, entries, size); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 109 | |
| 110 | required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN); |
| 111 | |
| 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"); |
| 115 | return NULL; |
| 116 | } |
| 117 | depot_index++; |
| 118 | depot_offset = 0; |
| 119 | /* |
| 120 | * smp_store_release() here pairs with smp_load_acquire() from |
Miles Chen | ee050dc | 2019-08-15 19:32:46 +0800 | [diff] [blame] | 121 | * |next_slab_inited| in stack_depot_save() and |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 122 | * init_stack_slab(). |
| 123 | */ |
| 124 | if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) |
| 125 | smp_store_release(&next_slab_inited, 0); |
| 126 | } |
| 127 | init_stack_slab(prealloc); |
| 128 | if (stack_slabs[depot_index] == NULL) |
| 129 | return NULL; |
| 130 | |
| 131 | stack = stack_slabs[depot_index] + depot_offset; |
| 132 | |
| 133 | stack->hash = hash; |
| 134 | stack->size = size; |
| 135 | stack->handle.slabindex = depot_index; |
| 136 | stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN; |
Joonsoo Kim | 7c31190 | 2016-05-05 16:22:35 -0700 | [diff] [blame] | 137 | stack->handle.valid = 1; |
Gustavo A. R. Silva | 47e684a | 2020-12-15 20:43:07 -0800 | [diff] [blame] | 138 | memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 139 | depot_offset += required_size; |
| 140 | |
| 141 | return stack; |
| 142 | } |
| 143 | |
Yogesh Lal | d262093 | 2021-02-25 17:21:24 -0800 | [diff] [blame] | 144 | #define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER) |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 145 | #define STACK_HASH_MASK (STACK_HASH_SIZE - 1) |
| 146 | #define STACK_HASH_SEED 0x9747b28c |
| 147 | |
Vijayanand Jitta | e1fdc40 | 2021-02-25 17:21:27 -0800 | [diff] [blame] | 148 | static bool stack_depot_disable; |
| 149 | static struct stack_record **stack_table; |
| 150 | |
| 151 | static int __init is_stack_depot_disabled(char *str) |
| 152 | { |
Vijayanand Jitta | 6442798 | 2021-02-25 17:21:31 -0800 | [diff] [blame] | 153 | int ret; |
| 154 | |
| 155 | ret = kstrtobool(str, &stack_depot_disable); |
| 156 | if (!ret && stack_depot_disable) { |
Vijayanand Jitta | e1fdc40 | 2021-02-25 17:21:27 -0800 | [diff] [blame] | 157 | pr_info("Stack Depot is disabled\n"); |
| 158 | stack_table = NULL; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | early_param("stack_depot_disable", is_stack_depot_disabled); |
| 163 | |
| 164 | int __init stack_depot_init(void) |
| 165 | { |
| 166 | if (!stack_depot_disable) { |
| 167 | size_t size = (STACK_HASH_SIZE * sizeof(struct stack_record *)); |
| 168 | int i; |
| 169 | |
| 170 | stack_table = memblock_alloc(size, size); |
| 171 | for (i = 0; i < STACK_HASH_SIZE; i++) |
| 172 | stack_table[i] = NULL; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 176 | |
| 177 | /* Calculate hash for a stack */ |
| 178 | static inline u32 hash_stack(unsigned long *entries, unsigned int size) |
| 179 | { |
| 180 | return jhash2((u32 *)entries, |
Gustavo A. R. Silva | 180644f | 2020-12-15 20:43:10 -0800 | [diff] [blame] | 181 | array_size(size, sizeof(*entries)) / sizeof(u32), |
| 182 | STACK_HASH_SEED); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Potapenko | a571b27 | 2018-02-06 15:38:24 -0800 | [diff] [blame] | 185 | /* Use our own, non-instrumented version of memcmp(). |
| 186 | * |
| 187 | * We actually don't care about the order, just the equality. |
| 188 | */ |
| 189 | static inline |
| 190 | int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2, |
| 191 | unsigned int n) |
| 192 | { |
| 193 | for ( ; n-- ; u1++, u2++) { |
| 194 | if (*u1 != *u2) |
| 195 | return 1; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 200 | /* Find a stack that is equal to the one stored in entries in the hash */ |
| 201 | static inline struct stack_record *find_stack(struct stack_record *bucket, |
| 202 | unsigned long *entries, int size, |
| 203 | u32 hash) |
| 204 | { |
| 205 | struct stack_record *found; |
| 206 | |
| 207 | for (found = bucket; found; found = found->next) { |
| 208 | if (found->hash == hash && |
| 209 | found->size == size && |
Alexander Potapenko | a571b27 | 2018-02-06 15:38:24 -0800 | [diff] [blame] | 210 | !stackdepot_memcmp(entries, found->entries, size)) |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 211 | return found; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 212 | } |
| 213 | return NULL; |
| 214 | } |
| 215 | |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 216 | /** |
Imran Khan | 0f68d45 | 2021-11-08 18:33:16 -0800 | [diff] [blame] | 217 | * stack_depot_snprint - print stack entries from a depot into a buffer |
| 218 | * |
| 219 | * @handle: Stack depot handle which was returned from |
| 220 | * stack_depot_save(). |
| 221 | * @buf: Pointer to the print buffer |
| 222 | * |
| 223 | * @size: Size of the print buffer |
| 224 | * |
| 225 | * @spaces: Number of leading spaces to print |
| 226 | * |
| 227 | * Return: Number of bytes printed. |
| 228 | */ |
| 229 | int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, |
| 230 | int spaces) |
| 231 | { |
| 232 | unsigned long *entries; |
| 233 | unsigned int nr_entries; |
| 234 | |
| 235 | nr_entries = stack_depot_fetch(handle, &entries); |
| 236 | return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries, |
| 237 | spaces) : 0; |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(stack_depot_snprint); |
| 240 | |
| 241 | /** |
Imran Khan | 505be48 | 2021-11-08 18:33:12 -0800 | [diff] [blame] | 242 | * stack_depot_print - print stack entries from a depot |
| 243 | * |
| 244 | * @stack: Stack depot handle which was returned from |
| 245 | * stack_depot_save(). |
| 246 | * |
| 247 | */ |
| 248 | void stack_depot_print(depot_stack_handle_t stack) |
| 249 | { |
| 250 | unsigned long *entries; |
| 251 | unsigned int nr_entries; |
| 252 | |
| 253 | nr_entries = stack_depot_fetch(stack, &entries); |
| 254 | if (nr_entries > 0) |
| 255 | stack_trace_print(entries, nr_entries, 0); |
| 256 | } |
| 257 | EXPORT_SYMBOL_GPL(stack_depot_print); |
| 258 | |
| 259 | /** |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 260 | * stack_depot_fetch - Fetch stack entries from a depot |
| 261 | * |
| 262 | * @handle: Stack depot handle which was returned from |
| 263 | * stack_depot_save(). |
| 264 | * @entries: Pointer to store the entries address |
| 265 | * |
| 266 | * Return: The number of trace entries for this depot. |
| 267 | */ |
| 268 | unsigned int stack_depot_fetch(depot_stack_handle_t handle, |
| 269 | unsigned long **entries) |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 270 | { |
| 271 | union handle_parts parts = { .handle = handle }; |
Alexander Potapenko | 69866e1 | 2020-04-06 20:10:15 -0700 | [diff] [blame] | 272 | void *slab; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 273 | size_t offset = parts.offset << STACK_ALLOC_ALIGN; |
Alexander Potapenko | 69866e1 | 2020-04-06 20:10:15 -0700 | [diff] [blame] | 274 | struct stack_record *stack; |
| 275 | |
| 276 | *entries = NULL; |
Imran Khan | 4d4712c | 2021-11-08 18:33:09 -0800 | [diff] [blame] | 277 | if (!handle) |
| 278 | return 0; |
| 279 | |
Alexander Potapenko | 69866e1 | 2020-04-06 20:10:15 -0700 | [diff] [blame] | 280 | if (parts.slabindex > depot_index) { |
| 281 | WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n", |
| 282 | parts.slabindex, depot_index, handle); |
| 283 | return 0; |
| 284 | } |
| 285 | slab = stack_slabs[parts.slabindex]; |
| 286 | if (!slab) |
| 287 | return 0; |
| 288 | stack = slab + offset; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 289 | |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 290 | *entries = stack->entries; |
| 291 | return stack->size; |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(stack_depot_fetch); |
| 294 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 295 | /** |
Marco Elver | 11ac25c6 | 2021-11-05 13:35:39 -0700 | [diff] [blame] | 296 | * __stack_depot_save - Save a stack trace from an array |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 297 | * |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 298 | * @entries: Pointer to storage array |
| 299 | * @nr_entries: Size of the storage array |
| 300 | * @alloc_flags: Allocation gfp flags |
Marco Elver | 11ac25c6 | 2021-11-05 13:35:39 -0700 | [diff] [blame] | 301 | * @can_alloc: Allocate stack slabs (increased chance of failure if false) |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 302 | * |
Marco Elver | 11ac25c6 | 2021-11-05 13:35:39 -0700 | [diff] [blame] | 303 | * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is |
| 304 | * %true, is allowed to replenish the stack slab pool in case no space is left |
| 305 | * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids |
| 306 | * any allocations and will fail if no space is left to store the stack trace. |
| 307 | * |
| 308 | * Context: Any context, but setting @can_alloc to %false is required if |
| 309 | * alloc_pages() cannot be used from the current context. Currently |
| 310 | * this is the case from contexts where neither %GFP_ATOMIC nor |
| 311 | * %GFP_NOWAIT can be used (NMI, raw_spin_lock). |
| 312 | * |
| 313 | * Return: The handle of the stack struct stored in depot, 0 on failure. |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 314 | */ |
Marco Elver | 11ac25c6 | 2021-11-05 13:35:39 -0700 | [diff] [blame] | 315 | depot_stack_handle_t __stack_depot_save(unsigned long *entries, |
| 316 | unsigned int nr_entries, |
| 317 | gfp_t alloc_flags, bool can_alloc) |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 318 | { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 319 | struct stack_record *found = NULL, **bucket; |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 320 | depot_stack_handle_t retval = 0; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 321 | struct page *page = NULL; |
| 322 | void *prealloc = NULL; |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 323 | unsigned long flags; |
| 324 | u32 hash; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 325 | |
Vijayanand Jitta | e1fdc40 | 2021-02-25 17:21:27 -0800 | [diff] [blame] | 326 | if (unlikely(nr_entries == 0) || stack_depot_disable) |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 327 | goto fast_exit; |
| 328 | |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 329 | hash = hash_stack(entries, nr_entries); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 330 | bucket = &stack_table[hash & STACK_HASH_MASK]; |
| 331 | |
| 332 | /* |
| 333 | * Fast path: look the stack trace up without locking. |
| 334 | * The smp_load_acquire() here pairs with smp_store_release() to |
| 335 | * |bucket| below. |
| 336 | */ |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 337 | found = find_stack(smp_load_acquire(bucket), entries, |
| 338 | nr_entries, hash); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 339 | if (found) |
| 340 | goto exit; |
| 341 | |
| 342 | /* |
| 343 | * Check if the current or the next stack slab need to be initialized. |
| 344 | * If so, allocate the memory - we won't be able to do that under the |
| 345 | * lock. |
| 346 | * |
| 347 | * The smp_load_acquire() here pairs with smp_store_release() to |
| 348 | * |next_slab_inited| in depot_alloc_stack() and init_stack_slab(). |
| 349 | */ |
Marco Elver | 11ac25c6 | 2021-11-05 13:35:39 -0700 | [diff] [blame] | 350 | if (unlikely(can_alloc && !smp_load_acquire(&next_slab_inited))) { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 351 | /* |
| 352 | * Zero out zone modifiers, as we don't have specific zone |
| 353 | * requirements. Keep the flags related to allocation in atomic |
| 354 | * contexts and I/O. |
| 355 | */ |
| 356 | alloc_flags &= ~GFP_ZONEMASK; |
| 357 | alloc_flags &= (GFP_ATOMIC | GFP_KERNEL); |
Kirill A. Shutemov | 87cc271 | 2016-07-28 15:49:10 -0700 | [diff] [blame] | 358 | alloc_flags |= __GFP_NOWARN; |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 359 | page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER); |
| 360 | if (page) |
| 361 | prealloc = page_address(page); |
| 362 | } |
| 363 | |
Zqiang | 78564b9 | 2021-05-06 18:03:40 -0700 | [diff] [blame] | 364 | raw_spin_lock_irqsave(&depot_lock, flags); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 365 | |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 366 | found = find_stack(*bucket, entries, nr_entries, hash); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 367 | if (!found) { |
Marco Elver | 7f2b881 | 2021-11-05 13:35:36 -0700 | [diff] [blame] | 368 | struct stack_record *new = depot_alloc_stack(entries, nr_entries, hash, &prealloc); |
| 369 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 370 | if (new) { |
| 371 | new->next = *bucket; |
| 372 | /* |
| 373 | * This smp_store_release() pairs with |
| 374 | * smp_load_acquire() from |bucket| above. |
| 375 | */ |
| 376 | smp_store_release(bucket, new); |
| 377 | found = new; |
| 378 | } |
| 379 | } else if (prealloc) { |
| 380 | /* |
| 381 | * We didn't need to store this stack trace, but let's keep |
| 382 | * the preallocated memory for the future. |
| 383 | */ |
| 384 | WARN_ON(!init_stack_slab(&prealloc)); |
| 385 | } |
| 386 | |
Zqiang | 78564b9 | 2021-05-06 18:03:40 -0700 | [diff] [blame] | 387 | raw_spin_unlock_irqrestore(&depot_lock, flags); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 388 | exit: |
| 389 | if (prealloc) { |
| 390 | /* Nobody used this memory, ok to free it. */ |
| 391 | free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER); |
| 392 | } |
| 393 | if (found) |
| 394 | retval = found->handle.handle; |
| 395 | fast_exit: |
| 396 | return retval; |
| 397 | } |
Marco Elver | 11ac25c6 | 2021-11-05 13:35:39 -0700 | [diff] [blame] | 398 | EXPORT_SYMBOL_GPL(__stack_depot_save); |
| 399 | |
| 400 | /** |
| 401 | * stack_depot_save - Save a stack trace from an array |
| 402 | * |
| 403 | * @entries: Pointer to storage array |
| 404 | * @nr_entries: Size of the storage array |
| 405 | * @alloc_flags: Allocation gfp flags |
| 406 | * |
| 407 | * Context: Contexts where allocations via alloc_pages() are allowed. |
| 408 | * See __stack_depot_save() for more details. |
| 409 | * |
| 410 | * Return: The handle of the stack struct stored in depot, 0 on failure. |
| 411 | */ |
| 412 | depot_stack_handle_t stack_depot_save(unsigned long *entries, |
| 413 | unsigned int nr_entries, |
| 414 | gfp_t alloc_flags) |
| 415 | { |
| 416 | return __stack_depot_save(entries, nr_entries, alloc_flags, true); |
| 417 | } |
Thomas Gleixner | c0cfc337 | 2019-04-25 11:44:56 +0200 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(stack_depot_save); |