11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cd11016eSAlexander Potapenko /*
3b232b999SAndrey Konovalov * Stack depot - a stack trace storage that avoids duplication.
4cd11016eSAlexander Potapenko *
5b232b999SAndrey Konovalov * Internally, stack depot maintains a hash table of unique stacktraces. The
6b232b999SAndrey Konovalov * stack traces themselves are stored contiguously one after another in a set
7b232b999SAndrey Konovalov * of separate page allocations.
8b232b999SAndrey Konovalov *
9cd11016eSAlexander Potapenko * Author: Alexander Potapenko <glider@google.com>
10cd11016eSAlexander Potapenko * Copyright (C) 2016 Google, Inc.
11cd11016eSAlexander Potapenko *
12b232b999SAndrey Konovalov * Based on the code by Dmitry Chernenkov.
13cd11016eSAlexander Potapenko */
14cd11016eSAlexander Potapenko
154a6b5314SAndrey Konovalov #define pr_fmt(fmt) "stackdepot: " fmt
164a6b5314SAndrey Konovalov
17cd11016eSAlexander Potapenko #include <linux/gfp.h>
18cd11016eSAlexander Potapenko #include <linux/jhash.h>
19cd11016eSAlexander Potapenko #include <linux/kernel.h>
208e00b2dfSAlexander Potapenko #include <linux/kmsan.h>
21cd11016eSAlexander Potapenko #include <linux/mm.h>
222dba5eb1SVlastimil Babka #include <linux/mutex.h>
23cd11016eSAlexander Potapenko #include <linux/percpu.h>
24cd11016eSAlexander Potapenko #include <linux/printk.h>
25cd11016eSAlexander Potapenko #include <linux/slab.h>
26cd11016eSAlexander Potapenko #include <linux/stacktrace.h>
27cd11016eSAlexander Potapenko #include <linux/stackdepot.h>
28cd11016eSAlexander Potapenko #include <linux/string.h>
29cd11016eSAlexander Potapenko #include <linux/types.h>
30e1fdc403SVijayanand Jitta #include <linux/memblock.h>
31f9987921SVlastimil Babka #include <linux/kasan-enabled.h>
32cd11016eSAlexander Potapenko
33424cafeeSAndrey Konovalov #define DEPOT_HANDLE_BITS (sizeof(depot_stack_handle_t) * 8)
34cd11016eSAlexander Potapenko
35424cafeeSAndrey Konovalov #define DEPOT_VALID_BITS 1
36424cafeeSAndrey Konovalov #define DEPOT_POOL_ORDER 2 /* Pool size order, 4 pages */
37424cafeeSAndrey Konovalov #define DEPOT_POOL_SIZE (1LL << (PAGE_SHIFT + DEPOT_POOL_ORDER))
38424cafeeSAndrey Konovalov #define DEPOT_STACK_ALIGN 4
39424cafeeSAndrey Konovalov #define DEPOT_OFFSET_BITS (DEPOT_POOL_ORDER + PAGE_SHIFT - DEPOT_STACK_ALIGN)
40424cafeeSAndrey Konovalov #define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_VALID_BITS - \
41424cafeeSAndrey Konovalov DEPOT_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS)
42424cafeeSAndrey Konovalov #define DEPOT_POOLS_CAP 8192
43424cafeeSAndrey Konovalov #define DEPOT_MAX_POOLS \
44424cafeeSAndrey Konovalov (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \
45424cafeeSAndrey Konovalov (1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP)
46cd11016eSAlexander Potapenko
47b232b999SAndrey Konovalov /* Compact structure that stores a reference to a stack. */
48cd11016eSAlexander Potapenko union handle_parts {
49cd11016eSAlexander Potapenko depot_stack_handle_t handle;
50cd11016eSAlexander Potapenko struct {
51424cafeeSAndrey Konovalov u32 pool_index : DEPOT_POOL_INDEX_BITS;
52424cafeeSAndrey Konovalov u32 offset : DEPOT_OFFSET_BITS;
53424cafeeSAndrey Konovalov u32 valid : DEPOT_VALID_BITS;
5483a4f1efSAlexander Potapenko u32 extra : STACK_DEPOT_EXTRA_BITS;
55cd11016eSAlexander Potapenko };
56cd11016eSAlexander Potapenko };
57cd11016eSAlexander Potapenko
58cd11016eSAlexander Potapenko struct stack_record {
59cd11016eSAlexander Potapenko struct stack_record *next; /* Link in the hash table */
60b232b999SAndrey Konovalov u32 hash; /* Hash in the hash table */
61b232b999SAndrey Konovalov u32 size; /* Number of stored frames */
62cd11016eSAlexander Potapenko union handle_parts handle;
63b232b999SAndrey Konovalov unsigned long entries[]; /* Variable-sized array of frames */
64cd11016eSAlexander Potapenko };
65cd11016eSAlexander Potapenko
66735df3c3SAndrey Konovalov static bool stack_depot_disabled;
671c0310adSAndrey Konovalov static bool __stack_depot_early_init_requested __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT);
68a5f1783bSVlastimil Babka static bool __stack_depot_early_init_passed __initdata;
69a5f1783bSVlastimil Babka
700d249ac0SAndrey Konovalov /* Use one hash table bucket per 16 KB of memory. */
714c2e9a67SAndrey Konovalov #define STACK_HASH_TABLE_SCALE 14
720d249ac0SAndrey Konovalov /* Limit the number of buckets between 4K and 1M. */
734c2e9a67SAndrey Konovalov #define STACK_BUCKET_NUMBER_ORDER_MIN 12
744c2e9a67SAndrey Konovalov #define STACK_BUCKET_NUMBER_ORDER_MAX 20
750d249ac0SAndrey Konovalov /* Initial seed for jhash2. */
76cd11016eSAlexander Potapenko #define STACK_HASH_SEED 0x9747b28c
77cd11016eSAlexander Potapenko
780d249ac0SAndrey Konovalov /* Hash table of pointers to stored stack traces. */
790d249ac0SAndrey Konovalov static struct stack_record **stack_table;
800d249ac0SAndrey Konovalov /* Fixed order of the number of table buckets. Used when KASAN is enabled. */
814c2e9a67SAndrey Konovalov static unsigned int stack_bucket_number_order;
820d249ac0SAndrey Konovalov /* Hash mask for indexing the table. */
83f9987921SVlastimil Babka static unsigned int stack_hash_mask;
84f9987921SVlastimil Babka
850d249ac0SAndrey Konovalov /* Array of memory regions that store stack traces. */
86424cafeeSAndrey Konovalov static void *stack_pools[DEPOT_MAX_POOLS];
87961c949bSAndrey Konovalov /* Currently used pool in stack_pools. */
88961c949bSAndrey Konovalov static int pool_index;
89961c949bSAndrey Konovalov /* Offset to the unused space in the currently used pool. */
90961c949bSAndrey Konovalov static size_t pool_offset;
910d249ac0SAndrey Konovalov /* Lock that protects the variables above. */
92961c949bSAndrey Konovalov static DEFINE_RAW_SPINLOCK(pool_lock);
93d11a5621SAndrey Konovalov /*
94d11a5621SAndrey Konovalov * Stack depot tries to keep an extra pool allocated even before it runs out
95d11a5621SAndrey Konovalov * of space in the currently used pool.
96d11a5621SAndrey Konovalov * This flag marks that this next extra pool needs to be allocated and
97d11a5621SAndrey Konovalov * initialized. It has the value 0 when either the next pool is not yet
98d11a5621SAndrey Konovalov * initialized or the limit on the number of pools is reached.
99d11a5621SAndrey Konovalov */
100d11a5621SAndrey Konovalov static int next_pool_required = 1;
101e1fdc403SVijayanand Jitta
disable_stack_depot(char * str)102735df3c3SAndrey Konovalov static int __init disable_stack_depot(char *str)
103e1fdc403SVijayanand Jitta {
10464427985SVijayanand Jitta int ret;
10564427985SVijayanand Jitta
106735df3c3SAndrey Konovalov ret = kstrtobool(str, &stack_depot_disabled);
107735df3c3SAndrey Konovalov if (!ret && stack_depot_disabled) {
1084a6b5314SAndrey Konovalov pr_info("disabled\n");
109e1fdc403SVijayanand Jitta stack_table = NULL;
110e1fdc403SVijayanand Jitta }
111e1fdc403SVijayanand Jitta return 0;
112e1fdc403SVijayanand Jitta }
113735df3c3SAndrey Konovalov early_param("stack_depot_disable", disable_stack_depot);
114e1fdc403SVijayanand Jitta
stack_depot_request_early_init(void)1151c0310adSAndrey Konovalov void __init stack_depot_request_early_init(void)
116a5f1783bSVlastimil Babka {
1171c0310adSAndrey Konovalov /* Too late to request early init now. */
118a5f1783bSVlastimil Babka WARN_ON(__stack_depot_early_init_passed);
119a5f1783bSVlastimil Babka
1201c0310adSAndrey Konovalov __stack_depot_early_init_requested = true;
121a5f1783bSVlastimil Babka }
122a5f1783bSVlastimil Babka
123df225c87SAndrey Konovalov /* Allocates a hash table via memblock. Can only be used during early boot. */
stack_depot_early_init(void)124a5f1783bSVlastimil Babka int __init stack_depot_early_init(void)
125a5f1783bSVlastimil Babka {
126f9987921SVlastimil Babka unsigned long entries = 0;
127a5f1783bSVlastimil Babka
128df225c87SAndrey Konovalov /* This function must be called only once, from mm_init(). */
129a5f1783bSVlastimil Babka if (WARN_ON(__stack_depot_early_init_passed))
130a5f1783bSVlastimil Babka return 0;
131a5f1783bSVlastimil Babka __stack_depot_early_init_passed = true;
132a5f1783bSVlastimil Babka
133df225c87SAndrey Konovalov /*
134df225c87SAndrey Konovalov * If KASAN is enabled, use the maximum order: KASAN is frequently used
135df225c87SAndrey Konovalov * in fuzzing scenarios, which leads to a large number of different
136df225c87SAndrey Konovalov * stack traces being stored in stack depot.
137df225c87SAndrey Konovalov */
1384c2e9a67SAndrey Konovalov if (kasan_enabled() && !stack_bucket_number_order)
1394c2e9a67SAndrey Konovalov stack_bucket_number_order = STACK_BUCKET_NUMBER_ORDER_MAX;
140f9987921SVlastimil Babka
141735df3c3SAndrey Konovalov if (!__stack_depot_early_init_requested || stack_depot_disabled)
142a5f1783bSVlastimil Babka return 0;
143a5f1783bSVlastimil Babka
144df225c87SAndrey Konovalov /*
1454c2e9a67SAndrey Konovalov * If stack_bucket_number_order is not set, leave entries as 0 to rely
1464c2e9a67SAndrey Konovalov * on the automatic calculations performed by alloc_large_system_hash.
147df225c87SAndrey Konovalov */
1484c2e9a67SAndrey Konovalov if (stack_bucket_number_order)
1494c2e9a67SAndrey Konovalov entries = 1UL << stack_bucket_number_order;
150df225c87SAndrey Konovalov pr_info("allocating hash table via alloc_large_system_hash\n");
151f9987921SVlastimil Babka stack_table = alloc_large_system_hash("stackdepot",
152f9987921SVlastimil Babka sizeof(struct stack_record *),
153f9987921SVlastimil Babka entries,
1544c2e9a67SAndrey Konovalov STACK_HASH_TABLE_SCALE,
155f9987921SVlastimil Babka HASH_EARLY | HASH_ZERO,
156f9987921SVlastimil Babka NULL,
157f9987921SVlastimil Babka &stack_hash_mask,
1584c2e9a67SAndrey Konovalov 1UL << STACK_BUCKET_NUMBER_ORDER_MIN,
1594c2e9a67SAndrey Konovalov 1UL << STACK_BUCKET_NUMBER_ORDER_MAX);
160a5f1783bSVlastimil Babka if (!stack_table) {
1614a6b5314SAndrey Konovalov pr_err("hash table allocation failed, disabling\n");
162735df3c3SAndrey Konovalov stack_depot_disabled = true;
163a5f1783bSVlastimil Babka return -ENOMEM;
164a5f1783bSVlastimil Babka }
165a5f1783bSVlastimil Babka
166a5f1783bSVlastimil Babka return 0;
167a5f1783bSVlastimil Babka }
168a5f1783bSVlastimil Babka
169df225c87SAndrey Konovalov /* Allocates a hash table via kvcalloc. Can be used after boot. */
stack_depot_init(void)170a5f1783bSVlastimil Babka int stack_depot_init(void)
171e1fdc403SVijayanand Jitta {
1722dba5eb1SVlastimil Babka static DEFINE_MUTEX(stack_depot_init_mutex);
173c60324fbSAndrey Konovalov unsigned long entries;
174a5f1783bSVlastimil Babka int ret = 0;
1752dba5eb1SVlastimil Babka
1762dba5eb1SVlastimil Babka mutex_lock(&stack_depot_init_mutex);
177c60324fbSAndrey Konovalov
178c60324fbSAndrey Konovalov if (stack_depot_disabled || stack_table)
179c60324fbSAndrey Konovalov goto out_unlock;
180f9987921SVlastimil Babka
181df225c87SAndrey Konovalov /*
1824c2e9a67SAndrey Konovalov * Similarly to stack_depot_early_init, use stack_bucket_number_order
183df225c87SAndrey Konovalov * if assigned, and rely on automatic scaling otherwise.
184df225c87SAndrey Konovalov */
1854c2e9a67SAndrey Konovalov if (stack_bucket_number_order) {
1864c2e9a67SAndrey Konovalov entries = 1UL << stack_bucket_number_order;
187f9987921SVlastimil Babka } else {
1884c2e9a67SAndrey Konovalov int scale = STACK_HASH_TABLE_SCALE;
189df225c87SAndrey Konovalov
190f9987921SVlastimil Babka entries = nr_free_buffer_pages();
191f9987921SVlastimil Babka entries = roundup_pow_of_two(entries);
192f9987921SVlastimil Babka
193f9987921SVlastimil Babka if (scale > PAGE_SHIFT)
194f9987921SVlastimil Babka entries >>= (scale - PAGE_SHIFT);
195f9987921SVlastimil Babka else
196f9987921SVlastimil Babka entries <<= (PAGE_SHIFT - scale);
197f9987921SVlastimil Babka }
198f9987921SVlastimil Babka
1994c2e9a67SAndrey Konovalov if (entries < 1UL << STACK_BUCKET_NUMBER_ORDER_MIN)
2004c2e9a67SAndrey Konovalov entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MIN;
2014c2e9a67SAndrey Konovalov if (entries > 1UL << STACK_BUCKET_NUMBER_ORDER_MAX)
2024c2e9a67SAndrey Konovalov entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MAX;
203f9987921SVlastimil Babka
204c60324fbSAndrey Konovalov pr_info("allocating hash table of %lu entries via kvcalloc\n", entries);
205f9987921SVlastimil Babka stack_table = kvcalloc(entries, sizeof(struct stack_record *), GFP_KERNEL);
206a5f1783bSVlastimil Babka if (!stack_table) {
2074a6b5314SAndrey Konovalov pr_err("hash table allocation failed, disabling\n");
208735df3c3SAndrey Konovalov stack_depot_disabled = true;
209a5f1783bSVlastimil Babka ret = -ENOMEM;
210c60324fbSAndrey Konovalov goto out_unlock;
211e1fdc403SVijayanand Jitta }
212f9987921SVlastimil Babka stack_hash_mask = entries - 1;
213c60324fbSAndrey Konovalov
214c60324fbSAndrey Konovalov out_unlock:
2152dba5eb1SVlastimil Babka mutex_unlock(&stack_depot_init_mutex);
216c60324fbSAndrey Konovalov
217a5f1783bSVlastimil Babka return ret;
218e1fdc403SVijayanand Jitta }
2192dba5eb1SVlastimil Babka EXPORT_SYMBOL_GPL(stack_depot_init);
220cd11016eSAlexander Potapenko
221cd0fc64eSAndrey Konovalov /* Uses preallocated memory to initialize a new stack depot pool. */
depot_init_pool(void ** prealloc)222514d5c55SAndrey Konovalov static void depot_init_pool(void **prealloc)
22315ef6a98SAndrey Konovalov {
22415ef6a98SAndrey Konovalov /*
225d11a5621SAndrey Konovalov * If the next pool is already initialized or the maximum number of
226d11a5621SAndrey Konovalov * pools is reached, do not use the preallocated memory.
227cd0fc64eSAndrey Konovalov * smp_load_acquire() here pairs with smp_store_release() below and
228cd0fc64eSAndrey Konovalov * in depot_alloc_stack().
22915ef6a98SAndrey Konovalov */
230d11a5621SAndrey Konovalov if (!smp_load_acquire(&next_pool_required))
231514d5c55SAndrey Konovalov return;
232cd0fc64eSAndrey Konovalov
233cd0fc64eSAndrey Konovalov /* Check if the current pool is not yet allocated. */
234961c949bSAndrey Konovalov if (stack_pools[pool_index] == NULL) {
235cd0fc64eSAndrey Konovalov /* Use the preallocated memory for the current pool. */
236961c949bSAndrey Konovalov stack_pools[pool_index] = *prealloc;
23715ef6a98SAndrey Konovalov *prealloc = NULL;
23815ef6a98SAndrey Konovalov } else {
239cd0fc64eSAndrey Konovalov /*
240cd0fc64eSAndrey Konovalov * Otherwise, use the preallocated memory for the next pool
241cd0fc64eSAndrey Konovalov * as long as we do not exceed the maximum number of pools.
242cd0fc64eSAndrey Konovalov */
243424cafeeSAndrey Konovalov if (pool_index + 1 < DEPOT_MAX_POOLS) {
244961c949bSAndrey Konovalov stack_pools[pool_index + 1] = *prealloc;
24515ef6a98SAndrey Konovalov *prealloc = NULL;
24615ef6a98SAndrey Konovalov }
24715ef6a98SAndrey Konovalov /*
248d11a5621SAndrey Konovalov * At this point, either the next pool is initialized or the
249d11a5621SAndrey Konovalov * maximum number of pools is reached. In either case, take
250d11a5621SAndrey Konovalov * note that initializing another pool is not required.
251cd0fc64eSAndrey Konovalov * This smp_store_release pairs with smp_load_acquire() above
252cd0fc64eSAndrey Konovalov * and in stack_depot_save().
25315ef6a98SAndrey Konovalov */
254d11a5621SAndrey Konovalov smp_store_release(&next_pool_required, 0);
25515ef6a98SAndrey Konovalov }
25615ef6a98SAndrey Konovalov }
25715ef6a98SAndrey Konovalov
258cd0fc64eSAndrey Konovalov /* Allocates a new stack in a stack depot pool. */
25915ef6a98SAndrey Konovalov static struct stack_record *
depot_alloc_stack(unsigned long * entries,int size,u32 hash,void ** prealloc)26015ef6a98SAndrey Konovalov depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
26115ef6a98SAndrey Konovalov {
26215ef6a98SAndrey Konovalov struct stack_record *stack;
26315ef6a98SAndrey Konovalov size_t required_size = struct_size(stack, entries, size);
26415ef6a98SAndrey Konovalov
265424cafeeSAndrey Konovalov required_size = ALIGN(required_size, 1 << DEPOT_STACK_ALIGN);
26615ef6a98SAndrey Konovalov
267cd0fc64eSAndrey Konovalov /* Check if there is not enough space in the current pool. */
268424cafeeSAndrey Konovalov if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) {
269cd0fc64eSAndrey Konovalov /* Bail out if we reached the pool limit. */
270424cafeeSAndrey Konovalov if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) {
27115ef6a98SAndrey Konovalov WARN_ONCE(1, "Stack depot reached limit capacity");
27215ef6a98SAndrey Konovalov return NULL;
27315ef6a98SAndrey Konovalov }
274cd0fc64eSAndrey Konovalov
275beb3c23cSAndrey Konovalov /*
276beb3c23cSAndrey Konovalov * Move on to the next pool.
277beb3c23cSAndrey Konovalov * WRITE_ONCE pairs with potential concurrent read in
278beb3c23cSAndrey Konovalov * stack_depot_fetch().
279beb3c23cSAndrey Konovalov */
280beb3c23cSAndrey Konovalov WRITE_ONCE(pool_index, pool_index + 1);
281961c949bSAndrey Konovalov pool_offset = 0;
28215ef6a98SAndrey Konovalov /*
283d11a5621SAndrey Konovalov * If the maximum number of pools is not reached, take note
284d11a5621SAndrey Konovalov * that the next pool needs to initialized.
285cd0fc64eSAndrey Konovalov * smp_store_release() here pairs with smp_load_acquire() in
286cd0fc64eSAndrey Konovalov * stack_depot_save() and depot_init_pool().
28715ef6a98SAndrey Konovalov */
288424cafeeSAndrey Konovalov if (pool_index + 1 < DEPOT_MAX_POOLS)
289d11a5621SAndrey Konovalov smp_store_release(&next_pool_required, 1);
29015ef6a98SAndrey Konovalov }
291cd0fc64eSAndrey Konovalov
292cd0fc64eSAndrey Konovalov /* Assign the preallocated memory to a pool if required. */
293514d5c55SAndrey Konovalov if (*prealloc)
294cb788e84SAndrey Konovalov depot_init_pool(prealloc);
295cd0fc64eSAndrey Konovalov
296cd0fc64eSAndrey Konovalov /* Check if we have a pool to save the stack trace. */
297961c949bSAndrey Konovalov if (stack_pools[pool_index] == NULL)
29815ef6a98SAndrey Konovalov return NULL;
29915ef6a98SAndrey Konovalov
300cd0fc64eSAndrey Konovalov /* Save the stack trace. */
301961c949bSAndrey Konovalov stack = stack_pools[pool_index] + pool_offset;
30215ef6a98SAndrey Konovalov stack->hash = hash;
30315ef6a98SAndrey Konovalov stack->size = size;
304961c949bSAndrey Konovalov stack->handle.pool_index = pool_index;
305424cafeeSAndrey Konovalov stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN;
30615ef6a98SAndrey Konovalov stack->handle.valid = 1;
30715ef6a98SAndrey Konovalov stack->handle.extra = 0;
30815ef6a98SAndrey Konovalov memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
309961c949bSAndrey Konovalov pool_offset += required_size;
3108e00b2dfSAlexander Potapenko /*
3118e00b2dfSAlexander Potapenko * Let KMSAN know the stored stack record is initialized. This shall
3128e00b2dfSAlexander Potapenko * prevent false positive reports if instrumented code accesses it.
3138e00b2dfSAlexander Potapenko */
3148e00b2dfSAlexander Potapenko kmsan_unpoison_memory(stack, required_size);
31515ef6a98SAndrey Konovalov
31615ef6a98SAndrey Konovalov return stack;
31715ef6a98SAndrey Konovalov }
31815ef6a98SAndrey Konovalov
319b232b999SAndrey Konovalov /* Calculates the hash for a stack. */
hash_stack(unsigned long * entries,unsigned int size)320cd11016eSAlexander Potapenko static inline u32 hash_stack(unsigned long *entries, unsigned int size)
321cd11016eSAlexander Potapenko {
322cd11016eSAlexander Potapenko return jhash2((u32 *)entries,
323180644f8SGustavo A. R. Silva array_size(size, sizeof(*entries)) / sizeof(u32),
324cd11016eSAlexander Potapenko STACK_HASH_SEED);
325cd11016eSAlexander Potapenko }
326cd11016eSAlexander Potapenko
327b232b999SAndrey Konovalov /*
328b232b999SAndrey Konovalov * Non-instrumented version of memcmp().
329b232b999SAndrey Konovalov * Does not check the lexicographical order, only the equality.
330a571b272SAlexander Potapenko */
331a571b272SAlexander Potapenko static inline
stackdepot_memcmp(const unsigned long * u1,const unsigned long * u2,unsigned int n)332a571b272SAlexander Potapenko int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
333a571b272SAlexander Potapenko unsigned int n)
334a571b272SAlexander Potapenko {
335a571b272SAlexander Potapenko for ( ; n-- ; u1++, u2++) {
336a571b272SAlexander Potapenko if (*u1 != *u2)
337a571b272SAlexander Potapenko return 1;
338a571b272SAlexander Potapenko }
339a571b272SAlexander Potapenko return 0;
340a571b272SAlexander Potapenko }
341a571b272SAlexander Potapenko
342b232b999SAndrey Konovalov /* Finds a stack in a bucket of the hash table. */
find_stack(struct stack_record * bucket,unsigned long * entries,int size,u32 hash)343cd11016eSAlexander Potapenko static inline struct stack_record *find_stack(struct stack_record *bucket,
344cd11016eSAlexander Potapenko unsigned long *entries, int size,
345cd11016eSAlexander Potapenko u32 hash)
346cd11016eSAlexander Potapenko {
347cd11016eSAlexander Potapenko struct stack_record *found;
348cd11016eSAlexander Potapenko
349cd11016eSAlexander Potapenko for (found = bucket; found; found = found->next) {
350cd11016eSAlexander Potapenko if (found->hash == hash &&
351cd11016eSAlexander Potapenko found->size == size &&
352a571b272SAlexander Potapenko !stackdepot_memcmp(entries, found->entries, size))
353cd11016eSAlexander Potapenko return found;
354cd11016eSAlexander Potapenko }
355cd11016eSAlexander Potapenko return NULL;
356cd11016eSAlexander Potapenko }
357cd11016eSAlexander Potapenko
__stack_depot_save(unsigned long * entries,unsigned int nr_entries,gfp_t alloc_flags,bool can_alloc)35811ac25c6SMarco Elver depot_stack_handle_t __stack_depot_save(unsigned long *entries,
359c0cfc337SThomas Gleixner unsigned int nr_entries,
36011ac25c6SMarco Elver gfp_t alloc_flags, bool can_alloc)
361cd11016eSAlexander Potapenko {
362cd11016eSAlexander Potapenko struct stack_record *found = NULL, **bucket;
36383a4f1efSAlexander Potapenko union handle_parts retval = { .handle = 0 };
364cd11016eSAlexander Potapenko struct page *page = NULL;
365cd11016eSAlexander Potapenko void *prealloc = NULL;
366c0cfc337SThomas Gleixner unsigned long flags;
367c0cfc337SThomas Gleixner u32 hash;
368cd11016eSAlexander Potapenko
369e9400660SMarco Elver /*
370e9400660SMarco Elver * If this stack trace is from an interrupt, including anything before
371e9400660SMarco Elver * interrupt entry usually leads to unbounded stack depot growth.
372e9400660SMarco Elver *
373b232b999SAndrey Konovalov * Since use of filter_irq_stacks() is a requirement to ensure stack
374b232b999SAndrey Konovalov * depot can efficiently deduplicate interrupt stacks, always
375e9400660SMarco Elver * filter_irq_stacks() to simplify all callers' use of stack depot.
376e9400660SMarco Elver */
377e9400660SMarco Elver nr_entries = filter_irq_stacks(entries, nr_entries);
378e9400660SMarco Elver
379735df3c3SAndrey Konovalov if (unlikely(nr_entries == 0) || stack_depot_disabled)
380cd11016eSAlexander Potapenko goto fast_exit;
381cd11016eSAlexander Potapenko
382c0cfc337SThomas Gleixner hash = hash_stack(entries, nr_entries);
383f9987921SVlastimil Babka bucket = &stack_table[hash & stack_hash_mask];
384cd11016eSAlexander Potapenko
385cd11016eSAlexander Potapenko /*
386cd11016eSAlexander Potapenko * Fast path: look the stack trace up without locking.
387cd11016eSAlexander Potapenko * The smp_load_acquire() here pairs with smp_store_release() to
388cd11016eSAlexander Potapenko * |bucket| below.
389cd11016eSAlexander Potapenko */
390b232b999SAndrey Konovalov found = find_stack(smp_load_acquire(bucket), entries, nr_entries, hash);
391cd11016eSAlexander Potapenko if (found)
392cd11016eSAlexander Potapenko goto exit;
393cd11016eSAlexander Potapenko
394cd11016eSAlexander Potapenko /*
395d11a5621SAndrey Konovalov * Check if another stack pool needs to be initialized. If so, allocate
396d11a5621SAndrey Konovalov * the memory now - we won't be able to do that under the lock.
397cd11016eSAlexander Potapenko *
398cd11016eSAlexander Potapenko * The smp_load_acquire() here pairs with smp_store_release() to
399cb788e84SAndrey Konovalov * |next_pool_inited| in depot_alloc_stack() and depot_init_pool().
400cd11016eSAlexander Potapenko */
401d11a5621SAndrey Konovalov if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) {
402cd11016eSAlexander Potapenko /*
403cd11016eSAlexander Potapenko * Zero out zone modifiers, as we don't have specific zone
404cd11016eSAlexander Potapenko * requirements. Keep the flags related to allocation in atomic
405*b2916e14SAndrey Ryabinin * contexts, I/O, nolockdep.
406cd11016eSAlexander Potapenko */
407cd11016eSAlexander Potapenko alloc_flags &= ~GFP_ZONEMASK;
408*b2916e14SAndrey Ryabinin alloc_flags &= (GFP_ATOMIC | GFP_KERNEL | __GFP_NOLOCKDEP);
40987cc271dSKirill A. Shutemov alloc_flags |= __GFP_NOWARN;
410424cafeeSAndrey Konovalov page = alloc_pages(alloc_flags, DEPOT_POOL_ORDER);
411cd11016eSAlexander Potapenko if (page)
412cd11016eSAlexander Potapenko prealloc = page_address(page);
413cd11016eSAlexander Potapenko }
414cd11016eSAlexander Potapenko
415961c949bSAndrey Konovalov raw_spin_lock_irqsave(&pool_lock, flags);
416cd11016eSAlexander Potapenko
417c0cfc337SThomas Gleixner found = find_stack(*bucket, entries, nr_entries, hash);
418cd11016eSAlexander Potapenko if (!found) {
419b232b999SAndrey Konovalov struct stack_record *new =
420b232b999SAndrey Konovalov depot_alloc_stack(entries, nr_entries, hash, &prealloc);
4217f2b8818SMarco Elver
422cd11016eSAlexander Potapenko if (new) {
423cd11016eSAlexander Potapenko new->next = *bucket;
424cd11016eSAlexander Potapenko /*
425cd11016eSAlexander Potapenko * This smp_store_release() pairs with
426cd11016eSAlexander Potapenko * smp_load_acquire() from |bucket| above.
427cd11016eSAlexander Potapenko */
428cd11016eSAlexander Potapenko smp_store_release(bucket, new);
429cd11016eSAlexander Potapenko found = new;
430cd11016eSAlexander Potapenko }
431cd11016eSAlexander Potapenko } else if (prealloc) {
432cd11016eSAlexander Potapenko /*
433b232b999SAndrey Konovalov * Stack depot already contains this stack trace, but let's
434b232b999SAndrey Konovalov * keep the preallocated memory for the future.
435cd11016eSAlexander Potapenko */
436514d5c55SAndrey Konovalov depot_init_pool(&prealloc);
437cd11016eSAlexander Potapenko }
438cd11016eSAlexander Potapenko
439961c949bSAndrey Konovalov raw_spin_unlock_irqrestore(&pool_lock, flags);
440cd11016eSAlexander Potapenko exit:
441cd11016eSAlexander Potapenko if (prealloc) {
442b232b999SAndrey Konovalov /* Stack depot didn't use this memory, free it. */
443424cafeeSAndrey Konovalov free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER);
444cd11016eSAlexander Potapenko }
445cd11016eSAlexander Potapenko if (found)
44683a4f1efSAlexander Potapenko retval.handle = found->handle.handle;
447cd11016eSAlexander Potapenko fast_exit:
44883a4f1efSAlexander Potapenko return retval.handle;
449cd11016eSAlexander Potapenko }
45011ac25c6SMarco Elver EXPORT_SYMBOL_GPL(__stack_depot_save);
45111ac25c6SMarco Elver
stack_depot_save(unsigned long * entries,unsigned int nr_entries,gfp_t alloc_flags)45211ac25c6SMarco Elver depot_stack_handle_t stack_depot_save(unsigned long *entries,
45311ac25c6SMarco Elver unsigned int nr_entries,
45411ac25c6SMarco Elver gfp_t alloc_flags)
45511ac25c6SMarco Elver {
45636aa1e67SAndrey Konovalov return __stack_depot_save(entries, nr_entries, alloc_flags, true);
45711ac25c6SMarco Elver }
458c0cfc337SThomas Gleixner EXPORT_SYMBOL_GPL(stack_depot_save);
45915ef6a98SAndrey Konovalov
stack_depot_fetch(depot_stack_handle_t handle,unsigned long ** entries)46015ef6a98SAndrey Konovalov unsigned int stack_depot_fetch(depot_stack_handle_t handle,
46115ef6a98SAndrey Konovalov unsigned long **entries)
46215ef6a98SAndrey Konovalov {
46315ef6a98SAndrey Konovalov union handle_parts parts = { .handle = handle };
464beb3c23cSAndrey Konovalov /*
465beb3c23cSAndrey Konovalov * READ_ONCE pairs with potential concurrent write in
466beb3c23cSAndrey Konovalov * depot_alloc_stack.
467beb3c23cSAndrey Konovalov */
468beb3c23cSAndrey Konovalov int pool_index_cached = READ_ONCE(pool_index);
469961c949bSAndrey Konovalov void *pool;
470424cafeeSAndrey Konovalov size_t offset = parts.offset << DEPOT_STACK_ALIGN;
47115ef6a98SAndrey Konovalov struct stack_record *stack;
47215ef6a98SAndrey Konovalov
47315ef6a98SAndrey Konovalov *entries = NULL;
4748e00b2dfSAlexander Potapenko /*
4758e00b2dfSAlexander Potapenko * Let KMSAN know *entries is initialized. This shall prevent false
4768e00b2dfSAlexander Potapenko * positive reports if instrumented code accesses it.
4778e00b2dfSAlexander Potapenko */
4788e00b2dfSAlexander Potapenko kmsan_unpoison_memory(entries, sizeof(*entries));
4798e00b2dfSAlexander Potapenko
48015ef6a98SAndrey Konovalov if (!handle)
48115ef6a98SAndrey Konovalov return 0;
48215ef6a98SAndrey Konovalov
483beb3c23cSAndrey Konovalov if (parts.pool_index > pool_index_cached) {
484961c949bSAndrey Konovalov WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
485beb3c23cSAndrey Konovalov parts.pool_index, pool_index_cached, handle);
48615ef6a98SAndrey Konovalov return 0;
48715ef6a98SAndrey Konovalov }
488961c949bSAndrey Konovalov pool = stack_pools[parts.pool_index];
489961c949bSAndrey Konovalov if (!pool)
49015ef6a98SAndrey Konovalov return 0;
491961c949bSAndrey Konovalov stack = pool + offset;
49215ef6a98SAndrey Konovalov
49315ef6a98SAndrey Konovalov *entries = stack->entries;
49415ef6a98SAndrey Konovalov return stack->size;
49515ef6a98SAndrey Konovalov }
49615ef6a98SAndrey Konovalov EXPORT_SYMBOL_GPL(stack_depot_fetch);
49715ef6a98SAndrey Konovalov
stack_depot_print(depot_stack_handle_t stack)49815ef6a98SAndrey Konovalov void stack_depot_print(depot_stack_handle_t stack)
49915ef6a98SAndrey Konovalov {
50015ef6a98SAndrey Konovalov unsigned long *entries;
50115ef6a98SAndrey Konovalov unsigned int nr_entries;
50215ef6a98SAndrey Konovalov
50315ef6a98SAndrey Konovalov nr_entries = stack_depot_fetch(stack, &entries);
50415ef6a98SAndrey Konovalov if (nr_entries > 0)
50515ef6a98SAndrey Konovalov stack_trace_print(entries, nr_entries, 0);
50615ef6a98SAndrey Konovalov }
50715ef6a98SAndrey Konovalov EXPORT_SYMBOL_GPL(stack_depot_print);
50815ef6a98SAndrey Konovalov
stack_depot_snprint(depot_stack_handle_t handle,char * buf,size_t size,int spaces)50915ef6a98SAndrey Konovalov int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
51015ef6a98SAndrey Konovalov int spaces)
51115ef6a98SAndrey Konovalov {
51215ef6a98SAndrey Konovalov unsigned long *entries;
51315ef6a98SAndrey Konovalov unsigned int nr_entries;
51415ef6a98SAndrey Konovalov
51515ef6a98SAndrey Konovalov nr_entries = stack_depot_fetch(handle, &entries);
51615ef6a98SAndrey Konovalov return nr_entries ? stack_trace_snprint(buf, size, entries, nr_entries,
51715ef6a98SAndrey Konovalov spaces) : 0;
51815ef6a98SAndrey Konovalov }
51915ef6a98SAndrey Konovalov EXPORT_SYMBOL_GPL(stack_depot_snprint);
52015ef6a98SAndrey Konovalov
stack_depot_set_extra_bits(depot_stack_handle_t handle,unsigned int extra_bits)52136aa1e67SAndrey Konovalov depot_stack_handle_t __must_check stack_depot_set_extra_bits(
52236aa1e67SAndrey Konovalov depot_stack_handle_t handle, unsigned int extra_bits)
52336aa1e67SAndrey Konovalov {
52436aa1e67SAndrey Konovalov union handle_parts parts = { .handle = handle };
52536aa1e67SAndrey Konovalov
52636aa1e67SAndrey Konovalov /* Don't set extra bits on empty handles. */
52736aa1e67SAndrey Konovalov if (!handle)
52836aa1e67SAndrey Konovalov return 0;
52936aa1e67SAndrey Konovalov
53036aa1e67SAndrey Konovalov parts.extra = extra_bits;
53136aa1e67SAndrey Konovalov return parts.handle;
53236aa1e67SAndrey Konovalov }
53336aa1e67SAndrey Konovalov EXPORT_SYMBOL(stack_depot_set_extra_bits);
53436aa1e67SAndrey Konovalov
stack_depot_get_extra_bits(depot_stack_handle_t handle)53515ef6a98SAndrey Konovalov unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle)
53615ef6a98SAndrey Konovalov {
53715ef6a98SAndrey Konovalov union handle_parts parts = { .handle = handle };
53815ef6a98SAndrey Konovalov
53915ef6a98SAndrey Konovalov return parts.extra;
54015ef6a98SAndrey Konovalov }
54115ef6a98SAndrey Konovalov EXPORT_SYMBOL(stack_depot_get_extra_bits);
542