1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/gfp.h>
32#include <linux/jhash.h>
33#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/percpu.h>
36#include <linux/printk.h>
37#include <linux/slab.h>
38#include <linux/stacktrace.h>
39#include <linux/stackdepot.h>
40#include <linux/string.h>
41#include <linux/types.h>
42
43#define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
44
45#define STACK_ALLOC_NULL_PROTECTION_BITS 1
46#define STACK_ALLOC_ORDER 2
47#define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
48#define STACK_ALLOC_ALIGN 4
49#define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
50 STACK_ALLOC_ALIGN)
51#define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
52 STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
53#define STACK_ALLOC_SLABS_CAP 8192
54#define STACK_ALLOC_MAX_SLABS \
55 (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
56 (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
57
58
59union handle_parts {
60 depot_stack_handle_t handle;
61 struct {
62 u32 slabindex : STACK_ALLOC_INDEX_BITS;
63 u32 offset : STACK_ALLOC_OFFSET_BITS;
64 u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
65 };
66};
67
68struct stack_record {
69 struct stack_record *next;
70 u32 hash;
71 u32 size;
72 union handle_parts handle;
73 unsigned long entries[1];
74};
75
76static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
77
78static int depot_index;
79static int next_slab_inited;
80static size_t depot_offset;
81static DEFINE_SPINLOCK(depot_lock);
82
83static bool init_stack_slab(void **prealloc)
84{
85 if (!*prealloc)
86 return false;
87
88
89
90
91 if (smp_load_acquire(&next_slab_inited))
92 return true;
93 if (stack_slabs[depot_index] == NULL) {
94 stack_slabs[depot_index] = *prealloc;
95 } else {
96 stack_slabs[depot_index + 1] = *prealloc;
97
98
99
100
101 smp_store_release(&next_slab_inited, 1);
102 }
103 *prealloc = NULL;
104 return true;
105}
106
107
108static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
109 u32 hash, void **prealloc, gfp_t alloc_flags)
110{
111 int required_size = offsetof(struct stack_record, entries) +
112 sizeof(unsigned long) * size;
113 struct stack_record *stack;
114
115 required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
116
117 if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
118 if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
119 WARN_ONCE(1, "Stack depot reached limit capacity");
120 return NULL;
121 }
122 depot_index++;
123 depot_offset = 0;
124
125
126
127
128
129 if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
130 smp_store_release(&next_slab_inited, 0);
131 }
132 init_stack_slab(prealloc);
133 if (stack_slabs[depot_index] == NULL)
134 return NULL;
135
136 stack = stack_slabs[depot_index] + depot_offset;
137
138 stack->hash = hash;
139 stack->size = size;
140 stack->handle.slabindex = depot_index;
141 stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
142 stack->handle.valid = 1;
143 memcpy(stack->entries, entries, size * sizeof(unsigned long));
144 depot_offset += required_size;
145
146 return stack;
147}
148
149#define STACK_HASH_ORDER 20
150#define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
151#define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
152#define STACK_HASH_SEED 0x9747b28c
153
154static struct stack_record *stack_table[STACK_HASH_SIZE] = {
155 [0 ... STACK_HASH_SIZE - 1] = NULL
156};
157
158
159static inline u32 hash_stack(unsigned long *entries, unsigned int size)
160{
161 return jhash2((u32 *)entries,
162 size * sizeof(unsigned long) / sizeof(u32),
163 STACK_HASH_SEED);
164}
165
166
167
168
169
170static inline
171int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
172 unsigned int n)
173{
174 for ( ; n-- ; u1++, u2++) {
175 if (*u1 != *u2)
176 return 1;
177 }
178 return 0;
179}
180
181
182static inline struct stack_record *find_stack(struct stack_record *bucket,
183 unsigned long *entries, int size,
184 u32 hash)
185{
186 struct stack_record *found;
187
188 for (found = bucket; found; found = found->next) {
189 if (found->hash == hash &&
190 found->size == size &&
191 !stackdepot_memcmp(entries, found->entries, size))
192 return found;
193 }
194 return NULL;
195}
196
197
198
199
200
201
202
203
204
205
206unsigned int stack_depot_fetch(depot_stack_handle_t handle,
207 unsigned long **entries)
208{
209 union handle_parts parts = { .handle = handle };
210 void *slab = stack_slabs[parts.slabindex];
211 size_t offset = parts.offset << STACK_ALLOC_ALIGN;
212 struct stack_record *stack = slab + offset;
213
214 *entries = stack->entries;
215 return stack->size;
216}
217EXPORT_SYMBOL_GPL(stack_depot_fetch);
218
219void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
220{
221 unsigned int nent = stack_depot_fetch(handle, &trace->entries);
222
223 trace->max_entries = trace->nr_entries = nent;
224}
225EXPORT_SYMBOL_GPL(depot_fetch_stack);
226
227
228
229
230
231
232
233
234
235
236depot_stack_handle_t stack_depot_save(unsigned long *entries,
237 unsigned int nr_entries,
238 gfp_t alloc_flags)
239{
240 struct stack_record *found = NULL, **bucket;
241 depot_stack_handle_t retval = 0;
242 struct page *page = NULL;
243 void *prealloc = NULL;
244 unsigned long flags;
245 u32 hash;
246
247 if (unlikely(nr_entries == 0))
248 goto fast_exit;
249
250 hash = hash_stack(entries, nr_entries);
251 bucket = &stack_table[hash & STACK_HASH_MASK];
252
253
254
255
256
257
258 found = find_stack(smp_load_acquire(bucket), entries,
259 nr_entries, hash);
260 if (found)
261 goto exit;
262
263
264
265
266
267
268
269
270
271 if (unlikely(!smp_load_acquire(&next_slab_inited))) {
272
273
274
275
276
277 alloc_flags &= ~GFP_ZONEMASK;
278 alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
279 alloc_flags |= __GFP_NOWARN;
280 page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
281 if (page)
282 prealloc = page_address(page);
283 }
284
285 spin_lock_irqsave(&depot_lock, flags);
286
287 found = find_stack(*bucket, entries, nr_entries, hash);
288 if (!found) {
289 struct stack_record *new =
290 depot_alloc_stack(entries, nr_entries,
291 hash, &prealloc, alloc_flags);
292 if (new) {
293 new->next = *bucket;
294
295
296
297
298 smp_store_release(bucket, new);
299 found = new;
300 }
301 } else if (prealloc) {
302
303
304
305
306 WARN_ON(!init_stack_slab(&prealloc));
307 }
308
309 spin_unlock_irqrestore(&depot_lock, flags);
310exit:
311 if (prealloc) {
312
313 free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
314 }
315 if (found)
316 retval = found->handle.handle;
317fast_exit:
318 return retval;
319}
320EXPORT_SYMBOL_GPL(stack_depot_save);
321
322
323
324
325
326
327depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
328 gfp_t alloc_flags)
329{
330 return stack_depot_save(trace->entries, trace->nr_entries, alloc_flags);
331}
332EXPORT_SYMBOL_GPL(depot_save_stack);
333