stackdepot.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Generic stack depot for storing stack traces.
  3. *
  4. * Some debugging tools need to save stack traces of certain events which can
  5. * be later presented to the user. For example, KASAN needs to safe alloc and
  6. * free stacks for each object, but storing two stack traces per object
  7. * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
  8. * that).
  9. *
  10. * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
  11. * and free stacks repeat a lot, we save about 100x space.
  12. * Stacks are never removed from depot, so we store them contiguously one after
  13. * another in a contiguos memory allocation.
  14. *
  15. * Author: Alexander Potapenko <glider@google.com>
  16. * Copyright (C) 2016 Google, Inc.
  17. *
  18. * Based on code by Dmitry Chernenkov.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * version 2 as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. */
  30. #include <linux/gfp.h>
  31. #include <linux/jhash.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/percpu.h>
  35. #include <linux/printk.h>
  36. #include <linux/slab.h>
  37. #include <linux/stacktrace.h>
  38. #include <linux/stackdepot.h>
  39. #include <linux/string.h>
  40. #include <linux/types.h>
  41. #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
  42. #define STACK_ALLOC_NULL_PROTECTION_BITS 1
  43. #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
  44. #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
  45. #define STACK_ALLOC_ALIGN 4
  46. #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
  47. STACK_ALLOC_ALIGN)
  48. #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
  49. STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
  50. #define STACK_ALLOC_SLABS_CAP 8192
  51. #define STACK_ALLOC_MAX_SLABS \
  52. (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
  53. (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
  54. /* The compact structure to store the reference to stacks. */
  55. union handle_parts {
  56. depot_stack_handle_t handle;
  57. struct {
  58. u32 slabindex : STACK_ALLOC_INDEX_BITS;
  59. u32 offset : STACK_ALLOC_OFFSET_BITS;
  60. u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
  61. };
  62. };
  63. struct stack_record {
  64. struct stack_record *next; /* Link in the hashtable */
  65. u32 hash; /* Hash in the hastable */
  66. u32 size; /* Number of frames in the stack */
  67. union handle_parts handle;
  68. unsigned long entries[1]; /* Variable-sized array of entries. */
  69. };
  70. static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
  71. static int depot_index;
  72. static int next_slab_inited;
  73. static size_t depot_offset;
  74. static DEFINE_RAW_SPINLOCK(depot_lock);
  75. static bool init_stack_slab(void **prealloc)
  76. {
  77. if (!*prealloc)
  78. return false;
  79. /*
  80. * This smp_load_acquire() pairs with smp_store_release() to
  81. * |next_slab_inited| below and in depot_alloc_stack().
  82. */
  83. if (smp_load_acquire(&next_slab_inited))
  84. return true;
  85. if (stack_slabs[depot_index] == NULL) {
  86. stack_slabs[depot_index] = *prealloc;
  87. *prealloc = NULL;
  88. } else {
  89. /* If this is the last depot slab, do not touch the next one. */
  90. if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
  91. stack_slabs[depot_index + 1] = *prealloc;
  92. *prealloc = NULL;
  93. }
  94. /*
  95. * This smp_store_release pairs with smp_load_acquire() from
  96. * |next_slab_inited| above and in depot_save_stack().
  97. */
  98. smp_store_release(&next_slab_inited, 1);
  99. }
  100. return true;
  101. }
  102. /* Allocation of a new stack in raw storage */
  103. static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
  104. u32 hash, void **prealloc, gfp_t alloc_flags)
  105. {
  106. int required_size = offsetof(struct stack_record, entries) +
  107. sizeof(unsigned long) * size;
  108. struct stack_record *stack;
  109. required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
  110. if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
  111. if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
  112. WARN_ONCE(1, "Stack depot reached limit capacity");
  113. return NULL;
  114. }
  115. depot_index++;
  116. depot_offset = 0;
  117. /*
  118. * smp_store_release() here pairs with smp_load_acquire() from
  119. * |next_slab_inited| in depot_save_stack() and
  120. * init_stack_slab().
  121. */
  122. if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
  123. smp_store_release(&next_slab_inited, 0);
  124. }
  125. init_stack_slab(prealloc);
  126. if (stack_slabs[depot_index] == NULL)
  127. return NULL;
  128. stack = stack_slabs[depot_index] + depot_offset;
  129. stack->hash = hash;
  130. stack->size = size;
  131. stack->handle.slabindex = depot_index;
  132. stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
  133. stack->handle.valid = 1;
  134. memcpy(stack->entries, entries, size * sizeof(unsigned long));
  135. depot_offset += required_size;
  136. return stack;
  137. }
  138. #define STACK_HASH_ORDER 20
  139. #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
  140. #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
  141. #define STACK_HASH_SEED 0x9747b28c
  142. static struct stack_record *stack_table[STACK_HASH_SIZE] = {
  143. [0 ... STACK_HASH_SIZE - 1] = NULL
  144. };
  145. /* Calculate hash for a stack */
  146. static inline u32 hash_stack(unsigned long *entries, unsigned int size)
  147. {
  148. return jhash2((u32 *)entries,
  149. size * sizeof(unsigned long) / sizeof(u32),
  150. STACK_HASH_SEED);
  151. }
  152. /* Use our own, non-instrumented version of memcmp().
  153. *
  154. * We actually don't care about the order, just the equality.
  155. */
  156. static inline
  157. int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
  158. unsigned int n)
  159. {
  160. for ( ; n-- ; u1++, u2++) {
  161. if (*u1 != *u2)
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. /* Find a stack that is equal to the one stored in entries in the hash */
  167. static inline struct stack_record *find_stack(struct stack_record *bucket,
  168. unsigned long *entries, int size,
  169. u32 hash)
  170. {
  171. struct stack_record *found;
  172. for (found = bucket; found; found = found->next) {
  173. if (found->hash == hash &&
  174. found->size == size &&
  175. !stackdepot_memcmp(entries, found->entries, size))
  176. return found;
  177. }
  178. return NULL;
  179. }
  180. void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
  181. {
  182. union handle_parts parts = { .handle = handle };
  183. void *slab = stack_slabs[parts.slabindex];
  184. size_t offset = parts.offset << STACK_ALLOC_ALIGN;
  185. struct stack_record *stack = slab + offset;
  186. trace->nr_entries = trace->max_entries = stack->size;
  187. trace->entries = stack->entries;
  188. trace->skip = 0;
  189. }
  190. EXPORT_SYMBOL_GPL(depot_fetch_stack);
  191. /**
  192. * depot_save_stack - save stack in a stack depot.
  193. * @trace - the stacktrace to save.
  194. * @alloc_flags - flags for allocating additional memory if required.
  195. *
  196. * Returns the handle of the stack struct stored in depot.
  197. */
  198. depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
  199. gfp_t alloc_flags)
  200. {
  201. u32 hash;
  202. depot_stack_handle_t retval = 0;
  203. struct stack_record *found = NULL, **bucket;
  204. unsigned long flags;
  205. struct page *page = NULL;
  206. void *prealloc = NULL;
  207. if (unlikely(trace->nr_entries == 0))
  208. goto fast_exit;
  209. hash = hash_stack(trace->entries, trace->nr_entries);
  210. bucket = &stack_table[hash & STACK_HASH_MASK];
  211. /*
  212. * Fast path: look the stack trace up without locking.
  213. * The smp_load_acquire() here pairs with smp_store_release() to
  214. * |bucket| below.
  215. */
  216. found = find_stack(smp_load_acquire(bucket), trace->entries,
  217. trace->nr_entries, hash);
  218. if (found)
  219. goto exit;
  220. /*
  221. * Check if the current or the next stack slab need to be initialized.
  222. * If so, allocate the memory - we won't be able to do that under the
  223. * lock.
  224. *
  225. * The smp_load_acquire() here pairs with smp_store_release() to
  226. * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
  227. */
  228. if (unlikely(!smp_load_acquire(&next_slab_inited))) {
  229. /*
  230. * Zero out zone modifiers, as we don't have specific zone
  231. * requirements. Keep the flags related to allocation in atomic
  232. * contexts and I/O.
  233. */
  234. alloc_flags &= ~GFP_ZONEMASK;
  235. alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
  236. alloc_flags |= __GFP_NOWARN;
  237. page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
  238. if (page)
  239. prealloc = page_address(page);
  240. }
  241. raw_spin_lock_irqsave(&depot_lock, flags);
  242. found = find_stack(*bucket, trace->entries, trace->nr_entries, hash);
  243. if (!found) {
  244. struct stack_record *new =
  245. depot_alloc_stack(trace->entries, trace->nr_entries,
  246. hash, &prealloc, alloc_flags);
  247. if (new) {
  248. new->next = *bucket;
  249. /*
  250. * This smp_store_release() pairs with
  251. * smp_load_acquire() from |bucket| above.
  252. */
  253. smp_store_release(bucket, new);
  254. found = new;
  255. }
  256. } else if (prealloc) {
  257. /*
  258. * We didn't need to store this stack trace, but let's keep
  259. * the preallocated memory for the future.
  260. */
  261. WARN_ON(!init_stack_slab(&prealloc));
  262. }
  263. raw_spin_unlock_irqrestore(&depot_lock, flags);
  264. exit:
  265. if (prealloc) {
  266. /* Nobody used this memory, ok to free it. */
  267. free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
  268. }
  269. if (found)
  270. retval = found->handle.handle;
  271. fast_exit:
  272. return retval;
  273. }
  274. EXPORT_SYMBOL_GPL(depot_save_stack);