123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include <linux/gfp.h>
- #include <linux/jhash.h>
- #include <linux/kernel.h>
- #include <linux/mm.h>
- #include <linux/percpu.h>
- #include <linux/printk.h>
- #include <linux/slab.h>
- #include <linux/stacktrace.h>
- #include <linux/stackdepot.h>
- #include <linux/string.h>
- #include <linux/types.h>
- #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
- #define STACK_ALLOC_NULL_PROTECTION_BITS 1
- #define STACK_ALLOC_ORDER 2
- #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
- #define STACK_ALLOC_ALIGN 4
- #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
- STACK_ALLOC_ALIGN)
- #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
- STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
- #define STACK_ALLOC_SLABS_CAP 8192
- #define STACK_ALLOC_MAX_SLABS \
- (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
- (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
- union handle_parts {
- depot_stack_handle_t handle;
- struct {
- u32 slabindex : STACK_ALLOC_INDEX_BITS;
- u32 offset : STACK_ALLOC_OFFSET_BITS;
- u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
- };
- };
- struct stack_record {
- struct stack_record *next;
- u32 hash;
- u32 size;
- union handle_parts handle;
- unsigned long entries[1];
- };
- static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
- static int depot_index;
- static int next_slab_inited;
- static size_t depot_offset;
- static DEFINE_SPINLOCK(depot_lock);
- static bool init_stack_slab(void **prealloc)
- {
- if (!*prealloc)
- return false;
-
- if (smp_load_acquire(&next_slab_inited))
- return true;
- if (stack_slabs[depot_index] == NULL) {
- stack_slabs[depot_index] = *prealloc;
- } else {
- stack_slabs[depot_index + 1] = *prealloc;
-
- smp_store_release(&next_slab_inited, 1);
- }
- *prealloc = NULL;
- return true;
- }
- static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
- u32 hash, void **prealloc, gfp_t alloc_flags)
- {
- int required_size = offsetof(struct stack_record, entries) +
- sizeof(unsigned long) * size;
- struct stack_record *stack;
- required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
- if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
- if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
- WARN_ONCE(1, "Stack depot reached limit capacity");
- return NULL;
- }
- depot_index++;
- depot_offset = 0;
-
- if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
- smp_store_release(&next_slab_inited, 0);
- }
- init_stack_slab(prealloc);
- if (stack_slabs[depot_index] == NULL)
- return NULL;
- stack = stack_slabs[depot_index] + depot_offset;
- stack->hash = hash;
- stack->size = size;
- stack->handle.slabindex = depot_index;
- stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
- stack->handle.valid = 1;
- memcpy(stack->entries, entries, size * sizeof(unsigned long));
- depot_offset += required_size;
- return stack;
- }
- #define STACK_HASH_ORDER 20
- #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
- #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
- #define STACK_HASH_SEED 0x9747b28c
- static struct stack_record *stack_table[STACK_HASH_SIZE] = {
- [0 ... STACK_HASH_SIZE - 1] = NULL
- };
- static inline u32 hash_stack(unsigned long *entries, unsigned int size)
- {
- return jhash2((u32 *)entries,
- size * sizeof(unsigned long) / sizeof(u32),
- STACK_HASH_SEED);
- }
- static inline struct stack_record *find_stack(struct stack_record *bucket,
- unsigned long *entries, int size,
- u32 hash)
- {
- struct stack_record *found;
- for (found = bucket; found; found = found->next) {
- if (found->hash == hash &&
- found->size == size &&
- !memcmp(entries, found->entries,
- size * sizeof(unsigned long))) {
- return found;
- }
- }
- return NULL;
- }
- void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
- {
- union handle_parts parts = { .handle = handle };
- void *slab = stack_slabs[parts.slabindex];
- size_t offset = parts.offset << STACK_ALLOC_ALIGN;
- struct stack_record *stack = slab + offset;
- trace->nr_entries = trace->max_entries = stack->size;
- trace->entries = stack->entries;
- trace->skip = 0;
- }
- EXPORT_SYMBOL_GPL(depot_fetch_stack);
- depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
- gfp_t alloc_flags)
- {
- u32 hash;
- depot_stack_handle_t retval = 0;
- struct stack_record *found = NULL, **bucket;
- unsigned long flags;
- struct page *page = NULL;
- void *prealloc = NULL;
- if (unlikely(trace->nr_entries == 0))
- goto fast_exit;
- hash = hash_stack(trace->entries, trace->nr_entries);
- bucket = &stack_table[hash & STACK_HASH_MASK];
-
- found = find_stack(smp_load_acquire(bucket), trace->entries,
- trace->nr_entries, hash);
- if (found)
- goto exit;
-
- if (unlikely(!smp_load_acquire(&next_slab_inited))) {
-
- alloc_flags &= ~GFP_ZONEMASK;
- alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
- alloc_flags |= __GFP_NOWARN;
- page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
- if (page)
- prealloc = page_address(page);
- }
- spin_lock_irqsave(&depot_lock, flags);
- found = find_stack(*bucket, trace->entries, trace->nr_entries, hash);
- if (!found) {
- struct stack_record *new =
- depot_alloc_stack(trace->entries, trace->nr_entries,
- hash, &prealloc, alloc_flags);
- if (new) {
- new->next = *bucket;
-
- smp_store_release(bucket, new);
- found = new;
- }
- } else if (prealloc) {
-
- WARN_ON(!init_stack_slab(&prealloc));
- }
- spin_unlock_irqrestore(&depot_lock, flags);
- exit:
- if (prealloc) {
-
- free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
- }
- if (found)
- retval = found->handle.handle;
- fast_exit:
- return retval;
- }
- EXPORT_SYMBOL_GPL(depot_save_stack);
|