stackdepot.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_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. } else {
  88. stack_slabs[depot_index + 1] = *prealloc;
  89. /*
  90. * This smp_store_release pairs with smp_load_acquire() from
  91. * |next_slab_inited| above and in depot_save_stack().
  92. */
  93. smp_store_release(&next_slab_inited, 1);
  94. }
  95. *prealloc = NULL;
  96. return true;
  97. }
  98. /* Allocation of a new stack in raw storage */
  99. static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
  100. u32 hash, void **prealloc, gfp_t alloc_flags)
  101. {
  102. int required_size = offsetof(struct stack_record, entries) +
  103. sizeof(unsigned long) * size;
  104. struct stack_record *stack;
  105. required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
  106. if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
  107. if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
  108. WARN_ONCE(1, "Stack depot reached limit capacity");
  109. return NULL;
  110. }
  111. depot_index++;
  112. depot_offset = 0;
  113. /*
  114. * smp_store_release() here pairs with smp_load_acquire() from
  115. * |next_slab_inited| in depot_save_stack() and
  116. * init_stack_slab().
  117. */
  118. if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
  119. smp_store_release(&next_slab_inited, 0);
  120. }
  121. init_stack_slab(prealloc);
  122. if (stack_slabs[depot_index] == NULL)
  123. return NULL;
  124. stack = stack_slabs[depot_index] + depot_offset;
  125. stack->hash = hash;
  126. stack->size = size;
  127. stack->handle.slabindex = depot_index;
  128. stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
  129. stack->handle.valid = 1;
  130. memcpy(stack->entries, entries, size * sizeof(unsigned long));
  131. depot_offset += required_size;
  132. return stack;
  133. }
  134. #define STACK_HASH_ORDER 20
  135. #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
  136. #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
  137. #define STACK_HASH_SEED 0x9747b28c
  138. static struct stack_record *stack_table[STACK_HASH_SIZE] = {
  139. [0 ... STACK_HASH_SIZE - 1] = NULL
  140. };
  141. /* Calculate hash for a stack */
  142. static inline u32 hash_stack(unsigned long *entries, unsigned int size)
  143. {
  144. return jhash2((u32 *)entries,
  145. size * sizeof(unsigned long) / sizeof(u32),
  146. STACK_HASH_SEED);
  147. }
  148. /* Find a stack that is equal to the one stored in entries in the hash */
  149. static inline struct stack_record *find_stack(struct stack_record *bucket,
  150. unsigned long *entries, int size,
  151. u32 hash)
  152. {
  153. struct stack_record *found;
  154. for (found = bucket; found; found = found->next) {
  155. if (found->hash == hash &&
  156. found->size == size &&
  157. !memcmp(entries, found->entries,
  158. size * sizeof(unsigned long))) {
  159. return found;
  160. }
  161. }
  162. return NULL;
  163. }
  164. void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
  165. {
  166. union handle_parts parts = { .handle = handle };
  167. void *slab = stack_slabs[parts.slabindex];
  168. size_t offset = parts.offset << STACK_ALLOC_ALIGN;
  169. struct stack_record *stack = slab + offset;
  170. trace->nr_entries = trace->max_entries = stack->size;
  171. trace->entries = stack->entries;
  172. trace->skip = 0;
  173. }
  174. EXPORT_SYMBOL_GPL(depot_fetch_stack);
  175. /**
  176. * depot_save_stack - save stack in a stack depot.
  177. * @trace - the stacktrace to save.
  178. * @alloc_flags - flags for allocating additional memory if required.
  179. *
  180. * Returns the handle of the stack struct stored in depot.
  181. */
  182. depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
  183. gfp_t alloc_flags)
  184. {
  185. u32 hash;
  186. depot_stack_handle_t retval = 0;
  187. struct stack_record *found = NULL, **bucket;
  188. unsigned long flags;
  189. struct page *page = NULL;
  190. void *prealloc = NULL;
  191. if (unlikely(trace->nr_entries == 0))
  192. goto fast_exit;
  193. hash = hash_stack(trace->entries, trace->nr_entries);
  194. bucket = &stack_table[hash & STACK_HASH_MASK];
  195. /*
  196. * Fast path: look the stack trace up without locking.
  197. * The smp_load_acquire() here pairs with smp_store_release() to
  198. * |bucket| below.
  199. */
  200. found = find_stack(smp_load_acquire(bucket), trace->entries,
  201. trace->nr_entries, hash);
  202. if (found)
  203. goto exit;
  204. /*
  205. * Check if the current or the next stack slab need to be initialized.
  206. * If so, allocate the memory - we won't be able to do that under the
  207. * lock.
  208. *
  209. * The smp_load_acquire() here pairs with smp_store_release() to
  210. * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
  211. */
  212. if (unlikely(!smp_load_acquire(&next_slab_inited))) {
  213. /*
  214. * Zero out zone modifiers, as we don't have specific zone
  215. * requirements. Keep the flags related to allocation in atomic
  216. * contexts and I/O.
  217. */
  218. alloc_flags &= ~GFP_ZONEMASK;
  219. alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
  220. alloc_flags |= __GFP_NOWARN;
  221. page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
  222. if (page)
  223. prealloc = page_address(page);
  224. }
  225. spin_lock_irqsave(&depot_lock, flags);
  226. found = find_stack(*bucket, trace->entries, trace->nr_entries, hash);
  227. if (!found) {
  228. struct stack_record *new =
  229. depot_alloc_stack(trace->entries, trace->nr_entries,
  230. hash, &prealloc, alloc_flags);
  231. if (new) {
  232. new->next = *bucket;
  233. /*
  234. * This smp_store_release() pairs with
  235. * smp_load_acquire() from |bucket| above.
  236. */
  237. smp_store_release(bucket, new);
  238. found = new;
  239. }
  240. } else if (prealloc) {
  241. /*
  242. * We didn't need to store this stack trace, but let's keep
  243. * the preallocated memory for the future.
  244. */
  245. WARN_ON(!init_stack_slab(&prealloc));
  246. }
  247. spin_unlock_irqrestore(&depot_lock, flags);
  248. exit:
  249. if (prealloc) {
  250. /* Nobody used this memory, ok to free it. */
  251. free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
  252. }
  253. if (found)
  254. retval = found->handle.handle;
  255. fast_exit:
  256. return retval;
  257. }
  258. EXPORT_SYMBOL_GPL(depot_save_stack);