/* Use one hash table bucket per 16 KB of memory. */ #define STACK_HASH_TABLE_SCALE 14 /* Limit the number of buckets between 4K and 1M. */ #define STACK_BUCKET_NUMBER_ORDER_MIN 12 #define STACK_BUCKET_NUMBER_ORDER_MAX 20 /* Initial seed for jhash2. */ #define STACK_HASH_SEED 0x9747b28c
/* Hash table of stored stack records. */ staticstruct list_head *stack_table; /* Fixed order of the number of table buckets. Used when KASAN is enabled. */ staticunsignedint stack_bucket_number_order; /* Hash mask for indexing the table. */ staticunsignedint stack_hash_mask;
/* Array of memory regions that store stack records. */ staticvoid **stack_pools; /* Newly allocated pool that is not yet added to stack_pools. */ staticvoid *new_pool; /* Number of pools in stack_pools. */ staticint pools_num; /* Offset to the unused space in the currently used pool. */ static size_t pool_offset = DEPOT_POOL_SIZE; /* Freelist of stack records within stack_pools. */ static LIST_HEAD(free_stacks); /* The lock must be held when performing pool or freelist modifications. */ static DEFINE_RAW_SPINLOCK(pool_lock);
void __init stack_depot_request_early_init(void)
{ /* Too late to request early init now. */
WARN_ON(__stack_depot_early_init_passed);
__stack_depot_early_init_requested = true;
}
/* Initialize list_head's within the hash table. */ staticvoid init_stack_table(unsignedlong entries)
{ unsignedlong i;
for (i = 0; i < entries; i++)
INIT_LIST_HEAD(&stack_table[i]);
}
/* Allocates a hash table via memblock. Can only be used during early boot. */ int __init stack_depot_early_init(void)
{ unsignedlong entries = 0;
/* This function must be called only once, from mm_init(). */ if (WARN_ON(__stack_depot_early_init_passed)) return0;
__stack_depot_early_init_passed = true;
/* *Printdisabledmessageevenifearlyinithasnotbeenrequested: *stack_depot_init()willnotprintone.
*/ if (stack_depot_disabled) {
pr_info("disabled\n"); return0;
}
pr_info("allocating space for %u stack pools via memblock\n",
stack_max_pools);
stack_pools =
memblock_alloc(stack_max_pools * sizeof(void *), PAGE_SIZE); if (!stack_pools) {
pr_err("stack pools allocation failed, disabling\n");
memblock_free(stack_table, entries * sizeof(struct list_head));
stack_depot_disabled = true; return -ENOMEM;
}
return0;
}
/* Allocates a hash table via kvcalloc. Can be used after boot. */ int stack_depot_init(void)
{ static DEFINE_MUTEX(stack_depot_init_mutex); unsignedlong entries; int ret = 0;
mutex_lock(&stack_depot_init_mutex);
if (stack_depot_disabled || stack_table) goto out_unlock;
/* *Similarlytostack_depot_early_init,usestack_bucket_number_order *ifassigned,andrelyonautomaticscalingotherwise.
*/ if (stack_bucket_number_order) {
entries = 1UL << stack_bucket_number_order;
} else { int scale = STACK_HASH_TABLE_SCALE;
if (unlikely(pools_num >= stack_max_pools)) { /* Bail out if we reached the pool limit. */
WARN_ON_ONCE(pools_num > stack_max_pools); /* should never happen */
WARN_ON_ONCE(!new_pool); /* to avoid unnecessary pre-allocation */
WARN_ONCE(1, "Stack depot reached limit capacity"); returnfalse;
}
if (!new_pool && *prealloc) { /* We have preallocated memory, use it. */
WRITE_ONCE(new_pool, *prealloc);
*prealloc = NULL;
}
if (!new_pool) returnfalse; /* new_pool and *prealloc are NULL */
/* Save reference to the pool to be used by depot_fetch_stack(). */
stack_pools[pools_num] = new_pool;
/* Pairs with concurrent READ_ONCE() in depot_fetch_stack(). */
WRITE_ONCE(pools_num, pools_num + 1);
ASSERT_EXCLUSIVE_WRITER(pools_num);
pool_offset = 0;
returntrue;
}
/* Keeps the preallocated memory to be used for a new stack depot pool. */ staticvoid depot_keep_new_pool(void **prealloc)
{
lockdep_assert_held(&pool_lock);
/* *Ifanewpoolisalreadysavedorthemaximumnumberof *poolsisreached,donotusethepreallocatedmemory.
*/ if (new_pool) return;
/* Allocates a new stack in a stack depot pool. */ staticstruct stack_record *
depot_alloc_stack(unsignedlong *entries, unsignedint nr_entries, u32 hash, depot_flags_t flags, void **prealloc)
{ struct stack_record *stack = NULL;
size_t record_size;
lockdep_assert_held(&pool_lock);
/* This should already be checked by public API entry points. */ if (WARN_ON_ONCE(!nr_entries)) return NULL;
/* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ if (nr_entries > CONFIG_STACKDEPOT_MAX_FRAMES)
nr_entries = CONFIG_STACKDEPOT_MAX_FRAMES;
if (pool_index >= pools_num_cached) {
WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n",
pool_index, pools_num_cached, handle); return NULL;
}
pool = stack_pools[pool_index]; if (WARN_ON(!pool)) return NULL;
stack = pool + offset; if (WARN_ON(!refcount_read(&stack->count))) return NULL;
return stack;
}
/* Links stack into the freelist. */ staticvoid depot_free_stack(struct stack_record *stack)
{ unsignedlong flags;
/* Fast path: look the stack trace up without locking. */
found = find_stack(bucket, entries, nr_entries, hash, depot_flags); if (found) gotoexit;
/* *Allocatememoryforanewpoolifrequirednow: *wewon'tbeabletodothatunderthelock.
*/ if (unlikely(can_alloc && !READ_ONCE(new_pool))) {
page = alloc_pages(gfp_nested_mask(alloc_flags),
DEPOT_POOL_ORDER); if (page)
prealloc = page_address(page);
}
if (in_nmi() || !allow_spin) { /* We can never allocate in NMI context. */
WARN_ON_ONCE(can_alloc); /* Best effort; bail if we fail to take the lock. */ if (!raw_spin_trylock_irqsave(&pool_lock, flags)) gotoexit;
} else {
raw_spin_lock_irqsave(&pool_lock, flags);
}
printk_deferred_enter();
/* Try to find again, to avoid concurrently inserting duplicates. */
found = find_stack(bucket, entries, nr_entries, hash, depot_flags); if (!found) { struct stack_record *new =
depot_alloc_stack(entries, nr_entries, hash, depot_flags, &prealloc);
if (new) { /* *Thisreleasesthestackrecordintothebucketand *makesitvisibletoreadersinfind_stack().
*/
list_add_rcu(&new->hash_list, bucket);
found = new;
}
}
if (prealloc) { /* *Eitherstackdepotalreadycontainsthisstacktrace,or *depot_alloc_stack()didnotconsumethepreallocatedmemory. *Trytokeepthepreallocatedmemoryforfuture.
*/
depot_keep_new_pool(&prealloc);
}
printk_deferred_exit();
raw_spin_unlock_irqrestore(&pool_lock, flags); exit: if (prealloc) { /* Stack depot didn't use this memory, free it. */ if (!allow_spin)
free_pages_nolock(virt_to_page(prealloc), DEPOT_POOL_ORDER); else
free_pages((unsignedlong)prealloc, DEPOT_POOL_ORDER);
} if (found)
handle = found->handle.handle; return handle;
}
EXPORT_SYMBOL_GPL(stack_depot_save_flags);
stack = depot_fetch_stack(handle); /* *ShouldneverbeNULL,otherwisethisisause-after-put(orjusta *corrupthandle).
*/ if (WARN(!stack, "corrupt handle or use after stack_depot_put()")) return0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.