1802d0beecafe (Thomas Gleixner 2019-05-27 08:55:21 +0200 1) // SPDX-License-Identifier: GPL-2.0-only
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 2) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 3) * Generic stack depot for storing stack traces.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 4) *
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 5) * Some debugging tools need to save stack traces of certain events which can
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 6) * be later presented to the user. For example, KASAN needs to safe alloc and
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 7) * free stacks for each object, but storing two stack traces per object
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 8) * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 9) * that).
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 10) *
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 11) * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 12) * and free stacks repeat a lot, we save about 100x space.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 13) * Stacks are never removed from depot, so we store them contiguously one after
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 14) * another in a contiguos memory allocation.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 15) *
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 16) * Author: Alexander Potapenko <glider@google.com>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 17) * Copyright (C) 2016 Google, Inc.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 18) *
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 19) * Based on code by Dmitry Chernenkov.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 20) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 21)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 22) #include <linux/gfp.h>
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 23) #include <linux/interrupt.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 24) #include <linux/jhash.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 25) #include <linux/kernel.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 26) #include <linux/mm.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 27) #include <linux/percpu.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 28) #include <linux/printk.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 29) #include <linux/slab.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 30) #include <linux/stacktrace.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 31) #include <linux/stackdepot.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 32) #include <linux/string.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 33) #include <linux/types.h>
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 34) #include <linux/memblock.h>
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 35)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 36) #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 37)
7c31190bcfdbf (Joonsoo Kim 2016-05-05 16:22:35 -0700 38) #define STACK_ALLOC_NULL_PROTECTION_BITS 1
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 39) #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 40) #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 41) #define STACK_ALLOC_ALIGN 4
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 42) #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 43) STACK_ALLOC_ALIGN)
7c31190bcfdbf (Joonsoo Kim 2016-05-05 16:22:35 -0700 44) #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
7c31190bcfdbf (Joonsoo Kim 2016-05-05 16:22:35 -0700 45) STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
02754e0a484a5 (Dmitry Vyukov 2016-10-27 17:46:44 -0700 46) #define STACK_ALLOC_SLABS_CAP 8192
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 47) #define STACK_ALLOC_MAX_SLABS \
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 48) (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 49) (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 50)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 51) /* The compact structure to store the reference to stacks. */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 52) union handle_parts {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 53) depot_stack_handle_t handle;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 54) struct {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 55) u32 slabindex : STACK_ALLOC_INDEX_BITS;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 56) u32 offset : STACK_ALLOC_OFFSET_BITS;
7c31190bcfdbf (Joonsoo Kim 2016-05-05 16:22:35 -0700 57) u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 58) };
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 59) };
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 60)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 61) struct stack_record {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 62) struct stack_record *next; /* Link in the hashtable */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 63) u32 hash; /* Hash in the hastable */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 64) u32 size; /* Number of frames in the stack */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 65) union handle_parts handle;
3a2b67e6e3fdb (Gustavo A. R. Silva 2020-12-15 20:43:04 -0800 66) unsigned long entries[]; /* Variable-sized array of entries. */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 67) };
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 68)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 69) static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 70)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 71) static int depot_index;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 72) static int next_slab_inited;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 73) static size_t depot_offset;
78564b9434878 (Zqiang 2021-05-06 18:03:40 -0700 74) static DEFINE_RAW_SPINLOCK(depot_lock);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 75)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 76) static bool init_stack_slab(void **prealloc)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 77) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 78) if (!*prealloc)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 79) return false;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 80) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 81) * This smp_load_acquire() pairs with smp_store_release() to
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 82) * |next_slab_inited| below and in depot_alloc_stack().
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 83) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 84) if (smp_load_acquire(&next_slab_inited))
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 85) return true;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 86) if (stack_slabs[depot_index] == NULL) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 87) stack_slabs[depot_index] = *prealloc;
305e519ce48e9 (Alexander Potapenko 2020-02-20 20:04:30 -0800 88) *prealloc = NULL;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 89) } else {
305e519ce48e9 (Alexander Potapenko 2020-02-20 20:04:30 -0800 90) /* If this is the last depot slab, do not touch the next one. */
305e519ce48e9 (Alexander Potapenko 2020-02-20 20:04:30 -0800 91) if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
305e519ce48e9 (Alexander Potapenko 2020-02-20 20:04:30 -0800 92) stack_slabs[depot_index + 1] = *prealloc;
305e519ce48e9 (Alexander Potapenko 2020-02-20 20:04:30 -0800 93) *prealloc = NULL;
305e519ce48e9 (Alexander Potapenko 2020-02-20 20:04:30 -0800 94) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 95) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 96) * This smp_store_release pairs with smp_load_acquire() from
ee050dc83bc32 (Miles Chen 2019-08-15 19:32:46 +0800 97) * |next_slab_inited| above and in stack_depot_save().
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 98) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 99) smp_store_release(&next_slab_inited, 1);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 100) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 101) return true;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 102) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 103)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 104) /* Allocation of a new stack in raw storage */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 105) static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 106) u32 hash, void **prealloc, gfp_t alloc_flags)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 107) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 108) struct stack_record *stack;
3a2b67e6e3fdb (Gustavo A. R. Silva 2020-12-15 20:43:04 -0800 109) size_t required_size = struct_size(stack, entries, size);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 110)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 111) required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 112)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 113) if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 114) if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 115) WARN_ONCE(1, "Stack depot reached limit capacity");
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 116) return NULL;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 117) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 118) depot_index++;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 119) depot_offset = 0;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 120) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 121) * smp_store_release() here pairs with smp_load_acquire() from
ee050dc83bc32 (Miles Chen 2019-08-15 19:32:46 +0800 122) * |next_slab_inited| in stack_depot_save() and
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 123) * init_stack_slab().
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 124) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 125) if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 126) smp_store_release(&next_slab_inited, 0);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 127) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 128) init_stack_slab(prealloc);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 129) if (stack_slabs[depot_index] == NULL)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 130) return NULL;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 131)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 132) stack = stack_slabs[depot_index] + depot_offset;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 133)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 134) stack->hash = hash;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 135) stack->size = size;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 136) stack->handle.slabindex = depot_index;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 137) stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
7c31190bcfdbf (Joonsoo Kim 2016-05-05 16:22:35 -0700 138) stack->handle.valid = 1;
47e684aaa2661 (Gustavo A. R. Silva 2020-12-15 20:43:07 -0800 139) memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 140) depot_offset += required_size;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 141)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 142) return stack;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 143) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 144)
d262093656a0e (Yogesh Lal 2021-02-25 17:21:24 -0800 145) #define STACK_HASH_SIZE (1L << CONFIG_STACK_HASH_ORDER)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 146) #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 147) #define STACK_HASH_SEED 0x9747b28c
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 148)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 149) static bool stack_depot_disable;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 150) static struct stack_record **stack_table;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 151)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 152) static int __init is_stack_depot_disabled(char *str)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 153) {
64427985c76fc (Vijayanand Jitta 2021-02-25 17:21:31 -0800 154) int ret;
64427985c76fc (Vijayanand Jitta 2021-02-25 17:21:31 -0800 155)
64427985c76fc (Vijayanand Jitta 2021-02-25 17:21:31 -0800 156) ret = kstrtobool(str, &stack_depot_disable);
64427985c76fc (Vijayanand Jitta 2021-02-25 17:21:31 -0800 157) if (!ret && stack_depot_disable) {
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 158) pr_info("Stack Depot is disabled\n");
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 159) stack_table = NULL;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 160) }
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 161) return 0;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 162) }
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 163) early_param("stack_depot_disable", is_stack_depot_disabled);
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 164)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 165) int __init stack_depot_init(void)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 166) {
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 167) if (!stack_depot_disable) {
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 168) size_t size = (STACK_HASH_SIZE * sizeof(struct stack_record *));
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 169) int i;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 170)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 171) stack_table = memblock_alloc(size, size);
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 172) for (i = 0; i < STACK_HASH_SIZE; i++)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 173) stack_table[i] = NULL;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 174) }
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 175) return 0;
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 176) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 177)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 178) /* Calculate hash for a stack */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 179) static inline u32 hash_stack(unsigned long *entries, unsigned int size)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 180) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 181) return jhash2((u32 *)entries,
180644f80a028 (Gustavo A. R. Silva 2020-12-15 20:43:10 -0800 182) array_size(size, sizeof(*entries)) / sizeof(u32),
180644f80a028 (Gustavo A. R. Silva 2020-12-15 20:43:10 -0800 183) STACK_HASH_SEED);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 184) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 185)
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 186) /* Use our own, non-instrumented version of memcmp().
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 187) *
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 188) * We actually don't care about the order, just the equality.
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 189) */
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 190) static inline
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 191) int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 192) unsigned int n)
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 193) {
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 194) for ( ; n-- ; u1++, u2++) {
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 195) if (*u1 != *u2)
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 196) return 1;
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 197) }
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 198) return 0;
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 199) }
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 200)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 201) /* Find a stack that is equal to the one stored in entries in the hash */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 202) static inline struct stack_record *find_stack(struct stack_record *bucket,
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 203) unsigned long *entries, int size,
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 204) u32 hash)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 205) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 206) struct stack_record *found;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 207)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 208) for (found = bucket; found; found = found->next) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 209) if (found->hash == hash &&
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 210) found->size == size &&
a571b272ab0f8 (Alexander Potapenko 2018-02-06 15:38:24 -0800 211) !stackdepot_memcmp(entries, found->entries, size))
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 212) return found;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 213) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 214) return NULL;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 215) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 216)
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 217) /**
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 218) * stack_depot_fetch - Fetch stack entries from a depot
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 219) *
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 220) * @handle: Stack depot handle which was returned from
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 221) * stack_depot_save().
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 222) * @entries: Pointer to store the entries address
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 223) *
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 224) * Return: The number of trace entries for this depot.
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 225) */
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 226) unsigned int stack_depot_fetch(depot_stack_handle_t handle,
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 227) unsigned long **entries)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 228) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 229) union handle_parts parts = { .handle = handle };
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 230) void *slab;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 231) size_t offset = parts.offset << STACK_ALLOC_ALIGN;
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 232) struct stack_record *stack;
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 233)
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 234) *entries = NULL;
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 235) if (parts.slabindex > depot_index) {
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 236) WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 237) parts.slabindex, depot_index, handle);
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 238) return 0;
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 239) }
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 240) slab = stack_slabs[parts.slabindex];
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 241) if (!slab)
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 242) return 0;
69866e156ce28 (Alexander Potapenko 2020-04-06 20:10:15 -0700 243) stack = slab + offset;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 244)
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 245) *entries = stack->entries;
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 246) return stack->size;
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 247) }
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 248) EXPORT_SYMBOL_GPL(stack_depot_fetch);
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 249)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 250) /**
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 251) * stack_depot_save - Save a stack trace from an array
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 252) *
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 253) * @entries: Pointer to storage array
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 254) * @nr_entries: Size of the storage array
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 255) * @alloc_flags: Allocation gfp flags
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 256) *
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 257) * Return: The handle of the stack struct stored in depot
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 258) */
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 259) depot_stack_handle_t stack_depot_save(unsigned long *entries,
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 260) unsigned int nr_entries,
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 261) gfp_t alloc_flags)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 262) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 263) struct stack_record *found = NULL, **bucket;
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 264) depot_stack_handle_t retval = 0;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 265) struct page *page = NULL;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 266) void *prealloc = NULL;
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 267) unsigned long flags;
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 268) u32 hash;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 269)
e1fdc403349c6 (Vijayanand Jitta 2021-02-25 17:21:27 -0800 270) if (unlikely(nr_entries == 0) || stack_depot_disable)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 271) goto fast_exit;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 272)
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 273) hash = hash_stack(entries, nr_entries);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 274) bucket = &stack_table[hash & STACK_HASH_MASK];
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 275)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 276) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 277) * Fast path: look the stack trace up without locking.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 278) * The smp_load_acquire() here pairs with smp_store_release() to
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 279) * |bucket| below.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 280) */
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 281) found = find_stack(smp_load_acquire(bucket), entries,
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 282) nr_entries, hash);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 283) if (found)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 284) goto exit;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 285)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 286) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 287) * Check if the current or the next stack slab need to be initialized.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 288) * If so, allocate the memory - we won't be able to do that under the
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 289) * lock.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 290) *
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 291) * The smp_load_acquire() here pairs with smp_store_release() to
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 292) * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 293) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 294) if (unlikely(!smp_load_acquire(&next_slab_inited))) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 295) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 296) * Zero out zone modifiers, as we don't have specific zone
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 297) * requirements. Keep the flags related to allocation in atomic
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 298) * contexts and I/O.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 299) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 300) alloc_flags &= ~GFP_ZONEMASK;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 301) alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
87cc271d5e432 (Kirill A. Shutemov 2016-07-28 15:49:10 -0700 302) alloc_flags |= __GFP_NOWARN;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 303) page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 304) if (page)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 305) prealloc = page_address(page);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 306) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 307)
78564b9434878 (Zqiang 2021-05-06 18:03:40 -0700 308) raw_spin_lock_irqsave(&depot_lock, flags);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 309)
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 310) found = find_stack(*bucket, entries, nr_entries, hash);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 311) if (!found) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 312) struct stack_record *new =
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 313) depot_alloc_stack(entries, nr_entries,
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 314) hash, &prealloc, alloc_flags);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 315) if (new) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 316) new->next = *bucket;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 317) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 318) * This smp_store_release() pairs with
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 319) * smp_load_acquire() from |bucket| above.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 320) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 321) smp_store_release(bucket, new);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 322) found = new;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 323) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 324) } else if (prealloc) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 325) /*
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 326) * We didn't need to store this stack trace, but let's keep
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 327) * the preallocated memory for the future.
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 328) */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 329) WARN_ON(!init_stack_slab(&prealloc));
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 330) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 331)
78564b9434878 (Zqiang 2021-05-06 18:03:40 -0700 332) raw_spin_unlock_irqrestore(&depot_lock, flags);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 333) exit:
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 334) if (prealloc) {
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 335) /* Nobody used this memory, ok to free it. */
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 336) free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 337) }
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 338) if (found)
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 339) retval = found->handle.handle;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 340) fast_exit:
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 341) return retval;
cd11016e5f521 (Alexander Potapenko 2016-03-25 14:22:08 -0700 342) }
c0cfc337264c5 (Thomas Gleixner 2019-04-25 11:44:56 +0200 343) EXPORT_SYMBOL_GPL(stack_depot_save);
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 344)
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 345) static inline int in_irqentry_text(unsigned long ptr)
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 346) {
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 347) return (ptr >= (unsigned long)&__irqentry_text_start &&
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 348) ptr < (unsigned long)&__irqentry_text_end) ||
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 349) (ptr >= (unsigned long)&__softirqentry_text_start &&
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 350) ptr < (unsigned long)&__softirqentry_text_end);
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 351) }
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 352)
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 353) unsigned int filter_irq_stacks(unsigned long *entries,
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 354) unsigned int nr_entries)
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 355) {
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 356) unsigned int i;
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 357)
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 358) for (i = 0; i < nr_entries; i++) {
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 359) if (in_irqentry_text(entries[i])) {
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 360) /* Include the irqentry function into the stack. */
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 361) return i + 1;
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 362) }
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 363) }
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 364) return nr_entries;
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 365) }
505a0ef15f96c (Alexander Potapenko 2020-04-06 20:10:22 -0700 366) EXPORT_SYMBOL_GPL(filter_irq_stacks);