Back to home page

LXR

 
 

    


0001 /*
0002  * Generic stack depot for storing stack traces.
0003  *
0004  * Some debugging tools need to save stack traces of certain events which can
0005  * be later presented to the user. For example, KASAN needs to safe alloc and
0006  * free stacks for each object, but storing two stack traces per object
0007  * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
0008  * that).
0009  *
0010  * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
0011  * and free stacks repeat a lot, we save about 100x space.
0012  * Stacks are never removed from depot, so we store them contiguously one after
0013  * another in a contiguos memory allocation.
0014  *
0015  * Author: Alexander Potapenko <glider@google.com>
0016  * Copyright (C) 2016 Google, Inc.
0017  *
0018  * Based on code by Dmitry Chernenkov.
0019  *
0020  * This program is free software; you can redistribute it and/or
0021  * modify it under the terms of the GNU General Public License
0022  * version 2 as published by the Free Software Foundation.
0023  *
0024  * This program is distributed in the hope that it will be useful, but
0025  * WITHOUT ANY WARRANTY; without even the implied warranty of
0026  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027  * General Public License for more details.
0028  *
0029  */
0030 
0031 #include <linux/gfp.h>
0032 #include <linux/jhash.h>
0033 #include <linux/kernel.h>
0034 #include <linux/mm.h>
0035 #include <linux/percpu.h>
0036 #include <linux/printk.h>
0037 #include <linux/slab.h>
0038 #include <linux/stacktrace.h>
0039 #include <linux/stackdepot.h>
0040 #include <linux/string.h>
0041 #include <linux/types.h>
0042 
0043 #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8)
0044 
0045 #define STACK_ALLOC_NULL_PROTECTION_BITS 1
0046 #define STACK_ALLOC_ORDER 2 /* 'Slab' size order for stack depot, 4 pages */
0047 #define STACK_ALLOC_SIZE (1LL << (PAGE_SHIFT + STACK_ALLOC_ORDER))
0048 #define STACK_ALLOC_ALIGN 4
0049 #define STACK_ALLOC_OFFSET_BITS (STACK_ALLOC_ORDER + PAGE_SHIFT - \
0050                     STACK_ALLOC_ALIGN)
0051 #define STACK_ALLOC_INDEX_BITS (DEPOT_STACK_BITS - \
0052         STACK_ALLOC_NULL_PROTECTION_BITS - STACK_ALLOC_OFFSET_BITS)
0053 #define STACK_ALLOC_SLABS_CAP 8192
0054 #define STACK_ALLOC_MAX_SLABS \
0055     (((1LL << (STACK_ALLOC_INDEX_BITS)) < STACK_ALLOC_SLABS_CAP) ? \
0056      (1LL << (STACK_ALLOC_INDEX_BITS)) : STACK_ALLOC_SLABS_CAP)
0057 
0058 /* The compact structure to store the reference to stacks. */
0059 union handle_parts {
0060     depot_stack_handle_t handle;
0061     struct {
0062         u32 slabindex : STACK_ALLOC_INDEX_BITS;
0063         u32 offset : STACK_ALLOC_OFFSET_BITS;
0064         u32 valid : STACK_ALLOC_NULL_PROTECTION_BITS;
0065     };
0066 };
0067 
0068 struct stack_record {
0069     struct stack_record *next;  /* Link in the hashtable */
0070     u32 hash;           /* Hash in the hastable */
0071     u32 size;           /* Number of frames in the stack */
0072     union handle_parts handle;
0073     unsigned long entries[1];   /* Variable-sized array of entries. */
0074 };
0075 
0076 static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
0077 
0078 static int depot_index;
0079 static int next_slab_inited;
0080 static size_t depot_offset;
0081 static DEFINE_SPINLOCK(depot_lock);
0082 
0083 static bool init_stack_slab(void **prealloc)
0084 {
0085     if (!*prealloc)
0086         return false;
0087     /*
0088      * This smp_load_acquire() pairs with smp_store_release() to
0089      * |next_slab_inited| below and in depot_alloc_stack().
0090      */
0091     if (smp_load_acquire(&next_slab_inited))
0092         return true;
0093     if (stack_slabs[depot_index] == NULL) {
0094         stack_slabs[depot_index] = *prealloc;
0095     } else {
0096         stack_slabs[depot_index + 1] = *prealloc;
0097         /*
0098          * This smp_store_release pairs with smp_load_acquire() from
0099          * |next_slab_inited| above and in depot_save_stack().
0100          */
0101         smp_store_release(&next_slab_inited, 1);
0102     }
0103     *prealloc = NULL;
0104     return true;
0105 }
0106 
0107 /* Allocation of a new stack in raw storage */
0108 static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
0109         u32 hash, void **prealloc, gfp_t alloc_flags)
0110 {
0111     int required_size = offsetof(struct stack_record, entries) +
0112         sizeof(unsigned long) * size;
0113     struct stack_record *stack;
0114 
0115     required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
0116 
0117     if (unlikely(depot_offset + required_size > STACK_ALLOC_SIZE)) {
0118         if (unlikely(depot_index + 1 >= STACK_ALLOC_MAX_SLABS)) {
0119             WARN_ONCE(1, "Stack depot reached limit capacity");
0120             return NULL;
0121         }
0122         depot_index++;
0123         depot_offset = 0;
0124         /*
0125          * smp_store_release() here pairs with smp_load_acquire() from
0126          * |next_slab_inited| in depot_save_stack() and
0127          * init_stack_slab().
0128          */
0129         if (depot_index + 1 < STACK_ALLOC_MAX_SLABS)
0130             smp_store_release(&next_slab_inited, 0);
0131     }
0132     init_stack_slab(prealloc);
0133     if (stack_slabs[depot_index] == NULL)
0134         return NULL;
0135 
0136     stack = stack_slabs[depot_index] + depot_offset;
0137 
0138     stack->hash = hash;
0139     stack->size = size;
0140     stack->handle.slabindex = depot_index;
0141     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
0142     stack->handle.valid = 1;
0143     memcpy(stack->entries, entries, size * sizeof(unsigned long));
0144     depot_offset += required_size;
0145 
0146     return stack;
0147 }
0148 
0149 #define STACK_HASH_ORDER 20
0150 #define STACK_HASH_SIZE (1L << STACK_HASH_ORDER)
0151 #define STACK_HASH_MASK (STACK_HASH_SIZE - 1)
0152 #define STACK_HASH_SEED 0x9747b28c
0153 
0154 static struct stack_record *stack_table[STACK_HASH_SIZE] = {
0155     [0 ...  STACK_HASH_SIZE - 1] = NULL
0156 };
0157 
0158 /* Calculate hash for a stack */
0159 static inline u32 hash_stack(unsigned long *entries, unsigned int size)
0160 {
0161     return jhash2((u32 *)entries,
0162                    size * sizeof(unsigned long) / sizeof(u32),
0163                    STACK_HASH_SEED);
0164 }
0165 
0166 /* Find a stack that is equal to the one stored in entries in the hash */
0167 static inline struct stack_record *find_stack(struct stack_record *bucket,
0168                          unsigned long *entries, int size,
0169                          u32 hash)
0170 {
0171     struct stack_record *found;
0172 
0173     for (found = bucket; found; found = found->next) {
0174         if (found->hash == hash &&
0175             found->size == size &&
0176             !memcmp(entries, found->entries,
0177                 size * sizeof(unsigned long))) {
0178             return found;
0179         }
0180     }
0181     return NULL;
0182 }
0183 
0184 void depot_fetch_stack(depot_stack_handle_t handle, struct stack_trace *trace)
0185 {
0186     union handle_parts parts = { .handle = handle };
0187     void *slab = stack_slabs[parts.slabindex];
0188     size_t offset = parts.offset << STACK_ALLOC_ALIGN;
0189     struct stack_record *stack = slab + offset;
0190 
0191     trace->nr_entries = trace->max_entries = stack->size;
0192     trace->entries = stack->entries;
0193     trace->skip = 0;
0194 }
0195 EXPORT_SYMBOL_GPL(depot_fetch_stack);
0196 
0197 /**
0198  * depot_save_stack - save stack in a stack depot.
0199  * @trace - the stacktrace to save.
0200  * @alloc_flags - flags for allocating additional memory if required.
0201  *
0202  * Returns the handle of the stack struct stored in depot.
0203  */
0204 depot_stack_handle_t depot_save_stack(struct stack_trace *trace,
0205                     gfp_t alloc_flags)
0206 {
0207     u32 hash;
0208     depot_stack_handle_t retval = 0;
0209     struct stack_record *found = NULL, **bucket;
0210     unsigned long flags;
0211     struct page *page = NULL;
0212     void *prealloc = NULL;
0213 
0214     if (unlikely(trace->nr_entries == 0))
0215         goto fast_exit;
0216 
0217     hash = hash_stack(trace->entries, trace->nr_entries);
0218     bucket = &stack_table[hash & STACK_HASH_MASK];
0219 
0220     /*
0221      * Fast path: look the stack trace up without locking.
0222      * The smp_load_acquire() here pairs with smp_store_release() to
0223      * |bucket| below.
0224      */
0225     found = find_stack(smp_load_acquire(bucket), trace->entries,
0226                trace->nr_entries, hash);
0227     if (found)
0228         goto exit;
0229 
0230     /*
0231      * Check if the current or the next stack slab need to be initialized.
0232      * If so, allocate the memory - we won't be able to do that under the
0233      * lock.
0234      *
0235      * The smp_load_acquire() here pairs with smp_store_release() to
0236      * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
0237      */
0238     if (unlikely(!smp_load_acquire(&next_slab_inited))) {
0239         /*
0240          * Zero out zone modifiers, as we don't have specific zone
0241          * requirements. Keep the flags related to allocation in atomic
0242          * contexts and I/O.
0243          */
0244         alloc_flags &= ~GFP_ZONEMASK;
0245         alloc_flags &= (GFP_ATOMIC | GFP_KERNEL);
0246         alloc_flags |= __GFP_NOWARN;
0247         page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);
0248         if (page)
0249             prealloc = page_address(page);
0250     }
0251 
0252     spin_lock_irqsave(&depot_lock, flags);
0253 
0254     found = find_stack(*bucket, trace->entries, trace->nr_entries, hash);
0255     if (!found) {
0256         struct stack_record *new =
0257             depot_alloc_stack(trace->entries, trace->nr_entries,
0258                       hash, &prealloc, alloc_flags);
0259         if (new) {
0260             new->next = *bucket;
0261             /*
0262              * This smp_store_release() pairs with
0263              * smp_load_acquire() from |bucket| above.
0264              */
0265             smp_store_release(bucket, new);
0266             found = new;
0267         }
0268     } else if (prealloc) {
0269         /*
0270          * We didn't need to store this stack trace, but let's keep
0271          * the preallocated memory for the future.
0272          */
0273         WARN_ON(!init_stack_slab(&prealloc));
0274     }
0275 
0276     spin_unlock_irqrestore(&depot_lock, flags);
0277 exit:
0278     if (prealloc) {
0279         /* Nobody used this memory, ok to free it. */
0280         free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER);
0281     }
0282     if (found)
0283         retval = found->handle.handle;
0284 fast_exit:
0285     return retval;
0286 }
0287 EXPORT_SYMBOL_GPL(depot_save_stack);