stackdepot.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/jhash.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/percpu.h>
  26. #include <linux/printk.h>
  27. #include <linux/slab.h>
  28. #include <linux/stacktrace.h>
  29. #include <linux/stackdepot.h>
  30. #include <linux/string.h>
  31. #include <linux/types.h>
  32. #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
  33. #define STACK_ALLOC_NULL_PROTECTION_BITS 1
  34. #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
  35. #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
  36. #define STACK_ALLOC_ALIGN 4
  37. #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
  38. STACK_ALLOC_ALIGN)
  39. #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
  40. STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
  41. #define STACK_ALLOC_SLABS_CAP 8192
  42. #define STACK_ALLOC_MAX_SLABS \
  43. (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
  44. (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
  45. /* The compact structure to store the reference to stacks. */
  46. union handle_parts {
  47. depot_stack_handle_t handle;
  48. struct {
  49. u32 slabindex : STACK_ALLOC_INDEX_BITS;
  50. u32 offset : STACK_ALLOC_OFFSET_BITS;
  51. u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
  52. };
  53. };
  54. struct stack_record {
  55. struct stack_record *next; /* Link in the hashtable */
  56. u32 hash; /* Hash in the hastable */
  57. u32 size; /* Number of frames in the stack */
  58. union handle_parts handle;
  59. unsigned long entries[1]; /* Variable-sized array of entries. */
  60. };
  61. static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
  62. static int depot_index;
  63. static int next_slab_inited;
  64. static size_t depot_offset;
  65. static DEFINE_RAW_SPINLOCK(depot_lock);
  66. static bool init_stack_slab(void **prealloc)
  67. {
  68. if (!*prealloc)
  69. return false;
  70. /*
  71. * This smp_load_acquire() pairs with smp_store_release() to
  72. * |next_slab_inited| below and in depot_alloc_stack().
  73. */
  74. if (smp_load_acquire(&next_slab_inited))
  75. return true;
  76. if (stack_slabs[depot_index] == NULL) {
  77. stack_slabs[depot_index] = *prealloc;
  78. *prealloc = NULL;
  79. } else {
  80. /* If this is the last depot slab, do not touch the next one. */
  81. if (depot_index + 1 < STACK_ALLOC_MAX_SLABS) {
  82. stack_slabs[depot_index + 1] = *prealloc;
  83. *prealloc = NULL;
  84. }
  85. /*
  86. * This smp_store_release pairs with smp_load_acquire() from
  87. * |next_slab_inited| above and in stack_depot_save().
  88. */
  89. smp_store_release(&next_slab_inited, 1);
  90. }
  91. return true;
  92. }
  93. /* Allocation of a new stack in raw storage */
  94. static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
  95. u32 hash, void **prealloc, gfp_t alloc_flags)
  96. {
  97. int required_size = offsetof(struct stack_record, entries) +
  98. sizeof(unsigned long) * size;
  99. struct stack_record *stack;
  100. required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
  101. if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
  102. if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
  103. WARN_ONCE(1, "Stack depot reached limit capacity");
  104. return NULL;
  105. }
  106. depot_index++;
  107. depot_offset = 0;
  108. /*
  109. * smp_store_release() here pairs with smp_load_acquire() from
  110. * |next_slab_inited| in stack_depot_save() and
  111. * init_stack_slab().
  112. */
  113. if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
  114. smp_store_release(&next_slab_inited, 0);
  115. }
  116. init_stack_slab(prealloc);
  117. if (stack_slabs[depot_index] == NULL)
  118. return NULL;
  119. stack = stack_slabs[depot_index] + depot_offset;
  120. stack->hash = hash;
  121. stack->size = size;
  122. stack->handle.slabindex = depot_index;
  123. stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
  124. stack->handle.valid = 1;
  125. memcpy(stack->entries, entries, size * sizeof(unsigned long));
  126. depot_offset += required_size;
  127. return stack;
  128. }
  129. #define STACK_HASH_ORDER 20
  130. #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
  131. #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
  132. #define STACK_HASH_SEED 0x9747b28c
  133. static struct stack_record *stack_table[STACK_HASH_SIZE] = {
  134. [0 ... STACK_HASH_SIZE - 1] = NULL
  135. };
  136. /* Calculate hash for a stack */
  137. static inline u32 hash_stack(unsigned long *entries, unsigned int size)
  138. {
  139. return jhash2((u32 *)entries,
  140. size * sizeof(unsigned long) / sizeof(u32),
  141. STACK_HASH_SEED);
  142. }
  143. /* Use our own, non-instrumented version of memcmp().
  144. *
  145. * We actually don't care about the order, just the equality.
  146. */
  147. static inline
  148. int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
  149. unsigned int n)
  150. {
  151. for ( ; n-- ; u1++, u2++) {
  152. if (*u1 != *u2)
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. /* Find a stack that is equal to the one stored in entries in the hash */
  158. static inline struct stack_record *find_stack(struct stack_record *bucket,
  159. unsigned long *entries, int size,
  160. u32 hash)
  161. {
  162. struct stack_record *found;
  163. for (found = bucket; found; found = found->next) {
  164. if (found->hash == hash &&
  165. found->size == size &&
  166. !stackdepot_memcmp(entries, found->entries, size))
  167. return found;
  168. }
  169. return NULL;
  170. }
  171. /**
  172. * stack_depot_fetch - Fetch stack entries from a depot
  173. *
  174. * @handle: Stack depot handle which was returned from
  175. * stack_depot_save().
  176. * @entries: Pointer to store the entries address
  177. *
  178. * Return: The number of trace entries for this depot.
  179. */
  180. unsigned int stack_depot_fetch(depot_stack_handle_t handle,
  181. unsigned long **entries)
  182. {
  183. union handle_parts parts = { .handle = handle };
  184. void *slab = stack_slabs[parts.slabindex];
  185. size_t offset = parts.offset << STACK_ALLOC_ALIGN;
  186. struct stack_record *stack = slab + offset;
  187. *entries = stack->entries;
  188. return stack->size;
  189. }
  190. EXPORT_SYMBOL_GPL(stack_depot_fetch);
  191. /**
  192. * stack_depot_save - Save a stack trace from an array
  193. *
  194. * @entries: Pointer to storage array
  195. * @nr_entries: Size of the storage array
  196. * @alloc_flags: Allocation gfp flags
  197. *
  198. * Return: The handle of the stack struct stored in depot
  199. */
  200. depot_stack_handle_t stack_depot_save(unsigned long *entries,
  201. unsigned int nr_entries,
  202. gfp_t alloc_flags)
  203. {
  204. struct stack_record *found = NULL, **bucket;
  205. depot_stack_handle_t retval = 0;
  206. struct page *page = NULL;
  207. void *prealloc = NULL;
  208. unsigned long flags;
  209. u32 hash;
  210. if (unlikely(nr_entries == 0))
  211. goto fast_exit;
  212. hash = hash_stack(entries, nr_entries);
  213. bucket = &stack_table[hash & STACK_HASH_MASK];
  214. /*
  215. * Fast path: look the stack trace up without locking.
  216. * The smp_load_acquire() here pairs with smp_store_release() to
  217. * |bucket| below.
  218. */
  219. found = find_stack(smp_load_acquire(bucket), entries,
  220. nr_entries, hash);
  221. if (found)
  222. goto exit;
  223. /*
  224. * Check if the current or the next stack slab need to be initialized.
  225. * If so, allocate the memory - we won't be able to do that under the
  226. * lock.
  227. *
  228. * The smp_load_acquire() here pairs with smp_store_release() to
  229. * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
  230. */
  231. if (unlikely(!smp_load_acquire(&next_slab_inited))) {
  232. /*
  233. * Zero out zone modifiers, as we don't have specific zone
  234. * requirements. Keep the flags related to allocation in atomic
  235. * contexts and I/O.
  236. */
  237. alloc_flags &= ~GFP_ZONEMASK;
  238. alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
  239. alloc_flags |= __GFP_NOWARN;
  240. page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
  241. if (page)
  242. prealloc = page_address(page);
  243. }
  244. raw_spin_lock_irqsave(&depot_lock, flags);
  245. found = find_stack(*bucket, entries, nr_entries, hash);
  246. if (!found) {
  247. struct stack_record *new =
  248. depot_alloc_stack(entries, nr_entries,
  249. hash, &prealloc, alloc_flags);
  250. if (new) {
  251. new->next = *bucket;
  252. /*
  253. * This smp_store_release() pairs with
  254. * smp_load_acquire() from |bucket| above.
  255. */
  256. smp_store_release(bucket, new);
  257. found = new;
  258. }
  259. } else if (prealloc) {
  260. /*
  261. * We didn't need to store this stack trace, but let's keep
  262. * the preallocated memory for the future.
  263. */
  264. WARN_ON(!init_stack_slab(&prealloc));
  265. }
  266. raw_spin_unlock_irqrestore(&depot_lock, flags);
  267. exit:
  268. if (prealloc) {
  269. /* Nobody used this memory, ok to free it. */
  270. free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
  271. }
  272. if (found)
  273. retval = found->handle.handle;
  274. fast_exit:
  275. return retval;
  276. }
  277. EXPORT_SYMBOL_GPL(stack_depot_save);