stackdepot.c 10 KB

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