blob: 2f5aa851834ebb36c79274a2c6183c6cea45747b [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Alexander Potapenkocd110162016-03-25 14:22:08 -07002/*
Andrey Konovalovb232b992023-02-10 22:16:05 +01003 * Stack depot - a stack trace storage that avoids duplication.
Alexander Potapenkocd110162016-03-25 14:22:08 -07004 *
Andrey Konovalovb232b992023-02-10 22:16:05 +01005 * Internally, stack depot maintains a hash table of unique stacktraces. The
6 * stack traces themselves are stored contiguously one after another in a set
7 * of separate page allocations.
8 *
Alexander Potapenkocd110162016-03-25 14:22:08 -07009 * Author: Alexander Potapenko <glider@google.com>
10 * Copyright (C) 2016 Google, Inc.
11 *
Andrey Konovalovb232b992023-02-10 22:16:05 +010012 * Based on the code by Dmitry Chernenkov.
Alexander Potapenkocd110162016-03-25 14:22:08 -070013 */
14
Andrey Konovalov4a6b5312023-02-10 22:15:50 +010015#define pr_fmt(fmt) "stackdepot: " fmt
16
Alexander Potapenkocd110162016-03-25 14:22:08 -070017#include <linux/gfp.h>
18#include <linux/jhash.h>
19#include <linux/kernel.h>
Alexander Potapenko8e00b2d2023-03-06 12:13:21 +010020#include <linux/kmsan.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070021#include <linux/mm.h>
Vlastimil Babka2dba5eb2022-01-21 22:14:27 -080022#include <linux/mutex.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070023#include <linux/percpu.h>
24#include <linux/printk.h>
25#include <linux/slab.h>
26#include <linux/stacktrace.h>
27#include <linux/stackdepot.h>
28#include <linux/string.h>
29#include <linux/types.h>
Vijayanand Jittae1fdc402021-02-25 17:21:27 -080030#include <linux/memblock.h>
Vlastimil Babkaf9987922022-06-20 17:02:49 +020031#include <linux/kasan-enabled.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070032
Andrey Konovalov424cafe2023-02-10 22:15:58 +010033#define DEPOT_HANDLE_BITS (sizeof(depot_stack_handle_t) * 8)
Alexander Potapenkocd110162016-03-25 14:22:08 -070034
Andrey Konovalov424cafe2023-02-10 22:15:58 +010035#define DEPOT_VALID_BITS 1
36#define DEPOT_POOL_ORDER 2 /* Pool size order, 4 pages */
37#define DEPOT_POOL_SIZE (1LL << (PAGE_SHIFT + DEPOT_POOL_ORDER))
38#define DEPOT_STACK_ALIGN 4
39#define DEPOT_OFFSET_BITS (DEPOT_POOL_ORDER + PAGE_SHIFT - DEPOT_STACK_ALIGN)
40#define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_VALID_BITS - \
41 DEPOT_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS)
42#define DEPOT_POOLS_CAP 8192
43#define DEPOT_MAX_POOLS \
44 (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \
45 (1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP)
Alexander Potapenkocd110162016-03-25 14:22:08 -070046
Andrey Konovalovb232b992023-02-10 22:16:05 +010047/* Compact structure that stores a reference to a stack. */
Alexander Potapenkocd110162016-03-25 14:22:08 -070048union handle_parts {
49 depot_stack_handle_t handle;
50 struct {
Andrey Konovalov424cafe2023-02-10 22:15:58 +010051 u32 pool_index : DEPOT_POOL_INDEX_BITS;
52 u32 offset : DEPOT_OFFSET_BITS;
53 u32 valid : DEPOT_VALID_BITS;
54 u32 extra : STACK_DEPOT_EXTRA_BITS;
Alexander Potapenkocd110162016-03-25 14:22:08 -070055 };
56};
57
58struct stack_record {
Andrey Konovalovb232b992023-02-10 22:16:05 +010059 struct stack_record *next; /* Link in the hash table */
60 u32 hash; /* Hash in the hash table */
61 u32 size; /* Number of stored frames */
Alexander Potapenkocd110162016-03-25 14:22:08 -070062 union handle_parts handle;
Andrey Konovalovb232b992023-02-10 22:16:05 +010063 unsigned long entries[]; /* Variable-sized array of frames */
Alexander Potapenkocd110162016-03-25 14:22:08 -070064};
65
Andrey Konovalov735df3c2023-02-10 22:15:52 +010066static bool stack_depot_disabled;
Andrey Konovalov1c0310a2023-02-10 22:15:51 +010067static bool __stack_depot_early_init_requested __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT);
Vlastimil Babkaa5f17832022-03-02 12:02:22 +010068static bool __stack_depot_early_init_passed __initdata;
69
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010070/* Use one hash table bucket per 16 KB of memory. */
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +010071#define STACK_HASH_TABLE_SCALE 14
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010072/* Limit the number of buckets between 4K and 1M. */
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +010073#define STACK_BUCKET_NUMBER_ORDER_MIN 12
74#define STACK_BUCKET_NUMBER_ORDER_MAX 20
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010075/* Initial seed for jhash2. */
Alexander Potapenkocd110162016-03-25 14:22:08 -070076#define STACK_HASH_SEED 0x9747b28c
77
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010078/* Hash table of pointers to stored stack traces. */
79static struct stack_record **stack_table;
80/* Fixed order of the number of table buckets. Used when KASAN is enabled. */
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +010081static unsigned int stack_bucket_number_order;
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010082/* Hash mask for indexing the table. */
Vlastimil Babkaf9987922022-06-20 17:02:49 +020083static unsigned int stack_hash_mask;
84
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010085/* Array of memory regions that store stack traces. */
Andrey Konovalov424cafe2023-02-10 22:15:58 +010086static void *stack_pools[DEPOT_MAX_POOLS];
Andrey Konovalov961c9492023-02-10 22:15:57 +010087/* Currently used pool in stack_pools. */
88static int pool_index;
89/* Offset to the unused space in the currently used pool. */
90static size_t pool_offset;
Andrey Konovalov0d249ac2023-02-10 22:15:55 +010091/* Lock that protects the variables above. */
Andrey Konovalov961c9492023-02-10 22:15:57 +010092static DEFINE_RAW_SPINLOCK(pool_lock);
Andrey Konovalovd11a5622023-02-10 22:16:02 +010093/*
94 * Stack depot tries to keep an extra pool allocated even before it runs out
95 * of space in the currently used pool.
96 * This flag marks that this next extra pool needs to be allocated and
97 * initialized. It has the value 0 when either the next pool is not yet
98 * initialized or the limit on the number of pools is reached.
99 */
100static int next_pool_required = 1;
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800101
Andrey Konovalov735df3c2023-02-10 22:15:52 +0100102static int __init disable_stack_depot(char *str)
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800103{
Vijayanand Jitta64427982021-02-25 17:21:31 -0800104 int ret;
105
Andrey Konovalov735df3c2023-02-10 22:15:52 +0100106 ret = kstrtobool(str, &stack_depot_disabled);
107 if (!ret && stack_depot_disabled) {
Andrey Konovalov4a6b5312023-02-10 22:15:50 +0100108 pr_info("disabled\n");
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800109 stack_table = NULL;
110 }
111 return 0;
112}
Andrey Konovalov735df3c2023-02-10 22:15:52 +0100113early_param("stack_depot_disable", disable_stack_depot);
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800114
Andrey Konovalov1c0310a2023-02-10 22:15:51 +0100115void __init stack_depot_request_early_init(void)
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100116{
Andrey Konovalov1c0310a2023-02-10 22:15:51 +0100117 /* Too late to request early init now. */
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100118 WARN_ON(__stack_depot_early_init_passed);
119
Andrey Konovalov1c0310a2023-02-10 22:15:51 +0100120 __stack_depot_early_init_requested = true;
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100121}
122
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100123/* Allocates a hash table via memblock. Can only be used during early boot. */
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100124int __init stack_depot_early_init(void)
125{
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200126 unsigned long entries = 0;
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100127
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100128 /* This function must be called only once, from mm_init(). */
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100129 if (WARN_ON(__stack_depot_early_init_passed))
130 return 0;
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100131 __stack_depot_early_init_passed = true;
132
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100133 /*
134 * If KASAN is enabled, use the maximum order: KASAN is frequently used
135 * in fuzzing scenarios, which leads to a large number of different
136 * stack traces being stored in stack depot.
137 */
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100138 if (kasan_enabled() && !stack_bucket_number_order)
139 stack_bucket_number_order = STACK_BUCKET_NUMBER_ORDER_MAX;
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200140
Andrey Konovalov735df3c2023-02-10 22:15:52 +0100141 if (!__stack_depot_early_init_requested || stack_depot_disabled)
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100142 return 0;
143
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100144 /*
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100145 * If stack_bucket_number_order is not set, leave entries as 0 to rely
146 * on the automatic calculations performed by alloc_large_system_hash.
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100147 */
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100148 if (stack_bucket_number_order)
149 entries = 1UL << stack_bucket_number_order;
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100150 pr_info("allocating hash table via alloc_large_system_hash\n");
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200151 stack_table = alloc_large_system_hash("stackdepot",
152 sizeof(struct stack_record *),
153 entries,
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100154 STACK_HASH_TABLE_SCALE,
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200155 HASH_EARLY | HASH_ZERO,
156 NULL,
157 &stack_hash_mask,
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100158 1UL << STACK_BUCKET_NUMBER_ORDER_MIN,
159 1UL << STACK_BUCKET_NUMBER_ORDER_MAX);
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100160 if (!stack_table) {
Andrey Konovalov4a6b5312023-02-10 22:15:50 +0100161 pr_err("hash table allocation failed, disabling\n");
Andrey Konovalov735df3c2023-02-10 22:15:52 +0100162 stack_depot_disabled = true;
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100163 return -ENOMEM;
164 }
165
166 return 0;
167}
168
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100169/* Allocates a hash table via kvcalloc. Can be used after boot. */
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100170int stack_depot_init(void)
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800171{
Vlastimil Babka2dba5eb2022-01-21 22:14:27 -0800172 static DEFINE_MUTEX(stack_depot_init_mutex);
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100173 unsigned long entries;
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100174 int ret = 0;
Vlastimil Babka2dba5eb2022-01-21 22:14:27 -0800175
176 mutex_lock(&stack_depot_init_mutex);
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200177
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100178 if (stack_depot_disabled || stack_table)
179 goto out_unlock;
Andrey Konovalovdf225c82023-02-10 22:15:53 +0100180
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100181 /*
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100182 * Similarly to stack_depot_early_init, use stack_bucket_number_order
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100183 * if assigned, and rely on automatic scaling otherwise.
184 */
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100185 if (stack_bucket_number_order) {
186 entries = 1UL << stack_bucket_number_order;
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100187 } else {
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100188 int scale = STACK_HASH_TABLE_SCALE;
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200189
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100190 entries = nr_free_buffer_pages();
191 entries = roundup_pow_of_two(entries);
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200192
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100193 if (scale > PAGE_SHIFT)
194 entries >>= (scale - PAGE_SHIFT);
195 else
196 entries <<= (PAGE_SHIFT - scale);
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800197 }
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100198
Andrey Konovalov4c2e9a62023-02-10 22:15:56 +0100199 if (entries < 1UL << STACK_BUCKET_NUMBER_ORDER_MIN)
200 entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MIN;
201 if (entries > 1UL << STACK_BUCKET_NUMBER_ORDER_MAX)
202 entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MAX;
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100203
204 pr_info("allocating hash table of %lu entries via kvcalloc\n", entries);
205 stack_table = kvcalloc(entries, sizeof(struct stack_record *), GFP_KERNEL);
206 if (!stack_table) {
207 pr_err("hash table allocation failed, disabling\n");
208 stack_depot_disabled = true;
209 ret = -ENOMEM;
210 goto out_unlock;
211 }
212 stack_hash_mask = entries - 1;
213
214out_unlock:
Vlastimil Babka2dba5eb2022-01-21 22:14:27 -0800215 mutex_unlock(&stack_depot_init_mutex);
Andrey Konovalovc60324f2023-02-10 22:15:54 +0100216
Vlastimil Babkaa5f17832022-03-02 12:02:22 +0100217 return ret;
Vijayanand Jittae1fdc402021-02-25 17:21:27 -0800218}
Vlastimil Babka2dba5eb2022-01-21 22:14:27 -0800219EXPORT_SYMBOL_GPL(stack_depot_init);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700220
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100221/* Uses preallocated memory to initialize a new stack depot pool. */
Andrey Konovalov514d5c52023-02-10 22:16:00 +0100222static void depot_init_pool(void **prealloc)
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100223{
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100224 /*
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100225 * If the next pool is already initialized or the maximum number of
226 * pools is reached, do not use the preallocated memory.
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100227 * smp_load_acquire() here pairs with smp_store_release() below and
228 * in depot_alloc_stack().
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100229 */
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100230 if (!smp_load_acquire(&next_pool_required))
Andrey Konovalov514d5c52023-02-10 22:16:00 +0100231 return;
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100232
233 /* Check if the current pool is not yet allocated. */
Andrey Konovalov961c9492023-02-10 22:15:57 +0100234 if (stack_pools[pool_index] == NULL) {
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100235 /* Use the preallocated memory for the current pool. */
Andrey Konovalov961c9492023-02-10 22:15:57 +0100236 stack_pools[pool_index] = *prealloc;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100237 *prealloc = NULL;
238 } else {
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100239 /*
240 * Otherwise, use the preallocated memory for the next pool
241 * as long as we do not exceed the maximum number of pools.
242 */
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100243 if (pool_index + 1 < DEPOT_MAX_POOLS) {
Andrey Konovalov961c9492023-02-10 22:15:57 +0100244 stack_pools[pool_index + 1] = *prealloc;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100245 *prealloc = NULL;
246 }
247 /*
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100248 * At this point, either the next pool is initialized or the
249 * maximum number of pools is reached. In either case, take
250 * note that initializing another pool is not required.
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100251 * This smp_store_release pairs with smp_load_acquire() above
252 * and in stack_depot_save().
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100253 */
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100254 smp_store_release(&next_pool_required, 0);
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100255 }
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100256}
257
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100258/* Allocates a new stack in a stack depot pool. */
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100259static struct stack_record *
260depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
261{
262 struct stack_record *stack;
263 size_t required_size = struct_size(stack, entries, size);
264
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100265 required_size = ALIGN(required_size, 1 << DEPOT_STACK_ALIGN);
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100266
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100267 /* Check if there is not enough space in the current pool. */
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100268 if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) {
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100269 /* Bail out if we reached the pool limit. */
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100270 if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) {
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100271 WARN_ONCE(1, "Stack depot reached limit capacity");
272 return NULL;
273 }
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100274
Andrey Konovalovbeb3c232023-02-10 22:16:04 +0100275 /*
276 * Move on to the next pool.
277 * WRITE_ONCE pairs with potential concurrent read in
278 * stack_depot_fetch().
279 */
280 WRITE_ONCE(pool_index, pool_index + 1);
Andrey Konovalov961c9492023-02-10 22:15:57 +0100281 pool_offset = 0;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100282 /*
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100283 * If the maximum number of pools is not reached, take note
284 * that the next pool needs to initialized.
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100285 * smp_store_release() here pairs with smp_load_acquire() in
286 * stack_depot_save() and depot_init_pool().
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100287 */
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100288 if (pool_index + 1 < DEPOT_MAX_POOLS)
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100289 smp_store_release(&next_pool_required, 1);
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100290 }
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100291
292 /* Assign the preallocated memory to a pool if required. */
Andrey Konovalov514d5c52023-02-10 22:16:00 +0100293 if (*prealloc)
294 depot_init_pool(prealloc);
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100295
296 /* Check if we have a pool to save the stack trace. */
Andrey Konovalov961c9492023-02-10 22:15:57 +0100297 if (stack_pools[pool_index] == NULL)
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100298 return NULL;
299
Andrey Konovalovcd0fc642023-02-10 22:16:01 +0100300 /* Save the stack trace. */
Andrey Konovalov961c9492023-02-10 22:15:57 +0100301 stack = stack_pools[pool_index] + pool_offset;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100302 stack->hash = hash;
303 stack->size = size;
Andrey Konovalov961c9492023-02-10 22:15:57 +0100304 stack->handle.pool_index = pool_index;
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100305 stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100306 stack->handle.valid = 1;
307 stack->handle.extra = 0;
308 memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
Andrey Konovalov961c9492023-02-10 22:15:57 +0100309 pool_offset += required_size;
Alexander Potapenko8e00b2d2023-03-06 12:13:21 +0100310 /*
311 * Let KMSAN know the stored stack record is initialized. This shall
312 * prevent false positive reports if instrumented code accesses it.
313 */
314 kmsan_unpoison_memory(stack, required_size);
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100315
316 return stack;
317}
318
Andrey Konovalovb232b992023-02-10 22:16:05 +0100319/* Calculates the hash for a stack. */
Alexander Potapenkocd110162016-03-25 14:22:08 -0700320static inline u32 hash_stack(unsigned long *entries, unsigned int size)
321{
322 return jhash2((u32 *)entries,
Gustavo A. R. Silva180644f2020-12-15 20:43:10 -0800323 array_size(size, sizeof(*entries)) / sizeof(u32),
324 STACK_HASH_SEED);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700325}
326
Andrey Konovalovb232b992023-02-10 22:16:05 +0100327/*
328 * Non-instrumented version of memcmp().
329 * Does not check the lexicographical order, only the equality.
Alexander Potapenkoa571b272018-02-06 15:38:24 -0800330 */
331static inline
332int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
333 unsigned int n)
334{
335 for ( ; n-- ; u1++, u2++) {
336 if (*u1 != *u2)
337 return 1;
338 }
339 return 0;
340}
341
Andrey Konovalovb232b992023-02-10 22:16:05 +0100342/* Finds a stack in a bucket of the hash table. */
Alexander Potapenkocd110162016-03-25 14:22:08 -0700343static inline struct stack_record *find_stack(struct stack_record *bucket,
344 unsigned long *entries, int size,
345 u32 hash)
346{
347 struct stack_record *found;
348
349 for (found = bucket; found; found = found->next) {
350 if (found->hash == hash &&
351 found->size == size &&
Alexander Potapenkoa571b272018-02-06 15:38:24 -0800352 !stackdepot_memcmp(entries, found->entries, size))
Alexander Potapenkocd110162016-03-25 14:22:08 -0700353 return found;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700354 }
355 return NULL;
356}
357
Marco Elver11ac25c62021-11-05 13:35:39 -0700358depot_stack_handle_t __stack_depot_save(unsigned long *entries,
359 unsigned int nr_entries,
360 gfp_t alloc_flags, bool can_alloc)
Alexander Potapenkocd110162016-03-25 14:22:08 -0700361{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700362 struct stack_record *found = NULL, **bucket;
Alexander Potapenko83a4f1e2022-09-15 17:03:36 +0200363 union handle_parts retval = { .handle = 0 };
Alexander Potapenkocd110162016-03-25 14:22:08 -0700364 struct page *page = NULL;
365 void *prealloc = NULL;
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200366 unsigned long flags;
367 u32 hash;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700368
Marco Elvere9400662022-01-21 22:14:31 -0800369 /*
370 * If this stack trace is from an interrupt, including anything before
Andrey Konovalovb232b992023-02-10 22:16:05 +0100371 * interrupt entry usually leads to unbounded stack depot growth.
Marco Elvere9400662022-01-21 22:14:31 -0800372 *
Andrey Konovalovb232b992023-02-10 22:16:05 +0100373 * Since use of filter_irq_stacks() is a requirement to ensure stack
374 * depot can efficiently deduplicate interrupt stacks, always
375 * filter_irq_stacks() to simplify all callers' use of stack depot.
Marco Elvere9400662022-01-21 22:14:31 -0800376 */
377 nr_entries = filter_irq_stacks(entries, nr_entries);
378
Andrey Konovalov735df3c2023-02-10 22:15:52 +0100379 if (unlikely(nr_entries == 0) || stack_depot_disabled)
Alexander Potapenkocd110162016-03-25 14:22:08 -0700380 goto fast_exit;
381
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200382 hash = hash_stack(entries, nr_entries);
Vlastimil Babkaf9987922022-06-20 17:02:49 +0200383 bucket = &stack_table[hash & stack_hash_mask];
Alexander Potapenkocd110162016-03-25 14:22:08 -0700384
385 /*
386 * Fast path: look the stack trace up without locking.
387 * The smp_load_acquire() here pairs with smp_store_release() to
388 * |bucket| below.
389 */
Andrey Konovalovb232b992023-02-10 22:16:05 +0100390 found = find_stack(smp_load_acquire(bucket), entries, nr_entries, hash);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700391 if (found)
392 goto exit;
393
394 /*
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100395 * Check if another stack pool needs to be initialized. If so, allocate
396 * the memory now - we won't be able to do that under the lock.
Alexander Potapenkocd110162016-03-25 14:22:08 -0700397 *
398 * The smp_load_acquire() here pairs with smp_store_release() to
Andrey Konovalovcb788e82023-02-10 22:15:59 +0100399 * |next_pool_inited| in depot_alloc_stack() and depot_init_pool().
Alexander Potapenkocd110162016-03-25 14:22:08 -0700400 */
Andrey Konovalovd11a5622023-02-10 22:16:02 +0100401 if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) {
Alexander Potapenkocd110162016-03-25 14:22:08 -0700402 /*
403 * Zero out zone modifiers, as we don't have specific zone
404 * requirements. Keep the flags related to allocation in atomic
405 * contexts and I/O.
406 */
407 alloc_flags &= ~GFP_ZONEMASK;
408 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
Kirill A. Shutemov87cc2712016-07-28 15:49:10 -0700409 alloc_flags |= __GFP_NOWARN;
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100410 page = alloc_pages(alloc_flags, DEPOT_POOL_ORDER);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700411 if (page)
412 prealloc = page_address(page);
413 }
414
Andrey Konovalov961c9492023-02-10 22:15:57 +0100415 raw_spin_lock_irqsave(&pool_lock, flags);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700416
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200417 found = find_stack(*bucket, entries, nr_entries, hash);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700418 if (!found) {
Andrey Konovalovb232b992023-02-10 22:16:05 +0100419 struct stack_record *new =
420 depot_alloc_stack(entries, nr_entries, hash, &prealloc);
Marco Elver7f2b8812021-11-05 13:35:36 -0700421
Alexander Potapenkocd110162016-03-25 14:22:08 -0700422 if (new) {
423 new->next = *bucket;
424 /*
425 * This smp_store_release() pairs with
426 * smp_load_acquire() from |bucket| above.
427 */
428 smp_store_release(bucket, new);
429 found = new;
430 }
431 } else if (prealloc) {
432 /*
Andrey Konovalovb232b992023-02-10 22:16:05 +0100433 * Stack depot already contains this stack trace, but let's
434 * keep the preallocated memory for the future.
Alexander Potapenkocd110162016-03-25 14:22:08 -0700435 */
Andrey Konovalov514d5c52023-02-10 22:16:00 +0100436 depot_init_pool(&prealloc);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700437 }
438
Andrey Konovalov961c9492023-02-10 22:15:57 +0100439 raw_spin_unlock_irqrestore(&pool_lock, flags);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700440exit:
441 if (prealloc) {
Andrey Konovalovb232b992023-02-10 22:16:05 +0100442 /* Stack depot didn't use this memory, free it. */
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100443 free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700444 }
445 if (found)
Alexander Potapenko83a4f1e2022-09-15 17:03:36 +0200446 retval.handle = found->handle.handle;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700447fast_exit:
Alexander Potapenko83a4f1e2022-09-15 17:03:36 +0200448 return retval.handle;
Alexander Potapenkocd110162016-03-25 14:22:08 -0700449}
Marco Elver11ac25c62021-11-05 13:35:39 -0700450EXPORT_SYMBOL_GPL(__stack_depot_save);
451
Marco Elver11ac25c62021-11-05 13:35:39 -0700452depot_stack_handle_t stack_depot_save(unsigned long *entries,
453 unsigned int nr_entries,
454 gfp_t alloc_flags)
455{
Andrey Konovalov36aa1e62023-02-10 22:16:03 +0100456 return __stack_depot_save(entries, nr_entries, alloc_flags, true);
Marco Elver11ac25c62021-11-05 13:35:39 -0700457}
Thomas Gleixnerc0cfc3372019-04-25 11:44:56 +0200458EXPORT_SYMBOL_GPL(stack_depot_save);
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100459
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100460unsigned int stack_depot_fetch(depot_stack_handle_t handle,
461 unsigned long **entries)
462{
463 union handle_parts parts = { .handle = handle };
Andrey Konovalovbeb3c232023-02-10 22:16:04 +0100464 /*
465 * READ_ONCE pairs with potential concurrent write in
466 * depot_alloc_stack.
467 */
468 int pool_index_cached = READ_ONCE(pool_index);
Andrey Konovalov961c9492023-02-10 22:15:57 +0100469 void *pool;
Andrey Konovalov424cafe2023-02-10 22:15:58 +0100470 size_t offset = parts.offset << DEPOT_STACK_ALIGN;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100471 struct stack_record *stack;
472
473 *entries = NULL;
Alexander Potapenko8e00b2d2023-03-06 12:13:21 +0100474 /*
475 * Let KMSAN know *entries is initialized. This shall prevent false
476 * positive reports if instrumented code accesses it.
477 */
478 kmsan_unpoison_memory(entries, sizeof(*entries));
479
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100480 if (!handle)
481 return 0;
482
Andrey Konovalovbeb3c232023-02-10 22:16:04 +0100483 if (parts.pool_index > pool_index_cached) {
Andrey Konovalov961c9492023-02-10 22:15:57 +0100484 WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
Andrey Konovalovbeb3c232023-02-10 22:16:04 +0100485 parts.pool_index, pool_index_cached, handle);
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100486 return 0;
487 }
Andrey Konovalov961c9492023-02-10 22:15:57 +0100488 pool = stack_pools[parts.pool_index];
489 if (!pool)
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100490 return 0;
Andrey Konovalov961c9492023-02-10 22:15:57 +0100491 stack = pool + offset;
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100492
493 *entries = stack->entries;
494 return stack->size;
495}
496EXPORT_SYMBOL_GPL(stack_depot_fetch);
497
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100498void stack_depot_print(depot_stack_handle_t stack)
499{
500 unsigned long *entries;
501 unsigned int nr_entries;
502
503 nr_entries = stack_depot_fetch(stack, &entries);
504 if (nr_entries > 0)
505 stack_trace_print(entries, nr_entries, 0);
506}
507EXPORT_SYMBOL_GPL(stack_depot_print);
508
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100509int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
510 int spaces)
511{
512 unsigned long *entries;
513 unsigned int nr_entries;
514
515 nr_entries = stack_depot_fetch(handle, &entries);
516 return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries,
517 spaces) : 0;
518}
519EXPORT_SYMBOL_GPL(stack_depot_snprint);
520
Andrey Konovalov36aa1e62023-02-10 22:16:03 +0100521depot_stack_handle_t __must_check stack_depot_set_extra_bits(
522 depot_stack_handle_t handle, unsigned int extra_bits)
523{
524 union handle_parts parts = { .handle = handle };
525
526 /* Don't set extra bits on empty handles. */
527 if (!handle)
528 return 0;
529
530 parts.extra = extra_bits;
531 return parts.handle;
532}
533EXPORT_SYMBOL(stack_depot_set_extra_bits);
534
Andrey Konovalov15ef6a92023-02-10 22:15:49 +0100535unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle)
536{
537 union handle_parts parts = { .handle = handle };
538
539 return parts.extra;
540}
541EXPORT_SYMBOL(stack_depot_get_extra_bits);